~aw/sabayon-template/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#
# Copyright (C) 2005 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import gobject
import gtk
import gtk.gdk
import xlib
import util
import debuglog

def dprint (fmt, *args):
    debuglog.debug_log (False, debuglog.DEBUG_LOG_DOMAIN_SESSION_WIDGET, fmt % args)
    
def session_window_subwindow_created_cb (xid, user_data):
    self = user_data

    dprint ("subwindow got created with xid %x", xid)

    self.xephyr_window = gtk.gdk.window_foreign_new_for_display (gtk.gdk.display_get_default (), xid)
    dprint ("gdk foreign window created, %s", self.xephyr_window)

class SessionWidget (gtk.Widget):
    # FIXME:
    #   Should be able override do_size_allocate() without this
    #   See bug #308099
    __gsignals__ = { 'size-allocate' : 'override' }
    
    def __init__ (self, session_width, session_height):
        gtk.Widget.__init__ (self)
        
        self.set_flags (self.flags() | gtk.NO_WINDOW)
        self.set_property ("can-focus", True)

        self.session_width  = session_width
        self.session_height = session_height

        self.xephyr_window = None

    def do_size_request (self, requisition):
        focus_width = self.style_get_property ("focus-line-width")
        focus_pad   = self.style_get_property ("focus-padding")
        
        requisition.width  = self.session_width  + 2 * focus_width + 2 * focus_pad
        requisition.height = self.session_height + 2 * focus_width + 2 * focus_pad

    def __calculate_position (self, allocation):
        focus_width = self.style_get_property ("focus-line-width")
        focus_pad   = self.style_get_property ("focus-padding")

        x = allocation.x + focus_width + focus_pad
        y = allocation.y + focus_width + focus_pad
            
        alloc_width  = allocation.width  - 2 * focus_width - 2 * focus_pad
        alloc_height = allocation.height - 2 * focus_width - 2 * focus_pad

        x += max (0, (alloc_width  - self.session_width)  / 2)
        y += max (0, (alloc_height - self.session_height) / 2)

        return (x, y)

    def do_size_allocate (self, allocation):
        self.allocation = allocation

        if self.flags() & gtk.REALIZED:
            (x, y) = self.__calculate_position (allocation)
            
            self.session_window.move_resize (x, y, self.session_width, self.session_height)
            self.input_window.move_resize   (x, y, self.session_width, self.session_height)

    def do_realize (self):
        gtk.Widget.do_realize (self)

        (x, y) = self.__calculate_position (self.allocation)

        self.session_window = gtk.gdk.Window (parent = self.get_parent_window (),
                                              window_type = gtk.gdk.WINDOW_CHILD,
                                              x = x,
                                              y = y,
                                              width  = self.session_width,
                                              height = self.session_height,
                                              wclass = gtk.gdk.INPUT_OUTPUT,
                                              visual = self.get_visual (),
                                              colormap = self.get_colormap (),
                                              event_mask = gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.SUBSTRUCTURE_MASK)
        self.session_window.set_user_data (self)
        dprint ("session window has xid %x", self.session_window.xid)

        # We use Xephyr's "-parent" option to tell it where to create
        # its window.  However, Xephyr does not use our window
        # directly for drawing.  Instead, it creates a subwindow in
        # our parent window, and uses *that*.  So, the events that we
        # proxy to Xephyr must be sent to the window that it creates.
        #
        # To get notified when Xephyr creates its window inside ours,
        # we select for gtk.gdk.SUBSTRUCTURE_MASK above.  However,
        # there's no GDK_* event for CreateNotify events.  In an ideal
        # world, we could just install a window event filter to
        # extract that event ourselves.
        #
        # However, PyGTK is buggy and doesn't implement
        # gdk_window_add_filter() correctly (see
        # http://bugzilla.gnome.org/show_bug.cgi?id=156948).  So, we
        # use our own wrapper for that function...
        xlib.window_capture_create_notify (self.session_window, session_window_subwindow_created_cb, self)

        event_mask = gtk.gdk.BUTTON_PRESS_MASK   | \
                     gtk.gdk.BUTTON_RELEASE_MASK | \
                     gtk.gdk.POINTER_MOTION_MASK | \
                     gtk.gdk.ENTER_NOTIFY_MASK   | \
                     gtk.gdk.LEAVE_NOTIFY_MASK   | \
                     gtk.gdk.KEY_PRESS_MASK      | \
                     gtk.gdk.KEY_RELEASE_MASK

        self.input_window = gtk.gdk.Window (parent = self.get_parent_window (),
                                            window_type = gtk.gdk.WINDOW_CHILD,
                                            x = x,
                                            y = y,
                                            width  = self.session_width,
                                            height = self.session_height,
                                            wclass = gtk.gdk.INPUT_ONLY,
                                            visual = self.get_visual (),
                                            colormap = self.get_colormap (),
                                            event_mask = event_mask)
        self.input_window.set_user_data (self)
        dprint ("Created input_only window for child session")

    def do_unrealize (self):
        # FIXME:
        #   Causes a warning with pygtk 2.6.2
        #   See bug #308384
        self.input_window.set_user_data (None)
        self.input_window.destroy ()
        dprint ("Destroyed input_only window for child session")
        
        self.session_window.set_user_data (None)
        self.session_window.destroy ()
        
        gtk.Widget.do_unrealize (self)

    def do_map (self):
        gtk.Widget.do_map (self)
        self.session_window.show ()
        self.input_window.show ()
        dprint ("Showed input_only window for child session")

    def do_unmap (self):
        self.input_window.hide ()
        self.session_window.hide ()
        gtk.Widget.do_unmap (self)
        dprint ("Hid input_only window for child session")

    def do_expose_event (self, event):
        if self.flags() & gtk.HAS_FOCUS:
            focus_width = self.style_get_property ("focus-line-width")
            focus_pad   = self.style_get_property ("focus-padding")
            
            (x, y) = self.__calculate_position (self.allocation)

            self.style.paint_focus (self.window,
                                    self.state,
                                    event.area,
                                    self,
                                    "sabayon-session",
                                    x - focus_width - focus_pad,
                                    y - focus_width - focus_pad,
                                    self.session_width  + 2 * focus_width + 2 * focus_pad,
                                    self.session_height + 2 * focus_width + 2 * focus_pad)
        return False

    def __update_input_only_window (self):
        if not self.flags() & gtk.REALIZED:
            return
        
        if self.flags() & gtk.HAS_FOCUS:
            self.input_window.hide ()
            dprint ("Focus in: hiding input_only window for child session")
        else:
            self.input_window.show ()
            dprint ("Focus out: showing input_only window for child session")
            
    def do_focus_in_event (self, event):
        self.__update_input_only_window ()
        return gtk.Widget.do_focus_in_event (self, event)

    def do_focus_out_event (self, event):
        self.__update_input_only_window ()
        return gtk.Widget.do_focus_in_event (self, event)

    def do_button_press_event (self, event):
        if not self.flags() & gtk.HAS_FOCUS:
            self.grab_focus ()
            
        if not event.send_event:
            if self.xephyr_window:
                dprint ("Resending button press; button = %d, state = 0x%x", event.button, event.state)
                xlib.send_button_event (self.xephyr_window, True, event.time, event.button, event.state)
        
        return True
    
    def do_button_release_event (self, event):
        if not event.send_event:
            if self.xephyr_window:
                dprint ("Resending button release; button = %d, state = 0x%x", event.button, event.state)
                xlib.send_button_event (self.xephyr_window, False, event.time, event.button, event.state)
        
        return True

    def do_motion_notify_event (self, event):
        if not event.send_event:
            if self.xephyr_window:
                dprint ("Resending motion notify; x = %d, y = %d", event.x, event.y)
                xlib.send_motion_event (self.xephyr_window, event.time, event.x, event.y);
        
        return True
    
    def do_enter_notify_event (self, event):
        if not event.send_event:
            if self.xephyr_window:
                dprint ("Resending enter notify; x = %d, y = %d, detail = %d", event.x, event.y, event.detail)
                xlib.send_crossing_event (self.xephyr_window, False, event.time, event.x, event.y, event.detail);
        
        return True
    
    def do_leave_notify_event (self, event):
        if not event.send_event:
            if self.xephyr_window:
                dprint ("Resending leave notify; x = %d, y = %d, detail = %d", event.x, event.y, event.detail)
                xlib.send_crossing_event (self.xephyr_window, False, event.time, event.x, event.y, event.detail);
        
        return True
    
    def do_key_press_event (self, event):
        if not event.send_event:
            if self.xephyr_window:
                dprint ("Resending key press; keycode = 0x%x, state = 0x%x", event.hardware_keycode, event.state)
                xlib.send_key_event (self.xephyr_window, True, event.time, event.hardware_keycode, event.state)
        
        return True

    def do_key_release_event (self, event):
        if not event.send_event:
            if self.xephyr_window:
                dprint ("Resending key release; keycode = 0x%x, state = 0x%x", event.hardware_keycode, event.state)
                xlib.send_key_event (self.xephyr_window, False, event.time, event.hardware_keycode, event.state)
        
        return True

gobject.type_register (SessionWidget)