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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2002 CodeFactory AB
 * Copyright (C) 2002 Richard Hult <richard@imendio.com>
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <cdk/cdk.h>
#include <cdk/cdkx.h>
#include <ctk/ctk.h>
#include <math.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <X11/Xlib.h>
#include <X11/extensions/scrnsaver.h>
#include "drw-monitor.h"

struct _DrwMonitorPriv {
	XScreenSaverInfo *ss_info;
	guint             timeout_id;
	unsigned long     last_idle;

	time_t            last_activity;
};

/* Signals */
enum {
	ACTIVITY,
	LAST_SIGNAL
};


static void     drw_monitor_class_init    (DrwMonitorClass *klass);
static void     drw_monitor_init          (DrwMonitor      *monitor);
static void     drw_monitor_finalize      (GObject         *object);
static gboolean drw_monitor_setup         (DrwMonitor      *monitor);

static GObjectClass *parent_class;
static guint signals[LAST_SIGNAL] = { 0 };


GType
drw_monitor_get_type (void)
{
	static GType object_type = 0;

	if (!object_type) {
		static const GTypeInfo object_info = {
			sizeof (DrwMonitorClass),
			NULL,		/* base_init */
			NULL,		/* base_finalize */
			(GClassInitFunc) drw_monitor_class_init,
			NULL,		/* class_finalize */
			NULL,		/* class_data */
			sizeof (DrwMonitor),
			0,              /* n_preallocs */
			(GInstanceInitFunc) drw_monitor_init,
		};

		object_type = g_type_register_static (G_TYPE_OBJECT,
                                                      "DrwMonitor",
                                                      &object_info, 0);
	}

	return object_type;
}

static void
drw_monitor_class_init (DrwMonitorClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);

        parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));

        object_class->finalize = drw_monitor_finalize;

	signals[ACTIVITY] =
		g_signal_new ("activity",
			      G_TYPE_FROM_CLASS (klass),
			      G_SIGNAL_RUN_LAST,
			      0,
			      NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE, 0);
}

static void
drw_monitor_init (DrwMonitor *monitor)
{
        DrwMonitorPriv *priv;

        priv = g_new0 (DrwMonitorPriv, 1);
        monitor->priv = priv;

	drw_monitor_setup (monitor);
}

static void
drw_monitor_finalize (GObject *object)
{
        DrwMonitor     *monitor = DRW_MONITOR (object);
        DrwMonitorPriv *priv;

        priv = monitor->priv;

	g_source_remove (priv->timeout_id);
	priv->timeout_id = 0;

	if (priv->ss_info) {
		XFree (priv->ss_info);
	}

	g_free (priv);
	monitor->priv = NULL;

        if (G_OBJECT_CLASS (parent_class)->finalize) {
                (* G_OBJECT_CLASS (parent_class)->finalize) (object);
        }
}

static gboolean
drw_monitor_timeout (DrwMonitor *monitor)
{
	DrwMonitorPriv *priv;
 	time_t          now;

	priv = monitor->priv;

	if (XScreenSaverQueryInfo (CDK_DISPLAY_XDISPLAY(cdk_display_get_default()), DefaultRootWindow (CDK_DISPLAY_XDISPLAY(cdk_display_get_default())), priv->ss_info) != 0) {
		if (priv->ss_info->idle < priv->last_idle) {
 			now = time (NULL);

 			if (now - priv->last_activity < 25) {
 				g_signal_emit (monitor, signals[ACTIVITY], 0, NULL);
 			}

 			priv->last_activity = now;
		}

		priv->last_idle = priv->ss_info->idle;
	}

	return TRUE;
}

static gboolean
drw_monitor_setup (DrwMonitor *monitor)
{
	DrwMonitorPriv *priv;
	int             event_base;
	int             error_base;

	priv = monitor->priv;

	if (!XScreenSaverQueryExtension (CDK_DISPLAY_XDISPLAY(cdk_display_get_default()), &event_base, &error_base)) {
		return FALSE;
	}

	priv->ss_info = XScreenSaverAllocInfo ();

	priv->last_activity = time (NULL);

	priv->timeout_id = g_timeout_add_seconds (3, (GSourceFunc) drw_monitor_timeout, monitor);

	return TRUE;
}

DrwMonitor *
drw_monitor_new (void)
{
	return g_object_new (DRW_TYPE_MONITOR, NULL);
}