summarylogtreecommitdiffstats
path: root/0001-Rewrote-smartPlacement.patch
blob: 822ed226e97d415503560cf03bb29b948692cc4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
From ada426d2da8750421456ba51aa4c60be69a2fc3e Mon Sep 17 00:00:00 2001
From: Olivier Brunel <jjk@jjacky.com>
Date: Sun, 8 Mar 2015 13:22:12 +0100
Subject: [PATCH 1/3] Rewrote smartPlacement()

Instead of trying to find the position with the least overlaps, including all
stacked windows with the same weight, we now try to determine the place where
the lowest window on stack is visible, to put the new window there.

In effect, this means all windows hidden by another one are ignored, only
windows visible to the user should matter. We put the new window on top of the
window the lowest on stack, i.e. the last one to be activated/(fully) visible.

Signed-off-by: Olivier Brunel <jjk@jjacky.com>
---
 src/placement.c | 418 ++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 272 insertions(+), 146 deletions(-)

diff --git a/src/placement.c b/src/placement.c
index cae55ac..aba284f 100644
--- a/src/placement.c
+++ b/src/placement.c
@@ -555,16 +555,139 @@ clientAutoMaximize (Client * c, int full_w, int full_h)
 }
 
 static void
+expand_horizontal (cairo_region_t        *region,
+                   cairo_rectangle_int_t *rect,
+                   gint                   full_x,
+                   gint                   full_y,
+                   gint                   full_w,
+                   gint                   full_h)
+{
+    cairo_rectangle_int_t r;
+    cairo_region_overlap_t overlap;
+
+    /* a chance to expand on the left? */
+    if (rect->x > full_x)
+    {
+        /* try the full expansion first */
+        r.x = full_x;
+        r.y = rect->y;
+        r.width = rect->x;
+        r.height = rect->height;
+        overlap = cairo_region_contains_rectangle (region, &r);
+        if (overlap == CAIRO_REGION_OVERLAP_IN)
+            rect->x = r.x;
+        else if (overlap == CAIRO_REGION_OVERLAP_PART)
+        {
+            /* there might be some expansion possible */
+            r.x = rect->x;
+            r.width = 0;
+            do
+            {
+                --r.x;
+                ++r.width;
+                overlap = cairo_region_contains_rectangle (region, &r);
+            } while (overlap == CAIRO_REGION_OVERLAP_IN);
+            if (r.width - 1 > 0)
+                rect->x = r.x + 1;
+        }
+    }
+    /* a chance to expand on the right? */
+    if (rect->x + rect->width < full_x + full_w)
+    {
+        /* try the full expansion first */
+        r.x = rect->x + rect->width;
+        r.y = rect->y;
+        r.width = full_x + full_w - r.x;
+        r.height = rect->height;
+        overlap = cairo_region_contains_rectangle (region, &r);
+        if (overlap == CAIRO_REGION_OVERLAP_IN)
+            rect->width += r.width;
+        else if (overlap == CAIRO_REGION_OVERLAP_PART)
+        {
+            /* there might be some expansion possible */
+            r.width = 0;
+            do
+            {
+                ++r.width;
+                overlap = cairo_region_contains_rectangle (region, &r);
+            } while (overlap == CAIRO_REGION_OVERLAP_IN);
+            if (--r.width > 0)
+                rect->width += r.width;
+        }
+    }
+}
+
+static void
+expand_vertical (cairo_region_t        *region,
+                 cairo_rectangle_int_t *rect,
+                 gint                   full_x,
+                 gint                   full_y,
+                 gint                   full_w,
+                 gint                   full_h)
+{
+    cairo_rectangle_int_t r;
+    cairo_region_overlap_t overlap;
+
+    /* a chance to expand on top? */
+    if (rect->y > full_y)
+    {
+        /* try the full expansion first */
+        r.x = rect->x;
+        r.y = full_y;
+        r.width = rect->width;
+        r.height = rect->y - r.y;
+        overlap = cairo_region_contains_rectangle (region, &r);
+        if (overlap == CAIRO_REGION_OVERLAP_IN)
+            rect->y = r.y;
+        else if (overlap == CAIRO_REGION_OVERLAP_PART)
+        {
+            /* there might be some expansion possible */
+            r.height = 0;
+            r.y = rect->y;
+            do
+            {
+                --r.y;
+                ++r.height;
+                overlap = cairo_region_contains_rectangle (region, &r);
+            } while (overlap == CAIRO_REGION_OVERLAP_IN);
+            if (r.height - 1 > 0)
+                rect->y = r.y + 1;
+        }
+    }
+    /* a chance to expand on bottom? */
+    if (rect->y + rect->height < full_y + full_h)
+    {
+        /* try the full expansion first */
+        r.x = rect->x;
+        r.y = rect->y + rect->height;
+        r.width = rect->width;
+        r.height = full_y + full_h - r.y;
+        overlap = cairo_region_contains_rectangle (region, &r);
+        if (overlap == CAIRO_REGION_OVERLAP_IN)
+            rect->height += r.height;
+        else if (overlap == CAIRO_REGION_OVERLAP_PART)
+        {
+            /* there might be some expansion possible */
+            r.height = 0;
+            do
+            {
+                ++r.height;
+                overlap = cairo_region_contains_rectangle (region, &r);
+            } while (overlap == CAIRO_REGION_OVERLAP_IN);
+            if (--r.height > 0)
+                rect->height += r.height;
+        }
+    }
+}
+
+static void
 smartPlacement (Client * c, int full_x, int full_y, int full_w, int full_h)
 {
-    Client *c2;
     ScreenInfo *screen_info;
-    gfloat best_overlaps;
-    guint i;
-    gint test_x, test_y, xmax, ymax, best_x, best_y;
     gint frame_height, frame_width, frame_left, frame_top;
-    gint c2_x, c2_y;
-    gint xmin, ymin;
+    cairo_region_t *region_monitor, *region_used, *region_hole, *region_tmp;
+    cairo_rectangle_int_t rect;
+    GList *list;
 
     g_return_if_fail (c != NULL);
     TRACE ("entering smartPlacement");
@@ -575,165 +698,168 @@ smartPlacement (Client * c, int full_x, int full_y, int full_w, int full_h)
     frame_left = frameExtentLeft(c);
     frame_top = frameExtentTop (c);
 
-    /* max coordinates (bottom-right) */
-    xmax = full_x + full_w - c->width - frameExtentRight (c);
-    ymax = full_y + full_h - c->height - frameExtentBottom (c);
-
-    /* min coordinates (top-left) */
-    xmin = full_x + frameExtentLeft (c);
-    ymin = full_y + frameExtentTop (c);
-
-    /* start with worst-case position at top-left */
-    best_overlaps = G_MAXFLOAT;
-    best_x = xmin;
-    best_y = ymin;
-
-    TRACE ("analyzing %i clients", screen_info->client_count);
-
-    test_y = ymin;
-    do
+    /* region of the monitor (i.e. where we can/want to put the window) */
+    rect.x = full_x;
+    rect.y = full_y;
+    rect.width = full_w;
+    rect.height = full_h;
+    region_monitor = cairo_region_create_rectangle (&rect);
+    /* region where we'll add visible windows (i.e. used space) */
+    region_used = cairo_region_create ();
+    /* region of monitor we can use, i.e. monitor - used */
+    region_hole = NULL;
+
+    for (list = g_list_last (screen_info->windows_stack); list; list = g_list_previous (list))
     {
-        gint next_test_y = G_MAXINT;
-        gboolean first_test_x = TRUE;
-
-        TRACE ("testing y position %d", test_y);
-
-        test_x = xmin;
-        do
+        Client *c2 = list->data;
+        gint i, n;
+        gboolean done;
+        gint best_x, best_y;
+        gdouble best_surface = 0;
+        gboolean can_window_fit = FALSE;
+
+        if (!clientSelectMask (c2, NULL, SEARCH_INCLUDE_SKIP_PAGER | SEARCH_INCLUDE_SKIP_TASKBAR, WINDOW_REGULAR_FOCUSABLE))
+                continue;
+
+        /* rectangle for the window */
+        rect.x = frameExtentX (c2);
+        rect.y = frameExtentY (c2);
+        rect.width = frameExtentWidth (c2);
+        rect.height = frameExtentHeight (c2);
+
+        switch (cairo_region_contains_rectangle (region_monitor, &rect))
         {
-            gfloat count_overlaps = 0.0;
-            gint next_test_x = G_MAXINT;
-            gint c2_next_test_x;
-            gint c2_next_test_y;
-            gint c2_frame_height;
-            gint c2_frame_width;
-
-            TRACE ("testing x position %d", test_x);
-
-            for (c2 = screen_info->clients, i = 0; i < screen_info->client_count; c2 = c2->next, i++)
-            {
-                if ((c2 != c) && (c2->type != WINDOW_DESKTOP)
-                    && (c->win_workspace == c2->win_workspace)
-                    && FLAG_TEST (c2->xfwm_flags, XFWM_FLAG_VISIBLE))
+            case CAIRO_REGION_OVERLAP_OUT:
+                /* another monitor, ignore it */
+                continue;
+            case CAIRO_REGION_OVERLAP_IN:
+                /* add the window's rect to region_used */
+                cairo_region_union_rectangle (region_used, &rect);
+                break;
+            case CAIRO_REGION_OVERLAP_PART:
                 {
-                    c2_x = frameExtentX (c2);
-                    c2_frame_width = frameExtentWidth (c2);
-                    if (c2_x >= full_x + full_w
-                        || c2_x + c2_frame_width < full_x)
-                    {
-                        /* skip clients on right-of or left-of monitor */
-                        continue;
-                    }
-
-                    c2_y = frameExtentY (c2);
-                    c2_frame_height = frameExtentHeight (c2);
-                    if (c2_y >= full_y + full_h
-                        || c2_y + c2_frame_height < full_y)
-                    {
-                        /* skip clients on above-of or below-of monitor */
-                        continue;
-                    }
-
-                    count_overlaps += overlap (test_x - frame_left,
-                                               test_y - frame_top,
-                                               test_x - frame_left + frame_width,
-                                               test_y - frame_top + frame_height,
-                                               c2_x,
-                                               c2_y,
-                                               c2_x + c2_frame_width,
-                                               c2_y + c2_frame_height);
-
-                    /* find the next x boundy for the step */
-                    if (test_x > c2_x)
-                    {
-                        /* test location is beyond the x of the window,
-                         * take the window right corner as next target */
-                        c2_x += c2_frame_width;
-                    }
-                    c2_next_test_x = MIN (c2_x, xmax);
-                    if (c2_next_test_x < next_test_x
-                        && c2_next_test_x > test_x)
-                    {
-                        /* set new optimal next x step position */
-                        next_test_x = c2_next_test_x;
-                    }
-
-                    if (first_test_x)
-                    {
-                        /* find the next y boundry step */
-                        if (test_y > c2_y)
-                        {
-                            /* test location is beyond the y of the window,
-                             * take the window bottom corner as next target */
-                            c2_y += c2_frame_height;
-                        }
-                        c2_next_test_y = MIN (c2_y, ymax);
-                        if (c2_next_test_y < next_test_y
-                            && c2_next_test_y > test_y)
-                        {
-                            /* set new optimal next y step position */
-                            next_test_y = c2_next_test_y;
-                        }
-                    }
+                    /* get only the part of the window on our monitor */
+                    region_tmp = cairo_region_copy (region_monitor);
+                    cairo_region_intersect_rectangle (region_tmp, &rect);
+                    /* and add it to region_used */
+                    cairo_region_union (region_used, region_tmp);
+                    cairo_region_destroy (region_tmp);
                 }
-            }
+        }
 
-            /* don't look for the next y boundry this x row */
-            first_test_x = FALSE;
+        /* get a region of the monitor - visible window, i.e. leaving space
+         * where we can put out window. The goal is to reach a place where there
+         * isn't such space, and then go back one iteration, so we use the space
+         * of the visible (to the user) window the lowest on the stack */
+        region_tmp = cairo_region_copy (region_monitor);
+        cairo_region_subtract (region_tmp, region_used);
+
+        /* is there still free space left? */
+        n = cairo_region_num_rectangles (region_tmp);
+        done = TRUE;
+        for (i = 0; i < n; ++i)
+        {
+            cairo_region_get_rectangle (region_tmp, i, &rect);
+            /* ignore little areas between windows */
+            if (rect.width <= 15 || rect.height <= 15)
+                continue;
+            done = FALSE;
+            break;
+        }
+        if (!done)
+        {
+            if (region_hole)
+                cairo_region_destroy (region_hole);
+            region_hole = region_tmp;
+            continue;
+        }
+        cairo_region_destroy (region_tmp);
 
-            if (count_overlaps < best_overlaps)
-            {
-                /* found position with less overlap */
-                best_x = test_x;
-                best_y = test_y;
-                best_overlaps = count_overlaps;
+        /* got nothing, use defaults */
+        if (!region_hole)
+            break;
 
-                if (count_overlaps == 0.0f)
-                {
-                    /* overlap is ideal, stop searching */
-                    TRACE ("found position without overlap");
-                    goto found_best;
-                }
-            }
+        cairo_region_destroy (region_monitor);
+        cairo_region_destroy (region_used);
 
-            if (G_LIKELY (next_test_x != G_MAXINT))
-            {
-                test_x = MAX (next_test_x, next_test_x + frameExtentLeft (c));
-                if (test_x > xmax)
-                {
-                   /* always clamp on the monitor */
-                   test_x = xmax;
-                }
-            }
-            else
+        n = cairo_region_num_rectangles (region_hole);
+        for (i = 0; i < n; ++i)
+        {
+            cairo_rectangle_int_t r;
+            gint exp_x, exp_y;
+            gdouble exp_surface;
+            gboolean exp_can_window_fit;
+            gdouble surface;
+            gboolean can_fit;
+
+            cairo_region_get_rectangle (region_hole, i, &rect);
+
+            /* expand horizontally, then vertically */
+            expand_horizontal (region_hole, &rect, full_x, full_y, full_w, full_h);
+            expand_vertical (region_hole, &rect, full_x, full_y, full_w, full_h);
+            exp_x = rect.x;
+            exp_y = rect.y;
+            exp_surface = rect.width * rect.height;
+            exp_can_window_fit = frame_width <= rect.width && frame_height <= rect.height;
+
+            /* expand vertically, then horizontally */
+            expand_vertical (region_hole, &rect, full_x, full_y, full_w, full_h);
+            expand_horizontal (region_hole, &rect, full_x, full_y, full_w, full_h);
+            /* keep the best one */
+            surface = rect.width * rect.height;
+            can_fit = frame_width <= rect.width && frame_height <= rect.height;
+            /* if we can now fit the window, or still can't but in a larger
+             * rect; or still can but in a smaller rect */
+            if ((!exp_can_window_fit && (can_fit || surface > exp_surface))
+                    || (exp_can_window_fit && can_fit && surface < exp_surface))
             {
-                test_x++;
+                exp_can_window_fit = can_fit;
+                exp_surface = surface;
+                exp_x = rect.x;
+                exp_y = rect.y;
             }
-        }
-        while (test_x < xmax);
 
-        if (G_LIKELY (next_test_y != G_MAXINT))
-        {
-            test_y = MAX (next_test_y, next_test_y + frameExtentTop (c));
-            if (test_y > ymax)
+            /* is this the new best result ? (same criteria) */
+            if (best_surface == 0 /* our first result */
+                    || (!can_window_fit && (exp_can_window_fit || exp_surface > best_surface))
+                    || (can_window_fit && exp_can_window_fit && exp_surface < best_surface))
             {
-                /* always clamp on the monitor */
-                test_y = ymax;
+                can_window_fit = exp_can_window_fit;
+                best_surface = exp_surface;
+                best_x = exp_x;
+                best_y = exp_y;
             }
         }
-        else
+        cairo_region_destroy (region_hole);
+
+        c->x = best_x;
+        c->y = best_y;
+
+        /* unless it could fit, make sure it's fully within monitor */
+        if (!can_window_fit)
         {
-            test_y++;
+            /* move to the left if needed */
+            n = c->x + frame_width - full_x - full_w;
+            if (n > 0)
+                c->x -= n;
+            /* move to the top if needed */
+            n = c->y + frame_height - full_y - full_h;
+            if (n > 0)
+                c->y -= n;
         }
-    }
-    while (test_y < ymax);
 
-    found_best:
+        /* add frames */
+        c->x += frame_left;
+        c->y += frame_top;
+
+        return;
+    }
 
-    TRACE ("overlaps %f at %d,%d (x,y)", best_overlaps, best_x, best_y);
+    cairo_region_destroy (region_monitor);
+    cairo_region_destroy (region_used);
 
-    c->x = best_x;
-    c->y = best_y;
+    c->x = full_x + frame_left;
+    c->y = full_y + frame_top;
 }
 
 static void
-- 
2.3.1