-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
main.c
2320 lines (1908 loc) · 60.8 KB
/
main.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) 2019 - 2024 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
#include <inttypes.h>
#include <errno.h>
#include <neatvnc.h>
#include <aml.h>
#include <signal.h>
#include <libdrm/drm_fourcc.h>
#include <wayland-client.h>
#include <pixman.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "wlr-screencopy-unstable-v1.h"
#include "ext-image-copy-capture-v1.h"
#include "ext-image-capture-source-v1.h"
#include "wlr-virtual-pointer-unstable-v1.h"
#include "virtual-keyboard-unstable-v1.h"
#include "xdg-output-unstable-v1.h"
#include "wlr-output-power-management-unstable-v1.h"
#include "wlr-output-management-unstable-v1.h"
#include "linux-dmabuf-unstable-v1.h"
#include "ext-transient-seat-v1.h"
#include "screencopy-interface.h"
#include "data-control.h"
#include "strlcpy.h"
#include "output.h"
#include "output-management.h"
#include "pointer.h"
#include "keyboard.h"
#include "seat.h"
#include "cfg.h"
#include "transform-util.h"
#include "usdt.h"
#include "ctl-server.h"
#include "util.h"
#include "option-parser.h"
#include "pixels.h"
#include "buffer.h"
#ifdef ENABLE_PAM
#include "pam_auth.h"
#endif
#define DEFAULT_ADDRESS "127.0.0.1"
#define DEFAULT_PORT 5900
#define XSTR(x) STR(x)
#define STR(x) #x
#define MAYBE_UNUSED __attribute__((unused))
struct wayvnc_client;
enum socket_type {
SOCKET_TYPE_TCP = 0,
SOCKET_TYPE_UNIX,
SOCKET_TYPE_WEBSOCKET,
SOCKET_TYPE_FROM_FD,
};
struct wayvnc {
bool do_exit;
struct wl_display* display;
struct wl_registry* registry;
struct aml_handler* wl_handler;
struct wl_list outputs;
struct wl_list seats;
struct cfg cfg;
struct zwp_virtual_keyboard_manager_v1* keyboard_manager;
struct zwlr_virtual_pointer_manager_v1* pointer_manager;
struct zwlr_data_control_manager_v1* data_control_manager;
struct ext_transient_seat_manager_v1* transient_seat_manager;
struct output* selected_output;
struct seat* selected_seat;
struct screencopy* screencopy;
struct aml_handler* wayland_handler;
struct aml_signal* signal_handler;
struct nvnc* nvnc;
struct nvnc_display* nvnc_display;
const char* kb_layout;
const char* kb_variant;
uint32_t damage_area_sum;
uint32_t n_frames_captured;
bool disable_input;
bool use_transient_seat;
int nr_clients;
struct aml_ticker* performance_ticker;
struct aml_timer* capture_retry_timer;
struct ctl* ctl;
bool is_initializing;
bool start_detached;
bool overlay_cursor;
int max_rate;
bool enable_gpu_features;
bool enable_resizing;
struct wayvnc_client* master_layout_client;
struct wayvnc_client* cursor_master;
struct screencopy* cursor_sc;
};
struct wayvnc_client {
struct wayvnc* server;
struct nvnc_client* nvnc_client;
struct seat* seat;
struct ext_transient_seat_v1* transient_seat;
unsigned id;
struct pointer pointer;
struct keyboard keyboard;
struct data_control data_control;
};
void wayvnc_exit(struct wayvnc* self);
void on_capture_done(enum screencopy_result result, struct wv_buffer* buffer,
void* userdata);
static void on_cursor_capture_done(enum screencopy_result result,
struct wv_buffer* buffer, void* userdata);
static void on_nvnc_client_new(struct nvnc_client* client);
void switch_to_output(struct wayvnc*, struct output*);
void switch_to_next_output(struct wayvnc*);
void switch_to_prev_output(struct wayvnc*);
static void client_init_seat(struct wayvnc_client* self);
static void client_init_pointer(struct wayvnc_client* self);
static void client_init_keyboard(struct wayvnc_client* self);
static void client_init_data_control(struct wayvnc_client* self);
static void client_detach_wayland(struct wayvnc_client* self);
static int blank_screen(struct wayvnc* self);
static bool wayland_attach(struct wayvnc* self, const char* display,
const char* output);
static void wayland_detach(struct wayvnc* self);
static bool configure_cursor_sc(struct wayvnc* self,
struct wayvnc_client* client);
struct wl_shm* wl_shm = NULL;
struct zwp_linux_dmabuf_v1* zwp_linux_dmabuf = NULL;
struct zxdg_output_manager_v1* xdg_output_manager = NULL;
struct zwlr_output_power_manager_v1* wlr_output_power_manager = NULL;
struct zwlr_screencopy_manager_v1* screencopy_manager = NULL;
struct ext_output_image_capture_source_manager_v1*
ext_output_image_capture_source_manager = NULL;
struct ext_image_copy_capture_manager_v1* ext_image_copy_capture_manager = NULL;
extern struct screencopy_impl wlr_screencopy_impl, ext_image_copy_capture_impl;
static bool registry_add_input(void* data, struct wl_registry* registry,
uint32_t id, const char* interface,
uint32_t version)
{
struct wayvnc* self = data;
if (self->disable_input)
return false;
if (strcmp(interface, wl_seat_interface.name) == 0) {
struct wl_seat* wl_seat =
wl_registry_bind(registry, id, &wl_seat_interface, 7);
if (!wl_seat)
return true;
struct seat* seat = seat_new(wl_seat, id);
if (!seat) {
wl_seat_release(wl_seat);
return true;
}
wl_list_insert(&self->seats, &seat->link);
return true;
}
if (strcmp(interface, zwlr_virtual_pointer_manager_v1_interface.name) == 0) {
self->pointer_manager = wl_registry_bind(registry, id,
&zwlr_virtual_pointer_manager_v1_interface,
MIN(2, version));
return true;
}
if (strcmp(interface, zwp_virtual_keyboard_manager_v1_interface.name) == 0) {
self->keyboard_manager = wl_registry_bind(registry, id,
&zwp_virtual_keyboard_manager_v1_interface,
1);
return true;
}
if (strcmp(interface, zwlr_data_control_manager_v1_interface.name) == 0) {
self->data_control_manager = wl_registry_bind(registry, id,
&zwlr_data_control_manager_v1_interface, 2);
return true;
}
if (strcmp(interface, ext_transient_seat_manager_v1_interface.name) == 0) {
self->transient_seat_manager = wl_registry_bind(registry, id,
&ext_transient_seat_manager_v1_interface, 1);
return true;
}
return false;
}
static void registry_add(void* data, struct wl_registry* registry,
uint32_t id, const char* interface,
uint32_t version)
{
struct wayvnc* self = data;
if (strcmp(interface, wl_output_interface.name) == 0) {
nvnc_trace("Registering new output %u", id);
struct wl_output* wl_output =
wl_registry_bind(registry, id, &wl_output_interface, 3);
if (!wl_output)
return;
struct output* output = output_new(wl_output, id);
if (!output)
return;
wl_list_insert(&self->outputs, &output->link);
if (!self->is_initializing) {
wl_display_dispatch(self->display);
wl_display_roundtrip(self->display);
ctl_server_event_output_added(self->ctl, output->name);
}
return;
}
if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
nvnc_trace("Registering new xdg_output_manager");
xdg_output_manager =
wl_registry_bind(registry, id,
&zxdg_output_manager_v1_interface, 3);
output_setup_xdg_output_managers(&self->outputs);
return;
}
if (strcmp(interface, zwlr_output_power_manager_v1_interface.name) == 0) {
nvnc_trace("Registering new wlr_output_power_manager");
wlr_output_power_manager = wl_registry_bind(registry, id,
&zwlr_output_power_manager_v1_interface, 1);
return;
}
if (strcmp(interface, zwlr_output_manager_v1_interface.name) == 0) {
nvnc_trace("Registering new wlr_output_manager");
struct zwlr_output_manager_v1* wlr_output_manager =
wl_registry_bind(registry, id,
&zwlr_output_manager_v1_interface, 1);
wlr_output_manager_setup(wlr_output_manager);
return;
}
if (strcmp(interface, zwlr_screencopy_manager_v1_interface.name) == 0) {
screencopy_manager =
wl_registry_bind(registry, id,
&zwlr_screencopy_manager_v1_interface,
MIN(3, version));
return;
}
#if 1
if (strcmp(interface, ext_image_copy_capture_manager_v1_interface.name) == 0) {
ext_image_copy_capture_manager =
wl_registry_bind(registry, id,
&ext_image_copy_capture_manager_v1_interface,
MIN(1, version));
return;
}
if (strcmp(interface, ext_output_image_capture_source_manager_v1_interface.name) == 0) {
ext_output_image_capture_source_manager =
wl_registry_bind(registry, id,
&ext_output_image_capture_source_manager_v1_interface,
MIN(1, version));
return;
}
#endif
if (strcmp(interface, wl_shm_interface.name) == 0) {
wl_shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
return;
}
if (strcmp(interface, zwp_linux_dmabuf_v1_interface.name) == 0) {
zwp_linux_dmabuf = wl_registry_bind(registry, id,
&zwp_linux_dmabuf_v1_interface, 3);
return;
}
if (registry_add_input(data, registry, id, interface, version))
return;
}
static void disconnect_seat_clients(struct wayvnc* self, struct seat* seat)
{
struct nvnc_client* nvnc_client;
for (nvnc_client = nvnc_client_first(self->nvnc); nvnc_client;
nvnc_client = nvnc_client_next(nvnc_client)) {
struct wayvnc_client* client = nvnc_get_userdata(nvnc_client);
assert(client);
if (client->seat == seat) {
nvnc_client_close(nvnc_client);
}
}
}
static void registry_remove(void* data, struct wl_registry* registry,
uint32_t id)
{
struct wayvnc* self = data;
struct output* out = output_find_by_id(&self->outputs, id);
if (out) {
if (out == self->selected_output) {
nvnc_log(NVNC_LOG_WARNING, "Selected output %s went away",
out->name);
switch_to_prev_output(self);
} else
nvnc_log(NVNC_LOG_INFO, "Output %s went away", out->name);
ctl_server_event_output_removed(self->ctl, out->name);
wl_list_remove(&out->link);
output_destroy(out);
if (out == self->selected_output) {
if (self->start_detached) {
nvnc_log(NVNC_LOG_WARNING, "No fallback outputs left. Detaching...");
wayland_detach(self);
} else {
nvnc_log(NVNC_LOG_ERROR, "No fallback outputs left. Exiting...");
wayvnc_exit(self);
}
}
return;
}
struct seat* seat = seat_find_by_id(&self->seats, id);
if (seat) {
nvnc_log(NVNC_LOG_INFO, "Seat %s went away", seat->name);
disconnect_seat_clients(self, seat);
wl_list_remove(&seat->link);
seat_destroy(seat);
return;
}
}
static void wayland_detach(struct wayvnc* self)
{
if (!self->display)
return;
aml_stop(aml_get_default(), self->wl_handler);
aml_unref(self->wl_handler);
self->wl_handler = NULL;
// Screen blanking is required to release wl_shm of linux_dmabuf.
if (self->nvnc)
blank_screen(self);
if (self->nvnc) {
struct nvnc_client* nvnc_client;
for (nvnc_client = nvnc_client_first(self->nvnc); nvnc_client;
nvnc_client = nvnc_client_next(nvnc_client)) {
struct wayvnc_client* client =
nvnc_get_userdata(nvnc_client);
client_detach_wayland(client);
}
}
self->selected_output = NULL;
output_list_destroy(&self->outputs);
seat_list_destroy(&self->seats);
if (zwp_linux_dmabuf)
zwp_linux_dmabuf_v1_destroy(zwp_linux_dmabuf);
zwp_linux_dmabuf = NULL;
screencopy_stop(self->screencopy);
screencopy_destroy(self->screencopy);
self->screencopy = NULL;
screencopy_stop(self->cursor_sc);
screencopy_destroy(self->cursor_sc);
self->cursor_sc = NULL;
if (xdg_output_manager)
zxdg_output_manager_v1_destroy(xdg_output_manager);
xdg_output_manager = NULL;
if (wlr_output_power_manager)
zwlr_output_power_manager_v1_destroy(wlr_output_power_manager);
wlr_output_power_manager = NULL;
wlr_output_manager_destroy();
wl_shm_destroy(wl_shm);
wl_shm = NULL;
if (self->keyboard_manager)
zwp_virtual_keyboard_manager_v1_destroy(self->keyboard_manager);
self->keyboard_manager = NULL;
if (self->pointer_manager)
zwlr_virtual_pointer_manager_v1_destroy(self->pointer_manager);
self->pointer_manager = NULL;
if (self->data_control_manager)
zwlr_data_control_manager_v1_destroy(self->data_control_manager);
self->data_control_manager = NULL;
if (self->performance_ticker) {
aml_stop(aml_get_default(), self->performance_ticker);
aml_unref(self->performance_ticker);
}
self->performance_ticker = NULL;
if (screencopy_manager)
zwlr_screencopy_manager_v1_destroy(screencopy_manager);
screencopy_manager = NULL;
if (ext_output_image_capture_source_manager)
ext_output_image_capture_source_manager_v1_destroy(
ext_output_image_capture_source_manager);
ext_output_image_capture_source_manager = NULL;
if (ext_image_copy_capture_manager)
ext_image_copy_capture_manager_v1_destroy(ext_image_copy_capture_manager);
ext_image_copy_capture_manager = NULL;
if (self->capture_retry_timer) {
aml_stop(aml_get_default(), self->capture_retry_timer);
aml_unref(self->capture_retry_timer);
}
self->capture_retry_timer = NULL;
if (self->transient_seat_manager)
ext_transient_seat_manager_v1_destroy(self->transient_seat_manager);
wl_registry_destroy(self->registry);
self->registry = NULL;
wl_display_disconnect(self->display);
self->display = NULL;
if (self->ctl)
ctl_server_event_detached(self->ctl);
}
void wayvnc_destroy(struct wayvnc* self)
{
cfg_destroy(&self->cfg);
wayland_detach(self);
}
void on_wayland_event(void* obj)
{
struct wayvnc* self = aml_get_userdata(obj);
int rc MAYBE_UNUSED = wl_display_prepare_read(self->display);
assert(rc == 0);
if (wl_display_read_events(self->display) < 0) {
if (errno == EPIPE || errno == ECONNRESET) {
nvnc_log(NVNC_LOG_ERROR, "Compositor has gone away. Exiting...");
if (self->start_detached)
wayland_detach(self);
else
wayvnc_exit(self);
return;
} else {
nvnc_log(NVNC_LOG_ERROR, "Failed to read wayland events: %m");
}
}
if (wl_display_dispatch_pending(self->display) < 0) {
nvnc_log(NVNC_LOG_ERROR, "Failed to dispatch pending");
wayland_detach(self);
// TODO: Re-attach
}
}
static int init_wayland(struct wayvnc* self, const char* display)
{
self->is_initializing = true;
static const struct wl_registry_listener registry_listener = {
.global = registry_add,
.global_remove = registry_remove,
};
self->display = wl_display_connect(display);
if (!self->display) {
const char* display_name = display ? display:
getenv("WAYLAND_DISPLAY");
if (!display_name) {
nvnc_log(NVNC_LOG_ERROR, "WAYLAND_DISPLAY is not set in the environment");
} else {
nvnc_log(NVNC_LOG_ERROR, "Failed to connect to WAYLAND_DISPLAY=\"%s\"", display_name);
nvnc_log(NVNC_LOG_ERROR, "Ensure wayland is running with that display name");
}
return -1;
}
wl_list_init(&self->outputs);
wl_list_init(&self->seats);
self->registry = wl_display_get_registry(self->display);
if (!self->registry) {
nvnc_log(NVNC_LOG_ERROR, "Could not locate the wayland compositor object registry");
goto failure;
}
wl_registry_add_listener(self->registry, ®istry_listener, self);
wl_display_dispatch(self->display);
wl_display_roundtrip(self->display);
self->is_initializing = false;
if (!self->pointer_manager && !self->disable_input) {
nvnc_log(NVNC_LOG_ERROR, "Virtual Pointer protocol not supported by compositor.");
nvnc_log(NVNC_LOG_ERROR, "wayvnc may still work if started with --disable-input.");
goto failure;
}
if (!self->keyboard_manager && !self->disable_input) {
nvnc_log(NVNC_LOG_ERROR, "Virtual Keyboard protocol not supported by compositor.");
nvnc_log(NVNC_LOG_ERROR, "wayvnc may still work if started with --disable-input.");
goto failure;
}
if (!screencopy_manager && !ext_image_copy_capture_manager) {
nvnc_log(NVNC_LOG_ERROR, "Screencopy protocol not supported by compositor. Exiting. Refer to FAQ section in man page.");
goto failure;
}
if (!self->transient_seat_manager && self->use_transient_seat) {
nvnc_log(NVNC_LOG_ERROR, "Transient seat protocol not supported by compositor");
goto failure;
}
self->wl_handler = aml_handler_new(wl_display_get_fd(self->display),
on_wayland_event, self, NULL);
if (!self->wl_handler)
goto failure;
int rc = aml_start(aml_get_default(), self->wl_handler);
if (rc < 0)
goto handler_failure;
return 0;
failure:
wl_display_disconnect(self->display);
self->display = NULL;
handler_failure:
if (self->wl_handler)
aml_unref(self->wl_handler);
self->wl_handler = NULL;
return -1;
}
void wayvnc_exit(struct wayvnc* self)
{
self->do_exit = true;
}
void on_signal(void* obj)
{
nvnc_log(NVNC_LOG_INFO, "Received termination signal.");
struct wayvnc* self = aml_get_userdata(obj);
wayvnc_exit(self);
}
struct cmd_response* on_output_cycle(struct ctl* ctl, enum output_cycle_direction direction)
{
struct wayvnc* self = ctl_server_userdata(ctl);
nvnc_log(NVNC_LOG_INFO, "ctl command: Rotating to %s output",
direction == OUTPUT_CYCLE_FORWARD ? "next" : "previous");
struct output* next = output_cycle(&self->outputs,
self->selected_output, direction);
switch_to_output(self, next);
return cmd_ok();
}
struct cmd_response* on_output_switch(struct ctl* ctl,
const char* output_name)
{
nvnc_log(NVNC_LOG_INFO, "ctl command: Switch to output \"%s\"", output_name);
struct wayvnc* self = ctl_server_userdata(ctl);
if (!output_name || output_name[0] == '\0')
return cmd_failed("Output name is required");
struct output* output = output_find_by_name(&self->outputs, output_name);
if (!output) {
return cmd_failed("No such output \"%s\"", output_name);
}
switch_to_output(self, output);
return cmd_ok();
}
static struct ctl_server_client *client_next(struct ctl* ctl,
struct ctl_server_client *prev)
{
struct wayvnc* self = ctl_server_userdata(ctl);
struct nvnc_client* vnc_prev = (struct nvnc_client*)prev;
return prev ? (struct ctl_server_client*)nvnc_client_next(vnc_prev) :
(struct ctl_server_client*)nvnc_client_first(self->nvnc);
}
static void compose_client_info(const struct wayvnc_client* client,
struct ctl_server_client_info* info)
{
info->id = client->id;
socklen_t addrlen = sizeof(info->address_storage);
nvnc_client_get_address(client->nvnc_client,
(struct sockaddr*)&info->address_storage, &addrlen);
info->username = nvnc_client_get_auth_username(client->nvnc_client);
info->seat = client->seat ? client->seat->name : NULL;
}
static void client_info(const struct ctl_server_client* client_handle,
struct ctl_server_client_info* info)
{
const struct nvnc_client *vnc_client =
(const struct nvnc_client*)client_handle;
const struct wayvnc_client *client = nvnc_get_userdata(vnc_client);
compose_client_info(client, info);
}
static int get_output_list(struct ctl* ctl,
struct ctl_server_output** outputs)
{
struct wayvnc* self = ctl_server_userdata(ctl);
int n = wl_list_length(&self->outputs);
if (n == 0) {
*outputs = NULL;
return 0;
}
*outputs = calloc(n, sizeof(**outputs));
struct output* output;
struct ctl_server_output* item = *outputs;
wl_list_for_each(output, &self->outputs, link) {
strlcpy(item->name, output->name, sizeof(item->name));
strlcpy(item->description, output->description,
sizeof(item->description));
item->height = output->height;
item->width = output->width;
item->captured = (output->id == self->selected_output->id);
strlcpy(item->power, output_power_state_name(output->power),
sizeof(item->power));
item++;
}
return n;
}
static struct cmd_response* on_disconnect_client(struct ctl* ctl,
const char* id_string)
{
char* endptr;
unsigned int id = strtoul(id_string, &endptr, 0);
if (!*id_string || *endptr)
return cmd_failed("Invalid client ID \"%s\"", id_string);
struct wayvnc* self = ctl_server_userdata(ctl);
for (struct nvnc_client* nvnc_client = nvnc_client_first(self->nvnc);
nvnc_client;
nvnc_client = nvnc_client_next(nvnc_client)) {
struct wayvnc_client* client = nvnc_get_userdata(nvnc_client);
if (client->id == id) {
nvnc_log(NVNC_LOG_WARNING, "Disconnecting client %d via control socket command",
client->id);
nvnc_client_close(nvnc_client);
return cmd_ok();
}
}
return cmd_failed("No such client with ID \"%s\"", id_string);
}
static struct cmd_response* on_wayvnc_exit(struct ctl* ctl)
{
struct wayvnc* self = ctl_server_userdata(ctl);
nvnc_log(NVNC_LOG_WARNING, "Shutting down via control socket command");
wayvnc_exit(self);
return cmd_ok();
}
int init_main_loop(struct wayvnc* self)
{
struct aml* loop = aml_get_default();
struct aml_signal* sig;
sig = aml_signal_new(SIGINT, on_signal, self, NULL);
if (!sig)
return -1;
int rc = aml_start(loop, sig);
aml_unref(sig);
if (rc < 0)
return -1;
return 0;
}
static void on_pointer_event(struct nvnc_client* client, uint16_t x, uint16_t y,
enum nvnc_button_mask button_mask)
{
struct wayvnc_client* wv_client = nvnc_get_userdata(client);
struct wayvnc* wayvnc = wv_client->server;
if (!wv_client->pointer.pointer) {
return;
}
uint32_t xfx = 0, xfy = 0;
output_transform_coord(wayvnc->selected_output, x, y, &xfx, &xfy);
pointer_set(&wv_client->pointer, xfx, xfy, button_mask);
}
static void on_key_event(struct nvnc_client* client, uint32_t symbol,
bool is_pressed)
{
struct wayvnc_client* wv_client = nvnc_get_userdata(client);
if (!wv_client->keyboard.virtual_keyboard) {
return;
}
keyboard_feed(&wv_client->keyboard, symbol, is_pressed);
nvnc_client_set_led_state(wv_client->nvnc_client,
keyboard_get_led_state(&wv_client->keyboard));
}
static void on_key_code_event(struct nvnc_client* client, uint32_t code,
bool is_pressed)
{
struct wayvnc_client* wv_client = nvnc_get_userdata(client);
if (!wv_client->keyboard.virtual_keyboard) {
return;
}
keyboard_feed_code(&wv_client->keyboard, code + 8, is_pressed);
nvnc_client_set_led_state(wv_client->nvnc_client,
keyboard_get_led_state(&wv_client->keyboard));
}
static void on_client_cut_text(struct nvnc_client* nvnc_client,
const char* text, uint32_t len)
{
struct wayvnc_client* client = nvnc_get_userdata(nvnc_client);
if (client->data_control.manager) {
data_control_to_clipboard(&client->data_control, text, len);
}
}
static bool on_client_resize(struct nvnc_client* nvnc_client,
const struct nvnc_desktop_layout* layout)
{
struct wayvnc_client* client = nvnc_get_userdata(nvnc_client);
struct wayvnc* self = client->server;
uint16_t width = nvnc_desktop_layout_get_width(layout);
uint16_t height = nvnc_desktop_layout_get_height(layout);
struct output* output = client->server->selected_output;
if (output == NULL)
return false;
if (self->master_layout_client && self->master_layout_client != client)
return false;
self->master_layout_client = client;
nvnc_log(NVNC_LOG_DEBUG,
"Client resolution changed: %ux%u, capturing output %s which is headless: %s",
width, height, output->name,
output->is_headless ? "yes" : "no");
return wlr_output_manager_resize_output(output, width, height);
}
bool on_auth(const char* username, const char* password, void* ud)
{
struct wayvnc* self = ud;
#ifdef ENABLE_PAM
if (self->cfg.enable_pam)
return pam_auth(username, password);
#endif
if (strcmp(username, self->cfg.username) != 0)
return false;
if (strcmp(password, self->cfg.password) != 0)
return false;
return true;
}
static struct nvnc_fb* create_placeholder_buffer(uint16_t width, uint16_t height)
{
uint16_t stride = width;
struct nvnc_fb* fb = nvnc_fb_new(width, height, DRM_FORMAT_XRGB8888,
stride);
if (!fb)
return NULL;
size_t size = nvnc_fb_get_pixel_size(fb) * height * stride;
memset(nvnc_fb_get_addr(fb), 0x60, size);
return fb;
}
static int blank_screen(struct wayvnc* self)
{
int width = 1280;
int height = 720;
if (self->selected_output) {
width = output_get_transformed_width(self->selected_output);
height = output_get_transformed_height(self->selected_output);
}
struct nvnc_fb* placeholder_fb = create_placeholder_buffer(width, height);
if (!placeholder_fb) {
nvnc_log(NVNC_LOG_ERROR, "Failed to allocate a placeholder buffer");
return -1;
}
struct pixman_region16 damage;
pixman_region_init_rect(&damage, 0, 0,
nvnc_fb_get_width(placeholder_fb),
nvnc_fb_get_height(placeholder_fb));
nvnc_display_feed_buffer(self->nvnc_display, placeholder_fb, &damage);
pixman_region_fini(&damage);
nvnc_fb_unref(placeholder_fb);
return 0;
}
static char* get_cfg_path(const struct cfg* cfg, char* dst, const char* src)
{
if (!cfg->use_relative_paths || src[0] == '/') {
strlcpy(dst, src, PATH_MAX);
return dst;
}
snprintf(dst, PATH_MAX, "%s/%s", cfg->directory, src);
return dst;
}
static bool is_prefix(const char* prefix, const char* str)
{
return strncmp(str, prefix, strlen(prefix)) == 0;
}
static int count_colons_in_string(const char* str)
{
int n = 0;
for (int i = 0; str[i]; ++i)
if (str[i] == ':')
++n;
return n;
}
static char* parse_address_prefix(char* addr, enum socket_type* socket_type)
{
if (is_prefix("tcp:", addr)) {
*socket_type = SOCKET_TYPE_TCP;
addr += 4;
} else if (is_prefix("unix:", addr)) {
*socket_type = SOCKET_TYPE_UNIX;
addr += 5;
} else if (is_prefix("ws:", addr)) {
*socket_type = SOCKET_TYPE_WEBSOCKET;
addr += 3;
} else if (is_prefix("fd:", addr)) {
*socket_type = SOCKET_TYPE_FROM_FD;
addr += 3;
}
return addr;
}
static char* parse_address_port(char* addr, uint16_t* port)
{
// IPv6 addresses with port need to look like: [::]:5900
if (addr[0] == '[') {
char* end = strchr(addr, ']');
if (!end)
return addr;
*end++ = '\0';
if (*end == ':')
*port = atoi(end + 1);
return addr + 1;
}
if (count_colons_in_string(addr) > 1) {
// This is most likely IPv6, so let's leave it alone
return addr;
}
char* p = NULL;
p = strrchr(addr, ':');
if (!p)
return addr;
*p++ = '\0';
*port = atoi(p);
return addr;
}
static int add_listening_address(struct wayvnc* self, const char* address,
uint16_t port, enum socket_type socket_type)
{
char buffer[256];
char* addr = buffer;
strlcpy(buffer, address, sizeof(buffer));
addr = parse_address_prefix(addr, &socket_type);
if (socket_type == SOCKET_TYPE_TCP ||
socket_type == SOCKET_TYPE_WEBSOCKET) {
uint16_t parsed_port = 0;
addr = parse_address_port(addr, &parsed_port);
if (parsed_port)
port = parsed_port;
}
int rc = -1;
switch (socket_type) {
case SOCKET_TYPE_TCP:
rc = nvnc_listen_tcp(self->nvnc, addr, port,
NVNC_STREAM_NORMAL);
break;
case SOCKET_TYPE_UNIX:
rc = nvnc_listen_unix(self->nvnc, addr,
NVNC_STREAM_NORMAL);
break;
case SOCKET_TYPE_WEBSOCKET:
rc = nvnc_listen_tcp(self->nvnc, addr, port,
NVNC_STREAM_WEBSOCKET);
break;
case SOCKET_TYPE_FROM_FD:;
int fd = atoi(addr);
rc = nvnc_listen(self->nvnc, fd, NVNC_STREAM_NORMAL);
break;
default:
abort();
}