~ctwm/ctwm/trunk

« back to all changes in this revision

Viewing changes to workspace_utils.c

  • Committer: Matthew Fuller
  • Date: 2016-08-07 01:29:24 UTC
  • mto: This revision was merged to the branch mainline in revision 511.
  • Revision ID: fullermd@over-yonder.net-20160807012924-z20aildd4c0bi6gn
Break the general/utility workspace functions out into their own file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Various workspace handling and utilities.
 
3
 */
 
4
 
 
5
#include "ctwm.h"
 
6
 
 
7
#include <stdio.h>
 
8
#include <string.h>
 
9
#include <stdlib.h>
 
10
 
 
11
#include <X11/Xatom.h>
 
12
 
 
13
#include "animate.h"
 
14
#include "clicktofocus.h"
 
15
#include "ctwm_atoms.h"
 
16
#include "drawing.h"
 
17
#include "functions.h"
 
18
#include "iconmgr.h"
 
19
#include "image.h"
 
20
#include "otp.h"
 
21
#include "screen.h"
 
22
#include "util.h"
 
23
#include "workmgr.h"
 
24
#include "workspace_utils.h"
 
25
 
 
26
#ifdef EWMH
 
27
#  include "ewmh_atoms.h"
 
28
#endif
 
29
 
 
30
 
 
31
bool useBackgroundInfo = false;
 
32
 
 
33
 
 
34
/*
 
35
 * Various funcs for switching workspaces
 
36
 */
 
37
void GotoWorkSpaceByName(VirtualScreen *vs, char *wname)
 
38
{
 
39
        WorkSpace *ws;
 
40
 
 
41
        if(! Scr->workSpaceManagerActive) {
 
42
                return;
 
43
        }
 
44
        if(!vs) {
 
45
                return;
 
46
        }
 
47
        ws = GetWorkspace(wname);
 
48
        if(ws == NULL) {
 
49
                return;
 
50
        }
 
51
        GotoWorkSpace(vs, ws);
 
52
}
 
53
 
 
54
 
 
55
void GotoWorkSpaceByNumber(VirtualScreen *vs, int workspacenum)
 
56
{
 
57
        WorkSpace *ws;
 
58
        if(! Scr->workSpaceManagerActive) {
 
59
                return;
 
60
        }
 
61
        if(!vs) {
 
62
                return;
 
63
        }
 
64
        for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
 
65
                if(ws->number == workspacenum) {
 
66
                        break;
 
67
                }
 
68
        }
 
69
        if(ws == NULL) {
 
70
                return;
 
71
        }
 
72
        GotoWorkSpace(vs, ws);
 
73
}
 
74
 
 
75
 
 
76
void GotoPrevWorkSpace(VirtualScreen *vs)
 
77
{
 
78
        WorkSpace *ws1, *ws2;
 
79
 
 
80
        if(! Scr->workSpaceManagerActive) {
 
81
                return;
 
82
        }
 
83
        if(!vs) {
 
84
                return;
 
85
        }
 
86
        ws1 = Scr->workSpaceMgr.workSpaceList;
 
87
        if(ws1 == NULL) {
 
88
                return;
 
89
        }
 
90
        ws2 = ws1->next;
 
91
 
 
92
        while((ws2 != vs->wsw->currentwspc) && (ws2 != NULL)) {
 
93
                ws1 = ws2;
 
94
                ws2 = ws2->next;
 
95
        }
 
96
        GotoWorkSpace(vs, ws1);
 
97
}
 
98
 
 
99
 
 
100
void GotoNextWorkSpace(VirtualScreen *vs)
 
101
{
 
102
        WorkSpace *ws;
 
103
        if(! Scr->workSpaceManagerActive) {
 
104
                return;
 
105
        }
 
106
        if(!vs) {
 
107
                return;
 
108
        }
 
109
 
 
110
        ws = vs->wsw->currentwspc;
 
111
        ws = (ws->next != NULL) ? ws->next : Scr->workSpaceMgr.workSpaceList;
 
112
        GotoWorkSpace(vs, ws);
 
113
}
 
