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"
60 extern WPreferences wPreferences
;
61 extern XContext wWinContext
;
62 extern XContext wVEdgeContext
;
64 extern void ProcessPendingEvents();
66 static WMPropList
*dWorkspaces
=NULL
;
67 static WMPropList
*dClip
, *dName
;
69 #ifdef VIRTUAL_DESKTOP
70 static BOOL initVDesk
= False
;
77 if (dWorkspaces
!=NULL
)
80 dWorkspaces
= WMCreatePLString("Workspaces");
81 dName
= WMCreatePLString("Name");
82 dClip
= WMCreatePLString("Clip");
87 wWorkspaceMake(WScreen
*scr
, int count
)
97 wWorkspaceNew(WScreen
*scr
)
99 WWorkspace
*wspace
, **list
;
102 if (scr
->workspace_count
< MAX_WORKSPACES
) {
103 scr
->workspace_count
++;
105 wspace
= wmalloc(sizeof(WWorkspace
));
109 if (scr
->flags
.kwm_syncing_count
) {
110 wspace
->name
= wKWMGetWorkspaceName(scr
, scr
->workspace_count
-1);
114 wspace
->name
= wmalloc(strlen(_("Workspace %i"))+8);
115 sprintf(wspace
->name
, _("Workspace %i"), scr
->workspace_count
);
119 if (!wPreferences
.flags
.noclip
) {
120 wspace
->clip
= wDockCreate(scr
, WM_CLIP
);
124 list
= wmalloc(sizeof(WWorkspace
*)*scr
->workspace_count
);
126 for (i
=0; i
<scr
->workspace_count
-1; i
++) {
127 list
[i
] = scr
->workspaces
[i
];
131 wfree(scr
->workspaces
);
132 scr
->workspaces
= list
;
134 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
135 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
136 #ifdef VIRTUAL_DESKTOP
137 wspace
->view_x
= wspace
->view_y
= 0;
138 wspace
->height
= scr
->scr_height
;
139 wspace
->width
= scr
->scr_width
;
142 wNETWMUpdateDesktop(scr
);
145 WMPostNotificationName(WMNWorkspaceCreated
, scr
,
146 (void*)(scr
->workspace_count
-1));
149 return scr
->workspace_count
-1;
157 wWorkspaceDelete(WScreen
*scr
, int workspace
)
166 /* verify if workspace is in use by some window */
167 tmp
= scr
->focused_window
;
169 if (!IS_OMNIPRESENT(tmp
) && tmp
->frame
->workspace
==workspace
)
174 if (!wPreferences
.flags
.noclip
) {
175 wDockDestroy(scr
->workspaces
[workspace
]->clip
);
176 scr
->workspaces
[workspace
]->clip
= NULL
;
179 list
= wmalloc(sizeof(WWorkspace
*)*(scr
->workspace_count
-1));
181 for (i
=0; i
<scr
->workspace_count
; i
++) {
183 list
[j
++] = scr
->workspaces
[i
];
185 if (scr
->workspaces
[i
]->name
)
186 wfree(scr
->workspaces
[i
]->name
);
187 wfree(scr
->workspaces
[i
]);
190 wfree(scr
->workspaces
);
191 scr
->workspaces
= list
;
193 scr
->workspace_count
--;
197 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
198 /* clip workspace menu */
199 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
201 /* update also window menu */
202 if (scr
->workspace_submenu
) {
203 WMenu
*menu
= scr
->workspace_submenu
;
206 while (i
>scr
->workspace_count
)
207 wMenuRemoveItem(menu
, --i
);
211 if (scr
->clip_submenu
) {
212 WMenu
*menu
= scr
->clip_submenu
;
215 while (i
>scr
->workspace_count
)
216 wMenuRemoveItem(menu
, --i
);
221 wNETWMUpdateDesktop(scr
);
224 WMPostNotificationName(WMNWorkspaceDestroyed
, scr
,
225 (void*)(scr
->workspace_count
-1));
227 if (scr
->current_workspace
>= scr
->workspace_count
)
228 wWorkspaceChange(scr
, scr
->workspace_count
-1);
234 typedef struct WorkspaceNameData
{
243 hideWorkspaceName(void *data
)
245 WScreen
*scr
= (WScreen
*)data
;
247 if (!scr
->workspace_name_data
|| scr
->workspace_name_data
->count
== 0
248 || time(NULL
) > scr
->workspace_name_data
->timeout
) {
249 XUnmapWindow(dpy
, scr
->workspace_name
);
251 if (scr
->workspace_name_data
) {
252 RReleaseImage(scr
->workspace_name_data
->back
);
253 RReleaseImage(scr
->workspace_name_data
->text
);
254 wfree(scr
->workspace_name_data
);
256 scr
->workspace_name_data
= NULL
;
258 scr
->workspace_name_timer
= NULL
;
260 RImage
*img
= RCloneImage(scr
->workspace_name_data
->back
);
263 scr
->workspace_name_timer
=
264 WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
266 RCombineImagesWithOpaqueness(img
, scr
->workspace_name_data
->text
,
267 scr
->workspace_name_data
->count
*255/10);
269 RConvertImage(scr
->rcontext
, img
, &pix
);
273 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, pix
);
274 XClearWindow(dpy
, scr
->workspace_name
);
275 XFreePixmap(dpy
, pix
);
278 scr
->workspace_name_data
->count
--;
284 showWorkspaceName(WScreen
*scr
, int workspace
)
286 WorkspaceNameData
*data
;
291 char *name
= scr
->workspaces
[workspace
]->name
;
292 int len
= strlen(name
);
295 if (wPreferences
.workspace_name_display_position
== WD_NONE
||
296 scr
->workspace_count
< 2) {
300 if (scr
->workspace_name_timer
) {
301 WMDeleteTimerHandler(scr
->workspace_name_timer
);
302 XUnmapWindow(dpy
, scr
->workspace_name
);
305 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
,
306 hideWorkspaceName
, scr
);
308 if (scr
->workspace_name_data
) {
309 RReleaseImage(scr
->workspace_name_data
->back
);
310 RReleaseImage(scr
->workspace_name_data
->text
);
311 wfree(scr
->workspace_name_data
);
314 data
= wmalloc(sizeof(WorkspaceNameData
));
317 w
= WMWidthOfString(scr
->workspace_name_font
, name
, len
);
318 h
= WMFontHeight(scr
->workspace_name_font
);
320 switch (wPreferences
.workspace_name_display_position
) {
322 px
= (scr
->scr_width
- (w
+4))/2;
326 px
= (scr
->scr_width
- (w
+4))/2;
327 py
= scr
->scr_height
- (h
+4);
334 px
= scr
->scr_width
- (w
+4);
339 py
= scr
->scr_height
- (h
+4);
342 px
= scr
->scr_width
- (w
+4);
343 py
= scr
->scr_height
- (h
+4);
347 px
= (scr
->scr_width
- (w
+4))/2;
348 py
= (scr
->scr_height
- (h
+4))/2;
351 XResizeWindow(dpy
, scr
->workspace_name
, w
+4, h
+4);
352 XMoveWindow(dpy
, scr
->workspace_name
, px
, py
);
354 text
= XCreatePixmap(dpy
, scr
->w_win
, w
+4, h
+4, scr
->w_depth
);
355 mask
= XCreatePixmap(dpy
, scr
->w_win
, w
+4, h
+4, 1);
357 /*XSetForeground(dpy, scr->mono_gc, 0);
358 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4);*/
360 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+4, h
+4);
362 for (x
= 0; x
<= 4; x
++) {
363 for (y
= 0; y
<= 4; y
++) {
364 WMDrawString(scr
->wmscreen
, text
, scr
->white
,
365 scr
->workspace_name_font
, x
, y
, name
, len
);
369 XSetForeground(dpy
, scr
->mono_gc
, 1);
370 XSetBackground(dpy
, scr
->mono_gc
, 0);
372 XCopyPlane(dpy
, text
, mask
, scr
->mono_gc
, 0, 0, w
+4, h
+4, 0, 0, 1<<(scr
->w_depth
-1));
374 /*XSetForeground(dpy, scr->mono_gc, 1);*/
375 XSetBackground(dpy
, scr
->mono_gc
, 1);
377 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+4, h
+4);
379 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
,
383 XShapeCombineMask(dpy
, scr
->workspace_name
, ShapeBounding
, 0, 0, mask
,
386 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, text
);
387 XClearWindow(dpy
, scr
->workspace_name
);
389 data
->text
= RCreateImageFromDrawable(scr
->rcontext
, text
, None
);
391 XFreePixmap(dpy
, text
);
392 XFreePixmap(dpy
, mask
);
395 XMapRaised(dpy
, scr
->workspace_name
);
401 ximg
= RGetXImage(scr
->rcontext
, scr
->root_win
, px
, py
,
402 data
->text
->width
, data
->text
->height
);
404 if (!ximg
|| !ximg
->image
) {
408 XMapRaised(dpy
, scr
->workspace_name
);
411 data
->back
= RCreateImageFromXImage(scr
->rcontext
, ximg
->image
, NULL
);
412 RDestroyXImage(scr
->rcontext
, ximg
);
420 /* set a timeout for the effect */
421 data
->timeout
= time(NULL
) + 2 +
422 (WORKSPACE_NAME_DELAY
+ WORKSPACE_NAME_FADE_DELAY
*data
->count
)/1000;
424 scr
->workspace_name_data
= data
;
430 if (scr
->workspace_name_timer
)
431 WMDeleteTimerHandler(scr
->workspace_name_timer
);
434 RReleaseImage(data
->text
);
436 RReleaseImage(data
->back
);
439 scr
->workspace_name_data
= NULL
;
441 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
+
442 10*WORKSPACE_NAME_FADE_DELAY
,
443 hideWorkspaceName
, scr
);
448 wWorkspaceChange(WScreen
*scr
, int workspace
)
450 if (scr
->flags
.startup
|| scr
->flags
.startup2
) {
454 if (workspace
!= scr
->current_workspace
) {
455 wWorkspaceForceChange(scr
, workspace
);
457 showWorkspaceName(scr, workspace);
463 wWorkspaceRelativeChange(WScreen
*scr
, int amount
)
467 w
= scr
->current_workspace
+ amount
;
471 wWorkspaceChange(scr
, w
);
472 } else if (wPreferences
.ws_cycle
) {
473 wWorkspaceChange(scr
, scr
->workspace_count
+ w
);
475 } else if (amount
> 0) {
476 if (w
< scr
->workspace_count
) {
477 wWorkspaceChange(scr
, w
);
478 } else if (wPreferences
.ws_advance
) {
479 wWorkspaceChange(scr
, WMIN(w
, MAX_WORKSPACES
-1));
480 } else if (wPreferences
.ws_cycle
) {
481 wWorkspaceChange(scr
, w
% scr
->workspace_count
);
488 wWorkspaceForceChange(WScreen
*scr
, int workspace
)
490 WWindow
*tmp
, *foc
=NULL
, *foc2
=NULL
;
492 if (workspace
>= MAX_WORKSPACES
|| workspace
< 0)
495 SendHelperMessage(scr
, 'C', workspace
+1, NULL
);
497 if (workspace
> scr
->workspace_count
-1) {
498 wWorkspaceMake(scr
, workspace
- scr
->workspace_count
+ 1);
501 wClipUpdateForWorkspaceChange(scr
, workspace
);
503 scr
->current_workspace
= workspace
;
505 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
507 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
509 if ((tmp
= scr
->focused_window
)!= NULL
) {
510 if ((IS_OMNIPRESENT(tmp
) && (tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
511 !WFLAGP(tmp
, no_focusable
)) || tmp
->flags
.changing_workspace
) {
515 /* foc2 = tmp; will fix annoyance with gnome panel
516 * but will create annoyance for every other application
519 if (tmp
->frame
->workspace
!=workspace
&& !tmp
->flags
.selected
) {
520 /* unmap windows not on this workspace */
521 if ((tmp
->flags
.mapped
||tmp
->flags
.shaded
) &&
522 !IS_OMNIPRESENT(tmp
) && !tmp
->flags
.changing_workspace
) {
525 /* also unmap miniwindows not on this workspace */
526 if (!wPreferences
.sticky_icons
&& tmp
->flags
.miniaturized
&&
527 tmp
->icon
&& !IS_OMNIPRESENT(tmp
)) {
528 XUnmapWindow(dpy
, tmp
->icon
->core
->window
);
529 tmp
->icon
->mapped
= 0;
531 /* update current workspace of omnipresent windows */
532 if (IS_OMNIPRESENT(tmp
)) {
533 WApplication
*wapp
= wApplicationOf(tmp
->main_window
);
535 tmp
->frame
->workspace
= workspace
;
538 wapp
->last_workspace
= workspace
;
540 if (!foc2
&& (tmp
->flags
.mapped
|| tmp
->flags
.shaded
)) {
545 /* change selected windows' workspace */
546 if (tmp
->flags
.selected
) {
547 wWindowChangeWorkspace(tmp
, workspace
);
548 if (!tmp
->flags
.miniaturized
&& !foc
) {
552 if (!tmp
->flags
.hidden
) {
553 if (!(tmp
->flags
.mapped
|| tmp
->flags
.miniaturized
)) {
554 /* remap windows that are on this workspace */
556 if (!foc
&& !WFLAGP(tmp
, no_focusable
)) {
560 /* Also map miniwindow if not omnipresent */
561 if (!wPreferences
.sticky_icons
&&
562 tmp
->flags
.miniaturized
&&
563 !IS_OMNIPRESENT(tmp
) && tmp
->icon
) {
564 tmp
->icon
->mapped
= 1;
565 XMapWindow(dpy
, tmp
->icon
->core
->window
);
573 /* Gobble up events unleashed by our mapping & unmapping.
574 * These may trigger various grab-initiated focus &
575 * crossing events. However, we don't care about them,
576 * and ignore their focus implications altogether to avoid
579 scr
->flags
.ignore_focus_events
= 1;
580 ProcessPendingEvents();
581 scr
->flags
.ignore_focus_events
= 0;
586 if (scr
->focused_window
->flags
.mapped
&& !foc
) {
587 foc
= scr
->focused_window
;
589 if (wPreferences
.focus_mode
== WKF_CLICK
) {
590 wSetFocusTo(scr
, foc
);
598 if (XQueryPointer(dpy
, scr
->root_win
, &bar
, &win
,
599 &foo
, &foo
, &foo
, &foo
, &mask
)) {
600 tmp
= wWindowFor(win
);
603 /* If there's a window under the pointer, focus it.
604 * (we ate all other focus events above, so it's
605 * certainly not focused). Otherwise focus last
606 * focused, or the root (depending on sloppiness)
608 if (!tmp
&& wPreferences
.focus_mode
== WKF_SLOPPY
) {
609 wSetFocusTo(scr
, foc
);
611 wSetFocusTo(scr
, tmp
);
616 /* We need to always arrange icons when changing workspace, even if
617 * no autoarrange icons, because else the icons in different workspaces
619 * This can be avoided if appicons are also workspace specific.
621 if (!wPreferences
.sticky_icons
)
622 wArrangeIcons(scr
, False
);
625 wAppIconPaint(scr
->dock
->icon_array
[0]);
627 if (scr
->clip_icon
) {
628 if (scr
->workspaces
[workspace
]->clip
->auto_collapse
||
629 scr
->workspaces
[workspace
]->clip
->auto_raise_lower
) {
630 /* to handle enter notify. This will also */
631 XUnmapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
632 XMapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
634 wClipIconPaint(scr
->clip_icon
);
638 #if defined KWM_HINTS || defined NETWM_HINTS
639 wScreenUpdateUsableArea(scr
);
642 wNETWMUpdateDesktop(scr
);
645 showWorkspaceName(scr
, workspace
);
647 WMPostNotificationName(WMNWorkspaceChanged
, scr
, (void*)workspace
);
649 /* XSync(dpy, False); */
653 #ifdef VIRTUAL_DESKTOP
657 * 1) Allow border around each window so the scrolling
658 * won't just stop at the border.
663 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
664 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
665 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
666 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
667 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
668 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
671 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
675 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
678 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
680 static Bool
cmp_gez(int i
) { return (i
>= 0); }
682 static Bool
cmp_gz(int i
) { return (i
> 0); }
686 getClosestEdge(WScreen
* scr
, WMPoint direction
, Bool (*cmp
)(int))
688 WMPoint closest
= wmkpoint(0, 0);
689 int closest_len
= INT_MAX
;
692 for (wwin
=scr
->focused_window
; wwin
; wwin
=wwin
->prev
) {
693 if (wwin
->frame
->workspace
== scr
->current_workspace
) {
694 if (!wwin
->flags
.miniaturized
&&
696 !wwin
->flags
.hidden
) {
697 int border
= 2*HAS_BORDER(wwin
);
700 int head
= wGetHeadForWindow(wwin
);
701 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
704 x1
= wwin
->frame_x
- area
.x1
;
705 y1
= wwin
->frame_y
- area
.y1
;
706 x2
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ border
- area
.x2
;
707 y2
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ border
- area
.y2
;
710 len
= vec_dot(direction
, p
);
711 updateMinimum(len
, p
, closest_len
, closest
);
714 len
= vec_dot(direction
, p
);
715 updateMinimum(len
, p
, closest_len
, closest
);
718 len
= vec_dot(direction
, p
);
719 updateMinimum(len
, p
, closest_len
, closest
);
722 len
= vec_dot(direction
, p
);
723 updateMinimum(len
, p
, closest_len
, closest
);
733 wWorkspaceKeyboardMoveDesktop(WScreen
*scr
, WMPoint direction
)
736 WMPoint edge
= getClosestEdge(scr
, direction
, cmp_gz
);
737 int len
= vec_dot(edge
, direction
);
738 WMPoint step
= vec_scale(direction
, len
);
739 wWorkspaceGetViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
740 wWorkspaceSetViewPort(scr
, scr
->current_workspace
, x
+step
.x
, y
+step
.y
);
744 extern Cursor wCursor
[WCUR_LAST
];
748 vdMouseMoveDesktop(XEvent
*event
, WMPoint direction
)
750 static int lock
= False
;
756 WScreen
*scr
= wScreenForRootWindow(event
->xcrossing
.root
);
757 WMPoint old_pos
= wmkpoint(event
->xcrossing
.x_root
, event
->xcrossing
.y_root
);
762 if (XGrabPointer(dpy
, event
->xcrossing
.window
, False
,
763 PointerMotionMask
, GrabModeAsync
, GrabModeAsync
,
764 scr
->root_win
, wCursor
[WCUR_EMPTY
],
765 CurrentTime
) != GrabSuccess
) {
767 /* if the grab fails, do it the old fashioned way */
768 step
= vec_scale2(direction
, wPreferences
.vedge_hscrollspeed
,
769 wPreferences
.vedge_vscrollspeed
);
770 wWorkspaceGetViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
771 if (wWorkspaceSetViewPort(scr
, scr
->current_workspace
,
772 x
+step
.x
, y
+step
.y
)) {
773 step
= vec_scale(direction
, wPreferences
.vedge_thickness
+ 1);
774 XWarpPointer(dpy
, None
, scr
->root_win
, 0,0,0,0,
775 event
->xcrossing
.x_root
- step
.x
,
776 event
->xcrossing
.y_root
- step
.y
);
786 if (old_pos
.x
> scr
->scr_width
)
787 old_pos
.x
= scr
->scr_width
;
788 if (old_pos
.y
> scr
->scr_height
)
789 old_pos
.y
= scr
->scr_height
;
794 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
795 scr
->scr_width
/2, scr
->scr_height
/2);
798 WMMaskEvent(dpy
, PointerMotionMask
, &ev
);
804 step
= wmkpoint(ev
.xmotion
.x_root
-scr
->scr_width
/2,
805 ev
.xmotion
.y_root
-scr
->scr_height
/2);
806 step_len
= vec_dot(step
, direction
);
814 int resist
= wPreferences
.vedge_resistance
;
816 int closest_len
= INT_MAX
;
818 closest
= getClosestEdge(scr
, direction
, cmp_gez
);
819 closest_len
= vec_dot(direction
, closest
);
823 resisted
+= step_len
;
824 do_move
= resisted
>= resist
;
826 closest_len
= INT_MAX
;
827 step_len
= resisted
- resist
;
832 if (closest_len
<= wPreferences
.vedge_attraction
) {
833 step
= vec_scale(direction
, closest_len
);
835 step
= vec_scale(direction
, step_len
);
838 wWorkspaceGetViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
839 wWorkspaceSetViewPort(scr
, scr
->current_workspace
,
849 step
= vec_add(old_pos
, vec_scale(direction
, -1));
850 XWarpPointer(dpy
, None
, scr
->root_win
, 0,0,0,0, step
.x
, step
.y
);
851 XUngrabPointer(dpy
, CurrentTime
);
859 vdHandleEnter_u(XEvent
*event
) {
860 vdMouseMoveDesktop(event
, VEC_UP
);
865 vdHandleEnter_d(XEvent
*event
) {
866 vdMouseMoveDesktop(event
, VEC_DOWN
);
871 vdHandleEnter_l(XEvent
*event
) {
872 vdMouseMoveDesktop(event
, VEC_LEFT
);
877 vdHandleEnter_r(XEvent
*event
) {
878 vdMouseMoveDesktop(event
, VEC_RIGHT
);
883 wWorkspaceMapEdge(WScreen
*scr
)
886 if (wPreferences
.vedge_thickness
&& initVDesk
) {
887 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
888 XMapWindow(dpy
, scr
->virtual_edges
[i
]);
895 wWorkspaceUnmapEdge(WScreen
*scr
)
898 if (wPreferences
.vedge_thickness
&& initVDesk
) {
899 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
900 XUnmapWindow(dpy
, scr
->virtual_edges
[i
]);
906 #define LEFT_EDGE 0x01
907 #define RIGHT_EDGE 0x02
908 #define TOP_EDGE 0x04
909 #define BOTTOM_EDGE 0x08
910 #define ALL_EDGES 0x0F
913 wWorkspaceManageEdge(WScreen
*scr
)
915 if (!initVDesk
&& wPreferences
.vedge_thickness
) {
918 XSetWindowAttributes attribs
;
920 int heads
= wXineramaHeads(scr
);
921 int *hasEdges
= (int*)wmalloc(sizeof(int)*heads
);
923 int thickness
= wPreferences
.vedge_thickness
;
925 int max_edges
= 4*heads
;
927 Window
* edges
= (Window
*)wmalloc(sizeof(Window
)*max_edges
);
931 for (i
=0; i
<heads
; ++i
)
932 hasEdges
[i
] = ALL_EDGES
;
933 for (i
=0; i
<heads
; ++i
) {
934 WMRect i_rect
= wGetRectForHead(scr
, i
);
935 for (j
=i
+1; j
<heads
; ++j
) {
936 WMRect j_rect
= wGetRectForHead(scr
, j
);
938 int vlen
= (WMIN(i_rect
.pos
.y
+ i_rect
.size
.height
,
939 j_rect
.pos
.y
+ j_rect
.size
.height
) -
940 WMAX(i_rect
.pos
.y
, j_rect
.pos
.y
));
942 int hlen
= (WMIN(i_rect
.pos
.x
+ i_rect
.size
.width
,
943 j_rect
.pos
.x
+ j_rect
.size
.width
) -
944 WMAX( i_rect
.pos
.x
, j_rect
.pos
.x
));
946 if (vlen
> 0 && hlen
== 0) { /* horz alignment, vert edges touch */
947 if (i_rect
.pos
.x
< j_rect
.pos
.x
) { /* i left of j */
948 hasEdges
[i
] &= ~RIGHT_EDGE
;
949 hasEdges
[j
] &= ~LEFT_EDGE
;
950 } else { /* j left of i */
951 hasEdges
[j
] &= ~RIGHT_EDGE
;
952 hasEdges
[i
] &= ~LEFT_EDGE
;
954 } else if (vlen
== 0 && hlen
> 0) { /* vert alignment, horz edges touch */
955 if (i_rect
.pos
.y
< j_rect
.pos
.y
) { /* i top of j */
956 hasEdges
[i
] &= ~BOTTOM_EDGE
;
957 hasEdges
[j
] &= ~TOP_EDGE
;
958 } else { /* j top of i */
959 hasEdges
[j
] &= ~BOTTOM_EDGE
;
960 hasEdges
[i
] &= ~TOP_EDGE
;
966 for (w
= 0; w
< scr
->workspace_count
; w
++) {
967 /* puts("reset workspace"); */
968 wWorkspaceSetViewPort(scr
, w
, 0, 0);
971 vmask
= CWEventMask
|CWOverrideRedirect
;
972 attribs
.event_mask
= (EnterWindowMask
| LeaveWindowMask
| VisibilityChangeMask
);
973 attribs
.override_redirect
= True
;
975 for (head
=0; head
<wXineramaHeads(scr
); ++head
) {
976 WMRect rect
= wGetRectForHead(scr
, head
);
978 if (hasEdges
[head
] & TOP_EDGE
) {
980 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
981 rect
.size
.width
, thickness
, 0,
982 CopyFromParent
, InputOnly
, CopyFromParent
,
984 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
985 (XPointer
)vdHandleEnter_u
);
989 if (hasEdges
[head
] & BOTTOM_EDGE
) {
991 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
,
992 rect
.pos
.y
+rect
.size
.height
-thickness
,
993 rect
.size
.width
, thickness
, 0,
994 CopyFromParent
, InputOnly
, CopyFromParent
,
996 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
997 (XPointer
)vdHandleEnter_d
);
1001 if (hasEdges
[head
] & LEFT_EDGE
) {
1003 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
1004 thickness
, rect
.pos
.y
+rect
.size
.height
, 0,
1005 CopyFromParent
, InputOnly
, CopyFromParent
,
1007 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
1008 (XPointer
)vdHandleEnter_l
);
1012 if (hasEdges
[head
] & RIGHT_EDGE
) {
1014 XCreateWindow(dpy
, scr
->root_win
,
1015 rect
.pos
.x
+ rect
.size
.width
- thickness
, rect
.pos
.y
,
1016 thickness
, rect
.size
.height
, 0,
1017 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
,
1019 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
1020 (XPointer
)vdHandleEnter_r
);
1025 scr
->virtual_nr_edges
= nr_edges
;
1026 scr
->virtual_edges
= edges
;
1028 wWorkspaceMapEdge(scr
);
1029 wWorkspaceRaiseEdge(scr
);
1037 wWorkspaceUpdateEdge(WScreen
*scr
)
1039 if (!initVDesk
&& wPreferences
.vedge_thickness
) {
1040 wWorkspaceManageEdge(scr
);
1041 } else if (initVDesk
) {
1042 if (wPreferences
.vedge_thickness
) {
1043 wWorkspaceMapEdge(scr
);
1045 wWorkspaceUnmapEdge(scr
);
1052 wWorkspaceDestroyEdge(WScreen
*scr
)
1057 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
1058 XDeleteContext(dpy
, scr
->virtual_edges
[i
], wVEdgeContext
);
1059 XDestroyWindow(dpy
, scr
->virtual_edges
[i
]);
1062 wfree(scr
->virtual_edges
);
1063 scr
->virtual_edges
= NULL
;
1064 scr
->virtual_nr_edges
= 0;
1072 wWorkspaceRaiseEdge(WScreen
*scr
)
1074 static int toggle
= 0;
1077 if (wPreferences
.vedge_thickness
&& initVDesk
) {
1079 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
1080 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1083 for (i
=scr
->virtual_nr_edges
-1; i
>=0; --i
) {
1084 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1093 wWorkspaceLowerEdge(WScreen
*scr
)
1096 if (wPreferences
.vedge_thickness
&& initVDesk
) {
1097 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
1098 XLowerWindow(dpy
, scr
->virtual_edges
[i
]);
1105 wWorkspaceResizeViewPort(WScreen
*scr
, int workspace
)
1108 wWorkspaceGetViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
1109 wWorkspaceSetViewPort(scr
, scr
->current_workspace
, x
, y
);
1114 updateWorkspaceGeometry(WScreen
*scr
, int workspace
, int *view_x
, int *view_y
)
1116 int most_left
, most_right
, most_top
, most_bottom
;
1119 int heads
= wXineramaHeads(scr
);
1120 typedef int strut_t
[4];
1121 strut_t
* strut
= (strut_t
*)wmalloc(heads
*sizeof(strut_t
));
1124 for (head
=0; head
<heads
; ++head
) {
1125 WMRect rect
= wGetRectForHead(scr
, head
);
1126 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
1127 strut
[head
][0] = area
.x1
- rect
.pos
.x
;
1128 strut
[head
][1] = rect
.pos
.x
+ rect
.size
.width
- area
.x2
;
1129 strut
[head
][2] = area
.y1
- rect
.pos
.y
;
1130 strut
[head
][3] = rect
.pos
.y
+ rect
.size
.height
- area
.y2
;
1133 /* adjust workspace layout */
1134 wwin
= scr
->focused_window
;
1137 most_left
= scr
->scr_width
;
1138 most_top
= scr
->scr_height
;
1139 for(;wwin
; wwin
= wwin
->prev
) {
1140 if (wwin
->frame
->workspace
== workspace
) {
1141 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) &&
1142 !wwin
->flags
.hidden
) {
1144 head
= wGetHeadForWindow(wwin
);
1146 i
= wwin
->frame_x
- strut
[head
][0];
1147 if (i
< most_left
) /* record positions, should this be cached? */
1149 i
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ strut
[head
][1];
1150 if (HAS_BORDER(wwin
))
1154 i
= wwin
->frame_y
- strut
[head
][2];
1157 i
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ strut
[head
][3];
1158 if (HAS_BORDER(wwin
))
1160 if (i
> most_bottom
) {
1167 if (most_left
> 0) most_left
= 0;
1168 if (most_top
> 0) most_top
= 0;
1170 scr
->workspaces
[workspace
]->width
= WMAX(most_right
, scr
->scr_width
) - WMIN(most_left
, 0);
1171 scr
->workspaces
[workspace
]->height
= WMAX(most_bottom
, scr
->scr_height
) - WMIN(most_top
, 0);
1173 *view_x
+= -most_left
- scr
->workspaces
[workspace
]->view_x
;
1174 scr
->workspaces
[workspace
]->view_x
= -most_left
;
1176 *view_y
+= -most_top
- scr
->workspaces
[workspace
]->view_y
;
1177 scr
->workspaces
[workspace
]->view_y
= -most_top
;
1183 typedef struct _delay_configure
{
1190 sendConfigureNotify (_delay_configure
*delay
)
1194 delay
->delay_count
--;
1195 if (!delay
->delay_count
) {
1196 for (wwin
= delay
->wwin
; wwin
; wwin
= wwin
->prev
) {
1197 wWindowSynthConfigureNotify(wwin
);
1204 wWorkspaceSetViewPort(WScreen
*scr
, int workspace
, int view_x
, int view_y
)
1206 Bool adjust_flag
= False
;
1208 static _delay_configure delay_configure
= {NULL
, 0};
1212 wptr
= scr
->workspaces
[workspace
];
1214 /*printf("wWorkspaceSetViewPort %d %d\n", view_x, view_y);*/
1216 updateWorkspaceGeometry(scr
, workspace
, &view_x
, &view_y
);
1218 if (view_x
+ scr
->scr_width
> wptr
->width
) {
1219 /* puts("right edge of vdesk"); */
1220 view_x
= wptr
->width
- scr
->scr_width
;
1223 /* puts("left edge of vdesk"); */
1226 if (view_y
+ scr
->scr_height
> wptr
->height
) {
1227 /* puts("right edge of vdesk"); */
1228 view_y
= wptr
->height
- scr
->scr_height
;
1231 /* puts("left edge of vdesk"); */
1235 diff_x
= wptr
->view_x
- view_x
;
1236 diff_y
= wptr
->view_y
- view_y
;
1237 if (!diff_x
&& !diff_y
)
1240 wptr
->view_x
= view_x
;
1241 wptr
->view_y
= view_y
;
1244 wNETWMUpdateDesktop(scr
);
1247 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
1248 if (wwin
->frame
->workspace
== workspace
&& !IS_VSTUCK(wwin
)) {
1249 wWindowMove(wwin
, wwin
->frame_x
+ diff_x
, wwin
->frame_y
+ diff_y
);
1253 if (1) { /* if delay*/
1254 delay_configure
.delay_count
++;
1255 delay_configure
.wwin
= scr
->focused_window
;
1256 WMAddTimerHandler(200, (WMCallback
*)sendConfigureNotify
, &delay_configure
);
1264 wWorkspaceGetViewPosition(WScreen
*scr
, int workspace
, int *x
, int *y
)
1266 *x
= scr
->workspaces
[workspace
]->view_x
;
1267 *y
= scr
->workspaces
[workspace
]->view_y
;
1273 switchWSCommand(WMenu
*menu
, WMenuEntry
*entry
)
1275 wWorkspaceChange(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
1280 deleteWSCommand(WMenu
*menu
, WMenuEntry
*entry
)
1282 wWorkspaceDelete(menu
->frame
->screen_ptr
,
1283 menu
->frame
->screen_ptr
->workspace_count
-1);
1288 newWSCommand(WMenu
*menu
, WMenuEntry
*foo
)
1292 ws
= wWorkspaceNew(menu
->frame
->screen_ptr
);
1293 /* autochange workspace*/
1295 wWorkspaceChange(menu
->frame
->screen_ptr
, ws
);
1301 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1302 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1304 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1311 cropline(char *line
)
1315 if (strlen(line
)==0)
1319 end
= &(line
[strlen(line
)])-1;
1320 while (isspace(*line
) && *line
!=0) line
++;
1321 while (isspace(*end
) && end
!=line
) {
1330 wWorkspaceRename(WScreen
*scr
, int workspace
, char *name
)
1332 char buf
[MAX_WORKSPACENAME_WIDTH
+1];
1335 if (workspace
>= scr
->workspace_count
)
1338 /* trim white spaces */
1339 tmp
= cropline(name
);
1341 if (strlen(tmp
)==0) {
1342 sprintf(buf
, _("Workspace %i"), workspace
+1);
1344 strncpy(buf
, tmp
, MAX_WORKSPACENAME_WIDTH
);
1346 buf
[MAX_WORKSPACENAME_WIDTH
] = 0;
1348 /* update workspace */
1349 wfree(scr
->workspaces
[workspace
]->name
);
1350 scr
->workspaces
[workspace
]->name
= wstrdup(buf
);
1352 if (scr
->clip_ws_menu
) {
1353 if (strcmp(scr
->clip_ws_menu
->entries
[workspace
+2]->text
, buf
)!=0) {
1354 wfree(scr
->clip_ws_menu
->entries
[workspace
+2]->text
);
1355 scr
->clip_ws_menu
->entries
[workspace
+2]->text
= wstrdup(buf
);
1356 wMenuRealize(scr
->clip_ws_menu
);
1359 if (scr
->workspace_menu
) {
1360 if (strcmp(scr
->workspace_menu
->entries
[workspace
+2]->text
, buf
)!=0) {
1361 wfree(scr
->workspace_menu
->entries
[workspace
+2]->text
);
1362 scr
->workspace_menu
->entries
[workspace
+2]->text
= wstrdup(buf
);
1363 wMenuRealize(scr
->workspace_menu
);
1368 wClipIconPaint(scr
->clip_icon
);
1370 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void*)workspace
);
1376 /* callback for when menu entry is edited */
1378 onMenuEntryEdited(WMenu
*menu
, WMenuEntry
*entry
)
1383 wWorkspaceRename(menu
->frame
->screen_ptr
, (long)entry
->clientdata
, tmp
);
1388 wWorkspaceMenuMake(WScreen
*scr
, Bool titled
)
1392 wsmenu
= wMenuCreate(scr
, titled
? _("Workspaces") : NULL
, False
);
1394 wwarning(_("could not create Workspace menu"));
1398 /* callback to be called when an entry is edited */
1399 wsmenu
->on_edit
= onMenuEntryEdited
;
1401 wMenuAddCallback(wsmenu
, _("New"), newWSCommand
, NULL
);
1402 wMenuAddCallback(wsmenu
, _("Destroy Last"), deleteWSCommand
, NULL
);
1410 wWorkspaceMenuUpdate(WScreen
*scr
, WMenu
*menu
)
1414 char title
[MAX_WORKSPACENAME_WIDTH
+1];
1421 if (menu
->entry_no
< scr
->workspace_count
+2) {
1422 /* new workspace(s) added */
1423 i
= scr
->workspace_count
-(menu
->entry_no
-2);
1424 ws
= menu
->entry_no
- 2;
1426 strcpy(title
, scr
->workspaces
[ws
]->name
);
1428 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, (void*)ws
);
1429 entry
->flags
.indicator
= 1;
1430 entry
->flags
.editable
= 1;
1435 } else if (menu
->entry_no
> scr
->workspace_count
+2) {
1436 /* removed workspace(s) */
1437 for (i
= menu
->entry_no
-1; i
>= scr
->workspace_count
+2; i
--) {
1438 wMenuRemoveItem(menu
, i
);
1443 for (i
=0; i
<scr
->workspace_count
; i
++) {
1444 menu
->entries
[i
+2]->flags
.indicator_on
= 0;
1446 menu
->entries
[scr
->current_workspace
+2]->flags
.indicator_on
= 1;
1448 /* don't let user destroy current workspace */
1449 if (scr
->current_workspace
== scr
->workspace_count
-1) {
1450 wMenuSetEnabled(menu
, 1, False
);
1452 wMenuSetEnabled(menu
, 1, True
);
1455 tmp
= menu
->frame
->top_width
+ 5;
1456 /* if menu got unreachable, bring it to a visible place */
1457 if (menu
->frame_x
< tmp
- (int)menu
->frame
->core
->width
)
1458 wMenuMove(menu
, tmp
- (int)menu
->frame
->core
->width
, menu
->frame_y
, False
);
1465 wWorkspaceSaveState(WScreen
*scr
, WMPropList
*old_state
)
1467 WMPropList
*parr
, *pstr
, *wks_state
, *old_wks_state
, *foo
, *bar
;
1472 old_wks_state
= WMGetFromPLDictionary(old_state
, dWorkspaces
);
1473 parr
= WMCreatePLArray(NULL
);
1474 for (i
=0; i
< scr
->workspace_count
; i
++) {
1475 pstr
= WMCreatePLString(scr
->workspaces
[i
]->name
);
1476 wks_state
= WMCreatePLDictionary(dName
, pstr
, NULL
);
1477 WMReleasePropList(pstr
);
1478 if (!wPreferences
.flags
.noclip
) {
1479 pstr
= wClipSaveWorkspaceState(scr
, i
);
1480 WMPutInPLDictionary(wks_state
, dClip
, pstr
);
1481 WMReleasePropList(pstr
);
1482 } else if (old_wks_state
!=NULL
) {
1483 if ((foo
= WMGetFromPLArray(old_wks_state
, i
))!=NULL
) {
1484 if ((bar
= WMGetFromPLDictionary(foo
, dClip
))!=NULL
) {
1485 WMPutInPLDictionary(wks_state
, dClip
, bar
);
1489 WMAddToPLArray(parr
, wks_state
);
1490 WMReleasePropList(wks_state
);
1492 WMPutInPLDictionary(scr
->session_state
, dWorkspaces
, parr
);
1493 WMReleasePropList(parr
);
1498 wWorkspaceRestoreState(WScreen
*scr
)
1500 WMPropList
*parr
, *pstr
, *wks_state
, *clip_state
;
1505 if (scr
->session_state
== NULL
)
1508 parr
= WMGetFromPLDictionary(scr
->session_state
, dWorkspaces
);
1513 wscount
= scr
->workspace_count
;
1514 for (i
=0; i
< WMIN(WMGetPropListItemCount(parr
), MAX_WORKSPACES
); i
++) {
1515 wks_state
= WMGetFromPLArray(parr
, i
);
1516 if (WMIsPLDictionary(wks_state
))
1517 pstr
= WMGetFromPLDictionary(wks_state
, dName
);
1520 if (i
>= scr
->workspace_count
)
1522 if (scr
->workspace_menu
) {
1523 wfree(scr
->workspace_menu
->entries
[i
+2]->text
);
1524 scr
->workspace_menu
->entries
[i
+2]->text
= wstrdup(WMGetFromPLString(pstr
));
1525 scr
->workspace_menu
->flags
.realized
= 0;
1527 wfree(scr
->workspaces
[i
]->name
);
1528 scr
->workspaces
[i
]->name
= wstrdup(WMGetFromPLString(pstr
));
1529 if (!wPreferences
.flags
.noclip
) {
1530 clip_state
= WMGetFromPLDictionary(wks_state
, dClip
);
1531 if (scr
->workspaces
[i
]->clip
)
1532 wDockDestroy(scr
->workspaces
[i
]->clip
);
1533 scr
->workspaces
[i
]->clip
= wDockRestoreState(scr
, clip_state
,
1536 wDockHideIcons(scr
->workspaces
[i
]->clip
);
1538 /* We set the global icons here, because scr->workspaces[i]->clip
1539 * was not valid in wDockRestoreState().
1540 * There we only set icon->omnipresent to know which icons we
1543 for (j
=0; j
<scr
->workspaces
[i
]->clip
->max_icons
; j
++) {
1544 WAppIcon
*aicon
= scr
->workspaces
[i
]->clip
->icon_array
[j
];
1546 if (aicon
&& aicon
->omnipresent
) {
1547 aicon
->omnipresent
= 0;
1548 wClipMakeIconOmnipresent(aicon
, True
);
1549 XMapWindow(dpy
, aicon
->icon
->core
->window
);
1554 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void*)i
);