Skip to content

Instantly share code, notes, and snippets.

@jarcode-foss
Created August 1, 2018 04:05
Show Gist options
  • Save jarcode-foss/5d599d0501b1bf42cc136b0d999821a4 to your computer and use it in GitHub Desktop.
Save jarcode-foss/5d599d0501b1bf42cc136b0d999821a4 to your computer and use it in GitHub Desktop.
Desktop window
void
create_xlib_window(painter_t *painter)
{
painter->_display = XOpenDisplay(config.output_display);
if (!painter->_display) {
fprintf(stderr, "Unable to open display: '%s'\n", XDisplayName(config.output_display));
exit(EXIT_FAILURE);
}
int screen = DefaultScreen(painter->_display);
Window root = RootWindow(painter->_display, screen);
XVisualInfo vi;
if (!XMatchVisualInfo(painter->_display, screen, 32, TrueColor, &vi)) {
fprintf(stderr, "Failed to match a TrueColor visual for transparency support.\n"
"This is usually caused by the absense of a compositor.\n");
exit(EXIT_FAILURE);
}
XSetWindowAttributes attr;
attr.colormap = XCreateColormap(painter->_display, root, vi.visual, AllocNone);
attr.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask;
attr.border_pixel = 0;
attr.background_pixmap = None;
attr.background_pixel = 0;
if (!(painter->_window = XCreateWindow(painter->_display, root,
0, 0, config.output_width, config.output_height, 0,
vi.depth, InputOutput, vi.visual,
CWColormap | CWEventMask | CWBackPixmap | CWBorderPixel,
&attr))) {
fprintf(stderr, "XCreateWindow(): failed\n");
exit(EXIT_FAILURE);
}
/* Set EWMH window type to desktop */
/*
Atom wtype = XInternAtom(painter->_display, "_NET_WM_WINDOW_TYPE", false);
Atom desktop = XInternAtom(painter->_display, "_NET_WM_WINDOW_TYPE_DESKTOP", false);
XChangeProperty(painter->_display, painter->_window, wtype,
XA_ATOM, 32, PropModeReplace, (unsigned char*) &desktop, 1);
*/
/* Assign the newly created dektop to be visible on all workspaces (EWMH) */
Atom dvis = XInternAtom(painter->_display, "_NET_WM_DESKTOP", false);
unsigned long all_desktops = 0xFFFFFFFF;
XChangeProperty(painter->_display, painter->_window, dvis,
XA_CARDINAL, 32, PropModeReplace, (unsigned char*) &all_desktops, 1);
/* Disable window decoration (motif hints, respected by most WMs) */
struct {
unsigned long flags, functions, decorations;
long input_mode;
unsigned long status;
} hints;
hints.flags = 2;
hints.decorations = 0;
Atom motif = XInternAtom(painter->_display, "_MOTIF_WM_HINTS", false);
XChangeProperty(painter->_display, painter->_window, motif, motif, 32, PropModeReplace,
(unsigned char*) &hints, sizeof(hints) / sizeof(long));
/* Allow the window to be clicked through (requries XShape support from the WM) */
int _ignored;
if (XShapeQueryExtension(painter->_display, &_ignored, &_ignored)) {
Region region;
if ((region = XCreateRegion())) {
XShapeCombineRegion(painter->_display, painter->_window, ShapeInput, 0, 0, region, ShapeSet);
XDestroyRegion(region);
}
}
/* Assign window name and class */
static char *win_name = "pscircle-display", *win_class = "pscircle";
XSetClassHint(painter->_display, painter->_window,
&((XClassHint) { .res_name = win_class, .res_class = win_class }));
XStoreName(painter->_display, painter->_window, win_name);
/* Assign window close behaviour */
Atom dwin = XInternAtom(painter->_display, "WM_DELETE_WINDOW", false);
XSetWMProtocols(painter->_display, painter->_window, &dwin, 1);
painter->_pixmap = XCreatePixmap(painter->_display, painter->_window,
config.output_width, config.output_height, 32);
painter->_surface = cairo_xlib_surface_create(painter->_display,
painter->_pixmap, vi.visual, config.output_width,
config.output_height);
painter->_gc = XCreateGC(painter->_display, painter->_pixmap, 0, 0);
cairo_surface_set_user_data(painter->_surface, NULL, NULL, NULL);
cairo_xlib_surface_set_size(painter->_surface,
config.output_width, config.output_height);
/* Make window visible */
XMapWindow(painter->_display, painter->_window);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment