diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 55 |
1 files changed, 8 insertions, 47 deletions
@@ -5,17 +5,13 @@ */ #include "render.h" +#include "x11_support.h" #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> -#include <X11/Xlib.h> -#include <X11/Xutil.h> - -static Window __window = { 0 }; -static Display *__display = NULL; static const size_t kMaxPasswordLength = 128; @@ -50,18 +46,19 @@ void handle_key_event(saver_state_t *state, XKeyEvent *event) int poll_events(saver_state_t *state) { - const bool block_for_next_event = false; XEvent e; + const bool block_for_next_event = false; + // Temp: this should be handled by x11_support + Display *display = cairo_xlib_surface_get_display(state->surface); for (;;) { - if (block_for_next_event || XPending(__display)) { + if (block_for_next_event || XPending(display)) { // XNextEvent blocks the caller until an event arrives - XNextEvent(__display, &e); + XNextEvent(display, &e); } else { return 0; } - // TODO: listen for window resize events and resize cairo surface switch (e.type) { case ConfigureNotify: window_changed_size(state, (XConfigureEvent *)&e); @@ -159,41 +156,7 @@ int main(int argc, char **argv) int default_width = 800; int default_height = 600; - __display = XOpenDisplay(NULL); - if (__display == NULL) { - fprintf(stderr, "Error opening display\n"); - exit(1); - } - - Window root_window = DefaultRootWindow(__display); - __window = XCreateSimpleWindow( - __display, // display - root_window, // parent window - 0, 0, // x, y - default_width, // width - default_height, // height - 0, // border_width - 0, // border - 0 // background - ); - - // Enable key events - XSelectInput(__display, __window, ButtonPressMask | KeyPressMask | StructureNotifyMask); - - // Map window to display - XMapWindow(__display, __window); - - // Create cairo surface - int screen = DefaultScreen(__display); - Visual *visual = DefaultVisual(__display, screen); - cairo_surface_t *surface = cairo_xlib_surface_create( - __display, - __window, - visual, - default_width, - default_height - ); - + cairo_surface_t *surface = x11_helper_acquire_cairo_surface(default_width, default_height); if (surface == NULL) { fprintf(stderr, "Error creating cairo surface\n"); exit(1); @@ -204,9 +167,7 @@ int main(int argc, char **argv) int result = runloop(surface); - cairo_surface_destroy(surface); - XCloseDisplay(__display); - + x11_helper_destroy_surface(surface); return result; } |