Skip to content

Instantly share code, notes, and snippets.

@Yrds
Created August 27, 2021 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yrds/d823539449786524351f6af53a1f12c6 to your computer and use it in GitHub Desktop.
Save Yrds/d823539449786524351f6af53a1f12c6 to your computer and use it in GitHub Desktop.
Hello world X11 modern c++
#include <X11/Xlib.h>
#include <unistd.h>
#include <memory>
#include <functional>
int main() {
//initialize Display connection with a smart pointer
auto dpy = std::unique_ptr<Display, std::function<void(Display*)>>(
XOpenDisplay(0), XFlush);
//Create some colors
int blackColor = BlackPixel(dpy.get(), DefaultScreen(dpy.get()));
int whiteColor = WhitePixel(dpy.get(), DefaultScreen(dpy.get()));
//initialize window
Window w = XCreateSimpleWindow(dpy.get(), DefaultRootWindow(dpy.get()), 0, 0,
200, 100, 0, blackColor, blackColor);
//Call for map notify events
XSelectInput(dpy.get(), w, StructureNotifyMask);
//show window on screen
XMapWindow(dpy.get(), w);
GC gc = XCreateGC(dpy.get(), w, 0, nullptr);
//Set foreground color(for for draw lines later"
XSetForeground(dpy.get(), gc, whiteColor);
//wait for MapNotify event so we can proced with draw calls on new window
while(true) {
XEvent e;
XNextEvent(dpy.get(), &e);
if (e.type == MapNotify)
break;
}
//Draw a simple line
XDrawLine(dpy.get(), w, gc, 10, 60, 180, 20);
//Sleep 10 seconds then closes window
sleep(10);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment