Improve dockapp recognition
[wmaker-crm.git] / src / placement.c
blobb891a7a587427f049c92d090d62398850648cf01
1 /* placement.c - window and icon placement on screen
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <limits.h>
31 #include "WindowMaker.h"
32 #include "wcore.h"
33 #include "framewin.h"
34 #include "window.h"
35 #include "icon.h"
36 #include "appicon.h"
37 #include "actions.h"
38 #include "funcs.h"
39 #include "application.h"
40 #include "appicon.h"
41 #include "dock.h"
42 #include "xinerama.h"
44 extern WPreferences wPreferences;
46 #define X_ORIGIN WMAX(usableArea.x1,\
47 wPreferences.window_place_origin.x)
49 #define Y_ORIGIN WMAX(usableArea.y1,\
50 wPreferences.window_place_origin.y)
52 /* interactive window placement is in moveres.c */
53 extern void InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
54 unsigned width, unsigned height);
56 /* Returns True if it is an icon and is in this workspace */
57 static Bool
58 iconPosition(WCoreWindow *wcore, int sx1, int sy1, int sx2, int sy2,
59 int workspace, int *retX, int *retY)
61 void *parent;
62 int ok = 0;
64 parent = wcore->descriptor.parent;
66 /* if it is an application icon */
67 if (wcore->descriptor.parent_type == WCLASS_APPICON && !((WAppIcon *) parent)->docked) {
68 *retX = ((WAppIcon *) parent)->x_pos;
69 *retY = ((WAppIcon *) parent)->y_pos;
71 ok = 1;
72 } else if (wcore->descriptor.parent_type == WCLASS_MINIWINDOW &&
73 (((WIcon *) parent)->owner->frame->workspace == workspace
74 || IS_OMNIPRESENT(((WIcon *) parent)->owner)
75 || wPreferences.sticky_icons)
76 && ((WIcon *) parent)->mapped) {
78 *retX = ((WIcon *) parent)->owner->icon_x;
79 *retY = ((WIcon *) parent)->owner->icon_y;
81 ok = 1;
82 } else if (wcore->descriptor.parent_type == WCLASS_WINDOW
83 && ((WWindow *) parent)->flags.icon_moved
84 && (((WWindow *) parent)->frame->workspace == workspace || IS_OMNIPRESENT((WWindow *) parent)
85 || wPreferences.sticky_icons)) {
86 *retX = ((WWindow *) parent)->icon_x;
87 *retY = ((WWindow *) parent)->icon_y;
89 ok = 1;
92 /* Check if it is inside the screen */
93 if (ok) {
94 if (*retX < sx1 - wPreferences.icon_size)
95 ok = 0;
96 else if (*retX > sx2)
97 ok = 0;
99 if (*retY < sy1 - wPreferences.icon_size)
100 ok = 0;
101 else if (*retY > sy2)
102 ok = 0;
105 return ok;
108 void PlaceIcon(WScreen *scr, int *x_ret, int *y_ret, int head)
110 int pf; /* primary axis */
111 int sf; /* secondary axis */
112 int fullW;
113 int fullH;
114 char *map;
115 int pi, si;
116 WCoreWindow *obj;
117 int sx1, sx2, sy1, sy2; /* screen boundary */
118 int sw, sh;
119 int xo, yo;
120 int xs, ys;
121 int x, y;
122 int isize = wPreferences.icon_size;
123 int done = 0;
124 WMBagIterator iter;
125 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
127 /* Find out screen boundaries. */
129 /* Allows each head to have miniwindows */
130 sx1 = area.x1;
131 sy1 = area.y1;
132 sx2 = area.x2;
133 sy2 = area.y2;
134 sw = sx2 - sx1;
135 sh = sy2 - sy1;
137 sw = isize * (sw / isize);
138 sh = isize * (sh / isize);
139 fullW = (sx2 - sx1) / isize;
140 fullH = (sy2 - sy1) / isize;
142 /* icon yard boundaries */
143 if (wPreferences.icon_yard & IY_VERT) {
144 pf = fullH;
145 sf = fullW;
146 } else {
147 pf = fullW;
148 sf = fullH;
150 if (wPreferences.icon_yard & IY_RIGHT) {
151 xo = sx2 - isize;
152 xs = -1;
153 } else {
154 xo = sx1;
155 xs = 1;
157 if (wPreferences.icon_yard & IY_TOP) {
158 yo = sy1;
159 ys = 1;
160 } else {
161 yo = sy2 - isize;
162 ys = -1;
166 * Create a map with the occupied slots. 1 means the slot is used
167 * or at least partially used.
168 * The slot usage can be optimized by only marking fully used slots
169 * or slots that have most of it covered.
170 * Space usage is worse than the fvwm algorithm (used in the old version)
171 * but complexity is much better (faster) than it.
173 map = wmalloc((sw + 2) * (sh + 2));
174 memset(map, 0, (sw + 2) * (sh + 2));
176 #define INDEX(x,y) (((y)+1)*(sw+2) + (x) + 1)
178 WM_ETARETI_BAG(scr->stacking_list, obj, iter) {
180 while (obj) {
181 int x, y;
183 if (iconPosition(obj, sx1, sy1, sx2, sy2, scr->current_workspace, &x, &y)) {
184 int xdi, ydi; /* rounded down */
185 int xui, yui; /* rounded up */
187 xdi = x / isize;
188 ydi = y / isize;
189 xui = (x + isize / 2) / isize;
190 yui = (y + isize / 2) / isize;
191 map[INDEX(xdi, ydi)] = 1;
192 map[INDEX(xdi, yui)] = 1;
193 map[INDEX(xui, ydi)] = 1;
194 map[INDEX(xui, yui)] = 1;
196 obj = obj->stacking->under;
199 /* Default position */
200 *x_ret = 0;
201 *y_ret = 0;
203 /* Look for an empty slot */
204 for (si = 0; si < sf; si++) {
205 for (pi = 0; pi < pf; pi++) {
206 if (wPreferences.icon_yard & IY_VERT) {
207 x = xo + xs * (si * isize);
208 y = yo + ys * (pi * isize);
209 } else {
210 x = xo + xs * (pi * isize);
211 y = yo + ys * (si * isize);
213 if (!map[INDEX(x / isize, y / isize)]) {
214 *x_ret = x;
215 *y_ret = y;
216 done = 1;
217 break;
220 if (done)
221 break;
224 wfree(map);
227 /* Computes the intersecting length of two line sections */
228 int calcIntersectionLength(int p1, int l1, int p2, int l2)
230 int isect;
231 int tmp;
233 if (p1 > p2) {
234 tmp = p1;
235 p1 = p2;
236 p2 = tmp;
237 tmp = l1;
238 l1 = l2;
239 l2 = tmp;
242 if (p1 + l1 < p2)
243 isect = 0;
244 else if (p2 + l2 < p1 + l1)
245 isect = l2;
246 else
247 isect = p1 + l1 - p2;
249 return isect;
252 /* Computes the intersecting area of two rectangles */
253 int calcIntersectionArea(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
255 return calcIntersectionLength(x1, w1, x2, w2)
256 * calcIntersectionLength(y1, h1, y2, h2);
259 static int calcSumOfCoveredAreas(WWindow *wwin, int x, int y, int w, int h)
261 int sum_isect = 0;
262 WWindow *test_window;
263 int tw, tx, ty, th;
265 test_window = wwin->screen_ptr->focused_window;
266 for (; test_window != NULL && test_window->prev != NULL;)
267 test_window = test_window->prev;
269 for (; test_window != NULL; test_window = test_window->next) {
270 if (test_window->frame->core->stacking->window_level < WMNormalLevel) {
271 continue;
274 tw = test_window->frame->core->width;
275 th = test_window->frame->core->height;
276 tx = test_window->frame_x;
277 ty = test_window->frame_y;
279 if (test_window->flags.mapped || (test_window->flags.shaded &&
280 test_window->frame->workspace == wwin->screen_ptr->current_workspace &&
281 !(test_window->flags.miniaturized || test_window->flags.hidden))) {
282 sum_isect += calcIntersectionArea(tx, ty, tw, th, x, y, w, h);
286 return sum_isect;
289 static void set_width_height(WWindow *wwin, unsigned int *width, unsigned int *height)
291 if (wwin->frame) {
292 *height += wwin->frame->top_width + wwin->frame->bottom_width;
293 } else {
294 if (HAS_TITLEBAR(wwin))
295 *height += TITLEBAR_HEIGHT;
296 if (HAS_RESIZEBAR(wwin))
297 *height += RESIZEBAR_HEIGHT;
299 if (HAS_BORDER(wwin)) {
300 *height += 2 * FRAME_BORDER_WIDTH;
301 *width += 2 * FRAME_BORDER_WIDTH;
305 static void
306 smartPlaceWindow(WWindow *wwin, int *x_ret, int *y_ret, unsigned int width,
307 unsigned int height, WArea usableArea)
309 int test_x = 0, test_y = Y_ORIGIN;
310 int from_x, to_x, from_y, to_y;
311 int sx;
312 int min_isect, min_isect_x, min_isect_y;
313 int sum_isect;
315 set_width_height(wwin, &width, &height);
317 sx = X_ORIGIN;
318 min_isect = INT_MAX;
319 min_isect_x = sx;
320 min_isect_y = test_y;
322 while (((test_y + height) < usableArea.y2)) {
323 test_x = sx;
324 while ((test_x + width) < usableArea.x2) {
325 sum_isect = calcSumOfCoveredAreas(wwin, test_x, test_y, width, height);
327 if (sum_isect < min_isect) {
328 min_isect = sum_isect;
329 min_isect_x = test_x;
330 min_isect_y = test_y;
333 test_x += PLACETEST_HSTEP;
335 test_y += PLACETEST_VSTEP;
338 from_x = min_isect_x - PLACETEST_HSTEP + 1;
339 from_x = WMAX(from_x, X_ORIGIN);
340 to_x = min_isect_x + PLACETEST_HSTEP;
341 if (to_x + width > usableArea.x2)
342 to_x = usableArea.x2 - width;
344 from_y = min_isect_y - PLACETEST_VSTEP + 1;
345 from_y = WMAX(from_y, Y_ORIGIN);
346 to_y = min_isect_y + PLACETEST_VSTEP;
347 if (to_y + height > usableArea.y2)
348 to_y = usableArea.y2 - height;
350 for (test_x = from_x; test_x < to_x; test_x++) {
351 for (test_y = from_y; test_y < to_y; test_y++) {
352 sum_isect = calcSumOfCoveredAreas(wwin, test_x, test_y, width, height);
354 if (sum_isect < min_isect) {
355 min_isect = sum_isect;
356 min_isect_x = test_x;
357 min_isect_y = test_y;
362 *x_ret = min_isect_x;
363 *y_ret = min_isect_y;
366 static Bool
367 autoPlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
368 unsigned int width, unsigned int height, int tryCount, WArea usableArea)
370 WScreen *scr = wwin->screen_ptr;
371 int test_x = 0, test_y = Y_ORIGIN;
372 int loc_ok = False, tw, tx, ty, th;
373 int swidth, sx;
374 WWindow *test_window;
376 set_width_height(wwin, &width, &height);
377 swidth = usableArea.x2 - usableArea.x1;
378 sx = X_ORIGIN;
380 /* this was based on fvwm2's smart placement */
381 while (((test_y + height) < (usableArea.y2 - usableArea.y1)) && !loc_ok) {
382 test_x = sx;
384 while (((test_x + width) < swidth) && (!loc_ok)) {
386 loc_ok = True;
387 test_window = scr->focused_window;
389 while ((test_window != NULL) && (loc_ok == True)) {
391 if (test_window->frame->core->stacking->window_level
392 < WMNormalLevel && tryCount > 0) {
393 test_window = test_window->next;
394 continue;
396 tw = test_window->frame->core->width;
397 th = test_window->frame->core->height;
398 tx = test_window->frame_x;
399 ty = test_window->frame_y;
401 if ((tx < (test_x + width)) && ((tx + tw) > test_x) &&
402 (ty < (test_y + height)) && ((ty + th) > test_y) &&
403 (test_window->flags.mapped ||
404 (test_window->flags.shaded &&
405 test_window->frame->workspace == scr->current_workspace &&
406 !(test_window->flags.miniaturized || test_window->flags.hidden)))) {
408 loc_ok = False;
410 test_window = test_window->next;
413 test_window = scr->focused_window;
415 while ((test_window != NULL) && (loc_ok == True)) {
417 if (test_window->frame->core->stacking->window_level
418 < WMNormalLevel && tryCount > 0) {
419 test_window = test_window->prev;
420 continue;
422 tw = test_window->frame->core->width;
423 th = test_window->frame->core->height;
424 tx = test_window->frame_x;
425 ty = test_window->frame_y;
427 if ((tx < (test_x + width)) && ((tx + tw) > test_x) &&
428 (ty < (test_y + height)) && ((ty + th) > test_y) &&
429 (test_window->flags.mapped ||
430 (test_window->flags.shaded &&
431 test_window->frame->workspace == scr->current_workspace &&
432 !(test_window->flags.miniaturized || test_window->flags.hidden)))) {
434 loc_ok = False;
436 test_window = test_window->prev;
438 if (loc_ok == True) {
439 *x_ret = test_x;
440 *y_ret = test_y;
441 break;
443 test_x += PLACETEST_HSTEP;
445 test_y += PLACETEST_VSTEP;
448 return loc_ok;
451 static void
452 cascadeWindow(WScreen *scr, WWindow *wwin, int *x_ret, int *y_ret,
453 unsigned int width, unsigned int height, int h, WArea usableArea)
455 set_width_height(wwin, &width, &height);
457 *x_ret = h * scr->cascade_index + X_ORIGIN;
458 *y_ret = h * scr->cascade_index + Y_ORIGIN;
460 if (width + *x_ret > usableArea.x2 || height + *y_ret > usableArea.y2) {
461 scr->cascade_index = 0;
462 *x_ret = X_ORIGIN;
463 *y_ret = Y_ORIGIN;
467 static void randomPlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
468 unsigned int width, unsigned int height, WArea usableArea)
470 int w, h;
472 set_width_height(wwin, &width, &height);
474 w = ((usableArea.x2 - X_ORIGIN) - width);
475 h = ((usableArea.y2 - Y_ORIGIN) - height);
476 if (w < 1)
477 w = 1;
478 if (h < 1)
479 h = 1;
480 *x_ret = X_ORIGIN + rand() % w;
481 *y_ret = Y_ORIGIN + rand() % h;
484 void PlaceWindow(WWindow *wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
486 WScreen *scr = wwin->screen_ptr;
487 int h = WMFontHeight(scr->title_font)
488 + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
489 WArea usableArea = wGetUsableAreaForHead(scr, wGetHeadForPointerLocation(scr),
490 NULL, True);
492 switch (wPreferences.window_placement) {
493 case WPM_MANUAL:
494 InteractivePlaceWindow(wwin, x_ret, y_ret, width, height);
495 break;
497 case WPM_SMART:
498 smartPlaceWindow(wwin, x_ret, y_ret, width, height, usableArea);
499 break;
501 case WPM_AUTO:
502 if (autoPlaceWindow(wwin, x_ret, y_ret, width, height, 0, usableArea)) {
503 break;
504 } else if (autoPlaceWindow(wwin, x_ret, y_ret, width, height, 1, usableArea)) {
505 break;
507 /* there isn't a break here, because if we fail, it should fall
508 through to cascade placement, as people who want tiling want
509 automagicness aren't going to want to place their window */
511 case WPM_CASCADE:
512 if (wPreferences.window_placement == WPM_AUTO)
513 scr->cascade_index++;
515 cascadeWindow(scr, wwin, x_ret, y_ret, width, height, h, usableArea);
517 if (wPreferences.window_placement == WPM_CASCADE)
518 scr->cascade_index++;
519 break;
521 case WPM_RANDOM:
522 randomPlaceWindow(wwin, x_ret, y_ret, width, height, usableArea);
523 break;
527 * clip to usableArea instead of full screen
528 * this will also take dock/clip etc.. into account
529 * aswell as being xinerama friendly
531 if (*x_ret + width > usableArea.x2)
532 *x_ret = usableArea.x2 - width;
533 if (*x_ret < usableArea.x1)
534 *x_ret = usableArea.x1;
536 if (*y_ret + height > usableArea.y2)
537 *y_ret = usableArea.y2 - height;
538 if (*y_ret < usableArea.y1)
539 *y_ret = usableArea.y1;