~alan-griffiths/compiz-core/Bug-931283

« back to all changes in this revision

Viewing changes to src/window.c

  • Committer: David Reveman
  • Date: 2006-02-09 06:03:09 UTC
  • Revision ID: git-v1:9959c2b13ded64a5e66359a8097250dc9d87fc1c
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2005 Novell, Inc.
 
3
 *
 
4
 * Permission to use, copy, modify, distribute, and sell this software
 
5
 * and its documentation for any purpose is hereby granted without
 
6
 * fee, provided that the above copyright notice appear in all copies
 
7
 * and that both that copyright notice and this permission notice
 
8
 * appear in supporting documentation, and that the name of
 
9
 * Novell, Inc. not be used in advertising or publicity pertaining to
 
10
 * distribution of the software without specific, written prior permission.
 
11
 * Novell, Inc. makes no representations about the suitability of this
 
12
 * software for any purpose. It is provided "as is" without express or
 
13
 * implied warranty.
 
14
 *
 
15
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 
16
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 
17
 * NO EVENT SHALL NOVELL, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 
18
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 
19
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 
20
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 
21
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
22
 *
 
23
 * Author: David Reveman <davidr@novell.com>
 
24
 */
 
25
 
 
26
#include <X11/Xlib.h>
 
27
#include <X11/Xatom.h>
 
28
#include <X11/Xproto.h>
 
29
#include <X11/extensions/shape.h>
 
30
#include <X11/extensions/Xcomposite.h>
 
31
 
 
32
#include <stdio.h>
 
33
#include <string.h>
 
34
#include <strings.h>
 
35
#include <stdlib.h>
 
36
#include <stdint.h>
 
37
 
 
38
#include <compiz.h>
 
39
 
 
40
#define MwmHintsDecorations (1L << 1)
 
41
 
 
42
#define PropMotifWmHintElements 3
 
43
 
 
44
typedef struct {
 
45
    unsigned long flags;
 
46
    unsigned long functions;
 
47
    unsigned long decorations;
 
48
} MwmHints;
 
49
 
 
50
static int
 
51
reallocWindowPrivates (int  size,
 
52
                       void *closure)
 
53
{
 
54
    CompScreen *s = (CompScreen *) closure;
 
55
    CompWindow *w;
 
56
    void       *privates;
 
57
 
 
58
    for (w = s->windows; w; w = w->next)
 
59
    {
 
60
        privates = realloc (w->privates, size * sizeof (CompPrivate));
 
61
        if (!privates)
 
62
            return FALSE;
 
63
 
 
64
        w->privates = (CompPrivate *) privates;
 
65
    }
 
66
 
 
67
    return TRUE;
 
68
}
 
69
 
 
70
int
 
71
allocateWindowPrivateIndex (CompScreen *screen)
 
72
{
 
73
    return allocatePrivateIndex (&screen->windowPrivateLen,
 
74
                                 &screen->windowPrivateIndices,
 
75
                                 reallocWindowPrivates,
 
76
                                 (void *) screen);
 
77
}
 
78
 
 
79
void
 
80
freeWindowPrivateIndex (CompScreen *screen,
 
81
                        int        index)
 
82
{
 
83
    freePrivateIndex (screen->windowPrivateLen,
 
84
                      screen->windowPrivateIndices,
 
85
                      index);
 
86
}
 
87
 
 
88
static void
 
89
recalcNormalHints (CompWindow *window)
 
90
{
 
91
    window->sizeHints.x      = window->attrib.x;
 
92
    window->sizeHints.y      = window->attrib.y;
 
93
    window->sizeHints.width  = window->attrib.width;
 
94
    window->sizeHints.height = window->attrib.height;
 
95
 
 
96
    if (window->sizeHints.flags & PMinSize)
 
97
    {
 
98
        window->sizeHints.base_width  = window->sizeHints.min_width;
 
99
        window->sizeHints.base_height = window->sizeHints.min_height;
 
100
    }
 
101
    else
 
102
    {
 
103
        window->sizeHints.base_width  = 0;
 
104
        window->sizeHints.base_height = 0;
 
105
    }
 
106
 
 
107
    window->sizeHints.flags |= PBaseSize;
 
108
 
 
109
    if (window->sizeHints.flags & PBaseSize)
 
110
    {
 
111
        window->sizeHints.min_width  = window->sizeHints.base_width;
 
112
        window->sizeHints.min_height = window->sizeHints.base_height;
 
113
    }
 
114
    else
 
115
    {
 
116
        window->sizeHints.min_width  = 0;
 
117
        window->sizeHints.min_height = 0;
 
118
    }
 
119
    window->sizeHints.flags |= PMinSize;
 
120
 
 
121
    if (!(window->sizeHints.flags & PMaxSize))
 
122
    {
 
123
        window->sizeHints.max_width  = 65535;
 
124
        window->sizeHints.max_height = 65535;
 
125
        window->sizeHints.flags |= PMaxSize;
 
126
    }
 
127
 
 
128
    if (window->sizeHints.max_width < window->sizeHints.min_width)
 
129
        window->sizeHints.max_width = window->sizeHints.min_width;
 
130
 
 
131
    if (window->sizeHints.max_height < window->sizeHints.min_height)
 
132
        window->sizeHints.max_height = window->sizeHints.min_height;
 
133
 
 
134
    if (window->sizeHints.min_width < 1)
 
135
        window->sizeHints.min_width = 1;
 
136
 
 
137
    if (window->sizeHints.max_width < 1)
 
138
        window->sizeHints.max_width = 1;
 
139
 
 
140
    if (window->sizeHints.min_height < 1)
 
141
        window->sizeHints.min_height = 1;
 
142
 
 
143
    if (window->sizeHints.max_height < 1)
 
144
        window->sizeHints.max_height = 1;
 
145
 
 
146
    if (window->sizeHints.flags & PResizeInc)
 
147
    {
 
148
        if (window->sizeHints.width_inc == 0)
 
149
            window->sizeHints.width_inc = 1;
 
150
 
 
151
        if (window->sizeHints.height_inc == 0)
 
152
            window->sizeHints.height_inc = 1;
 
153
    }
 
154
    else
 
155
    {
 
156
        window->sizeHints.width_inc  = 1;
 
157
        window->sizeHints.height_inc = 1;
 
158
        window->sizeHints.flags |= PResizeInc;
 
159
    }
 
160
 
 
161
    if (window->sizeHints.flags & PAspect)
 
162
    {
 
163
        /* don't divide by 0 */
 
164
        if (window->sizeHints.min_aspect.y < 1)
 
165
            window->sizeHints.min_aspect.y = 1;
 
166
 
 
167
        if (window->sizeHints.max_aspect.y < 1)
 
168
            window->sizeHints.max_aspect.y = 1;
 
169
    }
 
170
    else
 
171
    {
 
172
        window->sizeHints.min_aspect.x = 1;
 
173
        window->sizeHints.min_aspect.y = 65535;
 
174
        window->sizeHints.max_aspect.x = 65535;
 
175
        window->sizeHints.max_aspect.y = 1;
 
176
        window->sizeHints.flags |= PAspect;
 
177
    }
 
178
 
 
179
    if (!(window->sizeHints.flags & PWinGravity))
 
180
    {
 
181
        window->sizeHints.win_gravity = NorthWestGravity;
 
182
        window->sizeHints.flags |= PWinGravity;
 
183
    }
 
184
}
 
185
 
 
186
void
 
187
updateNormalHints (CompWindow *w)
 
188
{
 
189
    Status status;
 
190
    long   supplied;
 
191
 
 
192
    status = XGetWMNormalHints (w->screen->display->display, w->id,
 
193
                                &w->sizeHints, &supplied);
 
194
 
 
195
    if (!status)
 
196
        w->sizeHints.flags = 0;
 
197
 
 
198
    recalcNormalHints (w);
 
199
}
 
200
 
 
201
void
 
202
updateWmHints (CompWindow *w)
 
203
{
 
204
    XWMHints *hints;
 
205
 
 
206
    hints = XGetWMHints (w->screen->display->display, w->id);
 
207
    if (hints)
 
208
    {
 
209
        if (hints->flags & InputHint)
 
210
            w->inputHint = hints->input;
 
211
 
 
212
        XFree (hints);
 
213
    }
 
214
}
 
215
 
 
216
void
 
217
updateWindowClassHints (CompWindow *w)
 
218
{
 
219
    XClassHint classHint;
 
220
    int        status;
 
221
 
 
222
    status = XGetClassHint (w->screen->display->display, w->id, &classHint);
 
223
 
 
224
    if (status)
 
225
    {
 
226
        if (classHint.res_name)
 
227
        {
 
228
            if (w->resName)
 
229
                free (w->resName);
 
230
 
 
231
            w->resName = strdup (classHint.res_name);
 
232
            XFree (classHint.res_name);
 
233
        }
 
234
 
 
235
        if (classHint.res_class)
 
236
        {
 
237
            if (w->resClass)
 
238
                free (w->resClass);
 
239
 
 
240
            w->resClass = strdup (classHint.res_class);
 
241
            XFree (classHint.res_class);
 
242
        }
 
243
    }
 
244
}
 
245
 
 
246
static Window
 
247
getClientLeaderOfAncestor (CompWindow *w)
 
248
{
 
249
    if (w->transientFor)
 
250
    {
 
251
        w = findWindowAtScreen (w->screen, w->transientFor);
 
252
        if (w)
 
253
        {
 
254
            if (w->clientLeader)
 
255
                return w->clientLeader;
 
256
 
 
257
            return getClientLeaderOfAncestor (w);
 
258
        }
 
259
    }
 
260
 
 
261
    return None;
 
262
}
 
263
 
 
264
Window
 
265
getClientLeader (CompWindow *w)
 
266
{
 
267
    Atom          actual;
 
268
    int           result, format;
 
269
    unsigned long n, left;
 
270
    unsigned char *data;
 
271
 
 
272
    result = XGetWindowProperty (w->screen->display->display, w->id,
 
273
                                 w->screen->display->wmClientLeaderAtom,
 
274
                                 0L, 1L, False, XA_WINDOW, &actual, &format,
 
275
                                 &n, &left, &data);
 
276
 
 
277
    if (result == Success && n && data)
 
278
    {
 
279
        Window win;
 
280
 
 
281
        memcpy (&win, data, sizeof (Window));
 
282
        XFree ((void *) data);
 
283
 
 
284
        if (win)
 
285
            return win;
 
286
    }
 
287
 
 
288
    return getClientLeaderOfAncestor (w);
 
289
}
 
290
 
 
291
int
 
292
getWmState (CompDisplay *display,
 
293
            Window      id)
 
294
{
 
295
    Atom          actual;
 
296
    int           result, format;
 
297
    unsigned long n, left;
 
298
    unsigned char *data;
 
299
    unsigned long state = NormalState;
 
300
 
 
301
    result = XGetWindowProperty (display->display, id,
 
302
                                 display->wmStateAtom, 0L, 2L, FALSE,
 
303
                                 display->wmStateAtom, &actual, &format,
 
304
                                 &n, &left, &data);
 
305
 
 
306
    if (result == Success && n && data)
 
307
    {
 
308
        memcpy (&state, data, sizeof (unsigned long));
 
309
        XFree ((void *) data);
 
310
    }
 
311
 
 
312
    return state;
 
313
}
 
314
 
 
315
void
 
316
setWmState (CompDisplay *display,
 
317
            int         state,
 
318
            Window      id)
 
319
{
 
320
    unsigned long data[2];
 
321
 
 
322
    data[0] = state;
 
323
    data[1] = None;
 
324
 
 
325
    XChangeProperty (display->display, id,
 
326
                     display->wmStateAtom, display->wmStateAtom,
 
327
                     32, PropModeReplace, (unsigned char *) data, 2);
 
328
}
 
329
 
 
330
unsigned int
 
331
windowStateMask (CompDisplay *display,
 
332
                 Atom        state)
 
333
{
 
334
    if (state == display->winStateModalAtom)
 
335
        return CompWindowStateModalMask;
 
336
    else if (state == display->winStateStickyAtom)
 
337
        return CompWindowStateStickyMask;
 
338
    else if (state == display->winStateMaximizedVertAtom)
 
339
        return CompWindowStateMaximizedVertMask;
 
340
    else if (state == display->winStateMaximizedHorzAtom)
 
341
        return CompWindowStateMaximizedHorzMask;
 
342
    else if (state == display->winStateShadedAtom)
 
343
        return CompWindowStateShadedMask;
 
344
    else if (state == display->winStateSkipTaskbarAtom)
 
345
        return CompWindowStateSkipTaskbarMask;
 
346
    else if (state == display->winStateSkipPagerAtom)
 
347
        return CompWindowStateSkipPagerMask;
 
348
    else if (state == display->winStateHiddenAtom)
 
349
        return CompWindowStateHiddenMask;
 
350
    else if (state == display->winStateFullscreenAtom)
 
351
        return CompWindowStateFullscreenMask;
 
352
    else if (state == display->winStateAboveAtom)
 
353
        return CompWindowStateAboveMask;
 
354
    else if (state == display->winStateBelowAtom)
 
355
        return CompWindowStateBelowMask;
 
356
    else if (state == display->winStateDemandsAttentionAtom)
 
357
        return CompWindowStateDemandsAttentationMask;
 
358
    else if (state == display->winStateDisplayModalAtom)
 
359
        return CompWindowStateDisplayModalMask;
 
360
 
 
361
    return 0;
 
362
}
 
363
 
 
364
unsigned int
 
365
getWindowState (CompDisplay *display,
 
366
                Window      id)
 
