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
59 extern WPreferences wPreferences
;
60 extern XContext wWinContext
;
61 extern XContext wVEdgeContext
;
63 extern void ProcessPendingEvents();
65 static WMPropList
*dWorkspaces
= NULL
;
66 static WMPropList
*dClip
, *dName
;
68 static void make_keys()
70 if (dWorkspaces
!= NULL
)
73 dWorkspaces
= WMCreatePLString("Workspaces");
74 dName
= WMCreatePLString("Name");
75 dClip
= WMCreatePLString("Clip");
78 void wWorkspaceMake(WScreen
* scr
, int count
)
86 int wWorkspaceNew(WScreen
* scr
)
88 WWorkspace
*wspace
, **list
;
91 if (scr
->workspace_count
< MAX_WORKSPACES
) {
92 scr
->workspace_count
++;
94 wspace
= wmalloc(sizeof(WWorkspace
));
98 wspace
->name
= wmalloc(strlen(_("Workspace %i")) + 8);
99 sprintf(wspace
->name
, _("Workspace %i"), scr
->workspace_count
);
102 if (!wPreferences
.flags
.noclip
) {
103 wspace
->clip
= wDockCreate(scr
, WM_CLIP
);
107 list
= wmalloc(sizeof(WWorkspace
*) * scr
->workspace_count
);
109 for (i
= 0; i
< scr
->workspace_count
- 1; i
++) {
110 list
[i
] = scr
->workspaces
[i
];
114 wfree(scr
->workspaces
);
115 scr
->workspaces
= list
;
117 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
118 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
119 #ifdef VIRTUAL_DESKTOP
120 wspace
->view_x
= wspace
->view_y
= 0;
121 wspace
->height
= scr
->scr_height
;
122 wspace
->width
= scr
->scr_width
;
125 wNETWMUpdateDesktop(scr
);
128 WMPostNotificationName(WMNWorkspaceCreated
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
131 return scr
->workspace_count
- 1;
137 Bool
wWorkspaceDelete(WScreen
* scr
, int workspace
)
146 /* verify if workspace is in use by some window */
147 tmp
= scr
->focused_window
;
149 if (!IS_OMNIPRESENT(tmp
) && tmp
->frame
->workspace
== workspace
)
154 if (!wPreferences
.flags
.noclip
) {
155 wDockDestroy(scr
->workspaces
[workspace
]->clip
);
156 scr
->workspaces
[workspace
]->clip
= NULL
;
159 list
= wmalloc(sizeof(WWorkspace
*) * (scr
->workspace_count
- 1));
161 for (i
= 0; i
< scr
->workspace_count
; i
++) {
162 if (i
!= workspace
) {
163 list
[j
++] = scr
->workspaces
[i
];
165 if (scr
->workspaces
[i
]->name
)
166 wfree(scr
->workspaces
[i
]->name
);
167 wfree(scr
->workspaces
[i
]);
170 wfree(scr
->workspaces
);
171 scr
->workspaces
= list
;
173 scr
->workspace_count
--;
176 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
177 /* clip workspace menu */
178 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
180 /* update also window menu */
181 if (scr
->workspace_submenu
) {
182 WMenu
*menu
= scr
->workspace_submenu
;
185 while (i
> scr
->workspace_count
)
186 wMenuRemoveItem(menu
, --i
);
190 if (scr
->clip_submenu
) {
191 WMenu
*menu
= scr
->clip_submenu
;
194 while (i
> scr
->workspace_count
)
195 wMenuRemoveItem(menu
, --i
);
199 wNETWMUpdateDesktop(scr
);
202 WMPostNotificationName(WMNWorkspaceDestroyed
, scr
, (void *)(uintptr_t) (scr
->workspace_count
- 1));
204 if (scr
->current_workspace
>= scr
->workspace_count
)
205 wWorkspaceChange(scr
, scr
->workspace_count
- 1);
210 typedef struct WorkspaceNameData
{
217 static void hideWorkspaceName(void *data
)
219 WScreen
*scr
= (WScreen
*) data
;
221 if (!scr
->workspace_name_data
|| scr
->workspace_name_data
->count
== 0
222 || time(NULL
) > scr
->workspace_name_data
->timeout
) {
223 XUnmapWindow(dpy
, scr
->workspace_name
);
225 if (scr
->workspace_name_data
) {
226 RReleaseImage(scr
->workspace_name_data
->back
);
227 RReleaseImage(scr
->workspace_name_data
->text
);
228 wfree(scr
->workspace_name_data
);
230 scr
->workspace_name_data
= NULL
;
232 scr
->workspace_name_timer
= NULL
;
234 RImage
*img
= RCloneImage(scr
->workspace_name_data
->back
);
237 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
239 RCombineImagesWithOpaqueness(img
, scr
->workspace_name_data
->text
,
240 scr
->workspace_name_data
->count
* 255 / 10);
242 RConvertImage(scr
->rcontext
, img
, &pix
);
246 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, pix
);
247 XClearWindow(dpy
, scr
->workspace_name
);
248 XFreePixmap(dpy
, pix
);
251 scr
->workspace_name_data
->count
--;
255 static void showWorkspaceName(WScreen
* scr
, int workspace
)
257 WorkspaceNameData
*data
;
262 char *name
= scr
->workspaces
[workspace
]->name
;
263 int len
= strlen(name
);
266 if (wPreferences
.workspace_name_display_position
== WD_NONE
|| scr
->workspace_count
< 2) {
270 if (scr
->workspace_name_timer
) {
271 WMDeleteTimerHandler(scr
->workspace_name_timer
);
272 XUnmapWindow(dpy
, scr
->workspace_name
);
275 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
, hideWorkspaceName
, scr
);
277 if (scr
->workspace_name_data
) {
278 RReleaseImage(scr
->workspace_name_data
->back
);
279 RReleaseImage(scr
->workspace_name_data
->text
);
280 wfree(scr
->workspace_name_data
);
283 data
= wmalloc(sizeof(WorkspaceNameData
));
286 w
= WMWidthOfString(scr
->workspace_name_font
, name
, len
);
287 h
= WMFontHeight(scr
->workspace_name_font
);
289 switch (wPreferences
.workspace_name_display_position
) {
291 px
= (scr
->scr_width
- (w
+ 4)) / 2;
295 px
= (scr
->scr_width
- (w
+ 4)) / 2;
296 py
= scr
->scr_height
- (h
+ 4);
303 px
= scr
->scr_width
- (w
+ 4);
308 py
= scr
->scr_height
- (h
+ 4);
311 px
= scr
->scr_width
- (w
+ 4);
312 py
= scr
->scr_height
- (h
+ 4);
316 px
= (scr
->scr_width
- (w
+ 4)) / 2;
317 py
= (scr
->scr_height
- (h
+ 4)) / 2;
320 XResizeWindow(dpy
, scr
->workspace_name
, w
+ 4, h
+ 4);
321 XMoveWindow(dpy
, scr
->workspace_name
, px
, py
);
323 text
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, scr
->w_depth
);
324 mask
= XCreatePixmap(dpy
, scr
->w_win
, w
+ 4, h
+ 4, 1);
326 /*XSetForeground(dpy, scr->mono_gc, 0);
327 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
329 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
331 for (x
= 0; x
<= 4; x
++) {
332 for (y
= 0; y
<= 4; y
++) {
333 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, x
, y
, name
, len
);
337 XSetForeground(dpy
, scr
->mono_gc
, 1);
338 XSetBackground(dpy
, scr
->mono_gc
, 0);
340 XCopyPlane(dpy
, text
, mask
, scr
->mono_gc
, 0, 0, w
+ 4, h
+ 4, 0, 0, 1 << (scr
->w_depth
- 1));
342 /*XSetForeground(dpy, scr->mono_gc, 1); */
343 XSetBackground(dpy
, scr
->mono_gc
, 1);
345 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+ 4, h
+ 4);
347 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
, 2, 2, name
, len
);
350 XShapeCombineMask(dpy
, scr
->workspace_name
, ShapeBounding
, 0, 0, mask
, ShapeSet
);
352 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, text
);
353 XClearWindow(dpy
, scr
->workspace_name
);
355 data
->text
= RCreateImageFromDrawable(scr
->rcontext
, text
, None
);
357 XFreePixmap(dpy
, text
);
358 XFreePixmap(dpy
, mask
);
361 XMapRaised(dpy
, scr
->workspace_name
);
367 ximg
= RGetXImage(scr
->rcontext
, scr
->root_win
, px
, py
, data
->text
->width
, data
->text
->height
);
369 if (!ximg
|| !ximg
->image
) {
373 XMapRaised(dpy
, scr
->workspace_name
);
376 data
->back
= RCreateImageFromXImage(scr
->rcontext
, ximg
->image
, NULL
);
377 RDestroyXImage(scr
->rcontext
, ximg
);
385 /* set a timeout for the effect */
386 data
->timeout
= time(NULL
) + 2 + (WORKSPACE_NAME_DELAY
+ WORKSPACE_NAME_FADE_DELAY
* data
->count
) / 1000;
388 scr
->workspace_name_data
= data
;
393 if (scr
->workspace_name_timer
)
394 WMDeleteTimerHandler(scr
->workspace_name_timer
);
397 RReleaseImage(data
->text
);
399 RReleaseImage(data
->back
);
402 scr
->workspace_name_data
= NULL
;
404 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
+
405 10 * WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
408 void wWorkspaceChange(WScreen
* scr
, int workspace
)
410 if (scr
->flags
.startup
|| scr
->flags
.startup2
) {
414 if (workspace
!= scr
->current_workspace
) {
415 wWorkspaceForceChange(scr
, workspace
);
417 showWorkspaceName(scr, workspace);
421 void wWorkspaceRelativeChange(WScreen
* scr
, int amount
)
425 w
= scr
->current_workspace
+ amount
;
429 wWorkspaceChange(scr
, w
);
430 } else if (wPreferences
.ws_cycle
) {
431 wWorkspaceChange(scr
, scr
->workspace_count
+ w
);
433 } else if (amount
> 0) {
434 if (w
< scr
->workspace_count
) {
435 wWorkspaceChange(scr
, w
);
436 } else if (wPreferences
.ws_advance
) {
437 wWorkspaceChange(scr
, WMIN(w
, MAX_WORKSPACES
- 1));
438 } else if (wPreferences
.ws_cycle
) {
439 wWorkspaceChange(scr
, w
% scr
->workspace_count
);
444 void wWorkspaceForceChange(WScreen
* scr
, int workspace
)
446 WWindow
*tmp
, *foc
= NULL
, *foc2
= NULL
;
448 if (workspace
>= MAX_WORKSPACES
|| workspace
< 0)
451 SendHelperMessage(scr
, 'C', workspace
+ 1, NULL
);
453 if (workspace
> scr
->workspace_count
- 1) {
454 wWorkspaceMake(scr
, workspace
- scr
->workspace_count
+ 1);
457 wClipUpdateForWorkspaceChange(scr
, workspace
);
459 scr
->current_workspace
= workspace
;
461 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
463 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
465 if ((tmp
= scr
->focused_window
) != NULL
) {
466 if ((IS_OMNIPRESENT(tmp
) && (tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
467 !WFLAGP(tmp
, no_focusable
)) || tmp
->flags
.changing_workspace
) {
471 /* foc2 = tmp; will fix annoyance with gnome panel
472 * but will create annoyance for every other application
475 if (tmp
->frame
->workspace
!= workspace
&& !tmp
->flags
.selected
) {
476 /* unmap windows not on this workspace */
477 if ((tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
478 !IS_OMNIPRESENT(tmp
) && !tmp
->flags
.changing_workspace
) {
481 /* also unmap miniwindows not on this workspace */
482 if (!wPreferences
.sticky_icons
&& tmp
->flags
.miniaturized
&&
483 tmp
->icon
&& !IS_OMNIPRESENT(tmp
)) {
484 XUnmapWindow(dpy
, tmp
->icon
->core
->window
);
485 tmp
->icon
->mapped
= 0;
487 /* update current workspace of omnipresent windows */
488 if (IS_OMNIPRESENT(tmp
)) {
489 WApplication
*wapp
= wApplicationOf(tmp
->main_window
);
491 tmp
->frame
->workspace
= workspace
;
494 wapp
->last_workspace
= workspace
;
496 if (!foc2
&& (tmp
->flags
.mapped
|| tmp
->flags
.shaded
)) {
501 /* change selected windows' workspace */
502 if (tmp
->flags
.selected
) {
503 wWindowChangeWorkspace(tmp
, workspace
);
504 if (!tmp
->flags
.miniaturized
&& !foc
) {
508 if (!tmp
->flags
.hidden
) {
509 if (!(tmp
->flags
.mapped
|| tmp
->flags
.miniaturized
)) {
510 /* remap windows that are on this workspace */
512 if (!foc
&& !WFLAGP(tmp
, no_focusable
)) {
516 /* Also map miniwindow if not omnipresent */
517 if (!wPreferences
.sticky_icons
&&
518 tmp
->flags
.miniaturized
&& !IS_OMNIPRESENT(tmp
) && tmp
->icon
) {
519 tmp
->icon
->mapped
= 1;
520 XMapWindow(dpy
, tmp
->icon
->core
->window
);
528 /* Gobble up events unleashed by our mapping & unmapping.
529 * These may trigger various grab-initiated focus &
530 * crossing events. However, we don't care about them,
531 * and ignore their focus implications altogether to avoid
534 scr
->flags
.ignore_focus_events
= 1;
535 ProcessPendingEvents();
536 scr
->flags
.ignore_focus_events
= 0;
541 if (scr
->focused_window
->flags
.mapped
&& !foc
) {
542 foc
= scr
->focused_window
;
544 if (wPreferences
.focus_mode
== WKF_CLICK
) {
545 wSetFocusTo(scr
, foc
);
553 if (XQueryPointer(dpy
, scr
->root_win
, &bar
, &win
, &foo
, &foo
, &foo
, &foo
, &mask
)) {
554 tmp
= wWindowFor(win
);
557 /* If there's a window under the pointer, focus it.
558 * (we ate all other focus events above, so it's
559 * certainly not focused). Otherwise focus last
560 * focused, or the root (depending on sloppiness)
562 if (!tmp
&& wPreferences
.focus_mode
== WKF_SLOPPY
) {
563 wSetFocusTo(scr
, foc
);
565 wSetFocusTo(scr
, tmp
);
570 /* We need to always arrange icons when changing workspace, even if
571 * no autoarrange icons, because else the icons in different workspaces
573 * This can be avoided if appicons are also workspace specific.
575 if (!wPreferences
.sticky_icons
)
576 wArrangeIcons(scr
, False
);
579 wAppIconPaint(scr
->dock
->icon_array
[0]);
581 if (scr
->clip_icon
) {
582 if (scr
->workspaces
[workspace
]->clip
->auto_collapse
||
583 scr
->workspaces
[workspace
]->clip
->auto_raise_lower
) {
584 /* to handle enter notify. This will also */
585 XUnmapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
586 XMapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
588 wClipIconPaint(scr
->clip_icon
);
592 wScreenUpdateUsableArea(scr
);
593 wNETWMUpdateDesktop(scr
);
596 showWorkspaceName(scr
, workspace
);
598 WMPostNotificationName(WMNWorkspaceChanged
, scr
, (void *)(uintptr_t) workspace
);
600 /* XSync(dpy, False); */
603 #ifdef VIRTUAL_DESKTOP
607 * 1) Allow border around each window so the scrolling
608 * won't just stop at the border.
613 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
614 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
615 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
616 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
617 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
618 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
621 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
625 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
628 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
630 static Bool
cmp_gez(int i
)
635 static Bool
cmp_gz(int i
)
640 static WMPoint
getClosestEdge(WScreen
* scr
, WMPoint direction
, Bool(*cmp
) (int))
642 WMPoint closest
= wmkpoint(0, 0);
643 int closest_len
= INT_MAX
;
646 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
647 if (wwin
->frame
->workspace
== scr
->current_workspace
) {
648 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) && !wwin
->flags
.hidden
) {
649 int border
= 2 * HAS_BORDER(wwin
);
652 int head
= wGetHeadForWindow(wwin
);
653 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
656 x1
= wwin
->frame_x
- area
.x1
;
657 y1
= wwin
->frame_y
- area
.y1
;
658 x2
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ border
- area
.x2
;
659 y2
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ border
- area
.y2
;
661 p
= wmkpoint(x1
, y1
);
662 len
= vec_dot(direction
, p
);
663 updateMinimum(len
, p
, closest_len
, closest
);
665 p
= wmkpoint(x1
, y2
);
666 len
= vec_dot(direction
, p
);
667 updateMinimum(len
, p
, closest_len
, closest
);
669 p
= wmkpoint(x2
, y1
);
670 len
= vec_dot(direction
, p
);
671 updateMinimum(len
, p
, closest_len
, closest
);
673 p
= wmkpoint(x2
, y2
);
674 len
= vec_dot(direction
, p
);
675 updateMinimum(len
, p
, closest_len
, closest
);
683 static void getViewPosition(WScreen
* scr
, int workspace
, int *x
, int *y
)
685 *x
= scr
->workspaces
[workspace
]->view_x
;
686 *y
= scr
->workspaces
[workspace
]->view_y
;
689 void wWorkspaceKeyboardMoveDesktop(WScreen
* scr
, WMPoint direction
)
692 WMPoint edge
= getClosestEdge(scr
, direction
, cmp_gz
);
693 int len
= vec_dot(edge
, direction
);
694 WMPoint step
= vec_scale(direction
, len
);
695 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
696 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
+ step
.x
, y
+ step
.y
);
699 extern Cursor wCursor
[WCUR_LAST
];
701 static void vdMouseMoveDesktop(XEvent
* event
, WMPoint direction
)
703 static int lock
= False
;
710 WScreen
*scr
= wScreenForRootWindow(event
->xcrossing
.root
);
711 WMPoint old_pos
= wmkpoint(event
->xcrossing
.x_root
, event
->xcrossing
.y_root
);
716 if (XGrabPointer(dpy
, event
->xcrossing
.window
, False
,
717 PointerMotionMask
, GrabModeAsync
, GrabModeAsync
,
718 scr
->root_win
, wCursor
[WCUR_EMPTY
], CurrentTime
) != GrabSuccess
) {
720 /* if the grab fails, do it the old fashioned way */
721 step
= vec_scale2(direction
, wPreferences
.vedge_hscrollspeed
, wPreferences
.vedge_vscrollspeed
);
722 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
723 if (wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
+ step
.x
, y
+ step
.y
)) {
724 step
= vec_scale(direction
, 2);
725 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
726 event
->xcrossing
.x_root
- step
.x
, event
->xcrossing
.y_root
- step
.y
);
736 if (old_pos
.x
> scr
->scr_width
)
737 old_pos
.x
= scr
->scr_width
;
738 if (old_pos
.y
> scr
->scr_height
)
739 old_pos
.y
= scr
->scr_height
;
744 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
745 scr
->scr_width
/ 2, scr
->scr_height
/ 2);
748 WMMaskEvent(dpy
, PointerMotionMask
, &ev
);
754 step
= wmkpoint(ev
.xmotion
.x_root
- scr
->scr_width
/ 2,
755 ev
.xmotion
.y_root
- scr
->scr_height
/ 2);
756 step_len
= vec_dot(step
, direction
);
764 int resist
= wPreferences
.vedge_resistance
;
766 int closest_len
= INT_MAX
;
768 closest
= getClosestEdge(scr
, direction
, cmp_gez
);
769 closest_len
= vec_dot(direction
, closest
);
773 resisted
+= step_len
;
774 do_move
= resisted
>= resist
;
776 closest_len
= INT_MAX
;
777 step_len
= resisted
- resist
;
782 if (closest_len
<= wPreferences
.vedge_attraction
) {
783 step
= vec_scale(direction
, closest_len
);
785 step
= vec_scale(direction
, step_len
);
788 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
789 wWorkspaceSetViewport(scr
, scr
->current_workspace
,
790 x
+ step
.x
, y
+ step
.y
);
799 step
= vec_add(old_pos
, vec_scale(direction
, -1));
800 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0, step
.x
, step
.y
);
801 XUngrabPointer(dpy
, CurrentTime
);
807 static void vdHandleEnter_u(XEvent
* event
)
809 vdMouseMoveDesktop(event
, VEC_UP
);
812 static void vdHandleEnter_d(XEvent
* event
)
814 vdMouseMoveDesktop(event
, VEC_DOWN
);
817 static void vdHandleEnter_l(XEvent
* event
)
819 vdMouseMoveDesktop(event
, VEC_LEFT
);
822 static void vdHandleEnter_r(XEvent
* event
)
824 vdMouseMoveDesktop(event
, VEC_RIGHT
);
827 #define LEFT_EDGE 0x01
828 #define RIGHT_EDGE 0x02
829 #define TOP_EDGE 0x04
830 #define BOTTOM_EDGE 0x08
831 #define ALL_EDGES 0x0F
833 static void createEdges(WScreen
* scr
)
835 if (!scr
->virtual_edges
) {
838 XSetWindowAttributes attribs
;
840 int heads
= wXineramaHeads(scr
);
841 int *hasEdges
= (int *)wmalloc(sizeof(int) * heads
);
845 int max_edges
= 4 * heads
;
847 Window
*edges
= (Window
*) wmalloc(sizeof(Window
) * max_edges
);
849 for (i
= 0; i
< heads
; ++i
)
850 hasEdges
[i
] = ALL_EDGES
;
851 for (i
= 0; i
< heads
; ++i
) {
852 WMRect i_rect
= wGetRectForHead(scr
, i
);
853 for (j
= i
+ 1; j
< heads
; ++j
) {
854 WMRect j_rect
= wGetRectForHead(scr
, j
);
856 int vlen
= (WMIN(i_rect
.pos
.y
+ i_rect
.size
.height
,
857 j_rect
.pos
.y
+ j_rect
.size
.height
) -
858 WMAX(i_rect
.pos
.y
, j_rect
.pos
.y
));
860 int hlen
= (WMIN(i_rect
.pos
.x
+ i_rect
.size
.width
,
861 j_rect
.pos
.x
+ j_rect
.size
.width
) -
862 WMAX(i_rect
.pos
.x
, j_rect
.pos
.x
));
864 if (vlen
> 0 && hlen
== 0) { /* horz alignment, vert edges touch */
865 if (i_rect
.pos
.x
< j_rect
.pos
.x
) { /* i left of j */
866 hasEdges
[i
] &= ~RIGHT_EDGE
;
867 hasEdges
[j
] &= ~LEFT_EDGE
;
868 } else { /* j left of i */
869 hasEdges
[j
] &= ~RIGHT_EDGE
;
870 hasEdges
[i
] &= ~LEFT_EDGE
;
872 } else if (vlen
== 0 && hlen
> 0) { /* vert alignment, horz edges touch */
873 if (i_rect
.pos
.y
< j_rect
.pos
.y
) { /* i top of j */
874 hasEdges
[i
] &= ~BOTTOM_EDGE
;
875 hasEdges
[j
] &= ~TOP_EDGE
;
876 } else { /* j top of i */
877 hasEdges
[j
] &= ~BOTTOM_EDGE
;
878 hasEdges
[i
] &= ~TOP_EDGE
;
884 for (w
= 0; w
< scr
->workspace_count
; w
++) {
885 /* puts("reset workspace"); */
886 wWorkspaceSetViewport(scr
, w
, 0, 0);
889 vmask
= CWEventMask
| CWOverrideRedirect
;
890 attribs
.event_mask
= (EnterWindowMask
| LeaveWindowMask
| VisibilityChangeMask
);
891 attribs
.override_redirect
= True
;
893 for (head
= 0; head
< wXineramaHeads(scr
); ++head
) {
894 WMRect rect
= wGetRectForHead(scr
, head
);
896 if (hasEdges
[head
] & TOP_EDGE
) {
898 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
899 rect
.size
.width
, thickness
, 0,
900 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
901 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_u
);
905 if (hasEdges
[head
] & BOTTOM_EDGE
) {
907 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
,
908 rect
.pos
.y
+ rect
.size
.height
- thickness
,
909 rect
.size
.width
, thickness
, 0,
910 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
911 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_d
);
915 if (hasEdges
[head
] & LEFT_EDGE
) {
917 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
918 thickness
, rect
.pos
.y
+ rect
.size
.height
, 0,
919 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
920 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_l
);
924 if (hasEdges
[head
] & RIGHT_EDGE
) {
926 XCreateWindow(dpy
, scr
->root_win
,
927 rect
.pos
.x
+ rect
.size
.width
- thickness
, rect
.pos
.y
,
928 thickness
, rect
.size
.height
, 0,
929 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
, &attribs
);
930 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
, (XPointer
) vdHandleEnter_r
);
935 scr
->virtual_nr_edges
= nr_edges
;
936 scr
->virtual_edges
= edges
;
938 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
939 XMapWindow(dpy
, scr
->virtual_edges
[i
]);
941 wWorkspaceRaiseEdge(scr
);
947 static void destroyEdges(WScreen
* scr
)
949 if (scr
->virtual_edges
) {
952 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
953 XDeleteContext(dpy
, scr
->virtual_edges
[i
], wVEdgeContext
);
954 XUnmapWindow(dpy
, scr
->virtual_edges
[i
]);
955 XDestroyWindow(dpy
, scr
->virtual_edges
[i
]);
958 wfree(scr
->virtual_edges
);
959 scr
->virtual_edges
= NULL
;
960 scr
->virtual_nr_edges
= 0;
964 void wWorkspaceUpdateEdge(WScreen
* scr
)
966 if (wPreferences
.vdesk_enable
) {
973 void wWorkspaceRaiseEdge(WScreen
* scr
)
975 static int toggle
= 0;
978 if (!scr
->virtual_edges
)
982 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
983 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
986 for (i
= scr
->virtual_nr_edges
- 1; i
>= 0; --i
) {
987 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
994 void wWorkspaceLowerEdge(WScreen
* scr
)
997 for (i
= 0; i
< scr
->virtual_nr_edges
; ++i
) {
998 XLowerWindow(dpy
, scr
->virtual_edges
[i
]);
1002 void wWorkspaceResizeViewport(WScreen
* scr
, int workspace
)
1005 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
1006 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
, y
);
1009 void updateWorkspaceGeometry(WScreen
* scr
, int workspace
, int *view_x
, int *view_y
)
1011 int most_left
, most_right
, most_top
, most_bottom
;
1014 int heads
= wXineramaHeads(scr
);
1015 typedef int strut_t
[4];
1016 strut_t
*strut
= (strut_t
*) wmalloc(heads
* sizeof(strut_t
));
1019 for (head
= 0; head
< heads
; ++head
) {
1020 WMRect rect
= wGetRectForHead(scr
, head
);
1021 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
1022 strut
[head
][0] = area
.x1
- rect
.pos
.x
;
1023 strut
[head
][1] = rect
.pos
.x
+ rect
.size
.width
- area
.x2
;
1024 strut
[head
][2] = area
.y1
- rect
.pos
.y
;
1025 strut
[head
][3] = rect
.pos
.y
+ rect
.size
.height
- area
.y2
;
1028 /* adjust workspace layout */
1029 wwin
= scr
->focused_window
;
1032 most_left
= scr
->scr_width
;
1033 most_top
= scr
->scr_height
;
1034 for (; wwin
; wwin
= wwin
->prev
) {
1035 if (wwin
->frame
->workspace
== workspace
) {
1036 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) && !wwin
->flags
.hidden
) {
1038 head
= wGetHeadForWindow(wwin
);
1040 i
= wwin
->frame_x
- strut
[head
][0];
1041 if (i
< most_left
) /* record positions, should this be cached? */
1043 i
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ strut
[head
][1];
1044 if (HAS_BORDER(wwin
))
1048 i
= wwin
->frame_y
- strut
[head
][2];
1051 i
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ strut
[head
][3];
1052 if (HAS_BORDER(wwin
))
1054 if (i
> most_bottom
) {
1066 scr
->workspaces
[workspace
]->width
= WMAX(most_right
, scr
->scr_width
) - WMIN(most_left
, 0);
1067 scr
->workspaces
[workspace
]->height
= WMAX(most_bottom
, scr
->scr_height
) - WMIN(most_top
, 0);
1069 *view_x
+= -most_left
- scr
->workspaces
[workspace
]->view_x
;
1070 scr
->workspaces
[workspace
]->view_x
= -most_left
;
1072 *view_y
+= -most_top
- scr
->workspaces
[workspace
]->view_y
;
1073 scr
->workspaces
[workspace
]->view_y
= -most_top
;
1078 typedef struct _delay_configure
{
1083 void sendConfigureNotify(_delay_configure
* delay
)
1087 delay
->delay_count
--;
1088 if (!delay
->delay_count
) {
1089 for (wwin
= delay
->wwin
; wwin
; wwin
= wwin
->prev
) {
1090 wWindowSynthConfigureNotify(wwin
);
1095 Bool
wWorkspaceSetViewport(WScreen
* scr
, int workspace
, int view_x
, int view_y
)
1097 Bool adjust_flag
= False
;
1099 static _delay_configure delay_configure
= { NULL
, 0 };
1103 wptr
= scr
->workspaces
[workspace
];
1105 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y); */
1107 updateWorkspaceGeometry(scr
, workspace
, &view_x
, &view_y
);
1109 if (view_x
+ scr
->scr_width
> wptr
->width
) {
1110 /* puts("right edge of vdesk"); */
1111 view_x
= wptr
->width
- scr
->scr_width
;
1114 /* puts("left edge of vdesk"); */
1117 if (view_y
+ scr
->scr_height
> wptr
->height
) {
1118 /* puts("right edge of vdesk"); */
1119 view_y
= wptr
->height
- scr
->scr_height
;
1122 /* puts("left edge of vdesk"); */
1126 diff_x
= wptr
->view_x
- view_x
;
1127 diff_y
= wptr
->view_y
- view_y
;
1128 if (!diff_x
&& !diff_y
)
1131 wptr
->view_x
= view_x
;
1132 wptr
->view_y
= view_y
;
1135 wNETWMUpdateDesktop(scr
);
1138 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
1139 if (wwin
->frame
->workspace
== workspace
&& !IS_VSTUCK(wwin
)) {
1140 wWindowMove(wwin
, wwin
->frame_x
+ diff_x
, wwin
->frame_y
+ diff_y
);
1144 if (1) { /* if delay */
1145 delay_configure
.delay_count
++;
1146 delay_configure
.wwin
= scr
->focused_window
;
1147 WMAddTimerHandler(200, (WMCallback
*) sendConfigureNotify
, &delay_configure
);
1155 static void switchWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
1157 wWorkspaceChange(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
1160 static void deleteWSCommand(WMenu
* menu
, WMenuEntry
* entry
)
1162 wWorkspaceDelete(menu
->frame
->screen_ptr
, menu
->frame
->screen_ptr
->workspace_count
- 1);
1165 static void newWSCommand(WMenu
* menu
, WMenuEntry
* foo
)
1169 ws
= wWorkspaceNew(menu
->frame
->screen_ptr
);
1170 /* autochange workspace */
1172 wWorkspaceChange(menu
->frame
->screen_ptr
, ws
);
1177 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1178 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1180 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1185 static char *cropline(char *line
)
1189 if (strlen(line
) == 0)
1193 end
= &(line
[strlen(line
)]) - 1;
1194 while (isspace(*line
) && *line
!= 0)
1196 while (isspace(*end
) && end
!= line
) {
1203 void wWorkspaceRename(WScreen
* scr
, int workspace
, char *name
)
1205 char buf
[MAX_WORKSPACENAME_WIDTH
+ 1];
1208 if (workspace
>= scr
->workspace_count
)
1211 /* trim white spaces */
1212 tmp
= cropline(name
);
1214 if (strlen(tmp
) == 0) {
1215 snprintf(buf
, sizeof(buf
), _("Workspace %i"), workspace
+ 1);
1217 strncpy(buf
, tmp
, MAX_WORKSPACENAME_WIDTH
);
1219 buf
[MAX_WORKSPACENAME_WIDTH
] = 0;
1221 /* update workspace */
1222 wfree(scr
->workspaces
[workspace
]->name
);
1223 scr
->workspaces
[workspace
]->name
= wstrdup(buf
);
1225 if (scr
->clip_ws_menu
) {
1226 if (strcmp(scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
, buf
) != 0) {
1227 wfree(scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
);
1228 scr
->clip_ws_menu
->entries
[workspace
+ 2]->text
= wstrdup(buf
);
1229 wMenuRealize(scr
->clip_ws_menu
);
1232 if (scr
->workspace_menu
) {
1233 if (strcmp(scr
->workspace_menu
->entries
[workspace
+ 2]->text
, buf
) != 0) {
1234 wfree(scr
->workspace_menu
->entries
[workspace
+ 2]->text
);
1235 scr
->workspace_menu
->entries
[workspace
+ 2]->text
= wstrdup(buf
);
1236 wMenuRealize(scr
->workspace_menu
);
1241 wClipIconPaint(scr
->clip_icon
);
1243 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) workspace
);
1246 /* callback for when menu entry is edited */
1247 static void onMenuEntryEdited(WMenu
* menu
, WMenuEntry
* entry
)
1252 wWorkspaceRename(menu
->frame
->screen_ptr
, (long)entry
->clientdata
, tmp
);
1255 WMenu
*wWorkspaceMenuMake(WScreen
* scr
, Bool titled
)
1259 wsmenu
= wMenuCreate(scr
, titled
? _("Workspaces") : NULL
, False
);
1261 wwarning(_("could not create Workspace menu"));
1265 /* callback to be called when an entry is edited */
1266 wsmenu
->on_edit
= onMenuEntryEdited
;
1268 wMenuAddCallback(wsmenu
, _("New"), newWSCommand
, NULL
);
1269 wMenuAddCallback(wsmenu
, _("Destroy Last"), deleteWSCommand
, NULL
);
1274 void wWorkspaceMenuUpdate(WScreen
* scr
, WMenu
* menu
)
1278 char title
[MAX_WORKSPACENAME_WIDTH
+ 1];
1285 if (menu
->entry_no
< scr
->workspace_count
+ 2) {
1286 /* new workspace(s) added */
1287 i
= scr
->workspace_count
- (menu
->entry_no
- 2);
1288 ws
= menu
->entry_no
- 2;
1290 strncpy(title
, scr
->workspaces
[ws
]->name
, MAX_WORKSPACENAME_WIDTH
);
1292 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, (void *)ws
);
1293 entry
->flags
.indicator
= 1;
1294 entry
->flags
.editable
= 1;
1299 } else if (menu
->entry_no
> scr
->workspace_count
+ 2) {
1300 /* removed workspace(s) */
1301 for (i
= menu
->entry_no
- 1; i
>= scr
->workspace_count
+ 2; i
--) {
1302 wMenuRemoveItem(menu
, i
);
1307 for (i
= 0; i
< scr
->workspace_count
; i
++) {
1308 menu
->entries
[i
+ 2]->flags
.indicator_on
= 0;
1310 menu
->entries
[scr
->current_workspace
+ 2]->flags
.indicator_on
= 1;
1312 /* don't let user destroy current workspace */
1313 if (scr
->current_workspace
== scr
->workspace_count
- 1) {
1314 wMenuSetEnabled(menu
, 1, False
);
1316 wMenuSetEnabled(menu
, 1, True
);
1319 tmp
= menu
->frame
->top_width
+ 5;
1320 /* if menu got unreachable, bring it to a visible place */
1321 if (menu
->frame_x
< tmp
- (int)menu
->frame
->core
->width
)
1322 wMenuMove(menu
, tmp
- (int)menu
->frame
->core
->width
, menu
->frame_y
, False
);
1327 void wWorkspaceSaveState(WScreen
* scr
, WMPropList
* old_state
)
1329 WMPropList
*parr
, *pstr
, *wks_state
, *old_wks_state
, *foo
, *bar
;
1334 old_wks_state
= WMGetFromPLDictionary(old_state
, dWorkspaces
);
1335 parr
= WMCreatePLArray(NULL
);
1336 for (i
= 0; i
< scr
->workspace_count
; i
++) {
1337 pstr
= WMCreatePLString(scr
->workspaces
[i
]->name
);
1338 wks_state
= WMCreatePLDictionary(dName
, pstr
, NULL
);
1339 WMReleasePropList(pstr
);
1340 if (!wPreferences
.flags
.noclip
) {
1341 pstr
= wClipSaveWorkspaceState(scr
, i
);
1342 WMPutInPLDictionary(wks_state
, dClip
, pstr
);
1343 WMReleasePropList(pstr
);
1344 } else if (old_wks_state
!= NULL
) {
1345 if ((foo
= WMGetFromPLArray(old_wks_state
, i
)) != NULL
) {
1346 if ((bar
= WMGetFromPLDictionary(foo
, dClip
)) != NULL
) {
1347 WMPutInPLDictionary(wks_state
, dClip
, bar
);
1351 WMAddToPLArray(parr
, wks_state
);
1352 WMReleasePropList(wks_state
);
1354 WMPutInPLDictionary(scr
->session_state
, dWorkspaces
, parr
);
1355 WMReleasePropList(parr
);
1358 void wWorkspaceRestoreState(WScreen
* scr
)
1360 WMPropList
*parr
, *pstr
, *wks_state
, *clip_state
;
1365 if (scr
->session_state
== NULL
)
1368 parr
= WMGetFromPLDictionary(scr
->session_state
, dWorkspaces
);
1373 wscount
= scr
->workspace_count
;
1374 for (i
= 0; i
< WMIN(WMGetPropListItemCount(parr
), MAX_WORKSPACES
); i
++) {
1375 wks_state
= WMGetFromPLArray(parr
, i
);
1376 if (WMIsPLDictionary(wks_state
))
1377 pstr
= WMGetFromPLDictionary(wks_state
, dName
);
1380 if (i
>= scr
->workspace_count
)
1382 if (scr
->workspace_menu
) {
1383 wfree(scr
->workspace_menu
->entries
[i
+ 2]->text
);
1384 scr
->workspace_menu
->entries
[i
+ 2]->text
= wstrdup(WMGetFromPLString(pstr
));
1385 scr
->workspace_menu
->flags
.realized
= 0;
1387 wfree(scr
->workspaces
[i
]->name
);
1388 scr
->workspaces
[i
]->name
= wstrdup(WMGetFromPLString(pstr
));
1389 if (!wPreferences
.flags
.noclip
) {
1390 int added_omnipresent_icons
= 0;
1391 clip_state
= WMGetFromPLDictionary(wks_state
, dClip
);
1392 if (scr
->workspaces
[i
]->clip
)
1393 wDockDestroy(scr
->workspaces
[i
]->clip
);
1394 scr
->workspaces
[i
]->clip
= wDockRestoreState(scr
, clip_state
, WM_CLIP
);
1396 wDockHideIcons(scr
->workspaces
[i
]->clip
);
1398 /* We set the global icons here, because scr->workspaces[i]->clip
1399 * was not valid in wDockRestoreState().
1400 * There we only set icon->omnipresent to know which icons we
1403 for (j
= 0; j
< scr
->workspaces
[i
]->clip
->max_icons
; j
++) {
1404 WAppIcon
*aicon
= scr
->workspaces
[i
]->clip
->icon_array
[j
];
1407 if (!aicon
|| !aicon
->omnipresent
)
1409 aicon
->omnipresent
= 0;
1410 if (wClipMakeIconOmnipresent(aicon
, True
) != WO_SUCCESS
)
1415 /* Move this appicon from workspace i to workspace 0 */
1416 scr
->workspaces
[i
]->clip
->icon_array
[j
] = NULL
;
1417 scr
->workspaces
[i
]->clip
->icon_count
--;
1419 added_omnipresent_icons
++;
1420 /* If there are too many omnipresent appicons, we are in trouble */
1421 assert(scr
->workspaces
[0]->clip
->icon_count
+ added_omnipresent_icons
1422 <= scr
->workspaces
[0]->clip
->max_icons
);
1423 /* Find first free spot on workspace 0 */
1424 for (k
= 0; k
< scr
->workspaces
[0]->clip
->max_icons
; k
++)
1425 if (scr
->workspaces
[0]->clip
->icon_array
[k
] == NULL
)
1427 scr
->workspaces
[0]->clip
->icon_array
[k
] = aicon
;
1428 aicon
->dock
= scr
->workspaces
[0]->clip
;
1430 scr
->workspaces
[0]->clip
->icon_count
+= added_omnipresent_icons
;
1433 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void *)(uintptr_t) i
);