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 "window.h"
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
static void
set_random_color(cairo_t *cr)
{
@ -120,29 +117,10 @@ int main(int argc, char *argv[])
int fd;
cairo_surface_t *s;
struct timespec ts;
GMainLoop *loop;
GSource *source;
struct flower flower;
struct display *d;
fd = open(gem_device, O_RDWR);
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);
d = display_create(&argc, &argv, NULL);
flower.x = 512;
flower.y = 384;
@ -155,6 +133,7 @@ int main(int argc, char *argv[])
srandom(ts.tv_nsec);
flower.offset = random();
window_set_decoration(flower.window, 0);
window_draw(flower.window);
s = window_get_surface(flower.window);
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_commit(flower.window, 0);
g_main_loop_run(loop);
display_run(d);
return 0;
}

View File

@ -353,9 +353,8 @@ static const struct wl_compositor_listener compositor_listener = {
};
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;
EGLint major, minor, count;
EGLConfig config;
@ -369,19 +368,13 @@ gears_create(struct display *display, int drm_fd)
EGL_NONE
};
get_typed_display_mesa =
(PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
if (get_typed_display_mesa == NULL)
die("eglGetDisplayMESA() not found\n");
gears = malloc(sizeof *gears);
memset(gears, 0, sizeof *gears);
gears->d = display;
gears->window = window_create(display, "Wayland Gears",
x, y, width, height);
gears->display = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
(void *) drm_fd);
gears->display = display_get_egl_display(gears->d);
if (gears->display == NULL)
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[])
{
struct wl_display *display;
struct display *d;
int fd;
GMainLoop *loop;
GSource *source;
struct gears *gears;
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);
gears = gears_create(d, fd);
g_main_loop_run(loop);
d = display_create(&argc, &argv, NULL);
gears = gears_create(d);
display_run(d);
return 0;
}

View File

@ -299,40 +299,10 @@ static const GOptionEntry option_entries[] = {
int
main(int argc, char *argv[])
{
struct wl_display *display;
int fd;
GMainLoop *loop;
GSource *source;
struct display *d;
GOptionContext *context;
GError *error;
int i;
context = g_option_context_new(NULL);
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 ();
d = display_create(&argc, &argv, option_entries);
for (i = 1; i < argc; i++) {
struct image *image;
@ -340,7 +310,7 @@ main(int argc, char *argv[])
image = image_create (d, i, argv[i]);
}
g_main_loop_run(loop);
display_run(d);
return 0;
}

View File

@ -42,8 +42,6 @@
#include "window.h"
static int option_fullscreen;
static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland";
#define MOD_SHIFT 0x01
#define MOD_ALT 0x02
@ -564,44 +562,16 @@ static const GOptionEntry option_entries[] = {
int main(int argc, char *argv[])
{
struct wl_display *display;
int fd;
GMainLoop *loop;
GSource *source;
struct display *d;
struct terminal *terminal;
GOptionContext *context;
GError *error;
context = g_option_context_new(NULL);
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);
d = display_create(&argc, &argv, option_entries);
terminal = terminal_create(d, option_fullscreen);
if (terminal_run(terminal, "/bin/bash"))
exit(EXIT_FAILURE);
g_main_loop_run(loop);
display_run(d);
return 0;
}

View File

@ -266,40 +266,10 @@ static const GOptionEntry option_entries[] = {
int
main(int argc, char *argv[])
{
struct wl_display *display;
int fd;
GMainLoop *loop;
GSource *source;
struct display *d;
GOptionContext *context;
GError *error = NULL;
int i;
context = g_option_context_new(NULL);
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 ();
d = display_create(&argc, &argv, option_entries);
for (i = 1; i < argc; i++) {
struct view *view;
@ -307,7 +277,7 @@ main(int argc, char *argv[])
view = view_create (d, i, argv[i]);
}
g_main_loop_run(loop);
display_run(d);
return 0;
}

View File

@ -56,6 +56,8 @@ struct display {
EGLContext ctx;
cairo_device_t *device;
int fd;
GMainLoop *loop;
GSource *source;
};
struct window {
@ -756,6 +758,7 @@ window_create(struct display *display, const char *title,
window->saved_allocation = window->allocation;
window->margin = 16;
window->state = WINDOW_STABLE;
window->decoration = 1;
wl_compositor_add_listener(display->compositor,
&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 *
display_create(struct wl_display *display, int fd)
display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
{
PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
struct display *d;
EGLint major, minor, count;
EGLConfig config;
struct wl_display *display;
int fd;
GOptionContext *context;
GError *error;
static const EGLint config_attribs[] = {
EGL_SURFACE_TYPE, 0,
@ -814,10 +824,31 @@ display_create(struct wl_display *display, int fd)
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);
if (d == 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 =
(PFNEGLGETTYPEDDISPLAYMESA) eglGetProcAddress("eglGetTypedDisplayMESA");
if (get_typed_display_mesa == NULL) {
@ -865,6 +896,10 @@ display_create(struct wl_display *display, int fd)
/* Process connection events. */
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;
}
@ -873,3 +908,15 @@ display_get_compositor(struct display *display)
{
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 *
display_create(struct wl_display *display, int fd);
display_create(int *argc, char **argv[], const GOptionEntry *option_entries);
struct wl_compositor *
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 {
WINDOW_MODIFIER_SHIFT = 0x01,
WINDOW_MODIFIER_ALT = 0x02,
@ -114,4 +123,5 @@ window_set_keyboard_focus_handler(struct window *window,
void
window_set_frame_handler(struct window *window,
window_frame_handler_t handler, void *data);
#endif