-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
note-toggle-display.py
executable file
·204 lines (170 loc) · 5.5 KB
/
note-toggle-display.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
#! /usr/bin/python3
"""
Copyright (c) 2017, shimoda as kuri65536 _dot_ hot mail _dot_ com
( email address: convert _dot_ to . and joint string )
This Source Code Form is subject to the terms of the Mozilla Public License,
v.2.0. If a copy of the MPL was not distributed with this file,
You can obtain one at https://mozilla.org/MPL/2.0/.
"""
import re
import subprocess as sp
import sys
from typing import List, Optional, Text, Type
import notify2 # type: ignore
List, Optional, Text, Type # for mypy
cmd_xrandr = "xrandr"
icons = ["notification-display-brightness-off",
"notification-display-brightness-full",
]
fNotify = True # use notify or only stdout
class status:
names = [] # type: List[Text]
conns = [] # type: List[int]
actives = [] # type: List[int]
nPrim = -1
@classmethod
def clear(cls):
# type: () -> None
cls.names = []
cls.conns = []
cls.actives = []
cls.nPrim = -1
class status_none(status):
pass
def fetch_status():
# type: () -> Type[status]
rex_screen = r"^(\S+)\s+(dis|)connected"
rex_active = r"\+[0-9]+\+[0-9]+"
try:
_src = sp.check_output([cmd_xrandr, "-q", "--current"])
src = _src.decode("utf-8")
except:
return status_none
status.clear()
for line in src.splitlines():
mo = re.search(rex_screen, line)
if mo is None:
continue
status.names.append(mo.group(1))
status.conns.append(1 if (mo.group(2) != "dis") else 0)
mo = re.search(rex_active, line)
status.actives.append(1 if mo is not None else 0)
if re.search("primary", line):
status.nPrim = len(status.names) - 1
if len(status.names) < 1:
return status_none
return status
def notify(nIcon, msg):
# type: (int, Text) -> None # type: ignore
print("notify: " + msg)
if not fNotify:
return
notify2.init("script")
nt = notify2.Notification("openbox", msg,
icons[nIcon])
nt.show()
def listup(icon, stat, msg): # {{{1
# type: (int, Type[status], Text) -> None
for n, i in enumerate(stat.names):
msg += i + (" (on)" if stat.actives[n] else "") + "\n"
return notify(icon, msg)
def display_on1(stat, n, fPrim):
# type: (Type[status], int, bool) -> List[Text]
ret = [] # type: List[Text]
for i in range(len(stat.names)):
ret += ["--output", stat.names[i]]
if i == n:
cmd = "--auto"
else:
cmd = "--off"
ret.append(cmd)
if fPrim and i == stat.nPrim and cmd != "--off":
ret.append("--primary")
return ret
def display_dups(stat):
# type: (Type[status]) -> List[Text]
ret = [] # type: List[Text]
name = stat.names[stat.nPrim]
for i in range(len(stat.names)):
ret += ["--output", stat.names[i], "--auto"]
if i == stat.nPrim:
ret.append("--primary")
else:
ret += ["--same-as", name]
return ret
def display_exts(stat):
# type: (Type[status]) -> List[Text]
ret = [] # type: List[Text]
name = stat.names[stat.nPrim]
for i in range(len(stat.names)):
ret += ["--output", stat.names[i], "--auto"]
if i == stat.nPrim:
ret.append("--primary")
else:
ret += ["--right-of", name]
return ret
def main(args):
# type: (List[Text]) -> None
if "-d" in args:
global fNotify
fNotify = False
icon = 0
stat = fetch_status()
if stat == status_none:
return notify(0, "failed to get screen status")
# if "-n" in args:
# return listup(stat)
# primary -> double -> secondary -> primary...
cmd = [cmd_xrandr] # type: List[Text]
if "-n" in args:
cmd += ["--dryrun"]
if sum(stat.conns) == 1:
cmd += ["--auto"] # set to active monitor
icon = 0
elif len(stat.conns) == 2:
# swap or dup with option
if sum(stat.actives) > 1:
cmd += display_on1(stat, stat.nPrim, True)
icon = 0
elif "--dups" in args:
cmd += display_dups(stat) # to duplicated
icon = 1
elif "--extends" in args:
cmd += display_exts(stat)
icon = 1
else:
n = stat.actives.index(1)
n = 0 if n == 1 else 1
cmd += display_on1(stat, n, True)
icon = 1
else:
# TODO: debug. did not have this environment.
# give up, rotate monitors among in primary and specified
if sum(stat.actives) > 1:
# to primary
cmd += display_on1(stat, stat.nPrim, True)
icon = 0
elif "--dups" in args:
n = stat.actives.index(1) + 1
n = n if n < len(stat.actives) else 0
cmd += ["--output", stat.names[n], "--same-as",
stat.names[stat.nPrim]]
icon = 1
else:
n = stat.actives.index(1) + 1
n = n if n < len(stat.actives) else 0
cmd += display_on1(stat, n, True)
icon = 1
# ret = if check_status()
try:
print(' '.join(cmd))
st = sp.check_call(cmd)
except sp.CalledProcessError as ex:
st = ex.returncode
if st != 0:
return notify(False, "display changing was failed: " + str(st))
stat = fetch_status()
return listup(icon, stat, "Display switched to: ")
if __name__ == "__main__":
main(sys.argv[1:])
# vi: ft=python:nowrap:et:ts=4:tw=80:fdm=marker