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)
#include <fcntl.h>
#include <i915_drm.h>
#include <sys/ioctl.h>
-#include <sys/poll.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
+#include <glib.h>
#include "wayland-client.h"
+#include "wayland-glib.h"
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
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;
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) {
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);
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;
}
#include <string.h>
#include <i915_drm.h>
#include <sys/ioctl.h>
-#include <sys/poll.h>
-#include <sys/timerfd.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <i915_drm.h>
#include <sys/ioctl.h>
-#include <sys/poll.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
-#include <time.h>
#include <cairo.h>
+#include <glib.h>
#include "wayland-client.h"
+#include "wayland-glib.h"
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
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;
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) {
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;
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;
}
WaylandSource *source = (WaylandSource *) base;
wl_display_iterate(source->display,
- WL_DISPLAY_READABLE | WL_DISPLAY_WRITABLE);
+ WL_DISPLAY_READABLE);
return TRUE;
}
#include <string.h>
#include <i915_drm.h>
#include <sys/ioctl.h>
-#include <sys/poll.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <cairo.h>
+#include <glib.h>
#include <GL/gl.h>
#include <eagle.h>
#include "wayland-client.h"
+#include "wayland-glib.h"
#include "gears.h"
static const char gem_device[] = "/dev/dri/card0";
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;
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 {
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;
}
}
}
}
-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)
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) {
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;
}