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) { |
| 60 | LOG(ERROR, "SwitchVT method error, no VT argument"); |
| 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()) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 65 | LOG(ERROR, "SwtichVT: invalid terminal"); |
| 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) { |
| 129 | LOG(ERROR, "SwitchVT method error, not VT argument"); |
| 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())) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 134 | LOG(ERROR, "SwtichVT: invalid terminal"); |
| 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) { |
| 184 | LOG(WARNING, "failed to create image"); |
| 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) { |
| 213 | LOG(WARNING, "image_load_image_from_file failed: %d", status); |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 214 | goto fail; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Dominik Behr | 4defb36 | 2016-01-13 12:36:14 -0800 | [diff] [blame] | 217 | terminal = term_get_current_terminal(); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 218 | if (!terminal) |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 219 | goto fail; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 220 | |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 221 | status = term_show_image(terminal, image); |
| 222 | if (status != 0) { |
| 223 | LOG(WARNING, "term_show_image failed: %d", status); |
| 224 | goto fail; |
| 225 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 226 | image_release(image); |
| 227 | |
| 228 | reply = dbus_message_new_method_return(message); |
| 229 | dbus_connection_send(connection, reply, NULL); |
| 230 | |
| 231 | return DBUS_HANDLER_RESULT_HANDLED; |
David Sodman | f0a925a | 2015-05-04 11:19:19 -0700 | [diff] [blame] | 232 | fail: |
| 233 | if (image) |
| 234 | image_release(image); |
| 235 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 238 | static void frecon_dbus_unregister(DBusConnection* connection, void* user_data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 239 | { |
| 240 | } |
| 241 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 242 | static DBusHandlerResult frecon_dbus_message_handler(DBusConnection* connection, |
| 243 | DBusMessage* message, |
| 244 | void* user_data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 245 | { |
| 246 | if (dbus_message_is_method_call(message, |
| 247 | kFreconDbusInterface, COMMAND_SWITCH_VT)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 248 | return handle_switchvt(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 249 | } else if (dbus_message_is_method_call(message, |
| 250 | kFreconDbusInterface, COMMAND_MAKE_VT)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 251 | return handle_makevt(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 252 | } |
| 253 | else if (dbus_message_is_method_call(message, |
| 254 | kFreconDbusInterface, COMMAND_TERMINATE)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 255 | return handle_terminate(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 256 | } else if (dbus_message_is_method_call(message, |
| 257 | kFreconDbusInterface, COMMAND_IMAGE)) { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 258 | return handle_image(connection, message); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static DBusObjectPathVTable |
| 265 | frecon_vtable = { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 266 | frecon_dbus_unregister, |
| 267 | frecon_dbus_message_handler, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 268 | NULL |
| 269 | }; |
| 270 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 271 | static dbus_bool_t add_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 272 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 273 | dbus_t* dbus = (dbus_t*)data; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 274 | dbus->watch = w; |
| 275 | |
| 276 | return TRUE; |
| 277 | } |
| 278 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 279 | static void remove_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 280 | { |
| 281 | } |
| 282 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 283 | static void toggle_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 284 | { |
| 285 | } |
| 286 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 287 | static DBusHandlerResult handle_login_prompt_visible(DBusMessage* message) |
| 288 | { |
| 289 | if (login_prompt_visible_callback) { |
| 290 | login_prompt_visible_callback(login_prompt_visible_callback_userptr); |
| 291 | login_prompt_visible_callback = NULL; |
| 292 | login_prompt_visible_callback_userptr = NULL; |
| 293 | } |
| 294 | chrome_is_already_up = true; |
| 295 | |
| 296 | return DBUS_HANDLER_RESULT_HANDLED; |
| 297 | } |
| 298 | |
| 299 | static DBusHandlerResult frecon_dbus_message_filter(DBusConnection* connection, |
| 300 | DBusMessage* message, |
| 301 | void* user_data) |
| 302 | { |
| 303 | if (dbus_message_is_signal(message, |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 304 | kSessionManagerInterface, kLoginPromptVisibleSignal)) |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 305 | return handle_login_prompt_visible(message); |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 306 | |
| 307 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 308 | } |
| 309 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 310 | bool dbus_is_initialized(void) |
| 311 | { |
| 312 | return !!dbus; |
| 313 | } |
| 314 | |
| 315 | bool dbus_init() |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 316 | { |
| 317 | dbus_t* new_dbus; |
| 318 | DBusError err; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 319 | int result; |
| 320 | dbus_bool_t stat; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 321 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 322 | if (dbus_first_init) { |
| 323 | dbus_first_init = false; |
| 324 | dbus_first_init_time = get_monotonic_time_ms(); |
| 325 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 326 | dbus_error_init(&err); |
| 327 | |
| 328 | new_dbus = (dbus_t*)calloc(1, sizeof(*new_dbus)); |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 329 | |
| 330 | if (!new_dbus) |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 331 | return false; |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 332 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 333 | new_dbus->fd = -1; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 334 | |
| 335 | new_dbus->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); |
| 336 | if (dbus_error_is_set(&err)) { |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 337 | if (!dbus_connect_fail) { |
| 338 | LOG(ERROR, "Cannot get DBUS connection"); |
| 339 | dbus_connect_fail = true; |
| 340 | dbus_connect_fail_time = get_monotonic_time_ms(); |
| 341 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 342 | free(new_dbus); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 343 | return false; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 346 | if (dbus_connect_fail) { |
| 347 | int64_t t = get_monotonic_time_ms() - dbus_connect_fail_time; |
| 348 | LOG(INFO, "DBUS connected after %.1f seconds", (float)t / 1000.0f); |
| 349 | } |
| 350 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 351 | result = dbus_bus_request_name(new_dbus->conn, kFreconDbusInterface, |
| 352 | DBUS_NAME_FLAG_DO_NOT_QUEUE, &err); |
| 353 | |
| 354 | if (result <= 0) { |
| 355 | LOG(ERROR, "Unable to get name for server"); |
| 356 | } |
| 357 | |
| 358 | stat = dbus_connection_register_object_path(new_dbus->conn, |
| 359 | kFreconDbusPath, |
| 360 | &frecon_vtable, |
| 361 | NULL); |
| 362 | |
| 363 | if (!stat) { |
| 364 | LOG(ERROR, "failed to register object path"); |
| 365 | } |
| 366 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 367 | dbus_bus_add_match(new_dbus->conn, kLoginPromptVisibleRule, &err); |
| 368 | |
| 369 | stat = dbus_connection_add_filter(new_dbus->conn, frecon_dbus_message_filter, NULL, NULL); |
| 370 | if (!stat) { |
| 371 | LOG(ERROR, "failed to add message filter"); |
| 372 | } |
| 373 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 374 | stat = dbus_connection_set_watch_functions(new_dbus->conn, |
| 375 | add_watch, remove_watch, toggle_watch, |
| 376 | new_dbus, NULL); |
| 377 | |
| 378 | if (!stat) { |
| 379 | LOG(ERROR, "Failed to set watch functions"); |
| 380 | } |
| 381 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 382 | dbus_connection_set_exit_on_disconnect(new_dbus->conn, FALSE); |
| 383 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 384 | dbus = new_dbus; |
| 385 | return true; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 388 | bool dbus_init_wait() |
| 389 | { |
| 390 | while (!dbus_is_initialized()) { |
| 391 | if (!dbus_init()) { |
| 392 | int64_t t = get_monotonic_time_ms() - dbus_first_init_time; |
| 393 | if (t >= DBUS_INIT_TIMEOUT_MS) { |
| 394 | LOG(ERROR, "DBUS init failed after a timeout of %u sec", DBUS_INIT_TIMEOUT_MS/1000); |
| 395 | return false; |
| 396 | } |
| 397 | } |
| 398 | usleep(DBUS_WAIT_DELAY_US); |
| 399 | } |
| 400 | return true; |
| 401 | } |
| 402 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 403 | static bool dbus_method_call0(const char* service_name, |
| 404 | const char* service_path, |
| 405 | const char* service_interface, |
| 406 | const char* method) |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 407 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 408 | DBusMessage* msg = NULL; |
| 409 | if (!dbus) { |
| 410 | LOG(ERROR, "dbus not initialized"); |
| 411 | return false; |
| 412 | } |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 413 | |
| 414 | msg = dbus_message_new_method_call(service_name, |
| 415 | service_path, service_interface, method); |
| 416 | |
| 417 | if (!msg) |
| 418 | return false; |
| 419 | |
| 420 | if (!dbus_connection_send_with_reply_and_block(dbus->conn, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 421 | msg, DBUS_DEFAULT_DELAY, NULL)) { |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 422 | dbus_message_unref(msg); |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | dbus_connection_flush(dbus->conn); |
| 427 | dbus_message_unref(msg); |
| 428 | |
| 429 | return true; |
| 430 | } |
| 431 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 432 | static bool dbus_method_call1(const char* service_name, |
| 433 | const char* service_path, |
| 434 | const char* service_interface, |
| 435 | const char* method, int arg_type, void* param) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 436 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 437 | DBusMessage* msg = NULL; |
| 438 | if (!dbus) { |
| 439 | LOG(ERROR, "dbus not initialized"); |
| 440 | return false; |
| 441 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 442 | |
| 443 | msg = dbus_message_new_method_call(service_name, |
| 444 | service_path, service_interface, method); |
| 445 | |
| 446 | if (!msg) |
| 447 | return false; |
| 448 | |
| 449 | if (!dbus_message_append_args(msg, |
David Sodman | 19e4f9d | 2015-03-10 11:11:09 -0700 | [diff] [blame] | 450 | arg_type, param, DBUS_TYPE_INVALID)) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 451 | dbus_message_unref(msg); |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | if (!dbus_connection_send_with_reply_and_block(dbus->conn, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 456 | msg, DBUS_DEFAULT_DELAY, NULL)) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 457 | dbus_message_unref(msg); |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | dbus_connection_flush(dbus->conn); |
| 462 | dbus_message_unref(msg); |
| 463 | |
| 464 | return true; |
| 465 | } |
| 466 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 467 | void dbus_destroy(void) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 468 | { |
| 469 | /* FIXME - not sure what the right counterpart to |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 470 | * dbus_bus_get() is, unref documentation is rather |
| 471 | * unclear. Not a big issue but it would be nice to |
| 472 | * clean up properly here |
| 473 | */ |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 474 | /* dbus_connection_unref(dbus->conn); */ |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 475 | if (dbus) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 476 | free(dbus); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 477 | dbus = NULL; |
| 478 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 481 | 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] | 482 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 483 | if (!dbus) |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 484 | return; |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 485 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 486 | if (dbus->fd < 0) |
| 487 | dbus->fd = dbus_watch_get_unix_fd(dbus->watch); |
| 488 | |
| 489 | if (dbus->fd >= 0) { |
| 490 | FD_SET(dbus->fd, read_set); |
| 491 | FD_SET(dbus->fd, exception_set); |
| 492 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 493 | |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 494 | if (dbus->fd > *maxfd) |
| 495 | *maxfd = dbus->fd; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 498 | void dbus_dispatch_io(void) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 499 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 500 | if (!dbus) |
| 501 | return; |
| 502 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 503 | dbus_watch_handle(dbus->watch, DBUS_WATCH_READABLE); |
| 504 | while (dbus_connection_get_dispatch_status(dbus->conn) |
| 505 | == DBUS_DISPATCH_DATA_REMAINS) { |
| 506 | dbus_connection_dispatch(dbus->conn); |
| 507 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 508 | } |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 509 | |
| 510 | void dbus_report_user_activity(int activity_type) |
| 511 | { |
| 512 | dbus_bool_t allow_off = false; |
| 513 | if (!dbus) |
| 514 | return; |
| 515 | |
| 516 | dbus_method_call1(kPowerManagerServiceName, |
| 517 | kPowerManagerServicePath, |
| 518 | kPowerManagerInterface, |
| 519 | kHandleUserActivityMethod, |
| 520 | DBUS_TYPE_INT32, &activity_type); |
| 521 | |
| 522 | switch (activity_type) { |
| 523 | case USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS: |
| 524 | (void)dbus_method_call0(kPowerManagerServiceName, |
| 525 | kPowerManagerServicePath, |
| 526 | kPowerManagerInterface, |
| 527 | kIncreaseScreenBrightnessMethod); |
| 528 | break; |
| 529 | case USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS: |
| 530 | /* |
| 531 | * Shouldn't allow the screen to go |
| 532 | * completely off while frecon is active |
| 533 | * so passing false to allow_off |
| 534 | */ |
| 535 | (void)dbus_method_call1(kPowerManagerServiceName, |
| 536 | kPowerManagerServicePath, |
| 537 | kPowerManagerInterface, |
| 538 | kDecreaseScreenBrightnessMethod, |
| 539 | DBUS_TYPE_BOOLEAN, &allow_off); |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 544 | /* |
| 545 | * tell Chrome to take ownership of the display (DRM master) |
| 546 | */ |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 547 | void dbus_take_display_ownership(void) |
| 548 | { |
| 549 | if (!dbus) |
| 550 | return; |
| 551 | (void)dbus_method_call0(kLibCrosServiceName, |
| 552 | kLibCrosServicePath, |
| 553 | kLibCrosServiceInterface, |
| 554 | kTakeDisplayOwnership); |
| 555 | } |
| 556 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 557 | /* |
| 558 | * ask Chrome to give up display ownership (DRM master) |
| 559 | */ |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 560 | void dbus_release_display_ownership(void) |
| 561 | { |
| 562 | if (!dbus) |
| 563 | return; |
| 564 | (void)dbus_method_call0(kLibCrosServiceName, |
| 565 | kLibCrosServicePath, |
| 566 | kLibCrosServiceInterface, |
| 567 | kReleaseDisplayOwnership); |
| 568 | } |
| 569 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 570 | void dbus_set_login_prompt_visible_callback(void (*callback)(void*), |
| 571 | void* userptr) |
| 572 | { |
| 573 | if (chrome_is_already_up) { |
| 574 | if (callback) |
| 575 | callback(userptr); |
| 576 | } else { |
| 577 | if (login_prompt_visible_callback && callback) { |
| 578 | LOG(ERROR, "trying to register login prompt visible callback multiple times"); |
| 579 | return; |
| 580 | } |
| 581 | login_prompt_visible_callback = callback; |
| 582 | login_prompt_visible_callback_userptr = userptr; |
| 583 | } |
| 584 | } |
Dominik Behr | f50c98f | 2016-03-31 14:05:29 -0700 | [diff] [blame] | 585 | |
| 586 | #else |
| 587 | |
| 588 | #include <stdlib.h> |
| 589 | #include <stdbool.h> |
| 590 | |
| 591 | bool dbus_init() |
| 592 | { |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | bool dbus_init_wait() |
| 597 | { |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | void dbus_destroy(void) |
| 602 | { |
| 603 | } |
| 604 | |
| 605 | void dbus_add_fds(fd_set* read_set, fd_set* exception_set, int *maxfd) |
| 606 | { |
| 607 | } |
| 608 | |
| 609 | void dbus_dispatch_io(void) |
| 610 | { |
| 611 | } |
| 612 | |
| 613 | void dbus_report_user_activity(int activity_type) |
| 614 | { |
| 615 | } |
| 616 | |
| 617 | void dbus_take_display_ownership(void) |
| 618 | { |
| 619 | } |
| 620 | |
| 621 | void dbus_release_display_ownership(void) |
| 622 | { |
| 623 | } |
| 624 | |
| 625 | bool dbus_is_initialized(void) |
| 626 | { |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | void dbus_set_login_prompt_visible_callback(void (*callback)(void*), |
| 631 | void* userptr) |
| 632 | { |
| 633 | } |
| 634 | |
| 635 | #endif |