Create interfaces for string-seeded blinking
authorJoachim Breitner <mail@joachim-breitner.de>
Sat, 30 Jan 2010 21:19:51 +0000 (21:19 +0000)
committerJoachim Breitner <mail@joachim-breitner.de>
Sat, 30 Jan 2010 21:19:51 +0000 (21:19 +0000)
src/blink.c
src/blink.h
src/demoblink.c
src/pidgin-blinklight.c

index ac5212a..ef5aa50 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <unistd.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include <blink.h>
@@ -44,14 +45,19 @@ static struct interface interfaces[] = {
 
 static struct interface *interface = NULL;
 
-struct blinky {
+typedef struct {
        int     state;
        int     time;
-};
+} blinky;
+
+typedef struct {
+       blinky *seq;
+       int pos;
+} blinkstate;
 
 /* TODO: We should really allow an option to continue blinking indefinitely
          until user opens message */
-struct blinky default_seq[] = {
+blinky default_seq[] = {
        {TOGGLE,        150},
        {TOGGLE,        125},
        {TOGGLE,        150},
@@ -59,12 +65,14 @@ struct blinky default_seq[] = {
 };
 
 static guint
-blinklight_blink(struct blinky *seq) {
+blinklight_blink(blinkstate *bstate) {
        FILE *file;
        char *new_state = NULL;
        char old_state[10] = "";
        int ret;
 
+       blinky *seq = &bstate->seq[bstate->pos];
+
        if (interface == NULL) return FALSE;
 
        // purple_debug_info("pidgin-blinklight","blink called with parameter: %i\n", seq->state);
@@ -100,15 +108,44 @@ blinklight_blink(struct blinky *seq) {
 
        // purple_debug_info("pidgin-blinklight","done setting new state: %s\n", new_state);
        
-       if (seq->time) 
-               blinklight_timeout_add(seq->time,(GSourceFunc)blinklight_blink,seq+1);
-
+       if (seq->time) { 
+               bstate->pos++;
+               blinklight_timeout_add(seq->time,(GSourceFunc)blinklight_blink,bstate);
+       } else {
+               free(bstate->seq);
+               free(bstate);
+       }
        return FALSE;
 }
-       
+
 void
-blinklight_startblink() {
-       blinklight_blink(default_seq);
+blinklight_startblink(char *seed) {
+       int length = 4;
+       blinkstate *bstate = malloc(sizeof(blinkstate));
+       blinky *seq ;
+
+       if (seed == NULL) {
+               seq = malloc(sizeof(default_seq));
+               memcpy(seq, default_seq, sizeof(default_seq));
+
+       } else {
+               seq = calloc(sizeof(blinky),length);
+               
+               // Initialize to four toggle commands
+               for (int i=0; i<length; i++) {
+                       seq[i].state = TOGGLE;
+               }
+               // Set timeing based on string
+               // 100 + [0,1]*100 ms
+               for (int i=0; i<length-1; i++) {
+                       seq[i].time = 100 + ((int)seed[i]*103) % 100;
+                       printf("Time %d: %d\n", i, seq[i].time);
+               }
+       }
+
+       bstate->seq = seq;
+       bstate->pos = 0;
+       blinklight_blink(bstate);
 }
 
 char *
index 7513b9d..2c474d7 100644 (file)
 #include <glib.h>
 
 void
-blinklight_startblink();
+blinklight_startblink(char *);
 
 guint
-blinklight_timeout_add (guint interval, GSourceFunc function, gpointer data);
+blinklight_timeout_add (guint, GSourceFunc, gpointer);
 
 char *
 blinklight_init();
index c744795..311a351 100644 (file)
@@ -48,7 +48,11 @@ int main(int argc, char **argv) {
        }
        printf("Using file %s.\n", file);
 
-       blinklight_startblink();
+       if (argc>1) {
+               blinklight_startblink(argv[1]);
+       } else {
+               blinklight_startblink(NULL);
+       }
 
        while (timeout_interval > 0) {
                usleep(timeout_interval*1000);
index 7e30ccb..4b3ca9a 100644 (file)
@@ -62,12 +62,12 @@ gt_blink(PurpleAccount *account, const gchar *sender,
        if (purple_prefs_get_bool("/plugins/core/pidgin-blinklight/focus"))
        {
                // Blink in any case
-               blinklight_startblink();
+               blinklight_startblink(NULL);
        } else {
                // Only blink when not having focus
                PurpleConversation *conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY, sender, account);
                if (! purple_conversation_has_focus(conv)) 
-                       blinklight_startblink();
+                       blinklight_startblink(NULL);
        }
 }