Consolidate more code in clients/window.c

This commit is contained in:
Kristian Høgsberg 2010-06-08 14:59:44 -04:00
parent a85fe3cf45
commit 7824d81e07
7 changed files with 73 additions and 156 deletions

View File

@ -36,9 +36,6 @@
#include "wayland-glib.h" #include "wayland-glib.h"
#include "window.h" #include "window.h"
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
static void static void
set_random_color(cairo_t *cr) set_random_color(cairo_t *cr)
{ {
@ -120,29 +117,10 @@ int main(int argc, char *argv[])
int fd; int fd;
cairo_surface_t *s; cairo_surface_t *s;
struct timespec ts; struct timespec ts;
GMainLoop *loop;
GSource *source;
struct flower flower; struct flower flower;
struct display *d; struct display *d;
fd = open(gem_device, O_RDWR); d = display_create(&argc, &argv, NULL);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
loop = g_main_loop_new(NULL, FALSE);
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
d = display_create(display, fd);
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
flower.x = 512; flower.x = 512;
flower.y = 384; flower.y = 384;
@ -155,6 +133,7 @@ int main(int argc, char *argv[])
srandom(ts.tv_nsec); srandom(ts.tv_nsec);
flower.offset = random(); flower.offset = random();
window_set_decoration(flower.window, 0);
window_draw(flower.window); window_draw(flower.window);
s = window_get_surface(flower.window); s = window_get_surface(flower.window);
if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) { if (s == NULL || cairo_surface_status (s) != CAIRO_STATUS_SUCCESS) {
@ -168,7 +147,7 @@ int main(int argc, char *argv[])
window_set_frame_handler(flower.window, handle_frame, &flower); window_set_frame_handler(flower.window, handle_frame, &flower);
window_commit(flower.window, 0); window_commit(flower.window, 0);
g_main_loop_run(loop); display_run(d);
return 0; return 0;
} }

View File

@ -353,9 +353,8 @@ static const struct wl_compositor_listener compositor_listener = {
}; };
static struct gears * static struct gears *
gears_create(struct display *display, int drm_fd) gears_create(struct display *display)
{ {
PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
const int x = 200, y = 200, width = 450, height = 500; const int x = 200, y = 200, width = 450, height = 500;
EGLint major, minor, count; EGLint major, minor, count;
EGLConfig config; EGLConfig config;
@ -369,19 +368,13 @@ gears_create(struct display *display, int drm_fd)
EGL_NONE EGL_NONE
}; };
get_typed_display_mesa =
(PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
if (get_typed_display_mesa == NULL)
die("eglGetDisplayMESA() not found\n");
gears = malloc(sizeof *gears); gears = malloc(sizeof *gears);
memset(gears, 0, sizeof *gears); memset(gears, 0, sizeof *gears);
gears->d = display; gears->d = display;
gears->window = window_create(display, "Wayland Gears", gears->window = window_create(display, "Wayland Gears",
x, y, width, height); x, y, width, height);
gears->display = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA, gears->display = display_get_egl_display(gears->d);
(void *) drm_fd);
if (gears->display == NULL) if (gears->display == NULL)
die("failed to create egl display\n"); die("failed to create egl display\n");
@ -458,34 +451,12 @@ gears_create(struct display *display, int drm_fd)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct wl_display *display;
struct display *d; struct display *d;
int fd;
GMainLoop *loop;
GSource *source;
struct gears *gears; struct gears *gears;
fd = open(gem_device, O_RDWR); d = display_create(&argc, &argv, NULL);
if (fd < 0) { gears = gears_create(d);
fprintf(stderr, "drm open failed: %m\n"); display_run(d);
return -1;
}
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
d = display_create(display, fd);
loop = g_main_loop_new(NULL, FALSE);
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
gears = gears_create(d, fd);
g_main_loop_run(loop);
return 0; return 0;
} }

View File

@ -299,40 +299,10 @@ static const GOptionEntry option_entries[] = {
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct wl_display *display;
int fd;
GMainLoop *loop;
GSource *source;
struct display *d; struct display *d;
GOptionContext *context;
GError *error;
int i; int i;
context = g_option_context_new(NULL); d = display_create(&argc, &argv, option_entries);
g_option_context_add_main_entries(context, option_entries, "Wayland Image");
if (!g_option_context_parse(context, &argc, &argv, &error)) {
fprintf(stderr, "option parsing failed: %s\n", error->message);
exit(EXIT_FAILURE);
}
fd = open(gem_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
d = display_create(display, fd);
loop = g_main_loop_new(NULL, FALSE);
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
g_type_init ();
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
struct image *image; struct image *image;
@ -340,7 +310,7 @@ main(int argc, char *argv[])
image = image_create (d, i, argv[i]); image = image_create (d, i, argv[i]);
} }
g_main_loop_run(loop); display_run(d);
return 0; return 0;
} }

View File

@ -42,8 +42,6 @@
#include "window.h" #include "window.h"
static int option_fullscreen; static int option_fullscreen;
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
#define MOD_SHIFT 0x01 #define MOD_SHIFT 0x01
#define MOD_ALT 0x02 #define MOD_ALT 0x02
@ -564,44 +562,16 @@ static const GOptionEntry option_entries[] = {
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct wl_display *display;
int fd;
GMainLoop *loop;
GSource *source;
struct display *d; struct display *d;
struct terminal *terminal; struct terminal *terminal;
GOptionContext *context;
GError *error;
context = g_option_context_new(NULL); d = display_create(&argc, &argv, option_entries);
g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
if (!g_option_context_parse(context, &argc, &argv, &error)) {
fprintf(stderr, "option parsing failed: %s\n", error->message);
exit(EXIT_FAILURE);
}
fd = open(gem_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
d = display_create(display, fd);
loop = g_main_loop_new(NULL, FALSE);
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
terminal = terminal_create(d, option_fullscreen); terminal = terminal_create(d, option_fullscreen);
if (terminal_run(terminal, "/bin/bash")) if (terminal_run(terminal, "/bin/bash"))
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
g_main_loop_run(loop); display_run(d);
return 0; return 0;
} }