367
{
 
368
    Atom          actual;
 
369
    int           result, format;
 
370
    unsigned long n, left;
 
371
    unsigned char *data;
 
372
    unsigned int  state = 0;
 
373
 
 
374
    result = XGetWindowProperty (display->display, id, display->winStateAtom,
 
375
                                 0L, 1024L, FALSE, XA_ATOM, &actual, &format,
 
376
                                 &n, &left, &data);
 
377
 
 
378
    if (result == Success && n && data)
 
379
    {
 
380
        Atom *a = (Atom *) data;
 
381
 
 
382
        while (n--)
 
383
            state |= windowStateMask (display, *a++);
 
384
 
 
385
        XFree ((void *) data);
 
386
    }
 
387
 
 
388
    return state;
 
389
}
 
390
 
 
391
void
 
392
setWindowState (CompDisplay  *display,
 
393
                unsigned int state,
 
394
                Window       id)
 
395
{
 
396
    Atom data[32];
 
397
    int  i = 0;
 
398
 
 
399
    if (state & CompWindowStateModalMask)
 
400
        data[i++] = display->winStateModalAtom;
 
401
    if (state & CompWindowStateStickyMask)
 
402
        data[i++] = display->winStateStickyAtom;
 
403
    if (state & CompWindowStateMaximizedVertMask)
 
404
        data[i++] = display->winStateMaximizedVertAtom;
 
405
    if (state & CompWindowStateMaximizedHorzMask)
 
406
        data[i++] = display->winStateMaximizedHorzAtom;
 
407
    if (state & CompWindowStateShadedMask)
 
408
        data[i++] = display->winStateShadedAtom;
 
409
    if (state & CompWindowStateSkipTaskbarMask)
 
410
        data[i++] = display->winStateSkipTaskbarAtom;
 
411
    if (state & CompWindowStateSkipPagerMask)
 
412
        data[i++] = display->winStateSkipPagerAtom;
 
413
    if (state & CompWindowStateHiddenMask)
 
414
        data[i++] = display->winStateHiddenAtom;
 
415
    if (state & CompWindowStateFullscreenMask)
 
416
        data[i++] = display->winStateFullscreenAtom;
 
417
    if (state & CompWindowStateAboveMask)
 
418
        data[i++] = display->winStateAboveAtom;
 
419
    if (state & CompWindowStateBelowMask)
 
420
        data[i++] = display->winStateBelowAtom;
 
421
    if (state & CompWindowStateDemandsAttentationMask)
 
422
        data[i++] = display->winStateDemandsAttentionAtom;
 
423
    if (state & CompWindowStateDisplayModalMask)
 
424
        data[i++] = display->winStateDisplayModalAtom;
 
425
 
 
426
    XChangeProperty (display->display, id, display->winStateAtom,
 
427
                     XA_ATOM, 32, PropModeReplace,
 
428
                     (unsigned char *) data, i);
 
429
}
 
430
 
 
431
static void
 
432
setWindowActions (CompDisplay  *display,
 
433
                  unsigned int actions,
 
434
                  Window       id)
 
435
{
 
436
    Atom data[32];
 
437
    int  i = 0;
 
438
 
 
439
    if (actions & CompWindowActionMoveMask)
 
440
        data[i++] = display->winActionMoveAtom;
 
441
    if (actions & CompWindowActionResizeMask)
 
442
        data[i++] = display->winActionResizeAtom;
 
443
    if (actions & CompWindowActionStickMask)
 
444
        data[i++] = display->winActionStickAtom;
 
445
    if (actions & CompWindowActionMinimizeMask)
 
446
        data[i++] = display->winActionMinimizeAtom;
 
447
    if (actions & CompWindowActionMaximizeHorzMask)
 
448
        data[i++] = display->winActionMaximizeHorzAtom;
 
449
    if (actions & CompWindowActionMaximizeVertMask)
 
450
        data[i++] = display->winActionMaximizeVertAtom;
 
451
    if (actions & CompWindowActionFullscreenMask)
 
452
        data[i++] = display->winActionFullscreenAtom;
 
453
    if (actions & CompWindowActionCloseMask)
 
454
        data[i++] = display->winActionCloseAtom;
 
455
 
 
456
    XChangeProperty (display->display, id, display->wmAllowedActionsAtom,
 
457
                     XA_ATOM, 32, PropModeReplace,
 
458
                     (unsigned char *) data, i);
 
459
}
 
460
 
 
461
static void
 
462
recalcWindowActions (CompWindow *w)
 
463
{
 
464
    unsigned int actions = 0;
 
465
 
 
466
    switch (w->type) {
 
467
    case CompWindowTypeFullscreenMask:
 
468
    case CompWindowTypeNormalMask:
 
469
        actions |=
 
470
            CompWindowActionMinimizeMask     |
 
471
            CompWindowActionMaximizeHorzMask |
 
472
            CompWindowActionMaximizeVertMask |
 
473
            CompWindowActionFullscreenMask;
 
474
        /* fall-through */
 
475
    case CompWindowTypeDialogMask:
 
476
    case CompWindowTypeModalDialogMask:
 
477
        actions |=
 
478
            CompWindowActionMoveMask   |
 
479
            CompWindowActionResizeMask |
 
480
            CompWindowActionStickMask  |
 
481
            CompWindowActionCloseMask;
 
482
        break;
 
483
    case CompWindowTypeUtilMask:
 
484
    case CompWindowTypeToolbarMask:
 
485
    case CompWindowTypeMenuMask:
 
486
    case CompWindowTypeSplashMask:
 
487
    case CompWindowTypeDesktopMask:
 
488
    case CompWindowTypeDockMask:
 
489
    case CompWindowTypeUnknownMask:
 
490
    default:
 
491
        break;
 
492
    }
 
493
 
 
494
    if (actions != w->actions)
 
495
    {
 
496
        w->actions = actions;
 
497
        setWindowActions (w->screen->display, actions, w->id);
 
498
    }
 
499
}
 
500
 
 
501
unsigned int
 
502
getWindowType (CompDisplay *display,
 
503
               Window      id)
 
504
{
 
505
    Atom          actual;
 
506
    int           result, format;
 
507
    unsigned long n, left;
 
508
    unsigned char *data;
 
509
 
 
510
    result = XGetWindowProperty (display->display, id, display->winTypeAtom,
 
511
                                 0L, 1L, FALSE, XA_ATOM, &actual, &format,
 
512
                                 &n, &left, &data);
 
513
 
 
514
    if (result == Success && n && data)
 
515
    {
 
516
        Atom a;
 
517
 
 
518
        memcpy (&a, data, sizeof (Atom));
 
519
        XFree ((void *) data);
 
520
 
 
521
        if (a == display->winTypeNormalAtom)
 
522
            return CompWindowTypeNormalMask;
 
523
        else if (a == display->winTypeMenuAtom)
 
524
            return CompWindowTypeMenuMask;
 
525
        else if (a == display->winTypeDesktopAtom)
 
526
            return CompWindowTypeDesktopMask;
 
527
        else if (a == display->winTypeDockAtom)
 
528
            return CompWindowTypeDockMask;
 
529
        else if (a == display->winTypeToolbarAtom)
 
530
            return CompWindowTypeToolbarMask;
 
531
        else if (a == display->winTypeUtilAtom)
 
532
            return CompWindowTypeUtilMask;
 
533
        else if (a == display->winTypeSplashAtom)
 
534
            return CompWindowTypeSplashMask;
 
535
        else if (a == display->winTypeDialogAtom)
 
536
            return CompWindowTypeDialogMask;
 
537
    }
 
538
 
 
539
    return CompWindowTypeUnknownMask;
 
540
}
 
541
 
 
542
void
 
543
recalcWindowType (CompWindow *w)
 
544
{
 
545
    unsigned int type;
 
546
 
 
547
    type = w->wmType;
 
548
 
 
549
    if (!w->attrib.override_redirect && w->wmType == CompWindowTypeUnknownMask)
 
550
        type = CompWindowTypeNormalMask;
 
551
 
 
552
    if (w->state & CompWindowStateFullscreenMask)
 
553
        type = CompWindowTypeFullscreenMask;
 
554
 
 
555
    if (type == CompWindowTypeNormalMask)
 
556
    {
 
557
        if (w->transientFor)
 
558
            type = CompWindowTypeDialogMask;
 
559
    }
 
560
 
 
561
    if ((type & (CompWindowTypeNormalMask | CompWindowTypeDialogMask)) &&
 
562
        (w->state & CompWindowStateModalMask))
 
563
        type = CompWindowTypeModalDialogMask;
 
564
 
 
565
    if (type != w->type)
 
566
    {
 
567
        w->type = type;
 
568
        recalcWindowActions (w);
 
569
    }
 
570
}
 
571
 
 
572
unsigned int
 
573
getMwmDecor (CompDisplay *display,
 
574
             Window      id)
 
575
{
 
576
    Atom          actual;
 
577
    int           result, format;
 
578
    unsigned long n, left;
 
579
    MwmHints      *mwmHints;
 
580
    unsigned int  decor = MwmDecorAll;
 
581
 
 
582
    result = XGetWindowProperty (display->display, id, display->mwmHintsAtom,
 
583
                                 0L, 20L, FALSE, display->mwmHintsAtom,
 
584
                                 &actual, &format, &n, &left,
 
585
                                 (unsigned char **) &mwmHints);
 
586
 
 
587
    if (result == Success && n && mwmHints)
 
588
    {
 
589
        if (n >= PropMotifWmHintElements)
 
590
        {
 
591
            if (mwmHints->flags & MwmHintsDecorations)
 
592
                decor = mwmHints->decorations;
 
593
        }
 
594
 
 
595
        XFree (mwmHints);
 
596
    }
 
597
 
 
598
    return decor;
 
599
}
 
600
 
 
601
unsigned int
 
602
getProtocols (CompDisplay *display,
 
603
              Window      id)
 
604
{
 
605
    Atom          actual;
 
606
    int           result, format;
 
607
    unsigned long n, left;
 
608
    Atom          *protocol;
 
609
    unsigned int  protocols = 0;
 
610
 
 
611
    result = XGetWindowProperty (display->display, id, display->wmProtocolsAtom,
 
612
                                 0L, 20L, FALSE, XA_ATOM,
 
613
                                 &actual, &format, &n, &left,
 
614
                                 (unsigned char **) &protocol);
 
615
 
 
616
    if (result == Success && n && protocol)
 
617
    {
 
618
        int i;
 
619
 
 
620
        for (i = 0; i < n; i++)
 
621
        {
 
622
            if (protocol[i] == display->wmDeleteWindowAtom)
 
623
                protocols |= CompWindowProtocolDeleteMask;
 
624
            else if (protocol[i] == display->wmTakeFocusAtom)
 
625
                protocols |= CompWindowProtocolTakeFocusMask;
 
626
            else if (protocol[i] == display->wmPingAtom)
 
627
                protocols |= CompWindowProtocolPingMask;
 
628
            else if (protocol[i] == display->wmSyncRequestAtom)
 
629
                protocols |= CompWindowProtocolSyncRequestMask;
 
630
        }
 
631
 
 
632
        XFree (protocol);
 
633
    }
 
634
 
 
635
    return protocols;
 
636
}
 
637
 
 
638
unsigned short
 
639
getWindowProp32 (CompDisplay    *display,
 
640
                 Window         id,
 
641
                 Atom           property,
 
642
                 unsigned short defaultValue)
 
643
{
 
644
    Atom          actual;
 
645
    int           result, format;
 
646
    unsigned long n, left;
 
647
    unsigned char *data;
 
648
 
 
649
    result = XGetWindowProperty (display->display, id, property,
 
650
                                 0L, 1L, FALSE, XA_CARDINAL, &actual, &format,
 
651
                                 &n, &left, &data);
 
652
 
 
653
    if (result == Success && n && data)
 
654
    {
 
655
        CARD32 value;
 
656
 
 
657
        memcpy (&value, data, sizeof (CARD32));
 
658
 
 
659
        XFree (data);
 
660
 
 
661
        return value >> 16;
 
662
    }
 
663
 
 
664
    return defaultValue;
 
665
}
 
666
 
 
667
void
 
668
setWindowProp32 (CompDisplay    *display,
 
669
                 Window         id,
 
670
                 Atom           property,
 
671
                 unsigned short value)
 
672
{
 
673
    CARD32 value32;
 
674
 
 
675
    value32 = value << 16 | value;
 
676
 
 
677
    XChangeProperty (display->display, id, property,
 
678
                     XA_CARDINAL, 32, PropModeReplace,
 
679
                     (unsigned char *) &value32, 1);
 
680
}
 
681
 
 
682
 
 
683
static void
 
684
updateFrameWindow (CompWindow *w)
 
