winmultiwindowwm.c 53.3 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1
2
/*
 *Copyright (C) 1994-2000 The XFree86 Project, Inc. All Rights Reserved.
3
 *Copyright (C) Colin Harrison 2005-2009
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
 *
 *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 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 XFREE86 PROJECT 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.
 *
 *Except as contained in this notice, the name of the XFree86 Project
 *shall not be used in advertising or otherwise to promote the sale, use
 *or other dealings in this Software without prior written authorization
 *from the XFree86 Project.
 *
 * Authors:	Kensuke Matsuzaki
30
 *              Colin Harrison
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
31
32
33
 */

/* X headers */
34
35
36
#ifdef HAVE_XWIN_CONFIG_H
#include <xwin-config.h>
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
37
38
39
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
40
#ifdef __CYGWIN__
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
41
#include <sys/select.h>
42
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
43
44
#include <fcntl.h>
#include <setjmp.h>
45
#define HANDLE void *
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
46
#include <pthread.h>
47
#undef HANDLE
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
48
49
50
51
52
53
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xlocale.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
54
#include <X11/cursorfont.h>
Jon Turney's avatar
Jon Turney committed
55
#include <X11/Xwindows.h>
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
56
57
58

/* Local headers */
#include "winwindow.h"
59
60
61
62
63
#include "winprefs.h"
#include "window.h"
#include "pixmapstr.h"
#include "windowstr.h"

64
#ifdef XWIN_MULTIWINDOWEXTWM
65
#include <X11/extensions/windowswmstr.h>
66
67
68
69
#else
/* We need the native HWND atom for intWM, so for consistency use the
   same name as extWM would if we were building with enabled... */
#define WINDOWSWM_NATIVE_HWND "_WINDOWSWM_NATIVE_HWND"
70
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
71

72
extern void winDebug(const char *format, ...);
73
74
extern void winReshapeMultiWindow(WindowPtr pWin);
extern void winUpdateRgnMultiWindow(WindowPtr pWin);
75

Kensuke Matsuzaki's avatar
Kensuke Matsuzaki committed
76
#ifndef CYGDEBUG
77
78
#define CYGDEBUG NO
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
79
80
81
82
83
84
85

/*
 * Constant defines
 */

#define WIN_CONNECT_RETRIES	5
#define WIN_CONNECT_DELAY	5
86
#ifdef HAS_DEVWINDOWS
87
#define WIN_MSG_QUEUE_FNAME	"/dev/windows"
88
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
89
90
91
92
93
94
95
96
#define WIN_JMP_OKAY		0
#define WIN_JMP_ERROR_IO	2

/*
 * Local structures
 */

typedef struct _WMMsgNodeRec {
97
98
    winWMMessageRec msg;
    struct _WMMsgNodeRec *pNext;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
99
100
101
} WMMsgNodeRec, *WMMsgNodePtr;

typedef struct _WMMsgQueueRec {
102
103
104
105
106
    struct _WMMsgNodeRec *pHead;
    struct _WMMsgNodeRec *pTail;
    pthread_mutex_t pmMutex;
    pthread_cond_t pcNotEmpty;
    int nQueueSize;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
107
108
109
} WMMsgQueueRec, *WMMsgQueuePtr;

typedef struct _WMInfo {
110
111
112
113
114
115
    Display *pDisplay;
    WMMsgQueueRec wmMsgQueue;
    Atom atmWmProtos;
    Atom atmWmDelete;
    Atom atmPrivMap;
    Bool fAllowOtherWM;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
116
117
118
} WMInfoRec, *WMInfoPtr;

typedef struct _WMProcArgRec {
119
120
121
    DWORD dwScreen;
    WMInfoPtr pWMInfo;
    pthread_mutex_t *ppmServerStarted;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
122
123
} WMProcArgRec, *WMProcArgPtr;

124
typedef struct _XMsgProcArgRec {
125
126
127
128
129
    Display *pDisplay;
    DWORD dwScreen;
    WMInfoPtr pWMInfo;
    pthread_mutex_t *ppmServerStarted;
    HWND hwndScreen;
130
131
} XMsgProcArgRec, *XMsgProcArgPtr;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
132
133
134
135
136
/*
 * Prototypes for local functions
 */

static void
137
 PushMessage(WMMsgQueuePtr pQueue, WMMsgNodePtr pNode);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
138

139
static WMMsgNodePtr PopMessage(WMMsgQueuePtr pQueue, WMInfoPtr pWMInfo);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
140
141

static Bool
142
 InitQueue(WMMsgQueuePtr pQueue);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
143
144

static void
145
 GetWindowName(Display * pDpy, Window iWin, char **ppWindowName);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
146
147

static int
148
 SendXMessage(Display * pDisplay, Window iWin, Atom atmType, long nData);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
149

150
static void
151
 UpdateName(WMInfoPtr pWMInfo, Window iWindow);
152

153
static void *winMultiWindowWMProc(void *pArg);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
154
155

static int
156
 winMultiWindowWMErrorHandler(Display * pDisplay, XErrorEvent * pErr);
157
158

static int
159
 winMultiWindowWMIOErrorHandler(Display * pDisplay);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
160

161
static void *winMultiWindowXMsgProc(void *pArg);
162

163
static int
164
 winMultiWindowXMsgProcErrorHandler(Display * pDisplay, XErrorEvent * pErr);
165
166

static int
167
 winMultiWindowXMsgProcIOErrorHandler(Display * pDisplay);
168
169

static int
170
 winRedirectErrorHandler(Display * pDisplay, XErrorEvent * pErr);
171

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
172
static void
173
 winInitMultiWindowWM(WMInfoPtr pWMInfo, WMProcArgPtr pProcArg);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
174

