rewrote actors objects
authortanya <tanya@tanin.oblgaz>
Thu, 8 Apr 2010 10:48:49 +0000 (13:48 +0300)
committertanya <tanya@tanin.oblgaz>
Thu, 8 Apr 2010 10:48:49 +0000 (13:48 +0300)
applet/src/livewp-common.h
applet/src/livewp-home-widget.c
applet/src/livewp-home-widget.h
applet/src/livewp-rules.c
applet/src/livewp-rules.h

index e899834..3931b9b 100644 (file)
@@ -31,6 +31,7 @@
 #include <glib/gi18n-lib.h>
 
 #define THEME_PATH "/usr/share/livewp/theme"
+#define NAME_SIZE 256:
 
 typedef struct {
     gchar * name;
@@ -67,4 +68,21 @@ struct _AWallpaperPluginClass
 
 };
 
+typedef struct {
+    GtkWidget * widget;
+    gchar  *name;
+    gchar *filename;
+    gint x;
+    gint y;
+    gint z;
+    gint width;
+    gint height;
+    gint scale;
+    gint opacity; 
+    gboolean visible;
+    void (*func_change)(gpointer, gpointer);
+    time_t time_start_animation;
+    gint duration_animation;
+    void (*func_probability)(gpointer);
 
+} Actor;
index e05fb42..cc064ba 100644 (file)
@@ -22,7 +22,7 @@
  * 02110-1301 USA
 */
 /*******************************************************************************/
-#include "livewp-common.h"
+#include "livewp-common.h" 
 #include "livewp-home-widget.h"
 #include <gconf/gconf-client.h>
 #include "livewp-rules.h"
@@ -157,20 +157,27 @@ actor_set_position_full(GtkWidget *actor, gint x, gint y, gint z)
  hildon_animation_actor_set_position_full (HILDON_ANIMATION_ACTOR (actor),x-xapplet, y-yapplet, z);
 }
 
