Coverage for /home/antoine/projects/xpra-git/dist/python3/lib64/python/xpra/x11/models/size_hints_util.py : 100%
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# This file is part of Xpra.
2# Copyright (C) 2008, 2009 Nathaniel Smith <njs@pobox.com>
3# Copyright (C) 2011-2019 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.
8from xpra.common import MAX_WINDOW_SIZE
9from xpra.log import Logger
11log = Logger("x11", "window")
13MAX_ASPECT = 2**15-1
16def sanitize_size_hints(size_hints):
17 """
18 Some applications may set nonsensical values,
19 try our best to come up with something that can actually be used.
20 """
21 if not size_hints:
22 return
23 for attr in ("min-aspect", "max-aspect"):
24 v = size_hints.get(attr)
25 if v is not None:
26 try:
27 f = float(v)
28 except ValueError:
29 f = None
30 if f is None or f<=0 or f>=MAX_ASPECT:
31 log.warn("Warning: clearing invalid aspect hint value for %s: %s", attr, v)
32 del size_hints[attr]
33 for attr in ("minimum-aspect-ratio", "maximum-aspect-ratio"):
34 v = size_hints.get(attr)
35 if v is not None:
36 try:
37 f = float(v[0])/float(v[1])
38 except (ValueError, ZeroDivisionError):
39 f = None
40 if f is None or f<=0 or f>=MAX_ASPECT:
41 log.warn("Warning: clearing invalid aspect hint value for %s: %s", attr, v)
42 del size_hints[attr]
43 for attr in ("maximum-size", "minimum-size", "base-size", "increment"):
44 v = size_hints.get(attr)
45 if v is not None:
46 try:
47 w,h = v
48 int(w)
49 int(h)
50 except (ValueError, TypeError):
51 w,h = None,None
52 if (w is None or h is None) or w>=MAX_WINDOW_SIZE or h>=MAX_WINDOW_SIZE:
53 log("clearing invalid size hint value for %s: %s", attr, v)
54 del size_hints[attr]
55 #if max-size is smaller than min-size (bogus), clamp it..
56 mins = size_hints.get("minimum-size")
57 maxs = size_hints.get("maximum-size")
58 if mins is not None and maxs is not None:
59 clamped = False
60 minw,minh = mins
61 maxw,maxh = maxs
62 if minw<0 and minh<0:
63 #doesn't do anything
64 size_hints["minimum-size"] = None
65 clamped = True
66 if maxw<=0 or maxh<=0:
67 #doesn't make sense!
68 size_hints["maximum-size"] = None
69 clamped = True
70 if not clamped:
71 if minw>0 and minw>maxw:
72 #min higher than max!
73 if minw<=256:
74 maxw = minw
75 elif maxw>=256:
76 minw = maxw
77 else:
78 minw = maxw = 256
79 clamped = True
80 if minh>0 and minh>maxh:
81 #min higher than max!
82 if minh<=256:
83 maxh = minh
84 elif maxh>=256:
85 minh = maxh
86 else:
87 minh = maxh = 256
88 clamped = True
89 if clamped:
90 size_hints["minimum-size"] = minw, minh
91 size_hints["maximum-size"] = maxw, maxh
92 if clamped:
93 log.warn("Warning: invalid size hints")
94 log.warn(" min_size=%s / max_size=%s", mins, maxs)
95 log.warn(" changed to: %s / %s", size_hints.get("minimum-size"), size_hints.get("maximum-size"))