175
176
#if 0
static void
177
 PreserveWin32Stack(WMInfoPtr pWMInfo, Window iWindow, UINT direction);
178
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
179

Kensuke Matsuzaki's avatar
Kensuke Matsuzaki committed
180
static Bool
181
182
183

CheckAnotherWindowManager(Display * pDisplay, DWORD dwScreen,
                          Bool fAllowOtherWM);
Kensuke Matsuzaki's avatar
Kensuke Matsuzaki committed
184

185
static void
186
 winApplyHints(Display * pDisplay, Window iWindow, HWND hWnd, HWND * zstyle);
187
188

void
189
 winUpdateWindowPosition(HWND hWnd, HWND * zstyle);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
190
191
192
193
194

/*
 * Local globals
 */

195
static jmp_buf g_jmpWMEntry;
196
197
static XIOErrorHandler g_winMultiWindowWMOldIOErrorHandler;
static pthread_t g_winMultiWindowWMThread;
198
static jmp_buf g_jmpXMsgProcEntry;
199
200
static XIOErrorHandler g_winMultiWindowXMsgProcOldIOErrorHandler;
static pthread_t g_winMultiWindowXMsgProcThread;
201
202
203
static Bool g_shutdown = FALSE;
static Bool redirectError = FALSE;
static Bool g_fAnotherWMRunning = FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
204
205
206
207
208
209

/*
 * PushMessage - Push a message onto the queue
 */

