-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnote-toggle-touchpad.py
executable file
·110 lines (92 loc) · 2.82 KB
/
note-toggle-touchpad.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
#! /usr/bin/python3
"""
Copyright (c) 2018, 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
import notify2 # type: ignore
List, Optional, Text # for mypy
cmd_xinput = "xinput"
icon1 = "notification-device-firewire"
icon2 = "notification-device-eject"
icon3 = "notification-network-wireless-disconnected"
fNotify = True # use notify or only stdout
nXdev = "0"
nXprop = "0"
def check_status():
# type: () -> Optional[bool]
global nXdev, nXprop
rex_touchpad = r"Touchpad\s+id=([0-9]+)"
rex_xprop = r"Device Enabled\s*\(([0-9]+)\)\s*:\s+([0-9]+)"
try:
_src = sp.check_output([cmd_xinput, "list"])
src = _src.decode("utf-8")
except:
return None
for line in src.splitlines():
mo = re.search(rex_touchpad, line)
if mo is None:
continue
_id = nXdev = mo.group(1)
break
else:
return None
try:
_src = sp.check_output([cmd_xinput, "list-props", _id])
src = _src.decode("utf-8")
except:
return None
for line in src.splitlines():
mo = re.search(rex_xprop, line)
if mo is None:
continue
nXprop = mo.group(1)
val = mo.group(2)
return val == "1"
return None
def notify(fIcon, msg):
# type: (Optional[bool], Text) -> None # type: ignore
if fIcon is None:
icon = icon3
elif fIcon:
icon = icon1
else:
icon = icon2
print("notify: " + msg)
if not fNotify:
return
notify2.init("script")
nt = notify2.Notification("openbox", msg,
icon)
nt.show()
def main(args):
# type: (List[Text]) -> None # type: ignore
if "-d" in args:
global fNotify
fNotify = False
ret = check_status()
if ret is None:
return notify(ret, "failed to get touchpad status")
if "-n" in args:
s = "on" if ret else "off"
return notify(ret, "touchpad status: " + s)
# ret = if check_status()
s = "0" if ret else "1" # toggle
try:
stat = sp.check_call([cmd_xinput, "set-prop", nXdev,
"--type=int", nXprop, s])
except sp.CalledProcessError as ex:
stat = ex.returncode
if stat != 0:
return notify(ret, "touchpad changing was failed: " + str(stat))
s = "off" if ret else "on"
return notify(ret, "touchpad toggled to: " + s)
if __name__ == "__main__":
main(sys.argv[1:])
# vi: ft=python:nowrap:et:ts=4:tw=80:fdm=marker