Use glib main loop for all clients.

dev
Kristian Høgsberg 16 years ago
parent fb59084fbf
commit 1cbaa6abac
  1. 13
      Makefile
  2. 40
      background.c
  3. 2
      flower.c
  4. 38
      pointer.c
  5. 2
      wayland-glib.c
  6. 133
      window.c

@ -43,19 +43,16 @@ libwayland.so $(compositors) :
gcc -o $@ $^ $(LDLIBS) -shared gcc -o $@ $^ $(LDLIBS) -shared
flower_objs = flower.o wayland-glib.o flower_objs = flower.o wayland-glib.o
pointer_objs = pointer.o pointer_objs = pointer.o wayland-glib.o
background_objs = background.o background_objs = background.o wayland-glib.o
window_objs = window.o gears.o window_objs = window.o gears.o wayland-glib.o
$(clients) : CFLAGS += $(shell pkg-config --cflags cairo) $(clients) : CFLAGS += $(shell pkg-config --cflags cairo glib-2.0)
$(clients) : LDLIBS += $(shell pkg-config --libs cairo) -lrt $(clients) : LDLIBS += $(shell pkg-config --libs cairo glib-2.0) -lrt
background : CFLAGS += $(shell pkg-config --cflags gdk-pixbuf-2.0) background : CFLAGS += $(shell pkg-config --cflags gdk-pixbuf-2.0)
background : LDLIBS += $(shell pkg-config --libs 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 : CFLAGS += $(EAGLE_CFLAGS)
window : LDLIBS += $(EAGLE_LDLIBS) window : LDLIBS += $(EAGLE_LDLIBS)

@ -4,10 +4,11 @@
#include <fcntl.h> #include <fcntl.h>
#include <i915_drm.h> #include <i915_drm.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/poll.h>
#include <gdk-pixbuf/gdk-pixbuf.h> #include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib.h>
#include "wayland-client.h" #include "wayland-client.h"
#include "wayland-glib.h"
static const char gem_device[] = "/dev/dri/card0"; static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland"; static const char socket_name[] = "\0wayland";
@ -93,20 +94,6 @@ static uint32_t name_pixbuf(int fd, GdkPixbuf *pixbuf)
return flink.name; 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[]) int main(int argc, char *argv[])
{ {
GdkPixbuf *image; GdkPixbuf *image;
@ -114,8 +101,9 @@ int main(int argc, char *argv[])
struct wl_display *display; struct wl_display *display;
struct wl_surface *surface; struct wl_surface *surface;
int fd, width, height, stride; int fd, width, height, stride;
uint32_t name, mask; uint32_t name;
struct pollfd p[1]; GMainLoop *loop;
GSource *source;
fd = open(gem_device, O_RDWR); fd = open(gem_device, O_RDWR);
if (fd < 0) { if (fd < 0) {
@ -128,8 +116,10 @@ int main(int argc, char *argv[])
fprintf(stderr, "failed to create display: %m\n"); fprintf(stderr, "failed to create display: %m\n");
return -1; 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); surface = wl_display_create_surface(display);
@ -142,21 +132,11 @@ int main(int argc, char *argv[])
height = gdk_pixbuf_get_height(image); height = gdk_pixbuf_get_height(image);
stride = gdk_pixbuf_get_rowstride(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_attach(surface, name, width, height, width * 4);
wl_surface_map(surface, 0, 0, width, height); wl_surface_map(surface, 0, 0, width, height);
while (1) { g_main_loop_run(loop);
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);
}
return 0; return 0;
} }

@ -4,8 +4,6 @@
#include <string.h> #include <string.h>
#include <i915_drm.h> #include <i915_drm.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/timerfd.h>
#include <time.h> #include <time.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>

@ -4,14 +4,14 @@
#include <string.h> #include <string.h>
#include <i915_drm.h> #include <i915_drm.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/poll.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <math.h> #include <math.h>
#include <time.h>
#include <cairo.h> #include <cairo.h>
#include <glib.h>
#include "wayland-client.h" #include "wayland-client.h"
#include "wayland-glib.h"
static const char gem_device[] = "/dev/dri/card0"; static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland"; static const char socket_name[] = "\0wayland";
@ -97,20 +97,6 @@ draw_pointer(int width, int height)
return surface; 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 { struct pointer {
int width, height; int width, height;
struct wl_surface *surface; struct wl_surface *surface;
@ -131,9 +117,10 @@ int main(int argc, char *argv[])
struct wl_display *display; struct wl_display *display;
struct pointer pointer; struct pointer pointer;
int fd; int fd;
uint32_t name, mask; uint32_t name;
cairo_surface_t *s; cairo_surface_t *s;
struct pollfd p[1]; GMainLoop *loop;
GSource *source;
fd = open(gem_device, O_RDWR); fd = open(gem_device, O_RDWR);
if (fd < 0) { if (fd < 0) {
@ -146,7 +133,10 @@ int main(int argc, char *argv[])
fprintf(stderr, "failed to create display: %m\n"); fprintf(stderr, "failed to create display: %m\n");
return -1; 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.width = 16;
pointer.height = 16; pointer.height = 16;
@ -162,15 +152,7 @@ int main(int argc, char *argv[])
wl_display_set_event_handler(display, event_handler, &pointer); wl_display_set_event_handler(display, event_handler, &pointer);
while (1) { g_main_loop_run(loop);
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);
}
return 0; return 0;
} }

@ -44,7 +44,7 @@ wayland_source_dispatch(GSource *base,
WaylandSource *source = (WaylandSource *) base; WaylandSource *source = (WaylandSource *) base;
wl_display_iterate(source->display, wl_display_iterate(source->display,
WL_DISPLAY_READABLE | WL_DISPLAY_WRITABLE); WL_DISPLAY_READABLE);
return TRUE; return TRUE;
} }

@ -4,17 +4,18 @@
#include <string.h> #include <string.h>
#include <i915_drm.h> #include <i915_drm.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/poll.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
#include <cairo.h> #include <cairo.h>
#include <glib.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <eagle.h> #include <eagle.h>
#include "wayland-client.h" #include "wayland-client.h"
#include "wayland-glib.h"
#include "gears.h" #include "gears.h"
static const char gem_device[] = "/dev/dri/card0"; static const char gem_device[] = "/dev/dri/card0";
@ -84,17 +85,20 @@ struct window {
int state; int state;
uint32_t name; uint32_t name;
int fd; int fd;
int need_redraw; int redraw_scheduled;
GLfloat gears_angle;
struct gears *gears;
EGLDisplay display; EGLDisplay display;
EGLContext context; EGLContext context;
EGLConfig config; EGLConfig config;
EGLSurface egl_surface; EGLSurface egl_surface;
}; };
static void * static gboolean
draw_window(struct window *window) draw_window(void *data)
{ {
struct window *window = data;
cairo_surface_t *surface; cairo_surface_t *surface;
cairo_t *cr; cairo_t *cr;
int border = 2, radius = 5, h; int border = 2, radius = 5, h;
@ -164,21 +168,12 @@ draw_window(struct window *window)
glViewport(border, window->height - h - margin - 300, 300, 300); glViewport(border, window->height - h - margin - 300, 300, 300);
return surface; if (window->gears != NULL)
} gears_draw(window->gears, window->gears_angle);
static int window->redraw_scheduled = 0;
connection_update(uint32_t mask, void *data)
{
struct pollfd *p = data;
p->events = 0; return FALSE;
if (mask & WL_DISPLAY_READABLE)
p->events |= POLLIN;
if (mask & WL_DISPLAY_WRITABLE)
p->events |= POLLOUT;
return 0;
} }
enum window_state { enum window_state {
@ -220,7 +215,10 @@ void event_handler(struct wl_display *display,
case WINDOW_RESIZING_LOWER_RIGHT: case WINDOW_RESIZING_LOWER_RIGHT:
window->width = window->drag_x + arg1; window->width = window->drag_x + arg1;
window->height = window->drag_y + arg2; 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; break;
} }
} }
@ -260,11 +258,25 @@ void event_handler(struct wl_display *display,
} }
} }
static void static struct window *
init_egl(struct window *window) window_create(struct wl_display *display, int fd)
{ {
EGLint major, minor, count; EGLint major, minor, count;
EGLConfig configs[64]; 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"); window->display = eglCreateDisplayNative("/dev/dri/card0", "i965");
if (window->display == NULL) if (window->display == NULL)
@ -282,18 +294,35 @@ init_egl(struct window *window)
die("failed to create context\n"); die("failed to create context\n");
window->egl_surface = EGL_NO_SURFACE; 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[]) int main(int argc, char *argv[])
{ {
struct wl_display *display; struct wl_display *display;
int fd, ret; int fd;
uint32_t mask; struct window *window;
cairo_surface_t *s; GMainLoop *loop;
struct pollfd p[1]; GSource *source;
struct window window;
struct gears *gears;
GLfloat angle = 0.0;
fd = open(gem_device, O_RDWR); fd = open(gem_device, O_RDWR);
if (fd < 0) { if (fd < 0) {
@ -306,42 +335,20 @@ int main(int argc, char *argv[])
fprintf(stderr, "failed to create display: %m\n"); fprintf(stderr, "failed to create display: %m\n");
return -1; 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);
window.surface = wl_display_create_surface(display); g_source_attach(source, NULL);
window.x = 200;
window.y = 200; window = window_create(display, fd);
window.width = 450;
window.height = 500; draw_window(window);
window.state = WINDOW_STABLE;
window.fd = fd; wl_display_set_event_handler(display, event_handler, window);
init_egl(&window); g_timeout_add(20, draw, window);
s = draw_window(&window); g_main_loop_run(loop);
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;
}
}
return 0; return 0;
} }

Loading…
Cancel
Save