-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstage.c
2279 lines (1814 loc) · 53.7 KB
/
stage.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 (c) 2022-2024 Ruslan Bukin <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/input-event-codes.h>
#include <wlr/backend.h>
#include <wlr/backend/libinput.h>
#include <wlr/interfaces/wlr_keyboard.h>
#include <wlr/render/allocator.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_data_control_v1.h>
#include <wlr/types/wlr_data_device.h>
#include <wlr/types/wlr_export_dmabuf_v1.h>
#include <wlr/types/wlr_fractional_scale_v1.h>
#include <wlr/types/wlr_gamma_control_v1.h>
#include <wlr/types/wlr_input_device.h>
#include <wlr/types/wlr_keyboard.h>
#include <wlr/types/wlr_layer_shell_v1.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_output_management_v1.h>
#include <wlr/types/wlr_pointer.h>
#include <wlr/types/wlr_presentation_time.h>
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/types/wlr_primary_selection_v1.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_screencopy_v1.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_server_decoration.h>
#include <wlr/types/wlr_session_lock_v1.h>
#include <wlr/types/wlr_subcompositor.h>
#include <wlr/types/wlr_viewporter.h>
#include <wlr/types/wlr_virtual_keyboard_v1.h>
#include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/types/wlr_xdg_activation_v1.h>
#include <wlr/types/wlr_xdg_decoration_v1.h>
#include <wlr/types/wlr_xdg_output_v1.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/types/wlr_text_input_v3.h>
#include <wlr/util/log.h>
#include <xkbcommon/xkbcommon.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>
#define dbg_printf(args...)
static const float color_focused[] = { 0.8, 0.4, 0.1, 0.1 };
static const float color_default[] = { 0.4, 0.4, 0.4, 0.1 };
#define SERVER_SOCK_FILE "/tmp/stage.sock"
enum stage_cursor_mode {
STAGE_CURSOR_PASSTHROUGH,
STAGE_CURSOR_MOVE, /* mod + left mouse button + move */
STAGE_CURSOR_RESIZE, /* mod + right mouse button + move */
STAGE_CURSOR_SCROLL, /* left mouse button + move */
};
struct stage_server {
struct wl_display *wl_disp;
struct wlr_backend *backend;
struct wlr_renderer *renderer;
struct wlr_allocator *allocator;
struct wlr_scene *scene;
struct wlr_presentation *presentation;
struct wl_listener new_output;
struct wl_listener new_xdg_surface;
struct wl_listener new_xdg_popup;
struct wlr_xdg_shell *xdg_shell;
struct wlr_output_layout *output_layout;
struct wl_list outputs;
struct wlr_scene_output_layout *scene_layout;
struct wlr_cursor *cursor;
struct wlr_input_device *device;
struct wlr_xcursor_manager *cursor_mgr;
struct wl_listener cursor_motion;
struct wl_listener cursor_motion_absolute;
struct wl_listener cursor_button;
struct wl_listener cursor_axis;
struct wl_listener cursor_frame;
struct wlr_seat *seat;
struct wl_listener new_input;
struct wl_listener request_cursor;
struct wl_listener request_set_selection;
struct wl_listener request_set_primary_selection;
struct wl_list keyboards;
enum stage_cursor_mode cursor_mode;
struct stage_view *grabbed_view;
double grab_x, grab_y;
struct wlr_box grab_geobox;
uint32_t resize_edges;
double cur_saved_x;
double cur_saved_y;
int current_layout;
int oldws;
struct wlr_layer_shell_v1 *shell;
struct wl_listener new_layer_shell_surface;
struct wlr_input_inhibit_manager *inhibit_manager;
struct wlr_session_lock_manager_v1 *lock;
struct wl_listener new_lock;
struct wlr_idle *idle;
struct wlr_compositor *compositor;
struct wlr_xdg_activation_v1 *activation;
struct wl_listener request_activate;
struct wl_listener layout_change;
struct wlr_output_manager_v1 *output_manager;
struct wl_listener output_manager_apply;
struct wl_listener output_manager_test;
bool locked;
};
struct stage_output {
struct wl_list link;
struct stage_server *server;
struct wlr_output *wlr_output;
struct wl_listener frame;
int curws;
};
enum stage_view_type {
VIEW_XDG,
VIEW_X11,
VIEW_SLOCK,
};
struct stage_view {
struct wl_list link;
struct stage_server *server;
struct wlr_xdg_toplevel *xdg_toplevel;
struct wlr_xdg_surface *xdg_surface;
struct wlr_scene_tree *scene_tree;
struct wl_listener map;
struct wl_listener unmap;
struct wl_listener destroy;
struct wl_listener request_move;
struct wl_listener request_resize;
struct wl_listener set_app_id;
struct wl_listener commit;
int x, y, w, h;
int sx, sy, sw, sh; /* saved */
int maxverted;
int maximized;
struct wlr_scene_rect *rect[4]; /* borders */
bool was_focused;
struct wlr_session_lock_surface_v1 *lock_surface;
enum stage_view_type type;
bool slot_set;
};
#define N_SLOTS 5
#define N_WORKSPACES 16
#ifdef STAGE_DEV
#define STAGE_MODIFIER WLR_MODIFIER_ALT
#else
#define STAGE_MODIFIER WLR_MODIFIER_LOGO
#endif
static void cursor_focus(struct stage_server *server, uint32_t time);
static struct terminal_slot {
int x;
int y;
int w;
int h;
int flags;
#define SLOT_NEW_WINDOW (1 << 0)
} slots[N_SLOTS];
static int nslots = 0;
static struct stage_workspace {
struct wl_list views;
char name;
} workspaces[N_WORKSPACES];
static char terminal[] = "foot";
static char ws[] = "ws";
#define TERMINAL_FONT_WIDTH 15
struct stage_keyboard {
struct wl_list link;
struct stage_server *server;
struct wlr_keyboard *wlr_keyboard;
struct wlr_input_device *device;
struct wl_listener modifiers;
struct wl_listener key;
struct wl_listener destroy;
};
struct stage_popup {
struct wlr_xdg_popup *xdg_popup;
struct wl_listener commit;
struct wl_listener destroy;
};
struct stage_lock {
struct wlr_session_lock_v1 *lock;
struct wl_listener new_surface;
struct wl_listener unlock;
struct wl_listener destroy;
struct stage_server *server;
};
static bool
view_is_slock(struct stage_view *view)
{
switch (view->type) {
case VIEW_SLOCK:
return (true);
default:
break;
}
return (false);
}
static struct stage_output *
output_at(struct stage_server *server, double x, double y)
{
struct wlr_output *o;
o = wlr_output_layout_output_at(server->output_layout, x, y);
if (o)
return (o->data);
return (NULL);
}
static struct stage_output *
cursor_at(struct stage_server *server)
{
struct stage_output *out;
out = output_at(server, server->cursor->x, server->cursor->y);
assert(out != NULL);
return (out);
}
struct stage_layer_surface {
struct wl_listener map;
struct wl_listener unmap;
struct wl_listener surface_commit;
struct wl_listener output_destroy;
struct wl_listener node_destroy;
struct wl_listener new_popup;
struct wlr_scene_layer_surface_v1 *scene;
struct wlr_scene_tree *tree;
struct wlr_layer_surface_v1 *layer_surface;
};
static struct stage_layer_surface *
stage_layer_surface_create(struct wlr_scene_layer_surface_v1 *scene)
{
struct stage_layer_surface *surface;
surface = calloc(1, sizeof(*surface));
surface->tree = scene->tree;
surface->scene = scene;
surface->layer_surface = scene->layer_surface;
surface->layer_surface->data = surface;
return (surface);
}
/*
* Layer surface.
*/
static void
handle_surface_commit(struct wl_listener *listener, void *data)
{
};
static void
handle_map(struct wl_listener *listener, void *data)
{
}
void
new_layer_shell_surface(struct wl_listener *listener, void *data)
{
struct wlr_scene_layer_surface_v1 *scene_surface;
struct wlr_layer_surface_v1 *layer_surface;
enum zwlr_layer_shell_v1_layer layer_type;
struct stage_layer_surface *surface;
struct wlr_scene_tree *output_layer;
struct wlr_output *output;
struct stage_server *server;
struct stage_output *out;
struct wlr_box full_area;
dbg_printf("%s\n", __func__);
layer_surface = data;
printf("%s: new layer surface %p, namespace %s layer %d achor %d "
"size %d %d margin %d %d %d %d\n", __func__, layer_surface,
layer_surface->namespace,
layer_surface->pending.layer,
layer_surface->pending.anchor,
layer_surface->pending.desired_width,
layer_surface->pending.desired_height,
layer_surface->pending.margin.top,
layer_surface->pending.margin.right,
layer_surface->pending.margin.bottom,
layer_surface->pending.margin.left);
server = wl_container_of(listener, server, new_layer_shell_surface);
out = cursor_at(server);
layer_type = layer_surface->pending.layer;
layer_surface->output = out->wlr_output;
wlr_output_effective_resolution(out->wlr_output, &full_area.width,
&full_area.height);
wlr_layer_surface_v1_configure(layer_surface, full_area.width,
full_area.height);
output_layer = &server->scene->tree;
scene_surface = wlr_scene_layer_surface_v1_create(output_layer,
layer_surface);
surface = stage_layer_surface_create(scene_surface);
printf("%s: new surface %p\n", __func__, surface);
wlr_fractional_scale_v1_notify_scale(layer_surface->surface,
layer_surface->output->scale);
wlr_surface_set_preferred_buffer_scale(layer_surface->surface,
ceil(layer_surface->output->scale));
surface->surface_commit.notify = handle_surface_commit;
wl_signal_add(&layer_surface->surface->events.commit,
&surface->surface_commit);
surface->map.notify = handle_map;
wl_signal_add(&layer_surface->surface->events.map, &surface->map);
#if 0
/* TODO */
surface->unmap.notify = handle_unmap;
wl_signal_add(&layer_surface->surface->events.unmap, &surface->unmap);
surface->new_popup.notify = handle_new_popup;
wl_signal_add(&layer_surface->events.new_popup, &surface->new_popup);
surface->output_destroy.notify = handle_output_destroy;
wl_signal_add(&output->events.disable, &surface->output_destroy);
surface->node_destroy.notify = handle_node_destroy;
wl_signal_add(&scene_surface->tree->node.events.destroy,
&surface->node_destroy);
#endif
}
static void
create_borders(struct stage_view *view)
{
int i;
for (i = 0; i < 4; i++)
view->rect[i] = wlr_scene_rect_create(view->scene_tree, 0, 0,
color_default);
}
static void
update_borders(struct stage_view *view)
{
struct wlr_scene_rect *rect;
/* left */
wlr_scene_node_set_position(&view->rect[0]->node, 0, 0);
wlr_scene_rect_set_size(view->rect[0], 1, view->h);
/* top */
wlr_scene_node_set_position(&view->rect[1]->node, 0, 0);
wlr_scene_rect_set_size(view->rect[1], view->w, 1);
/* bottom */
wlr_scene_node_set_position(&view->rect[2]->node, 0, view->h - 1);
wlr_scene_rect_set_size(view->rect[2], view->w, 1);
/* right */
wlr_scene_node_set_position(&view->rect[3]->node, view->w - 1, 0);
wlr_scene_rect_set_size(view->rect[3], 1, view->h);
wlr_scene_node_set_position(&view->scene_tree->node, view->x, view->y);
wlr_xdg_toplevel_set_size(view->xdg_toplevel, view->w, view->h);
}
static void
view_geometry(struct stage_view *view, struct wlr_box *geom)
{
if (view->type == VIEW_SLOCK) {
geom->x = 0;
geom->y = 0;
//geom->width = 100;
//geom->height = 100;
} else
geom = &view->xdg_toplevel->base->geometry;
}
static void
view_set_borders_active(struct stage_view *view, bool active)
{
const float *color;
int i;
if (active)
color = color_focused;
else
color = color_default;
for (i = 0; i < 4; i++)
wlr_scene_rect_set_color(view->rect[i], color);
}
static struct wlr_surface *
view_surface(struct stage_view *view)
{
struct wlr_surface *surface;
surface = NULL;
switch (view->type) {
case VIEW_XDG:
surface = view->xdg_toplevel->base->surface;
break;
case VIEW_SLOCK:
surface = view->lock_surface->surface;
break;
default:
break;
}
return (surface);
}
static void
focus_view(struct stage_view *view, struct wlr_surface *surface)
{
struct wlr_surface *prev_surface;
struct wlr_xdg_surface *previous;
struct wlr_scene_node *scene_node;
struct stage_server *server;
struct wlr_keyboard *kb;
struct wlr_seat *seat;
struct stage_view *prev_view;
if (view == NULL)
return;
server = view->server;
seat = server->seat;
kb = wlr_seat_get_keyboard(seat);
if (view_is_slock(view)) {
wlr_seat_keyboard_notify_enter(seat, view_surface(view),
kb->keycodes, kb->num_keycodes, &kb->modifiers);
return;
}
prev_surface = seat->keyboard_state.focused_surface;
if (prev_surface == surface)
return;
if (prev_surface) {
previous =
wlr_xdg_surface_try_from_wlr_surface(prev_surface);
if (previous) {
assert(previous->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL);
if (previous->toplevel != NULL)
wlr_xdg_toplevel_set_activated(
previous->toplevel, false);
scene_node = previous->data;
prev_view = scene_node->data;
view_set_borders_active(prev_view, false);
}
}
wlr_xdg_toplevel_set_activated(view->xdg_toplevel, true);
wlr_seat_keyboard_notify_enter(seat, view_surface(view),
kb->keycodes, kb->num_keycodes, &kb->modifiers);
view_set_borders_active(view, true);
}
static struct stage_view *
desktop_view_at(struct stage_server *server, double lx, double ly,
struct wlr_surface **surface, double *sx, double *sy)
{
struct wlr_scene_surface *scene_surface;
struct wlr_scene_buffer *scene_buffer;
struct wlr_scene_node *node;
struct wlr_scene_tree *tree;
struct stage_view *view;
struct stage_output *out;
struct stage_workspace *ws;
const struct wlr_box *box;
int i;
#if 0
out = output_at(server, lx, ly);
ws = &workspaces[out->curws];
wl_list_for_each(view, &ws->views, link) {
box = &view->xdg_toplevel->base->current.geometry;
if (wlr_box_contains_point(box, lx, ly))
return (view);
}
return (NULL);
#endif
#if 1
node = wlr_scene_node_at(&server->scene->tree.node, lx, ly, sx, sy);
if (node == NULL)
return (NULL);
if (surface && node->type == WLR_SCENE_NODE_BUFFER) {
scene_buffer = wlr_scene_buffer_from_node(node);
scene_surface = wlr_scene_surface_try_from_buffer(scene_buffer);
if (scene_surface == NULL)
return (NULL);
*surface = scene_surface->surface;
}
tree = node->parent;
while (tree != NULL && tree->node.data == NULL)
tree = tree->node.parent;
if (tree == NULL)
return (NULL);
view = tree->node.data;
#endif
return (view);
}
static const char *
get_app_id(struct stage_view *view)
{
const char *res;
res = view->xdg_toplevel->app_id;
return (res);
}
static void
view_set_slot(struct stage_view *view)
{
struct terminal_slot *slot;
struct stage_view *v;
uint32_t h, w, tw;
const char *app_id;
int i;
app_id = get_app_id(view);
if (!app_id)
return;
v = NULL;
dbg_printf("app id %s\n", app_id);
if (strcmp(app_id, "foot") == 0 ||
strcmp(app_id, "XTerm") == 0 ||
strcmp(app_id, "URxvt") == 0) {
for (i = 0; i < nslots; i++) {
slot = &slots[i];
if ((slot->flags & SLOT_NEW_WINDOW) == 0)
continue;
v = desktop_view_at(view->server, slot->x, slot->y,
NULL, NULL, NULL);
if (v != NULL)
continue;
view->x = slot->x;
view->y = slot->y;
view->w = slot->w;
view->h = slot->h;
view->slot_set = true;
break;
}
}
}
static void
view_align(struct stage_view *view)
{
struct wlr_box geom;
struct stage_output *out;
struct wlr_output *output;
out = cursor_at(view->server);
output = out->wlr_output;
geom = view->xdg_toplevel->base->geometry;
printf("%s: view geoms %d %d %d %d\n", __func__, geom.x, geom.y,
geom.width, geom.height);
view->w = geom.width;
view->h = geom.height;
view->x = (output->width - geom.width) / 2;
view->y = (output->height - geom.height) / 2;
}
static void
xdg_toplevel_map(struct wl_listener *listener, void *data)
{
struct stage_workspace *curws;
struct stage_output *out;
struct stage_view *view;
struct wlr_box geom;
view = wl_container_of(listener, view, map);
out = cursor_at(view->server);
curws = &workspaces[out->curws];
wl_list_insert(&curws->views, &view->link);
#if 0
enum wlr_edges edges = WLR_EDGE_NONE;
edges = WLR_EDGE_LEFT | WLR_EDGE_RIGHT | WLR_EDGE_TOP | WLR_EDGE_BOTTOM;
wlr_xdg_toplevel_set_tiled(view->xdg_toplevel, edges);
#endif
if (view->slot_set == false)
view_align(view);
view_geometry(view, &geom);
view->w = geom.width;
view->h = geom.height;
if (view->type == VIEW_SLOCK)
return;
update_borders(view);
focus_view(view, view_surface(view));
}
void
slock_destroy_view(struct wl_listener *listener, void *data)
{
struct wlr_session_lock_surface_v1 *lock_surface;
struct stage_view *view;
view = wl_container_of(listener, view, destroy);
printf("%s\n", __func__);
lock_surface = view->lock_surface;
wl_list_remove(&view->map.link);
wl_list_remove(&view->destroy.link);
}
static void
slock_map_view(struct wl_listener *listener, void *data)
{
struct wlr_session_lock_surface_v1 *lock_surface;
struct stage_view *view;
printf("%s\n", __func__);
view = wl_container_of(listener, view, map);
lock_surface = view->lock_surface;
}
void
slock_new_surface(struct wl_listener *listener, void *data)
{
struct wlr_session_lock_surface_v1 *lock_surface;
struct stage_server *server;
struct stage_lock *slock;
struct stage_view *view;
struct wlr_scene_surface *surface;
struct wlr_session_lock_v1 *lock;
printf("%s\n", __func__);
lock_surface = data;
slock = wl_container_of(listener, slock, new_surface);
server = slock->server;
lock = slock->lock;
view = malloc(sizeof(struct stage_view));
memset(view, 0, sizeof(struct stage_view));
view->type = VIEW_SLOCK;
view->server = server;
view->lock_surface = lock_surface;
view->map.notify = slock_map_view;
wl_signal_add(&lock_surface->surface->events.map, &view->map);
view->destroy.notify = slock_destroy_view;
wl_signal_add(&lock_surface->events.destroy, &view->destroy);
/* TODO: destroy surface when not needed? */
surface = wlr_scene_surface_create(&server->scene->tree,
lock_surface->surface);
view->scene_tree = NULL;
view->lock_surface->data = surface;
struct wlr_output *output;
struct stage_output *out;
out = cursor_at(server);
output = out->wlr_output;
view->w = output->width;
view->h = output->height;
wlr_session_lock_surface_v1_configure(lock_surface, view->w, view->h);
focus_view(view, view_surface(view));
}
void
slock_unlock(struct wl_listener *listener, void *data)
{
struct stage_server *server;
struct stage_lock *slock;
slock = wl_container_of(listener, slock, unlock);
printf("%s\n", __func__);
server = slock->server;
server->locked = false;
}
void
slock_destroy(struct wl_listener *listener, void *data)
{
struct stage_lock *slock;
struct wlr_session_lock_v1 *lock;
slock = wl_container_of(listener, slock, destroy);
lock = slock->lock;
printf("%s\n", __func__);
wl_list_remove(&slock->new_surface.link);
wl_list_remove(&slock->unlock.link);
wl_list_remove(&slock->destroy.link);
}
void
new_lock(struct wl_listener *listener, void *data)
{
struct wlr_session_lock_v1 *lock;
struct stage_lock *slock;
struct stage_server *server;
server = wl_container_of(listener, server, new_lock);
lock = data;
slock = malloc(sizeof(struct stage_lock));
slock->lock = lock;
slock->server = server;
//lock->data = slock;
slock->new_surface.notify = slock_new_surface;
wl_signal_add(&lock->events.new_surface, &slock->new_surface);
slock->unlock.notify = slock_unlock;
wl_signal_add(&lock->events.unlock, &slock->unlock);
slock->destroy.notify = slock_destroy;
wl_signal_add(&lock->events.destroy, &slock->destroy);
printf("%s\n", __func__);
wlr_session_lock_v1_send_locked(lock);
server->locked = true;
}
void
request_activate(struct wl_listener *listener, void *data)
{
printf("%s\n", __func__);
}
void
layout_change(struct wl_listener *listener, void *data)
{
printf("%s\n", __func__);
}
void
output_manager_apply(struct wl_listener *listener, void *data)
{
printf("%s\n", __func__);
}
void
output_manager_test(struct wl_listener *listener, void *data)
{
printf("%s\n", __func__);
}
static struct stage_view *
view_from_surface(struct stage_server *server, struct wlr_surface *surface)
{
struct wlr_xdg_surface *xdg_surface;
struct wlr_scene_node *scene_node;
struct stage_view *view;
xdg_surface = wlr_xdg_surface_try_from_wlr_surface(surface);
assert(xdg_surface != NULL);
scene_node = xdg_surface->data;
view = scene_node->data;
assert(view != NULL);
return (view);
}
static void
socket_send(char *str)
{
struct sockaddr_un addr;
char send_msg[16];
int fd;
if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
return;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, SERVER_SOCK_FILE);
sprintf(send_msg, "%s", str);
sendto(fd, send_msg, strlen(send_msg) + 1, 0, (struct sockaddr *)&addr,
sizeof(struct sockaddr_un));
close(fd);
}
static void
notify_ws_change(int oldws, int newws)
{
struct stage_workspace *ws;
char str[32];
char *cur;
int error;
int count;
int i;
memset(str, 0, 32);
str[0] = 'W';
cur = str + 1;
/* Start from workspace 1. End with workspace 0. */
i = 1;
do {
if (i == N_WORKSPACES)
i = 0;
ws = &workspaces[i];
if (i == newws) {
snprintf(cur, 3, "!%c", ws->name);
cur += 2;
}
#if 1
else if (!wl_list_empty(&ws->views)) {
if (i == oldws) {
snprintf(cur, 3, "?%c", ws->name);
cur += 2;
} else {
snprintf(cur, 2, "%c", ws->name);
cur += 1;
}
}
#endif
} while (i++);
socket_send(str);
}
static void
changeworkspace(struct stage_server *server, int newws)
{
struct stage_workspace *ws;
struct stage_view *view, *tmpview;
struct stage_view *focused_view;
struct stage_output *out;
struct wlr_surface *surface;
struct wlr_seat *seat;
int oldws;
out = cursor_at(server);
if (out->curws == newws)
return;
oldws = out->curws;
server->oldws = oldws;
out->curws = newws;
focused_view = NULL;
seat = server->seat;
surface = seat->keyboard_state.focused_surface;
if (surface)
focused_view = view_from_surface(server, surface);
dbg_printf("%s: ws %d -> %d\n", __func__, oldws, newws);
ws = &workspaces[oldws];
wl_list_for_each_safe(view, tmpview, &ws->views, link) {
wlr_scene_node_set_enabled(&view->scene_tree->node, false);
if (view == focused_view)
view->was_focused = true;
else
view->was_focused = false;
}
ws = &workspaces[newws];
wl_list_for_each_safe(view, tmpview, &ws->views, link) {
wlr_scene_node_set_enabled(&view->scene_tree->node, true);
if (view->was_focused)
focus_view(view, view_surface(view));
}
cursor_focus(server, 0);
notify_ws_change(oldws, newws);
}