Change to the linux kernel coding style
[wmaker-crm.git] / src / workspace.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/* workspace.c- Workspace management
1f22c070 id2000-11-25 21:06:31 +00002 *
9af1c6c4 dan1998-10-21 14:43:47 +00003 * Window Maker window manager
1f22c070 id2000-11-25 21:06:31 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1997-2003 Alfredo K. Kojima
1f22c070 id2000-11-25 21:06:31 +00006 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00007 * 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.
11 *
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.
16 *
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
9aca0d5f dan2004-10-12 01:34:32 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000020 * USA.
21 */
22#include "wconfig.h"
23
24#include <X11/Xlib.h>
25#include <X11/Xutil.h>
b147e8f8 kojima1999-04-17 18:24:23 +000026#ifdef SHAPE
27#include <X11/extensions/shape.h>
28#endif
29
9d2e6ef9 scottc1998-09-29 22:36:29 +000030#include <stdlib.h>
31#include <stdio.h>
06f59b99 Carlos R. Mafra2008-11-06 01:37:44 +010032#include <stdint.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000033#include <unistd.h>
34#include <ctype.h>
35#include <string.h>
daeb6d67 kojima1999-04-30 23:31:00 +000036#include <time.h>
9aca0d5f dan2004-10-12 01:34:32 +000037#include <sys/time.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000038
39#include "WindowMaker.h"
40#include "wcore.h"
41#include "framewin.h"
42#include "window.h"
43#include "icon.h"
44#include "funcs.h"
45#include "menu.h"
46#include "application.h"
47#include "dock.h"
48#include "actions.h"
49#include "workspace.h"
50#include "appicon.h"
9aca0d5f dan2004-10-12 01:34:32 +000051#ifdef NETWM_HINTS
52#include "wmspec.h"
53#endif
54
55#include "xinerama.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +000056
3c323e1e Carlos R. Mafra2008-11-09 20:18:05 +010057#define MAX_SHORTCUT_LENGTH 32
9d2e6ef9 scottc1998-09-29 22:36:29 +000058
59extern WPreferences wPreferences;
60extern XContext wWinContext;
9aca0d5f dan2004-10-12 01:34:32 +000061extern XContext wVEdgeContext;
9d2e6ef9 scottc1998-09-29 22:36:29 +000062
9aca0d5f dan2004-10-12 01:34:32 +000063extern void ProcessPendingEvents();
9d2e6ef9 scottc1998-09-29 22:36:29 +000064
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020065static WMPropList *dWorkspaces = NULL;
33cc542e dan2001-10-04 03:07:34 +000066static WMPropList *dClip, *dName;
9d2e6ef9 scottc1998-09-29 22:36:29 +000067
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020068static void make_keys()
9d2e6ef9 scottc1998-09-29 22:36:29 +000069{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020070 if (dWorkspaces != NULL)
71 return;
1f22c070 id2000-11-25 21:06:31 +000072
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020073 dWorkspaces = WMCreatePLString("Workspaces");
74 dName = WMCreatePLString("Name");
75 dClip = WMCreatePLString("Clip");
9d2e6ef9 scottc1998-09-29 22:36:29 +000076}
77
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020078void wWorkspaceMake(WScreen * scr, int count)
9d2e6ef9 scottc1998-09-29 22:36:29 +000079{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020080 while (count > 0) {
81 wWorkspaceNew(scr);
82 count--;
83 }
9d2e6ef9 scottc1998-09-29 22:36:29 +000084}
85
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020086int wWorkspaceNew(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +000087{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020088 WWorkspace *wspace, **list;
89 int i;
1f22c070 id2000-11-25 21:06:31 +000090
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020091 if (scr->workspace_count < MAX_WORKSPACES) {
92 scr->workspace_count++;
9d2e6ef9 scottc1998-09-29 22:36:29 +000093
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020094 wspace = wmalloc(sizeof(WWorkspace));
95 wspace->name = NULL;
0261c326 dan1999-01-06 15:22:33 +000096
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020097 if (!wspace->name) {
98 wspace->name = wmalloc(strlen(_("Workspace %i")) + 8);
99 sprintf(wspace->name, _("Workspace %i"), scr->workspace_count);
100 }
0261c326 dan1999-01-06 15:22:33 +0000101
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200102 if (!wPreferences.flags.noclip) {
103 wspace->clip = wDockCreate(scr, WM_CLIP);
104 } else
105 wspace->clip = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000106
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200107 list = wmalloc(sizeof(WWorkspace *) * scr->workspace_count);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000108
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200109 for (i = 0; i < scr->workspace_count - 1; i++) {
110 list[i] = scr->workspaces[i];
111 }
112 list[i] = wspace;
113 if (scr->workspaces)
114 wfree(scr->workspaces);
115 scr->workspaces = list;
0261c326 dan1999-01-06 15:22:33 +0000116
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200117 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
118 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
6aafaaa6 id2000-11-21 19:30:16 +0000119#ifdef VIRTUAL_DESKTOP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200120 wspace->view_x = wspace->view_y = 0;
121 wspace->height = scr->scr_height;
122 wspace->width = scr->scr_width;
6aafaaa6 id2000-11-21 19:30:16 +0000123#endif
9aca0d5f dan2004-10-12 01:34:32 +0000124#ifdef NETWM_HINTS
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200125 wNETWMUpdateDesktop(scr);
9aca0d5f dan2004-10-12 01:34:32 +0000126#endif
127
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200128 WMPostNotificationName(WMNWorkspaceCreated, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
129 XFlush(dpy);
0261c326 dan1999-01-06 15:22:33 +0000130
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200131 return scr->workspace_count - 1;
132 }
9aca0d5f dan2004-10-12 01:34:32 +0000133
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200134 return -1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000135}
136
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200137Bool wWorkspaceDelete(WScreen * scr, int workspace)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000138{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200139 WWindow *tmp;
140 WWorkspace **list;
141 int i, j;
142
143 if (workspace <= 0)
144 return False;
145
146 /* verify if workspace is in use by some window */
147 tmp = scr->focused_window;
148 while (tmp) {
149 if (!IS_OMNIPRESENT(tmp) && tmp->frame->workspace == workspace)
150 return False;
151 tmp = tmp->prev;
152 }
153
154 if (!wPreferences.flags.noclip) {
155 wDockDestroy(scr->workspaces[workspace]->clip);
156 scr->workspaces[workspace]->clip = NULL;
157 }
158
159 list = wmalloc(sizeof(WWorkspace *) * (scr->workspace_count - 1));
160 j = 0;
161 for (i = 0; i < scr->workspace_count; i++) {
162 if (i != workspace) {
163 list[j++] = scr->workspaces[i];
164 } else {
165 if (scr->workspaces[i]->name)
166 wfree(scr->workspaces[i]->name);
167 wfree(scr->workspaces[i]);
168 }
169 }
170 wfree(scr->workspaces);
171 scr->workspaces = list;
172
173 scr->workspace_count--;
174
175 /* update menu */
176 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
177 /* clip workspace menu */
178 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
179
180 /* update also window menu */
181 if (scr->workspace_submenu) {
182 WMenu *menu = scr->workspace_submenu;
183
184 i = menu->entry_no;
185 while (i > scr->workspace_count)
186 wMenuRemoveItem(menu, --i);
187 wMenuRealize(menu);
188 }
189 /* and clip menu */
190 if (scr->clip_submenu) {
191 WMenu *menu = scr->clip_submenu;
192
193 i = menu->entry_no;
194 while (i > scr->workspace_count)
195 wMenuRemoveItem(menu, --i);
196 wMenuRealize(menu);
197 }
9aca0d5f dan2004-10-12 01:34:32 +0000198#ifdef NETWM_HINTS
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200199 wNETWMUpdateDesktop(scr);
9aca0d5f dan2004-10-12 01:34:32 +0000200#endif
201
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200202 WMPostNotificationName(WMNWorkspaceDestroyed, scr, (void *)(uintptr_t) (scr->workspace_count - 1));
0261c326 dan1999-01-06 15:22:33 +0000203
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200204 if (scr->current_workspace >= scr->workspace_count)
205 wWorkspaceChange(scr, scr->workspace_count - 1);
0261c326 dan1999-01-06 15:22:33 +0000206
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200207 return True;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000208}
209
b147e8f8 kojima1999-04-17 18:24:23 +0000210typedef struct WorkspaceNameData {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200211 int count;
212 RImage *back;
213 RImage *text;
214 time_t timeout;
b147e8f8 kojima1999-04-17 18:24:23 +0000215} WorkspaceNameData;
216
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200217static void hideWorkspaceName(void *data)
b147e8f8 kojima1999-04-17 18:24:23 +0000218{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200219 WScreen *scr = (WScreen *) data;
b147e8f8 kojima1999-04-17 18:24:23 +0000220
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200221 if (!scr->workspace_name_data || scr->workspace_name_data->count == 0
222 || time(NULL) > scr->workspace_name_data->timeout) {
223 XUnmapWindow(dpy, scr->workspace_name);
b147e8f8 kojima1999-04-17 18:24:23 +0000224
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200225 if (scr->workspace_name_data) {
226 RReleaseImage(scr->workspace_name_data->back);
227 RReleaseImage(scr->workspace_name_data->text);
228 wfree(scr->workspace_name_data);
b147e8f8 kojima1999-04-17 18:24:23 +0000229
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200230 scr->workspace_name_data = NULL;
231 }
232 scr->workspace_name_timer = NULL;
233 } else {
234 RImage *img = RCloneImage(scr->workspace_name_data->back);
235 Pixmap pix;
b147e8f8 kojima1999-04-17 18:24:23 +0000236
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200237 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
9eb4f894 kojima1999-04-17 23:11:34 +0000238
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200239 RCombineImagesWithOpaqueness(img, scr->workspace_name_data->text,
240 scr->workspace_name_data->count * 255 / 10);
b147e8f8 kojima1999-04-17 18:24:23 +0000241
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200242 RConvertImage(scr->rcontext, img, &pix);
b147e8f8 kojima1999-04-17 18:24:23 +0000243
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200244 RReleaseImage(img);
b147e8f8 kojima1999-04-17 18:24:23 +0000245
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200246 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, pix);
247 XClearWindow(dpy, scr->workspace_name);
248 XFreePixmap(dpy, pix);
249 XFlush(dpy);
b147e8f8 kojima1999-04-17 18:24:23 +0000250
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200251 scr->workspace_name_data->count--;
252 }
b147e8f8 kojima1999-04-17 18:24:23 +0000253}
254
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200255static void showWorkspaceName(WScreen * scr, int workspace)
b147e8f8 kojima1999-04-17 18:24:23 +0000256{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200257 WorkspaceNameData *data;
258 RXImage *ximg;
259 Pixmap text, mask;
260 int w, h;
261 int px, py;
262 char *name = scr->workspaces[workspace]->name;
263 int len = strlen(name);
264 int x, y;
265
266 if (wPreferences.workspace_name_display_position == WD_NONE || scr->workspace_count < 2) {
267 return;
268 }
269
270 if (scr->workspace_name_timer) {
271 WMDeleteTimerHandler(scr->workspace_name_timer);
272 XUnmapWindow(dpy, scr->workspace_name);
273 XFlush(dpy);
274 }
275 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY, hideWorkspaceName, scr);
276
277 if (scr->workspace_name_data) {
278 RReleaseImage(scr->workspace_name_data->back);
279 RReleaseImage(scr->workspace_name_data->text);
280 wfree(scr->workspace_name_data);
281 }
282
283 data = wmalloc(sizeof(WorkspaceNameData));
284 data->back = NULL;
285
286 w = WMWidthOfString(scr->workspace_name_font, name, len);
287 h = WMFontHeight(scr->workspace_name_font);
288
289 switch (wPreferences.workspace_name_display_position) {
290 case WD_TOP:
291 px = (scr->scr_width - (w + 4)) / 2;
292 py = 0;
293 break;
294 case WD_BOTTOM:
295 px = (scr->scr_width - (w + 4)) / 2;
296 py = scr->scr_height - (h + 4);
297 break;
298 case WD_TOPLEFT:
299 px = 0;
300 py = 0;
301 break;
302 case WD_TOPRIGHT:
303 px = scr->scr_width - (w + 4);
304 py = 0;
305 break;
306 case WD_BOTTOMLEFT:
307 px = 0;
308 py = scr->scr_height - (h + 4);
309 break;
310 case WD_BOTTOMRIGHT:
311 px = scr->scr_width - (w + 4);
312 py = scr->scr_height - (h + 4);
313 break;
314 case WD_CENTER:
315 default:
316 px = (scr->scr_width - (w + 4)) / 2;
317 py = (scr->scr_height - (h + 4)) / 2;
318 break;
319 }
320 XResizeWindow(dpy, scr->workspace_name, w + 4, h + 4);
321 XMoveWindow(dpy, scr->workspace_name, px, py);
322
323 text = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, scr->w_depth);
324 mask = XCreatePixmap(dpy, scr->w_win, w + 4, h + 4, 1);
325
326 /*XSetForeground(dpy, scr->mono_gc, 0);
327 XFillRectangle(dpy, mask, scr->mono_gc, 0, 0, w+4, h+4); */
328
329 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
330
331 for (x = 0; x <= 4; x++) {
332 for (y = 0; y <= 4; y++) {
333 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, x, y, name, len);
334 }
335 }
336
337 XSetForeground(dpy, scr->mono_gc, 1);
338 XSetBackground(dpy, scr->mono_gc, 0);
339
340 XCopyPlane(dpy, text, mask, scr->mono_gc, 0, 0, w + 4, h + 4, 0, 0, 1 << (scr->w_depth - 1));
341
342 /*XSetForeground(dpy, scr->mono_gc, 1); */
343 XSetBackground(dpy, scr->mono_gc, 1);
344
345 XFillRectangle(dpy, text, WMColorGC(scr->black), 0, 0, w + 4, h + 4);
346
347 WMDrawString(scr->wmscreen, text, scr->white, scr->workspace_name_font, 2, 2, name, len);
b147e8f8 kojima1999-04-17 18:24:23 +0000348
b147e8f8 kojima1999-04-17 18:24:23 +0000349#ifdef SHAPE
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200350 XShapeCombineMask(dpy, scr->workspace_name, ShapeBounding, 0, 0, mask, ShapeSet);
b147e8f8 kojima1999-04-17 18:24:23 +0000351#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200352 XSetWindowBackgroundPixmap(dpy, scr->workspace_name, text);
353 XClearWindow(dpy, scr->workspace_name);
b147e8f8 kojima1999-04-17 18:24:23 +0000354
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200355 data->text = RCreateImageFromDrawable(scr->rcontext, text, None);
b147e8f8 kojima1999-04-17 18:24:23 +0000356
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200357 XFreePixmap(dpy, text);
358 XFreePixmap(dpy, mask);
b147e8f8 kojima1999-04-17 18:24:23 +0000359
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200360 if (!data->text) {
361 XMapRaised(dpy, scr->workspace_name);
362 XFlush(dpy);
b147e8f8 kojima1999-04-17 18:24:23 +0000363
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200364 goto erro;
365 }
b147e8f8 kojima1999-04-17 18:24:23 +0000366
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200367 ximg = RGetXImage(scr->rcontext, scr->root_win, px, py, data->text->width, data->text->height);
b147e8f8 kojima1999-04-17 18:24:23 +0000368
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200369 if (!ximg || !ximg->image) {
370 goto erro;
371 }
b147e8f8 kojima1999-04-17 18:24:23 +0000372
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200373 XMapRaised(dpy, scr->workspace_name);
374 XFlush(dpy);
b147e8f8 kojima1999-04-17 18:24:23 +0000375
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200376 data->back = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
377 RDestroyXImage(scr->rcontext, ximg);
b147e8f8 kojima1999-04-17 18:24:23 +0000378
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200379 if (!data->back) {
380 goto erro;
381 }
b147e8f8 kojima1999-04-17 18:24:23 +0000382
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200383 data->count = 10;
b147e8f8 kojima1999-04-17 18:24:23 +0000384
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200385 /* set a timeout for the effect */
386 data->timeout = time(NULL) + 2 + (WORKSPACE_NAME_DELAY + WORKSPACE_NAME_FADE_DELAY * data->count) / 1000;
daeb6d67 kojima1999-04-30 23:31:00 +0000387
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200388 scr->workspace_name_data = data;
b147e8f8 kojima1999-04-17 18:24:23 +0000389
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200390 return;
daeb6d67 kojima1999-04-30 23:31:00 +0000391
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200392 erro:
393 if (scr->workspace_name_timer)
394 WMDeleteTimerHandler(scr->workspace_name_timer);
b147e8f8 kojima1999-04-17 18:24:23 +0000395
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200396 if (data->text)
397 RReleaseImage(data->text);
398 if (data->back)
399 RReleaseImage(data->back);
400 wfree(data);
7f018497 kojima1999-04-24 20:08:20 +0000401
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200402 scr->workspace_name_data = NULL;
b147e8f8 kojima1999-04-17 18:24:23 +0000403
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200404 scr->workspace_name_timer = WMAddTimerHandler(WORKSPACE_NAME_DELAY +
405 10 * WORKSPACE_NAME_FADE_DELAY, hideWorkspaceName, scr);
b147e8f8 kojima1999-04-17 18:24:23 +0000406}
407
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200408void wWorkspaceChange(WScreen * scr, int workspace)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000409{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200410 if (scr->flags.startup || scr->flags.startup2) {
411 return;
412 }
413
414 if (workspace != scr->current_workspace) {
415 wWorkspaceForceChange(scr, workspace);
416 } /*else {
417 showWorkspaceName(scr, workspace);
418 } */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000419}
420
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200421void wWorkspaceRelativeChange(WScreen * scr, int amount)
0261c326 dan1999-01-06 15:22:33 +0000422{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200423 int w;
424
425 w = scr->current_workspace + amount;
426
427 if (amount < 0) {
428 if (w >= 0) {
429 wWorkspaceChange(scr, w);
430 } else if (wPreferences.ws_cycle) {
431 wWorkspaceChange(scr, scr->workspace_count + w);
432 }
433 } else if (amount > 0) {
434 if (w < scr->workspace_count) {
435 wWorkspaceChange(scr, w);
436 } else if (wPreferences.ws_advance) {
437 wWorkspaceChange(scr, WMIN(w, MAX_WORKSPACES - 1));
438 } else if (wPreferences.ws_cycle) {
439 wWorkspaceChange(scr, w % scr->workspace_count);
440 }
441 }
0261c326 dan1999-01-06 15:22:33 +0000442}
443
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200444void wWorkspaceForceChange(WScreen * scr, int workspace)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000445{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200446 WWindow *tmp, *foc = NULL, *foc2 = NULL;
447
448 if (workspace >= MAX_WORKSPACES || workspace < 0)
449 return;
450
451 SendHelperMessage(scr, 'C', workspace + 1, NULL);
452
453 if (workspace > scr->workspace_count - 1) {
454 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
455 }
456
457 wClipUpdateForWorkspaceChange(scr, workspace);
458
459 scr->current_workspace = workspace;
460
461 wWorkspaceMenuUpdate(scr, scr->workspace_menu);
462
463 wWorkspaceMenuUpdate(scr, scr->clip_ws_menu);
464
465 if ((tmp = scr->focused_window) != NULL) {
466 if ((IS_OMNIPRESENT(tmp) && (tmp->flags.mapped || tmp->flags.shaded) &&
467 !WFLAGP(tmp, no_focusable)) || tmp->flags.changing_workspace) {
468 foc = tmp;
469 }
470
471 /* foc2 = tmp; will fix annoyance with gnome panel
472 * but will create annoyance for every other application
473 */
474 while (tmp) {
475 if (tmp->frame->workspace != workspace && !tmp->flags.selected) {
476 /* unmap windows not on this workspace */
477 if ((tmp->flags.mapped || tmp->flags.shaded) &&
478 !IS_OMNIPRESENT(tmp) && !tmp->flags.changing_workspace) {
479 wWindowUnmap(tmp);
480 }
481 /* also unmap miniwindows not on this workspace */
482 if (!wPreferences.sticky_icons && tmp->flags.miniaturized &&
483 tmp->icon && !IS_OMNIPRESENT(tmp)) {
484 XUnmapWindow(dpy, tmp->icon->core->window);
485 tmp->icon->mapped = 0;
486 }
487 /* update current workspace of omnipresent windows */
488 if (IS_OMNIPRESENT(tmp)) {
489 WApplication *wapp = wApplicationOf(tmp->main_window);
490
491 tmp->frame->workspace = workspace;
492
493 if (wapp) {
494 wapp->last_workspace = workspace;
495 }
496 if (!foc2 && (tmp->flags.mapped || tmp->flags.shaded)) {
497 foc2 = tmp;
498 }
499 }
500 } else {
501 /* change selected windows' workspace */
502 if (tmp->flags.selected) {
503 wWindowChangeWorkspace(tmp, workspace);
504 if (!tmp->flags.miniaturized && !foc) {
505 foc = tmp;
506 }
507 } else {
508 if (!tmp->flags.hidden) {
509 if (!(tmp->flags.mapped || tmp->flags.miniaturized)) {
510 /* remap windows that are on this workspace */
511 wWindowMap(tmp);
512 if (!foc && !WFLAGP(tmp, no_focusable)) {
513 foc = tmp;
514 }
515 }
516 /* Also map miniwindow if not omnipresent */
517 if (!wPreferences.sticky_icons &&
518 tmp->flags.miniaturized && !IS_OMNIPRESENT(tmp) && tmp->icon) {
519 tmp->icon->mapped = 1;
520 XMapWindow(dpy, tmp->icon->core->window);
521 }
522 }
523 }
524 }
525 tmp = tmp->prev;
526 }
527
528 /* Gobble up events unleashed by our mapping & unmapping.
529 * These may trigger various grab-initiated focus &
530 * crossing events. However, we don't care about them,
531 * and ignore their focus implications altogether to avoid
532 * flicker.
533 */
534 scr->flags.ignore_focus_events = 1;
535 ProcessPendingEvents();
536 scr->flags.ignore_focus_events = 0;
537
538 if (!foc)
539 foc = foc2;
540
541 if (scr->focused_window->flags.mapped && !foc) {
542 foc = scr->focused_window;
543 }
544 if (wPreferences.focus_mode == WKF_CLICK) {
545 wSetFocusTo(scr, foc);
546 } else {
547 unsigned int mask;
548 int foo;
549 Window bar, win;
550 WWindow *tmp;
551
552 tmp = NULL;
553 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask)) {
554 tmp = wWindowFor(win);
555 }
556
557 /* If there's a window under the pointer, focus it.
558 * (we ate all other focus events above, so it's
559 * certainly not focused). Otherwise focus last
560 * focused, or the root (depending on sloppiness)
561 */
562 if (!tmp && wPreferences.focus_mode == WKF_SLOPPY) {
563 wSetFocusTo(scr, foc);
564 } else {
565 wSetFocusTo(scr, tmp);
566 }
567 }
568 }
569
570 /* We need to always arrange icons when changing workspace, even if
571 * no autoarrange icons, because else the icons in different workspaces
572 * can be superposed.
573 * This can be avoided if appicons are also workspace specific.
574 */
575 if (!wPreferences.sticky_icons)
576 wArrangeIcons(scr, False);
577
578 if (scr->dock)
579 wAppIconPaint(scr->dock->icon_array[0]);
580
581 if (scr->clip_icon) {
582 if (scr->workspaces[workspace]->clip->auto_collapse ||
583 scr->workspaces[workspace]->clip->auto_raise_lower) {
584 /* to handle enter notify. This will also */
585 XUnmapWindow(dpy, scr->clip_icon->icon->core->window);
586 XMapWindow(dpy, scr->clip_icon->icon->core->window);
587 } else {
588 wClipIconPaint(scr->clip_icon);
589 }
590 }
9aca0d5f dan2004-10-12 01:34:32 +0000591#ifdef NETWM_HINTS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200592 wScreenUpdateUsableArea(scr);
593 wNETWMUpdateDesktop(scr);
9aca0d5f dan2004-10-12 01:34:32 +0000594#endif
27a396d7 dan2003-06-06 04:11:00 +0000595
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200596 showWorkspaceName(scr, workspace);
b147e8f8 kojima1999-04-17 18:24:23 +0000597
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200598 WMPostNotificationName(WMNWorkspaceChanged, scr, (void *)(uintptr_t) workspace);
5ef342d9 kojima2001-09-06 21:42:28 +0000599
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200600 /* XSync(dpy, False); */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000601}
602
8eb0c79c id1999-12-18 11:52:49 +0000603#ifdef VIRTUAL_DESKTOP
9aca0d5f dan2004-10-12 01:34:32 +0000604
a9b75e03 id2000-11-23 10:15:21 +0000605/* TODO:
9aca0d5f dan2004-10-12 01:34:32 +0000606 *
607 * 1) Allow border around each window so the scrolling
608 * won't just stop at the border.
609 * 2) Make pager.
610 *
611 */
612
613#define vec_sub(a, b) wmkpoint((a).x-(b).x, (a).y-(b).y)
614#define vec_add(a, b) wmkpoint((a).x+(b).x, (a).y+(b).y)
615#define vec_inc(a, b) do { (a).x+=(b).x; (a).y+=(b).y; } while(0)
616#define vec_dot(a, b) ((a).x*(b).x + (a).y*(b).y)
617#define vec_scale(a, s) wmkpoint((a).x*s, (a).y*s)
618#define vec_scale2(a, s, t) wmkpoint((a).x*s, (a).y*t)
619
620#ifndef HAS_BORDER
621#define HAS_BORDER(w) (!(WFLAGP((w), no_border)))
622#endif
623
624#ifndef IS_VSTUCK
625#define IS_VSTUCK(w) (WFLAGP((w), virtual_stick))
626#endif
627
628#define updateMinimum(l,p,ml,mp) do { if (cmp(l) && (l)<(ml)) { (ml)=(l); (mp)=(p); }; } while(0)
629
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200630static Bool cmp_gez(int i)
9aca0d5f dan2004-10-12 01:34:32 +0000631{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200632 return (i >= 0);
9aca0d5f dan2004-10-12 01:34:32 +0000633}
634
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200635static Bool cmp_gz(int i)
897e75d5 dan2004-10-24 20:24:23 +0000636{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200637 return (i > 0);
897e75d5 dan2004-10-24 20:24:23 +0000638}
639
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200640static WMPoint getClosestEdge(WScreen * scr, WMPoint direction, Bool(*cmp) (int))
641{
642 WMPoint closest = wmkpoint(0, 0);
643 int closest_len = INT_MAX;
644 WWindow *wwin;
645
646 for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
647 if (wwin->frame->workspace == scr->current_workspace) {
648 if (!wwin->flags.miniaturized && !IS_VSTUCK(wwin) && !wwin->flags.hidden) {
649 int border = 2 * HAS_BORDER(wwin);
650 int len;
651 int x1, x2, y1, y2;
652 int head = wGetHeadForWindow(wwin);
653 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
654 WMPoint p;
655
656 x1 = wwin->frame_x - area.x1;
657 y1 = wwin->frame_y - area.y1;
658 x2 = wwin->frame_x + wwin->frame->core->width + border - area.x2;
659 y2 = wwin->frame_y + wwin->frame->core->height + border - area.y2;
660
661 p = wmkpoint(x1, y1);
662 len = vec_dot(direction, p);
663 updateMinimum(len, p, closest_len, closest);
664
665 p = wmkpoint(x1, y2);
666 len = vec_dot(direction, p);
667 updateMinimum(len, p, closest_len, closest);
668
669 p = wmkpoint(x2, y1);
670 len = vec_dot(direction, p);
671 updateMinimum(len, p, closest_len, closest);
672
673 p = wmkpoint(x2, y2);
674 len = vec_dot(direction, p);
675 updateMinimum(len, p, closest_len, closest);
676 }
677 }
678 }
679
680 return closest;
681}
897e75d5 dan2004-10-24 20:24:23 +0000682
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200683static void getViewPosition(WScreen * scr, int workspace, int *x, int *y)
9aca0d5f dan2004-10-12 01:34:32 +0000684{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200685 *x = scr->workspaces[workspace]->view_x;
686 *y = scr->workspaces[workspace]->view_y;
9aca0d5f dan2004-10-12 01:34:32 +0000687}
688
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200689void wWorkspaceKeyboardMoveDesktop(WScreen * scr, WMPoint direction)
690{
691 int x, y;
692 WMPoint edge = getClosestEdge(scr, direction, cmp_gz);
693 int len = vec_dot(edge, direction);
694 WMPoint step = vec_scale(direction, len);
695 getViewPosition(scr, scr->current_workspace, &x, &y);
696 wWorkspaceSetViewport(scr, scr->current_workspace, x + step.x, y + step.y);
697}
9aca0d5f dan2004-10-12 01:34:32 +0000698
699extern Cursor wCursor[WCUR_LAST];
700
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200701static void vdMouseMoveDesktop(XEvent * event, WMPoint direction)
9aca0d5f dan2004-10-12 01:34:32 +0000702{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200703 static int lock = False;
704 if (lock)
705 return;
706 lock = True;
707
708 Bool done = False;
709 Bool moved = True;
710 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
711 WMPoint old_pos = wmkpoint(event->xcrossing.x_root, event->xcrossing.y_root);
712 WMPoint step;
713 int x, y;
714 int resisted = 0;
715
716 if (XGrabPointer(dpy, event->xcrossing.window, False,
717 PointerMotionMask, GrabModeAsync, GrabModeAsync,
718 scr->root_win, wCursor[WCUR_EMPTY], CurrentTime) != GrabSuccess) {
719
720 /* if the grab fails, do it the old fashioned way */
721 step = vec_scale2(direction, wPreferences.vedge_hscrollspeed, wPreferences.vedge_vscrollspeed);
722 getViewPosition(scr, scr->current_workspace, &x, &y);
723 if (wWorkspaceSetViewport(scr, scr->current_workspace, x + step.x, y + step.y)) {
724 step = vec_scale(direction, 2);
725 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0,
726 event->xcrossing.x_root - step.x, event->xcrossing.y_root - step.y);
727 }
728 goto exit;
729 }
730 XSync(dpy, True);
731
732 if (old_pos.x < 0)
733 old_pos.x = 0;
734 if (old_pos.y < 0)
735 old_pos.y = 0;
736 if (old_pos.x > scr->scr_width)
737 old_pos.x = scr->scr_width;
738 if (old_pos.y > scr->scr_height)
739 old_pos.y = scr->scr_height;
740
741 while (!done) {
742 XEvent ev;
743 if (moved) {
744 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0,
745 scr->scr_width / 2, scr->scr_height / 2);
746 moved = False;
747 }
748 WMMaskEvent(dpy, PointerMotionMask, &ev);
749
750 switch (ev.type) {
751 case MotionNotify:
752 {
753 int step_len;
754 step = wmkpoint(ev.xmotion.x_root - scr->scr_width / 2,
755 ev.xmotion.y_root - scr->scr_height / 2);
756 step_len = vec_dot(step, direction);
757 if (step_len < 0) {
758 done = True;
759 break;
760 }
761
762 if (step_len > 0) {
763 Bool do_move = True;
764 int resist = wPreferences.vedge_resistance;
765 WMPoint closest;
766 int closest_len = INT_MAX;
767 if (resist) {
768 closest = getClosestEdge(scr, direction, cmp_gez);
769 closest_len = vec_dot(direction, closest);
770 }
771
772 if (!closest_len) {
773 resisted += step_len;
774 do_move = resisted >= resist;
775 if (do_move) {
776 closest_len = INT_MAX;
777 step_len = resisted - resist;
778 resisted = 0;
779 }
780 }
781 if (do_move) {
782 if (closest_len <= wPreferences.vedge_attraction) {
783 step = vec_scale(direction, closest_len);
784 } else {
785 step = vec_scale(direction, step_len);
786 }
787
788 getViewPosition(scr, scr->current_workspace, &x, &y);
789 wWorkspaceSetViewport(scr, scr->current_workspace,
790 x + step.x, y + step.y);
791 moved = True;
792 }
793 }
794 }
795 break;
796 }
797 }
798
799 step = vec_add(old_pos, vec_scale(direction, -1));
800 XWarpPointer(dpy, None, scr->root_win, 0, 0, 0, 0, step.x, step.y);
801 XUngrabPointer(dpy, CurrentTime);
802
803 exit:
804 lock = False;
9aca0d5f dan2004-10-12 01:34:32 +0000805}
806
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200807static void vdHandleEnter_u(XEvent * event)
808{
809 vdMouseMoveDesktop(event, VEC_UP);
9aca0d5f dan2004-10-12 01:34:32 +0000810}
811
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200812static void vdHandleEnter_d(XEvent * event)
813{
814 vdMouseMoveDesktop(event, VEC_DOWN);
9aca0d5f dan2004-10-12 01:34:32 +0000815}
816
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200817static void vdHandleEnter_l(XEvent * event)
818{
819 vdMouseMoveDesktop(event, VEC_LEFT);
9aca0d5f dan2004-10-12 01:34:32 +0000820}
821
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200822static void vdHandleEnter_r(XEvent * event)
823{
824 vdMouseMoveDesktop(event, VEC_RIGHT);
9aca0d5f dan2004-10-12 01:34:32 +0000825}
826
9aca0d5f dan2004-10-12 01:34:32 +0000827#define LEFT_EDGE 0x01
828#define RIGHT_EDGE 0x02
829#define TOP_EDGE 0x04
830#define BOTTOM_EDGE 0x08
831#define ALL_EDGES 0x0F
8eb0c79c id1999-12-18 11:52:49 +0000832
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200833static void createEdges(WScreen * scr)
b0506c37 id2000-11-19 10:33:41 +0000834{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200835 if (!scr->virtual_edges) {
836 int i, j, w;
837 int vmask;
838 XSetWindowAttributes attribs;
839
840 int heads = wXineramaHeads(scr);
841 int *hasEdges = (int *)wmalloc(sizeof(int) * heads);
842
843 int thickness = 1;
844 int nr_edges = 0;
845 int max_edges = 4 * heads;
846 int head;
847 Window *edges = (Window *) wmalloc(sizeof(Window) * max_edges);
848
849 for (i = 0; i < heads; ++i)
850 hasEdges[i] = ALL_EDGES;
851 for (i = 0; i < heads; ++i) {
852 WMRect i_rect = wGetRectForHead(scr, i);
853 for (j = i + 1; j < heads; ++j) {
854 WMRect j_rect = wGetRectForHead(scr, j);
855
856 int vlen = (WMIN(i_rect.pos.y + i_rect.size.height,
857 j_rect.pos.y + j_rect.size.height) -
858 WMAX(i_rect.pos.y, j_rect.pos.y));
859
860 int hlen = (WMIN(i_rect.pos.x + i_rect.size.width,
861 j_rect.pos.x + j_rect.size.width) -
862 WMAX(i_rect.pos.x, j_rect.pos.x));
863
864 if (vlen > 0 && hlen == 0) { /* horz alignment, vert edges touch */
865 if (i_rect.pos.x < j_rect.pos.x) { /* i left of j */
866 hasEdges[i] &= ~RIGHT_EDGE;
867 hasEdges[j] &= ~LEFT_EDGE;
868 } else { /* j left of i */
869 hasEdges[j] &= ~RIGHT_EDGE;
870 hasEdges[i] &= ~LEFT_EDGE;
871 }
872 } else if (vlen == 0 && hlen > 0) { /* vert alignment, horz edges touch */
873 if (i_rect.pos.y < j_rect.pos.y) { /* i top of j */
874 hasEdges[i] &= ~BOTTOM_EDGE;
875 hasEdges[j] &= ~TOP_EDGE;
876 } else { /* j top of i */
877 hasEdges[j] &= ~BOTTOM_EDGE;
878 hasEdges[i] &= ~TOP_EDGE;
879 }
880 }
881 }
882 }
883
884 for (w = 0; w < scr->workspace_count; w++) {
885 /* puts("reset workspace"); */
886 wWorkspaceSetViewport(scr, w, 0, 0);
887 }
888
889 vmask = CWEventMask | CWOverrideRedirect;
890 attribs.event_mask = (EnterWindowMask | LeaveWindowMask | VisibilityChangeMask);
891 attribs.override_redirect = True;
892
893 for (head = 0; head < wXineramaHeads(scr); ++head) {
894 WMRect rect = wGetRectForHead(scr, head);
895
896 if (hasEdges[head] & TOP_EDGE) {
897 edges[nr_edges] =
898 XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
899 rect.size.width, thickness, 0,
900 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
901 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_u);
902 ++nr_edges;
903 }
904
905 if (hasEdges[head] & BOTTOM_EDGE) {
906 edges[nr_edges] =
907 XCreateWindow(dpy, scr->root_win, rect.pos.x,
908 rect.pos.y + rect.size.height - thickness,
909 rect.size.width, thickness, 0,
910 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
911 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_d);
912 ++nr_edges;
913 }
914
915 if (hasEdges[head] & LEFT_EDGE) {
916 edges[nr_edges] =
917 XCreateWindow(dpy, scr->root_win, rect.pos.x, rect.pos.y,
918 thickness, rect.pos.y + rect.size.height, 0,
919 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
920 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_l);
921 ++nr_edges;
922 }
923
924 if (hasEdges[head] & RIGHT_EDGE) {
925 edges[nr_edges] =
926 XCreateWindow(dpy, scr->root_win,
927 rect.pos.x + rect.size.width - thickness, rect.pos.y,
928 thickness, rect.size.height, 0,
929 CopyFromParent, InputOnly, CopyFromParent, vmask, &attribs);
930 XSaveContext(dpy, edges[nr_edges], wVEdgeContext, (XPointer) vdHandleEnter_r);
931 ++nr_edges;
932 }
933 }
934
935 scr->virtual_nr_edges = nr_edges;
936 scr->virtual_edges = edges;
937
938 for (i = 0; i < scr->virtual_nr_edges; ++i) {
939 XMapWindow(dpy, scr->virtual_edges[i]);
940 }
941 wWorkspaceRaiseEdge(scr);
942
943 wfree(hasEdges);
944 }
8eb0c79c id1999-12-18 11:52:49 +0000945}
946
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200947static void destroyEdges(WScreen * scr)
9aca0d5f dan2004-10-12 01:34:32 +0000948{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200949 if (scr->virtual_edges) {
950 int i;
951
952 for (i = 0; i < scr->virtual_nr_edges; ++i) {
953 XDeleteContext(dpy, scr->virtual_edges[i], wVEdgeContext);
954 XUnmapWindow(dpy, scr->virtual_edges[i]);
955 XDestroyWindow(dpy, scr->virtual_edges[i]);
956 }
957
958 wfree(scr->virtual_edges);
959 scr->virtual_edges = NULL;
960 scr->virtual_nr_edges = 0;
961 }
897e75d5 dan2004-10-24 20:24:23 +0000962}
9aca0d5f dan2004-10-12 01:34:32 +0000963
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200964void wWorkspaceUpdateEdge(WScreen * scr)
897e75d5 dan2004-10-24 20:24:23 +0000965{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200966 if (wPreferences.vdesk_enable) {
967 createEdges(scr);
968 } else {
969 destroyEdges(scr);
970 }
9aca0d5f dan2004-10-12 01:34:32 +0000971}
972
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200973void wWorkspaceRaiseEdge(WScreen * scr)
b0506c37 id2000-11-19 10:33:41 +0000974{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200975 static int toggle = 0;
976 int i;
977
978 if (!scr->virtual_edges)
979 return;
980
981 if (toggle) {
982 for (i = 0; i < scr->virtual_nr_edges; ++i) {
983 XRaiseWindow(dpy, scr->virtual_edges[i]);
984 }
985 } else {
986 for (i = scr->virtual_nr_edges - 1; i >= 0; --i) {
987 XRaiseWindow(dpy, scr->virtual_edges[i]);
988 }
989 }
990
991 toggle ^= 1;
8eb0c79c id1999-12-18 11:52:49 +0000992}
993
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200994void wWorkspaceLowerEdge(WScreen * scr)
9d1306bf id2001-04-16 13:33:50 +0000995{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200996 int i;
997 for (i = 0; i < scr->virtual_nr_edges; ++i) {
998 XLowerWindow(dpy, scr->virtual_edges[i]);
999 }
9d1306bf id2001-04-16 13:33:50 +00001000}
1001
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001002void wWorkspaceResizeViewport(WScreen * scr, int workspace)
8eb0c79c id1999-12-18 11:52:49 +00001003{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001004 int x, y;
1005 getViewPosition(scr, scr->current_workspace, &x, &y);
1006 wWorkspaceSetViewport(scr, scr->current_workspace, x, y);
6aafaaa6 id2000-11-21 19:30:16 +00001007}
b0506c37 id2000-11-19 10:33:41 +00001008
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001009void updateWorkspaceGeometry(WScreen * scr, int workspace, int *view_x, int *view_y)
89a32e36 dan2002-01-05 06:16:10 +00001010{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001011 int most_left, most_right, most_top, most_bottom;
1012 WWindow *wwin;
1013
1014 int heads = wXineramaHeads(scr);
1015 typedef int strut_t[4];
1016 strut_t *strut = (strut_t *) wmalloc(heads * sizeof(strut_t));
1017 int head, i;
1018
1019 for (head = 0; head < heads; ++head) {
1020 WMRect rect = wGetRectForHead(scr, head);
1021 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1022 strut[head][0] = area.x1 - rect.pos.x;
1023 strut[head][1] = rect.pos.x + rect.size.width - area.x2;
1024 strut[head][2] = area.y1 - rect.pos.y;
1025 strut[head][3] = rect.pos.y + rect.size.height - area.y2;
1026 }
1027
1028 /* adjust workspace layout */
1029 wwin = scr->focused_window;
1030 most_right = 0;
1031 most_bottom = 0;
1032 most_left = scr->scr_width;
1033 most_top = scr->scr_height;
1034 for (; wwin; wwin = wwin->prev) {
1035 if (wwin->frame->workspace == workspace) {
1036 if (!wwin->flags.miniaturized && !IS_VSTUCK(wwin) && !wwin->flags.hidden) {
1037
1038 head = wGetHeadForWindow(wwin);
1039
1040 i = wwin->frame_x - strut[head][0];
1041 if (i < most_left) /* record positions, should this be cached? */
1042 most_left = i;
1043 i = wwin->frame_x + wwin->frame->core->width + strut[head][1];
1044 if (HAS_BORDER(wwin))
1045 i += 2;
1046 if (i > most_right)
1047 most_right = i;
1048 i = wwin->frame_y - strut[head][2];
1049 if (i < most_top)
1050 most_top = i;
1051 i = wwin->frame_y + wwin->frame->core->height + strut[head][3];
1052 if (HAS_BORDER(wwin))
1053 i += 2;
1054 if (i > most_bottom) {
1055 most_bottom = i;
1056 }
1057 }
1058 }
1059 }
1060
1061 if (most_left > 0)
1062 most_left = 0;
1063 if (most_top > 0)
1064 most_top = 0;
1065
1066 scr->workspaces[workspace]->width = WMAX(most_right, scr->scr_width) - WMIN(most_left, 0);
1067 scr->workspaces[workspace]->height = WMAX(most_bottom, scr->scr_height) - WMIN(most_top, 0);
1068
1069 *view_x += -most_left - scr->workspaces[workspace]->view_x;
1070 scr->workspaces[workspace]->view_x = -most_left;
1071
1072 *view_y += -most_top - scr->workspaces[workspace]->view_y;
1073 scr->workspaces[workspace]->view_y = -most_top;
1074
1075 wfree(strut);
6aafaaa6 id2000-11-21 19:30:16 +00001076}
b0506c37 id2000-11-19 10:33:41 +00001077
1f22c070 id2000-11-25 21:06:31 +00001078typedef struct _delay_configure {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001079 WWindow *wwin;
1080 int delay_count;
1f22c070 id2000-11-25 21:06:31 +00001081} _delay_configure;
1082
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001083void sendConfigureNotify(_delay_configure * delay)
89a32e36 dan2002-01-05 06:16:10 +00001084{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001085 WWindow *wwin;
1086
1087 delay->delay_count--;
1088 if (!delay->delay_count) {
1089 for (wwin = delay->wwin; wwin; wwin = wwin->prev) {
1090 wWindowSynthConfigureNotify(wwin);
1091 }
1092 }
1f22c070 id2000-11-25 21:06:31 +00001093}
1094
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001095Bool wWorkspaceSetViewport(WScreen * scr, int workspace, int view_x, int view_y)
6aafaaa6 id2000-11-21 19:30:16 +00001096{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001097 Bool adjust_flag = False;
1098 int diff_x, diff_y;
1099 static _delay_configure delay_configure = { NULL, 0 };
1100 WWorkspace *wptr;
1101 WWindow *wwin;
1102
1103 wptr = scr->workspaces[workspace];
1104
1105 /*printf("wWorkspaceSetViewport %d %d\n", view_x, view_y); */
1106
1107 updateWorkspaceGeometry(scr, workspace, &view_x, &view_y);
1108
1109 if (view_x + scr->scr_width > wptr->width) {
1110 /* puts("right edge of vdesk"); */
1111 view_x = wptr->width - scr->scr_width;
1112 }
1113 if (view_x < 0) {
1114 /* puts("left edge of vdesk"); */
1115 view_x = 0;
1116 }
1117 if (view_y + scr->scr_height > wptr->height) {
1118 /* puts("right edge of vdesk"); */
1119 view_y = wptr->height - scr->scr_height;
1120 }
1121 if (view_y < 0) {
1122 /* puts("left edge of vdesk"); */
1123 view_y = 0;
1124 }
1125
1126 diff_x = wptr->view_x - view_x;
1127 diff_y = wptr->view_y - view_y;
1128 if (!diff_x && !diff_y)
1129 return False;
1130
1131 wptr->view_x = view_x;
1132 wptr->view_y = view_y;
b0506c37 id2000-11-19 10:33:41 +00001133
9aca0d5f dan2004-10-12 01:34:32 +00001134#ifdef NETWM_HINTS
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001135 wNETWMUpdateDesktop(scr);
9aca0d5f dan2004-10-12 01:34:32 +00001136#endif
b0506c37 id2000-11-19 10:33:41 +00001137
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001138 for (wwin = scr->focused_window; wwin; wwin = wwin->prev) {
1139 if (wwin->frame->workspace == workspace && !IS_VSTUCK(wwin)) {
1140 wWindowMove(wwin, wwin->frame_x + diff_x, wwin->frame_y + diff_y);
1141 adjust_flag = True;
1142 }
1143 }
1144 if (1) { /* if delay */
1145 delay_configure.delay_count++;
1146 delay_configure.wwin = scr->focused_window;
1147 WMAddTimerHandler(200, (WMCallback *) sendConfigureNotify, &delay_configure);
1148 }
1149
1150 return adjust_flag;
8eb0c79c id1999-12-18 11:52:49 +00001151}
1152
8eb0c79c id1999-12-18 11:52:49 +00001153#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +00001154
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001155static void switchWSCommand(WMenu * menu, WMenuEntry * entry)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001156{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001157 wWorkspaceChange(menu->frame->screen_ptr, (long)entry->clientdata);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001158}
1159
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001160static void deleteWSCommand(WMenu * menu, WMenuEntry * entry)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001161{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001162 wWorkspaceDelete(menu->frame->screen_ptr, menu->frame->screen_ptr->workspace_count - 1);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001163}
1164
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001165static void newWSCommand(WMenu * menu, WMenuEntry * foo)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001166{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001167 int ws;
1168
1169 ws = wWorkspaceNew(menu->frame->screen_ptr);
1170 /* autochange workspace */
1171 if (ws >= 0)
1172 wWorkspaceChange(menu->frame->screen_ptr, ws);
1173
1174 /*
1175 if (ws<9) {
1176 int kcode;
1177 if (wKeyBindings[WKBD_WORKSPACE1+ws]) {
1178 kcode = wKeyBindings[WKBD_WORKSPACE1+ws]->keycode;
1179 entry->rtext =
1180 wstrdup(XKeysymToString(XKeycodeToKeysym(dpy, kcode, 0)));
1181 }
1182 } */
9d2e6ef9 scottc1998-09-29 22:36:29 +00001183}
1184
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001185static char *cropline(char *line)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001186{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001187 char *start, *end;
1188
1189 if (strlen(line) == 0)
1190 return line;
1191
1192 start = line;
1193 end = &(line[strlen(line)]) - 1;
1194 while (isspace(*line) && *line != 0)
1195 line++;
1196 while (isspace(*end) && end != line) {
1197 *end = 0;
1198 end--;
1199 }
1200 return line;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001201}
1202
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001203void wWorkspaceRename(WScreen * scr, int workspace, char *name)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001204{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001205 char buf[MAX_WORKSPACENAME_WIDTH + 1];
1206 char *tmp;
1207
1208 if (workspace >= scr->workspace_count)
1209 return;
1210
1211 /* trim white spaces */
1212 tmp = cropline(name);
1213
1214 if (strlen(tmp) == 0) {
1215 snprintf(buf, sizeof(buf), _("Workspace %i"), workspace + 1);
1216 } else {
1217 strncpy(buf, tmp, MAX_WORKSPACENAME_WIDTH);
1218 }
1219 buf[MAX_WORKSPACENAME_WIDTH] = 0;
1220
1221 /* update workspace */
1222 wfree(scr->workspaces[workspace]->name);
1223 scr->workspaces[workspace]->name = wstrdup(buf);
1224
1225 if (scr->clip_ws_menu) {
1226 if (strcmp(scr->clip_ws_menu->entries[workspace + 2]->text, buf) != 0) {
1227 wfree(scr->clip_ws_menu->entries[workspace + 2]->text);
1228 scr->clip_ws_menu->entries[workspace + 2]->text = wstrdup(buf);
1229 wMenuRealize(scr->clip_ws_menu);
1230 }
1231 }
1232 if (scr->workspace_menu) {
1233 if (strcmp(scr->workspace_menu->entries[workspace + 2]->text, buf) != 0) {
1234 wfree(scr->workspace_menu->entries[workspace + 2]->text);
1235 scr->workspace_menu->entries[workspace + 2]->text = wstrdup(buf);
1236 wMenuRealize(scr->workspace_menu);
1237 }
1238 }
1239
1240 if (scr->clip_icon)
1241 wClipIconPaint(scr->clip_icon);
1242
1243 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) workspace);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001244}
1245
9d2e6ef9 scottc1998-09-29 22:36:29 +00001246/* callback for when menu entry is edited */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001247static void onMenuEntryEdited(WMenu * menu, WMenuEntry * entry)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001248{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001249 char *tmp;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001250
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001251 tmp = entry->text;
1252 wWorkspaceRename(menu->frame->screen_ptr, (long)entry->clientdata, tmp);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001253}
1254
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001255WMenu *wWorkspaceMenuMake(WScreen * scr, Bool titled)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001256{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001257 WMenu *wsmenu;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001258
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001259 wsmenu = wMenuCreate(scr, titled ? _("Workspaces") : NULL, False);
1260 if (!wsmenu) {
1261 wwarning(_("could not create Workspace menu"));
1262 return NULL;
1263 }
1f22c070 id2000-11-25 21:06:31 +00001264
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001265 /* callback to be called when an entry is edited */
1266 wsmenu->on_edit = onMenuEntryEdited;
1f22c070 id2000-11-25 21:06:31 +00001267
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001268 wMenuAddCallback(wsmenu, _("New"), newWSCommand, NULL);
1269 wMenuAddCallback(wsmenu, _("Destroy Last"), deleteWSCommand, NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001270
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001271 return wsmenu;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001272}
1273
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001274void wWorkspaceMenuUpdate(WScreen * scr, WMenu * menu)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001275{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001276 int i;
1277 long ws;
1278 char title[MAX_WORKSPACENAME_WIDTH + 1];
1279 WMenuEntry *entry;
1280 int tmp;
1281
1282 if (!menu)
1283 return;
1284
1285 if (menu->entry_no < scr->workspace_count + 2) {
1286 /* new workspace(s) added */
1287 i = scr->workspace_count - (menu->entry_no - 2);
1288 ws = menu->entry_no - 2;
1289 while (i > 0) {
1290 strncpy(title, scr->workspaces[ws]->name, MAX_WORKSPACENAME_WIDTH);
1291
1292 entry = wMenuAddCallback(menu, title, switchWSCommand, (void *)ws);
1293 entry->flags.indicator = 1;
1294 entry->flags.editable = 1;
1295
1296 i--;
1297 ws++;
1298 }
1299 } else if (menu->entry_no > scr->workspace_count + 2) {
1300 /* removed workspace(s) */
1301 for (i = menu->entry_no - 1; i >= scr->workspace_count + 2; i--) {
1302 wMenuRemoveItem(menu, i);
1303 }
1304 }
1305 wMenuRealize(menu);
1306
1307 for (i = 0; i < scr->workspace_count; i++) {
1308 menu->entries[i + 2]->flags.indicator_on = 0;
1309 }
1310 menu->entries[scr->current_workspace + 2]->flags.indicator_on = 1;
1311
1312 /* don't let user destroy current workspace */
1313 if (scr->current_workspace == scr->workspace_count - 1) {
1314 wMenuSetEnabled(menu, 1, False);
1315 } else {
1316 wMenuSetEnabled(menu, 1, True);
1317 }
1318
1319 tmp = menu->frame->top_width + 5;
1320 /* if menu got unreachable, bring it to a visible place */
1321 if (menu->frame_x < tmp - (int)menu->frame->core->width)
1322 wMenuMove(menu, tmp - (int)menu->frame->core->width, menu->frame_y, False);
1323
1324 wMenuPaint(menu);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001325}
1326
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001327void wWorkspaceSaveState(WScreen * scr, WMPropList * old_state)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001328{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001329 WMPropList *parr, *pstr, *wks_state, *old_wks_state, *foo, *bar;
1330 int i;
1331
1332 make_keys();
1333
1334 old_wks_state = WMGetFromPLDictionary(old_state, dWorkspaces);
1335 parr = WMCreatePLArray(NULL);
1336 for (i = 0; i < scr->workspace_count; i++) {
1337 pstr = WMCreatePLString(scr->workspaces[i]->name);
1338 wks_state = WMCreatePLDictionary(dName, pstr, NULL);
1339 WMReleasePropList(pstr);
1340 if (!wPreferences.flags.noclip) {
1341 pstr = wClipSaveWorkspaceState(scr, i);
1342 WMPutInPLDictionary(wks_state, dClip, pstr);
1343 WMReleasePropList(pstr);
1344 } else if (old_wks_state != NULL) {
1345 if ((foo = WMGetFromPLArray(old_wks_state, i)) != NULL) {
1346 if ((bar = WMGetFromPLDictionary(foo, dClip)) != NULL) {
1347 WMPutInPLDictionary(wks_state, dClip, bar);
1348 }
1349 }
1350 }
1351 WMAddToPLArray(parr, wks_state);
1352 WMReleasePropList(wks_state);
1353 }
1354 WMPutInPLDictionary(scr->session_state, dWorkspaces, parr);
1355 WMReleasePropList(parr);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001356}
1357
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001358void wWorkspaceRestoreState(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001359{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001360 WMPropList *parr, *pstr, *wks_state, *clip_state;
1361 int i, j, wscount;
1362
1363 make_keys();
1364
1365 if (scr->session_state == NULL)
1366 return;
1367
1368 parr = WMGetFromPLDictionary(scr->session_state, dWorkspaces);
1369
1370 if (!parr)
1371 return;
1372
1373 wscount = scr->workspace_count;
1374 for (i = 0; i < WMIN(WMGetPropListItemCount(parr), MAX_WORKSPACES); i++) {
1375 wks_state = WMGetFromPLArray(parr, i);
1376 if (WMIsPLDictionary(wks_state))
1377 pstr = WMGetFromPLDictionary(wks_state, dName);
1378 else
1379 pstr = wks_state;
1380 if (i >= scr->workspace_count)
1381 wWorkspaceNew(scr);
1382 if (scr->workspace_menu) {
1383 wfree(scr->workspace_menu->entries[i + 2]->text);
1384 scr->workspace_menu->entries[i + 2]->text = wstrdup(WMGetFromPLString(pstr));
1385 scr->workspace_menu->flags.realized = 0;
1386 }
1387 wfree(scr->workspaces[i]->name);
1388 scr->workspaces[i]->name = wstrdup(WMGetFromPLString(pstr));
1389 if (!wPreferences.flags.noclip) {
1390 clip_state = WMGetFromPLDictionary(wks_state, dClip);
1391 if (scr->workspaces[i]->clip)
1392 wDockDestroy(scr->workspaces[i]->clip);
1393 scr->workspaces[i]->clip = wDockRestoreState(scr, clip_state, WM_CLIP);
1394 if (i > 0)
1395 wDockHideIcons(scr->workspaces[i]->clip);
1396
1397 /* We set the global icons here, because scr->workspaces[i]->clip
1398 * was not valid in wDockRestoreState().
1399 * There we only set icon->omnipresent to know which icons we
1400 * need to set here.
1401 */
1402 for (j = 0; j < scr->workspaces[i]->clip->max_icons; j++) {
1403 WAppIcon *aicon = scr->workspaces[i]->clip->icon_array[j];
1404
1405 if (aicon && aicon->omnipresent) {
1406 aicon->omnipresent = 0;
1407 wClipMakeIconOmnipresent(aicon, True);
1408 XMapWindow(dpy, aicon->icon->core->window);
1409 }
1410 }
1411 }
1412
1413 WMPostNotificationName(WMNWorkspaceNameChanged, scr, (void *)(uintptr_t) i);
1414 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001415}