This commit is contained in:
Kovid Goyal 2020-11-19 20:44:57 +05:30
parent ab377cfda5
commit fbb259b86b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 11 additions and 9 deletions

View File

@ -18,17 +18,19 @@ class Client:
self.current_callback = None self.current_callback = None
def __del__(self): def __del__(self):
self.sp_voice.shutdown_event_loop() if self.sp_voice is not None:
self.events_thread.join(5) self.sp_voice.shutdown_event_loop()
self.sp_voice = None self.events_thread.join(5)
self.sp_voice = None
shutdown = __del__ shutdown = __del__
def wait_for_events(self): def wait_for_events(self):
while True: while True:
if self.sp_voice.wait_for_event() is False: if self.sp_voice.wait_for_event() is False:
break break
if self.current_callback is not None: c = self.current_callback
self.current_callback() if c is not None:
c()
def get_events(self): def get_events(self):
from calibre_extensions.winsapi import SPEI_TTS_BOOKMARK, SPEI_START_INPUT_STREAM, SPEI_END_INPUT_STREAM from calibre_extensions.winsapi import SPEI_TTS_BOOKMARK, SPEI_START_INPUT_STREAM, SPEI_END_INPUT_STREAM

View File

@ -340,7 +340,7 @@ Voice_shutdown_event_loop(Voice *self, PyObject *args) {
} }
static PyObject* static PyObject*
get_events(Voice *self, PyObject *args) { Voice_get_events(Voice *self, PyObject *args) {
HRESULT hr; HRESULT hr;
const ULONG asz = 32; const ULONG asz = 32;
ULONG num_events; ULONG num_events;
@ -383,9 +383,9 @@ get_events(Voice *self, PyObject *args) {
} }
static PyObject* static PyObject*
Voice_wait_for_event(Voice *self, PyObject *callback) { Voice_wait_for_event(Voice *self, PyObject *args) {
if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "callback object is not callable"); return NULL; }
const HANDLE handles[2] = {self->shutdown_events_thread, self->events_available}; const HANDLE handles[2] = {self->shutdown_events_thread, self->events_available};
DWORD ev;
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
ev = WaitForMultipleObjects(2, handles, true, INFINITE); ev = WaitForMultipleObjects(2, handles, true, INFINITE);
Py_END_ALLOW_THREADS; Py_END_ALLOW_THREADS;
@ -420,7 +420,7 @@ static PyMethodDef Voice_methods[] = {
M(set_current_sound_output, METH_VARARGS), M(set_current_sound_output, METH_VARARGS),
M(shutdown_event_loop, METH_NOARGS), M(shutdown_event_loop, METH_NOARGS),
M(wait_for_event, METH_O), M(wait_for_event, METH_NOARGS),
M(get_events, METH_NOARGS), M(get_events, METH_NOARGS),
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };