forked from lubosz/wacom-utility
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tablet_capplet.py
470 lines (371 loc) · 13.8 KB
/
tablet_capplet.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
############################################################################
##
## Copyright (C) 2007 Alexander Macdonald. All rights reserved.
##
## Modified by QB89Dragon 2009 for inclusion to pen tablet utility
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License version 2
##
## Graphics Tablet Applet
##
############################################################################
import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
import gobject
import cairo
import sys
import os
import subprocess
import math
def GetPressCurve(devicename):
command = ["xsetwacom", "get", devicename, "PressureCurve"]
output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
bits = output.strip().split()
return [float(x) for x in bits]
def SetPressCurve(devicename, points):
command = ["xsetwacom", "set", devicename, "PressureCurve", str(points[0]), str(points[1]), str(points[2]), str(points[3])]
output = subprocess.Popen(command)
def GetClickForce(devicename):
command = ["xsetwacom", "get", devicename, "Threshold"]
output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
return float(output.strip())
def SetClickForce(devicename, force):
command = ["xsetwacom", "set", devicename, "Threshold", str(force)]
output = subprocess.Popen(command)
def GetMode(devicename):
command = ["xsetwacom", "get", devicename, "Mode"]
output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
return output.strip()
def SetMode(devicename, m):
command = ["xsetwacom", "set", devicename, "Mode", str(m)]
output = subprocess.Popen(command)
class PressureCurveWidget(gtk.DrawingArea):
def __init__(self):
gtk.DrawingArea.__init__(self)
self.Points = [0,100,100,0]
self.Pressure = 0.0
self.Radius = 5.0
self.ControlPointStroke = 2.0
self.ControlPointDiameter = (self.Radius * 2) + self.ControlPointStroke
self.WindowSize = None
self.Scale = None
self.ClickForce = None
self.DeviceName = ""
self.DraggingCP1 = False
self.DraggingCP2 = False
self.DraggingCF = False
self.set_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.BUTTON_MOTION_MASK | gtk.gdk.BUTTON1_MOTION_MASK | gtk.gdk.BUTTON2_MOTION_MASK | gtk.gdk.BUTTON3_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK)
self.connect("configure-event", self.ConfigureEvent)
self.connect("expose-event", self.ExposeEvent)
self.connect("motion-notify-event", self.MotionEvent)
self.connect("button-press-event", self.ButtonPress)
self.connect("button-release-event", self.ButtonRelease)
self.set_size_request(100,100)
def SetDevice(self, name):
self.DeviceName = name
self.ClickForce = GetClickForce(name)
if self.ClickForce != None:
self.ClickForce *= (100.0 / 19.0)
points = GetPressCurve(name)
if points == None:
self.Points = None
else:
self.Points = [points[0], 100.0 - points[1], points[2], 100.0 - points[3]]
def Update(self):
if not isinstance(self.window, gtk.gdk.Window):
return
self.window.invalidate_region(self.window.get_clip_region(), True)
def ClampValue(self, v):
if v < 0.0:
return 0.0
elif v > 100.0:
return 100.0
else:
return v
def ConfigureEvent(self, widget, event):
self.WindowSize = self.window.get_size()
self.Scale = ((self.WindowSize[0] - self.ControlPointDiameter) / 100.0, (self.WindowSize[1] - self.ControlPointDiameter) / 100.0)
def MotionEvent(self, widget, event):
pos = event.get_coords()
pos = (pos[0] / self.Scale[0], pos[1] / self.Scale[1])
if self.Points == None:
return
if self.DraggingCP1:
self.Points[0] = self.ClampValue(pos[0])
self.Points[1] = self.ClampValue(pos[1])
elif self.DraggingCP2:
self.Points[2] = self.ClampValue(pos[0])
self.Points[3] = self.ClampValue(pos[1])
elif self.DraggingCF:
self.ClickForce = int(self.ClampValue(pos[0]) / (100.0 / 19)) * (100.0 / 19)
def ButtonPress(self, widget, event):
if self.Points == None:
return
if self.DraggingCP1 or self.DraggingCP2 or self.DraggingCF:
self.DragFinished()
else:
pos = event.get_coords()
pos = (pos[0] / self.Scale[0], pos[1] / self.Scale[1])
if pos[0] > (self.Points[0] - self.ControlPointDiameter) and pos[0] < (self.Points[0] + self.ControlPointDiameter):
if pos[1] > (self.Points[1] - self.ControlPointDiameter) and pos[1] < (self.Points[1] + self.ControlPointDiameter):
self.DraggingCP1 = True
return
if pos[0] > self.Points[2] - self.ControlPointDiameter and pos[0] < self.Points[2] + self.ControlPointDiameter:
if pos[1] > self.Points[3] - self.ControlPointDiameter and pos[1] < self.Points[3] + self.ControlPointDiameter:
self.DraggingCP2 = True
return
if pos[0] > self.ClickForce - self.ControlPointDiameter and pos[0] < self.ClickForce + self.ControlPointDiameter:
self.DraggingCF = True
return
def ButtonRelease(self, widget, event):
self.DragFinished()
def DragFinished(self):
if self.Points != None:
if self.DraggingCP1: # Update to new curve constraints
self.Points[3] = self.Points[0]
self.Points[2] = self.Points[1]
elif self.DraggingCP2:
self.Points[0] = self.Points[3]
self.Points[1] = self.Points[2]
print int(self.Points[0]), int(100.5 - self.Points[1]), int(self.Points[2]), int(100.5 - self.Points[3])
SetPressCurve(self.DeviceName, [int(self.Points[0]+.5), int(100.5 - self.Points[1]), int(self.Points[2]+.5), int(100.5 - self.Points[3])])
if self.ClickForce != None:
SetClickForce(self.DeviceName, int(self.ClickForce / (100.0 / 19.0)) + 1)
self.DraggingCP1 = self.DraggingCP2 = self.DraggingCF = False
def ExposeEvent(self, widget, event):
cr = widget.window.cairo_create()
cr.set_line_cap(cairo.LINE_CAP_ROUND);
cr.save()
cr.translate(self.ControlPointDiameter / 2.0, self.ControlPointDiameter / 2.0)
# Grid
cr.set_line_width(0.5)
cr.set_source_rgba(0.0, 0.0, 0.0, 0.25)
cr.save()
cr.scale(self.Scale[0], self.Scale[1])
cr.new_path()
for x in range(11):
cr.move_to(x * 10.0, 0.0)
cr.line_to(x * 10.0, 100.0)
for y in range(11):
cr.move_to(0.0, y * 10.0)
cr.line_to(100.0, y * 10.0)
cr.restore()
cr.stroke()
if self.Pressure != None:
# Linear Line
cr.set_line_width(1.0)
cr.save()
cr.scale(self.Scale[0], self.Scale[1])
cr.new_path()
cr.move_to(0.0, 100.0)
cr.line_to(100.0, 0.0)
cr.restore()
cr.stroke()
# Click Force
if self.ClickForce != None:
cr.set_line_width(1.0)
cr.set_source_rgba(1.0, 0.0, 0.0, 0.25)
cr.save()
cr.scale(self.Scale[0], self.Scale[1])
cr.new_path()
cr.move_to(self.ClickForce, 0.0)
cr.line_to(self.ClickForce, 100.0)
cr.restore()
cr.stroke()
if self.Points == None:
points = [0.0, 100.0, 100.0, 0.0]
else:
points = self.Points
# Pressure
cr.save()
cr.scale(self.Scale[0], self.Scale[1])
cr.rectangle(0.0, 0.0, self.Pressure * 100.0, 100.0)
cr.clip()
cr.new_path()
cr.set_source_rgba(114.0 / 255.0, 159.0 / 255.0, 207.0 / 255.0, 0.5)
cr.move_to(0.0,100.0)
cr.curve_to(points[0], points[1], points[2], points[3], 100.0, 0.0)
cr.line_to(100.0, 100.0)
cr.fill()
cr.restore()
# Pressure Curve
cr.set_line_width(2.0)
cr.set_source_rgba(32.0 / 255.0, 74.0 / 255.0, 135.0 / 255.0, 1.0)
cr.save()
cr.scale(self.Scale[0], self.Scale[1])
cr.new_path()
cr.move_to(0.0,100.0)
cr.curve_to(points[0], points[1], points[2], points[3], 100.0, 0.0)
cr.restore()
cr.stroke()
if self.Points != None:
# Control Lines
cr.set_line_width(2.0)
cr.set_source_rgba(0.0, 0.0, 0.0, 0.5)
cr.save()
cr.scale(self.Scale[0], self.Scale[1])
cr.move_to(0.0,100.0)
cr.line_to(self.Points[0], self.Points[1])
cr.move_to(100.0,0.0)
cr.line_to(self.Points[2], self.Points[3])
cr.restore()
cr.stroke()
# Control Points
cr.set_line_width(2.0)
cr.save()
cr.arc(self.Points[0] * self.Scale[0], self.Points[1] * self.Scale[1], self.Radius, 0.0, 2.0 * math.pi);
cr.set_source_rgba(237.0 / 255.0, 212.0 / 255.0, 0.0, 0.5)
cr.fill_preserve()
cr.set_source_rgba(239.0 / 255.0, 41.0 / 255.0, 41.0 / 255.0, 1.0)
cr.stroke()
cr.arc(self.Points[2] * self.Scale[0], self.Points[3] * self.Scale[1], self.Radius, 0.0, 2.0 * math.pi);
cr.set_source_rgba(237.0 / 255.0, 212.0 / 255.0, 0.0, 0.5)
cr.fill_preserve()
cr.set_source_rgba(239.0 / 255.0, 41.0 / 255.0, 41.0 / 255.0, 1.0)
cr.stroke()
cr.restore()
cr.restore()
class DrawingTestWidget(gtk.DrawingArea):
def __init__(self):
gtk.DrawingArea.__init__(self)
self.Device = 0
self.Radius = 5.0
self.Drawing = False
self.WindowSize = None
self.Raster = None
self.RasterCr = None
self.set_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.BUTTON_MOTION_MASK | gtk.gdk.BUTTON1_MOTION_MASK | gtk.gdk.BUTTON2_MOTION_MASK | gtk.gdk.BUTTON3_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK)
self.connect("configure-event", self.ConfigureEvent)
self.connect("expose-event", self.ExposeEvent)
self.connect("motion-notify-event", self.MotionEvent)
self.connect("button-press-event", self.ButtonPress)
self.connect("button-release-event", self.ButtonRelease)
self.set_size_request(100,100)
def ConfigureEvent(self, widget, event):
self.WindowSize = self.window.get_size()
self.Raster = self.window.cairo_create().get_target().create_similar(cairo.CONTENT_COLOR, self.WindowSize[0], self.WindowSize[1])
self.RasterCr = cairo.Context(self.Raster)
self.RasterCr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
self.RasterCr.rectangle(0.0, 0.0, self.WindowSize[0], self.WindowSize[1])
self.RasterCr.fill()
def GetPressure(self):
dev = gtk.gdk.devices_list()[self.Device]
state = dev.get_state(self.window)
return dev.get_axis(state[0], gtk.gdk.AXIS_PRESSURE)
def MotionEvent(self, widget, event):
if self.Drawing:
pos = event.get_coords()
p = self.GetPressure()
if p == None:
p = 0.0
r = p * 50 + 5
self.RasterCr.set_line_width(2)
self.RasterCr.set_source_rgba(p, 1.0, 0.0, 0.5)
self.RasterCr.arc(pos[0], pos[1],r, 0.0, 2 * math.pi);
self.RasterCr.fill_preserve()
self.RasterCr.set_source_rgba(0.5, 0.2, p, 0.5)
self.RasterCr.stroke()
reg = gtk.gdk.Region()
reg.union_with_rect((int(pos[0] - r - 2), int(pos[1] - r - 2), int(2 * (r + 2)), int(2 * (r + 2))))
self.window.invalidate_region(reg, False)
def ButtonPress(self, widget, event):
self.Drawing = True
def ButtonRelease(self, widget, event):
self.Drawing = False
def ExposeEvent(self, widget, event):
cr = widget.window.cairo_create()
cr.set_source_surface(self.Raster, 0.0, 0.0)
cr.paint()
cr.set_line_width(2)
cr.set_source_rgba(0.0, 0.0, 0.0, 0.25)
cr.rectangle(0.0, 0.0, self.WindowSize[0], self.WindowSize[1])
cr.stroke()
class GraphicsTabletApplet:
def __init__(self, window, wTree, Device):
self.Active = 0 # Control
self.InLoop = 0 # Flag
self.WidgetTree = wTree
self.MainWindow = window
self.DrawingTestFrame = self.WidgetTree.get_widget("drawingalignment")
self.PressureVBox = self.WidgetTree.get_widget("pressurevbox")
self.DeviceModeCombo = self.WidgetTree.get_widget("devicemodecombo")
self.XTilt = self.WidgetTree.get_widget("xtilt")
self.YTilt = self.WidgetTree.get_widget("ytilt")
self.Curve = PressureCurveWidget()
self.Curve.show()
self.PressureVBox.add(self.Curve)
self.DrawingArea = DrawingTestWidget()
self.DrawingArea.show()
self.DrawingTestFrame.add(self.DrawingArea)
devices = gtk.gdk.devices_list()
for i in range(0,len(devices)):
item = devices[i].name
if item in Device:
self.Device = i
self.DeviceMode = None
self.DeviceName = Device
self.DrawingArea.Device = self.Device
self.DeviceName = gtk.gdk.devices_list()[self.Device].name
self.Curve.SetDevice(self.DeviceName)
self.UpdateDeviceMode()
self.DeviceModeCombo.connect("changed", self.ModeChanged)
self.DrawingArea.set_extension_events(gtk.gdk.EXTENSION_EVENTS_ALL)
def Run(self):
self.Active = 1
self.InLoop = 1
self.DeviceName = gtk.gdk.devices_list()[self.Device].name
self.UpdateDeviceMode()
gobject.timeout_add(20, self.Update)
def Stop(self):
self.Active = 0
def GetPressure(self):
dev = gtk.gdk.devices_list()[self.Device]
if not isinstance(self.DrawingArea.window, gtk.gdk.Window):
return (0.0, 0.0)
state = dev.get_state(self.DrawingArea.window)
return dev.get_axis(state[0], gtk.gdk.AXIS_PRESSURE)
def GetTilt(self):
dev = gtk.gdk.devices_list()[self.Device]
state = dev.get_state(self.MainWindow.window)
try:
x = float(dev.get_axis(state[0], gtk.gdk.AXIS_XTILT))
y = float(dev.get_axis(state[0], gtk.gdk.AXIS_YTILT))
return (x, y)
except:
return (0.0, 0.0)
def ModeChanged(self, widget):
SetMode(self.DeviceName, widget.get_active_text())
def UpdateDeviceMode(self):
self.DeviceMode = GetMode(self.DeviceName)
if self.DeviceMode == None:
self.DeviceModeCombo.set_sensitive(False)
else:
self.DeviceModeCombo.set_sensitive(True)
if self.DeviceMode == "Relative":
self.DeviceModeCombo.set_active(0)
elif self.DeviceMode == "Absolute":
self.DeviceModeCombo.set_active(1)
def DeviceSelected(self, widget):
self.Device = widget.get_active()
self.DrawingArea.Device = self.Device
self.DeviceName = gtk.gdk.devices_list()[self.Device].name
self.Curve.SetDevice(self.DeviceName)
self.UpdateDeviceMode()
def Update(self):
p = self.GetPressure()
if p == None:
self.Curve.Pressure = None
self.Curve.Update()
else:
self.Curve.Pressure = p
self.Curve.Update()
t = self.GetTilt()
self.XTilt.set_adjustment(gtk.Adjustment(t[0], -1.0, 1.0))
self.YTilt.set_adjustment(gtk.Adjustment(t[1], -1.0, 1.0))
if self.Active:
return True
else:
self.InLoop = 0