Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / internet / gtk2reactor.py
1 # -*- test-case-name: twisted.internet.test -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5
6 """
7 This module provides support for Twisted to interact with the glib/gtk2
8 mainloop.
9
10 In order to use this support, simply do the following::
11
12     from twisted.internet import gtk2reactor
13     gtk2reactor.install()
14
15 Then use twisted.internet APIs as usual.  The other methods here are not
16 intended to be called directly.
17 """
18
19 # System Imports
20 import sys
21
22 # Twisted Imports
23 from twisted.internet import _glibbase
24 from twisted.python import runtime
25
26 _glibbase.ensureNotImported(
27     ["gi"],
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"])
31
32 try:
33     if not hasattr(sys, 'frozen'):
34         # Don't want to check this for py2exe
35         import pygtk
36         pygtk.require('2.0')
37 except (ImportError, AttributeError):
38     pass # maybe we're using pygtk before this hack existed.
39
40 import gobject
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()
46
47
48
49 class Gtk2Reactor(_glibbase.GlibReactorBase):
50     """
51     PyGTK+ 2 event loop reactor.
52     """
53     _POLL_DISCONNECTED = gobject.IO_HUP | gobject.IO_ERR | gobject.IO_NVAL
54     _POLL_IN = gobject.IO_IN
55     _POLL_OUT = gobject.IO_OUT
56
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()
59     # call.
60     INFLAGS = _POLL_IN | _POLL_DISCONNECTED
61     OUTFLAGS = _POLL_OUT | _POLL_DISCONNECTED
62
63     def __init__(self, useGtk=True):
64         _gtk = None
65         if useGtk is True:
66             import gtk as _gtk
67
68         _glibbase.GlibReactorBase.__init__(self, gobject, _gtk, useGtk=useGtk)
69
70
71
72 class PortableGtkReactor(_glibbase.PortableGlibReactorBase):
73     """
74     Reactor that works on Windows.
75
76     Sockets aren't supported by GTK+'s input_add on Win32.
77     """
78     def __init__(self, useGtk=True):
79         _gtk = None
80         if useGtk is True:
81             import gtk as _gtk
82
83         _glibbase.PortableGlibReactorBase.__init__(self, gobject, _gtk,
84                                                    useGtk=useGtk)
85
86
87 def install(useGtk=True):
88     """
89     Configure the twisted mainloop to be run inside the gtk mainloop.
90
91     @param useGtk: should glib rather than GTK+ event loop be
92         used (this will be slightly faster but does not support GUI).
93     """
94     reactor = Gtk2Reactor(useGtk)
95     from twisted.internet.main import installReactor
96     installReactor(reactor)
97     return reactor
98
99
100 def portableInstall(useGtk=True):
101     """
102     Configure the twisted mainloop to be run inside the gtk mainloop.
103     """
104     reactor = PortableGtkReactor()
105     from twisted.internet.main import installReactor
106     installReactor(reactor)
107     return reactor
108
109
110 if runtime.platform.getType() != 'posix':
111     install = portableInstall
112
113
114 __all__ = ['install']