7
7
import os
8
8
from os .path import expanduser
9
9
import json
10
+ import subprocess
11
+ import re
10
12
import argparse
11
13
from wacom_data import Tablets
12
14
import wacom_menu
@@ -235,12 +237,12 @@ def getConfigs(self, idx):
235
237
with open ("%s/device_default" % conf_path , 'r' ) as f :
236
238
device = json .load (f )
237
239
# config file exists, use it
238
- if str (dev_id ) in device .keys () and device [str (dev_id )] in self .configs [dev ].keys ():
239
- self .config = device [str (dev_id )]
240
+ if str (dev_id ) in device .keys () and device [str (dev_id )][ 'config' ] in self .configs [dev ].keys ():
241
+ self .config = device [str (dev_id )][ 'config' ]
240
242
# set defaults in tablet_data, if it's detected
241
243
for idx in device .keys ():
242
244
try :
243
- self .tablet_data .tablets [self .dev ][int (idx )]['config' ] = device [idx ]
245
+ self .tablet_data .tablets [self .dev ][int (idx )]['config' ] = device [idx ][ 'config' ]
244
246
except Exception as e :
245
247
pass
246
248
self .configLayout .addButton (
@@ -407,9 +409,18 @@ def updateConfigs(self):
407
409
if self .dev is not None :
408
410
for idx , tablet in enumerate (self .tablet_data .tablets [self .dev ]):
409
411
if 'config' in tablet .keys ():
410
- default [idx ] = tablet ['config' ]
412
+ # check if toggle enabled
413
+ toggle = 0
414
+ if 'pad' in self .configs [self .dev ][tablet ['config' ]]:
415
+ for button in self .configs [self .dev ][tablet ['config' ]]['pad' ]['buttons' ].keys ():
416
+ if self .configs [self .dev ][tablet ['config' ]]['pad' ]['buttons' ][button ] == 'lhyper z' :
417
+ toggle = 1
418
+ break
419
+ default [idx ] = {'config' : tablet ['config' ],
420
+ 'toggle' : toggle }
411
421
else :
412
- default [idx ] = 'default'
422
+ default [idx ] = {'config' : 'default' ,
423
+ 'toggle' : 0 }
413
424
conf_path = os .path .join ("%s/.wacom-gui" % expanduser ("~" ), self .dev )
414
425
conf_file = os .path .join (conf_path , 'device_default' )
415
426
with open (conf_file , 'w' ) as outfile :
@@ -422,15 +433,6 @@ def quickLoad(self):
422
433
self .tabletSelect (idx )
423
434
sys .exit ()
424
435
425
- def toggleDisplay (self ):
426
- self .toggle = True
427
- for idx , button in enumerate (self .tabletButtons .btn_grp .buttons ()):
428
- self .tabletSelect (idx )
429
- if self .pad .is_toggle ():
430
- self .stylus .mapping .toggle_next ()
431
- self .updateConfigs ()
432
- sys .exit ()
433
-
434
436
def closeEvent (self , event ):
435
437
self .updateConfigs ()
436
438
event .accept ()
@@ -648,15 +650,89 @@ def loadToggleShortcut():
648
650
os .popen ("setxkbmap -option '" + keys + "'" )
649
651
650
652
653
+ def toggleDisplay ():
654
+ # get tablets
655
+ tablet_data = Tablets ()
656
+ # get displays
657
+ displays = {}
658
+ cmd = "xdpyinfo | grep 'dimensions:'"
659
+ p = subprocess .Popen (cmd , shell = True , stderr = subprocess .PIPE , stdout = subprocess .PIPE )
660
+ output = p .communicate ()[0 ]
661
+ pattern = r"\s+\S+\s+(\d+)x(\d+)"
662
+ full = re .match (pattern , output ).groups ()
663
+ displays ['Full' ] = {'cmd' : "%sx%s+0+0" % (full [0 ], full [1 ]),
664
+ 'x' : full [0 ],
665
+ 'y' : full [1 ],
666
+ 'xoff' : 0 ,
667
+ 'yoff' : 0 }
668
+ cmd = "xdpyinfo -ext all | grep head"
669
+ p = subprocess .Popen (cmd , shell = True , stderr = subprocess .PIPE , stdout = subprocess .PIPE )
670
+ output = p .communicate ()[0 ]
671
+ pattern = r"\s+(\S+)\s+#(\d+):\s+(\d+)x(\d+)\D+(\d+),(\d+)"
672
+ heads = re .findall (pattern , output )
673
+ for head in heads :
674
+ info = list (head )
675
+ displays ['%s-%s' % (info [0 ].upper (), info [1 ])] = {'cmd' : '%sx%s+%s+%s' %
676
+ (info [2 ], info [3 ], info [4 ], info [5 ]),
677
+ 'x' : int (info [2 ]),
678
+ 'y' : int (info [3 ]),
679
+ 'xoff' : int (info [4 ]),
680
+ 'yoff' : int (info [5 ])}
681
+ displays ['Partial...' ] = {'cmd' : "%sx%s+0+0" % (full [0 ], full [1 ]),
682
+ 'x' : full [0 ],
683
+ 'y' : full [1 ],
684
+ 'xoff' : 0 ,
685
+ 'yoff' : 0 }
686
+ for dev in tablet_data .tablets .keys ():
687
+ path = os .path .join (expanduser ("~" ), ".wacom-gui/%s" % dev )
688
+ config_name = None
689
+ if os .path .exists (os .path .join (path , 'device_default' )):
690
+ with open (os .path .join (path , 'device_default' ), 'r' ) as f :
691
+ config_name = json .load (f )
692
+ if config_name is not None :
693
+ for dev_id , data in enumerate (tablet_data .tablets [dev ]):
694
+ if config_name [str (dev_id )]['toggle' ] == 1 :
695
+ if os .path .exists (os .path .join (path , "%s.json" % config_name [str (dev_id )]['config' ])):
696
+ with open (os .path .join (path , "%s.json" % config_name [str (dev_id )]['config' ]), 'r' ) as f :
697
+ config = json .load (f )
698
+ if 'mapping' in config ['stylus' ].keys ():
699
+ if 'maptooutput' in config ['stylus' ]['mapping' ].keys ():
700
+ cur_display = config ['stylus' ]['mapping' ]['maptooutput' ]
701
+ disp_list = sorted (displays .keys ())
702
+ idx = disp_list .index (cur_display )
703
+ if idx + 1 <= displays .keys ().__len__ ():
704
+ idx = idx + 1
705
+ if displays [disp_list [idx ]]['cmd' ] == displays [disp_list [0 ]]['cmd' ]:
706
+ idx = 0
707
+ else :
708
+ idx = 0
709
+ # update config
710
+ config ['stylus' ]['mapping' ]['maptooutput' ] = disp_list [idx ]
711
+ conf_file = os .path .join (path , "%s.json" % config_name [str (dev_id )]['config' ])
712
+ if os .path .exists (conf_file ):
713
+ os .rename (conf_file , "%s.bak" % conf_file )
714
+ with open (conf_file , 'w' ) as outfile :
715
+ json .dump (config , outfile , sort_keys = True ,
716
+ indent = 4 , separators = (',' , ': ' ))
717
+ # update mapping
718
+ sid = tablet_data .tablets [dev ][dev_id ]['stylus' ]['id' ]
719
+ eid = tablet_data .tablets [dev ][dev_id ]['eraser' ]['id' ]
720
+ coords = displays [disp_list [idx ]]['cmd' ]
721
+ cmd = "xsetwacom --set %s maptooutput %s" % (sid , coords )
722
+ os .popen (cmd )
723
+ cmd = "xsetwacom --set %s maptooutput %s" % (eid , coords )
724
+ os .popen (cmd )
725
+ sys .exit ()
726
+
651
727
def main ():
652
728
loadToggleShortcut ()
653
729
app = QApplication (sys .argv )
654
- form = WacomGui ()
655
730
opts = parseArgs ()
731
+ if opts .toggle :
732
+ toggleDisplay ()
733
+ form = WacomGui ()
656
734
if opts .load :
657
735
form .quickLoad ()
658
- if opts .toggle :
659
- form .toggleDisplay ()
660
736
form .show ()
661
737
sys .exit (app .exec_ ())
662
738
0 commit comments