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>
38 #include "WindowMaker.h"
45 #include "application.h"
48 #include "workspace.h"
57 extern WPreferences wPreferences
;
58 extern XContext wWinContext
;
59 extern XContext wVEdgeContext
;
61 extern void ProcessPendingEvents();
63 static WMPropList
*dWorkspaces
=NULL
;
64 static WMPropList
*dClip
, *dName
;
71 if (dWorkspaces
!=NULL
)
74 dWorkspaces
= WMCreatePLString("Workspaces");
75 dName
= WMCreatePLString("Name");
76 dClip
= WMCreatePLString("Clip");
81 wWorkspaceMake(WScreen
*scr
, int count
)
91 wWorkspaceNew(WScreen
*scr
)
93 WWorkspace
*wspace
, **list
;
96 if (scr
->workspace_count
< MAX_WORKSPACES
) {
97 scr
->workspace_count
++;
99 wspace
= wmalloc(sizeof(WWorkspace
));
103 wspace
->name
= wmalloc(strlen(_("Workspace %i"))+8);
104 sprintf(wspace
->name
, _("Workspace %i"), scr
->workspace_count
);
108 if (!wPreferences
.flags
.noclip
) {
109 wspace
->clip
= wDockCreate(scr
, WM_CLIP
);
113 list
= wmalloc(sizeof(WWorkspace
*)*scr
->workspace_count
);
115 for (i
=0; i
<scr
->workspace_count
-1; i
++) {
116 list
[i
] = scr
->workspaces
[i
];
120 wfree(scr
->workspaces
);
121 scr
->workspaces
= list
;
123 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
124 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
125 #ifdef VIRTUAL_DESKTOP
126 wspace
->view_x
= wspace
->view_y
= 0;
127 wspace
->height
= scr
->scr_height
;
128 wspace
->width
= scr
->scr_width
;
131 wNETWMUpdateDesktop(scr
);
134 WMPostNotificationName(WMNWorkspaceCreated
, scr
,
135 (void*)(scr
->workspace_count
-1));
138 return scr
->workspace_count
-1;
146 wWorkspaceDelete(WScreen
*scr
, int workspace
)
155 /* verify if workspace is in use by some window */
156 tmp
= scr
->focused_window
;
158 if (!IS_OMNIPRESENT(tmp
) && tmp
->frame
->workspace
==workspace
)
163 if (!wPreferences
.flags
.noclip
) {
164 wDockDestroy(scr
->workspaces
[workspace
]->clip
);
165 scr
->workspaces
[workspace
]->clip
= NULL
;
168 list
= wmalloc(sizeof(WWorkspace
*)*(scr
->workspace_count
-1));
170 for (i
=0; i
<scr
->workspace_count
; i
++) {
172 list
[j
++] = scr
->workspaces
[i
];
174 if (scr
->workspaces
[i
]->name
)
175 wfree(scr
->workspaces
[i
]->name
);
176 wfree(scr
->workspaces
[i
]);
179 wfree(scr
->workspaces
);
180 scr
->workspaces
= list
;
182 scr
->workspace_count
--;
186 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
187 /* clip workspace menu */
188 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
190 /* update also window menu */
191 if (scr
->workspace_submenu
) {
192 WMenu
*menu
= scr
->workspace_submenu
;
195 while (i
>scr
->workspace_count
)
196 wMenuRemoveItem(menu
, --i
);
200 if (scr
->clip_submenu
) {
201 WMenu
*menu
= scr
->clip_submenu
;
204 while (i
>scr
->workspace_count
)
205 wMenuRemoveItem(menu
, --i
);
210 wNETWMUpdateDesktop(scr
);
213 WMPostNotificationName(WMNWorkspaceDestroyed
, scr
,
214 (void*)(scr
->workspace_count
-1));
216 if (scr
->current_workspace
>= scr
->workspace_count
)
217 wWorkspaceChange(scr
, scr
->workspace_count
-1);
223 typedef struct WorkspaceNameData
{
232 hideWorkspaceName(void *data
)
234 WScreen
*scr
= (WScreen
*)data
;
236 if (!scr
->workspace_name_data
|| scr
->workspace_name_data
->count
== 0
237 || time(NULL
) > scr
->workspace_name_data
->timeout
) {
238 XUnmapWindow(dpy
, scr
->workspace_name
);
240 if (scr
->workspace_name_data
) {
241 RReleaseImage(scr
->workspace_name_data
->back
);
242 RReleaseImage(scr
->workspace_name_data
->text
);
243 wfree(scr
->workspace_name_data
);
245 scr
->workspace_name_data
= NULL
;
247 scr
->workspace_name_timer
= NULL
;
249 RImage
*img
= RCloneImage(scr
->workspace_name_data
->back
);
252 scr
->workspace_name_timer
=
253 WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
255 RCombineImagesWithOpaqueness(img
, scr
->workspace_name_data
->text
,
256 scr
->workspace_name_data
->count
*255/10);
258 RConvertImage(scr
->rcontext
, img
, &pix
);
262 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, pix
);
263 XClearWindow(dpy
, scr
->workspace_name
);
264 XFreePixmap(dpy
, pix
);
267 scr
->workspace_name_data
->count
--;
273 showWorkspaceName(WScreen
*scr
, int workspace
)
275 WorkspaceNameData
*data
;
280 char *name
= scr
->workspaces
[workspace
]->name
;
281 int len
= strlen(name
);
284 if (wPreferences
.workspace_name_display_position
== WD_NONE
||
285 scr
->workspace_count
< 2) {
289 if (scr
->workspace_name_timer
) {
290 WMDeleteTimerHandler(scr
->workspace_name_timer
);
291 XUnmapWindow(dpy
, scr
->workspace_name
);
294 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
,
295 hideWorkspaceName
, scr
);
297 if (scr
->workspace_name_data
) {
298 RReleaseImage(scr
->workspace_name_data
->back
);
299 RReleaseImage(scr
->workspace_name_data
->text
);
300 wfree(scr
->workspace_name_data
);
303 data
= wmalloc(sizeof(WorkspaceNameData
));
306 w
= WMWidthOfString(scr
->workspace_name_font
, name
, len
);
307 h
= WMFontHeight(scr
->workspace_name_font
);
309 switch (wPreferences
.workspace_name_display_position
) {
311 px
= (scr
->scr_width
- (w
+4))/2;
315 px
= (scr
->scr_width
- (w
+4))/2;
316 py
= scr
->scr_height
- (h
+4);
323 px
= scr
->scr_width
- (w
+4);
328 py
= scr
->scr_height
- (h
+4);
331 px
= scr
->scr_width
- (w
+4);
332 py
= scr
->scr_height
- (h
+4);
336 px
= (scr
->scr_width
- (w
+4))/2;
337 py
= (scr
->scr_height
- (h
+4))/2;
340 XResizeWindow(dpy
, scr
->workspace_name
, w
+4, h
+4);
341 XMoveWindow(dpy
, scr
->workspace_name
, px
, py
);
343 text
= XCreatePixmap(dpy
, scr
->w_win
, w
+4, h
+4, scr
->w_depth
);
344 mask
= XCreatePixmap(dpy
, scr
->w_win
, w
+4, h
+4, 1);
346 /*XSetForeground(dpy, scr->mono_gc, 0);
347 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4);*/
349 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+4, h
+4);
351 for (x
= 0; x
<= 4; x
++) {
352 for (y
= 0; y
<= 4; y
++) {
353 WMDrawString(scr
->wmscreen
, text
, scr
->white
,
354 scr
->workspace_name_font
, x
, y
, name
, len
);
358 XSetForeground(dpy
, scr
->mono_gc
, 1);
359 XSetBackground(dpy
, scr
->mono_gc
, 0);
361 XCopyPlane(dpy
, text
, mask
, scr
->mono_gc
, 0, 0, w
+4, h
+4, 0, 0, 1<<(scr
->w_depth
-1));
363 /*XSetForeground(dpy, scr->mono_gc, 1);*/
364 XSetBackground(dpy
, scr
->mono_gc
, 1);
366 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+4, h
+4);
368 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
,
372 XShapeCombineMask(dpy
, scr
->workspace_name
, ShapeBounding
, 0, 0, mask
,
375 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, text
);
376 XClearWindow(dpy
, scr
->workspace_name
);
378 data
->text
= RCreateImageFromDrawable(scr
->rcontext
, text
, None
);
380 XFreePixmap(dpy
, text
);
381 XFreePixmap(dpy
, mask
);
384 XMapRaised(dpy
, scr
->workspace_name
);
390 ximg
= RGetXImage(scr
->rcontext
, scr
->root_win
, px
, py
,
391 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 +
411 (WORKSPACE_NAME_DELAY
+ WORKSPACE_NAME_FADE_DELAY
*data
->count
)/1000;
413 scr
->workspace_name_data
= data
;
419 if (scr
->workspace_name_timer
)
420 WMDeleteTimerHandler(scr
->workspace_name_timer
);
423 RReleaseImage(data
->text
);
425 RReleaseImage(data
->back
);
428 scr
->workspace_name_data
= NULL
;
430 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
+
431 10*WORKSPACE_NAME_FADE_DELAY
,
432 hideWorkspaceName
, scr
);
437 wWorkspaceChange(WScreen
*scr
, int workspace
)
439 if (scr
->flags
.startup
|| scr
->flags
.startup2
) {
443 if (workspace
!= scr
->current_workspace
) {
444 wWorkspaceForceChange(scr
, workspace
);
446 showWorkspaceName(scr, workspace);
452 wWorkspaceRelativeChange(WScreen
*scr
, int amount
)
456 w
= scr
->current_workspace
+ amount
;
460 wWorkspaceChange(scr
, w
);
461 } else if (wPreferences
.ws_cycle
) {
462 wWorkspaceChange(scr
, scr
->workspace_count
+ w
);
464 } else if (amount
> 0) {
465 if (w
< scr
->workspace_count
) {
466 wWorkspaceChange(scr
, w
);
467 } else if (wPreferences
.ws_advance
) {
468 wWorkspaceChange(scr
, WMIN(w
, MAX_WORKSPACES
-1));
469 } else if (wPreferences
.ws_cycle
) {
470 wWorkspaceChange(scr
, w
% scr
->workspace_count
);
477 wWorkspaceForceChange(WScreen
*scr
, int workspace
)
479 WWindow
*tmp
, *foc
=NULL
, *foc2
=NULL
;
481 if (workspace
>= MAX_WORKSPACES
|| workspace
< 0)
484 SendHelperMessage(scr
, 'C', workspace
+1, NULL
);
486 if (workspace
> scr
->workspace_count
-1) {
487 wWorkspaceMake(scr
, workspace
- scr
->workspace_count
+ 1);
490 wClipUpdateForWorkspaceChange(scr
, workspace
);
492 scr
->current_workspace
= workspace
;
494 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
496 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
498 if ((tmp
= scr
->focused_window
)!= NULL
) {
499 if ((IS_OMNIPRESENT(tmp
) && (tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
500 !WFLAGP(tmp
, no_focusable
)) || tmp
->flags
.changing_workspace
) {
504 /* foc2 = tmp; will fix annoyance with gnome panel
505 * but will create annoyance for every other application
508 if (tmp
->frame
->workspace
!=workspace
&& !tmp
->flags
.selected
) {
509 /* unmap windows not on this workspace */
510 if ((tmp
->flags
.mapped
||tmp
->flags
.shaded
) &&
511 !IS_OMNIPRESENT(tmp
) && !tmp
->flags
.changing_workspace
) {
514 /* also unmap miniwindows not on this workspace */
515 if (!wPreferences
.sticky_icons
&& tmp
->flags
.miniaturized
&&
516 tmp
->icon
&& !IS_OMNIPRESENT(tmp
)) {
517 XUnmapWindow(dpy
, tmp
->icon
->core
->window
);
518 tmp
->icon
->mapped
= 0;
520 /* update current workspace of omnipresent windows */
521 if (IS_OMNIPRESENT(tmp
)) {
522 WApplication
*wapp
= wApplicationOf(tmp
->main_window
);
524 tmp
->frame
->workspace
= workspace
;
527 wapp
->last_workspace
= workspace
;
529 if (!foc2
&& (tmp
->flags
.mapped
|| tmp
->flags
.shaded
)) {
534 /* change selected windows' workspace */
535 if (tmp
->flags
.selected
) {
536 wWindowChangeWorkspace(tmp
, workspace
);
537 if (!tmp
->flags
.miniaturized
&& !foc
) {
541 if (!tmp
->flags
.hidden
) {
542 if (!(tmp
->flags
.mapped
|| tmp
->flags
.miniaturized
)) {
543 /* remap windows that are on this workspace */
545 if (!foc
&& !WFLAGP(tmp
, no_focusable
)) {
549 /* Also map miniwindow if not omnipresent */
550 if (!wPreferences
.sticky_icons
&&
551 tmp
->flags
.miniaturized
&&
552 !IS_OMNIPRESENT(tmp
) && tmp
->icon
) {
553 tmp
->icon
->mapped
= 1;
554 XMapWindow(dpy
, tmp
->icon
->core
->window
);
562 /* Gobble up events unleashed by our mapping & unmapping.
563 * These may trigger various grab-initiated focus &
564 * crossing events. However, we don't care about them,
565 * and ignore their focus implications altogether to avoid
568 scr
->flags
.ignore_focus_events
= 1;
569 ProcessPendingEvents();
570 scr
->flags
.ignore_focus_events
= 0;
575 if (scr
->focused_window
->flags
.mapped
&& !foc
) {
576 foc
= scr
->focused_window
;
578 if (wPreferences
.focus_mode
== WKF_CLICK
) {
579 wSetFocusTo(scr
, foc
);
587 if (XQueryPointer(dpy
, scr
->root_win
, &bar
, &win
,
588 &foo
, &foo
, &foo
, &foo
, &mask
)) {
589 tmp
= wWindowFor(win
);
592 /* If there's a window under the pointer, focus it.
593 * (we ate all other focus events above, so it's
594 * certainly not focused). Otherwise focus last
595 * focused, or the root (depending on sloppiness)
597 if (!tmp
&& wPreferences
.focus_mode
== WKF_SLOPPY
) {
598 wSetFocusTo(scr
, foc
);
600 wSetFocusTo(scr
, tmp
);
605 /* We need to always arrange icons when changing workspace, even if
606 * no autoarrange icons, because else the icons in different workspaces
608 * This can be avoided if appicons are also workspace specific.
610 if (!wPreferences
.sticky_icons
)
611 wArrangeIcons(scr
, False
);
614 wAppIconPaint(scr
->dock
->icon_array
[0]);
616 if (scr
->clip_icon
) {
617 if (scr
->workspaces
[workspace
]->clip
->auto_collapse
||
618 scr
->workspaces
[workspace
]->clip
->auto_raise_lower
) {
619 /* to handle enter notify. This will also */
620 XUnmapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
621 XMapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
623 wClipIconPaint(scr
->clip_icon
);
628 wScreenUpdateUsableArea(scr
);
629 wNETWMUpdateDesktop(scr
);
632 showWorkspaceName(scr
, workspace
);
634 WMPostNotificationName(WMNWorkspaceChanged
, scr
, (void*)workspace
);
636 /* XSync(dpy, False); */
640 #ifdef VIRTUAL_DESKTOP
644 * 1) Allow border around each window so the scrolling
645 * won't just stop at the border.
650 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
651 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
652 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
653 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
654 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
655 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
658 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
662 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
665 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
667 static Bool
cmp_gez(int i
) { return (i
>= 0); }
669 static Bool
cmp_gz(int i
) { return (i
> 0); }
673 getClosestEdge(WScreen
* scr
, WMPoint direction
, Bool (*cmp
)(int))
675 WMPoint closest
= wmkpoint(0, 0);
676 int closest_len
= INT_MAX
;
679 for (wwin
=scr
->focused_window
; wwin
; wwin
=wwin
->prev
) {
680 if (wwin
->frame
->workspace
== scr
->current_workspace
) {
681 if (!wwin
->flags
.miniaturized
&&
683 !wwin
->flags
.hidden
) {
684 int border
= 2*HAS_BORDER(wwin
);
687 int head
= wGetHeadForWindow(wwin
);
688 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
691 x1
= wwin
->frame_x
- area
.x1
;
692 y1
= wwin
->frame_y
- area
.y1
;
693 x2
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ border
- area
.x2
;
694 y2
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ border
- area
.y2
;
697 len
= vec_dot(direction
, p
);
698 updateMinimum(len
, p
, closest_len
, closest
);
701 len
= vec_dot(direction
, p
);
702 updateMinimum(len
, p
, closest_len
, closest
);
705 len
= vec_dot(direction
, p
);
706 updateMinimum(len
, p
, closest_len
, closest
);
709 len
= vec_dot(direction
, p
);
710 updateMinimum(len
, p
, closest_len
, closest
);
720 getViewPosition(WScreen
*scr
, int workspace
, int *x
, int *y
)
722 *x
= scr
->workspaces
[workspace
]->view_x
;
723 *y
= scr
->workspaces
[workspace
]->view_y
;
728 wWorkspaceKeyboardMoveDesktop(WScreen
*scr
, WMPoint direction
)
731 WMPoint edge
= getClosestEdge(scr
, direction
, cmp_gz
);
732 int len
= vec_dot(edge
, direction
);
733 WMPoint step
= vec_scale(direction
, len
);
734 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
735 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
+step
.x
, y
+step
.y
);
739 extern Cursor wCursor
[WCUR_LAST
];
743 vdMouseMoveDesktop(XEvent
*event
, WMPoint direction
)
745 static int lock
= False
;
751 WScreen
*scr
= wScreenForRootWindow(event
->xcrossing
.root
);
752 WMPoint old_pos
= wmkpoint(event
->xcrossing
.x_root
, event
->xcrossing
.y_root
);
757 if (XGrabPointer(dpy
, event
->xcrossing
.window
, False
,
758 PointerMotionMask
, GrabModeAsync
, GrabModeAsync
,
759 scr
->root_win
, wCursor
[WCUR_EMPTY
],
760 CurrentTime
) != GrabSuccess
) {
762 /* if the grab fails, do it the old fashioned way */
763 step
= vec_scale2(direction
, wPreferences
.vedge_hscrollspeed
,
764 wPreferences
.vedge_vscrollspeed
);
765 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
766 if (wWorkspaceSetViewport(scr
, scr
->current_workspace
,
767 x
+step
.x
, y
+step
.y
)) {
768 step
= vec_scale(direction
, 2);
769 XWarpPointer(dpy
, None
, scr
->root_win
, 0,0,0,0,
770 event
->xcrossing
.x_root
- step
.x
,
771 event
->xcrossing
.y_root
- step
.y
);
781 if (old_pos
.x
> scr
->scr_width
)
782 old_pos
.x
= scr
->scr_width
;
783 if (old_pos
.y
> scr
->scr_height
)
784 old_pos
.y
= scr
->scr_height
;
789 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
790 scr
->scr_width
/2, scr
->scr_height
/2);
793 WMMaskEvent(dpy
, PointerMotionMask
, &ev
);
799 step
= wmkpoint(ev
.xmotion
.x_root
-scr
->scr_width
/2,
800 ev
.xmotion
.y_root
-scr
->scr_height
/2);
801 step_len
= vec_dot(step
, direction
);
809 int resist
= wPreferences
.vedge_resistance
;
811 int closest_len
= INT_MAX
;
813 closest
= getClosestEdge(scr
, direction
, cmp_gez
);
814 closest_len
= vec_dot(direction
, closest
);
818 resisted
+= step_len
;
819 do_move
= resisted
>= resist
;
821 closest_len
= INT_MAX
;
822 step_len
= resisted
- resist
;
827 if (closest_len
<= wPreferences
.vedge_attraction
) {
828 step
= vec_scale(direction
, closest_len
);
830 step
= vec_scale(direction
, step_len
);
833 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
834 wWorkspaceSetViewport(scr
, scr
->current_workspace
,
844 step
= vec_add(old_pos
, vec_scale(direction
, -1));
845 XWarpPointer(dpy
, None
, scr
->root_win
, 0,0,0,0, step
.x
, step
.y
);
846 XUngrabPointer(dpy
, CurrentTime
);
854 vdHandleEnter_u(XEvent
*event
) {
855 vdMouseMoveDesktop(event
, VEC_UP
);
860 vdHandleEnter_d(XEvent
*event
) {
861 vdMouseMoveDesktop(event
, VEC_DOWN
);
866 vdHandleEnter_l(XEvent
*event
) {
867 vdMouseMoveDesktop(event
, VEC_LEFT
);
872 vdHandleEnter_r(XEvent
*event
) {
873 vdMouseMoveDesktop(event
, VEC_RIGHT
);
877 #define LEFT_EDGE 0x01
878 #define RIGHT_EDGE 0x02
879 #define TOP_EDGE 0x04
880 #define BOTTOM_EDGE 0x08
881 #define ALL_EDGES 0x0F
884 createEdges(WScreen
*scr
)
886 if (!scr
->virtual_edges
) {
889 XSetWindowAttributes attribs
;
891 int heads
= wXineramaHeads(scr
);
892 int *hasEdges
= (int*)wmalloc(sizeof(int)*heads
);
896 int max_edges
= 4*heads
;
898 Window
*edges
= (Window
*)wmalloc(sizeof(Window
)*max_edges
);
900 for (i
=0; i
<heads
; ++i
)
901 hasEdges
[i
] = ALL_EDGES
;
902 for (i
=0; i
<heads
; ++i
) {
903 WMRect i_rect
= wGetRectForHead(scr
, i
);
904 for (j
=i
+1; j
<heads
; ++j
) {
905 WMRect j_rect
= wGetRectForHead(scr
, j
);
907 int vlen
= (WMIN(i_rect
.pos
.y
+ i_rect
.size
.height
,
908 j_rect
.pos
.y
+ j_rect
.size
.height
) -
909 WMAX(i_rect
.pos
.y
, j_rect
.pos
.y
));
911 int hlen
= (WMIN(i_rect
.pos
.x
+ i_rect
.size
.width
,
912 j_rect
.pos
.x
+ j_rect
.size
.width
) -
913 WMAX( i_rect
.pos
.x
, j_rect
.pos
.x
));
915 if (vlen
> 0 && hlen
== 0) { /* horz alignment, vert edges touch */
916 if (i_rect
.pos
.x
< j_rect
.pos
.x
) { /* i left of j */
917 hasEdges
[i
] &= ~RIGHT_EDGE
;
918 hasEdges
[j
] &= ~LEFT_EDGE
;
919 } else { /* j left of i */
920 hasEdges
[j
] &= ~RIGHT_EDGE
;
921 hasEdges
[i
] &= ~LEFT_EDGE
;
923 } else if (vlen
== 0 && hlen
> 0) { /* vert alignment, horz edges touch */
924 if (i_rect
.pos
.y
< j_rect
.pos
.y
) { /* i top of j */
925 hasEdges
[i
] &= ~BOTTOM_EDGE
;
926 hasEdges
[j
] &= ~TOP_EDGE
;
927 } else { /* j top of i */
928 hasEdges
[j
] &= ~BOTTOM_EDGE
;
929 hasEdges
[i
] &= ~TOP_EDGE
;
935 for (w
= 0; w
< scr
->workspace_count
; w
++) {
936 /* puts("reset workspace"); */
937 wWorkspaceSetViewport(scr
, w
, 0, 0);
940 vmask
= CWEventMask
|CWOverrideRedirect
;
941 attribs
.event_mask
= (EnterWindowMask
| LeaveWindowMask
| VisibilityChangeMask
);
942 attribs
.override_redirect
= True
;
944 for (head
=0; head
<wXineramaHeads(scr
); ++head
) {
945 WMRect rect
= wGetRectForHead(scr
, head
);
947 if (hasEdges
[head
] & TOP_EDGE
) {
949 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
950 rect
.size
.width
, thickness
, 0,
951 CopyFromParent
, InputOnly
, CopyFromParent
,
953 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
954 (XPointer
)vdHandleEnter_u
);
958 if (hasEdges
[head
] & BOTTOM_EDGE
) {
960 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
,
961 rect
.pos
.y
+rect
.size
.height
-thickness
,
962 rect
.size
.width
, thickness
, 0,
963 CopyFromParent
, InputOnly
, CopyFromParent
,
965 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
966 (XPointer
)vdHandleEnter_d
);
970 if (hasEdges
[head
] & LEFT_EDGE
) {
972 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
973 thickness
, rect
.pos
.y
+rect
.size
.height
, 0,
974 CopyFromParent
, InputOnly
, CopyFromParent
,
976 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
977 (XPointer
)vdHandleEnter_l
);
981 if (hasEdges
[head
] & RIGHT_EDGE
) {
983 XCreateWindow(dpy
, scr
->root_win
,
984 rect
.pos
.x
+ rect
.size
.width
- thickness
, rect
.pos
.y
,
985 thickness
, rect
.size
.height
, 0,
986 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
,
988 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
989 (XPointer
)vdHandleEnter_r
);
994 scr
->virtual_nr_edges
= nr_edges
;
995 scr
->virtual_edges
= edges
;
997 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
998 XMapWindow(dpy
, scr
->virtual_edges
[i
]);
1000 wWorkspaceRaiseEdge(scr
);
1008 destroyEdges(WScreen
*scr
)
1010 if (scr
->virtual_edges
) {
1013 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
1014 XDeleteContext(dpy
, scr
->virtual_edges
[i
], wVEdgeContext
);
1015 XUnmapWindow(dpy
, scr
->virtual_edges
[i
]);
1016 XDestroyWindow(dpy
, scr
->virtual_edges
[i
]);
1019 wfree(scr
->virtual_edges
);
1020 scr
->virtual_edges
= NULL
;
1021 scr
->virtual_nr_edges
= 0;
1027 wWorkspaceUpdateEdge(WScreen
*scr
)
1029 if (wPreferences
.vdesk_enable
) {
1038 wWorkspaceRaiseEdge(WScreen
*scr
)
1040 static int toggle
= 0;
1043 if (!scr
->virtual_edges
)
1047 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
1048 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1051 for (i
=scr
->virtual_nr_edges
-1; i
>=0; --i
) {
1052 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1061 wWorkspaceLowerEdge(WScreen
*scr
)
1064 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
1065 XLowerWindow(dpy
, scr
->virtual_edges
[i
]);
1071 wWorkspaceResizeViewport(WScreen
*scr
, int workspace
)
1074 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
1075 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
, y
);
1080 updateWorkspaceGeometry(WScreen
*scr
, int workspace
, int *view_x
, int *view_y
)
1082 int most_left
, most_right
, most_top
, most_bottom
;
1085 int heads
= wXineramaHeads(scr
);
1086 typedef int strut_t
[4];
1087 strut_t
* strut
= (strut_t
*)wmalloc(heads
*sizeof(strut_t
));
1090 for (head
=0; head
<heads
; ++head
) {
1091 WMRect rect
= wGetRectForHead(scr
, head
);
1092 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
1093 strut
[head
][0] = area
.x1
- rect
.pos
.x
;
1094 strut
[head
][1] = rect
.pos
.x
+ rect
.size
.width
- area
.x2
;
1095 strut
[head
][2] = area
.y1
- rect
.pos
.y
;
1096 strut
[head
][3] = rect
.pos
.y
+ rect
.size
.height
- area
.y2
;
1099 /* adjust workspace layout */
1100 wwin
= scr
->focused_window
;
1103 most_left
= scr
->scr_width
;
1104 most_top
= scr
->scr_height
;
1105 for(;wwin
; wwin
= wwin
->prev
) {
1106 if (wwin
->frame
->workspace
== workspace
) {
1107 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) &&
1108 !wwin
->flags
.hidden
) {
1110 head
= wGetHeadForWindow(wwin
);
1112 i
= wwin
->frame_x
- strut
[head
][0];
1113 if (i
< most_left
) /* record positions, should this be cached? */
1115 i
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ strut
[head
][1];
1116 if (HAS_BORDER(wwin
))
1120 i
= wwin
->frame_y
- strut
[head
][2];
1123 i
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ strut
[head
][3];
1124 if (HAS_BORDER(wwin
))
1126 if (i
> most_bottom
) {
1133 if (most_left
> 0) most_left
= 0;
1134 if (most_top
> 0) most_top
= 0;
1136 scr
->workspaces
[workspace
]->width
= WMAX(most_right
, scr
->scr_width
) - WMIN(most_left
, 0);
1137 scr
->workspaces
[workspace
]->height
= WMAX(most_bottom
, scr
->scr_height
) - WMIN(most_top
, 0);
1139 *view_x
+= -most_left
- scr
->workspaces
[workspace
]->view_x
;
1140 scr
->workspaces
[workspace
]->view_x
= -most_left
;
1142 *view_y
+= -most_top
- scr
->workspaces
[workspace
]->view_y
;
1143 scr
->workspaces
[workspace
]->view_y
= -most_top
;
1149 typedef struct _delay_configure
{
1156 sendConfigureNotify (_delay_configure
*delay
)
1160 delay
->delay_count
--;
1161 if (!delay
->delay_count
) {
1162 for (wwin
= delay
->wwin
; wwin
; wwin
= wwin
->prev
) {
1163 wWindowSynthConfigureNotify(wwin
);
1170 wWorkspaceSetViewport(WScreen
*scr
, int workspace
, int view_x
, int view_y
)
1172 Bool adjust_flag
= False
;
1174 static _delay_configure delay_configure
= {NULL
, 0};
1178 wptr
= scr
->workspaces
[workspace
];
1180 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y);*/
1182 updateWorkspaceGeometry(scr
, workspace
, &view_x
, &view_y
);
1184 if (view_x
+ scr
->scr_width
> wptr
->width
) {
1185 /* puts("right edge of vdesk"); */
1186 view_x
= wptr
->width
- scr
->scr_width
;
1189 /* puts("left edge of vdesk"); */
1192 if (view_y
+ scr
->scr_height
> wptr
->height
) {
1193 /* puts("right edge of vdesk"); */
1194 view_y
= wptr
->height
- scr
->scr_height
;
1197 /* puts("left edge of vdesk"); */
1201 diff_x
= wptr
->view_x
- view_x
;
1202 diff_y
= wptr
->view_y
- view_y
;
1203 if (!diff_x
&& !diff_y
)
1206 wptr
->view_x
= view_x
;
1207 wptr
->view_y
= view_y
;
1210 wNETWMUpdateDesktop(scr
);
1213 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
1214 if (wwin
->frame
->workspace
== workspace
&& !IS_VSTUCK(wwin
)) {
1215 wWindowMove(wwin
, wwin
->frame_x
+ diff_x
, wwin
->frame_y
+ diff_y
);
1219 if (1) { /* if delay*/
1220 delay_configure
.delay_count
++;
1221 delay_configure
.wwin
= scr
->focused_window
;
1222 WMAddTimerHandler(200, (WMCallback
*)sendConfigureNotify
, &delay_configure
);
1233 switchWSCommand(WMenu
*menu
, WMenuEntry
*entry
)
1235 wWorkspaceChange(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
1240 deleteWSCommand(WMenu
*menu
, WMenuEntry
*entry
)
1242 wWorkspaceDelete(menu
->frame
->screen_ptr
,
1243 menu
->frame
->screen_ptr
->workspace_count
-1);
1248 newWSCommand(WMenu
*menu
, WMenuEntry
*foo
)
1252 ws
= wWorkspaceNew(menu
->frame
->screen_ptr
);
1253 /* autochange workspace*/
1255 wWorkspaceChange(menu
->frame
->screen_ptr
, ws
);
1261 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1262 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1264 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1271 cropline(char *line
)
1275 if (strlen(line
)==0)
1279 end
= &(line
[strlen(line
)])-1;
1280 while (isspace(*line
) && *line
!=0) line
++;
1281 while (isspace(*end
) && end
!=line
) {
1290 wWorkspaceRename(WScreen
*scr
, int workspace
, char *name
)
1292 char buf
[MAX_WORKSPACENAME_WIDTH
+1];
1295 if (workspace
>= scr
->workspace_count
)
1298 /* trim white spaces */
1299 tmp
= cropline(name
);
1301 if (strlen(tmp
)==0) {
1302 snprintf(buf
, sizeof(buf
), _("Workspace %i"), workspace
+1);
1304 strncpy(buf
, tmp
, MAX_WORKSPACENAME_WIDTH
);
1306 buf
[MAX_WORKSPACENAME_WIDTH
] = 0;
1308 /* update workspace */
1309 wfree(scr
->workspaces
[workspace
]->name
);
1310 scr
->workspaces
[workspace
]->name
= wstrdup(buf
);
1312 if (scr
->clip_ws_menu
) {
1313 if (strcmp(scr
->clip_ws_menu
->entries
[workspace
+2]->text
, buf
)!=0) {
1314 wfree(scr
->clip_ws_menu
->entries
[workspace
+2]->text
);
1315 scr
->clip_ws_menu
->entries
[workspace
+2]->text
= wstrdup(buf
);
1316 wMenuRealize(scr
->clip_ws_menu
);
1319 if (scr
->workspace_menu
) {
1320 if (strcmp(scr
->workspace_menu
->entries
[workspace
+2]->text
, buf
)!=0) {
1321 wfree(scr
->workspace_menu
->entries
[workspace
+2]->text
);
1322 scr
->workspace_menu
->entries
[workspace
+2]->text
= wstrdup(buf
);
1323 wMenuRealize(scr
->workspace_menu
);
1328 wClipIconPaint(scr
->clip_icon
);
1330 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void*)workspace
);
1336 /* callback for when menu entry is edited */
1338 onMenuEntryEdited(WMenu
*menu
, WMenuEntry
*entry
)
1343 wWorkspaceRename(menu
->frame
->screen_ptr
, (long)entry
->clientdata
, tmp
);
1348 wWorkspaceMenuMake(WScreen
*scr
, Bool titled
)
1352 wsmenu
= wMenuCreate(scr
, titled
? _("Workspaces") : NULL
, False
);
1354 wwarning(_("could not create Workspace menu"));
1358 /* callback to be called when an entry is edited */
1359 wsmenu
->on_edit
= onMenuEntryEdited
;
1361 wMenuAddCallback(wsmenu
, _("New"), newWSCommand
, NULL
);
1362 wMenuAddCallback(wsmenu
, _("Destroy Last"), deleteWSCommand
, NULL
);
1370 wWorkspaceMenuUpdate(WScreen
*scr
, WMenu
*menu
)
1374 char title
[MAX_WORKSPACENAME_WIDTH
+1];
1381 if (menu
->entry_no
< scr
->workspace_count
+2) {
1382 /* new workspace(s) added */
1383 i
= scr
->workspace_count
-(menu
->entry_no
-2);
1384 ws
= menu
->entry_no
- 2;
1386 strcpy(title
, scr
->workspaces
[ws
]->name
);
1388 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, (void*)ws
);
1389 entry
->flags
.indicator
= 1;
1390 entry
->flags
.editable
= 1;
1395 } else if (menu
->entry_no
> scr
->workspace_count
+2) {
1396 /* removed workspace(s) */
1397 for (i
= menu
->entry_no
-1; i
>= scr
->workspace_count
+2; i
--) {
1398 wMenuRemoveItem(menu
, i
);
1403 for (i
=0; i
<scr
->workspace_count
; i
++) {
1404 menu
->entries
[i
+2]->flags
.indicator_on
= 0;
1406 menu
->entries
[scr
->current_workspace
+2]->flags
.indicator_on
= 1;
1408 /* don't let user destroy current workspace */
1409 if (scr
->current_workspace
== scr
->workspace_count
-1) {
1410 wMenuSetEnabled(menu
, 1, False
);
1412 wMenuSetEnabled(menu
, 1, True
);
1415 tmp
= menu
->frame
->top_width
+ 5;
1416 /* if menu got unreachable, bring it to a visible place */
1417 if (menu
->frame_x
< tmp
- (int)menu
->frame
->core
->width
)
1418 wMenuMove(menu
, tmp
- (int)menu
->frame
->core
->width
, menu
->frame_y
, False
);
1425 wWorkspaceSaveState(WScreen
*scr
, WMPropList
*old_state
)
1427 WMPropList
*parr
, *pstr
, *wks_state
, *old_wks_state
, *foo
, *bar
;
1432 old_wks_state
= WMGetFromPLDictionary(old_state
, dWorkspaces
);
1433 parr
= WMCreatePLArray(NULL
);
1434 for (i
=0; i
< scr
->workspace_count
; i
++) {
1435 pstr
= WMCreatePLString(scr
->workspaces
[i
]->name
);
1436 wks_state
= WMCreatePLDictionary(dName
, pstr
, NULL
);
1437 WMReleasePropList(pstr
);
1438 if (!wPreferences
.flags
.noclip
) {
1439 pstr
= wClipSaveWorkspaceState(scr
, i
);
1440 WMPutInPLDictionary(wks_state
, dClip
, pstr
);
1441 WMReleasePropList(pstr
);
1442 } else if (old_wks_state
!=NULL
) {
1443 if ((foo
= WMGetFromPLArray(old_wks_state
, i
))!=NULL
) {
1444 if ((bar
= WMGetFromPLDictionary(foo
, dClip
))!=NULL
) {
1445 WMPutInPLDictionary(wks_state
, dClip
, bar
);
1449 WMAddToPLArray(parr
, wks_state
);
1450 WMReleasePropList(wks_state
);
1452 WMPutInPLDictionary(scr
->session_state
, dWorkspaces
, parr
);
1453 WMReleasePropList(parr
);
1458 wWorkspaceRestoreState(WScreen
*scr
)
1460 WMPropList
*parr
, *pstr
, *wks_state
, *clip_state
;
1465 if (scr
->session_state
== NULL
)
1468 parr
= WMGetFromPLDictionary(scr
->session_state
, dWorkspaces
);
1473 wscount
= scr
->workspace_count
;
1474 for (i
=0; i
< WMIN(WMGetPropListItemCount(parr
), MAX_WORKSPACES
); i
++) {
1475 wks_state
= WMGetFromPLArray(parr
, i
);
1476 if (WMIsPLDictionary(wks_state
))
1477 pstr
= WMGetFromPLDictionary(wks_state
, dName
);
1480 if (i
>= scr
->workspace_count
)
1482 if (scr
->workspace_menu
) {
1483 wfree(scr
->workspace_menu
->entries
[i
+2]->text
);
1484 scr
->workspace_menu
->entries
[i
+2]->text
= wstrdup(WMGetFromPLString(pstr
));
1485 scr
->workspace_menu
->flags
.realized
= 0;
1487 wfree(scr
->workspaces
[i
]->name
);
1488 scr
->workspaces
[i
]->name
= wstrdup(WMGetFromPLString(pstr
));
1489 if (!wPreferences
.flags
.noclip
) {
1490 clip_state
= WMGetFromPLDictionary(wks_state
, dClip
);
1491 if (scr
->workspaces
[i
]->clip
)
1492 wDockDestroy(scr
->workspaces
[i
]->clip
);
1493 scr
->workspaces
[i
]->clip
= wDockRestoreState(scr
, clip_state
,
1496 wDockHideIcons(scr
->workspaces
[i
]->clip
);
1498 /* We set the global icons here, because scr->workspaces[i]->clip
1499 * was not valid in wDockRestoreState().
1500 * There we only set icon->omnipresent to know which icons we
1503 for (j
=0; j
<scr
->workspaces
[i
]->clip
->max_icons
; j
++) {
1504 WAppIcon
*aicon
= scr
->workspaces
[i
]->clip
->icon_array
[j
];
1506 if (aicon
&& aicon
->omnipresent
) {
1507 aicon
->omnipresent
= 0;
1508 wClipMakeIconOmnipresent(aicon
, True
);
1509 XMapWindow(dpy
, aicon
->icon
->core
->window
);
1514 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void*)i
);