static void
210
PushMessage(WMMsgQueuePtr pQueue, WMMsgNodePtr pNode)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
211
212
{

213
214
    /* Lock the queue mutex */
    pthread_mutex_lock(&pQueue->pmMutex);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
215

216
217
218
219
    pNode->pNext = NULL;

    if (pQueue->pTail != NULL) {
        pQueue->pTail->pNext = pNode;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
220
    }
221
    pQueue->pTail = pNode;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
222

223
224
225
    if (pQueue->pHead == NULL) {
        pQueue->pHead = pNode;
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
226
227

#if 0
228
    switch (pNode->msg.msg) {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
229
    case WM_WM_MOVE:
230
231
        ErrorF("\tWM_WM_MOVE\n");
        break;
Kensuke Matsuzaki's avatar
Kensuke Matsuzaki committed
232
    case WM_WM_SIZE:
233
234
        ErrorF("\tWM_WM_SIZE\n");
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
235
    case WM_WM_RAISE:
236
237
        ErrorF("\tWM_WM_RAISE\n");
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
238
    case WM_WM_LOWER:
239
240
        ErrorF("\tWM_WM_LOWER\n");
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
241
    case WM_WM_MAP:
242
243
        ErrorF("\tWM_WM_MAP\n");
        break;
244
    case WM_WM_MAP2:
245
246
        ErrorF("\tWM_WM_MAP2\n");
        break;
247
    case WM_WM_MAP3:
248
249
        ErrorF("\tWM_WM_MAP3\n");
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
250
    case WM_WM_UNMAP:
251
252
        ErrorF("\tWM_WM_UNMAP\n");
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
253
    case WM_WM_KILL:
254
255
        ErrorF("\tWM_WM_KILL\n");
        break;
Kensuke Matsuzaki's avatar
Kensuke Matsuzaki committed
256
    case WM_WM_ACTIVATE:
257
258
        ErrorF("\tWM_WM_ACTIVATE\n");
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
259
    default:
260
261
        ErrorF("\tUnknown Message.\n");
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
262
263
264
    }
#endif

265
266
    /* Increase the count of elements in the queue by one */
    ++(pQueue->nQueueSize);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
267

268
269
    /* Release the queue mutex */
    pthread_mutex_unlock(&pQueue->pmMutex);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
270

271
272
    /* Signal that the queue is not empty */
    pthread_cond_signal(&pQueue->pcNotEmpty);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
273
274
}

275
#if CYGMULTIWINDOW_DEBUG
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
276
277
278
279
280
/*
 * QueueSize - Return the size of the queue
 */

static int
281
QueueSize(WMMsgQueuePtr pQueue)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
282
{
283
284
285
286
287
288
289
290
    WMMsgNodePtr pNode;
    int nSize = 0;

    /* Loop through all elements in the queue */
    for (pNode = pQueue->pHead; pNode != NULL; pNode = pNode->pNext)
        ++nSize;

    return nSize;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
291
292
293
294
}
#endif

/*
295
 * PopMessage - Pop a message from the queue
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
296
297
298
 */

static WMMsgNodePtr
299
PopMessage(WMMsgQueuePtr pQueue, WMInfoPtr pWMInfo)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
300
{
301
    WMMsgNodePtr pNode;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
302

303
304
    /* Lock the queue mutex */
    pthread_mutex_lock(&pQueue->pmMutex);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
305

306
307
308
    /* Wait for --- */
    while (pQueue->pHead == NULL) {
        pthread_cond_wait(&pQueue->pcNotEmpty, &pQueue->pmMutex);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
309
    }
310
311
312
313

    pNode = pQueue->pHead;
    if (pQueue->pHead != NULL) {
        pQueue->pHead = pQueue->pHead->pNext;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
314
315
    }

316
317
    if (pQueue->pTail == pNode) {
        pQueue->pTail = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
318
319
    }

320
321
    /* Drop the number of elements in the queue by one */
    --(pQueue->nQueueSize);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
322
323

#if CYGMULTIWINDOW_DEBUG
324
    ErrorF("Queue Size %d %d\n", pQueue->nQueueSize, QueueSize(pQueue));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
325
326
#endif

327
328
    /* Release the queue mutex */
    pthread_mutex_unlock(&pQueue->pmMutex);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
329

330
331
    return pNode;
}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
332
333
334
335
336
337
338

#if 0
/*
 * HaveMessage - 
 */

static Bool
339
HaveMessage(WMMsgQueuePtr pQueue, UINT msg, Window iWindow)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
340
{
341
342
343
344
345
    WMMsgNodePtr pNode;

    for (pNode = pQueue->pHead; pNode != NULL; pNode = pNode->pNext) {
        if (pNode->msg.msg == msg && pNode->msg.iWindow == iWindow)
            return True;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
346
    }
347
348

    return False;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
349
350
351
352
353
354
355
356
}
#endif

/*
 * InitQueue - Initialize the Window Manager message queue
 */

static
357
358
    Bool
InitQueue(WMMsgQueuePtr pQueue)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
359
{
360
361
362
363
    /* Check if the pQueue pointer is NULL */
    if (pQueue == NULL) {
        ErrorF("InitQueue - pQueue is NULL.  Exiting.\n");
        return FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
364
365
    }

366
367
368
    /* Set the head and tail to NULL */
    pQueue->pHead = NULL;
    pQueue->pTail = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
369

370
371
    /* There are no elements initially */
    pQueue->nQueueSize = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
372
373

#if CYGMULTIWINDOW_DEBUG
374
375
    winDebug("InitQueue - Queue Size %d %d\n", pQueue->nQueueSize,
             QueueSize(pQueue));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
376
377
#endif

378
    winDebug("InitQueue - Calling pthread_mutex_init\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
379

380
381
    /* Create synchronization objects */
    pthread_mutex_init(&pQueue->pmMutex, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
382

383
384
    winDebug("InitQueue - pthread_mutex_init returned\n");
    winDebug("InitQueue - Calling pthread_cond_init\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
385

386
    pthread_cond_init(&pQueue->pcNotEmpty, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
387

388
    winDebug("InitQueue - pthread_cond_init returned\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
389

390
    return TRUE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
391
392
}

393
394
395
static
char *
Xutf8TextPropertyToString(Display * pDisplay, XTextProperty * xtp)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
396
{
397
    int nNum;
398
399
    char **ppList;
    char *pszReturnData;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
400

401
402
403
404
    if (Xutf8TextPropertyToTextList(pDisplay, xtp, &ppList, &nNum) >= Success &&
        nNum > 0 && *ppList) {
        int i;
        int iLen = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
405

406
407
408
409
410
411
412
413
414
415
416
417
418
        for (i = 0; i < nNum; i++)
            iLen += strlen(ppList[i]);
        pszReturnData = (char *) malloc(iLen + 1);
        pszReturnData[0] = '\0';
        for (i = 0; i < nNum; i++)
            strcat(pszReturnData, ppList[i]);
        if (ppList)
            XFreeStringList(ppList);
    }
    else {
        pszReturnData = (char *) malloc(1);
        pszReturnData[0] = '\0';
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
419

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
    return pszReturnData;
}

/*
 * GetWindowName - Retrieve the title of an X Window
 */

static void
GetWindowName(Display * pDisplay, Window iWin, char **ppWindowName)
{
    int nResult;
    XTextProperty xtpWindowName;
    char *pszWindowName;

#if CYGMULTIWINDOW_DEBUG
    ErrorF("GetWindowName\n");
#endif

    /* Intialize ppWindowName to NULL */
    *ppWindowName = NULL;

    /* Try to get window name */
    nResult = XGetWMName(pDisplay, iWin, &xtpWindowName);
    if (!nResult || !xtpWindowName.value || !xtpWindowName.nitems) {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
444
#if CYGMULTIWINDOW_DEBUG
445
        ErrorF("GetWindowName - XGetWMName failed.  No name.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
446
#endif
447
448
449
450
451
452
        return;
    }

    pszWindowName = Xutf8TextPropertyToString(pDisplay, &xtpWindowName);
    XFree(xtpWindowName.value);
    *ppWindowName = pszWindowName;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
453
454
455
456
457
458
459
}

/*
 * Send a message to the X server from the WM thread
 */

static int
460
SendXMessage(Display * pDisplay, Window iWin, Atom atmType, long nData)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
461
{
462
463
464
465
466
467
468
469
470
471
472
473
    XEvent e;

    /* Prepare the X event structure */
    e.type = ClientMessage;
    e.xclient.window = iWin;
    e.xclient.message_type = atmType;
    e.xclient.format = 32;
    e.xclient.data.l[0] = nData;
    e.xclient.data.l[1] = CurrentTime;

    /* Send the event to X */
    return XSendEvent(pDisplay, iWin, False, NoEventMask, &e);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
474
475
}

476
/*
477
 * See if we can get the stored HWND for this window...
478
 */
479
480
static HWND
getHwnd(WMInfoPtr pWMInfo, Window iWindow)
481
{
482
483
484
    Atom atmType;
    int fmtRet;
    unsigned long items, remain;
485
486
487
488
489
490
491
492
493
    HWND *retHwnd, hWnd = NULL;

    if (XGetWindowProperty(pWMInfo->pDisplay,
                           iWindow,
                           pWMInfo->atmPrivMap,
                           0,
                           1,
                           False,
                           XA_INTEGER,
494
495
496
497
498
499
500
501
                           &atmType,
                           &fmtRet,
                           &items,
                           &remain, (unsigned char **) &retHwnd) == Success) {
        if (retHwnd) {
            hWnd = *retHwnd;
            XFree(retHwnd);
        }
502
    }
503
504
505

    /* Some sanity checks */
    if (!hWnd)
506
        return NULL;
507
    if (!IsWindow(hWnd))
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
        return NULL;

    return hWnd;
}

/*
 * Updates the name of a HWND according to its X WM_NAME property
 */

static void
UpdateName(WMInfoPtr pWMInfo, Window iWindow)
{
    HWND hWnd;
    XWindowAttributes attr;

    hWnd = getHwnd(pWMInfo, iWindow);
    if (!hWnd)
525
526
        return;

527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
    /* If window isn't override-redirect */
    XGetWindowAttributes(pWMInfo->pDisplay, iWindow, &attr);
    if (!attr.override_redirect) {
        char *pszWindowName;

        /* Get the X windows window name */
        GetWindowName(pWMInfo->pDisplay, iWindow, &pszWindowName);

        if (pszWindowName) {
            /* Convert from UTF-8 to wide char */
            int iLen =
                MultiByteToWideChar(CP_UTF8, 0, pszWindowName, -1, NULL, 0);
            wchar_t *pwszWideWindowName =
                (wchar_t *) malloc(sizeof(wchar_t) * (iLen + 1));
            MultiByteToWideChar(CP_UTF8, 0, pszWindowName, -1,
                                pwszWideWindowName, iLen);

            /* Set the Windows window name */
            SetWindowTextW(hWnd, pwszWideWindowName);
546

547
548
549
            free(pwszWideWindowName);
            free(pszWindowName);
        }
550
551
552
    }
}

553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
 * Updates the icon of a HWND according to its X icon properties
 */

static void
UpdateIcon(WMInfoPtr pWMInfo, Window iWindow)
{
    HWND hWnd;
    HICON hIconNew = NULL;
    XWindowAttributes attr;

    hWnd = getHwnd(pWMInfo, iWindow);
    if (!hWnd)
        return;

    /* If window isn't override-redirect */
    XGetWindowAttributes(pWMInfo->pDisplay, iWindow, &attr);
    if (!attr.override_redirect) {
        XClassHint class_hint = { 0, 0 };
        char *window_name = 0;

        if (XGetClassHint(pWMInfo->pDisplay, iWindow, &class_hint)) {
            XFetchName(pWMInfo->pDisplay, iWindow, &window_name);

            hIconNew =
                (HICON) winOverrideIcon(class_hint.res_name,
                                        class_hint.res_class, window_name);

            if (class_hint.res_name)
                XFree(class_hint.res_name);
            if (class_hint.res_class)
                XFree(class_hint.res_class);
            if (window_name)
                XFree(window_name);
        }
    }

    winUpdateIcon(hWnd, pWMInfo->pDisplay, iWindow, hIconNew);
}

593
594
595
596
597
598
599
600
#if 0
/*
 * Fix up any differences between the X11 and Win32 window stacks
 * starting at the window passed in
 */
static void
PreserveWin32Stack(WMInfoPtr pWMInfo, Window iWindow, UINT direction)
{
601
    HWND hWnd;
602
603
604
605
    DWORD myWinProcID, winProcID;
    Window xWindow;
    WINDOWPLACEMENT wndPlace;

606
    hWnd = getHwnd(pWMInfo, iWindow);
607
608
609
610
    if (!hWnd)
        return;

    GetWindowThreadProcessId(hWnd, &myWinProcID);
611
612
    hWnd = GetNextWindow(hWnd, direction);

613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
    while (hWnd) {
        GetWindowThreadProcessId(hWnd, &winProcID);
        if (winProcID == myWinProcID) {
            wndPlace.length = sizeof(WINDOWPLACEMENT);
            GetWindowPlacement(hWnd, &wndPlace);
            if (!(wndPlace.showCmd == SW_HIDE ||
                  wndPlace.showCmd == SW_MINIMIZE)) {
                xWindow = (Window) GetProp(hWnd, WIN_WID_PROP);
                if (xWindow) {
                    if (direction == GW_HWNDPREV)
                        XRaiseWindow(pWMInfo->pDisplay, xWindow);
                    else
                        XLowerWindow(pWMInfo->pDisplay, xWindow);
                }
            }
        }
        hWnd = GetNextWindow(hWnd, direction);
    }
}
#endif                          /* PreserveWin32Stack */
633

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
634
635
636
637
638
/*
 * winMultiWindowWMProc
 */

static void *
639
winMultiWindowWMProc(void *pArg)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
640
{
641
642
643
644
645
646
    WMProcArgPtr pProcArg = (WMProcArgPtr) pArg;
    WMInfoPtr pWMInfo = pProcArg->pWMInfo;

    /* Initialize the Window Manager */
    winInitMultiWindowWM(pWMInfo, pProcArg);

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
647
#if CYGMULTIWINDOW_DEBUG
648
    ErrorF("winMultiWindowWMProc ()\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
649
650
#endif

651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
    /* Loop until we explicitly break out */
    for (;;) {
        WMMsgNodePtr pNode;

        if (g_fAnotherWMRunning) {      /* Another Window manager exists. */
            Sleep(1000);
            continue;
        }

        /* Pop a message off of our queue */
        pNode = PopMessage(&pWMInfo->wmMsgQueue, pWMInfo);
        if (pNode == NULL) {
            /* Bail if PopMessage returns without a message */
            /* NOTE: Remember that PopMessage is a blocking function. */
            ErrorF("winMultiWindowWMProc - Queue is Empty?  Exiting.\n");
            pthread_exit(NULL);
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
668
669

#if CYGMULTIWINDOW_DEBUG
670
671
        ErrorF("winMultiWindowWMProc - %d ms MSG: %d ID: %d\n",
               GetTickCount(), (int) pNode->msg.msg, (int) pNode->msg.dwID);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
672
#endif
Kensuke Matsuzaki's avatar
Kensuke Matsuzaki committed
673

674
675
        /* Branch on the message type */
        switch (pNode->msg.msg) {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
676
#if 0
677
678
679
        case WM_WM_MOVE:
            ErrorF("\tWM_WM_MOVE\n");
            break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
680

681
682
683
        case WM_WM_SIZE:
            ErrorF("\tWM_WM_SIZE\n");
            break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
684
685
#endif

686
        case WM_WM_RAISE:
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
687
#if CYGMULTIWINDOW_DEBUG
688
            ErrorF("\tWM_WM_RAISE\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
689
#endif
690
691
            /* Raise the window */
            XRaiseWindow(pWMInfo->pDisplay, pNode->msg.iWindow);
692
#if 0
693
            PreserveWin32Stack(pWMInfo, pNode->msg.iWindow, GW_HWNDPREV);
694
#endif
695
            break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
696

697
        case WM_WM_LOWER:
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
698
#if CYGMULTIWINDOW_DEBUG
699
            ErrorF("\tWM_WM_LOWER\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
700
701
#endif

702
703
704
            /* Lower the window */
            XLowerWindow(pWMInfo->pDisplay, pNode->msg.iWindow);
            break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
705

706
        case WM_WM_MAP:
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
707
#if CYGMULTIWINDOW_DEBUG
708
            ErrorF("\tWM_WM_MAP\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
709
#endif
710
711
712
713
714
715
            /* Put a note as to the HWND associated with this Window */
            XChangeProperty(pWMInfo->pDisplay, pNode->msg.iWindow, pWMInfo->atmPrivMap, XA_INTEGER,     //pWMInfo->atmPrivMap,
                            32,
                            PropModeReplace,
                            (unsigned char *) &(pNode->msg.hwndWindow), 1);
            UpdateName(pWMInfo, pNode->msg.iWindow);
716
            UpdateIcon(pWMInfo, pNode->msg.iWindow);
717
718
719
            break;

        case WM_WM_MAP2:
720
#if CYGMULTIWINDOW_DEBUG
721
            ErrorF("\tWM_WM_MAP2\n");
722
#endif
723
724
725
726
727
728
729
            XChangeProperty(pWMInfo->pDisplay, pNode->msg.iWindow, pWMInfo->atmPrivMap, XA_INTEGER,     //pWMInfo->atmPrivMap,
                            32,
                            PropModeReplace,
                            (unsigned char *) &(pNode->msg.hwndWindow), 1);
            break;

        case WM_WM_MAP3:
730
#if CYGMULTIWINDOW_DEBUG
731
            ErrorF("\tWM_WM_MAP3\n");
732
#endif
733
734
735
736
737
738
            /* Put a note as to the HWND associated with this Window */
            XChangeProperty(pWMInfo->pDisplay, pNode->msg.iWindow, pWMInfo->atmPrivMap, XA_INTEGER,     //pWMInfo->atmPrivMap,
                            32,
                            PropModeReplace,
                            (unsigned char *) &(pNode->msg.hwndWindow), 1);
            UpdateName(pWMInfo, pNode->msg.iWindow);
739
            UpdateIcon(pWMInfo, pNode->msg.iWindow);
740
741
742
743
744
            {
                HWND zstyle = HWND_NOTOPMOST;

                winApplyHints(pWMInfo->pDisplay, pNode->msg.iWindow,
                              pNode->msg.hwndWindow, &zstyle);
745
                winUpdateWindowPosition(pNode->msg.hwndWindow, &zstyle);
746
            }
747
748
749
750
751
752
753
754
755
756
757

            /* Reshape */
            {
                WindowPtr pWin =
                    GetProp(pNode->msg.hwndWindow, WIN_WINDOW_PROP);
                if (pWin) {
                    winReshapeMultiWindow(pWin);
                    winUpdateRgnMultiWindow(pWin);
                }
            }

758
759
760
            break;

        case WM_WM_UNMAP:
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
761
#if CYGMULTIWINDOW_DEBUG
762
            ErrorF("\tWM_WM_UNMAP\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
763
764
#endif

765
766
767
768
769
            /* Unmap the window */
            XUnmapWindow(pWMInfo->pDisplay, pNode->msg.iWindow);
            break;

        case WM_WM_KILL:
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
770
#if CYGMULTIWINDOW_DEBUG
771
            ErrorF("\tWM_WM_KILL\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
772
#endif
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
            {
                int i, n, found = 0;
                Atom *protocols;

                /* --- */
                if (XGetWMProtocols(pWMInfo->pDisplay,
                                    pNode->msg.iWindow, &protocols, &n)) {
                    for (i = 0; i < n; ++i)
                        if (protocols[i] == pWMInfo->atmWmDelete)
                            ++found;

                    XFree(protocols);
                }

                /* --- */
                if (found)
                    SendXMessage(pWMInfo->pDisplay,
                                 pNode->msg.iWindow,
                                 pWMInfo->atmWmProtos, pWMInfo->atmWmDelete);
                else
                    XKillClient(pWMInfo->pDisplay, pNode->msg.iWindow);
            }
            break;

        case WM_WM_ACTIVATE:
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
798
#if CYGMULTIWINDOW_DEBUG
799
            ErrorF("\tWM_WM_ACTIVATE\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
800
#endif
801
802
803
804
805
806
807
808
809
810
811

            /* Set the input focus */
            XSetInputFocus(pWMInfo->pDisplay,
                           pNode->msg.iWindow,
                           RevertToPointerRoot, CurrentTime);
            break;

        case WM_WM_NAME_EVENT:
            UpdateName(pWMInfo, pNode->msg.iWindow);
            break;

812
        case WM_WM_ICON_EVENT:
813
            UpdateIcon(pWMInfo, pNode->msg.iWindow);
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
            break;

        case WM_WM_CHANGE_STATE:
            /* Minimize the window in Windows */
            winMinimizeWindow(pNode->msg.iWindow);
            break;

        default:
            ErrorF("winMultiWindowWMProc - Unknown Message.  Exiting.\n");
            pthread_exit(NULL);
            break;
        }

        /* Free the retrieved message */
        free(pNode);

        /* Flush any pending events on our display */
        XFlush(pWMInfo->pDisplay);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
832
833
    }

834
835
836
837
838
839
840
841
842
    /* Free the condition variable */
    pthread_cond_destroy(&pWMInfo->wmMsgQueue.pcNotEmpty);

    /* Free the mutex variable */
    pthread_mutex_destroy(&pWMInfo->wmMsgQueue.pmMutex);

    /* Free the passed-in argument */
    free(pProcArg);

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
843
#if CYGMULTIWINDOW_DEBUG
844
    ErrorF("-winMultiWindowWMProc ()\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
845
#endif
846
    return NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
847
848
849
}

/*
850
 * X message procedure
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
851
852
 */

853
static void *
854
winMultiWindowXMsgProc(void *pArg)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
855
{
856
857
858
859
860
861
862
863
    winWMMessageRec msg;
    XMsgProcArgPtr pProcArg = (XMsgProcArgPtr) pArg;
    char pszDisplay[512];
    int iRetries;
    XEvent event;
    Atom atmWmName;
    Atom atmWmHints;
    Atom atmWmChange;
864
    Atom atmNetWmIcon;
865
866
867
    int iReturn;
    XIconSize *xis;

868
    winDebug("winMultiWindowXMsgProc - Hello\n");
869
870
871
872
873

    /* Check that argument pointer is not invalid */
    if (pProcArg == NULL) {
        ErrorF("winMultiWindowXMsgProc - pProcArg is NULL.  Exiting.\n");
        pthread_exit(NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
874
    }
875

876
    ErrorF("winMultiWindowXMsgProc - Calling pthread_mutex_lock ()\n");
877

878
879
880
881
882
883
    /* Grab the server started mutex - pause until we get it */
    iReturn = pthread_mutex_lock(pProcArg->ppmServerStarted);
    if (iReturn != 0) {
        ErrorF("winMultiWindowXMsgProc - pthread_mutex_lock () failed: %d.  "
               "Exiting.\n", iReturn);
        pthread_exit(NULL);
884
885
    }

886
    ErrorF("winMultiWindowXMsgProc - pthread_mutex_lock () returned.\n");
887

888
889
890
891
    /* Allow multiple threads to access Xlib */
    if (XInitThreads() == 0) {
        ErrorF("winMultiWindowXMsgProc - XInitThreads () failed.  Exiting.\n");
        pthread_exit(NULL);
892
    }
893

894
895
896
    /* See if X supports the current locale */
    if (XSupportsLocale() == False) {
        ErrorF("winMultiWindowXMsgProc - Warning: locale not supported by X\n");
897
898
    }

899
900
    /* Release the server started mutex */
    pthread_mutex_unlock(pProcArg->ppmServerStarted);
901

902
    ErrorF("winMultiWindowXMsgProc - pthread_mutex_unlock () returned.\n");
903

904
905
906
907
908
    /* Install our error handler */
    XSetErrorHandler(winMultiWindowXMsgProcErrorHandler);
    g_winMultiWindowXMsgProcThread = pthread_self();
    g_winMultiWindowXMsgProcOldIOErrorHandler =
        XSetIOErrorHandler(winMultiWindowXMsgProcIOErrorHandler);
909

910
911
    /* Set jump point for IO Error exits */
    iReturn = setjmp(g_jmpXMsgProcEntry);
912

913
914
915
916
917
918
    /* Check if we should continue operations */
    if (iReturn != WIN_JMP_ERROR_IO && iReturn != WIN_JMP_OKAY) {
        /* setjmp returned an unknown value, exit */
        ErrorF("winInitMultiWindowXMsgProc - setjmp returned: %d.  Exiting.\n",
               iReturn);
        pthread_exit(NULL);
919
    }
920
921
922
    else if (iReturn == WIN_JMP_ERROR_IO) {
        ErrorF("winInitMultiWindowXMsgProc - Caught IO Error.  Exiting.\n");
        pthread_exit(NULL);
923
924
    }

925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
    /* Setup the display connection string x */
    snprintf(pszDisplay,
             512, "127.0.0.1:%s.%d", display, (int) pProcArg->dwScreen);

    /* Print the display connection string */
    ErrorF("winMultiWindowXMsgProc - DISPLAY=%s\n", pszDisplay);

    /* Use our generated cookie for authentication */
    winSetAuthorization();

    /* Initialize retry count */
    iRetries = 0;

    /* Open the X display */
    do {
        /* Try to open the display */
        pProcArg->pDisplay = XOpenDisplay(pszDisplay);
        if (pProcArg->pDisplay == NULL) {
            ErrorF("winMultiWindowXMsgProc - Could not open display, try: %d, "
                   "sleeping: %d\n", iRetries + 1, WIN_CONNECT_DELAY);
            ++iRetries;
            sleep(WIN_CONNECT_DELAY);
            continue;
        }
        else
            break;
    }
    while (pProcArg->pDisplay == NULL && iRetries < WIN_CONNECT_RETRIES);
953

954
955
956
957
958
959
    /* Make sure that the display opened */
    if (pProcArg->pDisplay == NULL) {
        ErrorF("winMultiWindowXMsgProc - Failed opening the display.  "
               "Exiting.\n");
        pthread_exit(NULL);
    }
960

961
962
    ErrorF("winMultiWindowXMsgProc - XOpenDisplay () returned and "
           "successfully opened the display.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
963

964
965
966
967
968
969
970
971
972
    /* Check if another window manager is already running */
    g_fAnotherWMRunning =
        CheckAnotherWindowManager(pProcArg->pDisplay, pProcArg->dwScreen,
                                  pProcArg->pWMInfo->fAllowOtherWM);

    if (g_fAnotherWMRunning && !pProcArg->pWMInfo->fAllowOtherWM) {
        ErrorF("winMultiWindowXMsgProc - "
               "another window manager is running.  Exiting.\n");
        pthread_exit(NULL);
973
    }
974
975
976
977
978
979
980
981
982
983
984

    /* Set up the supported icon sizes */
    xis = XAllocIconSize();
    if (xis) {
        xis->min_width = xis->min_height = 16;
        xis->max_width = xis->max_height = 48;
        xis->width_inc = xis->height_inc = 16;
        XSetIconSizes(pProcArg->pDisplay,
                      RootWindow(pProcArg->pDisplay, pProcArg->dwScreen),
                      xis, 1);
        XFree(xis);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
985
986
    }

987
988
989
    atmWmName = XInternAtom(pProcArg->pDisplay, "WM_NAME", False);
    atmWmHints = XInternAtom(pProcArg->pDisplay, "WM_HINTS", False);
    atmWmChange = XInternAtom(pProcArg->pDisplay, "WM_CHANGE_STATE", False);
990
    atmNetWmIcon = XInternAtom(pProcArg->pDisplay, "_NET_WM_ICON", False);
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025

    /*
       iiimxcf had a bug until 2009-04-27, assuming that the
       WM_STATE atom exists, causing clients to fail with
       a BadAtom X error if it doesn't.

       Since this is on in the default Solaris 10 install,
       workaround this by making sure it does exist...
     */
    XInternAtom(pProcArg->pDisplay, "WM_STATE", 0);

    /* Loop until we explicitly break out */
    while (1) {
        if (g_shutdown)
            break;

        if (pProcArg->pWMInfo->fAllowOtherWM && !XPending(pProcArg->pDisplay)) {
            if (CheckAnotherWindowManager
                (pProcArg->pDisplay, pProcArg->dwScreen, TRUE)) {
                if (!g_fAnotherWMRunning) {
                    g_fAnotherWMRunning = TRUE;
                    SendMessage(*(HWND *) pProcArg->hwndScreen, WM_UNMANAGE, 0,
                                0);
                }
            }
            else {
                if (g_fAnotherWMRunning) {
                    g_fAnotherWMRunning = FALSE;
                    SendMessage(*(HWND *) pProcArg->hwndScreen, WM_MANAGE, 0,
                                0);
                }
            }
            Sleep(500);
            continue;
        }
1026

1027
1028
        /* Fetch next event */
        XNextEvent(pProcArg->pDisplay, &event);
1029

1030
1031
1032
        /* Branch on event type */
        if (event.type == CreateNotify) {
            XWindowAttributes attr;
1033

1034
1035
            XSelectInput(pProcArg->pDisplay,
                         event.xcreatewindow.window, PropertyChangeMask);
1036

1037
1038
1039
            /* Get the window attributes */
            XGetWindowAttributes(pProcArg->pDisplay,
                                 event.xcreatewindow.window, &attr);
1040

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
            if (!attr.override_redirect)
                XSetWindowBorderWidth(pProcArg->pDisplay,
                                      event.xcreatewindow.window, 0);
        }
        else if (event.type == MapNotify) {
            /* Fake a reparentNotify event as SWT/Motif expects a
               Window Manager to reparent a top-level window when
               it is mapped and waits until they do.

               We don't actually need to reparent, as the frame is
               a native window, not an X window

               We do this on MapNotify, not MapRequest like a real
               Window Manager would, so we don't have do get involved
               in actually mapping the window via it's (non-existent)
               parent...

               See sourceware bugzilla #9848
             */

            XWindowAttributes attr;
            Window root;
            Window parent;
            Window *children;
            unsigned int nchildren;

            if (XGetWindowAttributes(event.xmap.display,
                                     event.xmap.window,
                                     &attr) &&
                XQueryTree(event.xmap.display,
                           event.xmap.window,
                           &root, &parent, &children, &nchildren)) {
                if (children)
                    XFree(children);

                /*
                   It's a top-level window if the parent window is a root window
                   Only non-override_redirect windows can get reparented
                 */
                if ((attr.root == parent) && !event.xmap.override_redirect) {
                    XEvent event_send;

                    event_send.type = ReparentNotify;
                    event_send.xreparent.event = event.xmap.window;
                    event_send.xreparent.window = event.xmap.window;
                    event_send.xreparent.parent = parent;
                    event_send.xreparent.x = attr.x;
                    event_send.xreparent.y = attr.y;

                    XSendEvent(event.xmap.display,
                               event.xmap.window,
                               True, StructureNotifyMask, &event_send);
1093
1094
1095
                }
            }
        }
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
        else if (event.type == ConfigureNotify) {
            if (!event.xconfigure.send_event) {
                /*
                   Java applications using AWT on JRE 1.6.0 break with non-reparenting WMs AWT
                   doesn't explicitly know about (See sun bug #6434227)

                   XDecoratedPeer.handleConfigureNotifyEvent() only processes non-synthetic
                   ConfigureNotify events to update window location if it's identified the
                   WM as a non-reparenting WM it knows about (compiz or lookingglass)

                   Rather than tell all sorts of lies to get XWM to recognize us as one of
                   those, simply send a synthetic ConfigureNotify for every non-synthetic one
                 */
                XEvent event_send = event;

                event_send.xconfigure.send_event = TRUE;
                event_send.xconfigure.event = event.xconfigure.window;
                XSendEvent(event.xconfigure.display,
                           event.xconfigure.window,
                           True, StructureNotifyMask, &event_send);
1116
1117
            }
        }
1118
1119
1120
        else if (event.type == PropertyNotify) {
            if (event.xproperty.atom == atmWmName) {
                memset(&msg, 0, sizeof(msg));
1121

1122
1123
                msg.msg = WM_WM_NAME_EVENT;
                msg.iWindow = event.xproperty.window;
1124

1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
                /* Other fields ignored */
                winSendMessageToWM(pProcArg->pWMInfo, &msg);
            }
            else if ((event.xproperty.atom == atmWmHints) ||
                     (event.xproperty.atom == atmNetWmIcon)) {
                memset(&msg, 0, sizeof(msg));
                msg.msg = WM_WM_ICON_EVENT;
                msg.iWindow = event.xproperty.window;

                /* Other fields ignored */
                winSendMessageToWM(pProcArg->pWMInfo, &msg);
            }
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
        }
        else if (event.type == ClientMessage
                 && event.xclient.message_type == atmWmChange
                 && event.xclient.data.l[0] == IconicState) {
            ErrorF("winMultiWindowXMsgProc - WM_CHANGE_STATE - IconicState\n");

            memset(&msg, 0, sizeof(msg));

            msg.msg = WM_WM_CHANGE_STATE;
            msg.iWindow = event.xclient.window;

            winSendMessageToWM(pProcArg->pWMInfo, &msg);
        }
1150
    }
1151

1152
1153
1154
    XCloseDisplay(pProcArg->pDisplay);
    pthread_exit(NULL);
    return NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1155
1156
1157
1158
1159
1160
1161
1162
1163
}

/*
 * winInitWM - Entry point for the X server to spawn
 * the Window Manager thread.  Called from
 * winscrinit.c/winFinishScreenInitFB ().
 */

Bool
1164
1165
1166
1167
1168
winInitWM(void **ppWMInfo,
          pthread_t * ptWMProc,
          pthread_t * ptXMsgProc,
          pthread_mutex_t * ppmServerStarted,
          int dwScreen, HWND hwndScreen, BOOL allowOtherWM)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1169
{
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
    WMProcArgPtr pArg = (WMProcArgPtr) malloc(sizeof(WMProcArgRec));
    WMInfoPtr pWMInfo = (WMInfoPtr) malloc(sizeof(WMInfoRec));
    XMsgProcArgPtr pXMsgArg = (XMsgProcArgPtr) malloc(sizeof(XMsgProcArgRec));

    /* Bail if the input parameters are bad */
    if (pArg == NULL || pWMInfo == NULL || pXMsgArg == NULL) {
        ErrorF("winInitWM - malloc failed.\n");
        free(pArg);
        free(pWMInfo);
        free(pXMsgArg);
        return FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1181
    }
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200

    /* Zero the allocated memory */
    ZeroMemory(pArg, sizeof(WMProcArgRec));
    ZeroMemory(pWMInfo, sizeof(WMInfoRec));
    ZeroMemory(pXMsgArg, sizeof(XMsgProcArgRec));

    /* Set a return pointer to the Window Manager info structure */
    *ppWMInfo = pWMInfo;
    pWMInfo->fAllowOtherWM = allowOtherWM;

    /* Setup the argument structure for the thread function */
    pArg->dwScreen = dwScreen;
    pArg->pWMInfo = pWMInfo;
    pArg->ppmServerStarted = ppmServerStarted;

    /* Intialize the message queue */
    if (!InitQueue(&pWMInfo->wmMsgQueue)) {
        ErrorF("winInitWM - InitQueue () failed.\n");
        return FALSE;
1201
1202
    }

1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
    /* Spawn a thread for the Window Manager */
    if (pthread_create(ptWMProc, NULL, winMultiWindowWMProc, pArg)) {
        /* Bail if thread creation failed */
        ErrorF("winInitWM - pthread_create failed for Window Manager.\n");
        return FALSE;
    }

    /* Spawn the XNextEvent thread, will send messages to WM */
    pXMsgArg->dwScreen = dwScreen;
    pXMsgArg->pWMInfo = pWMInfo;
    pXMsgArg->ppmServerStarted = ppmServerStarted;
    pXMsgArg->hwndScreen = hwndScreen;
    if (pthread_create(ptXMsgProc, NULL, winMultiWindowXMsgProc, pXMsgArg)) {
        /* Bail if thread creation failed */
        ErrorF("winInitWM - pthread_create failed on XMSG.\n");
        return FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1219
1220
1221
    }

#if CYGDEBUG || YES
1222
    winDebug("winInitWM - Returning.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1223
1224
#endif

1225
    return TRUE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1226
1227
1228
}

/*
1229
 * Window manager thread - setup
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1230
1231
1232
 */

static void
1233
winInitMultiWindowWM(WMInfoPtr pWMInfo, WMProcArgPtr pProcArg)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1234
{
1235
1236
1237
    int iRetries = 0;
    char pszDisplay[512];
    int iReturn;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1238

1239
    winDebug("winInitMultiWindowWM - Hello\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1240

1241
1242
1243
1244
    /* Check that argument pointer is not invalid */
    if (pProcArg == NULL) {
        ErrorF("winInitMultiWindowWM - pProcArg is NULL.  Exiting.\n");
        pthread_exit(NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1245
1246
    }

1247
    ErrorF("winInitMultiWindowWM - Calling pthread_mutex_lock ()\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1248

1249
1250
1251
1252
1253
1254
    /* Grab our garbage mutex to satisfy pthread_cond_wait */
    iReturn = pthread_mutex_lock(pProcArg->ppmServerStarted);
    if (iReturn != 0) {
        ErrorF("winInitMultiWindowWM - pthread_mutex_lock () failed: %d.  "
               "Exiting.\n", iReturn);
        pthread_exit(NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1255
1256
    }

1257
    ErrorF("winInitMultiWindowWM - pthread_mutex_lock () returned.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1258

1259
1260
1261
1262
    /* Allow multiple threads to access Xlib */
    if (XInitThreads() == 0) {
        ErrorF("winInitMultiWindowWM - XInitThreads () failed.  Exiting.\n");
        pthread_exit(NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed