changed timer values in theme fifteen
[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 const gint width = 110;
33 const gint height = 110;
34 const gint timeout = 50; 
35 const gint time_bone = 15; 
36 const gint time_reload = 20; 
37
38 typedef struct _Scene1 Scene1;
39 struct _Scene1 
40 {
41     gint *pg;
42     //Actor *actors;
43     gint empty;
44     gint bone;
45     gint timer_num;
46 };
47 Scene1 *scene;
48
49 Actor *actors[15];
50
51 void init_pg(gint *pg)
52 {
53     srand(time(NULL));
54     gint i, j, t;
55     for (i=0; i<16; i++){
56         pg[i] = i;
57     }
58     for (i=0; i<15; i++){
59         j = rand()%15;
60         t = pg[i];
61         pg[i] = pg[j];
62         pg[j] = t;
63     }
64 }
65 void reinit(AWallpaperPlugin *desktop_plugin)
66 {
67     fprintf(stderr, "reinit\n");
68     gint i;
69     Actor *actor;
70     init_pg(scene->pg);
71
72     scene->empty = 15;
73     for (i=0; i<15; i++){
74         actor = actors[scene->pg[i]];
75         actor->x = (i%4)*width;
76         actor->y = (i/4)*height;
77         //fprintf(stderr, "x=%d, y=%d\n", actor->x, actor->y);
78         set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
79         //set_actor_position(actor, (i%4)*width, (i/4)*height, 2, desktop_plugin);
80     }
81 }
82
83 void moving_actor(gint num, gint max, AWallpaperPlugin *desktop_plugin)
84 {
85     Actor *actor = actors[scene->pg[scene->bone]];
86     gint x0 = actor->x, y0 = actor->y,
87          x1 = (scene->empty%4)*width, y1 = (scene->empty/4)*height,
88          x,y;
89     //x = x0 + (x1-x0)*(max - num)/max;
90     //y = y0 + (y1-y0)*(max - num)/max;
91     x = x0 + (x1-x0)*(max-num)*(max-num)/(max*max);
92     y = y0 + (y1-y0)*(max-num)*(max-num)/(max*max);
93     set_actor_position(actor, x, y, actor->z, desktop_plugin);
94     if (num == 0){
95         actor->x = x;
96         actor->y = y;
97         scene->pg[scene->empty] = scene->pg[scene->bone];
98         scene->pg[scene->bone] = 15;
99         scene->empty = scene->bone;
100     }
101 }
102 void moving_all(gint num, gint max, AWallpaperPlugin *desktop_plugin)
103 {
104     gint i, axis;
105     double angle;
106     angle = 360*(max - num)*(max-num)/(max*max);
107     for (i=0; i<15; i++){
108         if (i%2 == 0) axis = HILDON_AA_X_AXIS;
109         else axis = HILDON_AA_Y_AXIS;
110         set_actor_rotation(actors[i], axis, angle, width/2, height/2, 0);
111     }
112     //if (num == (int)max/2){
113     if (num == 0){
114         reinit(desktop_plugin);
115     }
116 }
117 gboolean main_timer(AWallpaperPlugin *desktop_plugin)
118 {
119     if (scene->timer_num > 0){
120         scene->timer_num--;
121         if (scene->bone>-1)
122             moving_actor(scene->timer_num, time_bone, desktop_plugin);
123         else 
124             moving_all(scene->timer_num, time_reload, desktop_plugin);
125         return TRUE;
126     } 
127     char * accel_filename = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
128     //char * accel_filename = "/home/tanya/coord";
129
130     gint bone;
131     FILE *fd = NULL;
132     int rs, ax, ay, az;
133     fd = fopen(accel_filename, "r");
134     if (fd == NULL){
135         fprintf(stderr, "cannot open file\n");
136         return TRUE;
137     }
138     rs = fscanf((FILE*)fd, "%i %i %i", &ax, &ay, &az);
139     fclose(fd);
140     if (rs != 3){
141         fprintf(stderr, "cannot read information from file\n");
142         return TRUE;
143     }
144
145     //fprintf(stderr, "change obj %i %i %i\n", ax, ay, az);
146     if (az < -2000) {
147         //reinit(desktop_plugin);
148         //sleep(1);
149         scene->timer_num = time_reload;
150         scene->bone = -1;
151         return TRUE;
152     }
153     if (abs(ax) - abs(ay) > 300){
154         if (ax > 0) {
155             // LEFT;
156             bone = scene->empty + 1;
157             if (bone % 4 == 0) return TRUE;
158         }
159         else {
160             // RIGHT;
161             bone = scene->empty - 1;
162             if (scene->empty % 4 == 0) return TRUE;
163         }
164     }else
165     if (abs(ay) - abs(ax) > 300){
166         if (ay > 0){ 
167             // UP;
168             bone = scene->empty + 4;
169             if (bone > 15) return TRUE;
170         }
171         else {
172             // DOWN;
173             bone = scene->empty - 4;
174             if (bone < 0) return TRUE;
175         }
176     } else return TRUE;
177     //fprintf(stderr, "move %d\n", bone);
178     scene->bone = bone;
179     scene->timer_num = time_bone;
180         
181     return TRUE;
182 }
183 void init_actors(AWallpaperPlugin *desktop_plugin)
184 {
185     gint i;
186     Actor *actor;
187     actor = init_object(desktop_plugin, "background", g_strdup("background.jpg"),
188                                 -180, -15, 2, 800, 480,
189                                 TRUE, TRUE, 100, 255,
190                                 NULL, NULL, NULL);
191     for (i=0; i<15; i++){
192         actors[i] = init_object(desktop_plugin, "bone", g_strdup_printf("%d.png", i+1),
193                                 0, 0, 2, width, height,
194                                 TRUE, TRUE, 100, 255,
195                                 NULL, NULL, NULL);
196     }
197     //return actors;
198 }
199 void
200 quit_from_program (Animation_WallpaperPrivate *priv)
201 {
202     gtk_main_quit();
203 }
204
205 void
206 view_state_changed (Animation_WallpaperPrivate *priv)
207 {
208     if (priv->visible && priv->long_timer == 0){
209         priv->long_timer = g_timeout_add(timeout, (GtkFunction)main_timer, priv->desktop_plugin);
210         //fprintf(stderr, "visible = 1 timeout_add %d\n", priv->long_timer);
211     }else {
212         //fprintf(stderr, "visible = 0 timer remove %d\n", priv->long_timer);
213         g_source_remove(priv->long_timer);
214         priv->long_timer = 0;
215     }
216     
217 }
218 gint
219 read_config (Animation_WallpaperPrivate *priv)
220 {
221     return 0;
222 }
223 void
224 reload_scene(AWallpaperPlugin *desktop_plugin){}
225
226 int main( int   argc, char *argv[] )
227 {
228     GtkWidget *window;
229     AWallpaperPlugin *desktop_plugin = g_new0 (AWallpaperPlugin, 1);
230     Animation_WallpaperPrivate *priv = g_new0 (Animation_WallpaperPrivate, 1);
231     scene = g_new0(Scene1, 1);
232     gint c, window_id=0, view=0;
233     gint pg[16];
234
235     while ((c = getopt(argc, argv, ":v:w:")) != -1){
236         switch (c){
237             case 'v':
238                 view = atoi(optarg);
239                 break;
240             case 'w':
241                 window_id = atoi(optarg);
242         }
243     }
244
245     //fprintf(stderr, "view=%d window_id=%d\n", view, window_id);
246
247     hildon_gtk_init (&argc, &argv);
248     g_set_application_name ("Fifteen");
249     window = hildon_window_new ();
250     //gtk_window_fullscreen (GTK_WINDOW(window));
251     gtk_window_set_title(GTK_WINDOW(window),"Fifteen");
252     gtk_window_set_wmclass(GTK_WINDOW(window),"Fifteen_exec","Fifteen_exec");
253     //window->window = window_id;
254     g_signal_connect (G_OBJECT (window), "delete_event",
255                         G_CALLBACK (gtk_main_quit), NULL);
256     priv->window = window;
257     priv->theme = g_strdup("Fifteen"); 
258     priv->xapplet = -180;
259     priv->yapplet = -15;
260     priv->desktop_plugin = desktop_plugin;
261     priv->view = view;
262     priv->long_timer = 0;
263     desktop_plugin->priv = priv;
264
265     priv->osso = osso_initialize("org.maemo.livewp", VERSION, TRUE, NULL);
266     livewp_initialize_dbus(priv);
267     
268
269     //init_pg(pg);
270     scene->pg = pg;
271     //scene->actors = init_actors(desktop_plugin);
272     init_actors(desktop_plugin);
273     scene->timer_num = 0;
274     reinit(desktop_plugin);
275     gtk_widget_show  (window);
276     //priv->long_timer = g_timeout_add(100, main_timer, desktop_plugin);
277     //fprintf(stderr, "create timer %d\n", priv->long_timer);
278     gtk_main ();
279     return 0;
280 }
281
282