Ignore the coding style change commit during git blame
[xserver.git] / hw / xwin / winmsgwindow.c
blob8792d779cc3077c5330dd5044894364b288e43b8
1 /*
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
13 * Software.
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>
27 #endif
29 #include "win.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
40 static
41 LRESULT CALLBACK
42 winMsgWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
44 #if ENABLE_DEBUG
45 winDebugWin32Message("winMsgWindowProc", hwnd, message, wParam, lParam);
46 #endif
48 switch (message) {
49 case WM_ENDSESSION:
50 if (!wParam)
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);
81 if (iReturn != 0) {
82 ErrorF("winMsgWindowProc - pthread_mutex_lock () failed: %d\n",
83 iReturn);
85 winDebug
86 ("winMsgWindowProc - WM_ENDSESSION termination lock acquired\n");
89 return 0;
92 return DefWindowProc(hwnd, message, wParam, lParam);
95 static HWND
96 winCreateMsgWindow(void)
98 HWND hwndMsg;
100 // register window class
102 WNDCLASSEX wcx;
104 wcx.cbSize = sizeof(WNDCLASSEX);
105 wcx.style = CS_HREDRAW | CS_VREDRAW;
106 wcx.lpfnWndProc = winMsgWindowProc;
107 wcx.cbClsExtra = 0;
108 wcx.cbWndExtra = 0;
109 wcx.hInstance = g_hInstance;
110 wcx.hIcon = NULL;
111 wcx.hCursor = 0;
112 wcx.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
113 wcx.lpszMenuName = NULL;
114 wcx.lpszClassName = WINDOW_CLASS_X_MSG;
115 wcx.hIconSm = NULL;
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
133 if (!hwndMsg) {
134 ErrorF("winCreateMsgWindow - Create msg window failed\n");
135 return NULL;
138 winDebug("winCreateMsgWindow - Created msg window hwnd 0x%p\n", hwndMsg);
140 return hwndMsg;
143 static void *
144 winMsgWindowThreadProc(void *arg)
146 HWND hwndMsg;
148 winDebug("winMsgWindowThreadProc - Hello\n");
150 hwndMsg = winCreateMsgWindow();
151 if (hwndMsg) {
152 MSG msg;
154 /* Pump the msg window message queue */
155 while (GetMessage(&msg, hwndMsg, 0, 0) > 0) {
156 #if ENABLE_DEBUG
157 winDebugWin32Message("winMsgWindowThread", msg.hwnd, msg.message,
158 msg.wParam, msg.lParam);
159 #endif
160 DispatchMessage(&msg);
164 winDebug("winMsgWindowThreadProc - Exit\n");
166 return NULL;
169 Bool
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");
179 return FALSE;
182 return TRUE;