clipboard: Run a GMainLoop in the clipboard thread

Use the new message source to do the task of processing messages.
This commit is contained in:
Benjamin Otte
2024-10-25 06:40:54 +02:00
parent 517ec1112c
commit 0b62165bf2
2 changed files with 20 additions and 5 deletions

View File

@@ -283,6 +283,7 @@ Otherwise it's similar to how the clipboard works. Only the DnD server
#include "gdkhdataoutputstream-win32.h"
#include "gdkwin32dnd.h"
#include "gdkwin32dnd-private.h"
#include "gdkwin32messagesourceprivate.h"
#include "gdkwin32.h"
#include "gdk/gdkdebugprivate.h"
@@ -1458,8 +1459,14 @@ _gdk_win32_clipboard_thread_main (gpointer data)
MSG msg;
GAsyncQueue *queue = self->clipboard_open_thread_queue;
GAsyncQueue *render_queue = self->clipboard_render_queue;
guint message_source_id;
GMainLoop *loop;
g_assert (self->clipboard_thread_items == NULL);
g_assert (self->clipboard_main_context != NULL);
g_main_context_push_thread_default (self->clipboard_main_context);
message_source_id = gdk_win32_message_source_add (self->clipboard_main_context);
self->clipboard_thread_items = g_new0 (GdkWin32ClipboardThread, 1);
CLIPDROP_CB_THREAD_MEMBER (self, input_queue) = queue;
@@ -1474,17 +1481,18 @@ _gdk_win32_clipboard_thread_main (gpointer data)
return NULL;
}
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
loop = g_main_loop_new (self->clipboard_main_context, TRUE);
g_main_loop_run (loop);
g_main_loop_unref (loop);
/* Just in case, as this should only happen when we shut down */
DestroyWindow (CLIPDROP_CB_THREAD_MEMBER (self, clipboard_hwnd));
CloseHandle (CLIPDROP_CB_THREAD_MEMBER (self, clipboard_hwnd));
g_clear_pointer (&self->clipboard_thread_items, g_free);
g_source_remove (message_source_id);
g_main_context_pop_thread_default (self->clipboard_main_context);
return NULL;
}
@@ -1808,6 +1816,7 @@ gdk_win32_clipdrop_init (GdkWin32Clipdrop *win32_clipdrop)
win32_clipdrop->clipboard_open_thread_queue = g_async_queue_new ();
win32_clipdrop->clipboard_render_queue = g_async_queue_new ();
win32_clipdrop->clipboard_main_context = g_main_context_new ();
win32_clipdrop->clipboard_open_thread = g_thread_new ("GDK Win32 Clipboard Thread",
_gdk_win32_clipboard_thread_main,
win32_clipdrop);

View File

@@ -163,6 +163,12 @@ struct _GdkWin32Clipdrop
*/
GThread *clipboard_open_thread;
/* The main loop run in the clipboard thread.
* When we want to communicate with the thread, we just add
* tasks to it via this context.
*/
GMainContext *clipboard_main_context;
/* Our primary means of communicating with the thread.
* The communication is one-way only - the thread replies
* by just queueing functions to be called in the main