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 | #include <dbus/dbus.h> |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 8 | #include <stdlib.h> |
Mike Frysinger | 006bdb2 | 2016-09-14 13:43:48 -0400 | [diff] [blame] | 9 | #include <unistd.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 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 18 | #define DBUS_WAIT_DELAY_US (50000) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 19 | #define DBUS_DEFAULT_DELAY 3000 |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 20 | #define DBUS_INIT_TIMEOUT_MS (60*1000) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 21 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 22 | typedef struct _dbus_t dbus_t; |
| 23 | |
Dominik Behr | 6e0f6fd | 2016-12-02 17:54:08 -0800 | [diff] [blame] | 24 | static void (*login_prompt_visible_callback)(void) = NULL; |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame] | 25 | static void (*suspend_done_callback)(void*) = NULL; |
| 26 | static void* suspend_done_callback_userptr = NULL; |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 27 | static bool chrome_is_already_up = false; |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 28 | static bool dbus_connect_fail = false; |
| 29 | static int64_t dbus_connect_fail_time; |
| 30 | static bool dbus_first_init = true; |
| 31 | static int64_t dbus_first_init_time; |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 32 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 33 | struct _dbus_t { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 34 | DBusConnection* conn; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 35 | DBusWatch* watch; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 36 | int fd; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 39 | static dbus_t *dbus = NULL; |
| 40 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 41 | static void frecon_dbus_unregister(DBusConnection* connection, void* user_data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
Stéphane Marchesin | 8fc1352 | 2015-12-14 17:02:28 -0800 | [diff] [blame] | 45 | static DBusHandlerResult frecon_dbus_message_handler(DBusConnection* connection, |
| 46 | DBusMessage* message, |
| 47 | void* user_data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 48 | { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 49 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static DBusObjectPathVTable |
| 53 | frecon_vtable = { |
Stéphane Marchesin | af9e9ff | 2015-12-11 17:49:12 -0800 | [diff] [blame] | 54 | frecon_dbus_unregister, |
| 55 | frecon_dbus_message_handler, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 56 | NULL |
| 57 | }; |
| 58 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 59 | static dbus_bool_t add_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 60 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 61 | dbus_t* dbus = (dbus_t*)data; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 62 | dbus->watch = w; |
| 63 | |
| 64 | return TRUE; |
| 65 | } |
| 66 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 67 | static void remove_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 68 | { |
| 69 | } |
| 70 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 71 | static void toggle_watch(DBusWatch* w, void* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 72 | { |
| 73 | } |
| 74 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 75 | static DBusHandlerResult handle_login_prompt_visible(DBusMessage* message) |
| 76 | { |
| 77 | if (login_prompt_visible_callback) { |
Dominik Behr | 6e0f6fd | 2016-12-02 17:54:08 -0800 | [diff] [blame] | 78 | login_prompt_visible_callback(); |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 79 | login_prompt_visible_callback = NULL; |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 80 | } |
| 81 | chrome_is_already_up = true; |
| 82 | |
| 83 | return DBUS_HANDLER_RESULT_HANDLED; |
| 84 | } |
| 85 | |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame] | 86 | static DBusHandlerResult handle_suspend_done(DBusMessage* message) |
| 87 | { |
| 88 | if (suspend_done_callback) |
| 89 | suspend_done_callback(suspend_done_callback_userptr); |
| 90 | |
| 91 | return DBUS_HANDLER_RESULT_HANDLED; |
| 92 | } |
| 93 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 94 | static DBusHandlerResult frecon_dbus_message_filter(DBusConnection* connection, |
| 95 | DBusMessage* message, |
| 96 | void* user_data) |
| 97 | { |
| 98 | if (dbus_message_is_signal(message, |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 99 | kSessionManagerInterface, kLoginPromptVisibleSignal)) |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 100 | return handle_login_prompt_visible(message); |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame] | 101 | else if (dbus_message_is_signal(message, |
| 102 | kPowerManagerInterface, kSuspendDoneSignal)) |
| 103 | return handle_suspend_done(message); |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 104 | |
| 105 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 106 | } |
| 107 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 108 | bool dbus_is_initialized(void) |
| 109 | { |
| 110 | return !!dbus; |
| 111 | } |
| 112 | |
| 113 | bool dbus_init() |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 114 | { |
| 115 | dbus_t* new_dbus; |
| 116 | DBusError err; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 117 | int result; |
| 118 | dbus_bool_t stat; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 119 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 120 | if (dbus_first_init) { |
| 121 | dbus_first_init = false; |
| 122 | dbus_first_init_time = get_monotonic_time_ms(); |
| 123 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 124 | dbus_error_init(&err); |
| 125 | |
| 126 | new_dbus = (dbus_t*)calloc(1, sizeof(*new_dbus)); |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 127 | |
| 128 | if (!new_dbus) |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 129 | return false; |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 130 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 131 | new_dbus->fd = -1; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 132 | |
| 133 | new_dbus->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); |
| 134 | if (dbus_error_is_set(&err)) { |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 135 | if (!dbus_connect_fail) { |
Nicolas Boichat | 4f044ad | 2021-03-22 15:18:44 +0800 | [diff] [blame] | 136 | LOG(DEBUG, "Cannot get DBUS connection"); |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 137 | dbus_connect_fail = true; |
| 138 | dbus_connect_fail_time = get_monotonic_time_ms(); |
| 139 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 140 | free(new_dbus); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 141 | return false; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 144 | if (dbus_connect_fail) { |
| 145 | int64_t t = get_monotonic_time_ms() - dbus_connect_fail_time; |
Nicolas Boichat | 4f044ad | 2021-03-22 15:18:44 +0800 | [diff] [blame] | 146 | LOG(DEBUG, "DBUS connected after %.1f seconds", (float)t / 1000.0f); |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 147 | } |
| 148 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 149 | result = dbus_bus_request_name(new_dbus->conn, kFreconDbusInterface, |
| 150 | DBUS_NAME_FLAG_DO_NOT_QUEUE, &err); |
| 151 | |
| 152 | if (result <= 0) { |
| 153 | LOG(ERROR, "Unable to get name for server"); |
| 154 | } |
| 155 | |
| 156 | stat = dbus_connection_register_object_path(new_dbus->conn, |
| 157 | kFreconDbusPath, |
| 158 | &frecon_vtable, |
| 159 | NULL); |
| 160 | |
| 161 | if (!stat) { |
| 162 | LOG(ERROR, "failed to register object path"); |
| 163 | } |
| 164 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 165 | dbus_bus_add_match(new_dbus->conn, kLoginPromptVisibleRule, &err); |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame] | 166 | dbus_bus_add_match(new_dbus->conn, kSuspendDoneRule, &err); |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 167 | |
| 168 | stat = dbus_connection_add_filter(new_dbus->conn, frecon_dbus_message_filter, NULL, NULL); |
| 169 | if (!stat) { |
| 170 | LOG(ERROR, "failed to add message filter"); |
| 171 | } |
| 172 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 173 | stat = dbus_connection_set_watch_functions(new_dbus->conn, |
| 174 | add_watch, remove_watch, toggle_watch, |
| 175 | new_dbus, NULL); |
| 176 | |
| 177 | if (!stat) { |
| 178 | LOG(ERROR, "Failed to set watch functions"); |
| 179 | } |
| 180 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 181 | dbus_connection_set_exit_on_disconnect(new_dbus->conn, FALSE); |
| 182 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 183 | dbus = new_dbus; |
| 184 | return true; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Dominik Behr | 5f6742f | 2016-03-10 18:03:54 -0800 | [diff] [blame] | 187 | bool dbus_init_wait() |
| 188 | { |
| 189 | while (!dbus_is_initialized()) { |
| 190 | if (!dbus_init()) { |
| 191 | int64_t t = get_monotonic_time_ms() - dbus_first_init_time; |
| 192 | if (t >= DBUS_INIT_TIMEOUT_MS) { |
| 193 | LOG(ERROR, "DBUS init failed after a timeout of %u sec", DBUS_INIT_TIMEOUT_MS/1000); |
| 194 | return false; |
| 195 | } |
| 196 | } |
| 197 | usleep(DBUS_WAIT_DELAY_US); |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 202 | static bool dbus_method_call0(const char* service_name, |
| 203 | const char* service_path, |
| 204 | const char* service_interface, |
| 205 | const char* method) |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 206 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 207 | DBusMessage* msg = NULL; |
| 208 | if (!dbus) { |
| 209 | LOG(ERROR, "dbus not initialized"); |
| 210 | return false; |
| 211 | } |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 212 | |
| 213 | msg = dbus_message_new_method_call(service_name, |
| 214 | service_path, service_interface, method); |
| 215 | |
| 216 | if (!msg) |
| 217 | return false; |
| 218 | |
| 219 | if (!dbus_connection_send_with_reply_and_block(dbus->conn, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 220 | msg, DBUS_DEFAULT_DELAY, NULL)) { |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 221 | dbus_message_unref(msg); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | dbus_connection_flush(dbus->conn); |
| 226 | dbus_message_unref(msg); |
| 227 | |
| 228 | return true; |
| 229 | } |
| 230 | |
Dominik Behr | a818a1e | 2016-10-26 19:46:22 -0700 | [diff] [blame] | 231 | static bool dbus_method_call0_bool(const char* service_name, |
| 232 | const char* service_path, |
| 233 | const char* service_interface, |
| 234 | const char* method) |
| 235 | { |
| 236 | DBusMessage* msg = NULL; |
| 237 | DBusMessage* reply = NULL; |
| 238 | int res = false; |
| 239 | |
| 240 | if (!dbus) { |
| 241 | LOG(ERROR, "dbus not initialized"); |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | msg = dbus_message_new_method_call(service_name, |
| 246 | service_path, service_interface, method); |
| 247 | |
| 248 | if (!msg) |
| 249 | return false; |
| 250 | |
| 251 | reply = dbus_connection_send_with_reply_and_block(dbus->conn, |
| 252 | msg, DBUS_DEFAULT_DELAY, NULL); |
| 253 | if (!reply) { |
| 254 | dbus_message_unref(msg); |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | dbus_message_get_args(reply, NULL, DBUS_TYPE_BOOLEAN, &res, DBUS_TYPE_INVALID); |
| 259 | |
| 260 | dbus_connection_flush(dbus->conn); |
| 261 | dbus_message_unref(msg); |
| 262 | dbus_message_unref(reply); |
| 263 | |
| 264 | return (bool)res; |
| 265 | } |
| 266 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 267 | static bool dbus_method_call1(const char* service_name, |
| 268 | const char* service_path, |
| 269 | const char* service_interface, |
| 270 | const char* method, int arg_type, void* param) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 271 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 272 | DBusMessage* msg = NULL; |
| 273 | if (!dbus) { |
| 274 | LOG(ERROR, "dbus not initialized"); |
| 275 | return false; |
| 276 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 277 | |
| 278 | msg = dbus_message_new_method_call(service_name, |
| 279 | service_path, service_interface, method); |
| 280 | |
| 281 | if (!msg) |
| 282 | return false; |
| 283 | |
| 284 | if (!dbus_message_append_args(msg, |
David Sodman | 19e4f9d | 2015-03-10 11:11:09 -0700 | [diff] [blame] | 285 | arg_type, param, DBUS_TYPE_INVALID)) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 286 | dbus_message_unref(msg); |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | if (!dbus_connection_send_with_reply_and_block(dbus->conn, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 291 | msg, DBUS_DEFAULT_DELAY, NULL)) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 292 | dbus_message_unref(msg); |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | dbus_connection_flush(dbus->conn); |
| 297 | dbus_message_unref(msg); |
| 298 | |
| 299 | return true; |
| 300 | } |
| 301 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 302 | void dbus_destroy(void) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 303 | { |
| 304 | /* FIXME - not sure what the right counterpart to |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 305 | * dbus_bus_get() is, unref documentation is rather |
| 306 | * unclear. Not a big issue but it would be nice to |
| 307 | * clean up properly here |
| 308 | */ |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 309 | /* dbus_connection_unref(dbus->conn); */ |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 310 | if (dbus) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 311 | free(dbus); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 312 | dbus = NULL; |
| 313 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 316 | 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] | 317 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 318 | if (!dbus) |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 319 | return; |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 320 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 321 | if (dbus->fd < 0) |
| 322 | dbus->fd = dbus_watch_get_unix_fd(dbus->watch); |
| 323 | |
| 324 | if (dbus->fd >= 0) { |
| 325 | FD_SET(dbus->fd, read_set); |
| 326 | FD_SET(dbus->fd, exception_set); |
| 327 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 328 | |
Dominik Behr | d711267 | 2016-01-20 16:59:34 -0800 | [diff] [blame] | 329 | if (dbus->fd > *maxfd) |
| 330 | *maxfd = dbus->fd; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 333 | void dbus_dispatch_io(void) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 334 | { |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 335 | if (!dbus) |
| 336 | return; |
| 337 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 338 | dbus_watch_handle(dbus->watch, DBUS_WATCH_READABLE); |
| 339 | while (dbus_connection_get_dispatch_status(dbus->conn) |
| 340 | == DBUS_DISPATCH_DATA_REMAINS) { |
| 341 | dbus_connection_dispatch(dbus->conn); |
| 342 | } |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 343 | } |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 344 | |
| 345 | void dbus_report_user_activity(int activity_type) |
| 346 | { |
| 347 | dbus_bool_t allow_off = false; |
| 348 | if (!dbus) |
| 349 | return; |
| 350 | |
| 351 | dbus_method_call1(kPowerManagerServiceName, |
| 352 | kPowerManagerServicePath, |
| 353 | kPowerManagerInterface, |
| 354 | kHandleUserActivityMethod, |
| 355 | DBUS_TYPE_INT32, &activity_type); |
| 356 | |
| 357 | switch (activity_type) { |
| 358 | case USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS: |
| 359 | (void)dbus_method_call0(kPowerManagerServiceName, |
| 360 | kPowerManagerServicePath, |
| 361 | kPowerManagerInterface, |
| 362 | kIncreaseScreenBrightnessMethod); |
| 363 | break; |
| 364 | case USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS: |
| 365 | /* |
| 366 | * Shouldn't allow the screen to go |
| 367 | * completely off while frecon is active |
| 368 | * so passing false to allow_off |
| 369 | */ |
| 370 | (void)dbus_method_call1(kPowerManagerServiceName, |
| 371 | kPowerManagerServicePath, |
| 372 | kPowerManagerInterface, |
| 373 | kDecreaseScreenBrightnessMethod, |
| 374 | DBUS_TYPE_BOOLEAN, &allow_off); |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 379 | /* |
| 380 | * tell Chrome to take ownership of the display (DRM master) |
| 381 | */ |
Dominik Behr | a818a1e | 2016-10-26 19:46:22 -0700 | [diff] [blame] | 382 | bool dbus_take_display_ownership(void) |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 383 | { |
| 384 | if (!dbus) |
Dominik Behr | a818a1e | 2016-10-26 19:46:22 -0700 | [diff] [blame] | 385 | return true; |
Lann Martin | 059fe66 | 2017-07-24 15:22:39 -0600 | [diff] [blame] | 386 | return dbus_method_call0_bool(kDisplayServiceName, |
| 387 | kDisplayServicePath, |
| 388 | kDisplayServiceInterface, |
| 389 | kTakeOwnership); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 392 | /* |
| 393 | * ask Chrome to give up display ownership (DRM master) |
| 394 | */ |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 395 | bool dbus_release_display_ownership(void) |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 396 | { |
| 397 | if (!dbus) |
Dominik Behr | 83864df | 2016-04-21 12:35:08 -0700 | [diff] [blame] | 398 | return true; |
Lann Martin | 059fe66 | 2017-07-24 15:22:39 -0600 | [diff] [blame] | 399 | return dbus_method_call0_bool(kDisplayServiceName, |
| 400 | kDisplayServicePath, |
| 401 | kDisplayServiceInterface, |
| 402 | kReleaseOwnership); |
Dominik Behr | 797a383 | 2016-01-11 15:53:11 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Dominik Behr | 6e0f6fd | 2016-12-02 17:54:08 -0800 | [diff] [blame] | 405 | void dbus_set_login_prompt_visible_callback(void (*callback)(void)) |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 406 | { |
| 407 | if (chrome_is_already_up) { |
| 408 | if (callback) |
Dominik Behr | 6e0f6fd | 2016-12-02 17:54:08 -0800 | [diff] [blame] | 409 | callback(); |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 410 | } else { |
| 411 | if (login_prompt_visible_callback && callback) { |
| 412 | LOG(ERROR, "trying to register login prompt visible callback multiple times"); |
| 413 | return; |
| 414 | } |
| 415 | login_prompt_visible_callback = callback; |
Dominik Behr | 46c567f | 2016-03-08 15:11:48 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
Dominik Behr | f50c98f | 2016-03-31 14:05:29 -0700 | [diff] [blame] | 418 | |
Dominik Behr | 1883c04 | 2016-04-27 12:31:02 -0700 | [diff] [blame] | 419 | void dbus_set_suspend_done_callback(void (*callback)(void*), |
| 420 | void* userptr) |
| 421 | { |
| 422 | if (suspend_done_callback && callback) { |
| 423 | LOG(ERROR, "trying to register login prompt visible callback multiple times"); |
| 424 | return; |
| 425 | } |
| 426 | suspend_done_callback = callback; |
| 427 | suspend_done_callback_userptr = userptr; |
| 428 | } |