Handle NULL pointer as good as possible (Coverity #50099)
[wmaker-crm.git] / src / workspace.c
blob380ffec0358c96702de48051df009f26b1e1d953
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #ifdef USE_XSHAPE
26 #include <X11/extensions/shape.h>
27 #endif
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <stdint.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <time.h>
36 #include <sys/time.h>
38 #include "WindowMaker.h"
39 #include "framewin.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "misc.h"
43 #include "menu.h"
44 #include "application.h"
45 #include "dock.h"
46 #include "actions.h"
47 #include "workspace.h"
48 #include "appicon.h"
49 #include "wmspec.h"
50 #include "xinerama.h"
51 #include "event.h"
52 #include "wsmap.h"
54 #define MC_NEW 0
55 #define MC_DESTROY_LAST 1
56 #define MC_LAST_USED 2
57 /* index of the first workspace menu entry */
58 #define MC_WORKSPACE1 3
60 #define WORKSPACE_NAME_DISPLAY_PADDING 32
62 static WMPropList *dWorkspaces = NULL;
63 static WMPropList *dClip, *dName;
65 static void make_keys(void)
67 if (dWorkspaces != NULL)
68 return;
70 dWorkspaces = WMCreatePLString("Workspaces");
71 dName = WMCreatePLString("Name");
72 dClip = WMCreatePLString("Clip");
75 void wWorkspaceMake(WScreen * scr, int count)
77 while (count > 0) {
78 wWorkspaceNew(scr);
79 count--;
83 int wWorkspaceNew(WScreen *scr)
85 WWorkspace *wspace, **list;
86 int i;
88 if (scr->workspace_count < MAX_WORKSPACES) {
89 scr->workspace_count++;
91 wspace = wmalloc(sizeof(WWorkspace));
92 wspace->name = NULL;
93 wspace->clip = NULL;
95 if (!wspace->name) {
96 static const char *new_name = NULL;
97 static size_t name_length;
99 if (new_name == NULL) {
100 new_name = _("Workspace %i");
101 name_length = strlen(new_name) + 8;
103 wspace->name = wmalloc(name_length);
104 snprintf(wspace->name, name_length, new_name, scr->workspace_count);
107 if (!wPreferences.flags.noclip)
108 wspace->clip = wDockCreate(scr, WM_CLIP, NULL);
110 list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
112 for (i = 0; i < scr->workspace_count - 1; i++)
113 list[i] = scr->workspaces[i];
115 list[i] = wspace;
116 if (scr->workspaces)
117 wfree(scr->workspaces);
119 scr->workspaces = list;
121 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
122 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
123 wNETWMUpdateDesktop(scr);
124 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
125 XFlush(dpy);
127 return scr->workspace_count - 1;
130 return -1;
133 Bool wWorkspaceDelete(WScreen * scr, int workspace)
135 WWindow *tmp;
136 WWorkspace **list;
137 int i, j;
139 if (workspace <= 0)
140 return False;
142 /* verify if workspace is in use by some window */
143 tmp = scr->focused_window;
144 while (tmp) {
145 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
146 return False;
147 tmp = tmp->prev;
150 if (!wPreferences.flags.noclip) {
151 wDockDestroy(scr->workspaces[workspace]->clip);
152 scr->workspaces[workspace]->clip = NULL;
155 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
156 j = 0;
157 for (i = 0; i < scr->workspace_count; i++) {
158 if (i != workspace) {
159 list[j++] = scr->workspaces[i];
160 } else {
161 if (scr->workspaces[i]->name)
162 wfree(scr->workspaces[i]->name);
163 if (scr->workspaces[i]->map)
164 RReleaseImage(scr->workspaces[i]->map);
165 wfree(scr->workspaces[i]);
168 wfree(scr->workspaces);
169 scr->workspaces = list;
171 scr->workspace_count--;
173 /* update menu */
174 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
175 /* clip workspace menu */
176 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
178 /* update also window menu */
179 if (scr->workspace_submenu) {
180 WMenu *menu = scr->workspace_submenu;
182 i = menu->entry_no;
183 while (i > scr->workspace_count)
184 wMenuRemoveItem(menu, --i);
185 wMenuRealize(menu);
187 /* and clip menu */
188 if (scr->clip_submenu) {
189 WMenu *menu = scr->clip_submenu;
191 i = menu->entry_no;
192 while (i > scr->workspace_count)
193 wMenuRemoveItem(menu, --i);
194 wMenuRealize(menu);
196 wNETWMUpdateDesktop(scr);
197 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
199 if (scr->current_workspace >= scr->workspace_count)
200 wWorkspaceChange(scr, scr->workspace_count - 1);
201 if (scr->last_workspace >= scr->workspace_count)
202 scr->last_workspace = 0;
204 return True;
207 typedef struct WorkspaceNameData {
208 int count;
209 RImage *back;
210 RImage *text;
211 time_t timeout;
212 } WorkspaceNameData;
214 static void hideWorkspaceName(void *data)
216 WScreen *scr = (WScreen *) data;
218 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
219 || time(NULL) > scr->workspace_name_data->timeout) {
220 XUnmapWindow(dpy, scr->workspace_name);
222 if (scr->workspace_name_data) {
223 RReleaseImage(scr->workspace_name_data->back);
224 RReleaseImage(scr->workspace_name_data->text);
225 wfree(scr->workspace_name_data);
227 scr->workspace_name_data = NULL;
229 scr->workspace_name_timer = NULL;
230 } else {
231 RImage *img = RCloneImage(scr->workspace_name_data->back);
232 Pixmap pix;
234 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
236 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
237 scr->workspace_name_data->count * 255 / 10);
239 RConvertImage(scr->rcontext, img, &pix);
241 RReleaseImage(img);
243 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
244 XClearWindow(dpy, scr->workspace_name);
245 XFreePixmap(dpy, pix);
246 XFlush(dpy);
248 scr->workspace_name_data->count--;
252 static void showWorkspaceName(WScreen * scr, int workspace)
254 WorkspaceNameData *data;
255 RXImage *ximg;
256 Pixmap text, mask;
257 int w, h;
258 int px, py;
259 char *name = scr->workspaces[workspace]->name;
260 int len = strlen(name);
261 int x, y;
262 #ifdef USE_XINERAMA
263 int head;
264 WMRect rect;
265 int xx, yy;
266 #endif
268 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2)
269 return;
271 if (scr->workspace_name_timer) {
272 WMDeleteTimerHandler(scr->workspace_name_timer);
273 XUnmapWindow(dpy, scr->workspace_name);
274 XFlush(dpy);
276 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
278 if (scr->workspace_name_data) {
279 RReleaseImage(scr->workspace_name_data->back);
280 RReleaseImage(scr->workspace_name_data->text);
281 wfree(scr->workspace_name_data);
284 data = wmalloc(sizeof(WorkspaceNameData));
285 data->back = NULL;
287 w = WMWidthOfString(scr->workspace_name_font, name, len);
288 h = WMFontHeight(scr->workspace_name_font);
290 #ifdef USE_XINERAMA
291 head = wGetHeadForPointerLocation(scr);
292 rect = wGetRectForHead(scr, head);
293 if (scr->xine_info.count) {
294 xx = rect.pos.x + (scr->xine_info.screens[head].size.width - (w + 4)) / 2;
295 yy = rect.pos.y + (scr->xine_info.screens[head].size.height - (h + 4)) / 2;
297 else {
298 xx = (scr->scr_width - (w + 4)) / 2;
299 yy = (scr->scr_height - (h + 4)) / 2;
301 #endif
303 switch (wPreferences.workspace_name_display_position) {
304 case WD_TOP:
305 #ifdef USE_XINERAMA
306 px = xx;
307 #else
308 px = (scr->scr_width - (w + 4)) / 2;
309 #endif
310 py = WORKSPACE_NAME_DISPLAY_PADDING;
311 break;
312 case WD_BOTTOM:
313 #ifdef USE_XINERAMA
314 px = xx;
315 #else
316 px = (scr->scr_width - (w + 4)) / 2;
317 #endif
318 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
319 break;
320 case WD_TOPLEFT:
321 px = WORKSPACE_NAME_DISPLAY_PADDING;
322 py = WORKSPACE_NAME_DISPLAY_PADDING;
323 break;
324 case WD_TOPRIGHT:
325 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
326 py = WORKSPACE_NAME_DISPLAY_PADDING;
327 break;
328 case WD_BOTTOMLEFT:
329 px = WORKSPACE_NAME_DISPLAY_PADDING;
330 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
331 break;
332 case WD_BOTTOMRIGHT:
333 px = scr->scr_width - (w + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
334 py = scr->scr_height - (h + 4 + WORKSPACE_NAME_DISPLAY_PADDING);
335 break;
336 case WD_CENTER:
337 default:
338 #ifdef USE_XINERAMA
339 px = xx;
340 py = yy;
341 #else
342 px = (scr->scr_width - (w + 4)) / 2;
343 py = (scr->scr_height - (h + 4)) / 2;
344 #endif
345 break;
347 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
348 XMoveWindow(dpy, scr->workspace_name, px, py);
350 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
351 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
353 /*XSetForeground(dpy, scr->mono_gc, 0);
354 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
356 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
358 for (x = 0; x <= 4; x++)
359 for (y = 0; y <= 4; y++)
360 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
362 XSetForeground(dpy, scr->mono_gc, 1);
363 XSetBackground(dpy, scr->mono_gc, 0);
365 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
367 /*XSetForeground(dpy, scr->mono_gc, 1); */
368 XSetBackground(dpy, scr->mono_gc, 1);
370 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
372 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
374 #ifdef USE_XSHAPE
375 if (w_global.xext.shape.supported)
376 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
377 #endif
378 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
379 XClearWindow(dpy, scr->workspace_name);
381 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
383 XFreePixmap(dpy, text);
384 XFreePixmap(dpy, mask);
386 if (!data->text) {
387 XMapRaised(dpy, scr->workspace_name);
388 XFlush(dpy);
390 goto erro;
393 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
394 if (!ximg)
395 goto erro;
397 XMapRaised(dpy, scr->workspace_name);
398 XFlush(dpy);
400 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
401 RDestroyXImage(scr->rcontext, ximg);
403 if (!data->back) {
404 goto erro;
407 data->count = 10;
409 /* set a timeout for the effect */
410 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
412 scr->workspace_name_data = data;
414 return;
416 erro:
417 if (scr->workspace_name_timer)
418 WMDeleteTimerHandler(scr->workspace_name_timer);
420 if (data->text)
421 RReleaseImage(data->text);
422 if (data->back)
423 RReleaseImage(data->back);
424 wfree(data);
426 scr->workspace_name_data = NULL;
428 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
429 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
432 void wWorkspaceChange(WScreen *scr, int workspace)
434 if (scr->flags.startup || scr->flags.startup2 || scr->flags.ignore_focus_events)
435 return;
437 if (workspace != scr->current_workspace)
438 wWorkspaceForceChange(scr, workspace);
441 void wWorkspaceRelativeChange(WScreen * scr, int amount)
443 int w;
445 /* While the deiconify animation is going on the window is
446 * still "flying" to its final position and we don't want to
447 * change workspace before the animation finishes, otherwise
448 * the window will land in the new workspace */
449 if (w_global.ignore_workspace_change)
450 return;
452 w = scr->current_workspace + amount;
454 if (amount < 0) {
455 if (w >= 0) {
456 wWorkspaceChange(scr, w);
457 } else if (wPreferences.ws_cycle) {
458 wWorkspaceChange(scr, scr->workspace_count + w);
460 } else if (amount > 0) {
461 if (w < scr->workspace_count) {
462 wWorkspaceChange(scr, w);
463 } else if (wPreferences.ws_advance) {
464 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
465 } else if (wPreferences.ws_cycle) {
466 wWorkspaceChange(scr, w % scr->workspace_count);
471 void wWorkspaceForceChange(WScreen * scr, int workspace)
473 WWindow *tmp, *foc = NULL, *foc2 = NULL;
475 if (workspace >= MAX_WORKSPACES || workspace < 0)
476 return;
478 if (!wPreferences.disable_workspace_pager && !w_global.process_workspacemap_event)
479 wWorkspaceMapUpdate(scr);
481 SendHelperMessage(scr, 'C', workspace + 1, NULL);
483 if (workspace > scr->workspace_count - 1)
484 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
486 wClipUpdateForWorkspaceChange(scr, workspace);
488 scr->last_workspace = scr->current_workspace;
489 scr->current_workspace = workspace;
491 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
493 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
495 if ((tmp = scr->focused_window) != NULL) {
496 WWindow **toUnmap;
497 int toUnmapSize, toUnmapCount;
499 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
500 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
501 foc = tmp;
504 toUnmapSize = 16;
505 toUnmapCount = 0;
506 toUnmap = wmalloc(toUnmapSize * sizeof(WWindow *));
508 /* foc2 = tmp; will fix annoyance with gnome panel
509 * but will create annoyance for every other application
511 while (tmp) {
512 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
513 /* unmap windows not on this workspace */
514 if ((tmp->flags.mapped || tmp->flags.shaded) &&
515 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
516 if (toUnmapCount == toUnmapSize)
518 toUnmapSize *= 2;
519 toUnmap = wrealloc(toUnmap, toUnmapSize * sizeof(WWindow *));
521 toUnmap[toUnmapCount++] = tmp;
523 /* also unmap miniwindows not on this workspace */
524 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
525 tmp->icon && !IS_OMNIPRESENT(tmp)) {
526 XUnmapWindow(dpy, tmp->icon->core->window);
527 tmp->icon->mapped = 0;
529 /* update current workspace of omnipresent windows */
530 if (IS_OMNIPRESENT(tmp)) {
531 WApplication *wapp = wApplicationOf(tmp->main_window);
533 tmp->frame->workspace = workspace;
535 if (wapp) {
536 wapp->last_workspace = workspace;
538 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
539 foc2 = tmp;
542 } else {
543 /* change selected windows' workspace */
544 if (tmp->flags.selected) {
545 wWindowChangeWorkspace(tmp, workspace);
546 if (!tmp->flags.miniaturized && !foc) {
547 foc = tmp;
549 } else {
550 if (!tmp->flags.hidden) {
551 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
552 /* remap windows that are on this workspace */
553 wWindowMap(tmp);
554 if (!foc && !WFLAGP(tmp, no_focusable)) {
555 foc = tmp;
558 /* Also map miniwindow if not omnipresent */
559 if (!wPreferences.sticky_icons &&
560 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
561 tmp->icon->mapped = 1;
562 XMapWindow(dpy, tmp->icon->core->window);
567 tmp = tmp->prev;
570 while (toUnmapCount > 0)
572 wWindowUnmap(toUnmap[--toUnmapCount]);
574 wfree(toUnmap);
576 /* Gobble up events unleashed by our mapping & unmapping.
577 * These may trigger various grab-initiated focus &
578 * crossing events. However, we don't care about them,
579 * and ignore their focus implications altogether to avoid
580 * flicker.
582 scr->flags.ignore_focus_events = 1;
583 ProcessPendingEvents();
584 scr->flags.ignore_focus_events = 0;
586 if (!foc)
587 foc = foc2;
589 if (scr->focused_window->flags.mapped && !foc) {
590 foc = scr->focused_window;
592 if (wPreferences.focus_mode == WKF_CLICK) {
593 wSetFocusTo(scr, foc);
594 } else {
595 unsigned int mask;
596 int foo;
597 Window bar, win;
598 WWindow *tmp;
600 tmp = NULL;
601 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
602 tmp = wWindowFor(win);
605 /* If there's a window under the pointer, focus it.
606 * (we ate all other focus events above, so it's
607 * certainly not focused). Otherwise focus last
608 * focused, or the root (depending on sloppiness)
610 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
611 wSetFocusTo(scr, foc);
612 } else {
613 wSetFocusTo(scr, tmp);
618 /* We need to always arrange icons when changing workspace, even if
619 * no autoarrange icons, because else the icons in different workspaces
620 * can be superposed.
621 * This can be avoided if appicons are also workspace specific.
623 if (!wPreferences.sticky_icons)
624 wArrangeIcons(scr, False);
626 if (scr->dock)
627 wAppIconPaint(scr->dock->icon_array[0]);
629 if (!wPreferences.flags.noclip && (scr->workspaces[workspace]->clip->auto_collapse ||
630 scr->workspaces[workspace]->clip->auto_raise_lower)) {
631 /* to handle enter notify. This will also */
632 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
633 XMapWindow(dpy, scr->clip_icon->icon->core->window);
635 else if (scr->clip_icon != NULL) {
636 wClipIconPaint(scr->clip_icon);
638 wScreenUpdateUsableArea(scr);
639 wNETWMUpdateDesktop(scr);
640 showWorkspaceName(scr, workspace);
642 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
644 /* XSync(dpy, False); */
647 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
649 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
652 static void lastWSCommand(WMenu *menu, WMenuEntry *entry)
654 /* Parameter not used, but tell the compiler that it is ok */
655 (void) entry;
657 wWorkspaceChange(menu->frame->screen_ptr, menu->frame->screen_ptr->last_workspace);
660 static void deleteWSCommand(WMenu *menu, WMenuEntry *entry)
662 /* Parameter not used, but tell the compiler that it is ok */
663 (void) entry;
665 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
668 static void newWSCommand(WMenu *menu, WMenuEntry *foo)
670 int ws;
672 /* Parameter not used, but tell the compiler that it is ok */
673 (void) foo;
675 ws = wWorkspaceNew(menu->frame->screen_ptr);
677 /* autochange workspace */
678 if (ws >= 0)
679 wWorkspaceChange(menu->frame->screen_ptr, ws);
682 void wWorkspaceRename(WScreen *scr, int workspace, const char *name)
684 char buf[MAX_WORKSPACENAME_WIDTH + 1];
685 char *tmp;
687 if (workspace >= scr->workspace_count)
688 return;
690 /* trim white spaces */
691 tmp = wtrimspace(name);
693 if (strlen(tmp) == 0) {
694 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
695 } else {
696 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
698 buf[MAX_WORKSPACENAME_WIDTH] = 0;
699 wfree(tmp);
701 /* update workspace */
702 wfree(scr->workspaces[workspace]->name);
703 scr->workspaces[workspace]->name = wstrdup(buf);
705 if (scr->clip_ws_menu) {
706 if (strcmp(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
707 wfree(scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text);
708 scr->clip_ws_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
709 wMenuRealize(scr->clip_ws_menu);
712 if (scr->workspace_menu) {
713 if (strcmp(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text, buf) != 0) {
714 wfree(scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text);
715 scr->workspace_menu->entries[workspace + MC_WORKSPACE1]->text = wstrdup(buf);
716 wMenuRealize(scr->workspace_menu);
720 if (scr->clip_icon)
721 wClipIconPaint(scr->clip_icon);
723 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
726 /* callback for when menu entry is edited */
727 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
729 char *tmp;
731 tmp = entry->text;
732 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
735 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
737 WMenu *wsmenu;
738 WMenuEntry *entry;
740 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
741 if (!wsmenu) {
742 wwarning(_("could not create Workspace menu"));
743 return NULL;
746 /* callback to be called when an entry is edited */
747 wsmenu->on_edit = onMenuEntryEdited;
749 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
750 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
752 entry = wMenuAddCallback(wsmenu, _("Last Used"), lastWSCommand, NULL);
753 entry->rtext = GetShortcutKey(wKeyBindings[WKBD_LASTWORKSPACE]);
755 return wsmenu;
758 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
760 int i;
761 long ws;
762 char title[MAX_WORKSPACENAME_WIDTH + 1];
763 WMenuEntry *entry;
764 int tmp;
766 if (!menu)
767 return;
769 if (menu->entry_no < scr->workspace_count + MC_WORKSPACE1) {
770 /* new workspace(s) added */
771 i = scr->workspace_count - (menu->entry_no - MC_WORKSPACE1);
772 ws = menu->entry_no - MC_WORKSPACE1;
773 while (i > 0) {
774 wstrlcpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
776 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
777 entry->flags.indicator = 1;
778 entry->flags.editable = 1;
780 i--;
781 ws++;
783 } else if (menu->entry_no > scr->workspace_count + MC_WORKSPACE1) {
784 /* removed workspace(s) */
785 for (i = menu->entry_no - 1; i >= scr->workspace_count + MC_WORKSPACE1; i--)
786 wMenuRemoveItem(menu, i);
789 for (i = 0; i < scr->workspace_count; i++) {
790 /* workspace shortcut labels */
791 if (i / 10 == scr->current_workspace / 10)
792 menu->entries[i + MC_WORKSPACE1]->rtext = GetShortcutKey(wKeyBindings[WKBD_WORKSPACE1 + (i % 10)]);
793 else
794 menu->entries[i + MC_WORKSPACE1]->rtext = NULL;
796 menu->entries[i + MC_WORKSPACE1]->flags.indicator_on = 0;
798 menu->entries[scr->current_workspace + MC_WORKSPACE1]->flags.indicator_on = 1;
799 wMenuRealize(menu);
801 /* don't let user destroy current workspace */
802 if (scr->current_workspace == scr->workspace_count - 1)
803 wMenuSetEnabled(menu, MC_DESTROY_LAST, False);
804 else
805 wMenuSetEnabled(menu, MC_DESTROY_LAST, True);
807 /* back to last workspace */
808 if (scr->workspace_count && scr->last_workspace != scr->current_workspace)
809 wMenuSetEnabled(menu, MC_LAST_USED, True);
810 else
811 wMenuSetEnabled(menu, MC_LAST_USED, False);
813 tmp = menu->frame->top_width + 5;
814 /* if menu got unreachable, bring it to a visible place */
815 if (menu->frame_x < tmp - (int)menu->frame->core->width)
816 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
818 wMenuPaint(menu);
821 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
823 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
824 int i;
826 make_keys();
828 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
829 parr = WMCreatePLArray(NULL);
830 for (i = 0; i < scr->workspace_count; i++) {
831 pstr = WMCreatePLString(scr->workspaces[i]->name);
832 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
833 WMReleasePropList(pstr);
834 if (!wPreferences.flags.noclip) {
835 pstr = wClipSaveWorkspaceState(scr, i);
836 WMPutInPLDictionary(wks_state, dClip, pstr);
837 WMReleasePropList(pstr);
838 } else if (old_wks_state != NULL) {
839 if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
840 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
841 WMPutInPLDictionary(wks_state, dClip, bar);
845 WMAddToPLArray(parr, wks_state);
846 WMReleasePropList(wks_state);
848 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
849 WMReleasePropList(parr);
852 void wWorkspaceRestoreState(WScreen *scr)
854 WMPropList *parr, *pstr, *wks_state, *clip_state;
855 int i, j;
857 make_keys();
859 if (scr->session_state == NULL)
860 return;
862 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
864 if (!parr)
865 return;
867 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
868 wks_state = WMGetFromPLArray(parr, i);
869 if (WMIsPLDictionary(wks_state))
870 pstr = WMGetFromPLDictionary(wks_state, dName);
871 else
872 pstr = wks_state;
874 if (i >= scr->workspace_count)
875 wWorkspaceNew(scr);
877 if (scr->workspace_menu) {
878 wfree(scr->workspace_menu->entries[i + MC_WORKSPACE1]->text);
879 scr->workspace_menu->entries[i + MC_WORKSPACE1]->text = wstrdup(WMGetFromPLString(pstr));
880 scr->workspace_menu->flags.realized = 0;
883 wfree(scr->workspaces[i]->name);
884 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
885 if (!wPreferences.flags.noclip) {
886 int added_omnipresent_icons = 0;
888 clip_state = WMGetFromPLDictionary(wks_state, dClip);
889 if (scr->workspaces[i]->clip)
890 wDockDestroy(scr->workspaces[i]->clip);
892 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
893 if (i > 0)
894 wDockHideIcons(scr->workspaces[i]->clip);
896 /* We set the global icons here, because scr->workspaces[i]->clip
897 * was not valid in wDockRestoreState().
898 * There we only set icon->omnipresent to know which icons we
899 * need to set here.
901 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
902 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
903 int k;
905 if (!aicon || !aicon->omnipresent)
906 continue;
907 aicon->omnipresent = 0;
908 if (wClipMakeIconOmnipresent(aicon, True) != WO_SUCCESS)
909 continue;
910 if (i == 0)
911 continue;
913 /* Move this appicon from workspace i to workspace 0 */
914 scr->workspaces[i]->clip->icon_array[j] = NULL;
915 scr->workspaces[i]->clip->icon_count--;
917 added_omnipresent_icons++;
918 /* If there are too many omnipresent appicons, we are in trouble */
919 assert(scr->workspaces[0]->clip->icon_count + added_omnipresent_icons
920 <= scr->workspaces[0]->clip->max_icons);
921 /* Find first free spot on workspace 0 */
922 for (k = 0; k < scr->workspaces[0]->clip->max_icons; k++)
923 if (scr->workspaces[0]->clip->icon_array[k] == NULL)
924 break;
925 scr->workspaces[0]->clip->icon_array[k] = aicon;
926 aicon->dock = scr->workspaces[0]->clip;
928 scr->workspaces[0]->clip->icon_count += added_omnipresent_icons;
931 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);
935 /* Returns the workspace number for a given workspace name */
936 int wGetWorkspaceNumber(WScreen *scr, const char *value)
938 int w, i;
940 if (sscanf(value, "%i", &w) != 1) {
941 w = -1;
942 for (i = 0; i < scr->workspace_count; i++) {
943 if (strcmp(scr->workspaces[i]->name, value) == 0) {
944 w = i;
945 break;
948 } else {
949 w--;
952 return w;