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