diff options
Diffstat (limited to 'src/display.c')
-rw-r--r-- | src/display.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/display.c b/src/display.c new file mode 100644 index 0000000..9010c31 --- /dev/null +++ b/src/display.c @@ -0,0 +1,128 @@ + +#include <stdint.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <wayland-client.h> + +#include "Xlib-wayland.h" + +#include "private.h" + +void +csx_display_bad_alloc(struct csx_display *display) +{ + fprintf(stderr, "error bad alloc\n"); + exit(1); +} + +static void +shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) +{ + struct csx_display *display = data; + + display->shm_formats |= (1 << format); +} + +struct wl_shm_listener shm_listenter = { + shm_format +}; + +static void +registry_handle_global(void *data, struct wl_registry *registry, + uint32_t id, const char *interface, uint32_t version) +{ + struct csx_display *display = data; + + if (strcmp(interface, "wl_compositor") == 0) { + display->compositor = + wl_registry_bind(registry, + id, &wl_compositor_interface, 1); + } else if (strcmp(interface, "wl_shell") == 0) { + display->shell = wl_registry_bind(registry, + id, &wl_shell_interface, 1); + } else if (strcmp(interface, "wl_shm") == 0) { + display->shm = wl_registry_bind(registry, + id, &wl_shm_interface, 1); + wl_shm_add_listener(display->shm, &shm_listenter, display); + } +} + +static void +registry_handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) +{ +} + +static const struct wl_registry_listener registry_listener = { + registry_handle_global, + registry_handle_global_remove +}; + +WL_EXPORT Display * +XOpenDisplay(const char *display_name) +{ + Display *xdisplay; + struct csx_display *display; + + xdisplay = malloc(sizeof *xdisplay + sizeof *display); + if (!xdisplay) + return NULL; + + memset(xdisplay, 0, sizeof *xdisplay); + display = xdisplay->csx_display = (void *) (xdisplay + 1); + display->display = wl_display_connect(display_name); + if (!display->display) { + free(display); + return NULL; + } + + display->registry = wl_display_get_registry(display->display); + wl_registry_add_listener(display->registry, + ®istry_listener, display); + wl_display_roundtrip(display->display); + if (display->shm == NULL) { + fprintf(stderr, "No wl_shm global\n"); + exit(1); + } + + wl_display_roundtrip(display->display); + + if (display->shm_formats == 0) { + fprintf(stderr, "no shm formats available\n"); + exit(1); + } + + csx_display_add_resource(display, NULL); /* None resource */ + display->root = csx_window_create(display, NULL); + + fprintf(stderr, "root window is id %d\n", display->root->id); + + return xdisplay; +} + +WL_EXPORT int +XConnectionNumber(Display *display) +{ + return wl_display_get_fd(display->csx_display->display); +} + +WL_EXPORT int +XCloseDisplay(Display *xdisplay) +{ + struct csx_display *display = xdisplay->csx_display; + + if (display->shm) + wl_shm_destroy(display->shm); + + if (display->shell) + wl_shell_destroy(display->shell); + + if (display->compositor) + wl_compositor_destroy(display->compositor); + + wl_registry_destroy(display->registry); + + wl_display_disconnect(display->display); + free(xdisplay); +} |