limit VirtualEdgeThickness values to the 0..10 range
[wmaker-crm.git] / src / workspace.c
blobb0de8b24696838402b4c49a41e034e28be441a38
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 <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 "wcore.h"
40 #include "framewin.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "funcs.h"
44 #include "menu.h"
45 #include "application.h"
46 #include "dock.h"
47 #include "actions.h"
48 #include "workspace.h"
49 #include "appicon.h"
50 #ifdef NETWM_HINTS
51 #include "wmspec.h"
52 #endif
54 #include "xinerama.h"
57 extern WPreferences wPreferences;
58 extern XContext wWinContext;
59 extern XContext wVEdgeContext;
61 extern void ProcessPendingEvents();
63 static WMPropList *dWorkspaces=NULL;
64 static WMPropList *dClip, *dName;
68 static void
69 make_keys()
71 if (dWorkspaces!=NULL)
72 return;
74 dWorkspaces = WMCreatePLString("Workspaces");
75 dName = WMCreatePLString("Name");
76 dClip = WMCreatePLString("Clip");
80 void
81 wWorkspaceMake(WScreen *scr, int count)
83 while (count>0) {
84 wWorkspaceNew(scr);
85 count--;
90 int
91 wWorkspaceNew(WScreen *scr)
93 WWorkspace *wspace, **list;
94 int i;
96 if (scr->workspace_count < MAX_WORKSPACES) {
97 scr->workspace_count++;
99 wspace = wmalloc(sizeof(WWorkspace));
100 wspace->name = NULL;
102 if (!wspace->name) {
103 wspace->name = wmalloc(strlen(_("Workspace %i"))+8);
104 sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
108 if (!wPreferences.flags.noclip) {
109 wspace->clip = wDockCreate(scr, WM_CLIP);
110 } else
111 wspace->clip = NULL;
113 list = wmalloc(sizeof(WWorkspace*)*scr->workspace_count);
115 for (i=0; i<scr->workspace_count-1; i++) {
116 list[i] = scr->workspaces[i];
118 list[i] = wspace;
119 if (scr->workspaces)
120 wfree(scr->workspaces);
121 scr->workspaces = list;
123 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
124 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
125 #ifdef VIRTUAL_DESKTOP
126 wspace->view_x = wspace->view_y = 0;
127 wspace->height = scr->scr_height;
128 wspace->width = scr->scr_width;
129 #endif
130 #ifdef NETWM_HINTS
131 wNETWMUpdateDesktop(scr);
132 #endif
134 WMPostNotificationName(WMNWorkspaceCreated, scr,
135 (void*)(scr->workspace_count-1));
136 XFlush(dpy);
138 return scr->workspace_count-1;
141 return -1;
145 Bool
146 wWorkspaceDelete(WScreen *scr, int workspace)
148 WWindow *tmp;
149 WWorkspace **list;
150 int i, j;
152 if (workspace<=0)
153 return False;
155 /* verify if workspace is in use by some window */
156 tmp = scr->focused_window;
157 while (tmp) {
158 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace==workspace)
159 return False;
160 tmp = tmp->prev;
163 if (!wPreferences.flags.noclip) {
164 wDockDestroy(scr->workspaces[workspace]->clip);
165 scr->workspaces[workspace]->clip = NULL;
168 list = wmalloc(sizeof(WWorkspace*)*(scr->workspace_count-1));
169 j = 0;
170 for (i=0; i<scr->workspace_count; i++) {
171 if (i!=workspace) {
172 list[j++] = scr->workspaces[i];
173 } else {
174 if (scr->workspaces[i]->name)
175 wfree(scr->workspaces[i]->name);
176 wfree(scr->workspaces[i]);
179 wfree(scr->workspaces);
180 scr->workspaces = list;
182 scr->workspace_count--;
185 /* update menu */
186 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
187 /* clip workspace menu */
188 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
190 /* update also window menu */
191 if (scr->workspace_submenu) {
192 WMenu *menu = scr->workspace_submenu;
194 i = menu->entry_no;
195 while (i>scr->workspace_count)
196 wMenuRemoveItem(menu, --i);
197 wMenuRealize(menu);
199 /* and clip menu */
200 if (scr->clip_submenu) {
201 WMenu *menu = scr->clip_submenu;
203 i = menu->entry_no;
204 while (i>scr->workspace_count)
205 wMenuRemoveItem(menu, --i);
206 wMenuRealize(menu);
209 #ifdef NETWM_HINTS
210 wNETWMUpdateDesktop(scr);
211 #endif
213 WMPostNotificationName(WMNWorkspaceDestroyed, scr,
214 (void*)(scr->workspace_count-1));
216 if (scr->current_workspace >= scr->workspace_count)
217 wWorkspaceChange(scr, scr->workspace_count-1);
219 return True;
223 typedef struct WorkspaceNameData {
224 int count;
225 RImage *back;
226 RImage *text;
227 time_t timeout;
228 } WorkspaceNameData;
231 static void
232 hideWorkspaceName(void *data)
234 WScreen *scr = (WScreen*)data;
236 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
237 || time(NULL) > scr->workspace_name_data->timeout) {
238 XUnmapWindow(dpy, scr->workspace_name);
240 if (scr->workspace_name_data) {
241 RReleaseImage(scr->workspace_name_data->back);
242 RReleaseImage(scr->workspace_name_data->text);
243 wfree(scr->workspace_name_data);
245 scr->workspace_name_data = NULL;
247 scr->workspace_name_timer = NULL;
248 } else {
249 RImage *img = RCloneImage(scr->workspace_name_data->back);
250 Pixmap pix;
252 scr->workspace_name_timer =
253 WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
255 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
256 scr->workspace_name_data->count*255/10);
258 RConvertImage(scr->rcontext, img, &pix);
260 RReleaseImage(img);
262 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
263 XClearWindow(dpy, scr->workspace_name);
264 XFreePixmap(dpy, pix);
265 XFlush(dpy);
267 scr->workspace_name_data->count--;
272 static void
273 showWorkspaceName(WScreen *scr, int workspace)
275 WorkspaceNameData *data;
276 RXImage *ximg;
277 Pixmap text, mask;
278 int w, h;
279 int px, py;
280 char *name = scr->workspaces[workspace]->name;
281 int len = strlen(name);
282 int x, y;
284 if (wPreferences.workspace_name_display_position == WD_NONE ||
285 scr->workspace_count < 2) {
286 return;
289 if (scr->workspace_name_timer) {
290 WMDeleteTimerHandler(scr->workspace_name_timer);
291 XUnmapWindow(dpy, scr->workspace_name);
292 XFlush(dpy);
294 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY,
295 hideWorkspaceName, scr);
297 if (scr->workspace_name_data) {
298 RReleaseImage(scr->workspace_name_data->back);
299 RReleaseImage(scr->workspace_name_data->text);
300 wfree(scr->workspace_name_data);
303 data = wmalloc(sizeof(WorkspaceNameData));
304 data->back = NULL;
306 w = WMWidthOfString(scr->workspace_name_font, name, len);
307 h = WMFontHeight(scr->workspace_name_font);
309 switch (wPreferences.workspace_name_display_position) {
310 case WD_TOP:
311 px = (scr->scr_width - (w+4))/2;
312 py = 0;
313 break;
314 case WD_BOTTOM:
315 px = (scr->scr_width - (w+4))/2;
316 py = scr->scr_height - (h+4);
317 break;
318 case WD_TOPLEFT:
319 px = 0;
320 py = 0;
321 break;
322 case WD_TOPRIGHT:
323 px = scr->scr_width - (w+4);
324 py = 0;
325 break;
326 case WD_BOTTOMLEFT:
327 px = 0;
328 py = scr->scr_height - (h+4);
329 break;
330 case WD_BOTTOMRIGHT:
331 px = scr->scr_width - (w+4);
332 py = scr->scr_height - (h+4);
333 break;
334 case WD_CENTER:
335 default:
336 px = (scr->scr_width - (w+4))/2;
337 py = (scr->scr_height - (h+4))/2;
338 break;
340 XResizeWindow(dpy, scr->workspace_name, w+4, h+4);
341 XMoveWindow(dpy, scr->workspace_name, px, py);
343 text = XCreatePixmap(dpy, scr->w_win, w+4, h+4, scr->w_depth);
344 mask = XCreatePixmap(dpy, scr->w_win, w+4, h+4, 1);
346 /*XSetForeground(dpy, scr->mono_gc, 0);
347 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4);*/
349 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w+4, h+4);
351 for (x = 0; x <= 4; x++) {
352 for (y = 0; y <= 4; y++) {
353 WMDrawString(scr->wmscreen, text, scr->white,
354 scr->workspace_name_font, x, y, name, len);
358 XSetForeground(dpy, scr->mono_gc, 1);
359 XSetBackground(dpy, scr->mono_gc, 0);
361 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w+4, h+4, 0, 0, 1<<(scr->w_depth-1));
363 /*XSetForeground(dpy, scr->mono_gc, 1);*/
364 XSetBackground(dpy, scr->mono_gc, 1);
366 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w+4, h+4);
368 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font,
369 2, 2, name, len);
371 #ifdef SHAPE
372 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask,
373 ShapeSet);
374 #endif
375 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
376 XClearWindow(dpy, scr->workspace_name);
378 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
380 XFreePixmap(dpy, text);
381 XFreePixmap(dpy, mask);
383 if (!data->text) {
384 XMapRaised(dpy, scr->workspace_name);
385 XFlush(dpy);
387 goto erro;
390 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py,
391 data->text->width, data->text->height);
393 if (!ximg || !ximg->image) {
394 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 +
411 (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY*data->count)/1000;
413 scr->workspace_name_data = data;
416 return;
418 erro:
419 if (scr->workspace_name_timer)
420 WMDeleteTimerHandler(scr->workspace_name_timer);
422 if (data->text)
423 RReleaseImage(data->text);
424 if (data->back)
425 RReleaseImage(data->back);
426 wfree(data);
428 scr->workspace_name_data = NULL;
430 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
431 10*WORKSPACE_NAME_FADE_DELAY,
432 hideWorkspaceName, scr);
436 void
437 wWorkspaceChange(WScreen *scr, int workspace)
439 if (scr->flags.startup || scr->flags.startup2) {
440 return;
443 if (workspace != scr->current_workspace) {
444 wWorkspaceForceChange(scr, workspace);
445 } /*else {
446 showWorkspaceName(scr, workspace);
451 void
452 wWorkspaceRelativeChange(WScreen *scr, int amount)
454 int w;
456 w = scr->current_workspace + amount;
458 if (amount < 0) {
459 if (w >= 0) {
460 wWorkspaceChange(scr, w);
461 } else if (wPreferences.ws_cycle) {
462 wWorkspaceChange(scr, scr->workspace_count + w);
464 } else if (amount > 0) {
465 if (w < scr->workspace_count) {
466 wWorkspaceChange(scr, w);
467 } else if (wPreferences.ws_advance) {
468 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES-1));
469 } else if (wPreferences.ws_cycle) {
470 wWorkspaceChange(scr, w % scr->workspace_count);
476 void
477 wWorkspaceForceChange(WScreen *scr, int workspace)
479 WWindow *tmp, *foc=NULL, *foc2=NULL;
481 if (workspace >= MAX_WORKSPACES || workspace < 0)
482 return;
484 SendHelperMessage(scr, 'C', workspace+1, NULL);
486 if (workspace > scr->workspace_count-1) {
487 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
490 wClipUpdateForWorkspaceChange(scr, workspace);
492 scr->current_workspace = workspace;
494 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
496 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
498 if ((tmp = scr->focused_window)!= NULL) {
499 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
500 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
501 foc = tmp;
504 /* foc2 = tmp; will fix annoyance with gnome panel
505 * but will create annoyance for every other application
507 while (tmp) {
508 if (tmp->frame->workspace!=workspace && !tmp->flags.selected) {
509 /* unmap windows not on this workspace */
510 if ((tmp->flags.mapped||tmp->flags.shaded) &&
511 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
512 wWindowUnmap(tmp);
514 /* also unmap miniwindows not on this workspace */
515 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
516 tmp->icon && !IS_OMNIPRESENT(tmp)) {
517 XUnmapWindow(dpy, tmp->icon->core->window);
518 tmp->icon->mapped = 0;
520 /* update current workspace of omnipresent windows */
521 if (IS_OMNIPRESENT(tmp)) {
522 WApplication *wapp = wApplicationOf(tmp->main_window);
524 tmp->frame->workspace = workspace;
526 if (wapp) {
527 wapp->last_workspace = workspace;
529 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
530 foc2 = tmp;
533 } else {
534 /* change selected windows' workspace */
535 if (tmp->flags.selected) {
536 wWindowChangeWorkspace(tmp, workspace);
537 if (!tmp->flags.miniaturized && !foc) {
538 foc = tmp;
540 } else {
541 if (!tmp->flags.hidden) {
542 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
543 /* remap windows that are on this workspace */
544 wWindowMap(tmp);
545 if (!foc && !WFLAGP(tmp, no_focusable)) {
546 foc = tmp;
549 /* Also map miniwindow if not omnipresent */
550 if (!wPreferences.sticky_icons &&
551 tmp->flags.miniaturized &&
552 !IS_OMNIPRESENT(tmp) && tmp->icon) {
553 tmp->icon->mapped = 1;
554 XMapWindow(dpy, tmp->icon->core->window);
559 tmp = tmp->prev;
562 /* Gobble up events unleashed by our mapping & unmapping.
563 * These may trigger various grab-initiated focus &
564 * crossing events. However, we don't care about them,
565 * and ignore their focus implications altogether to avoid
566 * flicker.
568 scr->flags.ignore_focus_events = 1;
569 ProcessPendingEvents();
570 scr->flags.ignore_focus_events = 0;
572 if (!foc)
573 foc = foc2;
575 if (scr->focused_window->flags.mapped && !foc) {
576 foc = scr->focused_window;
578 if (wPreferences.focus_mode == WKF_CLICK) {
579 wSetFocusTo(scr, foc);
580 } else {
581 unsigned int mask;
582 int foo;
583 Window bar, win;
584 WWindow *tmp;
586 tmp = NULL;
587 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
588 &foo, &foo, &foo, &foo, &mask)) {
589 tmp = wWindowFor(win);
592 /* If there's a window under the pointer, focus it.
593 * (we ate all other focus events above, so it's
594 * certainly not focused). Otherwise focus last
595 * focused, or the root (depending on sloppiness)
597 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
598 wSetFocusTo(scr, foc);
599 } else {
600 wSetFocusTo(scr, tmp);
605 /* We need to always arrange icons when changing workspace, even if
606 * no autoarrange icons, because else the icons in different workspaces
607 * can be superposed.
608 * This can be avoided if appicons are also workspace specific.
610 if (!wPreferences.sticky_icons)
611 wArrangeIcons(scr, False);
613 if (scr->dock)
614 wAppIconPaint(scr->dock->icon_array[0]);
616 if (scr->clip_icon) {
617 if (scr->workspaces[workspace]->clip->auto_collapse ||
618 scr->workspaces[workspace]->clip->auto_raise_lower) {
619 /* to handle enter notify. This will also */
620 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
621 XMapWindow(dpy, scr->clip_icon->icon->core->window);
622 } else {
623 wClipIconPaint(scr->clip_icon);
627 #ifdef NETWM_HINTS
628 wScreenUpdateUsableArea(scr);
629 wNETWMUpdateDesktop(scr);
630 #endif
632 showWorkspaceName(scr, workspace);
634 WMPostNotificationName(WMNWorkspaceChanged, scr, (void*)workspace);
636 /* XSync(dpy, False); */
640 #ifdef VIRTUAL_DESKTOP
642 /* TODO:
644 * 1) Allow border around each window so the scrolling
645 * won't just stop at the border.
646 * 2) Make pager.
650 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
651 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
652 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
653 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
654 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
655 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
657 #ifndef HAS_BORDER
658 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
659 #endif
661 #ifndef IS_VSTUCK
662 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
663 #endif
665 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
667 static Bool cmp_gez(int i) { return (i >= 0); }
669 static Bool cmp_gz(int i) { return (i > 0); }
672 static WMPoint
673 getClosestEdge(WScreen * scr, WMPoint direction, Bool (*cmp)(int))
675 WMPoint closest = wmkpoint(0, 0);
676 int closest_len = INT_MAX;
677 WWindow * wwin;
679 for (wwin=scr->focused_window; wwin; wwin=wwin->prev) {
680 if (wwin->frame->workspace == scr->current_workspace) {
681 if (!wwin->flags.miniaturized &&
682 !IS_VSTUCK(wwin) &&
683 !wwin->flags.hidden) {
684 int border = 2*HAS_BORDER(wwin);
685 int len;
686 int x1,x2,y1,y2;
687 int head = wGetHeadForWindow(wwin);
688 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
689 WMPoint p;
691 x1 = wwin->frame_x - area.x1;
692 y1 = wwin->frame_y - area.y1;
693 x2 = wwin->frame_x + wwin->frame->core->width + border - area.x2;
694 y2 = wwin->frame_y + wwin->frame->core->height + border - area.y2;
696 p = wmkpoint(x1,y1);
697 len = vec_dot(direction, p);
698 updateMinimum(len, p, closest_len, closest);
700 p = wmkpoint(x1,y2);
701 len = vec_dot(direction, p);
702 updateMinimum(len, p, closest_len, closest);
704 p = wmkpoint(x2,y1);
705 len = vec_dot(direction, p);
706 updateMinimum(len, p, closest_len, closest);
708 p = wmkpoint(x2,y2);
709 len = vec_dot(direction, p);
710 updateMinimum(len, p, closest_len, closest);
715 return closest;
719 static void
720 getViewPosition(WScreen *scr, int workspace, int *x, int *y)
722 *x = scr->workspaces[workspace]->view_x;
723 *y = scr->workspaces[workspace]->view_y;
727 void
728 wWorkspaceKeyboardMoveDesktop(WScreen *scr, WMPoint direction)
730 int x, y;
731 WMPoint edge = getClosestEdge(scr, direction, cmp_gz);
732 int len = vec_dot(edge, direction);
733 WMPoint step = vec_scale(direction, len);
734 getViewPosition(scr, scr->current_workspace, &x, &y);
735 wWorkspaceSetViewport(scr, scr->current_workspace, x+step.x, y+step.y);
739 extern Cursor wCursor[WCUR_LAST];
742 static void
743 vdMouseMoveDesktop(XEvent *event, WMPoint direction)
745 static int lock = False;
746 if (lock) return;
747 lock = True;
749 Bool done = False;
750 Bool moved = True;
751 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
752 WMPoint old_pos = wmkpoint(event->xcrossing.x_root, event->xcrossing.y_root);
753 WMPoint step;
754 int x, y;
755 int resisted = 0;
757 if (XGrabPointer(dpy, event->xcrossing.window, False,
758 PointerMotionMask, GrabModeAsync, GrabModeAsync,
759 scr->root_win, wCursor[WCUR_EMPTY],
760 CurrentTime) != GrabSuccess) {
762 /* if the grab fails, do it the old fashioned way */
763 step = vec_scale2(direction, wPreferences.vedge_hscrollspeed,
764 wPreferences.vedge_vscrollspeed);
765 getViewPosition(scr, scr->current_workspace, &x, &y);
766 if (wWorkspaceSetViewport(scr, scr->current_workspace,
767 x+step.x, y+step.y)) {
768 step = vec_scale(direction, wPreferences.vedge_thickness + 1);
769 XWarpPointer(dpy, None, scr->root_win, 0,0,0,0,
770 event->xcrossing.x_root - step.x,
771 event->xcrossing.y_root - step.y);
773 goto exit;
775 XSync(dpy, True);
777 if (old_pos.x < 0)
778 old_pos.x = 0;
779 if (old_pos.y < 0)
780 old_pos.y = 0;
781 if (old_pos.x > scr->scr_width)
782 old_pos.x = scr->scr_width;
783 if (old_pos.y > scr->scr_height)
784 old_pos.y = scr->scr_height;
786 while (!done) {
787 XEvent ev;
788 if (moved) {
789 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0,
790 scr->scr_width/2, scr->scr_height/2);
791 moved = False;
793 WMMaskEvent(dpy, PointerMotionMask, &ev);
795 switch (ev.type) {
796 case MotionNotify:
798 int step_len;
799 step = wmkpoint(ev.xmotion.x_root-scr->scr_width/2,
800 ev.xmotion.y_root-scr->scr_height/2);
801 step_len = vec_dot(step, direction);
802 if (step_len < 0) {
803 done = True;
804 break;
807 if (step_len > 0) {
808 Bool do_move = True;
809 int resist = wPreferences.vedge_resistance;
810 WMPoint closest;
811 int closest_len = INT_MAX;
812 if (resist) {
813 closest = getClosestEdge(scr, direction, cmp_gez);
814 closest_len = vec_dot(direction, closest);
817 if (!closest_len) {
818 resisted += step_len;
819 do_move = resisted >= resist;
820 if (do_move) {
821 closest_len = INT_MAX;
822 step_len = resisted - resist;
823 resisted = 0;
826 if (do_move) {
827 if (closest_len <= wPreferences.vedge_attraction) {
828 step = vec_scale(direction, closest_len);
829 } else {
830 step = vec_scale(direction, step_len);
833 getViewPosition(scr, scr->current_workspace, &x, &y);
834 wWorkspaceSetViewport(scr, scr->current_workspace,
835 x+step.x, y+step.y);
836 moved = True;
840 break;
844 step = vec_add(old_pos, vec_scale(direction, -1));
845 XWarpPointer(dpy, None, scr->root_win, 0,0,0,0, step.x, step.y);
846 XUngrabPointer(dpy, CurrentTime);
848 exit:
849 lock = False;
853 static void
854 vdHandleEnter_u(XEvent *event) {
855 vdMouseMoveDesktop(event, VEC_UP);
859 static void
860 vdHandleEnter_d(XEvent *event) {
861 vdMouseMoveDesktop(event, VEC_DOWN);
865 static void
866 vdHandleEnter_l(XEvent *event) {
867 vdMouseMoveDesktop(event, VEC_LEFT);
871 static void
872 vdHandleEnter_r(XEvent *event) {
873 vdMouseMoveDesktop(event, VEC_RIGHT);
877 #define LEFT_EDGE 0x01
878 #define RIGHT_EDGE 0x02
879 #define TOP_EDGE 0x04
880 #define BOTTOM_EDGE 0x08
881 #define ALL_EDGES 0x0F
883 void
884 wWorkspaceManageEdge(WScreen *scr)
886 if (!scr->virtual_edges && wPreferences.vedge_thickness) {
887 int i, j, w;
888 int vmask;
889 XSetWindowAttributes attribs;
891 int heads = wXineramaHeads(scr);
892 int *hasEdges = (int*)wmalloc(sizeof(int)*heads);
894 int thickness = wPreferences.vedge_thickness;
895 int nr_edges = 0;
896 int max_edges = 4*heads;
897 int head;
898 Window *edges = (Window *)wmalloc(sizeof(Window)*max_edges);
900 for (i=0; i<heads; ++i)
901 hasEdges[i] = ALL_EDGES;
902 for (i=0; i<heads; ++i) {
903 WMRect i_rect = wGetRectForHead(scr, i);
904 for (j=i+1; j<heads; ++j) {
905 WMRect j_rect = wGetRectForHead(scr, j);
907 int vlen = (WMIN(i_rect.pos.y + i_rect.size.height,
908 j_rect.pos.y + j_rect.size.height) -
909 WMAX(i_rect.pos.y, j_rect.pos.y));
911 int hlen = (WMIN(i_rect.pos.x + i_rect.size.width,
912 j_rect.pos.x + j_rect.size.width) -
913 WMAX( i_rect.pos.x, j_rect.pos.x));
915 if (vlen > 0 && hlen == 0) { /* horz alignment, vert edges touch */
916 if (i_rect.pos.x < j_rect.pos.x) { /* i left of j */
917 hasEdges[i] &= ~RIGHT_EDGE;
918 hasEdges[j] &= ~LEFT_EDGE;
919 } else { /* j left of i */
920 hasEdges[j] &= ~RIGHT_EDGE;
921 hasEdges[i] &= ~LEFT_EDGE;
923 } else if (vlen == 0 && hlen > 0) { /* vert alignment, horz edges touch */
924 if (i_rect.pos.y < j_rect.pos.y) { /* i top of j */
925 hasEdges[i] &= ~BOTTOM_EDGE;
926 hasEdges[j] &= ~TOP_EDGE;
927 } else { /* j top of i */
928 hasEdges[j] &= ~BOTTOM_EDGE;
929 hasEdges[i] &= ~TOP_EDGE;
935 for (w = 0; w < scr->workspace_count; w++) {
936 /* puts("reset workspace"); */
937 wWorkspaceSetViewport(scr, w, 0, 0);
940 vmask = CWEventMask|CWOverrideRedirect;
941 attribs.event_mask = (EnterWindowMask | LeaveWindowMask | VisibilityChangeMask);
942 attribs.override_redirect = True;
944 for (head=0; head<wXineramaHeads(scr); ++head) {
945 WMRect rect = wGetRectForHead(scr, head);
947 if (hasEdges[head] & TOP_EDGE) {
948 edges[nr_edges] =
949 XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
950 rect.size.width, thickness, 0,
951 CopyFromParent, InputOnly, CopyFromParent,
952 vmask, &attribs);
953 XSaveContext(dpy, edges[nr_edges], wVEdgeContext,
954 (XPointer)vdHandleEnter_u);
955 ++nr_edges;
958 if (hasEdges[head] & BOTTOM_EDGE) {
959 edges[nr_edges] =
960 XCreateWindow(dpy, scr->root_win, rect.pos.x,
961 rect.pos.y+rect.size.height-thickness,
962 rect.size.width, thickness, 0,
963 CopyFromParent, InputOnly, CopyFromParent,
964 vmask, &attribs);
965 XSaveContext(dpy, edges[nr_edges], wVEdgeContext,
966 (XPointer)vdHandleEnter_d);
967 ++nr_edges;
970 if (hasEdges[head] & LEFT_EDGE) {
971 edges[nr_edges] =
972 XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
973 thickness, rect.pos.y+rect.size.height, 0,
974 CopyFromParent, InputOnly, CopyFromParent,
975 vmask, &attribs);
976 XSaveContext(dpy, edges[nr_edges], wVEdgeContext,
977 (XPointer)vdHandleEnter_l);
978 ++nr_edges;
981 if (hasEdges[head] & RIGHT_EDGE) {
982 edges[nr_edges] =
983 XCreateWindow(dpy, scr->root_win,
984 rect.pos.x + rect.size.width - thickness, rect.pos.y,
985 thickness, rect.size.height, 0,
986 CopyFromParent, InputOnly, CopyFromParent, vmask,
987 &attribs);
988 XSaveContext(dpy, edges[nr_edges], wVEdgeContext,
989 (XPointer)vdHandleEnter_r);
990 ++nr_edges;
994 scr->virtual_nr_edges = nr_edges;
995 scr->virtual_edges = edges;
997 for (i=0; i<scr->virtual_nr_edges; ++i) {
998 XMapWindow(dpy, scr->virtual_edges[i]);
1000 wWorkspaceRaiseEdge(scr);
1002 wfree(hasEdges);
1007 static void
1008 destroyEdge(WScreen *scr)
1010 if (scr->virtual_edges) {
1011 int i;
1013 for (i=0; i<scr->virtual_nr_edges; ++i) {
1014 XDeleteContext(dpy, scr->virtual_edges[i], wVEdgeContext);
1015 XUnmapWindow(dpy, scr->virtual_edges[i]);
1016 XDestroyWindow(dpy, scr->virtual_edges[i]);
1019 wfree(scr->virtual_edges);
1020 scr->virtual_edges = NULL;
1021 scr->virtual_nr_edges = 0;
1026 void
1027 wWorkspaceUpdateEdge(WScreen *scr)
1029 static int thickness = -1;
1031 if ((int)wPreferences.vedge_thickness < 0)
1032 wPreferences.vedge_thickness = 0;
1033 else if ((int)wPreferences.vedge_thickness > 10)
1034 wPreferences.vedge_thickness = 10;
1036 if (wPreferences.vedge_thickness == thickness)
1037 return;
1039 thickness = wPreferences.vedge_thickness;
1041 if (wPreferences.vedge_thickness) {
1042 destroyEdge(scr);
1043 wWorkspaceManageEdge(scr);
1044 } else {
1045 destroyEdge(scr);
1050 void
1051 wWorkspaceRaiseEdge(WScreen *scr)
1053 static int toggle = 0;
1054 int i;
1056 if (!scr->virtual_edges)
1057 return;
1059 if (toggle) {
1060 for (i=0; i<scr->virtual_nr_edges; ++i) {
1061 XRaiseWindow(dpy, scr->virtual_edges[i]);
1063 } else {
1064 for (i=scr->virtual_nr_edges-1; i>=0; --i) {
1065 XRaiseWindow(dpy, scr->virtual_edges[i]);
1069 toggle ^= 1;
1073 void
1074 wWorkspaceLowerEdge(WScreen *scr)
1076 int i;
1077 for (i=0; i<scr->virtual_nr_edges; ++i) {
1078 XLowerWindow(dpy, scr->virtual_edges[i]);
1083 void
1084 wWorkspaceResizeViewport(WScreen *scr, int workspace)
1086 int x, y;
1087 getViewPosition(scr, scr->current_workspace, &x, &y);
1088 wWorkspaceSetViewport(scr, scr->current_workspace, x, y);
1092 void
1093 updateWorkspaceGeometry(WScreen *scr, int workspace, int *view_x, int *view_y)
1095 int most_left, most_right, most_top, most_bottom;
1096 WWindow *wwin;
1098 int heads = wXineramaHeads(scr);
1099 typedef int strut_t[4];
1100 strut_t * strut = (strut_t*)wmalloc(heads*sizeof(strut_t));
1101 int head, i;
1103 for (head=0; head<heads; ++head) {
1104 WMRect rect = wGetRectForHead(scr, head);
1105 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1106 strut[head][0] = area.x1 - rect.pos.x;
1107 strut[head][1] = rect.pos.x + rect.size.width - area.x2;
1108 strut[head][2] = area.y1 - rect.pos.y;
1109 strut[head][3] = rect.pos.y + rect.size.height - area.y2;
1112 /* adjust workspace layout */
1113 wwin = scr->focused_window;
1114 most_right = 0;
1115 most_bottom = 0;
1116 most_left = scr->scr_width;
1117 most_top = scr->scr_height;
1118 for(;wwin; wwin = wwin->prev) {
1119 if (wwin->frame->workspace == workspace) {
1120 if (!wwin->flags.miniaturized && !IS_VSTUCK(wwin) &&
1121 !wwin->flags.hidden) {
1123 head = wGetHeadForWindow(wwin);
1125 i = wwin->frame_x - strut[head][0];
1126 if (i < most_left) /* record positions, should this be cached? */
1127 most_left = i;
1128 i = wwin->frame_x + wwin->frame->core->width + strut[head][1];
1129 if (HAS_BORDER(wwin))
1130 i+=2;
1131 if (i > most_right)
1132 most_right = i;
1133 i = wwin->frame_y - strut[head][2];
1134 if (i < most_top)
1135 most_top = i;
1136 i = wwin->frame_y + wwin->frame->core->height + strut[head][3];
1137 if (HAS_BORDER(wwin))
1138 i+=2;
1139 if (i > most_bottom) {
1140 most_bottom = i;
1146 if (most_left > 0) most_left = 0;
1147 if (most_top > 0) most_top = 0;
1149 scr->workspaces[workspace]->width = WMAX(most_right, scr->scr_width) - WMIN(most_left, 0);
1150 scr->workspaces[workspace]->height = WMAX(most_bottom, scr->scr_height) - WMIN(most_top, 0);
1152 *view_x += -most_left - scr->workspaces[workspace]->view_x;
1153 scr->workspaces[workspace]->view_x = -most_left;
1155 *view_y += -most_top - scr->workspaces[workspace]->view_y;
1156 scr->workspaces[workspace]->view_y = -most_top;
1158 wfree(strut);
1162 typedef struct _delay_configure {
1163 WWindow *wwin;
1164 int delay_count;
1165 } _delay_configure;
1168 void
1169 sendConfigureNotify (_delay_configure *delay)
1171 WWindow *wwin;
1173 delay->delay_count--;
1174 if (!delay->delay_count) {
1175 for (wwin = delay->wwin; wwin; wwin = wwin->prev) {
1176 wWindowSynthConfigureNotify(wwin);
1182 Bool
1183 wWorkspaceSetViewport(WScreen *scr, int workspace, int view_x, int view_y)
1185 Bool adjust_flag = False;
1186 int diff_x, diff_y;
1187 static _delay_configure delay_configure = {NULL, 0};
1188 WWorkspace *wptr;
1189 WWindow *wwin;
1191 wptr = scr->workspaces[workspace];
1193 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y);*/
1195 updateWorkspaceGeometry(scr, workspace, &view_x, &view_y);
1197 if (view_x + scr->scr_width > wptr->width) {
1198 /* puts("right edge of vdesk"); */
1199 view_x = wptr->width - scr->scr_width;
1201 if (view_x < 0) {
1202 /* puts("left edge of vdesk"); */
1203 view_x = 0;
1205 if (view_y + scr->scr_height > wptr->height) {
1206 /* puts("right edge of vdesk"); */
1207 view_y = wptr->height - scr->scr_height;
1209 if (view_y < 0) {
1210 /* puts("left edge of vdesk"); */
1211 view_y = 0;
1214 diff_x = wptr->view_x - view_x;
1215 diff_y = wptr->view_y - view_y;
1216 if (!diff_x && !diff_y)
1217 return False;
1219 wptr->view_x = view_x;
1220 wptr->view_y = view_y;
1222 #ifdef NETWM_HINTS
1223 wNETWMUpdateDesktop(scr);
1224 #endif
1226 for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
1227 if (wwin->frame->workspace == workspace && !IS_VSTUCK(wwin)) {
1228 wWindowMove(wwin, wwin->frame_x + diff_x, wwin->frame_y + diff_y);
1229 adjust_flag = True;
1232 if (1) { /* if delay*/
1233 delay_configure.delay_count++;
1234 delay_configure.wwin = scr->focused_window;
1235 WMAddTimerHandler(200, (WMCallback *)sendConfigureNotify, &delay_configure);
1238 return adjust_flag;
1242 #endif
1245 static void
1246 switchWSCommand(WMenu *menu, WMenuEntry *entry)
1248 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
1252 static void
1253 deleteWSCommand(WMenu *menu, WMenuEntry *entry)
1255 wWorkspaceDelete(menu->frame->screen_ptr,
1256 menu->frame->screen_ptr->workspace_count-1);
1260 static void
1261 newWSCommand(WMenu *menu, WMenuEntry *foo)
1263 int ws;
1265 ws = wWorkspaceNew(menu->frame->screen_ptr);
1266 /* autochange workspace*/
1267 if (ws>=0)
1268 wWorkspaceChange(menu->frame->screen_ptr, ws);
1272 if (ws<9) {
1273 int kcode;
1274 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1275 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1276 entry->rtext =
1277 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1283 static char*
1284 cropline(char *line)
1286 char *start, *end;
1288 if (strlen(line)==0)
1289 return line;
1291 start = line;
1292 end = &(line[strlen(line)])-1;
1293 while (isspace(*line) && *line!=0) line++;
1294 while (isspace(*end) && end!=line) {
1295 *end=0;
1296 end--;
1298 return line;
1302 void
1303 wWorkspaceRename(WScreen *scr, int workspace, char *name)
1305 char buf[MAX_WORKSPACENAME_WIDTH+1];
1306 char *tmp;
1308 if (workspace >= scr->workspace_count)
1309 return;
1311 /* trim white spaces */
1312 tmp = cropline(name);
1314 if (strlen(tmp)==0) {
1315 sprintf(buf, _("Workspace %i"), workspace+1);
1316 } else {
1317 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
1319 buf[MAX_WORKSPACENAME_WIDTH] = 0;
1321 /* update workspace */
1322 wfree(scr->workspaces[workspace]->name);
1323 scr->workspaces[workspace]->name = wstrdup(buf);
1325 if (scr->clip_ws_menu) {
1326 if (strcmp(scr->clip_ws_menu->entries[workspace+2]->text, buf)!=0) {
1327 wfree(scr->clip_ws_menu->entries[workspace+2]->text);
1328 scr->clip_ws_menu->entries[workspace+2]->text = wstrdup(buf);
1329 wMenuRealize(scr->clip_ws_menu);
1332 if (scr->workspace_menu) {
1333 if (strcmp(scr->workspace_menu->entries[workspace+2]->text, buf)!=0) {
1334 wfree(scr->workspace_menu->entries[workspace+2]->text);
1335 scr->workspace_menu->entries[workspace+2]->text = wstrdup(buf);
1336 wMenuRealize(scr->workspace_menu);
1340 if (scr->clip_icon)
1341 wClipIconPaint(scr->clip_icon);
1343 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void*)workspace);
1349 /* callback for when menu entry is edited */
1350 static void
1351 onMenuEntryEdited(WMenu *menu, WMenuEntry *entry)
1353 char *tmp;
1355 tmp = entry->text;
1356 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
1360 WMenu*
1361 wWorkspaceMenuMake(WScreen *scr, Bool titled)
1363 WMenu *wsmenu;
1365 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
1366 if (!wsmenu) {
1367 wwarning(_("could not create Workspace menu"));
1368 return NULL;
1371 /* callback to be called when an entry is edited */
1372 wsmenu->on_edit = onMenuEntryEdited;
1374 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
1375 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
1377 return wsmenu;
1382 void
1383 wWorkspaceMenuUpdate(WScreen *scr, WMenu *menu)
1385 int i;
1386 long ws;
1387 char title[MAX_WORKSPACENAME_WIDTH+1];
1388 WMenuEntry *entry;
1389 int tmp;
1391 if (!menu)
1392 return;
1394 if (menu->entry_no < scr->workspace_count+2) {
1395 /* new workspace(s) added */
1396 i = scr->workspace_count-(menu->entry_no-2);
1397 ws = menu->entry_no - 2;
1398 while (i>0) {
1399 strcpy(title, scr->workspaces[ws]->name);
1401 entry = wMenuAddCallback(menu, title, switchWSCommand, (void*)ws);
1402 entry->flags.indicator = 1;
1403 entry->flags.editable = 1;
1405 i--;
1406 ws++;
1408 } else if (menu->entry_no > scr->workspace_count+2) {
1409 /* removed workspace(s) */
1410 for (i = menu->entry_no-1; i >= scr->workspace_count+2; i--) {
1411 wMenuRemoveItem(menu, i);
1414 wMenuRealize(menu);
1416 for (i=0; i<scr->workspace_count; i++) {
1417 menu->entries[i+2]->flags.indicator_on = 0;
1419 menu->entries[scr->current_workspace+2]->flags.indicator_on = 1;
1421 /* don't let user destroy current workspace */
1422 if (scr->current_workspace == scr->workspace_count-1) {
1423 wMenuSetEnabled(menu, 1, False);
1424 } else {
1425 wMenuSetEnabled(menu, 1, True);
1428 tmp = menu->frame->top_width + 5;
1429 /* if menu got unreachable, bring it to a visible place */
1430 if (menu->frame_x < tmp - (int)menu->frame->core->width)
1431 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
1433 wMenuPaint(menu);
1437 void
1438 wWorkspaceSaveState(WScreen *scr, WMPropList *old_state)
1440 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
1441 int i;
1443 make_keys();
1445 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
1446 parr = WMCreatePLArray(NULL);
1447 for (i=0; i < scr->workspace_count; i++) {
1448 pstr = WMCreatePLString(scr->workspaces[i]->name);
1449 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
1450 WMReleasePropList(pstr);
1451 if (!wPreferences.flags.noclip) {
1452 pstr = wClipSaveWorkspaceState(scr, i);
1453 WMPutInPLDictionary(wks_state, dClip, pstr);
1454 WMReleasePropList(pstr);
1455 } else if (old_wks_state!=NULL) {
1456 if ((foo = WMGetFromPLArray(old_wks_state, i))!=NULL) {
1457 if ((bar = WMGetFromPLDictionary(foo, dClip))!=NULL) {
1458 WMPutInPLDictionary(wks_state, dClip, bar);
1462 WMAddToPLArray(parr, wks_state);
1463 WMReleasePropList(wks_state);
1465 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
1466 WMReleasePropList(parr);
1470 void
1471 wWorkspaceRestoreState(WScreen *scr)
1473 WMPropList *parr, *pstr, *wks_state, *clip_state;
1474 int i, j, wscount;
1476 make_keys();
1478 if (scr->session_state == NULL)
1479 return;
1481 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
1483 if (!parr)
1484 return;
1486 wscount = scr->workspace_count;
1487 for (i=0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
1488 wks_state = WMGetFromPLArray(parr, i);
1489 if (WMIsPLDictionary(wks_state))
1490 pstr = WMGetFromPLDictionary(wks_state, dName);
1491 else
1492 pstr = wks_state;
1493 if (i >= scr->workspace_count)
1494 wWorkspaceNew(scr);
1495 if (scr->workspace_menu) {
1496 wfree(scr->workspace_menu->entries[i+2]->text);
1497 scr->workspace_menu->entries[i+2]->text = wstrdup(WMGetFromPLString(pstr));
1498 scr->workspace_menu->flags.realized = 0;
1500 wfree(scr->workspaces[i]->name);
1501 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
1502 if (!wPreferences.flags.noclip) {
1503 clip_state = WMGetFromPLDictionary(wks_state, dClip);
1504 if (scr->workspaces[i]->clip)
1505 wDockDestroy(scr->workspaces[i]->clip);
1506 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state,
1507 WM_CLIP);
1508 if (i>0)
1509 wDockHideIcons(scr->workspaces[i]->clip);
1511 /* We set the global icons here, because scr->workspaces[i]->clip
1512 * was not valid in wDockRestoreState().
1513 * There we only set icon->omnipresent to know which icons we
1514 * need to set here.
1516 for (j=0; j<scr->workspaces[i]->clip->max_icons; j++) {
1517 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
1519 if (aicon && aicon->omnipresent) {
1520 aicon->omnipresent = 0;
1521 wClipMakeIconOmnipresent(aicon, True);
1522 XMapWindow(dpy, aicon->icon->core->window);
1527 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void*)i);