View File

@ -266,40 +266,10 @@ static const GOptionEntry option_entries[] = {
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct wl_display *display;
int fd;
GMainLoop *loop;
GSource *source;
struct display *d; struct display *d;
GOptionContext *context;
GError *error = NULL;
int i; int i;
context = g_option_context_new(NULL); d = display_create(&argc, &argv, option_entries);
g_option_context_add_main_entries(context, option_entries, "Wayland View");
if (!g_option_context_parse(context, &argc, &argv, &error)) {
fprintf(stderr, "option parsing failed: %s\n", error->message);
exit(EXIT_FAILURE);
}
fd = open(gem_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return -1;
}
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
d = display_create(display, fd);
loop = g_main_loop_new(NULL, FALSE);
source = wl_glib_source_new(display);
g_source_attach(source, NULL);
g_type_init ();
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
struct view *view; struct view *view;
@ -307,7 +277,7 @@ main(int argc, char *argv[])
view = view_create (d, i, argv[i]); view = view_create (d, i, argv[i]);
} }
g_main_loop_run(loop); display_run(d);
return 0; return 0;
} }

View File

@ -56,6 +56,8 @@ struct display {
EGLContext ctx; EGLContext ctx;
cairo_device_t *device; cairo_device_t *device;
int fd; int fd;
GMainLoop *loop;
GSource *source;
}; };
struct window { struct window {
@ -756,6 +758,7 @@ window_create(struct display *display, const char *title,
window->saved_allocation = window->allocation; window->saved_allocation = window->allocation;
window->margin = 16; window->margin = 16;
window->state = WINDOW_STABLE; window->state = WINDOW_STABLE;
window->decoration = 1;
wl_compositor_add_listener(display->compositor, wl_compositor_add_listener(display->compositor,
&compositor_listener, window); &compositor_listener, window);
@ -799,13 +802,20 @@ display_handle_global(struct wl_display *display,
} }
} }
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
struct display * struct display *
display_create(struct wl_display *display, int fd) display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
{ {
PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa; PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
struct display *d; struct display *d;
EGLint major, minor, count; EGLint major, minor, count;
EGLConfig config; EGLConfig config;
struct wl_display *display;
int fd;
GOptionContext *context;
GError *error;
static const EGLint config_attribs[] = { static const EGLint config_attribs[] = {
EGL_SURFACE_TYPE, 0, EGL_SURFACE_TYPE, 0,
@ -814,10 +824,31 @@ display_create(struct wl_display *display, int fd)
EGL_NONE EGL_NONE
}; };
context = g_option_context_new(NULL);
if (option_entries) {
g_option_context_add_main_entries(context, option_entries, "Wayland View");
if (!g_option_context_parse(context, argc, argv, &error)) {
fprintf(stderr, "option parsing failed: %s\n", error->message);
exit(EXIT_FAILURE);
}
}
d = malloc(sizeof *d); d = malloc(sizeof *d);
if (d == NULL) if (d == NULL)
return NULL; return NULL;
fd = open(gem_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "drm open failed: %m\n");
return NULL;
}
display = wl_display_create(socket_name, sizeof socket_name);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return NULL;
}
get_typed_display_mesa = get_typed_display_mesa =
(PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA"); (PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
if (get_typed_display_mesa == NULL) { if (get_typed_display_mesa == NULL) {
@ -865,6 +896,10 @@ display_create(struct wl_display *display, int fd)
/* Process connection events. */ /* Process connection events. */
wl_display_iterate(display, WL_DISPLAY_READABLE); wl_display_iterate(display, WL_DISPLAY_READABLE);
d->loop = g_main_loop_new(NULL, FALSE);
d->source = wl_glib_source_new(display);
g_source_attach(d->source, NULL);
return d; return d;
} }
@ -873,3 +908,15 @@ display_get_compositor(struct display *display)
{ {
return display->compositor; return display->compositor;
} }
EGLDisplay
display_get_egl_display(struct display *d)
{
return d->dpy;
}
void
display_run(struct display *d)
{
g_main_loop_run(d->loop);
}

View File

@ -35,10 +35,19 @@ struct rectangle {
struct display; struct display;
struct display * struct display *
display_create(struct wl_display *display, int fd); display_create(int *argc, char **argv[], const GOptionEntry *option_entries);
struct wl_compositor * struct wl_compositor *
display_get_compositor(struct display *display); display_get_compositor(struct display *display);
#ifdef EGL_NO_DISPLAY
EGLDisplay
display_get_egl_display(struct display *d);
#endif
void
display_run(struct display *d);
enum { enum {
WINDOW_MODIFIER_SHIFT = 0x01, WINDOW_MODIFIER_SHIFT = 0x01,
WINDOW_MODIFIER_ALT = 0x02, WINDOW_MODIFIER_ALT = 0x02,
@ -114,4 +123,5 @@ window_set_keyboard_focus_handler(struct window *window,
void void
window_set_frame_handler(struct window *window, window_set_frame_handler(struct window *window,
window_frame_handler_t handler, void *data); window_frame_handler_t handler, void *data);
#endif #endif