| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2004-2006 William Jon McCann <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
* |
| 19 |
* Authors: William Jon McCann <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
#include <stdlib.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <time.h> |
| 27 |
#include <string.h> |
| 28 |
#include <unistd.h> |
| 29 |
|
| 30 |
#include <glib/gi18n.h> |
| 31 |
|
| 32 |
#include <dbus/dbus.h> |
| 33 |
#include <dbus/dbus-glib.h> |
| 34 |
#include <dbus/dbus-glib-lowlevel.h> |
| 35 |
|
| 36 |
#ifdef HAVE_MIT_SAVER_EXTENSION |
| 37 |
#include <gtk/gtk.h> |
| 38 |
#if GTK_CHECK_VERSION(3, 0, 0) |
| 39 |
#include <gtk/gtkx.h> |
| 40 |
#else |
| 41 |
#include <gdk/gdkx.h> |
| 42 |
#endif |
| 43 |
#include <X11/extensions/scrnsaver.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifdef WITH_SYSTEMD |
| 47 |
#include <systemd/sd-login.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
#include "gs-listener-dbus.h" |
| 51 |
#include "gs-marshal.h" |
| 52 |
#include "gs-debug.h" |
| 53 |
#include "gs-bus.h" |
| 54 |
|
| 55 |
/* this is for dbus < 0.3 */ |
| 56 |
#if ((DBUS_VERSION_MAJOR == 0) && (DBUS_VERSION_MINOR < 30)) |
| 57 |
#define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err) |
| 58 |
#define dbus_bus_request_name(connection, name, flags, err) dbus_bus_acquire_service(connection, name, flags, err) |
| 59 |
#endif |
| 60 |
|
| 61 |
static void gs_listener_class_init (GSListenerClass *klass); |
| 62 |
static void gs_listener_init (GSListener *listener); |
| 63 |
static void gs_listener_finalize (GObject *object); |
| 64 |
|
| 65 |
static DBusHandlerResult gs_listener_message_handler (DBusConnection *connection, |
| 66 |
DBusMessage *message, |
| 67 |
void *user_data); |
| 68 |
|
| 69 |
#define TYPE_MISMATCH_ERROR GS_INTERFACE ".TypeMismatch" |
| 70 |
|
| 71 |
#define GS_LISTENER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_LISTENER, GSListenerPrivate)) |
| 72 |
|
| 73 |
struct GSListenerPrivate |
| 74 |
{ |
| 75 |
DBusConnection *connection; |
| 76 |
DBusConnection *system_connection; |
| 77 |
|
| 78 |
guint active : 1; |
| 79 |
guint lid_closed : 1; |
| 80 |
guint blanked : 1; |
| 81 |
time_t blanked_start; |
| 82 |
char *session_id; |
| 83 |
char *seat_path; |
| 84 |
|
| 85 |
#ifdef WITH_SYSTEMD |
| 86 |
gboolean have_systemd; |
| 87 |
char *sd_session_id; |
| 88 |
int delay_fd; |
| 89 |
#endif |
| 90 |
|
| 91 |
dbus_uint32_t inhibit_last_cookie; |
| 92 |
GHashTable *inhibit_list; |
| 93 |
}; |
| 94 |
|
| 95 |
enum { |
| 96 |
LOCK, |
| 97 |
LOCKED, |
| 98 |
SESSION_SWITCHED, |
| 99 |
ACTIVE_CHANGED, |
| 100 |
SUSPEND, |
| 101 |
RESUME, |
| 102 |
SIMULATE_USER_ACTIVITY, |
| 103 |
BLANKING, |
| 104 |
INHIBIT, |
| 105 |
IDLE_TIME, |
| 106 |
LAST_SIGNAL |
| 107 |
}; |
| 108 |
|
| 109 |
enum { |
| 110 |
PROP_0, |
| 111 |
PROP_ACTIVE, |
| 112 |
PROP_LID_CLOSED, |
| 113 |
}; |
| 114 |
|
| 115 |
static DBusObjectPathVTable |
| 116 |
gs_listener_vtable = { NULL, |
| 117 |
&gs_listener_message_handler, |
| 118 |
NULL, |
| 119 |
NULL, |
| 120 |
NULL, |
| 121 |
NULL }; |
| 122 |
|
| 123 |
static guint signals [LAST_SIGNAL] = { 0, }; |
| 124 |
|
| 125 |
G_DEFINE_TYPE (GSListener, gs_listener, G_TYPE_OBJECT) |
| 126 |
|
| 127 |
gboolean |
| 128 |
gs_listener_is_lid_closed (GSListener *listener) |
| 129 |
{ |
| 130 |
return listener->priv->lid_closed; |
| 131 |
} |
| 132 |
|
| 133 |
void |
| 134 |
gs_listener_send_switch_greeter (GSListener *listener) |
| 135 |
{ |
| 136 |
dbus_bool_t sent; |
| 137 |
DBusMessage *message; |
| 138 |
|
| 139 |
gs_debug ("Send switch greeter"); |
| 140 |
|
| 141 |
#ifdef WITH_SYSTEMD |
| 142 |
/* Compare with 0. On failure this will return < 0. |
| 143 |
* In the later case we probably aren't using systemd. |
| 144 |
*/ |
| 145 |
if (sd_session_is_active (listener->priv->sd_session_id) == 0) { |
| 146 |
gs_debug ("Refusing to switch to greeter"); |
| 147 |
return; |
| 148 |
}; |
| 149 |
#endif |
| 150 |
|
| 151 |
if (listener->priv->system_connection == NULL) { |
| 152 |
gs_debug ("No connection to the system bus"); |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
message = dbus_message_new_method_call (DM_SERVICE, |
| 157 |
listener->priv->seat_path, |
| 158 |
DM_SEAT_INTERFACE, |
| 159 |
"SwitchToGreeter"); |
| 160 |
if (message == NULL) { |
| 161 |
gs_debug ("Couldn't allocate the dbus message"); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
sent = dbus_connection_send (listener->priv->system_connection, message, NULL); |
| 166 |
dbus_message_unref (message); |
| 167 |
|
| 168 |
if (sent == FALSE) { |
| 169 |
gs_debug ("Couldn't send the dbus message"); |
| 170 |
return; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void |
| 175 |
gs_listener_send_lock_session (GSListener *listener) |
| 176 |
{ |
| 177 |
dbus_bool_t sent; |
| 178 |
DBusMessage *message; |
| 179 |
|
| 180 |
gs_debug ("Send lock session"); |
| 181 |
|
| 182 |
#ifdef WITH_SYSTEMD |
| 183 |
/* Compare with 0. On failure this will return < 0. |
| 184 |
* In the later case we probably aren't using systemd. |
| 185 |
*/ |
| 186 |
if (sd_session_is_active (listener->priv->sd_session_id) == 0) { |
| 187 |
gs_debug ("Refusing to lock session"); |
| 188 |
return; |
| 189 |
}; |
| 190 |
#endif |
| 191 |
|
| 192 |
if (listener->priv->system_connection == NULL) { |
| 193 |
gs_debug ("No connection to the system bus"); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
message = dbus_message_new_method_call (DM_SERVICE, |
| 198 |
DM_SESSION_PATH, |
| 199 |
DM_SESSION_INTERFACE, |
| 200 |
"Lock"); |
| 201 |
if (message == NULL) { |
| 202 |
gs_debug ("Couldn't allocate the dbus message"); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
sent = dbus_connection_send (listener->priv->system_connection, message, NULL); |
| 207 |
dbus_message_unref (message); |
| 208 |
|
| 209 |
if (sent == FALSE) { |
| 210 |
gs_debug ("Couldn't send the dbus message"); |
| 211 |
return; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
GQuark |
| 216 |
gs_listener_error_quark (void) |
| 217 |
{ |
| 218 |
static GQuark quark = 0; |
| 219 |
if (!quark) { |
| 220 |
quark = g_quark_from_static_string ("gs_listener_error"); |
| 221 |
} |
| 222 |
|
| 223 |
return quark; |
| 224 |
} |
| 225 |
|
| 226 |
static gboolean |
| 227 |
send_dbus_message (DBusConnection *connection, |
| 228 |
DBusMessage *message) |
| 229 |
{ |
| 230 |
gboolean is_connected; |
| 231 |
gboolean sent; |
| 232 |
|
| 233 |
g_return_val_if_fail (message != NULL, FALSE); |
| 234 |
|
| 235 |
if (! connection) { |
| 236 |
gs_debug ("There is no valid connection to the message bus"); |
| 237 |
return FALSE; |
| 238 |
} |
| 239 |
|
| 240 |
is_connected = dbus_connection_get_is_connected (connection); |
| 241 |
if (! is_connected) { |
| 242 |
gs_debug ("Not connected to the message bus"); |
| 243 |
return FALSE; |
| 244 |
} |
| 245 |
|
| 246 |
sent = dbus_connection_send (connection, message, NULL); |
| 247 |
|
| 248 |
return sent; |
| 249 |
} |
| 250 |
|
| 251 |
static void |
| 252 |
send_dbus_boolean_signal (GSListener *listener, |
| 253 |
const char *name, |
| 254 |
gboolean value) |
| 255 |
{ |
| 256 |
DBusMessage *message; |
| 257 |
DBusMessageIter iter; |
| 258 |
|
| 259 |
g_return_if_fail (listener != NULL); |
| 260 |
|
| 261 |
message = dbus_message_new_signal (GS_PATH, |
| 262 |
GS_INTERFACE, |
| 263 |
name); |
| 264 |
|
| 265 |
dbus_message_iter_init_append (message, &iter); |
| 266 |
dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &value); |
| 267 |
|
| 268 |
if (! send_dbus_message (listener->priv->connection, message)) { |
| 269 |
gs_debug ("Could not send %s signal", name); |
| 270 |
} |
| 271 |
|
| 272 |
dbus_message_unref (message); |
| 273 |
|
| 274 |
/* Emit the signal on the KDE path */ |
| 275 |
message = dbus_message_new_signal (GS_PATH_KDE, |
| 276 |
GS_INTERFACE, |
| 277 |
name); |
| 278 |
|
| 279 |
dbus_message_iter_init_append (message, &iter); |
| 280 |
dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &value); |
| 281 |
|
| 282 |
if (! send_dbus_message (listener->priv->connection, message)) { |
| 283 |
gs_debug ("Could not send %s signal", name); |
| 284 |
} |
| 285 |
|
| 286 |
dbus_message_unref (message); |
| 287 |
|
| 288 |
/* Emit the signal on the GNOME interface */ |
| 289 |
message = dbus_message_new_signal (GS_PATH_GNOME, |
| 290 |
GS_INTERFACE_GNOME, |
| 291 |
name); |
| 292 |
|
| 293 |
dbus_message_iter_init_append (message, &iter); |
| 294 |
dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &value); |
| 295 |
|
| 296 |
if (! send_dbus_message (listener->priv->connection, message)) { |
| 297 |
gs_debug ("Could not send %s signal", name); |
| 298 |
} |
| 299 |
|
| 300 |
dbus_message_unref (message); |
| 301 |
} |
| 302 |
|
| 303 |
static void |
| 304 |
gs_listener_send_signal_active_changed (GSListener *listener, |
| 305 |
gboolean active) |
| 306 |
{ |
| 307 |
g_return_if_fail (listener != NULL); |
| 308 |
|
| 309 |
gs_debug ("Sending the ActiveChanged(%s) signal on the session bus", |
| 310 |
active ? "TRUE" : "FALSE"); |
| 311 |
|
| 312 |
send_dbus_boolean_signal (listener, "ActiveChanged", active); |
| 313 |
} |
| 314 |
|
| 315 |
void |
| 316 |
gs_listener_set_blanked (GSListener *listener, |
| 317 |
gboolean active) |
| 318 |
{ |
| 319 |
listener->priv->blanked = active; |
| 320 |
|
| 321 |
if (active) { |
| 322 |
listener->priv->blanked_start = time (NULL); |
| 323 |
} else { |
| 324 |
listener->priv->blanked_start = 0; |
| 325 |
} |
| 326 |
|
| 327 |
gs_listener_send_signal_active_changed (listener, active); |
| 328 |
} |
| 329 |
|
| 330 |
gboolean |
| 331 |
gs_listener_set_active (GSListener *listener, |
| 332 |
gboolean active) |
| 333 |
{ |
| 334 |
gboolean res; |
| 335 |
|
| 336 |
g_return_val_if_fail (GS_IS_LISTENER (listener), FALSE); |
| 337 |
|
| 338 |
if (listener->priv->active == active) { |
| 339 |
gs_debug ("Trying to set active state when already: %s", |
| 340 |
active ? "active" : "inactive"); |
| 341 |
return FALSE; |
| 342 |
} |
| 343 |
|
| 344 |
res = FALSE; |
| 345 |
g_signal_emit (listener, signals [ACTIVE_CHANGED], 0, active, &res); |
| 346 |
if (! res) { |
| 347 |
/* if the signal is not handled then we haven't changed state */ |
| 348 |
gs_debug ("Active-changed signal not handled"); |
| 349 |
|
| 350 |
return FALSE; |
| 351 |
} |
| 352 |
|
| 353 |
listener->priv->active = active; |
| 354 |
|
| 355 |
return TRUE; |
| 356 |
} |
| 357 |
|
| 358 |
void |
| 359 |
gs_listener_set_idle_hint (GSListener *listener, gboolean idle) |
| 360 |
{ |
| 361 |
dbus_bool_t sent; |
| 362 |
DBusMessage *message; |
| 363 |
|
| 364 |
gs_debug ("Send idle hint: %d", idle); |
| 365 |
|
| 366 |
#ifdef WITH_SYSTEMD |
| 367 |
if (listener->priv->have_systemd) { |
| 368 |
|
| 369 |
if (listener->priv->system_connection == NULL) { |
| 370 |
gs_debug ("No connection to the system bus"); |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
message = dbus_message_new_method_call (SYSTEMD_LOGIND_SERVICE, |
| 375 |
listener->priv->session_id, |
| 376 |
SYSTEMD_LOGIND_SESSION_INTERFACE, |
| 377 |
"SetIdleHint"); |
| 378 |
if (message == NULL) { |
| 379 |
gs_debug ("Couldn't allocate the dbus message"); |
| 380 |
return; |
| 381 |
} |
| 382 |
|
| 383 |
if (dbus_message_append_args (message, |
| 384 |
DBUS_TYPE_BOOLEAN, &idle, |
| 385 |
DBUS_TYPE_INVALID) == FALSE) { |
| 386 |
gs_debug ("Couldn't add args to the dbus message"); |
| 387 |
dbus_message_unref (message); |
| 388 |
return; |
| 389 |
} |
| 390 |
|
| 391 |
sent = dbus_connection_send (listener->priv->system_connection, message, NULL); |
| 392 |
dbus_message_unref (message); |
| 393 |
|
| 394 |
if (sent == FALSE) { |
| 395 |
gs_debug ("Couldn't send the dbus message"); |
| 396 |
return; |
| 397 |
} |
| 398 |
|
| 399 |
return; |
| 400 |
} |
| 401 |
#endif |
| 402 |
|
| 403 |
#ifdef WITH_CONSOLE_KIT |
| 404 |
if (listener->priv->system_connection == NULL) { |
| 405 |
gs_debug ("No connection to the system bus"); |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
message = dbus_message_new_method_call (CK_SERVICE, |
| 410 |
listener->priv->session_id, |
| 411 |
CK_SESSION_INTERFACE, |
| 412 |
"SetIdleHint"); |
| 413 |
if (message == NULL) { |
| 414 |
gs_debug ("Couldn't allocate the dbus message"); |
| 415 |
return; |
| 416 |
} |
| 417 |
|
| 418 |
if (dbus_message_append_args (message, |
| 419 |
DBUS_TYPE_BOOLEAN, &idle, |
| 420 |
DBUS_TYPE_INVALID) == FALSE) { |
| 421 |
gs_debug ("Couldn't add args to the dbus message"); |
| 422 |
dbus_message_unref (message); |
| 423 |
return; |
| 424 |
} |
| 425 |
|
| 426 |
sent = dbus_connection_send (listener->priv->system_connection, message, NULL); |
| 427 |
dbus_message_unref (message); |
| 428 |
|
| 429 |
if (sent == FALSE) { |
| 430 |
gs_debug ("Couldn't send the dbus message"); |
| 431 |
return; |
| 432 |
} |
| 433 |
#endif |
| 434 |
} |
| 435 |
|
| 436 |
void |
| 437 |
gs_listener_delay_suspend (GSListener *listener) |
| 438 |
{ |
| 439 |
#ifdef WITH_SYSTEMD |
| 440 |
DBusMessage *message; |
| 441 |
DBusMessage *reply; |
| 442 |
DBusError error; |
| 443 |
const char *what; |
| 444 |
const char *who; |
| 445 |
const char *why; |
| 446 |
const char *mode; |
| 447 |
int fd; |
| 448 |
|
| 449 |
gs_debug ("Delay suspend"); |
| 450 |
|
| 451 |
if (listener->priv->system_connection == NULL) { |
| 452 |
gs_debug ("No connection to the system bus"); |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
dbus_error_init (&error); |
| 457 |
|
| 458 |
message = dbus_message_new_method_call (SYSTEMD_LOGIND_SERVICE, SYSTEMD_LOGIND_PATH, SYSTEMD_LOGIND_INTERFACE, "Inhibit"); |
| 459 |
if (message == NULL) { |
| 460 |
gs_debug ("Couldn't allocate the dbus message"); |
| 461 |
return; |
| 462 |
} |
| 463 |
|
| 464 |
what = "sleep"; |
| 465 |
who = _("Screen Locker"); |
| 466 |
why = _("Lock the screen on suspend/resume"); |
| 467 |
mode = "delay"; |
| 468 |
|
| 469 |
if (dbus_message_append_args (message, |
| 470 |
DBUS_TYPE_STRING, &what, |
| 471 |
DBUS_TYPE_STRING, &who, |
| 472 |
DBUS_TYPE_STRING, &why, |
| 473 |
DBUS_TYPE_STRING, &mode, |
| 474 |
DBUS_TYPE_INVALID) == FALSE) { |
| 475 |
gs_debug ("Couldn't add args to the dbus message"); |
| 476 |
dbus_message_unref (message); |
| 477 |
return; |
| 478 |
} |
| 479 |
|
| 480 |
reply = dbus_connection_send_with_reply_and_block (listener->priv->system_connection, |
| 481 |
message, |
| 482 |
-1, &error); |
| 483 |
dbus_message_unref (message); |
| 484 |
|
| 485 |
if (dbus_error_is_set (&error)) { |
| 486 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 487 |
dbus_error_free (&error); |
| 488 |
return; |
| 489 |
} |
| 490 |
|
| 491 |
if (dbus_message_get_args (reply, &error, |
| 492 |
DBUS_TYPE_UNIX_FD, &fd, |
| 493 |
DBUS_TYPE_INVALID) == FALSE) { |
| 494 |
fd = -1; |
| 495 |
} |
| 496 |
|
| 497 |
dbus_message_unref (reply); |
| 498 |
|
| 499 |
if (dbus_error_is_set (&error)) { |
| 500 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 501 |
dbus_error_free (&error); |
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
listener->priv->delay_fd = fd; |
| 506 |
#endif |
| 507 |
} |
| 508 |
|
| 509 |
void |
| 510 |
gs_listener_resume_suspend (GSListener *listener) |
| 511 |
{ |
| 512 |
#ifdef WITH_SYSTEMD |
| 513 |
gs_debug ("Resume suspend: fd=%d", listener->priv->delay_fd); |
| 514 |
|
| 515 |
if (listener->priv->delay_fd >= 0) { |
| 516 |
close (listener->priv->delay_fd); |
| 517 |
listener->priv->delay_fd = -1; |
| 518 |
} |
| 519 |
#endif |
| 520 |
} |
| 521 |
|
| 522 |
static dbus_uint32_t |
| 523 |
gs_listener_add_inhibit (GSListener *listener, |
| 524 |
const char *owner) |
| 525 |
{ |
| 526 |
dbus_uint32_t *cookie; |
| 527 |
|
| 528 |
cookie = g_new (dbus_uint32_t, 1); |
| 529 |
if (cookie == NULL) { |
| 530 |
g_error ("No memory"); |
| 531 |
} |
| 532 |
|
| 533 |
*cookie = ++listener->priv->inhibit_last_cookie; |
| 534 |
|
| 535 |
if (g_hash_table_size (listener->priv->inhibit_list) == 0) { |
| 536 |
g_signal_emit (listener, signals [INHIBIT], 0, TRUE); |
| 537 |
} |
| 538 |
|
| 539 |
g_hash_table_insert (listener->priv->inhibit_list, cookie, g_strdup (owner)); |
| 540 |
|
| 541 |
return *cookie; |
| 542 |
} |
| 543 |
|
| 544 |
static void |
| 545 |
gs_listener_remove_inhibit (GSListener *listener, |
| 546 |
dbus_uint32_t cookie, |
| 547 |
const char *owner) |
| 548 |
{ |
| 549 |
const gchar *owned; |
| 550 |
|
| 551 |
owned = g_hash_table_lookup (listener->priv->inhibit_list, &cookie); |
| 552 |
|
| 553 |
if (owned == NULL) { |
| 554 |
return; |
| 555 |
} |
| 556 |
|
| 557 |
if (strcmp (owner, owned) != 0) { |
| 558 |
return; |
| 559 |
} |
| 560 |
|
| 561 |
g_hash_table_remove (listener->priv->inhibit_list, &cookie); |
| 562 |
|
| 563 |
if (g_hash_table_size (listener->priv->inhibit_list) == 0) { |
| 564 |
g_signal_emit (listener, signals [INHIBIT], 0, FALSE); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
static gboolean |
| 569 |
compare_owner (gpointer key, |
| 570 |
gpointer value, |
| 571 |
gpointer user_data) |
| 572 |
{ |
| 573 |
const gchar *oa = value; |
| 574 |
const char *ob = user_data; |
| 575 |
|
| 576 |
return strcmp (oa, ob) == 0;; |
| 577 |
} |
| 578 |
|
| 579 |
static void |
| 580 |
gs_listener_disonnect_inhibit (GSListener *listener, |
| 581 |
const char *owner) |
| 582 |
{ |
| 583 |
guint count; |
| 584 |
|
| 585 |
count = g_hash_table_foreach_remove (listener->priv->inhibit_list, compare_owner, (gpointer)owner); |
| 586 |
|
| 587 |
gs_debug ("Inhibitor disconnected: %s (%u)", owner, count); |
| 588 |
|
| 589 |
if (count > 0 && g_hash_table_size (listener->priv->inhibit_list) == 0) { |
| 590 |
g_signal_emit (listener, signals [INHIBIT], 0, FALSE); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
static void |
| 595 |
raise_property_type_error (DBusConnection *connection, |
| 596 |
DBusMessage *in_reply_to, |
| 597 |
const char *device_id) |
| 598 |
{ |
| 599 |
char buf [512]; |
| 600 |
DBusMessage *reply; |
| 601 |
|
| 602 |
snprintf (buf, 511, |
| 603 |
"Type mismatch setting property with id %s", |
| 604 |
device_id); |
| 605 |
gs_debug (buf); |
| 606 |
|
| 607 |
reply = dbus_message_new_error (in_reply_to, |
| 608 |
TYPE_MISMATCH_ERROR, |
| 609 |
buf); |
| 610 |
if (reply == NULL) { |
| 611 |
g_error ("No memory"); |
| 612 |
} |
| 613 |
if (! dbus_connection_send (connection, reply, NULL)) { |
| 614 |
g_error ("No memory"); |
| 615 |
} |
| 616 |
|
| 617 |
dbus_message_unref (reply); |
| 618 |
} |
| 619 |
|
| 620 |
static DBusHandlerResult |
| 621 |
send_success_reply (DBusConnection *connection, |
| 622 |
DBusMessage *message) |
| 623 |
{ |
| 624 |
DBusMessage *reply; |
| 625 |
|
| 626 |
if (dbus_message_get_no_reply (message)) { |
| 627 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 628 |
} |
| 629 |
|
| 630 |
reply = dbus_message_new_method_return (message); |
| 631 |
if (reply == NULL) { |
| 632 |
g_error ("No memory"); |
| 633 |
} |
| 634 |
|
| 635 |
if (! dbus_connection_send (connection, reply, NULL)) { |
| 636 |
g_error ("No memory"); |
| 637 |
} |
| 638 |
|
| 639 |
dbus_message_unref (reply); |
| 640 |
|
| 641 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 642 |
} |
| 643 |
|
| 644 |
static DBusHandlerResult |
| 645 |
listener_set_active (GSListener *listener, |
| 646 |
DBusConnection *connection, |
| 647 |
DBusMessage *message, |
| 648 |
gboolean send_reply) |
| 649 |
{ |
| 650 |
const char *path; |
| 651 |
int type; |
| 652 |
DBusMessageIter iter; |
| 653 |
DBusMessage *reply; |
| 654 |
gboolean new_state, res; |
| 655 |
dbus_bool_t v; |
| 656 |
|
| 657 |
path = dbus_message_get_path (message); |
| 658 |
|
| 659 |
dbus_message_iter_init (message, &iter); |
| 660 |
type = dbus_message_iter_get_arg_type (&iter); |
| 661 |
|
| 662 |
if (type != DBUS_TYPE_BOOLEAN) { |
| 663 |
gs_debug ("Unsupported property type %d", type); |
| 664 |
raise_property_type_error (connection, message, path); |
| 665 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 666 |
} |
| 667 |
|
| 668 |
dbus_message_iter_get_basic (&iter, &v); |
| 669 |
new_state = v; |
| 670 |
g_signal_emit (listener, signals [BLANKING], 0, new_state, &res); |
| 671 |
v = res; |
| 672 |
|
| 673 |
if (send_reply == FALSE) { |
| 674 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 675 |
} |
| 676 |
|
| 677 |
reply = dbus_message_new_method_return (message); |
| 678 |
|
| 679 |
if (reply == NULL) { |
| 680 |
g_error ("No memory"); |
| 681 |
} |
| 682 |
|
| 683 |
dbus_message_iter_init_append (reply, &iter); |
| 684 |
|
| 685 |
dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &v); |
| 686 |
|
| 687 |
if (! dbus_connection_send (connection, reply, NULL)) { |
| 688 |
g_error ("No memory"); |
| 689 |
} |
| 690 |
|
| 691 |
dbus_message_unref (reply); |
| 692 |
|
| 693 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 694 |
} |
| 695 |
|
| 696 |
static DBusHandlerResult |
| 697 |
listener_get_bool (GSListener *listener, |
| 698 |
DBusConnection *connection, |
| 699 |
DBusMessage *message, |
| 700 |
dbus_bool_t v) |
| 701 |
{ |
| 702 |
DBusMessageIter iter; |
| 703 |
DBusMessage *reply; |
| 704 |
|
| 705 |
reply = dbus_message_new_method_return (message); |
| 706 |
|
| 707 |
if (reply == NULL) { |
| 708 |
g_error ("No memory"); |
| 709 |
} |
| 710 |
|
| 711 |
dbus_message_iter_init_append (reply, &iter); |
| 712 |
|
| 713 |
dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &v); |
| 714 |
|
| 715 |
if (! dbus_connection_send (connection, reply, NULL)) { |
| 716 |
g_error ("No memory"); |
| 717 |
} |
| 718 |
|
| 719 |
dbus_message_unref (reply); |
| 720 |
|
| 721 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 722 |
} |
| 723 |
|
| 724 |
static DBusHandlerResult |
| 725 |
listener_get_time (GSListener *listener, |
| 726 |
DBusConnection *connection, |
| 727 |
DBusMessage *message, |
| 728 |
time_t t) |
| 729 |
{ |
| 730 |
DBusMessageIter iter; |
| 731 |
DBusMessage *reply; |
| 732 |
dbus_uint32_t secs = 0; |
| 733 |
time_t now; |
| 734 |
|
| 735 |
reply = dbus_message_new_method_return (message); |
| 736 |
|
| 737 |
if (reply == NULL) { |
| 738 |
g_error ("No memory"); |
| 739 |
} |
| 740 |
|
| 741 |
dbus_message_iter_init_append (reply, &iter); |
| 742 |
|
| 743 |
now = time (NULL); |
| 744 |
if (G_UNLIKELY (now < t)) { |
| 745 |
gs_debug ("Time is in the future"); |
| 746 |
} else if (G_UNLIKELY (t<= 0)) { |
| 747 |
//gs_debug ("Time was not set"); |
| 748 |
} else { |
| 749 |
secs = now - t; |
| 750 |
} |
| 751 |
dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &secs); |
| 752 |
|
| 753 |
if (! dbus_connection_send (connection, reply, NULL)) { |
| 754 |
g_error ("No memory"); |
| 755 |
} |
| 756 |
|
| 757 |
dbus_message_unref (reply); |
| 758 |
|
| 759 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 760 |
} |
| 761 |
|
| 762 |
static DBusHandlerResult |
| 763 |
listener_get_info (GSListener *listener, |
| 764 |
DBusConnection *connection, |
| 765 |
DBusMessage *message, |
| 766 |
int signal) |
| 767 |
{ |
| 768 |
DBusMessageIter iter; |
| 769 |
DBusMessage *reply; |
| 770 |
|
| 771 |
reply = dbus_message_new_method_return (message); |
| 772 |
|
| 773 |
if (reply == NULL) { |
| 774 |
g_error ("No memory"); |
| 775 |
} |
| 776 |
|
| 777 |
dbus_message_iter_init_append (reply, &iter); |
| 778 |
|
| 779 |
switch (signal) { |
| 780 |
case IDLE_TIME: |
| 781 |
{ |
| 782 |
dbus_uint32_t v; |
| 783 |
gulong res; |
| 784 |
g_signal_emit (listener, signals [signal], 0, &res); |
| 785 |
v = res; |
| 786 |
dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &v); |
| 787 |
break; |
| 788 |
} |
| 789 |
|
| 790 |
default: |
| 791 |
gs_debug ("Unsupported signal id %d", signal); |
| 792 |
break; |
| 793 |
} |
| 794 |
|
| 795 |
if (! dbus_connection_send (connection, reply, NULL)) { |
| 796 |
g_error ("No memory"); |
| 797 |
} |
| 798 |
|
| 799 |
dbus_message_unref (reply); |
| 800 |
|
| 801 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 802 |
} |
| 803 |
|
| 804 |
static DBusHandlerResult |
| 805 |
listener_inhibit (GSListener *listener, |
| 806 |
DBusConnection *connection, |
| 807 |
DBusMessage *message) |
| 808 |
{ |
| 809 |
const char *path; |
| 810 |
int type; |
| 811 |
DBusMessageIter iter; |
| 812 |
DBusMessage *reply; |
| 813 |
dbus_uint32_t cookie; |
| 814 |
const char *application; |
| 815 |
const char *reason; |
| 816 |
|
| 817 |
path = dbus_message_get_path (message); |
| 818 |
|
| 819 |
dbus_message_iter_init (message, &iter); |
| 820 |
type = dbus_message_iter_get_arg_type (&iter); |
| 821 |
|
| 822 |
if (type != DBUS_TYPE_STRING) { |
| 823 |
gs_debug ("Unsupported property type %d", type); |
| 824 |
raise_property_type_error (connection, message, path); |
| 825 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 826 |
} |
| 827 |
|
| 828 |
dbus_message_iter_get_basic (&iter, &application); |
| 829 |
|
| 830 |
dbus_message_iter_next (&iter); |
| 831 |
type = dbus_message_iter_get_arg_type (&iter); |
| 832 |
|
| 833 |
if (type != DBUS_TYPE_STRING) { |
| 834 |
gs_debug ("Unsupported property type %d", type); |
| 835 |
raise_property_type_error (connection, message, path); |
| 836 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 837 |
} |
| 838 |
|
| 839 |
dbus_message_iter_get_basic (&iter, &reason); |
| 840 |
|
| 841 |
gs_debug ("Inhibit requested: %s '%s'", application, reason); |
| 842 |
|
| 843 |
cookie = gs_listener_add_inhibit (listener, dbus_message_get_sender (message)); |
| 844 |
|
| 845 |
reply = dbus_message_new_method_return (message); |
| 846 |
|
| 847 |
dbus_message_iter_init_append (reply, &iter); |
| 848 |
|
| 849 |
if (reply == NULL) { |
| 850 |
g_error ("No memory"); |
| 851 |
} |
| 852 |
|
| 853 |
gs_debug ("Returning inhibit cookie %u", cookie); |
| 854 |
dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &cookie); |
| 855 |
|
| 856 |
if (! dbus_connection_send (connection, reply, NULL)) { |
| 857 |
g_error ("No memory"); |
| 858 |
} |
| 859 |
|
| 860 |
dbus_message_unref (reply); |
| 861 |
|
| 862 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 863 |
} |
| 864 |
|
| 865 |
static DBusHandlerResult |
| 866 |
listener_uninhibit (GSListener *listener, |
| 867 |
DBusConnection *connection, |
| 868 |
DBusMessage *message) |
| 869 |
{ |
| 870 |
const char *path; |
| 871 |
int type; |
| 872 |
DBusMessageIter iter; |
| 873 |
dbus_uint32_t cookie = 0; |
| 874 |
|
| 875 |
path = dbus_message_get_path (message); |
| 876 |
|
| 877 |
dbus_message_iter_init (message, &iter); |
| 878 |
type = dbus_message_iter_get_arg_type (&iter); |
| 879 |
|
| 880 |
if (type != DBUS_TYPE_UINT32) { |
| 881 |
gs_debug ("Unsupported property type %d", type); |
| 882 |
raise_property_type_error (connection, message, path); |
| 883 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 884 |
} |
| 885 |
|
| 886 |
dbus_message_iter_get_basic (&iter, &cookie); |
| 887 |
|
| 888 |
gs_debug ("Uninhibit requested: %u", cookie); |
| 889 |
|
| 890 |
gs_listener_remove_inhibit (listener, cookie, dbus_message_get_sender (message)); |
| 891 |
|
| 892 |
return send_success_reply (connection, message); |
| 893 |
} |
| 894 |
|
| 895 |
static DBusHandlerResult |
| 896 |
do_introspect (DBusConnection *connection, |
| 897 |
DBusMessage *message, |
| 898 |
dbus_bool_t local_interface) |
| 899 |
{ |
| 900 |
DBusMessage *reply; |
| 901 |
GString *xml; |
| 902 |
char *xml_string; |
| 903 |
|
| 904 |
/* standard header */ |
| 905 |
xml = g_string_new ("<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n" |
| 906 |
"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n" |
| 907 |
"<node>\n" |
| 908 |
" <interface name=\"org.freedesktop.DBus.Introspectable\">\n" |
| 909 |
" <method name=\"Introspect\">\n" |
| 910 |
" <arg name=\"data\" direction=\"out\" type=\"s\"/>\n" |
| 911 |
" </method>\n" |
| 912 |
" </interface>\n"); |
| 913 |
|
| 914 |
/* ScreenSaver interface */ |
| 915 |
xml = g_string_append (xml, |
| 916 |
" <interface name=\""GS_INTERFACE"\">\n" |
| 917 |
" <method name=\"Lock\">\n" |
| 918 |
" </method>\n" |
| 919 |
" <method name=\"SimulateUserActivity\">\n" |
| 920 |
" </method>\n" |
| 921 |
" <method name=\"GetActive\">\n" |
| 922 |
" <arg direction=\"out\" type=\"b\"/>\n" |
| 923 |
" </method>\n" |
| 924 |
" <method name=\"GetActiveTime\">\n" |
| 925 |
" <arg name=\"seconds\" direction=\"out\" type=\"u\"/>\n" |
| 926 |
" </method>\n" |
| 927 |
" <method name=\"GetSessionIdleTime\">\n" |
| 928 |
" <arg name=\"seconds\" direction=\"out\" type=\"u\"/>\n" |
| 929 |
" </method>\n" |
| 930 |
" <method name=\"SetActive\">\n" |
| 931 |
" <arg direction=\"out\" type=\"b\"/>\n" |
| 932 |
" <arg name=\"e\" direction=\"in\" type=\"b\"/>\n" |
| 933 |
" </method>\n" |
| 934 |
" <method name=\"Inhibit\">\n" |
| 935 |
" <arg direction=\"out\" type=\"u\"/>\n" |
| 936 |
" <arg name=\"application_name\" direction=\"in\" type=\"s\"/>\n" |
| 937 |
" <arg name=\"reason_for_inhibit\" direction=\"in\" type=\"s\"/>\n" |
| 938 |
" </method>\n" |
| 939 |
" <method name=\"UnInhibit\">\n" |
| 940 |
" <arg name=\"cookie\" direction=\"in\" type=\"u\"/>\n" |
| 941 |
" </method>\n" |
| 942 |
" <signal name=\"ActiveChanged\">\n" |
| 943 |
" <arg type=\"b\"/>\n" |
| 944 |
" </signal>\n" |
| 945 |
" </interface>\n"); |
| 946 |
|
| 947 |
/* ScreenSaver interface GNOME */ |
| 948 |
xml = g_string_append (xml, |
| 949 |
" <interface name=\""GS_INTERFACE_GNOME"\">\n" |
| 950 |
" <method name=\"Lock\">\n" |
| 951 |
" </method>\n" |
| 952 |
" <method name=\"SimulateUserActivity\">\n" |
| 953 |
" </method>\n" |
| 954 |
" <method name=\"GetActive\">\n" |
| 955 |
" <arg direction=\"out\" type=\"b\"/>\n" |
| 956 |
" </method>\n" |
| 957 |
" <method name=\"GetActiveTime\">\n" |
| 958 |
" <arg name=\"seconds\" direction=\"out\" type=\"u\"/>\n" |
| 959 |
" </method>\n" |
| 960 |
" <method name=\"SetActive\">\n" |
| 961 |
" <arg name=\"e\" direction=\"in\" type=\"b\"/>\n" |
| 962 |
" </method>\n" |
| 963 |
" <signal name=\"ActiveChanged\">\n" |
| 964 |
" <arg type=\"b\"/>\n" |
| 965 |
" </signal>\n" |
| 966 |
" </interface>\n"); |
| 967 |
|
| 968 |
reply = dbus_message_new_method_return (message); |
| 969 |
|
| 970 |
xml = g_string_append (xml, "</node>\n"); |
| 971 |
xml_string = g_string_free (xml, FALSE); |
| 972 |
|
| 973 |
dbus_message_append_args (reply, |
| 974 |
DBUS_TYPE_STRING, &xml_string, |
| 975 |
DBUS_TYPE_INVALID); |
| 976 |
|
| 977 |
g_free (xml_string); |
| 978 |
|
| 979 |
if (reply == NULL) { |
| 980 |
g_error ("No memory"); |
| 981 |
} |
| 982 |
|
| 983 |
if (! dbus_connection_send (connection, reply, NULL)) { |
| 984 |
g_error ("No memory"); |
| 985 |
} |
| 986 |
|
| 987 |
dbus_message_unref (reply); |
| 988 |
|
| 989 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 990 |
} |
| 991 |
|
| 992 |
static DBusHandlerResult |
| 993 |
listener_dbus_handle_session_message (GSListener *listener, |
| 994 |
DBusConnection *connection, |
| 995 |
DBusMessage *message, |
| 996 |
dbus_bool_t local_interface) |
| 997 |
{ |
| 998 |
#if 0 |
| 999 |
g_message ("obj_path=%s interface=%s method=%s destination=%s", |
| 1000 |
dbus_message_get_path (message), |
| 1001 |
dbus_message_get_interface (message), |
| 1002 |
dbus_message_get_member (message), |
| 1003 |
dbus_message_get_destination (message)); |
| 1004 |
#endif |
| 1005 |
|
| 1006 |
g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
| 1007 |
g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
| 1008 |
|
| 1009 |
if (dbus_message_is_method_call (message, GS_INTERFACE, "Lock")) { |
| 1010 |
gs_debug ("Received Lock request"); |
| 1011 |
g_signal_emit (listener, signals [LOCK], 0); |
| 1012 |
return send_success_reply (connection, message); |
| 1013 |
} |
| 1014 |
if (dbus_message_is_method_call (message, GS_INTERFACE, "SetActive")) { |
| 1015 |
gs_debug ("Received SetActive request"); |
| 1016 |
return listener_set_active (listener, connection, message, TRUE); |
| 1017 |
} |
| 1018 |
if (dbus_message_is_method_call (message, GS_INTERFACE, "GetActive")) { |
| 1019 |
gs_debug ("Received GetActive request: %d", listener->priv->blanked); |
| 1020 |
return listener_get_bool (listener, connection, message, listener->priv->blanked); |
| 1021 |
} |
| 1022 |
if (dbus_message_is_method_call (message, GS_INTERFACE, "GetActiveTime")) { |
| 1023 |
gs_debug ("Received GetActiveTime request: %d", listener->priv->blanked_start); |
| 1024 |
return listener_get_time (listener, connection, message, listener->priv->blanked_start); |
| 1025 |
} |
| 1026 |
if (dbus_message_is_method_call (message, GS_INTERFACE, "GetSessionIdleTime")) { |
| 1027 |
gs_debug ("Received GetSessionIdleTime request"); |
| 1028 |
return listener_get_info (listener, connection, message, IDLE_TIME); |
| 1029 |
} |
| 1030 |
if (dbus_message_is_method_call (message, GS_INTERFACE, "SimulateUserActivity")) { |
| 1031 |
gs_debug ("Received SimulateUserActivity request"); |
| 1032 |
g_signal_emit (listener, signals [SIMULATE_USER_ACTIVITY], 0); |
| 1033 |
return send_success_reply (connection, message); |
| 1034 |
} |
| 1035 |
if (dbus_message_is_method_call (message, GS_INTERFACE, "Inhibit")) { |
| 1036 |
gs_debug ("Received Inhibit request"); |
| 1037 |
return listener_inhibit (listener, connection, message); |
| 1038 |
} |
| 1039 |
if (dbus_message_is_method_call (message, GS_INTERFACE, "UnInhibit")) { |
| 1040 |
gs_debug ("Received UnInhibit request"); |
| 1041 |
return listener_uninhibit (listener, connection, message); |
| 1042 |
} |
| 1043 |
if (dbus_message_is_method_call (message, GS_INTERFACE_GNOME, "Lock")) { |
| 1044 |
gs_debug ("Received Lock request"); |
| 1045 |
g_signal_emit (listener, signals [LOCK], 0); |
| 1046 |
return send_success_reply (connection, message); |
| 1047 |
} |
| 1048 |
if (dbus_message_is_method_call (message, GS_INTERFACE_GNOME, "SetActive")) { |
| 1049 |
gs_debug ("Received SetActive request"); |
| 1050 |
listener_set_active (listener, connection, message, FALSE); |
| 1051 |
return send_success_reply (connection, message); |
| 1052 |
} |
| 1053 |
if (dbus_message_is_method_call (message, GS_INTERFACE_GNOME, "GetActive")) { |
| 1054 |
gs_debug ("Received GetActive request: %d", listener->priv->blanked); |
| 1055 |
return listener_get_bool (listener, connection, message, listener->priv->blanked); |
| 1056 |
} |
| 1057 |
if (dbus_message_is_method_call (message, GS_INTERFACE_GNOME, "GetActiveTime")) { |
| 1058 |
gs_debug ("Received GetActiveTime request: %d", listener->priv->blanked_start); |
| 1059 |
return listener_get_time (listener, connection, message, listener->priv->blanked_start); |
| 1060 |
} |
| 1061 |
if (dbus_message_is_method_call (message, GS_INTERFACE_GNOME, "SimulateUserActivity")) { |
| 1062 |
gs_debug ("Received SimulateUserActivity request"); |
| 1063 |
g_signal_emit (listener, signals [SIMULATE_USER_ACTIVITY], 0); |
| 1064 |
return send_success_reply (connection, message); |
| 1065 |
} |
| 1066 |
if (dbus_message_is_method_call (message, DBUS_INTROSPECTABLE_INTERFACE, "Introspect")) { |
| 1067 |
return do_introspect (connection, message, local_interface); |
| 1068 |
} |
| 1069 |
|
| 1070 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 1071 |
} |
| 1072 |
|
| 1073 |
static void |
| 1074 |
listener_dbus_handle_owner_changed (GSListener *listener, |
| 1075 |
DBusConnection *connection, |
| 1076 |
DBusMessage *message) |
| 1077 |
{ |
| 1078 |
DBusMessageIter iter; |
| 1079 |
int type; |
| 1080 |
const char *old_owner; |
| 1081 |
|
| 1082 |
dbus_message_iter_init (message, &iter); |
| 1083 |
dbus_message_iter_next (&iter); |
| 1084 |
type = dbus_message_iter_get_arg_type (&iter); |
| 1085 |
|
| 1086 |
if (type != DBUS_TYPE_STRING) { |
| 1087 |
gs_debug ("Invalid NameOwnerChanged message"); |
| 1088 |
return; |
| 1089 |
} |
| 1090 |
|
| 1091 |
dbus_message_iter_get_basic (&iter, &old_owner); |
| 1092 |
|
| 1093 |
gs_listener_disonnect_inhibit (listener, old_owner); |
| 1094 |
} |
| 1095 |
|
| 1096 |
static gboolean |
| 1097 |
_listener_message_path_is_our_session (GSListener *listener, |
| 1098 |
DBusMessage *message) |
| 1099 |
{ |
| 1100 |
const char *ssid; |
| 1101 |
|
| 1102 |
ssid = dbus_message_get_path (message); |
| 1103 |
if (ssid == NULL) |
| 1104 |
return FALSE; |
| 1105 |
|
| 1106 |
if (listener->priv->session_id == NULL) |
| 1107 |
return FALSE; |
| 1108 |
|
| 1109 |
if (strcmp (ssid, listener->priv->session_id) == 0) |
| 1110 |
return TRUE; |
| 1111 |
|
| 1112 |
return FALSE; |
| 1113 |
} |
| 1114 |
|
| 1115 |
#ifdef WITH_SYSTEMD |
| 1116 |
static gboolean |
| 1117 |
query_session_active (GSListener *listener) |
| 1118 |
{ |
| 1119 |
DBusMessage *message; |
| 1120 |
DBusMessage *reply; |
| 1121 |
DBusError error; |
| 1122 |
DBusMessageIter reply_iter; |
| 1123 |
DBusMessageIter sub_iter; |
| 1124 |
dbus_bool_t active = FALSE; |
| 1125 |
const char *interface; |
| 1126 |
const char *property; |
| 1127 |
|
| 1128 |
if (listener->priv->system_connection == NULL) { |
| 1129 |
gs_debug ("No connection to the system bus"); |
| 1130 |
return FALSE; |
| 1131 |
} |
| 1132 |
|
| 1133 |
dbus_error_init (&error); |
| 1134 |
|
| 1135 |
message = dbus_message_new_method_call (SYSTEMD_LOGIND_SERVICE, listener->priv->session_id, DBUS_PROPERTIES_INTERFACE, "Get"); |
| 1136 |
if (message == NULL) { |
| 1137 |
gs_debug ("Couldn't allocate the dbus message"); |
| 1138 |
return FALSE; |
| 1139 |
} |
| 1140 |
|
| 1141 |
interface = SYSTEMD_LOGIND_SESSION_INTERFACE; |
| 1142 |
property = "Active"; |
| 1143 |
|
| 1144 |
if (dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID) == FALSE) { |
| 1145 |
gs_debug ("Couldn't add args to the dbus message"); |
| 1146 |
dbus_message_unref (message); |
| 1147 |
return FALSE; |
| 1148 |
} |
| 1149 |
|
| 1150 |
/* FIXME: use async? */ |
| 1151 |
reply = dbus_connection_send_with_reply_and_block (listener->priv->system_connection, |
| 1152 |
message, |
| 1153 |
-1, &error); |
| 1154 |
dbus_message_unref (message); |
| 1155 |
|
| 1156 |
if (dbus_error_is_set (&error)) { |
| 1157 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 1158 |
dbus_error_free (&error); |
| 1159 |
return FALSE; |
| 1160 |
} |
| 1161 |
|
| 1162 |
if (dbus_message_iter_init (reply, &reply_iter) == TRUE |
| 1163 |
&& dbus_message_iter_get_arg_type (&reply_iter) == DBUS_TYPE_VARIANT) { |
| 1164 |
|
| 1165 |
dbus_message_iter_recurse (&reply_iter, &sub_iter); |
| 1166 |
|
| 1167 |
if (dbus_message_iter_get_arg_type (&sub_iter) == DBUS_TYPE_BOOLEAN) { |
| 1168 |
dbus_message_iter_get_basic (&sub_iter, &active); |
| 1169 |
} else { |
| 1170 |
gs_debug ("Unexpected return type"); |
| 1171 |
} |
| 1172 |
} else { |
| 1173 |
gs_debug ("Unexpected return type"); |
| 1174 |
} |
| 1175 |
|
| 1176 |
dbus_message_unref (reply); |
| 1177 |
|
| 1178 |
return active; |
| 1179 |
} |
| 1180 |
#endif |
| 1181 |
|
| 1182 |
#ifdef WITH_UPOWER |
| 1183 |
#ifdef WITH_LOCK_ON_LID |
| 1184 |
static gboolean |
| 1185 |
query_lid_closed (GSListener *listener) |
| 1186 |
{ |
| 1187 |
DBusMessage *message; |
| 1188 |
DBusMessage *reply; |
| 1189 |
DBusError error; |
| 1190 |
DBusMessageIter reply_iter; |
| 1191 |
DBusMessageIter sub_iter; |
| 1192 |
dbus_bool_t active = FALSE; |
| 1193 |
const char *interface; |
| 1194 |
const char *property; |
| 1195 |
|
| 1196 |
if (listener->priv->system_connection == NULL) { |
| 1197 |
gs_debug ("No connection to the system bus"); |
| 1198 |
return FALSE; |
| 1199 |
} |
| 1200 |
|
| 1201 |
dbus_error_init (&error); |
| 1202 |
|
| 1203 |
message = dbus_message_new_method_call (UP_SERVICE, UP_PATH, DBUS_PROPERTIES_INTERFACE, "Get"); |
| 1204 |
if (message == NULL) { |
| 1205 |
gs_debug ("Couldn't allocate the dbus message"); |
| 1206 |
return FALSE; |
| 1207 |
} |
| 1208 |
|
| 1209 |
interface = UP_INTERFACE; |
| 1210 |
property = "LidIsClosed"; |
| 1211 |
|
| 1212 |
if (dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID) == FALSE) { |
| 1213 |
gs_debug ("Couldn't add args to the dbus message"); |
| 1214 |
dbus_message_unref (message); |
| 1215 |
return FALSE; |
| 1216 |
} |
| 1217 |
|
| 1218 |
/* FIXME: use async? */ |
| 1219 |
reply = dbus_connection_send_with_reply_and_block (listener->priv->system_connection, |
| 1220 |
message, |
| 1221 |
-1, &error); |
| 1222 |
dbus_message_unref (message); |
| 1223 |
|
| 1224 |
if (dbus_error_is_set (&error)) { |
| 1225 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 1226 |
dbus_error_free (&error); |
| 1227 |
return FALSE; |
| 1228 |
} |
| 1229 |
|
| 1230 |
if (dbus_message_iter_init (reply, &reply_iter) == TRUE |
| 1231 |
&& dbus_message_iter_get_arg_type (&reply_iter) == DBUS_TYPE_VARIANT) { |
| 1232 |
|
| 1233 |
dbus_message_iter_recurse (&reply_iter, &sub_iter); |
| 1234 |
|
| 1235 |
if (dbus_message_iter_get_arg_type (&sub_iter) == DBUS_TYPE_BOOLEAN) { |
| 1236 |
dbus_message_iter_get_basic (&sub_iter, &active); |
| 1237 |
} else { |
| 1238 |
gs_debug ("Unexpected return type"); |
| 1239 |
} |
| 1240 |
} else { |
| 1241 |
gs_debug ("Unexpected return type"); |
| 1242 |
} |
| 1243 |
|
| 1244 |
dbus_message_unref (reply); |
| 1245 |
|
| 1246 |
return active; |
| 1247 |
} |
| 1248 |
#endif |
| 1249 |
#endif |
| 1250 |
|
| 1251 |
#if defined(WITH_SYSTEMD) || (defined(WITH_UPOWER) && defined(WITH_LOCK_ON_LID)) |
| 1252 |
static gboolean |
| 1253 |
properties_changed_match (DBusMessage *message, |
| 1254 |
const char *property) |
| 1255 |
{ |
| 1256 |
DBusMessageIter iter, sub, sub2; |
| 1257 |
|
| 1258 |
/* Checks whether a certain property is listed in the |
| 1259 |
* specified PropertiesChanged message */ |
| 1260 |
|
| 1261 |
if (!dbus_message_iter_init (message, &iter)) |
| 1262 |
goto failure; |
| 1263 |
|
| 1264 |
/* Jump over interface name */ |
| 1265 |
if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING) |
| 1266 |
goto failure; |
| 1267 |
|
| 1268 |
if (dbus_message_iter_next (&iter) == FALSE) |
| 1269 |
goto failure; |
| 1270 |
|
| 1271 |
/* First, iterate through the changed properties array */ |
| 1272 |
if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY || |
| 1273 |
dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_DICT_ENTRY) |
| 1274 |
goto failure; |
| 1275 |
|
| 1276 |
dbus_message_iter_recurse (&iter, &sub); |
| 1277 |
while (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_INVALID) { |
| 1278 |
const char *name = NULL; |
| 1279 |
|
| 1280 |
if (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_DICT_ENTRY) |
| 1281 |
goto failure; |
| 1282 |
|
| 1283 |
dbus_message_iter_recurse (&sub, &sub2); |
| 1284 |
|
| 1285 |
if (dbus_message_iter_get_arg_type (&sub2) != DBUS_TYPE_STRING) |
| 1286 |
goto failure; |
| 1287 |
|
| 1288 |
dbus_message_iter_get_basic (&sub2, &name); |
| 1289 |
|
| 1290 |
if (g_strcmp0 (name, property) == 0) |
| 1291 |
return TRUE; |
| 1292 |
|
| 1293 |
dbus_message_iter_next (&sub); |
| 1294 |
} |
| 1295 |
|
| 1296 |
if (dbus_message_iter_next (&iter) == FALSE) |
| 1297 |
goto failure; |
| 1298 |
|
| 1299 |
/* Second, iterate through the invalidated properties array */ |
| 1300 |
if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY || |
| 1301 |
dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING) |
| 1302 |
goto failure; |
| 1303 |
|
| 1304 |
dbus_message_iter_recurse (&iter, &sub); |
| 1305 |
while (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_INVALID) { |
| 1306 |
const char *name = NULL; |
| 1307 |
|
| 1308 |
if (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_STRING) |
| 1309 |
goto failure; |
| 1310 |
|
| 1311 |
dbus_message_iter_get_basic (&sub, &name); |
| 1312 |
|
| 1313 |
if (g_strcmp0 (name, property) == 0) |
| 1314 |
return TRUE; |
| 1315 |
|
| 1316 |
dbus_message_iter_next (&sub); |
| 1317 |
} |
| 1318 |
|
| 1319 |
return FALSE; |
| 1320 |
|
| 1321 |
failure: |
| 1322 |
gs_debug ("Failed to decode PropertiesChanged message."); |
| 1323 |
return FALSE; |
| 1324 |
} |
| 1325 |
#endif |
| 1326 |
|
| 1327 |
static DBusHandlerResult |
| 1328 |
listener_dbus_handle_system_message (DBusConnection *connection, |
| 1329 |
DBusMessage *message, |
| 1330 |
void *user_data, |
| 1331 |
dbus_bool_t local_interface) |
| 1332 |
{ |
| 1333 |
GSListener *listener = GS_LISTENER (user_data); |
| 1334 |
|
| 1335 |
g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
| 1336 |
g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
| 1337 |
|
| 1338 |
#if 1 |
| 1339 |
gs_debug ("obj_path=%s interface=%s method=%s destination=%s", |
| 1340 |
dbus_message_get_path (message), |
| 1341 |
dbus_message_get_interface (message), |
| 1342 |
dbus_message_get_member (message), |
| 1343 |
dbus_message_get_destination (message)); |
| 1344 |
#endif |
| 1345 |
|
| 1346 |
#ifdef WITH_SYSTEMD |
| 1347 |
|
| 1348 |
if (listener->priv->have_systemd) { |
| 1349 |
|
| 1350 |
if (dbus_message_is_signal (message, SYSTEMD_LOGIND_SESSION_INTERFACE, "Unlock")) { |
| 1351 |
if (_listener_message_path_is_our_session (listener, message)) { |
| 1352 |
gs_debug ("systemd requested session unlock"); |
| 1353 |
gs_listener_set_active (listener, FALSE); |
| 1354 |
} |
| 1355 |
|
| 1356 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1357 |
} else if (dbus_message_is_signal (message, SYSTEMD_LOGIND_SESSION_INTERFACE, "Lock")) { |
| 1358 |
if (_listener_message_path_is_our_session (listener, message)) { |
| 1359 |
gs_debug ("systemd requested session lock"); |
| 1360 |
g_signal_emit (listener, signals [LOCKED], 0); |
| 1361 |
} |
| 1362 |
|
| 1363 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1364 |
} else if (dbus_message_is_signal (message, DBUS_INTERFACE_PROPERTIES, "PropertiesChanged")) { |
| 1365 |
|
| 1366 |
/* Use the seat property ActiveSession. |
| 1367 |
* The session property Active only seems to be signalled when it becomes active. |
| 1368 |
*/ |
| 1369 |
if (properties_changed_match (message, "ActiveSession")) { |
| 1370 |
gboolean new_active; |
| 1371 |
|
| 1372 |
/* Do a DBus query, since the sd_session_is_active isn't up to date. */ |
| 1373 |
new_active = query_session_active (listener); |
| 1374 |
gs_debug ("systemd notified ActiveSession %d", new_active); |
| 1375 |
g_signal_emit (listener, signals [SESSION_SWITCHED], 0, new_active); |
| 1376 |
} |
| 1377 |
|
| 1378 |
#ifdef WITH_UPOWER |
| 1379 |
#ifdef WITH_LOCK_ON_LID |
| 1380 |
if (properties_changed_match (message, "LidIsClosed")) { |
| 1381 |
listener->priv->lid_closed = query_lid_closed (listener); |
| 1382 |
gs_debug ("UPower notified LidIsClosed %d", (int)listener->priv->lid_closed); |
| 1383 |
g_object_notify (G_OBJECT (listener), "lid-closed"); |
| 1384 |
} |
| 1385 |
#endif |
| 1386 |
#endif |
| 1387 |
|
| 1388 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1389 |
} |
| 1390 |
|
| 1391 |
#ifdef WITH_LOCK_ON_SUSPEND |
| 1392 |
if (dbus_message_is_signal (message, SYSTEMD_LOGIND_INTERFACE, "PrepareForSleep")) { |
| 1393 |
DBusError error; |
| 1394 |
dbus_bool_t new_active; |
| 1395 |
|
| 1396 |
dbus_error_init (&error); |
| 1397 |
if (dbus_message_get_args (message, &error, |
| 1398 |
DBUS_TYPE_BOOLEAN, &new_active, |
| 1399 |
DBUS_TYPE_INVALID)) { |
| 1400 |
gs_debug ("systemd initiating %s", new_active ? "sleep" : "resume"); |
| 1401 |
|
| 1402 |
g_signal_emit (listener, signals [new_active ? SUSPEND : RESUME], 0); |
| 1403 |
} |
| 1404 |
|
| 1405 |
if (dbus_error_is_set (&error)) { |
| 1406 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 1407 |
dbus_error_free (&error); |
| 1408 |
} |
| 1409 |
|
| 1410 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1411 |
} |
| 1412 |
#endif |
| 1413 |
|
| 1414 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 1415 |
} |
| 1416 |
#endif |
| 1417 |
|
| 1418 |
#ifdef WITH_CONSOLE_KIT |
| 1419 |
if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "Unlock")) { |
| 1420 |
if (_listener_message_path_is_our_session (listener, message)) { |
| 1421 |
gs_debug ("Console kit requested session unlock"); |
| 1422 |
gs_listener_set_active (listener, FALSE); |
| 1423 |
} |
| 1424 |
|
| 1425 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1426 |
} else if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "Lock")) { |
| 1427 |
if (_listener_message_path_is_our_session (listener, message)) { |
| 1428 |
gs_debug ("ConsoleKit requested session lock"); |
| 1429 |
g_signal_emit (listener, signals [LOCKED], 0); |
| 1430 |
} |
| 1431 |
|
| 1432 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1433 |
} else if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "ActiveChanged")) { |
| 1434 |
/* NB that `ActiveChanged' refers to the active |
| 1435 |
* session in ConsoleKit terminology - ie which |
| 1436 |
* session is currently displayed on the screen. |
| 1437 |
* light-locker uses `active' to mean `is the |
| 1438 |
* screensaver active' (ie, is the screen locked) but |
| 1439 |
* that's not what we're referring to here. |
| 1440 |
*/ |
| 1441 |
|
| 1442 |
if (_listener_message_path_is_our_session (listener, message)) { |
| 1443 |
DBusError error; |
| 1444 |
dbus_bool_t new_active; |
| 1445 |
|
| 1446 |
dbus_error_init (&error); |
| 1447 |
if (dbus_message_get_args (message, &error, |
| 1448 |
DBUS_TYPE_BOOLEAN, &new_active, |
| 1449 |
DBUS_TYPE_INVALID)) { |
| 1450 |
gs_debug ("ConsoleKit notified ActiveChanged %d", new_active); |
| 1451 |
|
| 1452 |
g_signal_emit (listener, signals [SESSION_SWITCHED], 0, new_active); |
| 1453 |
} |
| 1454 |
|
| 1455 |
if (dbus_error_is_set (&error)) { |
| 1456 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 1457 |
dbus_error_free (&error); |
| 1458 |
} |
| 1459 |
} |
| 1460 |
|
| 1461 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1462 |
} |
| 1463 |
#endif |
| 1464 |
|
| 1465 |
#ifdef WITH_UPOWER |
| 1466 |
#ifdef WITH_LOCK_ON_SUSPEND |
| 1467 |
if (dbus_message_is_signal (message, UP_INTERFACE, "Sleeping")) { |
| 1468 |
gs_debug ("UPower initiating sleep"); |
| 1469 |
g_signal_emit (listener, signals [SUSPEND], 0); |
| 1470 |
|
| 1471 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1472 |
} else if (dbus_message_is_signal (message, UP_INTERFACE, "Resuming")) { |
| 1473 |
gs_debug ("UPower initiating resume"); |
| 1474 |
g_signal_emit (listener, signals [RESUME], 0); |
| 1475 |
|
| 1476 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1477 |
} |
| 1478 |
#endif |
| 1479 |
#ifdef WITH_LOCK_ON_LID |
| 1480 |
if (dbus_message_is_signal (message, DBUS_INTERFACE_PROPERTIES, "PropertiesChanged")) { |
| 1481 |
|
| 1482 |
if (properties_changed_match (message, "LidIsClosed")) { |
| 1483 |
listener->priv->lid_closed = query_lid_closed (listener); |
| 1484 |
gs_debug ("UPower notified LidIsClosed %d", (int)listener->priv->lid_closed); |
| 1485 |
g_object_notify (G_OBJECT (listener), "lid-closed"); |
| 1486 |
} |
| 1487 |
|
| 1488 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1489 |
} |
| 1490 |
#endif |
| 1491 |
#endif |
| 1492 |
|
| 1493 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 1494 |
} |
| 1495 |
|
| 1496 |
static DBusHandlerResult |
| 1497 |
gs_listener_message_handler (DBusConnection *connection, |
| 1498 |
DBusMessage *message, |
| 1499 |
void *user_data) |
| 1500 |
{ |
| 1501 |
g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
| 1502 |
g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
| 1503 |
|
| 1504 |
#if 0 |
| 1505 |
g_message ("obj_path=%s interface=%s method=%s destination=%s", |
| 1506 |
dbus_message_get_path (message), |
| 1507 |
dbus_message_get_interface (message), |
| 1508 |
dbus_message_get_member (message), |
| 1509 |
dbus_message_get_destination (message)); |
| 1510 |
#endif |
| 1511 |
|
| 1512 |
if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") && |
| 1513 |
strcmp (dbus_message_get_path (message), DBUS_PATH_LOCAL) == 0) { |
| 1514 |
dbus_connection_unref (connection); |
| 1515 |
|
| 1516 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1517 |
} else { |
| 1518 |
return listener_dbus_handle_session_message (GS_LISTENER (user_data), connection, message, TRUE); |
| 1519 |
} |
| 1520 |
} |
| 1521 |
|
| 1522 |
static gboolean |
| 1523 |
gs_listener_dbus_init (GSListener *listener) |
| 1524 |
{ |
| 1525 |
DBusError error; |
| 1526 |
|
| 1527 |
dbus_error_init (&error); |
| 1528 |
|
| 1529 |
if (listener->priv->connection == NULL) { |
| 1530 |
listener->priv->connection = dbus_bus_get (DBUS_BUS_SESSION, &error); |
| 1531 |
if (listener->priv->connection == NULL) { |
| 1532 |
if (dbus_error_is_set (&error)) { |
| 1533 |
gs_debug ("couldn't connect to session bus: %s", |
| 1534 |
error.message); |
| 1535 |
dbus_error_free (&error); |
| 1536 |
} |
| 1537 |
return FALSE; |
| 1538 |
} |
| 1539 |
|
| 1540 |
dbus_connection_setup_with_g_main (listener->priv->connection, NULL); |
| 1541 |
dbus_connection_set_exit_on_disconnect (listener->priv->connection, FALSE); |
| 1542 |
} |
| 1543 |
|
| 1544 |
if (listener->priv->system_connection == NULL) { |
| 1545 |
listener->priv->system_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error); |
| 1546 |
if (listener->priv->system_connection == NULL) { |
| 1547 |
if (dbus_error_is_set (&error)) { |
| 1548 |
gs_debug ("couldn't connect to system bus: %s", |
| 1549 |
error.message); |
| 1550 |
dbus_error_free (&error); |
| 1551 |
} |
| 1552 |
return FALSE; |
| 1553 |
} |
| 1554 |
|
| 1555 |
dbus_connection_setup_with_g_main (listener->priv->system_connection, NULL); |
| 1556 |
dbus_connection_set_exit_on_disconnect (listener->priv->system_connection, FALSE); |
| 1557 |
} |
| 1558 |
|
| 1559 |
return TRUE; |
| 1560 |
} |
| 1561 |
|
| 1562 |
static gboolean |
| 1563 |
reinit_dbus (GSListener *listener) |
| 1564 |
{ |
| 1565 |
gboolean initialized; |
| 1566 |
gboolean try_again; |
| 1567 |
|
| 1568 |
initialized = gs_listener_dbus_init (listener); |
| 1569 |
|
| 1570 |
/* if we didn't initialize then try again */ |
| 1571 |
/* FIXME: Should we keep trying forever? If we fail more than |
| 1572 |
once or twice then the session bus may have died. The |
| 1573 |
problem is that if it is restarted it will likely have a |
| 1574 |
different bus address and we won't be able to find it */ |
| 1575 |
try_again = !initialized; |
| 1576 |
|
| 1577 |
return try_again; |
| 1578 |
} |
| 1579 |
|
| 1580 |
static DBusHandlerResult |
| 1581 |
listener_dbus_filter_function (DBusConnection *connection, |
| 1582 |
DBusMessage *message, |
| 1583 |
void *user_data) |
| 1584 |
{ |
| 1585 |
GSListener *listener = GS_LISTENER (user_data); |
| 1586 |
const char *path; |
| 1587 |
|
| 1588 |
path = dbus_message_get_path (message); |
| 1589 |
|
| 1590 |
if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") |
| 1591 |
&& strcmp (path, DBUS_PATH_LOCAL) == 0) { |
| 1592 |
|
| 1593 |
g_message ("Got disconnected from the session message bus; " |
| 1594 |
"retrying to reconnect every 10 seconds"); |
| 1595 |
|
| 1596 |
dbus_connection_unref (connection); |
| 1597 |
listener->priv->connection = NULL; |
| 1598 |
|
| 1599 |
g_timeout_add (10000, (GSourceFunc)reinit_dbus, listener); |
| 1600 |
} else if (dbus_message_is_signal (message, |
| 1601 |
DBUS_INTERFACE_DBUS, |
| 1602 |
"NameOwnerChanged")) { |
| 1603 |
if (g_hash_table_size (listener->priv->inhibit_list) > 0) |
| 1604 |
listener_dbus_handle_owner_changed (listener, connection, message); |
| 1605 |
} else { |
| 1606 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1611 |
} |
| 1612 |
|
| 1613 |
static DBusHandlerResult |
| 1614 |
listener_dbus_system_filter_function (DBusConnection *connection, |
| 1615 |
DBusMessage *message, |
| 1616 |
void *user_data) |
| 1617 |
{ |
| 1618 |
GSListener *listener = GS_LISTENER (user_data); |
| 1619 |
const char *path; |
| 1620 |
|
| 1621 |
path = dbus_message_get_path (message); |
| 1622 |
|
| 1623 |
if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") == TRUE |
| 1624 |
&& g_strcmp0 (path, DBUS_PATH_LOCAL) == 0) { |
| 1625 |
|
| 1626 |
g_message ("Got disconnected from the system message bus; " |
| 1627 |
"retrying to reconnect every 10 seconds"); |
| 1628 |
|
| 1629 |
dbus_connection_unref (connection); |
| 1630 |
listener->priv->system_connection = NULL; |
| 1631 |
|
| 1632 |
g_timeout_add (10000, (GSourceFunc)reinit_dbus, listener); |
| 1633 |
} else { |
| 1634 |
return listener_dbus_handle_system_message (connection, message, user_data, FALSE); |
| 1635 |
} |
| 1636 |
|
| 1637 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 1638 |
} |
| 1639 |
|
| 1640 |
static void |
| 1641 |
gs_listener_set_property (GObject *object, |
| 1642 |
guint prop_id, |
| 1643 |
const GValue *value, |
| 1644 |
GParamSpec *pspec) |
| 1645 |
{ |
| 1646 |
GSListener *self; |
| 1647 |
|
| 1648 |
self = GS_LISTENER (object); |
| 1649 |
|
| 1650 |
switch (prop_id) { |
| 1651 |
case PROP_ACTIVE: |
| 1652 |
gs_listener_set_active (self, g_value_get_boolean (value)); |
| 1653 |
break; |
| 1654 |
default: |
| 1655 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 1656 |
break; |
| 1657 |
} |
| 1658 |
} |
| 1659 |
|
| 1660 |
static void |
| 1661 |
gs_listener_get_property (GObject *object, |
| 1662 |
guint prop_id, |
| 1663 |
GValue *value, |
| 1664 |
GParamSpec *pspec) |
| 1665 |
{ |
| 1666 |
GSListener *self; |
| 1667 |
|
| 1668 |
self = GS_LISTENER (object); |
| 1669 |
|
| 1670 |
switch (prop_id) { |
| 1671 |
case PROP_ACTIVE: |
| 1672 |
g_value_set_boolean (value, self->priv->active); |
| 1673 |
break; |
| 1674 |
case PROP_LID_CLOSED: |
| 1675 |
g_value_set_boolean (value, self->priv->lid_closed); |
| 1676 |
break; |
| 1677 |
default: |
| 1678 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 1679 |
break; |
| 1680 |
} |
| 1681 |
} |
| 1682 |
|
| 1683 |
static void |
| 1684 |
gs_listener_class_init (GSListenerClass *klass) |
| 1685 |
{ |
| 1686 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 1687 |
|
| 1688 |
object_class->finalize = gs_listener_finalize; |
| 1689 |
object_class->get_property = gs_listener_get_property; |
| 1690 |
object_class->set_property = gs_listener_set_property; |
| 1691 |
|
| 1692 |
signals [LOCK] = |
| 1693 |
g_signal_new ("lock", |
| 1694 |
G_TYPE_FROM_CLASS (object_class), |
| 1695 |
G_SIGNAL_RUN_LAST, |
| 1696 |
G_STRUCT_OFFSET (GSListenerClass, lock), |
| 1697 |
NULL, |
| 1698 |
NULL, |
| 1699 |
g_cclosure_marshal_VOID__VOID, |
| 1700 |
G_TYPE_NONE, |
| 1701 |
0); |
| 1702 |
signals [LOCKED] = |
| 1703 |
g_signal_new ("locked", |
| 1704 |
G_TYPE_FROM_CLASS (object_class), |
| 1705 |
G_SIGNAL_RUN_LAST, |
| 1706 |
G_STRUCT_OFFSET (GSListenerClass, locked), |
| 1707 |
NULL, |
| 1708 |
NULL, |
| 1709 |
g_cclosure_marshal_VOID__VOID, |
| 1710 |
G_TYPE_NONE, |
| 1711 |
0); |
| 1712 |
signals [SESSION_SWITCHED] = |
| 1713 |
g_signal_new ("session-switched", |
| 1714 |
G_TYPE_FROM_CLASS (object_class), |
| 1715 |
G_SIGNAL_RUN_LAST, |
| 1716 |
G_STRUCT_OFFSET (GSListenerClass, session_switched), |
| 1717 |
NULL, |
| 1718 |
NULL, |
| 1719 |
g_cclosure_marshal_VOID__BOOLEAN, |
| 1720 |
G_TYPE_NONE, |
| 1721 |
1, |
| 1722 |
G_TYPE_BOOLEAN); |
| 1723 |
signals [ACTIVE_CHANGED] = |
| 1724 |
g_signal_new ("active-changed", |
| 1725 |
G_TYPE_FROM_CLASS (object_class), |
| 1726 |
G_SIGNAL_RUN_LAST, |
| 1727 |
G_STRUCT_OFFSET (GSListenerClass, active_changed), |
| 1728 |
NULL, |
| 1729 |
NULL, |
| 1730 |
gs_marshal_BOOLEAN__BOOLEAN, |
| 1731 |
G_TYPE_BOOLEAN, |
| 1732 |
1, |
| 1733 |
G_TYPE_BOOLEAN); |
| 1734 |
signals [SUSPEND] = |
| 1735 |
g_signal_new ("suspend", |
| 1736 |
G_TYPE_FROM_CLASS (object_class), |
| 1737 |
G_SIGNAL_RUN_LAST, |
| 1738 |
G_STRUCT_OFFSET (GSListenerClass, suspend), |
| 1739 |
NULL, |
| 1740 |
NULL, |
| 1741 |
g_cclosure_marshal_VOID__VOID, |
| 1742 |
G_TYPE_NONE, |
| 1743 |
0); |
| 1744 |
signals [RESUME] = |
| 1745 |
g_signal_new ("resume", |
| 1746 |
G_TYPE_FROM_CLASS (object_class), |
| 1747 |
G_SIGNAL_RUN_LAST, |
| 1748 |
G_STRUCT_OFFSET (GSListenerClass, resume), |
| 1749 |
NULL, |
| 1750 |
NULL, |
| 1751 |
g_cclosure_marshal_VOID__VOID, |
| 1752 |
G_TYPE_NONE, |
| 1753 |
0); |
| 1754 |
signals [SIMULATE_USER_ACTIVITY] = |
| 1755 |
g_signal_new ("simulate-user-activity", |
| 1756 |
G_TYPE_FROM_CLASS (object_class), |
| 1757 |
G_SIGNAL_RUN_LAST, |
| 1758 |
G_STRUCT_OFFSET (GSListenerClass, simulate_user_activity), |
| 1759 |
NULL, |
| 1760 |
NULL, |
| 1761 |
g_cclosure_marshal_VOID__VOID, |
| 1762 |
G_TYPE_NONE, |
| 1763 |
0); |
| 1764 |
signals [BLANKING] = |
| 1765 |
g_signal_new ("blanking", |
| 1766 |
G_TYPE_FROM_CLASS (object_class), |
| 1767 |
G_SIGNAL_RUN_LAST, |
| 1768 |
G_STRUCT_OFFSET (GSListenerClass, blanking), |
| 1769 |
NULL, |
| 1770 |
NULL, |
| 1771 |
gs_marshal_BOOLEAN__BOOLEAN, |
| 1772 |
G_TYPE_BOOLEAN, |
| 1773 |
1, |
| 1774 |
G_TYPE_BOOLEAN); |
| 1775 |
signals [INHIBIT] = |
| 1776 |
g_signal_new ("inhibit", |
| 1777 |
G_TYPE_FROM_CLASS (object_class), |
| 1778 |
G_SIGNAL_RUN_LAST, |
| 1779 |
G_STRUCT_OFFSET (GSListenerClass, inhibit), |
| 1780 |
NULL, |
| 1781 |
NULL, |
| 1782 |
g_cclosure_marshal_VOID__BOOLEAN, |
| 1783 |
G_TYPE_NONE, |
| 1784 |
1, |
| 1785 |
G_TYPE_BOOLEAN); |
| 1786 |
signals [IDLE_TIME] = |
| 1787 |
g_signal_new ("idle-time", |
| 1788 |
G_TYPE_FROM_CLASS (object_class), |
| 1789 |
G_SIGNAL_RUN_LAST, |
| 1790 |
G_STRUCT_OFFSET (GSListenerClass, idle_time), |
| 1791 |
NULL, |
| 1792 |
NULL, |
| 1793 |
gs_marshal_ULONG__VOID, |
| 1794 |
G_TYPE_ULONG, |
| 1795 |
0); |
| 1796 |
|
| 1797 |
g_object_class_install_property (object_class, |
| 1798 |
PROP_ACTIVE, |
| 1799 |
g_param_spec_boolean ("active", |
| 1800 |
NULL, |
| 1801 |
NULL, |
| 1802 |
FALSE, |
| 1803 |
G_PARAM_READWRITE)); |
| 1804 |
g_object_class_install_property (object_class, |
| 1805 |
PROP_LID_CLOSED, |
| 1806 |
g_param_spec_boolean ("lid-closed", |
| 1807 |
NULL, |
| 1808 |
NULL, |
| 1809 |
FALSE, |
| 1810 |
G_PARAM_READABLE)); |
| 1811 |
|
| 1812 |
g_type_class_add_private (klass, sizeof (GSListenerPrivate)); |
| 1813 |
} |
| 1814 |
|
| 1815 |
|
| 1816 |
static gboolean |
| 1817 |
screensaver_is_running (DBusConnection *connection) |
| 1818 |
{ |
| 1819 |
DBusError error; |
| 1820 |
gboolean exists; |
| 1821 |
|
| 1822 |
g_return_val_if_fail (connection != NULL, FALSE); |
| 1823 |
|
| 1824 |
dbus_error_init (&error); |
| 1825 |
exists = dbus_bus_name_has_owner (connection, GS_SERVICE, &error); |
| 1826 |
if (dbus_error_is_set (&error)) { |
| 1827 |
dbus_error_free (&error); |
| 1828 |
} |
| 1829 |
|
| 1830 |
if (exists == FALSE) { |
| 1831 |
/* Also check for the GNOME screensaver */ |
| 1832 |
dbus_error_init (&error); |
| 1833 |
exists = dbus_bus_name_has_owner (connection, GS_SERVICE_GNOME, &error); |
| 1834 |
if (dbus_error_is_set (&error)) { |
| 1835 |
dbus_error_free (&error); |
| 1836 |
} |
| 1837 |
} |
| 1838 |
|
| 1839 |
return exists; |
| 1840 |
} |
| 1841 |
|
| 1842 |
gboolean |
| 1843 |
gs_listener_acquire (GSListener *listener, |
| 1844 |
GError **error) |
| 1845 |
{ |
| 1846 |
int res; |
| 1847 |
DBusError buserror; |
| 1848 |
gboolean is_connected; |
| 1849 |
|
| 1850 |
g_return_val_if_fail (listener != NULL, FALSE); |
| 1851 |
|
| 1852 |
if (! listener->priv->connection) { |
| 1853 |
g_set_error (error, |
| 1854 |
GS_LISTENER_ERROR, |
| 1855 |
GS_LISTENER_ERROR_ACQUISITION_FAILURE, |
| 1856 |
"%s", |
| 1857 |
_("failed to register with the message bus")); |
| 1858 |
return FALSE; |
| 1859 |
} |
| 1860 |
|
| 1861 |
is_connected = dbus_connection_get_is_connected (listener->priv->connection); |
| 1862 |
if (! is_connected) { |
| 1863 |
g_set_error (error, |
| 1864 |
GS_LISTENER_ERROR, |
| 1865 |
GS_LISTENER_ERROR_ACQUISITION_FAILURE, |
| 1866 |
"%s", |
| 1867 |
_("not connected to the message bus")); |
| 1868 |
return FALSE; |
| 1869 |
} |
| 1870 |
|
| 1871 |
if (screensaver_is_running (listener->priv->connection)) { |
| 1872 |
g_set_error (error, |
| 1873 |
GS_LISTENER_ERROR, |
| 1874 |
GS_LISTENER_ERROR_ACQUISITION_FAILURE, |
| 1875 |
"%s", |
| 1876 |
_("screensaver already running in this session")); |
| 1877 |
return FALSE; |
| 1878 |
} |
| 1879 |
|
| 1880 |
dbus_error_init (&buserror); |
| 1881 |
|
| 1882 |
if (dbus_connection_register_object_path (listener->priv->connection, |
| 1883 |
GS_PATH, |
| 1884 |
&gs_listener_vtable, |
| 1885 |
listener) == FALSE) { |
| 1886 |
g_critical ("out of memory registering object path"); |
| 1887 |
return FALSE; |
| 1888 |
} |
| 1889 |
|
| 1890 |
/* Register KDE path */ |
| 1891 |
if (dbus_connection_register_object_path (listener->priv->connection, |
| 1892 |
GS_PATH_KDE, |
| 1893 |
&gs_listener_vtable, |
| 1894 |
listener) == FALSE) { |
| 1895 |
g_critical ("out of memory registering object path"); |
| 1896 |
return FALSE; |
| 1897 |
} |
| 1898 |
|
| 1899 |
/* Register GNOME interface */ |
| 1900 |
if (dbus_connection_register_object_path (listener->priv->connection, |
| 1901 |
GS_PATH_GNOME, |
| 1902 |
&gs_listener_vtable, |
| 1903 |
listener) == FALSE) { |
| 1904 |
g_critical ("out of memory registering object path"); |
| 1905 |
return FALSE; |
| 1906 |
} |
| 1907 |
|
| 1908 |
res = dbus_bus_request_name (listener->priv->connection, |
| 1909 |
GS_SERVICE, |
| 1910 |
DBUS_NAME_FLAG_DO_NOT_QUEUE, |
| 1911 |
&buserror); |
| 1912 |
if (dbus_error_is_set (&buserror)) { |
| 1913 |
g_set_error (error, |
| 1914 |
GS_LISTENER_ERROR, |
| 1915 |
GS_LISTENER_ERROR_ACQUISITION_FAILURE, |
| 1916 |
"%s", |
| 1917 |
buserror.message); |
| 1918 |
} |
| 1919 |
if (res == DBUS_REQUEST_NAME_REPLY_EXISTS) { |
| 1920 |
g_set_error (error, |
| 1921 |
GS_LISTENER_ERROR, |
| 1922 |
GS_LISTENER_ERROR_ACQUISITION_FAILURE, |
| 1923 |
"%s", |
| 1924 |
_("screensaver already running in this session")); |
| 1925 |
return FALSE; |
| 1926 |
} |
| 1927 |
|
| 1928 |
dbus_error_free (&buserror); |
| 1929 |
|
| 1930 |
res = dbus_bus_request_name (listener->priv->connection, |
| 1931 |
GS_SERVICE_GNOME, |
| 1932 |
DBUS_NAME_FLAG_DO_NOT_QUEUE, |
| 1933 |
&buserror); |
| 1934 |
if (dbus_error_is_set (&buserror)) { |
| 1935 |
g_set_error (error, |
| 1936 |
GS_LISTENER_ERROR, |
| 1937 |
GS_LISTENER_ERROR_ACQUISITION_FAILURE, |
| 1938 |
"%s", |
| 1939 |
buserror.message); |
| 1940 |
} |
| 1941 |
if (res == DBUS_REQUEST_NAME_REPLY_EXISTS) { |
| 1942 |
g_set_error (error, |
| 1943 |
GS_LISTENER_ERROR, |
| 1944 |
GS_LISTENER_ERROR_ACQUISITION_FAILURE, |
| 1945 |
"%s", |
| 1946 |
_("GNOME screensaver already running in this session")); |
| 1947 |
return FALSE; |
| 1948 |
} |
| 1949 |
|
| 1950 |
dbus_error_free (&buserror); |
| 1951 |
|
| 1952 |
dbus_connection_add_filter (listener->priv->connection, listener_dbus_filter_function, listener, NULL); |
| 1953 |
|
| 1954 |
dbus_bus_add_match (listener->priv->connection, |
| 1955 |
"type='signal'" |
| 1956 |
",interface='"DBUS_INTERFACE_DBUS"'" |
| 1957 |
",sender='"DBUS_SERVICE_DBUS"'" |
| 1958 |
",member='NameOwnerChanged'", |
| 1959 |
NULL); |
| 1960 |
|
| 1961 |
if (listener->priv->system_connection != NULL) { |
| 1962 |
dbus_connection_add_filter (listener->priv->system_connection, |
| 1963 |
listener_dbus_system_filter_function, |
| 1964 |
listener, |
| 1965 |
NULL); |
| 1966 |
#ifdef WITH_SYSTEMD |
| 1967 |
if (listener->priv->have_systemd) { |
| 1968 |
dbus_bus_add_match (listener->priv->system_connection, |
| 1969 |
"type='signal'" |
| 1970 |
",sender='"SYSTEMD_LOGIND_SERVICE"'" |
| 1971 |
",interface='"SYSTEMD_LOGIND_SESSION_INTERFACE"'" |
| 1972 |
",member='Unlock'", |
| 1973 |
NULL); |
| 1974 |
dbus_bus_add_match (listener->priv->system_connection, |
| 1975 |
"type='signal'" |
| 1976 |
",sender='"SYSTEMD_LOGIND_SERVICE"'" |
| 1977 |
",interface='"SYSTEMD_LOGIND_SESSION_INTERFACE"'" |
| 1978 |
",member='Lock'", |
| 1979 |
NULL); |
| 1980 |
dbus_bus_add_match (listener->priv->system_connection, |
| 1981 |
"type='signal'" |
| 1982 |
",sender='"SYSTEMD_LOGIND_SERVICE"'" |
| 1983 |
",interface='"DBUS_INTERFACE_PROPERTIES"'" |
| 1984 |
",member='PropertiesChanged'", |
| 1985 |
NULL); |
| 1986 |
|
| 1987 |
#ifdef WITH_LOCK_ON_SUSPEND |
| 1988 |
dbus_bus_add_match (listener->priv->system_connection, |
| 1989 |
"type='signal'" |
| 1990 |
",sender='"SYSTEMD_LOGIND_SERVICE"'" |
| 1991 |
",interface='"SYSTEMD_LOGIND_INTERFACE"'" |
| 1992 |
",member='PrepareForSleep'", |
| 1993 |
NULL); |
| 1994 |
#endif |
| 1995 |
|
| 1996 |
#ifdef WITH_UPOWER |
| 1997 |
#ifdef WITH_LOCK_ON_LID |
| 1998 |
dbus_bus_add_match (listener->priv->system_connection, |
| 1999 |
"type='signal'" |
| 2000 |
",sender='"UP_SERVICE"'" |
| 2001 |
",interface='"DBUS_INTERFACE_PROPERTIES"'" |
| 2002 |
",member='PropertiesChanged'", |
| 2003 |
NULL); |
| 2004 |
#endif |
| 2005 |
#endif |
| 2006 |
|
| 2007 |
return (res != -1); |
| 2008 |
} |
| 2009 |
#endif |
| 2010 |
|
| 2011 |
#ifdef WITH_CONSOLE_KIT |
| 2012 |
dbus_bus_add_match (listener->priv->system_connection, |
| 2013 |
"type='signal'" |
| 2014 |
",sender='"CK_SERVICE"'" |
| 2015 |
",interface='"CK_SESSION_INTERFACE"'" |
| 2016 |
",member='Unlock'", |
| 2017 |
NULL); |
| 2018 |
dbus_bus_add_match (listener->priv->system_connection, |
| 2019 |
"type='signal'" |
| 2020 |
",sender='"CK_SERVICE"'" |
| 2021 |
",interface='"CK_SESSION_INTERFACE"'" |
| 2022 |
",member='Lock'", |
| 2023 |
NULL); |
| 2024 |
dbus_bus_add_match (listener->priv->system_connection, |
| 2025 |
"type='signal'" |
| 2026 |
",sender='"CK_SERVICE"'" |
| 2027 |
",interface='"CK_SESSION_INTERFACE"'" |
| 2028 |
",member='ActiveChanged'", |
| 2029 |
NULL); |
| 2030 |
#endif |
| 2031 |
|
| 2032 |
#ifdef WITH_UPOWER |
| 2033 |
#ifdef WITH_LOCK_ON_SUSPEND |
| 2034 |
dbus_bus_add_match (listener->priv->system_connection, |
| 2035 |
"type='signal'" |
| 2036 |
",sender='"UP_SERVICE"'" |
| 2037 |
",interface='"UP_INTERFACE"'" |
| 2038 |
",member='Sleeping'", |
| 2039 |
NULL); |
| 2040 |
dbus_bus_add_match (listener->priv->system_connection, |
| 2041 |
"type='signal'" |
| 2042 |
",sender='"UP_SERVICE"'" |
| 2043 |
",interface='"UP_INTERFACE"'" |
| 2044 |
",member='Resuming'", |
| 2045 |
NULL); |
| 2046 |
#endif |
| 2047 |
#ifdef WITH_LOCK_ON_LID |
| 2048 |
dbus_bus_add_match (listener->priv->system_connection, |
| 2049 |
"type='signal'" |
| 2050 |
",sender='"UP_SERVICE"'" |
| 2051 |
",interface='"DBUS_INTERFACE_PROPERTIES"'" |
| 2052 |
",member='PropertiesChanged'", |
| 2053 |
NULL); |
| 2054 |
#endif |
| 2055 |
#endif |
| 2056 |
} |
| 2057 |
|
| 2058 |
return (res != -1); |
| 2059 |
} |
| 2060 |
|
| 2061 |
static char * |
| 2062 |
query_session_id (GSListener *listener) |
| 2063 |
{ |
| 2064 |
DBusMessage *message; |
| 2065 |
DBusMessage *reply; |
| 2066 |
DBusError error; |
| 2067 |
char *ssid; |
| 2068 |
|
| 2069 |
if (listener->priv->system_connection == NULL) { |
| 2070 |
gs_debug ("No connection to the system bus"); |
| 2071 |
return NULL; |
| 2072 |
} |
| 2073 |
|
| 2074 |
ssid = NULL; |
| 2075 |
|
| 2076 |
dbus_error_init (&error); |
| 2077 |
|
| 2078 |
#ifdef WITH_SYSTEMD |
| 2079 |
if (listener->priv->have_systemd) { |
| 2080 |
dbus_uint32_t pid = getpid(); |
| 2081 |
|
| 2082 |
message = dbus_message_new_method_call (SYSTEMD_LOGIND_SERVICE, SYSTEMD_LOGIND_PATH, SYSTEMD_LOGIND_INTERFACE, "GetSessionByPID"); |
| 2083 |
if (message == NULL) { |
| 2084 |
gs_debug ("Couldn't allocate the dbus message"); |
| 2085 |
return NULL; |
| 2086 |
} |
| 2087 |
|
| 2088 |
if (dbus_message_append_args (message, DBUS_TYPE_UINT32, &pid, DBUS_TYPE_INVALID) == FALSE) { |
| 2089 |
gs_debug ("Couldn't add args to the dbus message"); |
| 2090 |
dbus_message_unref (message); |
| 2091 |
return NULL; |
| 2092 |
} |
| 2093 |
|
| 2094 |
/* FIXME: use async? */ |
| 2095 |
reply = dbus_connection_send_with_reply_and_block (listener->priv->system_connection, |
| 2096 |
message, |
| 2097 |
-1, &error); |
| 2098 |
dbus_message_unref (message); |
| 2099 |
|
| 2100 |
if (dbus_error_is_set (&error)) { |
| 2101 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 2102 |
dbus_error_free (&error); |
| 2103 |
return NULL; |
| 2104 |
} |
| 2105 |
|
| 2106 |
if (dbus_message_get_args (reply, &error, |
| 2107 |
DBUS_TYPE_OBJECT_PATH, &ssid, |
| 2108 |
DBUS_TYPE_INVALID)) { |
| 2109 |
ssid = g_strdup (ssid); |
| 2110 |
} else { |
| 2111 |
ssid = NULL; |
| 2112 |
} |
| 2113 |
|
| 2114 |
dbus_message_unref (reply); |
| 2115 |
|
| 2116 |
if (dbus_error_is_set (&error)) { |
| 2117 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 2118 |
dbus_error_free (&error); |
| 2119 |
return NULL; |
| 2120 |
} |
| 2121 |
|
| 2122 |
return ssid; |
| 2123 |
} |
| 2124 |
#endif |
| 2125 |
|
| 2126 |
#ifdef WITH_CONSOLE_KIT |
| 2127 |
message = dbus_message_new_method_call (CK_SERVICE, CK_MANAGER_PATH, CK_MANAGER_INTERFACE, "GetCurrentSession"); |
| 2128 |
if (message == NULL) { |
| 2129 |
gs_debug ("Couldn't allocate the dbus message"); |
| 2130 |
return NULL; |
| 2131 |
} |
| 2132 |
|
| 2133 |
/* FIXME: use async? */ |
| 2134 |
reply = dbus_connection_send_with_reply_and_block (listener->priv->system_connection, |
| 2135 |
message, |
| 2136 |
-1, &error); |
| 2137 |
dbus_message_unref (message); |
| 2138 |
|
| 2139 |
if (dbus_error_is_set (&error)) { |
| 2140 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 2141 |
dbus_error_free (&error); |
| 2142 |
return NULL; |
| 2143 |
} |
| 2144 |
|
| 2145 |
if (dbus_message_get_args (reply, &error, |
| 2146 |
DBUS_TYPE_OBJECT_PATH, &ssid, |
| 2147 |
DBUS_TYPE_INVALID)) { |
| 2148 |
ssid = g_strdup (ssid); |
| 2149 |
} else { |
| 2150 |
ssid = NULL; |
| 2151 |
} |
| 2152 |
|
| 2153 |
dbus_message_unref (reply); |
| 2154 |
|
| 2155 |
if (dbus_error_is_set (&error)) { |
| 2156 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 2157 |
dbus_error_free (&error); |
| 2158 |
return NULL; |
| 2159 |
} |
| 2160 |
|
| 2161 |
return ssid; |
| 2162 |
#else |
| 2163 |
return NULL; |
| 2164 |
#endif |
| 2165 |
} |
| 2166 |
|
| 2167 |
#ifdef WITH_SYSTEMD |
| 2168 |
static char * |
| 2169 |
query_sd_session_id (GSListener *listener) |
| 2170 |
{ |
| 2171 |
char *ssid; |
| 2172 |
char *t; |
| 2173 |
int r; |
| 2174 |
|
| 2175 |
r = sd_pid_get_session (0, &t); |
| 2176 |
if (r < 0) { |
| 2177 |
gs_debug ("Couldn't determine our own sd session id: %s", strerror (-r)); |
| 2178 |
return NULL; |
| 2179 |
} |
| 2180 |
|
| 2181 |
ssid = g_strdup (t); |
| 2182 |
free (t); |
| 2183 |
|
| 2184 |
return ssid; |
| 2185 |
} |
| 2186 |
#endif |
| 2187 |
|
| 2188 |
static void |
| 2189 |
init_session_id (GSListener *listener) |
| 2190 |
{ |
| 2191 |
g_free (listener->priv->session_id); |
| 2192 |
listener->priv->session_id = query_session_id (listener); |
| 2193 |
if (listener->priv->session_id == NULL) |
| 2194 |
g_error ("session_id is not set, is /proc mounted with hidepid>0?"); |
| 2195 |
else |
| 2196 |
gs_debug ("Got session-id: %s", listener->priv->session_id); |
| 2197 |
|
| 2198 |
#ifdef WITH_SYSTEMD |
| 2199 |
g_free (listener->priv->sd_session_id); |
| 2200 |
listener->priv->sd_session_id = query_sd_session_id (listener); |
| 2201 |
if (listener->priv->sd_session_id == NULL) |
| 2202 |
{ |
| 2203 |
gs_debug ("Falling back to XDG_SESSION_ID environment variable"); |
| 2204 |
listener->priv->sd_session_id = g_strdup(getenv("XDG_SESSION_ID")); |
| 2205 |
} |
| 2206 |
gs_debug ("Got sd-session-id: %s", listener->priv->sd_session_id); |
| 2207 |
#endif |
| 2208 |
} |
| 2209 |
|
| 2210 |
static char * |
| 2211 |
query_seat_path (GSListener *listener) |
| 2212 |
{ |
| 2213 |
DBusMessage *message; |
| 2214 |
DBusMessage *reply; |
| 2215 |
DBusError error; |
| 2216 |
DBusMessageIter reply_iter; |
| 2217 |
DBusMessageIter sub_iter; |
| 2218 |
char *seat; |
| 2219 |
const char *interface; |
| 2220 |
const char *property; |
| 2221 |
|
| 2222 |
if (listener->priv->system_connection == NULL) { |
| 2223 |
gs_debug ("No connection to the system bus"); |
| 2224 |
return NULL; |
| 2225 |
} |
| 2226 |
|
| 2227 |
if (DM_SESSION_PATH == NULL) { |
| 2228 |
g_error ("Environment variable XDG_SESSION_PATH not set. Is LightDM running?"); |
| 2229 |
} |
| 2230 |
|
| 2231 |
seat = NULL; |
| 2232 |
|
| 2233 |
dbus_error_init (&error); |
| 2234 |
|
| 2235 |
message = dbus_message_new_method_call (DM_SERVICE, DM_SESSION_PATH, DBUS_PROPERTIES_INTERFACE, "Get"); |
| 2236 |
if (message == NULL) { |
| 2237 |
gs_debug ("Couldn't allocate the dbus message"); |
| 2238 |
return NULL; |
| 2239 |
} |
| 2240 |
|
| 2241 |
interface = DM_SESSION_INTERFACE; |
| 2242 |
property = "Seat"; |
| 2243 |
|
| 2244 |
if (dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID) == FALSE) { |
| 2245 |
gs_debug ("Couldn't add args to the dbus message"); |
| 2246 |
dbus_message_unref (message); |
| 2247 |
return NULL; |
| 2248 |
} |
| 2249 |
|
| 2250 |
/* FIXME: use async? */ |
| 2251 |
reply = dbus_connection_send_with_reply_and_block (listener->priv->system_connection, |
| 2252 |
message, |
| 2253 |
-1, &error); |
| 2254 |
dbus_message_unref (message); |
| 2255 |
|
| 2256 |
if (dbus_error_is_set (&error)) { |
| 2257 |
gs_debug ("%s raised:\n %s\n\n", error.name, error.message); |
| 2258 |
dbus_error_free (&error); |
| 2259 |
return NULL; |
| 2260 |
} |
| 2261 |
|
| 2262 |
if (dbus_message_iter_init (reply, &reply_iter) == TRUE |
| 2263 |
&& dbus_message_iter_get_arg_type (&reply_iter) == DBUS_TYPE_VARIANT) { |
| 2264 |
|
| 2265 |
dbus_message_iter_recurse (&reply_iter, &sub_iter); |
| 2266 |
|
| 2267 |
if (dbus_message_iter_get_arg_type (&sub_iter) == DBUS_TYPE_OBJECT_PATH) { |
| 2268 |
dbus_message_iter_get_basic (&sub_iter, &seat); |
| 2269 |
seat = g_strdup (seat); |
| 2270 |
} else { |
| 2271 |
gs_debug ("Unexpected return type"); |
| 2272 |
} |
| 2273 |
} else { |
| 2274 |
gs_debug ("Unexpected return type"); |
| 2275 |
} |
| 2276 |
|
| 2277 |
dbus_message_unref (reply); |
| 2278 |
|
| 2279 |
return seat; |
| 2280 |
} |
| 2281 |
|
| 2282 |
static void |
| 2283 |
init_seat_path (GSListener *listener) |
| 2284 |
{ |
| 2285 |
g_free (listener->priv->seat_path); |
| 2286 |
listener->priv->seat_path = query_seat_path (listener); |
| 2287 |
gs_debug ("Got seat: %s", listener->priv->seat_path); |
| 2288 |
} |
| 2289 |
|
| 2290 |
static void |
| 2291 |
gs_listener_init (GSListener *listener) |
| 2292 |
{ |
| 2293 |
listener->priv = GS_LISTENER_GET_PRIVATE (listener); |
| 2294 |
|
| 2295 |
#ifdef WITH_SYSTEMD |
| 2296 |
/* check if logind is running */ |
| 2297 |
listener->priv->have_systemd = (access("/run/systemd/seats/", F_OK) >= 0); |
| 2298 |
listener->priv->delay_fd = -1; |
| 2299 |
#endif |
| 2300 |
|
| 2301 |
gs_listener_dbus_init (listener); |
| 2302 |
|
| 2303 |
init_session_id (listener); |
| 2304 |
init_seat_path (listener); |
| 2305 |
|
| 2306 |
listener->priv->inhibit_list = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free); |
| 2307 |
} |
| 2308 |
|
| 2309 |
static void |
| 2310 |
gs_listener_finalize (GObject *object) |
| 2311 |
{ |
| 2312 |
GSListener *listener; |
| 2313 |
|
| 2314 |
g_return_if_fail (object != NULL); |
| 2315 |
g_return_if_fail (GS_IS_LISTENER (object)); |
| 2316 |
|
| 2317 |
listener = GS_LISTENER (object); |
| 2318 |
|
| 2319 |
g_return_if_fail (listener->priv != NULL); |
| 2320 |
|
| 2321 |
g_free (listener->priv->session_id); |
| 2322 |
g_free (listener->priv->seat_path); |
| 2323 |
|
| 2324 |
#ifdef WITH_SYSTEMD |
| 2325 |
g_free (listener->priv->sd_session_id); |
| 2326 |
#endif |
| 2327 |
|
| 2328 |
G_OBJECT_CLASS (gs_listener_parent_class)->finalize (object); |
| 2329 |
} |
| 2330 |
|
| 2331 |
GSListener * |
| 2332 |
gs_listener_new (void) |
| 2333 |
{ |
| 2334 |
GSListener *listener; |
| 2335 |
|
| 2336 |
listener = g_object_new (GS_TYPE_LISTENER, NULL); |
| 2337 |
|
| 2338 |
return GS_LISTENER (listener); |
| 2339 |
} |