-static GtkWidget* 
-init_object(AWallpaperPlugin *desktop_plugin, gchar * name, gchar * filename, gint x, gint y, gint z, gint width, gint height, gboolean visible, gint scale, gint opacity, void (*pfunc)(GtkWidget*, gpointer))
+void
+destroy_hildon_actor(Actor *actor)
+{
+    gtk_widget_destroy(actor->widget);
+    actor->widget = NULL;
+}
+
+void
+create_hildon_actor(AWallpaperPlugin *desktop_plugin, Actor *actor) 
 {
-  GtkWidget *actor = NULL;
+  GtkWidget *ha = NULL;
   GdkPixbuf *pixbuf = NULL;
   GtkWidget *image = NULL;
 
-  actor = hildon_animation_actor_new();
+  ha = hildon_animation_actor_new();
   gchar str[256];
-  snprintf(str, 255, "%s/%s/%s", THEME_PATH, desktop_plugin->priv->theme, filename);
-  //fprintf(stderr, "!!!init object !!!!file = %s\n", str);
+  snprintf(str, 255, "%s/%s/%s", THEME_PATH, desktop_plugin->priv->theme, actor->filename);
+  fprintf(stderr, "!!!init object !!!!file = %s\n", str);
   pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
-                                             width, 
-                                             height, 
+                                             actor->width, 
+                                             actor->height, 
                                              NULL);
   if (pixbuf){
       image = gtk_image_new_from_pixbuf (pixbuf);
@@ -179,29 +186,69 @@ init_object(AWallpaperPlugin *desktop_plugin, gchar * name, gchar * filename, gi
   if (image){
     g_signal_connect(G_OBJECT(image), "expose_event",
                            G_CALLBACK(expose_event), pixbuf);
-    gtk_container_add (GTK_CONTAINER (actor), image);
+    gtk_container_add (GTK_CONTAINER (ha), image);
   }  
 
-  actor_set_position_full(actor, x, y, z);
-  hildon_animation_actor_set_show (actor, visible);
-  hildon_animation_actor_set_scale(actor, (double)scale/100, (double)scale/100);
-  realize(actor);
-  gtk_widget_show_all(actor);
-  g_object_set_data(G_OBJECT(actor), "name", name);
-  g_object_set_data(G_OBJECT(actor), "filename", filename);
-  g_object_set_data(G_OBJECT(actor), "image", image);
-  g_object_set_data(G_OBJECT(actor), "x", x);
-  g_object_set_data(G_OBJECT(actor), "y", y);
-  g_object_set_data(G_OBJECT(actor), "z", z);
-  g_object_set_data(G_OBJECT(actor), "width", width);
-  g_object_set_data(G_OBJECT(actor), "height", height);
-  g_object_set_data(G_OBJECT(actor), "scale", scale);
-  g_object_set_data(G_OBJECT(actor), "visible", visible);
-  g_object_set_data(G_OBJECT(actor), "opacity", opacity);
-  g_object_set_data(G_OBJECT(actor), "func", pfunc);
-  
-  hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (actor), desktop_plugin);
-/*
+  actor_set_position_full(ha, actor->x, actor->y, actor->z);
+  hildon_animation_actor_set_show (ha, actor->visible);
+  hildon_animation_actor_set_scale(ha, (double)actor->scale/100, (double)actor->scale/100);
+  realize(ha);
+  gtk_widget_show_all(ha);
+
+  //g_object_set_data(G_OBJECT(ha), "name", name);
+  //g_object_set_data(G_OBJECT(ha), "filename", filename);
+  g_object_set_data(G_OBJECT(ha), "image", image);
+  /*
+  g_object_set_data(G_OBJECT(ha), "x", x);
+  g_object_set_data(G_OBJECT(ha), "y", y);
+  g_object_set_data(G_OBJECT(ha), "z", z);
+  g_object_set_data(G_OBJECT(ha), "width", width);
+  g_object_set_data(G_OBJECT(ha), "height", height);
+  g_object_set_data(G_OBJECT(ha), "scale", scale);
+  g_object_set_data(G_OBJECT(ha), "visible", visible);
+  g_object_set_data(G_OBJECT(ha), "opacity", opacity);
+  g_object_set_data(G_OBJECT(ha), "func", pfunc);
+  */
+  hildon_animation_actor_set_parent (HILDON_ANIMATION_ACTOR (ha), desktop_plugin);
+
+  actor->widget = ha;
+}
+
+static Actor* 
+init_object(AWallpaperPlugin *desktop_plugin, 
+            gchar * name, 
+            gchar * filename, 
+            gint x, 
+            gint y, 
+            gint z, 
+            gint width, 
+            gint height, 
+            gboolean visible, 
+            gint scale, 
+            gint opacity, 
+            void (*pfunc_change)(Actor*),
+            void (*pfunc_probability)(Actor*)
+           )
+{
+  Actor *actor = NULL;
+  actor = g_new0(Actor, 1);
+  actor->x = x;
+  actor->y = y;
+  actor->z = z;
+  actor->width = width;
+  actor->height = height;
+  actor->visible = visible;
+  actor->scale = scale;
+  actor->opacity = opacity;
+  actor->filename = g_strdup(filename);
+  actor->name = g_strdup(name);
+  actor->func_change = pfunc_change; 
+  actor->func_probability = pfunc_probability;
+  if (visible)
+    create_hildon_actor(desktop_plugin, actor);
+  actor->time_start_animation = 0;
+  actor->duration_animation = 0;
+  /*
   a.widget = actor;
   a.name = name;
   a.x = x;
@@ -213,61 +260,76 @@ init_object(AWallpaperPlugin *desktop_plugin, gchar * name, gchar * filename, gi
   return actor;
 }
 
-void change_sun(GtkWidget * actor, gpointer data)
+
+void 
+change_sun(Actor * actor, AWallpaperPlugin *desktop_plugin)
 {
     double alt, azm;
     gint x, y, z, daytime;
-    //fprintf(stderr, "change sun \n");
+
+    fprintf(stderr, "change sun %d\n", actor->time_start_animation);
     if (actor){
         daytime = get_daytime();
         if (daytime != TIME_NIGHT){
-             hildon_animation_actor_set_show(actor, 1);
-             get_sun_pos(&alt, &azm);
-             get_sun_screen_pos(alt, azm, &x, &y);
-             actor_set_position_full(actor, x, y, g_object_get_data(G_OBJECT(actor), "z"));
-             g_object_set_data(G_OBJECT(actor), "visible", 1);
-             g_object_set_data(G_OBJECT(actor), "x", x);
-             g_object_set_data(G_OBJECT(actor), "y", y);
-        }else {
-            hildon_animation_actor_set_show(actor, 0);
-            g_object_set_data(G_OBJECT(actor), "visible", 0);
+            if (!actor->visible){
+                actor->visible = TRUE;
+                create_hildon_actor(desktop_plugin, actor);
+            }
+            get_sun_pos(&alt, &azm);
+            get_sun_screen_pos(alt, azm, &x, &y);
+            actor->x = x;
+            actor->y = y;
+            actor_set_position_full(actor->widget, x, y, actor->z);
+            probability_sun(actor);
+            fprintf(stderr, "after change sun %d\n", actor->time_start_animation);
+         }else if (actor->visible){
+            actor->visible = FALSE;
+            destroy_hildon_actor(actor);
+            actor->time_start_animation = get_next_sunrise();
         } 
     }
+    
 }
-void change_static_actor(GtkWidget * actor, gpointer data)
+void
+change_hildon_actor(Actor *actor, AWallpaperPlugin *desktop_plugin)
 {
-    gint daytime;
-    gchar newfile[2048];
-    gchar * name;
-    GtkWidget *image;
-    GdkPixbuf *pixbuf;
-    AWallpaperPlugin *desktop_plugin;
-    if (!actor) return;
-    if (!data) return;
-    desktop_plugin = data;
-    daytime = get_daytime();
+    GtkWidget *image = NULL;
+    GdkPixbuf *pixbuf = NULL;
     
-    name = g_object_get_data(G_OBJECT(actor), "name");
-    snprintf(newfile, 2047, "%s/%s/%s%d.png", THEME_PATH, desktop_plugin->priv->theme, name, daytime);
-    
-    pixbuf = gdk_pixbuf_new_from_file_at_size (newfile, 
-                                               g_object_get_data(G_OBJECT(actor), "width"), 
-                                               g_object_get_data(G_OBJECT(actor), "height"), 
+    pixbuf = gdk_pixbuf_new_from_file_at_size (actor->filename, 
+                                               actor->width, 
+                                               actor->height, 
                                                NULL);
     if (pixbuf){
         image = gtk_image_new_from_pixbuf (pixbuf);
         g_object_unref(G_OBJECT(pixbuf));
     }
-    
-    g_signal_connect(G_OBJECT(image), "expose_event",
-                                   G_CALLBACK(expose_event), pixbuf);
-    if (g_object_get_data(G_OBJECT(actor), "image")){
-        gtk_container_remove(actor, g_object_get_data(G_OBJECT(actor), "image"));  
+    if (image){ 
+        g_signal_connect(G_OBJECT(image), "expose_event",
+                                       G_CALLBACK(expose_event), pixbuf);
+        if (g_object_get_data(G_OBJECT(actor->widget), "image")){
+            gtk_container_remove(actor->widget, g_object_get_data(G_OBJECT(actor->widget), "image"));  
+        }
+        g_object_set_data(G_OBJECT(actor->widget), "image", image);
+        gtk_container_add (GTK_CONTAINER (actor->widget), image);
+        realize(actor->widget);
+        gtk_widget_show_all(actor->widget);
     }
-    g_object_set_data(G_OBJECT(actor), "image", image);
-    gtk_container_add (GTK_CONTAINER (actor), image);
-    realize(actor);
-    gtk_widget_show_all(actor);
+
+}
+void change_static_actor(Actor * actor, AWallpaperPlugin *desktop_plugin)
+{
+    gint daytime;
+    gchar newfile[2048];
+    if (!actor) return;
+    daytime = get_daytime();
+    fprintf(stderr,"Name %s\n", actor->name); 
+    fprintf(stderr,"daytime %i\n", daytime); 
+    fprintf(stderr,"theme %s\n", desktop_plugin->priv->theme); 
+    snprintf(newfile, 2047, "%s/%s/%s%d.png", THEME_PATH, desktop_plugin->priv->theme, actor->name, daytime);
+    fprintf(stderr, "11%s\n", newfile);
+    actor->filename = newfile;
+    change_hildon_actor(actor, desktop_plugin);
 }
 
 static gint 
@@ -336,7 +398,7 @@ get_time(gint t)
 static void 
 destroy_scene(void)
 {
-    GSList * tmp = scene.dynamic_actors;
+    GSList * tmp = scene.actors;
     while (tmp != NULL){
         if (tmp->data){
             gtk_widget_destroy(tmp->data);
@@ -345,15 +407,6 @@ destroy_scene(void)
         tmp = g_slist_next(tmp);
     }
     g_slist_free(tmp);
-    tmp = scene.static_actors;
-    while (tmp != NULL){        
-        if (tmp->data){
-            gtk_widget_destroy(tmp->data);
-            tmp->data = NULL;
-        }
-        tmp = g_slist_next(tmp);
-    }
-    g_slist_free(tmp);
 
 }
 
@@ -365,50 +418,51 @@ reload_scene(AWallpaperPlugin *desktop_plugin)
     if (!strcmp(desktop_plugin->priv->theme,"Modern"))
         init_scene(desktop_plugin);
     else if (!strcmp(desktop_plugin->priv->theme,"Berlin")) 
-        init_scene1(desktop_plugin);
+        init_scene(desktop_plugin);
 }
 
-void
-anim_tram(GtkWidget *actor, Animation * an)
+void 
+probability_sun(Actor *actor)
 {
-    //change_tram1(an->actor, t, an->timeall);
+    actor->time_start_animation += 20 * 1;
+    actor->duration_animation = G_MAXINT;
 }
 
 static void
 init_scene(AWallpaperPlugin *desktop_plugin)
 {
-  GtkWidget *actor;
-  GdkPixbuf *pixbuf;
-  GtkWidget *image;
-  double alt, azm;
-  gint x, y;
+  Actor *actor;
   GSList * list = NULL;
   gchar str[256];
-  Animation *anim = NULL;
 
 fprintf(stderr, "init scene modern\n");
   scene.daytime = get_daytime();
-  scene.dynamic_actors = NULL;
-  scene.static_actors = NULL;
+  scene.actors = NULL;
   //get_sun_pos(&alt, &azm);
   //get_sun_screen_pos(alt, azm, &x, &y);
-  actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 11, 88, 88, 1, 100, 255, &change_sun);
-  change_sun(actor, NULL);
-  scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
+  actor = init_object(desktop_plugin, "sun", "sun.png", 0, 0, 11, 88, 88, TRUE, 100, 255, &change_sun, &probability_sun);
+  actor->time_start_animation = time(NULL);
+  actor->duration_animation = G_MAXINT;
+  change_sun(actor, desktop_plugin);
+  scene.actors = g_slist_append(scene.actors, actor);
   //scene.dynamic_actors = g_slist_append(scene.dynamic_actors, G_OBJECT(actor));
-
-  actor = init_object(desktop_plugin, "sky", "sky0.png", 0, 0, 5, 800, 480, 1, 100, 255, &change_static_actor);
-  change_static_actor(G_OBJECT(actor), desktop_plugin);
-  scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
-
-  actor = init_object(desktop_plugin, "town", "town0.png", 0, 480-374, 10, 800, 374, 1, 100, 255, &change_static_actor);
-  change_static_actor(G_OBJECT(actor), desktop_plugin);
-  scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
-  
-  actor = init_object(desktop_plugin, "border", "border0.png", 0, 480-79, 30, 800, 79, 1, 100, 255, &change_static_actor);
-  change_static_actor(G_OBJECT(actor), desktop_plugin);
-  scene.static_actors = g_slist_append(scene.static_actors, G_OBJECT(actor));
+  actor = init_object(desktop_plugin, "sky", "sky0.png", 0, 0, 5, 800, 480, 1, 100, 255, &change_static_actor, NULL);
+  change_static_actor(actor, desktop_plugin);
+  scene.actors = g_slist_append(scene.actors, actor);
+
+#if 0
+  actor = init_object(desktop_plugin, "town", "town0.png", 0, 480-374, 10, 800, 374, 1, 100, 255, &change_static_actor, NULL);
+  change_static_actor(actor, desktop_plugin);
+  scene.actors = g_slist_append(scene.actors, actor);
   
+  actor = init_object(desktop_plugin, "border", "border0.png", 0, 480-79, 30, 800, 79, 1, 100, 255, &change_static_actor, NULL);
+  change_static_actor(actor, desktop_plugin);
+  scene.actors = g_slist_append(scene.actors, actor);
+#endif
+
+#if 0  
   //if (scene.daytime == TIME_DAY) snprintf(str, 255, "tram.png");
   //else snprintf(str, 255, "tram_dark.png");
   //actor = init_object("tram", "tram.png", -300, 191, 25, 350, 210, 1, 100, 255, NULL);
@@ -424,8 +478,9 @@ fprintf(stderr, "init scene modern\n");
   anim->timeall = 10;
   
   scene.dynamic_actors = g_slist_append(scene.dynamic_actors, anim);
-  
+#endif  
  }
+#if 0
 static void
 init_scene1(AWallpaperPlugin *desktop_plugin)
 {
@@ -470,7 +525,7 @@ init_scene1(AWallpaperPlugin *desktop_plugin)
   */
 
 }
