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