Package elisa :: Package core :: Module input_manager

Source Code for Module elisa.core.input_manager

  1  # -*- coding: utf-8 -*- 
  2  # Moovida - Home multimedia server 
  3  # Copyright (C) 2006-2009 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Moovida with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Moovida is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Moovida" in the root directory of this distribution package 
 15  # for details on that license. 
 16  # 
 17  # Author: Florian Boucault <florian@fluendo.com> 
 18   
 19  from elisa.core.manager import Manager 
 20  from elisa.core.components.input_provider import PushInputProvider, \ 
 21                                                   PollInputProvider 
 22   
 23  from twisted.internet import task 
 24   
 25  import gobject, warnings 
 26   
 27   
28 -class InputManager(Manager, gobject.GObject):
29 """ 30 InputManager provides a common place to retrieve input events 31 coming from a GUI toolkit, additional input sources or even a 32 network. InputEvents can be pushed by 33 L{elisa.core.components.input_provider.InputProvider}s 34 or be polled by the InputManager, depending on the implementation 35 chosen: L{elisa.core.components.input_provider.PollInputProvider} 36 or L{elisa.core.components.input_provider.PushInputProvider}. 37 38 Other objects can connect to the manager's signals 39 that are emitted when L{elisa.core.input_event.InputEvent}s coming from 40 L{elisa.core.components.input_provider.InputProvider}s are received. 41 """ 42 43 __gsignals__ = { 44 'input_event' : (gobject.SIGNAL_RUN_LAST, 45 gobject.TYPE_NONE, 46 (object,)) 47 } 48 49 entry_point = 'elisa.core.components.input_provider' 50
51 - def __init__(self):
52 super(InputManager, self).__init__() 53 54 # 35Hz polling rate for the PollInputProvider 55 self._poll_rate = 1 / 35. 56 self._poll_call = task.LoopingCall(self._poll_events)
57
58 - def clean(self):
59 if self._poll_call.running: 60 self._poll_call.stop() 61 62 return super(InputManager, self).clean()
63
64 - def process_event(self, event):
65 """ Fire the signal corresponding to the event. 66 67 Each event type is mapped to a signal instance to which other 68 elisa components can connect (e.g to monitor user key presses). 69 70 This method can be called by 71 L{elisa.core.components.input_provider.PushInputProvider} 72 components when they receive input data from the input device. 73 74 @param event: the event to process 75 @type event: L{elisa.core.input_event.InputEvent} 76 """ 77 self.debug("Event received: %r", event) 78 79 if event == None: 80 return 81 82 self.emit('input_event', event)
83
84 - def register_component(self, component):
85 """ Register a new InputProvider in the InputManager so that the 86 events collected by the former are propagated by the latter. 87 88 @param component: the InputProvider instance to register 89 @type component: L{elisa.core.components.input_provider.InputProvider} 90 """ 91 dfr = super(InputManager, self).register_component(component) 92 dfr.addCallback(self._bind_component, component) 93 return dfr
94
95 - def unregister_component(self, component):
96 """ Clean the InputProvider and unregister it from the InputManager; 97 no events from the InputProvider will be propagated anymore. 98 99 @param component: the InputProvider instance to unregister 100 @type component: L{elisa.core.components.input_provider.InputProvider} 101 """ 102 dfr = super(InputManager, self).unregister_component(component) 103 dfr.addCallback(self._unbind_component, component) 104 return dfr
105
106 - def _bind_component(self, result, component):
107 if isinstance(component, PollInputProvider): 108 warnings.warn("PollInputProviders are deprecated", 109 DeprecationWarning) 110 self._check_polling() 111 return 112 113 component.input_manager = self 114 component.bind()
115
116 - def _unbind_component(self, result, component):
117 if isinstance(component, PushInputProvider): 118 component.unbind() 119 else: 120 self._check_polling()
121
122 - def _check_polling(self):
123 have_poll = False 124 for component in self.components: 125 if isinstance(component, PollInputProvider): 126 have_poll = True 127 break 128 129 if have_poll and not self._poll_call.running: 130 self._poll_call.start(self._poll_rate) 131 elif self._poll_call.running: 132 self._poll_call.stop()
133
134 - def _poll_events(self):
135 """ Poll each registered PollInputProvider for InputEvents to 136 process. 137 """ 138 for component in self.components: 139 if not isinstance(component, PollInputProvider): 140 continue 141 142 events = component.get_input_events() 143 for event in events: 144 self.process_event(event)
145 146 gobject.type_register(InputManager) 147