1 /* $Id: eyes.c 2397 2007-01-17 17:46:35Z nick $
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>
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)
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
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
33 #include <sys/types.h>
37 #include <libhildondesktop/libhildondesktop.h>
41 #include "accelerometer.h"
44 #define EYES_ROOT "Eyes"
45 #define DEFAULTTHEME "Default"
46 #define UPDATE_TIMEOUT 100
48 #define EYES_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,\
49 EYES_TYPE_HOME_PLUGIN,\
52 HD_DEFINE_PLUGIN_MODULE (EyesPlugin, eyes_plugin, HD_TYPE_HOME_PLUGIN_ITEM)
54 /*****************************
55 *** Eyes Plugin Functions ***
56 *****************************/
58 calculate_pupil_xy (EyesPluginContent *eyes_applet,
60 gint *pupil_x, gint *pupil_y, GtkWidget* widget)
68 gfloat xalign, yalign;
71 width = GTK_WIDGET(widget)->allocation.width;
72 height = GTK_WIDGET(widget)->allocation.height;
73 gtk_misc_get_alignment(GTK_MISC(widget), &xalign, &yalign);
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 )
82 *pupil_x = nx + eyes_applet->eye_width / 2;
83 *pupil_y = ny + eyes_applet->eye_height / 2;
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);
94 *pupil_x = temp * sina + (eyes_applet->eye_width / 2);
95 *pupil_y = temp * cosa + (eyes_applet->eye_height / 2);
101 draw_eye (EyesPluginContent *eyes,
107 GdkRectangle rect, r1, r2;
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;
116 r2.width = eyes->eye_width;
117 r2.height = eyes->eye_height;
118 if (gdk_rectangle_intersect (&r1, &r2, &rect))
120 gdk_pixbuf_composite (eyes->pupil_image, pixbuf,
125 pupil_x - eyes->pupil_width / 2,
126 pupil_y - eyes->pupil_height / 2, 1.0, 1.0,
129 gtk_image_set_from_pixbuf (GTK_IMAGE (eyes->eyes[eye_num]),
132 g_object_unref (G_OBJECT (pixbuf));
138 timer_cb(EyesPluginContent *eyes)
141 gint pupil_x, pupil_y;
144 for (i = 0; i < eyes->num_eyes; i++)
146 if (GTK_WIDGET_REALIZED(eyes->eyes[i]))
148 accel_read(&x, &y, &z);
152 if ((x != eyes->pointer_last_x[i]) || (y != eyes->pointer_last_y[i]))
155 calculate_pupil_xy (eyes, x, y, &pupil_x, &pupil_y, eyes->eyes[i]);
156 draw_eye (eyes, i, pupil_x, pupil_y);
158 eyes->pointer_last_x[i] = x;
159 eyes->pointer_last_y[i] = y;
170 properties_load(EyesPluginContent *eyes)
174 if (eyes->active_theme)
175 path = g_build_filename(THEMESDIR, eyes->active_theme, NULL);
177 path = g_build_filename(THEMESDIR, DEFAULTTHEME, NULL);
179 load_theme(eyes, path);
187 setup_eyes(EyesPluginContent *eyes)
189 g_warning ("setup_eyes");
192 if (eyes->hbox != NULL)
194 gtk_widget_destroy(eyes->hbox);
198 eyes->hbox = gtk_hbox_new(FALSE, 0);
199 gtk_container_add(GTK_CONTAINER(eyes->align), GTK_WIDGET(eyes->hbox));
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);
205 for (i = 0; i < eyes->num_eyes; i++)
207 eyes->eyes[i] = gtk_image_new ();
209 gtk_widget_set_size_request(GTK_WIDGET(eyes->eyes[i]),
213 gtk_widget_show(eyes->eyes[i]);
215 gtk_box_pack_start(GTK_BOX(eyes->hbox), eyes->eyes[i],
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);
223 gtk_misc_set_alignment (GTK_MISC (eyes->eyes[i]), 0.5, 0.5);
225 eyes->pointer_last_x[i] = G_MAXINT;
226 eyes->pointer_last_y[i] = G_MAXINT;
230 eyes->eye_height / 2);
233 gtk_widget_show(eyes->hbox);
239 eyes_applet_fill(EyesPluginContent *eyes)
241 gtk_widget_show_all(GTK_WIDGET(eyes->align));
243 if (eyes->timeout_id == 0)
245 eyes->timeout_id = g_timeout_add (UPDATE_TIMEOUT,
246 (GtkFunction)timer_cb, eyes);
252 /******************************
253 *** Panel Plugin Functions ***
254 ******************************/
256 eyes_free_data(EyesPluginContent *eyes)
258 g_warning("eyes_free_data");
259 g_return_if_fail(eyes != NULL);
261 if (eyes->timeout_id != 0)
262 g_source_remove (eyes->timeout_id);
265 g_free (eyes->pointer_last_x);
266 g_free (eyes->pointer_last_y);
268 if (eyes->active_theme != NULL)
269 g_free (eyes->active_theme);
271 if (eyes->eye_image != NULL)
272 g_object_unref (G_OBJECT (eyes->eye_image));
274 if (eyes->pupil_image != NULL)
275 g_object_unref (G_OBJECT (eyes->pupil_image));
277 if (eyes->theme_dir != NULL)
278 g_free(eyes->theme_dir);
280 if (eyes->theme_name != NULL)
281 g_free(eyes->theme_name);
283 if (eyes->eye_filename != NULL)
284 g_free(eyes->eye_filename);
286 if (eyes->pupil_filename != NULL)
287 g_free(eyes->pupil_filename);
293 eyes_read_rc_file (EyesPluginContent *eyes)
295 g_warning ("eyes_read_rc");
296 if (eyes->active_theme == NULL)
297 eyes->active_theme = g_strdup (DEFAULTTHEME);
300 static EyesPluginContent *
303 g_warning ("eyes_plugin_new");
304 EyesPluginContent *eyes;
306 eyes = g_new0(EyesPluginContent, 1);
308 eyes->align = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
310 gtk_widget_show(GTK_WIDGET(eyes->align));
312 eyes_read_rc_file (eyes);
314 properties_load(eyes);
316 eyes_applet_fill(eyes);
321 static void eyes_check_desktop (GObject *gobject, GParamSpec *pspec, EyesPluginContent *eyes)
323 g_warning ("eyes_check_desktop");
324 gchar *name = pspec->name;
326 g_object_get (gobject, name, &status, NULL);
328 eyes_applet_fill(eyes);
329 } else if (eyes->timeout_id != 0) {
330 g_source_remove (eyes->timeout_id);
331 eyes->timeout_id = 0;
335 static void eyes_plugin_dispose (GObject *object)
337 g_warning ("eyes_plugin_dispose");
339 G_OBJECT_CLASS (eyes_plugin_parent_class)->dispose (object);
342 static void eyes_plugin_finalize (GObject *object)
344 g_warning ("eyes_plugin_finalize");
345 EyesPlugin *self = EYES_HOME_PLUGIN (object);
347 eyes_free_data(self->priv);
349 G_OBJECT_CLASS (eyes_plugin_parent_class)->finalize (object);
353 eyes_plugin_realize (GtkWidget *widget)
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);
360 GTK_WIDGET_CLASS (eyes_plugin_parent_class)->realize (widget);
364 eyes_plugin_expose_event (GtkWidget *widget, GdkEventExpose *event)
366 g_warning ("eyes_plugin_expose_event");
369 cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
370 gdk_cairo_region (cr, event->region);
373 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
374 cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
379 return GTK_WIDGET_CLASS (eyes_plugin_parent_class)->expose_event (widget, event);
383 eyes_plugin_init (EyesPlugin *desktop_plugin)
385 g_warning ("eyes_plugin_init");
386 EyesPluginContent *eyes;
388 eyes = eyes_plugin_new (desktop_plugin);
389 desktop_plugin->priv = eyes;
391 g_signal_connect (desktop_plugin, "notify::is-on-current-desktop",
392 G_CALLBACK (eyes_check_desktop), eyes);
394 gtk_container_add (GTK_CONTAINER (desktop_plugin), eyes->align);
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);
403 object_class->dispose = eyes_plugin_dispose;
404 object_class->finalize = eyes_plugin_finalize;
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));
412 eyes_plugin_class_finalize (EyesPluginClass *class) {}