winmsgwindow.c (6019B)
1 /* 2 * Copyright (C) Jon TURNEY 2011 3 * 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: 10 * 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. 14 * 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. 22 * 23 */ 24 25 #ifdef HAVE_XWIN_CONFIG_H 26 #include <xwin-config.h> 27 #endif 28 29 #include "win.h" 30 31 /* 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. 34 */ 35 36 /* 37 * winMsgWindowProc - Window procedure for msg window 38 */ 39 40 static 41 LRESULT CALLBACK 42 winMsgWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 43 { 44 #if CYGDEBUG 45 winDebugWin32Message("winMsgWindowProc", hwnd, message, wParam, lParam); 46 #endif 47 48 switch (message) { 49 case WM_ENDSESSION: 50 if (!wParam) 51 return 0; /* shutdown is being cancelled */ 52 53 /* 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. 57 */ 58 { 59 ScreenPtr pScreen = screenInfo.screens[0]; 60 61 winScreenPriv(pScreen); 62 PostMessage(pScreenPriv->hwndScreen, WM_GIVEUP, 0, 0); 63 } 64 65 /* 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 72 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 :-) 75 76 http://blogs.msdn.com/b/michen/archive/2008/04/04/application-termination-when-user-logs-off.aspx 77 */ 78 { 79 int iReturn = pthread_mutex_lock(&g_pmTerminating); 80 81 if (iReturn != 0) { 82 ErrorF("winMsgWindowProc - pthread_mutex_lock () failed: %d\n", 83 iReturn); 84 } 85 winDebug 86 ("winMsgWindowProc - WM_ENDSESSION termination lock acquired\n"); 87 } 88 89 return 0; 90 } 91 92 return DefWindowProc(hwnd, message, wParam, lParam); 93 } 94 95 static HWND 96 winCreateMsgWindow(void) 97 { 98 HWND hwndMsg; 99 100 // register window class 101 { 102 WNDCLASSEX wcx; 103 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); 117 } 118 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 132 133 if (!hwndMsg) { 134 ErrorF("winCreateMsgWindow - Create msg window failed\n"); 135 return NULL; 136 } 137 138 winDebug("winCreateMsgWindow - Created msg window hwnd 0x%p\n", hwndMsg); 139 140 return hwndMsg; 141 } 142 143 static void * 144 winMsgWindowThreadProc(void *arg) 145 { 146 HWND hwndMsg; 147 148 winDebug("winMsgWindowThreadProc - Hello\n"); 149 150 hwndMsg = winCreateMsgWindow(); 151 if (hwndMsg) { 152 MSG msg; 153 154 /* Pump the msg window message queue */ 155 while (GetMessage(&msg, hwndMsg, 0, 0) > 0) { 156 #if CYGDEBUG 157 winDebugWin32Message("winMsgWindowThread", msg.hwnd, msg.message, 158 msg.wParam, msg.lParam); 159 #endif 160 DispatchMessage(&msg); 161 } 162 } 163 164 winDebug("winMsgWindowThreadProc - Exit\n"); 165 166 return NULL; 167 } 168 169 Bool 170 winCreateMsgWindowThread(void) 171 { 172 pthread_t ptMsgWindowThreadProc; 173 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; 180 } 181 182 return TRUE; 183 }