mirror of
https://github.com/chatmail/core.git
synced 2026-04-06 15:42:10 +03:00
minimal get_next_event api
This commit is contained in:
@@ -13,7 +13,6 @@ except DistributionNotFound:
|
||||
_DC_CALLBACK_MAP = {}
|
||||
|
||||
|
||||
@capi.ffi.def_extern()
|
||||
def py_dc_callback(ctx, evt, data1, data2):
|
||||
"""The global event handler.
|
||||
|
||||
@@ -32,9 +31,9 @@ def py_dc_callback(ctx, evt, data1, data2):
|
||||
evt_name = get_dc_event_name(evt)
|
||||
event_sig_types = capi.lib.dc_get_event_signature_types(evt)
|
||||
if data1 and event_sig_types & 1:
|
||||
data1 = ffi.string(ffi.cast('char*', data1)).decode("utf8")
|
||||
data1 = ffi.string(ffi.gc(ffi.cast('char*', data1), capi.lib.dc_str_unref)).decode("utf8")
|
||||
if data2 and event_sig_types & 2:
|
||||
data2 = ffi.string(ffi.cast('char*', data2)).decode("utf8")
|
||||
data2 = ffi.string(ffi.gc(ffi.cast('char*', data2), capi.lib.dc_str_unref)).decode("utf8")
|
||||
try:
|
||||
if isinstance(data2, bytes):
|
||||
data2 = data2.decode("utf8")
|
||||
@@ -43,18 +42,9 @@ def py_dc_callback(ctx, evt, data1, data2):
|
||||
# i don't want to hunt down encoding problems in the c lib
|
||||
pass
|
||||
try:
|
||||
ret = callback(ctx, evt_name, data1, data2)
|
||||
if ret is None:
|
||||
ret = 0
|
||||
assert isinstance(ret, int), repr(ret)
|
||||
if event_sig_types & 4:
|
||||
return ffi.cast('uintptr_t', ret)
|
||||
elif event_sig_types & 8:
|
||||
return ffi.cast('int', ret)
|
||||
callback(ctx, evt_name, data1, data2)
|
||||
except: # noqa
|
||||
raise
|
||||
ret = 0
|
||||
return ret
|
||||
|
||||
|
||||
def set_context_callback(dc_context, func):
|
||||
|
||||
@@ -92,13 +92,6 @@ def ffibuilder():
|
||||
finally:
|
||||
shutil.rmtree(tmpdir)
|
||||
|
||||
builder.cdef("""
|
||||
extern "Python" uintptr_t py_dc_callback(
|
||||
dc_context_t* context,
|
||||
int event,
|
||||
uintptr_t data1,
|
||||
uintptr_t data2);
|
||||
""")
|
||||
return builder
|
||||
|
||||
|
||||
|
||||
@@ -565,12 +565,14 @@ class IOThreads:
|
||||
self._thread_quitflag = False
|
||||
self._name2thread = {}
|
||||
self._log_event = log_event
|
||||
self._running = False
|
||||
|
||||
def is_started(self):
|
||||
return len(self._name2thread) > 0
|
||||
return self._running
|
||||
|
||||
def start(self, imap=True, smtp=True, mvbox=False, sentbox=False):
|
||||
assert not self.is_started()
|
||||
self._running = True
|
||||
self._start_one_thread("deltachat", self.dc_thread_run)
|
||||
|
||||
def _start_one_thread(self, name, func):
|
||||
@@ -583,11 +585,25 @@ class IOThreads:
|
||||
if wait:
|
||||
for name, thread in self._name2thread.items():
|
||||
thread.join()
|
||||
self._running = False
|
||||
|
||||
def dc_thread_run(self):
|
||||
self._log_event("py-bindings-info", 0, "DC THREAD START")
|
||||
|
||||
lib.dc_context_run(self._dc_context, lib.py_dc_callback)
|
||||
|
||||
lib.dc_context_run(self._dc_context)
|
||||
while self._running:
|
||||
if lib.dc_has_next_event(self._dc_context):
|
||||
event = lib.dc_get_next_event(self._dc_context)
|
||||
if event != ffi.NULL:
|
||||
deltachat.py_dc_callback(
|
||||
self._dc_context,
|
||||
lib.dc_event_get_id(event),
|
||||
lib.dc_event_get_data1(event),
|
||||
lib.dc_event_get_data2(event)
|
||||
)
|
||||
lib.dc_event_unref(event)
|
||||
else:
|
||||
time.sleep(0.05)
|
||||
|
||||
self._log_event("py-bindings-info", 0, "DC THREAD FINISHED")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user