Yet another trivial coding style cleanup
[wmaker-crm.git] / src / workspace.c
blob718bb46396e99adc1f9cb2ca65c04d474423f9a1
1 /* workspace.c- Workspace management
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
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.
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.
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.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
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>
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
55 #include "xinerama.h"
57 #define MAX_SHORTCUT_LENGTH 32
58 #define WORKSPACE_NAME_DISPLAY_PADDING 32
60 extern WPreferences wPreferences;
61 extern XContext wWinContext;
62 extern XContext wVEdgeContext;
64 extern void ProcessPendingEvents();
66 static WMPropList *dWorkspaces = NULL;
67 static WMPropList *dClip, *dName;
69 static void make_keys()
71 if (dWorkspaces != NULL)
72 return;
74 dWorkspaces = WMCreatePLString("Workspaces");
75 dName = WMCreatePLString("Name");
76 dClip = WMCreatePLString("Clip");
79 void wWorkspaceMake(WScreen * scr, int count)
81 while (count > 0) {
82 wWorkspaceNew(scr);
83 count--;
87 int wWorkspaceNew(WScreen * scr)
89 WWorkspace *wspace, **list;
90 int i;
92 if (scr->workspace_count < MAX_WORKSPACES) {
93 scr->workspace_count++;
95 wspace = wmalloc(sizeof(WWorkspace));
96 wspace->name = NULL;
98 if (!wspace->name) {
99 wspace->name = wmalloc(strlen(_("Workspace %i")) + 8);
100 sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
103 if (!wPreferences.flags.noclip) {
104 wspace->clip = wDockCreate(scr, WM_CLIP);
105 } else
106 wspace->clip = NULL;
108 list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
110 for (i = 0; i < scr->workspace_count - 1; i++) {
111 list[i] = scr->workspaces[i];
113 list[i] = wspace;
114 if (scr->workspaces)
115 wfree(scr->workspaces);
116 scr->workspaces = list;
118 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
119 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
120 #ifdef VIRTUAL_DESKTOP
121 wspace->view_x = wspace->view_y = 0;
122 wspace->height = scr->scr_height;
123 wspace->width = scr->scr_width;
124 #endif
125 #ifdef NETWM_HINTS
126 wNETWMUpdateDesktop(scr);
127 #endif
129 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
130 XFlush(dpy);
132 return scr->workspace_count - 1;
135 return -1;
138 Bool wWorkspaceDelete(WScreen * scr, int workspace)
140 WWindow *tmp;
141 WWorkspace **list;
142 int i, j;
144 if (workspace <= 0)
145 return False;
147 /* verify if workspace is in use by some window */
148 tmp = scr->focused_window;
149 while (tmp) {
150 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
151 return False;
152 tmp = tmp->prev;
155 if (!wPreferences.flags.noclip) {
156 wDockDestroy(scr->workspaces[workspace]->clip);
157 scr->workspaces[workspace]->clip = NULL;
160 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
161 j = 0;
162 for (i = 0; i < scr->workspace_count; i++) {
163 if (i != workspace) {
164 list[j++] = scr->workspaces[i];
165 } else {
166 if (scr->workspaces[i]->name)
167 wfree(scr->workspaces[i]->name);
168 wfree(scr->workspaces[i]);
171 wfree(scr->workspaces);
172 scr->workspaces = list;
174 scr->workspace_count--;
176 /* update menu */
177 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
178 /* clip workspace menu */
179 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
181 /* update also window menu */
182 if (scr->workspace_submenu) {
183 WMenu *menu = scr->workspace_submenu;
185 i = menu->entry_no;
186 while (i > scr->workspace_count)
187 wMenuRemoveItem(menu, --i);
188 wMenuRealize(menu);
190 /* and clip menu */
191 if (scr->clip_submenu) {
192 WMenu *menu = scr->clip_submenu;
194 i = menu->entry_no;
195 while (i > scr->workspace_count)
196 wMenuRemoveItem(menu, --i);
197 wMenuRealize(menu);
199 #ifdef NETWM_HINTS
200 wNETWMUpdateDesktop(scr);
201 #endif
203 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
205 if (scr->current_workspace >= scr->workspace_count)
206 wWorkspaceChange(scr, scr->workspace_count - 1);
208 return True;
211 typedef struct WorkspaceNameData {
212 int count;
213 RImage *back;
214 RImage *text;
215 time_t timeout;
216 } WorkspaceNameData;
218 static void hideWorkspaceName(void *data)
220 WScreen *scr = (WScreen *) data;
222 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
223 || time(NULL) > scr->workspace_name_data->timeout) {
224 XUnmapWindow(dpy, scr->workspace_name);
226 if (scr->workspace_name_data) {
227 RReleaseImage(scr->workspace_name_data->back);
228 RReleaseImage(scr->workspace_name_data->text);
229 wfree(scr->workspace_name_data);
231 scr->workspace_name_data = NULL;
233 scr->workspace_name_timer = NULL;
234 } else {
235 RImage *img = RCloneImage(scr->workspace_name_data->back);
236 Pixmap pix;
238 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
240 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
241 scr->workspace_name_data->count * 255 / 10);
243 RConvertImage(scr->rcontext, img, &pix);
245 RReleaseImage(img);
247 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
248 XClearWindow(dpy, scr->workspace_name);
249 XFreePixmap(dpy, pix);
250 XFlush(dpy);
252 scr->workspace_name_data->count--;
256 static void showWorkspaceName(WScreen * scr, int workspace)
258 WorkspaceNameData *data;
259 RXImage *ximg;
260 Pixmap text, mask;
261 int w, h;
262 int px, py;
263 char *name = scr->workspaces[workspace]->name;
264 int len = strlen(name);
265 int x, y;
266 #ifdef XINERAMA
267 int head;
268 WMRect rect;
269 int xx, yy;
270 #endif
272 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2) {
273 return;
276 if (scr->workspace_name_timer) {
277 WMDeleteTimerHandler(scr->workspace_name_timer);
278 XUnmapWindow(dpy, scr->workspace_name);
279 XFlush(dpy);
281 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
283 if (scr->workspace_name_data) {
284 RReleaseImage(scr->workspace_name_data->back);
285 RReleaseImage(scr->workspace_name_data->text);
286 wfree(scr->workspace_name_data);
289 data = wmalloc(sizeof(WorkspaceNameData));
290 data->back = NULL;
292 w = WMWidthOfString(scr->workspace_name_font, name, len);
293 h = WMFontHeight(scr->workspace_name_font);
295 #ifdef XINERAMA
296 head = wGetHeadForPointerLocation(scr);
297 rect = wGetRectForHead(scr, head);
298 if (scr->xine_info.count) {
299 xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
300 yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
302 else {
303 xx = (scr->scr_width - (w + 4)) / 2;
304 yy = (scr->scr_height - (h + 4)) / 2;
306 #endif
308 switch (wPreferences.workspace_name_display_position) {
309 case WD_TOP:
310 #ifdef XINERAMA
311 px = xx;
312 #else
313 px = (scr->scr_width - (w + 4)) / 2;
314 #endif
315 py = WORKSPACE_NAME_DISPLAY_PADDING;
316 break;
317 case WD_BOTTOM:
318 #ifdef XINERAMA
319 px = xx;
320 #else
321 px = (scr->scr_width - (w + 4)) / 2;
322 #endif
323 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
324 break;
325 case WD_TOPLEFT:
326 px = WORKSPACE_NAME_DISPLAY_PADDING;
327 py = WORKSPACE_NAME_DISPLAY_PADDING;
328 break;
329 case WD_TOPRIGHT:
330 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
331 py = WORKSPACE_NAME_DISPLAY_PADDING;
332 break;
333 case WD_BOTTOMLEFT:
334 px = WORKSPACE_NAME_DISPLAY_PADDING;
335 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
336 break;
337 case WD_BOTTOMRIGHT:
338 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
339 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
340 break;
341 case WD_CENTER:
342 default:
343 #ifdef XINERAMA
344 px = xx;
345 py = yy;
346 #else
347 px = (scr->scr_width - (w + 4)) / 2;
348 py = (scr->scr_height - (h + 4)) / 2;
349 #endif
350 break;
352 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
353 XMoveWindow(dpy, scr->workspace_name, px, py);
355 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
356 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
358 /*XSetForeground(dpy, scr->mono_gc, 0);
359 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
361 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
363 for (x = 0; x <= 4; x++) {
364 for (y = 0; y <= 4; y++) {
365 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
369 XSetForeground(dpy, scr->mono_gc, 1);
370 XSetBackground(dpy, scr->mono_gc, 0);
372 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
374 /*XSetForeground(dpy, scr->mono_gc, 1); */
375 XSetBackground(dpy, scr->mono_gc, 1);
377 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
379 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
381 #ifdef SHAPE
382 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
383 #endif
384 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
385 XClearWindow(dpy, scr->workspace_name);
387 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
389 XFreePixmap(dpy, text);
390 XFreePixmap(dpy, mask);
392 if (!data->text) {
393 XMapRaised(dpy, scr->workspace_name);
394 XFlush(dpy);
396 goto erro;
399 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
401 if (!ximg || !ximg->image) {
402 goto erro;
405 XMapRaised(dpy, scr->workspace_name);
406 XFlush(dpy);
408 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
409 RDestroyXImage(scr->rcontext, ximg);
411 if (!data->back) {
412 goto erro;
415 data->count = 10;
417 /* set a timeout for the effect */
418 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
420 scr->workspace_name_data = data;
422 return;
424 erro:
425 if (scr->workspace_name_timer)
426 WMDeleteTimerHandler(scr->workspace_name_timer);
428 if (data->text)
429 RReleaseImage(data->text);
430 if (data->back)
431 RReleaseImage(data->back);
432 wfree(data);
434 scr->workspace_name_data = NULL;
436 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
437 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
440 void wWorkspaceChange(WScreen *scr, int workspace)
442 if (scr->flags.startup || scr->flags.startup2)
443 return;
445 if (workspace != scr->current_workspace)
446 wWorkspaceForceChange(scr, workspace);
449 void wWorkspaceRelativeChange(WScreen * scr, int amount)
451 int w;
453 w = scr->current_workspace + amount;
455 if (amount < 0) {
456 if (w >= 0) {
457 wWorkspaceChange(scr, w);
458 } else if (wPreferences.ws_cycle) {
459 wWorkspaceChange(scr, scr->workspace_count + w);
461 } else if (amount > 0) {
462 if (w < scr->workspace_count) {
463 wWorkspaceChange(scr, w);
464 } else if (wPreferences.ws_advance) {
465 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
466 } else if (wPreferences.ws_cycle) {
467 wWorkspaceChange(scr, w % scr->workspace_count);
472 void wWorkspaceForceChange(WScreen * scr, int workspace)
474 WWindow *tmp, *foc = NULL, *foc2 = NULL;
476 if (workspace >= MAX_WORKSPACES || workspace < 0)
477 return;
479 SendHelperMessage(scr, 'C', workspace + 1, NULL);
481 if (workspace > scr->workspace_count - 1) {
482 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
485 wClipUpdateForWorkspaceChange(scr, workspace);
487 scr->current_workspace = workspace;
489 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
491 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
493 if ((tmp = scr->focused_window) != NULL) {
494 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
495 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
496 foc = tmp;
499 /* foc2 = tmp; will fix annoyance with gnome panel
500 * but will create annoyance for every other application
502 while (tmp) {
503 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
504 /* unmap windows not on this workspace */
505 if ((tmp->flags.mapped || tmp->flags.shaded) &&
506 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
507 wWindowUnmap(tmp);
509 /* also unmap miniwindows not on this workspace */
510 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
511 tmp->icon && !IS_OMNIPRESENT(tmp)) {
512 XUnmapWindow(dpy, tmp->icon->core->window);
513 tmp->icon->mapped = 0;
515 /* update current workspace of omnipresent windows */
516 if (IS_OMNIPRESENT(tmp)) {
517 WApplication *wapp = wApplicationOf(tmp->main_window);
519 tmp->frame->workspace = workspace;
521 if (wapp) {
522 wapp->last_workspace = workspace;
524 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
525 foc2 = tmp;
528 } else {
529 /* change selected windows' workspace */
530 if (tmp->flags.selected) {
531 wWindowChangeWorkspace(tmp, workspace);
532 if (!tmp->flags.miniaturized && !foc) {
533 foc = tmp;
535 } else {
536 if (!tmp->flags.hidden) {
537 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
538 /* remap windows that are on this workspace */
539 wWindowMap(tmp);
540 if (!foc && !WFLAGP(tmp, no_focusable)) {
541 foc = tmp;
544 /* Also map miniwindow if not omnipresent */
545 if (!wPreferences.sticky_icons &&
546 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
547 tmp->icon->mapped = 1;
548 XMapWindow(dpy, tmp->icon->core->window);
553 tmp = tmp->prev;
556 /* Gobble up events unleashed by our mapping & unmapping.
557 * These may trigger various grab-initiated focus &
558 * crossing events. However, we don't care about them,
559 * and ignore their focus implications altogether to avoid
560 * flicker.
562 scr->flags.ignore_focus_events = 1;
563 ProcessPendingEvents();
564 scr->flags.ignore_focus_events = 0;
566 if (!foc)
567 foc = foc2;
569 if (scr->focused_window->flags.mapped && !foc) {
570 foc = scr->focused_window;
572 if (wPreferences.focus_mode == WKF_CLICK) {
573 wSetFocusTo(scr, foc);
574 } else {
575 unsigned int mask;
576 int foo;
577 Window bar, win;
578 WWindow *tmp;
580 tmp = NULL;
581 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
582 tmp = wWindowFor(win);
585 /* If there's a window under the pointer, focus it.
586 * (we ate all other focus events above, so it's
587 * certainly not focused). Otherwise focus last
588 * focused, or the root (depending on sloppiness)
590 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
591 wSetFocusTo(scr, foc);
592 } else {
593 wSetFocusTo(scr, tmp);
598 /* We need to always arrange icons when changing workspace, even if
599 * no autoarrange icons, because else the icons in different workspaces
600 * can be superposed.
601 * This can be avoided if appicons are also workspace specific.
603 if (!wPreferences.sticky_icons)
604 wArrangeIcons(scr, False);
606 if (scr->dock)
607 wAppIconPaint(scr->dock->icon_array[0]);
609 if (scr->clip_icon) {
610 if (scr->workspaces[workspace]->clip->auto_collapse ||
611 scr->workspaces[workspace]->clip->auto_raise_lower) {
612 /* to handle enter notify. This will also */
613 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
614 XMapWindow(dpy, scr->clip_icon->icon->core->window);
615 } else {
616 wClipIconPaint(scr->clip_icon);
619 #ifdef NETWM_HINTS
620 wScreenUpdateUsableArea(scr);
621 wNETWMUpdateDesktop(scr);
622 #endif
624 showWorkspaceName(scr, workspace);
626 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
628 /* XSync(dpy, False); */
631 #ifdef VIRTUAL_DESKTOP
633 /* TODO:
635 * 1) Allow border around each window so the scrolling
636 * won't just stop at the border.
637 * 2) Make pager.
641 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
642 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
643 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
644 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
645 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
646 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
648 #ifndef HAS_BORDER
649 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
650 #endif
652 #ifndef IS_VSTUCK
653 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
654 #endif
656 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
658 static Bool cmp_gez(int i)
660 return (i >= 0);
663 static Bool cmp_gz(int i)
665 return (i > 0);
668 static WMPoint getClosestEdge(WScreen * scr, WMPoint direction, Bool(*cmp) (int))
670 WMPoint closest = wmkpoint(0, 0);
671 int closest_len = INT_MAX;
672 WWindow *wwin;
674 for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
675 if (wwin->frame->workspace == scr->current_workspace) {
676 if (!wwin->flags.miniaturized && !IS_VSTUCK(wwin) && !wwin->flags.hidden) {
677 int border = 2 * HAS_BORDER(wwin);
678 int len;
679 int x1, x2, y1, y2;
680 int head = wGetHeadForWindow(wwin);
681 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
682 WMPoint p;
684 x1 = wwin->frame_x - area.x1;
685 y1 = wwin->frame_y - area.y1;
686 x2 = wwin->frame_x + wwin->frame->core->width + border - area.x2;
687 y2 = wwin->frame_y + wwin->frame->core->height + border - area.y2;
689 p = wmkpoint(x1, y1);
690 len = vec_dot(direction, p);
691 updateMinimum(len, p, closest_len, closest);
693 p = wmkpoint(x1, y2);
694 len = vec_dot(direction, p);
695 updateMinimum(len, p, closest_len, closest);
697 p = wmkpoint(x2, y1);
698 len = vec_dot(direction, p);
699 updateMinimum(len, p, closest_len, closest);
701 p = wmkpoint(x2, y2);
702 len = vec_dot(direction, p);
703 updateMinimum(len, p, closest_len, closest);
708 return closest;
711 static void getViewPosition(WScreen * scr, int workspace, int *x, int *y)
713 *x = scr->workspaces[workspace]->view_x;
714 *y = scr->workspaces[workspace]->view_y;
717 void wWorkspaceKeyboardMoveDesktop(WScreen * scr, WMPoint direction)
719 int x, y;
720 WMPoint edge = getClosestEdge(scr, direction, cmp_gz);
721 int len = vec_dot(edge, direction);
722 WMPoint step = vec_scale(direction, len);
723 getViewPosition(scr, scr->current_workspace, &x, &y);
724 wWorkspaceSetViewport(scr, scr->current_workspace, x + step.x, y + step.y);
727 extern Cursor wCursor[WCUR_LAST];
729 static void vdMouseMoveDesktop(XEvent * event, WMPoint direction)
731 static int lock = False;
732 if (lock)
733 return;
734 lock = True;
736 Bool done = False;
737 Bool moved = True;
738 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
739 WMPoint old_pos = wmkpoint(event->xcrossing.x_root, event->xcrossing.y_root);
740 WMPoint step;
741 int x, y;
742 int resisted = 0;
744 if (XGrabPointer(dpy, event->xcrossing.window, False,
745 PointerMotionMask, GrabModeAsync, GrabModeAsync,
746 scr->root_win, wCursor[WCUR_EMPTY], CurrentTime) != GrabSuccess) {
748 /* if the grab fails, do it the old fashioned way */
749 step = vec_scale2(direction, wPreferences.vedge_hscrollspeed, wPreferences.vedge_vscrollspeed);
750 getViewPosition(scr, scr->current_workspace, &x, &y);
751 if (wWorkspaceSetViewport(scr, scr->current_workspace, x + step.x, y + step.y)) {
752 step = vec_scale(direction, 2);
753 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0,
754 event->xcrossing.x_root - step.x, event->xcrossing.y_root - step.y);
756 goto exit;
758 XSync(dpy, True);
760 if (old_pos.x < 0)
761 old_pos.x = 0;
762 if (old_pos.y < 0)
763 old_pos.y = 0;
764 if (old_pos.x > scr->scr_width)
765 old_pos.x = scr->scr_width;
766 if (old_pos.y > scr->scr_height)
767 old_pos.y = scr->scr_height;
769 while (!done) {
770 XEvent ev;
771 if (moved) {
772 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0,
773 scr->scr_width / 2, scr->scr_height / 2);
774 moved = False;
776 WMMaskEvent(dpy, PointerMotionMask, &ev);
778 switch (ev.type) {
779 case MotionNotify:
781 int step_len;
782 step = wmkpoint(ev.xmotion.x_root - scr->scr_width / 2,
783 ev.xmotion.y_root - scr->scr_height / 2);
784 step_len = vec_dot(step, direction);
785 if (step_len < 0) {
786 done = True;
787 break;
790 if (step_len > 0) {
791 Bool do_move = True;
792 int resist = wPreferences.vedge_resistance;
793 WMPoint closest;
794 int closest_len = INT_MAX;
795 if (resist) {
796 closest = getClosestEdge(scr, direction, cmp_gez);
797 closest_len = vec_dot(direction, closest);
800 if (!closest_len) {
801 resisted += step_len;
802 do_move = resisted >= resist;
803 if (do_move) {
804 closest_len = INT_MAX;
805 step_len = resisted - resist;
806 resisted = 0;
809 if (do_move) {
810 if (closest_len <= wPreferences.vedge_attraction) {
811 step = vec_scale(direction, closest_len);
812 } else {
813 step = vec_scale(direction, step_len);
816 getViewPosition(scr, scr->current_workspace, &x, &y);
817 wWorkspaceSetViewport(scr, scr->current_workspace,
818 x + step.x, y + step.y);
819 moved = True;
823 break;
827 step = vec_add(old_pos, vec_scale(direction, -1));
828 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0, step.x, step.y);
829 XUngrabPointer(dpy, CurrentTime);
831 exit:
832 lock = False;
835 static void vdHandleEnter_u(XEvent * event)
837 vdMouseMoveDesktop(event, VEC_UP);
840 static void vdHandleEnter_d(XEvent * event)
842 vdMouseMoveDesktop(event, VEC_DOWN);
845 static void vdHandleEnter_l(XEvent * event)
847 vdMouseMoveDesktop(event, VEC_LEFT);
850 static void vdHandleEnter_r(XEvent * event)
852 vdMouseMoveDesktop(event, VEC_RIGHT);
855 #define LEFT_EDGE 0x01
856 #define RIGHT_EDGE 0x02
857 #define TOP_EDGE 0x04
858 #define BOTTOM_EDGE 0x08
859 #define ALL_EDGES 0x0F
861 static void createEdges(WScreen * scr)
863 if (!scr->virtual_edges) {
864 int i, j, w;
865 int vmask;
866 XSetWindowAttributes attribs;
868 int heads = wXineramaHeads(scr);
869 int *hasEdges = (int *)wmalloc(sizeof(int) * heads);
871 int thickness = 1;
872 int nr_edges = 0;
873 int max_edges = 4 * heads;
874 int head;
875 Window *edges = (Window *) wmalloc(sizeof(Window) * max_edges);
877 for (i = 0; i < heads; ++i)
878 hasEdges[i] = ALL_EDGES;
879 for (i = 0; i < heads; ++i) {
880 WMRect i_rect = wGetRectForHead(scr, i);
881 for (j = i + 1; j < heads; ++j) {
882 WMRect j_rect = wGetRectForHead(scr, j);
884 int vlen = (WMIN(i_rect.pos.y + i_rect.size.height,
885 j_rect.pos.y + j_rect.size.height) -
886 WMAX(i_rect.pos.y, j_rect.pos.y));
888 int hlen = (WMIN(i_rect.pos.x + i_rect.size.width,
889 j_rect.pos.x + j_rect.size.width) -
890 WMAX(i_rect.pos.x, j_rect.pos.x));
892 if (vlen > 0 && hlen == 0) { /* horz alignment, vert edges touch */
893 if (i_rect.pos.x < j_rect.pos.x) { /* i left of j */
894 hasEdges[i] &= ~RIGHT_EDGE;
895 hasEdges[j] &= ~LEFT_EDGE;
896 } else { /* j left of i */
897 hasEdges[j] &= ~RIGHT_EDGE;
898 hasEdges[i] &= ~LEFT_EDGE;
900 } else if (vlen == 0 && hlen > 0) { /* vert alignment, horz edges touch */
901 if (i_rect.pos.y < j_rect.pos.y) { /* i top of j */
902 hasEdges[i] &= ~BOTTOM_EDGE;
903 hasEdges[j] &= ~TOP_EDGE;
904 } else { /* j top of i */
905 hasEdges[j] &= ~BOTTOM_EDGE;
906 hasEdges[i] &= ~TOP_EDGE;
912 for (w = 0; w < scr->workspace_count; w++) {
913 /* puts("reset workspace"); */
914 wWorkspaceSetViewport(scr, w, 0, 0);
917 vmask = CWEventMask | CWOverrideRedirect;
918 attribs.event_mask = (EnterWindowMask | LeaveWindowMask | VisibilityChangeMask);
919 attribs.override_redirect = True;
921 for (head = 0; head < wXineramaHeads(scr); ++head) {
922 WMRect rect = wGetRectForHead(scr, head);
924 if (hasEdges[head] & TOP_EDGE) {
925 edges[nr_edges] =
926 XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
927 rect.size.width, thickness, 0,
928 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
929 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_u);
930 ++nr_edges;
933 if (hasEdges[head] & BOTTOM_EDGE) {
934 edges[nr_edges] =
935 XCreateWindow(dpy, scr->root_win, rect.pos.x,
936 rect.pos.y + rect.size.height - thickness,
937 rect.size.width, thickness, 0,
938 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
939 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_d);
940 ++nr_edges;
943 if (hasEdges[head] & LEFT_EDGE) {
944 edges[nr_edges] =
945 XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
946 thickness, rect.pos.y + rect.size.height, 0,
947 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
948 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_l);
949 ++nr_edges;
952 if (hasEdges[head] & RIGHT_EDGE) {
953 edges[nr_edges] =
954 XCreateWindow(dpy, scr->root_win,
955 rect.pos.x + rect.size.width - thickness, rect.pos.y,
956 thickness, rect.size.height, 0,
957 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
958 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_r);
959 ++nr_edges;
963 scr->virtual_nr_edges = nr_edges;
964 scr->virtual_edges = edges;
966 for (i = 0; i < scr->virtual_nr_edges; ++i) {
967 XMapWindow(dpy, scr->virtual_edges[i]);
969 wWorkspaceRaiseEdge(scr);
971 wfree(hasEdges);
975 static void destroyEdges(WScreen * scr)
977 if (scr->virtual_edges) {
978 int i;
980 for (i = 0; i < scr->virtual_nr_edges; ++i) {
981 XDeleteContext(dpy, scr->virtual_edges[i], wVEdgeContext);
982 XUnmapWindow(dpy, scr->virtual_edges[i]);
983 XDestroyWindow(dpy, scr->virtual_edges[i]);
986 wfree(scr->virtual_edges);
987 scr->virtual_edges = NULL;
988 scr->virtual_nr_edges = 0;
992 void wWorkspaceUpdateEdge(WScreen * scr)
994 if (wPreferences.vdesk_enable) {
995 createEdges(scr);
996 } else {
997 destroyEdges(scr);
1001 void wWorkspaceRaiseEdge(WScreen * scr)
1003 static int toggle = 0;
1004 int i;
1006 if (!scr->virtual_edges)
1007 return;
1009 if (toggle) {
1010 for (i = 0; i < scr->virtual_nr_edges; ++i) {
1011 XRaiseWindow(dpy, scr->virtual_edges[i]);
1013 } else {
1014 for (i = scr->virtual_nr_edges - 1; i >= 0; --i) {
1015 XRaiseWindow(dpy, scr->virtual_edges[i]);
1019 toggle ^= 1;
1022 void wWorkspaceLowerEdge(WScreen * scr)
1024 int i;
1025 for (i = 0; i < scr->virtual_nr_edges; ++i) {
1026 XLowerWindow(dpy, scr->virtual_edges[i]);
1030 void wWorkspaceResizeViewport(WScreen * scr, int workspace)
1032 int x, y;
1033 getViewPosition(scr, scr->current_workspace, &x, &y);
1034 wWorkspaceSetViewport(scr, scr->current_workspace, x, y);
1037 void updateWorkspaceGeometry(WScreen * scr, int workspace, int *view_x, int *view_y)
1039 int most_left, most_right, most_top, most_bottom;
1040 WWindow *wwin;
1042 int heads = wXineramaHeads(scr);
1043 typedef int strut_t[4];
1044 strut_t *strut = (strut_t *) wmalloc(heads * sizeof(strut_t));
1045 int head, i;
1047 for (head = 0; head < heads; ++head) {
1048 WMRect rect = wGetRectForHead(scr, head);
1049 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1050 strut[head][0] = area.x1 - rect.pos.x;
1051 strut[head][1] = rect.pos.x + rect.size.width - area.x2;
1052 strut[head][2] = area.y1 - rect.pos.y;
1053 strut[head][3] = rect.pos.y + rect.size.height - area.y2;
1056 /* adjust workspace layout */
1057 wwin = scr->focused_window;
1058 most_right = 0;
1059 most_bottom = 0;
1060 most_left = scr->scr_width;
1061 most_top = scr->scr_height;
1062 for (; wwin; wwin = wwin->prev) {
1063 if (wwin->frame->workspace == workspace) {
1064 if (!wwin->flags.miniaturized && !IS_VSTUCK(wwin) && !wwin->flags.hidden) {
1066 head = wGetHeadForWindow(wwin);
1068 i = wwin->frame_x - strut[head][0];
1069 if (i < most_left) /* record positions, should this be cached? */
1070 most_left = i;
1071 i = wwin->frame_x + wwin->frame->core->width + strut[head][1];
1072 if (HAS_BORDER(wwin))
1073 i += 2;
1074 if (i > most_right)
1075 most_right = i;
1076 i = wwin->frame_y - strut[head][2];
1077 if (i < most_top)
1078 most_top = i;
1079 i = wwin->frame_y + wwin->frame->core->height + strut[head][3];
1080 if (HAS_BORDER(wwin))
1081 i += 2;
1082 if (i > most_bottom) {
1083 most_bottom = i;
1089 if (most_left > 0)
1090 most_left = 0;
1091 if (most_top > 0)
1092 most_top = 0;
1094 scr->workspaces[workspace]->width = WMAX(most_right, scr->scr_width) - WMIN(most_left, 0);
1095 scr->workspaces[workspace]->height = WMAX(most_bottom, scr->scr_height) - WMIN(most_top, 0);
1097 *view_x += -most_left - scr->workspaces[workspace]->view_x;
1098 scr->workspaces[workspace]->view_x = -most_left;
1100 *view_y += -most_top - scr->workspaces[workspace]->view_y;
1101 scr->workspaces[workspace]->view_y = -most_top;
1103 wfree(strut);
1106 typedef struct _delay_configure {
1107 WWindow *wwin;
1108 int delay_count;
1109 } _delay_configure;
1111 void sendConfigureNotify(_delay_configure * delay)
1113 WWindow *wwin;
1115 delay->delay_count--;
1116 if (!delay->delay_count) {
1117 for (wwin = delay->wwin; wwin; wwin = wwin->prev) {
1118 wWindowSynthConfigureNotify(wwin);
1123 Bool wWorkspaceSetViewport(WScreen * scr, int workspace, int view_x, int view_y)
1125 Bool adjust_flag = False;
1126 int diff_x, diff_y;
1127 static _delay_configure delay_configure = { NULL, 0 };
1128 WWorkspace *wptr;
1129 WWindow *wwin;
1131 wptr = scr->workspaces[workspace];
1133 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y); */
1135 updateWorkspaceGeometry(scr, workspace, &view_x, &view_y);
1137 if (view_x + scr->scr_width > wptr->width) {
1138 /* puts("right edge of vdesk"); */
1139 view_x = wptr->width - scr->scr_width;
1141 if (view_x < 0) {
1142 /* puts("left edge of vdesk"); */
1143 view_x = 0;
1145 if (view_y + scr->scr_height > wptr->height) {
1146 /* puts("right edge of vdesk"); */
1147 view_y = wptr->height - scr->scr_height;
1149 if (view_y < 0) {
1150 /* puts("left edge of vdesk"); */
1151 view_y = 0;
1154 diff_x = wptr->view_x - view_x;
1155 diff_y = wptr->view_y - view_y;
1156 if (!diff_x && !diff_y)
1157 return False;
1159 wptr->view_x = view_x;
1160 wptr->view_y = view_y;
1162 #ifdef NETWM_HINTS
1163 wNETWMUpdateDesktop(scr);
1164 #endif
1166 for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
1167 if (wwin->frame->workspace == workspace && !IS_VSTUCK(wwin)) {
1168 wWindowMove(wwin, wwin->frame_x + diff_x, wwin->frame_y + diff_y);
1169 adjust_flag = True;
1172 if (1) { /* if delay */
1173 delay_configure.delay_count++;
1174 delay_configure.wwin = scr->focused_window;
1175 WMAddTimerHandler(200, (WMCallback *) sendConfigureNotify, &delay_configure);
1178 return adjust_flag;
1181 #endif
1183 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
1185 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
1188 static void deleteWSCommand(WMenu * menu, WMenuEntry * entry)
1190 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
1193 static void newWSCommand(WMenu * menu, WMenuEntry * foo)
1195 int ws;
1197 ws = wWorkspaceNew(menu->frame->screen_ptr);
1198 /* autochange workspace */
1199 if (ws >= 0)
1200 wWorkspaceChange(menu->frame->screen_ptr, ws);
1203 if (ws<9) {
1204 int kcode;
1205 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1206 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1207 entry->rtext =
1208 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1210 } */
1213 static char *cropline(char *line)
1215 char *start, *end;
1217 if (strlen(line) == 0)
1218 return line;
1220 start = line;
1221 end = &(line[strlen(line)]) - 1;
1222 while (isspace(*line) && *line != 0)
1223 line++;
1224 while (isspace(*end) && end != line) {
1225 *end = 0;
1226 end--;
1228 return line;
1231 void wWorkspaceRename(WScreen * scr, int workspace, char *name)
1233 char buf[MAX_WORKSPACENAME_WIDTH + 1];
1234 char *tmp;
1236 if (workspace >= scr->workspace_count)
1237 return;
1239 /* trim white spaces */
1240 tmp = cropline(name);
1242 if (strlen(tmp) == 0) {
1243 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
1244 } else {
1245 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
1247 buf[MAX_WORKSPACENAME_WIDTH] = 0;
1249 /* update workspace */
1250 wfree(scr->workspaces[workspace]->name);
1251 scr->workspaces[workspace]->name = wstrdup(buf);
1253 if (scr->clip_ws_menu) {
1254 if (strcmp(scr->clip_ws_menu->entries[workspace + 2]->text, buf) != 0) {
1255 wfree(scr->clip_ws_menu->entries[workspace + 2]->text);
1256 scr->clip_ws_menu->entries[workspace + 2]->text = wstrdup(buf);
1257 wMenuRealize(scr->clip_ws_menu);
1260 if (scr->workspace_menu) {
1261 if (strcmp(scr->workspace_menu->entries[workspace + 2]->text, buf) != 0) {
1262 wfree(scr->workspace_menu->entries[workspace + 2]->text);
1263 scr->workspace_menu->entries[workspace + 2]->text = wstrdup(buf);
1264 wMenuRealize(scr->workspace_menu);
1268 if (scr->clip_icon)
1269 wClipIconPaint(scr->clip_icon);
1271 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
1274 /* callback for when menu entry is edited */
1275 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
1277 char *tmp;
1279 tmp = entry->text;
1280 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
1283 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
1285 WMenu *wsmenu;
1287 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
1288 if (!wsmenu) {
1289 wwarning(_("could not create Workspace menu"));
1290 return NULL;
1293 /* callback to be called when an entry is edited */
1294 wsmenu->on_edit = onMenuEntryEdited;
1296 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
1297 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
1299 return wsmenu;
1302 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
1304 int i;
1305 long ws;
1306 char title[MAX_WORKSPACENAME_WIDTH + 1];
1307 WMenuEntry *entry;
1308 int tmp;
1310 if (!menu)
1311 return;
1313 if (menu->entry_no < scr->workspace_count + 2) {
1314 /* new workspace(s) added */
1315 i = scr->workspace_count - (menu->entry_no - 2);
1316 ws = menu->entry_no - 2;
1317 while (i > 0) {
1318 strncpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
1320 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
1321 entry->flags.indicator = 1;
1322 entry->flags.editable = 1;
1324 i--;
1325 ws++;
1327 } else if (menu->entry_no > scr->workspace_count + 2) {
1328 /* removed workspace(s) */
1329 for (i = menu->entry_no - 1; i >= scr->workspace_count + 2; i--) {
1330 wMenuRemoveItem(menu, i);
1333 wMenuRealize(menu);
1335 for (i = 0; i < scr->workspace_count; i++) {
1336 menu->entries[i + 2]->flags.indicator_on = 0;
1338 menu->entries[scr->current_workspace + 2]->flags.indicator_on = 1;
1340 /* don't let user destroy current workspace */
1341 if (scr->current_workspace == scr->workspace_count - 1) {
1342 wMenuSetEnabled(menu, 1, False);
1343 } else {
1344 wMenuSetEnabled(menu, 1, True);
1347 tmp = menu->frame->top_width + 5;
1348 /* if menu got unreachable, bring it to a visible place */
1349 if (menu->frame_x < tmp - (int)menu->frame->core->width)
1350 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
1352 wMenuPaint(menu);
1355 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
1357 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
1358 int i;
1360 make_keys();
1362 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
1363 parr = WMCreatePLArray(NULL);
1364 for (i = 0; i < scr->workspace_count; i++) {
1365 pstr = WMCreatePLString(scr->workspaces[i]->name);
1366 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
1367 WMReleasePropList(pstr);
1368 if (!wPreferences.flags.noclip) {
1369 pstr = wClipSaveWorkspaceState(scr, i);
1370 WMPutInPLDictionary(wks_state, dClip, pstr);
1371 WMReleasePropList(pstr);
1372 } else if (old_wks_state != NULL) {
1373 if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
1374 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
1375 WMPutInPLDictionary(wks_state, dClip, bar);
1379 WMAddToPLArray(parr, wks_state);
1380 WMReleasePropList(wks_state);
1382 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
1383 WMReleasePropList(parr);
1386 void wWorkspaceRestoreState(WScreen * scr)
1388 WMPropList *parr, *pstr, *wks_state, *clip_state;
1389 int i, j, wscount;
1391 make_keys();
1393 if (scr->session_state == NULL)
1394 return;
1396 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
1398 if (!parr)
1399 return;
1401 wscount = scr->workspace_count;
1402 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
1403 wks_state = WMGetFromPLArray(parr, i);
1404 if (WMIsPLDictionary(wks_state))
1405 pstr = WMGetFromPLDictionary(wks_state, dName);
1406 else
1407 pstr = wks_state;
1408 if (i >= scr->workspace_count)
1409 wWorkspaceNew(scr);
1410 if (scr->workspace_menu) {
1411 wfree(scr->workspace_menu->entries[i + 2]->text);
1412 scr->workspace_menu->entries[i + 2]->text = wstrdup(WMGetFromPLString(pstr));
1413 scr->workspace_menu->flags.realized = 0;
1415 wfree(scr->workspaces[i]->name);
1416 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
1417 if (!wPreferences.flags.noclip) {
1418 int added_omnipresent_icons = 0;
1419 clip_state = WMGetFromPLDictionary(wks_state, dClip);
1420 if (scr->workspaces[i]->clip)
1421 wDockDestroy(scr->workspaces[i]->clip);
1422 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
1423 if (i > 0)
1424 wDockHideIcons(scr->workspaces[i]->clip);
1426 /* We set the global icons here, because scr->workspaces[i]->clip
1427 * was not valid in wDockRestoreState().
1428 * There we only set icon->omnipresent to know which icons we
1429 * need to set here.
1431 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
1432 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
1433 int k;
1435 if (!aicon || !aicon->omnipresent)
1436 continue;
1437 aicon->omnipresent = 0;
1438 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
1439 continue;
1440 if (i == 0)
1441 continue;
1443 /* Move this appicon from workspace i to workspace 0 */
1444 scr->workspaces[i]->clip->icon_array[j] = NULL;
1445 scr->workspaces[i]->clip->icon_count--;
1447 added_omnipresent_icons++;
1448 /* If there are too many omnipresent appicons, we are in trouble */
1449 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
1450 <= scr->workspaces[0]->clip->max_icons);
1451 /* Find first free spot on workspace 0 */
1452 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
1453 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
1454 break;
1455 scr->workspaces[0]->clip->icon_array[k] = aicon;
1456 aicon->dock = scr->workspaces[0]->clip;
1458 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
1461 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);