2 * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file mood.c Paraslash's mood handling functions. */
18 * Contains statistical data of the currently admissible audio files.
20 * It is used to assign normalized score values to each admissible audio file.
22 struct afs_statistics
{
23 /** Sum of num played over all admissible files. */
24 int64_t num_played_sum
;
25 /** Sum of last played times over all admissible files. */
26 int64_t last_played_sum
;
27 /** Quadratic deviation of num played time. */
28 int64_t num_played_qd
;
29 /** Quadratic deviation of last played time. */
30 int64_t last_played_qd
;
31 /** Number of admissible files */
34 struct afs_statistics statistics
;
37 * Assign scores according to a mood_method.
39 * Each mood_method has its own mood_score_function. The first three parameters
40 * passed to that function are informations about the audio file whose score is
41 * to be computed. The data argument depends on the mood method this function
42 * is used for. It usually is the argument given at the end of a mood line.
44 * Mood score functions must return values between -100 and +100 inclusively.
45 * Boolean score functions should always return either -100 or +100.
47 * \sa struct mood_method, mood_parser.
49 typedef int mood_score_function(const char *path
, const struct afs_info
*afsi
,
50 const struct afh_info
*afhi
, const void *data
);
53 * Pre-process a mood line.
55 * The mood_parser of a mood_method is called once at mood open time for each
56 * line of the current mood definition that contains the mood_method's name as
57 * a keyword. The line is passed to the mood_parser as the first argument. The
58 * mood_parser must determine whether the line is syntactically correct and
59 * return a positive value if so and a negative value otherwise.
61 * Some mood parsers pre-process the data given in the mood line to compute a
62 * structure which depends of the particular mood_method and which is used
63 * later in the mood_score_function of the mood_method. The mood_parser may
64 * store a pointer to its structure via the second argument.
66 * \sa mood_open(), mood_cleanup_function, mood_score_function.
68 typedef int mood_parser(const char *, void **);
71 * Deallocate resources which were allocated by the mood_parser.
73 * This optional function of a mood_method is used to free any resources
74 * allocated in mood_open() by the mood_parser. The argument passed is a
75 * pointer to the mood_method specific data structure that was returned by the
80 typedef void mood_cleanup_function(void *);
83 * Used for scoring and to determine whether a file is admissible.
86 /* The name of the method. */
88 /** Pointer to the mood parser. */
90 /** Pointer to the score function */
91 mood_score_function
*score_function
;
92 /** Optional cleanup function. */
93 mood_cleanup_function
*cleanup
;
97 * Each line of the current mood corresponds to a mood_item.
100 /** The method this line is referring to. */
101 const struct mood_method
*method
;
102 /** The data structure computed by the mood parser. */
104 /** The given score value, or zero if none was given. */
106 /** Non-zero if random scoring was requested. */
108 /** Whether the "not" keyword was given in the mood line. */
110 /** The position in the list of items. */
111 struct list_head mood_item_node
;
115 * Created from the mood definition by mood_open().
117 * When a mood is opened, each line of its definition is investigated, and a
118 * corresponding mood item is produced. Each mood line starts with \p accept,
119 * \p deny, or \p score which determines the type of the mood line. For each
120 * such type a linked list is maintained whose entries are the mood items.
122 * \sa mood_item, mood_open().
125 /** The name of this mood. */
127 /** The list of mood items of type \p accept. */
128 struct list_head accept_list
;
129 /** The list of mood items of type \p deny. */
130 struct list_head deny_list
;
131 /** The list of mood items of type \p score. */
132 struct list_head score_list
;
135 static struct mood
*current_mood
;
138 * Rough approximation to sqrt.
140 * \param x Integer of which to calculate the sqrt.
142 * \return An integer res with res * res <= x.
144 static uint64_t int_sqrt(uint64_t x
)
146 uint64_t op
, res
, one
= 1;
155 if (op
>= res
+ one
) {
156 op
= op
- (res
+ one
);
162 // PARA_NOTICE_LOG("sqrt(%llu) = %llu\n", x, res);
166 static int mm_no_attributes_set_parser(const char *arg
, __a_unused
void **ignored
)
169 PARA_WARNING_LOG("ignored junk at eol: %s\n", arg
);
173 static int mm_no_attributes_set_score_function(__a_unused
const char *path
,
174 const struct afs_info
*afsi
,
175 __a_unused
const struct afh_info
*afhi
,
176 __a_unused
const void *data
)
178 if (!afsi
->attributes
)
183 static int mm_played_rarely_score_function(__a_unused
const char *path
,
184 const struct afs_info
*afsi
,
185 __a_unused
const struct afh_info
*afhi
,
186 __a_unused
const void *data
)
189 int ret
= get_num_admissible_files(&num
);
193 if (statistics
.num_played_sum
- num
* afsi
->num_played
194 > int_sqrt(statistics
.num_played_qd
* num
))
199 static int mm_played_rarely_parser(const char *arg
, __a_unused
void **ignored
)
202 PARA_WARNING_LOG("ignored junk at eol: %s\n", arg
);
206 static int mm_name_like_score_function(const char *path
,
207 __a_unused
const struct afs_info
*afsi
,
208 __a_unused
const struct afh_info
*afhi
,
211 if (fnmatch(data
, path
, 0))
216 static int mm_name_like_parser(const char *arg
, void **data
)
218 *data
= para_strdup(arg
);
222 static void mm_name_like_cleanup(void *data
)
227 static int mm_is_set_parser(const char *arg
, void **bitnum
)
229 unsigned char *c
= para_malloc(1);
230 int ret
= get_attribute_bitnum_by_name(arg
, c
);
239 static int mm_is_set_score_function(__a_unused
const char *path
,
240 __a_unused
const struct afs_info
*afsi
,
241 __a_unused
const struct afh_info
*afhi
,
244 const unsigned char *bn
= data
;
245 if (afsi
->attributes
& (1ULL << *bn
))
250 /* returns 1 if row matches score item, negative otherwise */
251 static int add_item_score(const struct osl_row
*row
, struct mood_item
*item
, long *score
,
254 struct afs_info afsi
;
255 struct afh_info afhi
;
259 *score_arg_sum
+= item
->random_score
? 100 : PARA_ABS(item
->score_arg
);
262 ret
= get_afsi_of_row(row
, &afsi
);
265 ret
= get_afhi_of_row(row
, &afhi
);
268 ret
= get_audio_file_path_of_row(row
, &path
);
271 ret
= item
->method
->score_function(path
, &afsi
, &afhi
,
273 if ((ret
< 0 && !item
->logical_not
) || (ret
>= 0 && item
->logical_not
))
274 return -1; /* no match */
276 if (item
->random_score
)
277 *score
+= PARA_ABS(ret
) * para_random(100);
279 *score
+= PARA_ABS(ret
) * item
->score_arg
;
283 static int compute_mood_score(const struct osl_row
*aft_row
, struct mood
*m
,
286 struct mood_item
*item
;
288 long score_arg_sum
= 0, score
= 0;
292 /* reject audio file if it matches any entry in the deny list */
293 list_for_each_entry(item
, &m
->deny_list
, mood_item_node
)
294 if (add_item_score(aft_row
, item
, &score
, &score_arg_sum
) > 0)
295 return -E_NOT_ADMISSIBLE
;
296 list_for_each_entry(item
, &m
->accept_list
, mood_item_node
)
297 if (add_item_score(aft_row
, item
, &score
, &score_arg_sum
) > 0)
299 /* reject if there is no matching entry in the accept list */
300 if (!match
&& !list_empty(&m
->accept_list
))
301 return -E_NOT_ADMISSIBLE
;
302 list_for_each_entry(item
, &m
->score_list
, mood_item_node
)
303 add_item_score(aft_row
, item
, &score
, &score_arg_sum
);
305 score
/= score_arg_sum
;
310 #define DEFINE_MOOD_METHOD(_name) \
311 .parser = mm_ ## _name ## _parser, \
312 .score_function = mm_ ## _name ## _score_function, \
315 #define DEFINE_MOOD_METHOD_WITH_CLEANUP(_name) \
316 DEFINE_MOOD_METHOD(_name), \
317 .cleanup = mm_ ## _name ## _cleanup
319 static const struct mood_method mood_methods
[] = {
320 {DEFINE_MOOD_METHOD(no_attributes_set
)},
321 {DEFINE_MOOD_METHOD(played_rarely
)},
322 {DEFINE_MOOD_METHOD(is_set
)},
323 {DEFINE_MOOD_METHOD_WITH_CLEANUP(name_like
)},
327 static void cleanup_list_entry(struct mood_item
*item
)
329 if (item
->method
&& item
->method
->cleanup
)
330 item
->method
->cleanup(item
->parser_data
);
332 free(item
->parser_data
);
333 list_del(&item
->mood_item_node
);
337 static void destroy_mood(struct mood
*m
)
339 struct mood_item
*tmp
, *item
;
343 list_for_each_entry_safe(item
, tmp
, &m
->accept_list
, mood_item_node
)
344 cleanup_list_entry(item
);
345 list_for_each_entry_safe(item
, tmp
, &m
->deny_list
, mood_item_node
)
346 cleanup_list_entry(item
);
347 list_for_each_entry_safe(item
, tmp
, &m
->score_list
, mood_item_node
)
348 cleanup_list_entry(item
);
353 static struct mood
*alloc_new_mood(const char *name
)
355 struct mood
*m
= para_calloc(sizeof(struct mood
));
356 m
->name
= para_strdup(name
);
357 INIT_LIST_HEAD(&m
->accept_list
);
358 INIT_LIST_HEAD(&m
->deny_list
);
359 INIT_LIST_HEAD(&m
->score_list
);
363 /** The different types of a mood line. */
364 enum mood_line_type
{
375 /** Data passed to the parser of a mood line. */
376 struct mood_line_parser_data
{
377 /** The mood this mood line belongs to. */
379 /** The line number in the mood definition. */
384 * <accept [with score <score>] | deny [with score <score>] | score <score>>
385 * [if] [not] <mood_method> [options]
386 * <score> is either an integer or "random" which assigns a random score to
390 static int parse_mood_line(char *mood_line
, void *data
)
392 struct mood_line_parser_data
*mlpd
= data
;
398 enum mood_line_type mlt
= ML_INVALID
;
399 struct mood_item
*mi
= NULL
;
400 char *buf
= para_strdup(mood_line
);
403 num_words
= split_args(buf
, &argv
, delim
);
405 if (!num_words
) /* empty line */
408 if (**w
== '#') /* comment */
410 if (!strcmp(*w
, "accept"))
412 else if (!strcmp(*w
, "deny"))
414 else if (!strcmp(*w
, "score"))
416 ret
= -E_MOOD_SYNTAX
;
417 if (mlt
== ML_INVALID
)
419 mi
= para_calloc(sizeof(struct mood_item
));
420 if (mlt
!= ML_SCORE
) {
421 ret
= -E_MOOD_SYNTAX
;
425 if (strcmp(*w
, "with"))
430 if (strcmp(*w
, "score"))
433 if (mlt
== ML_SCORE
|| !strcmp(*w
, "score")) {
434 ret
= -E_MOOD_SYNTAX
;
438 if (strcmp(*w
, "random")) {
439 mi
->random_score
= 0;
440 ret
= para_atoi32(*w
, &mi
->score_arg
);
444 mi
->random_score
= 1;
446 goto success
; /* the line "score random" is valid */
450 ret
= -E_MOOD_SYNTAX
;
455 if (!strcmp(*w
, "if")) {
456 ret
= -E_MOOD_SYNTAX
;
461 if (!strcmp(*w
, "not")) {
462 ret
= -E_MOOD_SYNTAX
;
469 for (i
= 0; mood_methods
[i
].parser
; i
++) {
470 if (strcmp(*w
, mood_methods
[i
].name
))
474 ret
= -E_MOOD_SYNTAX
;
475 if (!mood_methods
[i
].parser
)
478 ret
= mood_methods
[i
].parser(*w
, &mi
->parser_data
);
481 mi
->method
= &mood_methods
[i
];
484 if (mlt
== ML_ACCEPT
)
485 para_list_add(&mi
->mood_item_node
, &mlpd
->m
->accept_list
);
486 else if (mlt
== ML_DENY
)
487 para_list_add(&mi
->mood_item_node
, &mlpd
->m
->deny_list
);
489 para_list_add(&mi
->mood_item_node
, &mlpd
->m
->score_list
);
491 PARA_DEBUG_LOG("%s entry added, method: %p\n", mlt
== ML_ACCEPT
? "accept" :
492 (mlt
== ML_DENY
? "deny" : "score"), mi
->method
);
500 free(mi
->parser_data
);
506 static int load_mood(const struct osl_row
*mood_row
, struct mood
**m
)
509 struct osl_object mood_def
;
510 struct mood_line_parser_data mlpd
= {.line_num
= 0};
511 int ret
= mood_get_name_and_def_by_row(mood_row
, &mood_name
, &mood_def
);
517 mlpd
.m
= alloc_new_mood(mood_name
);
518 ret
= for_each_line_ro(mood_def
.data
, mood_def
.size
,
519 parse_mood_line
, &mlpd
);
520 osl_close_disk_object(&mood_def
);
522 PARA_ERROR_LOG("unable to load mood %s: %s\n", mlpd
.m
->name
,
523 PARA_STRERROR(-ret
));
524 destroy_mood(mlpd
.m
);
531 static int check_mood(struct osl_row
*mood_row
, void *data
)
533 struct para_buffer
*pb
= data
;
535 struct osl_object mood_def
;
536 struct mood_line_parser_data mlpd
= {.line_num
= 0};
538 int ret
= mood_get_name_and_def_by_row(mood_row
, &mood_name
, &mood_def
);
541 para_printf(pb
, "failed to get mood definition\n");
544 if (!*mood_name
) /* ignore dummy row */
546 para_printf(pb
, "checking mood %s...\n", mood_name
);
547 ret
= for_each_line_ro(mood_def
.data
, mood_def
.size
,
548 parse_mood_line
, &mlpd
);
550 para_printf(pb
, "%s line %u: %s\n", mood_name
, mlpd
.line_num
,
551 PARA_STRERROR(-ret
));
553 osl_close_disk_object(&mood_def
);
558 * Check all moods for syntax errors.
560 * \param query Unused.
561 * \param result: Contains check messages.
563 int mood_check_callback(__a_unused
const struct osl_object
*query
,
564 struct osl_object
*result
)
566 struct para_buffer pb
= {.buf
= NULL
};
568 para_printf(&pb
, "checking moods...\n");
569 osl_rbtree_loop(moods_table
, BLOBCOL_ID
, &pb
,
571 result
->data
= pb
.buf
;
572 result
->size
= pb
.size
;
577 static unsigned int_log2(uint64_t x
)
589 static int64_t normalized_value(int64_t x
, int64_t n
, int64_t sum
, int64_t qd
)
593 return 100 * (n
* x
- sum
) / (int64_t)int_sqrt(n
* qd
);
596 static long compute_num_played_score(struct afs_info
*afsi
)
598 return -normalized_value(afsi
->num_played
, statistics
.num
,
599 statistics
.num_played_sum
, statistics
.num_played_qd
);
602 static long compute_last_played_score(struct afs_info
*afsi
)
604 return -normalized_value(afsi
->last_played
, statistics
.num
,
605 statistics
.last_played_sum
, statistics
.last_played_qd
);
608 static long compute_dynamic_score(const struct osl_row
*aft_row
)
610 struct afs_info afsi
;
611 int64_t score
, nscore
= 0, lscore
= 0;
614 ret
= get_afsi_of_row(aft_row
, &afsi
);
617 nscore
= compute_num_played_score(&afsi
);
618 lscore
= compute_last_played_score(&afsi
);
619 score
= nscore
+ lscore
;
623 static int add_afs_statistics(const struct osl_row
*row
)
626 struct afs_info afsi
;
629 ret
= get_afsi_of_row(row
, &afsi
);
633 x
= afsi
.last_played
;
634 s
= statistics
.last_played_sum
;
636 statistics
.last_played_qd
+= (x
- s
/ n
) * (x
- s
/ n
) * n
/ (n
+ 1);
637 statistics
.last_played_sum
+= x
;
640 s
= statistics
.num_played_sum
;
642 statistics
.num_played_qd
+= (x
- s
/ n
) * (x
- s
/ n
) * n
/ (n
+ 1);
643 statistics
.num_played_sum
+= x
;
648 static int del_afs_statistics(const struct osl_row
*row
)
650 uint64_t n
, s
, q
, a
, new_s
;
651 struct afs_info afsi
;
653 ret
= get_afsi_of_row(row
, &afsi
);
659 memset(&statistics
, 0, sizeof(statistics
));
663 s
= statistics
.last_played_sum
;
664 q
= statistics
.last_played_qd
;
665 a
= afsi
.last_played
;
667 statistics
.last_played_sum
= new_s
;
668 statistics
.last_played_qd
= q
+ s
* s
/ n
- a
* a
669 - new_s
* new_s
/ (n
- 1);
671 s
= statistics
.num_played_sum
;
672 q
= statistics
.num_played_qd
;
675 statistics
.num_played_sum
= new_s
;
676 statistics
.num_played_qd
= q
+ s
* s
/ n
- a
* a
677 - new_s
* new_s
/ (n
- 1);
684 * Structure used during mood_open().
686 * At mood open time, we look at each file in the audio file table in order to
687 * determine whether it is admissible. If a file happens to be admissible, its
688 * mood score is computed by calling each relevant mood_score_function. Next,
689 * we update the afs_statistics and add a struct admissible_file_info to a
692 * If all files have been processed that way, the final score of each
693 * admissible file is computed by adding the dynamic score (which depends on
694 * the afs_statistics) to the mood score. Finally, all audio files in the
695 * array are added to the score table and the admissible array is freed.
697 * \sa mood_method, admissible_array.
699 struct admissible_file_info
701 /** The admissible audio file. */
702 struct osl_row
*aft_row
;
707 /** The temporary array of admissible files. */
708 struct admissible_array
{
709 /** Files are admissible wrt. this mood. */
711 /** The size of the array */
713 /** Pointer to the array of admissible files. */
714 struct admissible_file_info
*array
;
718 * Add an entry to the array of admissible files.
720 * \param aft_row The audio file to be added.
721 * \param private_data Pointer to a struct admissible_file_info.
723 * \return Negative on errors, positive on success.
725 static int add_if_admissible(struct osl_row
*aft_row
, void *data
)
727 struct admissible_array
*aa
= data
;
731 ret
= compute_mood_score(aft_row
, aa
->m
, &score
);
733 return (ret
== -E_NOT_ADMISSIBLE
)? 1 : ret
;
734 if (statistics
.num
>= aa
->size
) {
737 aa
->array
= para_realloc(aa
->array
,
738 aa
->size
* sizeof(struct admissible_file_info
));
740 aa
->array
[statistics
.num
].aft_row
= aft_row
;
741 aa
->array
[statistics
.num
].score
= score
;
742 ret
= add_afs_statistics(aft_row
);
749 * Compute the new quadratic deviation in case one element changes.
751 * \param n Number of elements.
752 * \param old_qd The quadratic deviation before the change.
753 * \param old_val The value that was replaced.
754 * \param new_val The replacement value.
755 * \param old_sum The sum of all elements before the update.
757 * \return The new quadratic deviation resulting from replacing old_val
760 * Given n real numbers a_1, ..., a_n, their sum S = a_1 + ... + a_n,
761 * their quadratic deviation
763 * q = (a_1 - S/n)^2 + ... + (a_n - S/n)^2,
765 * and a real number b, the quadratic deviation q' of a_1,...a_{n-1}, b (ie.
766 * the last number a_n was replaced by b) may be computed in O(1) time in terms
767 * of n, q, a_n, b, and S as
769 * q' = q + d * s - (2 * S + d) * d / n,
771 * where d = b - a_n, and s = b + a_n.
773 * Example: n = 3, a_1 = 3, a_2 = 5, a_3 = 7, b = 10. Then S = 15, q = 8, d = 3,
776 * q + d * s - (2 * S + d) * d / n = 8 + 51 - 33 = 26,
778 * which equals q' = (3 - 6)^2 + (5 - 6)^2 + (10 - 6)^2.
781 _static_inline_
int64_t update_quadratic_deviation(int64_t n
, int64_t old_qd
,
782 int64_t old_val
, int64_t new_val
, int64_t old_sum
)
784 int64_t delta
= new_val
- old_val
;
785 int64_t sigma
= new_val
+ old_val
;
786 return old_qd
+ delta
* sigma
- (2 * old_sum
+ delta
) * delta
/ n
;
789 static int update_afs_statistics(struct afs_info
*old_afsi
, struct afs_info
*new_afsi
)
792 int ret
= get_num_admissible_files(&n
);
798 statistics
.last_played_qd
= update_quadratic_deviation(n
,
799 statistics
.last_played_qd
, old_afsi
->last_played
,
800 new_afsi
->last_played
, statistics
.last_played_sum
);
801 statistics
.last_played_sum
+= new_afsi
->last_played
- old_afsi
->last_played
;
803 statistics
.num_played_qd
= update_quadratic_deviation(n
,
804 statistics
.num_played_qd
, old_afsi
->num_played
,
805 new_afsi
->num_played
, statistics
.num_played_sum
);
806 statistics
.num_played_sum
+= new_afsi
->num_played
- old_afsi
->num_played
;
810 static int add_to_score_table(const struct osl_row
*aft_row
, long mood_score
)
812 long score
= (compute_dynamic_score(aft_row
) + mood_score
) / 3;
813 return score_add(aft_row
, score
);
816 static int delete_from_statistics_and_score_table(const struct osl_row
*aft_row
)
818 int ret
= del_afs_statistics(aft_row
);
821 return score_delete(aft_row
);
825 * Delete one entry from the statistics and from the score table.
827 * \param aft_row The audio file which is no longer admissible.
829 * \return Positive on success, negative on errors.
831 * \sa score_delete().
833 static int mood_delete_audio_file(const struct osl_row
*aft_row
)
837 ret
= row_belongs_to_score_table(aft_row
, NULL
);
840 if (!ret
) /* not admissible, nothing to do */
842 return delete_from_statistics_and_score_table(aft_row
);
846 * Compute the new score of an audio file wrt. the current mood.
848 * \param aft_row Determines the audio file.
849 * \param old_afsi The audio file selector info before updating.
851 * The \a old_afsi argument may be \p NULL which indicates that no changes to
852 * the audio file info were made.
854 * \return Positive on success, negative on errors.
856 static int mood_update_audio_file(const struct osl_row
*aft_row
,
857 struct afs_info
*old_afsi
)
860 int ret
, is_admissible
, was_admissible
= 0;
861 struct afs_info afsi
;
865 return 1; /* nothing to do */
866 ret
= row_belongs_to_score_table(aft_row
, &rank
);
869 was_admissible
= ret
;
870 ret
= compute_mood_score(aft_row
, current_mood
, &score
);
871 is_admissible
= (ret
> 0);
872 if (!was_admissible
&& !is_admissible
)
874 if (was_admissible
&& !is_admissible
)
875 return delete_from_statistics_and_score_table(aft_row
);
876 if (!was_admissible
&& is_admissible
) {
877 ret
= add_afs_statistics(aft_row
);
880 return add_to_score_table(aft_row
, score
);
883 ret
= get_afsi_of_row(aft_row
, &afsi
);
887 ret
= update_afs_statistics(old_afsi
, &afsi
);
891 score
+= compute_num_played_score(&afsi
);
892 score
+= compute_last_played_score(&afsi
);
894 PARA_DEBUG_LOG("score: %li\n", score
);
895 percent
= (score
+ 100) / 3;
898 else if (percent
< 0)
900 PARA_DEBUG_LOG("moving from rank %u to %lu%%\n", rank
, percent
);
901 return score_update(aft_row
, percent
);
904 static void log_statistics(void)
906 unsigned n
= statistics
.num
;
909 PARA_NOTICE_LOG("no admissible files\n");
912 PARA_INFO_LOG("last_played mean: %lli, last_played sigma: %llu\n",
913 (long long int)(statistics
.last_played_sum
/ n
),
914 (long long unsigned)int_sqrt(statistics
.last_played_qd
/ n
));
915 PARA_INFO_LOG("num_played mean: %lli, num_played sigma: %llu\n",
916 (long long int)statistics
.num_played_sum
/ n
,
917 (long long unsigned)int_sqrt(statistics
.num_played_qd
/ n
));
921 * Close the current mood.
923 * Free all resources of the current mood which were allocated during
926 void close_current_mood(void)
928 destroy_mood(current_mood
);
930 memset(&statistics
, 0, sizeof(statistics
));
935 * Change the current mood.
937 * \param mood_name The name of the mood to open.
939 * If \a mood_name is \a NULL, load the dummy mood that accepts every audio file
940 * and uses a scoring method based only on the \a last_played information.
942 * If there is already an open mood, it will be closed first.
944 * \return Positive on success, negative on errors. Loading the dummy mood
947 * \sa struct admissible_file_info, struct admissible_array, struct
948 * afs_info::last_played, mood_close().
950 int change_current_mood(char *mood_name
)
953 struct admissible_array aa
= {
961 struct osl_object obj
= {
963 .size
= strlen(mood_name
) + 1
965 ret
= osl_get_row(moods_table
, BLOBCOL_NAME
, &obj
, &row
);
967 PARA_NOTICE_LOG("no such mood: %s\n", mood_name
);
970 ret
= load_mood(row
, &m
);
973 close_current_mood();
976 close_current_mood();
977 current_mood
= alloc_new_mood("dummy");
980 PARA_NOTICE_LOG("computing statistics of admissible files\n");
981 ret
= audio_file_loop(&aa
, add_if_admissible
);
985 PARA_INFO_LOG("%d admissible files \n", statistics
.num
);
986 for (i
= 0; i
< statistics
.num
; i
++) {
987 struct admissible_file_info
*a
= aa
.array
+ i
;
988 ret
= add_to_score_table(a
->aft_row
, a
->score
);
992 PARA_NOTICE_LOG("loaded mood %s\n", current_mood
->name
);
993 ret
= statistics
.num
;
999 * Close and re-open the current mood.
1001 * This function is used if changes to the audio file table or the
1002 * attribute table were made that render the current list of admissible
1003 * files useless. For example, if an attribute is removed from the
1004 * attribute table, this function is called.
1006 * \return Positive on success, negative on errors. If no mood is currently
1007 * open, the function returns success.
1009 * \sa mood_open(), mood_close().
1011 int reload_current_mood(void)
1014 char *mood_name
= NULL
;
1016 PARA_NOTICE_LOG("reloading current mood\n");
1019 if (current_mood
->name
)
1020 mood_name
= para_strdup(current_mood
->name
);
1021 close_current_mood();
1022 ret
= change_current_mood(mood_name
);
1027 int moods_event_handler(enum afs_events event
, __a_unused
struct para_buffer
*pb
,
1032 * The three blob events might change the set of admissible files,
1033 * so we must reload the score list.
1038 if (data
== moods_table
|| data
== playlists_table
)
1039 return 1; /* no reload necessary for these */
1040 return reload_current_mood();
1041 /* these also require reload of the score table */
1043 case ATTRIBUTE_REMOVE
:
1044 case ATTRIBUTE_RENAME
:
1045 return reload_current_mood();
1046 /* changes to the aft only require to re-examine the audio file */
1048 struct afsi_change_event_data
*aced
= data
;
1049 return mood_update_audio_file(aced
->aft_row
, aced
->old_afsi
);
1052 case AUDIO_FILE_RENAME
:
1053 case AUDIO_FILE_ADD
:
1054 return mood_update_audio_file(data
, NULL
);
1055 case AUDIO_FILE_REMOVE
:
1056 return mood_delete_audio_file(data
);