114
 
 
115
 
 
116
void GotoRightWorkSpace(VirtualScreen *vs)
 
117
{
 
118
        WorkSpace *ws;
 
119
        int number, columns, count;
 
120
 
 
121
        if(!Scr->workSpaceManagerActive) {
 
122
                return;
 
123
        }
 
124
        if(!vs) {
 
125
                return;
 
126
        }
 
127
 
 
128
        ws      = vs->wsw->currentwspc;
 
129
        number  = ws->number;
 
130
        columns = Scr->workSpaceMgr.columns;
 
131
        count   = Scr->workSpaceMgr.count;
 
132
        number++;
 
133
        if((number % columns) == 0) {
 
134
                number -= columns;
 
135
        }
 
136
        else if(number >= count) {
 
137
                number = (number / columns) * columns;
 
138
        }
 
139
 
 
140
        GotoWorkSpaceByNumber(vs, number);
 
141
}
 
142
 
 
143
 
 
144
void GotoLeftWorkSpace(VirtualScreen *vs)
 
145
{
 
146
        WorkSpace *ws;
 
147
        int number, columns, count;
 
148
 
 
149
        if(!Scr->workSpaceManagerActive) {
 
150
                return;
 
151
        }
 
152
        if(!vs) {
 
153
                return;
 
154
        }
 
155
 
 
156
        ws      = vs->wsw->currentwspc;
 
157
        number  = ws->number;
 
158
        columns = Scr->workSpaceMgr.columns;
 
159
        count   = Scr->workSpaceMgr.count;
 
160
        number += (number % columns) ? -1 : (columns - 1);
 
161
        if(number >= count) {
 
162
                number = count - 1;
 
163
        }
 
164
        GotoWorkSpaceByNumber(vs, number);
 
165
}
 
166
 
 
167
 
 
168
void GotoUpWorkSpace(VirtualScreen *vs)
 
169
{
 
170
        WorkSpace *ws;
 
171
        int number, lines, columns, count;
 
172
 
 
173
        if(!Scr->workSpaceManagerActive) {
 
174
                return;
 
175
        }
 
176
        if(!vs) {
 
177
                return;
 
178
        }
 
179
 
 
180
        ws      = vs->wsw->currentwspc;
 
181
        number  = ws->number;
 
182
        lines   = Scr->workSpaceMgr.lines;
 
183
        columns = Scr->workSpaceMgr.columns;
 
184
        count   = Scr->workSpaceMgr.count;
 
185
        number -=  columns;
 
186
        if(number < 0) {
 
187
                number += lines * columns;
 
188
                /* If the number of workspaces is not a multiple of nr of columns */
 
189
                if(number >= count) {
 
190
                        number -= columns;
 
191
                }
 
192
        }
 
193
        GotoWorkSpaceByNumber(vs, number);
 
194
}
 
195
 
 
196
 
 
197
void GotoDownWorkSpace(VirtualScreen *vs)
 
198
{
 
199
        WorkSpace *ws;
 
200
        int number, columns, count;
 
201
 
 
202
        if(!Scr->workSpaceManagerActive) {
 
203
                return;
 
204
        }
 
205
        if(!vs) {
 
206
                return;
 
207
        }
 
208
 
 
209
        ws      = vs->wsw->currentwspc;
 
210
        number  = ws->number;
 
211
        columns = Scr->workSpaceMgr.columns;
 
212
        count   = Scr->workSpaceMgr.count;
 
213
        number +=  columns;
 
214
        if(number >= count) {
 
215
                number %= columns;
 
216
        }
 
217
        GotoWorkSpaceByNumber(vs, number);
 
218
}
 
219
 
 
220
 
 
221
 
 
222
/*
 
223
 * Show the background (by hiding all windows) or undo it.
 
224
 * f.showbackground, also can be called via EWMH bits.
 
225
 *
 
226
 * newstate is the desired showing state.
 
227
 * Pass -1 to toggle, 1 to show the background,
 
228
 * or 0 to re-show the windows.
 
229
 *
 
230
 * XXX Doesn't really belong here; more of a functions.c-ish thing
 
231
 * probably.  But left here for the moment.
 
232
 */
 
