31ba96bf8859b5679608c8ac0b233e9363aa92dd
[eyes-widget] / src / eyes.c
1 /* $Id: eyes.c 2397 2007-01-17 17:46:35Z nick $
2  * 
3  * Copyright (c) Benedikt Meurer <benedikt.meurer@unix-ag.uni-siegen.de>>
4  * Copyright (c) Danny Milosavljevic <danny_milo@gmx.net>
5  * Copyright (c) Dave Camp
6  * Copyright (c) Davyd Madeley  <davyd@madeley.id.au>
7  * Copyright (c) Nick Schermer <nick@xfce.org>
8  * Copyright (c) Mikko Vartiainen <mvartiainen@gmail.com>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18  * more details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #ifdef HAVE_MATH_H
30 #include <math.h>
31 #endif
32
33 #include <sys/types.h>
34 #include <dirent.h>
35 #include <string.h>
36
37 #include <libhildondesktop/libhildondesktop.h>
38
39 #include "eyes.h"
40 #include "themes.h"
41 #include "accelerometer.h"
42
43 /* for xml: */
44 #define EYES_ROOT      "Eyes"
45 #define DEFAULTTHEME   "Default"
46 #define UPDATE_TIMEOUT 100
47
48 #define EYES_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,\
49                                                                                  EYES_TYPE_HOME_PLUGIN,\
50                                                                                  EyesPluginContent))
51
52 HD_DEFINE_PLUGIN_MODULE (EyesPlugin, eyes_plugin,      HD_TYPE_HOME_PLUGIN_ITEM)
53
54 /*****************************
55  *** Eyes Plugin Functions ***
56  *****************************/
57 static void
58 calculate_pupil_xy (EyesPluginContent *eyes_applet,
59                     gint x, gint y,
60                     gint *pupil_x, gint *pupil_y, GtkWidget* widget)
61 {
62         double sina;
63         double cosa;
64         double h;
65         double temp;
66         double nx, ny;
67
68         gfloat xalign, yalign;
69         gint width, height;
70
71         width = GTK_WIDGET(widget)->allocation.width;
72         height = GTK_WIDGET(widget)->allocation.height;
73         gtk_misc_get_alignment(GTK_MISC(widget),  &xalign, &yalign);
74
75     nx = width*x/1000.0;
76     ny = height*y/1000.0;
77
78         h = hypot (nx, ny);
79
80     if ( nx*nx/((eyes_applet->eye_width/2.0 - eyes_applet->pupil_width / 2)*(eyes_applet->eye_width/2.0 - eyes_applet->pupil_width/2)) + ny*ny/((eyes_applet->eye_height/2 - eyes_applet->pupil_height/2)*(eyes_applet->eye_height/2 - eyes_applet->pupil_height/2)) < 1 )
81         {
82                         *pupil_x = nx + eyes_applet->eye_width / 2;
83                         *pupil_y = ny + eyes_applet->eye_height / 2;
84                         return;
85         }
86
87         sina = nx / h;
88         cosa = ny / h;
89
90         temp = hypot ((eyes_applet->eye_width / 2) * sina, (eyes_applet->eye_height / 2) * cosa);
91         temp -= hypot ((eyes_applet->pupil_width / 2) * sina, (eyes_applet->pupil_height / 2) * cosa);
92         temp -= hypot ((eyes_applet->wall_thickness / 2) * sina, (eyes_applet->wall_thickness / 2) * cosa);
93
94         *pupil_x = temp * sina + (eyes_applet->eye_width / 2);
95         *pupil_y = temp * cosa + (eyes_applet->eye_height / 2);
96 }
97
98
99
100 static void
101 draw_eye (EyesPluginContent *eyes,
102           gint    eye_num,
103           gint    pupil_x,
104           gint    pupil_y)
105 {
106     GdkPixbuf *pixbuf;
107     GdkRectangle rect, r1, r2;
108
109     pixbuf = gdk_pixbuf_copy (eyes->eye_image);
110     r1.x = pupil_x - eyes->pupil_width / 2;
111     r1.y = pupil_y - eyes->pupil_height / 2;
112     r1.width = eyes->pupil_width;
113     r1.height = eyes->pupil_height;
114     r2.x = 0;
115     r2.y = 0;
116     r2.width = eyes->eye_width;
117     r2.height = eyes->eye_height;
118     if (gdk_rectangle_intersect (&r1, &r2, &rect))
119     {
120         gdk_pixbuf_composite (eyes->pupil_image, pixbuf,
121                            rect.x,
122                            rect.y,
123                            rect.width,
124                                  rect.height,
125                                  pupil_x - eyes->pupil_width / 2,
126                            pupil_y - eyes->pupil_height / 2, 1.0, 1.0,
127                                  GDK_INTERP_BILINEAR,
128                                255);
129         gtk_image_set_from_pixbuf (GTK_IMAGE (eyes->eyes[eye_num]),
130                               pixbuf);
131     }
132     g_object_unref (G_OBJECT (pixbuf));
133 }
134
135
136
137 static gint
138 timer_cb(EyesPluginContent *eyes)
139 {
140     gint x, y, z;
141     gint pupil_x, pupil_y;
142     gint i;
143
144     for (i = 0; i < eyes->num_eyes; i++)
145     {
146         if (GTK_WIDGET_REALIZED(eyes->eyes[i]))
147         {
148             accel_read(&x, &y, &z);
149             x = -x;
150             y = -y;
151
152             if ((x != eyes->pointer_last_x[i]) || (y != eyes->pointer_last_y[i]))
153             {
154
155                 calculate_pupil_xy (eyes, x, y, &pupil_x, &pupil_y, eyes->eyes[i]);
156                 draw_eye (eyes, i, pupil_x, pupil_y);
157
158                 eyes->pointer_last_x[i] = x;
159                 eyes->pointer_last_y[i] = y;
160             }
161         }
162     }
163
164     return TRUE;
165 }
166
167
168
169 static void
170 properties_load(EyesPluginContent *eyes)
171 {
172     gchar *path;
173
174     if (eyes->active_theme)
175         path = g_build_filename(THEMESDIR, eyes->active_theme, NULL);
176     else
177         path = g_build_filename(THEMESDIR, DEFAULTTHEME, NULL);
178
179     load_theme(eyes, path);
180
181     g_free(path);
182 }
183
184
185
186 static void
187 setup_eyes(EyesPluginContent *eyes)
188 {
189     g_warning ("setup_eyes");
190     int i;
191
192     if (eyes->hbox != NULL)
193     {
194         gtk_widget_destroy(eyes->hbox);
195         eyes->hbox = NULL;
196     }
197
198     eyes->hbox = gtk_hbox_new(FALSE, 0);
199     gtk_container_add(GTK_CONTAINER(eyes->align), GTK_WIDGET(eyes->hbox));
200
201     eyes->eyes = g_new0 (GtkWidget *, eyes->num_eyes);
202         eyes->pointer_last_x = g_new0 (gint, eyes->num_eyes);
203         eyes->pointer_last_y = g_new0 (gint, eyes->num_eyes);
204
205     for (i = 0; i < eyes->num_eyes; i++)
206     {
207         eyes->eyes[i] = gtk_image_new ();
208
209         gtk_widget_set_size_request(GTK_WIDGET(eyes->eyes[i]),
210                                     eyes->eye_width,
211                                     eyes->eye_height);
212
213         gtk_widget_show(eyes->eyes[i]);
214
215         gtk_box_pack_start(GTK_BOX(eyes->hbox), eyes->eyes[i],
216                            FALSE, FALSE, 0);
217
218                 if ((eyes->num_eyes != 1) && (i == 0))
219             gtk_misc_set_alignment (GTK_MISC (eyes->eyes[i]), 1.0, 0.5);
220                 else if ((eyes->num_eyes != 1) && (i == eyes->num_eyes - 1))
221                         gtk_misc_set_alignment (GTK_MISC (eyes->eyes[i]), 0.0, 0.5);
222                 else
223                         gtk_misc_set_alignment (GTK_MISC (eyes->eyes[i]), 0.5, 0.5);
224
225                 eyes->pointer_last_x[i] = G_MAXINT;
226                 eyes->pointer_last_y[i] = G_MAXINT;
227
228                 draw_eye (eyes, i,
229                               eyes->eye_width / 2,
230                   eyes->eye_height / 2);
231     }
232
233     gtk_widget_show(eyes->hbox);
234 }
235
236
237
238 static gboolean
239 eyes_applet_fill(EyesPluginContent *eyes)
240 {
241     gtk_widget_show_all(GTK_WIDGET(eyes->align));
242
243     if (eyes->timeout_id == 0)
244     {
245         eyes->timeout_id = g_timeout_add (UPDATE_TIMEOUT,
246                                           (GtkFunction)timer_cb, eyes);
247     }
248
249     return TRUE;
250 }
251
252 /******************************
253  *** Panel Plugin Functions ***
254  ******************************/
255 static void
256 eyes_free_data(EyesPluginContent      *eyes)
257 {
258     g_warning("eyes_free_data");
259     g_return_if_fail(eyes != NULL);
260
261     if (eyes->timeout_id != 0)
262         g_source_remove (eyes->timeout_id);
263
264     g_free (eyes->eyes);
265         g_free (eyes->pointer_last_x);
266         g_free (eyes->pointer_last_y);
267
268         if (eyes->active_theme != NULL)
269                 g_free (eyes->active_theme);
270
271     if (eyes->eye_image != NULL)
272         g_object_unref (G_OBJECT (eyes->eye_image));
273
274     if (eyes->pupil_image != NULL)
275         g_object_unref (G_OBJECT (eyes->pupil_image));
276
277     if (eyes->theme_dir != NULL)
278         g_free(eyes->theme_dir);
279
280     if (eyes->theme_name != NULL)
281         g_free(eyes->theme_name);
282
283     if (eyes->eye_filename != NULL)
284         g_free(eyes->eye_filename);
285
286     if (eyes->pupil_filename != NULL)
287         g_free(eyes->pupil_filename);
288
289     g_free(eyes);
290 }
291
292 static void
293 eyes_read_rc_file (EyesPluginContent      *eyes)
294 {
295     g_warning ("eyes_read_rc");
296     if (eyes->active_theme == NULL)
297                 eyes->active_theme = g_strdup (DEFAULTTHEME);
298 }
299
300 static EyesPluginContent *
301 eyes_plugin_new ()
302 {
303     g_warning ("eyes_plugin_new");
304     EyesPluginContent *eyes;
305
306     eyes = g_new0(EyesPluginContent, 1);
307
308     eyes->align = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
309
310     gtk_widget_show(GTK_WIDGET(eyes->align));
311
312     eyes_read_rc_file (eyes);
313
314     properties_load(eyes);
315     setup_eyes(eyes);
316     eyes_applet_fill(eyes);
317
318     return eyes;
319 }
320
321 static void eyes_check_desktop (GObject *gobject, GParamSpec *pspec, EyesPluginContent *eyes)
322 {
323         g_warning ("eyes_check_desktop");
324         gchar *name = pspec->name;
325         gboolean status;
326         g_object_get (gobject, name, &status, NULL);
327         if (status) {
328             eyes_applet_fill(eyes);
329         } else if (eyes->timeout_id != 0) {
330         g_source_remove (eyes->timeout_id);
331                 eyes->timeout_id = 0;
332         }
333 }
334
335 static void eyes_plugin_dispose (GObject *object)
336 {
337         g_warning ("eyes_plugin_dispose");
338
339         G_OBJECT_CLASS (eyes_plugin_parent_class)->dispose (object);
340 }
341
342 static void eyes_plugin_finalize (GObject *object)
343 {
344         g_warning ("eyes_plugin_finalize");
345         EyesPlugin *self = EYES_HOME_PLUGIN (object);
346
347     eyes_free_data(self->priv);
348
349         G_OBJECT_CLASS (eyes_plugin_parent_class)->finalize (object);
350 }
351
352 static void
353 eyes_plugin_realize (GtkWidget *widget)
354 {
355     g_warning ("eyes_plugin_realize");
356     GdkScreen *screen = gtk_widget_get_screen (widget);
357     gtk_widget_set_colormap (widget, gdk_screen_get_rgba_colormap (screen));
358     gtk_widget_set_app_paintable (widget, TRUE);
359
360     GTK_WIDGET_CLASS (eyes_plugin_parent_class)->realize (widget);
361 }
362
363 static gboolean
364 eyes_plugin_expose_event (GtkWidget *widget, GdkEventExpose *event)
365 {
366     g_warning ("eyes_plugin_expose_event");
367     cairo_t *cr;
368
369     cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
370     gdk_cairo_region (cr, event->region);
371     cairo_clip (cr);
372
373     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
374     cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
375     cairo_paint (cr);
376   
377     cairo_destroy (cr);
378
379     return GTK_WIDGET_CLASS (eyes_plugin_parent_class)->expose_event (widget, event);
380 }
381
382 static void
383 eyes_plugin_init (EyesPlugin *desktop_plugin)
384 {
385     g_warning ("eyes_plugin_init");
386     EyesPluginContent *eyes;
387
388     eyes = eyes_plugin_new (desktop_plugin);
389     desktop_plugin->priv = eyes;
390
391     g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
392         G_CALLBACK (eyes_check_desktop), eyes);
393
394     gtk_container_add (GTK_CONTAINER (desktop_plugin), eyes->align);
395
396
397 static void
398 eyes_plugin_class_init (EyesPluginClass *klass) {
399     g_warning ("eyes_plugin_class_init");
400     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
401     GObjectClass *object_class = G_OBJECT_CLASS (klass);
402
403         object_class->dispose = eyes_plugin_dispose;
404         object_class->finalize = eyes_plugin_finalize;
405
406     widget_class->realize = eyes_plugin_realize;
407     widget_class->expose_event = eyes_plugin_expose_event;
408     g_type_class_add_private (klass, sizeof (EyesPluginContent));
409
410
411 static void
412 eyes_plugin_class_finalize (EyesPluginClass *class) {} 
413