next step for fifteen
[livewp] / applet / src / fifteen.c
index 24347d4..b3d9d4d 100644 (file)
@@ -10,9 +10,21 @@ enum {
     LEFT = 3
 };
 
-gint pg[16];
+const gint width = 200;
+const gint height = 120;
+
+typedef struct _Scene1 Scene1;
+struct _Scene1 
+{
+    gint *pg;
+    //Actor *actors;
+    gint empty;
+    gint bone;
+    gint timer_num;
+};
+Scene1 *scene;
+
 Actor *actors[15];
-gint empty;
 
 void init_pg(gint *pg)
 {
@@ -28,47 +40,164 @@ void init_pg(gint *pg)
         pg[j] = t;
     }
 }
-
-void move(gint direction, AWallpaperPlugin *desktop_plugin)
+#if 0
+gint move(gint direction, AWallpaperPlugin *desktop_plugin)
 {
     gint bone;
     Actor *actor;
     switch (direction) {
         case UP:
             bone = empty + 4;
-            if (bone > 15) return;
+            if (bone > 15) return -1;
             break;
         case RIGHT:
             bone = empty - 1;
-            if (empty % 4 == 0) return;
+            if (empty % 4 == 0) return -1;
             break;
         case DOWN:
             bone = empty - 4;
-            if (bone < 0) return;
+            if (bone < 0) return -1;
             break;
         case LEFT:
             bone = empty + 1;
-            if (bone % 4) return;
+            if (bone % 4 == 0) return -1;
+    }
+    //actor = actors[pg[bone]];
+    cur = bone;
+    //set_actor_position(actor, (empty%4)*width, (empty/4)*height, actor->z, desktop_plugin);
+    //pg[empty] = pg[bone];
+    //pg[bone] = 15;
+    //empty = bone;
+    return pg[bone]; 
+}
+#endif
+void reinit(AWallpaperPlugin *desktop_plugin)
+{
+    fprintf(stderr, "reinit\n");
+    gint i;
+    Actor *actor;
+    init_pg(scene->pg);
+
+    scene->empty = 15;
+    for (i=0; i<15; i++){
+        actor = actors[scene->pg[i]];
+        actor->x = (i%4)*width;
+        actor->y = (i/4)*height;
+        //fprintf(stderr, "x=%d, y=%d\n", actor->x, actor->y);
+        set_actor_position(actor, actor->x, actor->y, actor->z, desktop_plugin);
+        //set_actor_position(actor, (i%4)*width, (i/4)*height, 2, desktop_plugin);
+    }
+}
+
+void moving_actor(gint num, gint max, AWallpaperPlugin *desktop_plugin)
+{
+    Actor *actor = actors[scene->pg[scene->bone]];
+    gint x0 = actor->x, y0 = actor->y,
+         x1 = (scene->empty%4)*width, y1 = (scene->empty/4)*height,
+         x,y;
+    x = x0 + (x1-x0)*(max - num)/max;
+    y = y0 + (y1-y0)*(max - num)/max;
+    set_actor_position(actor, x, y, actor->z, desktop_plugin);
+    if (num == 0){
+        actor->x = x;
+        actor->y = y;
+        scene->pg[scene->empty] = scene->pg[scene->bone];
+        scene->pg[scene->bone] = 15;
+        scene->empty = scene->bone;
+    }
+}
+void moving_all(gint num, gint max, AWallpaperPlugin *desktop_plugin)
+{
+    gint i;
+
+    for (i=0; i<15; i++){
     }
-    fprintf(stderr, "from %d to %d x=%d y=%d\n", bone, empty, (empty%4)*200, (empty/4)*120);
-    actor = actors[pg[bone]];
-    set_actor_position(actor, (empty%4)*200, (empty/4)*120, actor->z, desktop_plugin);
-    pg[empty] = pg[bone];
-    pg[bone] = 15;
-    empty = bone;
 }
