/* diversity-control-backend.c - * * Copyright 2007-2008 OpenMoko, Inc. * Authored by Chia-I Wu * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include "control-marshal.h" #include "diversity-control-backend.h" #include "diversity-control.h" #define CONTROL_DBUS_SERVICE "org.openmoko.Diversity" #define CONTROL_DBUS_PATH "/org/openmoko/Diversity" static struct { DiversityControl *ctrl; DBusGConnection *connection; DBusGProxy *dbus_proxy; } control = { 0 }; int control_init(DiversityWorld *world, GError **error) { guint32 req_ret; if (!g_thread_supported()) { g_thread_init(NULL); dbus_g_thread_init(); } control.connection = dbus_g_bus_get(DIVERSITY_DBUS_BUS, error); if (!control.connection) { control_fini(); return FALSE; } control.dbus_proxy = dbus_g_proxy_new_for_name(control.connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); if (!control.dbus_proxy) { g_set_error(error, 0, 0, "failed to create proxy\n"); control_fini(); return FALSE; } if (!org_freedesktop_DBus_request_name(control.dbus_proxy, CONTROL_DBUS_SERVICE, 0, &req_ret, error)) { control_fini(); return FALSE; } if (req_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { g_set_error(error, 0, 0, "failed to become primary owner\n"); control_fini(); return FALSE; } control.ctrl = diversity_control_new(world, control.connection, CONTROL_DBUS_PATH); if (!control.ctrl) { g_set_error(error, 0, 0, "failed to create control\n"); control_fini(); return FALSE; } return TRUE; } void control_fini(void) { if (control.ctrl) { g_object_unref(G_OBJECT(control.ctrl)); control.ctrl = NULL; } if (control.dbus_proxy) { g_object_unref(G_OBJECT(control.dbus_proxy)); control.dbus_proxy = NULL; } if (control.connection) { dbus_g_connection_unref(control.connection); control.connection = NULL; } }