summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon TURNEY <jon.turney@dronecode.org.uk>2011-08-31 21:35:14 +0100
committerJon TURNEY <jon.turney@dronecode.org.uk>2012-03-10 13:26:14 +0000
commit82a670bdad39e170ad0a90b3904c4261c39e07db (patch)
tree825ae828e59e7a345d44a29ff3719131fd944b86
parent22d6b7041608365afd5eb756c2c1a436157ad921 (diff)
Improve WM_ENDSESSION handling using separate messaging window thread
WM_ENDSESSION just calls GiveUp to set the DE_TERMINATE flag. We also need the X server dispatch loop to be unblocked so it can notice that DE_TERMINATE has been set and the server exits cleanly, removing it's lock file and any unix domain socket. The system will terminate the process when the last UI thread in that process returns from processing WM_ENDSESSION for the last top-level window. Since WM_ENDSESSION appears to sent by the system via SendMessage(), which means the wndproc is called to process it directly from inside a blocking syscall in the message thread for that window (the X sever thread), we can't easily terminate the dispatch loop from inside the message processing. So, create a messaging window, a hidden, top-level window, with a separate thread to catch this message, and process it, blocking until the X server dispatch loop exits. Also, notice when this is a shutdown cancel WM_ENDSESSION message and take no action. Signed-off-by: Jon TURNEY <jon.turney@dronecode.org.uk>
-rw-r--r--hw/xwin/InitOutput.c32
-rw-r--r--hw/xwin/Makefile.am1
-rw-r--r--hw/xwin/win.h6
-rw-r--r--hw/xwin/winglobals.c2
-rw-r--r--hw/xwin/winglobals.h2
-rw-r--r--hw/xwin/winmsgwindow.c184
-rw-r--r--hw/xwin/winwindow.h1
-rw-r--r--hw/xwin/winwndproc.c1
8 files changed, 226 insertions, 3 deletions
diff --git a/hw/xwin/InitOutput.c b/hw/xwin/InitOutput.c
index a420d5b6a..ed00c5b3c 100644
--- a/hw/xwin/InitOutput.c
+++ b/hw/xwin/InitOutput.c
@@ -180,9 +180,24 @@ ddxBeforeReset (void)
void
ddxMain(void)
{
+ int iReturn;
+
/* Initialize DDX-specific hooks */
ddxHooks.ddxBeforeReset = ddxBeforeReset;
ddxHooks.ddxPushProviders = ddxPushProviders;
+
+ /* Create & acquire the termination mutex */
+ iReturn = pthread_mutex_init (&g_pmTerminating, NULL);
+ if (iReturn != 0)
+ {
+ ErrorF ("ddxMain - pthread_mutex_init () failed: %d\n", iReturn);
+ }
+
+ iReturn = pthread_mutex_lock (&g_pmTerminating);
+ if (iReturn != 0)
+ {
+ ErrorF ("ddxMain - pthread_mutex_lock () failed: %d\n", iReturn);
+ }
}
/* See Porting Layer Definition - p. 57 */
@@ -241,8 +256,19 @@ ddxGiveUp (enum ExitCode error)
/* Tell Windows that we want to end the app */
PostQuitMessage (0);
-}
+ {
+ winDebug ("ddxGiveUp - Releasing termination mutex\n");
+
+ int iReturn = pthread_mutex_unlock (&g_pmTerminating);
+ if (iReturn != 0)
+ {
+ ErrorF ("winMsgWindowProc - pthread_mutex_unlock () failed: %d\n", iReturn);
+ }
+ }
+
+ winDebug ("ddxGiveUp - End\n");
+}
/* See Porting Layer Definition - p. 57 */
void
@@ -971,6 +997,10 @@ InitOutput (ScreenInfo *screenInfo, int argc, char *argv[])
/* Store the instance handle */
g_hInstance = GetModuleHandle (NULL);
+ /* Create the messaging window */
+ if (serverGeneration == 1)
+ winCreateMsgWindowThread();
+
/* Initialize each screen */
for (i = 0; i < g_iNumScreens; ++i)
{
diff --git a/hw/xwin/Makefile.am b/hw/xwin/Makefile.am
index f593ac12f..96c2b4927 100644
--- a/hw/xwin/Makefile.am
+++ b/hw/xwin/Makefile.am
@@ -90,6 +90,7 @@ SRCS = InitInput.c \
winmonitors.c \
winmouse.c \
winmsg.c \
+ winmsgwindow.c \
winmultiwindowclass.c \
winmultiwindowicons.c \
winprefs.c \
diff --git a/hw/xwin/win.h b/hw/xwin/win.h
index daf364f92..a0631b3af 100644
--- a/hw/xwin/win.h
+++ b/hw/xwin/win.h
@@ -1495,6 +1495,12 @@ void
winGetDisplayName(char *szDisplay, unsigned int screen);
/*
+ * winmsgwindow.c
+ */
+Bool
+winCreateMsgWindowThread(void);
+
+/*
* END DDX and DIX Function Prototypes
*/
diff --git a/hw/xwin/winglobals.c b/hw/xwin/winglobals.c
index 655cdb131..888fcea5c 100644
--- a/hw/xwin/winglobals.c
+++ b/hw/xwin/winglobals.c
@@ -77,7 +77,7 @@ Bool g_fNoHelpMessageBox = FALSE;
Bool g_fSoftwareCursor = FALSE;
Bool g_fSilentDupError = FALSE;
Bool g_fNativeGl = FALSE;
-
+pthread_mutex_t g_pmTerminating = PTHREAD_MUTEX_INITIALIZER;
#ifdef XWIN_CLIPBOARD
/*
diff --git a/hw/xwin/winglobals.h b/hw/xwin/winglobals.h
index 89926796c..6b65667bf 100644
--- a/hw/xwin/winglobals.h
+++ b/hw/xwin/winglobals.h
@@ -85,4 +85,6 @@ extern Bool g_fButton[3];
extern Bool g_fNoConfigureWindow;
#endif
+extern pthread_mutex_t g_pmTerminating;
+
#endif /* WINGLOBALS_H */
diff --git a/hw/xwin/winmsgwindow.c b/hw/xwin/winmsgwindow.c
new file mode 100644
index 000000000..d8af1b5ff
--- /dev/null
+++ b/hw/xwin/winmsgwindow.c
@@ -0,0 +1,184 @@
+/*
+ * Copyright (C) Jon TURNEY 2011
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include "win.h"
+
+/*
+ * This is the messaging window, a hidden top-level window. We never do anything
+ * with it, but other programs may send messages to it.
+ */
+
+/*
+ * winMsgWindowProc - Window procedure for msg window
+ */
+
+static
+LRESULT CALLBACK
+winMsgWindowProc (HWND hwnd, UINT message,
+ WPARAM wParam, LPARAM lParam)
+{
+#if CYGDEBUG
+ winDebugWin32Message("winMsgWindowProc", hwnd, message, wParam, lParam);
+#endif
+
+ switch (message)
+ {
+ case WM_ENDSESSION:
+ if (!wParam)
+ return 0; /* shutdown is being cancelled */
+
+ /*
+ Send a WM_GIVEUP message to the X server thread so it wakes up if
+ blocked in select(), performs GiveUp(), and then notices that GiveUp()
+ has set the DE_TERMINATE flag so exits the msg dispatch loop.
+ */
+ {
+ ScreenPtr pScreen = screenInfo.screens[0];
+ winScreenPriv(pScreen);
+ PostMessage(pScreenPriv->hwndScreen, WM_GIVEUP, 0, 0);
+ }
+
+ /*
+ This process will be terminated by the system almost immediately
+ after the last thread with a message queue returns from processing
+ WM_ENDSESSION, so we cannot rely on any code executing after this
+ message is processed and need to wait here until ddxGiveUp() is called
+ and releases the termination mutex to guarantee that the lock file and
+ unix domain sockets have been removed
+
+ ofc, Microsoft doesn't document this under WM_ENDSESSION, you are supposed
+ to read the source of CRSS to find out how it works :-)
+
+ http://blogs.msdn.com/b/michen/archive/2008/04/04/application-termination-when-user-logs-off.aspx
+ */
+ {
+ int iReturn = pthread_mutex_lock (&g_pmTerminating);
+ if (iReturn != 0)
+ {
+ ErrorF ("winMsgWindowProc - pthread_mutex_lock () failed: %d\n", iReturn);
+ }
+ winDebug ("winMsgWindowProc - WM_ENDSESSION termination lock acquired\n");
+ }
+
+ return 0;
+ }
+
+ return DefWindowProc(hwnd, message, wParam, lParam);
+}
+
+static HWND
+winCreateMsgWindow(void)
+{
+ HWND hwndMsg;
+ wATOM winClass;
+
+ // register window class
+ {
+ WNDCLASSEX wcx ;
+ wcx.cbSize=sizeof(WNDCLASSEX);
+ wcx.style = CS_HREDRAW | CS_VREDRAW;
+ wcx.lpfnWndProc = winMsgWindowProc;
+ wcx.cbClsExtra = 0;
+ wcx.cbWndExtra = 0;
+ wcx.hInstance = g_hInstance;
+ wcx.hIcon = NULL;
+ wcx.hCursor = 0;
+ wcx.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
+ wcx.lpszMenuName = NULL;
+ wcx.lpszClassName = WINDOW_CLASS_X_MSG;
+ wcx.hIconSm = NULL;
+ winClass = RegisterClassEx(&wcx);
+ }
+
+ // Create the msg window.
+ hwndMsg = CreateWindowEx(
+ 0, // no extended styles
+ WINDOW_CLASS_X_MSG, // class name
+ "XWin Msg Window", // window name
+ WS_OVERLAPPEDWINDOW, // overlapped window
+ CW_USEDEFAULT, // default horizontal position
+ CW_USEDEFAULT, // default vertical position
+ CW_USEDEFAULT, // default width
+ CW_USEDEFAULT, // default height
+ (HWND) NULL, // no parent or owner window
+ (HMENU) NULL, // class menu used
+ GetModuleHandle (NULL), // instance handle
+ NULL); // no window creation data
+
+ if (!hwndMsg)
+ {
+ ErrorF("winCreateMsgWindow - Create msg window failed\n");
+ return NULL;
+ }
+
+ winDebug("winCreateMsgWindow - Created msg window hwnd 0x%x\n", hwndMsg);
+
+ return hwndMsg;
+}
+
+static void *
+winMsgWindowThreadProc(void *arg)
+{
+ HWND hwndMsg;
+
+ winDebug ("winMsgWindowThreadProc - Hello\n");
+
+ hwndMsg = winCreateMsgWindow();
+ if (hwndMsg)
+ {
+ MSG msg;
+
+ /* Pump the msg window message queue */
+ while (GetMessage (&msg, hwndMsg, 0, 0) > 0)
+ {
+#if CYGDEBUG
+ winDebugWin32Message("winMsgWindowThread", msg.hwnd, msg.message, msg.wParam, msg.lParam);
+#endif
+ DispatchMessage (&msg);
+ }
+ }
+
+ winDebug ("winMsgWindowThreadProc - Exit\n");
+
+ return NULL;
+}
+
+Bool
+winCreateMsgWindowThread(void)
+{
+ pthread_t ptMsgWindowThreadProc;
+
+ /* Spawn a thread for the msg window */
+ if (pthread_create (&ptMsgWindowThreadProc,
+ NULL,
+ winMsgWindowThreadProc,
+ NULL))
+ {
+ /* Bail if thread creation failed */
+ ErrorF ("winCreateMsgWindow - pthread_create failed.\n");
+ return FALSE;
+ }
+
+ return TRUE;
+}
diff --git a/hw/xwin/winwindow.h b/hw/xwin/winwindow.h
index ce62fba8c..7ac841aa3 100644
--- a/hw/xwin/winwindow.h
+++ b/hw/xwin/winwindow.h
@@ -50,6 +50,7 @@
#define WIN_SCR_PROP "cyg_screen_prop rl"
#define WINDOW_CLASS_X "cygwin/x X rl"
#define WINDOW_CLASS_X_CHILD "cygwin/x X child"
+#define WINDOW_CLASS_X_MSG "cygwin/x X msg"
#define WINDOW_TITLE_X PROJECT_NAME " X"
#define WIN_WINDOW_PROP "cyg_window_prop_rl"
#ifdef HAS_DEVWINDOWS
diff --git a/hw/xwin/winwndproc.c b/hw/xwin/winwndproc.c
index 88b506891..37fe2fbee 100644
--- a/hw/xwin/winwndproc.c
+++ b/hw/xwin/winwndproc.c
@@ -1257,7 +1257,6 @@ winWindowProc (HWND hwnd, UINT message,
}
break;
- case WM_ENDSESSION:
case WM_GIVEUP:
/* Tell X that we are giving up */
#ifdef XWIN_MULTIWINDOW