-gboolean make_move(AWallpaperPlugin *desktop_plugin)
+gboolean main_timer(AWallpaperPlugin *desktop_plugin)
 {
-    move(RIGHT, desktop_plugin);
+    if (scene->timer_num > 0){
+        scene->timer_num--;
+        if (scene->bone>-1)
+            moving_actor(scene->timer_num, 10, desktop_plugin);
+        return;
+    } 
+    char * accel_filename = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
+    //char * accel_filename = "/home/tanya/coord";
+
+    gint direction = -1, bone;
+    FILE *fd = NULL;
+    int rs, ax, ay, az, dx, dy;
+    fd = fopen(accel_filename, "r");
+    if (fd == NULL){
+        fprintf(stderr, "cannot open file\n");
+        return;
+    }
+    rs = fscanf((FILE*)fd, "%i %i %i", &ax, &ay, &az);
+    fclose(fd);
+    if (rs != 3){
+        fprintf(stderr, "cannot read information from file\n");
+        return;
+    }
+
+    //fprintf(stderr, "change obj %i %i %i\n", ax, ay, az);
+    if (az < -2000) {
+        reinit(desktop_plugin);
+        //sleep(1);
+        scene->timer_num = 10;
+        scene->bone = -1;
+        return TRUE;
+    }
+    if (abs(ax) - abs(ay) > 300){
+        if (ax > 0) {
+            // LEFT;
+            bone = scene->empty + 1;
+            if (bone % 4 == 0) return TRUE;
+        }
+        else {
+            // RIGHT;
+            bone = scene->empty - 1;
+            if (scene->empty % 4 == 0) return TRUE;
+        }
+    }else
+    if (abs(ay) - abs(ax) > 300){
+        if (ay > 0){ 
+            // UP;
+            bone = scene->empty + 4;
+            if (bone > 15) return TRUE;
+        }
+        else {
+            // DOWN;
+            bone = scene->empty - 4;
+            if (bone < 0) return TRUE;
+        }
+    } else return TRUE;
+    fprintf(stderr, "move %d\n", bone);
+    scene->bone = bone;
+    scene->timer_num = 10;
+        
     return TRUE;
 }
+void init_actors(AWallpaperPlugin *desktop_plugin)
+{
+    gint i;
+    //Actor *actors[15];
+    for (i=0; i<15; i++){
+        actors[i] = init_object(desktop_plugin, "bone", g_strdup_printf("%d.png", i+1),
+                                0, 0, 2, width, height,
+                                TRUE, TRUE, 100, 255,
+                                NULL, NULL, NULL);
+    }
+    //return actors;
+}
 int main( int   argc, char *argv[] )
 {
     GtkWidget *window;
     AWallpaperPlugin *desktop_plugin = g_new0 (AWallpaperPlugin, 1);
     Animation_WallpaperPrivate *priv = g_new0 (Animation_WallpaperPrivate, 1);
+    scene = g_new0(Scene1, 1);
     Actor *actor;
     gint i;
+    gint pg[16];
 
     hildon_gtk_init (&argc, &argv);
     g_set_application_name ("Simplest example");
@@ -80,17 +209,15 @@ int main( int   argc, char *argv[] )
     priv->xapplet = 0;
     priv->yapplet = 0;
     desktop_plugin->priv = priv;
-    init_pg(pg);
-    empty = 15;
-    for (i=0; i<15; i++){
-        actor = init_object(desktop_plugin, "bone", g_strdup_printf("%d.png", pg[i]+1), 
-                      (i%4)*200, (i/4)*120, 2, 200, 120, 
-                      TRUE, TRUE, 100, 255, 
-                      NULL, NULL, NULL);
-        actors[pg[i]] = actor;
-    }
+
+    //init_pg(pg);
+    scene->pg = pg;
+    //scene->actors = init_actors(desktop_plugin);
+    init_actors(desktop_plugin);
+    scene->timer_num = 0;
+    reinit(desktop_plugin);
     gtk_widget_show  (window);
-    g_timeout_add(4000, make_move, desktop_plugin);
+    g_timeout_add(100, main_timer, desktop_plugin);
     gtk_main ();
     return 0;
 }