swpanel: Start with the first window when all are minimized
[wmaker-crm.git] / src / workspace.c
blob6bac7bdc4a6fbac86426c8a7009b1668b5513995
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;
68 static void make_keys()
70 if (dWorkspaces != NULL)
71 return;
73 dWorkspaces = WMCreatePLString("Workspaces");
74 dName = WMCreatePLString("Name");
75 dClip = WMCreatePLString("Clip");
78 void wWorkspaceMake(WScreen * scr, int count)
80 while (count > 0) {
81 wWorkspaceNew(scr);
82 count--;
86 int wWorkspaceNew(WScreen * scr)
88 WWorkspace *wspace, **list;
89 int i;
91 if (scr->workspace_count < MAX_WORKSPACES) {
92 scr->workspace_count++;
94 wspace = wmalloc(sizeof(WWorkspace));
95 wspace->name = NULL;
97 if (!wspace->name) {
98 wspace->name = wmalloc(strlen(_("Workspace %i")) + 8);
99 sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
102 if (!wPreferences.flags.noclip) {
103 wspace->clip = wDockCreate(scr, WM_CLIP);
104 } else
105 wspace->clip = NULL;
107 list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
109 for (i = 0; i < scr->workspace_count - 1; i++) {
110 list[i] = scr->workspaces[i];
112 list[i] = wspace;
113 if (scr->workspaces)
114 wfree(scr->workspaces);
115 scr->workspaces = list;
117 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
118 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
119 #ifdef VIRTUAL_DESKTOP
120 wspace->view_x = wspace->view_y = 0;
121 wspace->height = scr->scr_height;
122 wspace->width = scr->scr_width;
123 #endif
124 #ifdef NETWM_HINTS
125 wNETWMUpdateDesktop(scr);
126 #endif
128 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
129 XFlush(dpy);
131 return scr->workspace_count - 1;
134 return -1;
137 Bool wWorkspaceDelete(WScreen * scr, int workspace)
139 WWindow *tmp;
140 WWorkspace **list;
141 int i, j;
143 if (workspace <= 0)
144 return False;
146 /* verify if workspace is in use by some window */
147 tmp = scr->focused_window;
148 while (tmp) {
149 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
150 return False;
151 tmp = tmp->prev;
154 if (!wPreferences.flags.noclip) {
155 wDockDestroy(scr->workspaces[workspace]->clip);
156 scr->workspaces[workspace]->clip = NULL;
159 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
160 j = 0;
161 for (i = 0; i < scr->workspace_count; i++) {
162 if (i != workspace) {
163 list[j++] = scr->workspaces[i];
164 } else {
165 if (scr->workspaces[i]->name)
166 wfree(scr->workspaces[i]->name);
167 wfree(scr->workspaces[i]);
170 wfree(scr->workspaces);
171 scr->workspaces = list;
173 scr->workspace_count--;
175 /* update menu */
176 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
177 /* clip workspace menu */
178 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
180 /* update also window menu */
181 if (scr->workspace_submenu) {
182 WMenu *menu = scr->workspace_submenu;
184 i = menu->entry_no;
185 while (i > scr->workspace_count)
186 wMenuRemoveItem(menu, --i);
187 wMenuRealize(menu);
189 /* and clip menu */
190 if (scr->clip_submenu) {
191 WMenu *menu = scr->clip_submenu;
193 i = menu->entry_no;
194 while (i > scr->workspace_count)
195 wMenuRemoveItem(menu, --i);
196 wMenuRealize(menu);
198 #ifdef NETWM_HINTS
199 wNETWMUpdateDesktop(scr);
200 #endif
202 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
204 if (scr->current_workspace >= scr->workspace_count)
205 wWorkspaceChange(scr, scr->workspace_count - 1);
207 return True;
210 typedef struct WorkspaceNameData {
211 int count;
212 RImage *back;
213 RImage *text;
214 time_t timeout;
215 } WorkspaceNameData;
217 static void hideWorkspaceName(void *data)
219 WScreen *scr = (WScreen *) data;
221 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
222 || time(NULL) > scr->workspace_name_data->timeout) {
223 XUnmapWindow(dpy, scr->workspace_name);
225 if (scr->workspace_name_data) {
226 RReleaseImage(scr->workspace_name_data->back);
227 RReleaseImage(scr->workspace_name_data->text);
228 wfree(scr->workspace_name_data);
230 scr->workspace_name_data = NULL;
232 scr->workspace_name_timer = NULL;
233 } else {
234 RImage *img = RCloneImage(scr->workspace_name_data->back);
235 Pixmap pix;
237 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
239 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
240 scr->workspace_name_data->count * 255 / 10);
242 RConvertImage(scr->rcontext, img, &pix);
244 RReleaseImage(img);
246 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
247 XClearWindow(dpy, scr->workspace_name);
248 XFreePixmap(dpy, pix);
249 XFlush(dpy);
251 scr->workspace_name_data->count--;
255 static void showWorkspaceName(WScreen * scr, int workspace)
257 WorkspaceNameData *data;
258 RXImage *ximg;
259 Pixmap text, mask;
260 int w, h;
261 int px, py;
262 char *name = scr->workspaces[workspace]->name;
263 int len = strlen(name);
264 int x, y;
266 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2) {
267 return;
270 if (scr->workspace_name_timer) {
271 WMDeleteTimerHandler(scr->workspace_name_timer);
272 XUnmapWindow(dpy, scr->workspace_name);
273 XFlush(dpy);
275 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
277 if (scr->workspace_name_data) {
278 RReleaseImage(scr->workspace_name_data->back);
279 RReleaseImage(scr->workspace_name_data->text);
280 wfree(scr->workspace_name_data);
283 data = wmalloc(sizeof(WorkspaceNameData));
284 data->back = NULL;
286 w = WMWidthOfString(scr->workspace_name_font, name, len);
287 h = WMFontHeight(scr->workspace_name_font);
289 switch (wPreferences.workspace_name_display_position) {
290 case WD_TOP:
291 px = (scr->scr_width - (w + 4)) / 2;
292 py = 0;
293 break;
294 case WD_BOTTOM:
295 px = (scr->scr_width - (w + 4)) / 2;
296 py = scr->scr_height - (h + 4);
297 break;
298 case WD_TOPLEFT:
299 px = 0;
300 py = 0;
301 break;
302 case WD_TOPRIGHT:
303 px = scr->scr_width - (w + 4);
304 py = 0;
305 break;
306 case WD_BOTTOMLEFT:
307 px = 0;
308 py = scr->scr_height - (h + 4);
309 break;
310 case WD_BOTTOMRIGHT:
311 px = scr->scr_width - (w + 4);
312 py = scr->scr_height - (h + 4);
313 break;
314 case WD_CENTER:
315 default:
316 px = (scr->scr_width - (w + 4)) / 2;
317 py = (scr->scr_height - (h + 4)) / 2;
318 break;
320 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
321 XMoveWindow(dpy, scr->workspace_name, px, py);
323 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
324 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
326 /*XSetForeground(dpy, scr->mono_gc, 0);
327 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
329 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
331 for (x = 0; x <= 4; x++) {
332 for (y = 0; y <= 4; y++) {
333 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
337 XSetForeground(dpy, scr->mono_gc, 1);
338 XSetBackground(dpy, scr->mono_gc, 0);
340 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
342 /*XSetForeground(dpy, scr->mono_gc, 1); */
343 XSetBackground(dpy, scr->mono_gc, 1);
345 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
347 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
349 #ifdef SHAPE
350 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
351 #endif
352 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
353 XClearWindow(dpy, scr->workspace_name);
355 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
357 XFreePixmap(dpy, text);
358 XFreePixmap(dpy, mask);
360 if (!data->text) {
361 XMapRaised(dpy, scr->workspace_name);
362 XFlush(dpy);
364 goto erro;
367 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
369 if (!ximg || !ximg->image) {
370 goto erro;
373 XMapRaised(dpy, scr->workspace_name);
374 XFlush(dpy);
376 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
377 RDestroyXImage(scr->rcontext, ximg);
379 if (!data->back) {
380 goto erro;
383 data->count = 10;
385 /* set a timeout for the effect */
386 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
388 scr->workspace_name_data = data;
390 return;
392 erro:
393 if (scr->workspace_name_timer)
394 WMDeleteTimerHandler(scr->workspace_name_timer);
396 if (data->text)
397 RReleaseImage(data->text);
398 if (data->back)
399 RReleaseImage(data->back);
400 wfree(data);
402 scr->workspace_name_data = NULL;
404 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
405 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
408 void wWorkspaceChange(WScreen * scr, int workspace)
410 if (scr->flags.startup || scr->flags.startup2) {
411 return;
414 if (workspace != scr->current_workspace) {
415 wWorkspaceForceChange(scr, workspace);
416 } /*else {
417 showWorkspaceName(scr, workspace);
418 } */
421 void wWorkspaceRelativeChange(WScreen * scr, int amount)
423 int w;
425 w = scr->current_workspace + amount;
427 if (amount < 0) {
428 if (w >= 0) {
429 wWorkspaceChange(scr, w);
430 } else if (wPreferences.ws_cycle) {
431 wWorkspaceChange(scr, scr->workspace_count + w);
433 } else if (amount > 0) {
434 if (w < scr->workspace_count) {
435 wWorkspaceChange(scr, w);
436 } else if (wPreferences.ws_advance) {
437 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
438 } else if (wPreferences.ws_cycle) {
439 wWorkspaceChange(scr, w % scr->workspace_count);
444 void wWorkspaceForceChange(WScreen * scr, int workspace)
446 WWindow *tmp, *foc = NULL, *foc2 = NULL;
448 if (workspace >= MAX_WORKSPACES || workspace < 0)
449 return;
451 SendHelperMessage(scr, 'C', workspace + 1, NULL);
453 if (workspace > scr->workspace_count - 1) {
454 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
457 wClipUpdateForWorkspaceChange(scr, workspace);
459 scr->current_workspace = workspace;
461 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
463 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
465 if ((tmp = scr->focused_window) != NULL) {
466 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
467 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
468 foc = tmp;
471 /* foc2 = tmp; will fix annoyance with gnome panel
472 * but will create annoyance for every other application
474 while (tmp) {
475 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
476 /* unmap windows not on this workspace */
477 if ((tmp->flags.mapped || tmp->flags.shaded) &&
478 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
479 wWindowUnmap(tmp);
481 /* also unmap miniwindows not on this workspace */
482 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
483 tmp->icon && !IS_OMNIPRESENT(tmp)) {
484 XUnmapWindow(dpy, tmp->icon->core->window);
485 tmp->icon->mapped = 0;
487 /* update current workspace of omnipresent windows */
488 if (IS_OMNIPRESENT(tmp)) {
489 WApplication *wapp = wApplicationOf(tmp->main_window);
491 tmp->frame->workspace = workspace;
493 if (wapp) {
494 wapp->last_workspace = workspace;
496 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
497 foc2 = tmp;
500 } else {
501 /* change selected windows' workspace */
502 if (tmp->flags.selected) {
503 wWindowChangeWorkspace(tmp, workspace);
504 if (!tmp->flags.miniaturized && !foc) {
505 foc = tmp;
507 } else {
508 if (!tmp->flags.hidden) {
509 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
510 /* remap windows that are on this workspace */
511 wWindowMap(tmp);
512 if (!foc && !WFLAGP(tmp, no_focusable)) {
513 foc = tmp;
516 /* Also map miniwindow if not omnipresent */
517 if (!wPreferences.sticky_icons &&
518 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
519 tmp->icon->mapped = 1;
520 XMapWindow(dpy, tmp->icon->core->window);
525 tmp = tmp->prev;
528 /* Gobble up events unleashed by our mapping & unmapping.
529 * These may trigger various grab-initiated focus &
530 * crossing events. However, we don't care about them,
531 * and ignore their focus implications altogether to avoid
532 * flicker.
534 scr->flags.ignore_focus_events = 1;
535 ProcessPendingEvents();
536 scr->flags.ignore_focus_events = 0;
538 if (!foc)
539 foc = foc2;
541 if (scr->focused_window->flags.mapped && !foc) {
542 foc = scr->focused_window;
544 if (wPreferences.focus_mode == WKF_CLICK) {
545 wSetFocusTo(scr, foc);
546 } else {
547 unsigned int mask;
548 int foo;
549 Window bar, win;
550 WWindow *tmp;
552 tmp = NULL;
553 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
554 tmp = wWindowFor(win);
557 /* If there's a window under the pointer, focus it.
558 * (we ate all other focus events above, so it's
559 * certainly not focused). Otherwise focus last
560 * focused, or the root (depending on sloppiness)
562 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
563 wSetFocusTo(scr, foc);
564 } else {
565 wSetFocusTo(scr, tmp);
570 /* We need to always arrange icons when changing workspace, even if
571 * no autoarrange icons, because else the icons in different workspaces
572 * can be superposed.
573 * This can be avoided if appicons are also workspace specific.
575 if (!wPreferences.sticky_icons)
576 wArrangeIcons(scr, False);
578 if (scr->dock)
579 wAppIconPaint(scr->dock->icon_array[0]);
581 if (scr->clip_icon) {
582 if (scr->workspaces[workspace]->clip->auto_collapse ||
583 scr->workspaces[workspace]->clip->auto_raise_lower) {
584 /* to handle enter notify. This will also */
585 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
586 XMapWindow(dpy, scr->clip_icon->icon->core->window);
587 } else {
588 wClipIconPaint(scr->clip_icon);
591 #ifdef NETWM_HINTS
592 wScreenUpdateUsableArea(scr);
593 wNETWMUpdateDesktop(scr);
594 #endif
596 showWorkspaceName(scr, workspace);
598 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
600 /* XSync(dpy, False); */
603 #ifdef VIRTUAL_DESKTOP
605 /* TODO:
607 * 1) Allow border around each window so the scrolling
608 * won't just stop at the border.
609 * 2) Make pager.
613 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
614 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
615 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
616 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
617 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
618 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
620 #ifndef HAS_BORDER
621 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
622 #endif
624 #ifndef IS_VSTUCK
625 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
626 #endif
628 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
630 static Bool cmp_gez(int i)
632 return (i >= 0);
635 static Bool cmp_gz(int i)
637 return (i > 0);
640 static WMPoint getClosestEdge(WScreen * scr, WMPoint direction, Bool(*cmp) (int))
642 WMPoint closest = wmkpoint(0, 0);
643 int closest_len = INT_MAX;
644 WWindow *wwin;
646 for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
647 if (wwin->frame->workspace == scr->current_workspace) {
648 if (!wwin->flags.miniaturized && !IS_VSTUCK(wwin) && !wwin->flags.hidden) {
649 int border = 2 * HAS_BORDER(wwin);
650 int len;
651 int x1, x2, y1, y2;
652 int head = wGetHeadForWindow(wwin);
653 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
654 WMPoint p;
656 x1 = wwin->frame_x - area.x1;
657 y1 = wwin->frame_y - area.y1;
658 x2 = wwin->frame_x + wwin->frame->core->width + border - area.x2;
659 y2 = wwin->frame_y + wwin->frame->core->height + border - area.y2;
661 p = wmkpoint(x1, y1);
662 len = vec_dot(direction, p);
663 updateMinimum(len, p, closest_len, closest);
665 p = wmkpoint(x1, y2);
666 len = vec_dot(direction, p);
667 updateMinimum(len, p, closest_len, closest);
669 p = wmkpoint(x2, y1);
670 len = vec_dot(direction, p);
671 updateMinimum(len, p, closest_len, closest);
673 p = wmkpoint(x2, y2);
674 len = vec_dot(direction, p);
675 updateMinimum(len, p, closest_len, closest);
680 return closest;
683 static void getViewPosition(WScreen * scr, int workspace, int *x, int *y)
685 *x = scr->workspaces[workspace]->view_x;
686 *y = scr->workspaces[workspace]->view_y;
689 void wWorkspaceKeyboardMoveDesktop(WScreen * scr, WMPoint direction)
691 int x, y;
692 WMPoint edge = getClosestEdge(scr, direction, cmp_gz);
693 int len = vec_dot(edge, direction);
694 WMPoint step = vec_scale(direction, len);
695 getViewPosition(scr, scr->current_workspace, &x, &y);
696 wWorkspaceSetViewport(scr, scr->current_workspace, x + step.x, y + step.y);
699 extern Cursor wCursor[WCUR_LAST];
701 static void vdMouseMoveDesktop(XEvent * event, WMPoint direction)
703 static int lock = False;
704 if (lock)
705 return;
706 lock = True;
708 Bool done = False;
709 Bool moved = True;
710 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
711 WMPoint old_pos = wmkpoint(event->xcrossing.x_root, event->xcrossing.y_root);
712 WMPoint step;
713 int x, y;
714 int resisted = 0;
716 if (XGrabPointer(dpy, event->xcrossing.window, False,
717 PointerMotionMask, GrabModeAsync, GrabModeAsync,
718 scr->root_win, wCursor[WCUR_EMPTY], CurrentTime) != GrabSuccess) {
720 /* if the grab fails, do it the old fashioned way */
721 step = vec_scale2(direction, wPreferences.vedge_hscrollspeed, wPreferences.vedge_vscrollspeed);
722 getViewPosition(scr, scr->current_workspace, &x, &y);
723 if (wWorkspaceSetViewport(scr, scr->current_workspace, x + step.x, y + step.y)) {
724 step = vec_scale(direction, 2);
725 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0,
726 event->xcrossing.x_root - step.x, event->xcrossing.y_root - step.y);
728 goto exit;
730 XSync(dpy, True);
732 if (old_pos.x < 0)
733 old_pos.x = 0;
734 if (old_pos.y < 0)
735 old_pos.y = 0;
736 if (old_pos.x > scr->scr_width)
737 old_pos.x = scr->scr_width;
738 if (old_pos.y > scr->scr_height)
739 old_pos.y = scr->scr_height;
741 while (!done) {
742 XEvent ev;
743 if (moved) {
744 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0,
745 scr->scr_width / 2, scr->scr_height / 2);
746 moved = False;
748 WMMaskEvent(dpy, PointerMotionMask, &ev);
750 switch (ev.type) {
751 case MotionNotify:
753 int step_len;
754 step = wmkpoint(ev.xmotion.x_root - scr->scr_width / 2,
755 ev.xmotion.y_root - scr->scr_height / 2);
756 step_len = vec_dot(step, direction);
757 if (step_len < 0) {
758 done = True;
759 break;
762 if (step_len > 0) {
763 Bool do_move = True;
764 int resist = wPreferences.vedge_resistance;
765 WMPoint closest;
766 int closest_len = INT_MAX;
767 if (resist) {
768 closest = getClosestEdge(scr, direction, cmp_gez);
769 closest_len = vec_dot(direction, closest);
772 if (!closest_len) {
773 resisted += step_len;
774 do_move = resisted >= resist;
775 if (do_move) {
776 closest_len = INT_MAX;
777 step_len = resisted - resist;
778 resisted = 0;
781 if (do_move) {
782 if (closest_len <= wPreferences.vedge_attraction) {
783 step = vec_scale(direction, closest_len);
784 } else {
785 step = vec_scale(direction, step_len);
788 getViewPosition(scr, scr->current_workspace, &x, &y);
789 wWorkspaceSetViewport(scr, scr->current_workspace,
790 x + step.x, y + step.y);
791 moved = True;
795 break;
799 step = vec_add(old_pos, vec_scale(direction, -1));
800 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0, step.x, step.y);
801 XUngrabPointer(dpy, CurrentTime);
803 exit:
804 lock = False;
807 static void vdHandleEnter_u(XEvent * event)
809 vdMouseMoveDesktop(event, VEC_UP);
812 static void vdHandleEnter_d(XEvent * event)
814 vdMouseMoveDesktop(event, VEC_DOWN);
817 static void vdHandleEnter_l(XEvent * event)
819 vdMouseMoveDesktop(event, VEC_LEFT);
822 static void vdHandleEnter_r(XEvent * event)
824 vdMouseMoveDesktop(event, VEC_RIGHT);
827 #define LEFT_EDGE 0x01
828 #define RIGHT_EDGE 0x02
829 #define TOP_EDGE 0x04
830 #define BOTTOM_EDGE 0x08
831 #define ALL_EDGES 0x0F
833 static void createEdges(WScreen * scr)
835 if (!scr->virtual_edges) {
836 int i, j, w;
837 int vmask;
838 XSetWindowAttributes attribs;
840 int heads = wXineramaHeads(scr);
841 int *hasEdges = (int *)wmalloc(sizeof(int) * heads);
843 int thickness = 1;
844 int nr_edges = 0;
845 int max_edges = 4 * heads;
846 int head;
847 Window *edges = (Window *) wmalloc(sizeof(Window) * max_edges);
849 for (i = 0; i < heads; ++i)
850 hasEdges[i] = ALL_EDGES;
851 for (i = 0; i < heads; ++i) {
852 WMRect i_rect = wGetRectForHead(scr, i);
853 for (j = i + 1; j < heads; ++j) {
854 WMRect j_rect = wGetRectForHead(scr, j);
856 int vlen = (WMIN(i_rect.pos.y + i_rect.size.height,
857 j_rect.pos.y + j_rect.size.height) -
858 WMAX(i_rect.pos.y, j_rect.pos.y));
860 int hlen = (WMIN(i_rect.pos.x + i_rect.size.width,
861 j_rect.pos.x + j_rect.size.width) -
862 WMAX(i_rect.pos.x, j_rect.pos.x));
864 if (vlen > 0 && hlen == 0) { /* horz alignment, vert edges touch */
865 if (i_rect.pos.x < j_rect.pos.x) { /* i left of j */
866 hasEdges[i] &= ~RIGHT_EDGE;
867 hasEdges[j] &= ~LEFT_EDGE;
868 } else { /* j left of i */
869 hasEdges[j] &= ~RIGHT_EDGE;
870 hasEdges[i] &= ~LEFT_EDGE;
872 } else if (vlen == 0 && hlen > 0) { /* vert alignment, horz edges touch */
873 if (i_rect.pos.y < j_rect.pos.y) { /* i top of j */
874 hasEdges[i] &= ~BOTTOM_EDGE;
875 hasEdges[j] &= ~TOP_EDGE;
876 } else { /* j top of i */
877 hasEdges[j] &= ~BOTTOM_EDGE;
878 hasEdges[i] &= ~TOP_EDGE;
884 for (w = 0; w < scr->workspace_count; w++) {
885 /* puts("reset workspace"); */
886 wWorkspaceSetViewport(scr, w, 0, 0);
889 vmask = CWEventMask | CWOverrideRedirect;
890 attribs.event_mask = (EnterWindowMask | LeaveWindowMask | VisibilityChangeMask);
891 attribs.override_redirect = True;
893 for (head = 0; head < wXineramaHeads(scr); ++head) {
894 WMRect rect = wGetRectForHead(scr, head);
896 if (hasEdges[head] & TOP_EDGE) {
897 edges[nr_edges] =
898 XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
899 rect.size.width, thickness, 0,
900 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
901 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_u);
902 ++nr_edges;
905 if (hasEdges[head] & BOTTOM_EDGE) {
906 edges[nr_edges] =
907 XCreateWindow(dpy, scr->root_win, rect.pos.x,
908 rect.pos.y + rect.size.height - thickness,
909 rect.size.width, thickness, 0,
910 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
911 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_d);
912 ++nr_edges;
915 if (hasEdges[head] & LEFT_EDGE) {
916 edges[nr_edges] =
917 XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
918 thickness, rect.pos.y + rect.size.height, 0,
919 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
920 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_l);
921 ++nr_edges;
924 if (hasEdges[head] & RIGHT_EDGE) {
925 edges[nr_edges] =
926 XCreateWindow(dpy, scr->root_win,
927 rect.pos.x + rect.size.width - thickness, rect.pos.y,
928 thickness, rect.size.height, 0,
929 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
930 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_r);
931 ++nr_edges;
935 scr->virtual_nr_edges = nr_edges;
936 scr->virtual_edges = edges;
938 for (i = 0; i < scr->virtual_nr_edges; ++i) {
939 XMapWindow(dpy, scr->virtual_edges[i]);
941 wWorkspaceRaiseEdge(scr);
943 wfree(hasEdges);
947 static void destroyEdges(WScreen * scr)
949 if (scr->virtual_edges) {
950 int i;
952 for (i = 0; i < scr->virtual_nr_edges; ++i) {
953 XDeleteContext(dpy, scr->virtual_edges[i], wVEdgeContext);
954 XUnmapWindow(dpy, scr->virtual_edges[i]);
955 XDestroyWindow(dpy, scr->virtual_edges[i]);
958 wfree(scr->virtual_edges);
959 scr->virtual_edges = NULL;
960 scr->virtual_nr_edges = 0;
964 void wWorkspaceUpdateEdge(WScreen * scr)
966 if (wPreferences.vdesk_enable) {
967 createEdges(scr);
968 } else {
969 destroyEdges(scr);
973 void wWorkspaceRaiseEdge(WScreen * scr)
975 static int toggle = 0;
976 int i;
978 if (!scr->virtual_edges)
979 return;
981 if (toggle) {
982 for (i = 0; i < scr->virtual_nr_edges; ++i) {
983 XRaiseWindow(dpy, scr->virtual_edges[i]);
985 } else {
986 for (i = scr->virtual_nr_edges - 1; i >= 0; --i) {
987 XRaiseWindow(dpy, scr->virtual_edges[i]);
991 toggle ^= 1;
994 void wWorkspaceLowerEdge(WScreen * scr)
996 int i;
997 for (i = 0; i < scr->virtual_nr_edges; ++i) {
998 XLowerWindow(dpy, scr->virtual_edges[i]);
1002 void wWorkspaceResizeViewport(WScreen * scr, int workspace)
1004 int x, y;
1005 getViewPosition(scr, scr->current_workspace, &x, &y);
1006 wWorkspaceSetViewport(scr, scr->current_workspace, x, y);
1009 void updateWorkspaceGeometry(WScreen * scr, int workspace, int *view_x, int *view_y)
1011 int most_left, most_right, most_top, most_bottom;
1012 WWindow *wwin;
1014 int heads = wXineramaHeads(scr);
1015 typedef int strut_t[4];
1016 strut_t *strut = (strut_t *) wmalloc(heads * sizeof(strut_t));
1017 int head, i;
1019 for (head = 0; head < heads; ++head) {
1020 WMRect rect = wGetRectForHead(scr, head);
1021 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1022 strut[head][0] = area.x1 - rect.pos.x;
1023 strut[head][1] = rect.pos.x + rect.size.width - area.x2;
1024 strut[head][2] = area.y1 - rect.pos.y;
1025 strut[head][3] = rect.pos.y + rect.size.height - area.y2;
1028 /* adjust workspace layout */
1029 wwin = scr->focused_window;
1030 most_right = 0;
1031 most_bottom = 0;
1032 most_left = scr->scr_width;
1033 most_top = scr->scr_height;
1034 for (; wwin; wwin = wwin->prev) {
1035 if (wwin->frame->workspace == workspace) {
1036 if (!wwin->flags.miniaturized && !IS_VSTUCK(wwin) && !wwin->flags.hidden) {
1038 head = wGetHeadForWindow(wwin);
1040 i = wwin->frame_x - strut[head][0];
1041 if (i < most_left) /* record positions, should this be cached? */
1042 most_left = i;
1043 i = wwin->frame_x + wwin->frame->core->width + strut[head][1];
1044 if (HAS_BORDER(wwin))
1045 i += 2;
1046 if (i > most_right)
1047 most_right = i;
1048 i = wwin->frame_y - strut[head][2];
1049 if (i < most_top)
1050 most_top = i;
1051 i = wwin->frame_y + wwin->frame->core->height + strut[head][3];
1052 if (HAS_BORDER(wwin))
1053 i += 2;
1054 if (i > most_bottom) {
1055 most_bottom = i;
1061 if (most_left > 0)
1062 most_left = 0;
1063 if (most_top > 0)
1064 most_top = 0;
1066 scr->workspaces[workspace]->width = WMAX(most_right, scr->scr_width) - WMIN(most_left, 0);
1067 scr->workspaces[workspace]->height = WMAX(most_bottom, scr->scr_height) - WMIN(most_top, 0);
1069 *view_x += -most_left - scr->workspaces[workspace]->view_x;
1070 scr->workspaces[workspace]->view_x = -most_left;
1072 *view_y += -most_top - scr->workspaces[workspace]->view_y;
1073 scr->workspaces[workspace]->view_y = -most_top;
1075 wfree(strut);
1078 typedef struct _delay_configure {
1079 WWindow *wwin;
1080 int delay_count;
1081 } _delay_configure;
1083 void sendConfigureNotify(_delay_configure * delay)
1085 WWindow *wwin;
1087 delay->delay_count--;
1088 if (!delay->delay_count) {
1089 for (wwin = delay->wwin; wwin; wwin = wwin->prev) {
1090 wWindowSynthConfigureNotify(wwin);
1095 Bool wWorkspaceSetViewport(WScreen * scr, int workspace, int view_x, int view_y)
1097 Bool adjust_flag = False;
1098 int diff_x, diff_y;
1099 static _delay_configure delay_configure = { NULL, 0 };
1100 WWorkspace *wptr;
1101 WWindow *wwin;
1103 wptr = scr->workspaces[workspace];
1105 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y); */
1107 updateWorkspaceGeometry(scr, workspace, &view_x, &view_y);
1109 if (view_x + scr->scr_width > wptr->width) {
1110 /* puts("right edge of vdesk"); */
1111 view_x = wptr->width - scr->scr_width;
1113 if (view_x < 0) {
1114 /* puts("left edge of vdesk"); */
1115 view_x = 0;
1117 if (view_y + scr->scr_height > wptr->height) {
1118 /* puts("right edge of vdesk"); */
1119 view_y = wptr->height - scr->scr_height;
1121 if (view_y < 0) {
1122 /* puts("left edge of vdesk"); */
1123 view_y = 0;
1126 diff_x = wptr->view_x - view_x;
1127 diff_y = wptr->view_y - view_y;
1128 if (!diff_x && !diff_y)
1129 return False;
1131 wptr->view_x = view_x;
1132 wptr->view_y = view_y;
1134 #ifdef NETWM_HINTS
1135 wNETWMUpdateDesktop(scr);
1136 #endif
1138 for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
1139 if (wwin->frame->workspace == workspace && !IS_VSTUCK(wwin)) {
1140 wWindowMove(wwin, wwin->frame_x + diff_x, wwin->frame_y + diff_y);
1141 adjust_flag = True;
1144 if (1) { /* if delay */
1145 delay_configure.delay_count++;
1146 delay_configure.wwin = scr->focused_window;
1147 WMAddTimerHandler(200, (WMCallback *) sendConfigureNotify, &delay_configure);
1150 return adjust_flag;
1153 #endif
1155 static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
1157 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
1160 static void deleteWSCommand(WMenu * menu, WMenuEntry * entry)
1162 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
1165 static void newWSCommand(WMenu * menu, WMenuEntry * foo)
1167 int ws;
1169 ws = wWorkspaceNew(menu->frame->screen_ptr);
1170 /* autochange workspace */
1171 if (ws >= 0)
1172 wWorkspaceChange(menu->frame->screen_ptr, ws);
1175 if (ws<9) {
1176 int kcode;
1177 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1178 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1179 entry->rtext =
1180 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1182 } */
1185 static char *cropline(char *line)
1187 char *start, *end;
1189 if (strlen(line) == 0)
1190 return line;
1192 start = line;
1193 end = &(line[strlen(line)]) - 1;
1194 while (isspace(*line) && *line != 0)
1195 line++;
1196 while (isspace(*end) && end != line) {
1197 *end = 0;
1198 end--;
1200 return line;
1203 void wWorkspaceRename(WScreen * scr, int workspace, char *name)
1205 char buf[MAX_WORKSPACENAME_WIDTH + 1];
1206 char *tmp;
1208 if (workspace >= scr->workspace_count)
1209 return;
1211 /* trim white spaces */
1212 tmp = cropline(name);
1214 if (strlen(tmp) == 0) {
1215 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
1216 } else {
1217 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
1219 buf[MAX_WORKSPACENAME_WIDTH] = 0;
1221 /* update workspace */
1222 wfree(scr->workspaces[workspace]->name);
1223 scr->workspaces[workspace]->name = wstrdup(buf);
1225 if (scr->clip_ws_menu) {
1226 if (strcmp(scr->clip_ws_menu->entries[workspace + 2]->text, buf) != 0) {
1227 wfree(scr->clip_ws_menu->entries[workspace + 2]->text);
1228 scr->clip_ws_menu->entries[workspace + 2]->text = wstrdup(buf);
1229 wMenuRealize(scr->clip_ws_menu);
1232 if (scr->workspace_menu) {
1233 if (strcmp(scr->workspace_menu->entries[workspace + 2]->text, buf) != 0) {
1234 wfree(scr->workspace_menu->entries[workspace + 2]->text);
1235 scr->workspace_menu->entries[workspace + 2]->text = wstrdup(buf);
1236 wMenuRealize(scr->workspace_menu);
1240 if (scr->clip_icon)
1241 wClipIconPaint(scr->clip_icon);
1243 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
1246 /* callback for when menu entry is edited */
1247 static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
1249 char *tmp;
1251 tmp = entry->text;
1252 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
1255 WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
1257 WMenu *wsmenu;
1259 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
1260 if (!wsmenu) {
1261 wwarning(_("could not create Workspace menu"));
1262 return NULL;
1265 /* callback to be called when an entry is edited */
1266 wsmenu->on_edit = onMenuEntryEdited;
1268 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
1269 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
1271 return wsmenu;
1274 void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
1276 int i;
1277 long ws;
1278 char title[MAX_WORKSPACENAME_WIDTH + 1];
1279 WMenuEntry *entry;
1280 int tmp;
1282 if (!menu)
1283 return;
1285 if (menu->entry_no < scr->workspace_count + 2) {
1286 /* new workspace(s) added */
1287 i = scr->workspace_count - (menu->entry_no - 2);
1288 ws = menu->entry_no - 2;
1289 while (i > 0) {
1290 strncpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
1292 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
1293 entry->flags.indicator = 1;
1294 entry->flags.editable = 1;
1296 i--;
1297 ws++;
1299 } else if (menu->entry_no > scr->workspace_count + 2) {
1300 /* removed workspace(s) */
1301 for (i = menu->entry_no - 1; i >= scr->workspace_count + 2; i--) {
1302 wMenuRemoveItem(menu, i);
1305 wMenuRealize(menu);
1307 for (i = 0; i < scr->workspace_count; i++) {
1308 menu->entries[i + 2]->flags.indicator_on = 0;
1310 menu->entries[scr->current_workspace + 2]->flags.indicator_on = 1;
1312 /* don't let user destroy current workspace */
1313 if (scr->current_workspace == scr->workspace_count - 1) {
1314 wMenuSetEnabled(menu, 1, False);
1315 } else {
1316 wMenuSetEnabled(menu, 1, True);
1319 tmp = menu->frame->top_width + 5;
1320 /* if menu got unreachable, bring it to a visible place */
1321 if (menu->frame_x < tmp - (int)menu->frame->core->width)
1322 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
1324 wMenuPaint(menu);
1327 void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
1329 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
1330 int i;
1332 make_keys();
1334 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
1335 parr = WMCreatePLArray(NULL);
1336 for (i = 0; i < scr->workspace_count; i++) {
1337 pstr = WMCreatePLString(scr->workspaces[i]->name);
1338 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
1339 WMReleasePropList(pstr);
1340 if (!wPreferences.flags.noclip) {
1341 pstr = wClipSaveWorkspaceState(scr, i);
1342 WMPutInPLDictionary(wks_state, dClip, pstr);
1343 WMReleasePropList(pstr);
1344 } else if (old_wks_state != NULL) {
1345 if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
1346 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
1347 WMPutInPLDictionary(wks_state, dClip, bar);
1351 WMAddToPLArray(parr, wks_state);
1352 WMReleasePropList(wks_state);
1354 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
1355 WMReleasePropList(parr);
1358 void wWorkspaceRestoreState(WScreen * scr)
1360 WMPropList *parr, *pstr, *wks_state, *clip_state;
1361 int i, j, wscount;
1363 make_keys();
1365 if (scr->session_state == NULL)
1366 return;
1368 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
1370 if (!parr)
1371 return;
1373 wscount = scr->workspace_count;
1374 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
1375 wks_state = WMGetFromPLArray(parr, i);
1376 if (WMIsPLDictionary(wks_state))
1377 pstr = WMGetFromPLDictionary(wks_state, dName);
1378 else
1379 pstr = wks_state;
1380 if (i >= scr->workspace_count)
1381 wWorkspaceNew(scr);
1382 if (scr->workspace_menu) {
1383 wfree(scr->workspace_menu->entries[i + 2]->text);
1384 scr->workspace_menu->entries[i + 2]->text = wstrdup(WMGetFromPLString(pstr));
1385 scr->workspace_menu->flags.realized = 0;
1387 wfree(scr->workspaces[i]->name);
1388 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
1389 if (!wPreferences.flags.noclip) {
1390 clip_state = WMGetFromPLDictionary(wks_state, dClip);
1391 if (scr->workspaces[i]->clip)
1392 wDockDestroy(scr->workspaces[i]->clip);
1393 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
1394 if (i > 0)
1395 wDockHideIcons(scr->workspaces[i]->clip);
1397 /* We set the global icons here, because scr->workspaces[i]->clip
1398 * was not valid in wDockRestoreState().
1399 * There we only set icon->omnipresent to know which icons we
1400 * need to set here.
1402 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
1403 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
1405 if (aicon && aicon->omnipresent) {
1406 aicon->omnipresent = 0;
1407 wClipMakeIconOmnipresent(aicon, True);
1408 XMapWindow(dpy, aicon->icon->core->window);
1413 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);