1 '''
2 Defines C{AEMain} which runs the main program loop.
3
4 @author: Peter Parente
5 @organization: IBM Corporation
6 @copyright: Copyright (c) 2005, 2007 IBM Corporation
7
8 @author: Frank Zenker
9 @author: Ramona Bunk
10 @organization: IT Science Center Ruegen gGmbH, Germany
11 @copyright: Copyright (c) 2007, 2008 ITSC Ruegen
12
13 @license: I{The BSD License}
14 All rights reserved. This program and the accompanying materials are made
15 available under the terms of the BSD license which accompanies
16 this distribution, and is available at
17 U{http://www.opensource.org/licenses/bsd-license.php}
18 '''
19 import gobject, bonobo
20 import AccessEngine
21 import time, signal, logging
22 from Tools.i18n import _
23
24 log = logging.getLogger('AccessEngine.AEMain')
25
26 -class _AEMain(object):
27 '''
28 Starts the main bonobo loop so that AT-SPI events are received and GUI monitors
29 function properly.
30
31 Allows clients to register for callbacks from the main loop on a set interval.
32
33 @ivar callbacks: Callback IDs for functions to invoke on timers or idle using
34 gobject
35 @type callbacks: dictionary
36 @ivar welcome: Output an introductory message when the engine starts?
37 @type welcome: boolean
38 @ivar alive: alive flag
39 @type alive: boolean
40 '''
42 '''
43 Create an C{AEMain} Object.
44 '''
45 self.callbacks = {}
46 self.alive = False
47 self.welcome = None
48
49 - def _init(self, profile, application_state):
50 '''
51 Calls init on each manager.
52
53 Manager:
54 L{AEDeviceManager <AEDeviceManger._AEDeviceManager>},
55 L{AEEventManager <AEEventManger._AEEventManager>},
56 L{AESettingsManager <AESettingsManger._AESettingsManager>},
57 L{AETierManager <AETierManger._AETierManager>},
58 L{AEViewManager <AEViewManger._AEViewManager>}
59
60 Called implicitly by L{run}.
61
62 @param profile: Name of the profile SUE was executed under
63 @type profile: string
64 @param application_state: defines the settings for this application
65 @type application_state: L{AESate <AEState.AEState>}
66 '''
67
68 AccessEngine.AESettingsManager.init(profile)
69 AccessEngine.AEEventManager.init()
70 AccessEngine.AEViewManager.init()
71 AccessEngine.AETierManager.init(application_state)
72 AccessEngine.AEDeviceManager.init()
73
74
75 if self.welcome:
76 AccessEngine.AEDeviceManager.sendWelcome()
77
78 AccessEngine.AEEventManager.flushEvents()
79
80
81 AccessEngine.AEViewManager.initViews()
82
84 '''
85 Shuts down all managers.
86
87 Called implicitly by L{run} when the main loop ends. Sends an exiting
88 message to the default device.
89 '''
90 if self.welcome:
91 AccessEngine.AEDeviceManager.sendGoodbye()
92 AccessEngine.AETierManager.close()
93 AccessEngine.AEViewManager.close()
94 AccessEngine.AEEventManager.close()
95 AccessEngine.AESettingsManager.close()
96 AccessEngine.AEDeviceManager.close()
97
98 - def quit(self, *args):
99 '''
100 Stops the event loop by calling the C{pyatspi.Registry.stop} function.
101
102 Extra parameters are ignored but captured by args so that this method may
103 be registered as a signal handler callback.
104 '''
105 return AccessEngine.AEEventManager.stopAccessibility()
106
107 - def addTimer(self, callback, ms=None, *args):
108 '''
109 Creates a timer that calls the given callback on the interval specified by
110 the given milliseconds. If C{ms} is None, registers for callbacks on idle.
111
112 @param callback: Function to call
113 @type callback: callable
114 @param ms: interval in milliseconds
115 @type ms: integer
116 @param args: Additional arguments to be given to the callback
117 @type args: list
118 '''
119 if ms is not None:
120 cid = gobject.timeout_add(ms, callback, *args)
121 else:
122 cid = gobject.idle_add(ms, callback, *args)
123 self.callbacks[callback] = cid
124
125 - def removeTimer(self, callback):
126 '''
127 Ceases calls to the given registered callback.
128
129 @param callback: Function to stop calling
130 @type callback: callable
131 @raise KeyError: When the callback is not registered
132 '''
133 cid = self.callbacks[callback]
134 gobject.source_remove(cid)
135
136 - def run(self, profile, welcome, application_state):
137 '''
138 Starts an event loop by calling the C{pyatspi.Registry.start} function.
139
140 Makes the timer callbacks registered via L{addTimer} on each iteration of
141 the loop.
142
143 @param profile: Name of the profile SUE was executed under
144 @type profile: string
145 @param welcome: Output an introductory message?
146 @type welcome: boolean
147 @param application_state: defines the settings for this application
148 @type application_state: L{AESate <AEState.AEState>}
149 '''
150
151 self.alive = True
152
153
154 signal.signal(signal.SIGINT, self.quit)
155 signal.signal(signal.SIGTERM, self.quit)
156
157
158 self.welcome = welcome
159 self._init(profile, application_state)
160
161
162 AccessEngine.AEEventManager.startAccessibility(async=True, gil=True)
163
164
165 for c in self.callbacks:
166 self.removeTimer(c)
167
168
169 self._close()
170
171
172 log.info('exiting...')
173
174 time.sleep(1)
175