Created
August 27, 2021 21:26
-
-
Save Yrds/d823539449786524351f6af53a1f12c6 to your computer and use it in GitHub Desktop.
Hello world X11 modern c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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