233
void ShowBackground(VirtualScreen *vs, int newstate)
 
234
{
 
235
        static int state = 0;
 
236
        TwmWindow *twmWin;
 
237
 
 
238
        if(newstate == state) {
 
239
                return;
 
240
        }
 
241
 
 
242
        if(state) {
 
243
                for(twmWin = Scr->FirstWindow; twmWin != NULL; twmWin = twmWin->next) {
 
244
                        if(twmWin->savevs == vs) {
 
245
                                DisplayWin(vs, twmWin);
 
246
                        }
 
247
                        twmWin->savevs = NULL;
 
248
                }
 
249
                state = 0;
 
250
        }
 
251
        else {
 
252
                for(twmWin = Scr->FirstWindow; twmWin != NULL; twmWin = twmWin->next) {
 
253
                        if(twmWin->vs == vs
 
254
#ifdef EWMH
 
255
                                        /* leave wt_Desktop and wt_Dock visible */
 
256
                                        && twmWin->ewmhWindowType == wt_Normal
 
257
#endif /* EWMH */
 
258
                          ) {
 
259
                                twmWin->savevs = twmWin->vs;
 
260
                                Vanish(vs, twmWin);
 
261
                        }
 
262
                }
 
263
                state = 1;
 
264
        }
 
265
#ifdef EWMH
 
266
        EwmhSet_NET_SHOWING_DESKTOP(state);
 
267
#endif /* EWMH */
 
268
}
 
269
 
 
270
 
 
271
/*
 
272
 * Belongs with the above GotoWorkSpace* funcs
 
273
 */
 
274
void GotoWorkSpace(VirtualScreen *vs, WorkSpace *ws)
 
