aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/hw/xwin/winmsgwindow.c
blob: e48a46af711f91b83e95e8ecc913e56bfd9ce901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * 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.
 *
 */

#ifdef HAVE_XWIN_CONFIG_H
#include <xwin-config.h>
#endif
#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;
}