From 38a25bb40b18fc820157d9af832daf08bbc7cd9e Mon Sep 17 00:00:00 2001 From: Matthias Vogelgesang Date: Mon, 5 Mar 2012 10:49:37 +0100 Subject: Use property notification instead of signal Up to this point we used a custom signal to notify about starting and stopping recording of data. However, such signals come (almost) for free for each property. This way, you can query "is-recording" but also be notified when "is-recording" was changed by the camera itself. --- src/uca-camera.c | 45 ++++++++++++++++++++------------------------- test/CMakeLists.txt | 12 ++++++------ test/test-mock.c | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 68 insertions(+), 39 deletions(-) diff --git a/src/uca-camera.c b/src/uca-camera.c index 7079279..35e15b7 100644 --- a/src/uca-camera.c +++ b/src/uca-camera.c @@ -33,9 +33,6 @@ GQuark uca_camera_error_quark() } enum { - RECORDING_STARTED, - RECORDING_STOPPED, - PROPERTY_CHANGED, LAST_SIGNAL }; @@ -50,6 +47,7 @@ enum { PROP_SENSOR_VERTICAL_BINNINGS, PROP_HAS_STREAMING, PROP_HAS_CAMRAM_RECORDING, + PROP_IS_RECORDING, N_PROPERTIES }; @@ -59,7 +57,7 @@ struct _UcaCameraPrivate { static GParamSpec *camera_properties[N_PROPERTIES] = { NULL, }; -static guint camera_signals[LAST_SIGNAL] = { 0 }; +/* static guint camera_signals[LAST_SIGNAL] = { 0 }; */ static void uca_camera_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { @@ -68,7 +66,16 @@ static void uca_camera_set_property(GObject *object, guint property_id, const GV static void uca_camera_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { - G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); + UcaCameraPrivate *priv = UCA_CAMERA_GET_PRIVATE(object); + + switch (property_id) { + case PROP_IS_RECORDING: + g_value_set_boolean(value, priv->recording); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); + } } static void uca_camera_class_init(UcaCameraClass *klass) @@ -150,27 +157,15 @@ static void uca_camera_class_init(UcaCameraClass *klass) "Is the camera able to record the data in-camera", FALSE, G_PARAM_READABLE); + camera_properties[PROP_IS_RECORDING] = + g_param_spec_boolean("is-recording", + "Is camera recording", + "Is the camera currently recording", + FALSE, G_PARAM_READABLE); + for (guint id = PROP_0 + 1; id < N_PROPERTIES; id++) g_object_class_install_property(gobject_class, id, camera_properties[id]); - camera_signals[RECORDING_STARTED] = - g_signal_new("recording-started", - G_OBJECT_CLASS_TYPE(gobject_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET(UcaCameraClass, recording_started), - NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0, NULL); - - camera_signals[RECORDING_STOPPED] = - g_signal_new("recording-stopped", - G_OBJECT_CLASS_TYPE(gobject_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET(UcaCameraClass, recording_stopped), - NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0, NULL); - g_type_class_add_private(klass, sizeof(UcaCameraPrivate)); } @@ -197,7 +192,7 @@ void uca_camera_start_recording(UcaCamera *camera, GError **error) camera->priv->recording = TRUE; (*klass->start_recording)(camera, error); - g_signal_emit_by_name(G_OBJECT(camera), "recording-started"); + g_object_notify_by_pspec(G_OBJECT(camera), camera_properties[PROP_IS_RECORDING]); } void uca_camera_stop_recording(UcaCamera *camera, GError **error) @@ -217,7 +212,7 @@ void uca_camera_stop_recording(UcaCamera *camera, GError **error) camera->priv->recording = FALSE; (*klass->stop_recording)(camera, error); - g_signal_emit_by_name(G_OBJECT(camera), "recording-stopped"); + g_object_notify_by_pspec(G_OBJECT(camera), camera_properties[PROP_IS_RECORDING]); } void uca_camera_set_grab_func(UcaCamera *camera, UcaCameraGrabFunc func) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b853cb5..4ee097b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -51,9 +51,9 @@ target_link_libraries(test-gobject ${GOBJECT2_LIBRARIES} ) -#add_executable(test-mock test-mock.c) -#target_link_libraries(test-mock -# uca-gobject -# ${GLIB2_LIBRARIES} -# ${GOBJECT2_LIBRARIES} -# ) +add_executable(test-mock test-mock.c) +target_link_libraries(test-mock + uca-gobject + ${GLIB2_LIBRARIES} + ${GOBJECT2_LIBRARIES} + ) diff --git a/test/test-mock.c b/test/test-mock.c index 2b298a9..ceb4b20 100644 --- a/test/test-mock.c +++ b/test/test-mock.c @@ -20,6 +20,12 @@ static void fixture_teardown(Fixture *fixture, gconstpointer data) g_object_unref(fixture->camera); } +static void on_property_change(gpointer instance, GParamSpec *pspec, gpointer user_data) +{ + gboolean *success = (gboolean *) user_data; + *success = TRUE; +} + static void test_recording(Fixture *fixture, gconstpointer data) { GError *error = NULL; @@ -35,6 +41,39 @@ static void test_recording(Fixture *fixture, gconstpointer data) g_assert(error == NULL); } +static void test_recording_signal(Fixture *fixture, gconstpointer data) +{ + UcaCamera *camera = UCA_CAMERA(fixture->camera); + gboolean success = FALSE; + g_signal_connect(G_OBJECT(camera), "notify::is-recording", + (GCallback) on_property_change, &success); + + uca_camera_start_recording(camera, NULL); + g_assert(success == TRUE); + + success = FALSE; + uca_camera_stop_recording(camera, NULL); + g_assert(success == TRUE); +} + +static void test_recording_property(Fixture *fixture, gconstpointer data) +{ + UcaCamera *camera = UCA_CAMERA(fixture->camera); + + gboolean is_recording = FALSE; + uca_camera_start_recording(camera, NULL); + g_object_get(G_OBJECT(camera), + "is-recording", &is_recording, + NULL); + g_assert(is_recording == TRUE); + + uca_camera_stop_recording(camera, NULL); + g_object_get(G_OBJECT(camera), + "is-recording", &is_recording, + NULL); + g_assert(is_recording == FALSE); +} + static void test_base_properties(Fixture *fixture, gconstpointer data) { UcaCamera *camera = UCA_CAMERA(fixture->camera); @@ -65,18 +104,11 @@ static void test_binnings_properties(Fixture *fixture, gconstpointer data) g_assert(g_value_get_uint(value) == 1); } -static void signal_handler(gpointer instance, const gchar *name, gpointer user_data) -{ - g_print("name: %s\n", name); - gboolean *success = (gboolean *) user_data; - *success = TRUE; -} - static void test_signal(Fixture *fixture, gconstpointer data) { UcaCamera *camera = UCA_CAMERA(fixture->camera); gboolean success = FALSE; - g_signal_connect(camera, "property-changed", (GCallback) signal_handler, &success); + g_signal_connect(camera, "notify::framerate", (GCallback) on_property_change, &success); g_object_set(G_OBJECT(camera), "framerate", 30, NULL); @@ -90,7 +122,9 @@ int main(int argc, char *argv[]) g_test_bug_base("http://ufo.kit.edu/ufo/ticket"); g_test_add("/recording", Fixture, NULL, fixture_setup, test_recording, fixture_teardown); + g_test_add("/recording/signal", Fixture, NULL, fixture_setup, test_recording_signal, fixture_teardown); g_test_add("/properties/base", Fixture, NULL, fixture_setup, test_base_properties, fixture_teardown); + g_test_add("/properties/recording", Fixture, NULL, fixture_setup, test_recording_property, fixture_teardown); g_test_add("/properties/binnings", Fixture, NULL, fixture_setup, test_binnings_properties, fixture_teardown); g_test_add("/signal", Fixture, NULL, fixture_setup, test_signal, fixture_teardown); -- cgit v1.2.3