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 int ignore_wks_change
;
58 extern WPreferences wPreferences
;
59 extern XContext wWinContext
;
60 extern XContext wVEdgeContext
;
62 extern void ProcessPendingEvents();
64 static WMPropList
*dWorkspaces
= NULL
;
65 static WMPropList
*dClip
, *dName
;
67 static void make_keys()
69 if (dWorkspaces
!= NULL
)
72 dWorkspaces
= WMCreatePLString("Workspaces");
73 dName
= WMCreatePLString("Name");
74 dClip
= WMCreatePLString("Clip");
77 void wWorkspaceMake(WScreen
* scr
, int count
)
85 int wWorkspaceNew(WScreen
* scr
)
87 WWorkspace
*wspace
, **list
;
90 if (scr
->workspace_count
< MAX_WORKSPACES
) {
91 scr
->workspace_count
++;
93 wspace
= wmalloc(sizeof(WWorkspace
));
97 wspace
->name
= wmalloc(strlen(_("Workspace %i")) + 8);
98 sprintf(wspace
->name
, _("Workspace %i"), scr
->workspace_count
);
101 if (!wPreferences
.flags
.noclip
) {
102 wspace
->clip
= wDockCreate(scr
, WM_CLIP
);
106 list
= wmalloc(sizeof(WWorkspace
*) * scr
->workspace_count
);
108 for (i
= 0; i
< scr
->workspace_count
- 1; i
++) {
109 list
[i
] = scr
->workspaces
[i
];
113 wfree(scr
->workspaces
);
114 scr
->workspaces
= list
;
116 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
117 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
118 #ifdef VIRTUAL_DESKTOP
119 wspace
->view_x
= wspace
->view_y
= 0;
120 wspace
->height
= scr
->scr_height
;
121 wspace
->width
= scr
->scr_width
;
123 wNETWMUpdateDesktop(scr
);
124 WMPostNotificationName(WMNWorkspaceCreated
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
127 return scr
->workspace_count
- 1;
133 Bool
wWorkspaceDelete(WScreen
* scr
, int workspace
)
142 /* verify if workspace is in use by some window */
143 tmp
= scr
->focused_window
;
145 if (!IS_OMNIPRESENT(tmp
) && tmp
->frame
->workspace
== workspace
)
150 if (!wPreferences
.flags
.noclip
) {
151 wDockDestroy(scr
->workspaces
[workspace
]->clip
);
152 scr
->workspaces
[workspace
]->clip
= NULL
;
155 list
= wmalloc(sizeof(WWorkspace
*) * (scr
->workspace_count
- 1));
157 for (i
= 0; i
< scr
->workspace_count
; i
++) {
158 if (i
!= workspace
) {
159 list
[j
++] = scr
->workspaces
[i
];
161 if (scr
->workspaces
[i
]->name
)
162 wfree(scr
->workspaces
[i
]->name
);
163 wfree(scr
->workspaces
[i
]);
166 wfree(scr
->workspaces
);
167 scr
->workspaces
= list
;
169 scr
->workspace_count
--;
172 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
173 /* clip workspace menu */
174 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
176 /* update also window menu */
177 if (scr
->workspace_submenu
) {
178 WMenu
*menu
= scr
->workspace_submenu
;
181 while (i
> scr
->workspace_count
)
182 wMenuRemoveItem(menu
, --i
);
186 if (scr
->clip_submenu
) {
187 WMenu
*menu
= scr
->clip_submenu
;
190 while (i
> scr
->workspace_count
)
191 wMenuRemoveItem(menu
, --i
);
194 wNETWMUpdateDesktop(scr
);
195 WMPostNotificationName(WMNWorkspaceDestroyed
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
197 if (scr
->current_workspace
>= scr
->workspace_count
)
198 wWorkspaceChange(scr
, scr
->workspace_count
- 1);
203 typedef struct WorkspaceNameData
{
210 static void hideWorkspaceName(void *data
)
212 WScreen
*scr
= (WScreen
*) data
;
214 if (!scr
->workspace_name_data
|| scr
->workspace_name_data
->count
== 0
215 || time(NULL
) > scr
->workspace_name_data
->timeout
) {
216 XUnmapWindow(dpy
, scr
->workspace_name
);
218 if (scr
->workspace_name_data
) {
219 RReleaseImage(scr
->workspace_name_data
->back
);
220 RReleaseImage(scr
->workspace_name_data
->text
);
221 wfree(scr
->workspace_name_data
);
223 scr
->workspace_name_data
= NULL
;
225 scr
->workspace_name_timer
= NULL
;
227 RImage
*img
= RCloneImage(scr
->workspace_name_data
->back
);
230 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
232 RCombineImagesWithOpaqueness(img
, scr
->workspace_name_data
->text
,
233 scr
->workspace_name_data
->count
* 255 / 10);
235 RConvertImage(scr
->rcontext
, img
, &pix
);
239 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, pix
);
240 XClearWindow(dpy
, scr
->workspace_name
);
241 XFreePixmap(dpy
, pix
);
244 scr
->workspace_name_data
->count
--;
248 static void showWorkspaceName(WScreen
* scr
, int workspace
)
250 WorkspaceNameData
*data
;
255 char *name
= scr
->workspaces
[workspace
]->name
;
256 int len
= strlen(name
);
264 if (wPreferences
.workspace_name_display_position
== WD_NONE
|| scr
->workspace_count
< 2) {
268 if (scr
->workspace_name_timer
) {
269 WMDeleteTimerHandler(scr
->workspace_name_timer
);
270 XUnmapWindow(dpy
, scr
->workspace_name
);
273 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
, hideWorkspaceName
, scr
);
275 if (scr
->workspace_name_data
) {
276 RReleaseImage(scr
->workspace_name_data
->back
);
277 RReleaseImage(scr
->workspace_name_data
->text
);
278 wfree(scr
->workspace_name_data
);
281 data
= wmalloc(sizeof(WorkspaceNameData
));
284 w
= WMWidthOfString(scr
->workspace_name_font
, name
, len
);
285 h
= WMFontHeight(scr
->workspace_name_font
);
288 head
= wGetHeadForPointerLocation(scr
);
289 rect
= wGetRectForHead(scr
, head
);
290 if (scr
->xine_info
.count
) {
291 xx
= rect
.pos
.x
+ (scr
->xine_info
.screens
[head
].size
.width
- (w
+ 4)) / 2;
292 yy
= rect
.pos
.y
+ (scr
->xine_info
.screens
[head
].size
.height
- (h
+ 4)) / 2;
295 xx
= (scr
->scr_width
- (w
+ 4)) / 2;
296 yy
= (scr
->scr_height
- (h
+ 4)) / 2;
300 switch (wPreferences
.workspace_name_display_position
) {
305 px
= (scr
->scr_width
- (w
+ 4)) / 2;
307 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
313 px
= (scr
->scr_width
- (w
+ 4)) / 2;
315 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
318 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
319 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
322 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
323 py
= WORKSPACE_NAME_DISPLAY_PADDING
;
326 px
= WORKSPACE_NAME_DISPLAY_PADDING
;
327 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
330 px
= scr
->scr_width
- (w
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
331 py
= scr
->scr_height
- (h
+ 4 + WORKSPACE_NAME_DISPLAY_PADDING
);
339 px
= (scr
->scr_width
- (w
+ 4)) / 2;
340 py
= (scr
->scr_height
- (h
+ 4)) / 2;
344 XResizeWindow(dpy
, scr
->workspace_name
, w
+ 4, h
+ 4);
345 XMoveWindow(dpy
, scr
->workspace_name
, px
, py
);
347 text
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, scr
->w_depth
);
348 mask
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, 1);
350 /*XSetForeground(dpy, scr->mono_gc, 0);
351 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
353 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
355 for (x
= 0; x
<= 4; x
++) {
356 for (y
= 0; y
<= 4; y
++) {
357 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, x
, y
, name
, len
);
361 XSetForeground(dpy
, scr
->mono_gc
, 1);
362 XSetBackground(dpy
, scr
->mono_gc
, 0);
364 XCopyPlane(dpy
, text
, mask
, scr
->mono_gc
, 0, 0, w
+ 4, h
+ 4, 0, 0, 1 << (scr
->w_depth
- 1));
366 /*XSetForeground(dpy, scr->mono_gc, 1); */
367 XSetBackground(dpy
, scr
->mono_gc
, 1);
369 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
371 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, 2, 2, name
, len
);
374 XShapeCombineMask(dpy
, scr
->workspace_name
, ShapeBounding
, 0, 0, mask
, ShapeSet
);
376 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, text
);
377 XClearWindow(dpy
, scr
->workspace_name
);
379 data
->text
= RCreateImageFromDrawable(scr
->rcontext
, text
, None
);
381 XFreePixmap(dpy
, text
);
382 XFreePixmap(dpy
, mask
);
385 XMapRaised(dpy
, scr
->workspace_name
);
391 ximg
= RGetXImage(scr
->rcontext
, scr
->root_win
, px
, py
, data
->text
->width
, data
->text
->height
);
393 if (!ximg
|| !ximg
->image
) {
397 XMapRaised(dpy
, scr
->workspace_name
);
400 data
->back
= RCreateImageFromXImage(scr
->rcontext
, ximg
->image
, NULL
);
401 RDestroyXImage(scr
->rcontext
, ximg
);
409 /* set a timeout for the effect */
410 data
->timeout
= time(NULL
) + 2 + (WORKSPACE_NAME_DELAY
+ WORKSPACE_NAME_FADE_DELAY
* data
->count
) / 1000;
412 scr
->workspace_name_data
= data
;
417 if (scr
->workspace_name_timer
)
418 WMDeleteTimerHandler(scr
->workspace_name_timer
);
421 RReleaseImage(data
->text
);
423 RReleaseImage(data
->back
);
426 scr
->workspace_name_data
= NULL
;
428 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
+
429 10 * WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
432 void wWorkspaceChange(WScreen
*scr
, int workspace
)
434 if (scr
->flags
.startup
|| scr
->flags
.startup2
|| scr
->flags
.ignore_focus_events
)
437 if (workspace
!= scr
->current_workspace
)
438 wWorkspaceForceChange(scr
, workspace
);
441 void wWorkspaceRelativeChange(WScreen
* scr
, int amount
)
445 /* While the deiconify animation is going on the window is
446 * still "flying" to its final position and we don't want to
447 * change workspace before the animation finishes, otherwise
448 * the window will land in the new workspace */
449 if (ignore_wks_change
)
452 w
= scr
->current_workspace
+ amount
;
456 wWorkspaceChange(scr
, w
);
457 } else if (wPreferences
.ws_cycle
) {
458 wWorkspaceChange(scr
, scr
->workspace_count
+ w
);
460 } else if (amount
> 0) {
461 if (w
< scr
->workspace_count
) {
462 wWorkspaceChange(scr
, w
);
463 } else if (wPreferences
.ws_advance
) {
464 wWorkspaceChange(scr
, WMIN(w
, MAX_WORKSPACES
- 1));
465 } else if (wPreferences
.ws_cycle
) {
466 wWorkspaceChange(scr
, w
% scr
->workspace_count
);
471 void wWorkspaceForceChange(WScreen
* scr
, int workspace
)
473 WWindow
*tmp
, *foc
= NULL
, *foc2
= NULL
;
475 if (workspace
>= MAX_WORKSPACES
|| workspace
< 0)
478 SendHelperMessage(scr
, 'C', workspace
+ 1, NULL
);
480 if (workspace
> scr
->workspace_count
- 1) {
481 wWorkspaceMake(scr
, workspace
- scr
->workspace_count
+ 1);
484 wClipUpdateForWorkspaceChange(scr
, workspace
);
486 scr
->current_workspace
= workspace
;
488 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
490 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
492 if ((tmp
= scr
->focused_window
) != NULL
) {
493 if ((IS_OMNIPRESENT(tmp
) && (tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
494 !WFLAGP(tmp
, no_focusable
)) || tmp
->flags
.changing_workspace
) {
498 /* foc2 = tmp; will fix annoyance with gnome panel
499 * but will create annoyance for every other application
502 if (tmp
->frame
->workspace
!= workspace
&& !tmp
->flags
.selected
) {
503 /* unmap windows not on this workspace */
504 if ((tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
505 !IS_OMNIPRESENT(tmp
) && !tmp
->flags
.changing_workspace
) {
508 /* also unmap miniwindows not on this workspace */
509 if (!wPreferences
.sticky_icons
&& tmp
->flags
.miniaturized
&&
510 tmp
->icon
&& !IS_OMNIPRESENT(tmp
)) {
511 XUnmapWindow(dpy
, tmp
->icon
->core
->window
);
512 tmp
->icon
->mapped
= 0;
514 /* update current workspace of omnipresent windows */
515 if (IS_OMNIPRESENT(tmp
)) {
516 WApplication
*wapp
= wApplicationOf(tmp
->main_window
);
518 tmp
->frame
->workspace
= workspace
;
521 wapp
->last_workspace
= workspace
;
523 if (!foc2
&& (tmp
->flags
.mapped
|| tmp
->flags
.shaded
)) {
528 /* change selected windows' workspace */
529 if (tmp
->flags
.selected
) {
530 wWindowChangeWorkspace(tmp
, workspace
);
531 if (!tmp
->flags
.miniaturized
&& !foc
) {
535 if (!tmp
->flags
.hidden
) {
536 if (!(tmp
->flags
.mapped
|| tmp
->flags
.miniaturized
)) {
537 /* remap windows that are on this workspace */
539 if (!foc
&& !WFLAGP(tmp
, no_focusable
)) {
543 /* Also map miniwindow if not omnipresent */
544 if (!wPreferences
.sticky_icons
&&
545 tmp
->flags
.miniaturized
&& !IS_OMNIPRESENT(tmp
) && tmp
->icon
) {
546 tmp
->icon
->mapped
= 1;
547 XMapWindow(dpy
, tmp
->icon
->core
->window
);
555 /* Gobble up events unleashed by our mapping & unmapping.
556 * These may trigger various grab-initiated focus &
557 * crossing events. However, we don't care about them,
558 * and ignore their focus implications altogether to avoid
561 scr
->flags
.ignore_focus_events
= 1;
562 ProcessPendingEvents();
563 scr
->flags
.ignore_focus_events
= 0;
568 if (scr
->focused_window
->flags
.mapped
&& !foc
) {
569 foc
= scr
->focused_window
;
571 if (wPreferences
.focus_mode
== WKF_CLICK
) {
572 wSetFocusTo(scr
, foc
);
580 if (XQueryPointer(dpy
, scr
->root_win
, &bar
, &win
, &foo
, &foo
, &foo
, &foo
, &mask
)) {
581 tmp
= wWindowFor(win
);
584 /* If there's a window under the pointer, focus it.
585 * (we ate all other focus events above, so it's
586 * certainly not focused). Otherwise focus last
587 * focused, or the root (depending on sloppiness)
589 if (!tmp
&& wPreferences
.focus_mode
== WKF_SLOPPY
) {
590 wSetFocusTo(scr
, foc
);
592 wSetFocusTo(scr
, tmp
);
597 /* We need to always arrange icons when changing workspace, even if
598 * no autoarrange icons, because else the icons in different workspaces
600 * This can be avoided if appicons are also workspace specific.
602 if (!wPreferences
.sticky_icons
)
603 wArrangeIcons(scr
, False
);
606 wAppIconPaint(scr
->dock
->icon_array
[0]);
608 if (scr
->clip_icon
) {
609 if (scr
->workspaces
[workspace
]->clip
->auto_collapse
||
610 scr
->workspaces
[workspace
]->clip
->auto_raise_lower
) {
611 /* to handle enter notify. This will also */
612 XUnmapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
613 XMapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
615 wClipIconPaint(scr
->clip_icon
);
618 wScreenUpdateUsableArea(scr
);
619 wNETWMUpdateDesktop(scr
);
620 showWorkspaceName(scr
, workspace
);
622 WMPostNotificationName(WMNWorkspaceChanged
, scr
, (void *)(uintptr_t) workspace
);
624 /* XSync(dpy, False); */
627 #ifdef VIRTUAL_DESKTOP
631 * 1) Allow border around each window so the scrolling
632 * won't just stop at the border.
637 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
638 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
639 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
640 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
641 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
642 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
645 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
649 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
652 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
654 static Bool
cmp_gez(int i
)
659 static Bool
cmp_gz(int i
)
664 static WMPoint
getClosestEdge(WScreen
* scr
, WMPoint direction
, Bool(*cmp
) (int))
666 WMPoint closest
= wmkpoint(0, 0);
667 int closest_len
= INT_MAX
;
670 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
671 if (wwin
->frame
->workspace
== scr
->current_workspace
) {
672 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) && !wwin
->flags
.hidden
) {
673 int border
= 2 * HAS_BORDER(wwin
);
676 int head
= wGetHeadForWindow(wwin
);
677 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
680 x1
= wwin
->frame_x
- area
.x1
;
681 y1
= wwin
->frame_y
- area
.y1
;
682 x2
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ border
- area
.x2
;
683 y2
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ border
- area
.y2
;
685 p
= wmkpoint(x1
, y1
);
686 len
= vec_dot(direction
, p
);
687 updateMinimum(len
, p
, closest_len
, closest
);
689 p
= wmkpoint(x1
, y2
);
690 len
= vec_dot(direction
, p
);
691 updateMinimum(len
, p
, closest_len
, closest
);
693 p
= wmkpoint(x2
, y1
);
694 len
= vec_dot(direction
, p
);
695 updateMinimum(len
, p
, closest_len
, closest
);
697 p
= wmkpoint(x2
, y2
);
698 len
= vec_dot(direction
, p
);
699 updateMinimum(len
, p
, closest_len
, closest
);
707 static void getViewPosition(WScreen
* scr
, int workspace
, int *x
, int *y
)
709 *x
= scr
->workspaces
[workspace
]->view_x
;
710 *y
= scr
->workspaces
[workspace
]->view_y
;
713 void wWorkspaceKeyboardMoveDesktop(WScreen
* scr
, WMPoint direction
)
716 WMPoint edge
= getClosestEdge(scr
, direction
, cmp_gz
);
717 int len
= vec_dot(edge
, direction
);
718 WMPoint step
= vec_scale(direction
, len
);
719 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
720 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
+ step
.x
, y
+ step
.y
);
723 extern Cursor wCursor
[WCUR_LAST
];
725 static void vdMouseMoveDesktop(XEvent
* event
, WMPoint direction
)
727 static int lock
= False
;
734 WScreen
*scr
= wScreenForRootWindow(event
->xcrossing
.root
);
735 WMPoint old_pos
= wmkpoint(event
->xcrossing
.x_root
, event
->xcrossing
.y_root
);
740 if (XGrabPointer(dpy
, event
->xcrossing
.window
, False
,
741 PointerMotionMask
, GrabModeAsync
, GrabModeAsync
,
742 scr
->root_win
, wCursor
[WCUR_EMPTY
], CurrentTime
) != GrabSuccess
) {
744 /* if the grab fails, do it the old fashioned way */
745 step
= vec_scale2(direction
, wPreferences
.vedge_hscrollspeed
, wPreferences
.vedge_vscrollspeed
);
746 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
747 if (wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
+ step
.x
, y
+ step
.y
)) {
748 step
= vec_scale(direction
, 2);
749 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
750 event
->xcrossing
.x_root
- step
.x
, event
->xcrossing
.y_root
- step
.y
);
760 if (old_pos
.x
> scr
->scr_width
)
761 old_pos
.x
= scr
->scr_width
;
762 if (old_pos
.y
> scr
->scr_height
)
763 old_pos
.y
= scr
->scr_height
;
768 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
769 scr
->scr_width
/ 2, scr
->scr_height
/ 2);
772 WMMaskEvent(dpy
, PointerMotionMask
, &ev
);
778 step
= wmkpoint(ev
.xmotion
.x_root
- scr
->scr_width
/ 2,
779 ev
.xmotion
.y_root
- scr
->scr_height
/ 2);
780 step_len
= vec_dot(step
, direction
);
788 int resist
= wPreferences
.vedge_resistance
;
790 int closest_len
= INT_MAX
;
792 closest
= getClosestEdge(scr
, direction
, cmp_gez
);
793 closest_len
= vec_dot(direction
, closest
);
797 resisted
+= step_len
;
798 do_move
= resisted
>= resist
;
800 closest_len
= INT_MAX
;
801 step_len
= resisted
- resist
;
806 if (closest_len
<= wPreferences
.vedge_attraction
) {
807 step
= vec_scale(direction
, closest_len
);
809 step
= vec_scale(direction
, step_len
);
812 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
813 wWorkspaceSetViewport(scr
, scr
->current_workspace
,
814 x
+ step
.x
, y
+ step
.y
);
823 step
= vec_add(old_pos
, vec_scale(direction
, -1));
824 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0, step
.x
, step
.y
);
825 XUngrabPointer(dpy
, CurrentTime
);
831 static void vdHandleEnter_u(XEvent
* event
)
833 vdMouseMoveDesktop(event
, VEC_UP
);
836 static void vdHandleEnter_d(XEvent
* event
)
838 vdMouseMoveDesktop(event
, VEC_DOWN
);
841 static void vdHandleEnter_l(XEvent
* event
)
843 vdMouseMoveDesktop(event
, VEC_LEFT
);
846 static void vdHandleEnter_r(XEvent
* event
)
848 vdMouseMoveDesktop(event
, VEC_RIGHT
);
851 #define LEFT_EDGE 0x01
852 #define RIGHT_EDGE 0x02
853 #define TOP_EDGE 0x04
854 #define BOTTOM_EDGE 0x08
855 #define ALL_EDGES 0x0F
857 static void createEdges(WScreen
* scr
)
859 if (!scr
->virtual_edges
) {
862 XSetWindowAttributes attribs
;
864 int heads
= wXineramaHeads(scr
);
865 int *hasEdges
= (int *)wmalloc(sizeof(int) * heads
);
869 int max_edges
= 4 * heads
;
871 Window
*edges
= (Window
*) wmalloc(sizeof(Window
) * max_edges
);
873 for (i
= 0; i
< heads
; ++i
)
874 hasEdges
[i
] = ALL_EDGES
;
875 for (i
= 0; i
< heads
; ++i
) {
876 WMRect i_rect
= wGetRectForHead(scr
, i
);
877 for (j
= i
+ 1; j
< heads
; ++j
) {
878 WMRect j_rect
= wGetRectForHead(scr
, j
);
880 int vlen
= (WMIN(i_rect
.pos
.y
+ i_rect
.size
.height
,
881 j_rect
.pos
.y
+ j_rect
.size
.height
) -
882 WMAX(i_rect
.pos
.y
, j_rect
.pos
.y
));
884 int hlen
= (WMIN(i_rect
.pos
.x
+ i_rect
.size
.width
,
885 j_rect
.pos
.x
+ j_rect
.size
.width
) -
886 WMAX(i_rect
.pos
.x
, j_rect
.pos
.x
));
888 if (vlen
> 0 && hlen
== 0) { /* horz alignment, vert edges touch */
889 if (i_rect
.pos
.x
< j_rect
.pos
.x
) { /* i left of j */
890 hasEdges
[i
] &= ~RIGHT_EDGE
;
891 hasEdges
[j
] &= ~LEFT_EDGE
;
892 } else { /* j left of i */
893 hasEdges
[j
] &= ~RIGHT_EDGE
;
894 hasEdges
[i
] &= ~LEFT_EDGE
;
896 } else if (vlen
== 0 && hlen
> 0) { /* vert alignment, horz edges touch */
897 if (i_rect
.pos
.y
< j_rect
.pos
.y
) { /* i top of j */
898 hasEdges
[i
] &= ~BOTTOM_EDGE
;
899 hasEdges
[j
] &= ~TOP_EDGE
;
900 } else { /* j top of i */
901 hasEdges
[j
] &= ~BOTTOM_EDGE
;
902 hasEdges
[i
] &= ~TOP_EDGE
;
908 for (w
= 0; w
< scr
->workspace_count
; w
++) {
909 /* puts("reset workspace"); */
910 wWorkspaceSetViewport(scr
, w
, 0, 0);
913 vmask
= CWEventMask
| CWOverrideRedirect
;
914 attribs
.event_mask
= (EnterWindowMask
| LeaveWindowMask
| VisibilityChangeMask
);
915 attribs
.override_redirect
= True
;
917 for (head
= 0; head
< wXineramaHeads(scr
); ++head
) {
918 WMRect rect
= wGetRectForHead(scr
, head
);
920 if (hasEdges
[head
] & TOP_EDGE
) {
922 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
923 rect
.size
.width
, thickness
, 0,
924 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
925 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_u
);
929 if (hasEdges
[head
] & BOTTOM_EDGE
) {
931 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
,
932 rect
.pos
.y
+ rect
.size
.height
- thickness
,
933 rect
.size
.width
, thickness
, 0,
934 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
935 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_d
);
939 if (hasEdges
[head
] & LEFT_EDGE
) {
941 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
942 thickness
, rect
.pos
.y
+ rect
.size
.height
, 0,
943 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
944 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_l
);
948 if (hasEdges
[head
] & RIGHT_EDGE
) {
950 XCreateWindow(dpy
, scr
->root_win
,
951 rect
.pos
.x
+ rect
.size
.width
- thickness
, rect
.pos
.y
,
952 thickness
, rect
.size
.height
, 0,
953 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
954 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_r
);
959 scr
->virtual_nr_edges
= nr_edges
;
960 scr
->virtual_edges
= edges
;
962 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
963 XMapWindow(dpy
, scr
->virtual_edges
[i
]);
965 wWorkspaceRaiseEdge(scr
);
971 static void destroyEdges(WScreen
* scr
)
973 if (scr
->virtual_edges
) {
976 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
977 XDeleteContext(dpy
, scr
->virtual_edges
[i
], wVEdgeContext
);
978 XUnmapWindow(dpy
, scr
->virtual_edges
[i
]);
979 XDestroyWindow(dpy
, scr
->virtual_edges
[i
]);
982 wfree(scr
->virtual_edges
);
983 scr
->virtual_edges
= NULL
;
984 scr
->virtual_nr_edges
= 0;
988 void wWorkspaceUpdateEdge(WScreen
* scr
)
990 if (wPreferences
.vdesk_enable
) {
997 void wWorkspaceRaiseEdge(WScreen
* scr
)
999 static int toggle
= 0;
1002 if (!scr
->virtual_edges
)
1006 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
1007 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1010 for (i
= scr
->virtual_nr_edges
- 1; i
>= 0; --i
) {
1011 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1018 void wWorkspaceLowerEdge(WScreen
* scr
)
1021 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
1022 XLowerWindow(dpy
, scr
->virtual_edges
[i
]);
1026 void wWorkspaceResizeViewport(WScreen
* scr
, int workspace
)
1029 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
1030 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
, y
);
1033 void updateWorkspaceGeometry(WScreen
* scr
, int workspace
, int *view_x
, int *view_y
)
1035 int most_left
, most_right
, most_top
, most_bottom
;
1038 int heads
= wXineramaHeads(scr
);
1039 typedef int strut_t
[4];
1040 strut_t
*strut
= (strut_t
*) wmalloc(heads
* sizeof(strut_t
));
1043 for (head
= 0; head
< heads
; ++head
) {
1044 WMRect rect
= wGetRectForHead(scr
, head
);
1045 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
1046 strut
[head
][0] = area
.x1
- rect
.pos
.x
;
1047 strut
[head
][1] = rect
.pos
.x
+ rect
.size
.width
- area
.x2
;
1048 strut
[head
][2] = area
.y1
- rect
.pos
.y
;
1049 strut
[head
][3] = rect
.pos
.y
+ rect
.size
.height
- area
.y2
;
1052 /* adjust workspace layout */
1053 wwin
= scr
->focused_window
;
1056 most_left
= scr
->scr_width
;
1057 most_top
= scr
->scr_height
;
1058 for (; wwin
; wwin
= wwin
->prev
) {
1059 if (wwin
->frame
->workspace
== workspace
) {
1060 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) && !wwin
->flags
.hidden
) {
1062 head
= wGetHeadForWindow(wwin
);
1064 i
= wwin
->frame_x
- strut
[head
][0];
1065 if (i
< most_left
) /* record positions, should this be cached? */
1067 i
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ strut
[head
][1];
1068 if (HAS_BORDER(wwin
))
1072 i
= wwin
->frame_y
- strut
[head
][2];
1075 i
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ strut
[head
][3];
1076 if (HAS_BORDER(wwin
))
1078 if (i
> most_bottom
) {
1090 scr
->workspaces
[workspace
]->width
= WMAX(most_right
, scr
->scr_width
) - WMIN(most_left
, 0);
1091 scr
->workspaces
[workspace
]->height
= WMAX(most_bottom
, scr
->scr_height
) - WMIN(most_top
, 0);
1093 *view_x
+= -most_left
- scr
->workspaces
[workspace
]->view_x
;
1094 scr
->workspaces
[workspace
]->view_x
= -most_left
;
1096 *view_y
+= -most_top
- scr
->workspaces
[workspace
]->view_y
;
1097 scr
->workspaces
[workspace
]->view_y
= -most_top
;
1102 typedef struct _delay_configure
{
1107 void sendConfigureNotify(_delay_configure
* delay
)
1111 delay
->delay_count
--;
1112 if (!delay
->delay_count
) {
1113 for (wwin
= delay
->wwin
; wwin
; wwin
= wwin
->prev
) {
1114 wWindowSynthConfigureNotify(wwin
);
1119 Bool
wWorkspaceSetViewport(WScreen
* scr
, int workspace
, int view_x
, int view_y
)
1121 Bool adjust_flag
= False
;
1123 static _delay_configure delay_configure
= { NULL
, 0 };
1127 wptr
= scr
->workspaces
[workspace
];
1129 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y); */
1131 updateWorkspaceGeometry(scr
, workspace
, &view_x
, &view_y
);
1133 if (view_x
+ scr
->scr_width
> wptr
->width
) {
1134 /* puts("right edge of vdesk"); */
1135 view_x
= wptr
->width
- scr
->scr_width
;
1138 /* puts("left edge of vdesk"); */
1141 if (view_y
+ scr
->scr_height
> wptr
->height
) {
1142 /* puts("right edge of vdesk"); */
1143 view_y
= wptr
->height
- scr
->scr_height
;
1146 /* puts("left edge of vdesk"); */
1150 diff_x
= wptr
->view_x
- view_x
;
1151 diff_y
= wptr
->view_y
- view_y
;
1152 if (!diff_x
&& !diff_y
)
1155 wptr
->view_x
= view_x
;
1156 wptr
->view_y
= view_y
;
1158 wNETWMUpdateDesktop(scr
);
1160 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
1161 if (wwin
->frame
->workspace
== workspace
&& !IS_VSTUCK(wwin
)) {
1162 wWindowMove(wwin
, wwin
->frame_x
+ diff_x
, wwin
->frame_y
+ diff_y
);
1166 if (1) { /* if delay */
1167 delay_configure
.delay_count
++;
1168 delay_configure
.wwin
= scr
->focused_window
;
1169 WMAddTimerHandler(200, (WMCallback
*) sendConfigureNotify
, &delay_configure
);
1177 static void switchWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
1179 wWorkspaceChange(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
1182 static void deleteWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
1184 wWorkspaceDelete(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->workspace_count
- 1);
1187 static void newWSCommand(WMenu
* menu
, WMenuEntry
* foo
)
1191 ws
= wWorkspaceNew(menu
->frame
->screen_ptr
);
1192 /* autochange workspace */
1194 wWorkspaceChange(menu
->frame
->screen_ptr
, ws
);
1199 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1200 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1202 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1207 static char *cropline(char *line
)
1211 if (strlen(line
) == 0)
1215 end
= &(line
[strlen(line
)]) - 1;
1216 while (isspace(*line
) && *line
!= 0)
1218 while (isspace(*end
) && end
!= line
) {
1225 void wWorkspaceRename(WScreen
* scr
, int workspace
, char *name
)
1227 char buf
[MAX_WORKSPACENAME_WIDTH
+ 1];
1230 if (workspace
>= scr
->workspace_count
)
1233 /* trim white spaces */
1234 tmp
= cropline(name
);
1236 if (strlen(tmp
) == 0) {
1237 snprintf(buf
, sizeof(buf
), _("Workspace %i"), workspace
+ 1);
1239 strncpy(buf
, tmp
, MAX_WORKSPACENAME_WIDTH
);
1241 buf
[MAX_WORKSPACENAME_WIDTH
] = 0;
1243 /* update workspace */
1244 wfree(scr
->workspaces
[workspace
]->name
);
1245 scr
->workspaces
[workspace
]->name
= wstrdup(buf
);
1247 if (scr
->clip_ws_menu
) {
1248 if (strcmp(scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
, buf
) != 0) {
1249 wfree(scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
);
1250 scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
= wstrdup(buf
);
1251 wMenuRealize(scr
->clip_ws_menu
);
1254 if (scr
->workspace_menu
) {
1255 if (strcmp(scr
->workspace_menu
->entries
[workspace
+ 2]->text
, buf
) != 0) {
1256 wfree(scr
->workspace_menu
->entries
[workspace
+ 2]->text
);
1257 scr
->workspace_menu
->entries
[workspace
+ 2]->text
= wstrdup(buf
);
1258 wMenuRealize(scr
->workspace_menu
);
1263 wClipIconPaint(scr
->clip_icon
);
1265 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) workspace
);
1268 /* callback for when menu entry is edited */
1269 static void onMenuEntryEdited(WMenu
* menu
, WMenuEntry
* entry
)
1274 wWorkspaceRename(menu
->frame
->screen_ptr
, (long)entry
->clientdata
, tmp
);
1277 WMenu
*wWorkspaceMenuMake(WScreen
* scr
, Bool titled
)
1281 wsmenu
= wMenuCreate(scr
, titled
? _("Workspaces") : NULL
, False
);
1283 wwarning(_("could not create Workspace menu"));
1287 /* callback to be called when an entry is edited */
1288 wsmenu
->on_edit
= onMenuEntryEdited
;
1290 wMenuAddCallback(wsmenu
, _("New"), newWSCommand
, NULL
);
1291 wMenuAddCallback(wsmenu
, _("Destroy Last"), deleteWSCommand
, NULL
);
1296 void wWorkspaceMenuUpdate(WScreen
* scr
, WMenu
* menu
)
1300 char title
[MAX_WORKSPACENAME_WIDTH
+ 1];
1307 if (menu
->entry_no
< scr
->workspace_count
+ 2) {
1308 /* new workspace(s) added */
1309 i
= scr
->workspace_count
- (menu
->entry_no
- 2);
1310 ws
= menu
->entry_no
- 2;
1312 strncpy(title
, scr
->workspaces
[ws
]->name
, MAX_WORKSPACENAME_WIDTH
);
1314 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, (void *)ws
);
1315 entry
->flags
.indicator
= 1;
1316 entry
->flags
.editable
= 1;
1321 } else if (menu
->entry_no
> scr
->workspace_count
+ 2) {
1322 /* removed workspace(s) */
1323 for (i
= menu
->entry_no
- 1; i
>= scr
->workspace_count
+ 2; i
--) {
1324 wMenuRemoveItem(menu
, i
);
1329 for (i
= 0; i
< scr
->workspace_count
; i
++) {
1330 menu
->entries
[i
+ 2]->flags
.indicator_on
= 0;
1332 menu
->entries
[scr
->current_workspace
+ 2]->flags
.indicator_on
= 1;
1334 /* don't let user destroy current workspace */
1335 if (scr
->current_workspace
== scr
->workspace_count
- 1) {
1336 wMenuSetEnabled(menu
, 1, False
);
1338 wMenuSetEnabled(menu
, 1, True
);
1341 tmp
= menu
->frame
->top_width
+ 5;
1342 /* if menu got unreachable, bring it to a visible place */
1343 if (menu
->frame_x
< tmp
- (int)menu
->frame
->core
->width
)
1344 wMenuMove(menu
, tmp
- (int)menu
->frame
->core
->width
, menu
->frame_y
, False
);
1349 void wWorkspaceSaveState(WScreen
* scr
, WMPropList
* old_state
)
1351 WMPropList
*parr
, *pstr
, *wks_state
, *old_wks_state
, *foo
, *bar
;
1356 old_wks_state
= WMGetFromPLDictionary(old_state
, dWorkspaces
);
1357 parr
= WMCreatePLArray(NULL
);
1358 for (i
= 0; i
< scr
->workspace_count
; i
++) {
1359 pstr
= WMCreatePLString(scr
->workspaces
[i
]->name
);
1360 wks_state
= WMCreatePLDictionary(dName
, pstr
, NULL
);
1361 WMReleasePropList(pstr
);
1362 if (!wPreferences
.flags
.noclip
) {
1363 pstr
= wClipSaveWorkspaceState(scr
, i
);
1364 WMPutInPLDictionary(wks_state
, dClip
, pstr
);
1365 WMReleasePropList(pstr
);
1366 } else if (old_wks_state
!= NULL
) {
1367 if ((foo
= WMGetFromPLArray(old_wks_state
, i
)) != NULL
) {
1368 if ((bar
= WMGetFromPLDictionary(foo
, dClip
)) != NULL
) {
1369 WMPutInPLDictionary(wks_state
, dClip
, bar
);
1373 WMAddToPLArray(parr
, wks_state
);
1374 WMReleasePropList(wks_state
);
1376 WMPutInPLDictionary(scr
->session_state
, dWorkspaces
, parr
);
1377 WMReleasePropList(parr
);
1380 void wWorkspaceRestoreState(WScreen
* scr
)
1382 WMPropList
*parr
, *pstr
, *wks_state
, *clip_state
;
1387 if (scr
->session_state
== NULL
)
1390 parr
= WMGetFromPLDictionary(scr
->session_state
, dWorkspaces
);
1395 wscount
= scr
->workspace_count
;
1396 for (i
= 0; i
< WMIN(WMGetPropListItemCount(parr
), MAX_WORKSPACES
); i
++) {
1397 wks_state
= WMGetFromPLArray(parr
, i
);
1398 if (WMIsPLDictionary(wks_state
))
1399 pstr
= WMGetFromPLDictionary(wks_state
, dName
);
1402 if (i
>= scr
->workspace_count
)
1404 if (scr
->workspace_menu
) {
1405 wfree(scr
->workspace_menu
->entries
[i
+ 2]->text
);
1406 scr
->workspace_menu
->entries
[i
+ 2]->text
= wstrdup(WMGetFromPLString(pstr
));
1407 scr
->workspace_menu
->flags
.realized
= 0;
1409 wfree(scr
->workspaces
[i
]->name
);
1410 scr
->workspaces
[i
]->name
= wstrdup(WMGetFromPLString(pstr
));
1411 if (!wPreferences
.flags
.noclip
) {
1412 int added_omnipresent_icons
= 0;
1413 clip_state
= WMGetFromPLDictionary(wks_state
, dClip
);
1414 if (scr
->workspaces
[i
]->clip
)
1415 wDockDestroy(scr
->workspaces
[i
]->clip
);
1416 scr
->workspaces
[i
]->clip
= wDockRestoreState(scr
, clip_state
, WM_CLIP
);
1418 wDockHideIcons(scr
->workspaces
[i
]->clip
);
1420 /* We set the global icons here, because scr->workspaces[i]->clip
1421 * was not valid in wDockRestoreState().
1422 * There we only set icon->omnipresent to know which icons we
1425 for (j
= 0; j
< scr
->workspaces
[i
]->clip
->max_icons
; j
++) {
1426 WAppIcon
*aicon
= scr
->workspaces
[i
]->clip
->icon_array
[j
];
1429 if (!aicon
|| !aicon
->omnipresent
)
1431 aicon
->omnipresent
= 0;
1432 if (wClipMakeIconOmnipresent(aicon
, True
) != WO_SUCCESS
)
1437 /* Move this appicon from workspace i to workspace 0 */
1438 scr
->workspaces
[i
]->clip
->icon_array
[j
] = NULL
;
1439 scr
->workspaces
[i
]->clip
->icon_count
--;
1441 added_omnipresent_icons
++;
1442 /* If there are too many omnipresent appicons, we are in trouble */
1443 assert(scr
->workspaces
[0]->clip
->icon_count
+ added_omnipresent_icons
1444 <= scr
->workspaces
[0]->clip
->max_icons
);
1445 /* Find first free spot on workspace 0 */
1446 for (k
= 0; k
< scr
->workspaces
[0]->clip
->max_icons
; k
++)
1447 if (scr
->workspaces
[0]->clip
->icon_array
[k
] == NULL
)
1449 scr
->workspaces
[0]->clip
->icon_array
[k
] = aicon
;
1450 aicon
->dock
= scr
->workspaces
[0]->clip
;
1452 scr
->workspaces
[0]->clip
->icon_count
+= added_omnipresent_icons
;
1455 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) i
);