Change to the linux kernel coding style
[wmaker-crm.git] / src / workspace.c
1 /* workspace.c- Workspace management
2  *
3  *  Window Maker window manager
4  *
5  *  Copyright (c) 1997-2003 Alfredo K. Kojima
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20  *  USA.
21  */
22 #include "wconfig.h"
23
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <unistd.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include <time.h>
37 #include <sys/time.h>
38
39 #include "WindowMaker.h"
40 #include "wcore.h"
41 #include "framewin.h"
42 #include "window.h"
43 #include "icon.h"
44 #include "funcs.h"
45 #include "menu.h"
46 #include "application.h"
47 #include "dock.h"
48 #include "actions.h"
49 #include "workspace.h"
50 #include "appicon.h"
51 #ifdef NETWM_HINTS
52 #include "wmspec.h"
53 #endif
54
55 #include "xinerama.h"
56
57 #define MAX_SHORTCUT_LENGTH 32
58
59 extern WPreferences wPreferences;
60 extern XContext wWinContext;
61 extern XContext wVEdgeContext;
62
63 extern void ProcessPendingEvents();
64
65 static WMPropList *dWorkspaces = NULL;
66 static WMPropList *dClip, *dName;
67
68 static void make_keys()
69 {
70         if (dWorkspaces != NULL)
71                 return;
72
73         dWorkspaces = WMCreatePLString("Workspaces");
74         dName = WMCreatePLString("Name");
75         dClip = WMCreatePLString("Clip");
76 }
77
78 void wWorkspaceMake(WScreen * scr, int count)
79 {
80         while (count > 0) {
81                 wWorkspaceNew(scr);
82                 count--;
83         }
84 }
85
86 int wWorkspaceNew(WScreen * scr)
87 {
88         WWorkspace *wspace, **list;
89         int i;
90
91         if (scr->workspace_count < MAX_WORKSPACES) {
92                 scr->workspace_count++;
93
94                 wspace = wmalloc(sizeof(WWorkspace));
95                 wspace->name = NULL;
96
97                 if (!wspace->name) {
98                         wspace->name = wmalloc(strlen(_("Workspace %i")) + 8);
99                         sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
100                 }
101
102                 if (!wPreferences.flags.noclip) {
103                         wspace->clip = wDockCreate(scr, WM_CLIP);
104                 } else
105                         wspace->clip = NULL;
106
107                 list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
108
109                 for (i = 0; i < scr->workspace_count - 1; i++) {
110                         list[i] = scr->workspaces[i];
111                 }
112                 list[i] = wspace;
113                 if (scr->workspaces)
114                         wfree(scr->workspaces);
115                 scr->workspaces = list;
116
117                 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
118                 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
119 #ifdef VIRTUAL_DESKTOP
120                 wspace->view_x = wspace->view_y = 0;
121                 wspace->height = scr->scr_height;
122                 wspace->width = scr->scr_width;
123 #endif
124 #ifdef NETWM_HINTS
125                 wNETWMUpdateDesktop(scr);
126 #endif
127
128                 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
129                 XFlush(dpy);
130
131                 return scr->workspace_count - 1;
132         }
133
134         return -1;
135 }
136
137 Bool wWorkspaceDelete(WScreen * scr, int workspace)
138 {
139         WWindow *tmp;
140         WWorkspace **list;
141         int i, j;
142
143         if (workspace <= 0)
144                 return False;
145
146         /* verify if workspace is in use by some window */
147         tmp = scr->focused_window;
148         while (tmp) {
149                 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
150                         return False;
151                 tmp = tmp->prev;
152         }
153
154         if (!wPreferences.flags.noclip) {
155                 wDockDestroy(scr->workspaces[workspace]->clip);
156                 scr->workspaces[workspace]->clip = NULL;
157         }
158
159         list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
160         j = 0;
161         for (i = 0; i < scr->workspace_count; i++) {
162                 if (i != workspace) {
163                         list[j++] = scr->workspaces[i];
164                 } else {
165                         if (scr->workspaces[i]->name)
166                                 wfree(scr->workspaces[i]->name);
167                         wfree(scr->workspaces[i]);
168                 }
169         }
170         wfree(scr->workspaces);
171         scr->workspaces = list;
172
173         scr->workspace_count--;
174
175         /* update menu */
176         wWorkspaceMenuUpdate(scr, scr->workspace_menu);
177         /* clip workspace menu */
178         wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
179
180         /* update also window menu */
181         if (scr->workspace_submenu) {
182                 WMenu *menu = scr->workspace_submenu;
183
184                 i = menu->entry_no;
185                 while (i > scr->workspace_count)
186                         wMenuRemoveItem(menu, --i);
187                 wMenuRealize(menu);
188         }
189         /* and clip menu */
190         if (scr->clip_submenu) {
191                 WMenu *menu = scr->clip_submenu;
192
193                 i = menu->entry_no;
194                 while (i > scr->workspace_count)
195                         wMenuRemoveItem(menu, --i);
196                 wMenuRealize(menu);
197         }
198 #ifdef NETWM_HINTS
199         wNETWMUpdateDesktop(scr);
200 #endif
201
202         WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
203
204         if (scr->current_workspace >= scr->workspace_count)
205                 wWorkspaceChange(scr, scr->workspace_count - 1);
206
207         return True;
208 }
209
210 typedef struct WorkspaceNameData {
211         int count;
212         RImage *back;
213         RImage *text;
214         time_t timeout;
215 } WorkspaceNameData;
216
217 static void hideWorkspaceName(void *data)
218 {
219         WScreen *scr = (WScreen *) data;
220
221         if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
222             || time(NULL) > scr->workspace_name_data->timeout) {
223                 XUnmapWindow(dpy, scr->workspace_name);
224
225                 if (scr->workspace_name_data) {
226                         RReleaseImage(scr->workspace_name_data->back);
227                         RReleaseImage(scr->workspace_name_data->text);
228                         wfree(scr->workspace_name_data);
229
230                         scr->workspace_name_data = NULL;
231                 }
232                 scr->workspace_name_timer = NULL;
233         } else {
234                 RImage *img = RCloneImage(scr->workspace_name_data->back);
235                 Pixmap pix;
236
237                 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
238
239                 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
240                                              scr->workspace_name_data->count * 255 / 10);
241
242                 RConvertImage(scr->rcontext, img, &pix);
243
244                 RReleaseImage(img);
245
246                 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
247                 XClearWindow(dpy, scr->workspace_name);
248                 XFreePixmap(dpy, pix);
249                 XFlush(dpy);
250
251                 scr->workspace_name_data->count--;
252         }
253 }
254
255 static void showWorkspaceName(WScreen * scr, int workspace)
256 {
257         WorkspaceNameData *data;
258         RXImage *ximg;
259         Pixmap text, mask;
260         int w, h;
261         int px, py;
262         char *name = scr->workspaces[workspace]->name;
263         int len = strlen(name);
264         int x, y;
265
266         if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2) {
267                 return;
268         }
269
270         if (scr->workspace_name_timer) {
271                 WMDeleteTimerHandler(scr->workspace_name_timer);
272                 XUnmapWindow(dpy, scr->workspace_name);
273                 XFlush(dpy);
274         }
275         scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
276
277         if (scr->workspace_name_data) {
278                 RReleaseImage(scr->workspace_name_data->back);
279                 RReleaseImage(scr->workspace_name_data->text);
280                 wfree(scr->workspace_name_data);
281         }
282
283         data = wmalloc(sizeof(WorkspaceNameData));
284         data->back = NULL;
285
286         w = WMWidthOfString(scr->workspace_name_font, name, len);
287         h = WMFontHeight(scr->workspace_name_font);
288
289         switch (wPreferences.workspace_name_display_position) {
290         case WD_TOP:
291                 px = (scr->scr_width - (w + 4)) / 2;
292                 py = 0;
293                 break;
294         case WD_BOTTOM:
295                 px = (scr->scr_width - (w + 4)) / 2;
296                 py = scr->scr_height - (h + 4);
297                 break;
298         case WD_TOPLEFT:
299                 px = 0;
300                 py = 0;
301                 break;
302         case WD_TOPRIGHT:
303                 px = scr->scr_width - (w + 4);
304                 py = 0;
305                 break;
306         case WD_BOTTOMLEFT:
307                 px = 0;
308                 py = scr->scr_height - (h + 4);
309                 break;
310         case WD_BOTTOMRIGHT:
311                 px = scr->scr_width - (w + 4);
312                 py = scr->scr_height - (h + 4);
313                 break;
314         case WD_CENTER:
315         default:
316                 px = (scr->scr_width - (w + 4)) / 2;
317                 py = (scr->scr_height - (h + 4)) / 2;
318                 break;
319         }
320         XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
321         XMoveWindow(dpy, scr->workspace_name, px, py);
322
323         text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
324         mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
325
326         /*XSetForeground(dpy, scr->mono_gc, 0);
327            XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
328
329         XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
330
331         for (x = 0; x <= 4; x++) {
332                 for (y = 0; y <= 4; y++) {
333                         WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
334                 }
335         }
336
337         XSetForeground(dpy, scr->mono_gc, 1);
338         XSetBackground(dpy, scr->mono_gc, 0);
339
340         XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
341
342         /*XSetForeground(dpy, scr->mono_gc, 1); */
343         XSetBackground(dpy, scr->mono_gc, 1);
344
345         XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
346
347         WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
348
349 #ifdef SHAPE
350         XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
351 #endif
352         XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
353         XClearWindow(dpy, scr->workspace_name);
354
355         data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
356
357         XFreePixmap(dpy, text);
358         XFreePixmap(dpy, mask);
359
360         if (!data->text) {
361                 XMapRaised(dpy, scr->workspace_name);
362                 XFlush(dpy);
363
364                 goto erro;
365         }
366
367         ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
368
369         if (!ximg || !ximg->image) {
370                 goto erro;
371         }
372
373         XMapRaised(dpy, scr->workspace_name);
374         XFlush(dpy);
375
376         data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
377         RDestroyXImage(scr->rcontext, ximg);
378
379         if (!data->back) {
380                 goto erro;
381         }
382
383         data->count = 10;
384
385         /* set a timeout for the effect */
386         data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
387
388         scr->workspace_name_data = data;
389
390         return;
391
392  erro:
393         if (scr->workspace_name_timer)
394                 WMDeleteTimerHandler(scr->workspace_name_timer);
395
396         if (data->text)
397                 RReleaseImage(data->text);
398         if (data->back)
399                 RReleaseImage(data->back);
400         wfree(data);
401
402         scr->workspace_name_data = NULL;
403
404         scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
405                                                       10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
406 }
407
408 void wWorkspaceChange(WScreen * scr, int workspace)
409 {
410         if (scr->flags.startup || scr->flags.startup2) {
411                 return;
412         }
413
414         if (workspace != scr->current_workspace) {
415                 wWorkspaceForceChange(scr, workspace);
416         }                       /*else {
417                                    showWorkspaceName(scr, workspace);
418                                    } */
419 }
420
421 void wWorkspaceRelativeChange(WScreen * scr, int amount)
422 {
423         int w;
424
425         w = scr->current_workspace + amount;
426
427         if (amount < 0) {
428                 if (w >= 0) {
429                         wWorkspaceChange(scr, w);
430                 } else if (wPreferences.ws_cycle) {
431                         wWorkspaceChange(scr, scr->workspace_count + w);
432                 }
433         } else if (amount > 0) {
434                 if (w < scr->workspace_count) {
435                         wWorkspaceChange(scr, w);
436                 } else if (wPreferences.ws_advance) {
437                         wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
438                 } else if (wPreferences.ws_cycle) {
439                         wWorkspaceChange(scr, w % scr->workspace_count);
440                 }
441         }
442 }
443
444 void wWorkspaceForceChange(WScreen * scr, int workspace)
445 {
446         WWindow *tmp, *foc = NULL, *foc2 = NULL;
447
448         if (workspace >= MAX_WORKSPACES || workspace < 0)
449                 return;
450
451         SendHelperMessage(scr, 'C', workspace + 1, NULL);
452
453         if (workspace > scr->workspace_count - 1) {
454                 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
455         }
456
457         wClipUpdateForWorkspaceChange(scr, workspace);
458
459         scr->current_workspace = workspace;
460
461         wWorkspaceMenuUpdate(scr, scr->workspace_menu);
462
463         wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
464
465         if ((tmp = scr->focused_window) != NULL) {
466                 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
467                      !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
468                         foc = tmp;
469                 }
470
471                 /* foc2 = tmp; will fix annoyance with gnome panel
472                  * but will create annoyance for every other application
473                  */
474                 while (tmp) {
475                         if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
476                                 /* unmap windows not on this workspace */
477                                 if ((tmp->flags.mapped || tmp->flags.shaded) &&
478                                     !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
479                                         wWindowUnmap(tmp);
480                                 }
481                                 /* also unmap miniwindows not on this workspace */
482                                 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
483                                     tmp->icon && !IS_OMNIPRESENT(tmp)) {
484                                         XUnmapWindow(dpy, tmp->icon->core->window);
485                                         tmp->icon->mapped = 0;
486                                 }
487                                 /* update current workspace of omnipresent windows */
488                                 if (IS_OMNIPRESENT(tmp)) {
489                                         WApplication *wapp = wApplicationOf(tmp->main_window);
490
491                                         tmp->frame->workspace = workspace;
492
493                                         if (wapp) {
494                                                 wapp->last_workspace = workspace;
495                                         }
496                                         if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
497                                                 foc2 = tmp;
498                                         }
499                                 }
500                         } else {
501                                 /* change selected windows' workspace */
502                                 if (tmp->flags.selected) {
503                                         wWindowChangeWorkspace(tmp, workspace);
504                                         if (!tmp->flags.miniaturized && !foc) {
505                                                 foc = tmp;
506                                         }
507                                 } else {
508                                         if (!tmp->flags.hidden) {
509                                                 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
510                                                         /* remap windows that are on this workspace */
511                                                         wWindowMap(tmp);
512                                                         if (!foc && !WFLAGP(tmp, no_focusable)) {
513                                                                 foc = tmp;
514                                                         }
515                                                 }
516                                                 /* Also map miniwindow if not omnipresent */
517                                                 if (!wPreferences.sticky_icons &&
518                                                     tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
519                                                         tmp->icon->mapped = 1;
520                                                         XMapWindow(dpy, tmp->icon->core->window);
521                                                 }
522                                         }
523                                 }
524                         }
525                         tmp = tmp->prev;
526                 }
527
528                 /* Gobble up events unleashed by our mapping & unmapping.
529                  * These may trigger various grab-initiated focus &
530                  * crossing events. However, we don't care about them,
531                  * and ignore their focus implications altogether to avoid
532                  * flicker.
533                  */
534                 scr->flags.ignore_focus_events = 1;
535                 ProcessPendingEvents();
536                 scr->flags.ignore_focus_events = 0;
537
538                 if (!foc)
539                         foc = foc2;
540
541                 if (scr->focused_window->flags.mapped && !foc) {
542                         foc = scr->focused_window;
543                 }
544                 if (wPreferences.focus_mode == WKF_CLICK) {
545                         wSetFocusTo(scr, foc);
546                 } else {
547                         unsigned int mask;
548                         int foo;
549                         Window bar, win;
550                         WWindow *tmp;
551
552                         tmp = NULL;
553                         if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
554                                 tmp = wWindowFor(win);
555                         }
556
557                         /* If there's a window under the pointer, focus it.
558                          * (we ate all other focus events above, so it's
559                          * certainly not focused). Otherwise focus last
560                          * focused, or the root (depending on sloppiness)
561                          */
562                         if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
563                                 wSetFocusTo(scr, foc);
564                         } else {
565                                 wSetFocusTo(scr, tmp);
566                         }
567                 }
568         }
569
570         /* We need to always arrange icons when changing workspace, even if
571          * no autoarrange icons, because else the icons in different workspaces
572          * can be superposed.
573          * This can be avoided if appicons are also workspace specific.
574          */
575         if (!wPreferences.sticky_icons)
576                 wArrangeIcons(scr, False);
577
578         if (scr->dock)
579                 wAppIconPaint(scr->dock->icon_array[0]);
580
581         if (scr->clip_icon) {
582                 if (scr->workspaces[workspace]->clip->auto_collapse ||
583                     scr->workspaces[workspace]->clip->auto_raise_lower) {
584                         /* to handle enter notify. This will also */
585                         XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
586                         XMapWindow(dpy, scr->clip_icon->icon->core->window);
587                 } else {
588                         wClipIconPaint(scr->clip_icon);
589                 }
590         }
591 #ifdef NETWM_HINTS
592         wScreenUpdateUsableArea(scr);
593         wNETWMUpdateDesktop(scr);
594 #endif
595
596         showWorkspaceName(scr, workspace);
597
598         WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
599
600         /*   XSync(dpy, False); */
601 }
602
603 #ifdef VIRTUAL_DESKTOP
604
605 /* TODO:
606  *
607  * 1) Allow border around each window so the scrolling
608  *    won't just stop at the border.
609  * 2) Make pager.
610  *
611  */
612
613 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
614 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
615 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
616 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
617 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
618 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
619
620 #ifndef HAS_BORDER
621 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
622 #endif
623
624 #ifndef IS_VSTUCK
625 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
626 #endif
627
628 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
629
630 static Bool cmp_gez(int i)
631 {
632         return (i >= 0);
633 }
634
635 static Bool cmp_gz(int i)
636 {
637         return (i > 0);
638 }
639
640 static WMPoint getClosestEdge(WScreen * scr, WMPoint direction, Bool(*cmp) (int))
641 {
642         WMPoint closest = wmkpoint(0, 0);
643         int closest_len = INT_MAX;
644         WWindow *wwin;
645
646         for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
647                 if (wwin->frame->workspace == scr->current_workspace) {
648                         if (!wwin->flags.miniaturized && !IS_VSTUCK(wwin) && !wwin->flags.hidden) {
649                                 int border = 2 * HAS_BORDER(wwin);
650                                 int len;
651                                 int x1, x2, y1, y2;
652                                 int head = wGetHeadForWindow(wwin);
653                                 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
654                                 WMPoint p;
655
656                                 x1 = wwin->frame_x - area.x1;
657                                 y1 = wwin->frame_y - area.y1;
658                                 x2 = wwin->frame_x + wwin->frame->core->width + border - area.x2;
659                                 y2 = wwin->frame_y + wwin->frame->core->height + border - area.y2;
660
661                                 p = wmkpoint(x1, y1);
662                                 len = vec_dot(direction, p);
663                                 updateMinimum(len, p, closest_len, closest);
664
665                                 p = wmkpoint(x1, y2);
666                                 len = vec_dot(direction, p);
667                                 updateMinimum(len, p, closest_len, closest);
668
669                                 p = wmkpoint(x2, y1);
670                                 len = vec_dot(direction, p);
671                                 updateMinimum(len, p, closest_len, closest);
672
673                                 p = wmkpoint(x2, y2);
674                                 len = vec_dot(direction, p);
675                                 updateMinimum(len, p, closest_len, closest);
676                         }
677                 }
678         }
679
680         return closest;
681 }
682
683 static void getViewPosition(WScreen * scr, int workspace, int *x, int *y)
684 {
685         *x = scr->workspaces[workspace]->view_x;
686         *y = scr->workspaces[workspace]->view_y;
687 }
688
689 void wWorkspaceKeyboardMoveDesktop(WScreen * scr, WMPoint direction)
690 {
691         int x, y;
692         WMPoint edge = getClosestEdge(scr, direction, cmp_gz);
693         int len = vec_dot(edge, direction);
694         WMPoint step = vec_scale(direction, len);
695         getViewPosition(scr, scr->current_workspace, &x, &y);
696         wWorkspaceSetViewport(scr, scr->current_workspace, x + step.x, y + step.y);
697 }
698
699 extern Cursor wCursor[WCUR_LAST];
700
701 static void vdMouseMoveDesktop(XEvent * event, WMPoint direction)
702 {
703         static int lock = False;
704         if (lock)
705                 return;
706         lock = True;
707
708         Bool done = False;
709         Bool moved = True;
710         WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
711         WMPoint old_pos = wmkpoint(event->xcrossing.x_root, event->xcrossing.y_root);
712         WMPoint step;
713         int x, y;
714         int resisted = 0;
715
716         if (XGrabPointer(dpy, event->xcrossing.window, False,
717                          PointerMotionMask, GrabModeAsync, GrabModeAsync,
718                          scr->root_win, wCursor[WCUR_EMPTY], CurrentTime) != GrabSuccess) {
719
720                 /* if the grab fails, do it the old fashioned way */
721                 step = vec_scale2(direction, wPreferences.vedge_hscrollspeed, wPreferences.vedge_vscrollspeed);
722                 getViewPosition(scr, scr->current_workspace, &x, &y);
723                 if (wWorkspaceSetViewport(scr, scr->current_workspace, x + step.x, y + step.y)) {
724                         step = vec_scale(direction, 2);
725                         XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0,
726                                      event->xcrossing.x_root - step.x, event->xcrossing.y_root - step.y);
727                 }
728                 goto exit;
729         }
730         XSync(dpy, True);
731
732         if (old_pos.x < 0)
733                 old_pos.x = 0;
734         if (old_pos.y < 0)
735                 old_pos.y = 0;
736         if (old_pos.x > scr->scr_width)
737                 old_pos.x = scr->scr_width;
738         if (old_pos.y > scr->scr_height)
739                 old_pos.y = scr->scr_height;
740
741         while (!done) {
742                 XEvent ev;
743                 if (moved) {
744                         XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0,
745                                      scr->scr_width / 2, scr->scr_height / 2);
746                         moved = False;
747                 }
748                 WMMaskEvent(dpy, PointerMotionMask, &ev);
749
750                 switch (ev.type) {
751                 case MotionNotify:
752                         {
753                                 int step_len;
754                                 step = wmkpoint(ev.xmotion.x_root - scr->scr_width / 2,
755                                                 ev.xmotion.y_root - scr->scr_height / 2);
756                                 step_len = vec_dot(step, direction);
757                                 if (step_len < 0) {
758                                         done = True;
759                                         break;
760                                 }
761
762                                 if (step_len > 0) {
763                                         Bool do_move = True;
764                                         int resist = wPreferences.vedge_resistance;
765                                         WMPoint closest;
766                                         int closest_len = INT_MAX;
767                                         if (resist) {
768                                                 closest = getClosestEdge(scr, direction, cmp_gez);
769                                                 closest_len = vec_dot(direction, closest);
770                                         }
771
772                                         if (!closest_len) {
773                                                 resisted += step_len;
774                                                 do_move = resisted >= resist;
775                                                 if (do_move) {
776                                                         closest_len = INT_MAX;
777                                                         step_len = resisted - resist;
778                                                         resisted = 0;
779                                                 }
780                                         }
781                                         if (do_move) {
782                                                 if (closest_len <= wPreferences.vedge_attraction) {
783                                                         step = vec_scale(direction, closest_len);
784                                                 } else {
785                                                         step = vec_scale(direction, step_len);
786                                                 }
787
788                                                 getViewPosition(scr, scr->current_workspace, &x, &y);
789                                                 wWorkspaceSetViewport(scr, scr->current_workspace,
790                                                                       x + step.x, y + step.y);
791                                                 moved = True;
792                                         }
793                                 }
794                         }
795                         break;
796                 }
797         }
798
799         step = vec_add(old_pos, vec_scale(direction, -1));
800         XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0, step.x, step.y);
801         XUngrabPointer(dpy, CurrentTime);
802
803  exit:
804         lock = False;
805 }
806
807 static void vdHandleEnter_u(XEvent * event)
808 {
809         vdMouseMoveDesktop(event, VEC_UP);
810 }
811
812 static void vdHandleEnter_d(XEvent * event)
813 {
814         vdMouseMoveDesktop(event, VEC_DOWN);
815 }
816
817 static void vdHandleEnter_l(XEvent * event)
818 {
819         vdMouseMoveDesktop(event, VEC_LEFT);
820 }
821
822 static void vdHandleEnter_r(XEvent * event)
823 {
824         vdMouseMoveDesktop(event, VEC_RIGHT);
825 }
826
827 #define LEFT_EDGE   0x01
828 #define RIGHT_EDGE  0x02
829 #define TOP_EDGE    0x04
830 #define BOTTOM_EDGE 0x08
831 #define ALL_EDGES   0x0F
832
833 static void createEdges(WScreen * scr)
834 {
835         if (!scr->virtual_edges) {
836                 int i, j, w;
837                 int vmask;
838                 XSetWindowAttributes attribs;
839
840                 int heads = wXineramaHeads(scr);
841                 int *hasEdges = (int *)wmalloc(sizeof(int) * heads);
842
843                 int thickness = 1;
844                 int nr_edges = 0;
845                 int max_edges = 4 * heads;
846                 int head;
847                 Window *edges = (Window *) wmalloc(sizeof(Window) * max_edges);
848
849                 for (i = 0; i < heads; ++i)
850                         hasEdges[i] = ALL_EDGES;
851                 for (i = 0; i < heads; ++i) {
852                         WMRect i_rect = wGetRectForHead(scr, i);
853                         for (j = i + 1; j < heads; ++j) {
854                                 WMRect j_rect = wGetRectForHead(scr, j);
855
856                                 int vlen = (WMIN(i_rect.pos.y + i_rect.size.height,
857                                                  j_rect.pos.y + j_rect.size.height) -
858                                             WMAX(i_rect.pos.y, j_rect.pos.y));
859
860                                 int hlen = (WMIN(i_rect.pos.x + i_rect.size.width,
861                                                  j_rect.pos.x + j_rect.size.width) -
862                                             WMAX(i_rect.pos.x, j_rect.pos.x));
863
864                                 if (vlen > 0 && hlen == 0) {    /* horz alignment, vert edges touch */
865                                         if (i_rect.pos.x < j_rect.pos.x) {      /* i left of j */
866                                                 hasEdges[i] &= ~RIGHT_EDGE;
867                                                 hasEdges[j] &= ~LEFT_EDGE;
868                                         } else {        /* j left of i */
869                                                 hasEdges[j] &= ~RIGHT_EDGE;
870                                                 hasEdges[i] &= ~LEFT_EDGE;
871                                         }
872                                 } else if (vlen == 0 && hlen > 0) {     /* vert alignment, horz edges touch */
873                                         if (i_rect.pos.y < j_rect.pos.y) {      /* i top of j */
874                                                 hasEdges[i] &= ~BOTTOM_EDGE;
875                                                 hasEdges[j] &= ~TOP_EDGE;
876                                         } else {        /* j top of i */
877                                                 hasEdges[j] &= ~BOTTOM_EDGE;
878                                                 hasEdges[i] &= ~TOP_EDGE;
879                                         }
880                                 }
881                         }
882                 }
883
884                 for (w = 0; w < scr->workspace_count; w++) {
885                         /* puts("reset workspace"); */
886                         wWorkspaceSetViewport(scr, w, 0, 0);
887                 }
888
889                 vmask = CWEventMask | CWOverrideRedirect;
890                 attribs.event_mask = (EnterWindowMask | LeaveWindowMask | VisibilityChangeMask);
891                 attribs.override_redirect = True;
892
893                 for (head = 0; head < wXineramaHeads(scr); ++head) {
894                         WMRect rect = wGetRectForHead(scr, head);
895
896                         if (hasEdges[head] & TOP_EDGE) {
897                                 edges[nr_edges] =
898                                     XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
899                                                   rect.size.width, thickness, 0,
900                                                   CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
901                                 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_u);
902                                 ++nr_edges;
903                         }
904
905                         if (hasEdges[head] & BOTTOM_EDGE) {
906                                 edges[nr_edges] =
907                                     XCreateWindow(dpy, scr->root_win, rect.pos.x,
908                                                   rect.pos.y + rect.size.height - thickness,
909                                                   rect.size.width, thickness, 0,
910                                                   CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
911                                 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_d);
912                                 ++nr_edges;
913                         }
914
915                         if (hasEdges[head] & LEFT_EDGE) {
916                                 edges[nr_edges] =
917                                     XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
918                                                   thickness, rect.pos.y + rect.size.height, 0,
919                                                   CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
920                                 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_l);
921                                 ++nr_edges;
922                         }
923
924                         if (hasEdges[head] & RIGHT_EDGE) {
925                                 edges[nr_edges] =
926                                     XCreateWindow(dpy, scr->root_win,
927                                                   rect.pos.x + rect.size.width - thickness, rect.pos.y,
928                                                   thickness, rect.size.height, 0,
929                                                   CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
930                                 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_r);
931                                 ++nr_edges;
932                         }
933                 }
934
935                 scr->virtual_nr_edges = nr_edges;
936                 scr->virtual_edges = edges;
937
938                 for (i = 0; i < scr->virtual_nr_edges; ++i) {
939                         XMapWindow(dpy, scr->virtual_edges[i]);
940                 }
941                 wWorkspaceRaiseEdge(scr);
942
943                 wfree(hasEdges);
944         }
945 }
946
947 static void destroyEdges(WScreen * scr)
948 {
949         if (scr->virtual_edges) {
950                 int i;
951
952                 for (i = 0; i < scr->virtual_nr_edges; ++i) {
953                         XDeleteContext(dpy, scr->virtual_edges[i], wVEdgeContext);
954                         XUnmapWindow(dpy, scr->virtual_edges[i]);
955                         XDestroyWindow(dpy, scr->virtual_edges[i]);
956                 }
957
958                 wfree(scr->virtual_edges);
959                 scr->virtual_edges = NULL;
960                 scr->virtual_nr_edges = 0;
961         }
962 }
963
964 void wWorkspaceUpdateEdge(WScreen * scr)
965 {
966         if (wPreferences.vdesk_enable) {
967                 createEdges(scr);
968         } else {
969                 destroyEdges(scr);
970         }
971 }
972
973 void wWorkspaceRaiseEdge(WScreen * scr)
974 {
975         static int toggle = 0;
976         int i;
977
978         if (!scr->virtual_edges)
979                 return;
980
981         if (toggle) {
982                 for (i = 0; i < scr->virtual_nr_edges; ++i) {
983                         XRaiseWindow(dpy, scr->virtual_edges[i]);
984                 }
985         } else {
986                 for (i = scr->virtual_nr_edges - 1; i >= 0; --i) {
987                         XRaiseWindow(dpy, scr->virtual_edges[i]);
988                 }
989         }
990
991         toggle ^= 1;
992 }
993
994 void wWorkspaceLowerEdge(WScreen * scr)
995 {
996         int i;
997         for (i = 0; i < scr->virtual_nr_edges; ++i) {
998                 XLowerWindow(dpy, scr->virtual_edges[i]);
999         }
1000 }
1001
1002 void wWorkspaceResizeViewport(WScreen * scr, int workspace)
1003 {
1004         int x, y;
1005         getViewPosition(scr, scr->current_workspace, &x, &y);
1006         wWorkspaceSetViewport(scr, scr->current_workspace, x, y);
1007 }
1008
1009 void updateWorkspaceGeometry(WScreen * scr, int workspace, int *view_x, int *view_y)
1010 {
1011         int most_left, most_right, most_top, most_bottom;
1012         WWindow *wwin;
1013
1014         int heads = wXineramaHeads(scr);
1015         typedef int strut_t[4];
1016         strut_t *strut = (strut_t *) wmalloc(heads * sizeof(strut_t));
1017         int head, i;
1018
1019         for (head = 0; head < heads; ++head) {
1020                 WMRect rect = wGetRectForHead(scr, head);
1021                 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1022                 strut[head][0] = area.x1 - rect.pos.x;
1023                 strut[head][1] = rect.pos.x + rect.size.width - area.x2;
1024                 strut[head][2] = area.y1 - rect.pos.y;
1025                 strut[head][3] = rect.pos.y + rect.size.height - area.y2;
1026         }
1027
1028         /* adjust workspace layout */
1029         wwin = scr->focused_window;
1030         most_right = 0;
1031         most_bottom = 0;
1032         most_left = scr->scr_width;
1033         most_top = scr->scr_height;
1034         for (; wwin; wwin = wwin->prev) {
1035                 if (wwin->frame->workspace == workspace) {
1036                         if (!wwin->flags.miniaturized && !IS_VSTUCK(wwin) && !wwin->flags.hidden) {
1037
1038                                 head = wGetHeadForWindow(wwin);
1039
1040                                 i = wwin->frame_x - strut[head][0];
1041                                 if (i < most_left)      /* record positions, should this be cached? */
1042                                         most_left = i;
1043                                 i = wwin->frame_x + wwin->frame->core->width + strut[head][1];
1044                                 if (HAS_BORDER(wwin))
1045                                         i += 2;
1046                                 if (i > most_right)
1047                                         most_right = i;
1048                                 i = wwin->frame_y - strut[head][2];
1049                                 if (i < most_top)
1050                                         most_top = i;
1051                                 i = wwin->frame_y + wwin->frame->core->height + strut[head][3];
1052                                 if (HAS_BORDER(wwin))
1053                                         i += 2;
1054                                 if (i > most_bottom) {
1055                                         most_bottom = i;
1056                                 }
1057                         }
1058                 }
1059         }
1060
1061         if (most_left > 0)
1062                 most_left = 0;
1063         if (most_top > 0)
1064                 most_top = 0;
1065
1066         scr->workspaces[workspace]->width = WMAX(most_right, scr->scr_width) - WMIN(most_left, 0);
1067         scr->workspaces[workspace]->height = WMAX(most_bottom, scr->scr_height) - WMIN(most_top, 0);
1068
1069         *view_x += -most_left - scr->workspaces[workspace]->view_x;
1070         scr->workspaces[workspace]->view_x = -most_left;
1071
1072         *view_y += -most_top - scr->workspaces[workspace]->view_y;
1073         scr->workspaces[workspace]->view_y = -most_top;
1074
1075         wfree(strut);
1076 }
1077
1078 typedef struct _delay_configure {
1079         WWindow *wwin;
1080         int delay_count;
1081 } _delay_configure;
1082
1083 void sendConfigureNotify(_delay_configure * delay)
1084 {
1085         WWindow *wwin;
1086
1087         delay->delay_count--;
1088         if (!delay->delay_count) {
1089                 for (wwin = delay->wwin; wwin; wwin = wwin->prev) {
1090                         wWindowSynthConfigureNotify(wwin);
1091                 }
1092         }
1093 }
1094
1095 Bool wWorkspaceSetViewport(WScreen * scr, int workspace, int view_x, int view_y)
1096 {
1097         Bool adjust_flag = False;
1098         int diff_x, diff_y;
1099         static _delay_configure delay_configure = { NULL, 0 };
1100         WWorkspace *wptr;
1101         WWindow *wwin;
1102
1103         wptr = scr->workspaces[workspace];
1104
1105         /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y); */
1106
1107         updateWorkspaceGeometry(scr, workspace, &view_x, &view_y);
1108
1109         if (view_x + scr->scr_width > wptr->width) {
1110                 /* puts("right edge of vdesk"); */
1111                 view_x = wptr->width - scr->scr_width;
1112         }
1113         if (view_x < 0) {
1114                 /* puts("left edge of vdesk"); */
1115                 view_x = 0;
1116         }
1117         if (view_y + scr->scr_height > wptr->height) {
1118                 /* puts("right edge of vdesk"); */
1119                 view_y = wptr->height - scr->scr_height;
1120         }
1121         if (view_y < 0) {
1122                 /* puts("left edge of vdesk"); */
1123                 view_y = 0;
1124         }
1125
1126         diff_x = wptr->view_x - view_x;
1127         diff_y = wptr->view_y - view_y;
1128         if (!diff_x && !diff_y)
1129                 return False;
1130
1131         wptr->view_x = view_x;
1132         wptr->view_y = view_y;
1133
1134 #ifdef NETWM_HINTS
1135         wNETWMUpdateDesktop(scr);
1136 #endif
1137
1138         for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
1139                 if (wwin->frame->workspace == workspace && !IS_VSTUCK(wwin)) {
1140                         wWindowMove(wwin, wwin->frame_x + diff_x, wwin->frame_y + diff_y);
1141                         adjust_flag = True;
1142                 }
1143         }
1144         if (1) {                /* if delay */
1145                 delay_configure.delay_count++;
1146                 delay_configure.wwin = scr->focused_window;
1147                 WMAddTimerHandler(200, (WMCallback *) sendConfigureNotify, &delay_configure);
1148         }
1149
1150         return adjust_flag;
1151 }
1152
1153 #endif
1154
1155 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
1156 {
1157         wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
1158 }
1159
1160 static void deleteWSCommand(WMenu * menu, WMenuEntry * entry)
1161 {
1162         wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
1163 }
1164
1165 static void newWSCommand(WMenu * menu, WMenuEntry * foo)
1166 {
1167         int ws;
1168
1169         ws = wWorkspaceNew(menu->frame->screen_ptr);
1170         /* autochange workspace */
1171         if (ws >= 0)
1172                 wWorkspaceChange(menu->frame->screen_ptr, ws);
1173
1174         /*
1175            if (ws<9) {
1176            int kcode;
1177            if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1178            kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1179            entry->rtext =
1180            wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1181            }
1182            } */
1183 }
1184
1185 static char *cropline(char *line)
1186 {
1187         char *start, *end;
1188
1189         if (strlen(line) == 0)
1190                 return line;
1191
1192         start = line;
1193         end = &(line[strlen(line)]) - 1;
1194         while (isspace(*line) && *line != 0)
1195                 line++;
1196         while (isspace(*end) && end != line) {
1197                 *end = 0;
1198                 end--;
1199         }
1200         return line;
1201 }
1202
1203 void wWorkspaceRename(WScreen * scr, int workspace, char *name)
1204 {
1205         char buf[MAX_WORKSPACENAME_WIDTH + 1];
1206         char *tmp;
1207
1208         if (workspace >= scr->workspace_count)
1209                 return;
1210
1211         /* trim white spaces */
1212         tmp = cropline(name);
1213
1214         if (strlen(tmp) == 0) {
1215                 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
1216         } else {
1217                 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
1218         }
1219         buf[MAX_WORKSPACENAME_WIDTH] = 0;
1220
1221         /* update workspace */
1222         wfree(scr->workspaces[workspace]->name);
1223         scr->workspaces[workspace]->name = wstrdup(buf);
1224
1225         if (scr->clip_ws_menu) {
1226                 if (strcmp(scr->clip_ws_menu->entries[workspace + 2]->text, buf) != 0) {
1227                         wfree(scr->clip_ws_menu->entries[workspace + 2]->text);
1228                         scr->clip_ws_menu->entries[workspace + 2]->text = wstrdup(buf);
1229                         wMenuRealize(scr->clip_ws_menu);
1230                 }
1231         }
1232         if (scr->workspace_menu) {
1233                 if (strcmp(scr->workspace_menu->entries[workspace + 2]->text, buf) != 0) {
1234                         wfree(scr->workspace_menu->entries[workspace + 2]->text);
1235                         scr->workspace_menu->entries[workspace + 2]->text = wstrdup(buf);
1236                         wMenuRealize(scr->workspace_menu);
1237                 }
1238         }
1239
1240         if (scr->clip_icon)
1241                 wClipIconPaint(scr->clip_icon);
1242
1243         WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
1244 }
1245
1246 /* callback for when menu entry is edited */
1247 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
1248 {
1249         char *tmp;
1250
1251         tmp = entry->text;
1252         wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
1253 }
1254
1255 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
1256 {
1257         WMenu *wsmenu;
1258
1259         wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
1260         if (!wsmenu) {
1261                 wwarning(_("could not create Workspace menu"));
1262                 return NULL;
1263         }
1264
1265         /* callback to be called when an entry is edited */
1266         wsmenu->on_edit = onMenuEntryEdited;
1267
1268         wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
1269         wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
1270
1271         return wsmenu;
1272 }
1273
1274 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
1275 {
1276         int i;
1277         long ws;
1278         char title[MAX_WORKSPACENAME_WIDTH + 1];
1279         WMenuEntry *entry;
1280         int tmp;
1281
1282         if (!menu)
1283                 return;
1284
1285         if (menu->entry_no < scr->workspace_count + 2) {
1286                 /* new workspace(s) added */
1287                 i = scr->workspace_count - (menu->entry_no - 2);
1288                 ws = menu->entry_no - 2;
1289                 while (i > 0) {
1290                         strncpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
1291
1292                         entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
1293                         entry->flags.indicator = 1;
1294                         entry->flags.editable = 1;
1295
1296                         i--;
1297                         ws++;
1298                 }
1299         } else if (menu->entry_no > scr->workspace_count + 2) {
1300                 /* removed workspace(s) */
1301                 for (i = menu->entry_no - 1; i >= scr->workspace_count + 2; i--) {
1302                         wMenuRemoveItem(menu, i);
1303                 }
1304         }
1305         wMenuRealize(menu);
1306
1307         for (i = 0; i < scr->workspace_count; i++) {
1308                 menu->entries[i + 2]->flags.indicator_on = 0;
1309         }
1310         menu->entries[scr->current_workspace + 2]->flags.indicator_on = 1;
1311
1312         /* don't let user destroy current workspace */
1313         if (scr->current_workspace == scr->workspace_count - 1) {
1314                 wMenuSetEnabled(menu, 1, False);
1315         } else {
1316                 wMenuSetEnabled(menu, 1, True);
1317         }
1318
1319         tmp = menu->frame->top_width + 5;
1320         /* if menu got unreachable, bring it to a visible place */
1321         if (menu->frame_x < tmp - (int)menu->frame->core->width)
1322                 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
1323
1324         wMenuPaint(menu);
1325 }
1326
1327 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
1328 {
1329         WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
1330         int i;
1331
1332         make_keys();
1333
1334         old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
1335         parr = WMCreatePLArray(NULL);
1336         for (i = 0; i < scr->workspace_count; i++) {
1337                 pstr = WMCreatePLString(scr->workspaces[i]->name);
1338                 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
1339                 WMReleasePropList(pstr);
1340                 if (!wPreferences.flags.noclip) {
1341                         pstr = wClipSaveWorkspaceState(scr, i);
1342                         WMPutInPLDictionary(wks_state, dClip, pstr);
1343                         WMReleasePropList(pstr);
1344                 } else if (old_wks_state != NULL) {
1345                         if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
1346                                 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
1347                                         WMPutInPLDictionary(wks_state, dClip, bar);
1348                                 }
1349                         }
1350                 }
1351                 WMAddToPLArray(parr, wks_state);
1352                 WMReleasePropList(wks_state);
1353         }
1354         WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
1355         WMReleasePropList(parr);
1356 }
1357
1358 void wWorkspaceRestoreState(WScreen * scr)
1359 {
1360         WMPropList *parr, *pstr, *wks_state, *clip_state;
1361         int i, j, wscount;
1362
1363         make_keys();
1364
1365         if (scr->session_state == NULL)
1366                 return;
1367
1368         parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
1369
1370         if (!parr)
1371                 return;
1372
1373         wscount = scr->workspace_count;
1374         for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
1375                 wks_state = WMGetFromPLArray(parr, i);
1376                 if (WMIsPLDictionary(wks_state))
1377                         pstr = WMGetFromPLDictionary(wks_state, dName);
1378                 else
1379                         pstr = wks_state;
1380                 if (i >= scr->workspace_count)
1381                         wWorkspaceNew(scr);
1382                 if (scr->workspace_menu) {
1383                         wfree(scr->workspace_menu->entries[i + 2]->text);
1384                         scr->workspace_menu->entries[i + 2]->text = wstrdup(WMGetFromPLString(pstr));
1385                         scr->workspace_menu->flags.realized = 0;
1386                 }
1387                 wfree(scr->workspaces[i]->name);
1388                 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
1389                 if (!wPreferences.flags.noclip) {
1390                         clip_state = WMGetFromPLDictionary(wks_state, dClip);
1391                         if (scr->workspaces[i]->clip)
1392                                 wDockDestroy(scr->workspaces[i]->clip);
1393                         scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
1394                         if (i > 0)
1395                                 wDockHideIcons(scr->workspaces[i]->clip);
1396
1397                         /* We set the global icons here, because scr->workspaces[i]->clip
1398                          * was not valid in wDockRestoreState().
1399                          * There we only set icon->omnipresent to know which icons we
1400                          * need to set here.
1401                          */
1402                         for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
1403                                 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
1404
1405                                 if (aicon && aicon->omnipresent) {
1406                                         aicon->omnipresent = 0;
1407                                         wClipMakeIconOmnipresent(aicon, True);
1408                                         XMapWindow(dpy, aicon->icon->core->window);
1409                                 }
1410                         }
1411                 }
1412
1413                 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);
1414         }
1415 }