275
{
 
276
        TwmWindow            *twmWin;
 
277
        WorkSpace            *oldws, *newws;
 
278
        WList                *wl, *wl1;
 
279
        WinList              winl;
 
280
        XSetWindowAttributes attr;
 
281
        XWindowAttributes    winattrs;
 
282
        unsigned long        eventMask;
 
283
        IconMgr              *iconmgr;
 
284
        Window               oldw;
 
285
        Window               neww;
 
286
        TwmWindow            *focuswindow;
 
287
        VirtualScreen        *tmpvs;
 
288
 
 
289
        if(! Scr->workSpaceManagerActive) {
 
290
                return;
 
291
        }
 
292
        for(tmpvs = Scr->vScreenList; tmpvs != NULL; tmpvs = tmpvs->next) {
 
293
                if(ws == tmpvs->wsw->currentwspc) {
 
294
                        XBell(dpy, 0);
 
295
                        return;
 
296
                }
 
297
        }
 
298
        oldws = vs->wsw->currentwspc;
 
299
        newws = ws;
 
300
        if(oldws == newws) {
 
301
                return;
 
302
        }
 
303
 
 
304
        attr.backing_store = NotUseful;
 
305
        attr.save_under    = False;
 
306
 
 
307
        if(useBackgroundInfo && ! Scr->DontPaintRootWindow) {
 
308
                if(newws->image == NULL) {
 
309
                        XSetWindowBackground(dpy, vs->window, newws->backcp.back);
 
310
                }
 
311
                else {
 
312
                        XSetWindowBackgroundPixmap(dpy, vs->window, newws->image->pixmap);
 
313
                }
 
314
                XClearWindow(dpy, vs->window);
 
315
        }
 
316
 
 
317
        /* If SaveWorkspaceFocus is on, save the focus of the last window. */
 
318
        if(Scr->SaveWorkspaceFocus) {
 
319
                oldws->save_focus = Scr->Focus;
 
320
        }
 
321
 
 
322
        focuswindow = NULL;
 
323
        /* For better visual effect, the order or map/unmap is important:
 
324
           - map from top to bottom.
 
325
           - unmap from bottom to top.
 
326
           - unmap after mapping.
 
327
           The guiding factor: at any point during the transition, something
 
328
           should be visible only if it was visible before the transition or if
 
329
           it will be visible at the end.  */
 
330
        OtpCheckConsistency();
 
331
 
 
332
        for(twmWin = OtpTopWin(); twmWin != NULL;
 
333
                        twmWin = OtpNextWinDown(twmWin)) {
 
334
 
 
335
                if(OCCUPY(twmWin, newws)) {
 
336
                        if(!twmWin->vs) {
 
337
                                DisplayWin(vs, twmWin);
 
338
                        }
 
339
#ifdef EWMH
 
340
                        if(OCCUPY(twmWin, oldws)) {
 
341
                                /*
 
342
                                 * If the window remains visible, re-order the workspace
 
343
                                 * numbers in NET_WM_DESKTOP.
 
344
                                 */
 
345
                                EwmhSet_NET_WM_DESKTOP_ws(twmWin, newws);
 
346
                        }
 
347
#endif
 
348
                }
 
349
        }
 
350
 
 
351
        for(twmWin = OtpBottomWin(); twmWin != NULL;
 
352
                        twmWin = OtpNextWinUp(twmWin)) {
 
353
                if(twmWin->vs == vs) {
 
354
                        if(!OCCUPY(twmWin, newws)) {
 
355
                                VirtualScreen *tvs;
 
356
 
 
357
                                Vanish(vs, twmWin);
 
358
                                /*
 
359
                                 * Now that the window has Vanished from one virtual screen,
 
360
                                 * check to see if it is wanted on another one.
 
361
                                 * This is relatively rare, so don't bother with the
 
362
                                 * top-to-bottom order here.
 
363
                                 */
 
364
                                if(Scr->numVscreens > 1) {
 
365
                                        for(tvs = Scr->vScreenList; tvs != NULL; tvs = tvs->next) {
 
366
                                                if(tvs == vs) { /* no, not back on the old one */
 
367
                                                        continue;
 
368
                                                }
 
369
                                                if(OCCUPY(twmWin, tvs->wsw->currentwspc)) {
 
370
                                                        DisplayWin(tvs, twmWin);
 
371
                                                        break;
 
372
                                                }
 
373
                                        }
 
374
                                }
 
375
                        }
 
376
                        else if(twmWin->hasfocusvisible) {
 
377
                                focuswindow = twmWin;
 
378
                                SetFocusVisualAttributes(focuswindow, false);
 
379
                        }
 
380
                }
 
381
        }
 
382
        OtpCheckConsistency();
 
383
 
 
384
        /*
 
385
           Reorganize icon manager window lists
 
386
        */
 
387
        for(twmWin = Scr->FirstWindow; twmWin != NULL; twmWin = twmWin->next) {
 
388
                wl = twmWin->iconmanagerlist;
 
389
                if(wl == NULL) {
 
390
                        continue;
 
391
                }
 
392
                if(OCCUPY(wl->iconmgr->twm_win, newws)) {
 
393
                        continue;
 
394
                }
 
395
                wl1 = wl;
 
396
                wl  = wl->nextv;
 
397
                while(wl != NULL) {
 
398
                        if(OCCUPY(wl->iconmgr->twm_win, newws)) {
 
399
                                break;
 
400
                        }
 
401
                        wl1 = wl;
 
402
                        wl  = wl->nextv;
 
403
                }
 
404
                if(wl != NULL) {
 
405
                        wl1->nextv = wl->nextv;
 
406
                        wl->nextv  = twmWin->iconmanagerlist;
 
407
                        twmWin->iconmanagerlist = wl;
 
408
                }
 
409
        }
 
410
        wl = NULL;
 
411
        for(iconmgr = newws->iconmgr; iconmgr; iconmgr = iconmgr->next) {
 
412
                if(iconmgr->first) {
 
413
                        wl = iconmgr->first;
 
414
                        break;
 
415
                }
 
416
        }
 
417
        CurrentIconManagerEntry(wl);
 
418
        if(focuswindow) {
 
419
                SetFocusVisualAttributes(focuswindow, true);
 
420
        }
 
421
        vs->wsw->currentwspc = newws;
 
422
        if(Scr->ReverseCurrentWorkspace && vs->wsw->state == WMS_map) {
 
423
                MapSubwindow *msw = vs->wsw->mswl [oldws->number];
 
424
                for(winl = msw->wl; winl != NULL; winl = winl->next) {
 
425
                        WMapRedrawName(vs, winl);
 
426
                }
 
427
                msw = vs->wsw->mswl [newws->number];
 
428
                for(winl = msw->wl; winl != NULL; winl = winl->next) {
 
429
                        WMapRedrawName(vs, winl);
 
430
                }
 
431
        }
 
432
        else if(vs->wsw->state == WMS_buttons) {
 
433
                ButtonSubwindow *bsw = vs->wsw->bswl [oldws->number];
 
434
                PaintWsButton(WSPCWINDOW, vs, bsw->w, oldws->label, oldws->cp, off);
 
435
                bsw = vs->wsw->bswl [newws->number];
 
436
                PaintWsButton(WSPCWINDOW, vs, bsw->w, newws->label, newws->cp,  on);
 
437
        }
 
438
        oldws->iconmgr = Scr->iconmgr;
 
439
        Scr->iconmgr = newws->iconmgr;
 
440
 
 
441
        oldw = vs->wsw->mswl [oldws->number]->w;
 
442
        neww = vs->wsw->mswl [newws->number]->w;
 
443
        if(useBackgroundInfo) {
 
444
                if(oldws->image == NULL || Scr->NoImagesInWorkSpaceManager) {
 
445
                        XSetWindowBackground(dpy, oldw, oldws->backcp.back);
 
446
                }
 
447
                else {
 
448
                        XSetWindowBackgroundPixmap(dpy, oldw, oldws->image->pixmap);
 
449
                }
 
450
        }
 
451
        else {
 
452
                if(Scr->workSpaceMgr.defImage == NULL || Scr->NoImagesInWorkSpaceManager) {
 
453
                        XSetWindowBackground(dpy, oldw, Scr->workSpaceMgr.defColors.back);
 
454
                }
 
455
                else {
 
456
                        XSetWindowBackgroundPixmap(dpy, oldw, Scr->workSpaceMgr.defImage->pixmap);
 
457
                }
 
458
        }
 
459
        attr.border_pixel = Scr->workSpaceMgr.defBorderColor;
 
460
        XChangeWindowAttributes(dpy, oldw, CWBorderPixel, &attr);
 
461
 
 
462
        if(Scr->workSpaceMgr.curImage == NULL) {
 
463
                if(Scr->workSpaceMgr.curPaint) {
 
464
                        XSetWindowBackground(dpy, neww, Scr->workSpaceMgr.curColors.back);
 
465
                }
 
466
        }
 
467
        else {
 
468
                XSetWindowBackgroundPixmap(dpy, neww, Scr->workSpaceMgr.curImage->pixmap);
 
469
        }
 
470
        attr.border_pixel =  Scr->workSpaceMgr.curBorderColor;
 
471
        XChangeWindowAttributes(dpy, neww, CWBorderPixel, &attr);
 
472
 
 
473
        XClearWindow(dpy, oldw);
 
474
        XClearWindow(dpy, neww);
 
475
 
 
476
        XGetWindowAttributes(dpy, Scr->Root, &winattrs);
 
477
        eventMask = winattrs.your_event_mask;
 
478
        XSelectInput(dpy, Scr->Root, eventMask & ~PropertyChangeMask);
 
479
 
 
480
        XChangeProperty(dpy, Scr->Root, XA_WM_CURRENTWORKSPACE, XA_STRING, 8,
 
481
                        PropModeReplace, (unsigned char *) newws->name, strlen(newws->name));
 
482
#ifdef EWMH
 
483
        {
 
484
                long number = newws->number;
 
485
                /*
 
486
                 * TODO: this should probably not use Scr->Root but ->XineramaRoot.
 
487
                 * That is the real root window if we're using virtual screens.
 
488
                 * Also, on the real root it would need values for each of the
 
489
                 * virtual roots, but that doesn't fit in the EWMH ideas.
 
490
                 */
 
491
                XChangeProperty(dpy, Scr->Root, XA__NET_CURRENT_DESKTOP,
 
492
                                XA_CARDINAL, 32,
 
493
                                PropModeReplace, (unsigned char *) &number, 1);
 
494
        }
 
495
#endif /* EWMH */
 
496
        XSelectInput(dpy, Scr->Root, eventMask);
 
497
 
 
498
        /*    XDestroyWindow (dpy, cachew);*/
 
499
        if(Scr->ChangeWorkspaceFunction.func != 0) {
 
500
                char *action;
 
501
                XEvent event;
 
502
 
 
503
                action = Scr->ChangeWorkspaceFunction.item ?
 
504
                         Scr->ChangeWorkspaceFunction.item->action : NULL;
 
505
                ExecuteFunction(Scr->ChangeWorkspaceFunction.func, action,
 
506
                                (Window) 0, NULL, &event, C_ROOT, false);
 
507
        }
 
508
 
 
509
        /* If SaveWorkspaceFocus is on, try to restore the focus to the last
 
510
           window which was focused when we left this workspace. */
 
511
        if(Scr->SaveWorkspaceFocus && newws->save_focus) {
 
512
                twmWin = newws->save_focus;
 
513
                if(OCCUPY(twmWin, newws)) {     /* check should not even be needed anymore */
 
514
                        WarpToWindow(twmWin, false);
 
515
                }
 
516
                else {
 
517
                        newws->save_focus = NULL;
 
518
                }
 
519
        }
 
520
 
 
521
        /* keep track of the order of the workspaces across restarts */
 
522
        CtwmSetVScreenMap(dpy, Scr->Root, Scr->vScreenList);
 
523
 
 
524
        XSync(dpy, 0);
 
525
        if(Scr->ClickToFocus || Scr->SloppyFocus) {
 
526
                set_last_window(newws);
 
527
        }
 
528
        MaybeAnimate = true;
 
529
}
 
