From 92f654564af5d2e5822e7ce81738464214343d16 Mon Sep 17 00:00:00 2001 From: comex Date: Mon, 29 May 2017 18:27:07 -0400 Subject: [PATCH] Run the core on the main thread. --- src/m64py/core/vidext.py | 2 ++ src/m64py/frontend/mainwindow.py | 13 ++++++---- src/m64py/frontend/settings.py | 3 +-- src/m64py/frontend/worker.py | 41 ++++++++++++++++++++++---------- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/m64py/core/vidext.py b/src/m64py/core/vidext.py index a593ca4..0e73e2e 100644 --- a/src/m64py/core/vidext.py +++ b/src/m64py/core/vidext.py @@ -24,6 +24,7 @@ glimport = False from PyQt5.QtOpenGL import QGLFormat +from PyQt5.QtCore import QCoreApplication from sdl2 import SDL_WasInit, SDL_InitSubSystem, SDL_QuitSubSystem, SDL_INIT_VIDEO from sdl2 import SDL_GetNumDisplayModes, SDL_DisplayMode, SDL_GetDisplayMode @@ -165,6 +166,7 @@ def gl_swap_buf(self): """Swaps the front/back buffers after rendering an output video frame. """ self.widget.swapBuffers() + QCoreApplication.processEvents() return M64ERR_SUCCESS def resize_window(self, width, height): diff --git a/src/m64py/frontend/mainwindow.py b/src/m64py/frontend/mainwindow.py index f845bbc..70fc295 100644 --- a/src/m64py/frontend/mainwindow.py +++ b/src/m64py/frontend/mainwindow.py @@ -233,15 +233,20 @@ def create_size_actions(self): def on_file_open(self, filepath=None, filename=None): """Opens ROM file.""" + if self.worker.is_stopping: + return if not filepath: action = self.sender() filepath = action.data() self.worker.core_state_query(M64CORE_EMU_STATE) + def after(): + self.worker.set_filepath(filepath, filename) + self.worker.start() + self.raise_() if self.worker.state in [M64EMU_RUNNING, M64EMU_PAUSED]: - self.worker.stop() - self.worker.set_filepath(filepath, filename) - self.worker.start() - self.raise_() + self.worker.stop(and_then=after) + else: + after() def update_status(self, status): """Updates label in status bar.""" diff --git a/src/m64py/frontend/settings.py b/src/m64py/frontend/settings.py index 7f1e3c8..dcae63d 100644 --- a/src/m64py/frontend/settings.py +++ b/src/m64py/frontend/settings.py @@ -103,8 +103,7 @@ def on_vidext_changed(self, state): self.parent.vidext = state self.comboResolution.setEnabled(not self.parent.vidext) self.checkFullscreen.setEnabled(not self.parent.vidext) - self.parent.worker.quit() - self.parent.worker.init() + self.parent.worker.quit(and_then=self.parent.worker.init) def connect_signals(self): self.browseLibrary.clicked.connect(lambda: self.browse_dialog( diff --git a/src/m64py/frontend/worker.py b/src/m64py/frontend/worker.py index 29cfb0a..0674316 100644 --- a/src/m64py/frontend/worker.py +++ b/src/m64py/frontend/worker.py @@ -17,7 +17,7 @@ import os import shutil -from PyQt5.QtCore import QThread, QTimer +from PyQt5.QtCore import QObject, QTimer from sdl2 import SDL_EnableScreenSaver, SDL_DisableScreenSaver from m64py.utils import sl @@ -30,12 +30,12 @@ from m64py.platform import DLL_EXT, DEFAULT_DYNLIB, SEARCH_DIRS -class Worker(QThread): - """Mupen64Plus thread worker""" +class Worker(QObject): + """Mupen64Plus worker""" def __init__(self, parent=None): """Constructor.""" - QThread.__init__(self, parent) + QObject.__init__(self, parent) self.parent = parent self.video = video self.plugin_files = [] @@ -46,6 +46,7 @@ def __init__(self, parent=None): self.state = M64EMU_STOPPED self.settings = self.parent.settings self.core = Core() + self.is_stopping = False def init(self, path=None): """Initialize.""" @@ -62,14 +63,19 @@ def init(self, path=None): self.parent.state_changed.emit((False, False, False, False)) self.parent.info_dialog.emit(self.tr("Mupen64Plus library not found.")) - def quit(self): + def quit(self, and_then=None): + def after(): + if self.core.get_handle(): + self.plugins_shutdown() + self.plugins_unload() + self.core_shutdown() + self.core_unload() + if and_then is not None: + and_then() if self.state in [M64EMU_RUNNING, M64EMU_PAUSED]: self.stop() - if self.core.get_handle(): - self.plugins_shutdown() - self.plugins_unload() - self.core_shutdown() - self.core_unload() + else: + after() def set_filepath(self, filepath, filename=None): """Sets rom file path.""" @@ -341,13 +347,16 @@ def toggle_actions(self): (load, pause, action, cheats) = True, True, True, cheat self.parent.state_changed.emit((load, pause, action, cheats)) - def stop(self): + def stop(self, and_then=None): """Stops thread.""" self.core.stop() - self.wait() + self.is_stopping = True + self.after_stop = and_then + + def start(self): + QTimer.singleShot(0, self.run) def run(self): - """Starts thread.""" self.rom_open() self.core.attach_plugins( self.get_plugins()) @@ -355,3 +364,9 @@ def run(self): self.core.detach_plugins() self.rom_close() self.toggle_actions() + if self.is_stopping: + after_stop = self.after_stop + del self.after_stop + if after_stop is not None: + after_stop() + self.is_stopping = False