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 | |
Dominik Behr | f50c98f | 2016-03-31 14:05:29 -0700 | [diff] [blame] | 7 | #if DBUS |
| 8 | #include <dbus/dbus.h> |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 9 | #include <stdlib.h> |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 10 | |
Stéphane Marchesin | 62561a1 | 2015-12-11 17:32:37 -0800 | [diff] [blame] | 11 | #include "dbus.h" |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 12 | #include "dbus_interface.h" |
| 13 | #include "image.h" |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 14 | #include "main.h" |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 15 | #include "term.h" |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 16 | #include "util.h" |
| 17 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 18 | #define COMMAND_MAKE_VT "MakeVT" |
| 19 | #define COMMAND_SWITCH_VT "SwitchVT" |
| 20 | #define COMMAND_TERMINATE "Terminate" |
| 21 | #define COMMAND_IMAGE "Image" |
| 22 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 23 | #define DBUS_WAIT_DELAY_US (50000) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 24 | #define DBUS_DEFAULT_DELAY 3000 |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 25 | #define DBUS_INIT_TIMEOUT_MS (60*1000) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 26 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 27 | typedef struct _dbus_t dbus_t; |
| 28 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 29 | static void (*login_prompt_visible_callback)(void*) = NULL; |
| 30 | static void* login_prompt_visible_callback_userptr = NULL; |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame^] | 31 | static void (*suspend_done_callback)(void*) = NULL; |
| 32 | static void* suspend_done_callback_userptr = NULL; |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 33 | static bool chrome_is_already_up = false; |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 34 | static bool dbus_connect_fail = false; |
| 35 | static int64_t dbus_connect_fail_time; |
| 36 | static bool dbus_first_init = true; |
| 37 | static int64_t dbus_first_init_time; |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 38 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 39 | struct _dbus_t { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 40 | DBusConnection* conn; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 41 | DBusWatch* watch; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 42 | int fd; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 45 | static dbus_t *dbus = NULL; |
| 46 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 47 | static DBusHandlerResult handle_switchvt(DBusConnection* connection, |
| 48 | DBusMessage* message) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 49 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 50 | DBusMessage* reply; |
| 51 | DBusMessage* msg; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 52 | DBusError error; |
| 53 | dbus_bool_t stat; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 54 | terminal_t* terminal; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 55 | unsigned int vt; |
| 56 | |
| 57 | dbus_error_init(&error); |
| 58 | stat = dbus_message_get_args(message, &error, DBUS_TYPE_UINT32, |
| 59 | &vt, DBUS_TYPE_INVALID); |
| 60 | |
| 61 | if (!stat) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 62 | LOG(ERROR, "SwitchVT method error, no VT argument."); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 63 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 64 | } |
| 65 | |
Stéphane Marchesin | 92a297d | 2016-01-07 20:24:55 -0800 | [diff] [blame] | 66 | if (vt > term_get_max_terminals()) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 67 | LOG(ERROR, "SwtichVT: invalid terminal."); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 68 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 69 | } |
| 70 | |
| 71 | if (vt == 0) { |
Stéphane Marchesin | 0a7ce42 | 2016-01-07 20:45:47 -0800 | [diff] [blame] | 72 | terminal = term_create_term(vt); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 73 | if (term_is_active(terminal)) { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 74 | term_deactivate(terminal); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 75 | msg = dbus_message_new_method_call( |
| 76 | kLibCrosServiceName, |
| 77 | kLibCrosServicePath, |
| 78 | kLibCrosServiceInterface, |
| 79 | kTakeDisplayOwnership); |
| 80 | dbus_connection_send_with_reply_and_block(connection, msg, |
| 81 | DBUS_DEFAULT_DELAY, NULL); |
| 82 | } |
| 83 | reply = dbus_message_new_method_return(message); |
| 84 | dbus_connection_send(connection, reply, NULL); |
| 85 | return DBUS_HANDLER_RESULT_HANDLED; |
| 86 | } else { |
| 87 | /* |
| 88 | * If we are switching to a new term, and if a |
| 89 | * given term is active, then de-activate the |
| 90 | * current terminal |
| 91 | */ |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 92 | terminal = term_get_current_terminal(); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 93 | if (term_is_active(terminal)) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 94 | term_deactivate(terminal); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 95 | |
Stéphane Marchesin | 0a7ce42 | 2016-01-07 20:45:47 -0800 | [diff] [blame] | 96 | terminal = term_create_term(vt); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 97 | if (term_is_valid(terminal)) { |
| 98 | msg = dbus_message_new_method_call( |
| 99 | kLibCrosServiceName, |
| 100 | kLibCrosServicePath, |
| 101 | kLibCrosServiceInterface, |
| 102 | kReleaseDisplayOwnership); |
| 103 | dbus_connection_send_with_reply_and_block(connection, msg, |
| 104 | DBUS_DEFAULT_DELAY, NULL); |
| 105 | term_activate(terminal); |
| 106 | |
| 107 | reply = dbus_message_new_method_return(message); |
| 108 | dbus_connection_send(connection, reply, NULL); |
| 109 | return DBUS_HANDLER_RESULT_HANDLED; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 114 | } |
| 115 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 116 | static DBusHandlerResult handle_makevt(DBusConnection* connection, |
| 117 | DBusMessage* message) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 118 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 119 | DBusMessage* reply; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 120 | DBusError error; |
| 121 | dbus_bool_t stat; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 122 | terminal_t* terminal; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 123 | unsigned int vt; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 124 | const char* reply_str; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 125 | |
| 126 | dbus_error_init(&error); |
| 127 | stat = dbus_message_get_args(message, &error, DBUS_TYPE_UINT32, |
| 128 | &vt, DBUS_TYPE_INVALID); |
| 129 | |
| 130 | if (!stat) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 131 | LOG(ERROR, "SwitchVT method error, not VT argument."); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 132 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 133 | } |
| 134 | |
Stéphane Marchesin | 92a297d | 2016-01-07 20:24:55 -0800 | [diff] [blame] | 135 | if ((vt < 1) || (vt > term_get_max_terminals())) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 136 | LOG(ERROR, "SwtichVT: invalid terminal."); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 137 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 138 | } |
| 139 | |
Stéphane Marchesin | 0a7ce42 | 2016-01-07 20:45:47 -0800 | [diff] [blame] | 140 | terminal = term_create_term(vt); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 141 | reply_str = term_get_ptsname(terminal); |
| 142 | |
| 143 | reply = dbus_message_new_method_return(message); |
| 144 | dbus_message_append_args(reply, |
| 145 | DBUS_TYPE_STRING, &reply_str, |
| 146 | DBUS_TYPE_INVALID); |
| 147 | dbus_connection_send(connection, reply, NULL); |
| 148 | |
| 149 | return DBUS_HANDLER_RESULT_HANDLED; |
| 150 | } |
| 151 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 152 | static DBusHandlerResult handle_terminate(DBusConnection* connection, |
| 153 | DBusMessage* message) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 154 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 155 | DBusMessage* reply; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 156 | |
| 157 | reply = dbus_message_new_method_return(message); |
| 158 | dbus_connection_send(connection, reply, NULL); |
| 159 | exit(EXIT_SUCCESS); |
| 160 | } |
| 161 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 162 | #define NUM_IMAGE_PARAMETERS (2) |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 163 | static DBusHandlerResult handle_image(DBusConnection* connection, |
| 164 | DBusMessage* message) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 165 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 166 | DBusMessage* reply; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 167 | DBusError error; |
| 168 | dbus_bool_t stat; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 169 | terminal_t* terminal; |
| 170 | image_t* image; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 171 | int i; |
| 172 | int x, y; |
| 173 | char* option[NUM_IMAGE_PARAMETERS]; |
| 174 | char* optname; |
| 175 | char* optval; |
| 176 | int status; |
| 177 | |
| 178 | dbus_error_init(&error); |
| 179 | stat = dbus_message_get_args(message, &error, |
| 180 | DBUS_TYPE_STRING, &option[0], |
| 181 | DBUS_TYPE_STRING, &option[1], |
| 182 | DBUS_TYPE_INVALID); |
| 183 | |
| 184 | image = image_create(); |
| 185 | if (image == NULL) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 186 | LOG(WARNING, "Failed to create image."); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 187 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 188 | } |
| 189 | |
| 190 | if (stat) { |
| 191 | for (i = 0; i < NUM_IMAGE_PARAMETERS; i++) { |
| 192 | optname = NULL; |
| 193 | optval = NULL; |
| 194 | parse_image_option(option[i], &optname, &optval); |
| 195 | if (strncmp(optname, "image", strlen("image")) == 0) { |
| 196 | image_set_filename(image, optval); |
| 197 | } else if (strncmp(optname, "location", strlen("location")) == 0) { |
| 198 | parse_location(optval, &x, &y); |
| 199 | image_set_location(image, x, y); |
| 200 | } else if (strncmp(optname, "offset", strlen("offset")) == 0) { |
| 201 | parse_location(optval, &x, &y); |
| 202 | image_set_offset(image, x, y); |
| 203 | } |
| 204 | if (optname) |
| 205 | free(optname); |
| 206 | if (optval) |
| 207 | free(optval); |
| 208 | } |
| 209 | } else { |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 210 | goto fail; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | status = image_load_image_from_file(image); |
| 214 | if (status != 0) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 215 | LOG(WARNING, "image_load_image_from_file(dbus) %s failed: %d:%s.", |
| 216 | image_get_filename(image), status, strerror(status)); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 217 | goto fail; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 220 | terminal = term_get_current_terminal(); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 221 | if (!terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 222 | goto fail; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 223 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 224 | status = term_show_image(terminal, image); |
| 225 | if (status != 0) { |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 226 | LOG(WARNING, "term_show_image(dbus) failed: %d.", status); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 227 | goto fail; |
| 228 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 229 | image_release(image); |
| 230 | |
| 231 | reply = dbus_message_new_method_return(message); |
| 232 | dbus_connection_send(connection, reply, NULL); |
| 233 | |
| 234 | return DBUS_HANDLER_RESULT_HANDLED; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 235 | fail: |
| 236 | if (image) |
| 237 | image_release(image); |
| 238 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 241 | static void frecon_dbus_unregister(DBusConnection* connection, void* user_data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 242 | { |
| 243 | } |
| 244 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 245 | static DBusHandlerResult frecon_dbus_message_handler(DBusConnection* connection, |
| 246 | DBusMessage* message, |
| 247 | void* user_data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 248 | { |
| 249 | if (dbus_message_is_method_call(message, |
| 250 | kFreconDbusInterface, COMMAND_SWITCH_VT)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 251 | return handle_switchvt(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 252 | } else if (dbus_message_is_method_call(message, |
| 253 | kFreconDbusInterface, COMMAND_MAKE_VT)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 254 | return handle_makevt(connection, message); |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame^] | 255 | } else if (dbus_message_is_method_call(message, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 256 | kFreconDbusInterface, COMMAND_TERMINATE)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 257 | return handle_terminate(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 258 | } else if (dbus_message_is_method_call(message, |
| 259 | kFreconDbusInterface, COMMAND_IMAGE)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 260 | return handle_image(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static DBusObjectPathVTable |
| 267 | frecon_vtable = { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 268 | frecon_dbus_unregister, |
| 269 | frecon_dbus_message_handler, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 270 | NULL |
| 271 | }; |
| 272 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 273 | static dbus_bool_t add_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 274 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 275 | dbus_t* dbus = (dbus_t*)data; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 276 | dbus->watch = w; |
| 277 | |
| 278 | return TRUE; |
| 279 | } |
| 280 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 281 | static void remove_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 282 | { |
| 283 | } |
| 284 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 285 | static void toggle_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 286 | { |
| 287 | } |
| 288 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 289 | static DBusHandlerResult handle_login_prompt_visible(DBusMessage* message) |
| 290 | { |
| 291 | if (login_prompt_visible_callback) { |
| 292 | login_prompt_visible_callback(login_prompt_visible_callback_userptr); |
| 293 | login_prompt_visible_callback = NULL; |
| 294 | login_prompt_visible_callback_userptr = NULL; |
| 295 | } |
| 296 | chrome_is_already_up = true; |
| 297 | |
| 298 | return DBUS_HANDLER_RESULT_HANDLED; |
| 299 | } |
| 300 | |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame^] | 301 | static DBusHandlerResult handle_suspend_done(DBusMessage* message) |
| 302 | { |
| 303 | if (suspend_done_callback) |
| 304 | suspend_done_callback(suspend_done_callback_userptr); |
| 305 | |
| 306 | return DBUS_HANDLER_RESULT_HANDLED; |
| 307 | } |
| 308 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 309 | static DBusHandlerResult frecon_dbus_message_filter(DBusConnection* connection, |
| 310 | DBusMessage* message, |
| 311 | void* user_data) |
| 312 | { |
| 313 | if (dbus_message_is_signal(message, |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 314 | kSessionManagerInterface, kLoginPromptVisibleSignal)) |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 315 | return handle_login_prompt_visible(message); |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame^] | 316 | else if (dbus_message_is_signal(message, |
| 317 | kPowerManagerInterface, kSuspendDoneSignal)) |
| 318 | return handle_suspend_done(message); |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 319 | |
| 320 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 321 | } |
| 322 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 323 | bool dbus_is_initialized(void) |
| 324 | { |
| 325 | return !!dbus; |
| 326 | } |
| 327 | |
| 328 | bool dbus_init() |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 329 | { |
| 330 | dbus_t* new_dbus; |
| 331 | DBusError err; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 332 | int result; |
| 333 | dbus_bool_t stat; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 334 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 335 | if (dbus_first_init) { |
| 336 | dbus_first_init = false; |
| 337 | dbus_first_init_time = get_monotonic_time_ms(); |
| 338 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 339 | dbus_error_init(&err); |
| 340 | |
| 341 | new_dbus = (dbus_t*)calloc(1, sizeof(*new_dbus)); |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 342 | |
| 343 | if (!new_dbus) |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 344 | return false; |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 345 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 346 | new_dbus->fd = -1; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 347 | |
| 348 | new_dbus->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); |
| 349 | if (dbus_error_is_set(&err)) { |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 350 | if (!dbus_connect_fail) { |
| 351 | LOG(ERROR, "Cannot get DBUS connection"); |
| 352 | dbus_connect_fail = true; |
| 353 | dbus_connect_fail_time = get_monotonic_time_ms(); |
| 354 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 355 | free(new_dbus); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 356 | return false; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 359 | if (dbus_connect_fail) { |
| 360 | int64_t t = get_monotonic_time_ms() - dbus_connect_fail_time; |
| 361 | LOG(INFO, "DBUS connected after %.1f seconds", (float)t / 1000.0f); |
| 362 | } |
| 363 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 364 | result = dbus_bus_request_name(new_dbus->conn, kFreconDbusInterface, |
| 365 | DBUS_NAME_FLAG_DO_NOT_QUEUE, &err); |
| 366 | |
| 367 | if (result <= 0) { |
| 368 | LOG(ERROR, "Unable to get name for server"); |
| 369 | } |
| 370 | |
| 371 | stat = dbus_connection_register_object_path(new_dbus->conn, |
| 372 | kFreconDbusPath, |
| 373 | &frecon_vtable, |
| 374 | NULL); |
| 375 | |
| 376 | if (!stat) { |
| 377 | LOG(ERROR, "failed to register object path"); |
| 378 | } |
| 379 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 380 | dbus_bus_add_match(new_dbus->conn, kLoginPromptVisibleRule, &err); |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame^] | 381 | dbus_bus_add_match(new_dbus->conn, kSuspendDoneRule, &err); |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 382 | |
| 383 | stat = dbus_connection_add_filter(new_dbus->conn, frecon_dbus_message_filter, NULL, NULL); |
| 384 | if (!stat) { |
| 385 | LOG(ERROR, "failed to add message filter"); |
| 386 | } |
| 387 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 388 | stat = dbus_connection_set_watch_functions(new_dbus->conn, |
| 389 | add_watch, remove_watch, toggle_watch, |
| 390 | new_dbus, NULL); |
| 391 | |
| 392 | if (!stat) { |
| 393 | LOG(ERROR, "Failed to set watch functions"); |
| 394 | } |
| 395 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 396 | dbus_connection_set_exit_on_disconnect(new_dbus->conn, FALSE); |
| 397 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 398 | dbus = new_dbus; |
| 399 | return true; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 402 | bool dbus_init_wait() |
| 403 | { |
| 404 | while (!dbus_is_initialized()) { |
| 405 | if (!dbus_init()) { |
| 406 | int64_t t = get_monotonic_time_ms() - dbus_first_init_time; |
| 407 | if (t >= DBUS_INIT_TIMEOUT_MS) { |
| 408 | LOG(ERROR, "DBUS init failed after a timeout of %u sec", DBUS_INIT_TIMEOUT_MS/1000); |
| 409 | return false; |
| 410 | } |
| 411 | } |
| 412 | usleep(DBUS_WAIT_DELAY_US); |
| 413 | } |
| 414 | return true; |
| 415 | } |
| 416 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 417 | static bool dbus_method_call0(const char* service_name, |
| 418 | const char* service_path, |
| 419 | const char* service_interface, |
| 420 | const char* method) |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 421 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 422 | DBusMessage* msg = NULL; |
| 423 | if (!dbus) { |
| 424 | LOG(ERROR, "dbus not initialized"); |
| 425 | return false; |
| 426 | } |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 427 | |
| 428 | msg = dbus_message_new_method_call(service_name, |
| 429 | service_path, service_interface, method); |
| 430 | |
| 431 | if (!msg) |
| 432 | return false; |
| 433 | |
| 434 | if (!dbus_connection_send_with_reply_and_block(dbus->conn, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 435 | msg, DBUS_DEFAULT_DELAY, NULL)) { |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 436 | dbus_message_unref(msg); |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | dbus_connection_flush(dbus->conn); |
| 441 | dbus_message_unref(msg); |
| 442 | |
| 443 | return true; |
| 444 | } |
| 445 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 446 | static bool dbus_method_call1(const char* service_name, |
| 447 | const char* service_path, |
| 448 | const char* service_interface, |
| 449 | const char* method, int arg_type, void* param) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 450 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 451 | DBusMessage* msg = NULL; |
| 452 | if (!dbus) { |
| 453 | LOG(ERROR, "dbus not initialized"); |
| 454 | return false; |
| 455 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 456 | |
| 457 | msg = dbus_message_new_method_call(service_name, |
| 458 | service_path, service_interface, method); |
| 459 | |
| 460 | if (!msg) |
| 461 | return false; |
| 462 | |
| 463 | if (!dbus_message_append_args(msg, |
David Sodman | 19e4f9d | 2015-03-10 11:11:09 -0700 | [diff] [blame] | 464 | arg_type, param, DBUS_TYPE_INVALID)) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 465 | dbus_message_unref(msg); |
| 466 | return false; |
| 467 | } |
| 468 | |
| 469 | if (!dbus_connection_send_with_reply_and_block(dbus->conn, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 470 | msg, DBUS_DEFAULT_DELAY, NULL)) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 471 | dbus_message_unref(msg); |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | dbus_connection_flush(dbus->conn); |
| 476 | dbus_message_unref(msg); |
| 477 | |
| 478 | return true; |
| 479 | } |
| 480 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 481 | void dbus_destroy(void) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 482 | { |
| 483 | /* FIXME - not sure what the right counterpart to |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 484 | * dbus_bus_get() is, unref documentation is rather |
| 485 | * unclear. Not a big issue but it would be nice to |
| 486 | * clean up properly here |
| 487 | */ |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 488 | /* dbus_connection_unref(dbus->conn); */ |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 489 | if (dbus) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 490 | free(dbus); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 491 | dbus = NULL; |
| 492 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 495 | 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] | 496 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 497 | if (!dbus) |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 498 | return; |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 499 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 500 | if (dbus->fd < 0) |
| 501 | dbus->fd = dbus_watch_get_unix_fd(dbus->watch); |
| 502 | |
| 503 | if (dbus->fd >= 0) { |
| 504 | FD_SET(dbus->fd, read_set); |
| 505 | FD_SET(dbus->fd, exception_set); |
| 506 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 507 | |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 508 | if (dbus->fd > *maxfd) |
| 509 | *maxfd = dbus->fd; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 512 | void dbus_dispatch_io(void) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 513 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 514 | if (!dbus) |
| 515 | return; |
| 516 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 517 | dbus_watch_handle(dbus->watch, DBUS_WATCH_READABLE); |
| 518 | while (dbus_connection_get_dispatch_status(dbus->conn) |
| 519 | == DBUS_DISPATCH_DATA_REMAINS) { |
| 520 | dbus_connection_dispatch(dbus->conn); |
| 521 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 522 | } |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 523 | |
| 524 | void dbus_report_user_activity(int activity_type) |
| 525 | { |
| 526 | dbus_bool_t allow_off = false; |
| 527 | if (!dbus) |
| 528 | return; |
| 529 | |
| 530 | dbus_method_call1(kPowerManagerServiceName, |
| 531 | kPowerManagerServicePath, |
| 532 | kPowerManagerInterface, |
| 533 | kHandleUserActivityMethod, |
| 534 | DBUS_TYPE_INT32, &activity_type); |
| 535 | |
| 536 | switch (activity_type) { |
| 537 | case USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS: |
| 538 | (void)dbus_method_call0(kPowerManagerServiceName, |
| 539 | kPowerManagerServicePath, |
| 540 | kPowerManagerInterface, |
| 541 | kIncreaseScreenBrightnessMethod); |
| 542 | break; |
| 543 | case USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS: |
| 544 | /* |
| 545 | * Shouldn't allow the screen to go |
| 546 | * completely off while frecon is active |
| 547 | * so passing false to allow_off |
| 548 | */ |
| 549 | (void)dbus_method_call1(kPowerManagerServiceName, |
| 550 | kPowerManagerServicePath, |
| 551 | kPowerManagerInterface, |
| 552 | kDecreaseScreenBrightnessMethod, |
| 553 | DBUS_TYPE_BOOLEAN, &allow_off); |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 558 | /* |
| 559 | * tell Chrome to take ownership of the display (DRM master) |
| 560 | */ |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 561 | void dbus_take_display_ownership(void) |
| 562 | { |
| 563 | if (!dbus) |
| 564 | return; |
| 565 | (void)dbus_method_call0(kLibCrosServiceName, |
| 566 | kLibCrosServicePath, |
| 567 | kLibCrosServiceInterface, |
| 568 | kTakeDisplayOwnership); |
| 569 | } |
| 570 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 571 | /* |
| 572 | * ask Chrome to give up display ownership (DRM master) |
| 573 | */ |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 574 | bool dbus_release_display_ownership(void) |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 575 | { |
| 576 | if (!dbus) |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 577 | return true; |
| 578 | return dbus_method_call0(kLibCrosServiceName, |
| 579 | kLibCrosServicePath, |
| 580 | kLibCrosServiceInterface, |
| 581 | kReleaseDisplayOwnership); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 584 | void dbus_set_login_prompt_visible_callback(void (*callback)(void*), |
| 585 | void* userptr) |
| 586 | { |
| 587 | if (chrome_is_already_up) { |
| 588 | if (callback) |
| 589 | callback(userptr); |
| 590 | } else { |
| 591 | if (login_prompt_visible_callback && callback) { |
| 592 | LOG(ERROR, "trying to register login prompt visible callback multiple times"); |
| 593 | return; |
| 594 | } |
| 595 | login_prompt_visible_callback = callback; |
| 596 | login_prompt_visible_callback_userptr = userptr; |
| 597 | } |
| 598 | } |
Dominik Behr | f50c98f | 2016-03-31 14:05:29 -0700 | [diff] [blame] | 599 | |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame^] | 600 | void dbus_set_suspend_done_callback(void (*callback)(void*), |
| 601 | void* userptr) |
| 602 | { |
| 603 | if (suspend_done_callback && callback) { |
| 604 | LOG(ERROR, "trying to register login prompt visible callback multiple times"); |
| 605 | return; |
| 606 | } |
| 607 | suspend_done_callback = callback; |
| 608 | suspend_done_callback_userptr = userptr; |
| 609 | } |
| 610 | |
Dominik Behr | f50c98f | 2016-03-31 14:05:29 -0700 | [diff] [blame] | 611 | #else |
| 612 | |
| 613 | #include <stdlib.h> |
| 614 | #include <stdbool.h> |
| 615 | |
| 616 | bool dbus_init() |
| 617 | { |
| 618 | return true; |
| 619 | } |
| 620 | |
| 621 | bool dbus_init_wait() |
| 622 | { |
| 623 | return true; |
| 624 | } |
| 625 | |
| 626 | void dbus_destroy(void) |
| 627 | { |
| 628 | } |
| 629 | |
| 630 | void dbus_add_fds(fd_set* read_set, fd_set* exception_set, int *maxfd) |
| 631 | { |
| 632 | } |
| 633 | |
| 634 | void dbus_dispatch_io(void) |
| 635 | { |
| 636 | } |
| 637 | |
| 638 | void dbus_report_user_activity(int activity_type) |
| 639 | { |
| 640 | } |
| 641 | |
| 642 | void dbus_take_display_ownership(void) |
| 643 | { |
| 644 | } |
| 645 | |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 646 | bool dbus_release_display_ownership(void) |
Dominik Behr | f50c98f | 2016-03-31 14:05:29 -0700 | [diff] [blame] | 647 | { |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 648 | return true; |
Dominik Behr | f50c98f | 2016-03-31 14:05:29 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | bool dbus_is_initialized(void) |
| 652 | { |
| 653 | return true; |
| 654 | } |
| 655 | |
| 656 | void dbus_set_login_prompt_visible_callback(void (*callback)(void*), |
| 657 | void* userptr) |
| 658 | { |
| 659 | } |
| 660 | |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame^] | 661 | void dbus_set_suspend_done_callback(void (*callback)(void*), |
| 662 | void* userptr) |
| 663 | { |
| 664 | } |
| 665 | |
Dominik Behr | f50c98f | 2016-03-31 14:05:29 -0700 | [diff] [blame] | 666 | #endif |