Coverage for /home/antoine/projects/xpra-git/dist/python3/lib64/python/xpra/x11/fakeXinerama.py : 77%
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# -*- coding: utf-8 -*-
3# This file is part of Xpra.
4# Copyright (C) 2014-2019 Antoine Martin <antoine@xpra.org>
5# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
6# later version. See the file COPYING for details.
8import os
10from xpra.util import prettify_plug_name
11from xpra.os_util import find_lib, find_lib_ldconfig, LINUX, POSIX
12from xpra.version_util import XPRA_VERSION
13from xpra.log import Logger
15log = Logger("x11", "server", "util")
17fakeXinerama_config_files = [
18 #the new fakexinerama file:
19 os.path.expanduser("~/.%s-fakexinerama" % os.environ.get("DISPLAY")),
20 #compat file for "old" version found on github:
21 os.path.expanduser("~/.fakexinerama"),
22 ]
24def find_libfakeXinerama():
25 libname = "fakeXinerama"
26 try:
27 from ctypes.util import find_library
28 flibname = find_library("fakeXinerama")
29 if flibname:
30 libname = flibname
31 except Exception:
32 pass
33 if POSIX:
34 for lib_dir in os.environ.get("LD_LIBRARY_PATH", "/usr/lib").split(os.pathsep):
35 lib_path = os.path.join(lib_dir, libname)
36 if not os.path.exists(lib_dir):
37 continue
38 if os.path.exists(lib_path) and os.path.isfile(lib_path):
39 return lib_path
40 if LINUX:
41 try:
42 libpath = find_lib_ldconfig("fakeXinerama")
43 if libpath:
44 return libpath
45 except Exception as e:
46 log("find_libfakeXinerama()", exc_info=True)
47 log.error("Error: cannot launch ldconfig -p to locate libfakeXinerama:")
48 log.error(" %s", e)
49 return find_lib(libname)
51current_xinerama_config = None
53def save_fakeXinerama_config(supported=True, source="", ss=()):
54 """ returns True if the fakexinerama config was modified """
55 global current_xinerama_config
56 def delfile(msg):
57 global current_xinerama_config
58 if msg:
59 log.warn(msg)
60 cleanup_fakeXinerama()
61 oldconf = current_xinerama_config
62 current_xinerama_config = None
63 return oldconf is not None
64 if not supported:
65 return delfile(None)
66 if not ss:
67 return delfile("cannot save fake xinerama settings: no display found")
68 if len(ss)>1:
69 return delfile("cannot save fake xinerama settings: more than one display found")
70 display_info = ss[0]
71 if len(display_info)==2 and isinstance(display_info[0], int) and isinstance(display_info[1], int):
72 #just WxH, not enough display information
73 return delfile("cannot save fake xinerama settings: missing display data from client %s" % source)
74 if len(display_info)<10:
75 return delfile("cannot save fake xinerama settings: incomplete display data from client %s" % source)
76 #display_name, width, height, width_mm, height_mm, \
77 #monitors, work_x, work_y, work_width, work_height = s[:11]
78 monitors = display_info[5]
79 if len(monitors)==0:
80 return delfile("cannot save fake xinerama settings: no monitors!")
81 if len(monitors)>=10:
82 return delfile("cannot save fake xinerama settings: too many monitors! (%s)" % len(monitors))
83 #generate the file data:
84 data = ["# file generated by xpra %s for display %s" % (XPRA_VERSION, os.environ.get("DISPLAY")),
85 "# %s monitors:" % len(monitors),
86 "%s" % len(monitors)]
87 #the new config (numeric values only)
88 config = [len(monitors)]
89 for i, m in enumerate(monitors):
90 if len(m)<7:
91 return delfile("cannot save fake xinerama settings: incomplete monitor data for monitor: %s" % (m, ))
92 plug_name, x, y, width, height, wmm, hmm = m[:7]
93 data.append("# %s (%smm x %smm)" % (prettify_plug_name(plug_name, "monitor %s" % i), wmm, hmm))
94 data.append("%s %s %s %s" % (x, y, width, height))
95 config.append((x, y, width, height))
96 if current_xinerama_config==config:
97 #we assume that no other process is going to overwrite the deprecated .fakexinerama
98 log("fake xinerama config unchanged")
99 return False
100 log("fake xinerama config changed:")
101 log(" old=%s", current_xinerama_config)
102 log(" new=%s", config)
103 current_xinerama_config = config
104 data.append("")
105 contents = "\n".join(data)
106 for filename in fakeXinerama_config_files:
107 try:
108 with open(filename, 'wb') as f:
109 f.write(contents.encode("utf8"))
110 except Exception as e:
111 log("writing to '%s'", filename, exc_info=True)
112 log.warn("Error writing fake xinerama file '%s':", filename)
113 log.warn(" %s", e)
114 log("saved %s monitors to fake xinerama files: %s", len(monitors), fakeXinerama_config_files)
115 return True
117def cleanup_fakeXinerama():
118 for f in fakeXinerama_config_files:
119 try:
120 if os.path.exists(f):
121 log("cleanup_fakexinerama() deleting fake xinerama file '%s'", f)
122 os.unlink(f)
123 except Exception as e:
124 log.error("Error: failed to delete fakexinerama config file")
125 log.error(" '%s': %s", f, e)
128def main():
129 print("libfakeXinerama=%s" % find_libfakeXinerama())
131if __name__ == "__main__":
132 main()