Skip to content

Instantly share code, notes, and snippets.

@apathyboy
Created September 27, 2018 14:00
Show Gist options
  • Select an option

  • Save apathyboy/96de0cc3ebd35bb60668d9460be14d6a to your computer and use it in GitHub Desktop.

Select an option

Save apathyboy/96de0cc3ebd35bb60668d9460be14d6a to your computer and use it in GitHub Desktop.
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
int main(int argc, char **argv)
{
Display *dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
int s = DefaultScreen(dpy);
Window win = XCreateSimpleWindow(
dpy, RootWindow(dpy, s), 10, 10, 660, 200, 1, BlackPixel(dpy, s), WhitePixel(dpy, s));
XSelectInput(dpy, win, ExposureMask | KeyPressMask);
XMapWindow(dpy, win);
XStoreName(dpy, win, "Example X11 Window");
Atom WM_DELETE_WINDOW = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(dpy, win, &WM_DELETE_WINDOW, 1);
bool uname_ok = false;
struct utsname sname;
int ret = uname(&sname);
if (ret != -1) {
uname_ok = true;
}
XEvent e;
while (1) {
XNextEvent(dpy, &e);
if (e.type == Expose) {
// Draw screen?
}
if (e.type == KeyPress) {
char buf[128] = {0};
KeySym keysym;
int len = XLookupString(&e.xkey, buf, sizeof buf, &keysym, NULL);
if (keysym == XK_Escape)
break;
}
if ((e.type == ClientMessage)
&& (static_cast<unsigned int>(e.xclient.data.l[0]) == WM_DELETE_WINDOW)) {
break;
}
}
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment