-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkpoint.py
279 lines (238 loc) · 8.98 KB
/
tkpoint.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
#
# Tkpoint 0.1 - Python Tkinter Trackpoint Config Tool
#
# nvz < enveezee@gmail.com >
#
rule_file="/etc/udev/rules.d/70-tkpoint.rules"
from os import listdir
from subprocess import PIPE, run
from tkinter import *
# The Tkpoint Application Class
class Tkpoint(Frame):
# Initial setup of the class instance
def __init__(self, master):
# Boilerplate code
super().__init__(master)
self.master = master
self.pack()
# dict of available trackpoints names and paths
self.trackpoints = {}
# Trackpoint settings
self.settings = {
# Descriptions taken from:
# https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-platform-trackpoint
# "bind_mode" : {
# 'default': 'auto',
# 'desc': '',
# 'type': 'str',
# 'values': ['auto'] },
"draghys" : {
'default': 255,
'desc': 'The drag hysteresis controls how hard it is to drag with z-axis pressed.',
'res': 1,
'max': 255,
'type': 'scale' },
"drift_time" : {
'default': 1,
'desc': 'This parameter controls the period of time to test for a hands off condition (i.e. when no force is applied) before a drift (noise) calibration occurs.',
'max': 255,
'res': 1,
'type': 'scale' },
"ext_dev" : {
'default': 0,
'desc': 'Disable (0) or enable (1) external pointing device',
'res': 1,
'max': 1,
'type': 'scale' },
"inertia" : {
'default': 5,
'desc': 'Negative inertia factor. High values cause the cursor to snap backward when the trackpoint is released.',
'res': 1,
'max': 255,
'type': 'scale' },
"jenks" : {
'default': 0,
'desc': 'Minimum curvature in degrees required to generate a double click without a release.',
'res': 1,
'max': 255,
'type': 'scale' },
"mindrag" : {
'default': 20,
'desc': '''Minimum amount of force needed to trigger dragging.''',
'res': 1,
'max': 255,
'type': 'scale' },
"press_to_select" : {
'default': 0,
'desc': '',
'res': 1,
'max': 1,
'type': 'scale' },
"rate" : {
'default': 100,
'desc': '''''',
'res': 1,
'max': 255,
'type': 'scale' },
"reach" : {
'default': 10,
'desc': '''''',
'res': 1,
'max': 255,
'type': 'scale' },
"resetafter" : {
'default': 5,
'desc': '''''',
'res': 1,
'max': 255,
'type': 'scale' },
"resolution" : {
'default': 200,
'desc': '''''',
'res': 1,
'max': 255,
'type': 'scale' },
"resync_time" : {
'default': 0,
'desc': '''''',
'res': 1,
'max': 255,
'type': 'scale' },
"sensitivity" : {
'default': 128,
'desc': '''''',
'res': 1,
'max': 255,
'type': 'scale' },
"skipback" : {
'default': 0,
'desc': '''''',
'res': 1,
'max': 255,
'type': 'scale' },
"speed" : {
'default': 100,
'desc': '''''',
'res': 1,
'max': 255,
'type': 'scale' },
"thresh" : {
'default': 8,
'desc': '''''',
'res': 1,
'max': 255,
'type': 'scale' },
"upthresh" : {
'default': 255,
'desc': '''''',
'res': 1,
'max': 255,
'type': 'scale' },
"ztime" : {
'default': 32,
'desc': '''''',
'res': 1,
'max': 32,
'type': 'scale' }
}
self.detect()
self.create_widgets()
# Handle callbacks on events
def callback(self, event):
# Widget's name
widget = str(event.widget).split('.')[-1]
# If trackpoint listbox was clicked
if widget == "trackpoints":
self.path = self.trackpoints[self.devices.get('active')] + 'device/device/'
# Load trackpoint
self.load()
# If a settings widget was clicked
elif widget in self.settings:
# Set the setting to the widget's value
self.set(widget,event.widget.get())
# Create the GUI widgets for the trackpoint settings
def create_widgets(self):
# Create a listbox for the trackpoints
self.devices = Listbox(self, name='trackpoints', selectmode=SINGLE)
self.devices.grid(row=0)
# Iterate over each trackpoint
for trackpoint in self.trackpoints:
# Insert name into the listbox
self.devices.insert(END, trackpoint)
# Bind event to listbox
self.devices.bind('<ButtonRelease-1>', self.callback)
self.path = self.trackpoints[trackpoint]+'device/device/'
# Initialize grid counters
r = 1
# Iterate over each setting in the settings dict
for setting in self.settings:
# Settings for the widget
widget = self.settings[setting]
# Create text label for the setting widget
Label(self, text=setting).grid(row=r)
# Handle specifics for different widget types
if widget['type'] == 'scale':
res = widget['res']
top = widget['max']
widget['widget'] = Scale(self, from_=0, to=top, orient=HORIZONTAL, resolution=res, name=setting)
# Pack the widget into the GUI
widget['widget'].grid(row=r, column=1)
# Register a event handler and callback function for the widget
widget['widget'].bind('<ButtonRelease-1>', self.callback)
# Increment grid counter
r+=1
self.load()
# Detect trackpoint devices, return a list
def detect(self):
# Get a list of /dev/input/mouse* devices
mice = []
for dev in listdir('/dev/input/'):
if dev[0:5] == 'mouse':
mice.append('/dev/input/' + dev)
# Get udev information for all mice
if mice:
udevinfo = []
for mouse in mice:
r = run(['udevadm','info',mouse], stdout=PIPE)
udevinfo.append(r.stdout.decode('utf-8').split('\n'))
else:
error('No pointing devices detected.')
# Detect trackpoints and their sysfs path
for info in udevinfo:
for line in info:
if line[0:2] == 'P:':
path = '/sys' + line[3:] + '/'
elif line == 'E: ID_INPUT_POINTINGSTICK=1':
with open(path + 'device/name', 'r') as f:
name = f.read()
self.trackpoints[name] = path
if not self.trackpoints:
error('No trackpoint devices detected.')
# Get current value of a trackpoint setting
def get(self, key):
with open(self.path + key, 'r') as f:
return f.read().split('\n')[0]
# Load current settings for a trackpoint
def load(self):
# Iterate over settings
for setting in self.settings:
# Read setting value from sysfs
val = self.get(setting)
# Set value on widget
if self.settings[setting]['type'] == 'scale':
self.settings[setting]['widget'].set(int(val))
# Save trackpoint settings to a udev rule
def save(self):
pass
# Set value of a trackpoint setting
def set(self, key, val):
with open(self.path + key,'w') as f:
f.write(str(val))
def error(msg):
print(msg)
exit(1)
# Instantiate the application class
trackpoint = Tkpoint(master=Tk())
# Trigger the main application loop
trackpoint.mainloop()