summaryrefslogtreecommitdiff
path: root/plugins/glue/idle/ohm-plugin-idle.c
blob: 5c6bff481b79784b990e273c8d8574d671bf1dca (plain)
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
/*
 * Copyright (C) 2007 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU Lesser General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <gmodule.h>
#include <glib.h>

#include <ohm-plugin.h>
#include <libidletime.h>
#include <stdlib.h>

static LibIdletime *idletime = NULL;

enum {
	CONF_XORG_HASXAUTH_CHANGED,
	CONF_IDLE_TIMEOUT_CHANGED,
	CONF_IDLE_STATE_CHANGED,
	CONF_LAST
};

static void
ohm_alarm_expired_cb (LibIdletime *idletime, guint alarm, gpointer data)
{
	OhmPlugin *plugin = (OhmPlugin *) data;
	gint state;

	if (alarm == 0 ) {
		/* activity, reset state to 0 */
		ohm_plugin_conf_set_key (plugin, "idle.state", 0);
	} else {
		ohm_plugin_conf_get_key (plugin, "idle.state", &state);
		ohm_plugin_conf_set_key (plugin, "idle.state", state+1);
	}
	g_print ("[evt %i]\n", alarm);
}

static void
plugin_connect_idletime (OhmPlugin *plugin)
{
	gboolean ret;
	gint timeout;
	const gchar *xauth;

	xauth = getenv ("XAUTHORITY");
	g_debug ("connecting with %s", xauth);

	idletime = idletime_new ();
	if (idletime == NULL) {
		g_error ("cannot create idletime");
	}
	g_signal_connect (idletime, "alarm-expired",
			  G_CALLBACK (ohm_alarm_expired_cb), plugin);

	ohm_plugin_conf_set_key (plugin, "idle.state", 0);

	ohm_plugin_conf_get_key (plugin, "idle.timeout", &timeout);

	if (timeout != 0) {
		ret = idletime_alarm_set (idletime, 1, timeout);
		if (ret == FALSE) {
			g_error ("cannot set alarm");
		}
	}
}

/**
 * plugin_initalize:
 * @plugin: This class instance
 *
 * Coldplug, i.e. read and set the initial state of the plugin.
 * We can assume all the required modules have been loaded, although it's
 * dangerous to assume the key values are anything other than the defaults.
 */
static void
plugin_initalize (OhmPlugin *plugin)
{
	gint value;

	/* check system inhibit - this is broken as any client can unref all */
	ohm_plugin_conf_get_key (plugin, "xorg.has_xauthority", &value);
	if (value == 1) {
		plugin_connect_idletime (plugin);
	}

}

/**
 * plugin_notify:
 * @plugin: This class instance
 *
 * Notify the plugin that a key marked with ohm_plugin_conf_interested ()
 * has it's value changed.
 * An enumerated numeric id rather than the key is returned for processing speed.
 */
static void
plugin_notify (OhmPlugin *plugin, gint id, gint value)
{
	gint timeout;
	if (id == CONF_XORG_HASXAUTH_CHANGED) {
		if (value == 1) {
			plugin_connect_idletime (plugin);
		}
	}
	if (idletime) {
		if (id == CONF_IDLE_TIMEOUT_CHANGED ) {
			g_debug("setting new timeout %d", value);
			idletime_alarm_set (idletime, 1, value);
		} else if (id == CONF_IDLE_STATE_CHANGED ) {
			if (value == 0) {
				ohm_plugin_conf_get_key (plugin, "idle.timeout", &timeout);
				idletime_alarm_set (idletime, 1, timeout);
			}
		}
	}
}
static void
plugin_destroy (OhmPlugin *plugin)
{
	if (idletime)
		g_object_unref (idletime);
}

OHM_PLUGIN_DESCRIPTION (
	"OHM IdleTime",			/* description */
	"0.0.1",			/* version */
	"richard@hughsie.com",		/* author */
	OHM_LICENSE_LGPL,		/* license */
	plugin_initalize,		/* initalize */
	plugin_destroy,			/* destroy */
	plugin_notify		/* notify */
);

OHM_PLUGIN_REQUIRES ("xorg");

OHM_PLUGIN_PROVIDES ("idle.state", "idle.timeout");

OHM_PLUGIN_INTERESTED (
	{"xorg.has_xauthority", CONF_XORG_HASXAUTH_CHANGED},
	{"idle.timeout", CONF_IDLE_TIMEOUT_CHANGED},
	{"idle.state", CONF_IDLE_STATE_CHANGED});