-
+#endif
 
 void 
 get_sun_screen_pos(double alt, double azm, gint * x, gint * y)
@@ -573,7 +628,6 @@ change_actor(GtkWidget * actor)
     }
 
 }
-gint ttt = 0;
 static gboolean
 plugin_on_timeout (AWallpaperPlugin *desktop_plugin)
 {
@@ -583,29 +637,35 @@ plugin_on_timeout (AWallpaperPlugin *desktop_plugin)
   void (*pfunc)(gpointer, gpointer);
   time_t now;
   gint t;
-  Animation *anim = NULL;
+  Actor *actor;
  
   if (scene.daytime != daytime){
-      tmp = scene.static_actors;
+      tmp = scene.actors;
       while (tmp != NULL){
           //change_actor(tmp->data);
-          pfunc = g_object_get_data(G_OBJECT(tmp->data), "func");
-          /*name = g_object_get_data(G_OBJECT(tmp->data), "name");
-          if (name == "tram"){
-              if (pfunc)
-                (*pfunc)(tmp->data, ttt);
-              ttt = get_time(ttt+1);
-              if (ttt >= 100) ttt = 0;
-            }else*/
+          pfunc =((Actor*)tmp->data)->func_change;
           if (pfunc){
-              (*pfunc)(tmp->data, NULL);
+              (*pfunc)(tmp->data, desktop_plugin);
           }
           tmp = g_slist_next(tmp);
       }
    }
-   tmp = scene.dynamic_actors;
+   
    now = time(NULL);
+//fprintf(stderr, "Now  %d\n", now);
+   tmp = scene.actors;
+   while (tmp != NULL){
+       actor = tmp->data;
+       if (now >= actor->time_start_animation && 
+           now - actor->time_start_animation <= actor->duration_animation){
+            pfunc = actor->func_change;
+            if (pfunc) 
+                (*pfunc)(actor, desktop_plugin);
+        }
+        tmp = g_slist_next(tmp);
+   }
    
+#if 0
    while (tmp != NULL)
    {    
         anim = tmp->data;
@@ -629,7 +689,7 @@ plugin_on_timeout (AWallpaperPlugin *desktop_plugin)
             tmp = g_slist_next(tmp);
         }
     }
