~ubuntu-branches/ubuntu/lucid/acpid/lucid-updates

« back to all changes in this revision

Viewing changes to ng/acpid/src/acpid.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Meskes
  • Date: 2009-11-17 14:50:01 UTC
  • mfrom: (2.1.12 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091117145001-y5hevyg7gcg6uwjk
Tags: 1.0.10-4
Updated netlink patch to version 6.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <assert.h>
 
3
 
 
4
#include <dbus/dbus.h>
 
5
#include <stdbool.h>
 
6
#include <unistd.h>
 
7
#include <stdio.h>
 
8
#include <stdlib.h>
 
9
#include <sys/types.h>
 
10
#include <sys/stat.h>
 
11
#include <fcntl.h>
 
12
#include <lua.h>
 
13
#include <lualib.h>
 
14
#include <lauxlib.h>
 
15
#include <syslog.h>
 
16
 
 
17
#include "acpi-watch.h"
 
18
#include "acpi-queue.h"
 
19
 
 
20
#include <acpid/driver.h>
 
21
 
 
22
static DBusConnection *conn;
 
23
 
 
24
static void dispatch(lua_State *L)
 
25
{
 
26
        struct acpi_channel_descriptor cds[12];
 
27
        unsigned long num = 0;
 
28
 
 
29
        lua_pushnil(L);
 
30
        while (lua_next(L, LUA_REGISTRYINDEX) != 0) {
 
31
                struct acpi_channel *channel = lua_touserdata(L, -2);
 
32
                num += (*channel->ops->setup)(channel, cds + num, 12 - num);
 
33
 
 
34
                lua_pop(L, 1);
 
35
                if (num == 12)
 
36
                        break;
 
37
        }
 
38
 
 
39
        lua_pop(L, 1);
 
40
 
 
41
        struct pollfd fds[num];
 
42
        for (unsigned long i = 0; i < num; ++i) {
 
43
                fds[i].fd = cds[i].fd;
 
44
                fds[i].events = cds[i].events;
 
45
        }
 
46
 
 
47
        int ret = poll(fds, num, -1);
 
48
 
 
49
        for (unsigned long i = 0; i < num; ++i) {
 
50
                if (fds[i].revents) {
 
51
                        int ret = (*cds[i].channel->ops->handle)(cds[i].channel, L, fds[i].fd, fds[i].revents); 
 
52
                }
 
53
        }
 
54
}
 
55
 
 
56
int acpi_dbus_event(const char *hid, unsigned long event, unsigned long data)
 
57
{
 
58
        DBusMessage *msg = dbus_message_new_signal("/org/kernel/acpi", "org.kernel.acpi.Event", "Broadcast");
 
59
        if (NULL == msg)
 
60
                exit(1);
 
61
 
 
62
        if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &hid, DBUS_TYPE_UINT32, &event, DBUS_TYPE_UINT32, &data, DBUS_TYPE_INVALID))
 
63
                exit(1);
 
64
 
 
65
        dbus_uint32_t serial = 0;
 
66
        if (!dbus_connection_send(conn, msg, &serial))
 
67
                exit(1);
 
68
 
 
69
        dbus_connection_flush(conn);
 
70
        dbus_message_unref(msg);
 
71
 
 
72
        return 0;
 
73
}
 
74
 
 
75
int main(int argc, char *argv[])
 
76
{
 
77
        DBusError err;
 
78
        dbus_error_init(&err);
 
79
 
 
80
        openlog("acpid-ng", 0, LOG_DAEMON);
 
81
        syslog(LOG_INFO, "starting\n");
 
82
 
 
83
        lua_State *L = luaL_newstate();
 
84
        luaL_dofile(L, "config.lua");
 
85
 
 
86
        lua_getfield(L, LUA_GLOBALSINDEX, "channels");
 
87
        lua_pushnil(L);
 
88
 
 
89
        while (lua_next(L, -2) != 0) {
 
90
                if (acpi_channel_create(L) == 0) {
 
91
                        lua_insert(L, -2);
 
92
                        lua_settable(L, LUA_REGISTRYINDEX);
 
93
                } else {
 
94
                        fprintf(stderr, "failed to create channel\n");
 
95
                        lua_pop(L, 1);
 
96
                }
 
97
        }
 
98
 
 
99
        lua_pop(L, 1);
 
100
 
 
101
        /* Remove the channel table */
 
102
        lua_pushnil(L);
 
103
        lua_setfield(L, LUA_GLOBALSINDEX, "channels");
 
104
 
 
105
        lua_createtable(L, 99, 0);
 
106
        lua_setfield(L, LUA_GLOBALSINDEX, "script");
 
107
        luaL_dofile(L, "scripts.lua");
 
108
 
 
109
        syslog(LOG_INFO, "loaded channels and scripts\n");
 
110
 
 
111
        /* At this point the lua stack should be empty! */
 
112
        assert(lua_gettop(L) == 0);
 
113
 
 
114
        conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
 
115
        if (conn == NULL) {
 
116
                fprintf(stderr, "Connection Error (%s)\n", err.message);
 
117
                dbus_error_free(&err);
 
118
                exit(1);
 
119
        }
 
120
 
 
121
        int ret = dbus_bus_request_name(conn, "org.kernel.acpi", DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
 
122
        if (dbus_error_is_set(&err)) {
 
123
                fprintf(stderr, "Name Error (%s)\n", err.message);
 
124
                dbus_error_free(&err);
 
125
                exit(1);
 
126
        }
 
127
 
 
128
        for (;;) {
 
129
                dispatch(L);
 
130
 
 
131
/*
 
132
                struct acpi_queue queue = { 0 };
 
133
 
 
134
                for (int i = 0; i < queue.num; ++i) {
 
135
                        printf("got an event from %s: %08x %08x %08x\n", queue.events[i].source, queue.events[i].type, queue.events[i].code, queue.events[i].value);
 
136
 
 
137
                        DBusMessage *msg = dbus_message_new_signal("/org/kernel/acpi", "org.kernel.acpi.Event", "Broadcast");
 
138
                        if (NULL == msg)
 
139
                                exit(1);
 
140
 
 
141
                        const char *source = queue.events[i].source;
 
142
                        if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &source, DBUS_TYPE_UINT32, &queue.events[i].type,
 
143
                                DBUS_TYPE_UINT32, &queue.events[i].code, DBUS_TYPE_UINT32, &queue.events[i].value, DBUS_TYPE_INVALID))
 
144
                                exit(1);
 
145
 
 
146
                        dbus_uint32_t serial = 0;
 
147
                        if (!dbus_connection_send(conn, msg, &serial))
 
148
                                exit(1);
 
149
 
 
150
                        dbus_connection_flush(conn);
 
151
                        dbus_message_unref(msg);
 
152
                }
 
153
*/
 
154
        }
 
155
 
 
156
        return 0;
 
157
}