| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2001-2007 Twisted Matrix Laboratories. | |
| 2 # See LICENSE for details. | |
| 3 | |
| 4 """ | |
| 5 This module provides support for Twisted to interact with the PyGTK mainloop. | |
| 6 | |
| 7 In order to use this support, simply do the following:: | |
| 8 | |
| 9 | from twisted.internet import gtkreactor | |
| 10 | gtkreactor.install() | |
| 11 | |
| 12 Then use twisted.internet APIs as usual. The other methods here are not | |
| 13 intended to be called directly. | |
| 14 | |
| 15 Maintainer: U{Itamar Shtull-Trauring<mailto:twisted@itamarst.org>} | |
| 16 """ | |
| 17 | |
| 18 import sys | |
| 19 | |
| 20 # System Imports | |
| 21 try: | |
| 22 import pygtk | |
| 23 pygtk.require('1.2') | |
| 24 except ImportError, AttributeError: | |
| 25 pass # maybe we're using pygtk before this hack existed. | |
| 26 import gtk | |
| 27 | |
| 28 from zope.interface import implements | |
| 29 | |
| 30 # Twisted Imports | |
| 31 from twisted.python import log, runtime | |
| 32 from twisted.internet.interfaces import IReactorFDSet | |
| 33 | |
| 34 # Sibling Imports | |
| 35 from twisted.internet import posixbase, selectreactor | |
| 36 | |
| 37 | |
| 38 class GtkReactor(posixbase.PosixReactorBase): | |
| 39 """ | |
| 40 GTK+ event loop reactor. | |
| 41 | |
| 42 @ivar _reads: A dictionary mapping L{FileDescriptor} instances to gtk INPUT_
READ | |
| 43 watch handles. | |
| 44 | |
| 45 @ivar _writes: A dictionary mapping L{FileDescriptor} instances to gtk | |
| 46 INTPUT_WRITE watch handles. | |
| 47 | |
| 48 @ivar _simtag: A gtk timeout handle for the next L{simulate} call. | |
| 49 """ | |
| 50 implements(IReactorFDSet) | |
| 51 | |
| 52 def __init__(self): | |
| 53 """ | |
| 54 Initialize the file descriptor tracking dictionaries and the base | |
| 55 class. | |
| 56 """ | |
| 57 self._simtag = None | |
| 58 self._reads = {} | |
| 59 self._writes = {} | |
| 60 posixbase.PosixReactorBase.__init__(self) | |
| 61 | |
| 62 | |
| 63 def addReader(self, reader): | |
| 64 if reader not in self._reads: | |
| 65 self._reads[reader] = gtk.input_add(reader, gtk.GDK.INPUT_READ, self
.callback) | |
| 66 | |
| 67 def addWriter(self, writer): | |
| 68 if writer not in self._writes: | |
| 69 self._writes[writer] = gtk.input_add(writer, gtk.GDK.INPUT_WRITE, se
lf.callback) | |
| 70 | |
| 71 | |
| 72 def getReaders(self): | |
| 73 return self._reads.keys() | |
| 74 | |
| 75 | |
| 76 def getWriters(self): | |
| 77 return self._writes.keys() | |
| 78 | |
| 79 | |
| 80 def removeAll(self): | |
| 81 return self._removeAll(self._reads, self._writes) | |
| 82 | |
| 83 def removeReader(self, reader): | |
| 84 if reader in self._reads: | |
| 85 gtk.input_remove(self._reads[reader]) | |
| 86 del self._reads[reader] | |
| 87 | |
| 88 def removeWriter(self, writer): | |
| 89 if writer in self._writes: | |
| 90 gtk.input_remove(self._writes[writer]) | |
| 91 del self._writes[writer] | |
| 92 | |
| 93 doIterationTimer = None | |
| 94 | |
| 95 def doIterationTimeout(self, *args): | |
| 96 self.doIterationTimer = None | |
| 97 return 0 # auto-remove | |
| 98 def doIteration(self, delay): | |
| 99 # flush some pending events, return if there was something to do | |
| 100 # don't use the usual "while gtk.events_pending(): mainiteration()" | |
| 101 # idiom because lots of IO (in particular test_tcp's | |
| 102 # ProperlyCloseFilesTestCase) can keep us from ever exiting. | |
| 103 log.msg(channel='system', event='iteration', reactor=self) | |
| 104 if gtk.events_pending(): | |
| 105 gtk.mainiteration(0) | |
| 106 return | |
| 107 # nothing to do, must delay | |
| 108 if delay == 0: | |
| 109 return # shouldn't delay, so just return | |
| 110 self.doIterationTimer = gtk.timeout_add(int(delay * 1000), | |
| 111 self.doIterationTimeout) | |
| 112 # This will either wake up from IO or from a timeout. | |
| 113 gtk.mainiteration(1) # block | |
| 114 # note: with the .simulate timer below, delays > 0.1 will always be | |
| 115 # woken up by the .simulate timer | |
| 116 if self.doIterationTimer: | |
| 117 # if woken by IO, need to cancel the timer | |
| 118 gtk.timeout_remove(self.doIterationTimer) | |
| 119 self.doIterationTimer = None | |
| 120 | |
| 121 def crash(self): | |
| 122 posixbase.PosixReactorBase.crash(self) | |
| 123 gtk.mainquit() | |
| 124 | |
| 125 def run(self, installSignalHandlers=1): | |
| 126 self.startRunning(installSignalHandlers=installSignalHandlers) | |
| 127 gtk.timeout_add(0, self.simulate) | |
| 128 gtk.mainloop() | |
| 129 | |
| 130 def _readAndWrite(self, source, condition): | |
| 131 # note: gtk-1.2's gtk_input_add presents an API in terms of gdk | |
| 132 # constants like INPUT_READ and INPUT_WRITE. Internally, it will add | |
| 133 # POLL_HUP and POLL_ERR to the poll() events, but if they happen it | |
| 134 # will turn them back into INPUT_READ and INPUT_WRITE. gdkevents.c | |
| 135 # maps IN/HUP/ERR to INPUT_READ, and OUT/ERR to INPUT_WRITE. This | |
| 136 # means there is no immediate way to detect a disconnected socket. | |
| 137 | |
| 138 # The g_io_add_watch() API is more suited to this task. I don't think | |
| 139 # pygtk exposes it, though. | |
| 140 why = None | |
| 141 didRead = None | |
| 142 try: | |
| 143 if condition & gtk.GDK.INPUT_READ: | |
| 144 why = source.doRead() | |
| 145 didRead = source.doRead | |
| 146 if not why and condition & gtk.GDK.INPUT_WRITE: | |
| 147 # if doRead caused connectionLost, don't call doWrite | |
| 148 # if doRead is doWrite, don't call it again. | |
| 149 if not source.disconnected and source.doWrite != didRead: | |
| 150 why = source.doWrite() | |
| 151 didRead = source.doWrite # if failed it was in write | |
| 152 except: | |
| 153 why = sys.exc_info()[1] | |
| 154 log.msg('Error In %s' % source) | |
| 155 log.deferr() | |
| 156 | |
| 157 if why: | |
| 158 self._disconnectSelectable(source, why, didRead == source.doRead) | |
| 159 | |
| 160 def callback(self, source, condition): | |
| 161 log.callWithLogger(source, self._readAndWrite, source, condition) | |
| 162 self.simulate() # fire Twisted timers | |
| 163 return 1 # 1=don't auto-remove the source | |
| 164 | |
| 165 def simulate(self): | |
| 166 """Run simulation loops and reschedule callbacks. | |
| 167 """ | |
| 168 if self._simtag is not None: | |
| 169 gtk.timeout_remove(self._simtag) | |
| 170 self.runUntilCurrent() | |
| 171 timeout = min(self.timeout(), 0.1) | |
| 172 if timeout is None: | |
| 173 timeout = 0.1 | |
| 174 # Quoth someone other than me, "grumble", yet I know not why. Try to be | |
| 175 # more specific in your complaints, guys. -exarkun | |
| 176 self._simtag = gtk.timeout_add(int(timeout * 1010), self.simulate) | |
| 177 | |
| 178 | |
| 179 | |
| 180 class PortableGtkReactor(selectreactor.SelectReactor): | |
| 181 """Reactor that works on Windows. | |
| 182 | |
| 183 input_add is not supported on GTK+ for Win32, apparently. | |
| 184 | |
| 185 @ivar _simtag: A gtk timeout handle for the next L{simulate} call. | |
| 186 """ | |
| 187 _simtag = None | |
| 188 | |
| 189 | |
| 190 def crash(self): | |
| 191 selectreactor.SelectReactor.crash(self) | |
| 192 gtk.mainquit() | |
| 193 | |
| 194 def run(self, installSignalHandlers=1): | |
| 195 self.startRunning(installSignalHandlers=installSignalHandlers) | |
| 196 self.simulate() | |
| 197 gtk.mainloop() | |
| 198 | |
| 199 def simulate(self): | |
| 200 """Run simulation loops and reschedule callbacks. | |
| 201 """ | |
| 202 if self._simtag is not None: | |
| 203 gtk.timeout_remove(self._simtag) | |
| 204 self.iterate() | |
| 205 timeout = min(self.timeout(), 0.1) | |
| 206 if timeout is None: | |
| 207 timeout = 0.1 | |
| 208 | |
| 209 # See comment for identical line in GtkReactor.simulate. | |
| 210 self._simtag = gtk.timeout_add((timeout * 1010), self.simulate) | |
| 211 | |
| 212 | |
| 213 | |
| 214 def install(): | |
| 215 """Configure the twisted mainloop to be run inside the gtk mainloop. | |
| 216 """ | |
| 217 reactor = GtkReactor() | |
| 218 from twisted.internet.main import installReactor | |
| 219 installReactor(reactor) | |
| 220 return reactor | |
| 221 | |
| 222 def portableInstall(): | |
| 223 """Configure the twisted mainloop to be run inside the gtk mainloop. | |
| 224 """ | |
| 225 reactor = PortableGtkReactor() | |
| 226 from twisted.internet.main import installReactor | |
| 227 installReactor(reactor) | |
| 228 return reactor | |
| 229 | |
| 230 if runtime.platform.getType() != 'posix': | |
| 231 install = portableInstall | |
| 232 | |
| 233 __all__ = ['install'] | |
| OLD | NEW |