~ubuntu-branches/ubuntu/karmic/devicekit-power/karmic-proposed

« back to all changes in this revision

Viewing changes to src/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-01-08 11:43:09 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090108114309-sz25jx234bgww096
Tags: 003-0ubuntu1
* New upstream version 003.
* debian/control: Bump libdevkit-gobject-dev build dependency.
* debian/contro: Replace Debian's Vcs-Svn: with our Vcs-Bzr: branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2
 
 *
3
 
 * Copyright (C) 2007 David Zeuthen <david@fubar.dk>
4
 
 *
5
 
 * Permission is hereby granted, free of charge, to any person
6
 
 * obtaining a copy of this software and associated documentation
7
 
 * files (the "Software"), to deal in the Software without
8
 
 * restriction, including without limitation the rights to use, copy,
9
 
 * modify, merge, publish, distribute, sublicense, and/or sell copies
10
 
 * of the Software, and to permit persons to whom the Software is
11
 
 * furnished to do so, subject to the following conditions:
12
 
 *
13
 
 * The above copyright notice and this permission notice shall be
14
 
 * included in all copies or substantial portions of the Software.
15
 
 *
16
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
 
 * DEALINGS IN THE SOFTWARE.
24
 
 *
25
 
 */
26
 
 
27
 
#ifdef HAVE_CONFIG_H
28
 
#  include "config.h"
29
 
#endif
30
 
 
31
 
#include <string.h>
32
 
#include <signal.h>
33
 
#include <sys/types.h>
34
 
#include <unistd.h>
35
 
 
36
 
#include <glib.h>
37
 
#include <glib/gi18n-lib.h>
38
 
#include <glib-object.h>
39
 
 
40
 
#include <dbus/dbus-glib.h>
41
 
#include <dbus/dbus-glib-lowlevel.h>
42
 
#include <devkit-gobject.h>
43
 
 
44
 
#include "egg-debug.h"
45
 
#include "dkp-daemon.h"
46
 
 
47
 
#define NAME_TO_CLAIM "org.freedesktop.DeviceKit.Power"
48
 
static GMainLoop *loop = NULL;
49
 
 
50
 
/**
51
 
 * main_acquire_name_on_proxy:
52
 
 **/
53
 
static gboolean
54
 
main_acquire_name_on_proxy (DBusGProxy *bus_proxy)
55
 
{
56
 
        GError *error;
57
 
        guint     result;
58
 
        gboolean        res;
59
 
        gboolean        ret;
60
 
 
61
 
        ret = FALSE;
62
 
 
63
 
        if (bus_proxy == NULL) {
64
 
                goto out;
65
 
        }
66
 
 
67
 
        error = NULL;
68
 
        res = dbus_g_proxy_call (bus_proxy,
69
 
                                 "RequestName",
70
 
                                 &error,
71
 
                                 G_TYPE_STRING, NAME_TO_CLAIM,
72
 
                                 G_TYPE_UINT, 0,
73
 
                                 G_TYPE_INVALID,
74
 
                                 G_TYPE_UINT, &result,
75
 
                                 G_TYPE_INVALID);
76
 
        if (!res) {
77
 
                if (error != NULL) {
78
 
                        egg_warning ("Failed to acquire %s: %s", NAME_TO_CLAIM, error->message);
79
 
                        g_error_free (error);
80
 
                } else {
81
 
                        egg_warning ("Failed to acquire %s", NAME_TO_CLAIM);
82
 
                }
83
 
                goto out;
84
 
        }
85
 
 
86
 
        if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
87
 
                if (error != NULL) {
88
 
                        egg_warning ("Failed to acquire %s: %s", NAME_TO_CLAIM, error->message);
89
 
                        g_error_free (error);
90
 
                } else {
91
 
                        egg_warning ("Failed to acquire %s", NAME_TO_CLAIM);
92
 
                }
93
 
                goto out;
94
 
        }
95
 
 
96
 
        ret = TRUE;
97
 
 
98
 
 out:
99
 
        return ret;
100
 
}
101
 
 
102
 
/**
103
 
 * dkp_main_sigint_handler:
104
 
 **/
105
 
static void
106
 
dkp_main_sigint_handler (int sig)
107
 
{
108
 
        egg_debug ("Handling SIGINT");
109
 
 
110
 
        /* restore default */
111
 
        signal (SIGINT, SIG_DFL);
112
 
 
113
 
        /* cleanup */
114
 
        g_main_loop_quit (loop);
115
 
}
116
 
 
117
 
/**
118
 
 * main:
119
 
 **/
120
 
int
121
 
main (int argc, char **argv)
122
 
{
123
 
        GError *error;
124
 
        DkpDaemon *power_daemon;
125
 
        GOptionContext *context;
126
 
        DBusGProxy *bus_proxy;
127
 
        DBusGConnection *bus;
128
 
        gboolean verbose = FALSE;
129
 
        int ret = 1;
130
 
 
131
 
        const GOptionEntry entries[] = {
132
 
                { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
133
 
                  _("Show extra debugging information"), NULL },
134
 
                { NULL }
135
 
        };
136
 
 
137
 
        g_type_init ();
138
 
 
139
 
        context = g_option_context_new ("DeviceKit Power Daemon");
140
 
        g_option_context_add_main_entries (context, entries, NULL);
141
 
        g_option_context_parse (context, &argc, &argv, NULL);
142
 
        g_option_context_free (context);
143
 
        egg_debug_init (verbose);
144
 
 
145
 
        error = NULL;
146
 
        bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
147
 
        if (bus == NULL) {
148
 
                egg_warning ("Couldn't connect to system bus: %s", error->message);
149
 
                g_error_free (error);
150
 
                goto out;
151
 
        }
152
 
 
153
 
        bus_proxy = dbus_g_proxy_new_for_name (bus, DBUS_SERVICE_DBUS,
154
 
                                               DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
155
 
        if (bus_proxy == NULL) {
156
 
                egg_warning ("Could not construct bus_proxy object; bailing out");
157
 
                goto out;
158
 
        }
159
 
 
160
 
        if (!main_acquire_name_on_proxy (bus_proxy) ) {
161
 
                egg_warning ("Could not acquire name; bailing out");
162
 
                goto out;
163
 
        }
164
 
 
165
 
        /* do stuff on ctrl-c */
166
 
        signal (SIGINT, dkp_main_sigint_handler);
167
 
 
168
 
        egg_debug ("Starting devkit-power-daemon version %s", VERSION);
169
 
 
170
 
        power_daemon = dkp_daemon_new ();
171
 
 
172
 
        if (power_daemon == NULL)
173
 
                goto out;
174
 
 
175
 
        loop = g_main_loop_new (NULL, FALSE);
176
 
        g_main_loop_run (loop);
177
 
 
178
 
        g_object_unref (power_daemon);
179
 
        g_main_loop_unref (loop);
180
 
        ret = 0;
181
 
 
182
 
out:
183
 
        return ret;
184
 
}