685
{
 
686
    if (w->input.left || w->input.right || w->input.top || w->input.bottom)
 
687
    {
 
688
        XRectangle rects[4];
 
689
        int        x, y, width, height;
 
690
 
 
691
        x      = w->serverX - w->input.left;
 
692
        y      = w->serverY - w->input.top;
 
693
        width  = w->width  + w->input.left + w->input.right;
 
694
        height = w->height + w->input.top  + w->input.bottom;
 
695
 
 
696
        if (!w->frame)
 
697
        {
 
698
            XSetWindowAttributes attr;
 
699
            XWindowChanges       xwc;
 
700
 
 
701
            attr.event_mask        = 0;
 
702
            attr.override_redirect = TRUE;
 
703
 
 
704
            w->frame = XCreateWindow (w->screen->display->display,
 
705
                                      w->screen->root,
 
706
                                      x, y, width, height, 0,
 
707
                                      CopyFromParent,
 
708
                                      InputOnly,
 
709
                                      CopyFromParent,
 
710
                                      CWOverrideRedirect | CWEventMask, &attr);
 
711
 
 
712
            XGrabButton (w->screen->display->display, AnyButton,
 
713
                         AnyModifier, w->frame, TRUE, ButtonPressMask |
 
714
                         ButtonReleaseMask | ButtonMotionMask,
 
715
                         GrabModeSync, GrabModeSync, None, None);
 
716
 
 
717
            xwc.stack_mode = Below;
 
718
            xwc.sibling    = w->id;
 
719
 
 
720
            XConfigureWindow (w->screen->display->display, w->frame,
 
721
                              CWSibling | CWStackMode, &xwc);
 
722
 
 
723
            if (w->mapNum)
 
724
                XMapWindow (w->screen->display->display, w->frame);
 
725
 
 
726
            XChangeProperty (w->screen->display->display, w->id,
 
727
                             w->screen->display->frameWindowAtom,
 
728
                             XA_WINDOW, 32, PropModeReplace,
 
729
                             (unsigned char *) &w->frame, 1);
 
730
        }
 
731
 
 
732
        XResizeWindow (w->screen->display->display, w->frame, width, height);
 
733
 
 
734
        rects[0].x      = 0;
 
735
        rects[0].y      = 0;
 
736
        rects[0].width  = width;
 
737
        rects[0].height = w->input.top;
 
738
 
 
739
        rects[1].x      = 0;
 
740
        rects[1].y      = w->input.top;
 
741
        rects[1].width  = w->input.left;
 
742
        rects[1].height = height - w->input.top - w->input.bottom;
 
743
 
 
744
        rects[2].x      = width - w->input.right;
 
745
        rects[2].y      = w->input.top;
 
746
        rects[2].width  = w->input.right;
 
747
        rects[2].height = height - w->input.top - w->input.bottom;
 
748
 
 
749
        rects[3].x      = 0;
 
750
        rects[3].y      = height - w->input.bottom;
 
751
        rects[3].width  = width;
 
752
        rects[3].height = w->input.bottom;
 
753
 
 
754
        XShapeCombineRectangles (w->screen->display->display,
 
755
                                 w->frame,
 
756
                                 ShapeInput,
 
757
                                 0,
 
758
                                 0,
 
759
                                 rects,
 
760
                                 4,
 
761
                                 ShapeSet,
 
762
                                 YXBanded);
 
763
    }
 
764
    else
 
765
    {
 
766
        if (w->frame)
 
767
        {
 
768
            XDestroyWindow (w->screen->display->display, w->frame);
 
769
            w->frame = None;
 
770
        }
 
771
    }
 
772
}
 
773
 
 
774
void
 
775
setWindowFrameExtents (CompWindow        *w,
 
776
                       CompWindowExtents *input,
 
777
                       CompWindowExtents *output)
 
778
{
 
779
    if (input->left   != w->input.left  ||
 
780
        input->right  != w->input.right ||
 
781
        input->top    != w->input.top   ||
 
782
        input->bottom != w->input.bottom)
 
783
    {
 
784
        unsigned long data[4];
 
785
 
 
786
        w->input = *input;
 
787
 
 
788
        data[0] = input->left;
 
789
        data[1] = input->right;
 
790
        data[2] = input->top;
 
791
        data[3] = input->bottom;
 
792
 
 
793
        updateFrameWindow (w);
 
794
 
 
795
        XChangeProperty (w->screen->display->display, w->id,
 
796
                         w->screen->display->frameExtentsAtom,
 
797
                         XA_CARDINAL, 32, PropModeReplace,
 
798
                         (unsigned char *) data, 4);
 
799
    }
 
800
 
 
801
    if (output->left   != w->output.left  ||
 
802
        output->right  != w->output.right ||
 
803
        output->top    != w->output.top   ||
 
804
        output->bottom != w->output.bottom)
 
805
    {
 
806
        w->output = *output;
 
807
 
 
808
        (*w->screen->windowResizeNotify) (w);
 
809
    }
 
810
}
 
811
 
 
812
static void
 
813
setWindowMatrix (CompWindow *w)
 
814
{
 
815
    w->matrix = w->texture.matrix;
 
816
    w->matrix.x0 -= (w->attrib.x * w->matrix.xx);
 
817
    w->matrix.y0 -= (w->attrib.y * w->matrix.yy);
 
818
}
 
819
 
 
820
void
 
821
bindWindow (CompWindow *w)
 
822
{
 
823
    if (testMode)
 
824
    {
 
825
        unsigned int width, height;
 
826
 
 
827
        if (readImageToTexture (w->screen, &w->texture,
 
828
                                windowImage, &width, &height))
 
829
        {
 
830
            XResizeWindow (w->screen->display->display, w->id, width, height);
 
831
 
 
832
            w->width  = width;
 
833
            w->height = height;
 
834
        }
 
835
 
 
836
        w->pixmap         = 1;
 
837
        w->texture.pixmap = 1;
 
838
    }
 
839
    else
 
840
    {
 
841
        if (!w->pixmap)
 
842
        {
 
843
            w->pixmap =
 
844
                XCompositeNameWindowPixmap (w->screen->display->display,
 
845
                                            w->id);
 
846
            if (!w->pixmap)
 
847
            {
 
848
                fprintf (stderr, "%s: XCompositeNameWindowPixmap failed\n",
 
849
                         programName);
 
850
            }
 
851
        }
 
852
 
 
853
        if (!bindPixmapToTexture (w->screen, &w->texture, w->pixmap,
 
854
                                  w->width, w->height,
 
855
                                  w->attrib.depth))
 
856
        {
 
857
            fprintf (stderr, "%s: Couldn't bind redirected window 0x%x to "
 
858
                     "texture\n", programName, (int) w->id);
 
859
        }
 
860
    }
 
861
 
 
862
    setWindowMatrix (w);
 
863
}
 
864
 
 
865
void
 
866
releaseWindow (CompWindow *w)
 
867
{
 
868
    if (w->pixmap)
 
869
    {
 
870
        releasePixmapFromTexture (w->screen, &w->texture);
 
871
 
 
872
        if (!testMode)
 
873
            XFreePixmap (w->screen->display->display, w->pixmap);
 
874
 
 
875
        w->pixmap = None;
 
876
    }
 
877
}
 
878
 
 
879
static void
 
880
freeWindow (CompWindow *w)
 
881
{
 
882
    releaseWindow (w);
 
883
 
 
884
    if (w->syncAlarm)
 
885
        XSyncDestroyAlarm (w->screen->display->display, w->syncAlarm);
 
886
 
 
887
    if (w->syncWaitHandle)
 
888
        compRemoveTimeout (w->syncWaitHandle);
 
889
 
 
890
    if (w->texture.name)
 
891
        finiTexture (w->screen, &w->texture);
 
892
 
 
893
    if (w->clip)
 
894
        XDestroyRegion (w->clip);
 
895
 
 
896
    if (w->region)
 
897
        XDestroyRegion (w->region);
 
898
 
 
899
    if (w->privates)
 
900
        free (w->privates);
 
901
 
 
902
    if (w->sizeDamage)
 
903
        free (w->damageRects);
 
904
 
 
905
    if (w->vertices)
 
906
        free (w->vertices);
 
907
 
 
908
    if (w->indices)
 
909
        free (w->indices);
 
910
 
 
911
    if (lastFoundWindow == w)
 
912
        lastFoundWindow = 0;
 
913
 
 
914
    if (lastDamagedWindow == w)
 
915
        lastDamagedWindow = 0;
 
916
 
 
917
    if (w->struts)
 
918
        free (w->struts);
 
919
 
 
920
    if (w->startupId)
 
921
        free (w->startupId);
 
922
 
 
923
    if (w->resName)
 
924
        free (w->resName);
 
925
 
 
926
    if (w->resClass)
 
927
        free (w->resClass);
 
928
 
 
929
    free (w);
 
930
}
 
931
 
 
932
void
 
933
damageWindowRegion (CompWindow *w,
 
934
                    Region     region)
 
935
{
 
936
    if (w->scaled)
 
937
    {
 
938
        REGION reg;
 
939
        int    x1, y1, x2, y2;
 
940
 
 
941
        reg.rects    = &reg.extents;
 
942
        reg.numRects = 1;
 
943
 
 
944
        x1 = region->extents.x1 - w->attrib.x;
 
945
        y1 = region->extents.y1 - w->attrib.y;
 
946
        x2 = region->extents.x2 - w->attrib.x;
 
947
        y2 = region->extents.y2 - w->attrib.y;
 
948
 
 
949
        reg.extents.x1 = (x1 * w->paint.xScale) + w->attrib.x;
 
950
        reg.extents.y1 = (y1 * w->paint.yScale) + w->attrib.y;
 
951
        reg.extents.x2 = (x2 * w->paint.xScale + 0.5f) + w->attrib.x;
 
952
        reg.extents.y2 = (y2 * w->paint.yScale + 0.5f) + w->attrib.y;
 
953
 
 
954
        if (reg.extents.x2 > reg.extents.x1 && reg.extents.y2 > reg.extents.y1)
 
955
            damageScreenRegion (w->screen, &reg);
 
956
    }
 
957
    else
 
958
    {
 
959
        damageScreenRegion (w->screen, region);
 
960
    }
 
961
}
 
962
 
 
963
void
 
964
damageWindowOutputExtents (CompWindow *w)
 
965
{
 
966
    if (w->screen->damageMask & COMP_SCREEN_DAMAGE_ALL_MASK)
 
967
        return;
 
968
 
 
969
    if (w->attrib.map_state == IsViewable && w->damaged)
 
970
    {
 
971
        REGION reg;
 
972
 
 
973
        reg.rects = &reg.extents;
 
974
        reg.numRects = reg.size = 1;
 
975
 
 
976
        /* top */
 
977
        reg.extents.x1 = w->attrib.x - w->output.left;
 
978
        reg.extents.y1 = w->attrib.y - w->output.top;
 
979
        reg.extents.x2 = w->attrib.x + w->width + w->output.right;
 
980
        reg.extents.y2 = w->attrib.y;
 
981
 
 
982
        if (reg.extents.x1 < reg.extents.x2 && reg.extents.y1 < reg.extents.y2)
 
983
            damageWindowRegion (w, &reg);
 
984
 
 
985
        /* bottom */
 
986
        reg.extents.y1 = w->attrib.y + w->height;
 
987
        reg.extents.y2 = reg.extents.y1 + w->output.bottom;
 
988
 
 
989
        if (reg.extents.x1 < reg.extents.x2 && reg.extents.y1 < reg.extents.y2)
 
990
            damageWindowRegion (w, &reg);
 
991
 
 
992
        /* left */
 
993
        reg.extents.x1 = w->attrib.x - w->output.left;
 
994
        reg.extents.y1 = w->attrib.y;
 
995
        reg.extents.x2 = w->attrib.x;
 
996
        reg.extents.y2 = w->attrib.y + w->height;
 
997
 
 
998
        if (reg.extents.x1 < reg.extents.x2 && reg.extents.y1 < reg.extents.y2)
 
999
            damageWindowRegion (w, &reg);
 
1000
 
 
1001
        /* right */
 
1002
        reg.extents.x1 = w->attrib.x + w->width;
 
1003
        reg.extents.x2 = reg.extents.x1 + w->output.right;
 
1004
 
 
1005
        if (reg.extents.x1 < reg.extents.x2 && reg.extents.y1 < reg.extents.y2)
 
1006
            damageWindowRegion (w, &reg);
 
1007
    }
 
1008
}
 
1009
 
 
1010
Bool
 
1011
damageWindowRect (CompWindow *w,
 
1012
                  Bool       initial,
 
1013
                  BoxPtr     rect)
 
1014
{
 
1015
    return FALSE;
 
1016
}
 
1017
 
 
1018
void
 
1019
addWindowDamage (CompWindow *w)
 
1020
{
 
1021
    if (w->screen->damageMask & COMP_SCREEN_DAMAGE_ALL_MASK)
 
1022
        return;
 
1023
 
 
1024
    if (w->attrib.map_state == IsViewable && w->damaged)
 
1025
    {
 
1026
        REGION region;
 
1027
 
 
1028
        region.extents.x1 = -w->output.left - w->attrib.border_width;
 
1029
        region.extents.y1 = -w->output.top - w->attrib.border_width;
 
1030
        region.extents.x2 = w->width + w->output.right;
 
1031
        region.extents.y2 = w->height + w->output.bottom;
 
1032
 
 
1033
        if (!(*w->screen->damageWindowRect) (w, FALSE, &region.extents))
 
1034
        {
 
1035
            region.extents.x1 += w->attrib.x + w->attrib.border_width;
 
1036
            region.extents.y1 += w->attrib.y + w->attrib.border_width;
 
1037
            region.extents.x2 += w->attrib.x + w->attrib.border_width;
 
1038
            region.extents.y2 += w->attrib.y + w->attrib.border_width;
 
1039
 
 
1040
            region.rects = &region.extents;
 
1041
            region.numRects = region.size = 1;
 
1042
 
 
1043
            damageWindowRegion (w, &region);
 
1044
        }
 
1045
    }
 
1046
}
 
1047
 
 
1048
void
 
1049
updateWindowRegion (CompWindow *w)
 
