Mercurial > projects > xinhibit-applet
comparison xia-inhibitor.c @ 0:9a16bf50daba
Initial revision
author | Guido Berhoerster <guido+xinhibit-applet@berhoerster.name> |
---|---|
date | Sat, 27 Apr 2013 00:33:11 +0200 |
parents | |
children | 957d697d27d3 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9a16bf50daba |
---|---|
1 /* | |
2 * Copyright (C) 2013 Guido Berhoerster <guido+xinhibit-applet@berhoerster.name> | |
3 * | |
4 * Permission is hereby granted, free of charge, to any person obtaining | |
5 * a copy of this software and associated documentation files (the | |
6 * "Software"), to deal in the Software without restriction, including | |
7 * without limitation the rights to use, copy, modify, merge, publish, | |
8 * distribute, sublicense, and/or sell copies of the Software, and to | |
9 * permit persons to whom the Software is furnished to do so, subject to | |
10 * the following conditions: | |
11 * | |
12 * The above copyright notice and this permission notice shall be included | |
13 * in all copies or substantial portions of the Software. | |
14 * | |
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
22 */ | |
23 | |
24 #include <X11/Xlib.h> | |
25 #include <X11/keysym.h> | |
26 #include <X11/extensions/Xext.h> | |
27 #ifdef HAVE_LIBXTST | |
28 #include <X11/extensions/XTest.h> | |
29 #endif /* HAVE_LIBXTST */ | |
30 #include <glib.h> | |
31 #include <glib/gi18n.h> | |
32 #include <gdk/gdkx.h> | |
33 #include "xia-inhibitor.h" | |
34 | |
35 #define FAKE_EVENT_TIMEOUT 20 | |
36 | |
37 G_DEFINE_TYPE(XiaInhibitor, xia_inhibitor, G_TYPE_OBJECT) | |
38 | |
39 #define XIA_INHIBITOR_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \ | |
40 XIA_TYPE_INHIBITOR, XiaInhibitorPrivate)) | |
41 | |
42 struct _XiaInhibitorPrivate | |
43 { | |
44 XiaInhibitor *inhibitor; | |
45 gboolean inhibited; | |
46 gboolean have_xtest; | |
47 guint generate_fake_event_id; | |
48 }; | |
49 | |
50 enum { PROP_0, PROP_INHIBITED }; | |
51 | |
52 static gboolean | |
53 generate_fake_event(gpointer data) | |
54 { | |
55 #ifdef HAVE_LIBXTST | |
56 XiaInhibitor *self = XIA_INHIBITOR(data); | |
57 static KeyCode keycode = 0; | |
58 | |
59 if (!self->priv->have_xtest) { | |
60 return (TRUE); | |
61 } | |
62 | |
63 if (keycode == 0) { | |
64 keycode = XKeysymToKeycode(gdk_x11_get_default_xdisplay(), | |
65 XK_Alt_L); | |
66 if (keycode == 0) { | |
67 return (TRUE); | |
68 } | |
69 } | |
70 | |
71 XTestFakeKeyEvent(gdk_x11_get_default_xdisplay(), keycode, True, 0); | |
72 XTestFakeKeyEvent(gdk_x11_get_default_xdisplay(), keycode, False, 0); | |
73 | |
74 g_debug("Generated fake key event using XTEST"); | |
75 #endif /* HAVE_LIBXTST */ | |
76 | |
77 return (TRUE); | |
78 } | |
79 | |
80 static void | |
81 xia_inhibitor_set_property(GObject *object, guint property_id, | |
82 const GValue *value, GParamSpec *pspec) | |
83 { | |
84 XiaInhibitor *self = XIA_INHIBITOR(object); | |
85 | |
86 switch (property_id) { | |
87 case PROP_INHIBITED: { | |
88 gboolean inhibited = g_value_get_boolean(value); | |
89 if (self->priv->inhibited == inhibited) { | |
90 return; | |
91 } | |
92 | |
93 if (inhibited) { | |
94 self->priv->generate_fake_event_id = \ | |
95 g_timeout_add_seconds(FAKE_EVENT_TIMEOUT, | |
96 (GSourceFunc)generate_fake_event, self); | |
97 } else { | |
98 g_source_remove(self->priv->generate_fake_event_id); | |
99 self->priv->generate_fake_event_id = 0; | |
100 } | |
101 | |
102 self->priv->inhibited = inhibited; | |
103 | |
104 g_debug("Automatic power management is %s", (inhibited) ? | |
105 "inhibited" : "enabled"); | |
106 break; | |
107 } | |
108 default: | |
109 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); | |
110 break; | |
111 } | |
112 } | |
113 | |
114 static void | |
115 xia_inhibitor_get_property(GObject *object, guint property_id, GValue *value, | |
116 GParamSpec *pspec) | |
117 { | |
118 XiaInhibitor *self = XIA_INHIBITOR(object); | |
119 | |
120 switch (property_id) { | |
121 case PROP_INHIBITED: | |
122 g_value_set_boolean(value, self->priv->inhibited); | |
123 break; | |
124 default: | |
125 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, | |
126 pspec); | |
127 break; | |
128 } | |
129 } | |
130 | |
131 static void | |
132 xia_inhibitor_finalize(GObject *gobject) | |
133 { | |
134 XiaInhibitor *self = XIA_INHIBITOR(gobject); | |
135 | |
136 if (self->priv->generate_fake_event_id != 0) { | |
137 g_source_remove(self->priv->generate_fake_event_id); | |
138 self->priv->generate_fake_event_id = 0; | |
139 } | |
140 | |
141 G_OBJECT_CLASS(xia_inhibitor_parent_class)->finalize(gobject); | |
142 } | |
143 | |
144 static void | |
145 xia_inhibitor_class_init(XiaInhibitorClass *klass) | |
146 { | |
147 GObjectClass *gobject_class = G_OBJECT_CLASS(klass); | |
148 GParamSpec *pspec; | |
149 | |
150 gobject_class->set_property = xia_inhibitor_set_property; | |
151 gobject_class->get_property = xia_inhibitor_get_property; | |
152 gobject_class->finalize = xia_inhibitor_finalize; | |
153 | |
154 pspec = g_param_spec_boolean("inhibited", "Inhibited", | |
155 "Whether automatic power management is inhibited", FALSE, | |
156 G_PARAM_READWRITE); | |
157 g_object_class_install_property(gobject_class, PROP_INHIBITED, pspec); | |
158 | |
159 g_type_class_add_private(klass, sizeof (XiaInhibitorPrivate)); | |
160 } | |
161 | |
162 static void | |
163 xia_inhibitor_init(XiaInhibitor *self) | |
164 { | |
165 #ifdef HAVE_LIBXTST | |
166 int event_base; | |
167 int error_base; | |
168 int xtest_major; | |
169 int xtest_minor; | |
170 #endif /* HAVE_LIBXTST */ | |
171 | |
172 self->priv = XIA_INHIBITOR_GET_PRIVATE(self); | |
173 | |
174 self->priv->inhibited = FALSE; | |
175 self->priv->generate_fake_event_id = 0; | |
176 | |
177 #ifdef HAVE_LIBXTST | |
178 self->priv->have_xtest = ((gboolean)XTestQueryExtension( | |
179 gdk_x11_get_default_xdisplay(), &event_base, &error_base, | |
180 &xtest_major, &xtest_minor)); | |
181 #endif /* HAVE_LIBXTST */ | |
182 } | |
183 | |
184 void | |
185 xia_inhibitor_set_inhibited(XiaInhibitor *self, gboolean inhibited) | |
186 { | |
187 g_return_if_fail(XIA_IS_INHIBITOR(self)); | |
188 | |
189 g_object_set(G_OBJECT(self), "inhibited", inhibited, NULL); | |
190 } | |
191 | |
192 gboolean | |
193 xia_inhibitor_get_inhibited(XiaInhibitor *self) | |
194 { | |
195 gboolean inhibited; | |
196 | |
197 g_return_val_if_fail(XIA_IS_INHIBITOR(self), FALSE); | |
198 | |
199 g_object_get(G_OBJECT(self), "inhibited", &inhibited, NULL); | |
200 | |
201 return (inhibited); | |
202 } | |
203 | |
204 XiaInhibitor * | |
205 xia_inhibitor_new(void) | |
206 { | |
207 return (g_object_new(XIA_TYPE_INHIBITOR, NULL)); | |
208 } |