From 2f17e8f82ab0f7725d3cf2253a3cc81980456c7a Mon Sep 17 00:00:00 2001 From: James Magahern Date: Fri, 18 Jan 2019 11:47:51 -0800 Subject: Started to split x11 stuff to separate files --- .gitignore | 1 + meson.build | 3 ++- src/main.c | 55 ++++++++------------------------------------------ src/x11_support.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/x11_support.h | 20 +++++++++++++++++++ 5 files changed, 91 insertions(+), 48 deletions(-) create mode 100644 src/x11_support.c create mode 100644 src/x11_support.h diff --git a/.gitignore b/.gitignore index 9c74d99..2790582 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build/ buzzsaver +tags diff --git a/meson.build b/meson.build index 023a066..4849f05 100644 --- a/meson.build +++ b/meson.build @@ -2,7 +2,8 @@ project('buzzlocker', 'c') sources = [ 'src/main.c', - 'src/render.c' + 'src/render.c', + 'src/x11_support.c', ] dependencies = [ diff --git a/src/main.c b/src/main.c index 395e6ff..6729a7f 100644 --- a/src/main.c +++ b/src/main.c @@ -5,17 +5,13 @@ */ #include "render.h" +#include "x11_support.h" #include #include #include #include #include -#include -#include - -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; } diff --git a/src/x11_support.c b/src/x11_support.c new file mode 100644 index 0000000..87dd1ac --- /dev/null +++ b/src/x11_support.c @@ -0,0 +1,60 @@ +/* + * x11_support.c + * + * Created by buzzert 2019-01-18 + */ + +#include "x11_support.h" + +#include +#include + +static Window __window = { 0 }; +static Display *__display = NULL; + +cairo_surface_t* x11_helper_acquire_cairo_surface(int width, int height) +{ + __display = XOpenDisplay(NULL); + if (__display == NULL) { + fprintf(stderr, "Error opening display\n"); + return NULL; + } + + Window root_window = DefaultRootWindow(__display); + __window = XCreateSimpleWindow( + __display, // display + root_window, // parent window + 0, 0, // x, y + width, // width + 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, + width, + height + ); + + return surface; +} + +void x11_helper_destroy_surface(cairo_surface_t *surface) +{ + cairo_surface_destroy(surface); + XCloseDisplay(__display); +} + diff --git a/src/x11_support.h b/src/x11_support.h new file mode 100644 index 0000000..2c24c98 --- /dev/null +++ b/src/x11_support.h @@ -0,0 +1,20 @@ +/* + * x11_support.h + * + * Relevant helper functions for acquiring a drawing surface on X11 + * Created by buzzert 2019-01-18 + */ + +#pragma once + +#include +#include +#include +#include + +// Sets up a window and returns a cairo_surface to draw onto +cairo_surface_t* x11_helper_acquire_cairo_surface(int width, int height); + +// Cleanup +void x11_helper_destroy_surface(cairo_surface_t *surface); + -- cgit v1.2.3