def _set_wm_state(self, *states): # Set property net_wm_state = xlib.XInternAtom(self._x_display, '_NET_WM_STATE', False) atoms = [] for state in states: atoms.append(xlib.XInternAtom(self._x_display, state, False)) atom_type = xlib.XInternAtom(self._x_display, 'ATOM', False) if len(atoms): atoms_ar = (xlib.Atom * len(atoms))(*atoms) xlib.XChangeProperty(self._x_display, self._window, net_wm_state, atom_type, 32, xlib.PropModePrepend, cast(pointer(atoms_ar), POINTER(c_ubyte)), len(atoms)) else: xlib.XDeleteProperty(self._x_display, self._window, net_wm_state) # Nudge the WM e = xlib.XEvent() e.xclient.type = xlib.ClientMessage e.xclient.message_type = net_wm_state e.xclient.display = cast(self._x_display, POINTER(xlib.Display)) e.xclient.window = self._window e.xclient.format = 32 e.xclient.data.l[0] = xlib.PropModePrepend for i, atom in enumerate(atoms): e.xclient.data.l[i + 1] = atom xlib.XSendEvent(self._x_display, self._get_root(), False, xlib.SubstructureRedirectMask, byref(e))
def _event_key(self, ev): if ev.type == xlib.KeyRelease: # Look in the queue for a matching KeyPress with same timestamp, # indicating an auto-repeat rather than actual key event. saved = [] while True: auto_event = xlib.XEvent() result = xlib.XCheckWindowEvent( self._x_display, self._window, xlib.KeyPress | xlib.KeyRelease, byref(auto_event)) if not result: break saved.append(auto_event) if auto_event.type == xlib.KeyRelease: # just save this off for restoration back to the queue continue if ev.xkey.keycode == auto_event.xkey.keycode: # Found a key repeat: dispatch EVENT_TEXT* event symbol = self._event_symbol(ev) modifiers = self._translate_modifiers(ev.xkey.state) text = self._event_text(auto_event) motion = self._event_text_motion(symbol, modifiers) if motion: if modifiers & key.MOD_SHIFT: self.dispatch_event('on_text_motion_select', motion) else: self.dispatch_event('on_text_motion', motion) elif text: self.dispatch_event('on_text', text) ditched = saved.pop() for auto_event in reversed(saved): xlib.XPutBackEvent(self._x_display, byref(auto_event)) return else: # Key code of press did not match, therefore no repeating # is going on, stop searching. break # Whoops, put the events back, it's for real. for auto_event in reversed(saved): xlib.XPutBackEvent(self._x_display, byref(auto_event)) symbol = self._event_symbol(ev) modifiers = self._translate_modifiers(ev.xkey.state) text = self._event_text(ev) motion = self._event_text_motion(symbol, modifiers) if ev.type == xlib.KeyPress: self.dispatch_event('on_key_press', symbol, modifiers) if motion: if modifiers & key.MOD_SHIFT: self.dispatch_event('on_text_motion_select', motion) else: self.dispatch_event('on_text_motion', motion) elif text: self.dispatch_event('on_text', text) elif ev.type == xlib.KeyRelease: self.dispatch_event('on_key_release', symbol, modifiers)
def run(self): self._setup() e = xlib.XEvent() t = 0 sleep_time = 0. self.dispatch_event('on_enter') while not self.has_exit: # Check for already pending events for display in displays: if xlib.XPending(display._display): pending_displays = (display, ) break else: # None found; select on all file descriptors or timeout iwtd = self.get_select_files() pending_displays, _, _ = select.select(iwtd, (), (), sleep_time) # Dispatch platform events for display in pending_displays: while xlib.XPending(display._display): xlib.XNextEvent(display._display, e) # Key events are filtered by the xlib window event # handler so they get a shot at the prefiltered event. if e.xany.type not in (xlib.KeyPress, xlib.KeyRelease): if xlib.XFilterEvent(e, e.xany.window): continue try: window = display._window_map[e.xany.window] except KeyError: continue window.dispatch_platform_event(e) # Dispatch resize events for window in windows: if window._needs_resize: window.switch_to() window.dispatch_event('on_resize', window._width, window._height) window.dispatch_event('on_expose') window._needs_resize = False sleep_time = self.idle() self.dispatch_event('on_exit')
def event_loop(dpy, win): while True: while xlib.XPending(dpy) > 0: event = xlib.XEvent() xlib.XNextEvent(dpy, event) if event.type == xlib.Expose: pass elif event.type == xlib.ConfigureNotify: reshape(event.xconfigure.width, event.xconfigure.height) elif event.type == xlib.KeyPress: pass # TODO #angle += 2.0 draw() glXSwapBuffers(dpy, win)
def _unmap(self): if not self._mapped: return xlib.XSelectInput(self._x_display, self._window, xlib.StructureNotifyMask) xlib.XUnmapWindow(self._x_display, self._window) e = xlib.XEvent() while True: xlib.XNextEvent(self._x_display, e) if e.type == xlib.UnmapNotify: break xlib.XSelectInput(self._x_display, self._window, self._default_event_mask) self._mapped = False
def dispatch_events(self): e = xlib.XEvent() while xlib.XPending(self._display): xlib.XNextEvent(self._display, e) # Key events are filtered by the xlib window event # handler so they get a shot at the prefiltered event. if e.xany.type not in (xlib.KeyPress, xlib.KeyRelease): if xlib.XFilterEvent(e, e.xany.window): continue try: window = self._window_map[e.xany.window] except KeyError: continue window.dispatch_platform_event(e)
def run(self): self._setup() e = xlib.XEvent() t = 0 sleep_time = 0. self.dispatch_event('on_enter') while not self.has_exit: # Check for already pending events for display in pyglet.app.displays: if xlib.XPending(display._display): pending_dispatchers = (display,) break else: # None found; select on all file descriptors or timeout iwtd = self.get_select_files() pending_dispatchers, _, _ = \ select.select(iwtd, (), (), sleep_time) # Dispatch platform events for dispatcher in pending_dispatchers: dispatcher.dispatch_events() # Dispatch resize events # XXX integrate into dispatchers? for window in pyglet.app.windows: if window._needs_resize: window.switch_to() window.dispatch_event('on_resize', window._width, window._height) window.dispatch_event('on_expose') window._needs_resize = False sleep_time = self.idle() self.dispatch_event('on_exit')
def dispatch_events(self): while self._event_queue: EventDispatcher.dispatch_event(self, *self._event_queue.pop(0)) # Dispatch any context-related events if self._lost_context: self._lost_context = False EventDispatcher.dispatch_event(self, 'on_context_lost') if self._lost_context_state: self._lost_context_state = False EventDispatcher.dispatch_event(self, 'on_context_state_lost') self._allow_dispatch_event = True e = xlib.XEvent() # Cache these in case window is closed from an event handler _x_display = self._x_display _window = self._window # Check for the events specific to this window while xlib.XCheckWindowEvent(_x_display, _window, 0x1ffffff, byref(e)): if xlib.XFilterEvent(e, 0): continue event_handler = self._event_handlers.get(e.type) if event_handler: event_handler(e) # Generic events for this window (the window close event). while xlib.XCheckTypedWindowEvent(_x_display, _window, xlib.ClientMessage, byref(e)): event_handler = self._event_handlers.get(e.type) if event_handler: event_handler(e) self._allow_dispatch_event = False
def _map(self): if self._mapped: return # Map the window, wait for map event before continuing. xlib.XSelectInput(self._x_display, self._window, xlib.StructureNotifyMask) xlib.XMapRaised(self._x_display, self._window) e = xlib.XEvent() while True: xlib.XNextEvent(self._x_display, e) if e.type == xlib.MapNotify: break xlib.XSelectInput(self._x_display, self._window, self._default_event_mask) self._mapped = True if self._fullscreen: # Possibly an override_redirect issue. self.activate() self.dispatch_event('on_resize', self._width, self._height) self.dispatch_event('on_show') self.dispatch_event('on_expose')