Change year in copyright message to 2012.
[paraslash.git] / mood.c
diff --git a/mood.c b/mood.c
index 26d68dc558512380f20f0f45990b268614118381..1a572cfa8f1859a946ea34708b48e990a036f5b2 100644 (file)
--- a/mood.c
+++ b/mood.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2007-2012 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -35,7 +35,7 @@ struct afs_statistics {
        /** Number of admissible files */
        unsigned num;
 };
-struct afs_statistics statistics;
+static struct afs_statistics statistics;
 
 /**
  * Each line of the current mood corresponds to a mood_item.
@@ -79,13 +79,13 @@ struct mood {
 static struct mood *current_mood;
 
 /**
- *  Rough approximation to sqrt.
+ * Rough approximation to sqrt.
  *
- *  \param x Integer of which to calculate the sqrt.
+ * \param x Integer of which to calculate the sqrt.
  *
- *  \return An integer res with res * res <= x.
+ * \return An integer res with res * res <= x.
  */
-static uint64_t int_sqrt(uint64_t x)
+__a_const static uint64_t int_sqrt(uint64_t x)
 {
        uint64_t op, res, one = 1;
        op = x;
@@ -107,28 +107,17 @@ static uint64_t int_sqrt(uint64_t x)
        return res;
 }
 
-/* returns 1 if row matches score item, 0 if not, negative on errors */
-static int get_item_score(const struct osl_row *row, struct mood_item *item,
-               long *score, long *score_arg_sum)
+/* returns 1 if row matches score item, 0 if not. */
+static int get_item_score(struct mood_item *item, const struct afs_info *afsi,
+               const struct afh_info *afhi, const char *path, long *score,
+               long *score_arg_sum)
 {
-       struct afs_info afsi;
-       struct afh_info afhi;
-       char *path;
        int ret, match = 1;
 
        *score_arg_sum += item->random_score? 100 : PARA_ABS(item->score_arg);
        ret = 100;
        if (item->method) {
-               ret = get_afsi_of_row(row, &afsi);
-               if (ret< 0)
-                       return ret;
-               ret = get_afhi_of_row(row, &afhi);
-               if (ret< 0)
-                       return ret;
-               ret = get_audio_file_path_of_row(row, &path);
-               if (ret< 0)
-                       return ret;
-               ret = item->method->score_function(path, &afsi, &afhi,
+               ret = item->method->score_function(path, afsi, afhi,
                        item->parser_data);
                if ((ret < 0 && !item->logical_not) || (ret >= 0 && item->logical_not))
                        match = 0; /* no match */
@@ -147,24 +136,32 @@ static int compute_mood_score(const struct osl_row *aft_row, struct mood *m,
        struct mood_item *item;
        int ret, match = 0;
        long score_arg_sum = 0, score = 0, item_score;
+       struct afs_info afsi;
+       struct afh_info afhi;
+       char *path;
 
        if (!m)
                return -E_NO_MOOD;
+       ret = get_afsi_of_row(aft_row, &afsi);
+       if (ret< 0)
+               return ret;
+       ret = get_afhi_of_row(aft_row, &afhi);
+       if (ret< 0)
+               return ret;
+       ret = get_audio_file_path_of_row(aft_row, &path);
+       if (ret< 0)
+               return ret;
        /* reject audio file if it matches any entry in the deny list */
        list_for_each_entry(item, &m->deny_list, mood_item_node) {
-               ret = get_item_score(aft_row, item, &item_score,
+               ret = get_item_score(item, &afsi, &afhi, path, &item_score,
                        &score_arg_sum);
-               if (ret < 0)
-                       return ret;
                if (ret > 0) /* not admissible */
                        return 0;
                score += item_score;
        }
        list_for_each_entry(item, &m->accept_list, mood_item_node) {
-               ret = get_item_score(aft_row, item, &item_score,
+               ret = get_item_score(item, &afsi, &afhi, path, &item_score,
                        &score_arg_sum);
-               if (ret < 0)
-                       return ret;
                if (ret == 0)
                        continue;
                match = 1;
@@ -174,10 +171,8 @@ static int compute_mood_score(const struct osl_row *aft_row, struct mood *m,
        if (!match && !list_empty(&m->accept_list))
                return 0;
        list_for_each_entry(item, &m->score_list, mood_item_node) {
-               ret = get_item_score(aft_row, item, &item_score,
+               ret = get_item_score(item, &afsi, &afhi, path, &item_score,
                        &score_arg_sum);
-               if (ret < 0)
-                       return ret;
                score += item_score;
        }
        if (score_arg_sum)
@@ -427,7 +422,7 @@ out:
 void mood_check_callback(int fd, __a_unused const struct osl_object *query)
 {
        struct para_buffer pb = {
-               .max_size = SHMMAX,
+               .max_size = shm_get_shmmax(),
                .private_data = &fd,
                .max_size_handler = pass_buffer_as_shm
        };
@@ -878,7 +873,7 @@ out:
  *
  * \sa mood_open(), mood_close().
  */
-int reload_current_mood(void)
+static int reload_current_mood(void)
 {
        int ret;
        char *mood_name = NULL;
@@ -895,10 +890,25 @@ int reload_current_mood(void)
        return ret;
 }
 
+/**
+ * Notification callback for the moods table.
+ *
+ * \param event Type of the event just occurred.
+ * \param pb Unused.
+ * \param data Its type depends on the event.
+ *
+ * This function performs actions required due to the occurrence of the given
+ * event. Possible actions include reload of the current mood and update of the
+ * score of an audio file.
+ */
 int moods_event_handler(enum afs_events event, __a_unused struct para_buffer *pb,
                void *data)
 {
-       switch(event) {
+       int ret;
+
+       if (!current_mood)
+               return 0;
+       switch (event) {
        /*
         * The three blob events might change the set of admissible files,
         * so we must reload the score list.
@@ -908,6 +918,10 @@ int moods_event_handler(enum afs_events event, __a_unused struct para_buffer *pb
        case BLOB_ADD:
                if (data == moods_table || data == playlists_table)
                        return 1; /* no reload necessary for these */
+               ret = clear_score_table();
+               if (ret < 0)
+                       PARA_CRIT_LOG("clear score table returned %s\n",
+                               para_strerror(-ret));
                return reload_current_mood();
        /* these also require reload of the score table */
        case ATTRIBUTE_ADD:
@@ -929,4 +943,3 @@ int moods_event_handler(enum afs_events event, __a_unused struct para_buffer *pb
                return 1;
        }
 }
-