530
 
 
531
 
 
532
/*
 
533
 * Get the name of the currently active WS.  Used in Execute() for
 
534
 * sub'ing in $currentworkspace in executing commands.
 
535
 */
 
536
char *GetCurrentWorkSpaceName(VirtualScreen *vs)
 
537
{
 
538
        if(! Scr->workSpaceManagerActive) {
 
539
                return (NULL);
 
540
        }
 
541
        if(!vs) {
 
542
                vs = Scr->vScreenList;
 
543
        }
 
544
        return vs->wsw->currentwspc->name;
 
545
}
 
546
 
 
547
 
 
548
/*
 
549
 * Find workspace by name
 
550
 */
 
551
WorkSpace *
 
552
GetWorkspace(char *wname)
 
553
{
 
554
        WorkSpace *ws;
 
555
 
 
556
        /* Guard */
 
557
        if(!wname) {
 
558
                return (NULL);
 
559
        }
 
560
 
 
561
        /* Check by label */
 
562
        for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
 
563
                if(strcmp(ws->label, wname) == 0) {
 
564
                        return ws;
 
565
                }
 
566
        }
 
567
 
 
568
        /* Check by name */
 
569
        for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
 
570
                if(strcmp(ws->name, wname) == 0) {
 
571
                        return ws;
 
572
                }
 
573
        }
 
574
 
 
575
        /* Nope */
 
576
        return NULL;
 
577
}
 
578
 
 
579
 
 
580
/*
 
581
 * Util for setting useBackgroundInfo var, to reduce its exposure across
 
582
 * the multiple files that might touch it.
 
583
 */
 
584
void ws_set_useBackgroundInfo(bool newval)
 
585
{
 
586
        useBackgroundInfo = newval;
 
587
}