def exit_event_loop(self): '''The exit_event_loop method is thread safe and is safe to call even if the event loop is not running. Calling exit_event_loop pushes a quit request onto the SDL event queue, causing self.event_loop() to exit gracefully (IE, return) if it is running.''' event = sdl2.SDL_Event() event.type = sdl2.SDL_QUIT sdl2.SDL_PushEvent(ctypes.byref(event))
def thread_target(): try: input_thread(readfunc, ringbuf, nbins, overlap, wf) except Exception as e: err_queue.put(e) event = sdl2.SDL_Event() event.type = sdl2.SDL_QUIT sdl2.SDL_PushEvent(event)
def emit(self, code: int = 0, data: typing.Any = None) -> sdl2.SDL_Event: event = sdl2.SDL_Event() sdl2.SDL_memset(ctypes.byref(event), 0, ctypes.sizeof(sdl2.SDL_Event)) event.type = self.type event.user.code = code event.user.data1 = ctypes.cast(ctypes.pointer(ctypes.py_object(data)), ctypes.c_void_p) if sdl2.SDL_PushEvent(event) != 1: raise sdl2.ext.SDLError() return event
def stop_event_wait(): """Post an event to break out of the event loop wait.""" try: user_event = sdl2.SDL_Event() user_event.type = sdl2.SDL_USEREVENT user_event.user.code = 2 user_event.user.data1 = None user_event.user.data2 = None sdl2.SDL_PushEvent(ctypes.byref(user_event)) except: pass
def pushPesEvent(eventType, data1="", data2=""): global EVENT_TYPE if EVENT_TYPE == None: logging.error("pushPesEvent: PES Event Type has not been registered!") return False pesEvent = sdl2.SDL_Event() pesEvent.type = EVENT_TYPE pesEvent.user.code = eventType pesEvent.user.data1 = cast(c_char_p(data1), c_void_p) pesEvent.user.data2 = cast(c_char_p(data2), c_void_p) logging.debug("pushPesEvent: pushing event (%d, %s, %s)" % (eventType, data1, data2)) sdl2.SDL_PushEvent(pesEvent) return True
def _on_axes_throttle_delay_expired_timer_callback(self, interval, _): # NB: SDL timer callbacks execute on a special thread that is not the main thread if not self.event_loop_is_running: return with self._axes_throttle_delay_lock: self._axes_throttle_delay_timer_set = False if sdl2.SDL_GetTicks() < self._next_axes_tick: if self.warnings_enabled: print('Axes throttling delay expiration callback pre-empted.', sys.stderr) return 0 event = sdl2.SDL_Event() event.type = self.AXES_THROTTLE_DELAY_EXPIRED_EVENT sdl2.SDL_PushEvent(event) # Returning 0 tells SDL to not recycle this timer. _handle_axes_motion, in the main SDL thread, will # ultimately be caused to set a new timer by the event we just pushed. return 0
def input_thread(readfunc, ringbuf, nbins, overlap, viewer): window = 0.5 * (1.0 - np.cos((2 * math.pi * np.arange(nbins)) / nbins)) #ringbuf.append(np.fromfile(fil, count=ringbuf.headlen, dtype=np.complex64)) ringbuf.append(np.frombuffer(readfunc(ringbuf.headlen * 8), dtype=np.complex64)) while True: #ringbuf.append(np.fromfile(fil, count=nbins - overlap, dtype=np.complex64)) ringbuf.append(np.frombuffer(readfunc((nbins - overlap) * 8), dtype=np.complex64)) frame = ringbuf.slice(ringbuf.fill_edge - nbins, ringbuf.fill_edge) spectrum = np.absolute(np.fft.fft(np.multiply(frame, window))) spectrum = np.concatenate((spectrum[nbins//2:nbins], spectrum[0:nbins//2])) spectrum = np.log10(spectrum) * 10 viewer.inserts.put(spectrum / 20.0) sdl2.SDL_PushEvent(UPDATE_EVENT)
def run(self): self.running = True array = sdl2.ext.pixels2d(self.sprite) for i in range(array.size): while self.running: try: pix = self.dp.read_pixel() array.flat[i] = int.from_bytes( pack("3B", *self.dp.raw_to_rgb(pix, 'absolute')), "big") sdl2.SDL_PushEvent(ctypes.byref(self.event)) except io.BlockingIOError: continue break self.completed_part = self.sprite.subsprite( (0, 0, int(i / self.sprite.size[0]), self.sprite.size[1])) if self.running is False: break
def callback(unused, buf, buflen): global audio_edge bufbuf = pybuf_from_memory(buf, buflen, 0x200) # PyBUF_WRITE array = np.frombuffer(bufbuf, np.float32) assert len(array) % filt.interp == 0 # TODO nreqframes = len(array) // filt.interp loc_ringbuf_edge = ringbuf.fill_edge if loc_ringbuf_edge < 0 or (loc_ringbuf_edge - audio_edge) % len(ringbuf) < nreqframes: print("audio underrun", file=sys.stderr) array.fill(0) return # TODO if audio_edge + nreqframes > len(ringbuf): audio_edge = 0 slic = ringbuf.slice(audio_edge - filt.nhistory, audio_edge + nreqframes) array[:] = np.real(freqx(filt(slic))) * wf.volume audio_edge += nreqframes sdl2.SDL_PushEvent(UPDATE_EVENT)
def run(self): self._running = True while self._running: if self._window.can_rotate: sdl2.SDL_PushEvent(self._rotate_event_pointer) self._receive_events()
def queue_event(e): ret = sdl2.SDL_PushEvent(byref(e)) if ret != 1: raise ValueError("Unable to add event to queue.")