1050
{
 
1051
    REGION     rect;
 
1052
    XRectangle r, *rects, *shapeRects = 0;
 
1053
    int        i, n = 0;
 
1054
 
 
1055
    EMPTY_REGION (w->region);
 
1056
 
 
1057
    if (w->screen->display->shapeExtension)
 
1058
    {
 
1059
        int order;
 
1060
 
 
1061
        shapeRects = XShapeGetRectangles (w->screen->display->display, w->id,
 
1062
                                          ShapeBounding, &n, &order);
 
1063
    }
 
1064
 
 
1065
    if (n < 2)
 
1066
    {
 
1067
        r.x      = -w->attrib.border_width;
 
1068
        r.y      = -w->attrib.border_width;
 
1069
        r.width  = w->width;
 
1070
        r.height = w->height;
 
1071
 
 
1072
        rects = &r;
 
1073
        n = 1;
 
1074
    }
 
1075
    else
 
1076
    {
 
1077
        rects = shapeRects;
 
1078
    }
 
1079
 
 
1080
    rect.rects = &rect.extents;
 
1081
    rect.numRects = rect.size = 1;
 
1082
 
 
1083
    for (i = 0; i < n; i++)
 
1084
    {
 
1085
        rect.extents.x1 = rects[i].x + w->attrib.border_width;
 
1086
        rect.extents.y1 = rects[i].y + w->attrib.border_width;
 
1087
        rect.extents.x2 = rect.extents.x1 + rects[i].width;
 
1088
        rect.extents.y2 = rect.extents.y1 + rects[i].height;
 
1089
 
 
1090
        if (rect.extents.x1 < 0)
 
1091
            rect.extents.x1 = 0;
 
1092
        if (rect.extents.y1 < 0)
 
1093
            rect.extents.y1 = 0;
 
1094
        if (rect.extents.x2 > w->width)
 
1095
            rect.extents.x2 = w->width;
 
1096
        if (rect.extents.y2 > w->height)
 
1097
            rect.extents.y2 = w->height;
 
1098
 
 
1099
        if (rect.extents.y1 < rect.extents.y2 &&
 
1100
            rect.extents.x1 < rect.extents.x2)
 
1101
        {
 
1102
            rect.extents.x1 += w->attrib.x;
 
1103
            rect.extents.y1 += w->attrib.y;
 
1104
            rect.extents.x2 += w->attrib.x;
 
1105
            rect.extents.y2 += w->attrib.y;
 
1106
 
 
1107
            XUnionRegion (&rect, w->region, w->region);
 
1108
        }
 
1109
    }
 
1110
 
 
1111
    if (shapeRects)
 
1112
        XFree (shapeRects);
 
1113
}
 
1114
 
 
1115
Bool
 
1116
updateWindowStruts (CompWindow *w)
 
1117
{
 
1118
    Atom          actual;
 
1119
    int           result, format;
 
1120
    unsigned long n, left;
 
1121
    unsigned long *struts = NULL;
 
1122
    Bool          hasOld, hasNew;
 
1123
    CompStruts    old, new;
 
1124
 
 
1125
#define MIN_EMPTY 76
 
1126
 
 
1127
    if (w->struts)
 
1128
    {
 
1129
        hasOld = TRUE;
 
1130
 
 
1131
        old.left   = w->struts->left;
 
1132
        old.right  = w->struts->right;
 
1133
        old.top    = w->struts->top;
 
1134
        old.bottom = w->struts->bottom;
 
1135
    }
 
1136
    else
 
1137
    {
 
1138
        hasOld = FALSE;
 
1139
    }
 
1140
 
 
1141
    hasNew = FALSE;
 
1142
 
 
1143
    new.left.x      = 0;
 
1144
    new.left.y      = 0;
 
1145
    new.left.width  = 0;
 
1146
    new.left.height = w->screen->height;
 
1147
 
 
1148
    new.right.x      = w->screen->width;
 
1149
    new.right.y      = 0;
 
1150
    new.right.width  = 0;
 
1151
    new.right.height = w->screen->height;
 
1152
 
 
1153
    new.top.x      = 0;
 
1154
    new.top.y      = 0;
 
1155
    new.top.width  = w->screen->width;
 
1156
    new.top.height = 0;
 
1157
 
 
1158
    new.bottom.x      = 0;
 
1159
    new.bottom.y      = w->screen->height;
 
1160
    new.bottom.width  = w->screen->width;
 
1161
    new.bottom.height = 0;
 
1162
 
 
1163
    result = XGetWindowProperty (w->screen->display->display, w->id,
 
1164
                                 w->screen->display->wmStrutPartialAtom,
 
1165
                                 0L, 12L, FALSE, XA_CARDINAL, &actual, &format,
 
1166
                                 &n, &left, (unsigned char **) &struts);
 
1167
 
 
1168
    if (result == Success && n && struts)
 
1169
    {
 
1170
        if (n == 12)
 
1171
        {
 
1172
            int gap;
 
1173
 
 
1174
            hasNew = TRUE;
 
1175
 
 
1176
            gap = w->screen->width - struts[0] - struts[1];
 
1177
            gap -= MIN_EMPTY;
 
1178
 
 
1179
            new.left.width  = (int) struts[0] + MIN (0, gap / 2);
 
1180
            new.right.width = (int) struts[1] + MIN (0, gap / 2);
 
1181
 
 
1182
            gap = w->screen->height - struts[2] - struts[3];
 
1183
            gap -= MIN_EMPTY;
 
1184
 
 
1185
            new.top.height    = (int) struts[2] + MIN (0, gap / 2);
 
1186
            new.bottom.height = (int) struts[3] + MIN (0, gap / 2);
 
1187
 
 
1188
            new.right.x  = w->screen->width  - new.right.width;
 
1189
            new.bottom.y = w->screen->height - new.bottom.height;
 
1190
 
 
1191
            new.left.y       = struts[4];
 
1192
            new.left.height  = struts[5] - new.left.y + 1;
 
1193
            new.right.y      = struts[6];
 
1194
            new.right.height = struts[7] - new.right.y + 1;
 
1195
 
 
1196
            new.top.x        = struts[8];
 
1197
            new.top.width    = struts[9] - new.top.x + 1;
 
1198
            new.bottom.x     = struts[10];
 
1199
            new.bottom.width = struts[11] - new.bottom.x + 1;
 
1200
        }
 
1201
 
 
1202
        XFree (struts);
 
1203
    }
 
1204
 
 
1205
    if (!hasNew)
 
1206
    {
 
1207
        result = XGetWindowProperty (w->screen->display->display, w->id,
 
1208
                                     w->screen->display->wmStrutAtom,
 
1209
                                     0L, 4L, FALSE, XA_CARDINAL,
 
1210
                                     &actual, &format, &n, &left,
 
1211
                                     (unsigned char **) &struts);
 
1212
 
 
1213
        if (result == Success && n && struts)
 
1214
        {
 
1215
            if (n == 4)
 
1216
            {
 
1217
                int gap;
 
1218
 
 
1219
                hasNew = TRUE;
 
1220
 
 
1221
                gap = w->screen->width - struts[0] - struts[1];
 
1222
                gap -= MIN_EMPTY;
 
1223
 
 
1224
                new.left.width  = (int) struts[0] + MIN (0, gap / 2);
 
1225
                new.right.width = (int) struts[1] + MIN (0, gap / 2);
 
1226
 
 
1227
                gap = w->screen->height - struts[2] - struts[3];
 
1228
                gap -= MIN_EMPTY;
 
1229
 
 
1230
                new.top.height    = (int) struts[2] + MIN (0, gap / 2);
 
1231
                new.bottom.height = (int) struts[3] + MIN (0, gap / 2);
 
1232
 
 
1233
                new.left.x  = 0;
 
1234
                new.right.x = w->screen->width - new.right.width;
 
1235
 
 
1236
                new.top.y    = 0;
 
1237
                new.bottom.y = w->screen->height - new.bottom.height;
 
1238
            }
 
1239
 
 
1240
            XFree (struts);
 
1241
        }
 
1242
    }
 
1243
 
 
1244
    if (hasOld != hasNew || (hasNew && hasOld &&
 
1245
                             memcmp (&new, &old, sizeof (CompStruts))))
 
1246
    {
 
1247
        if (hasNew)
 
1248
        {
 
1249
            if (!w->struts)
 
1250
                w->struts = malloc (sizeof (CompStruts));
 
1251
 
 
1252
            *w->struts = new;
 
1253
        }
 
1254
        else
 
1255
        {
 
1256
            free (w->struts);
 
1257
            w->struts = NULL;
 
1258
        }
 
1259
 
 
1260
        return TRUE;
 
1261
    }
 
1262
 
 
1263
    return FALSE;
 
1264
}
 
1265
 
 
1266
void
 
1267
addWindow (CompScreen *screen,
 
1268
           Window     id,
 
1269
           Window     aboveId)
 
1270
{
 
1271
    CompWindow *w;
 
1272
 
 
1273
    w = (CompWindow *) malloc (sizeof (CompWindow));
 
1274
    if (!w)
 
1275
        return;
 
1276
 
 
1277
    w->next = NULL;
 
1278
    w->prev = NULL;
 
1279
 
 
1280
    w->mapNum    = 0;
 
1281
    w->activeNum = 0;
 
1282
 
 
1283
    w->frame = None;
 
1284
 
 
1285
    w->placed    = FALSE;
 
1286
    w->minimized = FALSE;
 
1287
 
 
1288
    w->startupId = NULL;
 
1289
    w->resName   = NULL;
 
1290
    w->resClass  = NULL;
 
1291
 
 
1292
    initTexture (screen, &w->texture);
 
1293
 
 
1294
    w->screen    = screen;
 
1295
    w->pixmap    = None;
 
1296
    w->destroyed = FALSE;
 
1297
    w->damaged   = FALSE;
 
1298
 
 
1299
    w->destroyRefCnt = 1;
 
1300
    w->unmapRefCnt   = 1;
 
1301
 
 
1302
    w->group = NULL;
 
1303
 
 
1304
    w->damageRects = 0;
 
1305
    w->sizeDamage  = 0;
 
1306
    w->nDamage     = 0;
 
1307
 
 
1308
    w->vertices   = 0;
 
1309
    w->vertexSize = 0;
 
1310
    w->indices    = 0;
 
1311
    w->indexSize  = 0;
 
1312
    w->vCount     = 0;
 
1313
 
 
1314
    w->struts = 0;
 
1315
 
 
1316
    w->input.left   = 0;
 
1317
    w->input.right  = 0;
 
1318
    w->input.top    = 0;
 
1319
    w->input.bottom = 0;
 
1320
 
 
1321
    w->output.left   = 0;
 
1322
    w->output.right  = 0;
 
1323
    w->output.top    = 0;
 
1324
    w->output.bottom = 0;
 
1325
 
 
1326
    w->paint.opacity    = OPAQUE;
 
1327
    w->paint.brightness = 0xffff;
 
1328
    w->paint.saturation = COLOR;
 
1329
    w->paint.xScale     = 1.0f;
 
1330
    w->paint.yScale     = 1.0f;
 
1331
 
 
1332
    w->alive      = TRUE;
 
1333
    w->saturation = COLOR;
 
1334
 
 
1335
    w->scaled = FALSE;
 
1336
 
 
1337
    w->mwmDecor = MwmDecorAll;
 
1338
 
 
1339
    if (screen->windowPrivateLen)
 
1340
    {
 
1341
        w->privates = malloc (screen->windowPrivateLen * sizeof (CompPrivate));
 
1342
        if (!w->privates)
 
1343
        {
 
1344
            free (w);
 
1345
            return;
 
1346
        }
 
1347
    }
 
1348
    else
 
1349
        w->privates = 0;
 
1350
 
 
1351
    w->region = XCreateRegion ();
 
1352
    if (!w->region)
 
1353
    {
 
1354
        freeWindow (w);
 
1355
        return;
 
1356
    }
 
1357
 
 
1358
    w->clip = XCreateRegion ();
 
1359
    if (!w->clip)
 
1360
    {
 
1361
        freeWindow (w);
 
1362
        return;
 
1363
    }
 
1364
 
 
1365
    if (!XGetWindowAttributes (screen->display->display, id, &w->attrib))
 
1366
    {
 
1367
        freeWindow (w);
 
1368
        return;
 
1369
    }
 
1370
 
 
1371
    w->width  = w->attrib.width  + w->attrib.border_width * 2;
 
1372
    w->height = w->attrib.height + w->attrib.border_width * 2;
 
1373
 
 
1374
    w->sizeHints.flags = 0;
 
1375
 
 
1376
    recalcNormalHints (w);
 
1377
 
 
1378
    w->transientFor = None;
 
1379
    w->clientLeader = None;
 
1380
 
 
1381
    w->serverX = w->attrib.x;
 
1382
    w->serverY = w->attrib.y;
 
1383
 
 
1384
    w->syncAlarm      = None;
 
1385
    w->syncCounter    = 0;
 
1386
    w->syncWaitHandle = 0;
 
1387
 
 
1388
    w->syncWait        = FALSE;
 
1389
    w->syncX           = w->attrib.x;
 
1390
    w->syncY           = w->attrib.y;
 
1391
    w->syncWidth       = w->attrib.width;
 
1392
    w->syncHeight      = w->attrib.height;
 
1393
    w->syncBorderWidth = w->attrib.border_width;
 
1394
 
 
1395
    w->saveMask = 0;
 
1396
 
 
1397
    XSelectInput (screen->display->display, id,
 
1398
                  PropertyChangeMask |
 
1399
                  EnterWindowMask    |
 
1400
                  FocusChangeMask);
 
1401
 
 
1402
    w->id = id;
 
1403
 
 
1404
    XGrabButton (screen->display->display, AnyButton,
 
1405
                 AnyModifier, w->id, TRUE, ButtonPressMask |
 
1406
                 ButtonReleaseMask | ButtonMotionMask,
 
1407
                 GrabModeSync, GrabModeSync, None, None);
 
1408
 
 
1409
    w->inputHint = TRUE;
 
1410
    w->alpha     = (w->attrib.depth == 32);
 
1411
    w->wmType    = 0;
 
1412
    w->state     = 0;
 
1413
    w->actions   = 0;
 
1414
    w->protocols = 0;
 
1415
    w->type      = CompWindowTypeUnknownMask;
 
1416
    w->lastPong  = screen->display->lastPing;
 
1417
 
 
1418
    if (screen->display->shapeExtension)
 
1419
        XShapeSelectInput (screen->display->display, id, ShapeNotifyMask);
 
1420
 
 
1421
    insertWindowIntoScreen (screen, w, aboveId);
 
1422
 
 
1423
    EMPTY_REGION (w->region);
 
1424
 
 
1425
    if (w->attrib.class != InputOnly)
 
1426
    {
 
1427
        REGION rect;
 
1428
 
 
1429
        rect.rects = &rect.extents;
 
1430
        rect.numRects = rect.size = 1;
 
1431
 
 
1432
        rect.extents.x1 = w->attrib.x;
 
1433
        rect.extents.y1 = w->attrib.y;
 
1434
        rect.extents.x2 = w->attrib.x + w->width;
 
1435
        rect.extents.y2 = w->attrib.y + w->height;
 
1436
 
 
1437
        XUnionRegion (&rect, w->region, w->region);
 
1438
 
 
1439
        initTexture (screen, &w->texture);
 
1440
 
 
1441
        w->damage = XDamageCreate (screen->display->display, id,
 
1442
                                   XDamageReportRawRectangles);
 
1443
 
 
1444
        /* need to check for DisplayModal state on all windows */
 
1445
        w->state = getWindowState (w->screen->display, w->id);
 
1446
 
 
1447
        updateWindowClassHints (w);
 
1448
    }
 
1449
    else
 
1450
    {
 
1451
        w->damage = None;
 
1452
        w->attrib.map_state = IsUnmapped;
 
1453
    }
 
1454
 
 
1455
    if (testMode)
 
1456
    {
 
1457
        static int useAlpha = 0;
 
1458
 
 
1459
        w->attrib.map_state = IsViewable;
 
1460
        w->damaged          = TRUE;
 
1461
 
 
1462
        w->attrib.width  = 0;
 
1463
        w->attrib.height = 0;
 
1464
 
 
1465
        bindWindow (w);
 
1466
 
 
1467
        w->alpha = useAlpha;
 
1468
        useAlpha = !useAlpha;
 
1469
    }
 
1470
 
 
1471
    w->invisible = TRUE;
 
1472
 
 
1473
    if (!w->attrib.override_redirect)
 
1474
    {
 
1475
        updateNormalHints (w);
 
1476
        updateWindowStruts (w);
 
1477
 
 
1478
        updateWmHints (w);
 
1479
 
 
1480
        XGetTransientForHint (w->screen->display->display,
 
1481
                              w->id, &w->transientFor);
 
1482
 
 
1483
        w->clientLeader = getClientLeader (w);
 
1484
 
 
1485
        w->wmType = getWindowType (w->screen->display, w->id);
 
1486
 
 
1487
        recalcWindowType (w);
 
1488
 
 
1489
        w->mwmDecor  = getMwmDecor (w->screen->display, w->id);
 
1490
        w->protocols = getProtocols (w->screen->display, w->id);
 
1491
 
 
1492
        if (!(w->type & CompWindowTypeDesktopMask))
 
1493
            w->paint.opacity =
 
1494
                getWindowProp32 (w->screen->display, w->id,
 
1495
                                 w->screen->display->winOpacityAtom,
 
1496
                                 OPAQUE);
 
1497
 
 
1498
        w->paint.brightness =
 
1499
                getWindowProp32 (w->screen->display, w->id,
 
1500
                                 w->screen->display->winBrightnessAtom,
 
1501
                                 BRIGHT);
 
1502
 
 
1503
        if (w->screen->canDoSaturated)
 
1504
        {
 
1505
            w->saturation =
 
1506
                getWindowProp32 (w->screen->display, w->id,
 
1507
                                 w->screen->display->winSaturationAtom,
 
1508
                                 COLOR);
 
1509
            if (w->alive)
 
1510
                w->paint.saturation = w->saturation;
 
1511
        }
 
1512
    }
 
1513
 
 
1514
    if (w->attrib.map_state == IsViewable)
 
1515
    {
 
1516
        w->attrib.map_state = IsUnmapped;
 
1517
 
 
1518
        mapWindow (w);
 
1519
 
 
1520
        if (!w->attrib.override_redirect)
 
1521
            updateWindowAttributes (w);
 
1522
    }
 
1523
 
 
1524
    windowInitPlugins (w);
 
1525
}
 
