From 1cbaa6abacfef875ace3d35166f8dc04f4347601 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Fri, 7 Nov 2008 15:54:48 -0500 Subject: [PATCH] Use glib main loop for all clients. --- Makefile | 13 +++--- background.c | 40 +++++------------ flower.c | 2 - pointer.c | 38 +++++------------ wayland-glib.c | 2 +- window.c | 133 ++++++++++++++++++++++++++++++--------------------------- 6 files changed, 96 insertions(+), 132 deletions(-) diff --git a/Makefile b/Makefile index eee5c5f..0205537 100644 --- a/Makefile +++ b/Makefile @@ -43,19 +43,16 @@ libwayland.so $(compositors) : gcc -o $@ $^ $(LDLIBS) -shared flower_objs = flower.o wayland-glib.o -pointer_objs = pointer.o -background_objs = background.o -window_objs = window.o gears.o +pointer_objs = pointer.o wayland-glib.o +background_objs = background.o wayland-glib.o +window_objs = window.o gears.o wayland-glib.o -$(clients) : CFLAGS += $(shell pkg-config --cflags cairo) -$(clients) : LDLIBS += $(shell pkg-config --libs cairo) -lrt +$(clients) : CFLAGS += $(shell pkg-config --cflags cairo glib-2.0) +$(clients) : LDLIBS += $(shell pkg-config --libs cairo glib-2.0) -lrt background : CFLAGS += $(shell pkg-config --cflags gdk-pixbuf-2.0) background : LDLIBS += $(shell pkg-config --libs gdk-pixbuf-2.0) -flower : CFLAGS += $(shell pkg-config --cflags glib-2.0) -flower : LDLIBS += $(shell pkg-config --libs glib-2.0) - window : CFLAGS += $(EAGLE_CFLAGS) window : LDLIBS += $(EAGLE_LDLIBS) diff --git a/background.c b/background.c index 429216d..7ce654f 100644 --- a/background.c +++ b/background.c @@ -4,10 +4,11 @@ #include #include #include -#include #include +#include #include "wayland-client.h" +#include "wayland-glib.h" static const char gem_device[] = "/dev/dri/card0"; static const char socket_name[] = "\0wayland"; @@ -93,20 +94,6 @@ static uint32_t name_pixbuf(int fd, GdkPixbuf *pixbuf) return flink.name; } -static int -connection_update(uint32_t mask, void *data) -{ - struct pollfd *p = data; - - p->events = 0; - if (mask & WL_DISPLAY_READABLE) - p->events |= POLLIN; - if (mask & WL_DISPLAY_WRITABLE) - p->events |= POLLOUT; - - return 0; -} - int main(int argc, char *argv[]) { GdkPixbuf *image; @@ -114,8 +101,9 @@ int main(int argc, char *argv[]) struct wl_display *display; struct wl_surface *surface; int fd, width, height, stride; - uint32_t name, mask; - struct pollfd p[1]; + uint32_t name; + GMainLoop *loop; + GSource *source; fd = open(gem_device, O_RDWR); if (fd < 0) { @@ -128,8 +116,10 @@ int main(int argc, char *argv[]) fprintf(stderr, "failed to create display: %m\n"); return -1; } - p[0].fd = wl_display_get_fd(display, - connection_update, &p[0]); + + loop = g_main_loop_new(NULL, FALSE); + source = wayland_source_new(display); + g_source_attach(source, NULL); surface = wl_display_create_surface(display); @@ -142,21 +132,11 @@ int main(int argc, char *argv[]) height = gdk_pixbuf_get_height(image); stride = gdk_pixbuf_get_rowstride(image); - printf("width %d, height %d\n", width, height); - wl_surface_attach(surface, name, width, height, width * 4); wl_surface_map(surface, 0, 0, width, height); - while (1) { - poll(p, 1, -1); - mask = 0; - if (p[0].revents & POLLIN) - mask |= WL_DISPLAY_READABLE; - if (p[0].revents & POLLOUT) - mask |= WL_DISPLAY_WRITABLE; - wl_display_iterate(display, mask); - } + g_main_loop_run(loop); return 0; } diff --git a/flower.c b/flower.c index f74be9d..2536c5e 100644 --- a/flower.c +++ b/flower.c @@ -4,8 +4,6 @@ #include #include #include -#include -#include #include #include #include diff --git a/pointer.c b/pointer.c index 1233f8c..293f56a 100644 --- a/pointer.c +++ b/pointer.c @@ -4,14 +4,14 @@ #include #include #include -#include #include #include #include -#include #include +#include #include "wayland-client.h" +#include "wayland-glib.h" static const char gem_device[] = "/dev/dri/card0"; static const char socket_name[] = "\0wayland"; @@ -97,20 +97,6 @@ draw_pointer(int width, int height) return surface; } -static int -connection_update(uint32_t mask, void *data) -{ - struct pollfd *p = data; - - p->events = 0; - if (mask & WL_DISPLAY_READABLE) - p->events |= POLLIN; - if (mask & WL_DISPLAY_WRITABLE) - p->events |= POLLOUT; - - return 0; -} - struct pointer { int width, height; struct wl_surface *surface; @@ -131,9 +117,10 @@ int main(int argc, char *argv[]) struct wl_display *display; struct pointer pointer; int fd; - uint32_t name, mask; + uint32_t name; cairo_surface_t *s; - struct pollfd p[1]; + GMainLoop *loop; + GSource *source; fd = open(gem_device, O_RDWR); if (fd < 0) { @@ -146,7 +133,10 @@ int main(int argc, char *argv[]) fprintf(stderr, "failed to create display: %m\n"); return -1; } - p[0].fd = wl_display_get_fd(display, connection_update, &p[0]); + + loop = g_main_loop_new(NULL, FALSE); + source = wayland_source_new(display); + g_source_attach(source, NULL); pointer.width = 16; pointer.height = 16; @@ -162,15 +152,7 @@ int main(int argc, char *argv[]) wl_display_set_event_handler(display, event_handler, &pointer); - while (1) { - poll(p, 1, -1); - mask = 0; - if (p[0].revents & POLLIN) - mask |= WL_DISPLAY_READABLE; - if (p[0].revents & POLLOUT) - mask |= WL_DISPLAY_WRITABLE; - wl_display_iterate(display, mask); - } + g_main_loop_run(loop); return 0; } diff --git a/wayland-glib.c b/wayland-glib.c index 10bcfea..567e241 100644 --- a/wayland-glib.c +++ b/wayland-glib.c @@ -44,7 +44,7 @@ wayland_source_dispatch(GSource *base, WaylandSource *source = (WaylandSource *) base; wl_display_iterate(source->display, - WL_DISPLAY_READABLE | WL_DISPLAY_WRITABLE); + WL_DISPLAY_READABLE); return TRUE; } diff --git a/window.c b/window.c index b936c58..45d0306 100644 --- a/window.c +++ b/window.c @@ -4,17 +4,18 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include #include "wayland-client.h" +#include "wayland-glib.h" #include "gears.h" static const char gem_device[] = "/dev/dri/card0"; @@ -84,17 +85,20 @@ struct window { int state; uint32_t name; int fd; - int need_redraw; + int redraw_scheduled; + GLfloat gears_angle; + struct gears *gears; EGLDisplay display; EGLContext context; EGLConfig config; EGLSurface egl_surface; }; -static void * -draw_window(struct window *window) +static gboolean +draw_window(void *data) { + struct window *window = data; cairo_surface_t *surface; cairo_t *cr; int border = 2, radius = 5, h; @@ -164,21 +168,12 @@ draw_window(struct window *window) glViewport(border, window->height - h - margin - 300, 300, 300); - return surface; -} + if (window->gears != NULL) + gears_draw(window->gears, window->gears_angle); -static int -connection_update(uint32_t mask, void *data) -{ - struct pollfd *p = data; + window->redraw_scheduled = 0; - p->events = 0; - if (mask & WL_DISPLAY_READABLE) - p->events |= POLLIN; - if (mask & WL_DISPLAY_WRITABLE) - p->events |= POLLOUT; - - return 0; + return FALSE; } enum window_state { @@ -220,7 +215,10 @@ void event_handler(struct wl_display *display, case WINDOW_RESIZING_LOWER_RIGHT: window->width = window->drag_x + arg1; window->height = window->drag_y + arg2; - window->need_redraw = 1; + if (!window->redraw_scheduled) { + window->redraw_scheduled = 1; + g_idle_add(draw_window, window); + } break; } } @@ -260,11 +258,25 @@ void event_handler(struct wl_display *display, } } -static void -init_egl(struct window *window) +static struct window * +window_create(struct wl_display *display, int fd) { EGLint major, minor, count; EGLConfig configs[64]; + struct window *window; + + window = malloc(sizeof *window); + if (window == NULL) + return NULL; + + window->surface = wl_display_create_surface(display); + window->x = 200; + window->y = 200; + window->width = 450; + window->height = 500; + window->state = WINDOW_STABLE; + window->fd = fd; + window->gears = NULL; window->display = eglCreateDisplayNative("/dev/dri/card0", "i965"); if (window->display == NULL) @@ -282,18 +294,35 @@ init_egl(struct window *window) die("failed to create context\n"); window->egl_surface = EGL_NO_SURFACE; + + draw_window(window); + + window->gears = gears_create(); + window->gears_angle = 0.0; + + return window; +} + +static gboolean +draw(gpointer data) +{ + struct window *window = data; + + gears_draw(window->gears, window->gears_angle); + wl_surface_damage(window->surface, 0, 0, + window->width, window->height); + window->gears_angle += 1; + + return TRUE; } int main(int argc, char *argv[]) { struct wl_display *display; - int fd, ret; - uint32_t mask; - cairo_surface_t *s; - struct pollfd p[1]; - struct window window; - struct gears *gears; - GLfloat angle = 0.0; + int fd; + struct window *window; + GMainLoop *loop; + GSource *source; fd = open(gem_device, O_RDWR); if (fd < 0) { @@ -306,42 +335,20 @@ int main(int argc, char *argv[]) fprintf(stderr, "failed to create display: %m\n"); return -1; } - p[0].fd = wl_display_get_fd(display, - connection_update, &p[0]); - - window.surface = wl_display_create_surface(display); - window.x = 200; - window.y = 200; - window.width = 450; - window.height = 500; - window.state = WINDOW_STABLE; - window.fd = fd; - - init_egl(&window); - - s = draw_window(&window); - - wl_display_set_event_handler(display, event_handler, &window); - - gears = gears_create(); - - while (ret = poll(p, 1, 20), ret >= 0) { - mask = 0; - gears_draw(gears, angle); - wl_surface_damage(window.surface, 0, 0, - window.width, window.height); - angle += 1; - if (p[0].revents & POLLIN) - mask |= WL_DISPLAY_READABLE; - if (p[0].revents & POLLOUT) - mask |= WL_DISPLAY_WRITABLE; - if (mask) - wl_display_iterate(display, mask); - if (window.need_redraw) { - draw_window(&window); - window.need_redraw = 0; - } - } + + loop = g_main_loop_new(NULL, FALSE); + source = wayland_source_new(display); + g_source_attach(source, NULL); + + window = window_create(display, fd); + + draw_window(window); + + wl_display_set_event_handler(display, event_handler, window); + + g_timeout_add(20, draw, window); + + g_main_loop_run(loop); return 0; } -- 2.7.4