pulse: Add pa_operation_set_state_callback() API

[The original commit message didn't have any explanation why this
change is made, so I'll add that information here myself.
--Tanu Kaskinen]

This change is from the developers of a Haskell binding[1]. According
to them, this change isn't strictly necessary, but their code gets
significantly cleaner if they can register an operation callback that
is called when the operation is cancelled due to the context getting
disconnected.

[1] https://github.com/favonia/pulse
This commit is contained in:
Paul Meng 2013-01-07 21:41:26 +08:00 committed by Tanu Kaskinen
parent b1e47df72c
commit a255c29344
4 changed files with 30 additions and 0 deletions

View file

@ -220,6 +220,7 @@ pa_msleep;
pa_operation_cancel; pa_operation_cancel;
pa_operation_get_state; pa_operation_get_state;
pa_operation_ref; pa_operation_ref;
pa_operation_set_state_callback;
pa_operation_unref; pa_operation_unref;
pa_parse_sample_format; pa_parse_sample_format;
pa_path_get_filename; pa_path_get_filename;

View file

@ -234,6 +234,8 @@ struct pa_operation {
pa_operation_state_t state; pa_operation_state_t state;
void *userdata; void *userdata;
pa_operation_cb_t callback; pa_operation_cb_t callback;
void *state_userdata;
pa_operation_notify_cb_t state_callback;
void *private; /* some operations might need this */ void *private; /* some operations might need this */
}; };

View file

@ -26,6 +26,7 @@
#include <pulse/xmalloc.h> #include <pulse/xmalloc.h>
#include <pulsecore/macro.h> #include <pulsecore/macro.h>
#include <pulsecore/flist.h> #include <pulsecore/flist.h>
#include <pulse/fork-detect.h>
#include "internal.h" #include "internal.h"
#include "operation.h" #include "operation.h"
@ -91,6 +92,8 @@ static void operation_unlink(pa_operation *o) {
o->stream = NULL; o->stream = NULL;
o->callback = NULL; o->callback = NULL;
o->userdata = NULL; o->userdata = NULL;
o->state_callback = NULL;
o->state_userdata = NULL;
} }
static void operation_set_state(pa_operation *o, pa_operation_state_t st) { static void operation_set_state(pa_operation *o, pa_operation_state_t st) {
@ -104,6 +107,9 @@ static void operation_set_state(pa_operation *o, pa_operation_state_t st) {
o->state = st; o->state = st;
if (o->state_callback)
o->state_callback(o, o->state_userdata);
if ((o->state == PA_OPERATION_DONE) || (o->state == PA_OPERATION_CANCELED)) if ((o->state == PA_OPERATION_DONE) || (o->state == PA_OPERATION_CANCELED))
operation_unlink(o); operation_unlink(o);
@ -130,3 +136,17 @@ pa_operation_state_t pa_operation_get_state(pa_operation *o) {
return o->state; return o->state;
} }
void pa_operation_set_state_callback(pa_operation *o, pa_operation_notify_cb_t cb, void *userdata) {
pa_assert(o);
pa_assert(PA_REFCNT_VALUE(o) >= 1);
if (pa_detect_fork())
return;
if (o->state == PA_OPERATION_DONE || o->state == PA_OPERATION_CANCELED)
return;
o->state_callback = cb;
o->state_userdata = userdata;
}

View file

@ -34,6 +34,9 @@ PA_C_DECL_BEGIN
/** An asynchronous operation object */ /** An asynchronous operation object */
typedef struct pa_operation pa_operation; typedef struct pa_operation pa_operation;
/** A callback for operation state changes */
typedef void (*pa_operation_notify_cb_t) (pa_operation *o, void *userdata);
/** Increase the reference count by one */ /** Increase the reference count by one */
pa_operation *pa_operation_ref(pa_operation *o); pa_operation *pa_operation_ref(pa_operation *o);
@ -50,6 +53,10 @@ void pa_operation_cancel(pa_operation *o);
/** Return the current status of the operation */ /** Return the current status of the operation */
pa_operation_state_t pa_operation_get_state(pa_operation *o); pa_operation_state_t pa_operation_get_state(pa_operation *o);
/** Set the callback function that is called when the operation
* is canceled due to disconnection. \since 4.0 */
void pa_operation_set_state_callback(pa_operation *o, pa_operation_notify_cb_t cb, void *userdata);
PA_C_DECL_END PA_C_DECL_END
#endif #endif