1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2004-2006 William Jon McCann <mccann@jhu.edu>
* Copyright (C) 2008 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Authors: William Jon McCann <mccann@jhu.edu>
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <gdk/gdkx.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include "gs-watcher.h"
#include "gs-marshal.h"
#include "gs-debug.h"
#include "bus.h"
static void gs_watcher_class_init (GSWatcherClass *klass);
static void gs_watcher_init (GSWatcher *watcher);
static void gs_watcher_finalize (GObject *object);
static gboolean watchdog_timer (GSWatcher *watcher);
#define GS_WATCHER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_WATCHER, GSWatcherPrivate))
struct GSWatcherPrivate
{
/* settings */
guint enabled : 1;
guint delta_notice_timeout;
/* state */
guint active : 1;
guint idle : 1;
guint idle_notice : 1;
guint idle_id;
char *status_message;
DBusGProxy *presence_proxy;
guint watchdog_timer_id;
};
enum {
PROP_0,
PROP_STATUS_MESSAGE
};
enum {
IDLE_CHANGED,
IDLE_NOTICE_CHANGED,
LAST_SIGNAL
};
static guint signals [LAST_SIGNAL] = { 0, };
G_DEFINE_TYPE (GSWatcher, gs_watcher, G_TYPE_OBJECT)
static void
remove_watchdog_timer (GSWatcher *watcher)
{
if (watcher->priv->watchdog_timer_id != 0) {
g_source_remove (watcher->priv->watchdog_timer_id);
watcher->priv->watchdog_timer_id = 0;
}
}
static void
add_watchdog_timer (GSWatcher *watcher,
glong timeout)
{
watcher->priv->watchdog_timer_id = g_timeout_add (timeout,
(GSourceFunc)watchdog_timer,
watcher);
}
static void
gs_watcher_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GSWatcher *self;
self = GS_WATCHER (object);
switch (prop_id) {
case PROP_STATUS_MESSAGE:
g_value_set_string (value, self->priv->status_message);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
set_status_text (GSWatcher *watcher,
const char *text)
{
g_free (watcher->priv->status_message);
watcher->priv->status_message = g_strdup (text);
g_object_notify (G_OBJECT (watcher), "status-message");
}
static void
gs_watcher_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GSWatcher *self;
self = GS_WATCHER (object);
switch (prop_id) {
case PROP_STATUS_MESSAGE:
set_status_text (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gs_watcher_class_init (GSWatcherClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gs_watcher_finalize;
object_class->get_property = gs_watcher_get_property;
object_class->set_property = gs_watcher_set_property;
g_object_class_install_property (object_class,
PROP_STATUS_MESSAGE,
g_param_spec_string ("status-message",
NULL,
NULL,
NULL,
G_PARAM_READWRITE));
signals [IDLE_CHANGED] =
g_signal_new ("idle-changed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GSWatcherClass, idle_changed),
NULL,
NULL,
gs_marshal_BOOLEAN__BOOLEAN,
G_TYPE_BOOLEAN,
1, G_TYPE_BOOLEAN);
signals [IDLE_NOTICE_CHANGED] =
g_signal_new ("idle-notice-changed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GSWatcherClass, idle_notice_changed),
NULL,
NULL,
gs_marshal_BOOLEAN__BOOLEAN,
G_TYPE_BOOLEAN,
1, G_TYPE_BOOLEAN);
g_type_class_add_private (klass, sizeof (GSWatcherPrivate));
}
static gboolean
_gs_watcher_set_session_idle_notice (GSWatcher *watcher,
gboolean in_effect)
{
gboolean res;
res = FALSE;
if (in_effect != watcher->priv->idle_notice) {
g_signal_emit (watcher, signals [IDLE_NOTICE_CHANGED], 0, in_effect, &res);
if (res) {
gs_debug ("Changing idle notice state: %d", in_effect);
watcher->priv->idle_notice = in_effect;
} else {
gs_debug ("Idle notice signal not handled: %d", in_effect);
}
}
return res;
}
static gboolean
_gs_watcher_set_session_idle (GSWatcher *watcher,
gboolean is_idle)
{
gboolean res;
res = FALSE;
if (is_idle != watcher->priv->idle) {
g_signal_emit (watcher, signals [IDLE_CHANGED], 0, is_idle, &res);
if (res) {
gs_debug ("Changing idle state: %d", is_idle);
watcher->priv->idle = is_idle;
} else {
gs_debug ("Idle changed signal not handled: %d", is_idle);
}
}
return res;
}
gboolean
gs_watcher_get_active (GSWatcher *watcher)
{
gboolean active;
g_return_val_if_fail (GS_IS_WATCHER (watcher), FALSE);
active = watcher->priv->active;
return active;
}
static void
_gs_watcher_reset_state (GSWatcher *watcher)
{
watcher->priv->idle = FALSE;
watcher->priv->idle_notice = FALSE;
}
static gboolean
_gs_watcher_set_active_internal (GSWatcher *watcher,
gboolean active)
{
if (active != watcher->priv->active) {
/* reset state */
_gs_watcher_reset_state (watcher);
watcher->priv->active = active;
}
return TRUE;
}
gboolean
gs_watcher_set_active (GSWatcher *watcher,
gboolean active)
{
g_return_val_if_fail (GS_IS_WATCHER (watcher), FALSE);
gs_debug ("turning watcher: %s", active ? "ON" : "OFF");
if (watcher->priv->active == active) {
gs_debug ("Idle detection is already %s",
active ? "active" : "inactive");
return FALSE;
}
if (! watcher->priv->enabled) {
gs_debug ("Idle detection is disabled, cannot activate");
return FALSE;
}
return _gs_watcher_set_active_internal (watcher, active);
}
gboolean
gs_watcher_set_enabled (GSWatcher *watcher,
gboolean enabled)
{
g_return_val_if_fail (GS_IS_WATCHER (watcher), FALSE);
if (watcher->priv->enabled != enabled) {
gboolean is_active = gs_watcher_get_active (watcher);
watcher->priv->enabled = enabled;
/* if we are disabling the watcher and we are
active shut it down */
if (! enabled && is_active) {
_gs_watcher_set_active_internal (watcher, FALSE);
}
}
return TRUE;
}
gboolean
gs_watcher_get_enabled (GSWatcher *watcher)
{
gboolean enabled;
g_return_val_if_fail (GS_IS_WATCHER (watcher), FALSE);
enabled = watcher->priv->enabled;
return enabled;
}
static gboolean
on_idle_timeout (GSWatcher *watcher)
{
gboolean res;
res = _gs_watcher_set_session_idle (watcher, TRUE);
_gs_watcher_set_session_idle_notice (watcher, FALSE);
/* try again if we failed i guess */
return !res;
}
static void
set_status (GSWatcher *watcher,
guint status)
{
gboolean is_idle;
if (! watcher->priv->active) {
gs_debug ("GSWatcher: not active, ignoring status changes");
return;
}
is_idle = (status == 3);
if (!is_idle && !watcher->priv->idle_notice) {
/* no change in idleness */
return;
}
if (is_idle) {
_gs_watcher_set_session_idle_notice (watcher, is_idle);
/* queue an activation */
if (watcher->priv->idle_id > 0) {
g_source_remove (watcher->priv->idle_id);
}
watcher->priv->idle_id = g_timeout_add (watcher->priv->delta_notice_timeout,
(GSourceFunc)on_idle_timeout,
watcher);
} else {
/* cancel notice too */
if (watcher->priv->idle_id > 0) {
g_source_remove (watcher->priv->idle_id);
}
_gs_watcher_set_session_idle (watcher, FALSE);
_gs_watcher_set_session_idle_notice (watcher, FALSE);
}
}
static void
on_presence_status_changed (DBusGProxy *presence_proxy,
guint status,
GSWatcher *watcher)
{
set_status (watcher, status);
}
static void
on_presence_status_text_changed (DBusGProxy *presence_proxy,
const char *status_text,
GSWatcher *watcher)
{
set_status_text (watcher, status_text);
}
static void
connect_presence_watcher (GSWatcher *watcher)
{
DBusGConnection *bus;
GError *error;
DBusGProxy *proxy;
guint status;
const char *status_text;
GValue value = { 0, };
error = NULL;
bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
if (bus == NULL) {
g_warning ("Unable to get session bus: %s", error->message);
g_error_free (error);
return;
}
watcher->priv->presence_proxy = dbus_g_proxy_new_for_name (bus,
GSM_SERVICE,
GSM_PRESENCE_PATH,
GSM_PRESENCE_INTERFACE);
dbus_g_proxy_add_signal (watcher->priv->presence_proxy,
"StatusChanged",
G_TYPE_UINT,
G_TYPE_INVALID);
dbus_g_proxy_connect_signal (watcher->priv->presence_proxy,
"StatusChanged",
G_CALLBACK (on_presence_status_changed),
watcher,
NULL);
dbus_g_proxy_add_signal (watcher->priv->presence_proxy,
"StatusTextChanged",
G_TYPE_STRING,
G_TYPE_INVALID);
dbus_g_proxy_connect_signal (watcher->priv->presence_proxy,
"StatusTextChanged",
G_CALLBACK (on_presence_status_text_changed),
watcher,
NULL);
proxy = dbus_g_proxy_new_from_proxy (watcher->priv->presence_proxy,
"org.freedesktop.DBus.Properties",
GSM_PRESENCE_PATH);
status = 0;
status_text = NULL;
error = NULL;
dbus_g_proxy_call (proxy,
"Get",
&error,
G_TYPE_STRING, GSM_PRESENCE_INTERFACE,
G_TYPE_STRING, "status",
G_TYPE_INVALID,
G_TYPE_VALUE, &value,
G_TYPE_INVALID);
if (error != NULL) {
g_warning ("Couldn't get presence status: %s", error->message);
g_error_free (error);
return;
} else {
status = g_value_get_uint (&value);
}
g_value_unset (&value);
error = NULL;
dbus_g_proxy_call (proxy,
"Get",
&error,
G_TYPE_STRING, GSM_PRESENCE_INTERFACE,
G_TYPE_STRING, "status-text",
G_TYPE_INVALID,
G_TYPE_VALUE, &value,
G_TYPE_INVALID);
if (error != NULL) {
g_warning ("Couldn't get presence status text: %s", error->message);
g_error_free (error);
} else {
status_text = g_value_get_string (&value);
}
set_status (watcher, status);
set_status_text (watcher, status_text);
}
static void
gs_watcher_init (GSWatcher *watcher)
{
watcher->priv = GS_WATCHER_GET_PRIVATE (watcher);
watcher->priv->enabled = TRUE;
watcher->priv->active = FALSE;
connect_presence_watcher (watcher);
/* time before idle signal to send notice signal */
watcher->priv->delta_notice_timeout = 10000;
add_watchdog_timer (watcher, 600000);
}
static void
gs_watcher_finalize (GObject *object)
{
GSWatcher *watcher;
g_return_if_fail (object != NULL);
g_return_if_fail (GS_IS_WATCHER (object));
watcher = GS_WATCHER (object);
g_return_if_fail (watcher->priv != NULL);
remove_watchdog_timer (watcher);
if (watcher->priv->idle_id > 0) {
g_source_remove (watcher->priv->idle_id);
}
watcher->priv->active = FALSE;
if (watcher->priv->presence_proxy != NULL) {
g_object_unref (watcher->priv->presence_proxy);
}
g_free (watcher->priv->status_message);
G_OBJECT_CLASS (gs_watcher_parent_class)->finalize (object);
}
/* Figuring out what the appropriate XSetScreenSaver() parameters are
(one wouldn't expect this to be rocket science.)
*/
static void
disable_builtin_screensaver (GSWatcher *watcher,
gboolean unblank_screen)
{
int current_server_timeout, current_server_interval;
int current_prefer_blank, current_allow_exp;
int desired_server_timeout, desired_server_interval;
int desired_prefer_blank, desired_allow_exp;
XGetScreenSaver (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
¤t_server_timeout,
¤t_server_interval,
¤t_prefer_blank,
¤t_allow_exp);
desired_server_timeout = current_server_timeout;
desired_server_interval = current_server_interval;
desired_prefer_blank = current_prefer_blank;
desired_allow_exp = current_allow_exp;
desired_server_interval = 0;
/* I suspect (but am not sure) that DontAllowExposures might have
something to do with powering off the monitor as well, at least
on some systems that don't support XDPMS? Who know... */
desired_allow_exp = AllowExposures;
/* When we're not using an extension, set the server-side timeout to 0,
so that the server never gets involved with screen blanking, and we
do it all ourselves. (However, when we *are* using an extension,
we tell the server when to notify us, and rather than blanking the
screen, the server will send us an X event telling us to blank.)
*/
desired_server_timeout = 0;
if (desired_server_timeout != current_server_timeout
|| desired_server_interval != current_server_interval
|| desired_prefer_blank != current_prefer_blank
|| desired_allow_exp != current_allow_exp) {
gs_debug ("disabling server builtin screensaver:"
" (xset s %d %d; xset s %s; xset s %s)",
desired_server_timeout,
desired_server_interval,
(desired_prefer_blank ? "blank" : "noblank"),
(desired_allow_exp ? "expose" : "noexpose"));
XSetScreenSaver (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
desired_server_timeout,
desired_server_interval,
desired_prefer_blank,
desired_allow_exp);
XSync (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), FALSE);
}
if (unblank_screen) {
/* Turn off the server builtin saver if it is now running. */
XForceScreenSaver (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), ScreenSaverReset);
}
}
/* This timer goes off every few minutes, whether the user is idle or not,
to try and clean up anything that has gone wrong.
It calls disable_builtin_screensaver() so that if xset has been used,
or some other program (like xlock) has messed with the XSetScreenSaver()
settings, they will be set back to sensible values (if a server extension
is in use, messing with xlock can cause the screensaver to never get a wakeup
event, and could cause monitor power-saving to occur, and all manner of
heinousness.)
*/
static gboolean
watchdog_timer (GSWatcher *watcher)
{
disable_builtin_screensaver (watcher, FALSE);
return TRUE;
}
GSWatcher *
gs_watcher_new (void)
{
GSWatcher *watcher;
watcher = g_object_new (GS_TYPE_WATCHER,
NULL);
return GS_WATCHER (watcher);
}
|