1526
 
 
1527
void
 
1528
removeWindow (CompWindow *w)
 
1529
{
 
1530
    unhookWindowFromScreen (w->screen, w);
 
1531
 
 
1532
    if (w->attrib.map_state == IsViewable && w->damaged)
 
1533
    {
 
1534
        if (w->type == CompWindowTypeDesktopMask)
 
1535
            w->screen->desktopWindowCount--;
 
1536
 
 
1537
        if (w->struts)
 
1538
            updateWorkareaForScreen (w->screen);
 
1539
 
 
1540
        updateClientListForScreen (w->screen);
 
1541
    }
 
1542
    else if (w->state & CompWindowStateHiddenMask)
 
1543
    {
 
1544
        updateClientListForScreen (w->screen);
 
1545
    }
 
1546
 
 
1547
    windowFiniPlugins (w);
 
1548
 
 
1549
    freeWindow (w);
 
1550
}
 
1551
 
 
1552
void
 
1553
destroyWindow (CompWindow *w)
 
1554
{
 
1555
    w->id = 1;
 
1556
    w->mapNum = 0;
 
1557
 
 
1558
    w->destroyRefCnt--;
 
1559
    if (w->destroyRefCnt)
 
1560
        return;
 
1561
 
 
1562
    if (!w->destroyed)
 
1563
    {
 
1564
        w->destroyed = TRUE;
 
1565
        w->screen->pendingDestroys++;
 
1566
    }
 
1567
}
 
1568
 
 
1569
static void
 
1570
sendConfigureNotify (CompWindow *w)
 
1571
{
 
1572
    XConfigureEvent xev;
 
1573
 
 
1574
    xev.type         = ConfigureNotify;
 
1575
    xev.event        = w->id;
 
1576
    xev.window       = w->id;
 
1577
    xev.x            = w->serverX;
 
1578
    xev.y            = w->serverY;
 
1579
    xev.width        = w->attrib.width;
 
1580
    xev.height       = w->attrib.height;
 
1581
    xev.border_width = w->attrib.border_width;
 
1582
 
 
1583
    xev.above             = (w->prev) ? w->prev->id : None;
 
1584
    xev.override_redirect = w->attrib.override_redirect;
 
1585
 
 
1586
    XSendEvent (w->screen->display->display, w->id, FALSE,
 
1587
                StructureNotifyMask, (XEvent *) &xev);
 
1588
}
 
1589
 
 
1590
void
 
1591
mapWindow (CompWindow *w)
 
1592
{
 
1593
    if (w->attrib.class == InputOnly)
 
1594
        return;
 
1595
 
 
1596
    if (w->attrib.map_state == IsViewable)
 
1597
        return;
 
1598
 
 
1599
    w->unmapRefCnt = 1;
 
1600
 
 
1601
    w->attrib.map_state = IsViewable;
 
1602
 
 
1603
    setWmState (w->screen->display, NormalState, w->id);
 
1604
 
 
1605
    w->invisible = TRUE;
 
1606
    w->damaged   = FALSE;
 
1607
    w->alive     = TRUE;
 
1608
 
 
1609
    w->lastPong = w->screen->display->lastPing;
 
1610
 
 
1611
    w->mapNum = w->screen->mapNum++;
 
1612
 
 
1613
    updateWindowRegion (w);
 
1614
 
 
1615
    if (w->frame)
 
1616
        XMapWindow (w->screen->display->display, w->frame);
 
1617
 
 
1618
    if (w->struts)
 
1619
        updateWorkareaForScreen (w->screen);
 
1620
 
 
1621
    updateClientListForScreen (w->screen);
 
1622
 
 
1623
    if (w->type & CompWindowTypeDesktopMask)
 
1624
        w->screen->desktopWindowCount++;
 
1625
 
 
1626
    if (w->protocols & CompWindowProtocolSyncRequestMask)
 
1627
    {
 
1628
        sendSyncRequest (w);
 
1629
        sendConfigureNotify (w);
 
1630
    }
 
1631
}
 
1632
 
 
1633
void
 
1634
unmapWindow (CompWindow *w)
 
1635
{
 
1636
    w->mapNum = 0;
 
1637
 
 
1638
    if (w->frame)
 
1639
        XUnmapWindow (w->screen->display->display, w->frame);
 
1640
 
 
1641
    w->unmapRefCnt--;
 
1642
    if (w->unmapRefCnt > 0)
 
1643
        return;
 
1644
 
 
1645
    if (w->attrib.map_state != IsViewable)
 
1646
        return;
 
1647
 
 
1648
    if (w->type == CompWindowTypeDesktopMask)
 
1649
        w->screen->desktopWindowCount--;
 
1650
 
 
1651
    addWindowDamage (w);
 
1652
 
 
1653
    w->attrib.map_state = IsUnmapped;
 
1654
 
 
1655
    setWmState (w->screen->display, IconicState, w->id);
 
1656
 
 
1657
    w->invisible = TRUE;
 
1658
 
 
1659
    releaseWindow (w);
 
1660
 
 
1661
    if (w->struts)
 
1662
        updateWorkareaForScreen (w->screen);
 
1663
 
 
1664
    updateClientListForScreen (w->screen);
 
1665
}
 
1666
 
 
1667
static int
 
1668
restackWindow (CompWindow *w,
 
1669
               Window     aboveId)
 
1670
{
 
1671
    if (w->prev)
 
1672
    {
 
1673
        if (aboveId && aboveId == w->prev->id)
 
1674
            return 0;
 
1675
    }
 
1676
    else if (aboveId == None)
 
1677
        return 0;
 
1678
 
 
1679
    unhookWindowFromScreen (w->screen, w);
 
1680
    insertWindowIntoScreen (w->screen, w, aboveId);
 
1681
 
 
1682
    updateClientListForScreen (w->screen);
 
1683
 
 
1684
    return 1;
 
1685
}
 
1686
 
 
1687
Bool
 
1688
resizeWindow (CompWindow *w,
 
1689
              int        x,
 
1690
              int        y,
 
1691
              int        width,
 
1692
              int        height,
 
1693
              int        borderWidth)
 
1694
{
 
1695
    Window frame = None;
 
1696
 
 
1697
    if (x != w->serverX)
 
1698
    {
 
1699
        frame      = w->frame;
 
1700
        w->serverX = x;
 
1701
    }
 
1702
    else
 
1703
        x = w->attrib.x;
 
1704
 
 
1705
    if (y != w->serverY)
 
1706
    {
 
1707
        frame      = w->frame;
 
1708
        w->serverY = y;
 
1709
    }
 
1710
    else
 
1711
        y = w->attrib.y;
 
1712
 
 
1713
    if (frame)
 
1714
        XMoveWindow (w->screen->display->display, frame,
 
1715
                     w->serverX - w->input.left,
 
1716
                     w->serverY - w->input.top);
 
1717
 
 
1718
    if (w->attrib.width        != width  ||
 
1719
        w->attrib.height       != height ||
 
1720
        w->attrib.border_width != borderWidth)
 
1721
    {
 
1722
        unsigned int pw, ph, actualWidth, actualHeight, ui;
 
1723
        Pixmap       pixmap = None;
 
1724
        Window       root;
 
1725
        Status       result;
 
1726
        int          i;
 
1727
 
 
1728
        pw = width  + borderWidth * 2;
 
1729
        ph = height + borderWidth * 2;
 
1730
 
 
1731
        if (!w->invisible)
 
1732
        {
 
1733
            pixmap = XCompositeNameWindowPixmap (w->screen->display->display,
 
1734
                                                 w->id);
 
1735
            if (!pixmap)
 
1736
            {
 
1737
                fprintf (stderr, "%s: XCompositeNameWindowPixmap failed\n",
 
1738
                         programName);
 
1739
 
 
1740
                return FALSE;
 
1741
            }
 
1742
 
 
1743
            result = XGetGeometry (w->screen->display->display, pixmap, &root,
 
1744
                                   &i, &i, &actualWidth, &actualHeight,
 
1745
                                   &ui, &ui);
 
1746
 
 
1747
            if (actualWidth != pw || actualHeight != ph)
 
1748
            {
 
1749
                XFreePixmap (w->screen->display->display, pixmap);
 
1750
 
 
1751
                return FALSE;
 
1752
            }
 
1753
        }
 
1754
 
 
1755
        addWindowDamage (w);
 
1756
 
 
1757
        w->attrib.x            = x;
 
1758
        w->attrib.y            = y;
 
1759
        w->attrib.width        = width;
 
1760
        w->attrib.height       = height;
 
1761
        w->attrib.border_width = borderWidth;
 
1762
 
 
1763
        w->width  = pw;
 
1764
        w->height = ph;
 
1765
 
 
1766
        releaseWindow (w);
 
1767
 
 
1768
        w->pixmap = pixmap;
 
1769
 
 
1770
        if (w->mapNum)
 
1771
            updateWindowRegion (w);
 
1772
 
 
1773
        (*w->screen->windowResizeNotify) (w);
 
1774
 
 
1775
        addWindowDamage (w);
 
1776
 
 
1777
        w->invisible = WINDOW_INVISIBLE (w);
 
1778
 
 
1779
        updateFrameWindow (w);
 
1780
    }
 
1781
    else if (w->attrib.x != x || w->attrib.y != y)
 
1782
    {
 
1783
        int dx, dy;
 
1784
 
 
1785
        dx = x - w->attrib.x;
 
1786
        dy = y - w->attrib.y;
 
1787
 
 
1788
        moveWindow (w, dx, dy, TRUE);
 
1789
    }
 
1790
 
 
1791
    return TRUE;
 
1792
}
 
1793
 
 
1794
static void
 
1795
syncValueIncrement (XSyncValue *value)
 
1796
{
 
1797
    XSyncValue one;
 
1798
    int        overflow;
 
1799
 
 
1800
    XSyncIntToValue (&one, 1);
 
1801
    XSyncValueAdd (value, *value, one, &overflow);
 
1802
}
 
1803
 
 
1804
static Bool
 
1805
initializeSyncCounter (CompWindow *w)
 
