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