|
Lines 5-10
Link Here
|
| 5 |
#include <stdio.h> |
5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
7 |
#include <string.h> |
|
|
8 |
#include <fcntl.h> |
| 9 |
|
| 10 |
// Input devices |
| 11 |
#include <linux/input.h> |
| 12 |
|
| 13 |
#define BITS_PER_LONG (sizeof(long) * 8) |
| 14 |
#define NUM_BITS(x) ((((x) - 1) / BITS_PER_LONG) + 1) |
| 8 |
|
15 |
|
| 9 |
void reply_Bool(DBusMessage* msg, DBusConnection* conn, int value) { |
16 |
void reply_Bool(DBusMessage* msg, DBusConnection* conn, int value) { |
| 10 |
DBusMessage* reply; |
17 |
DBusMessage* reply; |
|
Lines 177-183
void reply_CanSetBrightness(DBusMessage* msg, DBusConnection* conn) {
Link Here
|
| 177 |
const char* member = dbus_message_get_member(msg); |
184 |
const char* member = dbus_message_get_member(msg); |
| 178 |
char* rawpath; |
185 |
char* rawpath; |
| 179 |
char* safepath; |
186 |
char* safepath; |
| 180 |
char path[256]; |
|
|
| 181 |
|
187 |
|
| 182 |
// read the arguments |
188 |
// read the arguments |
| 183 |
if (!dbus_message_iter_init(msg, &args)) { |
189 |
if (!dbus_message_iter_init(msg, &args)) { |
|
Lines 211-217
void reply_SetBrightness(DBusMessage* msg, DBusConnection* conn) {
Link Here
|
| 211 |
char* rawpath; |
217 |
char* rawpath; |
| 212 |
char* safepath; |
218 |
char* safepath; |
| 213 |
char* brightness; |
219 |
char* brightness; |
| 214 |
char path[256]; |
|
|
| 215 |
|
220 |
|
| 216 |
// read the arguments |
221 |
// read the arguments |
| 217 |
if (!dbus_message_iter_init(msg, &args)) { |
222 |
if (!dbus_message_iter_init(msg, &args)) { |
|
Lines 266-272
void reply_CanSetPower(DBusMessage* msg, DBusConnection* conn, char* state) {
Link Here
|
| 266 |
char *line = NULL; |
271 |
char *line = NULL; |
| 267 |
size_t len = 0; |
272 |
size_t len = 0; |
| 268 |
ssize_t read = getline(&line, &len, node); |
273 |
ssize_t read = getline(&line, &len, node); |
| 269 |
if (line) { |
274 |
if (read > 0 && line) { |
| 270 |
method = strstr(line, state) != NULL; |
275 |
method = strstr(line, state) != NULL; |
| 271 |
free(line); |
276 |
free(line); |
| 272 |
} |
277 |
} |
|
Lines 317-322
void reply_SetHibernationMethod(DBusMessage* msg, DBusConnection* conn) {
Link Here
|
| 317 |
} |
322 |
} |
| 318 |
} |
323 |
} |
| 319 |
|
324 |
|
|
|
325 |
void reply_InputEventsGetSwitches(DBusMessage* msg, DBusConnection* conn, bool active) { |
| 326 |
DBusMessage* reply; |
| 327 |
DBusMessageIter args, arrayIter; |
| 328 |
const char* member = dbus_message_get_member(msg); |
| 329 |
dbus_uint32_t serial = 0; |
| 330 |
char* rawpath; |
| 331 |
char* safepath; |
| 332 |
int fd, r; |
| 333 |
unsigned long switches[NUM_BITS(EV_CNT)]; |
| 334 |
|
| 335 |
// read the arguments |
| 336 |
if (!dbus_message_iter_init(msg, &args)) { |
| 337 |
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: no argument supplied\n", member); |
| 338 |
} |
| 339 |
else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) { |
| 340 |
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: argument not string\n", member); |
| 341 |
} |
| 342 |
else { |
| 343 |
dbus_message_iter_get_basic(&args, &rawpath); |
| 344 |
} |
| 345 |
|
| 346 |
safepath = realpath(rawpath, NULL); |
| 347 |
|
| 348 |
if (safepath && |
| 349 |
(strstr(safepath, "/dev/input/event") == safepath) |
| 350 |
) { |
| 351 |
|
| 352 |
fd = open(safepath, O_RDONLY); |
| 353 |
if( active ) { |
| 354 |
r = ioctl(fd, EVIOCGSW(sizeof(switches)), switches); |
| 355 |
} |
| 356 |
else { |
| 357 |
r = ioctl(fd, EVIOCGBIT(EV_SW, EV_CNT), switches); |
| 358 |
} |
| 359 |
if( r > 0 ) { |
| 360 |
dbus_uint32_t dSwitches[NUM_BITS(EV_CNT)]; |
| 361 |
dbus_uint32_t *dSwitchesP = dSwitches; |
| 362 |
int i; |
| 363 |
|
| 364 |
// create a reply from the message |
| 365 |
reply = dbus_message_new_method_return(msg); |
| 366 |
|
| 367 |
// add the arguments to the reply |
| 368 |
for( i = 0; i < sizeof(switches)/sizeof(switches[0]); i++ ) { |
| 369 |
dSwitches[i] = switches[i]; |
| 370 |
} |
| 371 |
dbus_message_iter_init_append(reply, &args); |
| 372 |
if (!dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY, "u", &arrayIter)) { |
| 373 |
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_message_iter_open_container failed\n", member); |
| 374 |
return; |
| 375 |
} |
| 376 |
if( !dbus_message_iter_append_fixed_array(&arrayIter, DBUS_TYPE_UINT32, |
| 377 |
&dSwitchesP, sizeof(switches)/sizeof(switches[0])) ) { |
| 378 |
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_message_iter_append_fixed_array failed\n", member); |
| 379 |
return; |
| 380 |
} |
| 381 |
if (!dbus_message_iter_close_container(&args, &arrayIter)) { |
| 382 |
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_message_iter_close_container failed\n", member); |
| 383 |
return; |
| 384 |
} |
| 385 |
} |
| 386 |
else { |
| 387 |
// create a reply from the message |
| 388 |
reply = dbus_message_new_error_printf(msg, |
| 389 |
"org.freedesktop.DBus.Error.NotSupported", |
| 390 |
"Event device \"%s\" not support EV_SW ioctl", |
| 391 |
safepath); |
| 392 |
} |
| 393 |
close(fd); |
| 394 |
} |
| 395 |
else { |
| 396 |
// create a reply from the message |
| 397 |
reply = dbus_message_new_error_printf(msg, |
| 398 |
"org.freedesktop.DBus.Error.InvalidArgs", |
| 399 |
"Event device \"%s\" is invalid", |
| 400 |
rawpath); |
| 401 |
} |
| 402 |
|
| 403 |
// send the reply && flush the connection |
| 404 |
if (!dbus_connection_send(conn, reply, &serial)) { |
| 405 |
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_connection_send failed\n", member); |
| 406 |
return; |
| 407 |
} |
| 408 |
dbus_connection_flush(conn); |
| 409 |
|
| 410 |
// free the reply |
| 411 |
dbus_message_unref(reply); |
| 412 |
|
| 413 |
// free safepath |
| 414 |
free(safepath); |
| 415 |
} |
| 416 |
|
| 320 |
void signal_NameAcquired(DBusMessage* msg) { |
417 |
void signal_NameAcquired(DBusMessage* msg) { |
| 321 |
DBusMessageIter args; |
418 |
DBusMessageIter args; |
| 322 |
char *name = NULL; |
419 |
char *name = NULL; |
|
Lines 354-359
void reply_Introspect(DBusMessage* msg, DBusConnection* conn) {
Link Here
|
| 354 |
} |
451 |
} |
| 355 |
else if(strcmp("/org/trinitydesktop/hardwarecontrol", path) == 0) { |
452 |
else if(strcmp("/org/trinitydesktop/hardwarecontrol", path) == 0) { |
| 356 |
strncat(data, |
453 |
strncat(data, |
|
|
454 |
" <interface name=\"org.trinitydesktop.hardwarecontrol.Brightness\">\n" |
| 455 |
" <method name=\"CanSetBrightness\">\n" |
| 456 |
" <arg name=\"device\" direction=\"in\" type=\"s\" />\n" |
| 457 |
" <arg name=\"value\" direction=\"out\" type=\"b\" />\n" |
| 458 |
" </method>\n" |
| 459 |
" <method name=\"SetBrightness\">\n" |
| 460 |
" <arg name=\"device\" direction=\"in\" type=\"s\" />\n" |
| 461 |
" <arg name=\"brightness\" direction=\"in\" type=\"s\" />\n" |
| 462 |
" <arg name=\"value\" direction=\"out\" type=\"b\" />\n" |
| 463 |
" </method>\n" |
| 464 |
" </interface>\n", |
| 465 |
size-strlen(data)); |
| 466 |
strncat(data, |
| 357 |
" <interface name=\"org.trinitydesktop.hardwarecontrol.CPUGovernor\">\n" |
467 |
" <interface name=\"org.trinitydesktop.hardwarecontrol.CPUGovernor\">\n" |
| 358 |
" <method name=\"CanSetCPUGovernor\">\n" |
468 |
" <method name=\"CanSetCPUGovernor\">\n" |
| 359 |
" <arg name=\"cpu\" direction=\"in\" type=\"i\" />\n" |
469 |
" <arg name=\"cpu\" direction=\"in\" type=\"i\" />\n" |
|
Lines 367-381
void reply_Introspect(DBusMessage* msg, DBusConnection* conn) {
Link Here
|
| 367 |
" </interface>\n", |
477 |
" </interface>\n", |
| 368 |
size-strlen(data)); |
478 |
size-strlen(data)); |
| 369 |
strncat(data, |
479 |
strncat(data, |
| 370 |
" <interface name=\"org.trinitydesktop.hardwarecontrol.Brightness\">\n" |
480 |
" <interface name=\"org.trinitydesktop.hardwarecontrol.InputEvents\">\n" |
| 371 |
" <method name=\"CanSetBrightness\">\n" |
481 |
" <method name=\"GetProvidedSwitches\">\n" |
| 372 |
" <arg name=\"device\" direction=\"in\" type=\"s\" />\n" |
482 |
" <arg name=\"device\" direction=\"in\" type=\"s\" />\n" |
| 373 |
" <arg name=\"value\" direction=\"out\" type=\"b\" />\n" |
483 |
" <arg name=\"value\" direction=\"out\" type=\"au\" />\n" |
| 374 |
" </method>\n" |
484 |
" </method>\n" |
| 375 |
" <method name=\"SetBrightness\">\n" |
485 |
" <method name=\"GetActiveSwitches\">\n" |
| 376 |
" <arg name=\"device\" direction=\"in\" type=\"s\" />\n" |
486 |
" <arg name=\"device\" direction=\"in\" type=\"s\" />\n" |
| 377 |
" <arg name=\"brightness\" direction=\"in\" type=\"s\" />\n" |
487 |
" <arg name=\"value\" direction=\"out\" type=\"au\" />\n" |
| 378 |
" <arg name=\"value\" direction=\"out\" type=\"b\" />\n" |
|
|
| 379 |
" </method>\n" |
488 |
" </method>\n" |
| 380 |
" </interface>\n", |
489 |
" </interface>\n", |
| 381 |
size-strlen(data)); |
490 |
size-strlen(data)); |
|
Lines 439-444
void reply_Introspect(DBusMessage* msg, DBusConnection* conn) {
Link Here
|
| 439 |
free((void*)data); |
548 |
free((void*)data); |
| 440 |
} |
549 |
} |
| 441 |
|
550 |
|
|
|
551 |
void reply_PropertiesGetAll(DBusMessage* msg, DBusConnection* conn) { |
| 552 |
DBusMessage* reply; |
| 553 |
DBusMessageIter args, arrayIter; |
| 554 |
const char* member = dbus_message_get_member(msg); |
| 555 |
dbus_uint32_t serial = 0; |
| 556 |
|
| 557 |
// create a reply from the message |
| 558 |
reply = dbus_message_new_method_return(msg); |
| 559 |
|
| 560 |
// add the arguments to the reply |
| 561 |
dbus_message_iter_init_append(reply, &args); |
| 562 |
if (!dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY, "sv", &arrayIter)) { |
| 563 |
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_message_iter_open_container failed\n", member); |
| 564 |
return; |
| 565 |
} |
| 566 |
if (!dbus_message_iter_close_container(&args, &arrayIter)) { |
| 567 |
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_message_iter_close_container failed\n", member); |
| 568 |
return; |
| 569 |
} |
| 570 |
|
| 571 |
// send the reply && flush the connection |
| 572 |
if (!dbus_connection_send(conn, reply, &serial)) { |
| 573 |
fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_connection_send failed\n", member); |
| 574 |
return; |
| 575 |
} |
| 576 |
dbus_connection_flush(conn); |
| 577 |
|
| 578 |
// free the reply |
| 579 |
dbus_message_unref(reply); |
| 580 |
} |
| 581 |
|
| 442 |
void error_UnknownMessage(DBusMessage* msg, DBusConnection* conn) { |
582 |
void error_UnknownMessage(DBusMessage* msg, DBusConnection* conn) { |
| 443 |
DBusMessage* reply; |
583 |
DBusMessage* reply; |
| 444 |
dbus_uint32_t serial = 0; |
584 |
dbus_uint32_t serial = 0; |
|
Lines 555-566
void listen() {
Link Here
|
| 555 |
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "SetHibernationMethod")) { |
695 |
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "SetHibernationMethod")) { |
| 556 |
reply_SetHibernationMethod(msg, conn); |
696 |
reply_SetHibernationMethod(msg, conn); |
| 557 |
} |
697 |
} |
|
|
698 |
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.InputEvents", "GetProvidedSwitches")) { |
| 699 |
reply_InputEventsGetSwitches(msg, conn, false); |
| 700 |
} |
| 701 |
else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.InputEvents", "GetActiveSwitches")) { |
| 702 |
reply_InputEventsGetSwitches(msg, conn, true); |
| 703 |
} |
| 558 |
else if (dbus_message_is_signal(msg, "org.freedesktop.DBus", "NameAcquired")) { |
704 |
else if (dbus_message_is_signal(msg, "org.freedesktop.DBus", "NameAcquired")) { |
| 559 |
signal_NameAcquired(msg); |
705 |
signal_NameAcquired(msg); |
| 560 |
} |
706 |
} |
| 561 |
else if (dbus_message_is_method_call(msg, "org.freedesktop.DBus.Introspectable", "Introspect")) { |
707 |
else if (dbus_message_is_method_call(msg, "org.freedesktop.DBus.Introspectable", "Introspect")) { |
| 562 |
reply_Introspect(msg, conn); |
708 |
reply_Introspect(msg, conn); |
| 563 |
} |
709 |
} |
|
|
710 |
else if (dbus_message_is_method_call(msg, "org.freedesktop.DBus.Properties", "GetAll")) { |
| 711 |
reply_PropertiesGetAll(msg, conn); |
| 712 |
} |
| 564 |
else { |
713 |
else { |
| 565 |
error_UnknownMessage(msg, conn); |
714 |
error_UnknownMessage(msg, conn); |
| 566 |
} |
715 |
} |