2 * Copyright (C) Jon TURNEY 2011
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #ifdef HAVE_XWIN_CONFIG_H
26 #include <xwin-config.h>
32 * This is the messaging window, a hidden top-level window. We never do anything
33 * with it, but other programs may send messages to it.
37 * winMsgWindowProc - Window procedure for msg window
42 winMsgWindowProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
45 winDebugWin32Message("winMsgWindowProc", hwnd
, message
, wParam
, lParam
);
51 return 0; /* shutdown is being cancelled */
54 Send a WM_GIVEUP message to the X server thread so it wakes up if
55 blocked in select(), performs GiveUp(), and then notices that GiveUp()
56 has set the DE_TERMINATE flag so exits the msg dispatch loop.
59 ScreenPtr pScreen
= screenInfo
.screens
[0];
61 winScreenPriv(pScreen
);
62 PostMessage(pScreenPriv
->hwndScreen
, WM_GIVEUP
, 0, 0);
66 This process will be terminated by the system almost immediately
67 after the last thread with a message queue returns from processing
68 WM_ENDSESSION, so we cannot rely on any code executing after this
69 message is processed and need to wait here until ddxGiveUp() is called
70 and releases the termination mutex to guarantee that the lock file and
71 unix domain sockets have been removed
73 ofc, Microsoft doesn't document this under WM_ENDSESSION, you are supposed
74 to read the source of CRSS to find out how it works :-)
76 http://blogs.msdn.com/b/michen/archive/2008/04/04/application-termination-when-user-logs-off.aspx
79 int iReturn
= pthread_mutex_lock(&g_pmTerminating
);
82 ErrorF("winMsgWindowProc - pthread_mutex_lock () failed: %d\n",
86 ("winMsgWindowProc - WM_ENDSESSION termination lock acquired\n");
92 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
96 winCreateMsgWindow(void)
100 // register window class
104 wcx
.cbSize
= sizeof(WNDCLASSEX
);
105 wcx
.style
= CS_HREDRAW
| CS_VREDRAW
;
106 wcx
.lpfnWndProc
= winMsgWindowProc
;
109 wcx
.hInstance
= g_hInstance
;
112 wcx
.hbrBackground
= (HBRUSH
) GetStockObject(WHITE_BRUSH
);
113 wcx
.lpszMenuName
= NULL
;
114 wcx
.lpszClassName
= WINDOW_CLASS_X_MSG
;
116 RegisterClassEx(&wcx
);
119 // Create the msg window.
120 hwndMsg
= CreateWindowEx(0, // no extended styles
121 WINDOW_CLASS_X_MSG
, // class name
122 "XWin Msg Window", // window name
123 WS_OVERLAPPEDWINDOW
, // overlapped window
124 CW_USEDEFAULT
, // default horizontal position
125 CW_USEDEFAULT
, // default vertical position
126 CW_USEDEFAULT
, // default width
127 CW_USEDEFAULT
, // default height
128 (HWND
) NULL
, // no parent or owner window
129 (HMENU
) NULL
, // class menu used
130 GetModuleHandle(NULL
), // instance handle
131 NULL
); // no window creation data
134 ErrorF("winCreateMsgWindow - Create msg window failed\n");
138 winDebug("winCreateMsgWindow - Created msg window hwnd 0x%p\n", hwndMsg
);
144 winMsgWindowThreadProc(void *arg
)
148 winDebug("winMsgWindowThreadProc - Hello\n");
150 hwndMsg
= winCreateMsgWindow();
154 /* Pump the msg window message queue */
155 while (GetMessage(&msg
, hwndMsg
, 0, 0) > 0) {
157 winDebugWin32Message("winMsgWindowThread", msg
.hwnd
, msg
.message
,
158 msg
.wParam
, msg
.lParam
);
160 DispatchMessage(&msg
);
164 winDebug("winMsgWindowThreadProc - Exit\n");
170 winCreateMsgWindowThread(void)
172 pthread_t ptMsgWindowThreadProc
;
174 /* Spawn a thread for the msg window */
175 if (pthread_create(&ptMsgWindowThreadProc
,
176 NULL
, winMsgWindowThreadProc
, NULL
)) {
177 /* Bail if thread creation failed */
178 ErrorF("winCreateMsgWindow - pthread_create failed.\n");