-
Notifications
You must be signed in to change notification settings - Fork 4
/
vncdisplaymm.cpp
1335 lines (1141 loc) · 41.4 KB
/
vncdisplaymm.cpp
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
/* This file is part of gsshvnc.
*
* gsshvnc is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* gsshvnc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gsshvnc. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vncdisplaymm.h"
#include "appsettings.h"
#include "credstorage.h"
#include <glibmm/exceptionhandler.h>
#include <glibmm/convert.h>
#include <giomm/socketaddress.h>
#include <gtkmm/box.h>
#include <gtkmm/grid.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/entry.h>
#include <gtkmm/checkbutton.h>
#include <gtkmm/menubar.h>
#include <gtkmm/checkmenuitem.h>
#include <gtkmm/radiomenuitem.h>
#include <gtkmm/separatormenuitem.h>
#include <gtkmm/settings.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/messagedialog.h>
#include <gtkmm/aboutdialog.h>
#include <iostream>
#include <memory>
#include <ctime>
#ifdef HAVE_PULSEAUDIO
#include <vncaudiopulse.h>
#endif
#ifdef __GNUC__
/* This is out of my control when gtk-vnc uses deprecated APIs */
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
static Vnc::DisplayWindow *s_instance = nullptr;
static gboolean _activate_menubar(Vnc::DisplayWindow *self, GtkAccelGroup *,
GObject *, guint, GdkModifierType)
{
self->activate_menubar();
return TRUE;
}
Vnc::DisplayWindow::DisplayWindow()
: m_vnc(), m_connected(false), m_accel_enabled(true), m_enable_mnemonics()
{
if (s_instance) {
std::cerr << "WARNING: Creating multiple Vnc::DisplayWindow instances is not supported"
<< std::endl;
}
s_instance = this;
set_default_icon_name("preferences-desktop-remote-desktop");
m_remote_size.width = -1;
m_remote_size.height = -1;
m_viewport = Gtk::manage(new Gtk::ScrolledWindow);
auto layout = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0));
m_menubar = Gtk::manage(new Gtk::MenuBar);
#ifdef HAVE_PULSEAUDIO
m_pulse_ifc = vnc_audio_pulse_new();
#endif
set_resizable(true);
auto remote = Gtk::manage(new Gtk::MenuItem("_Remote", true));
m_menubar->append(*remote);
auto submenu = Gtk::manage(new Gtk::Menu);
m_capture_keyboard = Gtk::manage(new Gtk::CheckMenuItem("Capture All _Keyboard Input", true));
auto send_f8 = Gtk::manage(new Gtk::MenuItem("Send F8", true));
auto send_cad = Gtk::manage(new Gtk::MenuItem("Send Ctrl+Alt+_Del", true));
auto screenshot = Gtk::manage(new Gtk::MenuItem("Take _Screenshot", true));
auto appquit = Gtk::manage(new Gtk::MenuItem("_Quit", true));
submenu->append(*m_capture_keyboard);
submenu->append(*send_f8);
submenu->append(*send_cad);
submenu->append(*Gtk::manage(new Gtk::SeparatorMenuItem));
submenu->append(*screenshot);
submenu->append(*Gtk::manage(new Gtk::SeparatorMenuItem));
submenu->append(*appquit);
remote->set_submenu(*submenu);
auto view = Gtk::manage(new Gtk::MenuItem("_View", true));
m_menubar->append(*view);
submenu = Gtk::manage(new Gtk::Menu);
m_hide_menubar = Gtk::manage(new Gtk::CheckMenuItem("_Hide Menu Bar", true));
m_fullscreen = Gtk::manage(new Gtk::CheckMenuItem("_Full Screen", true));
auto resize_menu = Gtk::manage(new Gtk::Menu);
Gtk::RadioMenuItem::Group resize_group;
m_resize_none = Gtk::manage(new Gtk::RadioMenuItem(resize_group, "_None", true));
m_resize_none->set_active(true);
resize_menu->append(*m_resize_none);
m_resize_scale = Gtk::manage(new Gtk::RadioMenuItem(resize_group, "_Scaled", true));
resize_menu->append(*m_resize_scale);
#if VNC_CHECK_VERSION(1, 2, 0)
m_resize_remote = Gtk::manage(new Gtk::RadioMenuItem(resize_group, "_Resize Remote", true));
resize_menu->append(*m_resize_remote);
#endif
auto resize = Gtk::manage(new Gtk::MenuItem("_Resize Mode", true));
resize->set_submenu(*resize_menu);
auto activate_menubar_accel = Gtk::AccelGroup::create();
add_accel_group(activate_menubar_accel);
gtk_accel_group_connect(activate_menubar_accel->gobj(), GDK_KEY_F8,
GdkModifierType(0), GtkAccelFlags(0),
g_cclosure_new_swap(G_CALLBACK(&_activate_menubar), this, nullptr));
submenu->append(*m_hide_menubar);
submenu->append(*Gtk::manage(new Gtk::SeparatorMenuItem));
submenu->append(*m_fullscreen);
submenu->append(*Gtk::manage(new Gtk::SeparatorMenuItem));
submenu->append(*resize);
#ifdef GTK_VNC_HAVE_SMOOTH_SCALING
m_smoothing = Gtk::manage(new Gtk::CheckMenuItem("S_mooth Scaling", true));
m_smoothing->set_active(true);
submenu->append(*m_smoothing);
#else
m_smoothing = nullptr;
#endif
#if VNC_CHECK_VERSION(1, 2, 0)
m_keep_ratio = Gtk::manage(new Gtk::CheckMenuItem("Keep _Aspect Ratio", true));
m_keep_ratio->set_active(true);
submenu->append(*m_keep_ratio);
#else
m_keep_ratio = nullptr;
#endif
view->set_submenu(*submenu);
auto help = Gtk::manage(new Gtk::MenuItem("_Help", true));
m_menubar->append(*help);
submenu = Gtk::manage(new Gtk::Menu);
auto about = Gtk::manage(new Gtk::MenuItem("_About", true));
submenu->append(*about);
help->set_submenu(*submenu);
layout->pack_start(*m_menubar, false, true);
layout->pack_start(*m_viewport, true, true);
add(*layout);
AppSettings settings;
auto saved_size = settings.get_window_size();
if (saved_size != std::make_tuple(-1, -1)) {
set_size_request(std::get<0>(saved_size), std::get<1>(saved_size));
} else {
// Minimal size in case a fixed size is not later requested
set_size_request(600, 400);
}
m_capture_keyboard->signal_activate().connect([this]() {
bool enable = m_capture_keyboard->get_active();
if (enable)
disable_modifiers();
else
enable_modifiers();
set_keyboard_grab(enable);
AppSettings settings;
settings.set_capture_keyboard(enable);
});
send_cad->signal_activate().connect([this]() {
send_keys({ GDK_KEY_Control_L, GDK_KEY_Alt_L, GDK_KEY_Delete });
});
send_f8->signal_activate().connect([this]() {
send_keys({ GDK_KEY_F8 });
});
screenshot->signal_activate().connect(sigc::mem_fun(this, &DisplayWindow::vnc_screenshot));
appquit->signal_activate().connect([this]() { get_application()->quit(); });
m_hide_menubar->signal_activate().connect([this]() {
toggle_menubar();
});
m_fullscreen->signal_toggled().connect([this]() {
if (m_fullscreen->get_active())
fullscreen();
else
unfullscreen();
});
m_resize_none->signal_toggled().connect([this]() {
if (!m_resize_none->get_active())
return;
on_set_scaling(false);
on_set_allow_resize(false);
update_scrolling();
AppSettings settings;
settings.set_scaled_display(false);
settings.set_allow_resize(false);
});
m_resize_scale->signal_toggled().connect([this]() {
if (!m_resize_scale->get_active())
return;
on_set_allow_resize(false);
on_set_scaling(true);
update_scrolling();
AppSettings settings;
settings.set_allow_resize(false);
settings.set_scaled_display(true);
});
#if VNC_CHECK_VERSION(1, 2, 0)
m_resize_remote->signal_toggled().connect([this]() {
if (!m_resize_remote->get_active())
return;
on_set_scaling(false);
on_set_allow_resize(true);
update_scrolling();
AppSettings settings;
settings.set_scaled_display(false);
settings.set_allow_resize(true);
});
#endif
#ifdef GTK_VNC_HAVE_SMOOTH_SCALING
m_smoothing->signal_toggled().connect([this]() {
bool enable = m_smoothing->get_active();
on_set_smoothing(enable);
AppSettings settings;
settings.set_smooth_scaling(enable);
});
#endif
#if VNC_CHECK_VERSION(1, 2, 0)
m_keep_ratio->signal_toggled().connect([this]() {
bool enable = m_keep_ratio->get_active();
on_set_keep_aspect_ratio(enable);
AppSettings settings;
settings.set_keep_aspect_ratio(enable);
});
#endif
about->signal_activate().connect([this]() {
Gtk::AboutDialog dialog;
dialog.set_transient_for(*this);
dialog.set_program_name("gsshvnc");
dialog.set_version(GSSHVNC_VERSION_STR);
dialog.set_logo_icon_name("preferences-desktop-remote-desktop");
dialog.set_comments("gsshvnc (pronounced \"Gosh VNC\") is a simple VNC "
"client with built-in support for SSH tunneling");
dialog.set_authors({"Michael Hansen <[email protected]>"});
dialog.set_copyright("Copyright \u00a9 2018 Michael Hansen");
dialog.set_license_type(Gtk::LICENSE_GPL_2_0);
dialog.set_website("https://github.com/zrax/gsshvnc");
(void)dialog.run();
});
// Don't capture keyboard when the window doesn't have focus
add_events(Gdk::FOCUS_CHANGE_MASK);
signal_focus_out_event().connect([this](GdkEventFocus *) -> bool {
if (m_capture_keyboard->get_active()) {
set_keyboard_grab(false);
set_pointer_grab(false);
}
return false;
});
signal_focus_in_event().connect([this](GdkEventFocus *) -> bool {
if (m_capture_keyboard->get_active()) {
set_keyboard_grab(true);
set_pointer_grab(true);
}
return false;
});
signal_show().connect([this]() {
if (m_hide_menubar->get_active())
m_menubar->hide();
});
signal_hide().connect([this]() {
int w, h;
get_size(w, h);
AppSettings settings;
settings.set_window_size(w, h);
});
}
Vnc::DisplayWindow::~DisplayWindow()
{
s_instance = nullptr;
}
bool Vnc::DisplayWindow::open_fd(int fd)
{
return static_cast<bool>(vnc_display_open_fd(get_vnc(), fd));
}
bool Vnc::DisplayWindow::open_fd(int fd, const Glib::ustring &hostname)
{
return static_cast<bool>(vnc_display_open_fd_with_hostname(get_vnc(), fd, hostname.c_str()));
}
bool Vnc::DisplayWindow::open_addr(Gio::SocketAddress *addr, const Glib::ustring &hostname)
{
return static_cast<bool>(vnc_display_open_addr(get_vnc(),
addr ? addr->gobj() : nullptr,
hostname.c_str()));
}
bool Vnc::DisplayWindow::open_host(const Glib::ustring &host, const Glib::ustring &port)
{
return static_cast<bool>(vnc_display_open_host(get_vnc(), host.c_str(), port.c_str()));
}
bool Vnc::DisplayWindow::is_open()
{
return static_cast<bool>(vnc_display_is_open(get_vnc()));
}
void Vnc::DisplayWindow::close_vnc()
{
vnc_display_close(get_vnc());
}
void Vnc::DisplayWindow::send_keys(const std::vector<guint> &keys)
{
vnc_display_send_keys(get_vnc(), keys.data(), static_cast<int>(keys.size()));
}
void Vnc::DisplayWindow::send_keys(const std::vector<guint> &keys,
VncDisplayKeyEvent kind)
{
vnc_display_send_keys_ex(get_vnc(), keys.data(), static_cast<int>(keys.size()),
kind);
}
void Vnc::DisplayWindow::send_pointer(gint x, gint y, int buttonmask)
{
vnc_display_send_pointer(get_vnc(), x, y, buttonmask);
}
void Vnc::DisplayWindow::set_grab_keys(Vnc::GrabSequence &seq)
{
vnc_display_set_grab_keys(get_vnc(), seq.gobj());
}
Vnc::GrabSequence Vnc::DisplayWindow::get_grab_keys()
{
return Vnc::GrabSequence(vnc_display_get_grab_keys(get_vnc()));
}
bool Vnc::DisplayWindow::set_credential(int type, const Glib::ustring &data)
{
return static_cast<bool>(vnc_display_set_credential(get_vnc(), type, data.c_str()));
}
void Vnc::DisplayWindow::set_pointer_local(bool enable)
{
vnc_display_set_pointer_local(get_vnc(), enable);
}
bool Vnc::DisplayWindow::get_pointer_local()
{
return static_cast<bool>(vnc_display_get_pointer_local(get_vnc()));
}
void Vnc::DisplayWindow::set_pointer_grab(bool enable)
{
vnc_display_set_pointer_grab(get_vnc(), enable);
}
bool Vnc::DisplayWindow::get_pointer_grab()
{
return static_cast<bool>(vnc_display_get_pointer_grab(get_vnc()));
}
void Vnc::DisplayWindow::set_keyboard_grab(bool enable)
{
vnc_display_set_keyboard_grab(get_vnc(), enable);
}
bool Vnc::DisplayWindow::get_keyboard_grab()
{
return static_cast<bool>(vnc_display_get_keyboard_grab(get_vnc()));
}
void Vnc::DisplayWindow::set_read_only(bool enable)
{
vnc_display_set_read_only(get_vnc(), enable);
}
bool Vnc::DisplayWindow::get_read_only()
{
return static_cast<bool>(vnc_display_get_read_only(get_vnc()));
}
Glib::RefPtr<Gdk::Pixbuf> Vnc::DisplayWindow::get_pixbuf()
{
return Glib::wrap(vnc_display_get_pixbuf(get_vnc()));
}
int Vnc::DisplayWindow::get_width()
{
return vnc_display_get_width(get_vnc());
}
int Vnc::DisplayWindow::get_height()
{
return vnc_display_get_height(get_vnc());
}
Glib::ustring Vnc::DisplayWindow::get_name()
{
auto vnc_name = vnc_display_get_name(get_vnc());
if (m_ssh_host.empty())
return vnc_name;
return Glib::ustring::compose("%1 [%2]", vnc_name, m_ssh_host);
}
void Vnc::DisplayWindow::client_cut_text(const std::string &text)
{
vnc_display_client_cut_text(get_vnc(), text.c_str());
}
void Vnc::DisplayWindow::set_lossy_encoding(bool enable)
{
vnc_display_set_lossy_encoding(get_vnc(), enable);
}
bool Vnc::DisplayWindow::get_lossy_encoding()
{
return static_cast<bool>(vnc_display_get_lossy_encoding(get_vnc()));
}
void Vnc::DisplayWindow::on_set_scaling(bool enable)
{
vnc_display_set_scaling(get_vnc(), enable);
}
void Vnc::DisplayWindow::set_scaling(bool enable)
{
on_set_scaling(enable);
if (enable)
m_resize_scale->set_active(true);
update_scrolling();
}
bool Vnc::DisplayWindow::get_scaling()
{
return static_cast<bool>(vnc_display_get_scaling(get_vnc()));
}
void Vnc::DisplayWindow::set_force_size(bool enable)
{
vnc_display_set_force_size(get_vnc(), enable);
}
bool Vnc::DisplayWindow::get_force_size()
{
return static_cast<bool>(vnc_display_get_force_size(get_vnc()));
}
void Vnc::DisplayWindow::on_set_allow_resize(bool enable)
{
#if VNC_CHECK_VERSION(1, 2, 0)
vnc_display_set_allow_resize(get_vnc(), enable);
#else
(void)enable;
#endif
}
void Vnc::DisplayWindow::set_allow_resize(bool enable)
{
on_set_allow_resize(enable);
#if VNC_CHECK_VERSION(1, 2, 0)
if (enable)
m_resize_remote->set_active(true);
update_scrolling();
#endif
}
bool Vnc::DisplayWindow::get_allow_resize()
{
#if VNC_CHECK_VERSION(1, 2, 0)
return static_cast<bool>(vnc_display_get_allow_resize(get_vnc()));
#else
return false;
#endif
}
void Vnc::DisplayWindow::on_set_smoothing(bool enable)
{
#ifdef GTK_VNC_HAVE_SMOOTH_SCALING
vnc_display_set_smoothing(get_vnc(), enable);
#else
(void)enable;
#endif
}
void Vnc::DisplayWindow::set_smoothing(bool enable)
{
on_set_smoothing(enable);
#ifdef GTK_VNC_HAVE_SMOOTH_SCALING
m_smoothing->set_active(enable);
#endif
}
bool Vnc::DisplayWindow::get_smoothing()
{
#ifdef GTK_VNC_HAVE_SMOOTH_SCALING
return static_cast<bool>(vnc_display_get_smoothing(get_vnc()));
#else
/* Always enabled for gtk-vnc < 0.7.0 */
return true;
#endif
}
void Vnc::DisplayWindow::on_set_keep_aspect_ratio(bool enable)
{
#if VNC_CHECK_VERSION(1, 2, 0)
vnc_display_set_keep_aspect_ratio(get_vnc(), enable);
#else
(void)enable;
#endif
}
void Vnc::DisplayWindow::set_keep_aspect_ratio(bool enable)
{
on_set_keep_aspect_ratio(enable);
#if VNC_CHECK_VERSION(1, 2, 0)
m_keep_ratio->set_active(enable);
#endif
}
bool Vnc::DisplayWindow::get_keep_aspect_ratio()
{
#if VNC_CHECK_VERSION(1, 2, 0)
return static_cast<bool>(vnc_display_get_keep_aspect_ratio(get_vnc()));
#else
return false;
#endif
}
void Vnc::DisplayWindow::set_shared_flag(bool enable)
{
vnc_display_set_shared_flag(get_vnc(), enable);
}
bool Vnc::DisplayWindow::get_shared_flag()
{
return static_cast<bool>(vnc_display_get_shared_flag(get_vnc()));
}
void Vnc::DisplayWindow::set_depth(VncDisplayDepthColor depth)
{
vnc_display_set_depth(get_vnc(), depth);
}
VncDisplayDepthColor Vnc::DisplayWindow::get_depth()
{
return vnc_display_get_depth(get_vnc());
}
void Vnc::DisplayWindow::force_grab(bool enable)
{
vnc_display_force_grab(get_vnc(), enable);
}
bool Vnc::DisplayWindow::is_pointer_absolute()
{
return static_cast<bool>(vnc_display_is_pointer_absolute(get_vnc()));
}
Glib::OptionGroup &Vnc::DisplayWindow::option_group()
{
static Glib::OptionGroup s_option_group(vnc_display_get_option_group());
return s_option_group;
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_connected =
{
"vnc-connected",
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback,
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback
};
Glib::SignalProxy0<void> Vnc::DisplayWindow::signal_vnc_connected()
{
return {m_vnc, &VncDisplay_signal_vnc_connected};
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_initialized =
{
"vnc-initialized",
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback,
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback
};
Glib::SignalProxy0<void> Vnc::DisplayWindow::signal_vnc_initialized()
{
return {m_vnc, &VncDisplay_signal_vnc_initialized};
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_disconnected =
{
"vnc-disconnected",
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback,
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback
};
Glib::SignalProxy0<void> Vnc::DisplayWindow::signal_vnc_disconnected()
{
return {m_vnc, &VncDisplay_signal_vnc_disconnected};
}
static void VncDisplay_signal_vnc_error_callback(GtkWidget *self,
const gchar *message,
void *data)
{
using SlotType = sigc::slot<void, const Glib::ustring &>;
auto obj = dynamic_cast<Gtk::Widget *>(Glib::ObjectBase::_get_current_wrapper((GObject *)self));
if (obj) {
try {
if (const auto slot = Glib::SignalProxyNormal::data_to_slot(data))
(*static_cast<SlotType *>(slot))(message ? Glib::ustring(message) : Glib::ustring());
} catch (...) {
Glib::exception_handlers_invoke();
}
}
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_error =
{
"vnc-error",
(GCallback) &VncDisplay_signal_vnc_error_callback,
(GCallback) &VncDisplay_signal_vnc_error_callback
};
Glib::SignalProxy1<void, const Glib::ustring &> Vnc::DisplayWindow::signal_vnc_error()
{
return {m_vnc, &VncDisplay_signal_vnc_error};
}
static void VncDisplay_signal_vnc_auth_credential_callback(GtkWidget *self,
GValueArray *creds,
void *data)
{
using SlotType = sigc::slot<void, const std::vector<VncDisplayCredential> &>;
auto obj = dynamic_cast<Gtk::Widget *>(Glib::ObjectBase::_get_current_wrapper((GObject *)self));
if (obj) {
try {
std::vector<VncDisplayCredential> creds_vec;
if (creds) {
creds_vec.resize(creds->n_values);
for (guint i = 0; i < creds->n_values; ++i) {
GValue *cred = g_value_array_get_nth(creds, i);
creds_vec[i] = static_cast<VncDisplayCredential>(g_value_get_enum(cred));
}
}
if (const auto slot = Glib::SignalProxyNormal::data_to_slot(data))
(*static_cast<SlotType *>(slot))(creds_vec);
} catch (...) {
Glib::exception_handlers_invoke();
}
}
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_auth_credential =
{
"vnc-auth-credential",
(GCallback) &VncDisplay_signal_vnc_auth_credential_callback,
(GCallback) &VncDisplay_signal_vnc_auth_credential_callback
};
Glib::SignalProxy1<void, const std::vector<VncDisplayCredential> &>
Vnc::DisplayWindow::signal_vnc_auth_credential()
{
return {m_vnc, &VncDisplay_signal_vnc_auth_credential};
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_pointer_grab =
{
"vnc-pointer-grab",
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback,
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback
};
Glib::SignalProxy0<void> Vnc::DisplayWindow::signal_vnc_pointer_grab()
{
return {m_vnc, &VncDisplay_signal_vnc_pointer_grab};
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_pointer_ungrab =
{
"vnc-pointer-ungrab",
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback,
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback
};
Glib::SignalProxy0<void> Vnc::DisplayWindow::signal_vnc_pointer_ungrab()
{
return {m_vnc, &VncDisplay_signal_vnc_pointer_ungrab};
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_keyboard_grab =
{
"vnc-keyboard-grab",
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback,
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback
};
Glib::SignalProxy0<void> Vnc::DisplayWindow::signal_vnc_keyboard_grab()
{
return {m_vnc, &VncDisplay_signal_vnc_keyboard_grab};
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_keyboard_ungrab =
{
"vnc-keyboard-ungrab",
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback,
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback
};
Glib::SignalProxy0<void> Vnc::DisplayWindow::signal_vnc_keyboard_ungrab()
{
return {m_vnc, &VncDisplay_signal_vnc_keyboard_ungrab};
}
static void VncDisplay_signal_vnc_desktop_resize_callback(GtkWidget *self,
gint width, gint height,
void *data)
{
using SlotType = sigc::slot<void, gint, gint>;
auto obj = dynamic_cast<Gtk::Widget *>(Glib::ObjectBase::_get_current_wrapper((GObject *)self));
if (obj) {
try {
if (const auto slot = Glib::SignalProxyNormal::data_to_slot(data))
(*static_cast<SlotType *>(slot))(width, height);
} catch (...) {
Glib::exception_handlers_invoke();
}
}
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_desktop_resize =
{
"vnc-desktop-resize",
(GCallback) &VncDisplay_signal_vnc_desktop_resize_callback,
(GCallback) &VncDisplay_signal_vnc_desktop_resize_callback
};
Glib::SignalProxy2<void, gint, gint> Vnc::DisplayWindow::signal_vnc_desktop_resize()
{
return {m_vnc, &VncDisplay_signal_vnc_desktop_resize};
}
static void VncDisplay_signal_vnc_auth_failure_callback(GtkWidget *self,
const gchar *message,
void *data)
{
using SlotType = sigc::slot<void, const Glib::ustring &>;
auto obj = dynamic_cast<Gtk::Widget *>(Glib::ObjectBase::_get_current_wrapper((GObject *)self));
if (obj) {
try {
if (const auto slot = Glib::SignalProxyNormal::data_to_slot(data))
(*static_cast<SlotType *>(slot))(message ? Glib::ustring(message) : Glib::ustring());
} catch (...) {
Glib::exception_handlers_invoke();
}
}
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_auth_failure =
{
"vnc-auth-failure",
(GCallback) &VncDisplay_signal_vnc_auth_failure_callback,
(GCallback) &VncDisplay_signal_vnc_auth_failure_callback
};
Glib::SignalProxy1<void, const Glib::ustring &> Vnc::DisplayWindow::signal_vnc_auth_failure()
{
return {m_vnc, &VncDisplay_signal_vnc_auth_failure};
}
static void VncDisplay_signal_vnc_auth_unsupported_callback(GtkWidget *self,
guint authType,
void *data)
{
using SlotType = sigc::slot<void, guint>;
auto obj = dynamic_cast<Gtk::Widget *>(Glib::ObjectBase::_get_current_wrapper((GObject *)self));
if (obj) {
try {
if (const auto slot = Glib::SignalProxyNormal::data_to_slot(data))
(*static_cast<SlotType *>(slot))(authType);
} catch (...) {
Glib::exception_handlers_invoke();
}
}
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_auth_unsupported =
{
"vnc-auth-unsupported",
(GCallback) &VncDisplay_signal_vnc_auth_unsupported_callback,
(GCallback) &VncDisplay_signal_vnc_auth_unsupported_callback
};
Glib::SignalProxy1<void, guint> Vnc::DisplayWindow::signal_vnc_auth_unsupported()
{
return {m_vnc, &VncDisplay_signal_vnc_auth_unsupported};
}
static void VncDisplay_signal_vnc_server_cut_text_callback(GtkWidget *self,
const gchar *text,
void *data)
{
using SlotType = sigc::slot<void, const std::string &>;
auto obj = dynamic_cast<Gtk::Widget *>(Glib::ObjectBase::_get_current_wrapper((GObject *)self));
if (obj) {
try {
if (const auto slot = Glib::SignalProxyNormal::data_to_slot(data))
(*static_cast<SlotType *>(slot))(text ? std::string(text) : std::string());
} catch (...) {
Glib::exception_handlers_invoke();
}
}
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_server_cut_text =
{
"vnc-server-cut-text",
(GCallback) &VncDisplay_signal_vnc_server_cut_text_callback,
(GCallback) &VncDisplay_signal_vnc_server_cut_text_callback
};
Glib::SignalProxy1<void, const std::string &> Vnc::DisplayWindow::signal_vnc_server_cut_text()
{
return {m_vnc, &VncDisplay_signal_vnc_server_cut_text};
}
static const Glib::SignalProxyInfo VncDisplay_signal_vnc_bell =
{
"vnc-bell",
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback,
(GCallback) &Glib::SignalProxyNormal::slot0_void_callback
};
Glib::SignalProxy0<void> Vnc::DisplayWindow::signal_vnc_bell()
{
return {m_vnc, &VncDisplay_signal_vnc_bell};
}
void Vnc::DisplayWindow::set_capture_keyboard(bool enable)
{
if (enable)
disable_modifiers();
else
enable_modifiers();
set_keyboard_grab(enable);
m_capture_keyboard->set_active(enable);
}
bool Vnc::DisplayWindow::get_capture_keyboard()
{
return m_capture_keyboard->get_active();
}
void Vnc::DisplayWindow::activate_menubar()
{
if (m_hide_menubar->get_active()) {
m_hide_menubar->set_active(false);
toggle_menubar();
}
m_menubar->select_first();
}
VncDisplay *Vnc::DisplayWindow::get_vnc()
{
init_vnc();
return VNC_DISPLAY(m_vnc->gobj());
}
void Vnc::DisplayWindow::init_vnc()
{
if (m_vnc)
return;
m_vnc = Glib::wrap(vnc_display_new());
m_viewport->remove();
m_viewport->add(*m_vnc);
signal_vnc_connected().connect([this]() { m_connected = true; });
signal_vnc_initialized().connect(sigc::mem_fun(this, &DisplayWindow::vnc_initialized));
signal_vnc_disconnected().connect([this]() {
handle_disconnect("VNC connection lost",
"Could not open VNC connection");
});
signal_vnc_error().connect([this](const Glib::ustring &message) {
auto text = Glib::ustring::compose("VNC Error: %1", message);
handle_disconnect(text, text);
});
signal_vnc_auth_credential().connect(sigc::mem_fun(this, &DisplayWindow::vnc_credential));
signal_vnc_auth_failure().connect([this](const Glib::ustring &message) {
auto text = Glib::ustring::compose("VNC Authentication failed: %1", message);
Gtk::MessageDialog dialog(*this, text, false, Gtk::MESSAGE_ERROR);
(void)dialog.run();
get_application()->quit();
});
signal_vnc_desktop_resize().connect([this](gint width, gint height) {
m_remote_size.width = width;
m_remote_size.height = height;
if (m_hide_menubar->get_active())
resize(width, height);
else
resize(width, height + m_menubar->get_height());
update_scrolling();
});
signal_vnc_pointer_grab().connect([this]() { update_title(true); });
signal_vnc_pointer_ungrab().connect([this]() { update_title(false); });
auto clipboard = Gtk::Clipboard::get();
signal_vnc_server_cut_text().connect(sigc::mem_fun(this, &DisplayWindow::remote_clipboard_text));
static bool clipboard_connected = false;
if (!clipboard_connected) {
clipboard->signal_owner_change().connect([this, clipboard](GdkEventOwnerChange *) {
clipboard->request_contents("UTF8_STRING",
sigc::mem_fun(this, &DisplayWindow::clipboard_text_received));
});
clipboard_connected = true;
}
}
void Vnc::DisplayWindow::handle_disconnect(const Glib::ustring &connected_msg,
const Glib::ustring &disconnected_msg)
{
m_signal_connection_lost.emit();
if (m_connected) {
int result = Gtk::RESPONSE_CANCEL;
{
Gtk::MessageDialog dialog(*this, connected_msg, false,
Gtk::MESSAGE_ERROR, Gtk::BUTTONS_NONE);
dialog.add_button("Reconnect", Gtk::RESPONSE_YES);
dialog.add_button("Close", Gtk::RESPONSE_CANCEL);
dialog.set_default_response(Gtk::RESPONSE_YES);
result = dialog.run();
}
delete m_vnc;
m_vnc = nullptr;
m_connected = false;
if (result == Gtk::RESPONSE_YES)
m_signal_reconnect.emit();
else
get_application()->quit();
} else {
Gtk::MessageDialog dialog(*this, disconnected_msg, false,
Gtk::MESSAGE_ERROR);
(void)dialog.run();
get_application()->quit();
}
}
static Glib::ustring gen_screenshot_name(const Glib::ustring &name)
{
char time_buf[64];
time_t now = time(nullptr);
struct tm *now_tm = localtime(&now);
strftime(time_buf, 64, "%Y-%m-%d_%H%M%S", now_tm);
std::string clean_name = name;
std::replace_if(clean_name.begin(), clean_name.end(), [](char ch) -> bool {
return (ch == ':' || ch == '\\' || ch == '/' || ch == '<' || ch == '>'