-    
+    #endif
   scene.daytime = daytime;
 
   //double azm, alt;
@@ -686,7 +746,7 @@ animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
 {
     GtkWidget *label;
 
-    //fprintf(stderr, "!!!!!!!plugin init \n");
+    fprintf(stderr, "!!!!!!!plugin init \n");
     Animation_WallpaperPrivate *priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
     desktop_plugin->priv =  Animation_Wallpaper_HOME_PLUGIN_GET_PRIVATE (desktop_plugin);
     /* Load config */
@@ -707,8 +767,8 @@ animation_wallpaper_plugin_init (AWallpaperPlugin *desktop_plugin)
   if (!strcmp(priv->theme,"Modern"))
       init_scene(desktop_plugin);
   else if (!strcmp(priv->theme,"Berlin")) 
-      init_scene1(desktop_plugin);
-  priv->timer = g_timeout_add(1000*1, plugin_on_timeout, desktop_plugin);
+      init_scene(desktop_plugin);
+  priv->timer = g_timeout_add(1000*5, plugin_on_timeout, desktop_plugin);
 }
 
 static void
index ea83d6c..55cf195 100644 (file)
@@ -26,7 +26,6 @@
 
 #include <glib-object.h>
 
-
 G_BEGIN_DECLS
 
 
