Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Run the core on the main thread. #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/m64py/core/vidext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
13 changes: 9 additions & 4 deletions src/m64py/frontend/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
3 changes: 1 addition & 2 deletions src/m64py/frontend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
41 changes: 28 additions & 13 deletions src/m64py/frontend/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []
Expand All @@ -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."""
Expand All @@ -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."""
Expand Down Expand Up @@ -341,17 +347,26 @@ 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())
self.core.execute()
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