David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #include <stdlib.h> |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 8 | |
Stéphane Marchesin | 62561a1 | 2015-12-11 17:32:37 -0800 | [diff] [blame] | 9 | #include "dbus.h" |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 10 | #include "dbus_interface.h" |
| 11 | #include "image.h" |
| 12 | #include "input.h" |
| 13 | #include "term.h" |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 14 | #include "util.h" |
| 15 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 16 | #define COMMAND_MAKE_VT "MakeVT" |
| 17 | #define COMMAND_SWITCH_VT "SwitchVT" |
| 18 | #define COMMAND_TERMINATE "Terminate" |
| 19 | #define COMMAND_IMAGE "Image" |
| 20 | |
| 21 | #define DBUS_DEFAULT_DELAY 3000 |
| 22 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 23 | typedef struct _dbus_t dbus_t; |
| 24 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 25 | struct _dbus_t { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 26 | DBusConnection* conn; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 27 | DBusWatch* watch; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 28 | int fd; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 31 | static dbus_t *dbus = NULL; |
| 32 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 33 | static DBusHandlerResult handle_switchvt(DBusConnection* connection, |
| 34 | DBusMessage* message) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 35 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 36 | DBusMessage* reply; |
| 37 | DBusMessage* msg; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 38 | DBusError error; |
| 39 | dbus_bool_t stat; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 40 | terminal_t* terminal; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 41 | unsigned int vt; |
| 42 | |
| 43 | dbus_error_init(&error); |
| 44 | stat = dbus_message_get_args(message, &error, DBUS_TYPE_UINT32, |
| 45 | &vt, DBUS_TYPE_INVALID); |
| 46 | |
| 47 | if (!stat) { |
| 48 | LOG(ERROR, "SwitchVT method error, no VT argument"); |
| 49 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 50 | } |
| 51 | |
Stéphane Marchesin | 92a297d | 2016-01-07 20:24:55 -0800 | [diff] [blame] | 52 | if (vt > term_get_max_terminals()) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 53 | LOG(ERROR, "SwtichVT: invalid terminal"); |
| 54 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 55 | } |
| 56 | |
| 57 | if (vt == 0) { |
Stéphane Marchesin | 0a7ce42 | 2016-01-07 20:45:47 -0800 | [diff] [blame] | 58 | terminal = term_create_term(vt); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 59 | if (term_is_active(terminal)) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 60 | term_deactivate(terminal); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 61 | msg = dbus_message_new_method_call( |
| 62 | kLibCrosServiceName, |
| 63 | kLibCrosServicePath, |
| 64 | kLibCrosServiceInterface, |
| 65 | kTakeDisplayOwnership); |
| 66 | dbus_connection_send_with_reply_and_block(connection, msg, |
| 67 | DBUS_DEFAULT_DELAY, NULL); |
| 68 | } |
| 69 | reply = dbus_message_new_method_return(message); |
| 70 | dbus_connection_send(connection, reply, NULL); |
| 71 | return DBUS_HANDLER_RESULT_HANDLED; |
| 72 | } else { |
| 73 | /* |
| 74 | * If we are switching to a new term, and if a |
| 75 | * given term is active, then de-activate the |
| 76 | * current terminal |
| 77 | */ |
Stéphane Marchesin | e38b8e7 | 2016-01-07 19:20:28 -0800 | [diff] [blame] | 78 | terminal = input_get_current_term(); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 79 | if (term_is_active(terminal)) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 80 | term_deactivate(terminal); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 81 | |
Stéphane Marchesin | 0a7ce42 | 2016-01-07 20:45:47 -0800 | [diff] [blame] | 82 | terminal = term_create_term(vt); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 83 | if (term_is_valid(terminal)) { |
| 84 | msg = dbus_message_new_method_call( |
| 85 | kLibCrosServiceName, |
| 86 | kLibCrosServicePath, |
| 87 | kLibCrosServiceInterface, |
| 88 | kReleaseDisplayOwnership); |
| 89 | dbus_connection_send_with_reply_and_block(connection, msg, |
| 90 | DBUS_DEFAULT_DELAY, NULL); |
| 91 | term_activate(terminal); |
| 92 | |
| 93 | reply = dbus_message_new_method_return(message); |
| 94 | dbus_connection_send(connection, reply, NULL); |
| 95 | return DBUS_HANDLER_RESULT_HANDLED; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 100 | } |
| 101 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 102 | static DBusHandlerResult handle_makevt(DBusConnection* connection, |
| 103 | DBusMessage* message) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 104 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 105 | DBusMessage* reply; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 106 | DBusError error; |
| 107 | dbus_bool_t stat; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 108 | terminal_t* terminal; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 109 | unsigned int vt; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 110 | const char* reply_str; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 111 | |
| 112 | dbus_error_init(&error); |
| 113 | stat = dbus_message_get_args(message, &error, DBUS_TYPE_UINT32, |
| 114 | &vt, DBUS_TYPE_INVALID); |
| 115 | |
| 116 | if (!stat) { |
| 117 | LOG(ERROR, "SwitchVT method error, not VT argument"); |
| 118 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 119 | } |
| 120 | |
Stéphane Marchesin | 92a297d | 2016-01-07 20:24:55 -0800 | [diff] [blame] | 121 | if ((vt < 1) || (vt > term_get_max_terminals())) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 122 | LOG(ERROR, "SwtichVT: invalid terminal"); |
| 123 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 124 | } |
| 125 | |
Stéphane Marchesin | 0a7ce42 | 2016-01-07 20:45:47 -0800 | [diff] [blame] | 126 | terminal = term_create_term(vt); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 127 | reply_str = term_get_ptsname(terminal); |
| 128 | |
| 129 | reply = dbus_message_new_method_return(message); |
| 130 | dbus_message_append_args(reply, |
| 131 | DBUS_TYPE_STRING, &reply_str, |
| 132 | DBUS_TYPE_INVALID); |
| 133 | dbus_connection_send(connection, reply, NULL); |
| 134 | |
| 135 | return DBUS_HANDLER_RESULT_HANDLED; |
| 136 | } |
| 137 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 138 | static DBusHandlerResult handle_terminate(DBusConnection* connection, |
| 139 | DBusMessage* message) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 140 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 141 | DBusMessage* reply; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 142 | |
| 143 | reply = dbus_message_new_method_return(message); |
| 144 | dbus_connection_send(connection, reply, NULL); |
| 145 | exit(EXIT_SUCCESS); |
| 146 | } |
| 147 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 148 | #define NUM_IMAGE_PARAMETERS (2) |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 149 | static DBusHandlerResult handle_image(DBusConnection* connection, |
| 150 | DBusMessage* message) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 151 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 152 | DBusMessage* reply; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 153 | DBusError error; |
| 154 | dbus_bool_t stat; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 155 | terminal_t* terminal; |
| 156 | image_t* image; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 157 | int i; |
| 158 | int x, y; |
| 159 | char* option[NUM_IMAGE_PARAMETERS]; |
| 160 | char* optname; |
| 161 | char* optval; |
| 162 | int status; |
| 163 | |
| 164 | dbus_error_init(&error); |
| 165 | stat = dbus_message_get_args(message, &error, |
| 166 | DBUS_TYPE_STRING, &option[0], |
| 167 | DBUS_TYPE_STRING, &option[1], |
| 168 | DBUS_TYPE_INVALID); |
| 169 | |
| 170 | image = image_create(); |
| 171 | if (image == NULL) { |
| 172 | LOG(WARNING, "failed to create image"); |
| 173 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 174 | } |
| 175 | |
| 176 | if (stat) { |
| 177 | for (i = 0; i < NUM_IMAGE_PARAMETERS; i++) { |
| 178 | optname = NULL; |
| 179 | optval = NULL; |
| 180 | parse_image_option(option[i], &optname, &optval); |
| 181 | if (strncmp(optname, "image", strlen("image")) == 0) { |
| 182 | image_set_filename(image, optval); |
| 183 | } else if (strncmp(optname, "location", strlen("location")) == 0) { |
| 184 | parse_location(optval, &x, &y); |
| 185 | image_set_location(image, x, y); |
| 186 | } else if (strncmp(optname, "offset", strlen("offset")) == 0) { |
| 187 | parse_location(optval, &x, &y); |
| 188 | image_set_offset(image, x, y); |
| 189 | } |
| 190 | if (optname) |
| 191 | free(optname); |
| 192 | if (optval) |
| 193 | free(optval); |
| 194 | } |
| 195 | } else { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 196 | goto fail; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | status = image_load_image_from_file(image); |
| 200 | if (status != 0) { |
| 201 | LOG(WARNING, "image_load_image_from_file failed: %d", status); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 202 | goto fail; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Stéphane Marchesin | e38b8e7 | 2016-01-07 19:20:28 -0800 | [diff] [blame] | 205 | terminal = input_get_current_term(); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 206 | if (!terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 207 | goto fail; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 208 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 209 | status = term_show_image(terminal, image); |
| 210 | if (status != 0) { |
| 211 | LOG(WARNING, "term_show_image failed: %d", status); |
| 212 | goto fail; |
| 213 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 214 | image_release(image); |
| 215 | |
| 216 | reply = dbus_message_new_method_return(message); |
| 217 | dbus_connection_send(connection, reply, NULL); |
| 218 | |
| 219 | return DBUS_HANDLER_RESULT_HANDLED; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 220 | fail: |
| 221 | if (image) |
| 222 | image_release(image); |
| 223 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 226 | static void frecon_dbus_unregister(DBusConnection* connection, void* user_data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 227 | { |
| 228 | } |
| 229 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 230 | static DBusHandlerResult frecon_dbus_message_handler(DBusConnection* connection, |
| 231 | DBusMessage* message, |
| 232 | void* user_data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 233 | { |
| 234 | if (dbus_message_is_method_call(message, |
| 235 | kFreconDbusInterface, COMMAND_SWITCH_VT)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 236 | return handle_switchvt(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 237 | } else if (dbus_message_is_method_call(message, |
| 238 | kFreconDbusInterface, COMMAND_MAKE_VT)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 239 | return handle_makevt(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 240 | } |
| 241 | else if (dbus_message_is_method_call(message, |
| 242 | kFreconDbusInterface, COMMAND_TERMINATE)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 243 | return handle_terminate(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 244 | } else if (dbus_message_is_method_call(message, |
| 245 | kFreconDbusInterface, COMMAND_IMAGE)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 246 | return handle_image(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static DBusObjectPathVTable |
| 253 | frecon_vtable = { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 254 | frecon_dbus_unregister, |
| 255 | frecon_dbus_message_handler, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 256 | NULL |
| 257 | }; |
| 258 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 259 | static dbus_bool_t add_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 260 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 261 | dbus_t* dbus = (dbus_t*)data; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 262 | dbus->watch = w; |
| 263 | |
| 264 | return TRUE; |
| 265 | } |
| 266 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 267 | static void remove_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 268 | { |
| 269 | } |
| 270 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 271 | static void toggle_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 272 | { |
| 273 | } |
| 274 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 275 | bool dbus_is_initialized(void) |
| 276 | { |
| 277 | return !!dbus; |
| 278 | } |
| 279 | |
| 280 | bool dbus_init() |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 281 | { |
| 282 | dbus_t* new_dbus; |
| 283 | DBusError err; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 284 | int result; |
| 285 | dbus_bool_t stat; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 286 | |
| 287 | dbus_error_init(&err); |
| 288 | |
| 289 | new_dbus = (dbus_t*)calloc(1, sizeof(*new_dbus)); |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 290 | |
| 291 | if (!new_dbus) |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 292 | return false; |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 293 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 294 | new_dbus->fd = -1; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 295 | |
| 296 | new_dbus->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); |
| 297 | if (dbus_error_is_set(&err)) { |
| 298 | LOG(ERROR, "Cannot get dbus connection"); |
| 299 | free(new_dbus); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 300 | return false; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 301 | } |
| 302 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 303 | result = dbus_bus_request_name(new_dbus->conn, kFreconDbusInterface, |
| 304 | DBUS_NAME_FLAG_DO_NOT_QUEUE, &err); |
| 305 | |
| 306 | if (result <= 0) { |
| 307 | LOG(ERROR, "Unable to get name for server"); |
| 308 | } |
| 309 | |
| 310 | stat = dbus_connection_register_object_path(new_dbus->conn, |
| 311 | kFreconDbusPath, |
| 312 | &frecon_vtable, |
| 313 | NULL); |
| 314 | |
| 315 | if (!stat) { |
| 316 | LOG(ERROR, "failed to register object path"); |
| 317 | } |
| 318 | |
| 319 | stat = dbus_connection_set_watch_functions(new_dbus->conn, |
| 320 | add_watch, remove_watch, toggle_watch, |
| 321 | new_dbus, NULL); |
| 322 | |
| 323 | if (!stat) { |
| 324 | LOG(ERROR, "Failed to set watch functions"); |
| 325 | } |
| 326 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 327 | dbus_connection_set_exit_on_disconnect(new_dbus->conn, FALSE); |
| 328 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 329 | dbus = new_dbus; |
| 330 | return true; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 333 | static bool dbus_method_call0(const char* service_name, |
| 334 | const char* service_path, |
| 335 | const char* service_interface, |
| 336 | const char* method) |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 337 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 338 | DBusMessage* msg = NULL; |
| 339 | if (!dbus) { |
| 340 | LOG(ERROR, "dbus not initialized"); |
| 341 | return false; |
| 342 | } |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 343 | |
| 344 | msg = dbus_message_new_method_call(service_name, |
| 345 | service_path, service_interface, method); |
| 346 | |
| 347 | if (!msg) |
| 348 | return false; |
| 349 | |
| 350 | if (!dbus_connection_send_with_reply_and_block(dbus->conn, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 351 | msg, DBUS_DEFAULT_DELAY, NULL)) { |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 352 | dbus_message_unref(msg); |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | dbus_connection_flush(dbus->conn); |
| 357 | dbus_message_unref(msg); |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 362 | static bool dbus_method_call1(const char* service_name, |
| 363 | const char* service_path, |
| 364 | const char* service_interface, |
| 365 | const char* method, int arg_type, void* param) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 366 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 367 | DBusMessage* msg = NULL; |
| 368 | if (!dbus) { |
| 369 | LOG(ERROR, "dbus not initialized"); |
| 370 | return false; |
| 371 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 372 | |
| 373 | msg = dbus_message_new_method_call(service_name, |
| 374 | service_path, service_interface, method); |
| 375 | |
| 376 | if (!msg) |
| 377 | return false; |
| 378 | |
| 379 | if (!dbus_message_append_args(msg, |
David Sodman | 19e4f9d | 2015-03-10 11:11:09 -0700 | [diff] [blame] | 380 | arg_type, param, DBUS_TYPE_INVALID)) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 381 | dbus_message_unref(msg); |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | if (!dbus_connection_send_with_reply_and_block(dbus->conn, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 386 | msg, DBUS_DEFAULT_DELAY, NULL)) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 387 | dbus_message_unref(msg); |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | dbus_connection_flush(dbus->conn); |
| 392 | dbus_message_unref(msg); |
| 393 | |
| 394 | return true; |
| 395 | } |
| 396 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 397 | void dbus_destroy(void) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 398 | { |
| 399 | /* FIXME - not sure what the right counterpart to |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 400 | * dbus_bus_get() is, unref documentation is rather |
| 401 | * unclear. Not a big issue but it would be nice to |
| 402 | * clean up properly here |
| 403 | */ |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 404 | /* dbus_connection_unref(dbus->conn); */ |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 405 | if (dbus) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 406 | free(dbus); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 407 | dbus = NULL; |
| 408 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 411 | int dbus_add_fds(fd_set* read_set, fd_set* exception_set) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 412 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 413 | if (!dbus) |
| 414 | return -1; |
| 415 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 416 | if (dbus->fd < 0) |
| 417 | dbus->fd = dbus_watch_get_unix_fd(dbus->watch); |
| 418 | |
| 419 | if (dbus->fd >= 0) { |
| 420 | FD_SET(dbus->fd, read_set); |
| 421 | FD_SET(dbus->fd, exception_set); |
| 422 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 423 | |
| 424 | return dbus->fd; |
| 425 | } |
| 426 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 427 | void dbus_dispatch_io(void) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 428 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 429 | if (!dbus) |
| 430 | return; |
| 431 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 432 | dbus_watch_handle(dbus->watch, DBUS_WATCH_READABLE); |
| 433 | while (dbus_connection_get_dispatch_status(dbus->conn) |
| 434 | == DBUS_DISPATCH_DATA_REMAINS) { |
| 435 | dbus_connection_dispatch(dbus->conn); |
| 436 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 437 | } |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame^] | 438 | |
| 439 | void dbus_report_user_activity(int activity_type) |
| 440 | { |
| 441 | dbus_bool_t allow_off = false; |
| 442 | if (!dbus) |
| 443 | return; |
| 444 | |
| 445 | dbus_method_call1(kPowerManagerServiceName, |
| 446 | kPowerManagerServicePath, |
| 447 | kPowerManagerInterface, |
| 448 | kHandleUserActivityMethod, |
| 449 | DBUS_TYPE_INT32, &activity_type); |
| 450 | |
| 451 | switch (activity_type) { |
| 452 | case USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS: |
| 453 | (void)dbus_method_call0(kPowerManagerServiceName, |
| 454 | kPowerManagerServicePath, |
| 455 | kPowerManagerInterface, |
| 456 | kIncreaseScreenBrightnessMethod); |
| 457 | break; |
| 458 | case USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS: |
| 459 | /* |
| 460 | * Shouldn't allow the screen to go |
| 461 | * completely off while frecon is active |
| 462 | * so passing false to allow_off |
| 463 | */ |
| 464 | (void)dbus_method_call1(kPowerManagerServiceName, |
| 465 | kPowerManagerServicePath, |
| 466 | kPowerManagerInterface, |
| 467 | kDecreaseScreenBrightnessMethod, |
| 468 | DBUS_TYPE_BOOLEAN, &allow_off); |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | void dbus_take_display_ownership(void) |
| 474 | { |
| 475 | if (!dbus) |
| 476 | return; |
| 477 | (void)dbus_method_call0(kLibCrosServiceName, |
| 478 | kLibCrosServicePath, |
| 479 | kLibCrosServiceInterface, |
| 480 | kTakeDisplayOwnership); |
| 481 | } |
| 482 | |
| 483 | void dbus_release_display_ownership(void) |
| 484 | { |
| 485 | if (!dbus) |
| 486 | return; |
| 487 | (void)dbus_method_call0(kLibCrosServiceName, |
| 488 | kLibCrosServicePath, |
| 489 | kLibCrosServiceInterface, |
| 490 | kReleaseDisplayOwnership); |
| 491 | } |
| 492 | |