Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python 

2# This file is part of Xpra. 

3# Copyright (C) 2016-2020 Antoine Martin <antoine@xpra.org> 

4# Xpra is released under the terms of the GNU GPL v2, or, at your option, any 

5# later version. See the file COPYING for details. 

6 

7import os 

8from subprocess import Popen, PIPE 

9 

10from xpra.os_util import POSIX, bytestostr 

11from xpra.scripts.server import _get_int, _get_str, _save_int, _save_str 

12from xpra.scripts.config import FALSE_OPTIONS 

13from xpra.log import Logger 

14 

15log = Logger("dbus") 

16 

17 

18def start_dbus(dbus_launch): 

19 if not dbus_launch or dbus_launch.lower() in FALSE_OPTIONS: 

20 log("start_dbus(%s) disabled", dbus_launch) 

21 return 0, {} 

22 bus_address = os.environ.get("DBUS_SESSION_BUS_ADDRESS") 

23 log("dbus_launch=%r, current DBUS_SESSION_BUS_ADDRESS=%s", dbus_launch, bus_address) 

24 if bus_address: 

25 log("start_dbus(%s) disabled, found an existing DBUS_SESSION_BUS_ADDRESS=%s", dbus_launch, bus_address) 

26 return 0, {} 

27 assert POSIX 

28 try: 

29 env = dict((k,v) for k,v in os.environ.items() if k in ( 

30 "PATH", 

31 "SSH_CLIENT", "SSH_CONNECTION", 

32 "XDG_CURRENT_DESKTOP", "XDG_SESSION_TYPE", "XDG_RUNTIME_DIR", 

33 "SHELL", "LANG", "USER", "LOGNAME", "HOME", 

34 "DISPLAY", "XAUTHORITY", "CKCON_X11_DISPLAY", 

35 )) 

36 import shlex 

37 cmd = shlex.split(dbus_launch) 

38 log("start_dbus(%s) env=%s", dbus_launch, env) 

39 proc = Popen(cmd, stdin=PIPE, stdout=PIPE, env=env, start_new_session=True) 

40 out = proc.communicate()[0] 

41 assert proc.poll()==0, "exit code is %s" % proc.poll() 

42 #parse and add to global env: 

43 dbus_env = {} 

44 log("out(%s)=%r", cmd, out) 

45 for l in bytestostr(out).splitlines(): 

46 if l.startswith("export "): 

47 continue 

48 sep = "=" 

49 if l.startswith("setenv "): 

50 l = l[len("setenv "):] 

51 sep = " " 

52 if l.startswith("set "): 

53 l = l[len("set "):] 

54 parts = l.split(sep, 1) 

55 if len(parts)!=2: 

56 continue 

57 k,v = parts 

58 if v.startswith("'") and v.endswith("';"): 

59 v = v[1:-2] 

60 elif v.endswith(";"): 

61 v = v[:-1] 

62 dbus_env[k] = v 

63 dbus_pid = int(dbus_env.get("DBUS_SESSION_BUS_PID", 0)) 

64 log("dbus_pid=%i, dbus-env=%s", dbus_pid, dbus_env) 

65 return dbus_pid, dbus_env 

66 except Exception as e: 

67 log("start_dbus(%s)", dbus_launch, exc_info=True) 

68 log.error("dbus-launch failed to start using command '%s':\n" % dbus_launch) 

69 log.error(" %s\n" % e) 

70 return 0, {} 

71 

72 

73def save_dbus_pid(pid): 

74 _save_int(b"_XPRA_DBUS_PID", pid) 

75 

76def get_saved_dbus_pid(): 

77 return _get_int(b"_XPRA_DBUS_PID") 

78 

79def get_saved_dbus_env(): 

80 env = {} 

81 for n,load in ( 

82 ("ADDRESS", _get_str), 

83 ("PID", _get_int), 

84 ("WINDOW_ID", _get_int)): 

85 k = "DBUS_SESSION_BUS_%s" % n 

86 try: 

87 v = load(k) 

88 if v: 

89 env[k] = bytestostr(v) 

90 except Exception as e: 

91 log.error("failed to load dbus environment variable '%s':\n" % k) 

92 log.error(" %s\n" % e) 

93 return env 

94 

95def save_dbus_env(env): 

96 #DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-B8CDeWmam9,guid=b77f682bd8b57a5cc02f870556cbe9e9 

97 #DBUS_SESSION_BUS_PID=11406 

98 #DBUS_SESSION_BUS_WINDOWID=50331649 

99 def u(s): 

100 try: 

101 return s.decode("latin1") 

102 except Exception: 

103 return str(s) 

104 for n,conv,save in ( 

105 ("ADDRESS", u, _save_str), 

106 ("PID", int, _save_int), 

107 ("WINDOW_ID", int, _save_int)): 

108 k = "DBUS_SESSION_BUS_%s" % n 

109 v = env.get(k) 

110 if v is None: 

111 continue 

112 try: 

113 tv = conv(v) 

114 save(k, tv) 

115 except Exception as e: 

116 log("save_dbus_env(%s)", env, exc_info=True) 

117 log.error("failed to save dbus environment variable '%s' with value '%s':\n" % (k, v)) 

118 log.error(" %s\n" % e)