1 /* dock.c- built-in Dock module for WindowMaker
2  *
3  *  Window Maker window manager
4  *
5  *  Copyright (c) 1997-2003 Alfredo K. Kojima
6  *  Copyright (c) 1998-2003 Dan Pascu
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License along
19  *  with this program; if not, write to the Free Software Foundation, Inc.,
20  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include "wconfig.h"
24 
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <libgen.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <unistd.h>
33 #include <math.h>
34 #include <limits.h>
35 
36 #ifndef PATH_MAX
37 #define PATH_MAX DEFAULT_PATH_MAX
38 #endif
39 
40 #include "WindowMaker.h"
41 #include "wcore.h"
42 #include "window.h"
43 #include "icon.h"
44 #include "appicon.h"
45 #include "actions.h"
46 #include "stacking.h"
47 #include "dock.h"
48 #include "dockedapp.h"
49 #include "dialog.h"
50 #include "main.h"
51 #include "properties.h"
52 #include "menu.h"
53 #include "client.h"
54 #include "defaults.h"
55 #include "workspace.h"
56 #include "framewin.h"
57 #include "superfluous.h"
58 #include "xinerama.h"
59 #include "placement.h"
60 #include "misc.h"
61 #include "event.h"
62 
63 /**** Local variables ****/
64 #define CLIP_REWIND       1
65 #define CLIP_IDLE         0
66 #define CLIP_FORWARD      2
67 
68 #define MOD_MASK wPreferences.modifier_mask
69 #define ICON_SIZE wPreferences.icon_size
70 
71 /***** Local variables ****/
72 
73 static WMPropList *dCommand = NULL;
74 static WMPropList *dPasteCommand = NULL;
75 #ifdef USE_DOCK_XDND
76 static WMPropList *dDropCommand = NULL;
77 #endif
78 static WMPropList *dAutoLaunch, *dLock;
79 static WMPropList *dName, *dForced, *dBuggyApplication, *dYes, *dNo;
80 static WMPropList *dHost, *dDock, *dClip;
81 static WMPropList *dAutoAttractIcons;
82 
83 static WMPropList *dPosition, *dApplications, *dLowered, *dCollapsed;
84 
85 static WMPropList *dAutoCollapse, *dAutoRaiseLower, *dOmnipresent;
86 
87 static WMPropList *dDrawers = NULL;
88 
89 static void dockIconPaint(WAppIcon *btn);
90 
91 static void iconMouseDown(WObjDescriptor *desc, XEvent *event);
92 
93 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state);
94 
95 static void trackDeadProcess(pid_t pid, unsigned int status, void *cdata);
96 
97 static int getClipButton(int px, int py);
98 
99 static void toggleLowered(WDock *dock);
100 
101 static void toggleCollapsed(WDock *dock);
102 
103 static void clipIconExpose(WObjDescriptor *desc, XEvent *event);
104 
105 static void clipLeave(WDock *dock);
106 
107 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event);
108 
109 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event);
110 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event);
111 static void clipAutoCollapse(void *cdata);
112 static void clipAutoExpand(void *cdata);
113 static void launchDockedApplication(WAppIcon *btn, Bool withSelection);
114 
115 static void clipAutoLower(void *cdata);
116 static void clipAutoRaise(void *cdata);
117 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name);
118 
119 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event);
120 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry);
121 static void drawerAppendToChain(WScreen *scr, WDock *drawer);
122 static char *findUniqueName(WScreen *scr, const char *instance_basename);
123 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry);
124 static void swapDrawers(WScreen *scr, int new_x);
125 static WDock* getDrawer(WScreen *scr, int y_index);
126 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking);
127 static void drawerConsolidateIcons(WDock *drawer);
128 
129 static int onScreen(WScreen *scr, int x, int y);
130 
make_keys(void)131 static void make_keys(void)
132 {
133 	if (dCommand != NULL)
134 		return;
135 
136 	dCommand = WMRetainPropList(WMCreatePLString("Command"));
137 	dPasteCommand = WMRetainPropList(WMCreatePLString("PasteCommand"));
138 #ifdef USE_DOCK_XDND
139 	dDropCommand = WMRetainPropList(WMCreatePLString("DropCommand"));
140 #endif
141 	dLock = WMRetainPropList(WMCreatePLString("Lock"));
142 	dAutoLaunch = WMRetainPropList(WMCreatePLString("AutoLaunch"));
143 	dName = WMRetainPropList(WMCreatePLString("Name"));
144 	dForced = WMRetainPropList(WMCreatePLString("Forced"));
145 	dBuggyApplication = WMRetainPropList(WMCreatePLString("BuggyApplication"));
146 	dYes = WMRetainPropList(WMCreatePLString("Yes"));
147 	dNo = WMRetainPropList(WMCreatePLString("No"));
148 	dHost = WMRetainPropList(WMCreatePLString("Host"));
149 
150 	dPosition = WMCreatePLString("Position");
151 	dApplications = WMCreatePLString("Applications");
152 	dLowered = WMCreatePLString("Lowered");
153 	dCollapsed = WMCreatePLString("Collapsed");
154 	dAutoCollapse = WMCreatePLString("AutoCollapse");
155 	dAutoRaiseLower = WMCreatePLString("AutoRaiseLower");
156 	dAutoAttractIcons = WMCreatePLString("AutoAttractIcons");
157 
158 	dOmnipresent = WMCreatePLString("Omnipresent");
159 
160 	dDock = WMCreatePLString("Dock");
161 	dClip = WMCreatePLString("Clip");
162 	dDrawers = WMCreatePLString("Drawers");
163 }
164 
renameCallback(WMenu * menu,WMenuEntry * entry)165 static void renameCallback(WMenu *menu, WMenuEntry *entry)
166 {
167 	WDock *dock = entry->clientdata;
168 	char buffer[128];
169 	int wspace;
170 	char *name;
171 
172 	/* Parameter not used, but tell the compiler that it is ok */
173 	(void) menu;
174 
175 	assert(entry->clientdata != NULL);
176 
177 	wspace = dock->screen_ptr->current_workspace;
178 
179 	name = wstrdup(dock->screen_ptr->workspaces[wspace]->name);
180 
181 	snprintf(buffer, sizeof(buffer), _("Type the name for workspace %i:"), wspace + 1);
182 	if (wInputDialog(dock->screen_ptr, _("Rename Workspace"), buffer, &name))
183 		wWorkspaceRename(dock->screen_ptr, wspace, name);
184 
185 	wfree(name);
186 }
187 
toggleLoweredCallback(WMenu * menu,WMenuEntry * entry)188 static void toggleLoweredCallback(WMenu *menu, WMenuEntry *entry)
189 {
190 	assert(entry->clientdata != NULL);
191 
192 	toggleLowered(entry->clientdata);
193 
194 	entry->flags.indicator_on = !(((WDock *) entry->clientdata)->lowered);
195 
196 	wMenuPaint(menu);
197 }
198 
matchWindow(const void * item,const void * cdata)199 static int matchWindow(const void *item, const void *cdata)
200 {
201 	return (((WFakeGroupLeader *) item)->leader == (Window) cdata);
202 }
203 
killCallback(WMenu * menu,WMenuEntry * entry)204 static void killCallback(WMenu *menu, WMenuEntry *entry)
205 {
206 	WScreen *scr = menu->menu->screen_ptr;
207 	WAppIcon *icon;
208 	WFakeGroupLeader *fPtr;
209 	char *buffer, *shortname, **argv;
210 	int argc;
211 
212 	if (!WCHECK_STATE(WSTATE_NORMAL))
213 		return;
214 
215 	assert(entry->clientdata != NULL);
216 
217 	icon = (WAppIcon *) entry->clientdata;
218 
219 	icon->editing = 1;
220 
221 	WCHANGE_STATE(WSTATE_MODAL);
222 
223 	/* strip away dir names */
224 	shortname = basename(icon->command);
225 	/* separate out command options */
226 	wtokensplit(shortname, &argv, &argc);
227 
228 	buffer = wstrconcat(argv[0],
229 			    _(" will be forcibly closed.\n"
230 			      "Any unsaved changes will be lost.\n" "Please confirm."));
231 
232 	if (icon->icon && icon->icon->owner) {
233 		fPtr = icon->icon->owner->fake_group;
234 	} else {
235 		/* is this really necessary? can we kill a non-running dock icon? */
236 		Window win = icon->main_window;
237 		int index;
238 
239 		index = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)win);
240 		if (index != WANotFound)
241 			fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
242 		else
243 			fPtr = NULL;
244 	}
245 
246 	if (wPreferences.dont_confirm_kill
247 	    || wMessageDialog(menu->frame->screen_ptr, _("Kill Application"),
248 			      buffer, _("Yes"), _("No"), NULL) == WAPRDefault) {
249 		if (fPtr != NULL) {
250 			WWindow *wwin, *twin;
251 
252 			wwin = scr->focused_window;
253 			while (wwin) {
254 				twin = wwin->prev;
255 				if (wwin->fake_group == fPtr)
256 					wClientKill(wwin);
257 
258 				wwin = twin;
259 			}
260 		} else if (icon->icon && icon->icon->owner) {
261 			wClientKill(icon->icon->owner);
262 		}
263 	}
264 
265 	wfree(buffer);
266 	wtokenfree(argv, argc);
267 
268 	icon->editing = 0;
269 
270 	WCHANGE_STATE(WSTATE_NORMAL);
271 }
272 
273 /* TODO: replace this function with a member of the dock struct */
numberOfSelectedIcons(WDock * dock)274 static int numberOfSelectedIcons(WDock *dock)
275 {
276 	WAppIcon *aicon;
277 	int i, n;
278 
279 	n = 0;
280 	for (i = 1; i < dock->max_icons; i++) {
281 		aicon = dock->icon_array[i];
282 		if (aicon && aicon->icon->selected)
283 			n++;
284 	}
285 
286 	return n;
287 }
288 
getSelected(WDock * dock)289 static WMArray *getSelected(WDock *dock)
290 {
291 	WMArray *ret = WMCreateArray(8);
292 	WAppIcon *btn;
293 	int i;
294 
295 	for (i = 1; i < dock->max_icons; i++) {
296 		btn = dock->icon_array[i];
297 		if (btn && btn->icon->selected)
298 			WMAddToArray(ret, btn);
299 	}
300 
301 	return ret;
302 }
303 
paintClipButtons(WAppIcon * clipIcon,Bool lpushed,Bool rpushed)304 static void paintClipButtons(WAppIcon *clipIcon, Bool lpushed, Bool rpushed)
305 {
306 	Window win = clipIcon->icon->core->window;
307 	WScreen *scr = clipIcon->icon->core->screen_ptr;
308 	XPoint p[4];
309 	int pt = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
310 	int tp = ICON_SIZE - pt;
311 	int as = pt - 15;	/* 15 = 5+5+5 */
312 	GC gc = scr->draw_gc;	/* maybe use WMColorGC() instead here? */
313 	WMColor *color;
314 
315 	color = scr->clip_title_color[CLIP_NORMAL];
316 
317 	XSetForeground(dpy, gc, WMColorPixel(color));
318 
319 	if (rpushed) {
320 		p[0].x = tp + 1;
321 		p[0].y = 1;
322 		p[1].x = ICON_SIZE - 2;
323 		p[1].y = 1;
324 		p[2].x = ICON_SIZE - 2;
325 		p[2].y = pt - 1;
326 	} else if (lpushed) {
327 		p[0].x = 1;
328 		p[0].y = tp;
329 		p[1].x = pt;
330 		p[1].y = ICON_SIZE - 2;
331 		p[2].x = 1;
332 		p[2].y = ICON_SIZE - 2;
333 	}
334 	if (lpushed || rpushed) {
335 		XSetForeground(dpy, scr->draw_gc, scr->white_pixel);
336 		XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
337 		XSetForeground(dpy, scr->draw_gc, scr->black_pixel);
338 	}
339 
340 	/* top right arrow */
341 	p[0].x = p[3].x = ICON_SIZE - 5 - as;
342 	p[0].y = p[3].y = 5;
343 	p[1].x = ICON_SIZE - 6;
344 	p[1].y = 5;
345 	p[2].x = ICON_SIZE - 6;
346 	p[2].y = 4 + as;
347 	if (rpushed) {
348 		XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
349 		XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
350 	} else {
351 		XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
352 		XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
353 	}
354 
355 	/* bottom left arrow */
356 	p[0].x = p[3].x = 5;
357 	p[0].y = p[3].y = ICON_SIZE - 5 - as;
358 	p[1].x = 5;
359 	p[1].y = ICON_SIZE - 6;
360 	p[2].x = 4 + as;
361 	p[2].y = ICON_SIZE - 6;
362 	if (lpushed) {
363 		XFillPolygon(dpy, win, scr->draw_gc, p, 3, Convex, CoordModeOrigin);
364 		XDrawLines(dpy, win, scr->draw_gc, p, 4, CoordModeOrigin);
365 	} else {
366 		XFillPolygon(dpy, win, gc, p, 3, Convex, CoordModeOrigin);
367 		XDrawLines(dpy, win, gc, p, 4, CoordModeOrigin);
368 	}
369 }
370 
wClipMakeTile(RImage * normalTile)371 RImage *wClipMakeTile(RImage *normalTile)
372 {
373 	RImage *tile = RCloneImage(normalTile);
374 	RColor black;
375 	RColor dark;
376 	RColor light;
377 	int pt, tp;
378 	int as;
379 
380 	pt = CLIP_BUTTON_SIZE * wPreferences.icon_size / 64;
381 	tp = wPreferences.icon_size - 1 - pt;
382 	as = pt - 15;
383 
384 	black.alpha = 255;
385 	black.red = black.green = black.blue = 0;
386 
387 	dark.alpha = 0;
388 	dark.red = dark.green = dark.blue = 60;
389 
390 	light.alpha = 0;
391 	light.red = light.green = light.blue = 80;
392 
393 	/* top right */
394 	ROperateLine(tile, RSubtractOperation, tp, 0, wPreferences.icon_size - 2, pt - 1, &dark);
395 	RDrawLine(tile, tp - 1, 0, wPreferences.icon_size - 1, pt + 1, &black);
396 	ROperateLine(tile, RAddOperation, tp, 2, wPreferences.icon_size - 3, pt, &light);
397 
398 	/* arrow bevel */
399 	ROperateLine(tile, RSubtractOperation, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, 4, &dark);
400 	ROperateLine(tile, RSubtractOperation, ICON_SIZE - 6 - as, 5, ICON_SIZE - 5, 6 + as, &dark);
401 	ROperateLine(tile, RAddOperation, ICON_SIZE - 5, 4, ICON_SIZE - 5, 6 + as, &light);
402 
403 	/* bottom left */
404 	ROperateLine(tile, RAddOperation, 2, tp + 2, pt - 2, wPreferences.icon_size - 3, &dark);
405 	RDrawLine(tile, 0, tp - 1, pt + 1, wPreferences.icon_size - 1, &black);
406 	ROperateLine(tile, RSubtractOperation, 0, tp - 2, pt + 1, wPreferences.icon_size - 2, &light);
407 
408 	/* arrow bevel */
409 	ROperateLine(tile, RSubtractOperation, 4, ICON_SIZE - 7 - as, 4, ICON_SIZE - 5, &dark);
410 	ROperateLine(tile, RSubtractOperation, 5, ICON_SIZE - 6 - as, 6 + as, ICON_SIZE - 5, &dark);
411 	ROperateLine(tile, RAddOperation, 4, ICON_SIZE - 5, 6 + as, ICON_SIZE - 5, &light);
412 
413 	return tile;
414 }
415 
omnipresentCallback(WMenu * menu,WMenuEntry * entry)416 static void omnipresentCallback(WMenu *menu, WMenuEntry *entry)
417 {
418 	WAppIcon *clickedIcon = entry->clientdata;
419 	WAppIcon *aicon;
420 	WDock *dock;
421 	WMArray *selectedIcons;
422 	WMArrayIterator iter;
423 	int failed;
424 
425 	/* Parameter not used, but tell the compiler that it is ok */
426 	(void) menu;
427 
428 	assert(entry->clientdata != NULL);
429 
430 	dock = clickedIcon->dock;
431 
432 	selectedIcons = getSelected(dock);
433 
434 	if (!WMGetArrayItemCount(selectedIcons))
435 		WMAddToArray(selectedIcons, clickedIcon);
436 
437 	failed = 0;
438 	WM_ITERATE_ARRAY(selectedIcons, aicon, iter) {
439 		if (wClipMakeIconOmnipresent(aicon, !aicon->omnipresent) == WO_FAILED)
440 			failed++;
441 		else if (aicon->icon->selected)
442 			wIconSelect(aicon->icon);
443 	}
444 	WMFreeArray(selectedIcons);
445 
446 	if (failed > 1) {
447 		wMessageDialog(dock->screen_ptr, _("Warning"),
448 			       _("Some icons cannot be made omnipresent. "
449 				 "Please make sure that no other icon is "
450 				 "docked in the same positions on the other "
451 				 "workspaces and the Clip is not full in "
452 				 "some workspace."), _("OK"), NULL, NULL);
453 	} else if (failed == 1) {
454 		wMessageDialog(dock->screen_ptr, _("Warning"),
455 			       _("Icon cannot be made omnipresent. "
456 				 "Please make sure that no other icon is "
457 				 "docked in the same position on the other "
458 				 "workspaces and the Clip is not full in "
459 				 "some workspace."), _("OK"), NULL, NULL);
460 	}
461 }
462 
removeIcons(WMArray * icons,WDock * dock)463 static void removeIcons(WMArray *icons, WDock *dock)
464 {
465 	WAppIcon *aicon;
466 	int keepit;
467 	WMArrayIterator it;
468 
469 	WM_ITERATE_ARRAY(icons, aicon, it) {
470 		keepit = aicon->running && wApplicationOf(aicon->main_window);
471 		wDockDetach(dock, aicon);
472 		if (keepit) {
473 			/* XXX: can: aicon->icon == NULL ? */
474 			PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
475 				wGetHeadForWindow(aicon->icon->owner));
476 			XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
477 			if (!dock->mapped || dock->collapsed)
478 				XMapWindow(dpy, aicon->icon->core->window);
479 		}
480 	}
481 	WMFreeArray(icons);
482 
483 	if (wPreferences.auto_arrange_icons)
484 		wArrangeIcons(dock->screen_ptr, True);
485 }
486 
removeIconsCallback(WMenu * menu,WMenuEntry * entry)487 static void removeIconsCallback(WMenu *menu, WMenuEntry *entry)
488 {
489 	WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
490 	WDock *dock;
491 	WMArray *selectedIcons;
492 
493 	/* Parameter not used, but tell the compiler that it is ok */
494 	(void) menu;
495 
496 	assert(clickedIcon != NULL);
497 
498 	dock = clickedIcon->dock;
499 
500 	selectedIcons = getSelected(dock);
501 
502 	if (WMGetArrayItemCount(selectedIcons)) {
503 		if (wMessageDialog(dock->screen_ptr,
504 					dock->type == WM_CLIP ? _("Workspace Clip") : _("Drawer"),
505 					_("All selected icons will be removed!"),
506 					_("OK"), _("Cancel"), NULL) != WAPRDefault) {
507 			WMFreeArray(selectedIcons);
508 			return;
509 		}
510 	} else {
511 		if (clickedIcon->xindex == 0 && clickedIcon->yindex == 0) {
512 			WMFreeArray(selectedIcons);
513 			return;
514 		}
515 		WMAddToArray(selectedIcons, clickedIcon);
516 	}
517 
518 	removeIcons(selectedIcons, dock);
519 
520 	if (dock->type == WM_DRAWER) {
521 		drawerConsolidateIcons(dock);
522 	}
523 }
524 
keepIconsCallback(WMenu * menu,WMenuEntry * entry)525 static void keepIconsCallback(WMenu *menu, WMenuEntry *entry)
526 {
527 	WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
528 	WDock *dock;
529 	WAppIcon *aicon;
530 	WMArray *selectedIcons;
531 	WMArrayIterator it;
532 
533 	/* Parameter not used, but tell the compiler that it is ok */
534 	(void) menu;
535 
536 	assert(clickedIcon != NULL);
537 	dock = clickedIcon->dock;
538 
539 	selectedIcons = getSelected(dock);
540 
541 	if (!WMGetArrayItemCount(selectedIcons)
542 	    && clickedIcon != dock->screen_ptr->clip_icon) {
543 		char *command = NULL;
544 
545 		if (!clickedIcon->command && !clickedIcon->editing) {
546 			clickedIcon->editing = 1;
547 			if (wInputDialog(dock->screen_ptr, _("Keep Icon"),
548 					 _("Type the command used to launch the application"), &command)) {
549 				if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
550 					wfree(command);
551 					command = NULL;
552 				}
553 				clickedIcon->command = command;
554 				clickedIcon->editing = 0;
555 			} else {
556 				clickedIcon->editing = 0;
557 				if (command)
558 					wfree(command);
559 				WMFreeArray(selectedIcons);
560 				return;
561 			}
562 		}
563 
564 		WMAddToArray(selectedIcons, clickedIcon);
565 	}
566 
567 	WM_ITERATE_ARRAY(selectedIcons, aicon, it) {
568 		if (aicon->icon->selected)
569 			wIconSelect(aicon->icon);
570 
571 		if (aicon->attracted && aicon->command) {
572 			aicon->attracted = 0;
573 			if (aicon->icon->shadowed) {
574 				aicon->icon->shadowed = 0;
575 
576 				/*
577 				 * Update icon pixmap, RImage doesn't change,
578 				 * so call wIconUpdate is not needed
579 				 */
580 				update_icon_pixmap(aicon->icon);
581 
582 				/* Paint it */
583 				wAppIconPaint(aicon);
584 			}
585 		}
586 		save_appicon(aicon);
587 	}
588 	WMFreeArray(selectedIcons);
589 }
590 
toggleAutoAttractCallback(WMenu * menu,WMenuEntry * entry)591 static void toggleAutoAttractCallback(WMenu *menu, WMenuEntry *entry)
592 {
593 	WDock *dock = (WDock *) entry->clientdata;
594 	WScreen *scr = dock->screen_ptr;
595 
596 	assert(entry->clientdata != NULL);
597 
598 	dock->attract_icons = !dock->attract_icons;
599 
600 	entry->flags.indicator_on = dock->attract_icons;
601 
602 	wMenuPaint(menu);
603 
604 	if (dock->attract_icons) {
605 		if (dock->type == WM_DRAWER) {
606 			/* The newly auto-attracting dock is a drawer: disable any clip and
607 			 * previously attracting drawer */
608 
609 			if (!wPreferences.flags.noclip) {
610 				int i;
611 				for (i = 0; i < scr->workspace_count; i++)
612 					scr->workspaces[ i ]->clip->attract_icons = False;
613 					/* dock menu will be updated later, when opened */
614 			}
615 
616 			if (scr->attracting_drawer != NULL)
617 				scr->attracting_drawer->attract_icons = False;
618 			scr->attracting_drawer = dock;
619 		} else {
620 			/* The newly auto-attracting dock is a clip: disable
621 			 * previously attracting drawer, if applicable */
622 			if (scr->attracting_drawer != NULL) {
623 				scr->attracting_drawer->attract_icons = False;
624 				/* again, its menu will be updated, later. */
625 				scr->attracting_drawer = NULL;
626 			}
627 		}
628 	}
629 }
630 
selectCallback(WMenu * menu,WMenuEntry * entry)631 static void selectCallback(WMenu *menu, WMenuEntry *entry)
632 {
633 	WAppIcon *icon = (WAppIcon *) entry->clientdata;
634 
635 	assert(icon != NULL);
636 
637 	wIconSelect(icon->icon);
638 
639 	wMenuPaint(menu);
640 }
641 
attractIconsCallback(WMenu * menu,WMenuEntry * entry)642 static void attractIconsCallback(WMenu *menu, WMenuEntry *entry)
643 {
644 	WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
645 	WDock *clip; /* clip... is a WM_CLIP or a WM_DRAWER */
646 	WAppIcon *aicon;
647 	int x, y, x_pos, y_pos;
648 	Bool update_icon = False;
649 
650 	/* Parameter not used, but tell the compiler that it is ok */
651 	(void) menu;
652 
653 	assert(entry->clientdata != NULL);
654 	clip = clickedIcon->dock;
655 
656 	aicon = clip->screen_ptr->app_icon_list;
657 
658 	while (aicon) {
659 		if (!aicon->docked && wDockFindFreeSlot(clip, &x, &y)) {
660 			x_pos = clip->x_pos + x * ICON_SIZE;
661 			y_pos = clip->y_pos + y * ICON_SIZE;
662 			if (aicon->x_pos != x_pos || aicon->y_pos != y_pos)
663 				move_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, x_pos, y_pos);
664 
665 			aicon->attracted = 1;
666 			if (!aicon->icon->shadowed) {
667 				aicon->icon->shadowed = 1;
668 				update_icon = True;
669 			}
670 			wDockAttachIcon(clip, aicon, x, y, update_icon);
671 			if (clip->collapsed || !clip->mapped)
672 				XUnmapWindow(dpy, aicon->icon->core->window);
673 		}
674 		aicon = aicon->next;
675 	}
676 }
677 
selectIconsCallback(WMenu * menu,WMenuEntry * entry)678 static void selectIconsCallback(WMenu *menu, WMenuEntry *entry)
679 {
680 	WAppIcon *clickedIcon = (WAppIcon *) entry->clientdata;
681 	WDock *dock;
682 	WMArray *selectedIcons;
683 	WMArrayIterator iter;
684 	WAppIcon *btn;
685 	int i;
686 
687 	assert(clickedIcon != NULL);
688 	dock = clickedIcon->dock;
689 
690 	selectedIcons = getSelected(dock);
691 
692 	if (!WMGetArrayItemCount(selectedIcons)) {
693 		for (i = 1; i < dock->max_icons; i++) {
694 			btn = dock->icon_array[i];
695 			if (btn && !btn->icon->selected)
696 				wIconSelect(btn->icon);
697 		}
698 	} else {
699 		WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
700 			wIconSelect(btn->icon);
701 		}
702 	}
703 	WMFreeArray(selectedIcons);
704 
705 	wMenuPaint(menu);
706 }
707 
toggleCollapsedCallback(WMenu * menu,WMenuEntry * entry)708 static void toggleCollapsedCallback(WMenu *menu, WMenuEntry *entry)
709 {
710 	assert(entry->clientdata != NULL);
711 
712 	toggleCollapsed(entry->clientdata);
713 
714 	entry->flags.indicator_on = ((WDock *) entry->clientdata)->collapsed;
715 
716 	wMenuPaint(menu);
717 }
718 
toggleAutoCollapseCallback(WMenu * menu,WMenuEntry * entry)719 static void toggleAutoCollapseCallback(WMenu *menu, WMenuEntry *entry)
720 {
721 	WDock *dock;
722 	assert(entry->clientdata != NULL);
723 
724 	dock = (WDock *) entry->clientdata;
725 
726 	dock->auto_collapse = !dock->auto_collapse;
727 
728 	entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_collapse;
729 
730 	wMenuPaint(menu);
731 }
732 
toggleAutoRaiseLower(WDock * dock)733 static void toggleAutoRaiseLower(WDock *dock)
734 {
735 	WDrawerChain *dc;
736 
737 	dock->auto_raise_lower = !dock->auto_raise_lower;
738 	if (dock->type == WM_DOCK)
739 	{
740 		for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
741 			toggleAutoRaiseLower(dc->adrawer);
742 		}
743 	}
744 }
745 
toggleAutoRaiseLowerCallback(WMenu * menu,WMenuEntry * entry)746 static void toggleAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
747 {
748 	WDock *dock;
749 
750 	/* Parameter not used, but tell the compiler that it is ok */
751 	(void) menu;
752 
753 	assert(entry->clientdata != NULL);
754 
755 	dock = (WDock *) entry->clientdata;
756 
757 	toggleAutoRaiseLower(dock);
758 
759 	entry->flags.indicator_on = ((WDock *) entry->clientdata)->auto_raise_lower;
760 
761 	wMenuPaint(menu);
762 }
763 
launchCallback(WMenu * menu,WMenuEntry * entry)764 static void launchCallback(WMenu *menu, WMenuEntry *entry)
765 {
766 	WAppIcon *btn = (WAppIcon *) entry->clientdata;
767 
768 	/* Parameter not used, but tell the compiler that it is ok */
769 	(void) menu;
770 
771 	launchDockedApplication(btn, False);
772 }
773 
settingsCallback(WMenu * menu,WMenuEntry * entry)774 static void settingsCallback(WMenu *menu, WMenuEntry *entry)
775 {
776 	WAppIcon *btn = (WAppIcon *) entry->clientdata;
777 
778 	/* Parameter not used, but tell the compiler that it is ok */
779 	(void) menu;
780 
781 	if (btn->editing)
782 		return;
783 	ShowDockAppSettingsPanel(btn);
784 }
785 
hideCallback(WMenu * menu,WMenuEntry * entry)786 static void hideCallback(WMenu *menu, WMenuEntry *entry)
787 {
788 	WApplication *wapp;
789 	WAppIcon *btn = (WAppIcon *) entry->clientdata;
790 
791 	/* Parameter not used, but tell the compiler that it is ok */
792 	(void) menu;
793 
794 	wapp = wApplicationOf(btn->icon->owner->main_window);
795 
796 	if (wapp->flags.hidden) {
797 		wWorkspaceChange(btn->icon->core->screen_ptr, wapp->last_workspace);
798 		wUnhideApplication(wapp, False, False);
799 	} else {
800 		wHideApplication(wapp);
801 	}
802 }
803 
unhideHereCallback(WMenu * menu,WMenuEntry * entry)804 static void unhideHereCallback(WMenu *menu, WMenuEntry *entry)
805 {
806 	WApplication *wapp;
807 	WAppIcon *btn = (WAppIcon *) entry->clientdata;
808 
809 	/* Parameter not used, but tell the compiler that it is ok */
810 	(void) menu;
811 
812 	wapp = wApplicationOf(btn->icon->owner->main_window);
813 
814 	wUnhideApplication(wapp, False, True);
815 }
816 
817 /* Name is only used when type == WM_DRAWER and when restoring a specific
818  * drawer, with a specific name. When creating a drawer, leave name to NULL
819  * and mainIconCreate will find the first unused unique name */
mainIconCreate(WScreen * scr,int type,const char * name)820 static WAppIcon *mainIconCreate(WScreen *scr, int type, const char *name)
821 {
822 	WAppIcon *btn;
823 	int x_pos;
824 
825 	switch(type) {
826 	case WM_CLIP:
827 		if (scr->clip_icon)
828 			return scr->clip_icon;
829 
830 		btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMClip", TILE_CLIP);
831 		btn->icon->core->descriptor.handle_expose = clipIconExpose;
832 		x_pos = 0;
833 		break;
834 	case WM_DOCK:
835 	default: /* to avoid a warning about btn and x_pos, basically */
836 		btn = wAppIconCreateForDock(scr, NULL, "Logo", "WMDock", TILE_NORMAL);
837 		if (wPreferences.flags.clip_merged_in_dock)
838 			btn->icon->core->descriptor.handle_expose = clipIconExpose;
839 		x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
840 		break;
841 	case WM_DRAWER:
842 		if (name == NULL)
843 			name = findUniqueName(scr, "Drawer");
844 		btn = wAppIconCreateForDock(scr, NULL, name, "WMDrawer", TILE_DRAWER);
845 		btn->icon->core->descriptor.handle_expose = drawerIconExpose;
846 		x_pos = 0;
847 	}
848 
849 	btn->xindex = 0;
850 	btn->yindex = 0;
851 
852 	btn->icon->core->descriptor.handle_mousedown = iconMouseDown;
853 	btn->icon->core->descriptor.handle_enternotify = clipEnterNotify;
854 	btn->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
855 	btn->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
856 	btn->icon->core->descriptor.parent = btn;
857 	XMapWindow(dpy, btn->icon->core->window);
858 	btn->x_pos = x_pos;
859 	btn->y_pos = 0;
860 	btn->docked = 1;
861 	if (type == WM_CLIP ||
862 		(type == WM_DOCK && wPreferences.flags.clip_merged_in_dock))
863 		scr->clip_icon = btn;
864 
865 	return btn;
866 }
867 
switchWSCommand(WMenu * menu,WMenuEntry * entry)868 static void switchWSCommand(WMenu *menu, WMenuEntry *entry)
869 {
870 	WAppIcon *btn, *icon = (WAppIcon *) entry->clientdata;
871 	WScreen *scr = icon->icon->core->screen_ptr;
872 	WDock *src, *dest;
873 	WMArray *selectedIcons;
874 	int x, y;
875 
876 	/* Parameter not used, but tell the compiler that it is ok */
877 	(void) menu;
878 
879 	if (entry->order == scr->current_workspace)
880 		return;
881 
882 	src = icon->dock;
883 	dest = scr->workspaces[entry->order]->clip;
884 
885 	selectedIcons = getSelected(src);
886 
887 	if (WMGetArrayItemCount(selectedIcons)) {
888 		WMArrayIterator iter;
889 
890 		WM_ITERATE_ARRAY(selectedIcons, btn, iter) {
891 			if (wDockFindFreeSlot(dest, &x, &y)) {
892 				wDockMoveIconBetweenDocks(src, dest, btn, x, y);
893 				XUnmapWindow(dpy, btn->icon->core->window);
894 			}
895 		}
896 	} else if (icon != scr->clip_icon) {
897 		if (wDockFindFreeSlot(dest, &x, &y)) {
898 			wDockMoveIconBetweenDocks(src, dest, icon, x, y);
899 			XUnmapWindow(dpy, icon->icon->core->window);
900 		}
901 	}
902 	WMFreeArray(selectedIcons);
903 }
904 
launchDockedApplication(WAppIcon * btn,Bool withSelection)905 static void launchDockedApplication(WAppIcon *btn, Bool withSelection)
906 {
907 	WScreen *scr = btn->icon->core->screen_ptr;
908 
909 	if (!btn->launching &&
910 	    ((!withSelection && btn->command != NULL) || (withSelection && btn->paste_command != NULL))) {
911 		if (!btn->forced_dock) {
912 			btn->relaunching = btn->running;
913 			btn->running = 1;
914 		}
915 		if (btn->wm_instance || btn->wm_class) {
916 			WWindowAttributes attr;
917 			memset(&attr, 0, sizeof(WWindowAttributes));
918 			wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
919 
920 			if (!attr.no_appicon && !btn->buggy_app)
921 				btn->launching = 1;
922 			else
923 				btn->running = 0;
924 		}
925 		btn->drop_launch = 0;
926 		btn->paste_launch = withSelection;
927 		scr->last_dock = btn->dock;
928 		btn->pid = execCommand(btn, (withSelection ? btn->paste_command : btn->command), NULL);
929 		if (btn->pid > 0) {
930 			if (btn->buggy_app) {
931 				/* give feedback that the app was launched */
932 				btn->launching = 1;
933 				dockIconPaint(btn);
934 				btn->launching = 0;
935 				WMAddTimerHandler(200, (WMCallback *) dockIconPaint, btn);
936 			} else {
937 				dockIconPaint(btn);
938 			}
939 		} else {
940 			wwarning(_("could not launch application %s"), btn->command);
941 			btn->launching = 0;
942 			if (!btn->relaunching)
943 				btn->running = 0;
944 		}
945 	}
946 }
947 
updateWorkspaceMenu(WMenu * menu,WAppIcon * icon)948 static void updateWorkspaceMenu(WMenu *menu, WAppIcon *icon)
949 {
950 	WScreen *scr = menu->frame->screen_ptr;
951 	int i;
952 
953 	if (!menu || !icon)
954 		return;
955 
956 	for (i = 0; i < scr->workspace_count; i++) {
957 		if (i < menu->entry_no) {
958 			if (strcmp(menu->entries[i]->text, scr->workspaces[i]->name) != 0) {
959 				wfree(menu->entries[i]->text);
960 				menu->entries[i]->text = wstrdup(scr->workspaces[i]->name);
961 				menu->flags.realized = 0;
962 			}
963 			menu->entries[i]->clientdata = (void *)icon;
964 		} else {
965 			wMenuAddCallback(menu, scr->workspaces[i]->name, switchWSCommand, (void *)icon);
966 
967 			menu->flags.realized = 0;
968 		}
969 
970 		if (i == scr->current_workspace)
971 			wMenuSetEnabled(menu, i, False);
972 		else
973 			wMenuSetEnabled(menu, i, True);
974 	}
975 
976 	if (!menu->flags.realized)
977 		wMenuRealize(menu);
978 }
979 
makeWorkspaceMenu(WScreen * scr)980 static WMenu *makeWorkspaceMenu(WScreen *scr)
981 {
982 	WMenu *menu;
983 
984 	menu = wMenuCreate(scr, NULL, False);
985 	if (!menu)
986 		wwarning(_("could not create workspace submenu for Clip menu"));
987 
988 	wMenuAddCallback(menu, "", switchWSCommand, (void *)scr->clip_icon);
989 
990 	menu->flags.realized = 0;
991 	wMenuRealize(menu);
992 
993 	return menu;
994 }
995 
updateClipOptionsMenu(WMenu * menu,WDock * dock)996 static void updateClipOptionsMenu(WMenu *menu, WDock *dock)
997 {
998 	WMenuEntry *entry;
999 	int index = 0;
1000 
1001 	if (!menu || !dock)
1002 		return;
1003 
1004 	/* keep on top */
1005 	entry = menu->entries[index];
1006 	entry->flags.indicator_on = !dock->lowered;
1007 	entry->clientdata = dock;
1008 	wMenuSetEnabled(menu, index, dock->type == WM_CLIP);
1009 
1010 	/* collapsed */
1011 	entry = menu->entries[++index];
1012 	entry->flags.indicator_on = dock->collapsed;
1013 	entry->clientdata = dock;
1014 
1015 	/* auto-collapse */
1016 	entry = menu->entries[++index];
1017 	entry->flags.indicator_on = dock->auto_collapse;
1018 	entry->clientdata = dock;
1019 
1020 	/* auto-raise/lower */
1021 	entry = menu->entries[++index];
1022 	entry->flags.indicator_on = dock->auto_raise_lower;
1023 	entry->clientdata = dock;
1024 	wMenuSetEnabled(menu, index, dock->lowered && (dock->type == WM_CLIP));
1025 
1026 	/* attract icons */
1027 	entry = menu->entries[++index];
1028 	entry->flags.indicator_on = dock->attract_icons;
1029 	entry->clientdata = dock;
1030 
1031 	menu->flags.realized = 0;
1032 	wMenuRealize(menu);
1033 }
1034 
1035 
makeClipOptionsMenu(WScreen * scr)1036 static WMenu *makeClipOptionsMenu(WScreen *scr)
1037 {
1038 	WMenu *menu;
1039 	WMenuEntry *entry;
1040 
1041 	menu = wMenuCreate(scr, NULL, False);
1042 	if (!menu) {
1043 		wwarning(_("could not create options submenu for Clip menu"));
1044 		return NULL;
1045 	}
1046 
1047 	entry = wMenuAddCallback(menu, _("Keep on Top"), toggleLoweredCallback, NULL);
1048 	entry->flags.indicator = 1;
1049 	entry->flags.indicator_on = 1;
1050 	entry->flags.indicator_type = MI_CHECK;
1051 
1052 	entry = wMenuAddCallback(menu, _("Collapsed"), toggleCollapsedCallback, NULL);
1053 	entry->flags.indicator = 1;
1054 	entry->flags.indicator_on = 1;
1055 	entry->flags.indicator_type = MI_CHECK;
1056 
1057 	entry = wMenuAddCallback(menu, _("Autocollapse"), toggleAutoCollapseCallback, NULL);
1058 	entry->flags.indicator = 1;
1059 	entry->flags.indicator_on = 1;
1060 	entry->flags.indicator_type = MI_CHECK;
1061 
1062 	entry = wMenuAddCallback(menu, _("Autoraise"), toggleAutoRaiseLowerCallback, NULL);
1063 	entry->flags.indicator = 1;
1064 	entry->flags.indicator_on = 1;
1065 	entry->flags.indicator_type = MI_CHECK;
1066 
1067 	entry = wMenuAddCallback(menu, _("Autoattract Icons"), toggleAutoAttractCallback, NULL);
1068 	entry->flags.indicator = 1;
1069 	entry->flags.indicator_on = 1;
1070 	entry->flags.indicator_type = MI_CHECK;
1071 
1072 	menu->flags.realized = 0;
1073 	wMenuRealize(menu);
1074 
1075 	return menu;
1076 }
1077 
1078 
setDockPositionNormalCallback(WMenu * menu,WMenuEntry * entry)1079 static void setDockPositionNormalCallback(WMenu *menu, WMenuEntry *entry)
1080 {
1081 	WDock *dock = (WDock *) entry->clientdata;
1082 	WDrawerChain *dc;
1083 
1084 	/* Parameter not used, but tell the compiler that it is ok */
1085 	(void) menu;
1086 
1087 	if (entry->flags.indicator_on) // already set, nothing to do
1088 		return;
1089 	// Do we come from auto raise lower or keep on top?
1090 	if (dock->auto_raise_lower)
1091 	{
1092 		dock->auto_raise_lower = 0;
1093 		// Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1094 		for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1095 			dc->adrawer->auto_raise_lower = 0;
1096 		}
1097 	}
1098 	else
1099 	{
1100 		// Will take care of setting lowered = 0 in drawers
1101 		toggleLowered(dock);
1102 	}
1103 	entry->flags.indicator_on = 1;
1104 }
1105 
setDockPositionAutoRaiseLowerCallback(WMenu * menu,WMenuEntry * entry)1106 static void setDockPositionAutoRaiseLowerCallback(WMenu *menu, WMenuEntry *entry)
1107 {
1108 	WDock *dock = (WDock *) entry->clientdata;
1109 	WDrawerChain *dc;
1110 
1111 	/* Parameter not used, but tell the compiler that it is ok */
1112 	(void) menu;
1113 
1114 	if (entry->flags.indicator_on) // already set, nothing to do
1115 		return;
1116 	// Do we come from normal or keep on top?
1117 	if (!dock->lowered)
1118 	{
1119 		toggleLowered(dock);
1120 	}
1121 	dock->auto_raise_lower = 1;
1122 	// Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1123 	for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1124 		dc->adrawer->auto_raise_lower = 1;
1125 	}
1126 	entry->flags.indicator_on = 1;
1127 }
1128 
setDockPositionKeepOnTopCallback(WMenu * menu,WMenuEntry * entry)1129 static void setDockPositionKeepOnTopCallback(WMenu *menu, WMenuEntry *entry)
1130 {
1131 	WDock *dock = (WDock *) entry->clientdata;
1132 	WDrawerChain *dc;
1133 
1134 	/* Parameter not used, but tell the compiler that it is ok */
1135 	(void) menu;
1136 
1137 	if (entry->flags.indicator_on) // already set, nothing to do
1138 		return;
1139 	dock->auto_raise_lower = 0;
1140 	// Only for aesthetic purposes, can be removed when Autoraise status is no longer exposed in drawer option menu
1141 	for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
1142 		dc->adrawer->auto_raise_lower = 0;
1143 	}
1144 	toggleLowered(dock);
1145 	entry->flags.indicator_on = 1;
1146 }
1147 
updateDockPositionMenu(WMenu * menu,WDock * dock)1148 static void updateDockPositionMenu(WMenu *menu, WDock *dock)
1149 {
1150 	WMenuEntry *entry;
1151 	int index = 0;
1152 
1153 	assert(menu);
1154 	assert(dock);
1155 
1156 	/* Normal level */
1157 	entry = menu->entries[index++];
1158 	entry->flags.indicator_on = (dock->lowered && !dock->auto_raise_lower);
1159 	entry->clientdata = dock;
1160 
1161 	/* Auto-raise/lower */
1162 	entry = menu->entries[index++];
1163 	entry->flags.indicator_on = dock->auto_raise_lower;
1164 	entry->clientdata = dock;
1165 
1166 	/* Keep on top */
1167 	entry = menu->entries[index++];
1168 	entry->flags.indicator_on = !dock->lowered;
1169 	entry->clientdata = dock;
1170 }
1171 
makeDockPositionMenu(WScreen * scr)1172 static WMenu *makeDockPositionMenu(WScreen *scr)
1173 {
1174 	/* When calling this, the dock is being created, so scr->dock is still not set
1175 	 * Therefore the callbacks' clientdata and the indicators can't be set,
1176 	 * they will be updated when the dock menu is opened. */
1177 	WMenu *menu;
1178 	WMenuEntry *entry;
1179 
1180 	menu = wMenuCreate(scr, NULL, False);
1181 	if (!menu) {
1182 		wwarning(_("could not create options submenu for dock position menu"));
1183 		return NULL;
1184 	}
1185 
1186 	entry = wMenuAddCallback(menu, _("Normal"), setDockPositionNormalCallback, NULL);
1187 	entry->flags.indicator = 1;
1188 	entry->flags.indicator_type = MI_DIAMOND;
1189 
1190 	entry = wMenuAddCallback(menu, _("Auto raise & lower"), setDockPositionAutoRaiseLowerCallback, NULL);
1191 	entry->flags.indicator = 1;
1192 	entry->flags.indicator_type = MI_DIAMOND;
1193 
1194 	entry = wMenuAddCallback(menu, _("Keep on Top"), setDockPositionKeepOnTopCallback, NULL);
1195 	entry->flags.indicator = 1;
1196 	entry->flags.indicator_type = MI_DIAMOND;
1197 
1198 	menu->flags.realized = 0;
1199 	wMenuRealize(menu);
1200 
1201 	return menu;
1202 }
1203 
1204 
dockMenuCreate(WScreen * scr,int type)1205 static WMenu *dockMenuCreate(WScreen *scr, int type)
1206 {
1207 	WMenu *menu;
1208 	WMenuEntry *entry;
1209 
1210 	if (type == WM_CLIP && scr->clip_menu)
1211 		return scr->clip_menu;
1212 
1213 	if (type == WM_DRAWER && scr->drawer_menu)
1214 		return scr->drawer_menu;
1215 
1216 	menu = wMenuCreate(scr, NULL, False);
1217 	if (type == WM_DOCK) {
1218 		entry = wMenuAddCallback(menu, _("Dock position"), NULL, NULL);
1219 		if (scr->dock_pos_menu == NULL)
1220 			scr->dock_pos_menu = makeDockPositionMenu(scr);
1221 		wMenuEntrySetCascade(menu, entry, scr->dock_pos_menu);
1222 
1223 		if (!wPreferences.flags.nodrawer)
1224 			wMenuAddCallback(menu, _("Add a drawer"), addADrawerCallback, NULL);
1225 
1226 	} else {
1227 		if (type == WM_CLIP)
1228 			entry = wMenuAddCallback(menu, _("Clip Options"), NULL, NULL);
1229 		else /* if (type == WM_DRAWER) */
1230 			entry = wMenuAddCallback(menu, _("Drawer options"), NULL, NULL);
1231 
1232 		if (scr->clip_options == NULL)
1233 			scr->clip_options = makeClipOptionsMenu(scr);
1234 
1235 		wMenuEntrySetCascade(menu, entry, scr->clip_options);
1236 
1237 		 /* The same menu is used for the dock and its appicons. If the menu
1238 		  * entry text is different between the two contexts, or if it can
1239 		  * change depending on some state, free the duplicated string (from
1240 		  * wMenuInsertCallback) and use gettext's string */
1241 		if (type == WM_CLIP) {
1242 			entry = wMenuAddCallback(menu, _("Rename Workspace"), renameCallback, NULL);
1243 			wfree(entry->text);
1244 			entry->text = _("Rename Workspace"); /* can be: (Toggle) Omnipresent */
1245 		}
1246 
1247 		entry = wMenuAddCallback(menu, _("Selected"), selectCallback, NULL);
1248 		entry->flags.indicator = 1;
1249 		entry->flags.indicator_on = 1;
1250 		entry->flags.indicator_type = MI_CHECK;
1251 
1252 		entry = wMenuAddCallback(menu, _("Select All Icons"), selectIconsCallback, NULL);
1253 		wfree(entry->text);
1254 		entry->text = _("Select All Icons"); /* can be: Unselect all icons */
1255 
1256 		entry = wMenuAddCallback(menu, _("Keep Icon"), keepIconsCallback, NULL);
1257 		wfree(entry->text);
1258 		entry->text = _("Keep Icon"); /* can be: Keep Icons */
1259 
1260 		if (type == WM_CLIP) {
1261 			entry = wMenuAddCallback(menu, _("Move Icon To"), NULL, NULL);
1262 			wfree(entry->text);
1263 			entry->text = _("Move Icon To"); /* can be: Move Icons to */
1264 			scr->clip_submenu = makeWorkspaceMenu(scr);
1265 			if (scr->clip_submenu)
1266 				wMenuEntrySetCascade(menu, entry, scr->clip_submenu);
1267 		}
1268 
1269 		entry = wMenuAddCallback(menu, _("Remove Icon"), removeIconsCallback, NULL);
1270 		wfree(entry->text);
1271 		entry->text = _("Remove Icon"); /* can be: Remove Icons */
1272 
1273 		wMenuAddCallback(menu, _("Attract Icons"), attractIconsCallback, NULL);
1274 	}
1275 
1276 	wMenuAddCallback(menu, _("Launch"), launchCallback, NULL);
1277 
1278 	wMenuAddCallback(menu, _("Unhide Here"), unhideHereCallback, NULL);
1279 
1280 	entry = wMenuAddCallback(menu, _("Hide"), hideCallback, NULL);
1281 	wfree(entry->text);
1282 	entry->text = _("Hide"); /* can be: Unhide */
1283 
1284 	wMenuAddCallback(menu, _("Settings..."), settingsCallback, NULL);
1285 
1286 	entry = wMenuAddCallback(menu, _("Kill"), killCallback, NULL);
1287 	wfree(entry->text);
1288 	entry->text = _("Kill"); /* can be: Remove drawer */
1289 
1290 	if (type == WM_CLIP)
1291 		scr->clip_menu = menu;
1292 
1293 	if (type == WM_DRAWER)
1294 		scr->drawer_menu = menu;
1295 
1296 	return menu;
1297 }
1298 
wDockCreate(WScreen * scr,int type,const char * name)1299 WDock *wDockCreate(WScreen *scr, int type, const char *name)
1300 {
1301 	WDock *dock;
1302 	WAppIcon *btn;
1303 
1304 	make_keys();
1305 
1306 	dock = wmalloc(sizeof(WDock));
1307 
1308 	switch (type) {
1309 	case WM_CLIP:
1310 		dock->max_icons = DOCK_MAX_ICONS;
1311 		break;
1312 	case WM_DRAWER:
1313 		dock->max_icons = scr->scr_width / wPreferences.icon_size;
1314 		break;
1315 	case WM_DOCK:
1316 	default:
1317 		dock->max_icons = scr->scr_height / wPreferences.icon_size;
1318 	}
1319 
1320 	dock->icon_array = wmalloc(sizeof(WAppIcon *) * dock->max_icons);
1321 
1322 	btn = mainIconCreate(scr, type, name);
1323 
1324 	btn->dock = dock;
1325 
1326 	dock->x_pos = btn->x_pos;
1327 	dock->y_pos = btn->y_pos;
1328 	dock->screen_ptr = scr;
1329 	dock->type = type;
1330 	dock->icon_count = 1;
1331 	if (type == WM_DRAWER)
1332 		dock->on_right_side = scr->dock->on_right_side;
1333 	else
1334 		dock->on_right_side = 1;
1335 	dock->collapsed = 0;
1336 	dock->auto_collapse = 0;
1337 	dock->auto_collapse_magic = NULL;
1338 	dock->auto_raise_lower = 0;
1339 	dock->auto_lower_magic = NULL;
1340 	dock->auto_raise_magic = NULL;
1341 	dock->attract_icons = 0;
1342 	dock->lowered = 1;
1343 	dock->icon_array[0] = btn;
1344 	wRaiseFrame(btn->icon->core);
1345 	XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
1346 
1347 	/* create dock menu */
1348 	dock->menu = dockMenuCreate(scr, type);
1349 
1350 	if (type == WM_DRAWER) {
1351 		drawerAppendToChain(scr, dock);
1352 		dock->auto_collapse = 1;
1353 	}
1354 
1355 	return dock;
1356 }
1357 
wDockDestroy(WDock * dock)1358 void wDockDestroy(WDock *dock)
1359 {
1360 	int i;
1361 	WAppIcon *aicon;
1362 
1363 	for (i = (dock->type == WM_CLIP) ? 1 : 0; i < dock->max_icons; i++) {
1364 		aicon = dock->icon_array[i];
1365 		if (aicon) {
1366 			int keepit = aicon->running && wApplicationOf(aicon->main_window);
1367 			wDockDetach(dock, aicon);
1368 			if (keepit) {
1369 				/* XXX: can: aicon->icon == NULL ? */
1370 				PlaceIcon(dock->screen_ptr, &aicon->x_pos, &aicon->y_pos,
1371 					  wGetHeadForWindow(aicon->icon->owner));
1372 				XMoveWindow(dpy, aicon->icon->core->window, aicon->x_pos, aicon->y_pos);
1373 				if (!dock->mapped || dock->collapsed)
1374 					XMapWindow(dpy, aicon->icon->core->window);
1375 			}
1376 		}
1377 	}
1378 	if (wPreferences.auto_arrange_icons)
1379 		wArrangeIcons(dock->screen_ptr, True);
1380 	wfree(dock->icon_array);
1381 	if (dock->menu && dock->type != WM_CLIP)
1382 		wMenuDestroy(dock->menu, True);
1383 	if (dock->screen_ptr->last_dock == dock)
1384 		dock->screen_ptr->last_dock = NULL;
1385 	wfree(dock);
1386 }
1387 
wClipIconPaint(WAppIcon * aicon)1388 void wClipIconPaint(WAppIcon *aicon)
1389 {
1390 	WScreen *scr = aicon->icon->core->screen_ptr;
1391 	WWorkspace *workspace = scr->workspaces[scr->current_workspace];
1392 	WMColor *color;
1393 	Window win = aicon->icon->core->window;
1394 	int length, nlength;
1395 	char *ws_name, ws_number[sizeof scr->current_workspace * CHAR_BIT / 3 + 1];
1396 	int ty, tx;
1397 
1398 	wIconPaint(aicon->icon);
1399 
1400 	length = strlen(workspace->name);
1401 	ws_name = wmalloc(length + 1);
1402 	snprintf(ws_name, length + 1, "%s", workspace->name);
1403 	snprintf(ws_number, sizeof ws_number, "%u", scr->current_workspace + 1);
1404 	nlength = strlen(ws_number);
1405 
1406 	if (wPreferences.flags.noclip || !workspace->clip->collapsed)
1407 		color = scr->clip_title_color[CLIP_NORMAL];
1408 	else
1409 		color = scr->clip_title_color[CLIP_COLLAPSED];
1410 
1411 	ty = ICON_SIZE - WMFontHeight(scr->clip_title_font) - 3;
1412 
1413 	tx = CLIP_BUTTON_SIZE * ICON_SIZE / 64;
1414 
1415 	if(wPreferences.show_clip_title)
1416 		WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, ty, ws_name, length);
1417 
1418 	tx = (ICON_SIZE / 2 - WMWidthOfString(scr->clip_title_font, ws_number, nlength)) / 2;
1419 
1420 	WMDrawString(scr->wmscreen, win, color, scr->clip_title_font, tx, 2, ws_number, nlength);
1421 
1422 	wfree(ws_name);
1423 
1424 	if (aicon->launching)
1425 		XFillRectangle(dpy, aicon->icon->core->window, scr->stipple_gc,
1426 			       0, 0, wPreferences.icon_size, wPreferences.icon_size);
1427 
1428 	paintClipButtons(aicon, aicon->dock->lclip_button_pushed, aicon->dock->rclip_button_pushed);
1429 }
1430 
clipIconExpose(WObjDescriptor * desc,XEvent * event)1431 static void clipIconExpose(WObjDescriptor *desc, XEvent *event)
1432 {
1433 	/* Parameter not used, but tell the compiler that it is ok */
1434 	(void) desc;
1435 	(void) event;
1436 
1437 	wClipIconPaint(desc->parent);
1438 }
1439 
dockIconPaint(WAppIcon * btn)1440 static void dockIconPaint(WAppIcon *btn)
1441 {
1442 	if (btn == btn->icon->core->screen_ptr->clip_icon) {
1443 		wClipIconPaint(btn);
1444 	} else if (wIsADrawer(btn)) {
1445 		wDrawerIconPaint(btn);
1446 	} else {
1447 		wAppIconPaint(btn);
1448 		save_appicon(btn);
1449 	}
1450 }
1451 
make_icon_state(WAppIcon * btn)1452 static WMPropList *make_icon_state(WAppIcon *btn)
1453 {
1454 	WMPropList *node = NULL;
1455 	WMPropList *command, *autolaunch, *lock, *name, *forced;
1456 	WMPropList *position, *buggy, *omnipresent;
1457 	char *tmp;
1458 	char buffer[64];
1459 
1460 	if (btn) {
1461 		if (!btn->command)
1462 			command = WMCreatePLString("-");
1463 		else
1464 			command = WMCreatePLString(btn->command);
1465 
1466 		autolaunch = btn->auto_launch ? dYes : dNo;
1467 
1468 		lock = btn->lock ? dYes : dNo;
1469 
1470 		tmp = EscapeWM_CLASS(btn->wm_instance, btn->wm_class);
1471 
1472 		name = WMCreatePLString(tmp);
1473 
1474 		wfree(tmp);
1475 
1476 		forced = btn->forced_dock ? dYes : dNo;
1477 
1478 		buggy = btn->buggy_app ? dYes : dNo;
1479 
1480 		if (!wPreferences.flags.clip_merged_in_dock && btn == btn->icon->core->screen_ptr->clip_icon)
1481 			snprintf(buffer, sizeof(buffer), "%i,%i", btn->x_pos, btn->y_pos);
1482 		else
1483 			snprintf(buffer, sizeof(buffer), "%hi,%hi", btn->xindex, btn->yindex);
1484 		position = WMCreatePLString(buffer);
1485 
1486 		node = WMCreatePLDictionary(dCommand, command,
1487 					    dName, name,
1488 					    dAutoLaunch, autolaunch,
1489 					    dLock, lock,
1490 					    dForced, forced, dBuggyApplication, buggy, dPosition, position, NULL);
1491 		WMReleasePropList(command);
1492 		WMReleasePropList(name);
1493 		WMReleasePropList(position);
1494 
1495 		omnipresent = btn->omnipresent ? dYes : dNo;
1496 		if (btn->dock != btn->icon->core->screen_ptr->dock && (btn->xindex != 0 || btn->yindex != 0))
1497 			WMPutInPLDictionary(node, dOmnipresent, omnipresent);
1498 
1499 #ifdef USE_DOCK_XDND
1500 		if (btn->dnd_command) {
1501 			command = WMCreatePLString(btn->dnd_command);
1502 			WMPutInPLDictionary(node, dDropCommand, command);
1503 			WMReleasePropList(command);
1504 		}
1505 #endif	/* USE_DOCK_XDND */
1506 
1507 		if (btn->paste_command) {
1508 			command = WMCreatePLString(btn->paste_command);
1509 			WMPutInPLDictionary(node, dPasteCommand, command);
1510 			WMReleasePropList(command);
1511 		}
1512 	}
1513 
1514 	return node;
1515 }
1516 
dockSaveState(WDock * dock)1517 static WMPropList *dockSaveState(WDock *dock)
1518 {
1519 	int i;
1520 	WMPropList *icon_info;
1521 	WMPropList *list = NULL, *dock_state = NULL;
1522 	WMPropList *value, *key;
1523 	char buffer[256];
1524 
1525 	list = WMCreatePLArray(NULL);
1526 
1527 	for (i = (dock->type == WM_DOCK ? 0 : 1); i < dock->max_icons; i++) {
1528 		WAppIcon *btn = dock->icon_array[i];
1529 
1530 		if (!btn || btn->attracted)
1531 			continue;
1532 
1533 		icon_info = make_icon_state(dock->icon_array[i]);
1534 		if (icon_info != NULL) {
1535 			WMAddToPLArray(list, icon_info);
1536 			WMReleasePropList(icon_info);
1537 		}
1538 	}
1539 
1540 	dock_state = WMCreatePLDictionary(dApplications, list, NULL);
1541 
1542 	if (dock->type == WM_DOCK) {
1543 		snprintf(buffer, sizeof(buffer), "Applications%i", dock->screen_ptr->scr_height);
1544 		key = WMCreatePLString(buffer);
1545 		WMPutInPLDictionary(dock_state, key, list);
1546 		WMReleasePropList(key);
1547 
1548 		snprintf(buffer, sizeof(buffer), "%i,%i", (dock->on_right_side ? -ICON_SIZE : 0), dock->y_pos);
1549 		value = WMCreatePLString(buffer);
1550 		WMPutInPLDictionary(dock_state, dPosition, value);
1551 		WMReleasePropList(value);
1552 	}
1553 	WMReleasePropList(list);
1554 
1555 	if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
1556 		value = (dock->collapsed ? dYes : dNo);
1557 		WMPutInPLDictionary(dock_state, dCollapsed, value);
1558 
1559 		value = (dock->auto_collapse ? dYes : dNo);
1560 		WMPutInPLDictionary(dock_state, dAutoCollapse, value);
1561 
1562 		value = (dock->attract_icons ? dYes : dNo);
1563 		WMPutInPLDictionary(dock_state, dAutoAttractIcons, value);
1564 	}
1565 
1566 	if (dock->type == WM_DOCK || dock->type == WM_CLIP) {
1567 		value = (dock->lowered ? dYes : dNo);
1568 		WMPutInPLDictionary(dock_state, dLowered, value);
1569 
1570 		value = (dock->auto_raise_lower ? dYes : dNo);
1571 		WMPutInPLDictionary(dock_state, dAutoRaiseLower, value);
1572 	}
1573 
1574 	return dock_state;
1575 }
1576 
wDockSaveState(WScreen * scr,WMPropList * old_state)1577 void wDockSaveState(WScreen *scr, WMPropList *old_state)
1578 {
1579 	WMPropList *dock_state;
1580 	WMPropList *keys;
1581 
1582 	dock_state = dockSaveState(scr->dock);
1583 
1584 	/*
1585 	 * Copy saved states of docks with different sizes.
1586 	 */
1587 	if (old_state) {
1588 		int i;
1589 		WMPropList *tmp;
1590 
1591 		keys = WMGetPLDictionaryKeys(old_state);
1592 		for (i = 0; i < WMGetPropListItemCount(keys); i++) {
1593 			tmp = WMGetFromPLArray(keys, i);
1594 
1595 			if (strncasecmp(WMGetFromPLString(tmp), "applications", 12) == 0
1596 			    && !WMGetFromPLDictionary(dock_state, tmp)) {
1597 
1598 				WMPutInPLDictionary(dock_state, tmp, WMGetFromPLDictionary(old_state, tmp));
1599 			}
1600 		}
1601 		WMReleasePropList(keys);
1602 	}
1603 
1604 	WMPutInPLDictionary(scr->session_state, dDock, dock_state);
1605 
1606 	WMReleasePropList(dock_state);
1607 }
1608 
wClipSaveState(WScreen * scr)1609 void wClipSaveState(WScreen *scr)
1610 {
1611 	WMPropList *clip_state;
1612 
1613 	clip_state = make_icon_state(scr->clip_icon);
1614 
1615 	WMPutInPLDictionary(scr->session_state, dClip, clip_state);
1616 
1617 	WMReleasePropList(clip_state);
1618 }
1619 
wClipSaveWorkspaceState(WScreen * scr,int workspace)1620 WMPropList *wClipSaveWorkspaceState(WScreen *scr, int workspace)
1621 {
1622 	return dockSaveState(scr->workspaces[workspace]->clip);
1623 }
1624 
getBooleanDockValue(WMPropList * value,WMPropList * key)1625 static Bool getBooleanDockValue(WMPropList *value, WMPropList *key)
1626 {
1627 	if (value) {
1628 		if (WMIsPLString(value)) {
1629 			if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1630 				return True;
1631 		} else {
1632 			wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(key));
1633 		}
1634 	}
1635 	return False;
1636 }
1637 
restore_icon_state(WScreen * scr,WMPropList * info,int type,int index)1638 static WAppIcon *restore_icon_state(WScreen *scr, WMPropList *info, int type, int index)
1639 {
1640 	WAppIcon *aicon;
1641 	WMPropList *cmd, *value;
1642 	char *wclass, *winstance, *command;
1643 
1644 	cmd = WMGetFromPLDictionary(info, dCommand);
1645 	if (!cmd || !WMIsPLString(cmd))
1646 		return NULL;
1647 
1648 	/* parse window name */
1649 	value = WMGetFromPLDictionary(info, dName);
1650 	if (!value)
1651 		return NULL;
1652 
1653 	ParseWindowName(value, &winstance, &wclass, "dock");
1654 
1655 	if (!winstance && !wclass)
1656 		return NULL;
1657 
1658 	/* get commands */
1659 	command = wstrdup(WMGetFromPLString(cmd));
1660 
1661 	if (strcmp(command, "-") == 0) {
1662 		wfree(command);
1663 
1664 		if (wclass)
1665 			wfree(wclass);
1666 		if (winstance)
1667 			wfree(winstance);
1668 
1669 		return NULL;
1670 	}
1671 
1672 	aicon = wAppIconCreateForDock(scr, command, winstance, wclass, TILE_NORMAL);
1673 	if (wclass)
1674 		wfree(wclass);
1675 	if (winstance)
1676 		wfree(winstance);
1677 
1678 	wfree(command);
1679 
1680 	aicon->icon->core->descriptor.handle_mousedown = iconMouseDown;
1681 	aicon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
1682 	aicon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
1683 	aicon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
1684 	aicon->icon->core->descriptor.parent = aicon;
1685 
1686 #ifdef USE_DOCK_XDND
1687 	cmd = WMGetFromPLDictionary(info, dDropCommand);
1688 	if (cmd)
1689 		aicon->dnd_command = wstrdup(WMGetFromPLString(cmd));
1690 #endif
1691 
1692 	cmd = WMGetFromPLDictionary(info, dPasteCommand);
1693 	if (cmd)
1694 		aicon->paste_command = wstrdup(WMGetFromPLString(cmd));
1695 
1696 	/* check auto launch */
1697 	value = WMGetFromPLDictionary(info, dAutoLaunch);
1698 
1699 	aicon->auto_launch = getBooleanDockValue(value, dAutoLaunch);
1700 
1701 	/* check lock */
1702 	value = WMGetFromPLDictionary(info, dLock);
1703 
1704 	aicon->lock = getBooleanDockValue(value, dLock);
1705 
1706 	/* check if it wasn't normally docked */
1707 	value = WMGetFromPLDictionary(info, dForced);
1708 
1709 	aicon->forced_dock = getBooleanDockValue(value, dForced);
1710 
1711 	/* check if we can rely on the stuff in the app */
1712 	value = WMGetFromPLDictionary(info, dBuggyApplication);
1713 
1714 	aicon->buggy_app = getBooleanDockValue(value, dBuggyApplication);
1715 
1716 	/* get position in the dock */
1717 	value = WMGetFromPLDictionary(info, dPosition);
1718 	if (value && WMIsPLString(value)) {
1719 		if (sscanf(WMGetFromPLString(value), "%hi,%hi", &aicon->xindex, &aicon->yindex) != 2)
1720 			wwarning(_("bad value in docked icon state info %s"), WMGetFromPLString(dPosition));
1721 
1722 		/* check position sanity */
1723 		/* *Very* incomplete section! */
1724 		if (type == WM_DOCK) {
1725 			aicon->xindex = 0;
1726 		}
1727 	} else {
1728 		aicon->yindex = index;
1729 		aicon->xindex = 0;
1730 	}
1731 
1732 	/* check if icon is omnipresent */
1733 	value = WMGetFromPLDictionary(info, dOmnipresent);
1734 
1735 	aicon->omnipresent = getBooleanDockValue(value, dOmnipresent);
1736 
1737 	aicon->running = 0;
1738 	aicon->docked = 1;
1739 
1740 	return aicon;
1741 }
1742 
1743 #define COMPLAIN(key) wwarning(_("bad value in dock state info:%s"), key)
1744 
wClipRestoreState(WScreen * scr,WMPropList * clip_state)1745 WAppIcon *wClipRestoreState(WScreen *scr, WMPropList *clip_state)
1746 {
1747 	WAppIcon *icon;
1748 	WMPropList *value;
1749 
1750 	icon = mainIconCreate(scr, WM_CLIP, NULL);
1751 
1752 	if (!clip_state)
1753 		return icon;
1754 
1755 	WMRetainPropList(clip_state);
1756 
1757 	/* restore position */
1758 
1759 	value = WMGetFromPLDictionary(clip_state, dPosition);
1760 
1761 	if (value) {
1762 		if (!WMIsPLString(value)) {
1763 			COMPLAIN("Position");
1764 		} else {
1765 			if (sscanf(WMGetFromPLString(value), "%i,%i", &icon->x_pos, &icon->y_pos) != 2)
1766 				COMPLAIN("Position");
1767 
1768 			/* check position sanity */
1769 			if (!onScreen(scr, icon->x_pos, icon->y_pos))
1770 				wScreenKeepInside(scr, &icon->x_pos, &icon->y_pos, ICON_SIZE, ICON_SIZE);
1771 		}
1772 	}
1773 #ifdef USE_DOCK_XDND
1774 	value = WMGetFromPLDictionary(clip_state, dDropCommand);
1775 	if (value && WMIsPLString(value))
1776 		icon->dnd_command = wstrdup(WMGetFromPLString(value));
1777 #endif
1778 
1779 	value = WMGetFromPLDictionary(clip_state, dPasteCommand);
1780 	if (value && WMIsPLString(value))
1781 		icon->paste_command = wstrdup(WMGetFromPLString(value));
1782 
1783 	WMReleasePropList(clip_state);
1784 
1785 	return icon;
1786 }
1787 
wDockRestoreState(WScreen * scr,WMPropList * dock_state,int type)1788 WDock *wDockRestoreState(WScreen *scr, WMPropList *dock_state, int type)
1789 {
1790 	WDock *dock;
1791 	WMPropList *apps;
1792 	WMPropList *value;
1793 	WAppIcon *aicon, *old_top;
1794 	int count, i;
1795 
1796 	dock = wDockCreate(scr, type, NULL);
1797 
1798 	if (!dock_state)
1799 		return dock;
1800 
1801 	WMRetainPropList(dock_state);
1802 
1803 	/* restore position */
1804 	value = WMGetFromPLDictionary(dock_state, dPosition);
1805 	if (value) {
1806 		if (!WMIsPLString(value)) {
1807 			COMPLAIN("Position");
1808 		} else {
1809 			if (sscanf(WMGetFromPLString(value), "%i,%i", &dock->x_pos, &dock->y_pos) != 2)
1810 				COMPLAIN("Position");
1811 
1812 			/* check position sanity */
1813 			if (!onScreen(scr, dock->x_pos, dock->y_pos)) {
1814 				int x = dock->x_pos;
1815 				wScreenKeepInside(scr, &x, &dock->y_pos, ICON_SIZE, ICON_SIZE);
1816 			}
1817 
1818 			/* Is this needed any more? */
1819 			if (type == WM_CLIP) {
1820 				if (dock->x_pos < 0) {
1821 					dock->x_pos = 0;
1822 				} else if (dock->x_pos > scr->scr_width - ICON_SIZE) {
1823 					dock->x_pos = scr->scr_width - ICON_SIZE;
1824 				}
1825 			} else {
1826 				if (dock->x_pos >= 0) {
1827 					dock->x_pos = DOCK_EXTRA_SPACE;
1828 					dock->on_right_side = 0;
1829 				} else {
1830 					dock->x_pos = scr->scr_width - DOCK_EXTRA_SPACE - ICON_SIZE;
1831 					dock->on_right_side = 1;
1832 				}
1833 			}
1834 		}
1835 	}
1836 
1837 	/* restore lowered/raised state */
1838 	dock->lowered = 0;
1839 
1840 	value = WMGetFromPLDictionary(dock_state, dLowered);
1841 	if (value) {
1842 		if (!WMIsPLString(value)) {
1843 			COMPLAIN("Lowered");
1844 		} else {
1845 			if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1846 				dock->lowered = 1;
1847 		}
1848 	}
1849 
1850 	/* restore collapsed state */
1851 	dock->collapsed = 0;
1852 
1853 	value = WMGetFromPLDictionary(dock_state, dCollapsed);
1854 	if (value) {
1855 		if (!WMIsPLString(value)) {
1856 			COMPLAIN("Collapsed");
1857 		} else {
1858 			if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1859 				dock->collapsed = 1;
1860 		}
1861 	}
1862 
1863 	/* restore auto-collapsed state */
1864 	value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
1865 	if (value) {
1866 		if (!WMIsPLString(value)) {
1867 			COMPLAIN("AutoCollapse");
1868 		} else {
1869 			if (strcasecmp(WMGetFromPLString(value), "YES") == 0) {
1870 				dock->auto_collapse = 1;
1871 				dock->collapsed = 1;
1872 			}
1873 		}
1874 	}
1875 
1876 	/* restore auto-raise/lower state */
1877 	value = WMGetFromPLDictionary(dock_state, dAutoRaiseLower);
1878 	if (value) {
1879 		if (!WMIsPLString(value)) {
1880 			COMPLAIN("AutoRaiseLower");
1881 		} else {
1882 			if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1883 				dock->auto_raise_lower = 1;
1884 		}
1885 	}
1886 
1887 	/* restore attract icons state */
1888 	dock->attract_icons = 0;
1889 
1890 	value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
1891 	if (value) {
1892 		if (!WMIsPLString(value)) {
1893 			COMPLAIN("AutoAttractIcons");
1894 		} else {
1895 			if (strcasecmp(WMGetFromPLString(value), "YES") == 0)
1896 				dock->attract_icons = 1;
1897 		}
1898 	}
1899 
1900 	/* application list */
1901 
1902 	{
1903 		WMPropList *tmp;
1904 		char buffer[64];
1905 
1906 		/*
1907 		 * When saving, it saves the dock state in
1908 		 * Applications and Applicationsnnn
1909 		 *
1910 		 * When loading, it will first try Applicationsnnn.
1911 		 * If it does not exist, use Applications as default.
1912 		 */
1913 
1914 		snprintf(buffer, sizeof(buffer), "Applications%i", scr->scr_height);
1915 
1916 		tmp = WMCreatePLString(buffer);
1917 		apps = WMGetFromPLDictionary(dock_state, tmp);
1918 		WMReleasePropList(tmp);
1919 
1920 		if (!apps)
1921 			apps = WMGetFromPLDictionary(dock_state, dApplications);
1922 	}
1923 
1924 	if (!apps)
1925 		goto finish;
1926 
1927 	count = WMGetPropListItemCount(apps);
1928 	if (count == 0)
1929 		goto finish;
1930 
1931 	old_top = dock->icon_array[0];
1932 
1933 	/* dock->icon_count is set to 1 when dock is created.
1934 	 * Since Clip is already restored, we want to keep it so for clip,
1935 	 * but for dock we may change the default top tile, so we set it to 0.
1936 	 */
1937 	if (type == WM_DOCK)
1938 		dock->icon_count = 0;
1939 
1940 	for (i = 0; i < count; i++) {
1941 		if (dock->icon_count >= dock->max_icons) {
1942 			wwarning(_("there are too many icons stored in dock. Ignoring what doesn't fit"));
1943 			break;
1944 		}
1945 
1946 		value = WMGetFromPLArray(apps, i);
1947 		aicon = restore_icon_state(scr, value, type, dock->icon_count);
1948 
1949 		dock->icon_array[dock->icon_count] = aicon;
1950 
1951 		if (aicon) {
1952 			aicon->dock = dock;
1953 			aicon->x_pos = dock->x_pos + (aicon->xindex * ICON_SIZE);
1954 			aicon->y_pos = dock->y_pos + (aicon->yindex * ICON_SIZE);
1955 
1956 			if (dock->lowered)
1957 				ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
1958 			else
1959 				ChangeStackingLevel(aicon->icon->core, WMDockLevel);
1960 
1961 			wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
1962 			if (!dock->collapsed)
1963 				XMapWindow(dpy, aicon->icon->core->window);
1964 
1965 			wRaiseFrame(aicon->icon->core);
1966 
1967 			dock->icon_count++;
1968 		} else if (dock->icon_count == 0 && type == WM_DOCK) {
1969 			dock->icon_count++;
1970 		}
1971 	}
1972 
1973 	/* if the first icon is not defined, use the default */
1974 	if (dock->icon_array[0] == NULL) {
1975 		/* update default icon */
1976 		old_top->x_pos = dock->x_pos;
1977 		old_top->y_pos = dock->y_pos;
1978 		if (dock->lowered)
1979 			ChangeStackingLevel(old_top->icon->core, WMNormalLevel);
1980 		else
1981 			ChangeStackingLevel(old_top->icon->core, WMDockLevel);
1982 
1983 		dock->icon_array[0] = old_top;
1984 		XMoveWindow(dpy, old_top->icon->core->window, dock->x_pos, dock->y_pos);
1985 		/* we don't need to increment dock->icon_count here because it was
1986 		 * incremented in the loop above.
1987 		 */
1988 	} else if (old_top != dock->icon_array[0]) {
1989 		if (old_top == scr->clip_icon) // TODO dande: understand the logic
1990 			scr->clip_icon = dock->icon_array[0];
1991 
1992 		wAppIconDestroy(old_top);
1993 	}
1994 
1995 finish:
1996 	WMReleasePropList(dock_state);
1997 
1998 	return dock;
1999 }
2000 
wDockLaunchWithState(WAppIcon * btn,WSavedState * state)2001 void wDockLaunchWithState(WAppIcon *btn, WSavedState *state)
2002 {
2003 	if (btn && btn->command && !btn->running && !btn->launching) {
2004 		btn->drop_launch = 0;
2005 		btn->paste_launch = 0;
2006 
2007 		btn->pid = execCommand(btn, btn->command, state);
2008 
2009 		if (btn->pid > 0) {
2010 			if (!btn->forced_dock && !btn->buggy_app) {
2011 				btn->launching = 1;
2012 				dockIconPaint(btn);
2013 			}
2014 		}
2015 	} else {
2016 		wfree(state);
2017 	}
2018 }
2019 
wDockDoAutoLaunch(WDock * dock,int workspace)2020 void wDockDoAutoLaunch(WDock *dock, int workspace)
2021 {
2022 	WAppIcon *btn;
2023 	WSavedState *state;
2024 	int i;
2025 
2026 	for (i = 0; i < dock->max_icons; i++) {
2027 		btn = dock->icon_array[i];
2028 		if (!btn || !btn->auto_launch)
2029 			continue;
2030 
2031 		state = wmalloc(sizeof(WSavedState));
2032 		state->workspace = workspace;
2033 		/* TODO: this is klugy and is very difficult to understand
2034 		 * what's going on. Try to clean up */
2035 		wDockLaunchWithState(btn, state);
2036 	}
2037 }
2038 
2039 #ifdef USE_DOCK_XDND
findDock(WScreen * scr,XEvent * event,int * icon_pos)2040 static WDock *findDock(WScreen *scr, XEvent *event, int *icon_pos)
2041 {
2042 	WDock *dock;
2043 	int i;
2044 
2045 	dock = scr->dock;
2046 	if (dock != NULL) {
2047 		for (i = 0; i < dock->max_icons; i++) {
2048 			if (dock->icon_array[i] &&
2049 			    dock->icon_array[i]->icon->core->window == event->xclient.window) {
2050 				*icon_pos = i;
2051 				return dock;
2052 			}
2053 		}
2054 	}
2055 
2056 	dock = scr->workspaces[scr->current_workspace]->clip;
2057 	if (dock != NULL) {
2058 		for (i = 0; i < dock->max_icons; i++) {
2059 			if (dock->icon_array[i] &&
2060 			    dock->icon_array[i]->icon->core->window == event->xclient.window) {
2061 				*icon_pos = i;
2062 				return dock;
2063 			}
2064 		}
2065 	}
2066 
2067 	*icon_pos = -1;
2068 	return NULL;
2069 }
2070 
wDockReceiveDNDDrop(WScreen * scr,XEvent * event)2071 int wDockReceiveDNDDrop(WScreen *scr, XEvent *event)
2072 {
2073 	WDock *dock;
2074 	WAppIcon *btn;
2075 	int icon_pos;
2076 
2077 	dock = findDock(scr, event, &icon_pos);
2078 	if (!dock)
2079 		return False;
2080 
2081 	/*
2082 	 * Return True if the drop was on an application icon window.
2083 	 * In this case, let the ClientMessage handler redirect the
2084 	 * message to the app.
2085 	 */
2086 	if (dock->icon_array[icon_pos]->icon->icon_win != None)
2087 		return True;
2088 
2089 	if (dock->icon_array[icon_pos]->dnd_command != NULL) {
2090 		scr->flags.dnd_data_convertion_status = 0;
2091 
2092 		btn = dock->icon_array[icon_pos];
2093 
2094 		if (!btn->forced_dock) {
2095 			btn->relaunching = btn->running;
2096 			btn->running = 1;
2097 		}
2098 		if (btn->wm_instance || btn->wm_class) {
2099 			WWindowAttributes attr;
2100 			memset(&attr, 0, sizeof(WWindowAttributes));
2101 			wDefaultFillAttributes(btn->wm_instance, btn->wm_class, &attr, NULL, True);
2102 
2103 			if (!attr.no_appicon)
2104 				btn->launching = 1;
2105 			else
2106 				btn->running = 0;
2107 		}
2108 
2109 		btn->paste_launch = 0;
2110 		btn->drop_launch = 1;
2111 		scr->last_dock = dock;
2112 		btn->pid = execCommand(btn, btn->dnd_command, NULL);
2113 		if (btn->pid > 0) {
2114 			dockIconPaint(btn);
2115 		} else {
2116 			btn->launching = 0;
2117 			if (!btn->relaunching)
2118 				btn->running = 0;
2119 		}
2120 	}
2121 	return False;
2122 }
2123 #endif	/* USE_DOCK_XDND */
2124 
wDockAttachIcon(WDock * dock,WAppIcon * icon,int x,int y,Bool update_icon)2125 Bool wDockAttachIcon(WDock *dock, WAppIcon *icon, int x, int y, Bool update_icon)
2126 {
2127 	WWindow *wwin;
2128 	Bool lupdate_icon = False;
2129 	char *command = NULL;
2130 	int index;
2131 
2132 	icon->editing = 0;
2133 
2134 	if (update_icon)
2135 		lupdate_icon = True;
2136 
2137 	if (icon->command == NULL) {
2138 		/* If icon->owner exists, it means the application is running */
2139 		if (icon->icon->owner) {
2140 			wwin = icon->icon->owner;
2141 			command = GetCommandForWindow(wwin->client_win);
2142 		}
2143 
2144 		if (command) {
2145 			icon->command = command;
2146 		} else {
2147 			/* icon->forced_dock = 1; */
2148 			if (dock->type != WM_CLIP || !icon->attracted) {
2149 				icon->editing = 1;
2150 				if (wInputDialog(dock->screen_ptr, _("Dock Icon"),
2151 						 _("Type the command used to launch the application"), &command)) {
2152 					if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2153 						wfree(command);
2154 						command = NULL;
2155 					}
2156 					icon->command = command;
2157 					icon->editing = 0;
2158 				} else {
2159 					icon->editing = 0;
2160 					if (command)
2161 						wfree(command);
2162 					/* If the target is the dock, reject the icon. If
2163 					 * the target is the clip, make it an attracted icon
2164 					 */
2165 					if (dock->type == WM_CLIP) {
2166 						icon->attracted = 1;
2167 						if (!icon->icon->shadowed) {
2168 							icon->icon->shadowed = 1;
2169 							lupdate_icon = True;
2170 						}
2171 					} else {
2172 						return False;
2173 					}
2174 				}
2175 			}
2176 		}
2177 	}
2178 
2179 	for (index = 1; index < dock->max_icons; index++)
2180 		if (dock->icon_array[index] == NULL)
2181 			break;
2182 	/* if (index == dock->max_icons)
2183 	   return; */
2184 
2185 	assert(index < dock->max_icons);
2186 
2187 	dock->icon_array[index] = icon;
2188 	icon->yindex = y;
2189 	icon->xindex = x;
2190 
2191 	icon->omnipresent = 0;
2192 
2193 	icon->x_pos = dock->x_pos + x * ICON_SIZE;
2194 	icon->y_pos = dock->y_pos + y * ICON_SIZE;
2195 
2196 	dock->icon_count++;
2197 
2198 	icon->running = 1;
2199 	icon->launching = 0;
2200 	icon->docked = 1;
2201 	icon->dock = dock;
2202 	icon->icon->core->descriptor.handle_mousedown = iconMouseDown;
2203 	icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2204 	icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2205 	icon->icon->core->descriptor.parent_type = WCLASS_DOCK_ICON;
2206 	icon->icon->core->descriptor.parent = icon;
2207 
2208 	MoveInStackListUnder(dock->icon_array[index - 1]->icon->core, icon->icon->core);
2209 	wAppIconMove(icon, icon->x_pos, icon->y_pos);
2210 
2211 	/*
2212 	 * Update icon pixmap, RImage doesn't change,
2213 	 * so call wIconUpdate is not needed
2214 	 */
2215 	if (lupdate_icon)
2216 		update_icon_pixmap(icon->icon);
2217 
2218 	/* Paint it */
2219 	wAppIconPaint(icon);
2220 
2221 	/* Save it */
2222 	save_appicon(icon);
2223 
2224 	if (wPreferences.auto_arrange_icons)
2225 		wArrangeIcons(dock->screen_ptr, True);
2226 
2227 #ifdef USE_DOCK_XDND
2228 	if (icon->command && !icon->dnd_command) {
2229 		int len = strlen(icon->command) + 8;
2230 		icon->dnd_command = wmalloc(len);
2231 		snprintf(icon->dnd_command, len, "%s %%d", icon->command);
2232 	}
2233 #endif
2234 
2235 	if (icon->command && !icon->paste_command) {
2236 		int len = strlen(icon->command) + 8;
2237 		icon->paste_command = wmalloc(len);
2238 		snprintf(icon->paste_command, len, "%s %%s", icon->command);
2239 	}
2240 
2241 	return True;
2242 }
2243 
wDockReattachIcon(WDock * dock,WAppIcon * icon,int x,int y)2244 void wDockReattachIcon(WDock *dock, WAppIcon *icon, int x, int y)
2245 {
2246 	int index;
2247 
2248 	for (index = 1; index < dock->max_icons; index++) {
2249 		if (dock->icon_array[index] == icon)
2250 			break;
2251 	}
2252 	assert(index < dock->max_icons);
2253 
2254 	icon->yindex = y;
2255 	icon->xindex = x;
2256 
2257 	icon->x_pos = dock->x_pos + x * ICON_SIZE;
2258 	icon->y_pos = dock->y_pos + y * ICON_SIZE;
2259 }
2260 
wDockMoveIconBetweenDocks(WDock * src,WDock * dest,WAppIcon * icon,int x,int y)2261 Bool wDockMoveIconBetweenDocks(WDock *src, WDock *dest, WAppIcon *icon, int x, int y)
2262 {
2263 	WWindow *wwin;
2264 	char *command = NULL;
2265 	int index;
2266 	Bool update_icon = False;
2267 
2268 	if (src == dest)
2269 		return True;	/* No move needed, we're already there */
2270 
2271 	if (dest == NULL)
2272 		return False;
2273 
2274 	/*
2275 	 * For the moment we can't do this if we move icons in Clip from one
2276 	 * workspace to other, because if we move two or more icons without
2277 	 * command, the dialog box will not be able to tell us to which of the
2278 	 * moved icons it applies. -Dan
2279 	 */
2280 	if ((dest->type == WM_DOCK /*|| dest->keep_attracted */ ) && icon->command == NULL) {
2281 		/* If icon->owner exists, it means the application is running */
2282 		if (icon->icon->owner) {
2283 			wwin = icon->icon->owner;
2284 			command = GetCommandForWindow(wwin->client_win);
2285 		}
2286 
2287 		if (command) {
2288 			icon->command = command;
2289 		} else {
2290 			icon->editing = 1;
2291 			/* icon->forced_dock = 1; */
2292 			if (wInputDialog(src->screen_ptr, _("Dock Icon"),
2293 					 _("Type the command used to launch the application"), &command)) {
2294 				if (command && (command[0] == 0 || (command[0] == '-' && command[1] == 0))) {
2295 					wfree(command);
2296 					command = NULL;
2297 				}
2298 				icon->command = command;
2299 			} else {
2300 				icon->editing = 0;
2301 				if (command)
2302 					wfree(command);
2303 				return False;
2304 			}
2305 			icon->editing = 0;
2306 		}
2307 	}
2308 
2309 	if (dest->type == WM_DOCK || dest->type == WM_DRAWER)
2310 		wClipMakeIconOmnipresent(icon, False);
2311 
2312 	for (index = 1; index < src->max_icons; index++) {
2313 		if (src->icon_array[index] == icon)
2314 			break;
2315 	}
2316 	assert(index < src->max_icons);
2317 
2318 	src->icon_array[index] = NULL;
2319 	src->icon_count--;
2320 
2321 	for (index = 1; index < dest->max_icons; index++) {
2322 		if (dest->icon_array[index] == NULL)
2323 			break;
2324 	}
2325 
2326 	assert(index < dest->max_icons);
2327 
2328 	dest->icon_array[index] = icon;
2329 	icon->dock = dest;
2330 
2331 	/* deselect the icon */
2332 	if (icon->icon->selected)
2333 		wIconSelect(icon->icon);
2334 
2335 	icon->icon->core->descriptor.handle_enternotify = clipEnterNotify;
2336 	icon->icon->core->descriptor.handle_leavenotify = clipLeaveNotify;
2337 
2338 	/* set it to be kept when moving to dock.
2339 	 * Unless the icon does not have a command set
2340 	 */
2341 	if (icon->command && (dest->type == WM_DOCK || dest->type == WM_DRAWER)) {
2342 		icon->attracted = 0;
2343 		if (icon->icon->shadowed) {
2344 			icon->icon->shadowed = 0;
2345 			update_icon = True;
2346 		}
2347 	}
2348 
2349 	if (src->auto_collapse || src->auto_raise_lower)
2350 		clipLeave(src);
2351 
2352 	icon->yindex = y;
2353 	icon->xindex = x;
2354 
2355 	icon->x_pos = dest->x_pos + x * ICON_SIZE;
2356 	icon->y_pos = dest->y_pos + y * ICON_SIZE;
2357 
2358 	dest->icon_count++;
2359 
2360 	MoveInStackListUnder(dest->icon_array[index - 1]->icon->core, icon->icon->core);
2361 
2362 	/*
2363 	 * Update icon pixmap, RImage doesn't change,
2364 	 * so call wIconUpdate is not needed
2365 	 */
2366 	if (update_icon)
2367 		update_icon_pixmap(icon->icon);
2368 
2369 	/* Paint it */
2370 	wAppIconPaint(icon);
2371 
2372 	return True;
2373 }
2374 
wDockDetach(WDock * dock,WAppIcon * icon)2375 void wDockDetach(WDock *dock, WAppIcon *icon)
2376 {
2377 	int index;
2378 	Bool update_icon = False;
2379 
2380 	/* make the settings panel be closed */
2381 	if (icon->panel)
2382 		DestroyDockAppSettingsPanel(icon->panel);
2383 
2384 	/* This must be called before icon->dock is set to NULL.
2385 	 * Don't move it. -Dan
2386 	 */
2387 	wClipMakeIconOmnipresent(icon, False);
2388 
2389 	icon->docked = 0;
2390 	icon->dock = NULL;
2391 	icon->attracted = 0;
2392 	icon->auto_launch = 0;
2393 	if (icon->icon->shadowed) {
2394 		icon->icon->shadowed = 0;
2395 		update_icon = True;
2396 	}
2397 
2398 	/* deselect the icon */
2399 	if (icon->icon->selected)
2400 		wIconSelect(icon->icon);
2401 
2402 	if (icon->command) {
2403 		wfree(icon->command);
2404 		icon->command = NULL;
2405 	}
2406 #ifdef USE_DOCK_XDND
2407 	if (icon->dnd_command) {
2408 		wfree(icon->dnd_command);
2409 		icon->dnd_command = NULL;
2410 	}
2411 #endif
2412 	if (icon->paste_command) {
2413 		wfree(icon->paste_command);
2414 		icon->paste_command = NULL;
2415 	}
2416 
2417 	for (index = 1; index < dock->max_icons; index++)
2418 		if (dock->icon_array[index] == icon)
2419 			break;
2420 
2421 	assert(index < dock->max_icons);
2422 	dock->icon_array[index] = NULL;
2423 	icon->yindex = -1;
2424 	icon->xindex = -1;
2425 
2426 	dock->icon_count--;
2427 
2428 	/* Remove the Cached Icon */
2429 	remove_cache_icon(icon->icon->file);
2430 
2431 	/* if the dock is not attached to an application or
2432 	 * the application did not set the appropriate hints yet,
2433 	 * destroy the icon */
2434 	if (!icon->running || !wApplicationOf(icon->main_window)) {
2435 		wAppIconDestroy(icon);
2436 	} else {
2437 		icon->icon->core->descriptor.handle_mousedown = appIconMouseDown;
2438 		icon->icon->core->descriptor.handle_enternotify = NULL;
2439 		icon->icon->core->descriptor.handle_leavenotify = NULL;
2440 		icon->icon->core->descriptor.parent_type = WCLASS_APPICON;
2441 		icon->icon->core->descriptor.parent = icon;
2442 
2443 		ChangeStackingLevel(icon->icon->core, NORMAL_ICON_LEVEL);
2444 
2445 		/*
2446 		 * Update icon pixmap, RImage doesn't change,
2447 		 * so call wIconUpdate is not needed
2448 		 */
2449 		if (update_icon)
2450 			update_icon_pixmap(icon->icon);
2451 
2452 		/* Paint it */
2453 		wAppIconPaint(icon);
2454 
2455 		if (wPreferences.auto_arrange_icons)
2456 			wArrangeIcons(dock->screen_ptr, True);
2457 	}
2458 
2459 	if (dock->auto_collapse || dock->auto_raise_lower)
2460 		clipLeave(dock);
2461 }
2462 
2463 /*
2464  * returns the closest Dock slot index for the passed
2465  * coordinates.
2466  *
2467  * Returns False if icon can't be docked.
2468  *
2469  * Note: this function should NEVER alter ret_x or ret_y, unless it will
2470  * return True. -Dan
2471  */
2472 /* Redocking == true means either icon->dock == dock (normal case)
2473  * or we are called from handleDockMove for a drawer */
wDockSnapIcon(WDock * dock,WAppIcon * icon,int req_x,int req_y,int * ret_x,int * ret_y,int redocking)2474 Bool wDockSnapIcon(WDock *dock, WAppIcon *icon, int req_x, int req_y, int *ret_x, int *ret_y, int redocking)
2475 {
2476 	WScreen *scr = dock->screen_ptr;
2477 	int dx, dy;
2478 	int ex_x, ex_y;
2479 	int i, offset = ICON_SIZE / 2;
2480 	WAppIcon *aicon = NULL;
2481 	WAppIcon *nicon = NULL;
2482 
2483 	if (wPreferences.flags.noupdates)
2484 		return False;
2485 
2486 	dx = dock->x_pos;
2487 	dy = dock->y_pos;
2488 
2489 	/* if the dock is full */
2490 	if (!redocking && (dock->icon_count >= dock->max_icons))
2491 		return False;
2492 
2493 	/* exact position */
2494 	if (req_y < dy)
2495 		ex_y = (req_y - offset - dy) / ICON_SIZE;
2496 	else
2497 		ex_y = (req_y + offset - dy) / ICON_SIZE;
2498 
2499 	if (req_x < dx)
2500 		ex_x = (req_x - offset - dx) / ICON_SIZE;
2501 	else
2502 		ex_x = (req_x + offset - dx) / ICON_SIZE;
2503 
2504 	/* check if the icon is outside the screen boundaries */
2505 	if (!onScreen(scr, dx + ex_x * ICON_SIZE, dy + ex_y * ICON_SIZE))
2506 		return False;
2507 
2508 	switch (dock->type) {
2509 	case WM_DOCK:
2510 		/* We can return False right away if
2511 		 * - we do not come from this dock (which is a WM_DOCK),
2512 		 * - we are not right over it, and
2513 		 * - we are not the main tile of a drawer.
2514 		 * In the latter case, we are called from handleDockMove. */
2515 		if (icon->dock != dock && ex_x != 0 &&
2516 			!(icon->dock && icon->dock->type == WM_DRAWER && icon == icon->dock->icon_array[0]))
2517 			return False;
2518 
2519 		if (!redocking && ex_x != 0)
2520 			return False;
2521 
2522 		if (getDrawer(scr, ex_y)) /* Return false so that the drawer gets it. */
2523 			return False;
2524 
2525 		aicon = NULL;
2526 		for (i = 0; i < dock->max_icons; i++) {
2527 			nicon = dock->icon_array[i];
2528 			if (nicon && nicon->yindex == ex_y) {
2529 				aicon = nicon;
2530 				break;
2531 			}
2532 		}
2533 
2534 		if (redocking) {
2535 			int sig, done, closest;
2536 
2537 			/* Possible cases when redocking:
2538 			 *
2539 			 * icon dragged out of range of any slot -> false
2540 			 * icon dragged on a drawer -> false (to open the drawer)
2541 			 * icon dragged to range of free slot
2542 			 * icon dragged to range of same slot
2543 			 * icon dragged to range of different icon
2544 			 */
2545 			if (abs(ex_x) > DOCK_DETTACH_THRESHOLD)
2546 				return False;
2547 
2548 			if (aicon == icon || !aicon) {
2549 				*ret_x = 0;
2550 				*ret_y = ex_y;
2551 				return True;
2552 			}
2553 
2554 			/* start looking at the upper slot or lower? */
2555 			if (ex_y * ICON_SIZE < (req_y + offset - dy))
2556 				sig = 1;
2557 			else
2558 				sig = -1;
2559 
2560 			done = 0;
2561 			/* look for closest free slot */
2562 			for (i = 0; i < (DOCK_DETTACH_THRESHOLD + 1) * 2 && !done; i++) {
2563 				int j;
2564 
2565 				done = 1;
2566 				closest = sig * (i / 2) + ex_y;
2567 				/* check if this slot is fully on the screen and not used */
2568 				if (onScreen(scr, dx, dy + closest * ICON_SIZE)) {
2569 					for (j = 0; j < dock->max_icons; j++) {
2570 						if (dock->icon_array[j]
2571 							&& dock->icon_array[j]->yindex == closest) {
2572 							/* slot is used by someone else */
2573 							if (dock->icon_array[j] != icon)
2574 								done = 0;
2575 							break;
2576 						}
2577 					}
2578 					/* slot is used by a drawer */
2579 					done = done && !getDrawer(scr, closest);
2580 				}
2581 				else // !onScreen
2582 					done = 0;
2583 				sig = -sig;
2584 			}
2585 			if (done &&
2586 				((ex_y >= closest && ex_y - closest < DOCK_DETTACH_THRESHOLD + 1)
2587 					|| (ex_y < closest && closest - ex_y <= DOCK_DETTACH_THRESHOLD + 1))) {
2588 				*ret_x = 0;
2589 				*ret_y = closest;
2590 				return True;
2591 			}
2592 		} else {	/* !redocking */
2593 
2594 			/* if slot is free and the icon is close enough, return it */
2595 			if (!aicon && ex_x == 0) {
2596 				*ret_x = 0;
2597 				*ret_y = ex_y;
2598 				return True;
2599 			}
2600 		}
2601 		break;
2602 	case WM_CLIP:
2603 	{
2604 		int neighbours = 0;
2605 		int start, stop, k;
2606 
2607 		start = icon->omnipresent ? 0 : scr->current_workspace;
2608 		stop = icon->omnipresent ? scr->workspace_count : start + 1;
2609 
2610 		aicon = NULL;
2611 		for (k = start; k < stop; k++) {
2612 			WDock *tmp = scr->workspaces[k]->clip;
2613 			if (!tmp)
2614 				continue;
2615 			for (i = 0; i < tmp->max_icons; i++) {
2616 				nicon = tmp->icon_array[i];
2617 				if (nicon && nicon->xindex == ex_x && nicon->yindex == ex_y) {
2618 					aicon = nicon;
2619 					break;
2620 				}
2621 			}
2622 			if (aicon)
2623 				break;
2624 		}
2625 		for (k = start; k < stop; k++) {
2626 			WDock *tmp = scr->workspaces[k]->clip;
2627 			if (!tmp)
2628 				continue;
2629 			for (i = 0; i < tmp->max_icons; i++) {
2630 				nicon = tmp->icon_array[i];
2631 				if (nicon && nicon != icon &&	/* Icon can't be it's own neighbour */
2632 				    (abs(nicon->xindex - ex_x) <= CLIP_ATTACH_VICINITY &&
2633 				     abs(nicon->yindex - ex_y) <= CLIP_ATTACH_VICINITY)) {
2634 					neighbours = 1;
2635 					break;
2636 				}
2637 			}
2638 			if (neighbours)
2639 				break;
2640 		}
2641 
2642 		if (neighbours && (aicon == NULL || (redocking && aicon == icon))) {
2643 			*ret_x = ex_x;
2644 			*ret_y = ex_y;
2645 			return True;
2646 		}
2647 		break;
2648 	}
2649 	case WM_DRAWER:
2650 	{
2651 		WAppIcon *aicons_to_shift[ dock->icon_count ];
2652 		int index_of_hole, j;
2653 
2654 		if (ex_y != 0 ||
2655 			abs(ex_x) - dock->icon_count > DOCK_DETTACH_THRESHOLD ||
2656 			(ex_x < 0 && !dock->on_right_side) ||
2657 			(ex_x > 0 &&  dock->on_right_side)) {
2658 			return False;
2659 		}
2660 
2661 		if (ex_x == 0)
2662 			ex_x = (dock->on_right_side ? -1 : 1);
2663 
2664 		/* "Reduce" ex_x but keep its sign */
2665 		if (redocking) {
2666 			if (abs(ex_x) > dock->icon_count - 1) /* minus 1: do not take icon_array[0] into account */
2667 				ex_x = ex_x * (dock->icon_count - 1) / abs(ex_x); /* don't use *= ! */
2668 		} else {
2669 			if (abs(ex_x) > dock->icon_count)
2670 				ex_x = ex_x * dock->icon_count / abs(ex_x);
2671 		}
2672 		index_of_hole = indexOfHole(dock, icon, redocking);
2673 
2674 		/* Find the appicons between where icon was (index_of_hole) and where
2675 		 * it wants to be (ex_x) and slide them. */
2676 		j = 0;
2677 		for (i = 1; i < dock->max_icons; i++) {
2678 			aicon = dock->icon_array[i];
2679 			if ((aicon != NULL) && (aicon != icon) &&
2680 				((ex_x <= aicon->xindex && aicon->xindex < index_of_hole) ||
2681 					(index_of_hole < aicon->xindex && aicon->xindex <= ex_x)))
2682 				aicons_to_shift[ j++ ] = aicon;
2683 		}
2684 		assert(j == abs(ex_x - index_of_hole));
2685 
2686 		wSlideAppicons(aicons_to_shift, j, (index_of_hole < ex_x));
2687 
2688 		*ret_x = ex_x;
2689 		*ret_y = ex_y;
2690 		return True;
2691 	}
2692 	}
2693 	return False;
2694 }
2695 
onScreen(WScreen * scr,int x,int y)2696 static int onScreen(WScreen *scr, int x, int y)
2697 {
2698 	WMRect rect;
2699 	int flags;
2700 
2701 	rect.pos.x = x;
2702 	rect.pos.y = y;
2703 	rect.size.width = rect.size.height = ICON_SIZE;
2704 
2705 	wGetRectPlacementInfo(scr, rect, &flags);
2706 
2707 	return !(flags & (XFLAG_DEAD | XFLAG_PARTIAL));
2708 }
2709 
2710 /*
2711  * returns true if it can find a free slot in the dock,
2712  * in which case it changes x_pos and y_pos accordingly.
2713  * Else returns false.
2714  */
wDockFindFreeSlot(WDock * dock,int * x_pos,int * y_pos)2715 Bool wDockFindFreeSlot(WDock *dock, int *x_pos, int *y_pos)
2716 {
2717 	WScreen *scr = dock->screen_ptr;
2718 	WAppIcon *btn;
2719 	WAppIconChain *chain;
2720 	unsigned char *slot_map;
2721 	int mwidth;
2722 	int r;
2723 	int x, y;
2724 	int i, done = False;
2725 	int corner;
2726 	int ex = scr->scr_width, ey = scr->scr_height;
2727 	int extra_count = 0;
2728 
2729 	if (dock->type == WM_DRAWER) {
2730 		if (dock->icon_count >= dock->max_icons) { /* drawer is full */
2731 			return False;
2732 		}
2733 		*x_pos = dock->icon_count * (dock->on_right_side ? -1 : 1);
2734 		*y_pos = 0;
2735 		return True;
2736 	}
2737 
2738 	if (dock->type == WM_CLIP && dock != scr->workspaces[scr->current_workspace]->clip)
2739 		extra_count = scr->global_icon_count;
2740 
2741 	/* if the dock is full */
2742 	if (dock->icon_count + extra_count >= dock->max_icons)
2743 		return False;
2744 
2745 	if (!wPreferences.flags.nodock && scr->dock && scr->dock->on_right_side) {
2746 		ex -= ICON_SIZE + DOCK_EXTRA_SPACE;
2747 	}
2748 
2749 	if (ex < dock->x_pos)
2750 		ex = dock->x_pos;
2751 #define C_NONE 0
2752 #define C_NW 1
2753 #define C_NE 2
2754 #define C_SW 3
2755 #define C_SE 4
2756 
2757 	/* check if clip is in a corner */
2758 	if (dock->type == WM_CLIP) {
2759 		if (dock->x_pos < 1 && dock->y_pos < 1)
2760 			corner = C_NE;
2761 		else if (dock->x_pos < 1 && dock->y_pos >= (ey - ICON_SIZE))
2762 			corner = C_SE;
2763 		else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos >= (ey - ICON_SIZE))
2764 			corner = C_SW;
2765 		else if (dock->x_pos >= (ex - ICON_SIZE) && dock->y_pos < 1)
2766 			corner = C_NW;
2767 		else
2768 			corner = C_NONE;
2769 	} else {
2770 		corner = C_NONE;
2771 	}
2772 
2773 	/* If the clip is in the corner, use only slots that are in the border
2774 	 * of the screen */
2775 	if (corner != C_NONE) {
2776 		char *hmap, *vmap;
2777 		int hcount, vcount;
2778 
2779 		hcount = WMIN(dock->max_icons, scr->scr_width / ICON_SIZE);
2780 		vcount = WMIN(dock->max_icons, scr->scr_height / ICON_SIZE);
2781 		hmap = wmalloc(hcount + 1);
2782 		vmap = wmalloc(vcount + 1);
2783 
2784 		/* mark used positions */
2785 		switch (corner) {
2786 		case C_NE:
2787 			for (i = 0; i < dock->max_icons; i++) {
2788 				btn = dock->icon_array[i];
2789 				if (!btn)
2790 					continue;
2791 
2792 				if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2793 					vmap[btn->yindex] = 1;
2794 				else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2795 					hmap[btn->xindex] = 1;
2796 			}
2797 			for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2798 				btn = chain->aicon;
2799 				if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2800 					vmap[btn->yindex] = 1;
2801 				else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2802 					hmap[btn->xindex] = 1;
2803 			}
2804 			break;
2805 		case C_NW:
2806 			for (i = 0; i < dock->max_icons; i++) {
2807 				btn = dock->icon_array[i];
2808 				if (!btn)
2809 					continue;
2810 
2811 				if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2812 					vmap[btn->yindex] = 1;
2813 				else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2814 					hmap[-btn->xindex] = 1;
2815 			}
2816 			for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2817 				btn = chain->aicon;
2818 				if (btn->xindex == 0 && btn->yindex > 0 && btn->yindex < vcount)
2819 					vmap[btn->yindex] = 1;
2820 				else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2821 					hmap[-btn->xindex] = 1;
2822 			}
2823 			break;
2824 		case C_SE:
2825 			for (i = 0; i < dock->max_icons; i++) {
2826 				btn = dock->icon_array[i];
2827 				if (!btn)
2828 					continue;
2829 
2830 				if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2831 					vmap[-btn->yindex] = 1;
2832 				else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2833 					hmap[btn->xindex] = 1;
2834 			}
2835 			for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2836 				btn = chain->aicon;
2837 				if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2838 					vmap[-btn->yindex] = 1;
2839 				else if (btn->yindex == 0 && btn->xindex > 0 && btn->xindex < hcount)
2840 					hmap[btn->xindex] = 1;
2841 			}
2842 			break;
2843 		case C_SW:
2844 		default:
2845 			for (i = 0; i < dock->max_icons; i++) {
2846 				btn = dock->icon_array[i];
2847 				if (!btn)
2848 					continue;
2849 
2850 				if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2851 					vmap[-btn->yindex] = 1;
2852 				else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2853 					hmap[-btn->xindex] = 1;
2854 			}
2855 			for (chain = scr->global_icons; chain != NULL; chain = chain->next) {
2856 				btn = chain->aicon;
2857 				if (btn->xindex == 0 && btn->yindex < 0 && btn->yindex > -vcount)
2858 					vmap[-btn->yindex] = 1;
2859 				else if (btn->yindex == 0 && btn->xindex < 0 && btn->xindex > -hcount)
2860 					hmap[-btn->xindex] = 1;
2861 			}
2862 		}
2863 		x = 0;
2864 		y = 0;
2865 		done = 0;
2866 		/* search a vacant slot */
2867 		for (i = 1; i < WMAX(vcount, hcount); i++) {
2868 			if (i < vcount && vmap[i] == 0) {
2869 				/* found a slot */
2870 				x = 0;
2871 				y = i;
2872 				done = 1;
2873 				break;
2874 			} else if (i < hcount && hmap[i] == 0) {
2875 				/* found a slot */
2876 				x = i;
2877 				y = 0;
2878 				done = 1;
2879 				break;
2880 			}
2881 		}
2882 		wfree(vmap);
2883 		wfree(hmap);
2884 		/* If found a slot, translate and return */
2885 		if (done) {
2886 			if (corner == C_NW || corner == C_NE)
2887 				*y_pos = y;
2888 			else
2889 				*y_pos = -y;
2890 
2891 			if (corner == C_NE || corner == C_SE)
2892 				*x_pos = x;
2893 			else
2894 				*x_pos = -x;
2895 
2896 			return True;
2897 		}
2898 		/* else, try to find a slot somewhere else */
2899 	}
2900 
2901 	/* a map of mwidth x mwidth would be enough if we allowed icons to be
2902 	 * placed outside of screen */
2903 	mwidth = (int)ceil(sqrt(dock->max_icons));
2904 
2905 	/* In the worst case (the clip is in the corner of the screen),
2906 	 * the amount of icons that fit in the clip is smaller.
2907 	 * Double the map to get a safe value.
2908 	 */
2909 	mwidth += mwidth;
2910 
2911 	r = (mwidth - 1) / 2;
2912 
2913 	slot_map = wmalloc(mwidth * mwidth);
2914 
2915 #define XY2OFS(x,y) (WMAX(abs(x),abs(y)) > r) ? 0 : (((y)+r)*(mwidth)+(x)+r)
2916 
2917 	/* mark used slots in the map. If the slot falls outside the map
2918 	 * (for example, when all icons are placed in line), ignore them. */
2919 	for (i = 0; i < dock->max_icons; i++) {
2920 		btn = dock->icon_array[i];
2921 		if (btn)
2922 			slot_map[XY2OFS(btn->xindex, btn->yindex)] = 1;
2923 	}
2924 
2925 	for (chain = scr->global_icons; chain != NULL; chain = chain->next)
2926 		slot_map[XY2OFS(chain->aicon->xindex, chain->aicon->yindex)] = 1;
2927 
2928 	/* Find closest slot from the center that is free by scanning the
2929 	 * map from the center to outward in circular passes.
2930 	 * This will not result in a neat layout, but will be optimal
2931 	 * in the sense that there will not be holes left.
2932 	 */
2933 	done = 0;
2934 	for (i = 1; i <= r && !done; i++) {
2935 		int tx, ty;
2936 
2937 		/* top and bottom parts of the ring */
2938 		for (x = -i; x <= i && !done; x++) {
2939 			tx = dock->x_pos + x * ICON_SIZE;
2940 			y = -i;
2941 			ty = dock->y_pos + y * ICON_SIZE;
2942 			if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2943 				*x_pos = x;
2944 				*y_pos = y;
2945 				done = 1;
2946 				break;
2947 			}
2948 			y = i;
2949 			ty = dock->y_pos + y * ICON_SIZE;
2950 			if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2951 				*x_pos = x;
2952 				*y_pos = y;
2953 				done = 1;
2954 				break;
2955 			}
2956 		}
2957 		/* left and right parts of the ring */
2958 		for (y = -i + 1; y <= i - 1; y++) {
2959 			ty = dock->y_pos + y * ICON_SIZE;
2960 			x = -i;
2961 			tx = dock->x_pos + x * ICON_SIZE;
2962 			if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2963 				*x_pos = x;
2964 				*y_pos = y;
2965 				done = 1;
2966 				break;
2967 			}
2968 			x = i;
2969 			tx = dock->x_pos + x * ICON_SIZE;
2970 			if (slot_map[XY2OFS(x, y)] == 0 && onScreen(scr, tx, ty)) {
2971 				*x_pos = x;
2972 				*y_pos = y;
2973 				done = 1;
2974 				break;
2975 			}
2976 		}
2977 	}
2978 	wfree(slot_map);
2979 #undef XY2OFS
2980 	return done;
2981 }
2982 
moveDock(WDock * dock,int new_x,int new_y)2983 static void moveDock(WDock *dock, int new_x, int new_y)
2984 {
2985 	WAppIcon *btn;
2986 	WDrawerChain *dc;
2987 	int i;
2988 
2989 	if (dock->type == WM_DOCK) {
2990 		for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
2991 			moveDock(dc->adrawer, new_x, dc->adrawer->y_pos - dock->y_pos + new_y);
2992 	}
2993 
2994 	dock->x_pos = new_x;
2995 	dock->y_pos = new_y;
2996 	for (i = 0; i < dock->max_icons; i++) {
2997 		btn = dock->icon_array[i];
2998 		if (btn) {
2999 			btn->x_pos = new_x + btn->xindex * ICON_SIZE;
3000 			btn->y_pos = new_y + btn->yindex * ICON_SIZE;
3001 			XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3002 		}
3003 	}
3004 }
3005 
swapDock(WDock * dock)3006 static void swapDock(WDock *dock)
3007 {
3008 	WScreen *scr = dock->screen_ptr;
3009 	WAppIcon *btn;
3010 	int x, i;
3011 
3012 	if (dock->on_right_side)
3013 		x = dock->x_pos = scr->scr_width - ICON_SIZE - DOCK_EXTRA_SPACE;
3014 	else
3015 		x = dock->x_pos = DOCK_EXTRA_SPACE;
3016 
3017 	swapDrawers(scr, x);
3018 
3019 	for (i = 0; i < dock->max_icons; i++) {
3020 		btn = dock->icon_array[i];
3021 		if (btn) {
3022 			btn->x_pos = x;
3023 			XMoveWindow(dpy, btn->icon->core->window, btn->x_pos, btn->y_pos);
3024 		}
3025 	}
3026 
3027 	wScreenUpdateUsableArea(scr);
3028 }
3029 
execCommand(WAppIcon * btn,const char * command,WSavedState * state)3030 static pid_t execCommand(WAppIcon *btn, const char *command, WSavedState *state)
3031 {
3032 	WScreen *scr = btn->icon->core->screen_ptr;
3033 	pid_t pid;
3034 	char **argv;
3035 	int argc;
3036 	char *cmdline;
3037 
3038 	cmdline = ExpandOptions(scr, command);
3039 
3040 	if (scr->flags.dnd_data_convertion_status || !cmdline) {
3041 		if (cmdline)
3042 			wfree(cmdline);
3043 		if (state)
3044 			wfree(state);
3045 		return 0;
3046 	}
3047 
3048 	wtokensplit(cmdline, &argv, &argc);
3049 
3050 	if (!argc) {
3051 		if (cmdline)
3052 			wfree(cmdline);
3053 		if (state)
3054 			wfree(state);
3055 		return 0;
3056 	}
3057 
3058 	pid = fork();
3059 	if (pid == 0) {
3060 		char **args;
3061 		int i;
3062 
3063 		SetupEnvironment(scr);
3064 
3065 #ifdef HAVE_SETSID
3066 		setsid();
3067 #endif
3068 
3069 		args = malloc(sizeof(char *) * (argc + 1));
3070 		if (!args)
3071 			exit(111);
3072 
3073 		for (i = 0; i < argc; i++)
3074 			args[i] = argv[i];
3075 
3076 		args[argc] = NULL;
3077 		execvp(argv[0], args);
3078 		exit(111);
3079 	}
3080 	wtokenfree(argv, argc);
3081 
3082 	if (pid > 0) {
3083 		if (!state) {
3084 			state = wmalloc(sizeof(WSavedState));
3085 			state->hidden = -1;
3086 			state->miniaturized = -1;
3087 			state->shaded = -1;
3088 			if (btn->dock == scr->dock || btn->dock->type == WM_DRAWER || btn->omnipresent)
3089 				state->workspace = -1;
3090 			else
3091 				state->workspace = scr->current_workspace;
3092 		}
3093 		wWindowAddSavedState(btn->wm_instance, btn->wm_class, cmdline, pid, state);
3094 		wAddDeathHandler(pid, trackDeadProcess, btn->dock);
3095 	} else if (state) {
3096 		wfree(state);
3097 	}
3098 	wfree(cmdline);
3099 	return pid;
3100 }
3101 
wDockHideIcons(WDock * dock)3102 void wDockHideIcons(WDock *dock)
3103 {
3104 	int i;
3105 
3106 	if (dock == NULL)
3107 		return;
3108 
3109 	for (i = 1; i < dock->max_icons; i++) {
3110 		if (dock->icon_array[i])
3111 			XUnmapWindow(dpy, dock->icon_array[i]->icon->core->window);
3112 	}
3113 	dock->mapped = 0;
3114 
3115 	dockIconPaint(dock->icon_array[0]);
3116 }
3117 
wDockShowIcons(WDock * dock)3118 void wDockShowIcons(WDock *dock)
3119 {
3120 	int i;
3121 	WAppIcon *btn;
3122 
3123 	if (dock == NULL)
3124 		return;
3125 
3126 	btn = dock->icon_array[0];
3127 	moveDock(dock, btn->x_pos, btn->y_pos);
3128 
3129 	/* Deleting any change in stacking level, this function is now only about
3130 	   mapping icons */
3131 
3132 	if (!dock->collapsed) {
3133 		for (i = 1; i < dock->max_icons; i++) {
3134 			if (dock->icon_array[i])
3135 				XMapWindow(dpy, dock->icon_array[i]->icon->core->window);
3136 		}
3137 	}
3138 	dock->mapped = 1;
3139 
3140 	dockIconPaint(btn);
3141 }
3142 
wDockLower(WDock * dock)3143 void wDockLower(WDock *dock)
3144 {
3145 	int i;
3146 	WDrawerChain *dc;
3147 
3148 	if (dock->type == WM_DOCK) {
3149 		for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3150 			wDockLower(dc->adrawer);
3151 	}
3152 	for (i = 0; i < dock->max_icons; i++) {
3153 		if (dock->icon_array[i])
3154 			wLowerFrame(dock->icon_array[i]->icon->core);
3155 	}
3156 }
3157 
wDockRaise(WDock * dock)3158 void wDockRaise(WDock *dock)
3159 {
3160 	int i;
3161 	WDrawerChain *dc;
3162 
3163 	for (i = dock->max_icons - 1; i >= 0; i--) {
3164 		if (dock->icon_array[i])
3165 			wRaiseFrame(dock->icon_array[i]->icon->core);
3166 	}
3167 	if (dock->type == WM_DOCK) {
3168 		for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next)
3169 			wDockRaise(dc->adrawer);
3170 	}
3171 }
3172 
wDockRaiseLower(WDock * dock)3173 void wDockRaiseLower(WDock *dock)
3174 {
3175 	if (!dock->icon_array[0]->icon->core->stacking->above
3176 	    || (dock->icon_array[0]->icon->core->stacking->window_level
3177 		!= dock->icon_array[0]->icon->core->stacking->above->stacking->window_level))
3178 		wDockLower(dock);
3179 	else
3180 		wDockRaise(dock);
3181 }
3182 
wDockFinishLaunch(WAppIcon * icon)3183 void wDockFinishLaunch(WAppIcon *icon)
3184 {
3185 	icon->launching = 0;
3186 	icon->relaunching = 0;
3187 	dockIconPaint(icon);
3188 }
3189 
wDockFindIconForWindow(WDock * dock,Window window)3190 WAppIcon *wDockFindIconForWindow(WDock *dock, Window window)
3191 {
3192 	WAppIcon *icon;
3193 	int i;
3194 
3195 	for (i = 0; i < dock->max_icons; i++) {
3196 		icon = dock->icon_array[i];
3197 		if (icon && icon->main_window == window)
3198 			return icon;
3199 	}
3200 	return NULL;
3201 }
3202 
wDockTrackWindowLaunch(WDock * dock,Window window)3203 void wDockTrackWindowLaunch(WDock *dock, Window window)
3204 {
3205 	WAppIcon *icon;
3206 	char *wm_class, *wm_instance;
3207 	int i;
3208 	Bool firstPass = True;
3209 	Bool found = False;
3210 	char *command = NULL;
3211 
3212 	if (!PropGetWMClass(window, &wm_class, &wm_instance)) {
3213 		free(wm_class);
3214 		free(wm_instance);
3215 		return;
3216 	}
3217 
3218 	command = GetCommandForWindow(window);
3219  retry:
3220 	for (i = 0; i < dock->max_icons; i++) {
3221 		icon = dock->icon_array[i];
3222 		if (!icon)
3223 			continue;
3224 
3225 		/* app is already attached to icon */
3226 		if (icon->main_window == window) {
3227 			found = True;
3228 			break;
3229 		}
3230 
3231 		if ((icon->wm_instance || icon->wm_class)
3232 		    && (icon->launching || !icon->running)) {
3233 
3234 			if (icon->wm_instance && wm_instance && strcmp(icon->wm_instance, wm_instance) != 0)
3235 				continue;
3236 
3237 			if (icon->wm_class && wm_class && strcmp(icon->wm_class, wm_class) != 0)
3238 				continue;
3239 
3240 			if (firstPass && command && strcmp(icon->command, command) != 0)
3241 				continue;
3242 
3243 			if (!icon->relaunching) {
3244 				WApplication *wapp;
3245 
3246 				/* Possibly an application that was docked with dockit,
3247 				 * but the user did not update WMState to indicate that
3248 				 * it was docked by force */
3249 				wapp = wApplicationOf(window);
3250 				if (!wapp) {
3251 					icon->forced_dock = 1;
3252 					icon->running = 0;
3253 				}
3254 				if (!icon->forced_dock)
3255 					icon->main_window = window;
3256 			}
3257 			found = True;
3258 			if (!wPreferences.no_animations && !icon->launching &&
3259 			    !dock->screen_ptr->flags.startup && !dock->collapsed) {
3260 				WAppIcon *aicon;
3261 				int x0, y0;
3262 
3263 				icon->launching = 1;
3264 				dockIconPaint(icon);
3265 
3266 				aicon = wAppIconCreateForDock(dock->screen_ptr, NULL,
3267 							      wm_instance, wm_class, TILE_NORMAL);
3268 				/* XXX: can: aicon->icon == NULL ? */
3269 				PlaceIcon(dock->screen_ptr, &x0, &y0, wGetHeadForWindow(aicon->icon->owner));
3270 				wAppIconMove(aicon, x0, y0);
3271 				/* Should this always be lowered? -Dan */
3272 				if (dock->lowered)
3273 					wLowerFrame(aicon->icon->core);
3274 				XMapWindow(dpy, aicon->icon->core->window);
3275 				aicon->launching = 1;
3276 				wAppIconPaint(aicon);
3277 				slide_window(aicon->icon->core->window, x0, y0, icon->x_pos, icon->y_pos);
3278 				XUnmapWindow(dpy, aicon->icon->core->window);
3279 				wAppIconDestroy(aicon);
3280 			}
3281 			wDockFinishLaunch(icon);
3282 			break;
3283 		}
3284 	}
3285 
3286 	if (firstPass && !found) {
3287 		firstPass = False;
3288 		goto retry;
3289 	}
3290 
3291 	if (command)
3292 		wfree(command);
3293 
3294 	if (wm_class)
3295 		free(wm_class);
3296 	if (wm_instance)
3297 		free(wm_instance);
3298 }
3299 
wClipUpdateForWorkspaceChange(WScreen * scr,int workspace)3300 void wClipUpdateForWorkspaceChange(WScreen *scr, int workspace)
3301 {
3302 	if (!wPreferences.flags.noclip) {
3303 		scr->clip_icon->dock = scr->workspaces[workspace]->clip;
3304 		if (scr->current_workspace != workspace) {
3305 			WDock *old_clip = scr->workspaces[scr->current_workspace]->clip;
3306 			WAppIconChain *chain = scr->global_icons;
3307 
3308 			while (chain) {
3309 				wDockMoveIconBetweenDocks(chain->aicon->dock,
3310 						     scr->workspaces[workspace]->clip,
3311 						     chain->aicon, chain->aicon->xindex, chain->aicon->yindex);
3312 				if (scr->workspaces[workspace]->clip->collapsed)
3313 					XUnmapWindow(dpy, chain->aicon->icon->core->window);
3314 				chain = chain->next;
3315 			}
3316 
3317 			wDockHideIcons(old_clip);
3318 			if (old_clip->auto_raise_lower) {
3319 				if (old_clip->auto_raise_magic) {
3320 					WMDeleteTimerHandler(old_clip->auto_raise_magic);
3321 					old_clip->auto_raise_magic = NULL;
3322 				}
3323 				wDockLower(old_clip);
3324 			}
3325 			if (old_clip->auto_collapse) {
3326 				if (old_clip->auto_expand_magic) {
3327 					WMDeleteTimerHandler(old_clip->auto_expand_magic);
3328 					old_clip->auto_expand_magic = NULL;
3329 				}
3330 				old_clip->collapsed = 1;
3331 			}
3332 			wDockShowIcons(scr->workspaces[workspace]->clip);
3333 		}
3334 	}
3335 }
3336 
trackDeadProcess(pid_t pid,unsigned int status,void * cdata)3337 static void trackDeadProcess(pid_t pid, unsigned int status, void *cdata)
3338 {
3339 	WDock *dock = cdata;
3340 	WAppIcon *icon;
3341 	int i;
3342 
3343 	for (i = 0; i < dock->max_icons; i++) {
3344 		icon = dock->icon_array[i];
3345 		if (!icon)
3346 			continue;
3347 
3348 		if (icon->launching && icon->pid == pid) {
3349 			if (!icon->relaunching) {
3350 				icon->running = 0;
3351 				icon->main_window = None;
3352 			}
3353 			wDockFinishLaunch(icon);
3354 			icon->pid = 0;
3355 			if (status == 111) {
3356 				char msg[PATH_MAX];
3357 				char *cmd;
3358 
3359 #ifdef USE_DOCK_XDND
3360 				if (icon->drop_launch)
3361 					cmd = icon->dnd_command;
3362 				else
3363 #endif
3364 				if (icon->paste_launch)
3365 					cmd = icon->paste_command;
3366 				else
3367 					cmd = icon->command;
3368 
3369 				snprintf(msg, sizeof(msg), _("Could not execute command \"%s\""), cmd);
3370 
3371 				wMessageDialog(dock->screen_ptr, _("Error"), msg, _("OK"), NULL, NULL);
3372 			}
3373 			break;
3374 		}
3375 	}
3376 }
3377 
3378 /* This function is called when the dock switches state between
3379  * "normal" (including auto-raise/lower) and "keep on top". It is
3380  * therefore clearly distinct from wDockLower/Raise, which are called
3381  * each time a not-kept-on-top dock is lowered/raised. */
toggleLowered(WDock * dock)3382 static void toggleLowered(WDock *dock)
3383 {
3384 	WAppIcon *tmp;
3385 	WDrawerChain *dc;
3386 	int newlevel, i;
3387 
3388 	if (!dock->lowered) {
3389 		newlevel = WMNormalLevel;
3390 		dock->lowered = 1;
3391 	} else {
3392 		newlevel = WMDockLevel;
3393 		dock->lowered = 0;
3394 	}
3395 
3396 	for (i = 0; i < dock->max_icons; i++) {
3397 		tmp = dock->icon_array[i];
3398 		if (!tmp)
3399 			continue;
3400 
3401 		ChangeStackingLevel(tmp->icon->core, newlevel);
3402 
3403 		/* When the dock is no longer "on top", explicitly lower it as well.
3404 		 * It saves some CPU cycles (probably) to do it ourselves here
3405 		 * rather than calling wDockLower at the end of toggleLowered */
3406 		if (dock->lowered)
3407 			wLowerFrame(tmp->icon->core);
3408 	}
3409 
3410 	if (dock->type == WM_DOCK) {
3411 		for (dc = dock->screen_ptr->drawers; dc != NULL; dc = dc->next) {
3412 			toggleLowered(dc->adrawer);
3413 		}
3414 		wScreenUpdateUsableArea(dock->screen_ptr);
3415 	}
3416 }
3417 
toggleCollapsed(WDock * dock)3418 static void toggleCollapsed(WDock *dock)
3419 {
3420 	if (dock->collapsed) {
3421 		dock->collapsed = 0;
3422 		wDockShowIcons(dock);
3423 	} else {
3424 		dock->collapsed = 1;
3425 		wDockHideIcons(dock);
3426 	}
3427 }
3428 
openDockMenu(WDock * dock,WAppIcon * aicon,XEvent * event)3429 static void openDockMenu(WDock *dock, WAppIcon *aicon, XEvent *event)
3430 {
3431 	WScreen *scr = dock->screen_ptr;
3432 	WObjDescriptor *desc;
3433 	WMenuEntry *entry;
3434 	WApplication *wapp = NULL;
3435 	int index = 0;
3436 	int x_pos;
3437 	int n_selected;
3438 	int appIsRunning = aicon->running && aicon->icon && aicon->icon->owner;
3439 
3440 	if (dock->type == WM_DOCK) {
3441 		/* Dock position menu */
3442 		updateDockPositionMenu(scr->dock_pos_menu, dock);
3443 		dock->menu->flags.realized = 0;
3444 		if (!wPreferences.flags.nodrawer) {
3445 			/* add a drawer */
3446 			entry = dock->menu->entries[++index];
3447 			entry->clientdata = aicon;
3448 			wMenuSetEnabled(dock->menu, index, True);
3449 		}
3450 	} else {
3451 		/* clip/drawer options */
3452 		if (scr->clip_options)
3453 			updateClipOptionsMenu(scr->clip_options, dock);
3454 
3455 		n_selected = numberOfSelectedIcons(dock);
3456 
3457 		if (dock->type == WM_CLIP) {
3458 			/* Rename Workspace */
3459 			entry = dock->menu->entries[++index];
3460 			if (aicon == scr->clip_icon) {
3461 				entry->callback = renameCallback;
3462 				entry->clientdata = dock;
3463 				entry->flags.indicator = 0;
3464 				entry->text = _("Rename Workspace");
3465 			} else {
3466 				entry->callback = omnipresentCallback;
3467 				entry->clientdata = aicon;
3468 				if (n_selected > 0) {
3469 					entry->flags.indicator = 0;
3470 					entry->text = _("Toggle Omnipresent");
3471 				} else {
3472 					entry->flags.indicator = 1;
3473 					entry->flags.indicator_on = aicon->omnipresent;
3474 					entry->flags.indicator_type = MI_CHECK;
3475 					entry->text = _("Omnipresent");
3476 				}
3477 			}
3478 		}
3479 
3480 		/* select/unselect icon */
3481 		entry = dock->menu->entries[++index];
3482 		entry->clientdata = aicon;
3483 		entry->flags.indicator_on = aicon->icon->selected;
3484 		wMenuSetEnabled(dock->menu, index, aicon != scr->clip_icon && !wIsADrawer(aicon));
3485 
3486 		/* select/unselect all icons */
3487 		entry = dock->menu->entries[++index];
3488 		entry->clientdata = aicon;
3489 		if (n_selected > 0)
3490 			entry->text = _("Unselect All Icons");
3491 		else
3492 			entry->text = _("Select All Icons");
3493 
3494 		wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3495 
3496 		/* keep icon(s) */
3497 		entry = dock->menu->entries[++index];
3498 		entry->clientdata = aicon;
3499 		if (n_selected > 1)
3500 			entry->text = _("Keep Icons");
3501 		else
3502 			entry->text = _("Keep Icon");
3503 
3504 		wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3505 
3506 		if (dock->type == WM_CLIP) {
3507 			/* this is the workspace submenu part */
3508 			entry = dock->menu->entries[++index];
3509 			if (n_selected > 1)
3510 				entry->text = _("Move Icons To");
3511 			else
3512 				entry->text = _("Move Icon To");
3513 
3514 			if (scr->clip_submenu)
3515 				updateWorkspaceMenu(scr->clip_submenu, aicon);
3516 
3517 			wMenuSetEnabled(dock->menu, index, !aicon->omnipresent);
3518 		}
3519 
3520 		/* remove icon(s) */
3521 		entry = dock->menu->entries[++index];
3522 		entry->clientdata = aicon;
3523 		if (n_selected > 1)
3524 			entry->text = _("Remove Icons");
3525 		else
3526 			entry->text = _("Remove Icon");
3527 
3528 		wMenuSetEnabled(dock->menu, index, dock->icon_count > 1);
3529 
3530 		/* attract icon(s) */
3531 		entry = dock->menu->entries[++index];
3532 		entry->clientdata = aicon;
3533 
3534 		dock->menu->flags.realized = 0;
3535 		wMenuRealize(dock->menu);
3536 	}
3537 
3538 	if (aicon->icon->owner)
3539 		wapp = wApplicationOf(aicon->icon->owner->main_window);
3540 	else
3541 		wapp = NULL;
3542 
3543 	/* launch */
3544 	entry = dock->menu->entries[++index];
3545 	entry->clientdata = aicon;
3546 	wMenuSetEnabled(dock->menu, index, aicon->command != NULL);
3547 
3548 	/* unhide here */
3549 	entry = dock->menu->entries[++index];
3550 	entry->clientdata = aicon;
3551 	if (wapp && wapp->flags.hidden)
3552 		entry->text = _("Unhide Here");
3553 	else
3554 		entry->text = _("Bring Here");
3555 
3556 	wMenuSetEnabled(dock->menu, index, appIsRunning);
3557 
3558 	/* hide */
3559 	entry = dock->menu->entries[++index];
3560 	entry->clientdata = aicon;
3561 	if (wapp && wapp->flags.hidden)
3562 		entry->text = _("Unhide");
3563 	else
3564 		entry->text = _("Hide");
3565 
3566 	wMenuSetEnabled(dock->menu, index, appIsRunning);
3567 
3568 	/* settings */
3569 	entry = dock->menu->entries[++index];
3570 	entry->clientdata = aicon;
3571 	wMenuSetEnabled(dock->menu, index, !aicon->editing && !wPreferences.flags.noupdates);
3572 
3573 	/* kill or remove drawer */
3574 	entry = dock->menu->entries[++index];
3575 	entry->clientdata = aicon;
3576 	if (wIsADrawer(aicon)) {
3577 		entry->callback = removeDrawerCallback;
3578 		entry->text = _("Remove drawer");
3579 		wMenuSetEnabled(dock->menu, index, True);
3580 	} else {
3581 		entry->callback = killCallback;
3582 		entry->text = _("Kill");
3583 		wMenuSetEnabled(dock->menu, index, appIsRunning);
3584 	}
3585 
3586 	if (!dock->menu->flags.realized)
3587 		wMenuRealize(dock->menu);
3588 
3589 	if (dock->type == WM_CLIP || dock->type == WM_DRAWER) {
3590 		/*x_pos = event->xbutton.x_root+2; */
3591 		x_pos = event->xbutton.x_root - dock->menu->frame->core->width / 2 - 1;
3592 		if (x_pos < 0) {
3593 			x_pos = 0;
3594 		} else if (x_pos + dock->menu->frame->core->width > scr->scr_width - 2) {
3595 			x_pos = scr->scr_width - dock->menu->frame->core->width - 4;
3596 		}
3597 	} else {
3598 		x_pos = dock->on_right_side ? scr->scr_width - dock->menu->frame->core->width - 3 : 0;
3599 	}
3600 
3601 	wMenuMapAt(dock->menu, x_pos, event->xbutton.y_root + 2, False);
3602 
3603 	/* allow drag select */
3604 	event->xany.send_event = True;
3605 	desc = &dock->menu->menu->descriptor;
3606 	(*desc->handle_mousedown) (desc, event);
3607 }
3608 
3609 /******************************************************************/
iconDblClick(WObjDescriptor * desc,XEvent * event)3610 static void iconDblClick(WObjDescriptor *desc, XEvent *event)
3611 {
3612 	WAppIcon *btn = desc->parent;
3613 	WDock *dock = btn->dock;
3614 	WApplication *wapp = NULL;
3615 	int unhideHere = 0;
3616 
3617 	if (btn->icon->owner && !(event->xbutton.state & ControlMask)) {
3618 		wapp = wApplicationOf(btn->icon->owner->main_window);
3619 
3620 		assert(wapp != NULL);
3621 
3622 		unhideHere = (event->xbutton.state & ShiftMask);
3623 
3624 		/* go to the last workspace that the user worked on the app */
3625 		if (wapp->last_workspace != dock->screen_ptr->current_workspace && !unhideHere)
3626 			wWorkspaceChange(dock->screen_ptr, wapp->last_workspace);
3627 
3628 		wUnhideApplication(wapp, event->xbutton.button == Button2, unhideHere);
3629 
3630 		if (event->xbutton.state & MOD_MASK)
3631 			wHideOtherApplications(btn->icon->owner);
3632 	} else {
3633 		if (event->xbutton.button == Button1) {
3634 			if (event->xbutton.state & MOD_MASK) {
3635 				/* raise/lower dock */
3636 				toggleLowered(dock);
3637 			} else if (btn == dock->screen_ptr->clip_icon) {
3638 				if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE)
3639 					handleClipChangeWorkspace(dock->screen_ptr, event);
3640 				else if (wPreferences.flags.clip_merged_in_dock) {
3641 					// Is actually the dock
3642 					if (btn->command)
3643 					{
3644 						if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3645 							launchDockedApplication(btn, False);
3646 					}
3647 					else
3648 					{
3649 						wShowInfoPanel(dock->screen_ptr);
3650 					}
3651 				}
3652 				else
3653 					toggleCollapsed(dock);
3654 			} else if (wIsADrawer(btn)) {
3655 				toggleCollapsed(dock);
3656 			} else if (btn->command) {
3657 				if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
3658 					launchDockedApplication(btn, False);
3659 			} else if (btn->xindex == 0 && btn->yindex == 0 && btn->dock->type == WM_DOCK) {
3660 				wShowInfoPanel(dock->screen_ptr);
3661 			}
3662 		}
3663 	}
3664 }
3665 
handleDockMove(WDock * dock,WAppIcon * aicon,XEvent * event)3666 static void handleDockMove(WDock *dock, WAppIcon *aicon, XEvent *event)
3667 {
3668 	WScreen *scr = dock->screen_ptr;
3669 	int ofs_x = event->xbutton.x, ofs_y = event->xbutton.y;
3670 	WIcon *icon = aicon->icon;
3671 	WAppIcon *tmpaicon;
3672 	WDrawerChain *dc;
3673 	int x = aicon->x_pos, y = aicon->y_pos;;
3674 	int shad_x = x, shad_y = y;
3675 	XEvent ev;
3676 	int grabbed = 0, done, previously_on_right, now_on_right, previous_x_pos, i;
3677 	Pixmap ghost = None;
3678 	int superfluous = wPreferences.superfluous;	/* we catch it to avoid problems */
3679 
3680 	if (XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
3681 			 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
3682 			 GrabModeAsync, None, None, CurrentTime) != GrabSuccess)
3683 		wwarning("pointer grab failed for dock move");
3684 
3685 	if (dock->type == WM_DRAWER) {
3686 		Window wins[2];
3687 		wins[0] = icon->core->window;
3688 		wins[1] = scr->dock_shadow;
3689 		XRestackWindows(dpy, wins, 2);
3690 		XMoveResizeWindow(dpy, scr->dock_shadow, aicon->x_pos, aicon->y_pos,
3691 				ICON_SIZE, ICON_SIZE);
3692 		if (superfluous) {
3693 			if (icon->pixmap!=None)
3694 				ghost = MakeGhostIcon(scr, icon->pixmap);
3695 			else
3696 				ghost = MakeGhostIcon(scr, icon->core->window);
3697 
3698 			XSetWindowBackgroundPixmap(dpy, scr->dock_shadow, ghost);
3699 			XClearWindow(dpy, scr->dock_shadow);
3700 		}
3701 		XMapWindow(dpy, scr->dock_shadow);
3702 	}
3703 
3704 	previously_on_right = now_on_right = dock->on_right_side;
3705 	previous_x_pos = dock->x_pos;
3706 	done = 0;
3707 	while (!done) {
3708 		WMMaskEvent(dpy, PointerMotionMask | ButtonReleaseMask | ButtonPressMask
3709 			    | ButtonMotionMask | ExposureMask | EnterWindowMask, &ev);
3710 		switch (ev.type) {
3711 		case Expose:
3712 			WMHandleEvent(&ev);
3713 			break;
3714 
3715                 case EnterNotify:
3716                         /* It means the cursor moved so fast that it entered
3717                          * something else (if moving slowly, it would have
3718                          * stayed in the dock that is being moved. Ignore such
3719                          * "spurious" EnterNotifiy's */
3720                         break;
3721 
3722 		case MotionNotify:
3723 			if (!grabbed) {
3724 				if (abs(ofs_x - ev.xmotion.x) >= MOVE_THRESHOLD
3725 				    || abs(ofs_y - ev.xmotion.y) >= MOVE_THRESHOLD) {
3726 					XChangeActivePointerGrab(dpy, ButtonMotionMask
3727 								 | ButtonReleaseMask | ButtonPressMask,
3728 								 wPreferences.cursor[WCUR_MOVE], CurrentTime);
3729 					grabbed = 1;
3730 				}
3731 				break;
3732 			}
3733 			switch (dock->type) {
3734 			case WM_CLIP:
3735 				x = ev.xmotion.x_root - ofs_x;
3736 				y = ev.xmotion.y_root - ofs_y;
3737 				wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3738 				moveDock(dock, x, y);
3739 				break;
3740 			case WM_DOCK:
3741 				x = ev.xmotion.x_root - ofs_x;
3742 				y = ev.xmotion.y_root - ofs_y;
3743 				if (previously_on_right)
3744 				{
3745 					now_on_right = (ev.xmotion.x_root >= previous_x_pos - ICON_SIZE);
3746 				}
3747 				else
3748 				{
3749 					now_on_right = (ev.xmotion.x_root > previous_x_pos + ICON_SIZE * 2);
3750 				}
3751 				if (now_on_right != dock->on_right_side)
3752 				{
3753 					dock->on_right_side = now_on_right;
3754 					swapDock(dock);
3755 					wArrangeIcons(scr, False);
3756 				}
3757 				// Also perform the vertical move
3758 				wScreenKeepInside(scr, &x, &y, ICON_SIZE, ICON_SIZE);
3759 				moveDock(dock, dock->x_pos, y);
3760 				if (wPreferences.flags.wrap_appicons_in_dock)
3761 				{
3762 					for (i = 0; i < dock->max_icons; i++) {
3763 						int new_y, new_index, j, ok;
3764 
3765 						tmpaicon = dock->icon_array[i];
3766 						if (tmpaicon == NULL)
3767 							continue;
3768 						if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3769 							continue;
3770 						new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3771 						new_index = (new_y - dock->y_pos) / ICON_SIZE;
3772 						if (!onScreen(scr, tmpaicon->x_pos, new_y))
3773 							continue;
3774 						ok = 1;
3775 						for (j = 0; j < dock->max_icons; j++)
3776 						{
3777 							if (dock->icon_array[j] != NULL &&
3778 								dock->icon_array[j]->yindex == new_index)
3779 							{
3780 								ok = 0;
3781 								break;
3782 							}
3783 						}
3784 						if (!ok || getDrawer(scr, new_index) != NULL)
3785 							continue;
3786 						wDockReattachIcon(dock, tmpaicon, tmpaicon->xindex, new_index);
3787 					}
3788 					for (dc = scr->drawers; dc != NULL; dc = dc->next)
3789 					{
3790 						int new_y, new_index, j, ok;
3791 						tmpaicon = dc->adrawer->icon_array[0];
3792 						if (onScreen(scr, tmpaicon->x_pos, tmpaicon->y_pos))
3793 							continue;
3794 						new_y = (tmpaicon->y_pos + ICON_SIZE * dock->max_icons) % (ICON_SIZE * dock->max_icons);
3795 						new_index = (new_y - dock->y_pos) / ICON_SIZE;
3796 						if (!onScreen(scr, tmpaicon->x_pos, new_y))
3797 							continue;
3798 						ok = 1;
3799 						for (j = 0; j < dock->max_icons; j++)
3800 						{
3801 							if (dock->icon_array[j] != NULL &&
3802 								dock->icon_array[j]->yindex == new_index)
3803 							{
3804 								ok = 0;
3805 								break;
3806 							}
3807 						}
3808 						if (!ok || getDrawer(scr, new_index) != NULL)
3809 							continue;
3810 						moveDock(dc->adrawer, tmpaicon->x_pos, new_y);
3811 					}
3812 				}
3813 				break;
3814 			case WM_DRAWER:
3815 			{
3816 				WDock *real_dock = dock->screen_ptr->dock;
3817 				Bool snapped;
3818 				int ix, iy;
3819 				x = ev.xmotion.x_root - ofs_x;
3820 				y = ev.xmotion.y_root - ofs_y;
3821 				snapped = wDockSnapIcon(real_dock, aicon, x, y, &ix, &iy, True);
3822 				if (snapped) {
3823 					shad_x = real_dock->x_pos + ix * wPreferences.icon_size;
3824 					shad_y = real_dock->y_pos + iy * wPreferences.icon_size;
3825 					XMoveWindow(dpy, scr->dock_shadow, shad_x, shad_y);
3826 				}
3827 				moveDock(dock, x, y);
3828 				break;
3829 			}
3830 			}
3831 			break;
3832 
3833 		case ButtonPress:
3834 			break;
3835 
3836 		case ButtonRelease:
3837 			if (ev.xbutton.button != event->xbutton.button)
3838 				break;
3839 			XUngrabPointer(dpy, CurrentTime);
3840 			if (dock->type == WM_DRAWER) {
3841 				Window wins[dock->icon_count];
3842 				int offset_index;
3843 
3844 				/*
3845 				 * When the dock is on the Right side, the index of the icons are negative to
3846 				 * reflect the fact that they are placed on the other side of the dock; we use
3847 				 * an offset here so we can have an always positive index for the storage in
3848 				 * the 'wins' array.
3849 				 */
3850 				if (dock->on_right_side)
3851 					offset_index = dock->icon_count - 1;
3852 				else
3853 					offset_index = 0;
3854 
3855 				for (i = 0; i < dock->max_icons; i++) {
3856 					tmpaicon = dock->icon_array[i];
3857 					if (tmpaicon == NULL)
3858 						continue;
3859 					wins[tmpaicon->xindex + offset_index] = tmpaicon->icon->core->window;
3860 				}
3861 				slide_windows(wins, dock->icon_count,
3862 					(dock->on_right_side ? x - (dock->icon_count - 1) * ICON_SIZE : x),
3863 					y,
3864 					(dock->on_right_side ? shad_x - (dock->icon_count - 1) * ICON_SIZE : shad_x),
3865 					shad_y);
3866 				XUnmapWindow(dpy, scr->dock_shadow);
3867 				moveDock(dock, shad_x, shad_y);
3868 				XResizeWindow(dpy, scr->dock_shadow, ICON_SIZE, ICON_SIZE);
3869 			}
3870 			done = 1;
3871 			break;
3872 		}
3873 	}
3874 	if (superfluous) {
3875 		if (ghost != None)
3876 			XFreePixmap(dpy, ghost);
3877 		XSetWindowBackground(dpy, scr->dock_shadow, scr->white_pixel);
3878 	}
3879 }
3880 
3881 
getClipButton(int px,int py)3882 static int getClipButton(int px, int py)
3883 {
3884 	int pt = (CLIP_BUTTON_SIZE + 2) * ICON_SIZE / 64;
3885 
3886 	if (px < 0 || py < 0 || px >= ICON_SIZE || py >= ICON_SIZE)
3887 		return CLIP_IDLE;
3888 
3889 	if (py <= pt - ((int)ICON_SIZE - 1 - px))
3890 		return CLIP_FORWARD;
3891 	else if (px <= pt - ((int)ICON_SIZE - 1 - py))
3892 		return CLIP_REWIND;
3893 
3894 	return CLIP_IDLE;
3895 }
3896 
handleClipChangeWorkspace(WScreen * scr,XEvent * event)3897 static void handleClipChangeWorkspace(WScreen *scr, XEvent *event)
3898 {
3899 	XEvent ev;
3900 	int done, direction, new_ws;
3901 	int new_dir;
3902 	WDock *clip = scr->clip_icon->dock;
3903 
3904 	direction = getClipButton(event->xbutton.x, event->xbutton.y);
3905 
3906 	clip->lclip_button_pushed = direction == CLIP_REWIND;
3907 	clip->rclip_button_pushed = direction == CLIP_FORWARD;
3908 
3909 	wClipIconPaint(scr->clip_icon);
3910 	done = 0;
3911 	while (!done) {
3912 		WMMaskEvent(dpy, ExposureMask | ButtonMotionMask | ButtonReleaseMask | ButtonPressMask, &ev);
3913 		switch (ev.type) {
3914 		case Expose:
3915 			WMHandleEvent(&ev);
3916 			break;
3917 
3918 		case MotionNotify:
3919 			new_dir = getClipButton(ev.xmotion.x, ev.xmotion.y);
3920 			if (new_dir != direction) {
3921 				direction = new_dir;
3922 				clip->lclip_button_pushed = direction == CLIP_REWIND;
3923 				clip->rclip_button_pushed = direction == CLIP_FORWARD;
3924 				wClipIconPaint(scr->clip_icon);
3925 			}
3926 			break;
3927 
3928 		case ButtonPress:
3929 			break;
3930 
3931 		case ButtonRelease:
3932 			if (ev.xbutton.button == event->xbutton.button)
3933 				done = 1;
3934 		}
3935 	}
3936 
3937 	clip->lclip_button_pushed = 0;
3938 	clip->rclip_button_pushed = 0;
3939 
3940 	new_ws = wPreferences.ws_advance || (event->xbutton.state & ControlMask);
3941 
3942 	if (direction == CLIP_FORWARD) {
3943 		if (scr->current_workspace < scr->workspace_count - 1)
3944 			wWorkspaceChange(scr, scr->current_workspace + 1);
3945 		else if (new_ws && scr->current_workspace < MAX_WORKSPACES - 1)
3946 			wWorkspaceChange(scr, scr->current_workspace + 1);
3947 		else if (wPreferences.ws_cycle)
3948 			wWorkspaceChange(scr, 0);
3949 	} else if (direction == CLIP_REWIND) {
3950 		if (scr->current_workspace > 0)
3951 			wWorkspaceChange(scr, scr->current_workspace - 1);
3952 		else if (scr->current_workspace == 0 && wPreferences.ws_cycle)
3953 			wWorkspaceChange(scr, scr->workspace_count - 1);
3954 	}
3955 
3956 	wClipIconPaint(scr->clip_icon);
3957 }
3958 
iconMouseDown(WObjDescriptor * desc,XEvent * event)3959 static void iconMouseDown(WObjDescriptor *desc, XEvent *event)
3960 {
3961 	WAppIcon *aicon = desc->parent;
3962 	WDock *dock = aicon->dock;
3963 	WScreen *scr = aicon->icon->core->screen_ptr;
3964 
3965 	if (aicon->editing || WCHECK_STATE(WSTATE_MODAL))
3966 		return;
3967 
3968 	scr->last_dock = dock;
3969 
3970 	if (dock->menu->flags.mapped)
3971 		wMenuUnmap(dock->menu);
3972 
3973 	if (IsDoubleClick(scr, event)) {
3974 		/* double-click was not in the main clip icon */
3975 		if (dock->type != WM_CLIP || aicon->xindex != 0 || aicon->yindex != 0
3976 		    || getClipButton(event->xbutton.x, event->xbutton.y) == CLIP_IDLE) {
3977 			iconDblClick(desc, event);
3978 			return;
3979 		}
3980 	}
3981 
3982 	if (event->xbutton.button == Button1) {
3983 		if (event->xbutton.state & MOD_MASK)
3984 			wDockLower(dock);
3985 		else
3986 			wDockRaise(dock);
3987 
3988 		if ((event->xbutton.state & ShiftMask) && aicon != scr->clip_icon && dock->type != WM_DOCK) {
3989 			wIconSelect(aicon->icon);
3990 			return;
3991 		}
3992 
3993 		if (aicon->yindex == 0 && aicon->xindex == 0) {
3994 			if (getClipButton(event->xbutton.x, event->xbutton.y) != CLIP_IDLE &&
3995 				(dock->type == WM_CLIP || (dock->type == WM_DOCK && wPreferences.flags.clip_merged_in_dock)))
3996 				handleClipChangeWorkspace(scr, event);
3997 			else
3998 				handleDockMove(dock, aicon, event);
3999 		} else {
4000 			Bool hasMoved = wHandleAppIconMove(aicon, event);
4001 			if (wPreferences.single_click && !hasMoved)
4002 				iconDblClick(desc, event);
4003 		}
4004 	} else if (event->xbutton.button == Button2 && aicon == scr->clip_icon) {
4005 		if (!scr->clip_ws_menu)
4006 			scr->clip_ws_menu = wWorkspaceMenuMake(scr, False);
4007 
4008 		if (scr->clip_ws_menu) {
4009 			WMenu *wsMenu = scr->clip_ws_menu;
4010 			int xpos;
4011 
4012 			wWorkspaceMenuUpdate(scr, wsMenu);
4013 
4014 			xpos = event->xbutton.x_root - wsMenu->frame->core->width / 2 - 1;
4015 			if (xpos < 0) {
4016 				xpos = 0;
4017 			} else if (xpos + wsMenu->frame->core->width > scr->scr_width - 2) {
4018 				xpos = scr->scr_width - wsMenu->frame->core->width - 4;
4019 			}
4020 			wMenuMapAt(wsMenu, xpos, event->xbutton.y_root + 2, False);
4021 
4022 			desc = &wsMenu->menu->descriptor;
4023 			event->xany.send_event = True;
4024 			(*desc->handle_mousedown) (desc, event);
4025 		}
4026 	} else if (event->xbutton.button == Button2 && dock->type == WM_CLIP &&
4027 		   (event->xbutton.state & ShiftMask) && aicon != scr->clip_icon) {
4028 		wClipMakeIconOmnipresent(aicon, !aicon->omnipresent);
4029 	} else if (event->xbutton.button == Button3) {
4030 		if (event->xbutton.send_event &&
4031 		    XGrabPointer(dpy, aicon->icon->core->window, True, ButtonMotionMask
4032 				 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
4033 				 GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
4034 			wwarning("pointer grab failed for dockicon menu");
4035 			return;
4036 		}
4037 
4038 		openDockMenu(dock, aicon, event);
4039 	} else if (event->xbutton.button == Button2) {
4040 		WAppIcon *btn = desc->parent;
4041 
4042 		if (!btn->launching && (!btn->running || (event->xbutton.state & ControlMask)))
4043 			launchDockedApplication(btn, True);
4044 	} else if (event->xbutton.button == Button4 && dock->type == WM_CLIP) {
4045 		wWorkspaceRelativeChange(scr, 1);
4046 	} else if (event->xbutton.button == Button5 && dock->type == WM_CLIP) {
4047 		wWorkspaceRelativeChange(scr, -1);
4048 	}
4049 }
4050 
clipEnterNotify(WObjDescriptor * desc,XEvent * event)4051 static void clipEnterNotify(WObjDescriptor *desc, XEvent *event)
4052 {
4053 	WAppIcon *btn = (WAppIcon *) desc->parent;
4054 	WDock *dock, *tmp;
4055 	WScreen *scr;
4056 
4057 	/* Parameter not used, but tell the compiler that it is ok */
4058 	(void) event;
4059 
4060 	assert(event->type == EnterNotify);
4061 
4062 	if (desc->parent_type != WCLASS_DOCK_ICON)
4063 		return;
4064 
4065 	scr = btn->icon->core->screen_ptr;
4066 	dock = btn->dock;
4067 
4068 	if (dock == NULL)
4069 		return;
4070 
4071 	/* The auto raise/lower code */
4072 	tmp = (dock->type == WM_DRAWER ? scr->dock : dock);
4073 	if (tmp->auto_lower_magic) {
4074 		WMDeleteTimerHandler(tmp->auto_lower_magic);
4075 		tmp->auto_lower_magic = NULL;
4076 	}
4077 	if (tmp->auto_raise_lower && !tmp->auto_raise_magic)
4078 		tmp->auto_raise_magic = WMAddTimerHandler(wPreferences.clip_auto_raise_delay, clipAutoRaise, (void *) tmp);
4079 
4080 	if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4081 		return;
4082 
4083 	/* The auto expand/collapse code */
4084 	if (dock->auto_collapse_magic) {
4085 		WMDeleteTimerHandler(dock->auto_collapse_magic);
4086 		dock->auto_collapse_magic = NULL;
4087 	}
4088 	if (dock->auto_collapse && !dock->auto_expand_magic)
4089 		dock->auto_expand_magic = WMAddTimerHandler(wPreferences.clip_auto_expand_delay, clipAutoExpand, (void *)dock);
4090 }
4091 
clipLeave(WDock * dock)4092 static void clipLeave(WDock *dock)
4093 {
4094 	XEvent event;
4095 	WObjDescriptor *desc = NULL;
4096 	WDock *tmp;
4097 
4098 	if (dock == NULL)
4099 		return;
4100 
4101 	if (XCheckTypedEvent(dpy, EnterNotify, &event) != False) {
4102 		if (XFindContext(dpy, event.xcrossing.window, w_global.context.client_win,
4103 				 (XPointer *) & desc) != XCNOENT
4104 		    && desc && desc->parent_type == WCLASS_DOCK_ICON
4105 		    && ((WAppIcon *) desc->parent)->dock == dock) {
4106 			/* We haven't left the dock/clip/drawer yet */
4107 			XPutBackEvent(dpy, &event);
4108 			return;
4109 		}
4110 
4111 		XPutBackEvent(dpy, &event);
4112 	} else {
4113 		/* We entered a withdrawn window, so we're still in Clip */
4114 		return;
4115 	}
4116 
4117 	tmp = (dock->type == WM_DRAWER ? dock->screen_ptr->dock : dock);
4118 	if (tmp->auto_raise_magic) {
4119 		WMDeleteTimerHandler(tmp->auto_raise_magic);
4120 		tmp->auto_raise_magic = NULL;
4121 	}
4122 	if (tmp->auto_raise_lower && !tmp->auto_lower_magic)
4123 		tmp->auto_lower_magic = WMAddTimerHandler(wPreferences.clip_auto_lower_delay, clipAutoLower, (void *)tmp);
4124 
4125 	if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4126 		return;
4127 
4128 	if (dock->auto_expand_magic) {
4129 		WMDeleteTimerHandler(dock->auto_expand_magic);
4130 		dock->auto_expand_magic = NULL;
4131 	}
4132 	if (dock->auto_collapse && !dock->auto_collapse_magic)
4133 		dock->auto_collapse_magic = WMAddTimerHandler(wPreferences.clip_auto_collapse_delay, clipAutoCollapse, (void *)dock);
4134 }
4135 
clipLeaveNotify(WObjDescriptor * desc,XEvent * event)4136 static void clipLeaveNotify(WObjDescriptor *desc, XEvent *event)
4137 {
4138 	WAppIcon *btn = (WAppIcon *) desc->parent;
4139 
4140 	/* Parameter not used, but tell the compiler that it is ok */
4141 	(void) event;
4142 
4143 	assert(event->type == LeaveNotify);
4144 
4145 	if (desc->parent_type != WCLASS_DOCK_ICON)
4146 		return;
4147 
4148 	clipLeave(btn->dock);
4149 }
4150 
clipAutoCollapse(void * cdata)4151 static void clipAutoCollapse(void *cdata)
4152 {
4153 	WDock *dock = (WDock *) cdata;
4154 
4155 	if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4156 		return;
4157 
4158 	if (dock->auto_collapse) {
4159 		dock->collapsed = 1;
4160 		wDockHideIcons(dock);
4161 	}
4162 	dock->auto_collapse_magic = NULL;
4163 }
4164 
clipAutoExpand(void * cdata)4165 static void clipAutoExpand(void *cdata)
4166 {
4167 	WDock *dock = (WDock *) cdata;
4168 
4169 	if (dock->type != WM_CLIP && dock->type != WM_DRAWER)
4170 		return;
4171 
4172 	if (dock->auto_collapse) {
4173 		dock->collapsed = 0;
4174 		wDockShowIcons(dock);
4175 	}
4176 	dock->auto_expand_magic = NULL;
4177 }
4178 
clipAutoLower(void * cdata)4179 static void clipAutoLower(void *cdata)
4180 {
4181 	WDock *dock = (WDock *) cdata;
4182 
4183 	if (dock->auto_raise_lower)
4184 		wDockLower(dock);
4185 
4186 	dock->auto_lower_magic = NULL;
4187 }
4188 
clipAutoRaise(void * cdata)4189 static void clipAutoRaise(void *cdata)
4190 {
4191 	WDock *dock = (WDock *) cdata;
4192 
4193 	if (dock->auto_raise_lower)
4194 		wDockRaise(dock);
4195 
4196 	dock->auto_raise_magic = NULL;
4197 }
4198 
iconCanBeOmnipresent(WAppIcon * aicon)4199 static Bool iconCanBeOmnipresent(WAppIcon *aicon)
4200 {
4201 	WScreen *scr = aicon->icon->core->screen_ptr;
4202 	WDock *clip;
4203 	WAppIcon *btn;
4204 	int i, j;
4205 
4206 	for (i = 0; i < scr->workspace_count; i++) {
4207 		clip = scr->workspaces[i]->clip;
4208 
4209 		if (clip == aicon->dock)
4210 			continue;
4211 
4212 		if (clip->icon_count + scr->global_icon_count >= clip->max_icons)
4213 			return False;	/* Clip is full in some workspace */
4214 
4215 		for (j = 0; j < clip->max_icons; j++) {
4216 			btn = clip->icon_array[j];
4217 			if (btn && btn->xindex == aicon->xindex && btn->yindex == aicon->yindex)
4218 				return False;
4219 		}
4220 	}
4221 
4222 	return True;
4223 }
4224 
wClipMakeIconOmnipresent(WAppIcon * aicon,int omnipresent)4225 int wClipMakeIconOmnipresent(WAppIcon *aicon, int omnipresent)
4226 {
4227 	WScreen *scr = aicon->icon->core->screen_ptr;
4228 	WAppIconChain *new_entry, *tmp, *tmp1;
4229 	int status = WO_SUCCESS;
4230 
4231 	if ((scr->dock && aicon->dock == scr->dock) || aicon == scr->clip_icon)
4232 		return WO_NOT_APPLICABLE;
4233 
4234 	if (aicon->omnipresent == omnipresent)
4235 		return WO_SUCCESS;
4236 
4237 	if (omnipresent) {
4238 		if (iconCanBeOmnipresent(aicon)) {
4239 			aicon->omnipresent = 1;
4240 			new_entry = wmalloc(sizeof(WAppIconChain));
4241 			new_entry->aicon = aicon;
4242 			new_entry->next = scr->global_icons;
4243 			scr->global_icons = new_entry;
4244 			scr->global_icon_count++;
4245 		} else {
4246 			aicon->omnipresent = 0;
4247 			status = WO_FAILED;
4248 		}
4249 	} else {
4250 		aicon->omnipresent = 0;
4251 		if (aicon == scr->global_icons->aicon) {
4252 			tmp = scr->global_icons->next;
4253 			wfree(scr->global_icons);
4254 			scr->global_icons = tmp;
4255 			scr->global_icon_count--;
4256 		} else {
4257 			tmp = scr->global_icons;
4258 			while (tmp->next) {
4259 				if (tmp->next->aicon == aicon) {
4260 					tmp1 = tmp->next->next;
4261 					wfree(tmp->next);
4262 					tmp->next = tmp1;
4263 					scr->global_icon_count--;
4264 					break;
4265 				}
4266 				tmp = tmp->next;
4267 			}
4268 		}
4269 	}
4270 
4271 	wAppIconPaint(aicon);
4272 
4273 	return status;
4274 }
4275 
drawerAppendToChain(WScreen * scr,WDock * drawer)4276 static void drawerAppendToChain(WScreen *scr, WDock *drawer)
4277 {
4278 	WDrawerChain **where_to_add;
4279 
4280 	where_to_add = &scr->drawers;
4281 	while ((*where_to_add) != NULL) {
4282 		where_to_add = &(*where_to_add)->next;
4283 	}
4284 
4285 	*where_to_add = wmalloc(sizeof(WDrawerChain));
4286 	(*where_to_add)->adrawer = drawer;
4287 	(*where_to_add)->next = NULL;
4288 	scr->drawer_count++;
4289 }
4290 
4291 
drawerRemoveFromChain(WScreen * scr,WDock * drawer)4292 static void drawerRemoveFromChain(WScreen *scr, WDock *drawer)
4293 {
4294 	WDrawerChain *next, **to_remove;
4295 
4296 	to_remove = &scr->drawers;
4297 	while (True) {
4298 		if (*to_remove == NULL) {
4299 			wwarning("The drawer to be removed can not be found.");
4300 			return;
4301 		}
4302 		if ((*to_remove)->adrawer == drawer)
4303 			break;
4304 
4305 		to_remove = &(*to_remove)->next;
4306 	}
4307 	next = (*to_remove)->next;
4308 	wfree(*to_remove);
4309 	*to_remove = next;
4310 	scr->drawer_count--;
4311 }
4312 
4313 
4314 /* Don't free the returned string. Duplicate it. */
findUniqueName(WScreen * scr,const char * instance_basename)4315 static char * findUniqueName(WScreen *scr, const char *instance_basename)
4316 {
4317 	static char buffer[128];
4318 	WDrawerChain *dc;
4319 	int i;
4320 	Bool already_in_use = True;
4321 
4322 #define UNIQUE_NAME_WATCHDOG 128
4323 	for (i = 0; already_in_use && i < UNIQUE_NAME_WATCHDOG; i++) {
4324 		snprintf(buffer, sizeof buffer, "%s%d", instance_basename, i);
4325 
4326 		already_in_use = False;
4327 
4328 		for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4329 			if (!strncmp(dc->adrawer->icon_array[0]->wm_instance, buffer,
4330 					sizeof buffer)) {
4331 				already_in_use = True;
4332 				break;
4333 			}
4334 		}
4335 	}
4336 
4337 	if (i == UNIQUE_NAME_WATCHDOG)
4338 		wwarning("Couldn't find a unique name for drawer in %d attempts.", i);
4339 #undef UNIQUE_NAME_WATCHDOG
4340 
4341 	return buffer;
4342 }
4343 
4344 
drawerIconExpose(WObjDescriptor * desc,XEvent * event)4345 static void drawerIconExpose(WObjDescriptor *desc, XEvent *event)
4346 {
4347 	/* Parameter not used, but tell the compiler that it is ok */
4348 	(void) event;
4349 
4350 	wDrawerIconPaint((WAppIcon *) desc->parent);
4351 }
4352 
4353 
addADrawer(WScreen * scr)4354 static int addADrawer(WScreen *scr)
4355 {
4356 	int i, y, sig, found_y;
4357 	WDock *drawer, *dock = scr->dock;
4358 	WDrawerChain *dc;
4359 	char can_be_here[2 * dock->max_icons - 1];
4360 
4361 	if (dock->icon_count + scr->drawer_count >= dock->max_icons)
4362 		return -1;
4363 
4364 	for (y = -dock->max_icons + 1; y < dock->max_icons; y++) {
4365 		can_be_here[y + dock->max_icons - 1] = True;
4366 	}
4367 	for (i = 0; i < dock->max_icons; i++) {
4368 		if (dock->icon_array[i] != NULL)
4369 			can_be_here[dock->icon_array[i]->yindex + dock->max_icons - 1] = False;
4370 	}
4371 	for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4372 		y = (int) ((dc->adrawer->y_pos - dock->y_pos) / ICON_SIZE);
4373 		can_be_here[y + dock->max_icons - 1] = False;
4374 	}
4375 
4376 	found_y = False;
4377 	for (sig = 1; !found_y && sig > -2; sig -= 2) // 1, then -1
4378 	{
4379 		for (y = sig; sig * y < dock->max_icons; y += sig)
4380 		{
4381 			if (can_be_here[y + dock->max_icons - 1] &&
4382 				onScreen(scr, dock->x_pos, dock->y_pos + y * ICON_SIZE))
4383 			{
4384 				found_y = True;
4385 				break;
4386 			}
4387 		}
4388 	}
4389 
4390 	if (!found_y)
4391 		/* This can happen even when dock->icon_count + scr->drawer_count
4392 		 * < dock->max_icons when the dock is not aligned on an
4393 		 * ICON_SIZE multiple, as some space is lost above and under it */
4394 		return -1;
4395 
4396 	drawer = wDockCreate(scr, WM_DRAWER, NULL);
4397 	drawer->lowered = scr->dock->lowered;
4398 	if (!drawer->lowered)
4399 		ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4400 	else
4401 		ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4402 	drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4403 	drawer->x_pos = dock->x_pos;
4404 	drawer->y_pos = dock->y_pos + ICON_SIZE * y;
4405 	drawer->icon_array[0]->xindex = 0;
4406 	drawer->icon_array[0]->yindex = 0;
4407 	drawer->icon_array[0]->x_pos = drawer->x_pos;
4408 	drawer->icon_array[0]->y_pos = drawer->y_pos;
4409 	XMoveWindow(dpy, drawer->icon_array[0]->icon->core->window,
4410 		drawer->icon_array[0]->x_pos, drawer->icon_array[0]->y_pos);
4411 
4412 	return 0;
4413 }
4414 
4415 
addADrawerCallback(WMenu * menu,WMenuEntry * entry)4416 static void addADrawerCallback(WMenu *menu, WMenuEntry *entry)
4417 {
4418 	/* Parameter not used, but tell the compiler that it is ok */
4419 	(void) menu;
4420 
4421 	assert(entry->clientdata!=NULL);
4422 	addADrawer(((WAppIcon *) entry->clientdata)->dock->screen_ptr);
4423 }
4424 
4425 
drawerDestroy(WDock * drawer)4426 static void drawerDestroy(WDock *drawer)
4427 {
4428 	WScreen *scr;
4429 	int i;
4430 	WAppIcon *aicon = NULL;
4431 	WMArray *icons;
4432 
4433 	if (drawer == NULL)
4434 		return;
4435 
4436 	scr = drawer->screen_ptr;
4437 
4438 	/* Note regarding menus: we can't delete any dock/clip/drawer menu, because
4439 	 * that would (attempt to) wfree some memory in gettext library (see menu
4440 	 * entries that have several "versions", such like "Hide" and "Unhide"). */
4441 	wDefaultPurgeInfo(drawer->icon_array[0]->wm_instance,
4442 			drawer->icon_array[0]->wm_class);
4443 
4444 	if (drawer->icon_count == 2) {
4445 		/* Drawer contains a single appicon: dock it where the drawer was */
4446 		for (i = 1; i < drawer->max_icons; i++) {
4447 			aicon = drawer->icon_array[i];
4448 			if (aicon != NULL)
4449 				break;
4450 		}
4451 
4452 		wDockMoveIconBetweenDocks(drawer, scr->dock, aicon,
4453 					0, (drawer->y_pos - scr->dock->y_pos) / ICON_SIZE);
4454 		XMoveWindow(dpy, aicon->icon->core->window, drawer->x_pos, drawer->y_pos);
4455 		XMapWindow(dpy, aicon->icon->core->window);
4456 	} else if (drawer->icon_count > 2) {
4457 		icons = WMCreateArray(drawer->icon_count - 1);
4458 		for (i = 1; i < drawer->max_icons; i++) {
4459 			aicon = drawer->icon_array[i];
4460 			if (aicon == NULL)
4461 				continue;
4462 			WMAddToArray(icons, aicon);
4463 		}
4464 		removeIcons(icons, drawer);
4465 	}
4466 
4467 	if (drawer->auto_collapse_magic) {
4468 		WMDeleteTimerHandler(drawer->auto_collapse_magic);
4469 		drawer->auto_collapse_magic = NULL;
4470 	}
4471 	if (drawer->auto_lower_magic) {
4472 		WMDeleteTimerHandler(drawer->auto_lower_magic);
4473 		drawer->auto_lower_magic = NULL;
4474 	}
4475 
4476 	wAppIconDestroy(drawer->icon_array[0]);
4477 	wfree(drawer->icon_array);
4478 	drawer->icon_array = NULL;
4479 
4480 	drawerRemoveFromChain(scr, drawer);
4481 	if (scr->last_dock == drawer)
4482 		scr->last_dock = NULL;
4483 	if (scr->attracting_drawer == drawer)
4484 		scr->attracting_drawer = NULL;
4485 
4486 	wfree(drawer);
4487 }
4488 
4489 
removeDrawerCallback(WMenu * menu,WMenuEntry * entry)4490 static void removeDrawerCallback(WMenu *menu, WMenuEntry *entry)
4491 {
4492 	WDock *dock = ((WAppIcon*)entry->clientdata)->dock;
4493 
4494 	/* Parameter not used, but tell the compiler that it is ok */
4495 	(void) menu;
4496 
4497 	assert(dock != NULL);
4498 
4499 	if (dock->icon_count > 2) {
4500 		if (wMessageDialog(dock->screen_ptr, _("Drawer"),
4501 					_("All icons in this drawer will be detached!"),
4502 					_("OK"), _("Cancel"), NULL) != WAPRDefault)
4503 			return;
4504 	}
4505 	drawerDestroy(dock);
4506 }
4507 
4508 
wDrawerIconPaint(WAppIcon * dicon)4509 void wDrawerIconPaint(WAppIcon *dicon)
4510 {
4511 	Window win = dicon->icon->core->window;
4512 	WScreen *scr = dicon->icon->core->screen_ptr;
4513 	XPoint p[4];
4514 	GC gc = scr->draw_gc;
4515 	WMColor *color;
4516 
4517 	wIconPaint(dicon->icon);
4518 
4519 	if (!dicon->dock->collapsed)
4520 		color = scr->clip_title_color[CLIP_NORMAL];
4521 	else
4522 		color = scr->clip_title_color[CLIP_COLLAPSED];
4523 	XSetForeground(dpy, gc, WMColorPixel(color));
4524 
4525 	if (dicon->dock->on_right_side) {
4526 		p[0].x = p[3].x = 10;
4527 		p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4528 		p[1].x = 10;
4529 		p[1].y = ICON_SIZE / 2 + 5;
4530 		p[2].x = 5;
4531 		p[2].y = ICON_SIZE / 2;
4532 	}
4533 	else {
4534 		p[0].x = p[3].x = ICON_SIZE-1 - 10;
4535 		p[0].y = p[3].y = ICON_SIZE / 2 - 5;
4536 		p[1].x = ICON_SIZE-1 - 10;
4537 		p[1].y = ICON_SIZE / 2 + 5;
4538 		p[2].x = ICON_SIZE-1 - 5;
4539 		p[2].y = ICON_SIZE / 2;
4540 	}
4541 	XFillPolygon(dpy, win, gc, p,3,Convex,CoordModeOrigin);
4542 	XDrawLines(dpy, win, gc, p,4,CoordModeOrigin);
4543 }
4544 
4545 
wDrawerMakeTile(WScreen * scr,RImage * normalTile)4546 RImage* wDrawerMakeTile(WScreen *scr, RImage *normalTile)
4547 {
4548 	RImage *tile = RCloneImage(normalTile);
4549 	RColor dark;
4550 	RColor light;
4551 
4552 	dark.alpha = 0;
4553 	dark.red = dark.green = dark.blue = 60;
4554 
4555 	light.alpha = 0;
4556 	light.red = light.green = light.blue = 80;
4557 
4558 	/* arrow bevel */
4559 	if (!scr->dock || scr->dock->on_right_side) {
4560 		ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 - 7,
4561 			4, ICON_SIZE / 2, &dark);       /* / */
4562 		ROperateLine(tile, RSubtractOperation, 11, ICON_SIZE / 2 + 7,
4563 			4, ICON_SIZE / 2, &dark);       /* \ */
4564 		ROperateLine(tile, RAddOperation,      11, ICON_SIZE / 2 - 7,
4565 			11, ICON_SIZE / 2 + 7, &light); /* | */
4566 	}
4567 	else {
4568 		ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4569 			ICON_SIZE-1 - 4, ICON_SIZE / 2, &dark);      /* \ */
4570 		ROperateLine(tile, RAddOperation,      ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7,
4571 			ICON_SIZE-1 - 4, ICON_SIZE / 2, &light);     /* / */
4572 		ROperateLine(tile, RSubtractOperation, ICON_SIZE-1 - 11, ICON_SIZE / 2 - 7,
4573 			ICON_SIZE-1 - 11, ICON_SIZE / 2 + 7, &dark); /* | */
4574 	}
4575 	return tile;
4576 }
4577 
4578 
swapDrawer(WDock * drawer,int new_x)4579 static void swapDrawer(WDock *drawer, int new_x)
4580 {
4581 	int i;
4582 
4583 	drawer->on_right_side = !drawer->on_right_side;
4584 	drawer->x_pos = new_x;
4585 
4586 	for (i = 0; i < drawer->max_icons; i++) {
4587 		WAppIcon *ai;
4588 
4589 		ai = drawer->icon_array[i];
4590 		if (ai == NULL)
4591 			continue;
4592 		ai->xindex *= -1; /* so A B C becomes C B A */
4593 		ai->x_pos = new_x + ai->xindex * ICON_SIZE;
4594 
4595 		/* Update drawer's tile */
4596 		if (i == 0) {
4597 			wIconUpdate(ai->icon);
4598 			wDrawerIconPaint(ai);
4599 		}
4600 		XMoveWindow(dpy, ai->icon->core->window, ai->x_pos, ai->y_pos);
4601 	}
4602 }
4603 
4604 
swapDrawers(WScreen * scr,int new_x)4605 static void swapDrawers(WScreen *scr, int new_x)
4606 {
4607 	WDrawerChain *dc;
4608 
4609 	if (scr->drawer_tile)
4610 		RReleaseImage(scr->drawer_tile);
4611 
4612 	scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
4613 
4614 	for (dc = scr->drawers; dc != NULL; dc = dc->next)
4615 		swapDrawer(dc->adrawer, new_x);
4616 }
4617 
4618 
wIsADrawer(WAppIcon * aicon)4619 int wIsADrawer(WAppIcon *aicon)
4620 {
4621 	return aicon && aicon->dock &&
4622 		aicon->dock->type == WM_DRAWER && aicon->dock->icon_array[0] == aicon;
4623 }
4624 
4625 
getDrawer(WScreen * scr,int y_index)4626 static WDock* getDrawer(WScreen *scr, int y_index)
4627 {
4628 	WDrawerChain *dc;
4629 
4630 	for (dc = scr->drawers; dc != NULL; dc = dc->next) {
4631 		if (dc->adrawer->y_pos - scr->dock->y_pos == y_index * ICON_SIZE)
4632 			return dc->adrawer;
4633 	}
4634 	return NULL;
4635 }
4636 
4637 
4638 
4639 /* Find the "hole" a moving appicon created when snapped into the
4640  * drawer. redocking is a boolean. If the moving appicon comes from the
4641  * drawer, drawer->icon_count is correct. If not, redocking is then false and
4642  * there are now drawer->icon_count plus one appicons in the drawer. */
indexOfHole(WDock * drawer,WAppIcon * moving_aicon,int redocking)4643 static int indexOfHole(WDock *drawer, WAppIcon *moving_aicon, int redocking)
4644 {
4645 	int index_of_hole, i;
4646 
4647 	/* Classic interview question...
4648 	 *
4649 	 * We have n-1 (n = drawer->icon_count-1 or drawer->icon_count, see
4650 	 * redocking) appicons, whose xindex are unique in [1..n]. One is missing:
4651 	 * that's where the ghost of the moving appicon is, that's what the
4652 	 * function should return.
4653 	 *
4654 	 * We compute 1+2+...+n (this sum is equal to n*(n+1)/2), we subtract to
4655 	 * this sum the xindex of each of the n-1 appicons, and we get the correct
4656 	 * index! */
4657 
4658 	if (redocking) {
4659 		index_of_hole = (drawer->icon_count - 1) * drawer->icon_count / 2;
4660 	} else {
4661 		index_of_hole = drawer->icon_count * (drawer->icon_count + 1) / 2;
4662 	}
4663 	index_of_hole *= (drawer->on_right_side ? -1 : 1);
4664 
4665 	for (i = 1; i < drawer->max_icons; i++) {
4666 		if (drawer->icon_array[i] && drawer->icon_array[i] != moving_aicon)
4667 			index_of_hole -= drawer->icon_array[i]->xindex;
4668 	}
4669 	/* wmessage(" Index of the moving appicon is %d (%sredocking)", index_of_hole, (redocking ? "" : "not ")); */
4670 	if (abs(index_of_hole) > abs(drawer->icon_count) - (redocking ? 1 : 0))
4671 		wwarning(" index_of_hole is too large ! (%d greater than %d)",
4672 			index_of_hole, abs(drawer->icon_count) - (redocking ? 1 : 0));
4673 	if (index_of_hole == 0)
4674 		wwarning(" index_of_hole == 0 (%sredocking, icon_count == %d)", (redocking ? "" : "not "), drawer->icon_count);
4675 
4676 	return index_of_hole;
4677 }
4678 
4679 
wSlideAppicons(WAppIcon ** appicons,int n,int to_the_left)4680 void wSlideAppicons(WAppIcon **appicons, int n, int to_the_left)
4681 {
4682 	int i;
4683 	int leftmost = -1, min_index = 9999, from_x = -1; // leftmost and from_x initialized to avoid warning
4684 	Window wins[n];
4685 	WAppIcon *aicon;
4686 
4687 	if (n < 1)
4688 		return;
4689 
4690 	for (i = 0; i < n; i++) {
4691 		aicon = appicons[i];
4692 		aicon->xindex += (to_the_left ? -1 : +1);
4693 		if (aicon->xindex < min_index) {
4694 			min_index = aicon->xindex;
4695 			leftmost = i;
4696 			from_x = aicon->x_pos;
4697 		}
4698 		aicon->x_pos += (to_the_left ? -ICON_SIZE : +ICON_SIZE);
4699 	}
4700 
4701 	for (i = 0; i < n; i++) {
4702 		aicon = appicons[i];
4703 		wins[aicon->xindex - min_index] = aicon->icon->core->window;
4704 	}
4705 	aicon = appicons[leftmost];
4706 	slide_windows(wins, n, from_x, aicon->y_pos, aicon->x_pos, aicon->y_pos);
4707 }
4708 
4709 
wDrawerFillTheGap(WDock * drawer,WAppIcon * aicon,Bool redocking)4710 void wDrawerFillTheGap(WDock *drawer, WAppIcon *aicon, Bool redocking)
4711 {
4712 	int i, j;
4713 	int index_of_hole = indexOfHole(drawer, aicon, redocking);
4714 	WAppIcon *aicons_to_shift[drawer->icon_count];
4715 
4716 	j = 0;
4717 	for (i = 0; i < drawer->max_icons; i++) {
4718 		WAppIcon *ai = drawer->icon_array[i];
4719 		if (ai && ai != aicon &&
4720 			abs(ai->xindex) > abs(index_of_hole))
4721 			aicons_to_shift[j++] = ai;
4722 	}
4723 	if (j != drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0))
4724 		wwarning("Removing aicon at index %d from %s: j=%d but should be %d",
4725 			index_of_hole, drawer->icon_array[0]->wm_instance,
4726 			j, drawer->icon_count - abs(index_of_hole) - (redocking ? 1 : 0));
4727 	wSlideAppicons(aicons_to_shift, j, !drawer->on_right_side);
4728 }
4729 
4730 
drawerConsolidateIcons(WDock * drawer)4731 static void drawerConsolidateIcons(WDock *drawer)
4732 {
4733 	WAppIcon *aicons_to_shift[drawer->icon_count];
4734 	int maxRemaining = 0;
4735 	int sum = 0;
4736 	int i;
4737 	for (i = 0; i < drawer->max_icons; i++) {
4738 		WAppIcon *ai = drawer->icon_array[i];
4739 		if (ai == NULL)
4740 			continue;
4741 		sum += abs(ai->xindex);
4742 		if (abs(ai->xindex) > maxRemaining)
4743 			maxRemaining = abs(ai->xindex);
4744 	}
4745 	while (sum != maxRemaining * (maxRemaining + 1) / 2) { // while there is a hole
4746 		WAppIcon *ai;
4747 		int n;
4748 		// Look up for the hole at max position
4749 		int maxDeleted;
4750 		for (maxDeleted = maxRemaining - 1; maxDeleted > 0; maxDeleted--) {
4751 			Bool foundAppIconThere = False;
4752 			for (i = 0; i < drawer->max_icons; i++) {
4753 				WAppIcon *ai = drawer->icon_array[i];
4754 				if (ai == NULL)
4755 					continue;
4756 				if (abs(ai->xindex) == maxDeleted) {
4757 					foundAppIconThere = True;
4758 					break;
4759 				}
4760 			}
4761 			if (!foundAppIconThere)
4762 				break;
4763 		}
4764 		assert(maxDeleted > 0); // would mean while test is wrong
4765 		n = 0;
4766 		for (i = 0; i < drawer->max_icons; i++) {
4767 			ai = drawer->icon_array[i];
4768 			if (ai != NULL && abs(ai->xindex) > maxDeleted)
4769 				aicons_to_shift[n++] = ai;
4770 		}
4771 		assert(n == maxRemaining - maxDeleted); // for the code review ;-)
4772 		wSlideAppicons(aicons_to_shift, n, !drawer->on_right_side);
4773 		// Efficient beancounting
4774 		maxRemaining -= 1;
4775 		sum -= n;
4776 	}
4777 }
4778 
4779 
4780 
4781 /* similar to wDockRestoreState, but a lot a specific stuff too... */
drawerRestoreState(WScreen * scr,WMPropList * drawer_state)4782 static WDock * drawerRestoreState(WScreen *scr, WMPropList *drawer_state)
4783 {
4784 	WDock *drawer;
4785 	WMPropList *apps, *value, *dock_state;
4786 	WAppIcon *aicon;
4787 	int count, i;
4788 
4789 	if (!drawer_state)
4790 		return NULL;
4791 
4792 	make_keys();
4793 
4794 	WMRetainPropList(drawer_state);
4795 
4796 	/* Get the instance name, and create a drawer */
4797 	value = WMGetFromPLDictionary(drawer_state, dName);
4798 	drawer = wDockCreate(scr, WM_DRAWER, WMGetFromPLString(value));
4799 
4800 	/* restore DnD command and paste command */
4801 #ifdef USE_DOCK_XDND
4802 	value = WMGetFromPLDictionary(drawer_state, dDropCommand);
4803 	if (value && WMIsPLString(value))
4804 		drawer->icon_array[0]->dnd_command = wstrdup(WMGetFromPLString(value));
4805 #endif /* USE_DOCK_XDND */
4806 
4807 	value = WMGetFromPLDictionary(drawer_state, dPasteCommand);
4808 	if (value && WMIsPLString(value))
4809 		drawer->icon_array[0]->paste_command = wstrdup(WMGetFromPLString(value));
4810 
4811 	/* restore position */
4812 	value = WMGetFromPLDictionary(drawer_state, dPosition);
4813 	if (!value || !WMIsPLString(value))
4814 		COMPLAIN("Position");
4815 	else {
4816 		int x, y, y_index;
4817 		if (sscanf(WMGetFromPLString(value), "%i,%i", &x, &y) != 2)
4818 			COMPLAIN("Position");
4819 
4820 		/* check position sanity */
4821 		if (x != scr->dock->x_pos) {
4822 			x = scr->dock->x_pos;
4823 		}
4824 		y_index = (y - scr->dock->y_pos) / ICON_SIZE;
4825 		if (y_index >= scr->dock->max_icons) {
4826 			/* Here we should do something more intelligent, since it
4827 			 * can happen even if the user hasn't hand-edited his
4828 			 * G/D/State file (but uses a lower resolution). */
4829 			y_index = scr->dock->max_icons - 1;
4830 		}
4831 		y = scr->dock->y_pos + y_index * ICON_SIZE;
4832 		moveDock(drawer, x, y);
4833 	}
4834 
4835 	/* restore dock properties (applist and others) */
4836 	dock_state = WMGetFromPLDictionary(drawer_state, dDock);
4837 
4838 	/* restore lowered/raised state: same as scr->dock, no matter what */
4839 	drawer->lowered = scr->dock->lowered;
4840 	if (!drawer->lowered)
4841 		ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMDockLevel);
4842 	else
4843 		ChangeStackingLevel(drawer->icon_array[0]->icon->core, WMNormalLevel);
4844 	wRaiseFrame(drawer->icon_array[0]->icon->core);
4845 
4846 	/* restore collapsed state */
4847 	drawer->collapsed = 0;
4848 	value = WMGetFromPLDictionary(dock_state, dCollapsed);
4849 	if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4850 		drawer->collapsed = 1;
4851 	}
4852 	/* restore auto-collapsed state */
4853 	value = WMGetFromPLDictionary(dock_state, dAutoCollapse);
4854 	if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4855 		drawer->auto_collapse = 1;
4856 		drawer->collapsed = 1;
4857 	} else {
4858 		drawer->auto_collapse = 0; // because wDockCreate sets it (drawers only)
4859 	}
4860 
4861 	/* restore auto-raise/lower state: same as scr->dock, no matter what */
4862 	drawer->auto_raise_lower = scr->dock->auto_raise_lower;
4863 
4864 	/* restore attract icons state */
4865 	drawer->attract_icons = 0;
4866 	value = WMGetFromPLDictionary(dock_state, dAutoAttractIcons);
4867 	if (value && strcasecmp(WMGetFromPLString(value), "YES") == 0) {
4868 		drawer->attract_icons = 1;
4869 		scr->attracting_drawer = drawer;
4870 	}
4871 
4872 	/* application list */
4873 	apps = WMGetFromPLDictionary(dock_state, dApplications);
4874 
4875 	if (!apps) {
4876 		goto finish;
4877 	}
4878 
4879 	count = WMGetPropListItemCount(apps);
4880 
4881 	if (count == 0)
4882 		goto finish;
4883 
4884 	for (i=0; i<count; i++) {
4885 		if (drawer->icon_count >= drawer->max_icons) {
4886 			wwarning(_("there are too many icons stored in drawer. Ignoring what doesn't fit"));
4887 			break;
4888 		}
4889 
4890 		value = WMGetFromPLArray(apps, i);
4891 		aicon = restore_icon_state(scr, value, WM_DRAWER, drawer->icon_count);
4892 
4893 		drawer->icon_array[drawer->icon_count] = aicon;
4894 
4895 		if (aicon) {
4896 			aicon->dock = drawer;
4897 			aicon->x_pos = drawer->x_pos + (aicon->xindex * ICON_SIZE);
4898 			aicon->y_pos = drawer->y_pos + (aicon->yindex * ICON_SIZE);
4899 
4900 			if (!drawer->lowered)
4901 				ChangeStackingLevel(aicon->icon->core, WMDockLevel);
4902 			else
4903 				ChangeStackingLevel(aicon->icon->core, WMNormalLevel);
4904 
4905 			wCoreConfigure(aicon->icon->core, aicon->x_pos, aicon->y_pos, 0, 0);
4906 
4907 			if (!drawer->collapsed)
4908 				XMapWindow(dpy, aicon->icon->core->window);
4909 			wRaiseFrame(aicon->icon->core);
4910 
4911 			drawer->icon_count++;
4912 		}
4913 	}
4914 
4915 finish:
4916 	WMReleasePropList(drawer_state);
4917 
4918 	return drawer;
4919 }
4920 
4921 
4922 /* Same kind of comment than for previous function: this function is
4923  * very similar to make_icon_state, but has substential differences as
4924  * well. */
drawerSaveState(WDock * drawer)4925 static WMPropList *drawerSaveState(WDock *drawer)
4926 {
4927 	WMPropList *pstr, *drawer_state;
4928 	WAppIcon *ai;
4929 	char buffer[64];
4930 
4931 	ai = drawer->icon_array[0];
4932 	/* Store its name */
4933 	pstr = WMCreatePLString(ai->wm_instance);
4934 	drawer_state = WMCreatePLDictionary(dName, pstr, NULL); /* we need this final NULL */
4935 	WMReleasePropList(pstr);
4936 
4937 	/* Store its position */
4938 	snprintf(buffer, sizeof(buffer), "%i,%i", ai->x_pos, ai->y_pos);
4939 	pstr = WMCreatePLString(buffer);
4940 	WMPutInPLDictionary(drawer_state, dPosition, pstr);
4941 	WMReleasePropList(pstr);
4942 
4943 #ifdef USE_DOCK_XDND
4944 	/* Store its DnD command */
4945 	if (ai->dnd_command) {
4946 		pstr = WMCreatePLString(ai->dnd_command);
4947 		WMPutInPLDictionary(drawer_state, dDropCommand, pstr);
4948 		WMReleasePropList(pstr);
4949 	}
4950 #endif /* USE_DOCK_XDND */
4951 
4952 	/* Store its paste command */
4953 	if (ai->paste_command) {
4954 		pstr = WMCreatePLString(ai->paste_command);
4955 		WMPutInPLDictionary(drawer_state, dPasteCommand, pstr);
4956 		WMReleasePropList(pstr);
4957 	}
4958 
4959 	/* Store applications list and other properties */
4960 	pstr = dockSaveState(drawer);
4961 	WMPutInPLDictionary(drawer_state, dDock, pstr);
4962 	WMReleasePropList(pstr);
4963 
4964 	return drawer_state;
4965 }
4966 
4967 
wDrawersSaveState(WScreen * scr)4968 void wDrawersSaveState(WScreen *scr)
4969 {
4970 	WMPropList *all_drawers, *drawer_state;
4971 	int i;
4972 	WDrawerChain *dc;
4973 
4974 	make_keys();
4975 
4976 	all_drawers = WMCreatePLArray(NULL);
4977 	for (i=0, dc = scr->drawers;
4978 	     i < scr->drawer_count;
4979 	     i++, dc = dc->next) {
4980 		drawer_state = drawerSaveState(dc->adrawer);
4981 		WMAddToPLArray(all_drawers, drawer_state);
4982 		WMReleasePropList(drawer_state);
4983 	}
4984 	WMPutInPLDictionary(scr->session_state, dDrawers, all_drawers);
4985 	WMReleasePropList(all_drawers);
4986 }
4987 
4988 
wDrawersRestoreState(WScreen * scr)4989 void wDrawersRestoreState(WScreen *scr)
4990 {
4991 	WMPropList *all_drawers, *drawer_state;
4992 	int i;
4993 
4994 	make_keys();
4995 
4996 	if (scr->session_state == NULL)
4997 		return;
4998 
4999 	all_drawers = WMGetFromPLDictionary(scr->session_state, dDrawers);
5000 	if (!all_drawers)
5001 		return;
5002 
5003 	for (i = 0; i < WMGetPropListItemCount(all_drawers); i++) {
5004 		drawer_state = WMGetFromPLArray(all_drawers, i);
5005 		drawerRestoreState(scr, drawer_state);
5006 		// Note: scr->drawers was updated when the the drawer was created
5007 	}
5008 }
5009