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