checked warnings, removed theme orrery
[livewp] / applet / src / fifteen.c
1 /*vim: set sw=4 ts=4 et: */
2 /*
3  * This file is part of Live Wallpaper (livewp)
4  * 
5  * Copyright (C) 2010 Vlad Vasiliev
6  * Copyright (C) 2010 Tanya Makova
7  *       for the code
8  * 
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  * 
14  * This software is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this software; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23 */
24 /*******************************************************************************/
25 #include <hildon/hildon.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include "livewp-common.h"
29 #include "livewp-actor.h"
30 #include "livewp-dbus.h"
31
32 enum {
33     UP = 0,
34     RIGHT = 1,
35     DOWN = 2,
36     LEFT = 3
37 };
38
39 const gint width = 110;
40 const gint height = 110;
41
42 typedef struct _Scene1 Scene1;
43 struct _Scene1 
44 {
45     gint *pg;
46     //Actor *actors;
47     gint empty;
48     gint bone;
49     gint timer_num;
50 };
51 Scene1 *scene;
52
53 Actor *actors[15];
54
55 void init_pg(gint *pg)
56 {
57     srand(time(NULL));
58     gint i, j, t;
59     for (i=0; i<16; i++){
60         pg[i] = i;
61     }
62     for (i=0; i<15; i++){
63         j = rand()%15;
64         t = pg[i];
65         pg[i] = pg[j];
66         pg[j] = t;
67     }
68 }
69 void reinit(AWallpaperPlugin *desktop_plugin)
70 {
71     fprintf(stderr, "reinit\n");
72     gint i;
73     Actor *actor;
74     init_pg(scene->pg);
75
76     scene->empty = 15;
77     for (i=0; i<15; i++){
78         actor = actors[scene->pg[i]];
79         actor->x = (i%4)*width;
80         actor->y = (i/4)*height;
81         //fprintf(stderr, "x=%d, y=%d\n", actor->x, actor->y);
82         set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
83         //set_actor_position(actor, (i%4)*width, (i/4)*height, 2, desktop_plugin);
84     }
85 }
86
87 void moving_actor(gint num, gint max, AWallpaperPlugin *desktop_plugin)
88 {
89     Actor *actor = actors[scene->pg[scene->bone]];
90     gint x0 = actor->x, y0 = actor->y,
91          x1 = (scene->empty%4)*width, y1 = (scene->empty/4)*height,
92          x,y;
93     //x = x0 + (x1-x0)*(max - num)/max;
94     //y = y0 + (y1-y0)*(max - num)/max;
95     x = x0 + (x1-x0)*(max-num)*(max-num)/(max*max);
96     y = y0 + (y1-y0)*(max-num)*(max-num)/(max*max);
97     set_actor_position(actor, x, y, actor->z, desktop_plugin);
98     if (num == 0){
99         actor->x = x;
100         actor->y = y;
101         scene->pg[scene->empty] = scene->pg[scene->bone];
102         scene->pg[scene->bone] = 15;
103         scene->empty = scene->bone;
104     }
105 }
106 void moving_all(gint num, gint max, AWallpaperPlugin *desktop_plugin)
107 {
108     gint i, axis;
109     double angle;
110     angle = 360*(max - num)*(max-num)/(max*max);
111     for (i=0; i<15; i++){
112         if (i%2 == 0) axis = HILDON_AA_X_AXIS;
113         else axis = HILDON_AA_Y_AXIS;
114         set_actor_rotation(actors[i], axis, angle, width/2, height/2, 0);
115     }
116     //if (num == (int)max/2){
117     if (num == 0){
118         reinit(desktop_plugin);
119     }
120 }
121 gboolean main_timer(AWallpaperPlugin *desktop_plugin)
122 {
123     if (scene->timer_num > 0){
124         scene->timer_num--;
125         if (scene->bone>-1)
126             moving_actor(scene->timer_num, 10, desktop_plugin);
127         else 
128             moving_all(scene->timer_num, 15, desktop_plugin);
129         return TRUE;
130     } 
131     char * accel_filename = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
132     //char * accel_filename = "/home/tanya/coord";
133
134     gint bone;
135     FILE *fd = NULL;
136     int rs, ax, ay, az;
137     fd = fopen(accel_filename, "r");
138     if (fd == NULL){
139         fprintf(stderr, "cannot open file\n");
140         return TRUE;
141     }
142     rs = fscanf((FILE*)fd, "%i %i %i", &ax, &ay, &az);
143     fclose(fd);
144     if (rs != 3){
145         fprintf(stderr, "cannot read information from file\n");
146         return TRUE;
147     }
148
149     //fprintf(stderr, "change obj %i %i %i\n", ax, ay, az);
150     if (az < -2000) {
151         //reinit(desktop_plugin);
152         //sleep(1);
153         scene->timer_num = 15;
154         scene->bone = -1;
155         return TRUE;
156     }
157     if (abs(ax) - abs(ay) > 300){
158         if (ax > 0) {
159             // LEFT;
160             bone = scene->empty + 1;
161             if (bone % 4 == 0) return TRUE;
162         }
163         else {
164             // RIGHT;
165             bone = scene->empty - 1;
166             if (scene->empty % 4 == 0) return TRUE;
167         }
168     }else
169     if (abs(ay) - abs(ax) > 300){
170         if (ay > 0){ 
171             // UP;
172             bone = scene->empty + 4;
173             if (bone > 15) return TRUE;
174         }
175         else {
176             // DOWN;
177             bone = scene->empty - 4;
178             if (bone < 0) return TRUE;
179         }
180     } else return TRUE;
181     //fprintf(stderr, "move %d\n", bone);
182     scene->bone = bone;
183     scene->timer_num = 10;
184         
185     return TRUE;
186 }
187 void init_actors(AWallpaperPlugin *desktop_plugin)
188 {
189     gint i;
190     Actor *actor;
191     actor = init_object(desktop_plugin, "background", g_strdup("background.jpg"),
192                                 -180, -15, 2, 800, 480,
193                                 TRUE, TRUE, 100, 255,
194                                 NULL, NULL, NULL);
195     for (i=0; i<15; i++){
196         actors[i] = init_object(desktop_plugin, "bone", g_strdup_printf("%d.png", i+1),
197                                 0, 0, 2, width, height,
198                                 TRUE, TRUE, 100, 255,
199                                 NULL, NULL, NULL);
200     }
201     //return actors;
202 }
203 void
204 quit_from_program (Animation_WallpaperPrivate *priv)
205 {
206     gtk_main_quit();
207 }
208
209 void
210 view_state_changed (Animation_WallpaperPrivate *priv)
211 {
212     if (priv->visible && priv->long_timer == 0){
213         priv->long_timer = g_timeout_add(100, (GtkFunction)main_timer, priv->desktop_plugin);
214         //fprintf(stderr, "visible = 1 timeout_add %d\n", priv->long_timer);
215     }else {
216         //fprintf(stderr, "visible = 0 timer remove %d\n", priv->long_timer);
217         g_source_remove(priv->long_timer);
218         priv->long_timer = 0;
219     }
220     
221 }
222 gint
223 read_config (Animation_WallpaperPrivate *priv)
224 {
225     return 0;
226 }
227 void
228 reload_scene(AWallpaperPlugin *desktop_plugin){}
229
230 int main( int   argc, char *argv[] )
231 {
232     GtkWidget *window;
233     AWallpaperPlugin *desktop_plugin = g_new0 (AWallpaperPlugin, 1);
234     Animation_WallpaperPrivate *priv = g_new0 (Animation_WallpaperPrivate, 1);
235     scene = g_new0(Scene1, 1);
236     gint c, window_id=0, view=0;
237     gint pg[16];
238
239     while ((c = getopt(argc, argv, ":v:w:")) != -1){
240         switch (c){
241             case 'v':
242                 view = atoi(optarg);
243                 break;
244             case 'w':
245                 window_id = atoi(optarg);
246         }
247     }
248
249     //fprintf(stderr, "view=%d window_id=%d\n", view, window_id);
250
251     hildon_gtk_init (&argc, &argv);
252     g_set_application_name ("Fifteen");
253     window = hildon_window_new ();
254     //gtk_window_fullscreen (GTK_WINDOW(window));
255     gtk_window_set_title(GTK_WINDOW(window),"Fifteen");
256     gtk_window_set_wmclass(GTK_WINDOW(window),"Fifteen_exec","Fifteen_exec");
257     //window->window = window_id;
258     g_signal_connect (G_OBJECT (window), "delete_event",
259                         G_CALLBACK (gtk_main_quit), NULL);
260     priv->window = window;
261     priv->theme = g_strdup("Fifteen"); 
262     priv->xapplet = -180;
263     priv->yapplet = -15;
264     priv->desktop_plugin = desktop_plugin;
265     priv->view = view;
266     priv->long_timer = 0;
267     desktop_plugin->priv = priv;
268
269     priv->osso = osso_initialize("org.maemo.livewp", VERSION, TRUE, NULL);
270     livewp_initialize_dbus(priv);
271     
272
273     //init_pg(pg);
274     scene->pg = pg;
275     //scene->actors = init_actors(desktop_plugin);
276     init_actors(desktop_plugin);
277     scene->timer_num = 0;
278     reinit(desktop_plugin);
279     gtk_widget_show  (window);
280     //priv->long_timer = g_timeout_add(100, main_timer, desktop_plugin);
281     //fprintf(stderr, "create timer %d\n", priv->long_timer);
282     gtk_main ();
283     return 0;
284 }
285
286