1806
{
 
1807
    XSyncAlarmAttributes values;
 
1808
    Atom                 actual;
 
1809
    int                  result, format;
 
1810
    unsigned long        n, left;
 
1811
    unsigned long        *counter;
 
1812
 
 
1813
    if (w->syncCounter)
 
1814
        return w->syncAlarm != None;
 
1815
 
 
1816
    if (!(w->protocols & CompWindowProtocolSyncRequestMask))
 
1817
        return FALSE;
 
1818
 
 
1819
    result = XGetWindowProperty (w->screen->display->display, w->id,
 
1820
                                 w->screen->display->wmSyncRequestCounterAtom,
 
1821
                                 0L, 1L, FALSE, XA_CARDINAL, &actual, &format,
 
1822
                                 &n, &left, (unsigned char **) &counter);
 
1823
 
 
1824
    if (result == Success && n && counter)
 
1825
    {
 
1826
        w->syncCounter = *counter;
 
1827
 
 
1828
        XFree (counter);
 
1829
 
 
1830
        XSyncIntsToValue (&w->syncValue, (unsigned int) rand (), 0);
 
1831
        XSyncSetCounter (w->screen->display->display,
 
1832
                         w->syncCounter,
 
1833
                         w->syncValue);
 
1834
 
 
1835
        syncValueIncrement (&w->syncValue);
 
1836
 
 
1837
        values.events = TRUE;
 
1838
 
 
1839
        values.trigger.counter    = w->syncCounter;
 
1840
        values.trigger.wait_value = w->syncValue;
 
1841
 
 
1842
        values.trigger.value_type = XSyncAbsolute;
 
1843
        values.trigger.test_type  = XSyncPositiveComparison;
 
1844
 
 
1845
        XSyncIntToValue (&values.delta, 1);
 
1846
 
 
1847
        values.events = TRUE;
 
1848
 
 
1849
        compCheckForError (w->screen->display->display);
 
1850
 
 
1851
        /* Note that by default, the alarm increments the trigger value
 
1852
         * when it fires until the condition (counter.value < trigger.value)
 
1853
         * is FALSE again.
 
1854
         */
 
1855
        w->syncAlarm = XSyncCreateAlarm (w->screen->display->display,
 
1856
                                         XSyncCACounter   |
 
1857
                                         XSyncCAValue     |
 
1858
                                         XSyncCAValueType |
 
1859
                                         XSyncCATestType  |
 
1860
                                         XSyncCADelta     |
 
1861
                                         XSyncCAEvents,
 
1862
                                         &values);
 
1863
 
 
1864
        if (!compCheckForError (w->screen->display->display))
 
1865
            return TRUE;
 
1866
 
 
1867
        XSyncDestroyAlarm (w->screen->display->display, w->syncAlarm);
 
1868
        w->syncAlarm = None;
 
1869
    }
 
1870
 
 
1871
    return FALSE;
 
1872
}
 
1873
 
 
1874
static Bool
 
1875
syncWaitTimeout (void *closure)
 
1876
{
 
1877
    CompWindow *w = closure;
 
1878
 
 
1879
    w->syncWaitHandle = 0;
 
1880
    handleSyncAlarm (w);
 
1881
 
 
1882
    return FALSE;
 
1883
}
 
1884
 
 
1885
void
 
1886
sendSyncRequest (CompWindow *w)
 
1887
{
 
1888
    XClientMessageEvent xev;
 
1889
 
 
1890
    if (w->syncWait)
 
1891
        return;
 
1892
 
 
1893
    if (!initializeSyncCounter (w))
 
1894
        return;
 
1895
 
 
1896
    xev.type         = ClientMessage;
 
1897
    xev.window       = w->id;
 
1898
    xev.message_type = w->screen->display->wmProtocolsAtom;
 
1899
    xev.format       = 32;
 
1900
    xev.data.l[0]    = w->screen->display->wmSyncRequestAtom;
 
1901
    xev.data.l[1]    = CurrentTime;
 
1902
    xev.data.l[2]    = XSyncValueLow32 (w->syncValue);
 
1903
    xev.data.l[3]    = XSyncValueHigh32 (w->syncValue);
 
1904
    xev.data.l[4]    = 0;
 
1905
 
 
1906
    syncValueIncrement (&w->syncValue);
 
1907
 
 
1908
    XSendEvent (w->screen->display->display, w->id, FALSE, 0, (XEvent *) &xev);
 
1909
 
 
1910
    w->syncWait        = TRUE;
 
1911
    w->syncX           = w->serverX;
 
1912
    w->syncY           = w->serverY;
 
1913
    w->syncWidth       = w->attrib.width;
 
1914
    w->syncHeight      = w->attrib.height;
 
1915
    w->syncBorderWidth = w->attrib.border_width;
 
1916
 
 
1917
    if (!w->syncWaitHandle)
 
1918
        w->syncWaitHandle = compAddTimeout (1000, syncWaitTimeout, w);
 
1919
}
 
1920
 
 
1921
void
 
1922
configureWindow (CompWindow      *w,
 
1923
                 XConfigureEvent *ce)
 
1924
{
 
1925
    if (w->syncWait)
 
1926
    {
 
1927
        w->syncX           = ce->x;
 
1928
        w->syncY           = ce->y;
 
1929
        w->syncWidth       = ce->width;
 
1930
        w->syncHeight      = ce->height;
 
1931
        w->syncBorderWidth = ce->border_width;
 
1932
    }
 
1933
    else
 
1934
    {
 
1935
        resizeWindow (w, ce->x, ce->y, ce->width, ce->height,
 
1936
                      ce->border_width);
 
1937
    }
 
1938
 
 
1939
    w->attrib.override_redirect = ce->override_redirect;
 
1940
 
 
1941
    if (restackWindow (w, ce->above))
 
1942
        addWindowDamage (w);
 
1943
}
 
1944
 
 
1945
void
 
1946
circulateWindow (CompWindow      *w,
 
1947
                 XCirculateEvent *ce)
 
1948
{
 
1949
    Window newAboveId;
 
1950
 
 
1951
    if (ce->place == PlaceOnTop && w->screen->reverseWindows)
 
1952
        newAboveId = w->screen->reverseWindows->id;
 
1953
    else
 
1954
        newAboveId = 0;
 
1955
 
 
1956
    if (restackWindow (w, newAboveId))
 
1957
        addWindowDamage (w);
 
1958
}
 
1959
 
 
1960
void
 
1961
moveWindow (CompWindow *w,
 
1962
            int        dx,
 
1963
            int        dy,
 
1964
            Bool       damage)
 
1965
{
 
1966
    if (dx || dy)
 
1967
    {
 
1968
        if (damage)
 
1969
            addWindowDamage (w);
 
1970
 
 
1971
        w->attrib.x += dx;
 
1972
        w->attrib.y += dy;
 
1973
 
 
1974
        XOffsetRegion (w->region, dx, dy);
 
1975
 
 
1976
        setWindowMatrix (w);
 
1977
 
 
1978
        w->invisible = WINDOW_INVISIBLE (w);
 
1979
 
 
1980
        (*w->screen->windowMoveNotify) (w, dx, dy);
 
1981
 
 
1982
        if (damage)
 
1983
            addWindowDamage (w);
 
1984
    }
 
1985
}
 
1986
 
 
1987
void
 
1988
syncWindowPosition (CompWindow *w)
 
1989
{
 
1990
    if (w->attrib.x != w->serverX || w->attrib.y != w->serverY)
 
1991
    {
 
1992
        XMoveWindow (w->screen->display->display, w->id,
 
1993
                     w->attrib.x,
 
1994
                     w->attrib.y);
 
1995
 
 
1996
        if (0 && !w->attrib.override_redirect)
 
1997
        {
 
1998
            XConfigureEvent ce;
 
1999
 
 
2000
            ce.type              = ConfigureNotify;
 
2001
            ce.display           = w->screen->display->display;
 
2002
            ce.event             = w->id;
 
2003
            ce.window            = w->id;
 
2004
            ce.x                 = w->attrib.x;
 
2005
            ce.y                 = w->attrib.y;
 
2006
            ce.width             = w->attrib.width;
 
2007
            ce.height            = w->attrib.height;
 
2008
            ce.border_width      = w->attrib.border_width;
 
2009
            ce.above             = (w->prev) ? w->prev->id : None;
 
2010
            ce.override_redirect = FALSE;
 
2011
 
 
2012
            XSendEvent (w->screen->display->display,
 
2013
                        w->id, FALSE, StructureNotifyMask,
 
2014
                        (XEvent *) &ce);
 
2015
        }
 
2016
    }
 
2017
}
 
2018
 
 
2019
void
 
2020
setWindowScale (CompWindow *w,
 
2021
                float      xScale,
 
2022
                float      yScale)
 
2023
{
 
2024
    if (xScale > 0.999f && xScale < 1.001f &&
 
2025
        yScale > 0.999f && yScale < 1.001f)
 
2026
    {
 
2027
        w->paint.xScale = 1.0f;
 
2028
        w->paint.yScale = 1.0f;
 
2029
 
 
2030
        w->scaled = FALSE;
 
2031
    }
 
2032
    else
 
2033
    {
 
2034
        w->paint.xScale = xScale;
 
2035
        w->paint.yScale = yScale;
 
2036
 
 
2037
        w->scaled = TRUE;
 
2038
    }
 
2039
}
 
2040
 
 
2041
Bool
 
2042
focusWindow (CompWindow *w)
 
2043
{
 
2044
    if (w->attrib.override_redirect)
 
2045
        return FALSE;
 
2046
 
 
2047
    if (!w->mapNum || w->attrib.map_state != IsViewable)
 
2048
        return FALSE;
 
2049
 
 
2050
    if (w->attrib.x + w->width  <= 0    ||
 
2051
        w->attrib.y + w->height <= 0    ||
 
2052
        w->attrib.x >= w->screen->width ||
 
2053
        w->attrib.y >= w->screen->height)
 
2054
        return FALSE;
 
2055
 
 
2056
    if (w->inputHint || (w->protocols & CompWindowProtocolTakeFocusMask))
 
2057
        return TRUE;
 
2058
 
 
2059
    return FALSE;
 
2060
}
 
2061
 
 
2062
void
 
2063
windowResizeNotify (CompWindow *w)
 
2064
{
 
2065
}
 
2066
 
 
2067
void
 
2068
windowMoveNotify (CompWindow *w,
 
2069
                  int        dx,
 
2070
                  int        dy)
 
2071
{
 
2072
}
 
2073
 
 
2074
void
 
2075
windowGrabNotify (CompWindow   *w,
 
2076
                  int          x,
 
2077
                  int          y,
 
2078
                  unsigned int state,
 
2079
                  unsigned int mask)
 
2080
{
 
2081
}
 
2082
 
 
2083
void
 
2084
windowUngrabNotify (CompWindow *w)
 
2085
{
 
2086
}
 
2087
 
 
2088
static Bool
 
2089
isGroupTransient (CompWindow *w,
 
2090
                  Window     clientLeader)
 
2091
{
 
2092
    if (!clientLeader)
 
2093
        return FALSE;
 
2094
 
 
2095
    if (w->transientFor == None || w->transientFor == w->screen->root)
 
2096
    {
 
2097
        if (w->type & (CompWindowTypeDialogMask |
 
2098
                       CompWindowTypeModalDialogMask))
 
2099
        {
 
2100
            if (w->clientLeader == clientLeader)
 
2101
                return TRUE;
 
2102
        }
 
2103
    }
 
2104
 
 
2105
    return FALSE;
 
2106
}
 
2107
 
 
2108
static CompWindow *
 
2109
getModalTransient (CompWindow *window)
 
2110
{
 
2111
    CompWindow *w, *modalTransient;
 
2112
 
 
2113
    modalTransient = window;
 
2114
 
 
2115
    for (w = window->screen->reverseWindows; w; w = w->prev)
 
2116
    {
 
2117
        if (w == modalTransient || w->mapNum == 0)
 
2118
            continue;
 
2119
 
 
2120
        if (w->transientFor == modalTransient->id)
 
2121
        {
 
2122
            if (w->state & CompWindowStateModalMask)
 
2123
            {
 
2124
                modalTransient = w;
 
2125
                w = window->screen->reverseWindows;
 
2126
            }
 
2127
        }
 
2128
    }
 
2129
 
 
2130
    if (modalTransient == window)
 
2131
    {
 
2132
        for (w = window->screen->reverseWindows; w; w = w->prev)
 
2133
        {
 
2134
            if (w == modalTransient || w->mapNum == 0)
 
2135
                continue;
 
2136
 
 
2137
            if (isGroupTransient (w, modalTransient->clientLeader))
 
2138
            {
 
2139
                if (w->state & CompWindowStateModalMask)
 
2140
                {
 
2141
                    modalTransient = w;
 
2142
                    break;
 
2143
                }
 
2144
            }
 
2145
        }
 
2146
    }
 
2147
 
 
2148
    if (modalTransient == window)
 
2149
        modalTransient = NULL;
 
2150
 
 
2151
    return modalTransient;
 
2152
}
 
2153
 
 
2154
void
 
2155
moveInputFocusToWindow (CompWindow *w)
 
2156
{
 
2157
    CompDisplay *d = w->screen->display;
 
2158
    CompWindow  *modalTransient;
 
2159
 
 
2160
    modalTransient = getModalTransient (w);
 
2161
    if (modalTransient)
 
2162
        w = modalTransient;
 
2163
 
 
2164
    if (w->id != d->activeWindow)
 
2165
    {
 
2166
        if (w->inputHint || (w->protocols & CompWindowProtocolTakeFocusMask))
 
2167
            XSetInputFocus (d->display, w->id, RevertToPointerRoot,
 
2168
                            CurrentTime);
 
2169
    }
 
2170
}
 
2171
 
 
2172
static Bool
 
2173
isAncestorTo (CompWindow *transient,
 
2174
              CompWindow *ancestor)
 
2175
{
 
2176
    if (transient->transientFor)
 
2177
    {
 
2178
        if (transient->transientFor == ancestor->id)
 
2179
            return TRUE;
 
2180
 
 
2181
        transient = findWindowAtScreen (transient->screen,
 
2182
                                        transient->transientFor);
 
2183
        if (transient)
 
2184
            return isAncestorTo (transient, ancestor);
 
2185
    }
 
2186
 
 
2187
    return FALSE;
 
2188
}
 
2189
 
 
2190
 
 
2191
static Bool
 
