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