SDL: Updated X11 event debugging

From 6f6f4fbfd3e438da6bca8b6c9d2bfdecb677aedd Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 4 Aug 2024 08:06:43 -0700
Subject: [PATCH] Updated X11 event debugging

---
 src/video/x11/SDL_x11events.c | 128 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 63 deletions(-)

diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c
index e4650c32c89f4..cb46a4e377c6f 100644
--- a/src/video/x11/SDL_x11events.c
+++ b/src/video/x11/SDL_x11events.c
@@ -44,7 +44,9 @@
 
 #include <stdio.h>
 
-/*#define DEBUG_XEVENTS*/
+#if 0
+#define DEBUG_XEVENTS
+#endif
 
 #ifndef _NET_WM_MOVERESIZE_SIZE_TOPLEFT
 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
@@ -335,7 +337,7 @@ void X11_ReconcileKeyboardState(SDL_VideoDevice *_this)
 static void X11_DispatchFocusIn(SDL_VideoDevice *_this, SDL_WindowData *data)
 {
 #ifdef DEBUG_XEVENTS
-    printf("window %p: Dispatching FocusIn\n", data);
+    SDL_Log("window 0x%lx: Dispatching FocusIn\n", data->xwindow);
 #endif
     SDL_SetKeyboardFocus(data->window);
     X11_ReconcileKeyboardState(_this);
@@ -355,7 +357,7 @@ static void X11_DispatchFocusIn(SDL_VideoDevice *_this, SDL_WindowData *data)
 static void X11_DispatchFocusOut(SDL_VideoDevice *_this, SDL_WindowData *data)
 {
 #ifdef DEBUG_XEVENTS
-    printf("window %p: Dispatching FocusOut\n", data);
+    SDL_Log("window 0x%lx: Dispatching FocusOut\n", data->xwindow);
 #endif
     /* If another window has already processed a focus in, then don't try to
      * remove focus here.  Doing so will incorrectly remove focus from that
@@ -517,7 +519,7 @@ static void X11_UpdateUserTime(SDL_WindowData *data, const unsigned long latest)
                             XA_CARDINAL, 32, PropModeReplace,
                             (const unsigned char *)&latest, 1);
 #ifdef DEBUG_XEVENTS
-        printf("window %p: updating _NET_WM_USER_TIME to %lu\n", data, latest);
+        SDL_Log("window 0x%lx: updating _NET_WM_USER_TIME to %lu\n", data->xwindow, latest);
 #endif
         data->user_time = latest;
     }
@@ -547,7 +549,7 @@ static void X11_HandleClipboardEvent(SDL_VideoDevice *_this, const XEvent *xeven
 #ifdef DEBUG_XEVENTS
         char *atom_name;
         atom_name = X11_XGetAtomName(display, req->target);
-        printf("window CLIPBOARD: SelectionRequest (requestor = %ld, target = %ld, mime_type = %s)\n",
+        SDL_Log("window CLIPBOARD: SelectionRequest (requestor = 0x%lx, target = 0x%lx, mime_type = %s)\n",
                req->requestor, req->target, atom_name);
         if (atom_name) {
             X11_XFree(atom_name);
@@ -616,7 +618,7 @@ static void X11_HandleClipboardEvent(SDL_VideoDevice *_this, const XEvent *xeven
     case SelectionNotify:
     {
 #ifdef DEBUG_XEVENTS
-        printf("window CLIPBOARD: SelectionNotify (requestor = %ld, target = %ld)\n",
+        SDL_Log("window CLIPBOARD: SelectionNotify (requestor = 0x%lx, target = 0x%lx)\n",
                xevent->xselection.requestor, xevent->xselection.target);
 #endif
         videodata->selection_waiting = SDL_FALSE;
@@ -629,7 +631,7 @@ static void X11_HandleClipboardEvent(SDL_VideoDevice *_this, const XEvent *xeven
         SDLX11_ClipboardData *clipboard = NULL;
 
 #ifdef DEBUG_XEVENTS
-        printf("window CLIPBOARD: SelectionClear (requestor = %ld, target = %ld)\n",
+        SDL_Log("window CLIPBOARD: SelectionClear (requestor = 0x%lx, target = 0x%lx)\n",
                xevent->xselection.requestor, xevent->xselection.target);
 #endif
 
@@ -751,7 +753,7 @@ void X11_HandleKeyEvent(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_
     SDL_bool handled_by_ime = SDL_FALSE;
 
 #ifdef DEBUG_XEVENTS
-    printf("window %p: %s (X11 keycode = 0x%X)\n", data, (xevent->type == KeyPress ? "KeyPress" : "KeyRelease"), xevent->xkey.keycode);
+    SDL_Log("window 0x%lx %s (X11 keycode = 0x%X)\n", xevent->xany.window, (xevent->type == KeyPress ? "KeyPress" : "KeyRelease"), xevent->xkey.keycode);
 #endif
 #ifdef DEBUG_SCANCODES
     if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN && keycode) {
@@ -777,8 +779,8 @@ void X11_HandleKeyEvent(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_
 
         /* filter events catches XIM events and sends them to the correct handler */
         if (X11_XFilterEvent(xevent, None)) {
-#if 0
-            printf("Filtered event type = %d display = %d window = %d\n",
+#ifdef DEBUG_XEVENTS
+            SDL_Log("Filtered event type = %d display = %p window = 0x%lx\n",
                    xevent->type, xevent->xany.display, xevent->xany.window);
 #endif
             /* Make sure dead key press/release events are sent */
@@ -845,7 +847,7 @@ void X11_HandleButtonPress(SDL_VideoDevice *_this, SDL_WindowData *windowdata, S
     Display *display = videodata->display;
     int xticks = 0, yticks = 0;
 #ifdef DEBUG_XEVENTS
-    printf("window %p: ButtonPress (X11 button = %d)\n", window, button);
+    SDL_Log("window 0x%lx: ButtonPress (X11 button = %d)\n", windowdata->xwindow, button);
 #endif
     if (X11_IsWheelEvent(display, button, &xticks, &yticks)) {
         SDL_SendMouseWheel(0, window, mouseID, (float)-xticks, (float)yticks, SDL_MOUSEWHEEL_NORMAL);
@@ -883,7 +885,7 @@ void X11_HandleButtonRelease(SDL_VideoDevice *_this, SDL_WindowData *windowdata,
     /* The X server sends a Release event for each Press for wheels. Ignore them. */
     int xticks = 0, yticks = 0;
 #ifdef DEBUG_XEVENTS
-    printf("window %p: ButtonRelease (X11 button = %d)\n", data, xevent->xbutton.button);
+    SDL_Log("window 0x%lx: ButtonRelease (X11 button = %d)\n", windowdata->xwindow, button);
 #endif
     if (!X11_IsWheelEvent(display, button, &xticks, &yticks)) {
         if (button > 7) {
@@ -916,7 +918,7 @@ void X11_GetBorderValues(SDL_WindowData *data)
             X11_XFree(property);
 
 #ifdef DEBUG_XEVENTS
-            printf("New _NET_FRAME_EXTENTS: left=%d right=%d, top=%d, bottom=%d\n", data->border_left, data->border_right, data->border_top, data->border_bottom);
+            SDL_Log("New _NET_FRAME_EXTENTS: left=%d right=%d, top=%d, bottom=%d\n", data->border_left, data->border_right, data->border_top, data->border_bottom);
 #endif
         }
     } else {
@@ -939,8 +941,8 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
     /* Key press/release events are filtered in X11_HandleKeyEvent() */
     if (xevent->type != KeyPress && xevent->type != KeyRelease) {
         if (X11_XFilterEvent(xevent, None)) {
-#if 0
-            printf("Filtered event type = %d display = %d window = %d\n",
+#ifdef DEBUG_XEVENTS
+            SDL_Log("Filtered event type = %d display = %p window = 0x%lx\n",
                    xevent->type, xevent->xany.display, xevent->xany.window);
 #endif
             return;
@@ -967,8 +969,8 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
     }
 #endif
 
-#if 0
-    printf("type = %d display = %d window = %d\n",
+#ifdef DEBUG_XEVENTS
+    SDL_Log("X11 event type = %d display = %p window = 0x%lx\n",
            xevent->type, xevent->xany.display, xevent->xany.window);
 #endif
 
@@ -981,7 +983,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
         Atom XA_CLIPBOARD = X11_XInternAtom(display, "CLIPBOARD", 0);
 
 #ifdef DEBUG_XEVENTS
-        printf("window CLIPBOARD: XFixesSelectionNotify (selection = %s)\n",
+        SDL_Log("window CLIPBOARD: XFixesSelectionNotify (selection = %s)\n",
                X11_XGetAtomName(display, ev->selection));
 #endif
 
@@ -1011,7 +1013,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
         /* The window for KeymapNotify, etc events is 0 */
         if (xevent->type == KeymapNotify) {
 #ifdef DEBUG_XEVENTS
-            printf("window %p: KeymapNotify!\n", data);
+            SDL_Log("window 0x%lx: KeymapNotify!\n", xevent->xany.window);
 #endif
             if (SDL_GetKeyboardFocus() != NULL) {
 #ifdef SDL_VIDEO_DRIVER_X11_HAS_XKBLOOKUPKEYSYM
@@ -1032,7 +1034,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
             const int request = xevent->xmapping.request;
 
 #ifdef DEBUG_XEVENTS
-            printf("window %p: MappingNotify!\n", data);
+            SDL_Log("window 0x%lx: MappingNotify!\n", xevent->xany.window);
 #endif
             if ((request == MappingKeyboard) || (request == MappingModifier)) {
                 X11_XRefreshKeyboardMapping(&xevent->xmapping);
@@ -1077,15 +1079,15 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
     {
         SDL_Mouse *mouse = SDL_GetMouse();
 #ifdef DEBUG_XEVENTS
-        printf("window %p: EnterNotify! (%d,%d,%d)\n", data,
+        SDL_Log("window 0x%lx: EnterNotify! (%d,%d,%d)\n", xevent->xany.window,
                xevent->xcrossing.x,
                xevent->xcrossing.y,
                xevent->xcrossing.mode);
         if (xevent->xcrossing.mode == NotifyGrab) {
-            printf("Mode: NotifyGrab\n");
+            SDL_Log("Mode: NotifyGrab\n");
         }
         if (xevent->xcrossing.mode == NotifyUngrab) {
-            printf("Mode: NotifyUngrab\n");
+            SDL_Log("Mode: NotifyUngrab\n");
         }
 #endif
         SDL_SetMouseFocus(data->window);
@@ -1116,15 +1118,15 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
     case LeaveNotify:
     {
 #ifdef DEBUG_XEVENTS
-        printf("window %p: LeaveNotify! (%d,%d,%d)\n", data,
+        SDL_Log("window 0x%lx: LeaveNotify! (%d,%d,%d)\n", xevent->xany.window,
                xevent->xcrossing.x,
                xevent->xcrossing.y,
                xevent->xcrossing.mode);
         if (xevent->xcrossing.mode == NotifyGrab) {
-            printf("Mode: NotifyGrab\n");
+            SDL_Log("Mode: NotifyGrab\n");
         }
         if (xevent->xcrossing.mode == NotifyUngrab) {
-            printf("Mode: NotifyUngrab\n");
+            SDL_Log("Mode: NotifyUngrab\n");
         }
 #endif
         if (!SDL_GetMouse()->relative_mode) {
@@ -1151,19 +1153,19 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
         if (xevent->xfocus.mode == NotifyGrab || xevent->xfocus.mode == NotifyUngrab) {
             /* Someone is handling a global hotkey, ignore it */
 #ifdef DEBUG_XEVENTS
-            printf("window %p: FocusIn (NotifyGrab/NotifyUngrab, ignoring)\n", data);
+            SDL_Log("window 0x%lx: FocusIn (NotifyGrab/NotifyUngrab, ignoring)\n", xevent->xany.window);
 #endif
             break;
         }
 
         if (xevent->xfocus.detail == NotifyInferior || xevent->xfocus.detail == NotifyPointer) {
 #ifdef DEBUG_XEVENTS
-            printf("window %p: FocusIn (NotifyInferior/NotifyPointer, ignoring)\n", data);
+            SDL_Log("window 0x%lx: FocusIn (NotifyInferior/NotifyPointer, ignoring)\n", xevent->xany.window);
 #endif
             break;
         }
 #ifdef DEBUG_XEVENTS
-        printf("window %p: FocusIn!\n", data);
+        SDL_Log("window 0x%lx: FocusIn!\n", xevent->xany.window);
 #endif
         if (!videodata->last_mode_change_deadline) /* no recent mode changes */ {
             data->pending_focus = PENDING_FOCUS_NONE;
@@ -1182,7 +1184,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
         if (xevent->xfocus.mode == NotifyGrab || xevent->xfocus.mode == NotifyUngrab) {
             /* Someone is handling a global hotkey, ignore it */
 #ifdef DEBUG_XEVENTS
-            printf("window %p: FocusOut (NotifyGrab/NotifyUngrab, ignoring)\n", data);
+            SDL_Log("window 0x%lx: FocusOut (NotifyGrab/NotifyUngrab, ignoring)\n", xevent->xany.window);
 #endif
             break;
         }
@@ -1191,12 +1193,12 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
                care about the position of the pointer when the keyboard
                focus changed. */
 #ifdef DEBUG_XEVENTS
-            printf("window %p: FocusOut (NotifyInferior/NotifyPointer, ignoring)\n", data);
+            SDL_Log("window 0x%lx: FocusOut (NotifyInferior/NotifyPointer, ignoring)\n", xevent->xany.window);
 #endif
             break;
         }
 #ifdef DEBUG_XEVENTS
-        printf("window %p: FocusOut!\n", data);
+        SDL_Log("window 0x%lx: FocusOut!\n", xevent->xany.window);
 #endif
         if (!videodata->last_mode_change_deadline) /* no recent mode changes */ {
             data->pending_focus = PENDING_FOCUS_NONE;
@@ -1222,7 +1224,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
         XEvent ev;
 
 #ifdef DEBUG_XEVENTS
-        printf("window %p: UnmapNotify!\n", data);
+        SDL_Log("window 0x%lx: UnmapNotify!\n", xevent->xany.window);
 #endif
 
         if (X11_XCheckIfEvent(display, &ev, &isReparentNotify, (XPointer)&xevent->xunmap)) {
@@ -1243,7 +1245,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
     case MapNotify:
     {
 #ifdef DEBUG_XEVENTS
-        printf("window %p: MapNotify!\n", data);
+        SDL_Log("window 0x%lx: MapNotify!\n", xevent->xany.window);
 #endif
         X11_DispatchMapNotify(data);
 
@@ -1259,7 +1261,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
     case ConfigureNotify:
     {
 #ifdef DEBUG_XEVENTS
-        printf("window %p: ConfigureNotify! (position: %d,%d, size: %dx%d)\n", data,
+        SDL_Log("window 0x%lx: ConfigureNotify! (position: %d,%d, size: %dx%d)\n", xevent->xany.window,
                xevent->xconfigure.x, xevent->xconfigure.y,
                xevent->xconfigure.width, xevent->xconfigure.height);
 #endif
@@ -1328,9 +1330,9 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
             data->xdnd_source = xevent->xclient.data.l[0];
             xdnd_version = (xevent->xclient.data.l[1] >> 24);
 #ifdef DEBUG_XEVENTS
-            printf("XID of source window : %ld\n", data->xdnd_source);
-            printf("Protocol version to use : %d\n", xdnd_version);
-            printf("More then 3 data types : %d\n", (int)use_list);
+            SDL_Log("XID of source window : 0x%lx\n", data->xdnd_source);
+            SDL_Log("Protocol version to use : %d\n", xdnd_version);
+            SDL_Log("More then 3 data types : %d\n", (int)use_list);
 #endif
 
             if (use_list) {
@@ -1351,7 +1353,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
             if (xdnd_version >= 2) {
                 act = xevent->xclient.data.l[4];
             }
-            printf("Action requested by user is : %s\n", X11_XGetAtomName(display, act));
+            SDL_Log("Action requested by user is : %s\n", X11_XGetAtomName(display, act));
 #endif
             {
                 /* Drag and Drop position */
@@ -1408,7 +1410,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
             Window root = DefaultRootWindow(display);
 
 #ifdef DEBUG_XEVENTS
-            printf("window %p: _NET_WM_PING\n", data);
+            SDL_Log("window 0x%lx: _NET_WM_PING\n", xevent->xany.window);
 #endif
             xevent->xclient.window = root;
             X11_XSendEvent(display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, xevent);
@@ -1420,7 +1422,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
                  (xevent->xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
 
 #ifdef DEBUG_XEVENTS
-            printf("window %p: WM_DELETE_WINDOW\n", data);
+            SDL_Log("window 0x%lx: WM_DELETE_WINDOW\n", xevent->xany.window);
 #endif
             SDL_SendWindowEvent(data->window, SDL_EVENT_WINDOW_CLOSE_REQUESTED, 0, 0);
             break;
@@ -1431,7 +1433,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
     case Expose:
     {
 #ifdef DEBUG_XEVENTS
-        printf("window %p: Expose (count = %d)\n", data, xevent->xexpose.count);
+        SDL_Log("window 0x%lx: Expose (count = %d)\n", xevent->xany.window, xevent->xexpose.count);
 #endif
         SDL_SendWindowEvent(data->window, SDL_EVENT_WINDOW_EXPOSED, 0, 0);
     } break;
@@ -1464,7 +1466,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
         SDL_Mouse *mouse = SDL_GetMouse();
         if (!mouse->relative_mode || mouse->relative_mode_warp) {
 #ifdef DEBUG_MOTION
-            printf("window %p: X11 motion: %d,%d\n", data, xevent->xmotion.x, xevent->xmotion.y);
+            SDL_Log("window 0x%lx: X11 motion: %d,%d\n", xevent->xany.window, xevent->xmotion.x, xevent->xmotion.y);
 #endif
 
             X11_ProcessHitTest(_this, data, (float)xevent->xmotion.x, (float)xevent->xmotion.y, SDL_FALSE);
@@ -1503,7 +1505,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
 
         char *name = X11_XGetAtomName(display, xevent->xproperty.atom);
         if (name) {
-            printf("window %p: PropertyNotify: %s %s time=%lu\n", data, name, (xevent->xproperty.state == PropertyDelete) ? "deleted" : "changed", xevent->xproperty.time);
+            SDL_Log("window 0x%lx: PropertyNotify: %s %s time=%lu\n", xevent->xany.window, name, (xevent->xproperty.state == PropertyDelete) ? "deleted" : "changed", xevent->xproperty.time);
             X11_XFree(name);
         }
 
@@ -1512,55 +1514,55 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
             if (real_type == XA_INTEGER) {
                 int *values = (int *)propdata;
 
-                printf("{");
+                SDL_Log("{");
                 for (i = 0; i < items_read; i++) {
-                    printf(" %d", values[i]);
+                    SDL_Log(" %d", values[i]);
                 }
-                printf(" }\n");
+                SDL_Log(" }\n");
             } else if (real_type == XA_CARDINAL) {
                 if (real_format == 32) {
                     Uint32 *values = (Uint32 *)propdata;
 
-                    printf("{");
+                    SDL_Log("{");
                     for (i = 0; i < items_read; i++) {
-                        printf(" %d", values[i]);
+                        SDL_Log(" %d", values[i]);
                     }
-                    printf(" }\n");
+                    SDL_Log(" }\n");
                 } else if (real_format == 16) {
                     Uint16 *values = (Uint16 *)propdata;
 
-                    printf("{");
+                    SDL_Log("{");
                     for (i = 0; i < items_read; i++) {
-                        printf(" %d", values[i]);
+                        SDL_Log(" %d", values[i]);
                     }
-                    printf(" }\n");
+                    SDL_Log(" }\n");
                 } else if (real_format == 8) {
                     Uint8 *values = (Uint8 *)propdata;
 
-                    printf("{");
+                    SDL_Log("{");
                     for (i = 0; i < items_read; i++) {
-                        printf(" %d", values[i]);
+                        SDL_Log(" %d", values[i]);
                     }
-                    printf(" }\n");
+                    SDL_Log(" }\n");
                 }
             } else if (real_type == XA_STRING ||
                        real_type == videodata->UTF8_STRING) {
-                printf("{ \"%s\" }\n", propdata);
+                SDL_Log("{ \"%s\" }\n", propdata);
             } else if (real_type == XA_ATOM) {
                 Atom *atoms = (Atom *)propdata;
 
-                printf("{");
+                SDL_Log("{");
                 for (i = 0; i < items_read; i++) {
                     char *atomname = X11_XGetAtomName(display, atoms[i]);
                     if (atomname) {
-                        printf(" %s", atomname);
+                        SDL_Log(" %s", atomname);
                         X11_XFree(atomname);
                     }
                 }
-                printf(" }\n");
+                SDL_Log(" }\n");
             } else {
                 char *atomname = X11_XGetAtomName(display, real_type);
-                printf("Unknown type: %ld (%s)\n", real_type, atomname ? atomname : "UNKNOWN");
+                SDL_Log("Unknown type: 0x%lx (%s)\n", real_type, atomname ? atomname : "UNKNOWN");
                 if (atomname) {
                     X11_XFree(atomname);
                 }
@@ -1743,7 +1745,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
     {
         Atom target = xevent->xselection.target;
 #ifdef DEBUG_XEVENTS
-        printf("window %p: SelectionNotify (requestor = %ld, target = %ld)\n", data,
+        SDL_Log("window 0x%lx: SelectionNotify (requestor = 0x%lx, target = 0x%lx)\n", xevent->xany.window,
                xevent->xselection.requestor, xevent->xselection.target);
 #endif
         if (target == data->xdnd_req) {
@@ -1791,7 +1793,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
     default:
     {
 #ifdef DEBUG_XEVENTS
-        printf("window %p: Unhandled event %d\n", data, xevent->type);
+        SDL_Log("window 0x%lx: Unhandled event %d\n", xevent->xany.window, xevent->type);
 #endif
     } break;
     }