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