This repository has been archived by the owner on Nov 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server_base.py
1452 lines (1266 loc) · 59.9 KB
/
server_base.py
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
# coding=utf8
# This file is part of Xpra.
# Copyright (C) 2011 Serviware (Arthur Huillet, <ahuillet@serviware.com>)
# Copyright (C) 2010-2013 Antoine Martin <antoine@devloop.org.uk>
# Copyright (C) 2008 Nathaniel Smith <njs@pobox.com>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.
import os.path
import sys
import time
from xpra.log import Logger
log = Logger()
from xpra.keyboard.mask import DEFAULT_MODIFIER_MEANINGS
from xpra.server.server_core import ServerCore
from xpra.os_util import thread, get_hex_uuid
from xpra.version_util import add_version_info
from xpra.util import alnum
from xpra.codecs.loader import PREFERED_ENCODING_ORDER, codec_versions, has_codec, get_codec
from xpra.codecs.video_helper import getVideoHelper
if sys.version > '3':
unicode = str #@ReservedAssignment
MAX_CONCURRENT_CONNECTIONS = 20
class ServerBase(ServerCore):
"""
This is the base class for servers.
It provides all the generic functions but is not tied
to a specific backend (X11 or otherwise).
See GTKServerBase/X11ServerBase and other platform specific subclasses.
"""
def __init__(self):
ServerCore.__init__(self)
log("ServerBase.__init__()")
self.init_uuid()
# This must happen early, before loading in windows at least:
self._server_sources = {}
#so clients can store persistent attributes on windows:
self.client_properties = {}
self.supports_mmap = False
self.randr = False
self._window_to_id = {}
self._id_to_window = {}
# Window id 0 is reserved for "not a window"
self._max_window_id = 1
self.default_quality = -1
self.default_min_quality = 0
self.default_speed = -1
self.default_min_speed = 0
self.pulseaudio = False
self.sharing = False
self.bell = False
self.cursors = False
self.default_dpi = 96
self.dpi = 96
self.supports_clipboard = False
self.supports_dbus_proxy = False
self.dbus_helper = None
#encodings:
self.core_encodings = []
self.encodings = []
self.lossless_encodings = []
self.lossless_mode_encodings = []
self.default_encoding = None
self.init_encodings()
self.init_packet_handlers()
self.init_aliases()
def idle_add(self, *args, **kwargs):
raise NotImplementedError()
def timeout_add(self, *args, **kwargs):
raise NotImplementedError()
def source_remove(self, timer):
raise NotImplementedError()
def init(self, opts):
ServerCore.init(self, opts)
log("ServerBase.init(%s)", opts)
self.supports_mmap = opts.mmap
self.init_encoding(opts.encoding)
self.default_quality = opts.quality
self.default_min_quality = opts.min_quality
self.default_speed = opts.speed
self.default_min_speed = opts.min_speed
self.pulseaudio = opts.pulseaudio
self.sharing = opts.sharing
self.bell = opts.bell
self.cursors = opts.cursors
self.default_dpi = int(opts.dpi)
self.dpi = self.default_dpi
self.supports_clipboard = opts.clipboard
self.supports_dbus_proxy = opts.dbus_proxy
log("starting component init")
self.init_clipboard(self.supports_clipboard, opts.clipboard_filter_file)
self.init_keyboard()
self.init_sound(opts.speaker, opts.speaker_codec, opts.microphone, opts.microphone_codec)
self.init_notification_forwarder(opts.notifications)
self.init_dbus_helper()
self.load_existing_windows(opts.system_tray)
if opts.pings:
self.timeout_add(1000, self.send_ping)
else:
self.timeout_add(10*1000, self.send_ping)
thread.start_new_thread(self.threaded_init, ())
def threaded_init(self):
log("threaded_init() start")
#try to load video encoders in advance as this can take some time:
getVideoHelper().may_init()
log("threaded_init() end")
def init_encodings(self):
#core encodings: all the specific encoding formats we can encode:
self.core_encodings = ["rgb24", "rgb32"]
#encodings: the format families we can encode (same as core, except for rgb):
self.encodings = ["rgb"]
def add_encodings(encodings):
for e in encodings:
if e not in self.encodings:
self.encodings.append(e)
if e not in self.core_encodings:
self.core_encodings.append(e)
#video encoders (actual encodings supported are queried):
for codec_name in ("enc_vpx", "enc_x264", "enc_nvenc"):
codec = get_codec(codec_name)
if codec:
#codec.get_type() #ie: "vpx", "x264" or "nvenc"
log("init_encodings() codec %s found, adding: %s", codec.get_type(), codec.get_encodings())
add_encodings(codec.get_encodings()) #ie: ["vp8"] or ["h264"]
for module, encodings in {
"enc_webp" : ["webp"],
"PIL" : ["png", "png/L", "png/P", "jpeg"],
}.items():
if not has_codec(module):
log("init_encodings() codec module %s is missing, not adding: %s", module, encodings)
continue
add_encodings(encodings)
self.lossless_encodings = [x for x in self.core_encodings if (x.startswith("png") or x.startswith("rgb"))]
self.lossless_mode_encodings = []
if has_codec("enc_webp_lossless"):
self.lossless_mode_encodings.append("webp")
self.lossless_encodings.append("webp")
self.default_encoding = [x for x in PREFERED_ENCODING_ORDER if x in self.encodings][0]
def init_encoding(self, cmdline_encoding):
if cmdline_encoding and cmdline_encoding not in self.encodings:
log.warn("ignored invalid default encoding option: %s", cmdline_encoding)
else:
self.default_encoding = cmdline_encoding
def init_uuid(self):
# Define a server UUID if needed:
self.uuid = self.get_uuid()
if not self.uuid:
self.uuid = unicode(get_hex_uuid())
self.save_uuid()
log.info("server uuid is %s", self.uuid)
def get_uuid(self):
return None
def save_uuid(self):
pass
def init_notification_forwarder(self, notifications):
log("init_notification_forwarder(%s)", notifications)
self.notifications_forwarder = None
if notifications and os.name=="posix" and not sys.platform.startswith("darwin"):
try:
from xpra.x11.dbus_notifications_forwarder import register
self.notifications_forwarder = register(self.notify_callback, self.notify_close_callback)
if self.notifications_forwarder:
log.info("using notification forwarder: %s", self.notifications_forwarder)
except Exception, e:
log.error("error loading or registering our dbus notifications forwarder:")
log.error(" %s", e)
log.info("if you do not have a dedicated dbus session for this xpra instance,")
log.info(" you should use the '--no-notifications' flag")
log.info("")
def init_sound(self, speaker, speaker_codec, microphone, microphone_codec):
try:
from xpra.sound.gstreamer_util import has_gst, get_sound_codecs
except Exception, e:
log("cannot load gstreamer: %s", e)
has_gst = False
log("init_sound(%s, %s, %s, %s) has_gst=%s", speaker, speaker_codec, microphone, microphone_codec, has_gst)
self.supports_speaker = bool(speaker) and has_gst
self.supports_microphone = bool(microphone) and has_gst
self.speaker_codecs = speaker_codec
if len(self.speaker_codecs)==0 and self.supports_speaker:
self.speaker_codecs = get_sound_codecs(True, True)
self.supports_speaker = len(self.speaker_codecs)>0
self.microphone_codecs = microphone_codec
if len(self.microphone_codecs)==0 and self.supports_microphone:
self.microphone_codecs = get_sound_codecs(False, False)
self.supports_microphone = len(self.microphone_codecs)>0
try:
from xpra.sound.pulseaudio_util import add_audio_tagging_env
add_audio_tagging_env()
except Exception, e:
log("failed to set pulseaudio audio tagging: %s", e)
def init_clipboard(self, clipboard_enabled, clipboard_filter_file):
log("init_clipboard(%s, %s)", clipboard_enabled, clipboard_filter_file)
### Clipboard handling:
self._clipboard_helper = None
self._clipboard_client = None
self._clipboards = []
if not clipboard_enabled:
return
from xpra.platform.features import CLIPBOARDS
clipboard_filter_res = []
if clipboard_filter_file:
if not os.path.exists(clipboard_filter_file):
log.error("invalid clipboard filter file: '%s' does not exist - clipboard disabled!", clipboard_filter_file)
return
try:
f = open(clipboard_filter_file, "r" )
try:
for line in f:
clipboard_filter_res.append(line.strip())
log("loaded %s regular expressions from clipboard filter file %s", len(clipboard_filter_res), clipboard_filter_file)
finally:
f.close()
except:
log.error("error reading clipboard filter file %s - clipboard disabled!", clipboard_filter_file, exc_info=True)
return
try:
from xpra.clipboard.gdk_clipboard import GDKClipboardProtocolHelper
self._clipboard_helper = GDKClipboardProtocolHelper(self.send_clipboard_packet, self.clipboard_progress, CLIPBOARDS, clipboard_filter_res)
self._clipboards = CLIPBOARDS
except Exception, e:
log.error("failed to setup clipboard helper: %s" % e)
def init_keyboard(self):
log("init_keyboard()")
## These may get set by the client:
self.xkbmap_mod_meanings = {}
self.keyboard_config = None
self.keymap_changing = False #to ignore events when we know we are changing the configuration
self.keyboard_sync = True
self.key_repeat_delay = -1
self.key_repeat_interval = -1
#store list of currently pressed keys
#(using a dict only so we can display their names in debug messages)
self.keys_pressed = {}
self.keys_timedout = {}
#timers for cancelling key repeat when we get jitter
self.keys_repeat_timers = {}
self.watch_keymap_changes()
def watch_keymap_changes(self):
pass
def init_dbus_helper(self):
if not self.supports_dbus_proxy:
return
try:
from xpra.x11.dbus_helper import DBusHelper
self.dbus_helper = DBusHelper()
except Exception, e:
log.warn("cannot load dbus helper: %s", e)
self.supports_dbus_proxy = False
def load_existing_windows(self, system_tray):
pass
def is_shown(self, window):
return True
def init_packet_handlers(self):
ServerCore.init_packet_handlers(self)
self._authenticated_packet_handlers = {
"set-clipboard-enabled": self._process_clipboard_enabled_status,
"set-keyboard-sync-enabled": self._process_keyboard_sync_enabled_status,
"damage-sequence": self._process_damage_sequence,
"ping": self._process_ping,
"ping_echo": self._process_ping_echo,
"set-cursors": self._process_set_cursors,
"set-notify": self._process_set_notify,
"set-bell": self._process_set_bell,
}
self._authenticated_ui_packet_handlers = self._default_packet_handlers.copy()
self._authenticated_ui_packet_handlers.update({
#windows:
"map-window": self._process_map_window,
"unmap-window": self._process_unmap_window,
"configure-window": self._process_configure_window,
"move-window": self._process_move_window,
"resize-window": self._process_resize_window,
"close-window": self._process_close_window,
"focus": self._process_focus,
#keyboard:
"key-action": self._process_key_action,
"key-repeat": self._process_key_repeat,
"layout-changed": self._process_layout,
"keymap-changed": self._process_keymap,
#mouse:
"button-action": self._process_button_action,
"pointer-position": self._process_pointer_position,
#attributes / settings:
"server-settings": self._process_server_settings,
"jpeg-quality": self._process_quality,
"quality": self._process_quality,
"min-quality": self._process_min_quality,
"speed": self._process_speed,
"min-speed": self._process_min_speed,
"set_deflate": self._process_set_deflate,
"desktop_size": self._process_desktop_size,
"encoding": self._process_encoding,
"suspend": self._process_suspend,
"resume": self._process_resume,
#dbus:
"rpc": self._process_rpc,
#sound:
"sound-control": self._process_sound_control,
"sound-data": self._process_sound_data,
#requests:
"shutdown-server": self._process_shutdown_server,
"exit-server": self._process_exit_server,
"buffer-refresh": self._process_buffer_refresh,
"screenshot": self._process_screenshot,
"disconnect": self._process_disconnect,
# Note: "clipboard-*" packets are handled via a special case..
})
def init_aliases(self):
packet_types = list(self._default_packet_handlers.keys())
packet_types += list(self._authenticated_packet_handlers.keys())
packet_types += list(self._authenticated_ui_packet_handlers.keys())
self.do_init_aliases(packet_types)
def cleanup(self, *args):
if self.notifications_forwarder:
thread.start_new_thread(self.notifications_forwarder.release, ())
self.notifications_forwarder = None
ServerCore.cleanup(self)
def add_listen_socket(self, socktype, socket):
raise NotImplementedError()
def _disconnect_all(self, message):
for p in self._potential_protocols:
try:
self.send_disconnect(p, message)
except:
pass
def _process_exit_server(self, proto, packet):
log.info("Exiting response to request")
self._disconnect_all("server exiting")
self.timeout_add(1000, self.clean_quit, False, ServerCore.EXITING_CODE)
def _process_shutdown_server(self, proto, packet):
log.info("Shutting down in response to request")
self._disconnect_all("server shutdown")
self.timeout_add(1000, self.clean_quit)
def force_disconnect(self, proto):
self.cleanup_source(proto)
ServerCore.force_disconnect(self, proto)
def disconnect_protocol(self, protocol, reason):
ServerCore.disconnect_protocol(self, protocol, reason)
self.cleanup_source(protocol)
def cleanup_source(self, protocol):
#this ensures that from now on we ignore any incoming packets coming
#from this connection as these could potentially set some keys pressed, etc
if protocol in self._potential_protocols:
self._potential_protocols.remove(protocol)
source = self._server_sources.get(protocol)
if source:
source.close()
del self._server_sources[protocol]
if self.exit_with_client:
log.info("Last client has disconnected, terminating")
self.quit(0)
else:
log.info("xpra client disconnected.")
return source
def is_timedout(self, protocol):
return ServerCore.is_timedout(self, protocol) and protocol not in self._server_sources
def no_more_clients(self):
#so it is now safe to clear them:
#(this may fail during shutdown - which is ok)
try:
self._clear_keys_pressed()
except:
pass
self._focus(None, 0, [])
def _process_disconnect(self, proto, packet):
self.disconnect_protocol(proto, "on client request")
def _process_connection_lost(self, proto, packet):
log.info("Connection lost")
if self._clipboard_client and self._clipboard_client.protocol==proto:
self._clipboard_client = None
source = self.cleanup_source(proto)
if len(self._server_sources)==0:
self._clear_keys_pressed()
self._focus(source, 0, [])
sys.stdout.flush()
def hello_oked(self, proto, packet, c, auth_caps):
if c.boolget("screenshot_request"):
self.send_screenshot(proto)
return
if c.boolget("info_request", False):
self.send_hello_info(proto)
return
command_req = c.strlistget("command_request")
if len(command_req)>0:
self.handle_command_request(proto, command_req)
return
#"normal" connection, so log welcome message:
log.info("Handshake complete; enabling connection")
# Things are okay, we accept this connection, and may disconnect previous one(s)
share_count = 0
for p,ss in self._server_sources.items():
#check existing sessions are willing to share:
if not self.sharing:
self.disconnect_client(p, "new valid connection received, this session does not allow sharing")
elif not c.boolget("share"):
self.disconnect_client(p, "new valid connection received, the new client does not wish to share")
elif not ss.share:
self.disconnect_client(p, "new valid connection received, this client had not enabled sharing ")
else:
share_count += 1
if share_count>0:
log.info("sharing with %s other session(s)", share_count)
self.dpi = c.intget("dpi", self.default_dpi)
if self.dpi>0:
#some non-posix clients never send us 'resource-manager' settings
#so just use a fake one to ensure the dpi gets applied:
self.update_server_settings({'resource-manager' : ""})
#max packet size from client (the biggest we can get are clipboard packets)
proto.max_packet_size = 1024*1024 #1MB
proto.send_aliases = c.dictget("aliases")
#use blocking sockets from now on:
self.set_socket_timeout(proto._conn, None)
def drop_client(reason="unknown"):
self.disconnect_client(proto, reason)
def get_window_id(wid):
return self._window_to_id.get(wid)
from xpra.server.source import ServerSource
ss = ServerSource(proto, drop_client,
self.idle_add, self.timeout_add, self.source_remove,
self.get_transient_for, self.get_focus,
get_window_id,
self.supports_mmap,
self.compression_level,
self.core_encodings, self.encodings, self.default_encoding,
self.supports_speaker, self.supports_microphone,
self.speaker_codecs, self.microphone_codecs,
self.default_quality, self.default_min_quality,
self.default_speed, self.default_min_speed)
log("process_hello serversource=%s", ss)
ss.parse_hello(c)
self._server_sources[proto] = ss
root_w, root_h = self.set_best_screen_size()
self.calculate_workarea()
#take the clipboard if no-one else has yet:
if ss.clipboard_enabled and self._clipboard_helper is not None and \
(self._clipboard_client is None or self._clipboard_client.is_closed()):
self._clipboard_client = ss
#deal with buggy win32 clipboards:
if "clipboard.greedy" not in c:
#old clients without the flag: take a guess based on platform:
client_platform = c.strget("platform", "")
greedy = client_platform.startswith("win") or client_platform.startswith("darwin")
else:
greedy = c.boolget("clipboard.greedy")
self._clipboard_helper.set_greedy_client(greedy)
want_targets = c.boolget("clipboard.want_targets")
self._clipboard_helper.set_want_targets_client(want_targets)
#the selections the client supports (default to all):
from xpra.platform.features import CLIPBOARDS
client_selections = c.strlistget("clipboard.selections", CLIPBOARDS)
log("process_hello server has clipboards: %s, client supports: %s", self._clipboards, client_selections)
self._clipboard_helper.enable_selections(client_selections)
#so only activate this feature afterwards:
self.keyboard_sync = c.boolget("keyboard_sync", True)
key_repeat = c.intpair("key_repeat")
self.set_keyboard_repeat(key_repeat)
#always clear modifiers before setting a new keymap
ss.make_keymask_match(c.strlistget("modifiers", []))
self.set_keymap(ss)
#send_hello will take care of sending the current and max screen resolutions
self.send_hello(ss, root_w, root_h, key_repeat, auth_caps)
# now we can set the modifiers to match the client
self.send_windows_and_cursors(ss)
ss.startup_complete()
def update_server_settings(self, settings):
log("server settings ignored: ", settings)
def set_keyboard_repeat(self, key_repeat):
pass
def set_keymap(self, ss):
pass
def get_transient_for(self, window):
return None
def send_windows_and_cursors(self, ss):
pass
def sanity_checks(self, proto, c):
server_uuid = c.strget("server_uuid")
if server_uuid:
if server_uuid==self.uuid:
self.send_disconnect(proto, "cannot connect a client running on the same display that the server it connects to is managing - this would create a loop!")
return False
log.warn("This client is running within the Xpra server %s", server_uuid)
return True
def make_hello(self):
capabilities = ServerCore.make_hello(self)
capabilities.update({
"max_desktop_size" : self.get_max_screen_size(),
"clipboards" : self._clipboards,
"notifications" : self.notifications_forwarder is not None,
"bell" : self.bell,
"cursors" : self.cursors,
"dbus_proxy" : self.supports_dbus_proxy,
"toggle_cursors_bell_notify" : True,
"toggle_keyboard_sync" : True,
"window_configure" : True,
"window_unmap" : True,
"xsettings-tuple" : True,
"change-quality" : True,
"change-min-quality" : True,
"change-speed" : True,
"change-min-speed" : True,
"client_window_properties" : True,
"sound_sequence" : True,
"notify-startup-complete" : True,
"suspend-resume" : True,
"encoding.generic" : True,
"exit_server" : True,
"sound.server_driven" : True,
"server_type" : "base",
})
add_version_info(capabilities)
for k,v in codec_versions.items():
capabilities["encoding.%s.version" % k] = v
return capabilities
def get_encoding_info(self):
"""
Warning: the encodings values may get
re-written on the way out.
(see ServerSource.rewrite_encoding_values)
"""
return {
"encodings" : self.encodings,
"encodings.core" : self.core_encodings,
"encodings.lossless" : self.lossless_encodings,
"encodings.with_speed" : [x for x in self.core_encodings if x in ("png", "png/P", "png/L", "jpeg", "h264", "rgb")],
"encodings.with_quality" : [x for x in self.core_encodings if x in ("jpeg", "webp", "h264")],
"encodings.with_lossless_mode" : self.lossless_mode_encodings}
def send_hello(self, server_source, root_w, root_h, key_repeat, server_cipher):
capabilities = self.make_hello()
capabilities.update(self.get_encoding_info())
capabilities.update({
"actual_desktop_size" : (root_w, root_h),
"root_window_size" : (root_w, root_h),
"desktop_size" : self._get_desktop_size_capability(server_source, root_w, root_h),
})
if key_repeat:
capabilities.update({
"key_repeat" : key_repeat,
"key_repeat_modifiers" : True})
capabilities["clipboard"] = self._clipboard_helper is not None and self._clipboard_client == server_source
if server_cipher:
capabilities.update(server_cipher)
server_source.hello(capabilities)
def handle_command_request(self, proto, args):
try:
self.do_handle_command_request(proto, args)
except Exception, e:
log.error("error processing command %s", args, exc_info=True)
proto.send_now(("hello", {"command_response" : (127, "error processing command: %s" % e)}))
def do_handle_command_request(self, proto, args):
assert len(args)>0
log("handle_command_request(%s, %s)", proto, args)
command = args[0]
def respond(error=0, response=""):
log("command request response(%s)=%s", command, response)
hello = {"command_response" : (error, response)}
proto.send_now(("hello", hello))
def argn_err(argn):
respond(4, "invalid number of arguments: %s expected" % argn)
def arg_err(n, msg):
respond(5, "invalid argument %s: %s" % (n, msg))
def success():
respond(0, "success")
commands = ("hello",
"compression", "encoder",
"sound-output",
"scaling",
"suspend", "resume", "name",
"client")
if command=="help":
return respond(0, "control supports: %s" % (", ".join(commands)))
if command not in commands:
return respond(6, "invalid command")
if command=="hello":
return respond(0, "hello")
#from here on, we assume the command applies to the
#current client connection, of which there must only be one:
sss = list(self._server_sources.items())
if len(sss)==0:
return respond(2, "no client connected")
elif len(sss)>1:
return respond(3, "more than one client connected")
cproto, csource = sss[0]
def may_forward_client_command(client_command):
if client_command[0] not in csource.control_commands:
log.info("not forwarded to client (not supported)")
return False
csource.send_client_command(*client_command)
return True
log("handle_command_request will apply to client: %s", csource)
if command=="compression":
if len(args)!=2:
return argn_err(2)
compression = args[1].lower()
opts = ("lz4", "zlib")
if compression=="lz4":
cproto.enable_lz4()
may_forward_client_command(["enable_lz4"])
return success()
elif compression=="zlib":
cproto.enable_zlib()
may_forward_client_command(["enable_zlib"])
return success()
return arg_err(1, "must be one of: %s" % (", ".join(opts)))
elif command=="encoder":
if len(args)!=2:
return argn_err(2)
encoder = args[1].lower()
opts = ("bencode", "rencode")
if encoder=="bencode":
cproto.enable_bencode()
may_forward_client_command(["enable_bencode"])
return success()
elif encoder=="rencode":
cproto.enable_rencode()
may_forward_client_command(["enable_rencode"])
return success()
return arg_err(1, "must be one of: %s" % (", ".join(opts)))
elif command=="sound-output":
if len(args)<2:
return argn_err("more than 1")
msg = csource.sound_control(*args[1:])
return respond(0, msg)
elif command=="suspend":
csource.suspend(True, self._id_to_window)
return respond(0, "suspended")
elif command=="resume":
csource.resume(True, self._id_to_window)
return respond(0, "resumed")
elif command=="scaling":
if len(args)!=3:
return argn_err(3)
if args[1]=="*":
wids = csource.window_sources.keys()
else:
try:
wid = int(args[1])
csource.window_sources[wid]
wids = [wid]
except:
return respond(10, "cannot find window id %s" % args[1])
try:
from xpra.server.window_video_source import parse_scaling_value
scaling = parse_scaling_value(args[2])
except:
return respond(11, "invalid scaling value %s" % args[2])
for wid in wids:
window = self._id_to_window.get(wid)
if not window:
continue
ws = csource.window_sources.get(wid)
if ws:
ws.set_scaling(scaling)
csource.refresh(wid, window, {})
return respond(0, "scaling set to %s" % str(scaling))
elif command=="name":
if len(args)!=2:
return argn_err(1)
self.session_name = args[1]
log.info("changed session name: %s", self.session_name)
may_forward_client_command(["name"])
return respond(0, "session name set")
elif command=="client":
if len(args)<2:
return argn_err("at least 2")
client_command = args[1:]
if client_command[0] not in csource.control_commands:
return respond(12, "client does not support control command '%s'" % client_command[0])
csource.send_client_command(*client_command)
return respond(0, "client control command '%s' forwarded" % (client_command[0]))
else:
return respond(9, "internal state error: invalid command '%s'", command)
def send_screenshot(self, proto):
#this is a screenshot request, handle it and disconnect
try:
packet = self.make_screenshot_packet()
if not packet:
self.send_disconnect(proto, "screenshot failed")
return
proto.send_now(packet)
self.timeout_add(5*1000, self.send_disconnect, proto, "screenshot sent")
except Exception, e:
log.error("failed to capture screenshot", exc_info=True)
self.send_disconnect(proto, "screenshot failed: %s" % e)
def send_hello_info(self, proto):
log.info("processing info request from %s", proto._conn)
self.get_all_info(self.do_send_info, proto, self._id_to_window.keys())
def get_ui_info(self, proto, wids, *args):
""" info that must be collected from the UI thread
(ie: things that query the display)
"""
info = {"server.max_desktop_size" : self.get_max_screen_size()}
#window info:
self.add_windows_info(info, wids)
return info
def get_info(self, proto, client_uuids=None, wids=None, *args):
start = time.time()
info = ServerCore.get_info(self, proto)
if client_uuids:
sources = [ss for ss in self._server_sources.values() if ss.uuid in client_uuids]
else:
sources = self._server_sources.values()
if not wids:
wids = self._id_to_window.keys()
log("info-request: sources=%s, wids=%s", sources, wids)
ei = self.do_get_info(proto, sources, wids)
info.update(ei)
log("get_info took %.1fms", 1000.0*(time.time()-start))
return info
def do_get_info(self, proto, server_sources=None, window_ids=None):
info = {
"features.randr" : self.randr,
"features.cursors" : self.cursors,
"features.bell" : self.bell,
"features.notifications" : self.notifications_forwarder is not None,
"features.pulseaudio" : self.pulseaudio,
"features.dbus_proxy" : self.supports_dbus_proxy,
"features.clipboard" : self.supports_clipboard}
if self._clipboard_helper is not None:
for k,v in self._clipboard_helper.get_info().items():
info["clipboard.%s" % k] = v
info.update(self.get_encoding_info())
for k,v in codec_versions.items():
info["encoding.%s.version" % k] = v
info["windows"] = len([window for window in list(self._id_to_window.values()) if window.is_managed()])
info.update({
"keyboard.sync" : self.keyboard_sync,
"keyboard.repeat.delay" : self.key_repeat_delay,
"keyboard.repeat.interval" : self.key_repeat_interval,
"keyboard.keys_pressed" : self.keys_pressed.values(),
"keyboard.modifiers" : self.xkbmap_mod_meanings})
if self.keyboard_config:
for k,v in self.keyboard_config.get_info().items():
if v is not None:
info["keyboard."+k] = v
# csc and video encoders:
info.update(getVideoHelper().get_info())
# other clients:
info["clients"] = len([p for p in self._server_sources.keys() if p!=proto])
info["clients.unauthenticated"] = len([p for p in self._potential_protocols if ((p is not proto) and (p not in self._server_sources.keys()))])
#find the source to report on:
n = len(server_sources)
if n==1:
ss = server_sources[0]
ss.add_info(info)
ss.add_stats(info, window_ids)
elif n>1:
i = 0
for ss in server_sources:
ss.add_info(info, suffix="{%s}" % i)
ss.add_stats(info, window_ids, suffix="{%s}" % i)
i += 1
return info
def add_windows_info(self, info, window_ids):
for wid, window in self._id_to_window.items():
if wid not in window_ids:
continue
for k,v in self.get_window_info(window).items():
wp = "window[%s]." % wid
info[wp + k] = v
def get_window_info(self, window):
from xpra.server.source import make_window_metadata
info = {}
for prop in window.get_property_names():
if prop=="icon" or prop is None:
continue
metadata = make_window_metadata(window, prop,
generic_window_types=True, client_supports_png=False,
get_transient_for=self.get_transient_for)
info.update(metadata)
if "size-constraints" in info:
size_constraints = info["size-constraints"]
del info["size-constraints"]
for k,v in size_constraints.items():
info["size-constraints.%s" % k] = v
info.update({
"override-redirect" : window.is_OR(),
"tray" : window.is_tray(),
"size" : window.get_dimensions(),
"position" : window.get_position()})
return info
def clipboard_progress(self, local_requests, remote_requests):
assert self._clipboard_helper is not None
if self._clipboard_client and self._clipboard_client.clipboard_notifications:
log("sending clipboard-pending-requests=%s to %s", local_requests, self._clipboard_client)
self._clipboard_client.send("clipboard-pending-requests", local_requests)
def send_clipboard_packet(self, *parts):
assert self._clipboard_helper is not None
if self._clipboard_client:
self._clipboard_client.send(*parts)
def notify_callback(self, dbus_id, nid, app_name, replaces_nid, app_icon, summary, body, expire_timeout):
assert self.notifications_forwarder
log("notify_callback(%s,%s,%s,%s,%s,%s,%s,%s)", dbus_id, nid, app_name, replaces_nid, app_icon, summary, body, expire_timeout)
for ss in self._server_sources.values():
ss.notify(dbus_id, int(nid), str(app_name), int(replaces_nid), str(app_icon), str(summary), str(body), int(expire_timeout))
def notify_close_callback(self, nid):
assert self.notifications_forwarder
log("notify_close_callback(%s)", nid)
for ss in self._server_sources.values():
ss.notify_close(int(nid))
def _keys_changed(self, *args):
if not self.keymap_changing:
for ss in self._server_sources.values():
ss.keys_changed()
def _clear_keys_pressed(self):
pass
def _focus(self, server_source, wid, modifiers):
log("_focus(%s,%s)", wid, modifiers)
def get_focus(self):
#can be overriden by subclasses that do manage focus
#(ie: not shadow servers which only have a single window)
#default: no focus
return -1
def _add_new_window_common(self, window):
wid = self._max_window_id
self._max_window_id += 1
self._window_to_id[window] = wid
self._id_to_window[wid] = window
return wid
def _do_send_new_window_packet(self, ptype, window, geometry):
wid = self._window_to_id[window]
x, y, w, h = geometry
for ss in self._server_sources.values():
wprops = self.client_properties.get("%s|%s" % (wid, ss.uuid))
ss.new_window(ptype, wid, window, x, y, w, h, wprops)
def _screen_size_changed(self, *args):
log("_screen_size_changed(%s)", args)
#randr has resized the screen, tell the client (if it supports it)
self.calculate_workarea()
self.idle_add(self.send_updated_screen_size)
def get_root_window_size(self):
raise NotImplementedError()
def send_updated_screen_size(self):
max_w, max_h = self.get_max_screen_size()
root_w, root_h = self.get_root_window_size()
log.info("sending updated screen size to clients: %sx%s (max %sx%s)", root_w, root_h, max_w, max_h)
for ss in self._server_sources.values():
ss.updated_desktop_size(root_w, root_h, max_w, max_h)
def get_max_screen_size(self):
max_w, max_h = self.get_root_window_size()
return max_w, max_h
def _get_desktop_size_capability(self, server_source, root_w, root_h):
client_size = server_source.desktop_size
log("client resolution is %s, current server resolution is %sx%s", client_size, root_w, root_h)
if not client_size:
""" client did not specify size, just return what we have """
return root_w, root_h
client_w, client_h = client_size
w = min(client_w, root_w)
h = min(client_h, root_h)
return w, h
def set_best_screen_size(self):
root_w, root_h = self.get_root_window_size()
return root_w, root_h
def _process_desktop_size(self, proto, packet):
width, height = packet[1:3]
ss = self._server_sources.get(proto)
if ss is None:
return
log("client requesting new size: %sx%s", width, height)
self.set_screen_size(width, height)
if len(packet)>=4:
ss.set_screen_sizes(packet[3])
self.calculate_workarea()
def calculate_workarea(self):
raise NotImplementedError()
def set_workarea(self, workarea):
pass
def _process_encoding(self, proto, packet):
encoding = packet[1]
ss = self._server_sources.get(proto)
if ss is None:
return
if len(packet)>=3:
#client specified which windows this is for:
in_wids = packet[2]
wids = []
wid_windows = {}
for wid in in_wids: