SDL: video: cache some more atoms under X11

From 801870c8cf041a9c00fa84a1a278eb0eaffccee8 Mon Sep 17 00:00:00 2001
From: David Fort <[EMAIL REDACTED]>
Date: Mon, 30 Sep 2024 16:42:38 +0200
Subject: [PATCH] video: cache some more atoms under X11

This patch adds the caching of some atoms used by the clipboard.
---
 src/video/x11/SDL_x11clipboard.c | 19 ++++---------------
 src/video/x11/SDL_x11events.c    |  5 +----
 src/video/x11/SDL_x11video.c     |  3 +++
 src/video/x11/SDL_x11video.h     |  3 +++
 4 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/src/video/x11/SDL_x11clipboard.c b/src/video/x11/SDL_x11clipboard.c
index 93b0debe345a5..3b7332b53094b 100644
--- a/src/video/x11/SDL_x11clipboard.c
+++ b/src/video/x11/SDL_x11clipboard.c
@@ -174,7 +174,6 @@ static void *GetSelectionData(SDL_VideoDevice *_this, Atom selection_type,
     unsigned char *src = NULL;
     bool incr_success = false;
     Atom XA_MIME = X11_XInternAtom(display, mime_type, False);
-    Atom XA_INCR = X11_XInternAtom(display, "INCR", False);
 
     *length = 0;
 
@@ -199,7 +198,7 @@ static void *GetSelectionData(SDL_VideoDevice *_this, Atom selection_type,
     } else {
         // Request that the selection owner copy the data to our window
         owner = window;
-        selection = X11_XInternAtom(display, "SDL_SELECTION", False);
+        selection = videodata->SDL_SELECTION;
         X11_XConvertSelection(display, selection_type, XA_MIME, selection, owner,
                               CurrentTime);
 
@@ -213,7 +212,7 @@ static void *GetSelectionData(SDL_VideoDevice *_this, Atom selection_type,
             if (seln_type == XA_MIME) {
                 *length = (size_t)count;
                 data = CloneDataBuffer(src, count);
-            } else if (seln_type == XA_INCR) {
+            } else if (seln_type == videodata->INCR) {
                 while (1) {
                     // Only delete the property after being done with the previous "chunk".
                     X11_XDeleteProperty(display, owner, selection);
@@ -268,23 +267,13 @@ const char **X11_GetTextMimeTypes(SDL_VideoDevice *_this, size_t *num_mime_types
 bool X11_SetClipboardData(SDL_VideoDevice *_this)
 {
     SDL_VideoData *videodata = _this->internal;
-    Atom XA_CLIPBOARD = X11_XInternAtom(videodata->display, "CLIPBOARD", 0);
-    if (XA_CLIPBOARD == None) {
-        return SDL_SetError("Couldn't access X clipboard");
-    }
-    return SetSelectionData(_this, XA_CLIPBOARD, _this->clipboard_callback, _this->clipboard_userdata, (const char **)_this->clipboard_mime_types, _this->num_clipboard_mime_types, _this->clipboard_sequence);
+    return SetSelectionData(_this, videodata->CLIPBOARD, _this->clipboard_callback, _this->clipboard_userdata, (const char **)_this->clipboard_mime_types, _this->num_clipboard_mime_types, _this->clipboard_sequence);
 }
 
 void *X11_GetClipboardData(SDL_VideoDevice *_this, const char *mime_type, size_t *length)
 {
     SDL_VideoData *videodata = _this->internal;
-    Atom XA_CLIPBOARD = X11_XInternAtom(videodata->display, "CLIPBOARD", 0);
-    if (XA_CLIPBOARD == None) {
-        SDL_SetError("Couldn't access X clipboard");
-        *length = 0;
-        return NULL;
-    }
-    return GetSelectionData(_this, XA_CLIPBOARD, mime_type, length);
+    return GetSelectionData(_this, videodata->CLIPBOARD, mime_type, length);
 }
 
 bool X11_HasClipboardData(SDL_VideoDevice *_this, const char *mime_type)
diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c
index 66f0401e3611c..73454e681998f 100644
--- a/src/video/x11/SDL_x11events.c
+++ b/src/video/x11/SDL_x11events.c
@@ -989,16 +989,13 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
         xevent->type == X11_GetXFixesSelectionNotifyEvent()) {
         XFixesSelectionNotifyEvent *ev = (XFixesSelectionNotifyEvent *)xevent;
 
-        // !!! FIXME: cache atoms
-        Atom XA_CLIPBOARD = X11_XInternAtom(display, "CLIPBOARD", 0);
-
 #ifdef DEBUG_XEVENTS
         SDL_Log("window CLIPBOARD: XFixesSelectionNotify (selection = %s)\n",
                X11_XGetAtomName(display, ev->selection));
 #endif
 
         if (ev->selection == XA_PRIMARY ||
-            (XA_CLIPBOARD != None && ev->selection == XA_CLIPBOARD)) {
+            (videodata->CLIPBOARD != None && ev->selection == videodata->CLIPBOARD)) {
             SDL_SendClipboardUpdate();
             return;
         }
diff --git a/src/video/x11/SDL_x11video.c b/src/video/x11/SDL_x11video.c
index 04a811de74b92..ffe94f2c799e0 100644
--- a/src/video/x11/SDL_x11video.c
+++ b/src/video/x11/SDL_x11video.c
@@ -417,6 +417,9 @@ static bool X11_VideoInit(SDL_VideoDevice *_this)
     GET_ATOM(_SDL_WAKEUP);
     GET_ATOM(UTF8_STRING);
     GET_ATOM(PRIMARY);
+    GET_ATOM(CLIPBOARD);
+    GET_ATOM(INCR);
+    GET_ATOM(SDL_SELECTION);
     GET_ATOM(XdndEnter);
     GET_ATOM(XdndPosition);
     GET_ATOM(XdndStatus);
diff --git a/src/video/x11/SDL_x11video.h b/src/video/x11/SDL_x11video.h
index e05d454ecb960..0bce790c36dac 100644
--- a/src/video/x11/SDL_x11video.h
+++ b/src/video/x11/SDL_x11video.h
@@ -94,6 +94,9 @@ struct SDL_VideoData
     Atom _SDL_WAKEUP;
     Atom UTF8_STRING;
     Atom PRIMARY;
+    Atom CLIPBOARD;
+    Atom INCR;
+    Atom SDL_SELECTION;
     Atom XdndEnter;
     Atom XdndPosition;
     Atom XdndStatus;