~chunsang/+junk/powerd

« back to all changes in this revision

Viewing changes to src/powerd.cpp

  • Committer: Matthew Fischer
  • Date: 2013-05-14 19:57:42 UTC
  • mto: (9.5.1 mfisch)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: matthew.fischer@canonical.com-20130514195742-0vcbjaom3hncq51u
initial code that talks on dbus

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include <errno.h>
29
29
#include <string.h>
30
30
 
 
31
#include <glib-object.h>
 
32
#include <gio/gio.h>
 
33
#include "powerd-dbus.h"
 
34
#include "powerd.h"
 
35
 
31
36
#include <hybris/input/input_stack_compatibility_layer.h>
32
37
#include <hybris/input/input_stack_compatibility_layer_codes_key.h>
33
38
 
36
41
namespace
37
42
{
38
43
 
 
44
enum SysPowerStates {
 
45
    SUSPEND = 0,
 
46
    ACTIVE
 
47
};
 
48
 
 
49
struct SysStateRequest {
 
50
    GString *name;
 
51
    GString *owner;
 
52
    SysPowerStates state;
 
53
};
 
54
 
39
55
enum {
40
56
    ACQUIRE_PARTIAL_WAKE_LOCK = 0,
41
57
    RELEASE_WAKE_LOCK,
49
65
    "/sys/power/state"
50
66
};
51
67
 
 
68
 
 
69
G_DEFINE_TYPE (PowerdSource, powerd_source, G_TYPE_OBJECT);
 
70
 
 
71
#if 0
 
72
struct _PowerdSourcePrivate {
 
73
    GDBusConnection * session;
 
74
    gchar * path;
 
75
    ComCanonicalPowerd * skel;
 
76
};
 
77
#endif
 
78
 
 
79
#define POWERD_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), POWERD_TYPE_SOURCE, PowerdSourcePrivate))
 
80
 
 
81
GSList *requests = NULL;
 
82
 
52
83
static const char *off_state = "mem";
53
84
static const char *on_state = "on";
54
85
 
75
106
    SHUTDOWN
76
107
};
77
108
 
 
109
static void powerd_source_class_init (PowerdSourceClass * klass);
 
110
static void powerd_source_init       (PowerdSource *      self);
 
111
static void powerd_source_dispose    (GObject *           object);
 
112
static void powerd_source_finalize   (GObject *           object);
 
113
static const gchar * state_to_string(int state);
 
114
 
78
115
static state button_state  = BUTTON_UP;
79
116
 
 
117
/* Instance */
 
118
static void
 
119
powerd_source_init (PowerdSource *self)
 
120
{
 
121
    self->priv = POWERD_SOURCE_GET_PRIVATE(self);
 
122
    self->priv->session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
 
123
    return;
 
124
}
 
125
 
 
126
/* Class Init */
 
127
static void
 
128
powerd_source_class_init (PowerdSourceClass *self)
 
129
{
 
130
    GObjectClass *object_class = G_OBJECT_CLASS (self);
 
131
    g_type_class_add_private (self, sizeof (PowerdSourcePrivate));
 
132
    object_class->dispose = powerd_source_dispose;
 
133
    object_class->finalize = powerd_source_finalize;
 
134
    return;
 
135
}
 
136
 
 
137
/* Clean up references */
 
138
static void
 
139
powerd_source_dispose (GObject *object)
 
140
{
 
141
    PowerdSource * self = POWERD_SOURCE(object);
 
142
    if (self->priv->skel != NULL) {
 
143
        g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(self->priv->skel));
 
144
        g_clear_object(&self->priv->skel);
 
145
    }
 
146
    g_clear_object(&self->priv->session);
 
147
    return;
 
148
}
 
149
 
 
150
/* Free memory */
 
151
static void
 
152
powerd_source_finalize (GObject *object)
 
153
{
 
154
    return;
 
155
}
 
156
 
80
157
 
81
158
void get_brightness_path (void)
82
159
{
278
355
                    lasttime = tm->tm_sec;
279
356
                    if (button_timer > 0)
280
357
                    {
281
 
                        g_source_remove(button_timer);               
 
358
                        g_source_remove(button_timer);
282
359
                        button_timer = 0;
283
360
                    }
284
361
                    button_timer = g_timeout_add_seconds(2, call_shutdown, NULL);
290
367
    }
291
368
}
292
369
 
293
 
}
294
 
 
 
370
/* simple helper for debugging */
 
371
static const gchar *
 
372
state_to_string(int state)
 
373
{
 
374
    switch (state) {
 
375
        case ACTIVE:
 
376
            return "STATE_ACTIVE";
 
377
            break;
 
378
        // you really shouldnt get this state since it's not requestable
 
379
        // externally
 
380
        case SUSPEND:
 
381
            return "STATE_SUSPEND";
 
382
            break;
 
383
        default:
 
384
            return "UNKNOWN";
 
385
            break;
 
386
    }
 
387
}
 
388
 
 
389
static gboolean
 
390
handle_list_requests (PowerdSource *obj, GDBusMethodInvocation *invocation)
 
391
{
 
392
    GSList *iterator = NULL;
 
393
    if (!requests) {
 
394
        g_message("no requests active");
 
395
    }
 
396
    else {
 
397
        for (iterator = requests; iterator; iterator = iterator->next) {
 
398
            SysStateRequest *sr = (SysStateRequest *)iterator->data;
 
399
            g_message("Owner: %s, Name: %s, State: %d (%s)",\
 
400
                sr->owner->str, sr->name->str, sr->state,\
 
401
                state_to_string(sr->state));
 
402
        }
 
403
    }
 
404
    g_dbus_method_invocation_return_value(invocation, NULL);
 
405
    return TRUE;
 
406
}
 
407
 
 
408
}
295
409
 
296
410
int main(int argc, char** argv)
297
411
{
298
 
 
 
412
    GError *error = NULL;
299
413
    GMainLoop* main_loop = NULL;
300
414
 
301
415
    main_loop = g_main_loop_new (NULL, FALSE);
 
416
    g_type_init();
 
417
    PowerdSource *source = (PowerdSource *)g_object_new(POWERD_TYPE_SOURCE, NULL);
 
418
    source->priv->skel = com_canonical_powerd_skeleton_new();
 
419
    source->priv->path = "/com/canonical/powerd";
302
420
 
303
421
    AndroidEventListener listener;
304
422
    listener.on_new_event = on_new_event;
312
430
    android_input_stack_initialize(&listener, &config);
313
431
    android_input_stack_start();
314
432
 
315
 
    g_type_init();
316
433
    get_brightness_path();
 
434
 
 
435
    if (!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(source->priv->skel),
 
436
            source->priv->session, source->priv->path, &error)) {
 
437
        g_error("Unable to export skeleton on path '%s': %s", source->priv->path, error->message);
 
438
        g_error_free(error);
 
439
        error = NULL;
 
440
        return -1;
 
441
    }
 
442
 
 
443
//    g_signal_connect(G_OBJECT(source->priv->skel), "handle-request-state",\
 
444
        G_CALLBACK(handle_request_state), source);
 
445
    g_signal_connect(G_OBJECT(source->priv->skel), "handle-list-requests",\
 
446
        G_CALLBACK(handle_list_requests), source);
 
447
 
317
448
    g_main_loop_run(main_loop);
318
449
 
319
450
    android_input_stack_stop();
320
451
    android_input_stack_shutdown();
 
452
 
 
453
    // XXX - mfisch need to cleanup our list of active requests
 
454
    return 0;
321
455
}
322