Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added update functionality. Still missing: Update songs

  • Loading branch information...
commit 3f5ba6148e5394184b86b81c6a11bd1c73b7d4a2 1 parent 0f6c6f4
@sahib sahib authored
View
28 lib/main.c
@@ -7,9 +7,10 @@
Proto_Connector * conn = NULL;
-static void event (enum mpd_idle event, bool connection_change)
+static void event (enum mpd_idle event, void * user_data)
{
- g_print ("%d %d\n", event, connection_change);
+ (void) user_data;
+ g_print ("event = %d\n", event);
}
/////////////////////////////
@@ -18,7 +19,13 @@ gboolean next_song (gpointer user_data)
{
GMainLoop * loop = (GMainLoop *) user_data;
g_print ("NEXT!\n");
- next (conn);
+
+ proto_update(conn, INT_MAX);
+
+ for(int i = 0; i < 1; i++)
+ {
+ next (conn);
+ }
while (g_main_context_iteration (NULL, TRUE) );
g_usleep (1 * 1000 * 100);
@@ -26,8 +33,12 @@ gboolean next_song (gpointer user_data)
/* Disconnect all */
g_print("Disconnecting\n");
proto_disconnect (conn);
- conn = NULL;
g_print("Disconnecting done\n");
+ proto_connect (conn, NULL, "localhost", 6600, 2);
+
+ next (conn);
+ while (g_main_context_iteration (NULL, TRUE) );
+ proto_disconnect (conn);
g_main_loop_quit (loop);
@@ -38,6 +49,8 @@ gboolean next_song (gpointer user_data)
int main (void)
{
+ //g_mem_set_vtable (glib_mem_profiler_table);
+
conn = proto_create_cmnder();
const char * err = proto_connect (conn, NULL, "localhost", 6600, 2);
if (err != NULL)
@@ -49,12 +62,15 @@ int main (void)
GMainLoop * loop = g_main_loop_new (NULL, FALSE);
proto_add_event_callback (conn, event, NULL);
- //next (conn);
g_timeout_add (500, next_song, loop);
g_main_loop_run (loop);
g_main_loop_unref (loop);
-
}
+
+ g_print("\n--------------------------------\n");
+ //g_mem_profile ();
+
+
return EXIT_SUCCESS;
}
View
17 lib/mpd/pm/cmnd_core.c
@@ -65,8 +65,11 @@ static gboolean cmnder_event_callback (GAsyncQueue * queue, gpointer user_data)
/* Now iterate over the happened events, and callback maybe */
for (GList * iter = event_list; iter; iter = iter->next)
+ {
g_print ("<=== %d\n", GPOINTER_TO_INT (iter->data) );
+ }
+
g_list_free (event_list);
return TRUE;
}
@@ -127,8 +130,8 @@ static void cmnder_shutdown_listener(Proto_CmndConnector * self)
if (self->watch_source_id != -1)
{
- //g_thread_join (self->listener_thread);
g_source_remove(self->watch_source_id);
+ g_thread_join (self->listener_thread);
}
self->watch_source_id = -1;
@@ -204,12 +207,13 @@ static mpd_connection * cmnder_do_get (Proto_Connector * self)
return child (self)->cmnd_con;
}
-///////////////////////
+//////////////////////
-static void cmnder_do_put (Proto_Connector * self)
+static void cmnder_do_free (Proto_Connector * parent)
{
- (void) self;
- /* NOOP */
+ Proto_CmndConnector * self = child(parent);
+ memset(self, 0, sizeof(Proto_CmndConnector));
+ g_free(self);
}
//////////////////////
@@ -224,7 +228,8 @@ Proto_Connector * proto_create_cmnder (void)
Proto_CmndConnector * self = g_new0 (Proto_CmndConnector, 1);
self->logic.do_disconnect = cmnder_do_disconnect;
self->logic.do_get = cmnder_do_get;
- self->logic.do_put = cmnder_do_put;
+ self->logic.do_put = NULL;
+ self->logic.do_free = cmnder_do_free;
self->logic.do_connect = cmnder_do_connect;
self->logic.do_is_connected = cmnder_do_is_connected;
View
5 lib/mpd/pm/common.h
@@ -2,10 +2,11 @@
#define COMMON_H_GUARD
#include <glib.h>
-
#include <mpd/client.h>
#include <mpd/async.h>
+#include "../protocol.h"
+
typedef enum mpd_async_event mpd_async_event;
typedef struct mpd_connection mpd_connection;
@@ -38,4 +39,6 @@ GIOCondition mpd_async_to_gio (mpd_async_event events);
*/
mpd_connection * mpd_connect (const char * host, int port, int timeout);
+
+
#endif /* end of include guard: COMMON_H_GUARD */
View
29 lib/mpd/protocol.c
@@ -77,7 +77,10 @@ void proto_put (Proto_Connector * self)
/*
* Put connection back to event listening.
*/
- self->do_put (self);
+ if (self->do_put != NULL)
+ {
+ self->do_put (self);
+ }
}
///////////////////
@@ -140,3 +143,27 @@ const char * proto_disconnect (Proto_Connector * self)
return NULL;
}
+
+///////////////////
+
+void proto_free (Proto_Connector * self)
+{
+ if(self->do_free != NULL)
+ {
+ self->do_free(self);
+ }
+}
+
+///////////////////
+
+void proto_update (Proto_Connector * self, enum mpd_idle events)
+{
+ for(GList * iter = self->_event_callbacks; iter; iter = iter->next)
+ {
+ Proto_CallbackTag * tag = iter->data;
+ if(tag != NULL && (tag->mask & events) > 0)
+ {
+ tag->callback(events, tag->user_data);
+ }
+ }
+}
View
40 lib/mpd/protocol.h
@@ -12,22 +12,24 @@ typedef enum mpd_error mpd_error;
typedef enum mpd_idle mpd_idle;
/* Event callback */
-typedef void (* Proto_EventCallback) (enum mpd_idle, bool);
+typedef void (* Proto_EventCallback) (enum mpd_idle, void * userdata);
/* Error callback */
-typedef void (* Proto_ErrorCallback) (mpd_error);
+typedef void (* Proto_ErrorCallback) (mpd_error, void * userdata);
// TODO:
-// reconnect functionality
// where to put up-to-date currentsong/status/whatever?
+// -> as callback that was added prior all other callbacks,
+// -> via proto_add_event_callback
// move glib away from headers as far as possible
+// -> not possible entirely.
/**
* @brief Structure representing a connection handle,
* and an interface to send commands and recv. events.
*
* It's able to:
- * - Server-hosts (without loosing registered callbacks)
+ * - change hosts (connecting/disconnecting) (without loosing registered callbacks)
* - Notifying you on events / errors / connection-changes
* - Send commands to the server.
*
@@ -48,7 +50,7 @@ typedef struct _Proto_Connector
/*
* Return the command sending connection, made ready to rock.
- * May be NULL.
+ * May not be NULL.
*/
mpd_connection * (* do_get) (struct _Proto_Connector *);
@@ -72,6 +74,12 @@ typedef struct _Proto_Connector
bool (* do_is_connected) (struct _Proto_Connector *);
/*
+ * Free the connector. disconnect() won't free it!
+ * May be NULL
+ */
+ void (* do_free) (struct _Proto_Connector *);
+
+ /*
* Callback lists
*/
GList * _event_callbacks;
@@ -174,4 +182,26 @@ void proto_put (Proto_Connector * self);
*/
const char * proto_disconnect (Proto_Connector * self);
+/**
+ * @brief Send a event to all registered callbacks.
+ *
+ * This is usually called internally, but it might
+ * be useful for you to update the view of you application
+ * on initial start when no events happened yet.
+ *
+ * @param self connector to operate on
+ * @param events a enump mpd_idle
+ */
+void proto_update (Proto_Connector * self, enum mpd_idle events);
+
+
+/**
+ * @brief Free all data associated with this connector.
+ *
+ * You have to call proto_disconnect beforehand!
+ *
+ * @param self the connector to operate on
+ */
+void proto_free (Proto_Connector * self);
+
#endif /* end of include guard: PROTOCOL_H */
Please sign in to comment.
Something went wrong with that request. Please try again.