-
Notifications
You must be signed in to change notification settings - Fork 11
/
katana_bridge_start
executable file
·125 lines (94 loc) · 3.41 KB
/
katana_bridge_start
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
#!/usr/bin/python3
# -*-python-*-
import os
import sys
from os.path import expanduser
from pwd import getpwnam
from grp import getgrnam
import glob
import syslog
# from string import split
import mido
import usb.core
mido.set_backend( 'mido.backends.rtmidi' )
rundir = "/var/run/katana/"
####### Start User Edits #########
# Controller USB parms (iConnectivity)
control_vid = 0x2321
control_pid = 0x0005
# Controller MIDI device
control_midi_tokens = ( 'mio2', 'MIDI 2' )
# MIDI listen channel
control_midi_channel = 2
# Katana USB parms - NOTE: Leave the '0x0000' entry as-is!
katana_vid = 0x0582
katana_pids = ( 0x01d8, 0x0000 )
katana_midi_tokens = ( 'KATANA', 'MIDI 1' )
katana_midi_channel = 1
######## End User Edits ##########
def find_midi_devices():
control_midi_device = None
for name in mido.get_input_names():
if control_midi_tokens[0] in name and control_midi_tokens[1] in name:
control_midi_device = name
break
if control_midi_device == None:
syslog.syslog( "%d: Cannot find controller MIDI device" % pid )
katana_midi_device = None
for name in mido.get_output_names():
if katana_midi_tokens[0] in name and katana_midi_tokens[1] in name:
katana_midi_device = name
break
if katana_midi_device == None:
syslog.syslog( "%d: Cannot find Katana MIDI device" % pid )
return control_midi_device, katana_midi_device
# Look for devices
controller = usb.core.find( idVendor = control_vid, idProduct = control_pid )
if ( not controller ):
sys.exit( 0 )
katana = False
for pid in katana_pids:
device = usb.core.find( idVendor = katana_vid, idProduct = pid )
if ( device ):
katana = True
break
if ( not katana ):
sys.exit( 0 )
pid = os.getpid()
# syslog.syslog( "%d: Starting" % pid )
control_midi_device, katana_midi_device = find_midi_devices()
# Look for atomic pid file
os.chdir( rundir )
filelist = glob.glob( 'katana_*' )
if len( filelist ) != 1:
syslog.syslog( "%d: /var/run/katana is not properly setup" % pid )
sys.exit( 0 )
# Found file. Parse out the PID of the process that created it.
lockfile = filelist[0]
oldpid = lockfile.split( '_' )[1]
# syslog.syslog( "%d: Check for path /proc/%s" % (pid, oldpid) )
if not os.path.exists( "/proc/%s" % oldpid ):
# No such process, ok to start ours
# syslog.syslog( "%d: Renaming %s to katana_%d" % (pid, lockfile, pid) )
try:
os.rename( lockfile, "katana_%d" % os.getpid() )
except Exception:
pass
# syslog.syslog( "%d: Unable to rename file" % pid )
else:
# syslog.syslog( "%d: About to exec" % pid )
# Drop privileges and start the program
pwObj = getpwnam('katana-user')
os.setgid( pwObj.pw_gid )
# Need secondary groups to access MIDI and USB devices
sec_gids = ( getgrnam('plugdev').gr_gid, getgrnam('audio').gr_gid )
os.setgroups( sec_gids )
os.setuid( pwObj.pw_uid )
# Set path for support modules
os.environ['PYTHONPATH'] = "/usr/local/share/katana"
# Run in katana-user's home directory so we can write preset file.
os.chdir( "/home/katana-user" )
os.execl( "/usr/local/bin/katana_bridge_app", "katana_bridge",
"%s" % control_midi_device, "%s" % control_midi_channel,
"%s" % katana_midi_device, "%s" % katana_midi_channel,
"preset.data" )