-
Notifications
You must be signed in to change notification settings - Fork 12
/
wacom_utility.py
executable file
·471 lines (423 loc) · 18.8 KB
/
wacom_utility.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/env python2
# Import global modules
# Dep: pygtk python-xml.dom.minidom wacom-tools
import sys
import gtk
import gtk.glade
import os
import gc
from copy import copy
# Import local modules
from wacom_identify import TabletIdClass
from wacom_interface import XSetWacom
import wacom_xorg
from dialogbox import DialogBox
from cairo_framework import Pad
from tablet_capplet import GraphicsTabletApplet
class Main:
def __init__(self):
# Set working directory
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
# Check command line args
for item in sys.argv:
if item == "--configure" or item == "-c":
f1 = open(os.path.expanduser("~/.wacom_utility"), 'r')
data = f1.readlines()
f1.close()
for line in data: # Check that we are running this stuff on startup
if line[0] != "#":
if line.split("=")[0] == "configureonlogin":
self.ConfigureOnLogin = int(line.split("=")[1])
if self.ConfigureOnLogin == 1:
for line in data:
if line.count("=") == 0 and line[0] != "#":
os.popen(line)
sys.exit()
# Set up GTK Window
self.PressureMachine = None
self.Create_Window()
# Identify Wacom Product
self.TabletIdObject = TabletIdClass()
self.xSetWacomObject = XSetWacom()
self.Tablets = self.TabletIdObject.identify()
self.Tablet=None
# Load configuration file
self.SaveConfig = 1
try:
os.stat(os.path.expanduser("~/.wacom_utility"))
except:
f1 = open(os.path.expanduser("~/.wacom_utility"), 'w')
f1.writelines("forcemodel=''\n")
f1.writelines("configureonlogin=1\n")
f1.close()
f1 = open(os.path.expanduser("~/.wacom_utility"), 'r')
data = f1.readlines()
f1.close()
for line in data:
if line[0] != "#":
if line.split("=")[0] == "configureonlogin":
self.ConfigureOnLogin = int(line.split("=")[1])
elif line.split("=")[0] == "forcemodel":
self.Tablets = self.TabletIdObject.identify(line.split("=")[1].replace("\"","").replace("\'","").replace("\n",""))
# If no tablets identified
if len(self.Tablets) == 0:
widget = self.wTree.get_widget("tablet-icon")
file = "images/generic.png"
widget.set_from_file(file)
# Set tablet name
widget = self.wTree.get_widget("tablet-label")
widget.set_label("No graphics tablets detected")
gtk.main()
return
# Check for autostart desktop file, if not present, generate it
try:
os.stat(os.path.expanduser("~/.config/autostart/Wacom_Utility.desktop"))
except:
# Ensure directories .config/autostart exist
try:
os.stat(os.path.expanduser("~/.config"))
try:
os.stat(os.path.expanduser("~/.config/autostart"))
except:
os.system("mkdir ~/.config/autostart")
except:
os.system("mkdir ~/.config")
os.system("mkdir ~/.config/autostart")
f1 = open(os.path.expanduser("~/.config/autostart/Wacom_Utility.desktop"), 'w')
f1.writelines("#!/usr/bin/env xdg-open\n\n")
f1.writelines("[Desktop Entry]\n")
f1.writelines("Version=1.0\n")
f1.writelines("Encoding=UTF-8\n")
f1.writelines("Name=Wacom_Utility\n")
f1.writelines("Type=Application\n")
f1.writelines("Exec=/usr/share/wacom-utility/wacom_utility.py -c\n")
f1.writelines("Terminal=false\n")
f1.writelines("Icon=None\n")
f1.writelines("Comment=Apply tablet settings on boot\n")
f1.writelines("Categories=Utility;\n")
f1.writelines("Hidden=false\n")
f1.writelines("Name[en_CA]=Wacom_Utility.desktop\n")
f1.writelines("Comment[en_CA]=This applies settings on boot\n")
f1.writelines("X-GNOME-Autostart-enabled=true\n")
f1.close()
# Set the first tablet discovered to be the configured device
self.Tablet = self.Tablets[0]
# Configure Window
self.window.set_size_request(600+self.Tablet.GraphicWidth,400)
# Attempt to load custom icon for tablet model
widget = self.wTree.get_widget("tablet-icon")
try:
file = "images/"+self.Tablet.Model+".png"
os.stat(file)
except:
file = "images/generic.png"
widget.set_from_file(file)
# Set tablet name
widget = self.wTree.get_widget("tablet-label")
widget.set_label(self.Tablet.Name)
# Set up treeview list for input devices
devices = self.xSetWacomObject.listInterfaces()
widget = self.wTree.get_widget("input-list")
widget.connect("cursor-changed",self.SelectDevice)
list = gtk.ListStore(str)
col = gtk.TreeViewColumn("Input Device")
widget.append_column(col)
celltext = gtk.CellRendererText()
col.pack_start(celltext)
col.add_attribute(celltext,'text',0)
widget.set_model(list)
for device in devices:
list.append([device])
list.append(["options"])
# Set up selection of modifiers
widget = self.wTree.get_widget("availkeys")
list = gtk.ListStore(str, str)
widget.set_model(list)
data = self.xSetWacomObject.listModifiers()
for item in data:
list.append(item)
celltext = gtk.CellRendererText()
widget.pack_start(celltext)
widget.add_attribute(celltext,'text',1)
widget.set_active(0)
# Set up modify action window
self.ModKeyWindow = self.wTree.get_widget("ModifyKey")
self.ModKeyWindow.set_title("Edit Button Action")
self.ModKeyWindow.set_transient_for(self.window)
# Set up radio buttons
widget0 = self.wTree.get_widget("rb1")
widget1 = self.wTree.get_widget("rb2")
widget1.set_group(widget0)
widget2 = self.wTree.get_widget("rb3")
widget2.set_group(widget0)
# Set up selection of mouse actions
widget = self.wTree.get_widget("MouseConfig")
list = gtk.ListStore(str, str)
widget.set_model(list)
data = self.xSetWacomObject.listMouseActions()
for item in data:
list.append(item)
celltext = gtk.CellRendererText()
widget.pack_start(celltext)
widget.add_attribute(celltext,'text',1)
widget.set_active(0)
widget = self.wTree.get_widget("modhelp")
widget.connect("button-press-event",self.Help,2)
# Check for old xorg.conf configuration and offer to remove it
if wacom_xorg.CheckXorgConfig()[0]:
self.DialogBox.NewMessage("<b>Wacom Control Panel has detected an old configuration</b>\n\nWacom Control Panel now supports the new hotplugging support introduced with Ubuntu 9.04. Your xorg.conf contains tablet configuration that must be removed in order to use these new features. Clicking ok will remove these settings for you. Please enter your password if required to do so.\n\n<b>Your tablet will work as normal again after you log out and log back in again.</b>","Update old configuration")
wacom_xorg.SetXorgConfig(0)
os.system("/bin/sh -c 'gnome-session-save --logout-dialog' &")
sys.exit()
# Gtk main loop
gtk.main()
def Create_Window(self):
# Create widgets
self.wTree = gtk.glade.XML("wacom_utility.glade")
self.window = self.wTree.get_widget("window1")
self.window.set_title("Wacom Tablet Configuration")
self.window.connect("destroy",self.Close)
# Set default button actions
widget = self.wTree.get_widget("button-close")
widget.connect("button-press-event",self.Close)
widget = self.wTree.get_widget("button-help")
widget.connect("button-press-event",self.Help,1)
self.window.show_all()
self.DialogBox = DialogBox(self.window,self.wTree)
self.SelectedItem = "Welcome Screen"
self.ChangeScreen()
def Help(self, widget, event, page):
# Page = 1: Main help
# Page = 2: Pad settings help
# Fixme: help not yet implamented
os.system("/bin/sh -c \"gnome-open 'http://www.gtk-apps.org/content/show.php/Wacom+Control+Panel?content=104309'\"")
def Close(self, widget, event):
if self.SaveConfig == 1:
if self.Tablet:
self.xSetWacomObject.saveToXSession(self.Tablet)
else:
self.xSetWacomObject.purgeXSession()
gtk.main_quit()
def CheckBoxClick(self, widget,setting):
value = int(widget.get_active())
self.ChangeSetting(setting, value)
def ChangeSetting(self, setting, value):
if setting == 1:
self.ChangeSettingFile("configureonlogin", value)
self.ConfigureOnLogin = value
elif setting == 2:
wacom_xorg.SetXorgConfig(value)
self.DialogBox.NewMessage("You need to log out and log back in again for the new configuration to take effect.\n\nA backup has been made in your home directory.","Wacom Control Panel")
self.ChangeScreen()
def ChangeSettingFile(self, setting, value):
# Purges .wacom_utility file of any previous configuration and updates new
try:
os.stat(os.path.expanduser("~/.wacom_utility"))
except:
return
f1 = open(os.path.expanduser("~/.wacom_utility"), 'r')
data = f1.readlines()
f1.close()
newdata = copy(data)
for line in data:
if line[0:len(setting)] == setting:
newdata.remove(line)
newdata.insert(0,setting+"="+str(value)+"\n")
f2 = open(os.path.expanduser("~/.wacom_utility"), 'w')
f2.writelines(newdata)
f2.close()
def SelectDevice(self,widget):
Dataset = widget.get_model()
Index = widget.get_cursor()
activerow = Index[0][0]
self.SelectedItem = Dataset[activerow][0]
self.ChangeScreen()
def ChangeScreen(self):
container = self.wTree.get_widget("mainbox")
if len(container.get_children()) == 2:
container.remove(container.get_children()[1])
if self.PressureMachine:
self.PressureMachine.Stop()
del self.PressureMachine
gc.collect()
self.PressureMachine=None
# Removing its gdk.window reference
# Get a second copy of the tree structure
wTree = gtk.glade.XML("wacom_utility.glade")
if self.SelectedItem == "Welcome Screen":
# Place container for this screen
widget = wTree.get_widget("WelcomeScreen")
widget.reparent(container)
elif "pad" in self.SelectedItem.lower():
# Place container for this screen
widget = wTree.get_widget("PadContainer")
widget.reparent(container)
# Configures Pad Graphic
widget = Pad()
widget.set_parameters(self.Tablet)
widget.set_size_request(self.Tablet.GraphicWidth,-1)
container = wTree.get_widget("padbox")
container.pack_start(widget)
widget.show()
# Configures button maps
container = wTree.get_widget("padbuttonlist")
for item in self.Tablet.Buttons:
placeholder = gtk.HBox()
widget1 = gtk.Label()
widget1.set_use_markup(True)
widget1.set_markup("<span foreground='#000000' font_desc='sans 18' stretch='normal' weight='normal'>"+str(item.Number)+"</span>")
widget2 = gtk.Label(item.Name)
widget3 = gtk.Label(self.xSetWacomObject.getTypeAndName(self.SelectedItem, item.Callsign)[1])
widget4 = gtk.Button(stock="gtk-edit")
widget4.connect("button-press-event",self.ShowModWindow, item)
placeholder.pack_start(widget1,0,0,4)
placeholder.pack_start(widget2,0,0,4)
placeholder.pack_end(widget4,0,0,4)
placeholder.pack_end(widget3,0,0,4)
container.pack_start(placeholder,0,1,10)
container.show_all()
elif self.SelectedItem == "options":
# Place container for this screen
widget = wTree.get_widget("OptionsContainer")
widget.reparent(container)
# Configure events on options page
widget = wTree.get_widget("applyonstartup")
widget.set_active(self.ConfigureOnLogin)
widget.connect("toggled",self.CheckBoxClick,1)
else:
# Place container for this screen
widget = wTree.get_widget("PressureContainer")
widget.reparent(container)
self.PressureMachine = GraphicsTabletApplet(self.window, wTree, self.SelectedItem)
self.PressureMachine.Run()
def ShowModWindow(self, widget, event, button):
a = ModifyAction(self.Tablet,self.wTree,self.SelectedItem, button, self.xSetWacomObject)
self.ChangeScreen()
# Ensures no memory leaks
del a
gc.collect()
class ModifyAction:
def __init__(self, Tablet, wTree, Device, Button, xSetWacomObject):
self.Tablet = Tablet
self.SelectedItem = Device
self.wTree = wTree
self.Button = Button
self.xSetWacomObject = xSetWacomObject
self.Events = []
# Display Window
self.window = self.wTree.get_widget("ModifyKey")
ev = self.window.connect("delete-event",self.close)
self.Events.append([self.window,ev])
# Connect events
widget = self.wTree.get_widget("modclose")
ev = widget.connect("button-press-event", self.close)
self.Events.append([widget, ev])
widget = self.wTree.get_widget("addaction")
ev = widget.connect("button-press-event", self.AddMod)
self.Events.append([widget, ev])
# Set up widgets
widget = self.wTree.get_widget("modlbl")
widget.set_markup("<b>Modifying Action for " + self.Button.Name + "</b>")
# Set contents of text box
widget = self.wTree.get_widget("ModifyAction")
widget.set_text("")
ev = widget.connect("changed",self.CheckValidity)
self.Events.append([widget, ev])
# Configure radio buttons
widget0 = self.wTree.get_widget("rb1")
ev = widget0.connect("toggled",self.ChangeState)
self.Events.append([widget0, ev])
widget1 = self.wTree.get_widget("rb2")
ev = widget1.connect("toggled",self.ChangeState)
self.Events.append([widget1, ev])
widget2 = self.wTree.get_widget("rb3")
ev = widget2.connect("toggled",self.ChangeState)
self.Events.append([widget2, ev])
widget0.set_active(False)
widget1.set_active(False)
widget2.set_active(False)
if self.xSetWacomObject.getTypeAndName(self.SelectedItem, self.Button.Callsign)[0] == 0:
widget0.set_active(True)
self.UpdateActiveRegion(0)
elif self.xSetWacomObject.getTypeAndName(self.SelectedItem, self.Button.Callsign)[0] == 1:
widget1.set_active(True)
self.UpdateActiveRegion(1)
self.UpdateForm()
else:
widget2.set_active(True)
self.UpdateActiveRegion(2)
self.UpdateForm()
# Show Window
self.window.show_all()
gtk.main()
def ChangeState(self, widget):
if widget == self.wTree.get_widget("rb1"):
self.UpdateActiveRegion(0)
elif widget == self.wTree.get_widget("rb2"):
self.UpdateActiveRegion(1)
else:
self.UpdateActiveRegion(2)
def AddMod(self, widget, event):
widget = self.wTree.get_widget("availkeys")
widget2 = self.wTree.get_widget("ModifyAction")
widget2.set_text(widget.get_model()[widget.get_active()][0]+" "+widget2.get_text())
self.CheckValidity(widget2)
def CheckValidity(self, widget):
notice = self.wTree.get_widget("isvalid")
if self.xSetWacomObject.verifyString(widget.get_text()) == 1:
notice.hide()
else:
notice.show()
def UpdateActiveRegion(self, index):
#0 = Ign
#1 = Mouse
#2 = Kbd
if index == 0:
widget = self.wTree.get_widget("MouseConfig")
widget.set_sensitive(False)
widget = self.wTree.get_widget("KeyConfig")
widget.set_sensitive(False)
elif index == 1:
widget = self.wTree.get_widget("MouseConfig")
widget.set_sensitive(True)
widget = self.wTree.get_widget("KeyConfig")
widget.set_sensitive(False)
else:
widget = self.wTree.get_widget("MouseConfig")
widget.set_sensitive(False)
widget = self.wTree.get_widget("KeyConfig")
widget.set_sensitive(True)
def UpdateForm(self):
if self.xSetWacomObject.getTypeAndName(self.SelectedItem,self.Button.Callsign)[0] == 0:
pass
elif self.xSetWacomObject.getTypeAndName(self.SelectedItem,self.Button.Callsign)[0] == 1:
widget = self.wTree.get_widget("MouseConfig")
i = 0
for item in widget.get_model():
if item[1] == self.xSetWacomObject.getTypeAndName(self.SelectedItem,self.Button.Callsign)[1]:
widget.set_active(i)
i += 1
else:
widget = self.wTree.get_widget("ModifyAction")
widget.set_text(self.xSetWacomObject.getTypeAndName(self.SelectedItem,self.Button.Callsign)[1])
def CommitChanges(self):
if self.wTree.get_widget("rb1").get_active():
self.xSetWacomObject.setByTypeAndName(self.SelectedItem,0,self.Button.Callsign)
elif self.wTree.get_widget("rb2").get_active():
widget = self.wTree.get_widget("MouseConfig")
self.xSetWacomObject.setByTypeAndName(self.SelectedItem,1,self.Button.Callsign,widget.get_model()[widget.get_active()][1])
else:
widget = self.wTree.get_widget("ModifyAction")
if self.xSetWacomObject.verifyString(widget.get_text())==1:
self.xSetWacomObject.setByTypeAndName(self.SelectedItem,2,self.Button.Callsign,widget.get_text())
def close(self, widget, event):
self.CommitChanges()
# Unlink all gtk events or python will not allow this object
# to be destroyed, as there are events linked into this object's
# code
for event in self.Events:
event[0].disconnect(event[1])
gtk.main_quit()
self.window.hide()
if __name__ == "__main__":
Main()