1 import Views, Events, Logger, Bindings, Actions
2 from Views import EventWidget
3 import time, os, struct, curses
5 class View(Views.WidgetView):
7 def __init__(self, context, label, numeric_switches, alpha_switches, x, y, dx=0, size=11):
9 dx = max(dx,size*numeric_switches+3)
10 delta = dx - size*numeric_switches - 3
12 delta = dx - size*numeric_switches - 2*shift
13 Views.WidgetView.__init__(self, context, label, x, y, dx, 7)
15 split = numeric_switches // 2
16 for i in range(split):
17 self.add( EventWidget(i,str(i+1)[-1:],size*i+1+shift,4,size) )
18 for i in range(split,numeric_switches):
19 self.add( EventWidget(i,str(i+1)[-1:],size*i+shift+delta,4,size) )
20 split = max(0,alpha_switches-(numeric_switches-split))
21 offset = size//2+(numeric_switches-alpha_switches-1)*size
22 for i in range(split):
23 self.add( EventWidget(i+numeric_switches, chr(ord('A')+i), size*i+1+offset+shift,1,size) )
24 for i in range(split, alpha_switches):
25 self.add( EventWidget(i+numeric_switches, chr(ord('A')+i), size*i+delta+offset+shift,1,size) )
31 STRUCT_len = struct.calcsize(STRUCT)
37 def __init__(self, data):
38 self.time, self.value, self.type, self.number = struct.unpack(self.STRUCT,data)
41 return "%s(time=%d, value=%d,type=%d (%s)), number=%d)" % (
42 str(self.__class__),self.time, self.value, self.type, self.decode_type(), self.number)
44 def decode_type(self):
45 return ('','INIT|')[(self.type & self.TYPE_INIT) and 1 or 0] \
46 + ('','BUTTON','AXIS')[self.type & ~self.TYPE_INIT]
48 def readFrom(klass,f):
49 return klass(os.read(f.fileno(),klass.STRUCT_len))
50 readFrom=classmethod(readFrom)
52 def readMultipleFrom(klass,f,maxevents=256):
53 data = os.read(f.fileno(),maxevents*klass.STRUCT_len)
56 rv.append(klass(data[:klass.STRUCT_len]))
57 data = data[klass.STRUCT_len:]
59 readMultipleFrom=classmethod(readMultipleFrom)
62 class Source(Events.EventSource):
64 def __init__(self, joydev, context, bits=None, mindelay=100):
65 Events.EventSource.__init__(self, file(joydev), context)
68 self._mindelay = mindelay
69 self._controllers = {}
76 jsevents = JSEvent.readMultipleFrom(self.fd())
77 for event in jsevents:
78 if event.type == JSEvent.TYPE_AXIS:
79 return self._controller(event.number, event.value)
80 if event.type == JSEvent.TYPE_BUTTON and event.value == 1:
81 self._lastevent = event.time
82 if event.time - lev < self._mindelay : return []
83 n = n | self._bits[event.number]
87 event = JSEvent.readFrom(self.fd())
88 if event.type == JSEvent.TYPE_AXIS:
89 return self._controller(event.number, event.value)
90 if event.type == JSEvent.TYPE_BUTTON and event.value == 1:
91 self._lastevent = event.time
92 if event.time - lev < self._mindelay : return []
96 return [Events.Event(self.context(), n)]
98 def registerController(self, controller, low, high):
99 self._controllers[controller] = { 'low': low, 'high': high }
101 def _controller(self, number, value):
102 controller = self._controllers.get(number)
104 value = (value - controller['low']) * 1000 / (controller['high']-controller['low'])
105 value = max(min(value,1000),0)
106 return [Events.ControlEvent(self.context(), 'p%d' % number, value)]
110 class Controller(Views.View):
112 def __init__(self, context, name, x, y, dx, dy, keylist, dispatcher,
113 controller, source, low, high):
114 Views.View.__init__(self, context, name, x, y, dx, dy)
115 self._valueEvent = None
116 self._setCommand = None
117 self._valueCommand = None
118 self._parameter = None
119 self._keylist = keylist
120 self._keymap = Bindings.KeyMap()
121 self._keymap.add( Bindings.Binding(Events.Event(source.context(), 'p%d' % controller), '',
122 Actions.Command('controllerChanged',
123 self._controllerChanged)) )
124 self._keylist.prepend(self._keymap)
125 source.registerController(controller, low, high)
126 self._dispatcher = dispatcher
131 self._controlValue = None
133 def updateView(self, bindings):
137 Views.View.init(self)
140 def assign(self, parameter, setCommand, valueCommand, valueEvent, min, max, stops=[]):
141 self._keylist.removeMap(self._keymap)
142 if self._valueEvent is not None:
143 self._keymap.unbind(self._valueEvent)
144 self._parameter = parameter
145 self._valueEvent = valueEvent
146 self._setCommand = setCommand
147 self._valueCommand = valueCommand
151 self._keymap.add( Bindings.Binding( self._valueEvent, '',
152 Actions.Command('updateValue',
153 self._updateValue)) )
154 self._keylist.prepend(self._keymap)
159 def _updateValue(self, binding):
160 event = self._dispatcher.currentEvent()
161 self._value = event.value
164 def _controllerChanged(self, binding):
165 event = self._dispatcher.currentEvent()
166 Logger.log('ctl',"value = %d" % event.value)
167 self._controlValue = event.value
168 if self._controlValue >= 999 or self._controlValue <= 1:
169 self._dispatcher.setIdleCallback(self._changeValue,50)
171 self._dispatcher.unsetIdleCallback()
172 self._redrawController()
174 def _changeValue(self):
175 if self._value is None: return
176 if self._controlValue >= 999:
178 elif self._controlValue <= 1:
181 def stepValue(self, direction):
183 newValue = self._value + (self._max - self._min) / (3000/50)
184 crossed = [ x for x in self._stops if x > self._value*1.0001 and x <= newValue ]
186 newValue = self._value - (self._max - self._min) / (3000/50)
187 crossed = [ x for x in self._stops if x < self._value/1.0001 and x >= newValue ]
188 if newValue >= self._max:
189 crossed = [ self._max ]
190 elif newValue <= self._min:
191 crossed = [ self._min ]
193 newValue = crossed[0]
194 self._dispatcher.unsetIdleCallback()
195 self._setCommand(newValue)
196 # Hmm ... why does value_command not work sometimes ??
197 self._value = newValue
200 self._setCommand(newValue)
204 height, width = self.win().getmaxyx()
205 if self._parameter is not None:
206 self.win().addstr(1,2,self._parameter[:width-4].ljust(width-4), curses.A_BOLD)
207 self._redrawValue(False)
208 self._redrawController()
210 def _flt(self, value, width):
211 return ('%.3f' % value)[:width].ljust(width)
213 def _redrawValue(self, refresh=True):
214 height, width = self.win().getmaxyx()
216 if self._value is not None:
217 pos = height - 3 - int( (self._value - self._min) * (height-6)
218 / (self._max - self._min) + .5 )
219 if self._max is not None:
220 self.win().addstr(2,2, self._flt(self._max,width-7))
221 if pos is not None and pos == 3:
222 self.win().addstr(pos, 5, self._flt(self._value,width-7), curses.A_BOLD)
224 self.win().addstr(3, 5, "".ljust(width-7))
225 for row in range(4,height-3):
226 if pos is not None and row == pos:
227 self.win().addch(pos,3,curses.ACS_PLUS)
228 self.win().addstr(pos, 5, self._flt(self._value,width-7), curses.A_BOLD)
230 self.win().addch(row,3,curses.ACS_VLINE)
231 self.win().addstr(row, 5, "".ljust(width-7))
232 if pos is not None and pos == height-3:
233 self.win().addstr(pos, 5, self._flt(self._value,width-7), curses.A_BOLD)
235 self.win().addstr(height-3, 5, "".ljust(width-7))
236 if self._min is not None:
237 self.win().addstr(height-2,2,self._flt(self._min,width-7))
242 def _redrawController(self, refresh=True):
243 height, width = self.win().getmaxyx()
244 if self._controlValue is not None and self._controlValue >= 999:
245 self.win().addch(3,3,curses.ACS_UARROW)
247 self.win().addch(3,3,curses.ACS_TTEE)
248 if self._controlValue is not None and self._controlValue <= 1:
249 self.win().addch(height-3,3,curses.ACS_DARROW)
251 self.win().addch(height-3,3,curses.ACS_BTEE)
257 class StepController(Actions.Action):
259 def __init__(self, name, controller, direction):
260 Actions.Action.__init__(self, name)
261 self._controller = controller
262 self._direction = direction
264 def __call__(self, binding):
265 self._controller.stepValue(self._direction)
268 def register( viewmanager,
281 viewmanager.registerView( View(context, label, numeric_switches, alpha_switches, x, y, dx, size) )
282 source = Source(device, context, bits)
283 dispatcher.registerSource( source )
286 def registerController( viewmanager, dispatcher, keylist, source, context, name, x, y, dx, dy,
287 controller, low, high ):
288 controller = Controller(context, name, x, y, dx, dy, keylist, dispatcher,
289 controller, source, low, high)
290 viewmanager.registerView( controller )