forked from project-repo/cagebreak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keybinding.c
1798 lines (1681 loc) · 52.5 KB
/
keybinding.c
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 - 2023, project-repo and the cagebreak contributors
// SPDX -License-Identifier: MIT
#define _POSIX_C_SOURCE 200809L
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <wayland-server-core.h>
#include <wlr/backend/multi.h>
#include <wlr/backend/session.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_keyboard_group.h>
#include <wlr/types/wlr_output_damage.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/util/log.h>
#include "input.h"
#include "input_manager.h"
#include "keybinding.h"
#include "message.h"
#include "output.h"
#include "seat.h"
#include "server.h"
#include "util.h"
#include "view.h"
#include "workspace.h"
char *keybinding_action_string[] = {FOREACH_KEYBINDING(GENERATE_STRING)};
int
keybinding_resize(struct keybinding_list *list) {
if(list->length == list->capacity) {
list->capacity *= 2;
struct keybinding **new_list =
realloc(list->keybindings, sizeof(void *) * list->capacity);
if(new_list == NULL) {
return -1;
} else {
list->keybindings = new_list;
return 0;
}
}
return 0;
}
struct keybinding **
find_keybinding(const struct keybinding_list *list,
const struct keybinding *keybinding) {
struct keybinding **it = list->keybindings;
for(size_t i = 0; i < list->length; ++i, ++it) {
if(!(keybinding->modifiers ^ (*it)->modifiers) &&
keybinding->mode == (*it)->mode && keybinding->key == (*it)->key) {
return it;
}
}
return NULL;
}
void
keybinding_free(struct keybinding *keybinding, bool recursive) {
switch(keybinding->action) {
case KEYBINDING_DEFINEMODE:
case KEYBINDING_RUN_COMMAND:
if(keybinding->data.c != NULL) {
free(keybinding->data.c);
}
break;
case KEYBINDING_DEFINEKEY:
if(keybinding->data.kb != NULL && recursive) {
keybinding_free(keybinding->data.kb, true);
}
break;
case KEYBINDING_CONFIGURE_OUTPUT:
free(keybinding->data.o_cfg->output_name);
free(keybinding->data.o_cfg);
break;
case KEYBINDING_CONFIGURE_INPUT:
free(keybinding->data.i_cfg->identifier);
if(keybinding->data.i_cfg->mapped_from_region) {
free(keybinding->data.i_cfg->mapped_from_region);
}
if(keybinding->data.i_cfg->mapped_to_output) {
free(keybinding->data.i_cfg->mapped_to_output);
}
free(keybinding->data.o_cfg);
break;
case KEYBINDING_CONFIGURE_MESSAGE:
if(keybinding->data.m_cfg->font != NULL) {
free(keybinding->data.m_cfg->font);
}
break;
case KEYBINDING_DISPLAY_MESSAGE:
if(keybinding->data.c != NULL) {
free(keybinding->data.c);
}
default:
break;
}
free(keybinding);
}
int
keybinding_list_push(struct keybinding_list *list,
struct keybinding *keybinding) {
/* Error resizing list */
if(keybinding_resize(list) != 0) {
return -1;
}
/*Maintain that only a single keybinding for a key, modifier and mode may
* exist*/
struct keybinding **found_keybinding = find_keybinding(list, keybinding);
if(found_keybinding != NULL) {
keybinding_free(*found_keybinding, true);
*found_keybinding = keybinding;
wlr_log(WLR_DEBUG, "A keybinding was found twice in the config file.");
} else {
list->keybindings[list->length] = keybinding;
++list->length;
}
return 0;
}
struct keybinding_list *
keybinding_list_init(void) {
struct keybinding_list *list = malloc(sizeof(struct keybinding_list));
list->keybindings = malloc(sizeof(struct keybinding *));
list->capacity = 1;
list->length = 0;
return list;
}
void
keybinding_list_free(struct keybinding_list *list) {
if(!list) {
return;
}
for(unsigned int i = 0; i < list->length; ++i) {
keybinding_free(list->keybindings[i], true);
}
free(list->keybindings);
free(list);
}
// Returns whether x is between a and b (a exclusive, b exclusive) where
// a<b
bool
is_between_strict(int a, int b, int x) {
return a < x && x < b;
}
struct cg_tile *
find_right_tile(const struct cg_tile *tile) {
struct cg_tile *it = tile->next;
int center = tile->tile.y + tile->tile.height / 2;
while(it != tile) {
if(it->tile.x == tile->tile.x + tile->tile.width) {
if(is_between_strict(it->tile.y, it->tile.y + it->tile.height,
center) ||
it->tile.y + it->tile.height == center) {
return it;
}
}
it = it->next;
}
return NULL;
}
struct cg_tile *
find_left_tile(const struct cg_tile *tile) {
struct cg_tile *it = tile->next;
int center = tile->tile.y + tile->tile.height / 2;
while(it != tile) {
if(it->tile.x + it->tile.width == tile->tile.x) {
if(is_between_strict(it->tile.y, it->tile.y + it->tile.height,
center) ||
it->tile.y + it->tile.height == center) {
return it;
}
}
it = it->next;
}
return NULL;
}
struct cg_tile *
find_top_tile(const struct cg_tile *tile) {
struct cg_tile *it = tile->next;
int center = tile->tile.x + tile->tile.width / 2;
while(it != tile) {
if(it->tile.y + it->tile.height == tile->tile.y) {
if(is_between_strict(it->tile.x, it->tile.x + it->tile.width,
center) ||
it->tile.x + it->tile.width == center) {
return it;
}
}
it = it->next;
}
return NULL;
}
struct cg_tile *
find_bottom_tile(const struct cg_tile *tile) {
struct cg_tile *it = tile->next;
int center = tile->tile.x + tile->tile.width / 2;
while(it != tile) {
if(it->tile.y == tile->tile.y + tile->tile.height) {
if(is_between_strict(it->tile.x, it->tile.x + it->tile.width,
center) ||
it->tile.x + it->tile.width == center) {
return it;
}
}
it = it->next;
}
return NULL;
}
void
swap_tile(struct cg_tile *tile,
struct cg_tile *(*find_tile)(const struct cg_tile *)) {
struct cg_tile *swap_tile = find_tile(tile);
struct cg_server *server = tile->workspace->server;
if(swap_tile == NULL || swap_tile == tile) {
return;
}
struct cg_view *tmp_view = tile->view;
struct cg_view *tmp_swap_view = swap_tile->view;
workspace_tile_update_view(tile, NULL);
workspace_tile_update_view(swap_tile, tmp_view);
workspace_tile_update_view(tile, tmp_swap_view);
workspace_focus_tile(
server->curr_output->workspaces[server->curr_output->curr_workspace],
swap_tile);
seat_set_focus(server->seat, swap_tile->view);
ipc_send_event(
tile->workspace->output->server,
"{\"event_name\":\"swap_tile\",\"tile_id\":%d,\"swap_"
"tile_id\":%d,\"workspace\":%d,\"output\":\"%s\",\"output_id\":%d}",
tile->id, swap_tile->id, tile->workspace->num + 1,
tile->workspace->output->wlr_output->name,
output_get_num(tile->workspace->output));
}
void
swap_tile_left(struct cg_tile *tile) {
swap_tile(tile, find_left_tile);
}
void
swap_tile_right(struct cg_tile *tile) {
swap_tile(tile, find_right_tile);
}
void
swap_tile_top(struct cg_tile *tile) {
swap_tile(tile, find_top_tile);
}
void
swap_tile_bottom(struct cg_tile *tile) {
swap_tile(tile, find_bottom_tile);
}
void
focus_tile(struct cg_tile *tile,
struct cg_tile *(*find_tile)(const struct cg_tile *tile)) {
struct cg_tile *new_tile = find_tile(tile);
if(new_tile == NULL) {
return;
}
struct cg_server *server = new_tile->workspace->server;
workspace_focus_tile(tile->workspace, new_tile);
seat_set_focus(server->seat, new_tile->view);
}
void
focus_tile_left(struct cg_tile *tile) {
focus_tile(tile, find_left_tile);
}
void
focus_tile_right(struct cg_tile *tile) {
focus_tile(tile, find_right_tile);
}
void
focus_tile_top(struct cg_tile *tile) {
focus_tile(tile, find_top_tile);
}
void
focus_tile_bottom(struct cg_tile *tile) {
focus_tile(tile, find_bottom_tile);
}
int
get_compl_coord(struct cg_tile *tile, int *(*get_coord)(struct cg_tile *tile)) {
return tile->tile.x + tile->tile.y - *get_coord(tile);
}
int
get_compl_dim(struct cg_tile *tile, int *(*get_dim)(struct cg_tile *tile)) {
return tile->tile.width + tile->tile.height - *get_dim(tile);
}
bool
intervalls_intersect(int x1, int x2, int y1, int y2) {
return y2 > x1 && y1 < x2;
}
bool
resize_allowed(struct cg_tile *tile, const struct cg_tile *parent,
int coord_offset, int dim_offset,
int *(*get_coord)(struct cg_tile *tile),
int *(*get_dim)(struct cg_tile *tile), struct cg_tile *orig) {
if(coord_offset == 0 && dim_offset == 0) {
return true;
} else if(*get_dim(tile) - coord_offset + dim_offset <= 0) {
return false;
}
for(struct cg_tile *it = tile->next; it != tile && it != NULL;
it = it->next) {
if(it == parent || it == orig) {
continue;
}
if(intervalls_intersect(
get_compl_coord(tile, get_coord),
get_compl_coord(tile, get_coord) + get_compl_dim(tile, get_dim),
get_compl_coord(it, get_coord),
get_compl_coord(it, get_coord) + get_compl_dim(it, get_dim))) {
if(*get_coord(it) == *get_coord(tile) + *get_dim(tile)) {
if(!resize_allowed(it, tile, dim_offset + coord_offset,
-dim_offset - coord_offset, get_coord,
get_dim, orig)) {
return false;
}
} else if(*get_coord(it) + *get_dim(it) == *get_coord(tile)) {
if(!resize_allowed(it, tile, 0, coord_offset, get_coord,
get_dim, orig)) {
return false;
}
}
}
}
return true;
}
void
resize(struct cg_tile *tile, const struct cg_tile *parent, int coord_offset,
int dim_offset, int *(*get_coord)(struct cg_tile *tile),
int *(*get_dim)(struct cg_tile *tile), struct cg_tile *orig) {
if(coord_offset == 0 && dim_offset == 0) {
return;
}
for(struct cg_tile *it = tile->next; it != tile && it != NULL;
it = it->next) {
if(it == parent || it == orig) {
continue;
}
if(intervalls_intersect(
get_compl_coord(tile, get_coord),
get_compl_coord(tile, get_coord) + get_compl_dim(tile, get_dim),
get_compl_coord(it, get_coord),
get_compl_coord(it, get_coord) + get_compl_dim(it, get_dim))) {
if(*get_coord(it) == *get_coord(tile) + *get_dim(tile)) {
resize(it, tile, dim_offset + coord_offset,
-dim_offset - coord_offset, get_coord, get_dim, orig);
} else if(*get_coord(it) + *get_dim(it) == *get_coord(tile)) {
resize(it, tile, 0, coord_offset, get_coord, get_dim, orig);
}
}
}
int old_x = tile->tile.x, old_y = tile->tile.y,
old_height = tile->tile.height, old_width = tile->tile.width;
*get_coord(tile) += coord_offset;
*get_dim(tile) += dim_offset;
if(tile->view != NULL) {
view_maximize(tile->view, tile);
}
ipc_send_event(tile->workspace->output->server,
"{\"event_name\":\"resize_tile\",\"tile_id\":%d,\"old_"
"dims\":\"[%d,%d,%d,%d]\",\"new_dims\":\"[%d,%d,%d,%d]\","
"\"workspace\":%d,\"output\":\"%s\",\"output_id\":%d}",
tile->id, old_x, old_y, old_height, old_width, tile->tile.x,
tile->tile.y, tile->tile.height, tile->tile.width,
tile->workspace->num + 1,
tile->workspace->output->wlr_output->name,
output_get_num(tile->workspace->output));
}
int *
get_width(struct cg_tile *tile) {
return &tile->tile.width;
}
int *
get_height(struct cg_tile *tile) {
return &tile->tile.height;
}
int *
get_x(struct cg_tile *tile) {
return &tile->tile.x;
}
int *
get_y(struct cg_tile *tile) {
return &tile->tile.y;
}
bool
resize_allowed_horizontal(struct cg_tile *tile, struct cg_tile *parent,
int x_offset, int width_offset) {
return resize_allowed(tile, parent, x_offset, width_offset, get_x,
get_width, tile);
}
bool
resize_allowed_vertical(struct cg_tile *tile, struct cg_tile *parent,
int y_offset, int height_offset) {
return resize_allowed(tile, parent, y_offset, height_offset, get_y,
get_height, tile);
}
void
resize_horizontal(struct cg_tile *tile, struct cg_tile *parent, int x_offset,
int width_offset) {
resize(tile, parent, x_offset, width_offset, get_x, get_width, tile);
}
void
resize_vertical(struct cg_tile *tile, struct cg_tile *parent, int y_offset,
int height_offset) {
resize(tile, parent, y_offset, height_offset, get_y, get_height, tile);
}
/* hpixs: positiv -> right, negative -> left; vpixs: positiv -> down, negative
* -> up */
void
resize_tile(struct cg_server *server, int hpixs, int vpixs) {
struct cg_output *output = server->curr_output;
struct wlr_box output_box;
wlr_output_layout_get_box(output->server->output_layout, output->wlr_output,
&output_box);
struct cg_tile *focused =
output->workspaces[output->curr_workspace]->focused_tile;
/* First do the horizontal adjustment */
if(hpixs != 0 && focused->tile.width < output_box.width &&
is_between_strict(0, output_box.width, focused->tile.width + hpixs)) {
int x_offset = 0;
/* In case we are on the total right, move the left edge of the tile */
if(focused->tile.x + focused->tile.width == output_box.width) {
x_offset = -hpixs;
}
bool resize_allowed =
resize_allowed_horizontal(focused, NULL, x_offset, hpixs);
if(resize_allowed) {
resize_horizontal(focused, NULL, x_offset, hpixs);
}
}
/* Repeat for vertical */
if(vpixs != 0 && focused->tile.height < output_box.height &&
is_between_strict(0, output_box.height, focused->tile.height + vpixs)) {
int y_offset = 0;
if(focused->tile.y + focused->tile.height == output_box.height) {
y_offset = -vpixs;
}
bool resize_allowed =
resize_allowed_vertical(focused, NULL, y_offset, vpixs);
if(resize_allowed) {
resize_vertical(focused, NULL, y_offset, vpixs);
}
}
}
void
keybinding_workspace_fullscreen(struct cg_server *server) {
output_make_workspace_fullscreen(server->curr_output,
server->curr_output->curr_workspace);
struct cg_output *output = server->curr_output;
ipc_send_event(server,
"{\"event_name\":\"fullscreen\",\"tile_id\":%d,"
"\"workspace\":%d,\"output\":\"%s\",\"output_id\":%d}",
output->workspaces[output->curr_workspace]->focused_tile->id,
output->workspaces[output->curr_workspace]->num + 1,
output->wlr_output->name, output_get_num(output));
}
// Switch to a differerent virtual terminal
static int
keybinding_switch_vt(struct wlr_backend *backend, unsigned int vt) {
if(wlr_backend_is_multi(backend)) {
struct wlr_session *session = wlr_backend_get_session(backend);
if(session) {
wlr_session_change_vt(session, vt);
}
return 0;
}
return -1;
}
/* Split screen (vertical or horizontal)
* Important: Do not attempt to perform mathematical simplifications in this
* function without taking rounding errors into account. */
static void
keybinding_split_output(struct cg_output *output, bool vertical) {
struct cg_view *original_view = seat_get_focus(output->server->seat);
struct cg_workspace *curr_workspace =
output->workspaces[output->curr_workspace];
int32_t width = curr_workspace->focused_tile->tile.width;
int32_t height = curr_workspace->focused_tile->tile.height;
int32_t x = curr_workspace->focused_tile->tile.x;
int32_t y = curr_workspace->focused_tile->tile.y;
int32_t new_width, new_height;
if(vertical) {
new_width = width / 2;
new_height = height;
} else {
new_width = width;
new_height = height / 2;
}
if(new_width < 1 || new_height < 1) {
return;
}
struct cg_view *next_view = NULL;
struct cg_view *it;
wl_list_for_each(it, &output->workspaces[output->curr_workspace]->views,
link) {
if(!view_is_visible(it) && original_view != it) {
next_view = it;
break;
}
}
int32_t new_x, new_y;
if(vertical) {
new_x = x + new_width;
new_y = y;
} else {
new_x = x;
new_y = y + new_height;
}
struct cg_tile *new_tile = calloc(1, sizeof(struct cg_tile));
if(!new_tile) {
wlr_log(WLR_ERROR, "Failed to allocate new tile for splitting");
return;
}
new_tile->id = output->server->tiles_curr_id;
++output->server->tiles_curr_id;
new_tile->tile.x = new_x;
new_tile->tile.y = new_y;
new_tile->tile.width = x + width - new_x;
new_tile->tile.height = y + height - new_y;
new_tile->prev = curr_workspace->focused_tile;
new_tile->next = curr_workspace->focused_tile->next;
workspace_tile_update_view(new_tile, next_view);
new_tile->workspace = curr_workspace;
curr_workspace->focused_tile->next->prev = new_tile;
curr_workspace->focused_tile->next = new_tile;
curr_workspace->focused_tile->tile.width = new_width;
curr_workspace->focused_tile->tile.height = new_height;
workspace_focus_tile(curr_workspace, curr_workspace->focused_tile);
if(next_view != NULL) {
view_maximize(next_view, new_tile);
}
if(original_view != NULL) {
view_maximize(original_view, curr_workspace->focused_tile);
}
ipc_send_event(
output->server,
"{\"event_name\":\"split\",\"tile_id\":%d,\"new_tile_id\":%d,"
"\"workspace\":%d,\"output\":\"%s\",\"output_id\":%d,\"vertical\":%d}",
curr_workspace->focused_tile->id, new_tile->id, curr_workspace->num + 1,
curr_workspace->output->wlr_output->name,
output_get_num(curr_workspace->output), vertical);
}
static void
keybinding_close_view(struct cg_view *view) {
if(view == NULL) {
return;
}
struct cg_output *outp = view->workspace->output;
uint32_t view_id = view->id;
uint32_t view_pid = view->impl->get_pid(view);
uint32_t tile_id = view->id;
uint32_t ws = view->workspace->num;
view->impl->close(view);
ipc_send_event(
outp->server,
"{\"event_name\":\"close\",\"view_id\":%d,\"view_pid\":%d,\"tile_id\":"
"%d,\"workspace\":%d,\"output\":\"%s\",\"output_id\":%d}",
view_id, view_pid, tile_id, ws + 1, outp->wlr_output->name,
output_get_num(outp));
}
static void
keybinding_split_vertical(struct cg_server *server) {
keybinding_split_output(server->curr_output, true);
}
static void
keybinding_split_horizontal(struct cg_server *server) {
keybinding_split_output(server->curr_output, false);
}
static void
into_process(const char *command) {
execl("/bin/sh", "sh", "-c", command, (char *)NULL);
_exit(1);
}
void
set_output(struct cg_server *server, struct cg_output *output) {
server->curr_output = output;
seat_set_focus(
server->seat,
server->curr_output->workspaces[server->curr_output->curr_workspace]
->focused_tile->view);
message_printf(server->curr_output, "Current Output");
}
void
keybinding_cycle_outputs(struct cg_server *server, bool reverse,
bool trigger_event) {
struct cg_output *output = NULL;
struct cg_output *old_output = server->curr_output;
if(reverse) {
output = wl_container_of(server->curr_output->link.prev,
server->curr_output, link);
} else {
output = wl_container_of(server->curr_output->link.next,
server->curr_output, link);
}
if(&output->link == &server->outputs) {
if(reverse) {
output = wl_container_of(output->link.prev, output, link);
} else {
output = wl_container_of(output->link.next, output, link);
}
}
set_output(server, output);
if(trigger_event) {
ipc_send_event(
output->server,
"{\"event_name\":\"cycle_outputs\",\"old_output\":\"%s\",\"old_"
"output_id\":%d,"
"\"new_output\":\"%s\",\"new_output_id\":%d,\"reverse\":%d}",
old_output->wlr_output->name, output_get_num(old_output),
output->wlr_output->name, output_get_num(output), reverse);
}
}
/* Cycle through views, whereby the workspace does not change */
void
keybinding_cycle_views(struct cg_server *server, bool reverse, bool ipc) {
struct cg_workspace *curr_workspace =
server->curr_output->workspaces[server->curr_output->curr_workspace];
struct cg_view *current_view = curr_workspace->focused_tile->view;
struct cg_view *it_view, *next_view = NULL;
if(reverse) {
wl_list_for_each(it_view, &curr_workspace->views, link) {
if(!view_is_visible(it_view)) {
next_view = it_view;
break;
}
}
} else {
wl_list_for_each_reverse(it_view, &curr_workspace->views, link) {
if(!view_is_visible(it_view)) {
next_view = it_view;
break;
}
}
}
if(next_view == NULL) {
return;
}
seat_set_focus(server->seat, next_view);
if(ipc) {
ipc_send_event(
curr_workspace->output->server,
"{\"event_name\":\"cycle_views\",\"old_view_id\":%d,\"old_view_"
"pid\":%d,"
"\"new_view_id\":%d,\"new_view_pid\":%d,\"tile_id\":%d,"
"\"workspace\":%d,\"output\":\"%s\",\"output_id\":%d}",
current_view->link.next == curr_workspace->views.next
? -1
: (int)current_view->id,
current_view->link.next == curr_workspace->views.next
? -1
: (int)current_view->impl->get_pid(current_view),
next_view == NULL ? -1 : (int)next_view->id,
next_view == NULL ? -1 : (int)next_view->impl->get_pid(next_view),
next_view->tile->id, curr_workspace->num + 1,
curr_workspace->output->wlr_output->name,
output_get_num(curr_workspace->output));
}
}
void
keybinding_focus_tile(struct cg_server *server, uint32_t tile_id) {
struct cg_output *output = server->curr_output;
struct cg_workspace *workspace = output->workspaces[output->curr_workspace];
struct cg_tile *old_tile = workspace->focused_tile;
bool first = true;
for(struct cg_tile *tile = workspace->focused_tile;
first || tile != workspace->focused_tile; tile = tile->next) {
first = false;
if(tile->id == tile_id) {
workspace_focus_tile(workspace, tile);
struct cg_view *next_view = workspace->focused_tile->view;
seat_set_focus(server->seat, next_view);
ipc_send_event(
output->server,
"{\"event_name\":\"focus_tile\",\"old_tile_id\":%d,\"new_tile_"
"id\":%d,\"workspace\":%d,\"output\":\"%s\",\"output_id\":%d}",
old_tile->id, workspace->focused_tile->id, workspace->num + 1,
output->wlr_output->name, output_get_num(output));
break;
}
}
}
void
keybinding_cycle_tiles(struct cg_server *server, bool reverse) {
struct cg_output *output = server->curr_output;
struct cg_workspace *workspace = output->workspaces[output->curr_workspace];
if(reverse) {
keybinding_focus_tile(server, workspace->focused_tile->prev->id);
} else {
keybinding_focus_tile(server, workspace->focused_tile->next->id);
}
}
int
keybinding_switch_ws(struct cg_server *server, uint32_t ws) {
if(ws >= server->nws) {
wlr_log(WLR_ERROR,
"Requested workspace %u, but only have %u workspaces.", ws + 1,
server->nws);
return -1;
}
struct cg_output *output = server->curr_output;
uint32_t old_ws = server->curr_output->curr_workspace;
workspace_focus(output, ws);
seat_set_focus(server->seat,
server->curr_output->workspaces[ws]->focused_tile->view);
message_printf(server->curr_output, "Workspace %d", ws + 1);
ipc_send_event(output->server,
"{\"event_name\":\"switch_ws\",\"old_workspace\":%d,"
"\"new_workspace\":%d,\"output\":\"%s\",\"output_id\":%d}",
old_ws + 1, ws + 1, output->wlr_output->name,
output_get_num(output));
return 0;
}
void
keybinding_show_time(struct cg_server *server) {
char *msg, *tmp;
time_t timep;
timep = time(NULL);
tmp = ctime(&timep);
msg = strdup(tmp);
msg[strcspn(msg, "\n")] = '\0'; /* Remove the newline */
message_printf(server->curr_output, "%s", msg);
free(msg);
}
struct dyn_str {
uint32_t len;
uint32_t cur_pos;
char **str_arr;
};
/*Note that inp is in an invalid state after calling the function * and should
* no longer be used.*/
char *
dyn_str_to_str(struct dyn_str *inp) {
char *outp = calloc(inp->len + 1, sizeof(char));
if(outp == NULL) {
return NULL;
}
outp[inp->len] = '\0';
uint32_t tmp_pos = 0;
for(uint32_t i = 0; i < inp->cur_pos; ++i) {
strcat(outp + tmp_pos, inp->str_arr[i]);
tmp_pos += strlen(inp->str_arr[i]);
free(inp->str_arr[i]);
}
free(inp->str_arr);
return outp;
}
int
print_str(struct dyn_str *outp, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
char *ret = malloc_vsprintf_va_list(fmt, args);
if(ret == NULL) {
return -1;
}
outp->str_arr[outp->cur_pos] = ret;
++outp->cur_pos;
outp->len += strlen(ret);
return 0;
}
void
print_modes(struct dyn_str *str, char **modes) {
uint32_t len = 0;
char **tmp = modes;
uint32_t nmemb = 0;
while(*tmp != NULL) {
len += strlen(*tmp);
++nmemb;
++tmp;
}
if(nmemb == 0) {
wlr_log(WLR_ERROR,
"This is a bug: Cagebreak has no valid modes. This should not "
"occur, since default modes are defined on startup.");
return;
}
/* We are assuming here that we have at least one mode, which is given by
* the initialization of cagebreak */
char *modes_str = calloc(len + 3 * (nmemb - 1) + 1, sizeof(char));
modes_str[len + nmemb - 1] = '\0';
uint32_t tmp_pos = 0;
for(uint32_t i = 0; i < nmemb; ++i) {
if(i != 0) {
strcat(modes_str + tmp_pos, "\",\"");
tmp_pos += 3;
}
strcat(modes_str + tmp_pos, modes[i]);
tmp_pos += strlen(modes[i]);
}
print_str(str, "\"modes\":[\"%s\"],\n", modes_str);
free(modes_str);
}
char *
print_view(struct cg_view *view) {
struct dyn_str outp_str;
outp_str.len = 0;
outp_str.cur_pos = 0;
uint32_t nmemb = 4;
outp_str.str_arr = calloc(nmemb, sizeof(char *));
print_str(&outp_str, "\"id\": %d,\n", view->id);
print_str(&outp_str, "\"pid\": %d,\n", view->impl->get_pid(view));
print_str(&outp_str, "\"coords\": {\"x\":%d,\"y\":%d},\n", view->ox,
view->oy);
#if CG_HAS_XWAYLAND
print_str(&outp_str, "\"type\": \"%s\"\n",
view->type == CG_XWAYLAND_VIEW ? "xwayland" : "xdg");
#else
print_str(&outp_str, "\"type\": \"xdg\"\n");
#endif
return dyn_str_to_str(&outp_str);
}
char *
print_tile(struct cg_tile *tile) {
struct dyn_str outp_str;
outp_str.len = 0;
outp_str.cur_pos = 0;
uint32_t nmemb = 4;
outp_str.str_arr = calloc(nmemb, sizeof(char *));
print_str(&outp_str, "\"id\": %d,\n", tile->id);
print_str(&outp_str, "\"coords\": {\"x\":%d,\"y\":%d},\n", tile->tile.x,
tile->tile.y);
print_str(&outp_str, "\"size\": {\"width\":%d,\"height\":%d},\n",
tile->tile.width, tile->tile.height);
print_str(&outp_str, "\"view_id\": %d\n",
tile->view == NULL ? -1 : (int)tile->view->id);
return dyn_str_to_str(&outp_str);
}
char *
print_views(struct cg_workspace *ws) {
struct dyn_str outp_str;
outp_str.len = 0;
outp_str.cur_pos = 0;
uint32_t nviews = wl_list_length(&ws->views);
if(nviews == 0) {
return strdup("{}");
}
outp_str.str_arr = calloc(2 * nviews - 1, sizeof(char *));
struct cg_view *it;
uint32_t count = 0;
wl_list_for_each(it, &ws->views, link) {
if(count != 0) {
print_str(&outp_str, ",");
}
++count;
char *view_str = print_view(it);
if(view_str != NULL) {
print_str(&outp_str, "{\n%s\n}", view_str);
free(view_str);
}
}
return dyn_str_to_str(&outp_str);
}
char *
print_tiles(struct cg_workspace *ws) {
struct dyn_str outp_str;
outp_str.len = 0;
outp_str.cur_pos = 0;
uint32_t ntiles = 0;
bool first = true;
for(struct cg_tile *tile = ws->focused_tile;
first || tile != ws->focused_tile; tile = tile->next) {
first = false;
++ntiles;
}
if(ntiles == 0) {
return strdup("{}");
}
outp_str.str_arr = calloc(2 * ntiles - 1, sizeof(char *));
first = true;
for(struct cg_tile *tile = ws->focused_tile;
first || tile != ws->focused_tile; tile = tile->next) {
if(first == false) {
print_str(&outp_str, ",");
}
first = false;
char *tile_str = print_tile(tile);
if(tile_str != NULL) {
print_str(&outp_str, "{\n%s\n}", tile_str);
free(tile_str);
}
}
return dyn_str_to_str(&outp_str);
}
char *
print_workspace(struct cg_workspace *ws) {
struct dyn_str outp_str;
outp_str.len = 0;
outp_str.cur_pos = 0;
uint32_t nmemb = 6;
outp_str.str_arr = calloc(nmemb, sizeof(char *));
print_str(&outp_str, "\"views\": [");
char *views_str = print_views(ws);
if(views_str != NULL) {
print_str(&outp_str, "%s", views_str);
free(views_str);
}
print_str(&outp_str, "],");
print_str(&outp_str, "\"tiles\": [");
char *tiles_str = print_tiles(ws);
if(tiles_str != NULL) {
print_str(&outp_str, "%s", tiles_str);
free(tiles_str);
}
print_str(&outp_str, "]");
return dyn_str_to_str(&outp_str);
}
char *
print_workspaces(struct cg_output *outp) {
struct dyn_str outp_str;
outp_str.len = 0;
outp_str.cur_pos = 0;
outp_str.str_arr = calloc((2 * outp->server->nws - 1) + 2, sizeof(char *));
print_str(&outp_str, "\"workspaces\": [");
for(int i = 0; i < outp->server->nws; ++i) {
if(i != 0) {
print_str(&outp_str, ",");
}
char *ws = print_workspace(outp->workspaces[i]);
if(ws != NULL) {
print_str(&outp_str, "{%s}", ws);
free(ws);
}
}