Back to index

easystroke  0.5.5.1
dbus-server.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008-2009, Thomas Jaeger <ThJaeger@gmail.com>
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00011  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
00013  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
00014  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 #include <dbus/dbus-protocol.h>
00017 #include <dbus/dbus-glib.h>
00018 #include <dbus/dbus-glib-bindings.h>
00019 
00020 #include <stdio.h>
00021 
00022 #define SERVER_OBJECT_TYPE (server_object_get_type())
00023 
00024 typedef struct _ServerObject {
00025        GObject object;
00026 } ServerObject;
00027 
00028 typedef struct _ServerObjectClass {
00029        GObjectClass object_class;
00030 } ServerObjectClass;
00031 
00032 void run_by_name(const char *str);
00033 
00034 static gboolean server_send(ServerObject *obj, const char *str, GError **error) {
00035        run_by_name(str);
00036        return TRUE;
00037 }
00038 
00039 #include "dbus-server.h"
00040 
00041 static void server_object_class_init(ServerObjectClass *klass) {
00042        dbus_g_object_type_install_info(G_TYPE_FROM_CLASS(klass), &dbus_glib_server_object_info);
00043 }
00044 
00045 static void server_object_init(ServerObject *obj) {}
00046 
00047 G_DEFINE_TYPE(ServerObject, server_object, G_TYPE_OBJECT);
00048 
00049 int start_dbus() {
00050        DBusGConnection *connection;
00051        DBusGProxy *proxy;
00052        GError *error = NULL;
00053        ServerObject *obj;
00054        guint32 request_name_ret;
00055 
00056        connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
00057        if (!connection)
00058               return false;
00059 
00060        proxy = dbus_g_proxy_new_for_name(connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
00061        if (!org_freedesktop_DBus_request_name(proxy, "org.easystroke", 0, &request_name_ret, &error))
00062               return false;
00063 
00064        if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
00065               return -1;
00066 
00067        g_object_unref(proxy);
00068 
00069        obj =(ServerObject *)g_object_new(SERVER_OBJECT_TYPE, NULL);
00070        dbus_g_connection_register_g_object(connection, "/org/easystroke", G_OBJECT(obj));
00071        return true;
00072 }