done fifteen
[livewp] / applet / src / fifteen.c
1 #include <hildon/hildon.h>
2 #include <time.h>
3 #include "livewp-common.h"
4 #include "livewp-actor.h"
5
6 enum {
7     UP = 0,
8     RIGHT = 1,
9     DOWN = 2,
10     LEFT = 3
11 };
12
13 const gint width = 200;
14 const gint height = 120;
15
16 typedef struct _Scene1 Scene1;
17 struct _Scene1 
18 {
19     gint *pg;
20     //Actor *actors;
21     gint empty;
22     gint bone;
23     gint timer_num;
24 };
25 Scene1 *scene;
26
27 Actor *actors[15];
28
29 void init_pg(gint *pg)
30 {
31     srand(time(NULL));
32     gint i, j, t;
33     for (i=0; i<16; i++){
34         pg[i] = i;
35     }
36     for (i=0; i<15; i++){
37         j = rand()%15;
38         t = pg[i];
39         pg[i] = pg[j];
40         pg[j] = t;
41     }
42 }
43 #if 0
44 gint move(gint direction, AWallpaperPlugin *desktop_plugin)
45 {
46     gint bone;
47     Actor *actor;
48     switch (direction) {
49         case UP:
50             bone = empty + 4;
51             if (bone > 15) return -1;
52             break;
53         case RIGHT:
54             bone = empty - 1;
55             if (empty % 4 == 0) return -1;
56             break;
57         case DOWN:
58             bone = empty - 4;
59             if (bone < 0) return -1;
60             break;
61         case LEFT:
62             bone = empty + 1;
63             if (bone % 4 == 0) return -1;
64     }
65     //actor = actors[pg[bone]];
66     cur = bone;
67     //set_actor_position(actor, (empty%4)*width, (empty/4)*height, actor->z, desktop_plugin);
68     //pg[empty] = pg[bone];
69     //pg[bone] = 15;
70     //empty = bone;
71     return pg[bone]; 
72 }
73 #endif
74 void reinit(AWallpaperPlugin *desktop_plugin)
75 {
76     fprintf(stderr, "reinit\n");
77     gint i;
78     Actor *actor;
79     init_pg(scene->pg);
80
81     scene->empty = 15;
82     for (i=0; i<15; i++){
83         actor = actors[scene->pg[i]];
84         actor->x = (i%4)*width;
85         actor->y = (i/4)*height;
86         //fprintf(stderr, "x=%d, y=%d\n", actor->x, actor->y);
87         set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
88         //set_actor_position(actor, (i%4)*width, (i/4)*height, 2, desktop_plugin);
89     }
90 }
91
92 void moving_actor(gint num, gint max, AWallpaperPlugin *desktop_plugin)
93 {
94     Actor *actor = actors[scene->pg[scene->bone]];
95     gint x0 = actor->x, y0 = actor->y,
96          x1 = (scene->empty%4)*width, y1 = (scene->empty/4)*height,
97          x,y;
98     //x = x0 + (x1-x0)*(max - num)/max;
99     //y = y0 + (y1-y0)*(max - num)/max;
100     x = x0 + (x1-x0)*(max-num)*(max-num)/(max*max);
101     y = y0 + (y1-y0)*(max-num)*(max-num)/(max*max);
102     set_actor_position(actor, x, y, actor->z, desktop_plugin);
103     if (num == 0){
104         actor->x = x;
105         actor->y = y;
106         scene->pg[scene->empty] = scene->pg[scene->bone];
107         scene->pg[scene->bone] = 15;
108         scene->empty = scene->bone;
109     }
110 }
111 void moving_all(gint num, gint max, AWallpaperPlugin *desktop_plugin)
112 {
113     gint i, axis;
114     double angle;
115     angle = 360*(max - num)/max;
116     for (i=0; i<15; i++){
117         if (i%2 == 0) axis = HILDON_AA_Y_AXIS;
118         else axis = HILDON_AA_X_AXIS;
119         set_actor_rotation(actors[i], axis, angle, 0, 0, 0);
120     }
121     if (num == (int)max/2){
122         reinit(desktop_plugin);
123     }
124 }
125 gboolean main_timer(AWallpaperPlugin *desktop_plugin)
126 {
127     if (scene->timer_num > 0){
128         scene->timer_num--;
129         if (scene->bone>-1)
130             moving_actor(scene->timer_num, 10, desktop_plugin);
131         else 
132             moving_all(scene->timer_num, 20, desktop_plugin);
133         return;
134     } 
135     //char * accel_filename = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
136     char * accel_filename = "/home/tanya/coord";
137
138     gint direction = -1, bone;
139     FILE *fd = NULL;
140     int rs, ax, ay, az, dx, dy;
141     fd = fopen(accel_filename, "r");
142     if (fd == NULL){
143         fprintf(stderr, "cannot open file\n");
144         return;
145     }
146     rs = fscanf((FILE*)fd, "%i %i %i", &ax, &ay, &az);
147     fclose(fd);
148     if (rs != 3){
149         fprintf(stderr, "cannot read information from file\n");
150         return;
151     }
152
153     //fprintf(stderr, "change obj %i %i %i\n", ax, ay, az);
154     if (az < -2000) {
155         //reinit(desktop_plugin);
156         //sleep(1);
157         scene->timer_num = 20;
158         scene->bone = -1;
159         return TRUE;
160     }
161     if (abs(ax) - abs(ay) > 300){
162         if (ax > 0) {
163             // LEFT;
164             bone = scene->empty + 1;
165             if (bone % 4 == 0) return TRUE;
166         }
167         else {
168             // RIGHT;
169             bone = scene->empty - 1;
170             if (scene->empty % 4 == 0) return TRUE;
171         }
172     }else
173     if (abs(ay) - abs(ax) > 300){
174         if (ay > 0){ 
175             // UP;
176             bone = scene->empty + 4;
177             if (bone > 15) return TRUE;
178         }
179         else {
180             // DOWN;
181             bone = scene->empty - 4;
182             if (bone < 0) return TRUE;
183         }
184     } else return TRUE;
185     fprintf(stderr, "move %d\n", bone);
186     scene->bone = bone;
187     scene->timer_num = 10;
188         
189     return TRUE;
190 }
191 void init_actors(AWallpaperPlugin *desktop_plugin)
192 {
193     gint i;
194     //Actor *actors[15];
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 int main( int   argc, char *argv[] )
204 {
205     GtkWidget *window;
206     AWallpaperPlugin *desktop_plugin = g_new0 (AWallpaperPlugin, 1);
207     Animation_WallpaperPrivate *priv = g_new0 (Animation_WallpaperPrivate, 1);
208     scene = g_new0(Scene1, 1);
209     Actor *actor;
210     gint i;
211     gint pg[16];
212
213     hildon_gtk_init (&argc, &argv);
214     g_set_application_name ("Simplest example");
215     window = hildon_window_new ();
216     g_signal_connect (G_OBJECT (window), "delete_event",
217                         G_CALLBACK (gtk_main_quit), NULL);
218     priv->window = window;
219     priv->theme = g_strdup("Fifteen"); 
220     priv->xapplet = 0;
221     priv->yapplet = 0;
222     desktop_plugin->priv = priv;
223
224     //init_pg(pg);
225     scene->pg = pg;
226     //scene->actors = init_actors(desktop_plugin);
227     init_actors(desktop_plugin);
228     scene->timer_num = 0;
229     reinit(desktop_plugin);
230     gtk_widget_show  (window);
231     g_timeout_add(100, main_timer, desktop_plugin);
232     gtk_main ();
233     return 0;
234 }
235
236