1 # -*- test-case-name: twisted.internet.test -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
7 This module provides support for Twisted to interact with the glib/gtk2
10 In order to use this support, simply do the following::
12 from twisted.internet import gtk2reactor
15 Then use twisted.internet APIs as usual. The other methods here are not
16 intended to be called directly.
23 from twisted.internet import _glibbase
24 from twisted.python import runtime
26 _glibbase.ensureNotImported(
28 "Introspected and static glib/gtk bindings must not be mixed; can't "
29 "import gtk2reactor since gi module is already imported.",
30 preventImports=["gi"])
33 if not hasattr(sys, 'frozen'):
34 # Don't want to check this for py2exe
37 except (ImportError, AttributeError):
38 pass # maybe we're using pygtk before this hack existed.
41 if hasattr(gobject, "threads_init"):
42 # recent versions of python-gtk expose this. python-gtk=2.4.1
43 # (wrapping glib-2.4.7) does. python-gtk=2.0.0 (wrapping
44 # glib-2.2.3) does not.
45 gobject.threads_init()
49 class Gtk2Reactor(_glibbase.GlibReactorBase):
51 PyGTK+ 2 event loop reactor.
53 _POLL_DISCONNECTED = gobject.IO_HUP | gobject.IO_ERR | gobject.IO_NVAL
54 _POLL_IN = gobject.IO_IN
55 _POLL_OUT = gobject.IO_OUT
57 # glib's iochannel sources won't tell us about any events that we haven't
58 # asked for, even if those events aren't sensible inputs to the poll()
60 INFLAGS = _POLL_IN | _POLL_DISCONNECTED
61 OUTFLAGS = _POLL_OUT | _POLL_DISCONNECTED
63 def __init__(self, useGtk=True):
68 _glibbase.GlibReactorBase.__init__(self, gobject, _gtk, useGtk=useGtk)
72 class PortableGtkReactor(_glibbase.PortableGlibReactorBase):
74 Reactor that works on Windows.
76 Sockets aren't supported by GTK+'s input_add on Win32.
78 def __init__(self, useGtk=True):
83 _glibbase.PortableGlibReactorBase.__init__(self, gobject, _gtk,
87 def install(useGtk=True):
89 Configure the twisted mainloop to be run inside the gtk mainloop.
91 @param useGtk: should glib rather than GTK+ event loop be
92 used (this will be slightly faster but does not support GUI).
94 reactor = Gtk2Reactor(useGtk)
95 from twisted.internet.main import installReactor
96 installReactor(reactor)
100 def portableInstall(useGtk=True):
102 Configure the twisted mainloop to be run inside the gtk mainloop.
104 reactor = PortableGtkReactor()
105 from twisted.internet.main import installReactor
106 installReactor(reactor)
110 if runtime.platform.getType() != 'posix':
111 install = portableInstall
114 __all__ = ['install']