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"
57 #define MAX_SHORTCUT_LENGTH 32
58 #define WORKSPACE_NAME_DISPLAY_PADDING 32
60 extern WPreferences wPreferences
;
61 extern XContext wWinContext
;
62 extern XContext wVEdgeContext
;
64 extern void ProcessPendingEvents();
66 static WMPropList
*dWorkspaces
= NULL
;
67 static WMPropList
*dClip
, *dName
;
69 static void make_keys()
71 if (dWorkspaces
!= NULL
)
74 dWorkspaces
= WMCreatePLString("Workspaces");
75 dName
= WMCreatePLString("Name");
76 dClip
= WMCreatePLString("Clip");
79 void wWorkspaceMake(WScreen
* scr
, int count
)
87 int wWorkspaceNew(WScreen
* scr
)
89 WWorkspace
*wspace
, **list
;
92 if (scr
->workspace_count
< MAX_WORKSPACES
) {
93 scr
->workspace_count
++;
95 wspace
= wmalloc(sizeof(WWorkspace
));
99 wspace
->name
= wmalloc(strlen(_("Workspace %i")) + 8);
100 sprintf(wspace
->name
, _("Workspace %i"), scr
->workspace_count
);
103 if (!wPreferences
.flags
.noclip
) {
104 wspace
->clip
= wDockCreate(scr
, WM_CLIP
);
108 list
= wmalloc(sizeof(WWorkspace
*) * scr
->workspace_count
);
110 for (i
= 0; i
< scr
->workspace_count
- 1; i
++) {
111 list
[i
] = scr
->workspaces
[i
];
115 wfree(scr
->workspaces
);
116 scr
->workspaces
= list
;
118 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
119 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
120 #ifdef VIRTUAL_DESKTOP
121 wspace
->view_x
= wspace
->view_y
= 0;
122 wspace
->height
= scr
->scr_height
;
123 wspace
->width
= scr
->scr_width
;
126 wNETWMUpdateDesktop(scr
);
129 WMPostNotificationName(WMNWorkspaceCreated
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
132 return scr
->workspace_count
- 1;
138 Bool
wWorkspaceDelete(WScreen
* scr
, int workspace
)
147 /* verify if workspace is in use by some window */
148 tmp
= scr
->focused_window
;
150 if (!IS_OMNIPRESENT(tmp
) && tmp
->frame
->workspace
== workspace
)
155 if (!wPreferences
.flags
.noclip
) {
156 wDockDestroy(scr
->workspaces
[workspace
]->clip
);
157 scr
->workspaces
[workspace
]->clip
= NULL
;
160 list
= wmalloc(sizeof(WWorkspace
*) * (scr
->workspace_count
- 1));
162 for (i
= 0; i
< scr
->workspace_count
; i
++) {
163 if (i
!= workspace
) {
164 list
[j
++] = scr
->workspaces
[i
];
166 if (scr
->workspaces
[i
]->name
)
167 wfree(scr
->workspaces
[i
]->name
);
168 wfree(scr
->workspaces
[i
]);
171 wfree(scr
->workspaces
);
172 scr
->workspaces
= list
;
174 scr
->workspace_count
--;
177 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
178 /* clip workspace menu */
179 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
181 /* update also window menu */
182 if (scr
->workspace_submenu
) {
183 WMenu
*menu
= scr
->workspace_submenu
;
186 while (i
> scr
->workspace_count
)
187 wMenuRemoveItem(menu
, --i
);
191 if (scr
->clip_submenu
) {
192 WMenu
*menu
= scr
->clip_submenu
;
195 while (i
> scr
->workspace_count
)
196 wMenuRemoveItem(menu
, --i
);
200 wNETWMUpdateDesktop(scr
);
203 WMPostNotificationName(WMNWorkspaceDestroyed
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
205 if (scr
->current_workspace
>= scr
->workspace_count
)
206 wWorkspaceChange(scr
, scr
->workspace_count
- 1);
211 typedef struct WorkspaceNameData
{
218 static void hideWorkspaceName(void *data
)
220 WScreen
*scr
= (WScreen
*) data
;
222 if (!scr
->workspace_name_data
|| scr
->workspace_name_data
->count
== 0
223 || time(NULL
) > scr
->workspace_name_data
->timeout
) {
224 XUnmapWindow(dpy
, scr
->workspace_name
);
226 if (scr
->workspace_name_data
) {
227 RReleaseImage(scr
->workspace_name_data
->back
);
228 RReleaseImage(scr
->workspace_name_data
->text
);
229 wfree(scr
->workspace_name_data
);
231 scr
->workspace_name_data
= NULL
;
233 scr
->workspace_name_timer
= NULL
;
235 RImage
*img
= RCloneImage(scr
->workspace_name_data
->back
);
238 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
240 RCombineImagesWithOpaqueness(img
, scr
->workspace_name_data
->text
,
241 scr
->workspace_name_data
->count
* 255 / 10);
243 RConvertImage(scr
->rcontext
, img
, &pix
);
247 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, pix
);
248 XClearWindow(dpy
, scr
->workspace_name
);
249 XFreePixmap(dpy
, pix
);
252 scr
->workspace_name_data
->count
--;
256 static void showWorkspaceName(WScreen
* scr
, int workspace
)
258 WorkspaceNameData
*data
;
263 char *name
= scr
->workspaces
[workspace
]->name
;
264 int len
= strlen(name
);
272 if (wPreferences
.workspace_name_display_position
== WD_NONE
|| scr
->workspace_count
< 2) {
276 if (scr
->workspace_name_timer
) {
277 WMDeleteTimerHandler(scr
->workspace_name_timer
);
278 XUnmapWindow(dpy
, scr
->workspace_name
);
281 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
, hideWorkspaceName
, scr
);
283 if (scr
->workspace_name_data
) {
284 RReleaseImage(scr
->workspace_name_data
->back
);
285 RReleaseImage(scr
->workspace_name_data
->text
);
286 wfree(scr
->workspace_name_data
);
289 data
= wmalloc(sizeof(WorkspaceNameData
));
292 w
= WMWidthOfString(scr
->workspace_name_font
, name
, len
);
293 h
= WMFontHeight(scr
->workspace_name_font
);
296 head
= wGetHeadForPointerLocation(scr
);
297 rect
= wGetRectForHead(scr
, head
);
298 if (scr
->xine_info
.count
) {
299 xx
= rect
.pos
.x
+ (scr
->xine_info
.screens
[head
].size
.width
- (w
+ 4)) / 2;
300 yy
= rect
.pos
.y
+ (scr
->xine_info
.screens
[head
].size
.height
- (h
+ 4)) / 2;
303 xx
= (scr
->scr_width
- (w
+ 4)) / 2;
304 yy
= (scr
->scr_height
- (h
+ 4)) / 2;
308 switch (wPreferences
.workspace_name_display_position
) {
313 px
= (scr
->scr_width
- (w
+ 4)) / 2;
315 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
321 px
= (scr
->scr_width
- (w
+ 4)) / 2;
323 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
326 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
327 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
330 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
331 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
334 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
335 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
338 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
339 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
347 px
= (scr
->scr_width
- (w
+ 4)) / 2;
348 py
= (scr
->scr_height
- (h
+ 4)) / 2;
352 XResizeWindow(dpy
, scr
->workspace_name
, w
+ 4, h
+ 4);
353 XMoveWindow(dpy
, scr
->workspace_name
, px
, py
);
355 text
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, scr
->w_depth
);
356 mask
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, 1);
358 /*XSetForeground(dpy, scr->mono_gc, 0);
359 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
361 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
363 for (x
= 0; x
<= 4; x
++) {
364 for (y
= 0; y
<= 4; y
++) {
365 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, x
, y
, name
, len
);
369 XSetForeground(dpy
, scr
->mono_gc
, 1);
370 XSetBackground(dpy
, scr
->mono_gc
, 0);
372 XCopyPlane(dpy
, text
, mask
, scr
->mono_gc
, 0, 0, w
+ 4, h
+ 4, 0, 0, 1 << (scr
->w_depth
- 1));
374 /*XSetForeground(dpy, scr->mono_gc, 1); */
375 XSetBackground(dpy
, scr
->mono_gc
, 1);
377 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
379 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, 2, 2, name
, len
);
382 XShapeCombineMask(dpy
, scr
->workspace_name
, ShapeBounding
, 0, 0, mask
, ShapeSet
);
384 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, text
);
385 XClearWindow(dpy
, scr
->workspace_name
);
387 data
->text
= RCreateImageFromDrawable(scr
->rcontext
, text
, None
);
389 XFreePixmap(dpy
, text
);
390 XFreePixmap(dpy
, mask
);
393 XMapRaised(dpy
, scr
->workspace_name
);
399 ximg
= RGetXImage(scr
->rcontext
, scr
->root_win
, px
, py
, data
->text
->width
, data
->text
->height
);
401 if (!ximg
|| !ximg
->image
) {
405 XMapRaised(dpy
, scr
->workspace_name
);
408 data
->back
= RCreateImageFromXImage(scr
->rcontext
, ximg
->image
, NULL
);
409 RDestroyXImage(scr
->rcontext
, ximg
);
417 /* set a timeout for the effect */
418 data
->timeout
= time(NULL
) + 2 + (WORKSPACE_NAME_DELAY
+ WORKSPACE_NAME_FADE_DELAY
* data
->count
) / 1000;
420 scr
->workspace_name_data
= data
;
425 if (scr
->workspace_name_timer
)
426 WMDeleteTimerHandler(scr
->workspace_name_timer
);
429 RReleaseImage(data
->text
);
431 RReleaseImage(data
->back
);
434 scr
->workspace_name_data
= NULL
;
436 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
+
437 10 * WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
440 void wWorkspaceChange(WScreen
*scr
, int workspace
)
442 if (scr
->flags
.startup
|| scr
->flags
.startup2
)
445 if (workspace
!= scr
->current_workspace
)
446 wWorkspaceForceChange(scr
, workspace
);
449 void wWorkspaceRelativeChange(WScreen
* scr
, int amount
)
453 w
= scr
->current_workspace
+ amount
;
457 wWorkspaceChange(scr
, w
);
458 } else if (wPreferences
.ws_cycle
) {
459 wWorkspaceChange(scr
, scr
->workspace_count
+ w
);
461 } else if (amount
> 0) {
462 if (w
< scr
->workspace_count
) {
463 wWorkspaceChange(scr
, w
);
464 } else if (wPreferences
.ws_advance
) {
465 wWorkspaceChange(scr
, WMIN(w
, MAX_WORKSPACES
- 1));
466 } else if (wPreferences
.ws_cycle
) {
467 wWorkspaceChange(scr
, w
% scr
->workspace_count
);
472 void wWorkspaceForceChange(WScreen
* scr
, int workspace
)
474 WWindow
*tmp
, *foc
= NULL
, *foc2
= NULL
;
476 if (workspace
>= MAX_WORKSPACES
|| workspace
< 0)
479 SendHelperMessage(scr
, 'C', workspace
+ 1, NULL
);
481 if (workspace
> scr
->workspace_count
- 1) {
482 wWorkspaceMake(scr
, workspace
- scr
->workspace_count
+ 1);
485 wClipUpdateForWorkspaceChange(scr
, workspace
);
487 scr
->current_workspace
= workspace
;
489 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
491 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
493 if ((tmp
= scr
->focused_window
) != NULL
) {
494 if ((IS_OMNIPRESENT(tmp
) && (tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
495 !WFLAGP(tmp
, no_focusable
)) || tmp
->flags
.changing_workspace
) {
499 /* foc2 = tmp; will fix annoyance with gnome panel
500 * but will create annoyance for every other application
503 if (tmp
->frame
->workspace
!= workspace
&& !tmp
->flags
.selected
) {
504 /* unmap windows not on this workspace */
505 if ((tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
506 !IS_OMNIPRESENT(tmp
) && !tmp
->flags
.changing_workspace
) {
509 /* also unmap miniwindows not on this workspace */
510 if (!wPreferences
.sticky_icons
&& tmp
->flags
.miniaturized
&&
511 tmp
->icon
&& !IS_OMNIPRESENT(tmp
)) {
512 XUnmapWindow(dpy
, tmp
->icon
->core
->window
);
513 tmp
->icon
->mapped
= 0;
515 /* update current workspace of omnipresent windows */
516 if (IS_OMNIPRESENT(tmp
)) {
517 WApplication
*wapp
= wApplicationOf(tmp
->main_window
);
519 tmp
->frame
->workspace
= workspace
;
522 wapp
->last_workspace
= workspace
;
524 if (!foc2
&& (tmp
->flags
.mapped
|| tmp
->flags
.shaded
)) {
529 /* change selected windows' workspace */
530 if (tmp
->flags
.selected
) {
531 wWindowChangeWorkspace(tmp
, workspace
);
532 if (!tmp
->flags
.miniaturized
&& !foc
) {
536 if (!tmp
->flags
.hidden
) {
537 if (!(tmp
->flags
.mapped
|| tmp
->flags
.miniaturized
)) {
538 /* remap windows that are on this workspace */
540 if (!foc
&& !WFLAGP(tmp
, no_focusable
)) {
544 /* Also map miniwindow if not omnipresent */
545 if (!wPreferences
.sticky_icons
&&
546 tmp
->flags
.miniaturized
&& !IS_OMNIPRESENT(tmp
) && tmp
->icon
) {
547 tmp
->icon
->mapped
= 1;
548 XMapWindow(dpy
, tmp
->icon
->core
->window
);
556 /* Gobble up events unleashed by our mapping & unmapping.
557 * These may trigger various grab-initiated focus &
558 * crossing events. However, we don't care about them,
559 * and ignore their focus implications altogether to avoid
562 scr
->flags
.ignore_focus_events
= 1;
563 ProcessPendingEvents();
564 scr
->flags
.ignore_focus_events
= 0;
569 if (scr
->focused_window
->flags
.mapped
&& !foc
) {
570 foc
= scr
->focused_window
;
572 if (wPreferences
.focus_mode
== WKF_CLICK
) {
573 wSetFocusTo(scr
, foc
);
581 if (XQueryPointer(dpy
, scr
->root_win
, &bar
, &win
, &foo
, &foo
, &foo
, &foo
, &mask
)) {
582 tmp
= wWindowFor(win
);
585 /* If there's a window under the pointer, focus it.
586 * (we ate all other focus events above, so it's
587 * certainly not focused). Otherwise focus last
588 * focused, or the root (depending on sloppiness)
590 if (!tmp
&& wPreferences
.focus_mode
== WKF_SLOPPY
) {
591 wSetFocusTo(scr
, foc
);
593 wSetFocusTo(scr
, tmp
);
598 /* We need to always arrange icons when changing workspace, even if
599 * no autoarrange icons, because else the icons in different workspaces
601 * This can be avoided if appicons are also workspace specific.
603 if (!wPreferences
.sticky_icons
)
604 wArrangeIcons(scr
, False
);
607 wAppIconPaint(scr
->dock
->icon_array
[0]);
609 if (scr
->clip_icon
) {
610 if (scr
->workspaces
[workspace
]->clip
->auto_collapse
||
611 scr
->workspaces
[workspace
]->clip
->auto_raise_lower
) {
612 /* to handle enter notify. This will also */
613 XUnmapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
614 XMapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
616 wClipIconPaint(scr
->clip_icon
);
620 wScreenUpdateUsableArea(scr
);
621 wNETWMUpdateDesktop(scr
);
624 showWorkspaceName(scr
, workspace
);
626 WMPostNotificationName(WMNWorkspaceChanged
, scr
, (void *)(uintptr_t) workspace
);
628 /* XSync(dpy, False); */
631 #ifdef VIRTUAL_DESKTOP
635 * 1) Allow border around each window so the scrolling
636 * won't just stop at the border.
641 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
642 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
643 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
644 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
645 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
646 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
649 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
653 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
656 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
658 static Bool
cmp_gez(int i
)
663 static Bool
cmp_gz(int i
)
668 static WMPoint
getClosestEdge(WScreen
* scr
, WMPoint direction
, Bool(*cmp
) (int))
670 WMPoint closest
= wmkpoint(0, 0);
671 int closest_len
= INT_MAX
;
674 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
675 if (wwin
->frame
->workspace
== scr
->current_workspace
) {
676 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) && !wwin
->flags
.hidden
) {
677 int border
= 2 * HAS_BORDER(wwin
);
680 int head
= wGetHeadForWindow(wwin
);
681 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
684 x1
= wwin
->frame_x
- area
.x1
;
685 y1
= wwin
->frame_y
- area
.y1
;
686 x2
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ border
- area
.x2
;
687 y2
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ border
- area
.y2
;
689 p
= wmkpoint(x1
, y1
);
690 len
= vec_dot(direction
, p
);
691 updateMinimum(len
, p
, closest_len
, closest
);
693 p
= wmkpoint(x1
, y2
);
694 len
= vec_dot(direction
, p
);
695 updateMinimum(len
, p
, closest_len
, closest
);
697 p
= wmkpoint(x2
, y1
);
698 len
= vec_dot(direction
, p
);
699 updateMinimum(len
, p
, closest_len
, closest
);
701 p
= wmkpoint(x2
, y2
);
702 len
= vec_dot(direction
, p
);
703 updateMinimum(len
, p
, closest_len
, closest
);
711 static void getViewPosition(WScreen
* scr
, int workspace
, int *x
, int *y
)
713 *x
= scr
->workspaces
[workspace
]->view_x
;
714 *y
= scr
->workspaces
[workspace
]->view_y
;
717 void wWorkspaceKeyboardMoveDesktop(WScreen
* scr
, WMPoint direction
)
720 WMPoint edge
= getClosestEdge(scr
, direction
, cmp_gz
);
721 int len
= vec_dot(edge
, direction
);
722 WMPoint step
= vec_scale(direction
, len
);
723 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
724 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
+ step
.x
, y
+ step
.y
);
727 extern Cursor wCursor
[WCUR_LAST
];
729 static void vdMouseMoveDesktop(XEvent
* event
, WMPoint direction
)
731 static int lock
= False
;
738 WScreen
*scr
= wScreenForRootWindow(event
->xcrossing
.root
);
739 WMPoint old_pos
= wmkpoint(event
->xcrossing
.x_root
, event
->xcrossing
.y_root
);
744 if (XGrabPointer(dpy
, event
->xcrossing
.window
, False
,
745 PointerMotionMask
, GrabModeAsync
, GrabModeAsync
,
746 scr
->root_win
, wCursor
[WCUR_EMPTY
], CurrentTime
) != GrabSuccess
) {
748 /* if the grab fails, do it the old fashioned way */
749 step
= vec_scale2(direction
, wPreferences
.vedge_hscrollspeed
, wPreferences
.vedge_vscrollspeed
);
750 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
751 if (wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
+ step
.x
, y
+ step
.y
)) {
752 step
= vec_scale(direction
, 2);
753 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
754 event
->xcrossing
.x_root
- step
.x
, event
->xcrossing
.y_root
- step
.y
);
764 if (old_pos
.x
> scr
->scr_width
)
765 old_pos
.x
= scr
->scr_width
;
766 if (old_pos
.y
> scr
->scr_height
)
767 old_pos
.y
= scr
->scr_height
;
772 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
773 scr
->scr_width
/ 2, scr
->scr_height
/ 2);
776 WMMaskEvent(dpy
, PointerMotionMask
, &ev
);
782 step
= wmkpoint(ev
.xmotion
.x_root
- scr
->scr_width
/ 2,
783 ev
.xmotion
.y_root
- scr
->scr_height
/ 2);
784 step_len
= vec_dot(step
, direction
);
792 int resist
= wPreferences
.vedge_resistance
;
794 int closest_len
= INT_MAX
;
796 closest
= getClosestEdge(scr
, direction
, cmp_gez
);
797 closest_len
= vec_dot(direction
, closest
);
801 resisted
+= step_len
;
802 do_move
= resisted
>= resist
;
804 closest_len
= INT_MAX
;
805 step_len
= resisted
- resist
;
810 if (closest_len
<= wPreferences
.vedge_attraction
) {
811 step
= vec_scale(direction
, closest_len
);
813 step
= vec_scale(direction
, step_len
);
816 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
817 wWorkspaceSetViewport(scr
, scr
->current_workspace
,
818 x
+ step
.x
, y
+ step
.y
);
827 step
= vec_add(old_pos
, vec_scale(direction
, -1));
828 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0, step
.x
, step
.y
);
829 XUngrabPointer(dpy
, CurrentTime
);
835 static void vdHandleEnter_u(XEvent
* event
)
837 vdMouseMoveDesktop(event
, VEC_UP
);
840 static void vdHandleEnter_d(XEvent
* event
)
842 vdMouseMoveDesktop(event
, VEC_DOWN
);
845 static void vdHandleEnter_l(XEvent
* event
)
847 vdMouseMoveDesktop(event
, VEC_LEFT
);
850 static void vdHandleEnter_r(XEvent
* event
)
852 vdMouseMoveDesktop(event
, VEC_RIGHT
);
855 #define LEFT_EDGE 0x01
856 #define RIGHT_EDGE 0x02
857 #define TOP_EDGE 0x04
858 #define BOTTOM_EDGE 0x08
859 #define ALL_EDGES 0x0F
861 static void createEdges(WScreen
* scr
)
863 if (!scr
->virtual_edges
) {
866 XSetWindowAttributes attribs
;
868 int heads
= wXineramaHeads(scr
);
869 int *hasEdges
= (int *)wmalloc(sizeof(int) * heads
);
873 int max_edges
= 4 * heads
;
875 Window
*edges
= (Window
*) wmalloc(sizeof(Window
) * max_edges
);
877 for (i
= 0; i
< heads
; ++i
)
878 hasEdges
[i
] = ALL_EDGES
;
879 for (i
= 0; i
< heads
; ++i
) {
880 WMRect i_rect
= wGetRectForHead(scr
, i
);
881 for (j
= i
+ 1; j
< heads
; ++j
) {
882 WMRect j_rect
= wGetRectForHead(scr
, j
);
884 int vlen
= (WMIN(i_rect
.pos
.y
+ i_rect
.size
.height
,
885 j_rect
.pos
.y
+ j_rect
.size
.height
) -
886 WMAX(i_rect
.pos
.y
, j_rect
.pos
.y
));
888 int hlen
= (WMIN(i_rect
.pos
.x
+ i_rect
.size
.width
,
889 j_rect
.pos
.x
+ j_rect
.size
.width
) -
890 WMAX(i_rect
.pos
.x
, j_rect
.pos
.x
));
892 if (vlen
> 0 && hlen
== 0) { /* horz alignment, vert edges touch */
893 if (i_rect
.pos
.x
< j_rect
.pos
.x
) { /* i left of j */
894 hasEdges
[i
] &= ~RIGHT_EDGE
;
895 hasEdges
[j
] &= ~LEFT_EDGE
;
896 } else { /* j left of i */
897 hasEdges
[j
] &= ~RIGHT_EDGE
;
898 hasEdges
[i
] &= ~LEFT_EDGE
;
900 } else if (vlen
== 0 && hlen
> 0) { /* vert alignment, horz edges touch */
901 if (i_rect
.pos
.y
< j_rect
.pos
.y
) { /* i top of j */
902 hasEdges
[i
] &= ~BOTTOM_EDGE
;
903 hasEdges
[j
] &= ~TOP_EDGE
;
904 } else { /* j top of i */
905 hasEdges
[j
] &= ~BOTTOM_EDGE
;
906 hasEdges
[i
] &= ~TOP_EDGE
;
912 for (w
= 0; w
< scr
->workspace_count
; w
++) {
913 /* puts("reset workspace"); */
914 wWorkspaceSetViewport(scr
, w
, 0, 0);
917 vmask
= CWEventMask
| CWOverrideRedirect
;
918 attribs
.event_mask
= (EnterWindowMask
| LeaveWindowMask
| VisibilityChangeMask
);
919 attribs
.override_redirect
= True
;
921 for (head
= 0; head
< wXineramaHeads(scr
); ++head
) {
922 WMRect rect
= wGetRectForHead(scr
, head
);
924 if (hasEdges
[head
] & TOP_EDGE
) {
926 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
927 rect
.size
.width
, thickness
, 0,
928 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
929 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_u
);
933 if (hasEdges
[head
] & BOTTOM_EDGE
) {
935 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
,
936 rect
.pos
.y
+ rect
.size
.height
- thickness
,
937 rect
.size
.width
, thickness
, 0,
938 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
939 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_d
);
943 if (hasEdges
[head
] & LEFT_EDGE
) {
945 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
946 thickness
, rect
.pos
.y
+ rect
.size
.height
, 0,
947 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
948 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_l
);
952 if (hasEdges
[head
] & RIGHT_EDGE
) {
954 XCreateWindow(dpy
, scr
->root_win
,
955 rect
.pos
.x
+ rect
.size
.width
- thickness
, rect
.pos
.y
,
956 thickness
, rect
.size
.height
, 0,
957 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
958 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_r
);
963 scr
->virtual_nr_edges
= nr_edges
;
964 scr
->virtual_edges
= edges
;
966 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
967 XMapWindow(dpy
, scr
->virtual_edges
[i
]);
969 wWorkspaceRaiseEdge(scr
);
975 static void destroyEdges(WScreen
* scr
)
977 if (scr
->virtual_edges
) {
980 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
981 XDeleteContext(dpy
, scr
->virtual_edges
[i
], wVEdgeContext
);
982 XUnmapWindow(dpy
, scr
->virtual_edges
[i
]);
983 XDestroyWindow(dpy
, scr
->virtual_edges
[i
]);
986 wfree(scr
->virtual_edges
);
987 scr
->virtual_edges
= NULL
;
988 scr
->virtual_nr_edges
= 0;
992 void wWorkspaceUpdateEdge(WScreen
* scr
)
994 if (wPreferences
.vdesk_enable
) {
1001 void wWorkspaceRaiseEdge(WScreen
* scr
)
1003 static int toggle
= 0;
1006 if (!scr
->virtual_edges
)
1010 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
1011 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1014 for (i
= scr
->virtual_nr_edges
- 1; i
>= 0; --i
) {
1015 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1022 void wWorkspaceLowerEdge(WScreen
* scr
)
1025 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
1026 XLowerWindow(dpy
, scr
->virtual_edges
[i
]);
1030 void wWorkspaceResizeViewport(WScreen
* scr
, int workspace
)
1033 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
1034 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
, y
);
1037 void updateWorkspaceGeometry(WScreen
* scr
, int workspace
, int *view_x
, int *view_y
)
1039 int most_left
, most_right
, most_top
, most_bottom
;
1042 int heads
= wXineramaHeads(scr
);
1043 typedef int strut_t
[4];
1044 strut_t
*strut
= (strut_t
*) wmalloc(heads
* sizeof(strut_t
));
1047 for (head
= 0; head
< heads
; ++head
) {
1048 WMRect rect
= wGetRectForHead(scr
, head
);
1049 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
1050 strut
[head
][0] = area
.x1
- rect
.pos
.x
;
1051 strut
[head
][1] = rect
.pos
.x
+ rect
.size
.width
- area
.x2
;
1052 strut
[head
][2] = area
.y1
- rect
.pos
.y
;
1053 strut
[head
][3] = rect
.pos
.y
+ rect
.size
.height
- area
.y2
;
1056 /* adjust workspace layout */
1057 wwin
= scr
->focused_window
;
1060 most_left
= scr
->scr_width
;
1061 most_top
= scr
->scr_height
;
1062 for (; wwin
; wwin
= wwin
->prev
) {
1063 if (wwin
->frame
->workspace
== workspace
) {
1064 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) && !wwin
->flags
.hidden
) {
1066 head
= wGetHeadForWindow(wwin
);
1068 i
= wwin
->frame_x
- strut
[head
][0];
1069 if (i
< most_left
) /* record positions, should this be cached? */
1071 i
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ strut
[head
][1];
1072 if (HAS_BORDER(wwin
))
1076 i
= wwin
->frame_y
- strut
[head
][2];
1079 i
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ strut
[head
][3];
1080 if (HAS_BORDER(wwin
))
1082 if (i
> most_bottom
) {
1094 scr
->workspaces
[workspace
]->width
= WMAX(most_right
, scr
->scr_width
) - WMIN(most_left
, 0);
1095 scr
->workspaces
[workspace
]->height
= WMAX(most_bottom
, scr
->scr_height
) - WMIN(most_top
, 0);
1097 *view_x
+= -most_left
- scr
->workspaces
[workspace
]->view_x
;
1098 scr
->workspaces
[workspace
]->view_x
= -most_left
;
1100 *view_y
+= -most_top
- scr
->workspaces
[workspace
]->view_y
;
1101 scr
->workspaces
[workspace
]->view_y
= -most_top
;
1106 typedef struct _delay_configure
{
1111 void sendConfigureNotify(_delay_configure
* delay
)
1115 delay
->delay_count
--;
1116 if (!delay
->delay_count
) {
1117 for (wwin
= delay
->wwin
; wwin
; wwin
= wwin
->prev
) {
1118 wWindowSynthConfigureNotify(wwin
);
1123 Bool
wWorkspaceSetViewport(WScreen
* scr
, int workspace
, int view_x
, int view_y
)
1125 Bool adjust_flag
= False
;
1127 static _delay_configure delay_configure
= { NULL
, 0 };
1131 wptr
= scr
->workspaces
[workspace
];
1133 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y); */
1135 updateWorkspaceGeometry(scr
, workspace
, &view_x
, &view_y
);
1137 if (view_x
+ scr
->scr_width
> wptr
->width
) {
1138 /* puts("right edge of vdesk"); */
1139 view_x
= wptr
->width
- scr
->scr_width
;
1142 /* puts("left edge of vdesk"); */
1145 if (view_y
+ scr
->scr_height
> wptr
->height
) {
1146 /* puts("right edge of vdesk"); */
1147 view_y
= wptr
->height
- scr
->scr_height
;
1150 /* puts("left edge of vdesk"); */
1154 diff_x
= wptr
->view_x
- view_x
;
1155 diff_y
= wptr
->view_y
- view_y
;
1156 if (!diff_x
&& !diff_y
)
1159 wptr
->view_x
= view_x
;
1160 wptr
->view_y
= view_y
;
1163 wNETWMUpdateDesktop(scr
);
1166 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
1167 if (wwin
->frame
->workspace
== workspace
&& !IS_VSTUCK(wwin
)) {
1168 wWindowMove(wwin
, wwin
->frame_x
+ diff_x
, wwin
->frame_y
+ diff_y
);
1172 if (1) { /* if delay */
1173 delay_configure
.delay_count
++;
1174 delay_configure
.wwin
= scr
->focused_window
;
1175 WMAddTimerHandler(200, (WMCallback
*) sendConfigureNotify
, &delay_configure
);
1183 static void switchWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
1185 wWorkspaceChange(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
1188 static void deleteWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
1190 wWorkspaceDelete(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->workspace_count
- 1);
1193 static void newWSCommand(WMenu
* menu
, WMenuEntry
* foo
)
1197 ws
= wWorkspaceNew(menu
->frame
->screen_ptr
);
1198 /* autochange workspace */
1200 wWorkspaceChange(menu
->frame
->screen_ptr
, ws
);
1205 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1206 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1208 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1213 static char *cropline(char *line
)
1217 if (strlen(line
) == 0)
1221 end
= &(line
[strlen(line
)]) - 1;
1222 while (isspace(*line
) && *line
!= 0)
1224 while (isspace(*end
) && end
!= line
) {
1231 void wWorkspaceRename(WScreen
* scr
, int workspace
, char *name
)
1233 char buf
[MAX_WORKSPACENAME_WIDTH
+ 1];
1236 if (workspace
>= scr
->workspace_count
)
1239 /* trim white spaces */
1240 tmp
= cropline(name
);
1242 if (strlen(tmp
) == 0) {
1243 snprintf(buf
, sizeof(buf
), _("Workspace %i"), workspace
+ 1);
1245 strncpy(buf
, tmp
, MAX_WORKSPACENAME_WIDTH
);
1247 buf
[MAX_WORKSPACENAME_WIDTH
] = 0;
1249 /* update workspace */
1250 wfree(scr
->workspaces
[workspace
]->name
);
1251 scr
->workspaces
[workspace
]->name
= wstrdup(buf
);
1253 if (scr
->clip_ws_menu
) {
1254 if (strcmp(scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
, buf
) != 0) {
1255 wfree(scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
);
1256 scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
= wstrdup(buf
);
1257 wMenuRealize(scr
->clip_ws_menu
);
1260 if (scr
->workspace_menu
) {
1261 if (strcmp(scr
->workspace_menu
->entries
[workspace
+ 2]->text
, buf
) != 0) {
1262 wfree(scr
->workspace_menu
->entries
[workspace
+ 2]->text
);
1263 scr
->workspace_menu
->entries
[workspace
+ 2]->text
= wstrdup(buf
);
1264 wMenuRealize(scr
->workspace_menu
);
1269 wClipIconPaint(scr
->clip_icon
);
1271 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) workspace
);
1274 /* callback for when menu entry is edited */
1275 static void onMenuEntryEdited(WMenu
* menu
, WMenuEntry
* entry
)
1280 wWorkspaceRename(menu
->frame
->screen_ptr
, (long)entry
->clientdata
, tmp
);
1283 WMenu
*wWorkspaceMenuMake(WScreen
* scr
, Bool titled
)
1287 wsmenu
= wMenuCreate(scr
, titled
? _("Workspaces") : NULL
, False
);
1289 wwarning(_("could not create Workspace menu"));
1293 /* callback to be called when an entry is edited */
1294 wsmenu
->on_edit
= onMenuEntryEdited
;
1296 wMenuAddCallback(wsmenu
, _("New"), newWSCommand
, NULL
);
1297 wMenuAddCallback(wsmenu
, _("Destroy Last"), deleteWSCommand
, NULL
);
1302 void wWorkspaceMenuUpdate(WScreen
* scr
, WMenu
* menu
)
1306 char title
[MAX_WORKSPACENAME_WIDTH
+ 1];
1313 if (menu
->entry_no
< scr
->workspace_count
+ 2) {
1314 /* new workspace(s) added */
1315 i
= scr
->workspace_count
- (menu
->entry_no
- 2);
1316 ws
= menu
->entry_no
- 2;
1318 strncpy(title
, scr
->workspaces
[ws
]->name
, MAX_WORKSPACENAME_WIDTH
);
1320 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, (void *)ws
);
1321 entry
->flags
.indicator
= 1;
1322 entry
->flags
.editable
= 1;
1327 } else if (menu
->entry_no
> scr
->workspace_count
+ 2) {
1328 /* removed workspace(s) */
1329 for (i
= menu
->entry_no
- 1; i
>= scr
->workspace_count
+ 2; i
--) {
1330 wMenuRemoveItem(menu
, i
);
1335 for (i
= 0; i
< scr
->workspace_count
; i
++) {
1336 menu
->entries
[i
+ 2]->flags
.indicator_on
= 0;
1338 menu
->entries
[scr
->current_workspace
+ 2]->flags
.indicator_on
= 1;
1340 /* don't let user destroy current workspace */
1341 if (scr
->current_workspace
== scr
->workspace_count
- 1) {
1342 wMenuSetEnabled(menu
, 1, False
);
1344 wMenuSetEnabled(menu
, 1, True
);
1347 tmp
= menu
->frame
->top_width
+ 5;
1348 /* if menu got unreachable, bring it to a visible place */
1349 if (menu
->frame_x
< tmp
- (int)menu
->frame
->core
->width
)
1350 wMenuMove(menu
, tmp
- (int)menu
->frame
->core
->width
, menu
->frame_y
, False
);
1355 void wWorkspaceSaveState(WScreen
* scr
, WMPropList
* old_state
)
1357 WMPropList
*parr
, *pstr
, *wks_state
, *old_wks_state
, *foo
, *bar
;
1362 old_wks_state
= WMGetFromPLDictionary(old_state
, dWorkspaces
);
1363 parr
= WMCreatePLArray(NULL
);
1364 for (i
= 0; i
< scr
->workspace_count
; i
++) {
1365 pstr
= WMCreatePLString(scr
->workspaces
[i
]->name
);
1366 wks_state
= WMCreatePLDictionary(dName
, pstr
, NULL
);
1367 WMReleasePropList(pstr
);
1368 if (!wPreferences
.flags
.noclip
) {
1369 pstr
= wClipSaveWorkspaceState(scr
, i
);
1370 WMPutInPLDictionary(wks_state
, dClip
, pstr
);
1371 WMReleasePropList(pstr
);
1372 } else if (old_wks_state
!= NULL
) {
1373 if ((foo
= WMGetFromPLArray(old_wks_state
, i
)) != NULL
) {
1374 if ((bar
= WMGetFromPLDictionary(foo
, dClip
)) != NULL
) {
1375 WMPutInPLDictionary(wks_state
, dClip
, bar
);
1379 WMAddToPLArray(parr
, wks_state
);
1380 WMReleasePropList(wks_state
);
1382 WMPutInPLDictionary(scr
->session_state
, dWorkspaces
, parr
);
1383 WMReleasePropList(parr
);
1386 void wWorkspaceRestoreState(WScreen
* scr
)
1388 WMPropList
*parr
, *pstr
, *wks_state
, *clip_state
;
1393 if (scr
->session_state
== NULL
)
1396 parr
= WMGetFromPLDictionary(scr
->session_state
, dWorkspaces
);
1401 wscount
= scr
->workspace_count
;
1402 for (i
= 0; i
< WMIN(WMGetPropListItemCount(parr
), MAX_WORKSPACES
); i
++) {
1403 wks_state
= WMGetFromPLArray(parr
, i
);
1404 if (WMIsPLDictionary(wks_state
))
1405 pstr
= WMGetFromPLDictionary(wks_state
, dName
);
1408 if (i
>= scr
->workspace_count
)
1410 if (scr
->workspace_menu
) {
1411 wfree(scr
->workspace_menu
->entries
[i
+ 2]->text
);
1412 scr
->workspace_menu
->entries
[i
+ 2]->text
= wstrdup(WMGetFromPLString(pstr
));
1413 scr
->workspace_menu
->flags
.realized
= 0;
1415 wfree(scr
->workspaces
[i
]->name
);
1416 scr
->workspaces
[i
]->name
= wstrdup(WMGetFromPLString(pstr
));
1417 if (!wPreferences
.flags
.noclip
) {
1418 int added_omnipresent_icons
= 0;
1419 clip_state
= WMGetFromPLDictionary(wks_state
, dClip
);
1420 if (scr
->workspaces
[i
]->clip
)
1421 wDockDestroy(scr
->workspaces
[i
]->clip
);
1422 scr
->workspaces
[i
]->clip
= wDockRestoreState(scr
, clip_state
, WM_CLIP
);
1424 wDockHideIcons(scr
->workspaces
[i
]->clip
);
1426 /* We set the global icons here, because scr->workspaces[i]->clip
1427 * was not valid in wDockRestoreState().
1428 * There we only set icon->omnipresent to know which icons we
1431 for (j
= 0; j
< scr
->workspaces
[i
]->clip
->max_icons
; j
++) {
1432 WAppIcon
*aicon
= scr
->workspaces
[i
]->clip
->icon_array
[j
];
1435 if (!aicon
|| !aicon
->omnipresent
)
1437 aicon
->omnipresent
= 0;
1438 if (wClipMakeIconOmnipresent(aicon
, True
) != WO_SUCCESS
)
1443 /* Move this appicon from workspace i to workspace 0 */
1444 scr
->workspaces
[i
]->clip
->icon_array
[j
] = NULL
;
1445 scr
->workspaces
[i
]->clip
->icon_count
--;
1447 added_omnipresent_icons
++;
1448 /* If there are too many omnipresent appicons, we are in trouble */
1449 assert(scr
->workspaces
[0]->clip
->icon_count
+ added_omnipresent_icons
1450 <= scr
->workspaces
[0]->clip
->max_icons
);
1451 /* Find first free spot on workspace 0 */
1452 for (k
= 0; k
< scr
->workspaces
[0]->clip
->max_icons
; k
++)
1453 if (scr
->workspaces
[0]->clip
->icon_array
[k
] == NULL
)
1455 scr
->workspaces
[0]->clip
->icon_array
[k
] = aicon
;
1456 aicon
->dock
= scr
->workspaces
[0]->clip
;
1458 scr
->workspaces
[0]->clip
->icon_count
+= added_omnipresent_icons
;
1461 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) i
);