From 6014e77b9f7cf0570fe799fde240970a7cafb7ce Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Mon, 1 Jul 2013 11:22:17 -0700 Subject: [PATCH] util-x11: Rework init_window interface to accept XVisualInfo This is a much more correct way of doing things. Previously, we would select a visual when creating the OpenGL context, but then use a default visual when creating a window. This was fragile and would fail if the default visual was not identical to what we had created. Now, instead, we pass the selected XVisualInfo to our init_window interface and call XCreateWindow instead of XCreateSimpleWindow. This guarantees that the visuals match as required. --- test/glx-dlopen-dlsym.c | 12 +++++++---- test/glx-dlopen-gpa.c | 12 +++++++---- test/glx-dlopen-gpaa.c | 12 +++++++---- test/glx-link-call.c | 10 +++++++--- test/glx-link-gpa.c | 12 +++++++---- test/glx-link-gpaa.c | 12 +++++++---- test/handle-events.c | 17 ++++++++++------ test/util-x11.c | 44 +++++++++++++++++++++++++++++------------ test/util-x11.h | 9 +++++---- 9 files changed, 94 insertions(+), 46 deletions(-) diff --git a/test/glx-dlopen-dlsym.c b/test/glx-dlopen-dlsym.c index 2ac98ac..a967798 100644 --- a/test/glx-dlopen-dlsym.c +++ b/test/glx-dlopen-dlsym.c @@ -149,14 +149,18 @@ main (void) { Display *dpy; Window window; + GLXContext ctx; + XVisualInfo *visual_info; - util_x11_init_display (&dpy); + resolve_symbols (); - util_x11_init_window (dpy, &window); + dpy = util_x11_init_display (); - resolve_symbols (); + create_context (dpy, &ctx, &visual_info); + + window = util_x11_init_window (dpy, visual_info); - handle_events (dpy, window); + handle_events (dpy, ctx, window); util_x11_fini_window (dpy, window); diff --git a/test/glx-dlopen-gpa.c b/test/glx-dlopen-gpa.c index 24238b2..4971794 100644 --- a/test/glx-dlopen-gpa.c +++ b/test/glx-dlopen-gpa.c @@ -146,14 +146,18 @@ main (void) { Display *dpy; Window window; + GLXContext ctx; + XVisualInfo *visual_info; - util_x11_init_display (&dpy); + resolve_symbols (); - util_x11_init_window (dpy, &window); + dpy = util_x11_init_display (); - resolve_symbols (); + create_context (dpy, &ctx, &visual_info); + + window = util_x11_init_window (dpy, visual_info); - handle_events (dpy, window); + handle_events (dpy, ctx, window); util_x11_fini_window (dpy, window); diff --git a/test/glx-dlopen-gpaa.c b/test/glx-dlopen-gpaa.c index 1189c3c..cd0734b 100644 --- a/test/glx-dlopen-gpaa.c +++ b/test/glx-dlopen-gpaa.c @@ -146,14 +146,18 @@ main (void) { Display *dpy; Window window; + GLXContext ctx; + XVisualInfo *visual_info; - util_x11_init_display (&dpy); + resolve_symbols (); - util_x11_init_window (dpy, &window); + dpy = util_x11_init_display (); - resolve_symbols (); + create_context (dpy, &ctx, &visual_info); + + window = util_x11_init_window (dpy, visual_info); - handle_events (dpy, window); + handle_events (dpy, ctx, window); util_x11_fini_window (dpy, window); diff --git a/test/glx-link-call.c b/test/glx-link-call.c index f685246..46cb563 100644 --- a/test/glx-link-call.c +++ b/test/glx-link-call.c @@ -40,12 +40,16 @@ main (void) { Display *dpy; Window window; + GLXContext ctx; + XVisualInfo *visual_info; - util_x11_init_display (&dpy); + dpy = util_x11_init_display (); - util_x11_init_window (dpy, &window); + create_context (dpy, &ctx, &visual_info); - handle_events (dpy, window); + window = util_x11_init_window (dpy, visual_info); + + handle_events (dpy, ctx, window); util_x11_fini_window (dpy, window); diff --git a/test/glx-link-gpa.c b/test/glx-link-gpa.c index 67338fb..8db8009 100644 --- a/test/glx-link-gpa.c +++ b/test/glx-link-gpa.c @@ -150,14 +150,18 @@ main (void) { Display *dpy; Window window; + GLXContext ctx; + XVisualInfo *visual_info; - util_x11_init_display (&dpy); + resolve_symbols (); - util_x11_init_window (dpy, &window); + dpy = util_x11_init_display (); - resolve_symbols (); + create_context (dpy, &ctx, &visual_info); + + window = util_x11_init_window (dpy, visual_info); - handle_events (dpy, window); + handle_events (dpy, ctx, window); util_x11_fini_window (dpy, window); diff --git a/test/glx-link-gpaa.c b/test/glx-link-gpaa.c index 7def0d8..682ee93 100644 --- a/test/glx-link-gpaa.c +++ b/test/glx-link-gpaa.c @@ -150,14 +150,18 @@ main (void) { Display *dpy; Window window; + GLXContext ctx; + XVisualInfo *visual_info; - util_x11_init_display (&dpy); + resolve_symbols (); - util_x11_init_window (dpy, &window); + dpy = util_x11_init_display (); - resolve_symbols (); + create_context (dpy, &ctx, &visual_info); + + window = util_x11_init_window (dpy, visual_info); - handle_events (dpy, window); + handle_events (dpy, ctx, window); util_x11_fini_window (dpy, window); diff --git a/test/handle-events.c b/test/handle-events.c index 3939c04..a28e1e6 100644 --- a/test/handle-events.c +++ b/test/handle-events.c @@ -62,9 +62,8 @@ paint_rgb_using_clear (double r, double g, double b) } static void -draw (Display *dpy, Window window, int width, int height) +create_context (Display *dpy, GLXContext *ctx_ret, XVisualInfo **visual_info_ret) { - int i; int visual_attr[] = { GLX_RGBA, GLX_RED_SIZE, 8, @@ -79,8 +78,14 @@ draw (Display *dpy, Window window, int width, int height) }; /* Window and context setup. */ - XVisualInfo *visual_info = _(glXChooseVisual) (dpy, 0, visual_attr); - GLXContext ctx = _(glXCreateContext) (dpy, visual_info, NULL, True); + *visual_info_ret = _(glXChooseVisual) (dpy, 0, visual_attr); + *ctx_ret = _(glXCreateContext) (dpy, *visual_info_ret, NULL, True); +} + +static void +draw (Display *dpy, GLXContext ctx, Window window, int width, int height) +{ + int i; _(glXMakeCurrent) (dpy, window, ctx); _(glViewport) (0, 0, width, height); @@ -113,7 +118,7 @@ draw (Display *dpy, Window window, int width, int height) } static void -handle_events(Display *dpy, Window window) +handle_events(Display *dpy, GLXContext ctx, Window window) { XEvent xev; int width = 0; @@ -130,7 +135,7 @@ handle_events(Display *dpy, Window window) break; case Expose: if (xev.xexpose.count == 0) { - draw (dpy, window, width, height); + draw (dpy, ctx, window, width, height); return; } break; diff --git a/test/util-x11.c b/test/util-x11.c index 7ab6ebc..e3b3557 100644 --- a/test/util-x11.c +++ b/test/util-x11.c @@ -24,16 +24,20 @@ #include "util-x11.h" -void -util_x11_init_display (Display **dpy) +Display * +util_x11_init_display (void) { - *dpy = XOpenDisplay (NULL); + Display *dpy; + + dpy = XOpenDisplay (NULL); - if (*dpy == NULL) { + if (dpy == NULL) { fprintf(stderr, "Failed to open display %s\n", XDisplayName(NULL)); exit (1); } + + return dpy; } void @@ -42,24 +46,38 @@ util_x11_fini_display (Display *dpy) XCloseDisplay (dpy); } -void -util_x11_init_window (Display *dpy, Window *window) +Window +util_x11_init_window (Display *dpy, XVisualInfo *visual_info) { int width = 64; int height = 64; + Window root, window; + XSetWindowAttributes window_attr; + Colormap colormap; + unsigned long window_mask, event_mask; + unsigned long black; + root = DefaultRootWindow (dpy); + black = BlackPixel (dpy, DefaultScreen (dpy)); + colormap = XCreateColormap (dpy, root, visual_info->visual, AllocNone); + event_mask = KeyPressMask | StructureNotifyMask | ExposureMask; + window_mask = 0; + window_mask |= CWBackPixel; window_attr.background_pixel = black; + window_mask |= CWBorderPixel; window_attr.border_pixel = black; + window_mask |= CWColormap; window_attr.colormap = colormap; + window_mask |= CWEventMask; window_attr.event_mask = event_mask; - *window = XCreateSimpleWindow(dpy, DefaultRootWindow (dpy), - 0, 0, width, height, 0, - BlackPixel (dpy, DefaultScreen (dpy)), - BlackPixel (dpy, DefaultScreen (dpy))); + window = XCreateWindow(dpy, root, 0, 0, width, height, 0, + visual_info->depth, InputOutput, + visual_info->visual, + window_mask, &window_attr); - XSelectInput(dpy, *window, - KeyPressMask | StructureNotifyMask | ExposureMask); + XMapWindow (dpy, window); - XMapWindow (dpy, *window); + XFreeColormap (dpy, colormap); + return window; } void diff --git a/test/util-x11.h b/test/util-x11.h index 398b263..7ae0c59 100644 --- a/test/util-x11.h +++ b/test/util-x11.h @@ -23,15 +23,16 @@ #define UTIL_X11_H #include +#include -void -util_x11_init_display (Display **dpy); +Display * +util_x11_init_display (void); void util_x11_fini_display (Display *dpy); -void -util_x11_init_window (Display *dpy, Window *window); +Window +util_x11_init_window (Display *dpy, XVisualInfo *visual_info); void util_x11_fini_window (Display *dpy, Window window); -- 2.45.2