Fix buffer overflows in shortcut and workspace name handling
[wmaker-crm.git] / src / workspace.c
blob4649bbe1d77d88cb15f56b807fa68db0c6174f65
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
59 extern WPreferences wPreferences;
60 extern XContext wWinContext;
61 extern XContext wVEdgeContext;
63 extern void ProcessPendingEvents();
65 static WMPropList *dWorkspaces=NULL;
66 static WMPropList *dClip, *dName;
70 static void
71 make_keys()
73 if (dWorkspaces!=NULL)
74 return;
76 dWorkspaces = WMCreatePLString("Workspaces");
77 dName = WMCreatePLString("Name");
78 dClip = WMCreatePLString("Clip");
82 void
83 wWorkspaceMake(WScreen *scr, int count)
85 while (count>0) {
86 wWorkspaceNew(scr);
87 count--;
92 int
93 wWorkspaceNew(WScreen *scr)
95 WWorkspace *wspace, **list;
96 int i;
98 if (scr->workspace_count < MAX_WORKSPACES) {
99 scr->workspace_count++;
101 wspace = wmalloc(sizeof(WWorkspace));
102 wspace->name = NULL;
104 if (!wspace->name) {
105 wspace->name = wmalloc(strlen(_("Workspace %i"))+8);
106 sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
110 if (!wPreferences.flags.noclip) {
111 wspace->clip = wDockCreate(scr, WM_CLIP);
112 } else
113 wspace->clip = NULL;
115 list = wmalloc(sizeof(WWorkspace*)*scr->workspace_count);
117 for (i=0; i<scr->workspace_count-1; i++) {
118 list[i] = scr->workspaces[i];
120 list[i] = wspace;
121 if (scr->workspaces)
122 wfree(scr->workspaces);
123 scr->workspaces = list;
125 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
126 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
127 #ifdef VIRTUAL_DESKTOP
128 wspace->view_x = wspace->view_y = 0;
129 wspace->height = scr->scr_height;
130 wspace->width = scr->scr_width;
131 #endif
132 #ifdef NETWM_HINTS
133 wNETWMUpdateDesktop(scr);
134 #endif
136 WMPostNotificationName(WMNWorkspaceCreated, scr,
137 (void*)(uintptr_t)(scr->workspace_count-1));
138 XFlush(dpy);
140 return scr->workspace_count-1;
143 return -1;
147 Bool
148 wWorkspaceDelete(WScreen *scr, int workspace)
150 WWindow *tmp;
151 WWorkspace **list;
152 int i, j;
154 if (workspace<=0)
155 return False;
157 /* verify if workspace is in use by some window */
158 tmp = scr->focused_window;
159 while (tmp) {
160 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace==workspace)
161 return False;
162 tmp = tmp->prev;
165 if (!wPreferences.flags.noclip) {
166 wDockDestroy(scr->workspaces[workspace]->clip);
167 scr->workspaces[workspace]->clip = NULL;
170 list = wmalloc(sizeof(WWorkspace*)*(scr->workspace_count-1));
171 j = 0;
172 for (i=0; i<scr->workspace_count; i++) {
173 if (i!=workspace) {
174 list[j++] = scr->workspaces[i];
175 } else {
176 if (scr->workspaces[i]->name)
177 wfree(scr->workspaces[i]->name);
178 wfree(scr->workspaces[i]);
181 wfree(scr->workspaces);
182 scr->workspaces = list;
184 scr->workspace_count--;
187 /* update menu */
188 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
189 /* clip workspace menu */
190 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
192 /* update also window menu */
193 if (scr->workspace_submenu) {
194 WMenu *menu = scr->workspace_submenu;
196 i = menu->entry_no;
197 while (i>scr->workspace_count)
198 wMenuRemoveItem(menu, --i);
199 wMenuRealize(menu);
201 /* and clip menu */
202 if (scr->clip_submenu) {
203 WMenu *menu = scr->clip_submenu;
205 i = menu->entry_no;
206 while (i>scr->workspace_count)
207 wMenuRemoveItem(menu, --i);
208 wMenuRealize(menu);
211 #ifdef NETWM_HINTS
212 wNETWMUpdateDesktop(scr);
213 #endif
215 WMPostNotificationName(WMNWorkspaceDestroyed, scr,
216 (void*)(uintptr_t)(scr->workspace_count-1));
218 if (scr->current_workspace >= scr->workspace_count)
219 wWorkspaceChange(scr, scr->workspace_count-1);
221 return True;
225 typedef struct WorkspaceNameData {
226 int count;
227 RImage *back;
228 RImage *text;
229 time_t timeout;
230 } WorkspaceNameData;
233 static void
234 hideWorkspaceName(void *data)
236 WScreen *scr = (WScreen*)data;
238 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
239 || time(NULL) > scr->workspace_name_data->timeout) {
240 XUnmapWindow(dpy, scr->workspace_name);
242 if (scr->workspace_name_data) {
243 RReleaseImage(scr->workspace_name_data->back);
244 RReleaseImage(scr->workspace_name_data->text);
245 wfree(scr->workspace_name_data);
247 scr->workspace_name_data = NULL;
249 scr->workspace_name_timer = NULL;
250 } else {
251 RImage *img = RCloneImage(scr->workspace_name_data->back);
252 Pixmap pix;
254 scr->workspace_name_timer =
255 WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
257 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
258 scr->workspace_name_data->count*255/10);
260 RConvertImage(scr->rcontext, img, &pix);
262 RReleaseImage(img);
264 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
265 XClearWindow(dpy, scr->workspace_name);
266 XFreePixmap(dpy, pix);
267 XFlush(dpy);
269 scr->workspace_name_data->count--;
274 static void
275 showWorkspaceName(WScreen *scr, int workspace)
277 WorkspaceNameData *data;
278 RXImage *ximg;
279 Pixmap text, mask;
280 int w, h;
281 int px, py;
282 char *name = scr->workspaces[workspace]->name;
283 int len = strlen(name);
284 int x, y;
286 if (wPreferences.workspace_name_display_position == WD_NONE ||
287 scr->workspace_count < 2) {
288 return;
291 if (scr->workspace_name_timer) {
292 WMDeleteTimerHandler(scr->workspace_name_timer);
293 XUnmapWindow(dpy, scr->workspace_name);
294 XFlush(dpy);
296 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY,
297 hideWorkspaceName, scr);
299 if (scr->workspace_name_data) {
300 RReleaseImage(scr->workspace_name_data->back);
301 RReleaseImage(scr->workspace_name_data->text);
302 wfree(scr->workspace_name_data);
305 data = wmalloc(sizeof(WorkspaceNameData));
306 data->back = NULL;
308 w = WMWidthOfString(scr->workspace_name_font, name, len);
309 h = WMFontHeight(scr->workspace_name_font);
311 switch (wPreferences.workspace_name_display_position) {
312 case WD_TOP:
313 px = (scr->scr_width - (w+4))/2;
314 py = 0;
315 break;
316 case WD_BOTTOM:
317 px = (scr->scr_width - (w+4))/2;
318 py = scr->scr_height - (h+4);
319 break;
320 case WD_TOPLEFT:
321 px = 0;
322 py = 0;
323 break;
324 case WD_TOPRIGHT:
325 px = scr->scr_width - (w+4);
326 py = 0;
327 break;
328 case WD_BOTTOMLEFT:
329 px = 0;
330 py = scr->scr_height - (h+4);
331 break;
332 case WD_BOTTOMRIGHT:
333 px = scr->scr_width - (w+4);
334 py = scr->scr_height - (h+4);
335 break;
336 case WD_CENTER:
337 default:
338 px = (scr->scr_width - (w+4))/2;
339 py = (scr->scr_height - (h+4))/2;
340 break;
342 XResizeWindow(dpy, scr->workspace_name, w+4, h+4);
343 XMoveWindow(dpy, scr->workspace_name, px, py);
345 text = XCreatePixmap(dpy, scr->w_win, w+4, h+4, scr->w_depth);
346 mask = XCreatePixmap(dpy, scr->w_win, w+4, h+4, 1);
348 /*XSetForeground(dpy, scr->mono_gc, 0);
349 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4);*/
351 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w+4, h+4);
353 for (x = 0; x <= 4; x++) {
354 for (y = 0; y <= 4; y++) {
355 WMDrawString(scr->wmscreen, text, scr->white,
356 scr->workspace_name_font, x, y, name, len);
360 XSetForeground(dpy, scr->mono_gc, 1);
361 XSetBackground(dpy, scr->mono_gc, 0);
363 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w+4, h+4, 0, 0, 1<<(scr->w_depth-1));
365 /*XSetForeground(dpy, scr->mono_gc, 1);*/
366 XSetBackground(dpy, scr->mono_gc, 1);
368 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w+4, h+4);
370 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font,
371 2, 2, name, len);
373 #ifdef SHAPE
374 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask,
375 ShapeSet);
376 #endif
377 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
378 XClearWindow(dpy, scr->workspace_name);
380 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
382 XFreePixmap(dpy, text);
383 XFreePixmap(dpy, mask);
385 if (!data->text) {
386 XMapRaised(dpy, scr->workspace_name);
387 XFlush(dpy);
389 goto erro;
392 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py,
393 data->text->width, data->text->height);
395 if (!ximg || !ximg->image) {
396 goto erro;
399 XMapRaised(dpy, scr->workspace_name);
400 XFlush(dpy);
402 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
403 RDestroyXImage(scr->rcontext, ximg);
405 if (!data->back) {
406 goto erro;
409 data->count = 10;
411 /* set a timeout for the effect */
412 data->timeout = time(NULL) + 2 +
413 (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY*data->count)/1000;
415 scr->workspace_name_data = data;
418 return;
420 erro:
421 if (scr->workspace_name_timer)
422 WMDeleteTimerHandler(scr->workspace_name_timer);
424 if (data->text)
425 RReleaseImage(data->text);
426 if (data->back)
427 RReleaseImage(data->back);
428 wfree(data);
430 scr->workspace_name_data = NULL;
432 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
433 10*WORKSPACE_NAME_FADE_DELAY,
434 hideWorkspaceName, scr);
438 void
439 wWorkspaceChange(WScreen *scr, int workspace)
441 if (scr->flags.startup || scr->flags.startup2) {
442 return;
445 if (workspace != scr->current_workspace) {
446 wWorkspaceForceChange(scr, workspace);
447 } /*else {
448 showWorkspaceName(scr, workspace);
453 void
454 wWorkspaceRelativeChange(WScreen *scr, int amount)
456 int w;
458 w = scr->current_workspace + amount;
460 if (amount < 0) {
461 if (w >= 0) {
462 wWorkspaceChange(scr, w);
463 } else if (wPreferences.ws_cycle) {
464 wWorkspaceChange(scr, scr->workspace_count + w);
466 } else if (amount > 0) {
467 if (w < scr->workspace_count) {
468 wWorkspaceChange(scr, w);
469 } else if (wPreferences.ws_advance) {
470 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES-1));
471 } else if (wPreferences.ws_cycle) {
472 wWorkspaceChange(scr, w % scr->workspace_count);
478 void
479 wWorkspaceForceChange(WScreen *scr, int workspace)
481 WWindow *tmp, *foc=NULL, *foc2=NULL;
483 if (workspace >= MAX_WORKSPACES || workspace < 0)
484 return;
486 SendHelperMessage(scr, 'C', workspace+1, NULL);
488 if (workspace > scr->workspace_count-1) {
489 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
492 wClipUpdateForWorkspaceChange(scr, workspace);
494 scr->current_workspace = workspace;
496 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
498 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
500 if ((tmp = scr->focused_window)!= NULL) {
501 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
502 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
503 foc = tmp;
506 /* foc2 = tmp; will fix annoyance with gnome panel
507 * but will create annoyance for every other application
509 while (tmp) {
510 if (tmp->frame->workspace!=workspace && !tmp->flags.selected) {
511 /* unmap windows not on this workspace */
512 if ((tmp->flags.mapped||tmp->flags.shaded) &&
513 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
514 wWindowUnmap(tmp);
516 /* also unmap miniwindows not on this workspace */
517 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
518 tmp->icon && !IS_OMNIPRESENT(tmp)) {
519 XUnmapWindow(dpy, tmp->icon->core->window);
520 tmp->icon->mapped = 0;
522 /* update current workspace of omnipresent windows */
523 if (IS_OMNIPRESENT(tmp)) {
524 WApplication *wapp = wApplicationOf(tmp->main_window);
526 tmp->frame->workspace = workspace;
528 if (wapp) {
529 wapp->last_workspace = workspace;
531 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
532 foc2 = tmp;
535 } else {
536 /* change selected windows' workspace */
537 if (tmp->flags.selected) {
538 wWindowChangeWorkspace(tmp, workspace);
539 if (!tmp->flags.miniaturized && !foc) {
540 foc = tmp;
542 } else {
543 if (!tmp->flags.hidden) {
544 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
545 /* remap windows that are on this workspace */
546 wWindowMap(tmp);
547 if (!foc && !WFLAGP(tmp, no_focusable)) {
548 foc = tmp;
551 /* Also map miniwindow if not omnipresent */
552 if (!wPreferences.sticky_icons &&
553 tmp->flags.miniaturized &&
554 !IS_OMNIPRESENT(tmp) && tmp->icon) {
555 tmp->icon->mapped = 1;
556 XMapWindow(dpy, tmp->icon->core->window);
561 tmp = tmp->prev;
564 /* Gobble up events unleashed by our mapping & unmapping.
565 * These may trigger various grab-initiated focus &
566 * crossing events. However, we don't care about them,
567 * and ignore their focus implications altogether to avoid
568 * flicker.
570 scr->flags.ignore_focus_events = 1;
571 ProcessPendingEvents();
572 scr->flags.ignore_focus_events = 0;
574 if (!foc)
575 foc = foc2;
577 if (scr->focused_window->flags.mapped && !foc) {
578 foc = scr->focused_window;
580 if (wPreferences.focus_mode == WKF_CLICK) {
581 wSetFocusTo(scr, foc);
582 } else {
583 unsigned int mask;
584 int foo;
585 Window bar, win;
586 WWindow *tmp;
588 tmp = NULL;
589 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
590 &foo, &foo, &foo, &foo, &mask)) {
591 tmp = wWindowFor(win);
594 /* If there's a window under the pointer, focus it.
595 * (we ate all other focus events above, so it's
596 * certainly not focused). Otherwise focus last
597 * focused, or the root (depending on sloppiness)
599 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
600 wSetFocusTo(scr, foc);
601 } else {
602 wSetFocusTo(scr, tmp);
607 /* We need to always arrange icons when changing workspace, even if
608 * no autoarrange icons, because else the icons in different workspaces
609 * can be superposed.
610 * This can be avoided if appicons are also workspace specific.
612 if (!wPreferences.sticky_icons)
613 wArrangeIcons(scr, False);
615 if (scr->dock)
616 wAppIconPaint(scr->dock->icon_array[0]);
618 if (scr->clip_icon) {
619 if (scr->workspaces[workspace]->clip->auto_collapse ||
620 scr->workspaces[workspace]->clip->auto_raise_lower) {
621 /* to handle enter notify. This will also */
622 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
623 XMapWindow(dpy, scr->clip_icon->icon->core->window);
624 } else {
625 wClipIconPaint(scr->clip_icon);
629 #ifdef NETWM_HINTS
630 wScreenUpdateUsableArea(scr);
631 wNETWMUpdateDesktop(scr);
632 #endif
634 showWorkspaceName(scr, workspace);
636 WMPostNotificationName(WMNWorkspaceChanged, scr, (void*)(uintptr_t)workspace);
638 /* XSync(dpy, False); */
642 #ifdef VIRTUAL_DESKTOP
644 /* TODO:
646 * 1) Allow border around each window so the scrolling
647 * won't just stop at the border.
648 * 2) Make pager.
652 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
653 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
654 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
655 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
656 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
657 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
659 #ifndef HAS_BORDER
660 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
661 #endif
663 #ifndef IS_VSTUCK
664 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
665 #endif
667 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
669 static Bool cmp_gez(int i) { return (i >= 0); }
671 static Bool cmp_gz(int i) { return (i > 0); }
674 static WMPoint
675 getClosestEdge(WScreen * scr, WMPoint direction, Bool (*cmp)(int))
677 WMPoint closest = wmkpoint(0, 0);
678 int closest_len = INT_MAX;
679 WWindow * wwin;
681 for (wwin=scr->focused_window; wwin; wwin=wwin->prev) {
682 if (wwin->frame->workspace == scr->current_workspace) {
683 if (!wwin->flags.miniaturized &&
684 !IS_VSTUCK(wwin) &&
685 !wwin->flags.hidden) {
686 int border = 2*HAS_BORDER(wwin);
687 int len;
688 int x1,x2,y1,y2;
689 int head = wGetHeadForWindow(wwin);
690 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
691 WMPoint p;
693 x1 = wwin->frame_x - area.x1;
694 y1 = wwin->frame_y - area.y1;
695 x2 = wwin->frame_x + wwin->frame->core->width + border - area.x2;
696 y2 = wwin->frame_y + wwin->frame->core->height + border - area.y2;
698 p = wmkpoint(x1,y1);
699 len = vec_dot(direction, p);
700 updateMinimum(len, p, closest_len, closest);
702 p = wmkpoint(x1,y2);
703 len = vec_dot(direction, p);
704 updateMinimum(len, p, closest_len, closest);
706 p = wmkpoint(x2,y1);
707 len = vec_dot(direction, p);
708 updateMinimum(len, p, closest_len, closest);
710 p = wmkpoint(x2,y2);
711 len = vec_dot(direction, p);
712 updateMinimum(len, p, closest_len, closest);
717 return closest;
721 static void
722 getViewPosition(WScreen *scr, int workspace, int *x, int *y)
724 *x = scr->workspaces[workspace]->view_x;
725 *y = scr->workspaces[workspace]->view_y;
729 void
730 wWorkspaceKeyboardMoveDesktop(WScreen *scr, WMPoint direction)
732 int x, y;
733 WMPoint edge = getClosestEdge(scr, direction, cmp_gz);
734 int len = vec_dot(edge, direction);
735 WMPoint step = vec_scale(direction, len);
736 getViewPosition(scr, scr->current_workspace, &x, &y);
737 wWorkspaceSetViewport(scr, scr->current_workspace, x+step.x, y+step.y);
741 extern Cursor wCursor[WCUR_LAST];
744 static void
745 vdMouseMoveDesktop(XEvent *event, WMPoint direction)
747 static int lock = False;
748 if (lock) return;
749 lock = True;
751 Bool done = False;
752 Bool moved = True;
753 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
754 WMPoint old_pos = wmkpoint(event->xcrossing.x_root, event->xcrossing.y_root);
755 WMPoint step;
756 int x, y;
757 int resisted = 0;
759 if (XGrabPointer(dpy, event->xcrossing.window, False,
760 PointerMotionMask, GrabModeAsync, GrabModeAsync,
761 scr->root_win, wCursor[WCUR_EMPTY],
762 CurrentTime) != GrabSuccess) {
764 /* if the grab fails, do it the old fashioned way */
765 step = vec_scale2(direction, wPreferences.vedge_hscrollspeed,
766 wPreferences.vedge_vscrollspeed);
767 getViewPosition(scr, scr->current_workspace, &x, &y);
768 if (wWorkspaceSetViewport(scr, scr->current_workspace,
769 x+step.x, y+step.y)) {
770 step = vec_scale(direction, 2);
771 XWarpPointer(dpy, None, scr->root_win, 0,0,0,0,
772 event->xcrossing.x_root - step.x,
773 event->xcrossing.y_root - step.y);
775 goto exit;
777 XSync(dpy, True);
779 if (old_pos.x < 0)
780 old_pos.x = 0;
781 if (old_pos.y < 0)
782 old_pos.y = 0;
783 if (old_pos.x > scr->scr_width)
784 old_pos.x = scr->scr_width;
785 if (old_pos.y > scr->scr_height)
786 old_pos.y = scr->scr_height;
788 while (!done) {
789 XEvent ev;
790 if (moved) {
791 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0,
792 scr->scr_width/2, scr->scr_height/2);
793 moved = False;
795 WMMaskEvent(dpy, PointerMotionMask, &ev);
797 switch (ev.type) {
798 case MotionNotify:
800 int step_len;
801 step = wmkpoint(ev.xmotion.x_root-scr->scr_width/2,
802 ev.xmotion.y_root-scr->scr_height/2);
803 step_len = vec_dot(step, direction);
804 if (step_len < 0) {
805 done = True;
806 break;
809 if (step_len > 0) {
810 Bool do_move = True;
811 int resist = wPreferences.vedge_resistance;
812 WMPoint closest;
813 int closest_len = INT_MAX;
814 if (resist) {
815 closest = getClosestEdge(scr, direction, cmp_gez);
816 closest_len = vec_dot(direction, closest);
819 if (!closest_len) {
820 resisted += step_len;
821 do_move = resisted >= resist;
822 if (do_move) {
823 closest_len = INT_MAX;
824 step_len = resisted - resist;
825 resisted = 0;
828 if (do_move) {
829 if (closest_len <= wPreferences.vedge_attraction) {
830 step = vec_scale(direction, closest_len);
831 } else {
832 step = vec_scale(direction, step_len);
835 getViewPosition(scr, scr->current_workspace, &x, &y);
836 wWorkspaceSetViewport(scr, scr->current_workspace,
837 x+step.x, y+step.y);
838 moved = True;
842 break;
846 step = vec_add(old_pos, vec_scale(direction, -1));
847 XWarpPointer(dpy, None, scr->root_win, 0,0,0,0, step.x, step.y);
848 XUngrabPointer(dpy, CurrentTime);
850 exit:
851 lock = False;
855 static void
856 vdHandleEnter_u(XEvent *event) {
857 vdMouseMoveDesktop(event, VEC_UP);
861 static void
862 vdHandleEnter_d(XEvent *event) {
863 vdMouseMoveDesktop(event, VEC_DOWN);
867 static void
868 vdHandleEnter_l(XEvent *event) {
869 vdMouseMoveDesktop(event, VEC_LEFT);
873 static void
874 vdHandleEnter_r(XEvent *event) {
875 vdMouseMoveDesktop(event, VEC_RIGHT);
879 #define LEFT_EDGE 0x01
880 #define RIGHT_EDGE 0x02
881 #define TOP_EDGE 0x04
882 #define BOTTOM_EDGE 0x08
883 #define ALL_EDGES 0x0F
885 static void
886 createEdges(WScreen *scr)
888 if (!scr->virtual_edges) {
889 int i, j, w;
890 int vmask;
891 XSetWindowAttributes attribs;
893 int heads = wXineramaHeads(scr);
894 int *hasEdges = (int*)wmalloc(sizeof(int)*heads);
896 int thickness = 1;
897 int nr_edges = 0;
898 int max_edges = 4*heads;
899 int head;
900 Window *edges = (Window *)wmalloc(sizeof(Window)*max_edges);
902 for (i=0; i<heads; ++i)
903 hasEdges[i] = ALL_EDGES;
904 for (i=0; i<heads; ++i) {
905 WMRect i_rect = wGetRectForHead(scr, i);
906 for (j=i+1; j<heads; ++j) {
907 WMRect j_rect = wGetRectForHead(scr, j);
909 int vlen = (WMIN(i_rect.pos.y + i_rect.size.height,
910 j_rect.pos.y + j_rect.size.height) -
911 WMAX(i_rect.pos.y, j_rect.pos.y));
913 int hlen = (WMIN(i_rect.pos.x + i_rect.size.width,
914 j_rect.pos.x + j_rect.size.width) -
915 WMAX( i_rect.pos.x, j_rect.pos.x));
917 if (vlen > 0 && hlen == 0) { /* horz alignment, vert edges touch */
918 if (i_rect.pos.x < j_rect.pos.x) { /* i left of j */
919 hasEdges[i] &= ~RIGHT_EDGE;
920 hasEdges[j] &= ~LEFT_EDGE;
921 } else { /* j left of i */
922 hasEdges[j] &= ~RIGHT_EDGE;
923 hasEdges[i] &= ~LEFT_EDGE;
925 } else if (vlen == 0 && hlen > 0) { /* vert alignment, horz edges touch */
926 if (i_rect.pos.y < j_rect.pos.y) { /* i top of j */
927 hasEdges[i] &= ~BOTTOM_EDGE;
928 hasEdges[j] &= ~TOP_EDGE;
929 } else { /* j top of i */
930 hasEdges[j] &= ~BOTTOM_EDGE;
931 hasEdges[i] &= ~TOP_EDGE;
937 for (w = 0; w < scr->workspace_count; w++) {
938 /* puts("reset workspace"); */
939 wWorkspaceSetViewport(scr, w, 0, 0);
942 vmask = CWEventMask|CWOverrideRedirect;
943 attribs.event_mask = (EnterWindowMask | LeaveWindowMask | VisibilityChangeMask);
944 attribs.override_redirect = True;
946 for (head=0; head<wXineramaHeads(scr); ++head) {
947 WMRect rect = wGetRectForHead(scr, head);
949 if (hasEdges[head] & TOP_EDGE) {
950 edges[nr_edges] =
951 XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
952 rect.size.width, thickness, 0,
953 CopyFromParent, InputOnly, CopyFromParent,
954 vmask, &attribs);
955 XSaveContext(dpy, edges[nr_edges], wVEdgeContext,
956 (XPointer)vdHandleEnter_u);
957 ++nr_edges;
960 if (hasEdges[head] & BOTTOM_EDGE) {
961 edges[nr_edges] =
962 XCreateWindow(dpy, scr->root_win, rect.pos.x,
963 rect.pos.y+rect.size.height-thickness,
964 rect.size.width, thickness, 0,
965 CopyFromParent, InputOnly, CopyFromParent,
966 vmask, &attribs);
967 XSaveContext(dpy, edges[nr_edges], wVEdgeContext,
968 (XPointer)vdHandleEnter_d);
969 ++nr_edges;
972 if (hasEdges[head] & LEFT_EDGE) {
973 edges[nr_edges] =
974 XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
975 thickness, rect.pos.y+rect.size.height, 0,
976 CopyFromParent, InputOnly, CopyFromParent,
977 vmask, &attribs);
978 XSaveContext(dpy, edges[nr_edges], wVEdgeContext,
979 (XPointer)vdHandleEnter_l);
980 ++nr_edges;
983 if (hasEdges[head] & RIGHT_EDGE) {
984 edges[nr_edges] =
985 XCreateWindow(dpy, scr->root_win,
986 rect.pos.x + rect.size.width - thickness, rect.pos.y,
987 thickness, rect.size.height, 0,
988 CopyFromParent, InputOnly, CopyFromParent, vmask,
989 &attribs);
990 XSaveContext(dpy, edges[nr_edges], wVEdgeContext,
991 (XPointer)vdHandleEnter_r);
992 ++nr_edges;
996 scr->virtual_nr_edges = nr_edges;
997 scr->virtual_edges = edges;
999 for (i=0; i<scr->virtual_nr_edges; ++i) {
1000 XMapWindow(dpy, scr->virtual_edges[i]);
1002 wWorkspaceRaiseEdge(scr);
1004 wfree(hasEdges);
1009 static void
1010 destroyEdges(WScreen *scr)
1012 if (scr->virtual_edges) {
1013 int i;
1015 for (i=0; i<scr->virtual_nr_edges; ++i) {
1016 XDeleteContext(dpy, scr->virtual_edges[i], wVEdgeContext);
1017 XUnmapWindow(dpy, scr->virtual_edges[i]);
1018 XDestroyWindow(dpy, scr->virtual_edges[i]);
1021 wfree(scr->virtual_edges);
1022 scr->virtual_edges = NULL;
1023 scr->virtual_nr_edges = 0;
1028 void
1029 wWorkspaceUpdateEdge(WScreen *scr)
1031 if (wPreferences.vdesk_enable) {
1032 createEdges(scr);
1033 } else {
1034 destroyEdges(scr);
1039 void
1040 wWorkspaceRaiseEdge(WScreen *scr)
1042 static int toggle = 0;
1043 int i;
1045 if (!scr->virtual_edges)
1046 return;
1048 if (toggle) {
1049 for (i=0; i<scr->virtual_nr_edges; ++i) {
1050 XRaiseWindow(dpy, scr->virtual_edges[i]);
1052 } else {
1053 for (i=scr->virtual_nr_edges-1; i>=0; --i) {
1054 XRaiseWindow(dpy, scr->virtual_edges[i]);
1058 toggle ^= 1;
1062 void
1063 wWorkspaceLowerEdge(WScreen *scr)
1065 int i;
1066 for (i=0; i<scr->virtual_nr_edges; ++i) {
1067 XLowerWindow(dpy, scr->virtual_edges[i]);
1072 void
1073 wWorkspaceResizeViewport(WScreen *scr, int workspace)
1075 int x, y;
1076 getViewPosition(scr, scr->current_workspace, &x, &y);
1077 wWorkspaceSetViewport(scr, scr->current_workspace, x, y);
1081 void
1082 updateWorkspaceGeometry(WScreen *scr, int workspace, int *view_x, int *view_y)
1084 int most_left, most_right, most_top, most_bottom;
1085 WWindow *wwin;
1087 int heads = wXineramaHeads(scr);
1088 typedef int strut_t[4];
1089 strut_t * strut = (strut_t*)wmalloc(heads*sizeof(strut_t));
1090 int head, i;
1092 for (head=0; head<heads; ++head) {
1093 WMRect rect = wGetRectForHead(scr, head);
1094 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1095 strut[head][0] = area.x1 - rect.pos.x;
1096 strut[head][1] = rect.pos.x + rect.size.width - area.x2;
1097 strut[head][2] = area.y1 - rect.pos.y;
1098 strut[head][3] = rect.pos.y + rect.size.height - area.y2;
1101 /* adjust workspace layout */
1102 wwin = scr->focused_window;
1103 most_right = 0;
1104 most_bottom = 0;
1105 most_left = scr->scr_width;
1106 most_top = scr->scr_height;
1107 for(;wwin; wwin = wwin->prev) {
1108 if (wwin->frame->workspace == workspace) {
1109 if (!wwin->flags.miniaturized && !IS_VSTUCK(wwin) &&
1110 !wwin->flags.hidden) {
1112 head = wGetHeadForWindow(wwin);
1114 i = wwin->frame_x - strut[head][0];
1115 if (i < most_left) /* record positions, should this be cached? */
1116 most_left = i;
1117 i = wwin->frame_x + wwin->frame->core->width + strut[head][1];
1118 if (HAS_BORDER(wwin))
1119 i+=2;
1120 if (i > most_right)
1121 most_right = i;
1122 i = wwin->frame_y - strut[head][2];
1123 if (i < most_top)
1124 most_top = i;
1125 i = wwin->frame_y + wwin->frame->core->height + strut[head][3];
1126 if (HAS_BORDER(wwin))
1127 i+=2;
1128 if (i > most_bottom) {
1129 most_bottom = i;
1135 if (most_left > 0) most_left = 0;
1136 if (most_top > 0) most_top = 0;
1138 scr->workspaces[workspace]->width = WMAX(most_right, scr->scr_width) - WMIN(most_left, 0);
1139 scr->workspaces[workspace]->height = WMAX(most_bottom, scr->scr_height) - WMIN(most_top, 0);
1141 *view_x += -most_left - scr->workspaces[workspace]->view_x;
1142 scr->workspaces[workspace]->view_x = -most_left;
1144 *view_y += -most_top - scr->workspaces[workspace]->view_y;
1145 scr->workspaces[workspace]->view_y = -most_top;
1147 wfree(strut);
1151 typedef struct _delay_configure {
1152 WWindow *wwin;
1153 int delay_count;
1154 } _delay_configure;
1157 void
1158 sendConfigureNotify (_delay_configure *delay)
1160 WWindow *wwin;
1162 delay->delay_count--;
1163 if (!delay->delay_count) {
1164 for (wwin = delay->wwin; wwin; wwin = wwin->prev) {
1165 wWindowSynthConfigureNotify(wwin);
1171 Bool
1172 wWorkspaceSetViewport(WScreen *scr, int workspace, int view_x, int view_y)
1174 Bool adjust_flag = False;
1175 int diff_x, diff_y;
1176 static _delay_configure delay_configure = {NULL, 0};
1177 WWorkspace *wptr;
1178 WWindow *wwin;
1180 wptr = scr->workspaces[workspace];
1182 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y);*/
1184 updateWorkspaceGeometry(scr, workspace, &view_x, &view_y);
1186 if (view_x + scr->scr_width > wptr->width) {
1187 /* puts("right edge of vdesk"); */
1188 view_x = wptr->width - scr->scr_width;
1190 if (view_x < 0) {
1191 /* puts("left edge of vdesk"); */
1192 view_x = 0;
1194 if (view_y + scr->scr_height > wptr->height) {
1195 /* puts("right edge of vdesk"); */
1196 view_y = wptr->height - scr->scr_height;
1198 if (view_y < 0) {
1199 /* puts("left edge of vdesk"); */
1200 view_y = 0;
1203 diff_x = wptr->view_x - view_x;
1204 diff_y = wptr->view_y - view_y;
1205 if (!diff_x && !diff_y)
1206 return False;
1208 wptr->view_x = view_x;
1209 wptr->view_y = view_y;
1211 #ifdef NETWM_HINTS
1212 wNETWMUpdateDesktop(scr);
1213 #endif
1215 for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
1216 if (wwin->frame->workspace == workspace && !IS_VSTUCK(wwin)) {
1217 wWindowMove(wwin, wwin->frame_x + diff_x, wwin->frame_y + diff_y);
1218 adjust_flag = True;
1221 if (1) { /* if delay*/
1222 delay_configure.delay_count++;
1223 delay_configure.wwin = scr->focused_window;
1224 WMAddTimerHandler(200, (WMCallback *)sendConfigureNotify, &delay_configure);
1227 return adjust_flag;
1231 #endif
1234 static void
1235 switchWSCommand(WMenu *menu, WMenuEntry *entry)
1237 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
1241 static void
1242 deleteWSCommand(WMenu *menu, WMenuEntry *entry)
1244 wWorkspaceDelete(menu->frame->screen_ptr,
1245 menu->frame->screen_ptr->workspace_count-1);
1249 static void
1250 newWSCommand(WMenu *menu, WMenuEntry *foo)
1252 int ws;
1254 ws = wWorkspaceNew(menu->frame->screen_ptr);
1255 /* autochange workspace*/
1256 if (ws>=0)
1257 wWorkspaceChange(menu->frame->screen_ptr, ws);
1261 if (ws<9) {
1262 int kcode;
1263 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1264 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1265 entry->rtext =
1266 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1272 static char*
1273 cropline(char *line)
1275 char *start, *end;
1277 if (strlen(line)==0)
1278 return line;
1280 start = line;
1281 end = &(line[strlen(line)])-1;
1282 while (isspace(*line) && *line!=0) line++;
1283 while (isspace(*end) && end!=line) {
1284 *end=0;
1285 end--;
1287 return line;
1291 void
1292 wWorkspaceRename(WScreen *scr, int workspace, char *name)
1294 char buf[MAX_WORKSPACENAME_WIDTH+1];
1295 char *tmp;
1297 if (workspace >= scr->workspace_count)
1298 return;
1300 /* trim white spaces */
1301 tmp = cropline(name);
1303 if (strlen(tmp)==0) {
1304 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace+1);
1305 } else {
1306 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
1308 buf[MAX_WORKSPACENAME_WIDTH] = 0;
1310 /* update workspace */
1311 wfree(scr->workspaces[workspace]->name);
1312 scr->workspaces[workspace]->name = wstrdup(buf);
1314 if (scr->clip_ws_menu) {
1315 if (strcmp(scr->clip_ws_menu->entries[workspace+2]->text, buf)!=0) {
1316 wfree(scr->clip_ws_menu->entries[workspace+2]->text);
1317 scr->clip_ws_menu->entries[workspace+2]->text = wstrdup(buf);
1318 wMenuRealize(scr->clip_ws_menu);
1321 if (scr->workspace_menu) {
1322 if (strcmp(scr->workspace_menu->entries[workspace+2]->text, buf)!=0) {
1323 wfree(scr->workspace_menu->entries[workspace+2]->text);
1324 scr->workspace_menu->entries[workspace+2]->text = wstrdup(buf);
1325 wMenuRealize(scr->workspace_menu);
1329 if (scr->clip_icon)
1330 wClipIconPaint(scr->clip_icon);
1332 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void*)(uintptr_t)workspace);
1338 /* callback for when menu entry is edited */
1339 static void
1340 onMenuEntryEdited(WMenu *menu, WMenuEntry *entry)
1342 char *tmp;
1344 tmp = entry->text;
1345 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
1349 WMenu*
1350 wWorkspaceMenuMake(WScreen *scr, Bool titled)
1352 WMenu *wsmenu;
1354 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
1355 if (!wsmenu) {
1356 wwarning(_("could not create Workspace menu"));
1357 return NULL;
1360 /* callback to be called when an entry is edited */
1361 wsmenu->on_edit = onMenuEntryEdited;
1363 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
1364 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
1366 return wsmenu;
1371 void
1372 wWorkspaceMenuUpdate(WScreen *scr, WMenu *menu)
1374 int i;
1375 long ws;
1376 char title[MAX_WORKSPACENAME_WIDTH+1];
1377 WMenuEntry *entry;
1378 int tmp;
1380 if (!menu)
1381 return;
1383 if (menu->entry_no < scr->workspace_count+2) {
1384 /* new workspace(s) added */
1385 i = scr->workspace_count-(menu->entry_no-2);
1386 ws = menu->entry_no - 2;
1387 while (i>0) {
1388 strncpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
1390 entry = wMenuAddCallback(menu, title, switchWSCommand, (void*)ws);
1391 entry->flags.indicator = 1;
1392 entry->flags.editable = 1;
1394 i--;
1395 ws++;
1397 } else if (menu->entry_no > scr->workspace_count+2) {
1398 /* removed workspace(s) */
1399 for (i = menu->entry_no-1; i >= scr->workspace_count+2; i--) {
1400 wMenuRemoveItem(menu, i);
1403 wMenuRealize(menu);
1405 for (i=0; i<scr->workspace_count; i++) {
1406 menu->entries[i+2]->flags.indicator_on = 0;
1408 menu->entries[scr->current_workspace+2]->flags.indicator_on = 1;
1410 /* don't let user destroy current workspace */
1411 if (scr->current_workspace == scr->workspace_count-1) {
1412 wMenuSetEnabled(menu, 1, False);
1413 } else {
1414 wMenuSetEnabled(menu, 1, True);
1417 tmp = menu->frame->top_width + 5;
1418 /* if menu got unreachable, bring it to a visible place */
1419 if (menu->frame_x < tmp - (int)menu->frame->core->width)
1420 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
1422 wMenuPaint(menu);
1426 void
1427 wWorkspaceSaveState(WScreen *scr, WMPropList *old_state)
1429 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
1430 int i;
1432 make_keys();
1434 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
1435 parr = WMCreatePLArray(NULL);
1436 for (i=0; i < scr->workspace_count; i++) {
1437 pstr = WMCreatePLString(scr->workspaces[i]->name);
1438 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
1439 WMReleasePropList(pstr);
1440 if (!wPreferences.flags.noclip) {
1441 pstr = wClipSaveWorkspaceState(scr, i);
1442 WMPutInPLDictionary(wks_state, dClip, pstr);
1443 WMReleasePropList(pstr);
1444 } else if (old_wks_state!=NULL) {
1445 if ((foo = WMGetFromPLArray(old_wks_state, i))!=NULL) {
1446 if ((bar = WMGetFromPLDictionary(foo, dClip))!=NULL) {
1447 WMPutInPLDictionary(wks_state, dClip, bar);
1451 WMAddToPLArray(parr, wks_state);
1452 WMReleasePropList(wks_state);
1454 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
1455 WMReleasePropList(parr);
1459 void
1460 wWorkspaceRestoreState(WScreen *scr)
1462 WMPropList *parr, *pstr, *wks_state, *clip_state;
1463 int i, j, wscount;
1465 make_keys();
1467 if (scr->session_state == NULL)
1468 return;
1470 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
1472 if (!parr)
1473 return;
1475 wscount = scr->workspace_count;
1476 for (i=0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
1477 wks_state = WMGetFromPLArray(parr, i);
1478 if (WMIsPLDictionary(wks_state))
1479 pstr = WMGetFromPLDictionary(wks_state, dName);
1480 else
1481 pstr = wks_state;
1482 if (i >= scr->workspace_count)
1483 wWorkspaceNew(scr);
1484 if (scr->workspace_menu) {
1485 wfree(scr->workspace_menu->entries[i+2]->text);
1486 scr->workspace_menu->entries[i+2]->text = wstrdup(WMGetFromPLString(pstr));
1487 scr->workspace_menu->flags.realized = 0;
1489 wfree(scr->workspaces[i]->name);
1490 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
1491 if (!wPreferences.flags.noclip) {
1492 clip_state = WMGetFromPLDictionary(wks_state, dClip);
1493 if (scr->workspaces[i]->clip)
1494 wDockDestroy(scr->workspaces[i]->clip);
1495 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state,
1496 WM_CLIP);
1497 if (i>0)
1498 wDockHideIcons(scr->workspaces[i]->clip);
1500 /* We set the global icons here, because scr->workspaces[i]->clip
1501 * was not valid in wDockRestoreState().
1502 * There we only set icon->omnipresent to know which icons we
1503 * need to set here.
1505 for (j=0; j<scr->workspaces[i]->clip->max_icons; j++) {
1506 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
1508 if (aicon && aicon->omnipresent) {
1509 aicon->omnipresent = 0;
1510 wClipMakeIconOmnipresent(aicon, True);
1511 XMapWindow(dpy, aicon->icon->core->window);
1516 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void*)(uintptr_t)i);