1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) Jon TURNEY 2011
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun * Software.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*4882a593Smuzhiyun * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*4882a593Smuzhiyun * DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun */
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #ifdef HAVE_XWIN_CONFIG_H
26*4882a593Smuzhiyun #include <xwin-config.h>
27*4882a593Smuzhiyun #endif
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "win.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun * This is the messaging window, a hidden top-level window. We never do anything
33*4882a593Smuzhiyun * with it, but other programs may send messages to it.
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun /*
37*4882a593Smuzhiyun * winMsgWindowProc - Window procedure for msg window
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun static
41*4882a593Smuzhiyun LRESULT CALLBACK
winMsgWindowProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)42*4882a593Smuzhiyun winMsgWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun #if CYGDEBUG
45*4882a593Smuzhiyun winDebugWin32Message("winMsgWindowProc", hwnd, message, wParam, lParam);
46*4882a593Smuzhiyun #endif
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun switch (message) {
49*4882a593Smuzhiyun case WM_ENDSESSION:
50*4882a593Smuzhiyun if (!wParam)
51*4882a593Smuzhiyun return 0; /* shutdown is being cancelled */
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun Send a WM_GIVEUP message to the X server thread so it wakes up if
55*4882a593Smuzhiyun blocked in select(), performs GiveUp(), and then notices that GiveUp()
56*4882a593Smuzhiyun has set the DE_TERMINATE flag so exits the msg dispatch loop.
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun ScreenPtr pScreen = screenInfo.screens[0];
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun winScreenPriv(pScreen);
62*4882a593Smuzhiyun PostMessage(pScreenPriv->hwndScreen, WM_GIVEUP, 0, 0);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun This process will be terminated by the system almost immediately
67*4882a593Smuzhiyun after the last thread with a message queue returns from processing
68*4882a593Smuzhiyun WM_ENDSESSION, so we cannot rely on any code executing after this
69*4882a593Smuzhiyun message is processed and need to wait here until ddxGiveUp() is called
70*4882a593Smuzhiyun and releases the termination mutex to guarantee that the lock file and
71*4882a593Smuzhiyun unix domain sockets have been removed
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun ofc, Microsoft doesn't document this under WM_ENDSESSION, you are supposed
74*4882a593Smuzhiyun to read the source of CRSS to find out how it works :-)
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun http://blogs.msdn.com/b/michen/archive/2008/04/04/application-termination-when-user-logs-off.aspx
77*4882a593Smuzhiyun */
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun int iReturn = pthread_mutex_lock(&g_pmTerminating);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun if (iReturn != 0) {
82*4882a593Smuzhiyun ErrorF("winMsgWindowProc - pthread_mutex_lock () failed: %d\n",
83*4882a593Smuzhiyun iReturn);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun winDebug
86*4882a593Smuzhiyun ("winMsgWindowProc - WM_ENDSESSION termination lock acquired\n");
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun return DefWindowProc(hwnd, message, wParam, lParam);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun static HWND
winCreateMsgWindow(void)96*4882a593Smuzhiyun winCreateMsgWindow(void)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun HWND hwndMsg;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun // register window class
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun WNDCLASSEX wcx;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun wcx.cbSize = sizeof(WNDCLASSEX);
105*4882a593Smuzhiyun wcx.style = CS_HREDRAW | CS_VREDRAW;
106*4882a593Smuzhiyun wcx.lpfnWndProc = winMsgWindowProc;
107*4882a593Smuzhiyun wcx.cbClsExtra = 0;
108*4882a593Smuzhiyun wcx.cbWndExtra = 0;
109*4882a593Smuzhiyun wcx.hInstance = g_hInstance;
110*4882a593Smuzhiyun wcx.hIcon = NULL;
111*4882a593Smuzhiyun wcx.hCursor = 0;
112*4882a593Smuzhiyun wcx.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
113*4882a593Smuzhiyun wcx.lpszMenuName = NULL;
114*4882a593Smuzhiyun wcx.lpszClassName = WINDOW_CLASS_X_MSG;
115*4882a593Smuzhiyun wcx.hIconSm = NULL;
116*4882a593Smuzhiyun RegisterClassEx(&wcx);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun // Create the msg window.
120*4882a593Smuzhiyun hwndMsg = CreateWindowEx(0, // no extended styles
121*4882a593Smuzhiyun WINDOW_CLASS_X_MSG, // class name
122*4882a593Smuzhiyun "XWin Msg Window", // window name
123*4882a593Smuzhiyun WS_OVERLAPPEDWINDOW, // overlapped window
124*4882a593Smuzhiyun CW_USEDEFAULT, // default horizontal position
125*4882a593Smuzhiyun CW_USEDEFAULT, // default vertical position
126*4882a593Smuzhiyun CW_USEDEFAULT, // default width
127*4882a593Smuzhiyun CW_USEDEFAULT, // default height
128*4882a593Smuzhiyun (HWND) NULL, // no parent or owner window
129*4882a593Smuzhiyun (HMENU) NULL, // class menu used
130*4882a593Smuzhiyun GetModuleHandle(NULL), // instance handle
131*4882a593Smuzhiyun NULL); // no window creation data
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (!hwndMsg) {
134*4882a593Smuzhiyun ErrorF("winCreateMsgWindow - Create msg window failed\n");
135*4882a593Smuzhiyun return NULL;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun winDebug("winCreateMsgWindow - Created msg window hwnd 0x%p\n", hwndMsg);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun return hwndMsg;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun static void *
winMsgWindowThreadProc(void * arg)144*4882a593Smuzhiyun winMsgWindowThreadProc(void *arg)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun HWND hwndMsg;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun winDebug("winMsgWindowThreadProc - Hello\n");
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun hwndMsg = winCreateMsgWindow();
151*4882a593Smuzhiyun if (hwndMsg) {
152*4882a593Smuzhiyun MSG msg;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /* Pump the msg window message queue */
155*4882a593Smuzhiyun while (GetMessage(&msg, hwndMsg, 0, 0) > 0) {
156*4882a593Smuzhiyun #if CYGDEBUG
157*4882a593Smuzhiyun winDebugWin32Message("winMsgWindowThread", msg.hwnd, msg.message,
158*4882a593Smuzhiyun msg.wParam, msg.lParam);
159*4882a593Smuzhiyun #endif
160*4882a593Smuzhiyun DispatchMessage(&msg);
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun winDebug("winMsgWindowThreadProc - Exit\n");
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun return NULL;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun Bool
winCreateMsgWindowThread(void)170*4882a593Smuzhiyun winCreateMsgWindowThread(void)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun pthread_t ptMsgWindowThreadProc;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* Spawn a thread for the msg window */
175*4882a593Smuzhiyun if (pthread_create(&ptMsgWindowThreadProc,
176*4882a593Smuzhiyun NULL, winMsgWindowThreadProc, NULL)) {
177*4882a593Smuzhiyun /* Bail if thread creation failed */
178*4882a593Smuzhiyun ErrorF("winCreateMsgWindow - pthread_create failed.\n");
179*4882a593Smuzhiyun return FALSE;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun return TRUE;
183*4882a593Smuzhiyun }
184