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
;
73 if (dWorkspaces
!=NULL
)
76 dWorkspaces
= WMCreatePLString("Workspaces");
77 dName
= WMCreatePLString("Name");
78 dClip
= WMCreatePLString("Clip");
83 wWorkspaceMake(WScreen
*scr
, int count
)
93 wWorkspaceNew(WScreen
*scr
)
95 WWorkspace
*wspace
, **list
;
98 if (scr
->workspace_count
< MAX_WORKSPACES
) {
99 scr
->workspace_count
++;
101 wspace
= wmalloc(sizeof(WWorkspace
));
105 wspace
->name
= wmalloc(strlen(_("Workspace %i"))+8);
106 sprintf(wspace
->name
, _("Workspace %i"), scr
->workspace_count
);
110 if (!wPreferences
.flags
.noclip
) {
111 wspace
->clip
= wDockCreate(scr
, WM_CLIP
);
115 list
= wmalloc(sizeof(WWorkspace
*)*scr
->workspace_count
);
117 for (i
=0; i
<scr
->workspace_count
-1; i
++) {
118 list
[i
] = scr
->workspaces
[i
];
122 wfree(scr
->workspaces
);
123 scr
->workspaces
= list
;
125 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
126 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
127 #ifdef VIRTUAL_DESKTOP
128 wspace
->view_x
= wspace
->view_y
= 0;
129 wspace
->height
= scr
->scr_height
;
130 wspace
->width
= scr
->scr_width
;
133 wNETWMUpdateDesktop(scr
);
136 WMPostNotificationName(WMNWorkspaceCreated
, scr
,
137 (void*)(uintptr_t)(scr
->workspace_count
-1));
140 return scr
->workspace_count
-1;
148 wWorkspaceDelete(WScreen
*scr
, int workspace
)
157 /* verify if workspace is in use by some window */
158 tmp
= scr
->focused_window
;
160 if (!IS_OMNIPRESENT(tmp
) && tmp
->frame
->workspace
==workspace
)
165 if (!wPreferences
.flags
.noclip
) {
166 wDockDestroy(scr
->workspaces
[workspace
]->clip
);
167 scr
->workspaces
[workspace
]->clip
= NULL
;
170 list
= wmalloc(sizeof(WWorkspace
*)*(scr
->workspace_count
-1));
172 for (i
=0; i
<scr
->workspace_count
; i
++) {
174 list
[j
++] = scr
->workspaces
[i
];
176 if (scr
->workspaces
[i
]->name
)
177 wfree(scr
->workspaces
[i
]->name
);
178 wfree(scr
->workspaces
[i
]);
181 wfree(scr
->workspaces
);
182 scr
->workspaces
= list
;
184 scr
->workspace_count
--;
188 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
189 /* clip workspace menu */
190 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
192 /* update also window menu */
193 if (scr
->workspace_submenu
) {
194 WMenu
*menu
= scr
->workspace_submenu
;
197 while (i
>scr
->workspace_count
)
198 wMenuRemoveItem(menu
, --i
);
202 if (scr
->clip_submenu
) {
203 WMenu
*menu
= scr
->clip_submenu
;
206 while (i
>scr
->workspace_count
)
207 wMenuRemoveItem(menu
, --i
);
212 wNETWMUpdateDesktop(scr
);
215 WMPostNotificationName(WMNWorkspaceDestroyed
, scr
,
216 (void*)(uintptr_t)(scr
->workspace_count
-1));
218 if (scr
->current_workspace
>= scr
->workspace_count
)
219 wWorkspaceChange(scr
, scr
->workspace_count
-1);
225 typedef struct WorkspaceNameData
{
234 hideWorkspaceName(void *data
)
236 WScreen
*scr
= (WScreen
*)data
;
238 if (!scr
->workspace_name_data
|| scr
->workspace_name_data
->count
== 0
239 || time(NULL
) > scr
->workspace_name_data
->timeout
) {
240 XUnmapWindow(dpy
, scr
->workspace_name
);
242 if (scr
->workspace_name_data
) {
243 RReleaseImage(scr
->workspace_name_data
->back
);
244 RReleaseImage(scr
->workspace_name_data
->text
);
245 wfree(scr
->workspace_name_data
);
247 scr
->workspace_name_data
= NULL
;
249 scr
->workspace_name_timer
= NULL
;
251 RImage
*img
= RCloneImage(scr
->workspace_name_data
->back
);
254 scr
->workspace_name_timer
=
255 WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY
, hideWorkspaceName
, scr
);
257 RCombineImagesWithOpaqueness(img
, scr
->workspace_name_data
->text
,
258 scr
->workspace_name_data
->count
*255/10);
260 RConvertImage(scr
->rcontext
, img
, &pix
);
264 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, pix
);
265 XClearWindow(dpy
, scr
->workspace_name
);
266 XFreePixmap(dpy
, pix
);
269 scr
->workspace_name_data
->count
--;
275 showWorkspaceName(WScreen
*scr
, int workspace
)
277 WorkspaceNameData
*data
;
282 char *name
= scr
->workspaces
[workspace
]->name
;
283 int len
= strlen(name
);
286 if (wPreferences
.workspace_name_display_position
== WD_NONE
||
287 scr
->workspace_count
< 2) {
291 if (scr
->workspace_name_timer
) {
292 WMDeleteTimerHandler(scr
->workspace_name_timer
);
293 XUnmapWindow(dpy
, scr
->workspace_name
);
296 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
,
297 hideWorkspaceName
, scr
);
299 if (scr
->workspace_name_data
) {
300 RReleaseImage(scr
->workspace_name_data
->back
);
301 RReleaseImage(scr
->workspace_name_data
->text
);
302 wfree(scr
->workspace_name_data
);
305 data
= wmalloc(sizeof(WorkspaceNameData
));
308 w
= WMWidthOfString(scr
->workspace_name_font
, name
, len
);
309 h
= WMFontHeight(scr
->workspace_name_font
);
311 switch (wPreferences
.workspace_name_display_position
) {
313 px
= (scr
->scr_width
- (w
+4))/2;
317 px
= (scr
->scr_width
- (w
+4))/2;
318 py
= scr
->scr_height
- (h
+4);
325 px
= scr
->scr_width
- (w
+4);
330 py
= scr
->scr_height
- (h
+4);
333 px
= scr
->scr_width
- (w
+4);
334 py
= scr
->scr_height
- (h
+4);
338 px
= (scr
->scr_width
- (w
+4))/2;
339 py
= (scr
->scr_height
- (h
+4))/2;
342 XResizeWindow(dpy
, scr
->workspace_name
, w
+4, h
+4);
343 XMoveWindow(dpy
, scr
->workspace_name
, px
, py
);
345 text
= XCreatePixmap(dpy
, scr
->w_win
, w
+4, h
+4, scr
->w_depth
);
346 mask
= XCreatePixmap(dpy
, scr
->w_win
, w
+4, h
+4, 1);
348 /*XSetForeground(dpy, scr->mono_gc, 0);
349 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4);*/
351 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+4, h
+4);
353 for (x
= 0; x
<= 4; x
++) {
354 for (y
= 0; y
<= 4; y
++) {
355 WMDrawString(scr
->wmscreen
, text
, scr
->white
,
356 scr
->workspace_name_font
, x
, y
, name
, len
);
360 XSetForeground(dpy
, scr
->mono_gc
, 1);
361 XSetBackground(dpy
, scr
->mono_gc
, 0);
363 XCopyPlane(dpy
, text
, mask
, scr
->mono_gc
, 0, 0, w
+4, h
+4, 0, 0, 1<<(scr
->w_depth
-1));
365 /*XSetForeground(dpy, scr->mono_gc, 1);*/
366 XSetBackground(dpy
, scr
->mono_gc
, 1);
368 XFillRectangle(dpy
, text
, WMColorGC(scr
->black
), 0, 0, w
+4, h
+4);
370 WMDrawString(scr
->wmscreen
, text
, scr
->white
, scr
->workspace_name_font
,
374 XShapeCombineMask(dpy
, scr
->workspace_name
, ShapeBounding
, 0, 0, mask
,
377 XSetWindowBackgroundPixmap(dpy
, scr
->workspace_name
, text
);
378 XClearWindow(dpy
, scr
->workspace_name
);
380 data
->text
= RCreateImageFromDrawable(scr
->rcontext
, text
, None
);
382 XFreePixmap(dpy
, text
);
383 XFreePixmap(dpy
, mask
);
386 XMapRaised(dpy
, scr
->workspace_name
);
392 ximg
= RGetXImage(scr
->rcontext
, scr
->root_win
, px
, py
,
393 data
->text
->width
, data
->text
->height
);
395 if (!ximg
|| !ximg
->image
) {
399 XMapRaised(dpy
, scr
->workspace_name
);
402 data
->back
= RCreateImageFromXImage(scr
->rcontext
, ximg
->image
, NULL
);
403 RDestroyXImage(scr
->rcontext
, ximg
);
411 /* set a timeout for the effect */
412 data
->timeout
= time(NULL
) + 2 +
413 (WORKSPACE_NAME_DELAY
+ WORKSPACE_NAME_FADE_DELAY
*data
->count
)/1000;
415 scr
->workspace_name_data
= data
;
421 if (scr
->workspace_name_timer
)
422 WMDeleteTimerHandler(scr
->workspace_name_timer
);
425 RReleaseImage(data
->text
);
427 RReleaseImage(data
->back
);
430 scr
->workspace_name_data
= NULL
;
432 scr
->workspace_name_timer
= WMAddTimerHandler(WORKSPACE_NAME_DELAY
+
433 10*WORKSPACE_NAME_FADE_DELAY
,
434 hideWorkspaceName
, scr
);
439 wWorkspaceChange(WScreen
*scr
, int workspace
)
441 if (scr
->flags
.startup
|| scr
->flags
.startup2
) {
445 if (workspace
!= scr
->current_workspace
) {
446 wWorkspaceForceChange(scr
, workspace
);
448 showWorkspaceName(scr, workspace);
454 wWorkspaceRelativeChange(WScreen
*scr
, int amount
)
458 w
= scr
->current_workspace
+ amount
;
462 wWorkspaceChange(scr
, w
);
463 } else if (wPreferences
.ws_cycle
) {
464 wWorkspaceChange(scr
, scr
->workspace_count
+ w
);
466 } else if (amount
> 0) {
467 if (w
< scr
->workspace_count
) {
468 wWorkspaceChange(scr
, w
);
469 } else if (wPreferences
.ws_advance
) {
470 wWorkspaceChange(scr
, WMIN(w
, MAX_WORKSPACES
-1));
471 } else if (wPreferences
.ws_cycle
) {
472 wWorkspaceChange(scr
, w
% scr
->workspace_count
);
479 wWorkspaceForceChange(WScreen
*scr
, int workspace
)
481 WWindow
*tmp
, *foc
=NULL
, *foc2
=NULL
;
483 if (workspace
>= MAX_WORKSPACES
|| workspace
< 0)
486 SendHelperMessage(scr
, 'C', workspace
+1, NULL
);
488 if (workspace
> scr
->workspace_count
-1) {
489 wWorkspaceMake(scr
, workspace
- scr
->workspace_count
+ 1);
492 wClipUpdateForWorkspaceChange(scr
, workspace
);
494 scr
->current_workspace
= workspace
;
496 wWorkspaceMenuUpdate(scr
, scr
->workspace_menu
);
498 wWorkspaceMenuUpdate(scr
, scr
->clip_ws_menu
);
500 if ((tmp
= scr
->focused_window
)!= NULL
) {
501 if ((IS_OMNIPRESENT(tmp
) && (tmp
->flags
.mapped
|| tmp
->flags
.shaded
) &&
502 !WFLAGP(tmp
, no_focusable
)) || tmp
->flags
.changing_workspace
) {
506 /* foc2 = tmp; will fix annoyance with gnome panel
507 * but will create annoyance for every other application
510 if (tmp
->frame
->workspace
!=workspace
&& !tmp
->flags
.selected
) {
511 /* unmap windows not on this workspace */
512 if ((tmp
->flags
.mapped
||tmp
->flags
.shaded
) &&
513 !IS_OMNIPRESENT(tmp
) && !tmp
->flags
.changing_workspace
) {
516 /* also unmap miniwindows not on this workspace */
517 if (!wPreferences
.sticky_icons
&& tmp
->flags
.miniaturized
&&
518 tmp
->icon
&& !IS_OMNIPRESENT(tmp
)) {
519 XUnmapWindow(dpy
, tmp
->icon
->core
->window
);
520 tmp
->icon
->mapped
= 0;
522 /* update current workspace of omnipresent windows */
523 if (IS_OMNIPRESENT(tmp
)) {
524 WApplication
*wapp
= wApplicationOf(tmp
->main_window
);
526 tmp
->frame
->workspace
= workspace
;
529 wapp
->last_workspace
= workspace
;
531 if (!foc2
&& (tmp
->flags
.mapped
|| tmp
->flags
.shaded
)) {
536 /* change selected windows' workspace */
537 if (tmp
->flags
.selected
) {
538 wWindowChangeWorkspace(tmp
, workspace
);
539 if (!tmp
->flags
.miniaturized
&& !foc
) {
543 if (!tmp
->flags
.hidden
) {
544 if (!(tmp
->flags
.mapped
|| tmp
->flags
.miniaturized
)) {
545 /* remap windows that are on this workspace */
547 if (!foc
&& !WFLAGP(tmp
, no_focusable
)) {
551 /* Also map miniwindow if not omnipresent */
552 if (!wPreferences
.sticky_icons
&&
553 tmp
->flags
.miniaturized
&&
554 !IS_OMNIPRESENT(tmp
) && tmp
->icon
) {
555 tmp
->icon
->mapped
= 1;
556 XMapWindow(dpy
, tmp
->icon
->core
->window
);
564 /* Gobble up events unleashed by our mapping & unmapping.
565 * These may trigger various grab-initiated focus &
566 * crossing events. However, we don't care about them,
567 * and ignore their focus implications altogether to avoid
570 scr
->flags
.ignore_focus_events
= 1;
571 ProcessPendingEvents();
572 scr
->flags
.ignore_focus_events
= 0;
577 if (scr
->focused_window
->flags
.mapped
&& !foc
) {
578 foc
= scr
->focused_window
;
580 if (wPreferences
.focus_mode
== WKF_CLICK
) {
581 wSetFocusTo(scr
, foc
);
589 if (XQueryPointer(dpy
, scr
->root_win
, &bar
, &win
,
590 &foo
, &foo
, &foo
, &foo
, &mask
)) {
591 tmp
= wWindowFor(win
);
594 /* If there's a window under the pointer, focus it.
595 * (we ate all other focus events above, so it's
596 * certainly not focused). Otherwise focus last
597 * focused, or the root (depending on sloppiness)
599 if (!tmp
&& wPreferences
.focus_mode
== WKF_SLOPPY
) {
600 wSetFocusTo(scr
, foc
);
602 wSetFocusTo(scr
, tmp
);
607 /* We need to always arrange icons when changing workspace, even if
608 * no autoarrange icons, because else the icons in different workspaces
610 * This can be avoided if appicons are also workspace specific.
612 if (!wPreferences
.sticky_icons
)
613 wArrangeIcons(scr
, False
);
616 wAppIconPaint(scr
->dock
->icon_array
[0]);
618 if (scr
->clip_icon
) {
619 if (scr
->workspaces
[workspace
]->clip
->auto_collapse
||
620 scr
->workspaces
[workspace
]->clip
->auto_raise_lower
) {
621 /* to handle enter notify. This will also */
622 XUnmapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
623 XMapWindow(dpy
, scr
->clip_icon
->icon
->core
->window
);
625 wClipIconPaint(scr
->clip_icon
);
630 wScreenUpdateUsableArea(scr
);
631 wNETWMUpdateDesktop(scr
);
634 showWorkspaceName(scr
, workspace
);
636 WMPostNotificationName(WMNWorkspaceChanged
, scr
, (void*)(uintptr_t)workspace
);
638 /* XSync(dpy, False); */
642 #ifdef VIRTUAL_DESKTOP
646 * 1) Allow border around each window so the scrolling
647 * won't just stop at the border.
652 #define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
653 #define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
654 #define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
655 #define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
656 #define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
657 #define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
660 #define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
664 #define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
667 #define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
669 static Bool
cmp_gez(int i
) { return (i
>= 0); }
671 static Bool
cmp_gz(int i
) { return (i
> 0); }
675 getClosestEdge(WScreen
* scr
, WMPoint direction
, Bool (*cmp
)(int))
677 WMPoint closest
= wmkpoint(0, 0);
678 int closest_len
= INT_MAX
;
681 for (wwin
=scr
->focused_window
; wwin
; wwin
=wwin
->prev
) {
682 if (wwin
->frame
->workspace
== scr
->current_workspace
) {
683 if (!wwin
->flags
.miniaturized
&&
685 !wwin
->flags
.hidden
) {
686 int border
= 2*HAS_BORDER(wwin
);
689 int head
= wGetHeadForWindow(wwin
);
690 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
693 x1
= wwin
->frame_x
- area
.x1
;
694 y1
= wwin
->frame_y
- area
.y1
;
695 x2
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ border
- area
.x2
;
696 y2
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ border
- area
.y2
;
699 len
= vec_dot(direction
, p
);
700 updateMinimum(len
, p
, closest_len
, closest
);
703 len
= vec_dot(direction
, p
);
704 updateMinimum(len
, p
, closest_len
, closest
);
707 len
= vec_dot(direction
, p
);
708 updateMinimum(len
, p
, closest_len
, closest
);
711 len
= vec_dot(direction
, p
);
712 updateMinimum(len
, p
, closest_len
, closest
);
722 getViewPosition(WScreen
*scr
, int workspace
, int *x
, int *y
)
724 *x
= scr
->workspaces
[workspace
]->view_x
;
725 *y
= scr
->workspaces
[workspace
]->view_y
;
730 wWorkspaceKeyboardMoveDesktop(WScreen
*scr
, WMPoint direction
)
733 WMPoint edge
= getClosestEdge(scr
, direction
, cmp_gz
);
734 int len
= vec_dot(edge
, direction
);
735 WMPoint step
= vec_scale(direction
, len
);
736 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
737 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
+step
.x
, y
+step
.y
);
741 extern Cursor wCursor
[WCUR_LAST
];
745 vdMouseMoveDesktop(XEvent
*event
, WMPoint direction
)
747 static int lock
= False
;
753 WScreen
*scr
= wScreenForRootWindow(event
->xcrossing
.root
);
754 WMPoint old_pos
= wmkpoint(event
->xcrossing
.x_root
, event
->xcrossing
.y_root
);
759 if (XGrabPointer(dpy
, event
->xcrossing
.window
, False
,
760 PointerMotionMask
, GrabModeAsync
, GrabModeAsync
,
761 scr
->root_win
, wCursor
[WCUR_EMPTY
],
762 CurrentTime
) != GrabSuccess
) {
764 /* if the grab fails, do it the old fashioned way */
765 step
= vec_scale2(direction
, wPreferences
.vedge_hscrollspeed
,
766 wPreferences
.vedge_vscrollspeed
);
767 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
768 if (wWorkspaceSetViewport(scr
, scr
->current_workspace
,
769 x
+step
.x
, y
+step
.y
)) {
770 step
= vec_scale(direction
, 2);
771 XWarpPointer(dpy
, None
, scr
->root_win
, 0,0,0,0,
772 event
->xcrossing
.x_root
- step
.x
,
773 event
->xcrossing
.y_root
- step
.y
);
783 if (old_pos
.x
> scr
->scr_width
)
784 old_pos
.x
= scr
->scr_width
;
785 if (old_pos
.y
> scr
->scr_height
)
786 old_pos
.y
= scr
->scr_height
;
791 XWarpPointer(dpy
, None
, scr
->root_win
, 0, 0, 0, 0,
792 scr
->scr_width
/2, scr
->scr_height
/2);
795 WMMaskEvent(dpy
, PointerMotionMask
, &ev
);
801 step
= wmkpoint(ev
.xmotion
.x_root
-scr
->scr_width
/2,
802 ev
.xmotion
.y_root
-scr
->scr_height
/2);
803 step_len
= vec_dot(step
, direction
);
811 int resist
= wPreferences
.vedge_resistance
;
813 int closest_len
= INT_MAX
;
815 closest
= getClosestEdge(scr
, direction
, cmp_gez
);
816 closest_len
= vec_dot(direction
, closest
);
820 resisted
+= step_len
;
821 do_move
= resisted
>= resist
;
823 closest_len
= INT_MAX
;
824 step_len
= resisted
- resist
;
829 if (closest_len
<= wPreferences
.vedge_attraction
) {
830 step
= vec_scale(direction
, closest_len
);
832 step
= vec_scale(direction
, step_len
);
835 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
836 wWorkspaceSetViewport(scr
, scr
->current_workspace
,
846 step
= vec_add(old_pos
, vec_scale(direction
, -1));
847 XWarpPointer(dpy
, None
, scr
->root_win
, 0,0,0,0, step
.x
, step
.y
);
848 XUngrabPointer(dpy
, CurrentTime
);
856 vdHandleEnter_u(XEvent
*event
) {
857 vdMouseMoveDesktop(event
, VEC_UP
);
862 vdHandleEnter_d(XEvent
*event
) {
863 vdMouseMoveDesktop(event
, VEC_DOWN
);
868 vdHandleEnter_l(XEvent
*event
) {
869 vdMouseMoveDesktop(event
, VEC_LEFT
);
874 vdHandleEnter_r(XEvent
*event
) {
875 vdMouseMoveDesktop(event
, VEC_RIGHT
);
879 #define LEFT_EDGE 0x01
880 #define RIGHT_EDGE 0x02
881 #define TOP_EDGE 0x04
882 #define BOTTOM_EDGE 0x08
883 #define ALL_EDGES 0x0F
886 createEdges(WScreen
*scr
)
888 if (!scr
->virtual_edges
) {
891 XSetWindowAttributes attribs
;
893 int heads
= wXineramaHeads(scr
);
894 int *hasEdges
= (int*)wmalloc(sizeof(int)*heads
);
898 int max_edges
= 4*heads
;
900 Window
*edges
= (Window
*)wmalloc(sizeof(Window
)*max_edges
);
902 for (i
=0; i
<heads
; ++i
)
903 hasEdges
[i
] = ALL_EDGES
;
904 for (i
=0; i
<heads
; ++i
) {
905 WMRect i_rect
= wGetRectForHead(scr
, i
);
906 for (j
=i
+1; j
<heads
; ++j
) {
907 WMRect j_rect
= wGetRectForHead(scr
, j
);
909 int vlen
= (WMIN(i_rect
.pos
.y
+ i_rect
.size
.height
,
910 j_rect
.pos
.y
+ j_rect
.size
.height
) -
911 WMAX(i_rect
.pos
.y
, j_rect
.pos
.y
));
913 int hlen
= (WMIN(i_rect
.pos
.x
+ i_rect
.size
.width
,
914 j_rect
.pos
.x
+ j_rect
.size
.width
) -
915 WMAX( i_rect
.pos
.x
, j_rect
.pos
.x
));
917 if (vlen
> 0 && hlen
== 0) { /* horz alignment, vert edges touch */
918 if (i_rect
.pos
.x
< j_rect
.pos
.x
) { /* i left of j */
919 hasEdges
[i
] &= ~RIGHT_EDGE
;
920 hasEdges
[j
] &= ~LEFT_EDGE
;
921 } else { /* j left of i */
922 hasEdges
[j
] &= ~RIGHT_EDGE
;
923 hasEdges
[i
] &= ~LEFT_EDGE
;
925 } else if (vlen
== 0 && hlen
> 0) { /* vert alignment, horz edges touch */
926 if (i_rect
.pos
.y
< j_rect
.pos
.y
) { /* i top of j */
927 hasEdges
[i
] &= ~BOTTOM_EDGE
;
928 hasEdges
[j
] &= ~TOP_EDGE
;
929 } else { /* j top of i */
930 hasEdges
[j
] &= ~BOTTOM_EDGE
;
931 hasEdges
[i
] &= ~TOP_EDGE
;
937 for (w
= 0; w
< scr
->workspace_count
; w
++) {
938 /* puts("reset workspace"); */
939 wWorkspaceSetViewport(scr
, w
, 0, 0);
942 vmask
= CWEventMask
|CWOverrideRedirect
;
943 attribs
.event_mask
= (EnterWindowMask
| LeaveWindowMask
| VisibilityChangeMask
);
944 attribs
.override_redirect
= True
;
946 for (head
=0; head
<wXineramaHeads(scr
); ++head
) {
947 WMRect rect
= wGetRectForHead(scr
, head
);
949 if (hasEdges
[head
] & TOP_EDGE
) {
951 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
952 rect
.size
.width
, thickness
, 0,
953 CopyFromParent
, InputOnly
, CopyFromParent
,
955 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
956 (XPointer
)vdHandleEnter_u
);
960 if (hasEdges
[head
] & BOTTOM_EDGE
) {
962 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
,
963 rect
.pos
.y
+rect
.size
.height
-thickness
,
964 rect
.size
.width
, thickness
, 0,
965 CopyFromParent
, InputOnly
, CopyFromParent
,
967 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
968 (XPointer
)vdHandleEnter_d
);
972 if (hasEdges
[head
] & LEFT_EDGE
) {
974 XCreateWindow(dpy
, scr
->root_win
, rect
.pos
.x
, rect
.pos
.y
,
975 thickness
, rect
.pos
.y
+rect
.size
.height
, 0,
976 CopyFromParent
, InputOnly
, CopyFromParent
,
978 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
979 (XPointer
)vdHandleEnter_l
);
983 if (hasEdges
[head
] & RIGHT_EDGE
) {
985 XCreateWindow(dpy
, scr
->root_win
,
986 rect
.pos
.x
+ rect
.size
.width
- thickness
, rect
.pos
.y
,
987 thickness
, rect
.size
.height
, 0,
988 CopyFromParent
, InputOnly
, CopyFromParent
, vmask
,
990 XSaveContext(dpy
, edges
[nr_edges
], wVEdgeContext
,
991 (XPointer
)vdHandleEnter_r
);
996 scr
->virtual_nr_edges
= nr_edges
;
997 scr
->virtual_edges
= edges
;
999 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
1000 XMapWindow(dpy
, scr
->virtual_edges
[i
]);
1002 wWorkspaceRaiseEdge(scr
);
1010 destroyEdges(WScreen
*scr
)
1012 if (scr
->virtual_edges
) {
1015 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
1016 XDeleteContext(dpy
, scr
->virtual_edges
[i
], wVEdgeContext
);
1017 XUnmapWindow(dpy
, scr
->virtual_edges
[i
]);
1018 XDestroyWindow(dpy
, scr
->virtual_edges
[i
]);
1021 wfree(scr
->virtual_edges
);
1022 scr
->virtual_edges
= NULL
;
1023 scr
->virtual_nr_edges
= 0;
1029 wWorkspaceUpdateEdge(WScreen
*scr
)
1031 if (wPreferences
.vdesk_enable
) {
1040 wWorkspaceRaiseEdge(WScreen
*scr
)
1042 static int toggle
= 0;
1045 if (!scr
->virtual_edges
)
1049 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
1050 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1053 for (i
=scr
->virtual_nr_edges
-1; i
>=0; --i
) {
1054 XRaiseWindow(dpy
, scr
->virtual_edges
[i
]);
1063 wWorkspaceLowerEdge(WScreen
*scr
)
1066 for (i
=0; i
<scr
->virtual_nr_edges
; ++i
) {
1067 XLowerWindow(dpy
, scr
->virtual_edges
[i
]);
1073 wWorkspaceResizeViewport(WScreen
*scr
, int workspace
)
1076 getViewPosition(scr
, scr
->current_workspace
, &x
, &y
);
1077 wWorkspaceSetViewport(scr
, scr
->current_workspace
, x
, y
);
1082 updateWorkspaceGeometry(WScreen
*scr
, int workspace
, int *view_x
, int *view_y
)
1084 int most_left
, most_right
, most_top
, most_bottom
;
1087 int heads
= wXineramaHeads(scr
);
1088 typedef int strut_t
[4];
1089 strut_t
* strut
= (strut_t
*)wmalloc(heads
*sizeof(strut_t
));
1092 for (head
=0; head
<heads
; ++head
) {
1093 WMRect rect
= wGetRectForHead(scr
, head
);
1094 WArea area
= wGetUsableAreaForHead(scr
, head
, NULL
, False
);
1095 strut
[head
][0] = area
.x1
- rect
.pos
.x
;
1096 strut
[head
][1] = rect
.pos
.x
+ rect
.size
.width
- area
.x2
;
1097 strut
[head
][2] = area
.y1
- rect
.pos
.y
;
1098 strut
[head
][3] = rect
.pos
.y
+ rect
.size
.height
- area
.y2
;
1101 /* adjust workspace layout */
1102 wwin
= scr
->focused_window
;
1105 most_left
= scr
->scr_width
;
1106 most_top
= scr
->scr_height
;
1107 for(;wwin
; wwin
= wwin
->prev
) {
1108 if (wwin
->frame
->workspace
== workspace
) {
1109 if (!wwin
->flags
.miniaturized
&& !IS_VSTUCK(wwin
) &&
1110 !wwin
->flags
.hidden
) {
1112 head
= wGetHeadForWindow(wwin
);
1114 i
= wwin
->frame_x
- strut
[head
][0];
1115 if (i
< most_left
) /* record positions, should this be cached? */
1117 i
= wwin
->frame_x
+ wwin
->frame
->core
->width
+ strut
[head
][1];
1118 if (HAS_BORDER(wwin
))
1122 i
= wwin
->frame_y
- strut
[head
][2];
1125 i
= wwin
->frame_y
+ wwin
->frame
->core
->height
+ strut
[head
][3];
1126 if (HAS_BORDER(wwin
))
1128 if (i
> most_bottom
) {
1135 if (most_left
> 0) most_left
= 0;
1136 if (most_top
> 0) most_top
= 0;
1138 scr
->workspaces
[workspace
]->width
= WMAX(most_right
, scr
->scr_width
) - WMIN(most_left
, 0);
1139 scr
->workspaces
[workspace
]->height
= WMAX(most_bottom
, scr
->scr_height
) - WMIN(most_top
, 0);
1141 *view_x
+= -most_left
- scr
->workspaces
[workspace
]->view_x
;
1142 scr
->workspaces
[workspace
]->view_x
= -most_left
;
1144 *view_y
+= -most_top
- scr
->workspaces
[workspace
]->view_y
;
1145 scr
->workspaces
[workspace
]->view_y
= -most_top
;
1151 typedef struct _delay_configure
{
1158 sendConfigureNotify (_delay_configure
*delay
)
1162 delay
->delay_count
--;
1163 if (!delay
->delay_count
) {
1164 for (wwin
= delay
->wwin
; wwin
; wwin
= wwin
->prev
) {
1165 wWindowSynthConfigureNotify(wwin
);
1172 wWorkspaceSetViewport(WScreen
*scr
, int workspace
, int view_x
, int view_y
)
1174 Bool adjust_flag
= False
;
1176 static _delay_configure delay_configure
= {NULL
, 0};
1180 wptr
= scr
->workspaces
[workspace
];
1182 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y);*/
1184 updateWorkspaceGeometry(scr
, workspace
, &view_x
, &view_y
);
1186 if (view_x
+ scr
->scr_width
> wptr
->width
) {
1187 /* puts("right edge of vdesk"); */
1188 view_x
= wptr
->width
- scr
->scr_width
;
1191 /* puts("left edge of vdesk"); */
1194 if (view_y
+ scr
->scr_height
> wptr
->height
) {
1195 /* puts("right edge of vdesk"); */
1196 view_y
= wptr
->height
- scr
->scr_height
;
1199 /* puts("left edge of vdesk"); */
1203 diff_x
= wptr
->view_x
- view_x
;
1204 diff_y
= wptr
->view_y
- view_y
;
1205 if (!diff_x
&& !diff_y
)
1208 wptr
->view_x
= view_x
;
1209 wptr
->view_y
= view_y
;
1212 wNETWMUpdateDesktop(scr
);
1215 for (wwin
= scr
->focused_window
; wwin
; wwin
= wwin
->prev
) {
1216 if (wwin
->frame
->workspace
== workspace
&& !IS_VSTUCK(wwin
)) {
1217 wWindowMove(wwin
, wwin
->frame_x
+ diff_x
, wwin
->frame_y
+ diff_y
);
1221 if (1) { /* if delay*/
1222 delay_configure
.delay_count
++;
1223 delay_configure
.wwin
= scr
->focused_window
;
1224 WMAddTimerHandler(200, (WMCallback
*)sendConfigureNotify
, &delay_configure
);
1235 switchWSCommand(WMenu
*menu
, WMenuEntry
*entry
)
1237 wWorkspaceChange(menu
->frame
->screen_ptr
, (long)entry
->clientdata
);
1242 deleteWSCommand(WMenu
*menu
, WMenuEntry
*entry
)
1244 wWorkspaceDelete(menu
->frame
->screen_ptr
,
1245 menu
->frame
->screen_ptr
->workspace_count
-1);
1250 newWSCommand(WMenu
*menu
, WMenuEntry
*foo
)
1254 ws
= wWorkspaceNew(menu
->frame
->screen_ptr
);
1255 /* autochange workspace*/
1257 wWorkspaceChange(menu
->frame
->screen_ptr
, ws
);
1263 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1264 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1266 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1273 cropline(char *line
)
1277 if (strlen(line
)==0)
1281 end
= &(line
[strlen(line
)])-1;
1282 while (isspace(*line
) && *line
!=0) line
++;
1283 while (isspace(*end
) && end
!=line
) {
1292 wWorkspaceRename(WScreen
*scr
, int workspace
, char *name
)
1294 char buf
[MAX_WORKSPACENAME_WIDTH
+1];
1297 if (workspace
>= scr
->workspace_count
)
1300 /* trim white spaces */
1301 tmp
= cropline(name
);
1303 if (strlen(tmp
)==0) {
1304 snprintf(buf
, sizeof(buf
), _("Workspace %i"), workspace
+1);
1306 strncpy(buf
, tmp
, MAX_WORKSPACENAME_WIDTH
);
1308 buf
[MAX_WORKSPACENAME_WIDTH
] = 0;
1310 /* update workspace */
1311 wfree(scr
->workspaces
[workspace
]->name
);
1312 scr
->workspaces
[workspace
]->name
= wstrdup(buf
);
1314 if (scr
->clip_ws_menu
) {
1315 if (strcmp(scr
->clip_ws_menu
->entries
[workspace
+2]->text
, buf
)!=0) {
1316 wfree(scr
->clip_ws_menu
->entries
[workspace
+2]->text
);
1317 scr
->clip_ws_menu
->entries
[workspace
+2]->text
= wstrdup(buf
);
1318 wMenuRealize(scr
->clip_ws_menu
);
1321 if (scr
->workspace_menu
) {
1322 if (strcmp(scr
->workspace_menu
->entries
[workspace
+2]->text
, buf
)!=0) {
1323 wfree(scr
->workspace_menu
->entries
[workspace
+2]->text
);
1324 scr
->workspace_menu
->entries
[workspace
+2]->text
= wstrdup(buf
);
1325 wMenuRealize(scr
->workspace_menu
);
1330 wClipIconPaint(scr
->clip_icon
);
1332 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void*)(uintptr_t)workspace
);
1338 /* callback for when menu entry is edited */
1340 onMenuEntryEdited(WMenu
*menu
, WMenuEntry
*entry
)
1345 wWorkspaceRename(menu
->frame
->screen_ptr
, (long)entry
->clientdata
, tmp
);
1350 wWorkspaceMenuMake(WScreen
*scr
, Bool titled
)
1354 wsmenu
= wMenuCreate(scr
, titled
? _("Workspaces") : NULL
, False
);
1356 wwarning(_("could not create Workspace menu"));
1360 /* callback to be called when an entry is edited */
1361 wsmenu
->on_edit
= onMenuEntryEdited
;
1363 wMenuAddCallback(wsmenu
, _("New"), newWSCommand
, NULL
);
1364 wMenuAddCallback(wsmenu
, _("Destroy Last"), deleteWSCommand
, NULL
);
1372 wWorkspaceMenuUpdate(WScreen
*scr
, WMenu
*menu
)
1376 char title
[MAX_WORKSPACENAME_WIDTH
+1];
1383 if (menu
->entry_no
< scr
->workspace_count
+2) {
1384 /* new workspace(s) added */
1385 i
= scr
->workspace_count
-(menu
->entry_no
-2);
1386 ws
= menu
->entry_no
- 2;
1388 strncpy(title
, scr
->workspaces
[ws
]->name
, MAX_WORKSPACENAME_WIDTH
);
1390 entry
= wMenuAddCallback(menu
, title
, switchWSCommand
, (void*)ws
);
1391 entry
->flags
.indicator
= 1;
1392 entry
->flags
.editable
= 1;
1397 } else if (menu
->entry_no
> scr
->workspace_count
+2) {
1398 /* removed workspace(s) */
1399 for (i
= menu
->entry_no
-1; i
>= scr
->workspace_count
+2; i
--) {
1400 wMenuRemoveItem(menu
, i
);
1405 for (i
=0; i
<scr
->workspace_count
; i
++) {
1406 menu
->entries
[i
+2]->flags
.indicator_on
= 0;
1408 menu
->entries
[scr
->current_workspace
+2]->flags
.indicator_on
= 1;
1410 /* don't let user destroy current workspace */
1411 if (scr
->current_workspace
== scr
->workspace_count
-1) {
1412 wMenuSetEnabled(menu
, 1, False
);
1414 wMenuSetEnabled(menu
, 1, True
);
1417 tmp
= menu
->frame
->top_width
+ 5;
1418 /* if menu got unreachable, bring it to a visible place */
1419 if (menu
->frame_x
< tmp
- (int)menu
->frame
->core
->width
)
1420 wMenuMove(menu
, tmp
- (int)menu
->frame
->core
->width
, menu
->frame_y
, False
);
1427 wWorkspaceSaveState(WScreen
*scr
, WMPropList
*old_state
)
1429 WMPropList
*parr
, *pstr
, *wks_state
, *old_wks_state
, *foo
, *bar
;
1434 old_wks_state
= WMGetFromPLDictionary(old_state
, dWorkspaces
);
1435 parr
= WMCreatePLArray(NULL
);
1436 for (i
=0; i
< scr
->workspace_count
; i
++) {
1437 pstr
= WMCreatePLString(scr
->workspaces
[i
]->name
);
1438 wks_state
= WMCreatePLDictionary(dName
, pstr
, NULL
);
1439 WMReleasePropList(pstr
);
1440 if (!wPreferences
.flags
.noclip
) {
1441 pstr
= wClipSaveWorkspaceState(scr
, i
);
1442 WMPutInPLDictionary(wks_state
, dClip
, pstr
);
1443 WMReleasePropList(pstr
);
1444 } else if (old_wks_state
!=NULL
) {
1445 if ((foo
= WMGetFromPLArray(old_wks_state
, i
))!=NULL
) {
1446 if ((bar
= WMGetFromPLDictionary(foo
, dClip
))!=NULL
) {
1447 WMPutInPLDictionary(wks_state
, dClip
, bar
);
1451 WMAddToPLArray(parr
, wks_state
);
1452 WMReleasePropList(wks_state
);
1454 WMPutInPLDictionary(scr
->session_state
, dWorkspaces
, parr
);
1455 WMReleasePropList(parr
);
1460 wWorkspaceRestoreState(WScreen
*scr
)
1462 WMPropList
*parr
, *pstr
, *wks_state
, *clip_state
;
1467 if (scr
->session_state
== NULL
)
1470 parr
= WMGetFromPLDictionary(scr
->session_state
, dWorkspaces
);
1475 wscount
= scr
->workspace_count
;
1476 for (i
=0; i
< WMIN(WMGetPropListItemCount(parr
), MAX_WORKSPACES
); i
++) {
1477 wks_state
= WMGetFromPLArray(parr
, i
);
1478 if (WMIsPLDictionary(wks_state
))
1479 pstr
= WMGetFromPLDictionary(wks_state
, dName
);
1482 if (i
>= scr
->workspace_count
)
1484 if (scr
->workspace_menu
) {
1485 wfree(scr
->workspace_menu
->entries
[i
+2]->text
);
1486 scr
->workspace_menu
->entries
[i
+2]->text
= wstrdup(WMGetFromPLString(pstr
));
1487 scr
->workspace_menu
->flags
.realized
= 0;
1489 wfree(scr
->workspaces
[i
]->name
);
1490 scr
->workspaces
[i
]->name
= wstrdup(WMGetFromPLString(pstr
));
1491 if (!wPreferences
.flags
.noclip
) {
1492 clip_state
= WMGetFromPLDictionary(wks_state
, dClip
);
1493 if (scr
->workspaces
[i
]->clip
)
1494 wDockDestroy(scr
->workspaces
[i
]->clip
);
1495 scr
->workspaces
[i
]->clip
= wDockRestoreState(scr
, clip_state
,
1498 wDockHideIcons(scr
->workspaces
[i
]->clip
);
1500 /* We set the global icons here, because scr->workspaces[i]->clip
1501 * was not valid in wDockRestoreState().
1502 * There we only set icon->omnipresent to know which icons we
1505 for (j
=0; j
<scr
->workspaces
[i
]->clip
->max_icons
; j
++) {
1506 WAppIcon
*aicon
= scr
->workspaces
[i
]->clip
->icon_array
[j
];
1508 if (aicon
&& aicon
->omnipresent
) {
1509 aicon
->omnipresent
= 0;
1510 wClipMakeIconOmnipresent(aicon
, True
);
1511 XMapWindow(dpy
, aicon
->icon
->core
->window
);
1516 WMPostNotificationName(WMNWorkspaceNameChanged
, scr
, (void*)(uintptr_t)i
);