~ctwm/ctwm/trunk

« back to all changes in this revision

Viewing changes to win_iconify.c

  • Committer: Matthew Fuller
  • Date: 2016-09-03 04:29:20 UTC
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: fullermd@over-yonder.net-20160903042920-4m0rvevk8z1dejcs
Bust functions related to iconifying/deiconifying windows out of
menus.c (the obvious place) and into their own file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Window de/iconification handling
 
3
 */
 
4
 
 
5
#include "ctwm.h"
 
6
 
 
7
#include <stdlib.h>
 
8
 
 
9
#include <X11/extensions/shape.h>
 
10
 
 
11
#include "add_window.h"
 
12
#include "events.h"
 
13
#include "functions.h"
 
14
#include "iconmgr.h"
 
15
#include "icons.h"
 
16
#include "menus.h"
 
17
#include "otp.h"
 
18
#include "screen.h"
 
19
#include "util.h"
 
20
#include "win_iconify.h"
 
21
#include "workspace_manager.h"
 
22
 
 
23
 
 
24
/* Animations */
 
25
static void MosaicFade(TwmWindow *tmp_win, Window blanket);
 
26
static void ZoomInWindow(TwmWindow *tmp_win, Window blanket);
 
27
static void ZoomOutWindow(TwmWindow *tmp_win, Window blanket);
 
28
static void FadeWindow(TwmWindow *tmp_win, Window blanket);
 
29
static void SweepWindow(TwmWindow *tmp_win, Window blanket);
 
30
 
 
31
/* De/iconify utils */
 
32
static void ReMapOne(TwmWindow *t, TwmWindow *leader);
 
33
static void waitamoment(float timeout);
 
34
 
 
35
 
 
36
 
 
37
/*
 
38
 * The main routines for iconifying...
 
39
 */
 
40
void
 
41
Iconify(TwmWindow *tmp_win, int def_x, int def_y)
 
