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