-
Notifications
You must be signed in to change notification settings - Fork 0
/
hellwm.c
3453 lines (2844 loc) · 122 KB
/
hellwm.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
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <assert.h>
#include <getopt.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-keysyms.h>
#include <wayland-util.h>
#include <wayland-server-core.h>
#include <wlr/backend.h>
#include <wlr/util/box.h>
#include <wlr/util/log.h>
#include <wlr/types/wlr_drm.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/backend/session.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/render/allocator.h>
#include <wlr/backend/libinput.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_pointer.h>
#include <wlr/types/wlr_keyboard.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_viewporter.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_input_device.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_subcompositor.h>
#include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/types/wlr_server_decoration.h>
#include <wlr/types/wlr_screencopy_v1.h>
#include <wlr/types/wlr_xdg_output_v1.h>
#include <wlr/types/wlr_layer_shell_v1.h>
#include <wlr/types/wlr_data_control_v1.h>
#include <wlr/types/wlr_export_dmabuf_v1.h>
#include <wlr/types/wlr_xdg_decoration_v1.h>
#include <wlr/types/wlr_alpha_modifier_v1.h>
#include <wlr/types/wlr_presentation_time.h>
#include <wlr/types/wlr_fractional_scale_v1.h>
#include <wlr/types/wlr_linux_drm_syncobj_v1.h>
#include <wlr/types/wlr_primary_selection_v1.h>
#include <wlr/types/wlr_single_pixel_buffer_v1.h>
#include "wlr-layer-shell-unstable-v1-protocol.h"
/* structures */
enum hellwm_cursor_mode
{
HELLWM_CURSOR_MOVE,
HELLWM_CURSOR_RESIZE,
HELLWM_CURSOR_PASSTHROUGH,
};
enum hellwm_keybind_type
{
HELLWM_KEYBIND_COMMAND,
HELLWM_KEYBIND_FUNCTION,
HELLWM_KEYBIND_WORKSPACE,
};
enum HELLWM_LAYER
{
HELLWM_BACKGROUND,
HELLWM_TOP,
HELLWM_BOTTOM,
HELLWM_OVERLAY,
HELLWM_LAYER_COUNT,
};
enum HELLWM_VIEW
{
HELLWM_VIEW_TOPLEVEL,
HELLWM_VIEW_POPUP,
HELLWM_VIEW_LAYER
};
struct hellwm_server
{
char **exec_args;
size_t exec_args_count;
uint32_t resize_edges;
int layout_reapply;
unsigned cursor_mode;
unsigned mapped_functions;
double cursor_grab_x, cursor_grab_y;
/* hellwm */
struct hellwm_output *active_output;
struct hellwm_toplevel *grabbed_toplevel;
struct hellwm_workspace *active_workspace;
struct hellwm_config_manager *config_manager;
struct hellwm_function_map_entry *hellwm_function_map;
/* wayland */
struct wl_list outputs;
struct wl_list keyboards;
struct wl_list workspaces;
struct wl_display *wl_display;
struct wl_event_loop *event_loop;
/* wlroots */
struct wlr_seat *seat;
struct wlr_scene *scene;
struct wlr_cursor *cursor;
struct wlr_backend *backend;
struct wlr_box grab_geometry;
struct wlr_session * session;
struct wlr_renderer *renderer;
struct wlr_allocator *allocator;
struct wlr_xdg_shell *xdg_shell;
struct wlr_compositor *compositor;
struct wlr_xcursor_manager *cursor_mgr;
struct wlr_output_layout *output_layout;
struct wlr_subcompositor *subcompositor;
struct wlr_scene_output_layout *scene_layout;
struct wlr_server_decoration_manager *wlr_server_decoration_manager;
struct hellwm_layer_surface *layer_exclusive_keyboard;
struct wlr_scene_tree *layers[4]; /*
0 -> background
1 -> bottom
2 -> top
3 -> overlay */
struct wlr_layer_shell_v1 *layer_shell;
struct wlr_xdg_decoration_manager_v1 *xdg_decoration_manager;
/* listeners */
struct wl_listener renderer_lost;
struct wl_listener cursor_axis;
struct wl_listener cursor_frame;
struct wl_listener cursor_button;
struct wl_listener cursor_motion;
struct wl_listener cursor_motion_absolute;
struct wl_listener cursor_motion_relative;
struct wl_listener backend_new_input;
struct wl_listener backend_new_output;
struct wl_listener request_cursor;
struct wl_listener request_set_selection;
struct wl_listener new_xdg_popup;
struct wl_listener new_xdg_toplevel;
struct wl_listener new_layer_surface;
struct wl_listener xdg_decoration_new_toplevel_decoration;
};
struct hellwm_output
{
struct wl_list link;
struct wlr_box usable_area;
struct wlr_box total_area;
struct wlr_output *wlr_output;
struct hellwm_server *server;
struct hellwm_workspace *active_workspace;
struct {
struct wl_list background;
struct wl_list bottom;
struct wl_list top;
struct wl_list overlay;
} layers;
/* listeners */
struct wl_listener frame;
struct wl_listener request_state;
struct wl_listener destroy;
};
struct hellwm_workspace
{
int id;
int layout;
struct wl_list link;
struct wl_list toplevels;
struct hellwm_server *server;
struct hellwm_output *output;
struct hellwm_toplevel *last_focused;
struct hellwm_toplevel *prev_focused;
};
struct hellwm_toplevel
{
bool floating;
struct wl_list link;
struct hellwm_server *server;
struct hellwm_workspace *workspace;
struct wlr_box prev_geom;
struct wlr_scene_tree *scene_tree;
struct wlr_xdg_toplevel *xdg_toplevel;
/* listeners */
struct wl_listener map;
struct wl_listener unmap;
struct wl_listener commit;
struct wl_listener destroy;
struct wl_listener set_title;
struct wl_listener set_app_id;
struct wl_listener request_move;
struct wl_listener request_resize;
struct wl_listener request_maximize;
struct wl_listener request_fullscreen;
};
struct hellwm_layer_surface
{
struct wl_list link;
struct hellwm_server *server;
struct wlr_scene_layer_surface_v1 *scene;
struct wlr_layer_surface_v1 *wlr_layer_surface;
struct wl_listener map;
struct wl_listener unmap;
struct wl_listener commit;
struct wl_listener destroy;
struct wl_listener new_popup;
};
struct hellwm_view
{
enum HELLWM_VIEW view_type;
union {
struct hellwm_toplevel *toplevel;
struct hellwm_popup *popup;
struct hellwm_layer_surface *layer_surface;
};
};
struct hellwm_popup
{
struct hellwm_server *server;
struct wlr_xdg_popup *xdg_popup;
struct wlr_scene_tree *scene_tree;
/* listeners */
struct wl_listener commit;
struct wl_listener destroy;
};
struct hellwm_keyboard
{
struct wl_list link;
struct hellwm_server *server;
struct wlr_keyboard *wlr_keyboard;
/* listeners */
struct wl_listener key;
struct wl_listener destroy;
struct wl_listener modifiers;
};
struct hellwm_config_keybind_workspace
{
int workspace_id;
int binary_workspace_val;
bool pressed;
bool move_with_active;
bool binary_workspaces_enabled;
};
union hellwm_function
{
char *as_void; /* If keybind is just executing some command */
void (*as_func)(struct hellwm_server*); /* If keybind is running mapped function */
struct hellwm_config_keybind_workspace workspace; /* If keybind is meant for changing workspaces */
};
struct hellwm_function_map_entry
{
const char* name;
union hellwm_function func;
};
/* keybinding */
typedef struct
{
int count;
xkb_keysym_t *keysyms;
enum hellwm_keybind_type type;
union hellwm_function *content;
} hellwm_config_keybind;
/* keybindings config */
typedef struct
{
unsigned count;
hellwm_config_keybind **keybindings;
} hellwm_config_manager_keybindings;
typedef struct
{
char * name;
char* rules;
char* model;
char* layout;
char* variant;
char* options;
int32_t delay;
int32_t rate;
} hellwm_config_keyboard;
typedef struct
{
int count;
hellwm_config_keyboard **keyboards;
hellwm_config_keyboard *default_keyboard;
} hellwm_config_manager_keyboard;
typedef struct
{
char *name;
bool vrr;
bool enabled;
float scale;
int32_t hz;
int32_t width;
int32_t height;
int32_t transfrom;
} hellwm_config_monitor;
typedef struct
{
int count;
hellwm_config_monitor **monitors;
} hellwm_config_manager_monitor;
typedef struct
{
float master_ratio;
uint32_t inner_gap;
uint32_t outer_gap;
uint32_t border_size;
float border_color[4];
} hellwm_config_manager_decoration;
typedef struct
{
bool tap_click;
bool natural_scroll;
} hellwm_config_manager_input;
struct hellwm_binary_workspaces_manager
{
int workspace_sum;
bool binary_started;
int keybinds_count;
hellwm_config_keybind **keybinds;
};
struct hellwm_config_manager
{
/* Todo : config manger for this */
unsigned tiling_layout;
/* appearance */
hellwm_config_manager_decoration *decoration;
/* functionality */
hellwm_config_manager_input *input;
hellwm_config_manager_keybindings *keybindings;
hellwm_config_manager_monitor *monitor_manager;
hellwm_config_manager_keyboard *keyboard_manager;
struct hellwm_binary_workspaces_manager *binary_workspaces_manager;
};
/* functions declarations */
void ERR(const char *format, ...);
void RUN_EXEC(char *commnad);
void LOG(const char *format, ...);
void check_usage(int argc, char**argv);
void hellwm_lua_error (lua_State *L, const char *fmt, ...);
struct hellwm_config_manager *hellwm_config_manager_create();
int hellwm_lua_add_keyboard(lua_State *L);
int hellwm_lua_add_monitor(lua_State *L);
int hellwm_lua_add_keybind(lua_State *L);
void hellwm_config_print(struct hellwm_config_manager *config);
bool hellwm_function_find(const char* name, struct hellwm_server* server, union hellwm_function *func);
bool hellwm_convert_string_to_xkb_keys(xkb_keysym_t **keysyms_arr, const char *keys_str, int *num_keys);
bool hellwm_config_manager_keyboard_find_and_apply(hellwm_config_manager_keyboard *keyboard_manager, struct hellwm_keyboard *keyboard);
void hellwm_function_expose(struct hellwm_server *server);
void hellwm_config_manager_load_from_file(char * filename);
void hellwm_config_manager_monitor_find_and_apply(char *name, struct wlr_output_state *state, hellwm_config_manager_monitor *monitor_manager);
/* returns number of active workspaces + 1 */
static int hellwm_workspace_get_next_id(struct hellwm_server *server);
/* destroy workspace */
static void hellwm_workspace_destroy(struct hellwm_workspace *workspace);
/* changes workspace to provided id, if not exist create from function hellwm_workspace_create() */
static void hellwm_workspace_change(struct hellwm_server *server, int workspace);
/* creates workspace from provided id */
static void hellwm_workspace_create(struct hellwm_server *server, int workspace_id);
struct hellwm_workspace *hellwm_workspace_create_begin(struct hellwm_server *server, int workspace_id);
static void hellwm_workspace_create_for_output(struct hellwm_server *server, int workspace_id, struct hellwm_output *output);
/* find workspace by id, if function cannot find workspace returns NULL */
static struct hellwm_workspace *hellwm_workspace_find(struct hellwm_server *server, int id);
static void hellwm_binary_workspaces_manager_add_keybind(struct hellwm_binary_workspaces_manager *manager, hellwm_config_keybind *keybind);
/* add toplevel to specific workspace */
static void hellwm_workspace_add_toplevel(struct hellwm_workspace *workspace, struct hellwm_toplevel *toplevel);
/* move toplevel from workspace to new active workspace */
void hellwm_toplevel_move_to_workspace(struct hellwm_server *server, int workspace_id);
void hellwm_config_manager_free(struct hellwm_config_manager *config);
void hellwm_config_manager_monitor_free(hellwm_config_manager_monitor *monitor);
void hellwm_config_manager_keyboard_free(hellwm_config_manager_keyboard *keyboard);
void hellwm_config_keybindings_free(hellwm_config_manager_keybindings * keybindings);
void hellwm_config_manager_binary_workspaces_free(struct hellwm_binary_workspaces_manager *binary_workspaces);
hellwm_config_manager_input *hellwm_config_manager_input_create();
hellwm_config_manager_monitor *hellwm_config_manager_monitor_create();
hellwm_config_manager_keyboard *hellwm_config_manager_keyboard_create();
hellwm_config_manager_decoration *hellwm_config_manager_decoration_create();
hellwm_config_manager_keybindings *hellwm_config_manager_keybindings_create();
struct hellwm_binary_workspaces_manager *hellwm_config_binary_workspace_manager_create();
void hellwm_config_manager_reload(struct hellwm_server *server);
void hellwm_config_manager_monitor_reload(struct hellwm_server *server);
void hellwm_config_manager_keyboard_reload(struct hellwm_server *server);
void hellwm_config_manager_keyboard_set(struct hellwm_keyboard *keyboard, hellwm_config_keyboard *config);
void hellwm_config_manager_monitor_set(hellwm_config_manager_monitor *config, struct hellwm_output *output);
void hellwm_config_keybind_add_allocate(hellwm_config_manager_keybindings *config_keybindings);
void hellwm_config_keybind_add_function_or_cmd_to_config(struct hellwm_server *server, char *keys, void *content);
void hellwm_config_keybind_add_workspace_to_config(struct hellwm_server *server, char *keys, struct hellwm_config_keybind_workspace workspace);
void hellwm_function_add_to_map(struct hellwm_server *server, const char* name, void (*func)(struct hellwm_server*));
void hellwm_config_manager_monitor_add(hellwm_config_manager_monitor *monitor_manager, hellwm_config_monitor *monitor);
void hellwm_config_manager_keyboard_add(hellwm_config_manager_keyboard *keyboard_manager, hellwm_config_keyboard *keyboard);
static uint32_t hellwm_xkb_keysym_to_wlr_modifier(xkb_keysym_t sym);
static bool handle_keybinding_binary_workspaces(struct hellwm_server *server, xkb_keysym_t sym);
static bool handle_keybinding(struct hellwm_server *server, uint32_t modifiers, xkb_keysym_t sym);
struct hellwm_view *root_parent_surface(struct wlr_surface *wlr_surface);
struct hellwm_view *desktop_view_at(struct hellwm_server *server, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy);
static struct hellwm_toplevel *desktop_toplevel_at(struct hellwm_server *server, double lx, double ly, struct wlr_surface **surface, double *sx, double *sy);
static void hellwm_server_kill(struct hellwm_server *server);
static void hellwm_toplevel_kill_active(struct hellwm_server *server);
static void hellwm_focus_toplevel(struct hellwm_toplevel *toplevel);
static void hellwm_focus_next_toplevel(struct hellwm_server *server);
static void hellwm_focus_prev_toplevel(struct hellwm_server *server);
static void hellwm_focus_prev_toplevel_center(struct hellwm_server *server);
static void hellwm_focus_next_toplevel_center(struct hellwm_server *server);
static void keyboard_handle_key(struct wl_listener *listener, void *data);
static void keyboard_handle_destroy(struct wl_listener *listener, void *data);
static void keyboard_handle_modifiers(struct wl_listener *listener, void *data);
static void server_new_pointer(struct hellwm_server *server, struct wlr_input_device *device);
static void server_new_keyboard(struct hellwm_server *server, struct wlr_input_device *device);
static void handle_renderer_lost(struct wl_listener *listener, void *data);
static void server_backend_new_input(struct wl_listener *listener, void *data);
static void server_backend_new_output(struct wl_listener *listener, void *data);
static void seat_request_cursor(struct wl_listener *listener, void *data);
static void seat_request_set_selection(struct wl_listener *listener, void *data);
static void reset_cursor_mode(struct hellwm_server *server);
static void server_cursor_axis(struct wl_listener *listener, void *data) ;
static void server_cursor_button(struct wl_listener *listener, void *data);
static void server_cursor_frame(struct wl_listener *listener, void *data) ;
static void server_cursor_motion(struct wl_listener *listener, void *data);
static void process_cursor_move(struct hellwm_server *server, uint32_t time);
static void process_cursor_resize(struct hellwm_server *server, uint32_t time);
static void process_cursor_motion(struct hellwm_server *server, uint32_t time);
static void server_cursor_motion_absolute(struct wl_listener *listener, void *data);
static void begin_interactive(struct hellwm_toplevel *toplevel, enum hellwm_cursor_mode mode, uint32_t edges);
static void output_frame(struct wl_listener *listener, void *data);
static void output_destroy(struct wl_listener *listener, void *data);
static void output_request_state(struct wl_listener *listener, void *data);
static void focus_layer_surface(struct hellwm_layer_surface *layer_surface);
static void handle_new_layer_surface(struct wl_listener *listener, void *data);
static void layer_surface_handle_map(struct wl_listener *listener, void *data);
static void layer_surface_map_func(struct hellwm_layer_surface *layer_surface);
static void layer_surface_handle_unmap(struct wl_listener *listener, void *data);
static void layer_surface_unmap_func(struct hellwm_layer_surface *layer_surface);
static void layer_surface_handle_commit(struct wl_listener *listener, void *data);
static void layer_surface_handle_destroy(struct wl_listener *listener, void *data);
static void xdg_toplevel_map(struct wl_listener *listener, void *data);
static void xdg_toplevel_unmap(struct wl_listener *listener, void *data);
static void xdg_toplevel_commit(struct wl_listener *listener, void *data);
static void xdg_toplevel_destroy(struct wl_listener *listener, void *data);
static void xdg_toplevel_request_move(struct wl_listener *listener, void *data);
static void xdg_toplevel_request_resize(struct wl_listener *listener, void *data);
static void xdg_toplevel_request_maximize(struct wl_listener *listener, void *data);
static void xdg_toplevel_request_fullscreen(struct wl_listener *listener, void *data);
static void xdg_decoration_new_toplevel_decoration(struct wl_listener *listener, void *data);
static void xdg_popup_commit(struct wl_listener *listener, void *data);
static void xdg_popup_destroy(struct wl_listener *listener, void *data);
static void server_new_xdg_popup(struct wl_listener *listener, void *data);
static void server_new_xdg_toplevel(struct wl_listener *listener, void *data);
/* Global Variables */
struct hellwm_server *GLOBAL_SERVER = NULL;
/*
* WARNING: TEMPORARY LAYOUTS AND REALLY BAD TILING - TODO: REMOVE THIS ekhm.. BEAUTIFUL CODE
*/
void layout_horizontal_split(struct hellwm_workspace *workspace)
{
int i = 0;
int count = wl_list_length(&workspace->toplevels);
if (count == 0) return;
int gaps = workspace->server->config_manager->decoration->outer_gap;
struct hellwm_output *output = workspace->output;
int window_width = output->wlr_output->width / count;
int window_height = output->wlr_output->height;
struct hellwm_toplevel *toplevel;
wl_list_for_each(toplevel, &workspace->toplevels, link)
{
int x = i * window_width;
int y = 0;
wlr_scene_node_set_position(&toplevel->scene_tree->node, x + gaps, y + output->usable_area.y + gaps);
wlr_xdg_toplevel_set_size(toplevel->xdg_toplevel, window_width - gaps*2, window_height - output->usable_area.y - gaps*2);
i++;
}
}
void apply_layout(struct hellwm_workspace *workspace, int layout_type)
{
if (workspace == NULL)
return;
layout_type = workspace->output->server->config_manager->tiling_layout;
switch (layout_type)
{
case 1:
layout_horizontal_split(workspace);
break;
default:
layout_horizontal_split(workspace);
break;
}
}
/* functions implementations */
void LOG(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stdout, format, ap);
va_end(ap);
FILE *file = fopen("./logfile.log", "a");
if (file != NULL)
{
va_start(ap, format);
vfprintf(file, format, ap);
va_end(ap);
fclose(file);
}
}
void ERR(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stdout, format, ap);
va_end(ap);
FILE *file = fopen("./logfile.log", "a");
if (file != NULL)
{
va_start(ap, format);
vfprintf(file, format, ap);
va_end(ap);
fclose(file);
}
exit(EXIT_FAILURE);
}
void hellwm_lua_error (lua_State *L, const char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
lua_close(L);
exit(EXIT_FAILURE);
}
void RUN_EXEC(char *commnad)
{
if (fork() == 0)
{
execl("/bin/sh", "/bin/sh", "-c", commnad, (void *)NULL);
}
}
void remove_spaces(char *str)
{
char *dest = str;
while (*str)
{
if (*str != ' ')
{
*dest++ = *str;
}
str++;
}
*dest = '\0';
}
void check_usage(int argc, char**argv)
{
int c;
while ((c = getopt(argc, argv, "s:h")) != -1)
{
switch (c)
{
case 's':
//startup_cmd = optarg;
break;
default:
printf("Usage: %s [-s startup command]\n", argv[0]);
exit(0);
}
}
if (optind < argc)
{
printf("Usage: %s [-s startup command]\n", argv[0]);
exit(0);
}
}
hellwm_config_manager_monitor *hellwm_config_manager_monitor_create()
{
hellwm_config_manager_monitor *monitor_manager = calloc(1, sizeof(hellwm_config_manager_monitor));
if (!monitor_manager)
{
LOG("hellwm_config_manager_monitor_create()%s:%d: failed to allocate memory for hellwm_config_manager_monitor\n",__func__, __LINE__);
return NULL;
}
monitor_manager->count = 0;
monitor_manager->monitors = NULL;
return monitor_manager;
}
hellwm_config_manager_keyboard *hellwm_config_manager_keyboard_create()
{
hellwm_config_manager_keyboard *keyboard_manager = calloc(1, sizeof(hellwm_config_manager_keyboard));
if (!keyboard_manager)
{
LOG("%s:%d failed to allocate memory for hellwm_config_manager_keyboard\n",__func__, __LINE__);
return NULL;
}
keyboard_manager->count = 0;
keyboard_manager->keyboards = NULL;
keyboard_manager->default_keyboard = calloc(1, sizeof(hellwm_config_keyboard));
keyboard_manager->default_keyboard->name = "default";
keyboard_manager->default_keyboard->layout = "us";
keyboard_manager->default_keyboard->rate = 25;
keyboard_manager->default_keyboard->delay = 600;
keyboard_manager->default_keyboard->rules = NULL;
keyboard_manager->default_keyboard->model = NULL;
keyboard_manager->default_keyboard->variant = NULL;
keyboard_manager->default_keyboard->options = NULL;
return keyboard_manager;
}
hellwm_config_manager_keybindings *hellwm_config_manager_keybindings_create()
{
hellwm_config_manager_keybindings *keybindings = calloc(1, sizeof(hellwm_config_manager_keybindings));
keybindings->count = 0;
return keybindings;
}
/* TODO: load from config */
hellwm_config_manager_input *hellwm_config_manager_input_create()
{
hellwm_config_manager_input *input = calloc(1, sizeof(hellwm_config_manager_input));
input->tap_click = true;
/* inverted scroll */
input->natural_scroll = false;
return input;
}
/* TODO: load from config */
hellwm_config_manager_decoration *hellwm_config_manager_decoration_create()
{
hellwm_config_manager_decoration *decoration = calloc(1, sizeof(hellwm_config_manager_decoration));
/* tiling */
decoration->master_ratio = 0.7;
/* gaps */
decoration->inner_gap = 10;
decoration->outer_gap = 10;
/* border */
decoration->border_size = 3;
for (size_t i = 0; i < 4; i++) /* white color */
decoration->border_color[0] = 1.f;
return decoration;
}
struct hellwm_binary_workspaces_manager *hellwm_config_binary_workspace_manager_create()
{
/* binary workspaces manager */
struct hellwm_binary_workspaces_manager *binary_workspaces = calloc(1, sizeof(struct hellwm_binary_workspaces_manager));
binary_workspaces->binary_started = false;
binary_workspaces->keybinds_count = 0;
binary_workspaces->workspace_sum = 0;
binary_workspaces->keybinds = NULL;
return binary_workspaces;
}
struct hellwm_config_manager *hellwm_config_manager_create()
{
struct hellwm_config_manager *config_manager = calloc(1, sizeof(struct hellwm_config_manager));
/* remove this and create it as sub config_manager */
config_manager->tiling_layout = 1;
/* decoration */
config_manager->decoration = hellwm_config_manager_decoration_create();
/* input */
config_manager->input = hellwm_config_manager_input_create();
/* keybindings */
config_manager->keybindings = hellwm_config_manager_keybindings_create();
/* keyboard */
config_manager->keyboard_manager = hellwm_config_manager_keyboard_create();
/* monitor */
config_manager->monitor_manager = hellwm_config_manager_monitor_create();
/* binary workspaces */
config_manager->binary_workspaces_manager = hellwm_config_binary_workspace_manager_create();
return config_manager;
}
static void hellwm_focus_toplevel(struct hellwm_toplevel *toplevel)
{
struct wlr_surface *surface = toplevel->xdg_toplevel->base->surface;
if (toplevel == NULL)
return;
struct hellwm_server *server = toplevel->server;
struct wlr_seat *seat = server->seat;
struct wlr_surface *prev_surface = seat->keyboard_state.focused_surface;
if (prev_surface == surface)
{
/* Don't re-focus an already focused surface. */
return;
}
if (prev_surface)
{
/*
* Deactivate the previously focused surface. This lets the client know
* it no longer has focus and the client will repaint accordingly, e.g.
* stop displaying a caret.
*/
struct wlr_xdg_toplevel *prev_toplevel = wlr_xdg_toplevel_try_from_wlr_surface(prev_surface);
if (prev_toplevel != NULL)
{
wlr_xdg_toplevel_set_activated(prev_toplevel, false);
}
}
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat);
/* Move the toplevel to the front */
wlr_scene_node_raise_to_top(&toplevel->scene_tree->node);
wl_list_remove(&toplevel->link);
wl_list_insert(&server->active_workspace->toplevels, &toplevel->link);
server->active_workspace->last_focused = toplevel;
/* Activate the new surface */
wlr_xdg_toplevel_set_activated(toplevel->xdg_toplevel, true);
/*
* Tell the seat to have the keyboard enter this surface. wlroots will keep
* track of this and automatically send key events to the appropriate
* clients without additional work on your part.
*/
if (keyboard != NULL)
{
wlr_seat_keyboard_notify_enter(seat, toplevel->xdg_toplevel->base->surface, keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers);
}
}
static void unfocus_focused(struct hellwm_server *server)
{
if (server == NULL)
return;
struct wlr_seat *seat = server->seat;
struct wlr_surface *focused_surface = seat->keyboard_state.focused_surface;
if (focused_surface == NULL) {
return;
}
struct hellwm_toplevel *toplevel = NULL;
wl_list_for_each(toplevel, &server->active_workspace->toplevels, link)
{
if (toplevel->xdg_toplevel->base->surface == focused_surface)
{
wlr_xdg_toplevel_set_activated(toplevel->xdg_toplevel, false);
wlr_seat_keyboard_clear_focus(seat);
server->active_workspace->last_focused = NULL;
return;
}
}
wlr_seat_keyboard_clear_focus(seat);
}
static void focus_layer_surface(struct hellwm_layer_surface *layer_surface)
{
struct hellwm_server *server = layer_surface->server;
enum zwlr_layer_surface_v1_keyboard_interactivity keyboard_interactive = layer_surface->wlr_layer_surface->current.keyboard_interactive;
switch(keyboard_interactive)
{
case ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE: {
return;
}
case ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE: {
unfocus_focused(server);
server->layer_exclusive_keyboard = layer_surface;
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(server->seat);
if(keyboard != NULL) {
wlr_seat_keyboard_notify_enter(server->seat, layer_surface->wlr_layer_surface->surface, keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers);
}
return;
}
case ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_ON_DEMAND: {
if(server->layer_exclusive_keyboard != NULL) return;
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(server->seat);
if(keyboard != NULL)
{
wlr_seat_keyboard_notify_enter(server->seat, layer_surface->wlr_layer_surface->surface, keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers);
}
return;
}
}
}
static void hellwm_focus_next_toplevel(struct hellwm_server *server)
{
if (wl_list_length(&server->active_workspace->toplevels) < 1)
return;
struct hellwm_toplevel *next_toplevel = wl_container_of(server->active_workspace->toplevels.prev, next_toplevel, link);
if (next_toplevel)
hellwm_focus_toplevel(next_toplevel);
else
{
struct hellwm_toplevel *toplevel;
wl_list_for_each(toplevel, &server->active_workspace->toplevels, link)
{
hellwm_focus_toplevel(next_toplevel);
return;
}
}
}
/*
* TODO: its not working.
*/
static void hellwm_focus_prev_toplevel(struct hellwm_server *server)
{
int count = wl_list_length(&server->active_workspace->toplevels);
if (count < 1)
return;
struct hellwm_toplevel *prev = NULL;
struct hellwm_toplevel *toplevel;
wl_list_for_each(toplevel, &server->active_workspace->toplevels, link)
{
struct hellwm_toplevel *next_toplevel = wl_container_of(server->active_workspace->toplevels.prev, next_toplevel, link);
if (prev != NULL && next_toplevel == toplevel)
{
hellwm_focus_toplevel(prev);
return;
}
prev = toplevel;
}
}
static void hellwm_focus_next_toplevel_center(struct hellwm_server *server)
{
hellwm_focus_next_toplevel(server);
server->layout_reapply = 1;
}
static void hellwm_focus_prev_toplevel_center(struct hellwm_server *server)
{
hellwm_focus_prev_toplevel(server);
server->layout_reapply = 1;
}
static void hellwm_toplevel_kill_active(struct hellwm_server *server)
{
if (!server->active_workspace) return;
if (wl_list_length(&server->active_workspace->toplevels) < 1) return;
struct wlr_xdg_toplevel *toplevel = wlr_xdg_toplevel_try_from_wlr_surface(server->seat->keyboard_state.focused_surface);
if (!toplevel)
return;
wlr_xdg_toplevel_send_close(toplevel);
hellwm_focus_next_toplevel(server);
server->layout_reapply = 1;
}