@@ -49,11 +48,10 @@ G_BEGIN_DECLS
 
 
 typedef struct {
-    GSList * static_actors;
-    GSList * dynamic_actors;
+    GSList * actors;
     gint daytime;
 } Scene;
-
+/*
 typedef struct {
     time_t timestart;
     gint timeall;
@@ -62,18 +60,7 @@ typedef struct {
     void (*func_change)(GtkWidget *, double);
     gint (*func_time)(gint);
 } Animation;
-
-typedef struct {
-    GtkWidget * widget;
-    gchar * name;
-    GtkWidget * image;
-    gint x;
-    gint y;
-    gint z;
-    double scale;
-    gboolean visible;
-
-} Actor;
+*/
 
 GType animation_wallpaper_plugin_get_type(void);
 
index 06689f2..63719d7 100644 (file)
@@ -70,6 +70,10 @@ void get_sun_pos(double * alt, double * azm)
     }
     *azm = (*azm - 90) / 180; // sun azimuth in percent
 }
+time_t get_next_sunrise()
+{
+    return time(NULL) + 60*60*8;
+}
 int get_daytime()
 {
     double alt, azm;
index 9def725..caffce4 100644 (file)
@@ -45,5 +45,7 @@ void get_coord(double * lat, double * lon);
 void get_sun_pos(double * alt, double * azm);
 int get_daytime();
 int get_moon_phase();
+time_t get_next_sunrise();
+
 /*******************************************************************************/
 #endif