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,
25 #include <X11/Xutil.h>
27 #include <X11/extensions/shape.h>
39 #include "WindowMaker.h"
46 #include "application.h"
49 #include "workspace.h"
54 #define MAX_SHORTCUT_LENGTH 32
55 #define WORKSPACE_NAME_DISPLAY_PADDING 32
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
;
66 static void make_keys()
68 if (dWorkspaces
!= NULL
)
71 dWorkspaces
= WMCreatePLString("Workspaces");
72 dName
= WMCreatePLString("Name");
73 dClip
= WMCreatePLString("Clip");
76 void wWorkspaceMake(WScreen
* scr
, int count
)
84 int wWorkspaceNew(WScreen
* scr
)
86 WWorkspace
*wspace
, **list
;
89 if (scr
->workspace_count
< MAX_WORKSPACES
) {
90 scr
->workspace_count
++;
92 wspace
= wmalloc(sizeof(WWorkspace
));
96 wspace
->name
= wmalloc(strlen(_("Workspace %i")) + 8);
97 sprintf(wspace
->name
, _("Workspace %i"), scr
->workspace_count
);
100 if (!wPreferences
.flags
.noclip
) {
101 wspace
->clip
= wDockCreate(scr
, WM_CLIP
);
105 list
= wmalloc(sizeof(WWorkspace
*) * scr
->workspace_count
);
107 for (i
= 0; i
< scr
->workspace_count
- 1; i
++) {
108 list
[i
] = scr
->workspaces
[i
];
112 wfree(scr
->workspaces
);
113 scr
->workspaces
= list
;
115 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
116 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
117 #ifdef VIRTUAL_DESKTOP
118 wspace
->view_x
= wspace
->view_y
= 0;
119 wspace
->height
= scr
->scr_height
;
120 wspace
->width
= scr
->scr_width
;
122 wNETWMUpdateDesktop(scr
);
123 WMPostNotificationName(WMNWorkspaceCreated
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
126 return scr
->workspace_count
- 1;
132 Bool
wWorkspaceDelete(WScreen
* scr
, int workspace
)
141 /* verify if workspace is in use by some window */
142 tmp
= scr
->focused_window
;
144 if (!IS_OMNIPRESENT(tmp
) && tmp
->frame
->workspace
== workspace
)
149 if (!wPreferences
.flags
.noclip
) {
150 wDockDestroy(scr
->workspaces
[workspace
]->clip
);
151 scr
->workspaces
[workspace
]->clip
= NULL
;
154 list
= wmalloc(sizeof(WWorkspace
*) * (scr
->workspace_count
- 1));
156 for (i
= 0; i
< scr
->workspace_count
; i
++) {
157 if (i
!= workspace
) {
158 list
[j
++] = scr
->workspaces
[i
];
160 if (scr
->workspaces
[i
]->name
)
161 wfree(scr
->workspaces
[i
]->name
);
162 wfree(scr
->workspaces
[i
]);
165 wfree(scr
->workspaces
);
166 scr
->workspaces
= list
;
168 scr
->workspace_count
--;
171 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
172 /* clip workspace menu */
173 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
175 /* update also window menu */
176 if (scr
->workspace_submenu
) {
177 WMenu
*menu
= scr
->workspace_submenu
;
180 while (i
> scr
->workspace_count
)
181 wMenuRemoveItem(menu
, --i
);
185 if (scr
->clip_submenu
) {
186 WMenu
*menu
= scr
->clip_submenu
;
189 while (i
> scr
->workspace_count
)
190 wMenuRemoveItem(menu
, --i
);
193 wNETWMUpdateDesktop(scr
);
194 WMPostNotificationName(WMNWorkspaceDestroyed
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
196 if (scr
->current_workspace
>= scr
->workspace_count
)
197 wWorkspaceChange(scr
, scr
->workspace_count
- 1);
202 typedef struct WorkspaceNameData
{
209 static void hideWorkspaceName(void *data
)
211 WScreen
*scr
= (WScreen
*) data
;
213 if (!scr
->workspace_name_data
|| scr
->workspace_name_data
->count
== 0
214 || time(NULL
) > scr
->workspace_name_data
->timeout
) {
215 XUnmapWindow(dpy
, scr
->workspace_name
);
217 if (scr
->workspace_name_data
) {
218 RReleaseImage(scr
->workspace_name_data
->back
);
219 RReleaseImage(scr
->workspace_name_data
->text
);
220 wfree(scr
->workspace_name_data
);
222 scr
->workspace_name_data
= NULL
;
224 scr
->workspace_name_timer
= NULL
;
226 RImage
*img
= RCloneImage(scr
->workspace_name_data
->back
);
229 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
231 RCombineImagesWithOpaqueness(img
, scr
->workspace_name_data
->text
,
232 scr
->workspace_name_data
->count
* 255 / 10);
234 RConvertImage(scr
->rcontext
, img
, &pix
);
238 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, pix
);
239 XClearWindow(dpy
, scr
->workspace_name
);
240 XFreePixmap(dpy
, pix
);
243 scr
->workspace_name_data
->count
--;
247 static void showWorkspaceName(WScreen
* scr
, int workspace
)
249 WorkspaceNameData
*data
;
254 char *name
= scr
->workspaces
[workspace
]->name
;
255 int len
= strlen(name
);
263 if (wPreferences
.workspace_name_display_position
== WD_NONE
|| scr
->workspace_count
< 2) {
267 if (scr
->workspace_name_timer
) {
268 WMDeleteTimerHandler(scr
->workspace_name_timer
);
269 XUnmapWindow(dpy
, scr
->workspace_name
);
272 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
, hideWorkspaceName
, scr
);
274 if (scr
->workspace_name_data
) {
275 RReleaseImage(scr
->workspace_name_data
->back
);
276 RReleaseImage(scr
->workspace_name_data
->text
);
277 wfree(scr
->workspace_name_data
);
280 data
= wmalloc(sizeof(WorkspaceNameData
));
283 w
= WMWidthOfString(scr
->workspace_name_font
, name
, len
);
284 h
= WMFontHeight(scr
->workspace_name_font
);
287 head
= wGetHeadForPointerLocation(scr
);
288 rect
= wGetRectForHead(scr
, head
);
289 if (scr
->xine_info
.count
) {
290 xx
= rect
.pos
.x
+ (scr
->xine_info
.screens
[head
].size
.width
- (w
+ 4)) / 2;
291 yy
= rect
.pos
.y
+ (scr
->xine_info
.screens
[head
].size
.height
- (h
+ 4)) / 2;
294 xx
= (scr
->scr_width
- (w
+ 4)) / 2;
295 yy
= (scr
->scr_height
- (h
+ 4)) / 2;
299 switch (wPreferences
.workspace_name_display_position
) {
304 px
= (scr
->scr_width
- (w
+ 4)) / 2;
306 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
312 px
= (scr
->scr_width
- (w
+ 4)) / 2;
314 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
317 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
318 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
321 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
322 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
325 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
326 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
329 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
330 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
338 px
= (scr
->scr_width
- (w
+ 4)) / 2;
339 py
= (scr
->scr_height
- (h
+ 4)) / 2;
343 XResizeWindow(dpy
, scr
->workspace_name
, w
+ 4, h
+ 4);
344 XMoveWindow(dpy
, scr
->workspace_name
, px
, py
);
346 text
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, scr
->w_depth
);
347 mask
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, 1);
349 /*XSetForeground(dpy, scr->mono_gc, 0);
350 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
352 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
354 for (x
= 0; x
<= 4; x
++) {
355 for (y
= 0; y
<= 4; y
++) {
356 WMDrawString(scr
->wmscreen
, text
, scr
->white
, 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
, 2, 2, name
, len
);
373 XShapeCombineMask(dpy
, scr
->workspace_name
, ShapeBounding
, 0, 0, mask
, ShapeSet
);
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
);
384 XMapRaised(dpy
, scr
->workspace_name
);
390 ximg
= RGetXImage(scr
->rcontext
, scr
->root_win
, px
, py
, data
->text
->width
, data
->text
->height
);
392 if (!ximg
|| !ximg
->image
) {
396 XMapRaised(dpy
, scr
->workspace_name
);
399 data
->back
= RCreateImageFromXImage(scr
->rcontext
, ximg
->image
, NULL
);
400 RDestroyXImage(scr
->rcontext
, ximg
);
408 /* set a timeout for the effect */
409 data
->timeout
= time(NULL
) + 2 + (WORKSPACE_NAME_DELAY
+ WORKSPACE_NAME_FADE_DELAY
* data
->count
) / 1000;
411 scr
->workspace_name_data
= data
;
416 if (scr
->workspace_name_timer
)
417 WMDeleteTimerHandler(scr
->workspace_name_timer
);
420 RReleaseImage(data
->text
);
422 RReleaseImage(data
->back
);
425 scr
->workspace_name_data
= NULL
;
427 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
+
428 10 * WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
431 void wWorkspaceChange(WScreen
*scr
, int workspace
)
433 if (scr
->flags
.startup
|| scr
->flags
.startup2
|| scr
->flags
.ignore_focus_events
)
436 if (workspace
!= scr
->current_workspace
)
437 wWorkspaceForceChange(scr
, workspace
);
440 void wWorkspaceRelativeChange(WScreen
* scr
, int amount
)
444 w
= scr
->current_workspace
+ amount
;
448 wWorkspaceChange(scr
, w
);
449 } else if (wPreferences
.ws_cycle
) {
450 wWorkspaceChange(scr
, scr
->workspace_count
+ w
);
452 } else if (amount
> 0) {
453 if (w
< scr
->workspace_count
) {
454 wWorkspaceChange(scr
, w
);
455 } else if (wPreferences
.ws_advance
) {
456 wWorkspaceChange(scr
, WMIN(w
, MAX_WORKSPACES
- 1));
457 } else if (wPreferences
.ws_cycle
) {
458 wWorkspaceChange(scr
, w
% scr
->workspace_count
);
463 void wWorkspaceForceChange(WScreen
* scr
, int workspace
)
465 WWindow
*tmp
, *foc
= NULL
, *foc2
= NULL
;
467 if (workspace
>= MAX_WORKSPACES
|| workspace
< 0)
470 SendHelperMessage(scr
, 'C', workspace
+ 1, NULL
);
472 if (workspace
> scr
->workspace_count
- 1) {
473 wWorkspaceMake(scr
, workspace
- scr
->workspace_count
+ 1);
476 wClipUpdateForWorkspaceChange(scr
, workspace
);
478 scr
->current_workspace
= workspace
;
480 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
482 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
484 if ((tmp
= scr
->focused_window
) != NULL
) {
485 if ((IS_OMNIPRESENT(tmp
) && (tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
486 !WFLAGP(tmp
, no_focusable
)) || tmp
->flags
.changing_workspace
) {
490 /* foc2 = tmp; will fix annoyance with gnome panel
491 * but will create annoyance for every other application
494 if (tmp
->frame
->workspace
!= workspace
&& !tmp
->flags
.selected
) {
495 /* unmap windows not on this workspace */
496 if ((tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
497 !IS_OMNIPRESENT(tmp
) && !tmp
->flags
.changing_workspace
) {
500 /* also unmap miniwindows not on this workspace */
501 if (!wPreferences
.sticky_icons
&& tmp
->flags
.miniaturized
&&
502 tmp
->icon
&& !IS_OMNIPRESENT(tmp
)) {
503 XUnmapWindow(dpy
, tmp
->icon
->core
->window
);
504 tmp
->icon
->mapped
= 0;
506 /* update current workspace of omnipresent windows */
507 if (IS_OMNIPRESENT(tmp
)) {
508 WApplication
*wapp
= wApplicationOf(tmp
->main_window
);
510 tmp
->frame
->workspace
= workspace
;
513 wapp
->last_workspace
= workspace
;
515 if (!foc2
&& (tmp
->flags
.mapped
|| tmp
->flags
.shaded
)) {
520 /* change selected windows' workspace */
521 if (tmp
->flags
.selected
) {
522 wWindowChangeWorkspace(tmp
, workspace
);
523 if (!tmp
->flags
.miniaturized
&& !foc
) {
527 if (!tmp
->flags
.hidden
) {
528 if (!(tmp
->flags
.mapped
|| tmp
->flags
.miniaturized
)) {
529 /* remap windows that are on this workspace */
531 if (!foc
&& !WFLAGP(tmp
, no_focusable
)) {
535 /* Also map miniwindow if not omnipresent */
536 if (!wPreferences
.sticky_icons
&&
537 tmp
->flags
.miniaturized
&& !IS_OMNIPRESENT(tmp
) && tmp
->icon
) {
538 tmp
->icon
->mapped
= 1;
539 XMapWindow(dpy
, tmp
->icon
->core
->window
);
547 /* Gobble up events unleashed by our mapping & unmapping.
548 * These may trigger various grab-initiated focus &
549 * crossing events. However, we don't care about them,
550 * and ignore their focus implications altogether to avoid
553 scr
->flags
.ignore_focus_events
= 1;
554 ProcessPendingEvents();
555 scr
->flags
.ignore_focus_events
= 0;
560 if (scr
->focused_window
->flags
.mapped
&& !foc
) {
561 foc
= scr
->focused_window
;
563 if (wPreferences
.focus_mode
== WKF_CLICK
) {
564 wSetFocusTo(scr
, foc
);
572 if (XQueryPointer(dpy
, scr
->root_win
, &bar
, &win
, &foo
, &foo
, &foo
, &foo
, &mask
)) {
573 tmp
= wWindowFor(win
);
576 /* If there's a window under the pointer, focus it.
577 * (we ate all other focus events above, so it's
578 * certainly not focused). Otherwise focus last
579 * focused, or the root (depending on sloppiness)
581 if (!tmp
&& wPreferences
.focus_mode
== WKF_SLOPPY
) {
582 wSetFocusTo(scr
, foc
);
584 wSetFocusTo(scr
, tmp
);
589 /* We need to always arrange icons when changing workspace, even if
590 * no autoarrange icons, because else the icons in different workspaces
592 * This can be avoided if appicons are also workspace specific.
594 if (!wPreferences
.sticky_icons
)
595 wArrangeIcons(scr
, False
);
598 wAppIconPaint(scr
->dock
->icon_array
[0]);
600 if (scr
->clip_icon
) {
601 if (scr
->workspaces
[workspace
]->clip
->auto_collapse
||
602 scr
->workspaces
[workspace
]->clip
->auto_raise_lower
) {
603 /* to handle enter notify. This will also */
604 XUnmapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
605 XMapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
607 wClipIconPaint(scr
->clip_icon
);
610 wScreenUpdateUsableArea(scr
);
611 wNETWMUpdateDesktop(scr
);
612 showWorkspaceName(scr
, workspace
);
614 WMPostNotificationName(WMNWorkspaceChanged
, scr
, (void *)(uintptr_t) workspace
);
616 /* XSync(dpy, False); */
619 #ifdef VIRTUAL_DESKTOP
623 * 1) Allow border around each window so the scrolling
624 * won't just stop at the border.
629 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
630 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
631 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
632 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
633 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
634 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
637 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
641 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
644 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
646 static Bool
cmp_gez(int i
)
651 static Bool
cmp_gz(int i
)
656 static WMPoint
getClosestEdge(WScreen
* scr
, WMPoint direction
, Bool(*cmp
) (int))
658 WMPoint closest
= wmkpoint(0, 0);
659 int closest_len
= INT_MAX
;
662 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
663 if (wwin
->frame
->workspace
== scr
->current_workspace
) {
664 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) && !wwin
->flags
.hidden
) {
665 int border
= 2 * HAS_BORDER(wwin
);
668 int head
= wGetHeadForWindow(wwin
);
669 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
672 x1
= wwin
->frame_x
- area
.x1
;
673 y1
= wwin
->frame_y
- area
.y1
;
674 x2
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ border
- area
.x2
;
675 y2
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ border
- area
.y2
;
677 p
= wmkpoint(x1
, y1
);
678 len
= vec_dot(direction
, p
);
679 updateMinimum(len
, p
, closest_len
, closest
);
681 p
= wmkpoint(x1
, y2
);
682 len
= vec_dot(direction
, p
);
683 updateMinimum(len
, p
, closest_len
, closest
);
685 p
= wmkpoint(x2
, y1
);
686 len
= vec_dot(direction
, p
);
687 updateMinimum(len
, p
, closest_len
, closest
);
689 p
= wmkpoint(x2
, y2
);
690 len
= vec_dot(direction
, p
);
691 updateMinimum(len
, p
, closest_len
, closest
);
699 static void getViewPosition(WScreen
* scr
, int workspace
, int *x
, int *y
)
701 *x
= scr
->workspaces
[workspace
]->view_x
;
702 *y
= scr
->workspaces
[workspace
]->view_y
;
705 void wWorkspaceKeyboardMoveDesktop(WScreen
* scr
, WMPoint direction
)
708 WMPoint edge
= getClosestEdge(scr
, direction
, cmp_gz
);
709 int len
= vec_dot(edge
, direction
);
710 WMPoint step
= vec_scale(direction
, len
);
711 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
712 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
+ step
.x
, y
+ step
.y
);
715 extern Cursor wCursor
[WCUR_LAST
];
717 static void vdMouseMoveDesktop(XEvent
* event
, WMPoint direction
)
719 static int lock
= False
;
726 WScreen
*scr
= wScreenForRootWindow(event
->xcrossing
.root
);
727 WMPoint old_pos
= wmkpoint(event
->xcrossing
.x_root
, event
->xcrossing
.y_root
);
732 if (XGrabPointer(dpy
, event
->xcrossing
.window
, False
,
733 PointerMotionMask
, GrabModeAsync
, GrabModeAsync
,
734 scr
->root_win
, wCursor
[WCUR_EMPTY
], CurrentTime
) != GrabSuccess
) {
736 /* if the grab fails, do it the old fashioned way */
737 step
= vec_scale2(direction
, wPreferences
.vedge_hscrollspeed
, wPreferences
.vedge_vscrollspeed
);
738 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
739 if (wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
+ step
.x
, y
+ step
.y
)) {
740 step
= vec_scale(direction
, 2);
741 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
742 event
->xcrossing
.x_root
- step
.x
, event
->xcrossing
.y_root
- step
.y
);
752 if (old_pos
.x
> scr
->scr_width
)
753 old_pos
.x
= scr
->scr_width
;
754 if (old_pos
.y
> scr
->scr_height
)
755 old_pos
.y
= scr
->scr_height
;
760 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
761 scr
->scr_width
/ 2, scr
->scr_height
/ 2);
764 WMMaskEvent(dpy
, PointerMotionMask
, &ev
);
770 step
= wmkpoint(ev
.xmotion
.x_root
- scr
->scr_width
/ 2,
771 ev
.xmotion
.y_root
- scr
->scr_height
/ 2);
772 step_len
= vec_dot(step
, direction
);
780 int resist
= wPreferences
.vedge_resistance
;
782 int closest_len
= INT_MAX
;
784 closest
= getClosestEdge(scr
, direction
, cmp_gez
);
785 closest_len
= vec_dot(direction
, closest
);
789 resisted
+= step_len
;
790 do_move
= resisted
>= resist
;
792 closest_len
= INT_MAX
;
793 step_len
= resisted
- resist
;
798 if (closest_len
<= wPreferences
.vedge_attraction
) {
799 step
= vec_scale(direction
, closest_len
);
801 step
= vec_scale(direction
, step_len
);
804 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
805 wWorkspaceSetViewport(scr
, scr
->current_workspace
,
806 x
+ step
.x
, y
+ step
.y
);
815 step
= vec_add(old_pos
, vec_scale(direction
, -1));
816 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0, step
.x
, step
.y
);
817 XUngrabPointer(dpy
, CurrentTime
);
823 static void vdHandleEnter_u(XEvent
* event
)
825 vdMouseMoveDesktop(event
, VEC_UP
);
828 static void vdHandleEnter_d(XEvent
* event
)
830 vdMouseMoveDesktop(event
, VEC_DOWN
);
833 static void vdHandleEnter_l(XEvent
* event
)
835 vdMouseMoveDesktop(event
, VEC_LEFT
);
838 static void vdHandleEnter_r(XEvent
* event
)
840 vdMouseMoveDesktop(event
, VEC_RIGHT
);
843 #define LEFT_EDGE 0x01
844 #define RIGHT_EDGE 0x02
845 #define TOP_EDGE 0x04
846 #define BOTTOM_EDGE 0x08
847 #define ALL_EDGES 0x0F
849 static void createEdges(WScreen
* scr
)
851 if (!scr
->virtual_edges
) {
854 XSetWindowAttributes attribs
;
856 int heads
= wXineramaHeads(scr
);
857 int *hasEdges
= (int *)wmalloc(sizeof(int) * heads
);
861 int max_edges
= 4 * heads
;
863 Window
*edges
= (Window
*) wmalloc(sizeof(Window
) * max_edges
);
865 for (i
= 0; i
< heads
; ++i
)
866 hasEdges
[i
] = ALL_EDGES
;
867 for (i
= 0; i
< heads
; ++i
) {
868 WMRect i_rect
= wGetRectForHead(scr
, i
);
869 for (j
= i
+ 1; j
< heads
; ++j
) {
870 WMRect j_rect
= wGetRectForHead(scr
, j
);
872 int vlen
= (WMIN(i_rect
.pos
.y
+ i_rect
.size
.height
,
873 j_rect
.pos
.y
+ j_rect
.size
.height
) -
874 WMAX(i_rect
.pos
.y
, j_rect
.pos
.y
));
876 int hlen
= (WMIN(i_rect
.pos
.x
+ i_rect
.size
.width
,
877 j_rect
.pos
.x
+ j_rect
.size
.width
) -
878 WMAX(i_rect
.pos
.x
, j_rect
.pos
.x
));
880 if (vlen
> 0 && hlen
== 0) { /* horz alignment, vert edges touch */
881 if (i_rect
.pos
.x
< j_rect
.pos
.x
) { /* i left of j */
882 hasEdges
[i
] &= ~RIGHT_EDGE
;
883 hasEdges
[j
] &= ~LEFT_EDGE
;
884 } else { /* j left of i */
885 hasEdges
[j
] &= ~RIGHT_EDGE
;
886 hasEdges
[i
] &= ~LEFT_EDGE
;
888 } else if (vlen
== 0 && hlen
> 0) { /* vert alignment, horz edges touch */
889 if (i_rect
.pos
.y
< j_rect
.pos
.y
) { /* i top of j */
890 hasEdges
[i
] &= ~BOTTOM_EDGE
;
891 hasEdges
[j
] &= ~TOP_EDGE
;
892 } else { /* j top of i */
893 hasEdges
[j
] &= ~BOTTOM_EDGE
;
894 hasEdges
[i
] &= ~TOP_EDGE
;
900 for (w
= 0; w
< scr
->workspace_count
; w
++) {
901 /* puts("reset workspace"); */
902 wWorkspaceSetViewport(scr
, w
, 0, 0);
905 vmask
= CWEventMask
| CWOverrideRedirect
;
906 attribs
.event_mask
= (EnterWindowMask
| LeaveWindowMask
| VisibilityChangeMask
);
907 attribs
.override_redirect
= True
;
909 for (head
= 0; head
< wXineramaHeads(scr
); ++head
) {
910 WMRect rect
= wGetRectForHead(scr
, head
);
912 if (hasEdges
[head
] & TOP_EDGE
) {
914 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
915 rect
.size
.width
, thickness
, 0,
916 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
917 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_u
);
921 if (hasEdges
[head
] & BOTTOM_EDGE
) {
923 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
,
924 rect
.pos
.y
+ rect
.size
.height
- thickness
,
925 rect
.size
.width
, thickness
, 0,
926 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
927 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_d
);
931 if (hasEdges
[head
] & LEFT_EDGE
) {
933 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
934 thickness
, rect
.pos
.y
+ rect
.size
.height
, 0,
935 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
936 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_l
);
940 if (hasEdges
[head
] & RIGHT_EDGE
) {
942 XCreateWindow(dpy
, scr
->root_win
,
943 rect
.pos
.x
+ rect
.size
.width
- thickness
, rect
.pos
.y
,
944 thickness
, rect
.size
.height
, 0,
945 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
946 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_r
);
951 scr
->virtual_nr_edges
= nr_edges
;
952 scr
->virtual_edges
= edges
;
954 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
955 XMapWindow(dpy
, scr
->virtual_edges
[i
]);
957 wWorkspaceRaiseEdge(scr
);
963 static void destroyEdges(WScreen
* scr
)
965 if (scr
->virtual_edges
) {
968 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
969 XDeleteContext(dpy
, scr
->virtual_edges
[i
], wVEdgeContext
);
970 XUnmapWindow(dpy
, scr
->virtual_edges
[i
]);
971 XDestroyWindow(dpy
, scr
->virtual_edges
[i
]);
974 wfree(scr
->virtual_edges
);
975 scr
->virtual_edges
= NULL
;
976 scr
->virtual_nr_edges
= 0;
980 void wWorkspaceUpdateEdge(WScreen
* scr
)
982 if (wPreferences
.vdesk_enable
) {
989 void wWorkspaceRaiseEdge(WScreen
* scr
)
991 static int toggle
= 0;
994 if (!scr
->virtual_edges
)
998 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
999 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1002 for (i
= scr
->virtual_nr_edges
- 1; i
>= 0; --i
) {
1003 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1010 void wWorkspaceLowerEdge(WScreen
* scr
)
1013 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
1014 XLowerWindow(dpy
, scr
->virtual_edges
[i
]);
1018 void wWorkspaceResizeViewport(WScreen
* scr
, int workspace
)
1021 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
1022 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
, y
);
1025 void updateWorkspaceGeometry(WScreen
* scr
, int workspace
, int *view_x
, int *view_y
)
1027 int most_left
, most_right
, most_top
, most_bottom
;
1030 int heads
= wXineramaHeads(scr
);
1031 typedef int strut_t
[4];
1032 strut_t
*strut
= (strut_t
*) wmalloc(heads
* sizeof(strut_t
));
1035 for (head
= 0; head
< heads
; ++head
) {
1036 WMRect rect
= wGetRectForHead(scr
, head
);
1037 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
1038 strut
[head
][0] = area
.x1
- rect
.pos
.x
;
1039 strut
[head
][1] = rect
.pos
.x
+ rect
.size
.width
- area
.x2
;
1040 strut
[head
][2] = area
.y1
- rect
.pos
.y
;
1041 strut
[head
][3] = rect
.pos
.y
+ rect
.size
.height
- area
.y2
;
1044 /* adjust workspace layout */
1045 wwin
= scr
->focused_window
;
1048 most_left
= scr
->scr_width
;
1049 most_top
= scr
->scr_height
;
1050 for (; wwin
; wwin
= wwin
->prev
) {
1051 if (wwin
->frame
->workspace
== workspace
) {
1052 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) && !wwin
->flags
.hidden
) {
1054 head
= wGetHeadForWindow(wwin
);
1056 i
= wwin
->frame_x
- strut
[head
][0];
1057 if (i
< most_left
) /* record positions, should this be cached? */
1059 i
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ strut
[head
][1];
1060 if (HAS_BORDER(wwin
))
1064 i
= wwin
->frame_y
- strut
[head
][2];
1067 i
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ strut
[head
][3];
1068 if (HAS_BORDER(wwin
))
1070 if (i
> most_bottom
) {
1082 scr
->workspaces
[workspace
]->width
= WMAX(most_right
, scr
->scr_width
) - WMIN(most_left
, 0);
1083 scr
->workspaces
[workspace
]->height
= WMAX(most_bottom
, scr
->scr_height
) - WMIN(most_top
, 0);
1085 *view_x
+= -most_left
- scr
->workspaces
[workspace
]->view_x
;
1086 scr
->workspaces
[workspace
]->view_x
= -most_left
;
1088 *view_y
+= -most_top
- scr
->workspaces
[workspace
]->view_y
;
1089 scr
->workspaces
[workspace
]->view_y
= -most_top
;
1094 typedef struct _delay_configure
{
1099 void sendConfigureNotify(_delay_configure
* delay
)
1103 delay
->delay_count
--;
1104 if (!delay
->delay_count
) {
1105 for (wwin
= delay
->wwin
; wwin
; wwin
= wwin
->prev
) {
1106 wWindowSynthConfigureNotify(wwin
);
1111 Bool
wWorkspaceSetViewport(WScreen
* scr
, int workspace
, int view_x
, int view_y
)
1113 Bool adjust_flag
= False
;
1115 static _delay_configure delay_configure
= { NULL
, 0 };
1119 wptr
= scr
->workspaces
[workspace
];
1121 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y); */
1123 updateWorkspaceGeometry(scr
, workspace
, &view_x
, &view_y
);
1125 if (view_x
+ scr
->scr_width
> wptr
->width
) {
1126 /* puts("right edge of vdesk"); */
1127 view_x
= wptr
->width
- scr
->scr_width
;
1130 /* puts("left edge of vdesk"); */
1133 if (view_y
+ scr
->scr_height
> wptr
->height
) {
1134 /* puts("right edge of vdesk"); */
1135 view_y
= wptr
->height
- scr
->scr_height
;
1138 /* puts("left edge of vdesk"); */
1142 diff_x
= wptr
->view_x
- view_x
;
1143 diff_y
= wptr
->view_y
- view_y
;
1144 if (!diff_x
&& !diff_y
)
1147 wptr
->view_x
= view_x
;
1148 wptr
->view_y
= view_y
;
1150 wNETWMUpdateDesktop(scr
);
1152 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
1153 if (wwin
->frame
->workspace
== workspace
&& !IS_VSTUCK(wwin
)) {
1154 wWindowMove(wwin
, wwin
->frame_x
+ diff_x
, wwin
->frame_y
+ diff_y
);
1158 if (1) { /* if delay */
1159 delay_configure
.delay_count
++;
1160 delay_configure
.wwin
= scr
->focused_window
;
1161 WMAddTimerHandler(200, (WMCallback
*) sendConfigureNotify
, &delay_configure
);
1169 static void switchWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
1171 wWorkspaceChange(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
1174 static void deleteWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
1176 wWorkspaceDelete(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->workspace_count
- 1);
1179 static void newWSCommand(WMenu
* menu
, WMenuEntry
* foo
)
1183 ws
= wWorkspaceNew(menu
->frame
->screen_ptr
);
1184 /* autochange workspace */
1186 wWorkspaceChange(menu
->frame
->screen_ptr
, ws
);
1191 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1192 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1194 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1199 static char *cropline(char *line
)
1203 if (strlen(line
) == 0)
1207 end
= &(line
[strlen(line
)]) - 1;
1208 while (isspace(*line
) && *line
!= 0)
1210 while (isspace(*end
) && end
!= line
) {
1217 void wWorkspaceRename(WScreen
* scr
, int workspace
, char *name
)
1219 char buf
[MAX_WORKSPACENAME_WIDTH
+ 1];
1222 if (workspace
>= scr
->workspace_count
)
1225 /* trim white spaces */
1226 tmp
= cropline(name
);
1228 if (strlen(tmp
) == 0) {
1229 snprintf(buf
, sizeof(buf
), _("Workspace %i"), workspace
+ 1);
1231 strncpy(buf
, tmp
, MAX_WORKSPACENAME_WIDTH
);
1233 buf
[MAX_WORKSPACENAME_WIDTH
] = 0;
1235 /* update workspace */
1236 wfree(scr
->workspaces
[workspace
]->name
);
1237 scr
->workspaces
[workspace
]->name
= wstrdup(buf
);
1239 if (scr
->clip_ws_menu
) {
1240 if (strcmp(scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
, buf
) != 0) {
1241 wfree(scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
);
1242 scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
= wstrdup(buf
);
1243 wMenuRealize(scr
->clip_ws_menu
);
1246 if (scr
->workspace_menu
) {
1247 if (strcmp(scr
->workspace_menu
->entries
[workspace
+ 2]->text
, buf
) != 0) {
1248 wfree(scr
->workspace_menu
->entries
[workspace
+ 2]->text
);
1249 scr
->workspace_menu
->entries
[workspace
+ 2]->text
= wstrdup(buf
);
1250 wMenuRealize(scr
->workspace_menu
);
1255 wClipIconPaint(scr
->clip_icon
);
1257 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) workspace
);
1260 /* callback for when menu entry is edited */
1261 static void onMenuEntryEdited(WMenu
* menu
, WMenuEntry
* entry
)
1266 wWorkspaceRename(menu
->frame
->screen_ptr
, (long)entry
->clientdata
, tmp
);
1269 WMenu
*wWorkspaceMenuMake(WScreen
* scr
, Bool titled
)
1273 wsmenu
= wMenuCreate(scr
, titled
? _("Workspaces") : NULL
, False
);
1275 wwarning(_("could not create Workspace menu"));
1279 /* callback to be called when an entry is edited */
1280 wsmenu
->on_edit
= onMenuEntryEdited
;
1282 wMenuAddCallback(wsmenu
, _("New"), newWSCommand
, NULL
);
1283 wMenuAddCallback(wsmenu
, _("Destroy Last"), deleteWSCommand
, NULL
);
1288 void wWorkspaceMenuUpdate(WScreen
* scr
, WMenu
* menu
)
1292 char title
[MAX_WORKSPACENAME_WIDTH
+ 1];
1299 if (menu
->entry_no
< scr
->workspace_count
+ 2) {
1300 /* new workspace(s) added */
1301 i
= scr
->workspace_count
- (menu
->entry_no
- 2);
1302 ws
= menu
->entry_no
- 2;
1304 strncpy(title
, scr
->workspaces
[ws
]->name
, MAX_WORKSPACENAME_WIDTH
);
1306 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, (void *)ws
);
1307 entry
->flags
.indicator
= 1;
1308 entry
->flags
.editable
= 1;
1313 } else if (menu
->entry_no
> scr
->workspace_count
+ 2) {
1314 /* removed workspace(s) */
1315 for (i
= menu
->entry_no
- 1; i
>= scr
->workspace_count
+ 2; i
--) {
1316 wMenuRemoveItem(menu
, i
);
1321 for (i
= 0; i
< scr
->workspace_count
; i
++) {
1322 menu
->entries
[i
+ 2]->flags
.indicator_on
= 0;
1324 menu
->entries
[scr
->current_workspace
+ 2]->flags
.indicator_on
= 1;
1326 /* don't let user destroy current workspace */
1327 if (scr
->current_workspace
== scr
->workspace_count
- 1) {
1328 wMenuSetEnabled(menu
, 1, False
);
1330 wMenuSetEnabled(menu
, 1, True
);
1333 tmp
= menu
->frame
->top_width
+ 5;
1334 /* if menu got unreachable, bring it to a visible place */
1335 if (menu
->frame_x
< tmp
- (int)menu
->frame
->core
->width
)
1336 wMenuMove(menu
, tmp
- (int)menu
->frame
->core
->width
, menu
->frame_y
, False
);
1341 void wWorkspaceSaveState(WScreen
* scr
, WMPropList
* old_state
)
1343 WMPropList
*parr
, *pstr
, *wks_state
, *old_wks_state
, *foo
, *bar
;
1348 old_wks_state
= WMGetFromPLDictionary(old_state
, dWorkspaces
);
1349 parr
= WMCreatePLArray(NULL
);
1350 for (i
= 0; i
< scr
->workspace_count
; i
++) {
1351 pstr
= WMCreatePLString(scr
->workspaces
[i
]->name
);
1352 wks_state
= WMCreatePLDictionary(dName
, pstr
, NULL
);
1353 WMReleasePropList(pstr
);
1354 if (!wPreferences
.flags
.noclip
) {
1355 pstr
= wClipSaveWorkspaceState(scr
, i
);
1356 WMPutInPLDictionary(wks_state
, dClip
, pstr
);
1357 WMReleasePropList(pstr
);
1358 } else if (old_wks_state
!= NULL
) {
1359 if ((foo
= WMGetFromPLArray(old_wks_state
, i
)) != NULL
) {
1360 if ((bar
= WMGetFromPLDictionary(foo
, dClip
)) != NULL
) {
1361 WMPutInPLDictionary(wks_state
, dClip
, bar
);
1365 WMAddToPLArray(parr
, wks_state
);
1366 WMReleasePropList(wks_state
);
1368 WMPutInPLDictionary(scr
->session_state
, dWorkspaces
, parr
);
1369 WMReleasePropList(parr
);
1372 void wWorkspaceRestoreState(WScreen
* scr
)
1374 WMPropList
*parr
, *pstr
, *wks_state
, *clip_state
;
1379 if (scr
->session_state
== NULL
)
1382 parr
= WMGetFromPLDictionary(scr
->session_state
, dWorkspaces
);
1387 wscount
= scr
->workspace_count
;
1388 for (i
= 0; i
< WMIN(WMGetPropListItemCount(parr
), MAX_WORKSPACES
); i
++) {
1389 wks_state
= WMGetFromPLArray(parr
, i
);
1390 if (WMIsPLDictionary(wks_state
))
1391 pstr
= WMGetFromPLDictionary(wks_state
, dName
);
1394 if (i
>= scr
->workspace_count
)
1396 if (scr
->workspace_menu
) {
1397 wfree(scr
->workspace_menu
->entries
[i
+ 2]->text
);
1398 scr
->workspace_menu
->entries
[i
+ 2]->text
= wstrdup(WMGetFromPLString(pstr
));
1399 scr
->workspace_menu
->flags
.realized
= 0;
1401 wfree(scr
->workspaces
[i
]->name
);
1402 scr
->workspaces
[i
]->name
= wstrdup(WMGetFromPLString(pstr
));
1403 if (!wPreferences
.flags
.noclip
) {
1404 int added_omnipresent_icons
= 0;
1405 clip_state
= WMGetFromPLDictionary(wks_state
, dClip
);
1406 if (scr
->workspaces
[i
]->clip
)
1407 wDockDestroy(scr
->workspaces
[i
]->clip
);
1408 scr
->workspaces
[i
]->clip
= wDockRestoreState(scr
, clip_state
, WM_CLIP
);
1410 wDockHideIcons(scr
->workspaces
[i
]->clip
);
1412 /* We set the global icons here, because scr->workspaces[i]->clip
1413 * was not valid in wDockRestoreState().
1414 * There we only set icon->omnipresent to know which icons we
1417 for (j
= 0; j
< scr
->workspaces
[i
]->clip
->max_icons
; j
++) {
1418 WAppIcon
*aicon
= scr
->workspaces
[i
]->clip
->icon_array
[j
];
1421 if (!aicon
|| !aicon
->omnipresent
)
1423 aicon
->omnipresent
= 0;
1424 if (wClipMakeIconOmnipresent(aicon
, True
) != WO_SUCCESS
)
1429 /* Move this appicon from workspace i to workspace 0 */
1430 scr
->workspaces
[i
]->clip
->icon_array
[j
] = NULL
;
1431 scr
->workspaces
[i
]->clip
->icon_count
--;
1433 added_omnipresent_icons
++;
1434 /* If there are too many omnipresent appicons, we are in trouble */
1435 assert(scr
->workspaces
[0]->clip
->icon_count
+ added_omnipresent_icons
1436 <= scr
->workspaces
[0]->clip
->max_icons
);
1437 /* Find first free spot on workspace 0 */
1438 for (k
= 0; k
< scr
->workspaces
[0]->clip
->max_icons
; k
++)
1439 if (scr
->workspaces
[0]->clip
->icon_array
[k
] == NULL
)
1441 scr
->workspaces
[0]->clip
->icon_array
[k
] = aicon
;
1442 aicon
->dock
= scr
->workspaces
[0]->clip
;
1444 scr
->workspaces
[0]->clip
->icon_count
+= added_omnipresent_icons
;
1447 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) i
);