From 3b64a439f08dd9fceebfdf5243c434334c9e61c2 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Tue, 28 Jun 2011 13:38:50 +0100 Subject: [PATCH] replace public native_event APIs with typesafe APIs This adds Xlib and Win32 typesafe replacements for cogl_renderer_handle_native_event, cogl_renderer_add_native_filter, cogl_renderer_remove_native_filter. The old functions are kept as an implementation detail so we can share code. Signed-off-by: Neil Roberts --- cogl/Makefile.am | 1 + cogl/cogl-renderer-private.h | 17 +++ cogl/cogl-renderer-xlib.c | 26 ++++ cogl/cogl-renderer.c | 16 +-- cogl/cogl-renderer.h | 123 ++++++++++++++---- cogl/cogl-types.h | 22 ---- cogl/cogl-win32-renderer.c | 58 +++++++++ cogl/cogl-xlib.c | 2 +- cogl/winsys/cogl-texture-pixmap-x11.c | 15 +-- cogl/winsys/cogl-winsys-egl.c | 15 +-- cogl/winsys/cogl-winsys-glx.c | 15 +-- cogl/winsys/cogl-winsys-wgl.c | 20 +-- .../cogl-2.0-experimental/cogl-sections.txt | 13 +- examples/x11-foreign.c | 3 +- 14 files changed, 250 insertions(+), 96 deletions(-) create mode 100644 cogl/cogl-win32-renderer.c diff --git a/cogl/Makefile.am b/cogl/Makefile.am index 714dc13f7..cb2df236f 100644 --- a/cogl/Makefile.am +++ b/cogl/Makefile.am @@ -332,6 +332,7 @@ cogl_sources_c += \ endif if SUPPORT_WGL cogl_sources_c += \ + $(srcdir)/cogl-win32-renderer.c \ $(srcdir)/winsys/cogl-winsys-wgl.c \ $(srcdir)/winsys/cogl-winsys-wgl-feature-functions.h endif diff --git a/cogl/cogl-renderer-private.h b/cogl/cogl-renderer-private.h index fa07b3208..a6110d35c 100644 --- a/cogl/cogl-renderer-private.h +++ b/cogl/cogl-renderer-private.h @@ -53,4 +53,21 @@ struct _CoglRenderer void *winsys; }; +typedef CoglFilterReturn (* CoglNativeFilterFunc) (void *native_event, + void *data); + +CoglFilterReturn +_cogl_renderer_handle_native_event (CoglRenderer *renderer, + void *event); + +void +_cogl_renderer_add_native_filter (CoglRenderer *renderer, + CoglNativeFilterFunc func, + void *data); + +void +_cogl_renderer_remove_native_filter (CoglRenderer *renderer, + CoglNativeFilterFunc func, + void *data); + #endif /* __COGL_RENDERER_PRIVATE_H */ diff --git a/cogl/cogl-renderer-xlib.c b/cogl/cogl-renderer-xlib.c index f8dbe504f..ec5eb2a7e 100644 --- a/cogl/cogl-renderer-xlib.c +++ b/cogl/cogl-renderer-xlib.c @@ -198,3 +198,29 @@ cogl_renderer_xlib_get_display (CoglRenderer *renderer) return xlib_renderer->xdpy; } + +CoglFilterReturn +cogl_xlib_renderer_handle_event (CoglRenderer *renderer, + XEvent *event) +{ + return _cogl_renderer_handle_native_event (renderer, event); +} + +void +cogl_xlib_renderer_add_filter (CoglRenderer *renderer, + CoglXlibFilterFunc func, + void *data) +{ + _cogl_renderer_add_native_filter (renderer, + (CoglNativeFilterFunc)func, data); +} + +void +cogl_xlib_renderer_remove_filter (CoglRenderer *renderer, + CoglXlibFilterFunc func, + void *data) +{ + _cogl_renderer_remove_native_filter (renderer, + (CoglNativeFilterFunc)func, data); +} + diff --git a/cogl/cogl-renderer.c b/cogl/cogl-renderer.c index d89065d50..fb6e94865 100644 --- a/cogl/cogl-renderer.c +++ b/cogl/cogl-renderer.c @@ -229,8 +229,8 @@ cogl_renderer_connect (CoglRenderer *renderer, GError **error) } CoglFilterReturn -cogl_renderer_handle_native_event (CoglRenderer *renderer, - void *event) +_cogl_renderer_handle_native_event (CoglRenderer *renderer, + void *event) { GSList *l, *next; @@ -254,9 +254,9 @@ cogl_renderer_handle_native_event (CoglRenderer *renderer, } void -cogl_renderer_add_native_filter (CoglRenderer *renderer, - CoglNativeFilterFunc func, - void *data) +_cogl_renderer_add_native_filter (CoglRenderer *renderer, + CoglNativeFilterFunc func, + void *data) { CoglNativeFilterClosure *closure; @@ -268,9 +268,9 @@ cogl_renderer_add_native_filter (CoglRenderer *renderer, } void -cogl_renderer_remove_native_filter (CoglRenderer *renderer, - CoglNativeFilterFunc func, - void *data) +_cogl_renderer_remove_native_filter (CoglRenderer *renderer, + CoglNativeFilterFunc func, + void *data) { GSList *l, *prev = NULL; diff --git a/cogl/cogl-renderer.h b/cogl/cogl-renderer.h index 2c6d9d0ef..13679b796 100644 --- a/cogl/cogl-renderer.h +++ b/cogl/cogl-renderer.h @@ -122,17 +122,17 @@ cogl_renderer_set_winsys_id (CoglRenderer *renderer, CoglWinsysID cogl_renderer_get_winsys_id (CoglRenderer *renderer); -#define cogl_renderer_handle_native_event cogl_renderer_handle_native_event_EXP +#ifdef COGL_HAS_XLIB + +#define cogl_xlib_renderer_handle_event \ + cogl_xlib_renderer_handle_event_EXP /* - * cogl_renderer_handle_native_event: - * @event: pointer to native event structure + * cogl_xlib_renderer_handle_event: + * @event: pointer to an XEvent structure * * This function processes a single event; it can be used to hook into * external event retrieval (for example that done by Clutter or - * GDK). The type of the structure that event points to depends on the - * window system used for the renderer. On an xlib renderer it would - * be a pointer to an XEvent or an a Windows renderer it would be a - * pointer to a MSG struct. + * GDK). * * Return value: #CoglFilterReturn. %COGL_FILTER_REMOVE indicates that * Cogl has internally handled the event and the caller should do no @@ -141,38 +141,47 @@ cogl_renderer_get_winsys_id (CoglRenderer *renderer); * internal state without taking any exclusive action. */ CoglFilterReturn -cogl_renderer_handle_native_event (CoglRenderer *renderer, - void *event); +cogl_xlib_renderer_handle_event (CoglRenderer *renderer, + XEvent *event); -#define cogl_renderer_add_native_filter cogl_renderer_add_native_filter_EXP /* - * cogl_renderer_add_native_filter: + * CoglXlibFilterFunc: + * @event: pointer to an XEvent structure + * @data: The data that was given when the filter was added + * + * A callback function that can be registered with + * cogl_xlib_renderer_add_filter(). The function should return + * %COGL_FILTER_REMOVE if it wants to prevent further processing or + * %COGL_FILTER_CONTINUE otherwise. + */ +typedef CoglFilterReturn (* CoglXlibFilterFunc) (XEvent *event, + void *data); + +#define cogl_xlib_renderer_add_filter cogl_xlib_renderer_add_filter_EXP +/* + * cogl_xlib_renderer_add_filter: * * Adds a callback function that will receive all native events. The * function can stop further processing of the event by return - * %COGL_FILTER_REMOVE. What is considered a native event depends on - * the type of renderer used. An xlib based renderer would pass all - * XEvents whereas a Windows based renderer would pass MSGs. + * %COGL_FILTER_REMOVE. */ void -cogl_renderer_add_native_filter (CoglRenderer *renderer, - CoglNativeFilterFunc func, - void *data); +cogl_xlib_renderer_add_filter (CoglRenderer *renderer, + CoglXlibFilterFunc func, + void *data); -#define cogl_renderer_remove_native_filter \ - cogl_renderer_remove_native_filter_EXP +#define cogl_xlib_renderer_remove_filter \ + cogl_xlib_renderer_remove_filter_EXP /* - * cogl_renderer_remove_native_filter: + * cogl_xlib_renderer_remove_filter: * * Removes a callback that was previously added with - * cogl_renderer_add_native_filter(). + * cogl_xlib_renderer_add_filter(). */ void -cogl_renderer_remove_native_filter (CoglRenderer *renderer, - CoglNativeFilterFunc func, - void *data); - -#ifdef COGL_HAS_XLIB +cogl_xlib_renderer_remove_filter (CoglRenderer *renderer, + CoglXlibFilterFunc func, + void *data); #define cogl_renderer_xlib_get_foreign_display \ cogl_renderer_xlib_get_foreign_display_EXP @@ -228,6 +237,68 @@ struct wl_compositor * cogl_renderer_wayland_get_compositor (CoglRenderer *renderer); #endif /* COGL_HAS_EGL_PLATFORM_WAYLAND_SUPPORT */ +#ifdef COGL_HAS_WGL_SUPPORT + +#define cogl_win32_renderer_handle_event \ + cogl_win32_renderer_handle_event_EXP +/* + * cogl_win32_renderer_handle_event: + * @message: A pointer to a win32 MSG struct + * + * This function processes a single event; it can be used to hook into + * external event retrieval (for example that done by Clutter or + * GDK). + * + * Return value: #CoglFilterReturn. %COGL_FILTER_REMOVE indicates that + * Cogl has internally handled the event and the caller should do no + * further processing. %COGL_FILTER_CONTINUE indicates that Cogl is + * either not interested in the event, or has used the event to update + * internal state without taking any exclusive action. + */ +CoglFilterReturn +cogl_win32_renderer_handle_event (CoglRenderer *renderer, + MSG *message); + +/* + * CoglXlibFilterFunc: + * @message: A pointer to a win32 MSG struct + * @data: The data that was given when the filter was added + * + * A callback function that can be registered with + * cogl_win32_renderer_add_filter(). The function should return + * %COGL_FILTER_REMOVE if it wants to prevent further processing or + * %COGL_FILTER_CONTINUE otherwise. + */ +typedef CoglFilterReturn (* CoglWin32FilterFunc) (MSG *message, + void *data); + +#define cogl_win32_renderer_add_filter cogl_win32_renderer_add_filter_EXP +/* + * cogl_win32_renderer_add_filter: + * + * Adds a callback function that will receive all native events. The + * function can stop further processing of the event by return + * %COGL_FILTER_REMOVE. + */ +void +cogl_win32_renderer_add_filter (CoglRenderer *renderer, + CoglWin32FilterFunc func, + void *data); + +#define cogl_win32_renderer_remove_filter \ + cogl_win32_renderer_remove_filter_EXP +/* + * cogl_win32_renderer_remove_filter: + * + * Removes a callback that was previously added with + * cogl_win32_renderer_add_filter(). + */ +void +cogl_win32_renderer_remove_filter (CoglRenderer *renderer, + CoglWin32FilterFunc func, + void *data); +#endif /* COGL_HAS_WGL_SUPPORT */ + #define cogl_renderer_check_onscreen_template \ cogl_renderer_check_onscreen_template_EXP gboolean diff --git a/cogl/cogl-types.h b/cogl/cogl-types.h index ab610c173..55f22969c 100644 --- a/cogl/cogl-types.h +++ b/cogl/cogl-types.h @@ -671,28 +671,6 @@ typedef enum _CoglWinsysFeature COGL_WINSYS_FEATURE_N_FEATURES } CoglWinsysFeature; -/* XXX: Note these enum types are only referenced by experimental API - * so although they aren't explicitly guarded they are implicitly - * experimental too. */ - -/* - * CoglNativeFilterFunc: - * @native_event: A pointer to the native system event - * @data: The data that was given when the filter was added - * - * A callback function that can be registered with - * cogl_renderer_add_native_filter(). The function should return - * %COGL_FILTER_REMOVE if it wants to prevent further processing or - * %COGL_FILTER_CONTINUE otherwise. - * - * The type that @native_event points to depends on the type of the - * underlying renderer. On xlib based renderers this would point to an - * XEvent struct and on Windows it would point to a MSG struct. - */ -typedef CoglFilterReturn (* CoglNativeFilterFunc) (void *native_event, - void *data); - - G_END_DECLS #endif /* __COGL_TYPES_H__ */ diff --git a/cogl/cogl-win32-renderer.c b/cogl/cogl-win32-renderer.c new file mode 100644 index 000000000..ad7e7912b --- /dev/null +++ b/cogl/cogl-win32-renderer.c @@ -0,0 +1,58 @@ +/* + * Cogl + * + * An object oriented GL/GLES Abstraction/Utility Layer + * + * Copyright (C) 2011 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Authors: + * Robert Bragg + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "cogl-renderer-private.h" +#include "cogl-win32-renderer.h" + +CoglFilterReturn +cogl_win32_renderer_handle_event (CoglRenderer *renderer, + MSG *event) +{ + return _cogl_renderer_handle_native_event (renderer, event); +} + +void +cogl_win32_renderer_add_filter (CoglRenderer *renderer, + CoglWin32FilterFunc func, + void *data) +{ + _cogl_renderer_add_native_filter (renderer, + (CoglNativeFilterFunc)func, data); +} + +void +cogl_win32_renderer_remove_filter (CoglRenderer *renderer, + CoglWin32FilterFunc func, + void *data) +{ + _cogl_renderer_remove_native_filter (renderer, + (CoglNativeFilterFunc)func, data); +} + diff --git a/cogl/cogl-xlib.c b/cogl/cogl-xlib.c index e70992f0f..2f65dc926 100644 --- a/cogl/cogl-xlib.c +++ b/cogl/cogl-xlib.c @@ -78,7 +78,7 @@ cogl_xlib_handle_event (XEvent *xevent) _COGL_GET_CONTEXT (ctx, COGL_FILTER_CONTINUE); /* Pass the event on to the renderer */ - return cogl_renderer_handle_native_event (ctx->display->renderer, xevent); + return cogl_xlib_renderer_handle_event (ctx->display->renderer, xevent); } void diff --git a/cogl/winsys/cogl-texture-pixmap-x11.c b/cogl/winsys/cogl-texture-pixmap-x11.c index 48bcaac49..134d193cf 100644 --- a/cogl/winsys/cogl-texture-pixmap-x11.c +++ b/cogl/winsys/cogl-texture-pixmap-x11.c @@ -213,9 +213,8 @@ process_damage_event (CoglTexturePixmapX11 *tex_pixmap, } static CoglFilterReturn -_cogl_texture_pixmap_x11_filter (void *native_event, void *data) +_cogl_texture_pixmap_x11_filter (XEvent *event, void *data) { - XEvent *event = native_event; CoglTexturePixmapX11 *tex_pixmap = data; int damage_base; @@ -241,9 +240,9 @@ set_damage_object_internal (CoglContext *ctx, { if (tex_pixmap->damage) { - cogl_renderer_remove_native_filter (ctx->display->renderer, - _cogl_texture_pixmap_x11_filter, - tex_pixmap); + cogl_xlib_renderer_remove_filter (ctx->display->renderer, + _cogl_texture_pixmap_x11_filter, + tex_pixmap); if (tex_pixmap->damage_owned) { @@ -256,9 +255,9 @@ set_damage_object_internal (CoglContext *ctx, tex_pixmap->damage_report_level = report_level; if (damage) - cogl_renderer_add_native_filter (ctx->display->renderer, - _cogl_texture_pixmap_x11_filter, - tex_pixmap); + cogl_xlib_renderer_add_filter (ctx->display->renderer, + _cogl_texture_pixmap_x11_filter, + tex_pixmap); } CoglHandle diff --git a/cogl/winsys/cogl-winsys-egl.c b/cogl/winsys/cogl-winsys-egl.c index 69da52990..f489d2a58 100644 --- a/cogl/winsys/cogl-winsys-egl.c +++ b/cogl/winsys/cogl-winsys-egl.c @@ -252,9 +252,8 @@ find_onscreen_for_xid (CoglContext *context, guint32 xid) } static CoglFilterReturn -event_filter_cb (void *event, void *data) +event_filter_cb (XEvent *event, void *data) { - XEvent *xevent = event; CoglContext *context = data; if (xevent->type == ConfigureNotify) @@ -1108,9 +1107,9 @@ _cogl_winsys_context_init (CoglContext *context, GError **error) context->winsys = g_new0 (CoglContextEGL, 1); #ifdef COGL_HAS_EGL_PLATFORM_POWERVR_X11_SUPPORT - cogl_renderer_add_native_filter (context->display->renderer, - event_filter_cb, - context); + cogl_xlib_renderer_add_filter (context->display->renderer, + event_filter_cb, + context); #endif update_winsys_features (context); @@ -1121,9 +1120,9 @@ static void _cogl_winsys_context_deinit (CoglContext *context) { #ifdef COGL_HAS_EGL_PLATFORM_POWERVR_X11_SUPPORT - cogl_renderer_remove_native_filter (context->display->renderer, - event_filter_cb, - context); + cogl_xlib_renderer_remove_filter (context->display->renderer, + event_filter_cb, + context); #endif g_free (context->winsys); } diff --git a/cogl/winsys/cogl-winsys-glx.c b/cogl/winsys/cogl-winsys-glx.c index 77e7ebe43..047bc28a3 100644 --- a/cogl/winsys/cogl-winsys-glx.c +++ b/cogl/winsys/cogl-winsys-glx.c @@ -220,9 +220,8 @@ notify_swap_buffers (CoglContext *context, GLXDrawable drawable) } static CoglFilterReturn -glx_event_filter_cb (void *native_event, void *data) +glx_event_filter_cb (XEvent *xevent, void *data) { - XEvent *xevent = native_event; CoglContext *context = data; #ifdef GLX_INTEL_swap_event CoglRendererGLX *glx_renderer; @@ -672,9 +671,9 @@ _cogl_winsys_context_init (CoglContext *context, GError **error) { context->winsys = g_new0 (CoglContextGLX, 1); - cogl_renderer_add_native_filter (context->display->renderer, - glx_event_filter_cb, - context); + cogl_xlib_renderer_add_filter (context->display->renderer, + glx_event_filter_cb, + context); update_winsys_features (context); return TRUE; @@ -683,9 +682,9 @@ _cogl_winsys_context_init (CoglContext *context, GError **error) static void _cogl_winsys_context_deinit (CoglContext *context) { - cogl_renderer_remove_native_filter (context->display->renderer, - glx_event_filter_cb, - context); + cogl_xlib_renderer_remove_filter (context->display->renderer, + glx_event_filter_cb, + context); g_free (context->winsys); } diff --git a/cogl/winsys/cogl-winsys-wgl.c b/cogl/winsys/cogl-winsys-wgl.c index a20cd02d6..c221775c1 100644 --- a/cogl/winsys/cogl-winsys-wgl.c +++ b/cogl/winsys/cogl-winsys-wgl.c @@ -41,6 +41,7 @@ #include "cogl-onscreen-template-private.h" #include "cogl-private.h" #include "cogl-feature-private.h" +#include "cogl-win32-renderer.h" typedef struct _CoglRendererWgl { @@ -166,9 +167,8 @@ find_onscreen_for_hwnd (CoglContext *context, HWND hwnd) } static CoglFilterReturn -win32_event_filter_cb (void *native_event, void *data) +win32_event_filter_cb (MSG *msg, void *data) { - MSG *msg = native_event; CoglContext *context = data; if (msg->message == WM_SIZE) @@ -217,7 +217,7 @@ window_proc (HWND hwnd, UINT umsg, the window proc is. We want the application to forward on all messages through Cogl so that it can have a chance to process them which might mean that that in it's GetMessage loop it could - call cogl_renderer_handle_native_event for every message. However + call cogl_win32_renderer_handle_event for every message. However the message loop would usually call DispatchMessage as well which mean this window proc would be invoked and Cogl would see the message twice. However we can't just ignore messages in the @@ -260,7 +260,7 @@ window_proc (HWND hwnd, UINT umsg, renderer = COGL_FRAMEBUFFER (onscreen)->context->display->renderer; message_handled = - cogl_renderer_handle_native_event (renderer, &msg); + cogl_win32_renderer_handle_event (renderer, &msg); } if (!message_handled) @@ -587,9 +587,9 @@ _cogl_winsys_context_init (CoglContext *context, GError **error) wgl_context = context->winsys = g_new0 (CoglContextWgl, 1); - cogl_renderer_add_native_filter (context->display->renderer, - win32_event_filter_cb, - context); + cogl_win32_renderer_add_filter (context->display->renderer, + win32_event_filter_cb, + context); update_winsys_features (context); @@ -599,9 +599,9 @@ _cogl_winsys_context_init (CoglContext *context, GError **error) static void _cogl_winsys_context_deinit (CoglContext *context) { - cogl_renderer_remove_native_filter (context->display->renderer, - win32_event_filter_cb, - context); + cogl_win32_renderer_remove_filter (context->display->renderer, + win32_event_filter_cb, + context); g_free (context->winsys); } diff --git a/doc/reference/cogl-2.0-experimental/cogl-sections.txt b/doc/reference/cogl-2.0-experimental/cogl-sections.txt index 8909a72e3..a4da25613 100644 --- a/doc/reference/cogl-2.0-experimental/cogl-sections.txt +++ b/doc/reference/cogl-2.0-experimental/cogl-sections.txt @@ -21,9 +21,16 @@ cogl_renderer_xlib_set_foreign_display cogl_renderer_xlib_get_foreign_display -cogl_renderer_add_native_filter -cogl_renderer_remove_native_filter -cogl_renderer_handle_native_event +CoglXlibFilterFunc +cogl_xlib_renderer_add_native_filter +cogl_xlib_renderer_remove_native_filter +cogl_xlib_renderer_handle_native_event + + +CoglWin32FilterFunc +cogl_win32_renderer_add_native_filter +cogl_win32_renderer_remove_native_filter +cogl_win32_renderer_handle_native_event cogl_renderer_wayland_set_foreign_display diff --git a/examples/x11-foreign.c b/examples/x11-foreign.c index 6f68adeff..9337d9ec0 100644 --- a/examples/x11-foreign.c +++ b/examples/x11-foreign.c @@ -175,8 +175,7 @@ main (int argc, char **argv) case ButtonRelease: return 0; } - /* FIXME: This should be replaced with some equivalent cogl_xlib_ typesafe API... */ - cogl_renderer_handle_native_event (renderer, &event); + cogl_xlib_renderer_handle_event (renderer, &event); } cogl_clear (&black, COGL_BUFFER_BIT_COLOR); cogl_primitive_draw (triangle);