2192
stackLayerCheck (CompWindow *w,
 
2193
                 Window     clientLeader,
 
2194
                 CompWindow *below)
 
2195
{
 
2196
    if (w->transientFor == below->id)
 
2197
        return TRUE;
 
2198
 
 
2199
    if (isAncestorTo (below, w))
 
2200
        return FALSE;
 
2201
 
 
2202
    if (clientLeader && below->clientLeader == clientLeader)
 
2203
        if (isGroupTransient (below, clientLeader))
 
2204
            return FALSE;
 
2205
 
 
2206
    if (w->state & CompWindowStateAboveMask)
 
2207
    {
 
2208
        return TRUE;
 
2209
    }
 
2210
    else if (w->state & CompWindowStateBelowMask)
 
2211
    {
 
2212
        if (below->state & CompWindowStateBelowMask)
 
2213
            return TRUE;
 
2214
    }
 
2215
    else if (!(below->state & CompWindowStateAboveMask))
 
2216
    {
 
2217
        return TRUE;
 
2218
    }
 
2219
 
 
2220
    return FALSE;
 
2221
}
 
2222
 
 
2223
/* goes through the stack, top-down until we find a window we should
 
2224
   stack above, normal windows can be stacked above fullscreen windows. */
 
2225
static CompWindow *
 
2226
findSibilingBelow (CompWindow *w)
 
2227
{
 
2228
    CompWindow   *below;
 
2229
    Window       clientLeader = w->clientLeader;
 
2230
    unsigned int type = w->type;
 
2231
 
 
2232
    /* normal stacking fullscreen windows with below state */
 
2233
    if ((type & CompWindowTypeFullscreenMask) &&
 
2234
        (w->state & CompWindowStateBelowMask))
 
2235
        type = CompWindowTypeNormalMask;
 
2236
 
 
2237
    if (w->transientFor || isGroupTransient (w, clientLeader))
 
2238
        clientLeader = None;
 
2239
 
 
2240
    for (below = w->screen->reverseWindows; below; below = below->prev)
 
2241
    {
 
2242
        if (below == w)
 
2243
            continue;
 
2244
 
 
2245
        if (below->attrib.override_redirect)
 
2246
            continue;
 
2247
 
 
2248
        if (below->attrib.map_state != IsViewable || below->mapNum == 0)
 
2249
            continue;
 
2250
 
 
2251
        /* always above desktop windows */
 
2252
        if (below->type & CompWindowTypeDesktopMask)
 
2253
            return below;
 
2254
 
 
2255
        switch (type) {
 
2256
        case CompWindowTypeDesktopMask:
 
2257
            /* desktop window layer */
 
2258
            break;
 
2259
        case CompWindowTypeFullscreenMask:
 
2260
        case CompWindowTypeDockMask:
 
2261
            /* fullscreen and dock layer */
 
2262
            if (below->type & (CompWindowTypeFullscreenMask |
 
2263
                               CompWindowTypeDockMask))
 
2264
            {
 
2265
                if (stackLayerCheck (w, clientLeader, below))
 
2266
                    return below;
 
2267
            }
 
2268
            else
 
2269
            {
 
2270
                return below;
 
2271
            }
 
2272
            break;
 
2273
        default:
 
2274
            /* fullscreen and normal layer */
 
2275
            if (!(below->type & CompWindowTypeDockMask))
 
2276
            {
 
2277
                if (stackLayerCheck (w, clientLeader, below))
 
2278
                    return below;
 
2279
            }
 
2280
            break;
 
2281
        }
 
2282
    }
 
2283
 
 
2284
    return NULL;
 
2285
}
 
2286
 
 
2287
static void
 
2288
saveWindowGeometry (CompWindow *w,
 
2289
                    int        mask)
 
2290
{
 
2291
    int m = mask & ~w->saveMask;
 
2292
 
 
2293
    /* only save geometry if window has been placed */
 
2294
    if (!w->placed)
 
2295
        return;
 
2296
 
 
2297
    if (m & CWX)
 
2298
        w->saveWc.x = w->attrib.x;
 
2299
 
 
2300
    if (m & CWY)
 
2301
        w->saveWc.y = w->attrib.y;
 
2302
 
 
2303
    if (m & CWWidth)
 
2304
        w->saveWc.width = w->attrib.width;
 
2305
 
 
2306
    if (m & CWHeight)
 
2307
        w->saveWc.height = w->attrib.height;
 
2308
 
 
2309
    if (m & CWBorderWidth)
 
2310
        w->saveWc.border_width = w->attrib.border_width;
 
2311
 
 
2312
    w->saveMask |= m;
 
2313
}
 
2314
 
 
2315
static int
 
2316
restoreWindowGeometry (CompWindow     *w,
 
2317
                       XWindowChanges *xwc,
 
2318
                       int            mask)
 
2319
{
 
2320
    int m = mask & w->saveMask;
 
2321
 
 
2322
    if (m & CWX)
 
2323
        xwc->x = w->saveWc.x;
 
2324
 
 
2325
    if (m & CWY)
 
2326
        xwc->y = w->saveWc.y;
 
2327
 
 
2328
    if (m & CWWidth)
 
2329
        xwc->width = w->saveWc.width;
 
2330
 
 
2331
    if (m & CWHeight)
 
2332
        xwc->height = w->saveWc.height;
 
2333
 
 
2334
    if (m & CWBorderWidth)
 
2335
        xwc->border_width = w->saveWc.border_width;
 
2336
 
 
2337
    w->saveMask &= ~mask;
 
2338
 
 
2339
    return m;
 
2340
}
 
2341
static void
 
2342
configureXWindow (Display        *dpy,
 
2343
                  CompWindow     *w,
 
2344
                  unsigned int   valueMask,
 
2345
                  XWindowChanges *xwc)
 
2346
{
 
2347
    XConfigureWindow (w->screen->display->display, w->id,
 
2348
                      valueMask, xwc);
 
2349
 
 
2350
    if (w->frame && (valueMask & (CWSibling | CWStackMode)))
 
2351
        XConfigureWindow (w->screen->display->display, w->frame,
 
2352
                          valueMask & (CWSibling | CWStackMode), xwc);
 
2353
}
 
2354
 
 
2355
static Bool
 
2356
stackTransients (CompWindow     *w,
 
2357
                 CompWindow     *avoid,
 
2358
                 XWindowChanges *xwc)
 
2359
{
 
2360
    CompWindow *t;
 
2361
    Window     clientLeader = w->clientLeader;
 
2362
 
 
2363
    if (w->transientFor || isGroupTransient (w, clientLeader))
 
2364
        clientLeader = None;
 
2365
 
 
2366
    for (t = w->screen->reverseWindows; t; t = t->prev)
 
2367
    {
 
2368
        if (t == w || t == avoid)
 
2369
            continue;
 
2370
 
 
2371
        if (t->transientFor == w->id || isGroupTransient (t, clientLeader))
 
2372
        {
 
2373
            if (!stackTransients (t, avoid, xwc))
 
2374
                return FALSE;
 
2375
 
 
2376
            if (xwc->sibling == t->id)
 
2377
                return FALSE;
 
2378
 
 
2379
            if (t->mapNum)
 
2380
                configureXWindow (w->screen->display->display, t,
 
2381
                                  CWSibling | CWStackMode, xwc);
 
2382
        }
 
2383
    }
 
2384
 
 
2385
    return TRUE;
 
2386
}
 
2387
 
 
2388
static void
 
2389
stackAncestors (CompWindow     *w,
 
2390
                XWindowChanges *xwc)
 
2391
{
 
2392
    if (w->transientFor && xwc->sibling != w->transientFor)
 
2393
    {
 
2394
        CompWindow *ancestor;
 
2395
 
 
2396
        ancestor = findWindowAtScreen (w->screen, w->transientFor);
 
2397
        if (ancestor)
 
2398
        {
 
2399
            if (!stackTransients (ancestor, w, xwc))
 
2400
                return;
 
2401
 
 
2402
            if (ancestor->mapNum)
 
2403
                configureXWindow (w->screen->display->display, ancestor,
 
2404
                                  CWSibling | CWStackMode,
 
2405
                                  xwc);
 
2406
 
 
2407
            stackAncestors (ancestor, xwc);
 
2408
        }
 
2409
    }
 
2410
    else if (isGroupTransient (w, w->clientLeader))
 
2411
    {
 
2412
        CompWindow *a;
 
2413
 
 
2414
        for (a = w->screen->reverseWindows; a; a = a->prev)
 
2415
        {
 
2416
            if (a->clientLeader == w->clientLeader &&
 
2417
                a->transientFor == None            &&
 
2418
                !isGroupTransient (a, w->clientLeader))
 
2419
            {
 
2420
                if (xwc->sibling == a->id)
 
2421
                    break;
 
2422
 
 
2423
                if (!stackTransients (a, w, xwc))
 
2424
                    break;
 
2425
 
 
2426
                if (a->mapNum)
 
2427
                    configureXWindow (w->screen->display->display, a,
 
2428
                                      CWSibling | CWStackMode,
 
2429
                                      xwc);
 
2430
            }
 
2431
        }
 
2432
    }
 
2433
}
 
2434
 
 
2435
void
 
2436
updateWindowAttributes (CompWindow *w)
 
2437
{
 
2438
    CompWindow     *sibiling;
 
2439
    XWindowChanges xwc;
 
2440
    int            mask = 0;
 
2441
 
 
2442
    if (w->state & CompWindowStateHiddenMask)
 
2443
        return;
 
2444
 
 
2445
    xwc.stack_mode = Above;
 
2446
    xwc.sibling    = None;
 
2447
 
 
2448
    sibiling = findSibilingBelow (w);
 
2449
    if (sibiling)
 
2450
        xwc.sibling = sibiling->id;
 
2451
 
 
2452
    if (xwc.sibling != w->id)
 
2453
    {
 
2454
        if (w->prev)
 
2455
        {
 
2456
            if (xwc.sibling == None)
 
2457
            {
 
2458
                XLowerWindow (w->screen->display->display, w->id);
 
2459
                if (w->frame)
 
2460
                    XLowerWindow (w->screen->display->display, w->frame);
 
2461
            }
 
2462
            else if (xwc.sibling != w->prev->id)
 
2463
                mask |= CWSibling | CWStackMode;
 
2464
        }
 
2465
        else if (xwc.sibling != None)
 
2466
            mask |= CWSibling | CWStackMode;
 
2467
    }
 
2468
 
 
2469
    /* only update fullscreen and maximized size if window is visible on
 
2470
       current viewport. Size is updated once we switch to the windows
 
2471
       viewport. */
 
2472
    if (w->attrib.x < w->screen->width && w->attrib.x + w->width > 0)
 
2473
    {
 
2474
        if (w->type & CompWindowTypeFullscreenMask)
 
2475
        {
 
2476
            saveWindowGeometry (w,
 
2477
                                CWX | CWY | CWWidth | CWHeight |
 
2478
                                CWBorderWidth);
 
2479
 
 
2480
            xwc.width        = w->screen->width;
 
2481
            xwc.height       = w->screen->height;
 
2482
            xwc.border_width = 0;
 
2483
 
 
2484
            mask |= CWWidth | CWHeight | CWBorderWidth;
 
2485
        }
 
2486
        else
 
2487
        {
 
2488
            mask |= restoreWindowGeometry (w, &xwc, CWBorderWidth);
 
2489
 
 
2490
            if (w->state & CompWindowStateMaximizedVertMask)
 
2491
            {
 
2492
                saveWindowGeometry (w, CWY | CWHeight);
 
2493
 
 
2494
                xwc.height = w->screen->workArea.height - w->input.top -
 
2495
                    w->input.bottom - w->attrib.border_width * 2;
 
2496
 
 
2497
                mask |= CWHeight;
 
2498
            }
 
2499
            else
 
2500
            {
 
2501
                mask |= restoreWindowGeometry (w, &xwc, CWY | CWHeight);
 
2502
            }
 
2503
 
 
2504
            if (w->state & CompWindowStateMaximizedHorzMask)
 
2505
            {
 
2506
                saveWindowGeometry (w, CWX | CWWidth);
 
2507
 
 
2508
                xwc.width = w->screen->workArea.width - w->input.left -
 
2509
                    w->input.right - w->attrib.border_width * 2;
 
2510
 
 
2511
                mask |= CWWidth;
 
2512
            }
 
2513
            else
 
2514
            {
 
2515
                mask |= restoreWindowGeometry (w, &xwc, CWX | CWWidth);
 
2516
            }
 
2517
        }
 
2518
    }
 
2519
 
 
2520
    if (mask & (CWWidth | CWHeight))
 
2521
    {
 
2522
        if (w->type & CompWindowTypeFullscreenMask)
 
2523
        {
 
2524
            xwc.x = 0;
 
2525
            xwc.y = 0;
 
2526
 
 
2527
            mask |= CWX | CWY;
 
2528
        }
 
2529
        else
 
2530
        {
 
2531
            int width, height, max;
 
2532
 
 
2533
            width  = (mask & CWWidth)  ? xwc.width  : w->attrib.width;
 
2534
            height = (mask & CWHeight) ? xwc.height : w->attrib.height;
 
2535
 
 
2536
            xwc.width  = w->attrib.width;
 
2537
            xwc.height = w->attrib.height;
 
2538
 
 
2539
            if (constrainNewWindowSize (w, width, height, &width, &height))
 
2540
            {
 
2541
                xwc.width  = width;
 
2542
                xwc.height = height;
 
2543
            }
 
2544
            else
 
2545
                mask &= ~(CWWidth | CWHeight);
 
2546
 
 
2547
            if (w->state & CompWindowStateMaximizedVertMask)
 
2548
            {
 
2549
                if (w->attrib.y < w->screen->workArea.y + w->input.top)
 
2550
                {
 
2551
                    xwc.y = w->screen->workArea.y + w->input.top;
 
2552
                    mask |= CWY;
 
2553
                }
 
2554
                else
 
2555
                {
 
2556
                    height = xwc.height + w->attrib.border_width * 2;
 
2557
 
 
2558
                    max = w->screen->workArea.y + w->screen->workArea.height;
 
2559
                    if (w->attrib.y + height + w->input.bottom > max)
 
2560
                    {
 
2561
                        xwc.y = max - height - w->input.bottom;
 
2562
                        mask |= CWY;
 
2563
                    }
 
2564
                }
 
2565
            }
 
2566
 
 
2567
            if (w->state & CompWindowStateMaximizedHorzMask)
 
2568
            {
 
2569
                if (w->attrib.x < w->screen->workArea.x + w->input.left)
 
2570
                {
 
2571
                    xwc.x = w->screen->workArea.x + w->input.left;
 
2572
                    mask |= CWX;
 
2573
                }
 
2574
                else
 
2575
                {
 
2576
                    width = xwc.width + w->attrib.border_width * 2;
 
2577
 
 
2578
                    max = w->screen->workArea.x + w->screen->workArea.width;
 
2579
                    if (w->attrib.x + width + w->input.right > max)
 
2580
                    {
 
2581
                        xwc.x = max - width - w->input.right;
 
2582
                        mask |= CWX;
 
2583
                    }
 
2584
                }
 
2585
            }
 
2586
        }
 
2587
    }
 
2588
 
 
2589
    if (!mask)
 
2590
        return;
 
2591
 
 
2592
    if (mask & (CWSibling | CWStackMode))
 
2593
    {
 
2594
        /* a normal window can be stacked above fullscreen windows but we
 
2595
           don't wont normal windows to be stacked above dock window so if
 
2596
           the sibiling we're stacking above is a fullscreen window we also
 
2597
           update all dock windows. */
 
2598
        if ((sibiling->type & CompWindowTypeFullscreenMask) &&
 
2599
            (!(w->type & (CompWindowTypeFullscreenMask |
 
2600
                          CompWindowTypeDockMask))))
 
2601
        {
 
2602
            CompWindow *dw;
 
2603
 
 
2604
            for (dw = w->screen->reverseWindows; dw; dw = dw->prev)
 
2605
                if (dw == sibiling)
 
2606
                    break;
 
2607
 
 
2608
            for (; dw; dw = dw->prev)
 
2609
                if (dw->type & CompWindowTypeDockMask)
 
2610
                    configureXWindow (w->screen->display->display, dw,
 
2611
                                      CWSibling | CWStackMode,
 
2612
                                      &xwc);
 
2613
        }
 
2614
 
 
2615
        /* transient children above */
 
2616
        if (stackTransients (w, NULL, &xwc))
 
2617
        {
 
2618
            configureXWindow (w->screen->display->display, w, mask, &xwc);
 
2619
 
 
2620
            /* ancestors, sibilings and sibiling transients below */
 
2621
            stackAncestors (w, &xwc);
 
2622
        }
 
2623
    }
 
2624
    else
 
2625
    {
 
2626
        configureXWindow (w->screen->display->display, w, mask, &xwc);
 
2627
    }
 
2628
}
 
