Skip to content

Commit 2730960

Browse files
committed
full fledged callback support
1 parent da43aab commit 2730960

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

examples/play.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from pyrilla import internal
44
from time import sleep
55

6-
def finished(*args):
7-
print("finished")
6+
def finished(sound):
7+
print("finished:", sound)
8+
quit()
89

910
go_on = False
1011

extensions/internal.pyx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from cython.operator cimport dereference as deref
2-
from python_ref cimport Py_DECREF, Py_INCREF
2+
from cpython cimport Py_DECREF, Py_INCREF
33

44
cimport ga
55
cimport gau
@@ -21,14 +21,21 @@ def update():
2121

2222

2323
cdef on_finish_callback(ga.Handle* in_handle, void* in_context):
24-
(<object>in_context)()
25-
print "after callback"
24+
cdef CallbackContext context = <CallbackContext> in_context
25+
context.callback(context.sound)
26+
# note: it was casted on void* so we need to manually decrease
27+
# reference counter
28+
Py_DECREF(context)
2629
ga.handle_destroy(in_handle)
2730

2831

29-
ctypedef struct CallbackContext:
30-
void* callback
31-
void* sound
32+
cdef class CallbackContext(object):
33+
cdef object callback
34+
cdef Sound sound
35+
36+
def __cinit__(self, object callback, Sound sound):
37+
self.callback = callback
38+
self.sound = sound
3239

3340

3441
cdef class Sound(object):
@@ -52,13 +59,20 @@ cdef class Sound(object):
5259

5360
def play(self, on_finish=None):
5461
cdef ga.Handle* handle
62+
cdef CallbackContext context
5563

5664
if on_finish:
65+
context = CallbackContext(on_finish, self)
66+
67+
# note: we are going to cast on void* so we need to manually
68+
# control reference counters
69+
Py_INCREF(context)
70+
5771
handle = gau.create_handle_sound(
5872
global_mixer,
5973
self.sound,
6074
<ga.FinishCallback>&on_finish_callback,
61-
<void*>on_finish,
75+
<void*>context,
6276
NULL
6377
)
6478
else:

0 commit comments

Comments
 (0)