diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-08-21 01:09:12 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-08-21 01:09:12 +0200 |
commit | 0ef865bb301954df86b87cd1978ca711c01e71d5 (patch) | |
tree | 56b63932d56927b3937e95e70fe4f1d57363bcad | |
parent | f156f9a4f120a055b023ce8e60fac3012e0c185f (diff) |
Added the basics for local control commands.
-rw-r--r-- | SConstruct | 1 | ||||
-rw-r--r-- | control_commands.c | 14 | ||||
-rw-r--r-- | control_commands.h | 8 | ||||
-rw-r--r-- | control_service.c | 59 | ||||
-rw-r--r-- | control_service.h | 9 | ||||
-rw-r--r-- | main.c | 5 |
6 files changed, 96 insertions, 0 deletions
@@ -15,6 +15,7 @@ else: env.ParseConfig('pkg-config --cflags --libs glib-2.0') env.ParseConfig('pkg-config --cflags --libs gio-2.0') +env.ParseConfig('pkg-config --cflags --libs gio-unix-2.0') env.ParseConfig('pkg-config --cflags --libs libmpg123') env.ParseConfig('pkg-config --cflags --libs id3tag') diff --git a/control_commands.c b/control_commands.c new file mode 100644 index 0000000..1ee261c --- /dev/null +++ b/control_commands.c @@ -0,0 +1,14 @@ +#include "control_commands.h" + +#include <glib.h> +#include <string.h> + +void control_commands_handle(const gchar *cmd) { + gchar *args = strchr(cmd, ' '); + if(args != NULL) { + *args = '\0'; + args++; + } + + g_debug("handling command %s with arguments %s", cmd, args); +} diff --git a/control_commands.h b/control_commands.h new file mode 100644 index 0000000..03d557b --- /dev/null +++ b/control_commands.h @@ -0,0 +1,8 @@ +#ifndef CONTROL_COMMANDS_H +#define CONTROL_COMMANDS_H + +#include <glib.h> + +void control_commands_handle(const gchar *cmd); + +#endif diff --git a/control_service.c b/control_service.c new file mode 100644 index 0000000..a6f035d --- /dev/null +++ b/control_service.c @@ -0,0 +1,59 @@ +#include "control_service.h" +#include "control_commands.h" + +#include <glib.h> +#include <glib/gstdio.h> +#include <gio/gio.h> +#include <gio/gunixsocketaddress.h> + +static GSocketService *ss = NULL; +static GSocketAddress *address; + +static gboolean service_incoming(GSocketService *service, + GSocketConnection *connection, GObject *source_object, + gpointer user_data) { + g_debug("local service got incoming connection"); + + GSocket *socket = g_socket_connection_get_socket(connection); + gchar buffer[0x400]; + gssize size = g_socket_receive(socket, buffer, 0x400, NULL, NULL); + + gchar *pos = g_strstr_len(buffer, size, "\r"); + if(pos == NULL) { + pos = g_strstr_len(buffer, size, "\n"); + } + if(pos == NULL) { + g_warning("EOL not found"); + return FALSE; + } + *pos = '\0'; + + control_commands_handle(buffer); + + return FALSE; +} + +gboolean control_service_start() { + address = g_unix_socket_address_new("audist.sock"); + + ss = g_threaded_socket_service_new(2); + + if(g_socket_listener_add_address((GSocketListener*)ss, address, + G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL, + NULL, NULL) == FALSE) { + g_error("g_socket_listener_add_socket() failed"); + } + + g_signal_connect(ss, "incoming", (GCallback)service_incoming, NULL); + + g_socket_service_start((GSocketService*)ss); + + return TRUE; +} + +void control_service_stop() { + g_socket_service_stop((GSocketService*)ss); + g_unlink(g_unix_socket_address_get_path((GUnixSocketAddress*)address)); + g_object_unref(ss); + g_object_unref(address); +} diff --git a/control_service.h b/control_service.h new file mode 100644 index 0000000..70cadc5 --- /dev/null +++ b/control_service.h @@ -0,0 +1,9 @@ +#ifndef CONTROL_SERVICE_H +#define CONTROL_SERVICE_H + +#include <glib.h> + +gboolean control_service_start(); +void control_service_stop(); + +#endif @@ -3,6 +3,7 @@ #include "command_service.h" #include "conf.h" #include "servers.h" +#include "control_service.h" #include <glib.h> #include <glib-object.h> @@ -21,6 +22,8 @@ int main(int argc, char **argv) { conf_load(); + control_service_start(); + servers_init(); music_init(); @@ -41,6 +44,8 @@ int main(int argc, char **argv) { servers_free(); + control_service_stop(); + conf_free(); return 0; |