2629
 
 
2630
void
 
2631
activateWindow (CompWindow *w)
 
2632
{
 
2633
    if (w->state & CompWindowStateHiddenMask)
 
2634
    {
 
2635
        if (w->minimized)
 
2636
            unminimizeWindow (w);
 
2637
 
 
2638
        if (w->screen->showingDesktopMask)
 
2639
            leaveShowDesktopMode (w->screen);
 
2640
 
 
2641
        showWindow (w);
 
2642
    }
 
2643
 
 
2644
    if (w->state & CompWindowStateHiddenMask)
 
2645
        return;
 
2646
 
 
2647
    updateWindowAttributes (w);
 
2648
 
 
2649
    if (!(w->type & CompWindowTypeDockMask))
 
2650
        moveInputFocusToWindow (w);
 
2651
}
 
2652
 
 
2653
void
 
2654
closeWindow (CompWindow *w)
 
2655
{
 
2656
    CompDisplay *display = w->screen->display;
 
2657
 
 
2658
    if (w->actions & CompWindowActionCloseMask)
 
2659
    {
 
2660
        XEvent ev;
 
2661
 
 
2662
        ev.type                 = ClientMessage;
 
2663
        ev.xclient.window       = w->id;
 
2664
        ev.xclient.message_type = display->wmProtocolsAtom;
 
2665
        ev.xclient.format       = 32;
 
2666
        ev.xclient.data.l[0]    = display->wmDeleteWindowAtom;
 
2667
        ev.xclient.data.l[1]    = CurrentTime;
 
2668
        ev.xclient.data.l[2]    = 0;
 
2669
        ev.xclient.data.l[3]    = 0;
 
2670
        ev.xclient.data.l[4]    = 0;
 
2671
 
 
2672
        XSendEvent (display->display, w->id, FALSE, NoEventMask, &ev);
 
2673
    }
 
2674
    else
 
2675
    {
 
2676
        XKillClient (display->display, w->id);
 
2677
    }
 
2678
}
 
2679
 
 
2680
void
 
2681
getOuterRectOfWindow (CompWindow *w,
 
2682
                      XRectangle *r)
 
2683
{
 
2684
    r->x      = w->attrib.x - w->input.left;
 
2685
    r->y      = w->attrib.y - w->input.top;
 
2686
    r->width  = w->width  + w->input.left + w->input.right;
 
2687
    r->height = w->height + w->input.top  + w->input.bottom;
 
2688
}
 
2689
 
 
2690
Bool
 
2691
constrainNewWindowSize (CompWindow *w,
 
2692
                        int        width,
 
2693
                        int        height,
 
2694
                        int        *newWidth,
 
2695
                        int        *newHeight)
 
2696
{
 
2697
    const XSizeHints *hints = &w->sizeHints;
 
2698
    int              min_width = 0;
 
2699
    int              min_height = 0;
 
2700
    int              base_width = 0;
 
2701
    int              base_height = 0;
 
2702
    int              xinc = 1;
 
2703
    int              yinc = 1;
 
2704
    int              max_width = MAXSHORT;
 
2705
    int              max_height = MAXSHORT;
 
2706
 
 
2707
    /* Ater gdk_window_constrain_size(), which is partially borrowed from fvwm.
 
2708
     *
 
2709
     * Copyright 1993, Robert Nation
 
2710
     *     You may use this code for any purpose, as long as the original
 
2711
     *     copyright remains in the source code and all documentation
 
2712
     *
 
2713
     * which in turn borrows parts of the algorithm from uwm
 
2714
     */
 
2715
 
 
2716
#define FLOOR(value, base) (((int) ((value) / (base))) * (base))
 
2717
#define FLOOR64(value, base) (((uint64_t) ((value) / (base))) * (base))
 
2718
#define CLAMP(v, min, max) ((v) <= (min) ? (min) : (v) >= (max) ? (max) : (v))
 
2719
 
 
2720
    if ((hints->flags & PBaseSize) && (hints->flags & PMinSize))
 
2721
    {
 
2722
        base_width = hints->base_width;
 
2723
        base_height = hints->base_height;
 
2724
        min_width = hints->min_width;
 
2725
        min_height = hints->min_height;
 
2726
    }
 
2727
    else if (hints->flags & PBaseSize)
 
2728
    {
 
2729
        base_width = hints->base_width;
 
2730
        base_height = hints->base_height;
 
2731
        min_width = hints->base_width;
 
2732
        min_height = hints->base_height;
 
2733
    }
 
2734
    else if (hints->flags & PMinSize)
 
2735
    {
 
2736
        base_width = hints->min_width;
 
2737
        base_height = hints->min_height;
 
2738
        min_width = hints->min_width;
 
2739
        min_height = hints->min_height;
 
2740
    }
 
2741
 
 
2742
    if (hints->flags & PMaxSize)
 
2743
    {
 
2744
        max_width = hints->max_width ;
 
2745
        max_height = hints->max_height;
 
2746
    }
 
2747
 
 
2748
    if (hints->flags & PResizeInc)
 
2749
    {
 
2750
        xinc = MAX (xinc, hints->width_inc);
 
2751
        yinc = MAX (yinc, hints->height_inc);
 
2752
    }
 
2753
 
 
2754
    /* clamp width and height to min and max values */
 
2755
    width  = CLAMP (width, min_width, max_width);
 
2756
    height = CLAMP (height, min_height, max_height);
 
2757
 
 
2758
    /* shrink to base + N * inc */
 
2759
    width  = base_width + FLOOR (width - base_width, xinc);
 
2760
    height = base_height + FLOOR (height - base_height, yinc);
 
2761
 
 
2762
    /* constrain aspect ratio, according to:
 
2763
     *
 
2764
     * min_aspect.x       width      max_aspect.x
 
2765
     * ------------  <= -------- <=  -----------
 
2766
     * min_aspect.y       height     max_aspect.y
 
2767
     */
 
2768
    if (hints->flags & PAspect &&
 
2769
        hints->min_aspect.y > 0 && hints->max_aspect.x > 0)
 
2770
    {
 
2771
        /* Use 64 bit arithmetic to prevent overflow */
 
2772
 
 
2773
        uint64_t min_aspect_x = hints->min_aspect.x;
 
2774
        uint64_t min_aspect_y = hints->min_aspect.y;
 
2775
        uint64_t max_aspect_x = hints->max_aspect.x;
 
2776
        uint64_t max_aspect_y = hints->max_aspect.y;
 
2777
        uint64_t delta;
 
2778
 
 
2779
        if (min_aspect_x * height > width * min_aspect_y)
 
2780
        {
 
2781
            delta = FLOOR64 (height - width * min_aspect_y / min_aspect_x, yinc);
 
2782
            if (height - delta >= min_height)
 
2783
                height -= delta;
 
2784
            else
 
2785
            {
 
2786
                delta = FLOOR64 (height * min_aspect_x / min_aspect_y - width,
 
2787
                                 xinc);
 
2788
                if (width + delta <= max_width)
 
2789
                    width += delta;
 
2790
            }
 
2791
        }
 
2792
 
 
2793
        if (width * max_aspect_y > max_aspect_x * height)
 
2794
        {
 
2795
            delta = FLOOR64 (width - height * max_aspect_x / max_aspect_y, xinc);
 
2796
            if (width - delta >= min_width)
 
2797
                width -= delta;
 
2798
            else
 
2799
            {
 
2800
                delta = FLOOR64 (width * min_aspect_y / max_aspect_y - height,
 
2801
                                 yinc);
 
2802
                if (height + delta <= max_height)
 
2803
                    height += delta;
 
2804
            }
 
2805
        }
 
2806
    }
 
2807
 
 
2808
#undef FLOOR
 
2809
#undef FLOOR64
 
2810
 
 
2811
    if (width != w->attrib.width || height != w->attrib.height)
 
2812
    {
 
2813
        *newWidth  = width;
 
2814
        *newHeight = height;
 
2815
 
 
2816
        return TRUE;
 
2817
    }
 
2818
 
 
2819
    return FALSE;
 
2820
}
 
2821
 
 
2822
void
 
2823
hideWindow (CompWindow *w)
 
2824
{
 
2825
    if (w->attrib.map_state != IsViewable)
 
2826
        return;
 
2827
 
 
2828
    if (w->state & CompWindowStateHiddenMask)
 
2829
        return;
 
2830
 
 
2831
    if (!w->minimized && !(w->type & w->screen->showingDesktopMask))
 
2832
        return;
 
2833
 
 
2834
    w->state |= CompWindowStateHiddenMask;
 
2835
 
 
2836
    XUnmapWindow (w->screen->display->display, w->id);
 
2837
 
 
2838
    setWindowState (w->screen->display, w->state, w->id);
 
2839
}
 
2840
 
 
2841
void
 
2842
showWindow (CompWindow *w)
 
2843
{
 
2844
    if (!(w->state & CompWindowStateHiddenMask))
 
2845
        return;
 
2846
 
 
2847
    if (w->minimized || (w->type & w->screen->showingDesktopMask))
 
2848
        return;
 
2849
 
 
2850
    w->state &= ~CompWindowStateHiddenMask;
 
2851
 
 
2852
    XMapWindow (w->screen->display->display, w->id);
 
2853
 
 
2854
    setWindowState (w->screen->display, w->state, w->id);
 
2855
}
 
2856
 
 
2857
static void
 
2858
minimizeTransients (CompWindow *w,
 
2859
                    void       *closure)
 
2860
{
 
2861
    CompWindow *ancestor = closure;
 
2862
 
 
2863
    if (w->transientFor == ancestor->id ||
 
2864
        isGroupTransient (w, ancestor->clientLeader))
 
2865
        minimizeWindow (w);
 
2866
}
 
2867
 
 
2868
void
 
2869
minimizeWindow (CompWindow *w)
 
2870
{
 
2871
    if (!w->minimized)
 
2872
    {
 
2873
        w->minimized = TRUE;
 
2874
 
 
2875
        forEachWindowOnScreen (w->screen, minimizeTransients, (void *) w);
 
2876
 
 
2877
        hideWindow (w);
 
2878
    }
 
2879
}
 
2880
 
 
2881
static void
 
2882
unminimizeTransients (CompWindow *w,
 
2883
                      void       *closure)
 
2884
{
 
2885
    CompWindow *ancestor = closure;
 
2886
 
 
2887
    if (w->transientFor == ancestor->id ||
 
2888
        isGroupTransient (w, ancestor->clientLeader))
 
2889
        unminimizeWindow (w);
 
2890
}
 
2891
 
 
2892
void
 
2893
unminimizeWindow (CompWindow *w)
 
2894
{
 
2895
    if (w->minimized)
 
2896
    {
 
2897
        w->minimized = FALSE;
 
2898
 
 
2899
        showWindow (w);
 
2900
 
 
2901
        forEachWindowOnScreen (w->screen, unminimizeTransients, (void *) w);
 
2902
    }
 
2903
}