42
{
 
43
        TwmWindow *t;
 
44
        bool iconify;
 
45
        long eventMask;
 
46
        WList *wl;
 
47
        Window leader = (Window) - 1;
 
48
        Window blanket = (Window) - 1;
 
49
 
 
50
        iconify = (!tmp_win->iconify_by_unmapping);
 
51
        t = NULL;
 
52
        if(tmp_win->istransient) {
 
53
                leader = tmp_win->transientfor;
 
54
                t = GetTwmWindow(leader);
 
55
        }
 
56
        else if((leader = tmp_win->group) != 0 && leader != tmp_win->w) {
 
57
                t = GetTwmWindow(leader);
 
58
        }
 
59
        if(t && t->icon_on) {
 
60
                iconify = false;
 
61
        }
 
62
        if(iconify) {
 
63
                if(!tmp_win->icon || !tmp_win->icon->w) {
 
64
                        CreateIconWindow(tmp_win, def_x, def_y);
 
65
                }
 
66
                else {
 
67
                        IconUp(tmp_win);
 
68
                }
 
69
                if(visible(tmp_win)) {
 
70
                        if(Scr->WindowMask) {
 
71
                                OtpRaise(tmp_win, IconWin);
 
72
                                XMapWindow(dpy, tmp_win->icon->w);
 
73
                        }
 
74
                        else {
 
75
                                OtpRaise(tmp_win, IconWin);
 
76
                                XMapWindow(dpy, tmp_win->icon->w);
 
77
                        }
 
78
                }
 
79
        }
 
80
        if(tmp_win->iconmanagerlist) {
 
81
                for(wl = tmp_win->iconmanagerlist; wl != NULL; wl = wl->nextv) {
 
82
                        XMapWindow(dpy, wl->icon);
 
83
                }
 
84
        }
 
85
 
 
86
        /* Don't mask anything yet, just get the current for various uses */
 
87
        eventMask = mask_out_event(tmp_win->w, 0);
 
88
 
 
89
        /* iconify transients and window group first */
 
90
        UnmapTransients(tmp_win, iconify, eventMask);
 
91
 
 
92
        if(iconify) {
 
93
                Zoom(tmp_win->frame, tmp_win->icon->w);
 
94
        }
 
95
 
 
96
        /*
 
97
         * Prevent the receipt of an UnmapNotify, since that would
 
98
         * cause a transition to the Withdrawn state.
 
99
         */
 
100
        tmp_win->mapped = false;
 
101
 
 
102
        if((Scr->IconifyStyle != ICONIFY_NORMAL) && !Scr->WindowMask) {
 
103
                XWindowAttributes winattrs;
 
104
                XSetWindowAttributes attr;
 
105
 
 
106
                XGetWindowAttributes(dpy, tmp_win->frame, &winattrs);
 
107
                attr.backing_store = NotUseful;
 
108
                attr.save_under    = False;
 
109
                blanket = XCreateWindow(dpy, Scr->Root, winattrs.x, winattrs.y,
 
110
                                        winattrs.width, winattrs.height, 0,
 
111
                                        CopyFromParent, CopyFromParent,
 
112
                                        CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
 
113
                XMapWindow(dpy, blanket);
 
114
        }
 
115
 
 
116
        mask_out_event_mask(tmp_win->w, StructureNotifyMask, eventMask);
 
117
        XUnmapWindow(dpy, tmp_win->w);
 
118
        XUnmapWindow(dpy, tmp_win->frame);
 
119
        restore_mask(tmp_win->w, eventMask);
 
120
 
 
121
        SetMapStateProp(tmp_win, IconicState);
 
122
 
 
123
        if((Scr->IconifyStyle != ICONIFY_NORMAL) && !Scr->WindowMask) {
 
124
                switch(Scr->IconifyStyle) {
 
125
                        case ICONIFY_MOSAIC:
 
126
                                MosaicFade(tmp_win, blanket);
 
127
                                break;
 
128
                        case ICONIFY_ZOOMIN:
 
129
                                ZoomInWindow(tmp_win, blanket);
 
130
                                break;
 
131
                        case ICONIFY_ZOOMOUT:
 
132
                                ZoomOutWindow(tmp_win, blanket);
 
133
                                break;
 
134
                        case ICONIFY_FADE:
 
135
                                FadeWindow(tmp_win, blanket);
 
136
                                break;
 
137
                        case ICONIFY_SWEEP:
 
138
                                SweepWindow(tmp_win, blanket);
 
139
                                break;
 
140
                        case ICONIFY_NORMAL:
 
141
                                /* Placate insufficiently smart clang warning */
 
142
                                break;
 
143
                }
 
144
                XDestroyWindow(dpy, blanket);
 
145
        }
 
146
        if(tmp_win == Scr->Focus) {
 
147
                SetFocus(NULL, LastTimestamp());
 
148
                if(! Scr->ClickToFocus) {
 
149
                        Scr->FocusRoot = true;
 
150
                }
 
151
        }
 
152
        tmp_win->isicon = true;
 
153
        tmp_win->icon_on = iconify;
 
154
        WMapIconify(tmp_win);
 
155
        if(! Scr->WindowMask && Scr->IconifyFunction.func != 0) {
 
156
                char *action;
 
157
                XEvent event;
 
158
 
 
159
                action = Scr->IconifyFunction.item ? Scr->IconifyFunction.item->action : NULL;
 
160
                ExecuteFunction(Scr->IconifyFunction.func, action,
 
161
                                (Window) 0, tmp_win, &event, C_ROOT, false);
 
162
        }
 
163
        XSync(dpy, 0);
 
164
}
 
165
 
 
166
 
 
167
/*
 
168
 * ... and its complement
 
169
 */
 
170
void
 
171
DeIconify(TwmWindow *tmp_win)
 
172
{
 
173
        TwmWindow *t = tmp_win;
 
174
        bool isicon = false;
 
175
 
 
176
        /* de-iconify the main window */
 
177
        if(Scr->WindowMask) {
 
178
                XRaiseWindow(dpy, Scr->WindowMask);
 
179
        }
 
180
        if(tmp_win->isicon) {
 
181
                isicon = true;
 
182
                if(tmp_win->icon_on && tmp_win->icon && tmp_win->icon->w) {
 
183
                        Zoom(tmp_win->icon->w, tmp_win->frame);
 
184
                }
 
185
                else if(tmp_win->group != (Window) 0) {
 
186
                        t = GetTwmWindow(tmp_win->group);
 
187
                        if(t && t->icon_on && t->icon && t->icon->w) {
 
188
                                Zoom(t->icon->w, tmp_win->frame);
 
189
                        }
 
190
                }
 
191
        }
 
192
 
 
193
        ReMapOne(tmp_win, t);
 
194
 
 
195
        if(isicon &&
 
196
                        (Scr->WarpCursor ||
 
197
                         LookInList(Scr->WarpCursorL, tmp_win->full_name, &tmp_win->class))) {
 
198
                WarpToWindow(tmp_win, false);
 
199
        }
 
200
 
 
201
        /* now de-iconify any window group transients */
 
202
        ReMapTransients(tmp_win);
 
203
 
 
204
        if(! Scr->WindowMask && Scr->DeIconifyFunction.func != 0) {
 
205
                char *action;
 
206
                XEvent event;
 
207
 
 
208
                action = Scr->DeIconifyFunction.item ?
 
209
                         Scr->DeIconifyFunction.item->action : NULL;
 
210
                ExecuteFunction(Scr->DeIconifyFunction.func, action,
 
211
                                (Window) 0, tmp_win, &event, C_ROOT, false);
 
212
        }
 
213
        XSync(dpy, 0);
 
214
}
 
215
 
 
216
 
 
217
 
 
218
/*
 
219
 * Animations for popping windows around.
 
220
 */
 
221
static void
 
222
MosaicFade(TwmWindow *tmp_win, Window blanket)
 
223
{
 
224
        int         srect;
 
225
        int         i, j, nrects;
 
226
        Pixmap      mask;
 
227
        GC          gc;
 
228
        XGCValues   gcv;
 
229
        XRectangle *rectangles;
 
230
        int  width = tmp_win->frame_width;
 
231
        int height = tmp_win->frame_height;
 
232
 
 
233
        srect = (width < height) ? (width / 20) : (height / 20);
 
234
        mask  = XCreatePixmap(dpy, blanket, width, height, 1);
 
235
 
 
236
        gcv.foreground = 1;
 
237
        gc = XCreateGC(dpy, mask, GCForeground, &gcv);
 
238
        XFillRectangle(dpy, mask, gc, 0, 0, width, height);
 
239
        gcv.function = GXclear;
 
240
        XChangeGC(dpy, gc, GCFunction, &gcv);
 
241
 
 
242
        nrects = ((width * height) / (srect * srect)) / 10;
 
243
        rectangles = calloc(nrects, sizeof(XRectangle));
 
244
        for(j = 0; j < nrects; j++) {
 
245
                rectangles [j].width  = srect;
 
246
                rectangles [j].height = srect;
 
247
        }
 
248
        for(i = 0; i < 10; i++) {
 
249
                for(j = 0; j < nrects; j++) {
 
250
                        rectangles [j].x = ((lrand48() %  width) / srect) * srect;
 
251
                        rectangles [j].y = ((lrand48() % height) / srect) * srect;
 
252
                }
 
253
                XFillRectangles(dpy, mask, gc, rectangles, nrects);
 
254
                XShapeCombineMask(dpy, blanket, ShapeBounding, 0, 0, mask, ShapeSet);
 
255
                XFlush(dpy);
 
256
                waitamoment(0.020);
 
257
        }
 
258
        XFreePixmap(dpy, mask);
 
259
        XFreeGC(dpy, gc);
 
260
        free(rectangles);
 
261
}
 
262
 
 
263
 
 
264
static void
 
265
ZoomInWindow(TwmWindow *tmp_win, Window blanket)
 
266
{
 
267
        Pixmap        mask;
 
268
        GC            gc, gcn;
 
269
        XGCValues     gcv;
 
270
 
 
271
        int i, nsteps = 20;
 
272
        int w = tmp_win->frame_width;
 
273
        int h = tmp_win->frame_height;
 
274
        int step = (MAX(w, h)) / (2.0 * nsteps);
 
275
 
 
276
        mask = XCreatePixmap(dpy, blanket, w, h, 1);
 
277
        gcv.foreground = 1;
 
278
        gc  = XCreateGC(dpy, mask, GCForeground, &gcv);
 
279
        gcv.function = GXclear;
 
280
        gcn = XCreateGC(dpy, mask, GCForeground | GCFunction, &gcv);
 
281
 
 
282
        for(i = 0; i < nsteps; i++) {
 
283
                XFillRectangle(dpy, mask, gcn, 0, 0, w, h);
 
284
                XFillArc(dpy, mask, gc, (w / 2) - ((nsteps - i) * step),
 
285
                         (h / 2) - ((nsteps - i) * step),
 
286
                         2 * (nsteps - i) * step,
 
287
                         2 * (nsteps - i) * step,
 
288
                         0, 360 * 64);
 
289
                XShapeCombineMask(dpy, blanket, ShapeBounding, 0, 0, mask, ShapeSet);
 
290
                XFlush(dpy);
 
291
                waitamoment(0.020);
 
292
        }
 
293
}
 
294
 
 
295
 
 
296
static void
 
297
ZoomOutWindow(TwmWindow *tmp_win, Window blanket)
 
298
{
 
299
        Pixmap        mask;
 
300
        GC            gc;
 
301
        XGCValues     gcv;
 
302
 
 
303
        int i, nsteps = 20;
 
304
        int w = tmp_win->frame_width;
 
305
        int h = tmp_win->frame_height;
 
306
        int step = (MAX(w, h)) / (2.0 * nsteps);
 
307
 
 
308
        mask  = XCreatePixmap(dpy, blanket, w, h, 1);
 
309
        gcv.foreground = 1;
 
310
        gc = XCreateGC(dpy, mask, GCForeground, &gcv);
 
311
        XFillRectangle(dpy, mask, gc, 0, 0, w, h);
 
312
        gcv.function = GXclear;
 
313
        XChangeGC(dpy, gc, GCFunction, &gcv);
 
314
 
 
315
        for(i = 0; i < nsteps; i++) {
 
316
                XFillArc(dpy, mask, gc, (w / 2) - (i * step),
 
317
                         (h / 2) - (i * step),
 
318
                         2 * i * step,
 
319
                         2 * i * step,
 
320
                         0, 360 * 64);
 
321
                XShapeCombineMask(dpy, blanket, ShapeBounding, 0, 0, mask, ShapeSet);
 
322
                XFlush(dpy);
 
323
                waitamoment(0.020);
 
324
        }
 
325
}
 
326
 
 
327
 
 
328
void
 
329
FadeWindow(TwmWindow *tmp_win, Window blanket)
 
330
{
 
331
        Pixmap        mask, stipple;
 
332
        GC            gc;
 
333
        XGCValues     gcv;
 
334
        static unsigned char stipple_bits[] = { 0x0F, 0x0F,
 
335
                                                0xF0, 0xF0,
 
336
                                                0x0F, 0x0F,
 
337
                                                0xF0, 0xF0,
 
338
                                                0x0F, 0x0F,
 
339
                                                0xF0, 0xF0,
 
340
                                                0x0F, 0x0F,
 
341
                                                0xF0, 0xF0,
 
342
                                              };
 
343
        int w = tmp_win->frame_width;
 
344
        int h = tmp_win->frame_height;
 
345
 
 
346
        stipple = XCreateBitmapFromData(dpy, blanket, (char *)stipple_bits, 8, 8);
 
347
        mask    = XCreatePixmap(dpy, blanket, w, h, 1);
 
348
        gcv.background = 0;
 
349
        gcv.foreground = 1;
 
350
        gcv.stipple    = stipple;
 
351
        gcv.fill_style = FillOpaqueStippled;
 
352
        gc = XCreateGC(dpy, mask, GCBackground | GCForeground | GCFillStyle | GCStipple,
 
353
                       &gcv);
 
354
        XFillRectangle(dpy, mask, gc, 0, 0, w, h);
 
355
 
 
356
        XShapeCombineMask(dpy, blanket, ShapeBounding, 0, 0, mask, ShapeSet);
 
357
        XFlush(dpy);
 
358
        waitamoment(0.10);
 
359
        XFreePixmap(dpy, stipple);
 
360
        XFlush(dpy);
 
361
}
 
362
 
 
363
 
 
364
static void
 
365
SweepWindow(TwmWindow *tmp_win, Window blanket)
 
366
{
 
367
        float step = 0.0;
 
368
        int i, nsteps = 20;
 
369
        int dir = 0, dist = tmp_win->frame_x, dist1;
 
370
 
 
371
        dist1 = tmp_win->frame_y;
 
372
        if(dist1 < dist) {
 
373
                dir = 1;
 
374
                dist = dist1;
 
375
        }
 
376
        dist1 = tmp_win->vs->w - (tmp_win->frame_x + tmp_win->frame_width);
 
377
        if(dist1 < dist) {
 
378
                dir = 2;
 
379
                dist = dist1;
 
380
        }
 
381
        dist1 = tmp_win->vs->h - (tmp_win->frame_y + tmp_win->frame_height);
 
382
        if(dist1 < dist) {
 
383
                dir = 3;
 
384
                dist = dist1;
 
385
        }
 
386
 
 
387
        switch(dir) {
 
388
                case 0:
 
389
                        step = tmp_win->frame_x + tmp_win->frame_width;
 
390
                        break;
 
391
                case 1:
 
392
                        step = tmp_win->frame_y + tmp_win->frame_height;
 
393
                        break;
 
394
                case 2:
 
395
                        step = tmp_win->vs->w - tmp_win->frame_x;
 
396
                        break;
 
397
                case 3:
 
398
                        step = tmp_win->vs->h - tmp_win->frame_y;
 
399
                        break;
 
400
        }
 
401
        step /= (float) nsteps;
 
402
        step /= (float) nsteps;
 
403
        for(i = 0; i < 20; i++) {
 
404
                int x = tmp_win->frame_x;
 
405
                int y = tmp_win->frame_y;
 
406
                switch(dir) {
 
407
                        case 0:
 
408
                                x -= i * i * step;
 
409
                                break;
 
410
                        case 1:
 
411
                                y -= i * i * step;
 
412
                                break;
 
413
                        case 2:
 
414
                                x += i * i * step;
 
415
                                break;
 
416
                        case 3:
 
417
                                y += i * i * step;
 
418
                                break;
 
419
                }
 
420
                XMoveWindow(dpy, blanket, x, y);
 
421
                XFlush(dpy);
 
422
                waitamoment(0.020);
 
423
        }
 
424
}
 
425
 
 
426
 
 
427
 
 
428
/*
 
429
 * Utils used by various bits above
 
430
 */
 
431
static void
 
432
ReMapOne(TwmWindow *t, TwmWindow *leader)
 
433
{
 
434
        if(t->icon_on) {
 
435
                Zoom(t->icon->w, t->frame);
 
436
        }
 
437
        else if(leader->icon) {
 
438
                Zoom(leader->icon->w, t->frame);
 
439
        }
 
440
 
 
441
        if(!t->squeezed) {
 
442
                XMapWindow(dpy, t->w);
 
443
        }
 
444
        t->mapped = true;
 
445
        if(False && Scr->Root != Scr->CaptiveRoot) {        /* XXX dubious test */
 
446
                ReparentWindow(dpy, t, WinWin, Scr->Root, t->frame_x, t->frame_y);
 
447
        }
 
448
        if(!Scr->NoRaiseDeicon) {
 
449
                OtpRaise(t, WinWin);
 
450
        }
 
451
        XMapWindow(dpy, t->frame);
 
452
        SetMapStateProp(t, NormalState);
 
453
 
 
454
        if(t->icon && t->icon->w) {
 
455
                XUnmapWindow(dpy, t->icon->w);
 
456
                IconDown(t);
 
457
                if(Scr->ShrinkIconTitles) {
 
458
                        t->icon->title_shrunk = true;
 
459
                }
 
460
        }
 
461
        if(t->iconmanagerlist) {
 
462
                WList *wl;
 
463
 
 
464
                for(wl = t->iconmanagerlist; wl != NULL; wl = wl->nextv) {
 
465
                        XUnmapWindow(dpy, wl->icon);
 
466
                }
 
467
        }
 
468
        t->isicon = false;
 
469
        t->icon_on = false;
 
470
        WMapDeIconify(t);
 
471
}
 
472
 
 
473
 
 
474
/*
 
475
 * Mostly internal util of iconification, but squeezing code needs it
 
476
 * too.
 
477
 */
 
478
void
 
479
ReMapTransients(TwmWindow *tmp_win)
 
480
{
 
481
        TwmWindow *t;
 
482
 
 
483
        /* find t such that it is a transient or group member window */
 
484
        for(t = Scr->FirstWindow; t != NULL; t = t->next) {
 
485
                if(t != tmp_win &&
 
486
                                ((t->istransient && t->transientfor == tmp_win->w) ||
 
487
                                 (t->group == tmp_win->w && t->isicon))) {
 
488
                        ReMapOne(t, tmp_win);
 
489
                }
 
490
        }
 
491
}
 
492
 
 
493
 
 
494
/*
 
495
 * Ditto previous note about squeezing.
 
496
 */
 
497
void
 
498
UnmapTransients(TwmWindow *tmp_win, bool iconify, long eventMask)
 
499
{
 
500
        TwmWindow *t;
 
501
 
 
502
        for(t = Scr->FirstWindow; t != NULL; t = t->next) {
 
503
                if(t != tmp_win &&
 
504
                                ((t->istransient && t->transientfor == tmp_win->w) ||
 
505
                                 t->group == tmp_win->w)) {
 
506
                        if(iconify) {
 
507
                                if(t->icon_on) {
 
508
                                        Zoom(t->icon->w, tmp_win->icon->w);
 
509
                                }
 
510
                                else if(tmp_win->icon) {
 
511
                                        Zoom(t->frame, tmp_win->icon->w);
 
512
                                }
 
513
                        }
 
514
 
 
515
                        /*
 
516
                         * Prevent the receipt of an UnmapNotify, since that would
 
517
                         * cause a transition to the Withdrawn state.
 
518
                         */
 
519
                        t->mapped = false;
 
520
 
 
521
                        /*
 
522
                         * Note that here, we're setting masks relative to what we
 
523
                         * were passed, which is that of the window these are
 
524
                         * transient for, rather than relative to these windows'
 
525
                         * current masks.  I believe in practice it's the same thing,
 
526
                         * and it saves getting attributes on each for masking.
 
527
                         * Still, a little odd...
 
528
                         */
 
529
                        mask_out_event_mask(t->w, StructureNotifyMask, eventMask);
 
530
                        XUnmapWindow(dpy, t->w);
 
531
                        XUnmapWindow(dpy, t->frame);
 
532
                        restore_mask(t->w, eventMask);
 
533
 
 
534
                        if(t->icon && t->icon->w) {
 
535
                                XUnmapWindow(dpy, t->icon->w);
 
536
                        }
 
537
                        SetMapStateProp(t, IconicState);
 
538
                        if(t == Scr->Focus) {
 
539
                                SetFocus(NULL, LastTimestamp());
 
540
                                if(! Scr->ClickToFocus) {
 
541
                                        Scr->FocusRoot = true;
 
542
                                }
 
543
                        }
 
544
                        if(t->iconmanagerlist) {
 
545
                                XMapWindow(dpy, t->iconmanagerlist->icon);
 
546
                        }
 
547
                        t->isicon = true;
 
548
                        t->icon_on = false;
 
549
                        WMapIconify(t);
 
550
                }
 
551
        }
 
552
}
 
553
 
 
554
 
 
555
static void
 
556
waitamoment(float timeout)
 
557
{
 
558
        struct timeval timeoutstruct;
 
559
        int usec = timeout * 1000000;
 
560
        timeoutstruct.tv_usec = usec % (unsigned long) 1000000;
 
561
        timeoutstruct.tv_sec  = usec / (unsigned long) 1000000;
 
562
        select(0, NULL, NULL, NULL, &timeoutstruct);
 
563
}