blob: 6cf12ca90ef1baf60057ae9ec610b440e2831c47 [file] [log] [blame]
David Sodmanbbcb0522014-09-19 10:34:07 -07001/*
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 Behrf50c98f2016-03-31 14:05:29 -07007#if DBUS
8#include <dbus/dbus.h>
David Sodmanbbcb0522014-09-19 10:34:07 -07009#include <stdlib.h>
David Sodman8ef20062015-01-06 09:23:40 -080010
Stéphane Marchesin62561a12015-12-11 17:32:37 -080011#include "dbus.h"
David Sodman8ef20062015-01-06 09:23:40 -080012#include "dbus_interface.h"
13#include "image.h"
Dominik Behr46c567f2016-03-08 15:11:48 -080014#include "main.h"
David Sodman8ef20062015-01-06 09:23:40 -080015#include "term.h"
David Sodmanbbcb0522014-09-19 10:34:07 -070016#include "util.h"
17
David Sodman8ef20062015-01-06 09:23:40 -080018#define COMMAND_MAKE_VT "MakeVT"
19#define COMMAND_SWITCH_VT "SwitchVT"
20#define COMMAND_TERMINATE "Terminate"
21#define COMMAND_IMAGE "Image"
22
Dominik Behr5f6742f2016-03-10 18:03:54 -080023#define DBUS_WAIT_DELAY_US (50000)
David Sodman8ef20062015-01-06 09:23:40 -080024#define DBUS_DEFAULT_DELAY 3000
Dominik Behr5f6742f2016-03-10 18:03:54 -080025#define DBUS_INIT_TIMEOUT_MS (60*1000)
David Sodman8ef20062015-01-06 09:23:40 -080026
Dominik Behr797a3832016-01-11 15:53:11 -080027typedef struct _dbus_t dbus_t;
28
Dominik Behr46c567f2016-03-08 15:11:48 -080029static void (*login_prompt_visible_callback)(void*) = NULL;
30static void* login_prompt_visible_callback_userptr = NULL;
31static bool chrome_is_already_up = false;
Dominik Behr5f6742f2016-03-10 18:03:54 -080032static bool dbus_connect_fail = false;
33static int64_t dbus_connect_fail_time;
34static bool dbus_first_init = true;
35static int64_t dbus_first_init_time;
Dominik Behr46c567f2016-03-08 15:11:48 -080036
David Sodmanbbcb0522014-09-19 10:34:07 -070037struct _dbus_t {
Stéphane Marchesin00ff1872015-12-14 13:40:09 -080038 DBusConnection* conn;
Stéphane Marchesin00ff1872015-12-14 13:40:09 -080039 DBusWatch* watch;
David Sodman8ef20062015-01-06 09:23:40 -080040 int fd;
David Sodmanbbcb0522014-09-19 10:34:07 -070041};
42
Dominik Behr797a3832016-01-11 15:53:11 -080043static dbus_t *dbus = NULL;
44
Stéphane Marchesin8fc13522015-12-14 17:02:28 -080045static DBusHandlerResult handle_switchvt(DBusConnection* connection,
46 DBusMessage* message)
David Sodman8ef20062015-01-06 09:23:40 -080047{
Stéphane Marchesin00ff1872015-12-14 13:40:09 -080048 DBusMessage* reply;
49 DBusMessage* msg;
David Sodman8ef20062015-01-06 09:23:40 -080050 DBusError error;
51 dbus_bool_t stat;
Stéphane Marchesin00ff1872015-12-14 13:40:09 -080052 terminal_t* terminal;
David Sodman8ef20062015-01-06 09:23:40 -080053 unsigned int vt;
54
55 dbus_error_init(&error);
56 stat = dbus_message_get_args(message, &error, DBUS_TYPE_UINT32,
57 &vt, DBUS_TYPE_INVALID);
58
59 if (!stat) {
Dominik Behr46755a42016-04-21 18:08:33 -070060 LOG(ERROR, "SwitchVT method error, no VT argument.");
David Sodman8ef20062015-01-06 09:23:40 -080061 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
62 }
63
Stéphane Marchesin92a297d2016-01-07 20:24:55 -080064 if (vt > term_get_max_terminals()) {
Dominik Behr46755a42016-04-21 18:08:33 -070065 LOG(ERROR, "SwtichVT: invalid terminal.");
David Sodman8ef20062015-01-06 09:23:40 -080066 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
67 }
68
69 if (vt == 0) {
Stéphane Marchesin0a7ce422016-01-07 20:45:47 -080070 terminal = term_create_term(vt);
David Sodman8ef20062015-01-06 09:23:40 -080071 if (term_is_active(terminal)) {
David Sodmanf0a925a2015-05-04 11:19:19 -070072 term_deactivate(terminal);
David Sodman8ef20062015-01-06 09:23:40 -080073 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 Behr4defb362016-01-13 12:36:14 -080090 terminal = term_get_current_terminal();
David Sodman8ef20062015-01-06 09:23:40 -080091 if (term_is_active(terminal))
David Sodmanf0a925a2015-05-04 11:19:19 -070092 term_deactivate(terminal);
David Sodman8ef20062015-01-06 09:23:40 -080093
Stéphane Marchesin0a7ce422016-01-07 20:45:47 -080094 terminal = term_create_term(vt);
David Sodman8ef20062015-01-06 09:23:40 -080095 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 Marchesin8fc13522015-12-14 17:02:28 -0800114static DBusHandlerResult handle_makevt(DBusConnection* connection,
115 DBusMessage* message)
David Sodman8ef20062015-01-06 09:23:40 -0800116{
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800117 DBusMessage* reply;
David Sodman8ef20062015-01-06 09:23:40 -0800118 DBusError error;
119 dbus_bool_t stat;
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800120 terminal_t* terminal;
David Sodman8ef20062015-01-06 09:23:40 -0800121 unsigned int vt;
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800122 const char* reply_str;
David Sodman8ef20062015-01-06 09:23:40 -0800123
124 dbus_error_init(&error);
125 stat = dbus_message_get_args(message, &error, DBUS_TYPE_UINT32,
126 &vt, DBUS_TYPE_INVALID);
127
128 if (!stat) {
Dominik Behr46755a42016-04-21 18:08:33 -0700129 LOG(ERROR, "SwitchVT method error, not VT argument.");
David Sodman8ef20062015-01-06 09:23:40 -0800130 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
131 }
132
Stéphane Marchesin92a297d2016-01-07 20:24:55 -0800133 if ((vt < 1) || (vt > term_get_max_terminals())) {
Dominik Behr46755a42016-04-21 18:08:33 -0700134 LOG(ERROR, "SwtichVT: invalid terminal.");
David Sodman8ef20062015-01-06 09:23:40 -0800135 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
136 }
137
Stéphane Marchesin0a7ce422016-01-07 20:45:47 -0800138 terminal = term_create_term(vt);
David Sodman8ef20062015-01-06 09:23:40 -0800139 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 Marchesin8fc13522015-12-14 17:02:28 -0800150static DBusHandlerResult handle_terminate(DBusConnection* connection,
151 DBusMessage* message)
David Sodman8ef20062015-01-06 09:23:40 -0800152{
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800153 DBusMessage* reply;
David Sodman8ef20062015-01-06 09:23:40 -0800154
155 reply = dbus_message_new_method_return(message);
156 dbus_connection_send(connection, reply, NULL);
157 exit(EXIT_SUCCESS);
158}
159
David Sodman8ef20062015-01-06 09:23:40 -0800160#define NUM_IMAGE_PARAMETERS (2)
Stéphane Marchesin8fc13522015-12-14 17:02:28 -0800161static DBusHandlerResult handle_image(DBusConnection* connection,
162 DBusMessage* message)
David Sodman8ef20062015-01-06 09:23:40 -0800163{
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800164 DBusMessage* reply;
David Sodman8ef20062015-01-06 09:23:40 -0800165 DBusError error;
166 dbus_bool_t stat;
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800167 terminal_t* terminal;
168 image_t* image;
David Sodman8ef20062015-01-06 09:23:40 -0800169 int i;
170 int x, y;
171 char* option[NUM_IMAGE_PARAMETERS];
172 char* optname;
173 char* optval;
174 int status;
175
176 dbus_error_init(&error);
177 stat = dbus_message_get_args(message, &error,
178 DBUS_TYPE_STRING, &option[0],
179 DBUS_TYPE_STRING, &option[1],
180 DBUS_TYPE_INVALID);
181
182 image = image_create();
183 if (image == NULL) {
Dominik Behr46755a42016-04-21 18:08:33 -0700184 LOG(WARNING, "Failed to create image.");
David Sodman8ef20062015-01-06 09:23:40 -0800185 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 Sodmanf0a925a2015-05-04 11:19:19 -0700208 goto fail;
David Sodman8ef20062015-01-06 09:23:40 -0800209 }
210
211 status = image_load_image_from_file(image);
212 if (status != 0) {
Dominik Behr46755a42016-04-21 18:08:33 -0700213 LOG(WARNING, "image_load_image_from_file(dbus) %s failed: %d:%s.",
214 image_get_filename(image), status, strerror(status));
David Sodmanf0a925a2015-05-04 11:19:19 -0700215 goto fail;
David Sodman8ef20062015-01-06 09:23:40 -0800216 }
217
Dominik Behr4defb362016-01-13 12:36:14 -0800218 terminal = term_get_current_terminal();
David Sodman8ef20062015-01-06 09:23:40 -0800219 if (!terminal)
David Sodmanf0a925a2015-05-04 11:19:19 -0700220 goto fail;
David Sodman8ef20062015-01-06 09:23:40 -0800221
David Sodmanf0a925a2015-05-04 11:19:19 -0700222 status = term_show_image(terminal, image);
223 if (status != 0) {
Dominik Behr46755a42016-04-21 18:08:33 -0700224 LOG(WARNING, "term_show_image(dbus) failed: %d.", status);
David Sodmanf0a925a2015-05-04 11:19:19 -0700225 goto fail;
226 }
David Sodman8ef20062015-01-06 09:23:40 -0800227 image_release(image);
228
229 reply = dbus_message_new_method_return(message);
230 dbus_connection_send(connection, reply, NULL);
231
232 return DBUS_HANDLER_RESULT_HANDLED;
David Sodmanf0a925a2015-05-04 11:19:19 -0700233fail:
234 if (image)
235 image_release(image);
236 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
David Sodman8ef20062015-01-06 09:23:40 -0800237}
238
Stéphane Marchesin8fc13522015-12-14 17:02:28 -0800239static void frecon_dbus_unregister(DBusConnection* connection, void* user_data)
David Sodman8ef20062015-01-06 09:23:40 -0800240{
241}
242
Stéphane Marchesin8fc13522015-12-14 17:02:28 -0800243static DBusHandlerResult frecon_dbus_message_handler(DBusConnection* connection,
244 DBusMessage* message,
245 void* user_data)
David Sodman8ef20062015-01-06 09:23:40 -0800246{
247 if (dbus_message_is_method_call(message,
248 kFreconDbusInterface, COMMAND_SWITCH_VT)) {
Stéphane Marchesinaf9e9ff2015-12-11 17:49:12 -0800249 return handle_switchvt(connection, message);
David Sodman8ef20062015-01-06 09:23:40 -0800250 } else if (dbus_message_is_method_call(message,
251 kFreconDbusInterface, COMMAND_MAKE_VT)) {
Stéphane Marchesinaf9e9ff2015-12-11 17:49:12 -0800252 return handle_makevt(connection, message);
David Sodman8ef20062015-01-06 09:23:40 -0800253 }
254 else if (dbus_message_is_method_call(message,
255 kFreconDbusInterface, COMMAND_TERMINATE)) {
Stéphane Marchesinaf9e9ff2015-12-11 17:49:12 -0800256 return handle_terminate(connection, message);
David Sodman8ef20062015-01-06 09:23:40 -0800257 } else if (dbus_message_is_method_call(message,
258 kFreconDbusInterface, COMMAND_IMAGE)) {
Stéphane Marchesinaf9e9ff2015-12-11 17:49:12 -0800259 return handle_image(connection, message);
David Sodman8ef20062015-01-06 09:23:40 -0800260 }
261
262 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
David Sodman8ef20062015-01-06 09:23:40 -0800263}
264
265static DBusObjectPathVTable
266frecon_vtable = {
Stéphane Marchesinaf9e9ff2015-12-11 17:49:12 -0800267 frecon_dbus_unregister,
268 frecon_dbus_message_handler,
David Sodman8ef20062015-01-06 09:23:40 -0800269 NULL
270};
271
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800272static dbus_bool_t add_watch(DBusWatch* w, void* data)
David Sodman8ef20062015-01-06 09:23:40 -0800273{
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800274 dbus_t* dbus = (dbus_t*)data;
David Sodman8ef20062015-01-06 09:23:40 -0800275 dbus->watch = w;
276
277 return TRUE;
278}
279
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800280static void remove_watch(DBusWatch* w, void* data)
David Sodman8ef20062015-01-06 09:23:40 -0800281{
282}
283
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800284static void toggle_watch(DBusWatch* w, void* data)
David Sodman8ef20062015-01-06 09:23:40 -0800285{
286}
287
Dominik Behr46c567f2016-03-08 15:11:48 -0800288static DBusHandlerResult handle_login_prompt_visible(DBusMessage* message)
289{
290 if (login_prompt_visible_callback) {
291 login_prompt_visible_callback(login_prompt_visible_callback_userptr);
292 login_prompt_visible_callback = NULL;
293 login_prompt_visible_callback_userptr = NULL;
294 }
295 chrome_is_already_up = true;
296
297 return DBUS_HANDLER_RESULT_HANDLED;
298}
299
300static DBusHandlerResult frecon_dbus_message_filter(DBusConnection* connection,
301 DBusMessage* message,
302 void* user_data)
303{
304 if (dbus_message_is_signal(message,
Dominik Behr5f6742f2016-03-10 18:03:54 -0800305 kSessionManagerInterface, kLoginPromptVisibleSignal))
Dominik Behr46c567f2016-03-08 15:11:48 -0800306 return handle_login_prompt_visible(message);
Dominik Behr46c567f2016-03-08 15:11:48 -0800307
308 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
309}
310
Dominik Behr797a3832016-01-11 15:53:11 -0800311bool dbus_is_initialized(void)
312{
313 return !!dbus;
314}
315
316bool dbus_init()
David Sodmanbbcb0522014-09-19 10:34:07 -0700317{
318 dbus_t* new_dbus;
319 DBusError err;
David Sodman8ef20062015-01-06 09:23:40 -0800320 int result;
321 dbus_bool_t stat;
David Sodmanbbcb0522014-09-19 10:34:07 -0700322
Dominik Behr5f6742f2016-03-10 18:03:54 -0800323 if (dbus_first_init) {
324 dbus_first_init = false;
325 dbus_first_init_time = get_monotonic_time_ms();
326 }
David Sodmanbbcb0522014-09-19 10:34:07 -0700327 dbus_error_init(&err);
328
329 new_dbus = (dbus_t*)calloc(1, sizeof(*new_dbus));
Stéphane Marchesinc236e0b2015-12-14 15:29:15 -0800330
331 if (!new_dbus)
Dominik Behr797a3832016-01-11 15:53:11 -0800332 return false;
Stéphane Marchesinc236e0b2015-12-14 15:29:15 -0800333
David Sodman8ef20062015-01-06 09:23:40 -0800334 new_dbus->fd = -1;
David Sodmanbbcb0522014-09-19 10:34:07 -0700335
336 new_dbus->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
337 if (dbus_error_is_set(&err)) {
Dominik Behr5f6742f2016-03-10 18:03:54 -0800338 if (!dbus_connect_fail) {
339 LOG(ERROR, "Cannot get DBUS connection");
340 dbus_connect_fail = true;
341 dbus_connect_fail_time = get_monotonic_time_ms();
342 }
David Sodmanbbcb0522014-09-19 10:34:07 -0700343 free(new_dbus);
Dominik Behr797a3832016-01-11 15:53:11 -0800344 return false;
David Sodmanbbcb0522014-09-19 10:34:07 -0700345 }
346
Dominik Behr5f6742f2016-03-10 18:03:54 -0800347 if (dbus_connect_fail) {
348 int64_t t = get_monotonic_time_ms() - dbus_connect_fail_time;
349 LOG(INFO, "DBUS connected after %.1f seconds", (float)t / 1000.0f);
350 }
351
David Sodman8ef20062015-01-06 09:23:40 -0800352 result = dbus_bus_request_name(new_dbus->conn, kFreconDbusInterface,
353 DBUS_NAME_FLAG_DO_NOT_QUEUE, &err);
354
355 if (result <= 0) {
356 LOG(ERROR, "Unable to get name for server");
357 }
358
359 stat = dbus_connection_register_object_path(new_dbus->conn,
360 kFreconDbusPath,
361 &frecon_vtable,
362 NULL);
363
364 if (!stat) {
365 LOG(ERROR, "failed to register object path");
366 }
367
Dominik Behr46c567f2016-03-08 15:11:48 -0800368 dbus_bus_add_match(new_dbus->conn, kLoginPromptVisibleRule, &err);
369
370 stat = dbus_connection_add_filter(new_dbus->conn, frecon_dbus_message_filter, NULL, NULL);
371 if (!stat) {
372 LOG(ERROR, "failed to add message filter");
373 }
374
David Sodman8ef20062015-01-06 09:23:40 -0800375 stat = dbus_connection_set_watch_functions(new_dbus->conn,
376 add_watch, remove_watch, toggle_watch,
377 new_dbus, NULL);
378
379 if (!stat) {
380 LOG(ERROR, "Failed to set watch functions");
381 }
382
David Sodmanbbcb0522014-09-19 10:34:07 -0700383 dbus_connection_set_exit_on_disconnect(new_dbus->conn, FALSE);
384
Dominik Behr797a3832016-01-11 15:53:11 -0800385 dbus = new_dbus;
386 return true;
David Sodmanbbcb0522014-09-19 10:34:07 -0700387}
388
Dominik Behr5f6742f2016-03-10 18:03:54 -0800389bool dbus_init_wait()
390{
391 while (!dbus_is_initialized()) {
392 if (!dbus_init()) {
393 int64_t t = get_monotonic_time_ms() - dbus_first_init_time;
394 if (t >= DBUS_INIT_TIMEOUT_MS) {
395 LOG(ERROR, "DBUS init failed after a timeout of %u sec", DBUS_INIT_TIMEOUT_MS/1000);
396 return false;
397 }
398 }
399 usleep(DBUS_WAIT_DELAY_US);
400 }
401 return true;
402}
403
Dominik Behr797a3832016-01-11 15:53:11 -0800404static bool dbus_method_call0(const char* service_name,
405 const char* service_path,
406 const char* service_interface,
407 const char* method)
David Sodman003faed2014-11-03 09:02:10 -0800408{
Dominik Behr797a3832016-01-11 15:53:11 -0800409 DBusMessage* msg = NULL;
410 if (!dbus) {
411 LOG(ERROR, "dbus not initialized");
412 return false;
413 }
David Sodman003faed2014-11-03 09:02:10 -0800414
415 msg = dbus_message_new_method_call(service_name,
416 service_path, service_interface, method);
417
418 if (!msg)
419 return false;
420
421 if (!dbus_connection_send_with_reply_and_block(dbus->conn,
David Sodman8ef20062015-01-06 09:23:40 -0800422 msg, DBUS_DEFAULT_DELAY, NULL)) {
David Sodman003faed2014-11-03 09:02:10 -0800423 dbus_message_unref(msg);
424 return false;
425 }
426
427 dbus_connection_flush(dbus->conn);
428 dbus_message_unref(msg);
429
430 return true;
431}
432
Dominik Behr797a3832016-01-11 15:53:11 -0800433static bool dbus_method_call1(const char* service_name,
434 const char* service_path,
435 const char* service_interface,
436 const char* method, int arg_type, void* param)
David Sodmanbbcb0522014-09-19 10:34:07 -0700437{
Dominik Behr797a3832016-01-11 15:53:11 -0800438 DBusMessage* msg = NULL;
439 if (!dbus) {
440 LOG(ERROR, "dbus not initialized");
441 return false;
442 }
David Sodmanbbcb0522014-09-19 10:34:07 -0700443
444 msg = dbus_message_new_method_call(service_name,
445 service_path, service_interface, method);
446
447 if (!msg)
448 return false;
449
450 if (!dbus_message_append_args(msg,
David Sodman19e4f9d2015-03-10 11:11:09 -0700451 arg_type, param, DBUS_TYPE_INVALID)) {
David Sodmanbbcb0522014-09-19 10:34:07 -0700452 dbus_message_unref(msg);
453 return false;
454 }
455
456 if (!dbus_connection_send_with_reply_and_block(dbus->conn,
David Sodman8ef20062015-01-06 09:23:40 -0800457 msg, DBUS_DEFAULT_DELAY, NULL)) {
David Sodmanbbcb0522014-09-19 10:34:07 -0700458 dbus_message_unref(msg);
459 return false;
460 }
461
462 dbus_connection_flush(dbus->conn);
463 dbus_message_unref(msg);
464
465 return true;
466}
467
Dominik Behr797a3832016-01-11 15:53:11 -0800468void dbus_destroy(void)
David Sodmanbbcb0522014-09-19 10:34:07 -0700469{
470 /* FIXME - not sure what the right counterpart to
Stéphane Marchesin00ff1872015-12-14 13:40:09 -0800471 * dbus_bus_get() is, unref documentation is rather
472 * unclear. Not a big issue but it would be nice to
473 * clean up properly here
474 */
David Sodmanbbcb0522014-09-19 10:34:07 -0700475 /* dbus_connection_unref(dbus->conn); */
Dominik Behr797a3832016-01-11 15:53:11 -0800476 if (dbus) {
David Sodman8ef20062015-01-06 09:23:40 -0800477 free(dbus);
Dominik Behr797a3832016-01-11 15:53:11 -0800478 dbus = NULL;
479 }
David Sodman8ef20062015-01-06 09:23:40 -0800480}
481
Dominik Behrd7112672016-01-20 16:59:34 -0800482void dbus_add_fds(fd_set* read_set, fd_set* exception_set, int *maxfd)
David Sodman8ef20062015-01-06 09:23:40 -0800483{
Dominik Behr797a3832016-01-11 15:53:11 -0800484 if (!dbus)
Dominik Behrd7112672016-01-20 16:59:34 -0800485 return;
Dominik Behr797a3832016-01-11 15:53:11 -0800486
David Sodman8ef20062015-01-06 09:23:40 -0800487 if (dbus->fd < 0)
488 dbus->fd = dbus_watch_get_unix_fd(dbus->watch);
489
490 if (dbus->fd >= 0) {
491 FD_SET(dbus->fd, read_set);
492 FD_SET(dbus->fd, exception_set);
493 }
David Sodman8ef20062015-01-06 09:23:40 -0800494
Dominik Behrd7112672016-01-20 16:59:34 -0800495 if (dbus->fd > *maxfd)
496 *maxfd = dbus->fd;
David Sodman8ef20062015-01-06 09:23:40 -0800497}
498
Dominik Behr797a3832016-01-11 15:53:11 -0800499void dbus_dispatch_io(void)
David Sodman8ef20062015-01-06 09:23:40 -0800500{
Dominik Behr797a3832016-01-11 15:53:11 -0800501 if (!dbus)
502 return;
503
David Sodman8ef20062015-01-06 09:23:40 -0800504 dbus_watch_handle(dbus->watch, DBUS_WATCH_READABLE);
505 while (dbus_connection_get_dispatch_status(dbus->conn)
506 == DBUS_DISPATCH_DATA_REMAINS) {
507 dbus_connection_dispatch(dbus->conn);
508 }
David Sodmanbbcb0522014-09-19 10:34:07 -0700509}
Dominik Behr797a3832016-01-11 15:53:11 -0800510
511void dbus_report_user_activity(int activity_type)
512{
513 dbus_bool_t allow_off = false;
514 if (!dbus)
515 return;
516
517 dbus_method_call1(kPowerManagerServiceName,
518 kPowerManagerServicePath,
519 kPowerManagerInterface,
520 kHandleUserActivityMethod,
521 DBUS_TYPE_INT32, &activity_type);
522
523 switch (activity_type) {
524 case USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS:
525 (void)dbus_method_call0(kPowerManagerServiceName,
526 kPowerManagerServicePath,
527 kPowerManagerInterface,
528 kIncreaseScreenBrightnessMethod);
529 break;
530 case USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS:
531 /*
532 * Shouldn't allow the screen to go
533 * completely off while frecon is active
534 * so passing false to allow_off
535 */
536 (void)dbus_method_call1(kPowerManagerServiceName,
537 kPowerManagerServicePath,
538 kPowerManagerInterface,
539 kDecreaseScreenBrightnessMethod,
540 DBUS_TYPE_BOOLEAN, &allow_off);
541 break;
542 }
543}
544
Dominik Behr46c567f2016-03-08 15:11:48 -0800545/*
546 * tell Chrome to take ownership of the display (DRM master)
547 */
Dominik Behr797a3832016-01-11 15:53:11 -0800548void dbus_take_display_ownership(void)
549{
550 if (!dbus)
551 return;
552 (void)dbus_method_call0(kLibCrosServiceName,
553 kLibCrosServicePath,
554 kLibCrosServiceInterface,
555 kTakeDisplayOwnership);
556}
557
Dominik Behr46c567f2016-03-08 15:11:48 -0800558/*
559 * ask Chrome to give up display ownership (DRM master)
560 */
Dominik Behr83864df2016-04-21 12:35:08 -0700561bool dbus_release_display_ownership(void)
Dominik Behr797a3832016-01-11 15:53:11 -0800562{
563 if (!dbus)
Dominik Behr83864df2016-04-21 12:35:08 -0700564 return true;
565 return dbus_method_call0(kLibCrosServiceName,
566 kLibCrosServicePath,
567 kLibCrosServiceInterface,
568 kReleaseDisplayOwnership);
Dominik Behr797a3832016-01-11 15:53:11 -0800569}
570
Dominik Behr46c567f2016-03-08 15:11:48 -0800571void dbus_set_login_prompt_visible_callback(void (*callback)(void*),
572 void* userptr)
573{
574 if (chrome_is_already_up) {
575 if (callback)
576 callback(userptr);
577 } else {
578 if (login_prompt_visible_callback && callback) {
579 LOG(ERROR, "trying to register login prompt visible callback multiple times");
580 return;
581 }
582 login_prompt_visible_callback = callback;
583 login_prompt_visible_callback_userptr = userptr;
584 }
585}
Dominik Behrf50c98f2016-03-31 14:05:29 -0700586
587#else
588
589#include <stdlib.h>
590#include <stdbool.h>
591
592bool dbus_init()
593{
594 return true;
595}
596
597bool dbus_init_wait()
598{
599 return true;
600}
601
602void dbus_destroy(void)
603{
604}
605
606void dbus_add_fds(fd_set* read_set, fd_set* exception_set, int *maxfd)
607{
608}
609
610void dbus_dispatch_io(void)
611{
612}
613
614void dbus_report_user_activity(int activity_type)
615{
616}
617
618void dbus_take_display_ownership(void)
619{
620}
621
Dominik Behr83864df2016-04-21 12:35:08 -0700622bool dbus_release_display_ownership(void)
Dominik Behrf50c98f2016-03-31 14:05:29 -0700623{
Dominik Behr83864df2016-04-21 12:35:08 -0700624 return true;
Dominik Behrf50c98f2016-03-31 14:05:29 -0700625}
626
627bool dbus_is_initialized(void)
628{
629 return true;
630}
631
632void dbus_set_login_prompt_visible_callback(void (*callback)(void*),
633 void* userptr)
634{
635}
636
637#endif