summaryrefslogtreecommitdiff
path: root/src/modules/tiling/window_tree.c
blob: fa3fa8da644903b6551beb4c2c7d669b950da031 (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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#include "e.h"

#include "window_tree.h"
#include "e_mod_tiling.h"

void tiling_window_tree_dump(Window_Tree *root, int level);

void
tiling_window_tree_walk(Window_Tree *root, void (*func)(void *))
{
   Eina_Inlist *itr_safe;
   Window_Tree *itr;

   if (!root)
     return;

   EINA_INLIST_FOREACH_SAFE(root->children, itr_safe, itr)
     {
        tiling_window_tree_walk(itr, func);
     }
   func(root);
}

void
tiling_window_tree_free(Window_Tree *root)
{
   tiling_window_tree_walk(root, free);
}

static void
_tiling_window_tree_split_add(Window_Tree *parent, Window_Tree *new_node, Eina_Bool append)
{
   /* Make a new node for the parent client and split the weights in half. */
   Window_Tree *new_parent_client = calloc(1, sizeof(*new_node));

   new_node->parent = parent;
   new_parent_client->parent = parent;
   new_parent_client->client = parent->client;
   parent->client = NULL;
   new_parent_client->weight = 0.5;
   new_node->weight = 0.5;

   parent->children = eina_inlist_append(parent->children, EINA_INLIST_GET(new_parent_client));

   if (append)
     parent->children = eina_inlist_append(parent->children, EINA_INLIST_GET(new_node));
   else
     parent->children = eina_inlist_prepend(parent->children, EINA_INLIST_GET(new_node));

}
static void
_tiling_window_tree_parent_add(Window_Tree *parent, Window_Tree *new_node, Window_Tree *rel, Eina_Bool append)
{
   /* Adjust existing children's weights */
   Window_Tree *itr;
   int children_count = eina_inlist_count(parent->children);
   float weight = 1.0 / (children_count + 1);

   new_node->parent = parent;
   new_node->weight = weight;

   weight *= children_count;
   EINA_INLIST_FOREACH(parent->children, itr)
     {
        itr->weight *= weight;
     }
   if (append)
     {
        parent->children = eina_inlist_append_relative(parent->children,
                                                       EINA_INLIST_GET(new_node),
                                                       EINA_INLIST_GET(rel));
     }
   else
     {
        parent->children = eina_inlist_prepend_relative(parent->children,
                                                        EINA_INLIST_GET(new_node),
                                                        EINA_INLIST_GET(rel));
     }

}

static int
_tiling_window_tree_split_type_get(Window_Tree *node)
{
   int ret = 0;

   while (node->parent)
     {
        ret++;
        node = node->parent;
     }

   return ret % 2;
}

#define VERIFY_TYPE(t) if (t > TILING_SPLIT_VERTICAL || t < 0) { ERR("Invalid insert type"); return root; }

Window_Tree *
tiling_window_tree_insert(Window_Tree *root, Window_Tree *buddy,
                          E_Client *client, Tiling_Split_Type split_type, Eina_Bool before)
{
   Window_Tree *new_node;
   Window_Tree *parent;
   Tiling_Split_Type parent_split_type;

   VERIFY_TYPE(split_type)

   new_node = calloc(1, sizeof(*new_node));
   new_node->client = client;

   if (!root)
     {
        root = calloc(1, sizeof(*root));
        root->weight = 1.0;

        _tiling_window_tree_parent_add(root, new_node, NULL, !before);
     }
   else
     {
        //if there is no buddy we are going to take the last child of the root
        if (!buddy)
          {
             buddy = root;
             do
               {
                  buddy = EINA_INLIST_CONTAINER_GET(eina_inlist_last(buddy->children), Window_Tree);
               }
             while (!buddy->client);
          }
        else
          {
             //make sure this buddy has a client,
             if (!buddy->client) free(new_node);
             EINA_SAFETY_ON_TRUE_RETURN_VAL(!buddy->client, root);
          }

        parent = buddy->parent;
        parent_split_type = _tiling_window_tree_split_type_get(parent);

        if (parent_split_type == split_type)
          {
             _tiling_window_tree_parent_add(parent, new_node, buddy, !before);
          }
        else
          {
             _tiling_window_tree_split_add(buddy, new_node, !before);
          }
     }

   return root;
}

static Window_Tree *
tiling_window_tree_unref(Window_Tree *root, Window_Tree *item)
{
   if (!item->client)
     {
        ERR("Tried to unref node %p that doesn't have a client.", item);
        return NULL;
     }
   Window_Tree *parent = item->parent;
   int children_count = eina_inlist_count(item->parent->children);

   if (children_count <= 2)
     {
        Window_Tree *grand_parent = parent->parent;
        Window_Tree *item_keep = NULL;

        EINA_INLIST_FOREACH(parent->children, item_keep)
          {
             if (item_keep != item)
               break;
          }
        if (!item_keep)
          {
             parent->children =eina_inlist_remove(parent->children, EINA_INLIST_GET(item));
             return parent;
          }
        else if (!item_keep->children && (parent != root))
          {
             parent->client = item_keep->client;
             parent->children = NULL;
             return grand_parent; //we must have a grand_parent here, case the parent is not root
          }
        else
          {
             parent->children =
               eina_inlist_remove(parent->children, EINA_INLIST_GET(item));
             if (grand_parent)
               {
                  /* Update the children's parent. */
                  {
                     Eina_Inlist *itr_safe;
                     Window_Tree *itr;

                     EINA_INLIST_FOREACH_SAFE(item_keep->children, itr_safe,
                                               itr)
                       {
                          /* We are prepending to double-reverse the order. */
                          grand_parent->children =
                            eina_inlist_prepend_relative(grand_parent->children,
                                                         EINA_INLIST_GET(itr), EINA_INLIST_GET(parent));
                          itr->weight *= parent->weight;
                          itr->parent = grand_parent;
                       }

                     grand_parent->children =
                       eina_inlist_remove(grand_parent->children,
                                          EINA_INLIST_GET(parent));
                     free(parent);
                  }
                  return grand_parent;
               }
             else if (item_keep)
               {
                  /* This is fine, as this is a child of the root so we allow
                   * two levels. */
                  item_keep->weight = 1.0;
                  return item_keep->parent;
               }
          }
     }
   else
     {
        Window_Tree *itr;
        float weight = 1.0 - item->weight;

        parent->children =
          eina_inlist_remove(parent->children, EINA_INLIST_GET(item));

        /* Adjust existing children's weights */
        EINA_INLIST_FOREACH(parent->children, itr)
          {
             itr->weight /= weight;
          }
        return parent;
     }
     ERR("This is a state where we should never come to.\n");
     return NULL;
}

Window_Tree *
tiling_window_tree_remove(Window_Tree *root, Window_Tree *item)
{
   if (root == item)
     {
        free(item);
        return NULL;
     }
   else if (!item->client)
     {
        ERR("Tried deleting node %p that doesn't have a client.", item);
        return root;
     }
   tiling_window_tree_unref(root, item);
   free(item);
   if (eina_inlist_count(root->children) == 0)
     {
        //the last possible client was closed so we remove root
        free(root);
        return NULL;
     }

   return root;
}

Window_Tree *
tiling_window_tree_client_find(Window_Tree *root, E_Client *client)
{
   Window_Tree *itr;

   if (!client)
     return NULL;

   if (!root || (root->client == client))
     return root;

   EINA_INLIST_FOREACH(root->children, itr)
     {
        Window_Tree *ret;

        ret = tiling_window_tree_client_find(itr, client);
        if (ret)
          return ret;
     }

   return NULL;
}

void
_tiling_window_tree_level_apply(Window_Tree *root, Evas_Coord x, Evas_Coord y,
                                Evas_Coord w, Evas_Coord h, int level, Evas_Coord padding,
                                Eina_List **floaters)
{
   Window_Tree *itr;
   Tiling_Split_Type split_type = level % 2;
   double total_weight = 0.0;

   root->space.x = x;
   root->space.y = y;
   root->space.w = w - padding;
   root->space.h = h - padding;

   if (root->client)
     {
        if (!e_object_is_del(E_OBJECT(root->client)))
          {
             if ((root->client->icccm.min_w > (w - padding)) ||
                 (root->client->icccm.min_h > (h - padding)))
               *floaters = eina_list_append(*floaters, root->client);
             tiling_e_client_move_resize_extra(root->client, x, y,
                                               w - padding, h - padding);
          }
        return;
     }

   if (split_type == TILING_SPLIT_HORIZONTAL)
     {
        EINA_INLIST_FOREACH(root->children, itr)
          {
             Evas_Coord itw = w * itr->weight;

             total_weight += itr->weight;
             _tiling_window_tree_level_apply(itr, x, y, itw, h, level + 1, padding, floaters);
             x += itw;
          }
     }
   else if (split_type == TILING_SPLIT_VERTICAL)
     {
        EINA_INLIST_FOREACH(root->children, itr)
          {
             Evas_Coord ith = h * itr->weight;

             total_weight += itr->weight;
             _tiling_window_tree_level_apply(itr, x, y, w, ith, level + 1, padding, floaters);
             y += ith;
          }
     }

   /* Adjust the last item's weight in case weight < 1.0 */
   ((Window_Tree *)(void *)root->children->last)->weight += 1.0 - total_weight;
}

Eina_Bool
tiling_window_tree_apply(Window_Tree *root, Evas_Coord x, Evas_Coord y,
                         Evas_Coord w, Evas_Coord h, Evas_Coord padding,
                         Eina_Bool force_float)
{
   Eina_List *floaters = NULL;
   E_Client *ec;

   x += padding;
   y += padding;
   w -= padding;
   h -= padding;
   _tiling_window_tree_level_apply(root, x, y, w, h, 0, padding, &floaters);

   if (floaters)
     {
        // some clients failed to properly fit and maybe SHOULD float
        EINA_LIST_FREE(floaters, ec)
          {
             if (force_float) tiling_e_client_does_not_fit(ec);
          }
        return EINA_FALSE;
     }
   return EINA_TRUE;
}

static Window_Tree *
_inlist_next(Window_Tree *it)
{
   return (Window_Tree *)(void *)EINA_INLIST_GET(it)->next;
}

static Window_Tree *
_inlist_prev(Window_Tree *it)
{
   return (Window_Tree *)(void *)EINA_INLIST_GET(it)->prev;
}

static Eina_Bool
_tiling_window_tree_node_resize_direction(Window_Tree *node,
                                          Window_Tree *parent, double dir_diff, int dir)
{
   double weight = 0.0;
   double weight_diff;
   Window_Tree *children_start;
   Window_Tree *itr;
   Window_Tree *(*itr_func)(Window_Tree *);

   if (dir > 0)
     {
        itr_func = _inlist_prev;
        children_start = (Window_Tree *)(void *)parent->children->last;
     }
   else
     {
        itr_func = _inlist_next;
        children_start = (Window_Tree *)(void *)parent->children;
     }

   itr = (Window_Tree *)children_start;
   while (itr != node)
     {
        weight += itr->weight;

        itr = itr_func(itr);
     }

   /* If it's at the edge, try the grandpa of the parent. */
   if (!EINA_DBL_NONZERO(weight))
     {
        if (parent->parent && parent->parent->parent)
          {
             return _tiling_window_tree_node_resize_direction(parent->parent,
                                                              parent->parent->parent, 1.0 + ((dir_diff - 1.) * node->weight),
                                                              dir);
          }
        return EINA_FALSE;
     }

   weight_diff = itr->weight;
   itr->weight *= dir_diff;
   weight_diff -= itr->weight;
   weight_diff /= weight;

   for (itr = children_start; itr != node; itr = itr_func(itr))
     {
        itr->weight += itr->weight * weight_diff;
     }

   return EINA_TRUE;
}

Eina_Bool
tiling_window_tree_node_resize(Window_Tree *node, int w_dir, double w_diff,
                               int h_dir, double h_diff)
{
   Window_Tree *parent = node->parent;
   Window_Tree *w_parent, *h_parent;
   Eina_Bool ret = EINA_FALSE;

   /* If we have no parent, means we need to be full screen anyway. */
   if (!parent)
     return EINA_FALSE;

   Window_Tree *grand_parent = parent->parent;
   Tiling_Split_Type parent_split_type =
     _tiling_window_tree_split_type_get(parent);

   /* w_diff related changes. */
   if (parent_split_type == TILING_SPLIT_HORIZONTAL)
     {
        w_parent = parent;
        h_parent = grand_parent;
     }
   else
     {
        w_parent = grand_parent;
        h_parent = parent;
     }

   if ((!eina_dbl_exact(h_diff, 1.0)) && h_parent)
     {
        Window_Tree *tmp_node = (h_parent == parent) ? node : parent;

        ret = ret ||
          _tiling_window_tree_node_resize_direction(tmp_node, h_parent,
                                                    h_diff, h_dir);
     }

   if ((!eina_dbl_exact(w_diff, 1.0)) && w_parent)
     {
        Window_Tree *tmp_node = (w_parent == parent) ? node : parent;

        ret = ret ||
          _tiling_window_tree_node_resize_direction(tmp_node, w_parent,
                                                    w_diff, w_dir);
     }

   return ret;
}

int
_tiling_window_tree_edges_get_helper(Window_Tree *node,
                                     Tiling_Split_Type split_type, Eina_Bool gave_up_this,
                                     Eina_Bool gave_up_parent)
{
   int ret =
     TILING_WINDOW_TREE_EDGE_LEFT | TILING_WINDOW_TREE_EDGE_RIGHT |
     TILING_WINDOW_TREE_EDGE_TOP | TILING_WINDOW_TREE_EDGE_BOTTOM;
   if (!node->parent)
     {
        return ret;
     }
   else if (gave_up_this && gave_up_parent)
     {
        return 0;
     }
   else if (gave_up_this)
     {
        /* Mixed the gave_up vals on purpose, we do it on every call. */
        return _tiling_window_tree_edges_get_helper(node->parent, !split_type,
                                                    gave_up_parent, gave_up_this);
     }

   if (EINA_INLIST_GET(node)->prev)
     {
        gave_up_this = EINA_TRUE;
        ret ^=
          (split_type ==
           TILING_SPLIT_HORIZONTAL) ? TILING_WINDOW_TREE_EDGE_LEFT :
          TILING_WINDOW_TREE_EDGE_TOP;
     }

   if (EINA_INLIST_GET(node)->next)
     {
        gave_up_this = EINA_TRUE;
        ret ^=
          (split_type ==
           TILING_SPLIT_HORIZONTAL) ? TILING_WINDOW_TREE_EDGE_RIGHT :
          TILING_WINDOW_TREE_EDGE_BOTTOM;
     }

   /* Mixed the gave_up vals on purpose, we do it on every call. */
   return ret & _tiling_window_tree_edges_get_helper(node->parent, !split_type,
                                                     gave_up_parent, gave_up_this);
}

int
tiling_window_tree_edges_get(Window_Tree *node)
{
   Tiling_Split_Type split_type = _tiling_window_tree_split_type_get(node);

   return _tiling_window_tree_edges_get_helper(node, !split_type, EINA_FALSE,
                                               EINA_FALSE);
}

/* Node move */
/**
 * - break would mean that the node will be breaked out in the parent node and
 *   put into the grand parent node. If there is no grand parent node a fake node will be placed there.
 *
 * - join would mean that the node will be put together with the next/previous
 *   child into a new split.
 *
 * - The eina_bool dir is the flag in which direction the node will be added ,
 *   appended or prependet, or joined with the previous child or the next child
 */
void
_tiling_window_tree_node_break_out(Window_Tree *root, Window_Tree *node, Window_Tree *par, Eina_Bool dir)
{
   Window_Tree *res, *ac; /* ac is the child of the root, that is a parent of a node */

   if (!par)
     {
        /* we have no parent, so we add a new node between this, later we do the normal break, */
        Window_Tree *rnode, *newnode, *newnode2;
        Eina_Inlist *il;

        newnode2 = calloc(1, sizeof(Window_Tree));
        newnode2->parent = root;
        newnode2->weight = 1.0;

        newnode = calloc(1, sizeof(Window_Tree));
        newnode->weight = 1.0;
        newnode->parent = newnode2;

        EINA_INLIST_FOREACH_SAFE(root->children, il, rnode)
          {
             rnode->parent = newnode;
             root->children = eina_inlist_remove(root->children, EINA_INLIST_GET(rnode));
             newnode->children = eina_inlist_append(newnode->children, EINA_INLIST_GET(rnode));
          }

        root->children = eina_inlist_append(root->children, EINA_INLIST_GET(newnode2));
        newnode2->children = eina_inlist_append(newnode2->children, EINA_INLIST_GET(newnode));
        par = newnode2;

     }

   /* search a path from the node to the par */
   ac = node;
   while (ac->parent != par)
     ac = ac->parent;

   if (dir)
     {
        res = _inlist_next(ac);
        if (res)
          {
             dir = EINA_FALSE;
          }
     }
   else
     {
        res = _inlist_prev(ac);
        if (res)
          {
             dir = EINA_TRUE;
          }
     }

   tiling_window_tree_unref(root, node);

   _tiling_window_tree_parent_add(par, node, res, dir);
}

void
_tiling_window_tree_node_join(Window_Tree *root, Window_Tree *node, Eina_Bool dir)
{
   Window_Tree *pn, *pl, *par;

   if (dir)
      pn = _inlist_next(node);
   else
      pn = _inlist_prev(node);

   if (!pn)
     {
        if (node->parent && node->parent->parent && node->parent->parent->parent)
          {
             _tiling_window_tree_node_break_out(root, node, node->parent->parent->parent, dir);
          }

        return;
     }

   par = node->parent;
   if ((eina_inlist_count(par->children) == 2) &&
      ((_inlist_next(node) && _inlist_next(node)->client) ||
       (_inlist_prev(node) && _inlist_prev(node)->client)))
      /* swap if there are just 2 simple windows*/
     {
        par->children = eina_inlist_demote(par->children, eina_inlist_first(par->children));
        return;
     }
   else
     {
        pl = tiling_window_tree_unref(root, node);
        if (pl == node->parent)
          {
             //unref has not changed the tree
             if (!pn->children)
               {
                  _tiling_window_tree_split_add(pn, node, EINA_TRUE);
               }
             else
               {
                  _tiling_window_tree_parent_add(pn, node, NULL, EINA_TRUE);
               }
          }
        else
          {
             //unref changed the position of pn in the tree, result of unref
             //will be the new parent
             _tiling_window_tree_parent_add(pl, node, NULL, EINA_TRUE);
          }
     }
}

void
tiling_window_tree_node_change_pos(Window_Tree *node, int key)
{
   if (!node->parent)
     return;

   Tiling_Split_Type parent_split_type =
     _tiling_window_tree_split_type_get(node->parent);

   Window_Tree *root = node->parent,
               *grand_parent = NULL;
   while(root->parent)
      root = root->parent;

   if (node->parent && node->parent->parent)
     grand_parent = node->parent->parent;

   switch(key)
     {
      case TILING_WINDOW_TREE_EDGE_LEFT:
         if (parent_split_type == TILING_SPLIT_HORIZONTAL)
            _tiling_window_tree_node_join(root, node, EINA_FALSE);
         else if (parent_split_type == TILING_SPLIT_VERTICAL)
            _tiling_window_tree_node_break_out(root, node, grand_parent, EINA_FALSE);
         break;
      case TILING_WINDOW_TREE_EDGE_RIGHT:
         if (parent_split_type == TILING_SPLIT_HORIZONTAL)
            _tiling_window_tree_node_join(root, node, EINA_TRUE);
         else if (parent_split_type == TILING_SPLIT_VERTICAL)
            _tiling_window_tree_node_break_out(root, node, grand_parent,EINA_TRUE);
         break;
      case TILING_WINDOW_TREE_EDGE_TOP:
         if (parent_split_type == TILING_SPLIT_HORIZONTAL)
            _tiling_window_tree_node_break_out(root, node, grand_parent, EINA_FALSE);
         else if (parent_split_type == TILING_SPLIT_VERTICAL)
            _tiling_window_tree_node_join(root, node, EINA_FALSE);
         break;
      case TILING_WINDOW_TREE_EDGE_BOTTOM:
         if (parent_split_type == TILING_SPLIT_HORIZONTAL)
            _tiling_window_tree_node_break_out(root, node, grand_parent, EINA_TRUE);
         else if (parent_split_type == TILING_SPLIT_VERTICAL)
            _tiling_window_tree_node_join(root, node, EINA_TRUE);
          break;
     }
}

/* End Node move. */

void
tiling_window_tree_dump(Window_Tree *root, int level)
{
   int i;

   if (!root)
     return;

   for (i = 0; i < level; i++)
     printf(" ");

   if (root->children)
     printf("\\-");
   else
     printf("|-");

   printf("%f (%p)\n", root->weight, root->client);

   Window_Tree *itr;

   EINA_INLIST_FOREACH(root->children, itr)
     {
        tiling_window_tree_dump(itr, level + 1);
     }
}