[AD] new window hooks patch |
[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
Ok, I'm submitting this new patch that replaces the old one I already posted
a while ago.
This new one adds routines to set up to 8 callbacks for window
closing/resizing events; the gfx driver developer only has to call
_handle_window_close() or _handle_window_resize(w, h) when needed.
The _handle_window_close() function other than calling queued callbacks, also
simulates an ESC keypress, as suggested by Robert J. Ohannessian, as this
works great and doesn't break compatibility with the past.
The patch includes window close handling for X (thanks for the info Michael!)
and BeOS windowed drivers; I didn't hack on the Win32 one, but I think it
should be easy as just replacing lines 203-204 of wwnd.c with a
_handle_window_close() call.
Also included in the patch are the docs for new functions.
Have fun and let me know!
--
Angelo Mottola
a.mottola@xxxxxxxxxx
ICQ UIN #66972680
diff -Nru allegro/include/allegro/aintern.h
allegro.new/include/allegro/aintern.h
--- allegro/include/allegro/aintern.h Thu Jan 4 01:53:32 2001
+++ allegro.new/include/allegro/aintern.h Sun Jan 7 21:23:20 2001
@@ -247,6 +247,12 @@
#endif
+#define MAX_WINDOW_CLOSE_CALLBACKS 8
+#define MAX_WINDOW_RESIZE_CALLBACKS 8
+
+/* functions to handle windowed gfx driver hooks */
+AL_FUNC(void, _handle_window_close, (void));
+AL_FUNC(void, _handle_window_resize, (int w, int h));
/* stuff for setting up bitmaps */
AL_FUNC(BITMAP *, _make_bitmap, (int w, int h, unsigned long addr, GFX_DRIVER
*driver, int color_depth, int bpl));
diff -Nru allegro/include/allegro.h allegro.new/include/allegro.h
--- allegro/include/allegro.h Sat Dec 16 01:35:49 2000
+++ allegro.new/include/allegro.h Sun Jan 7 21:23:20 2001
@@ -121,12 +121,16 @@
AL_PRINTFUNC(void, allegro_message, (AL_CONST char *msg, ...), 1, 2);
+AL_FUNC(int, set_window_close_callback, (AL_METHOD(void, cb, (void))));
+AL_FUNC(void, remove_window_close_callback, (AL_METHOD(void, cb, (void))));
+AL_FUNC(int, set_window_resize_callback, (AL_METHOD(void, cb, (int w, int
h))));
+AL_FUNC(void, remove_window_resize_callback, (AL_METHOD(void, cb, (int w, int
h))));
+
AL_FUNC(void, al_assert, (AL_CONST char *file, int line));
AL_PRINTFUNC(void, al_trace, (AL_CONST char *msg, ...), 1, 2);
AL_FUNC(void, register_assert_handler, (AL_METHOD(int, handler, (AL_CONST
char *msg))));
AL_FUNC(void, register_trace_handler, (AL_METHOD(int, handler, (AL_CONST char
*msg))));
-
#ifdef DEBUGMODE
#define ASSERT(condition) { if (!(condition)) al_assert(__FILE__,
__LINE__); }
diff -Nru allegro/src/graphics.c allegro.new/src/graphics.c
--- allegro/src/graphics.c Sun Dec 10 20:16:18 2000
+++ allegro.new/src/graphics.c Sun Jan 7 22:51:23 2001
@@ -12,6 +12,8 @@
*
* By Shawn Hargreaves.
*
+ * Window closing/resizing handlers added by Angelo Mottola.
+ *
* See readme.txt for copyright information.
*/
@@ -122,6 +124,11 @@
static VRAM_BITMAP *vram_bitmap_list = NULL;
+static void (*window_close_cb[MAX_WINDOW_CLOSE_CALLBACKS])(void) =
+ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+static void (*window_resize_cb[MAX_WINDOW_RESIZE_CALLBACKS])(int, int) =
+ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+
/* lock_bitmap:
@@ -568,7 +575,7 @@
if (system_driver->display_switch_lock)
system_driver->display_switch_lock(FALSE, FALSE);
-
+
return 0;
}
@@ -1244,6 +1251,104 @@
}
return -1;
+}
+
+
+
+/* set_window_close_callback:
+ * Adds a function to the window closing callbacks queue.
+ */
+int set_window_close_callback(void (*cb)(void))
+{
+ int i;
+
+ for (i=0; i<MAX_WINDOW_CLOSE_CALLBACKS; i++) {
+ if (window_close_cb[i] == NULL) {
+ window_close_cb[i] = cb;
+ return 0;
+ }
+ }
+ return -1;
+}
+
+
+
+/* remove_window_close_callback:
+ * Removes a function from the window closing callbacks queue.
+ */
+void remove_window_close_callback(void (*cb)(void))
+{
+ int i;
+
+ for (i=0; i<MAX_WINDOW_CLOSE_CALLBACKS; i++) {
+ if (window_close_cb[i] == cb)
+ window_close_cb[i] = NULL;
+ }
+}
+
+
+
+/* set_window_resize_callback:
+ * Adds a function to the window resizing callbacks queue.
+ */
+int set_window_resize_callback(void (*cb)(int, int))
+{
+ int i;
+
+ for (i=0; i<MAX_WINDOW_RESIZE_CALLBACKS; i++) {
+ if (window_resize_cb[i] == NULL) {
+ window_resize_cb[i] = cb;
+ return 0;
+ }
+ }
+ return -1;
+}
+
+
+
+/* remove_window_resize_callback:
+ * Removes a function from the window resizing callbacks queue.
+ */
+void remove_window_resize_callback(void (*cb)(int, int))
+{
+ int i;
+
+ for (i=0; i<MAX_WINDOW_RESIZE_CALLBACKS; i++) {
+ if (window_resize_cb[i] == cb)
+ window_resize_cb[i] = NULL;
+ }
+}
+
+
+
+/* _handle_window_close:
+ * Hook function for window closing event; calls queued callback functions
+ * and simulates an ESC keypress.
+ */
+void _handle_window_close()
+{
+ int i;
+
+ for (i=0; i<MAX_WINDOW_CLOSE_CALLBACKS; i++) {
+ if (window_close_cb[i])
+ window_close_cb[i]();
+ }
+ simulate_keypress((KEY_ESC << 8) | 27);
+}
+
+
+
+/* _handle_window_resize:
+ * Hook function for window resizing event; calls queued callback functions.
+ */
+void _handle_window_resize(int w, int h)
+{
+ int i;
+
+ for (i=0; i<MAX_WINDOW_RESIZE_CALLBACKS; i++) {
+ if (window_resize_cb[i])
+ window_resize_cb[i](w, h);
+ }
}
diff -Nru allegro/src/x/xwin.c allegro.new/src/x/xwin.c
--- allegro/src/x/xwin.c Sat Nov 11 11:07:20 2000
+++ allegro.new/src/x/xwin.c Sun Jan 7 22:40:25 2001
@@ -139,6 +139,8 @@
/* Array of keycodes which are pressed now (used for auto-repeat). */
int _xwin_keycode_pressed[256];
+/* Atom to catch window delete events */
+static Atom wm_delete_window;
/* Forward declarations for private functions. */
@@ -346,7 +348,7 @@
setattr.border_pixel = XBlackPixel(_xwin.display, _xwin.screen);
setattr.event_mask = (KeyPressMask | KeyReleaseMask
| EnterWindowMask | LeaveWindowMask
- | FocusChangeMask | ExposureMask
+ | FocusChangeMask | ExposureMask | PropertyChangeMask
| ButtonPressMask | ButtonReleaseMask | PointerMotionMask
/*| MappingNotifyMask (SubstructureRedirectMask?)*/);
_xwin.window = XCreateWindow(_xwin.display,
XDefaultRootWindow(_xwin.display),
@@ -372,6 +374,10 @@
/* Set default window parameters. */
(*_xwin_window_defaultor)();
+ /* Set WM_DELETE_WINDOW atom in WM_PROTOCOLS property (to get
window_delete requests). */
+ wm_delete_window = XInternAtom (_xwin.display, "WM_DELETE_WINDOW", False);
+ XSetWMProtocols (_xwin.display, _xwin.window, &wm_delete_window, 1);
+
/* Map window. */
XMapWindow(_xwin.display, _xwin.window);
@@ -2053,6 +2059,11 @@
if (event->xmapping.request == MappingKeyboard)
_xwin_private_init_keyboard_tables();
break;
+ case ClientMessage:
+ /* Window closing request. */
+ if (event->xclient.data.l[0] == wm_delete_window)
+ _handle_window_close();
+ break;
}
}
diff -Nru allegro/src/beos/bgfxapi.cpp allegro.new/src/beos/bgfxapi.cpp
--- allegro/src/beos/bgfxapi.cpp Mon Jan 8 23:21:28 2001
+++ allegro.new/src/beos/bgfxapi.cpp Mon Jan 8 23:22:05 2001
@@ -1169,6 +1169,7 @@
*/
bool BeAllegroWindow::QuitRequested(void)
{
+ _handle_window_close();
return false;
}
diff -Nru allegro/docs/allegro._tx allegro.new/docs/allegro._tx
--- allegro/docs/allegro._tx Mon Jan 8 23:30:24 2001
+++ allegro.new/docs/allegro._tx Mon Jan 8 23:54:10 2001
@@ -151,6 +151,30 @@
On platforms that are capable of it, this routine alters the window title
for your Allegro program.
+@@int @set_window_close_callback(void (*cb)());
+ On platforms that support windowed gfx drivers, this function adds a
+ callback to a queue of procedures Allegro will call when it receives
+ a window close event from the window manager (i.e. when the user attempts
+ to close the window); up to 8 different callbacks can be queued. On
+ closing events, Allegro applications will also automatically get an ESC
+ keypress event.
+
+@@void @remove_window_close_callback(void (*cb)());
+ Removes the specified procedure from the window closing event callbacks
+ queue.
+
+@@int @set_window_resize_callback(void (*cb)(int w, int h));
+ On platforms that supports windowed gfx drivers, this functions adds a
+ callback to a queue of procedures Allegro will call when it receives
+ a window resize event from the window manager. The callbacks get the
+ new window width and height in the w and h parameters; up to 8 callbacks
+ can be queued. Currently no gfx drivers support the resize event, so this
+ function is reserved for future enhancements.
+
+@@void @remove_window_resize_callback(void (*cb)(int w, int h));
+ Removed the specified procedure from the window resize event callbacks
+ queue.
+
@@int @desktop_color_depth();
On platforms that can run Allegro programs in a window on an existing
desktop, returns the currently selected desktop color depth (your program