next step for 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     set_actor_position(actor, x, y, actor->z, desktop_plugin);
101     if (num == 0){
102         actor->x = x;
103         actor->y = y;
104         scene->pg[scene->empty] = scene->pg[scene->bone];
105         scene->pg[scene->bone] = 15;
106         scene->empty = scene->bone;
107     }
108 }
109 void moving_all(gint num, gint max, AWallpaperPlugin *desktop_plugin)
110 {
111     gint i;
112
113     for (i=0; i<15; i++){
114     }
115 }
116 gboolean main_timer(AWallpaperPlugin *desktop_plugin)
117 {
118     if (scene->timer_num > 0){
119         scene->timer_num--;
120         if (scene->bone>-1)
121             moving_actor(scene->timer_num, 10, desktop_plugin);
122         return;
123     } 
124     char * accel_filename = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
125     //char * accel_filename = "/home/tanya/coord";
126
127     gint direction = -1, bone;
128     FILE *fd = NULL;
129     int rs, ax, ay, az, dx, dy;
130     fd = fopen(accel_filename, "r");
131     if (fd == NULL){
132         fprintf(stderr, "cannot open file\n");
133         return;
134     }
135     rs = fscanf((FILE*)fd, "%i %i %i", &ax, &ay, &az);
136     fclose(fd);
137     if (rs != 3){
138         fprintf(stderr, "cannot read information from file\n");
139         return;
140     }
141
142     //fprintf(stderr, "change obj %i %i %i\n", ax, ay, az);
143     if (az < -2000) {
144         reinit(desktop_plugin);
145         //sleep(1);
146         scene->timer_num = 10;
147         scene->bone = -1;
148         return TRUE;
149     }
150     if (abs(ax) - abs(ay) > 300){
151         if (ax > 0) {
152             // LEFT;
153             bone = scene->empty + 1;
154             if (bone % 4 == 0) return TRUE;
155         }
156         else {
157             // RIGHT;
158             bone = scene->empty - 1;
159             if (scene->empty % 4 == 0) return TRUE;
160         }
161     }else
162     if (abs(ay) - abs(ax) > 300){
163         if (ay > 0){ 
164             // UP;
165             bone = scene->empty + 4;
166             if (bone > 15) return TRUE;
167         }
168         else {
169             // DOWN;
170             bone = scene->empty - 4;
171             if (bone < 0) return TRUE;
172         }
173     } else return TRUE;
174     fprintf(stderr, "move %d\n", bone);
175     scene->bone = bone;
176     scene->timer_num = 10;
177         
178     return TRUE;
179 }
180 void init_actors(AWallpaperPlugin *desktop_plugin)
181 {
182     gint i;
183     //Actor *actors[15];
184     for (i=0; i<15; i++){
185         actors[i] = init_object(desktop_plugin, "bone", g_strdup_printf("%d.png", i+1),
186                                 0, 0, 2, width, height,
187                                 TRUE, TRUE, 100, 255,
188                                 NULL, NULL, NULL);
189     }
190     //return actors;
191 }
192 int main( int   argc, char *argv[] )
193 {
194     GtkWidget *window;
195     AWallpaperPlugin *desktop_plugin = g_new0 (AWallpaperPlugin, 1);
196     Animation_WallpaperPrivate *priv = g_new0 (Animation_WallpaperPrivate, 1);
197     scene = g_new0(Scene1, 1);
198     Actor *actor;
199     gint i;
200     gint pg[16];
201
202     hildon_gtk_init (&argc, &argv);
203     g_set_application_name ("Simplest example");
204     window = hildon_window_new ();
205     g_signal_connect (G_OBJECT (window), "delete_event",
206                         G_CALLBACK (gtk_main_quit), NULL);
207     priv->window = window;
208     priv->theme = g_strdup("Fifteen"); 
209     priv->xapplet = 0;
210     priv->yapplet = 0;
211     desktop_plugin->priv = priv;
212
213     //init_pg(pg);
214     scene->pg = pg;
215     //scene->actors = init_actors(desktop_plugin);
216     init_actors(desktop_plugin);
217     scene->timer_num = 0;
218     reinit(desktop_plugin);
219     gtk_widget_show  (window);
220     g_timeout_add(100, main_timer, desktop_plugin);
221     gtk_main ();
222     return 0;
223 }
224
225