2 * Copyright (C) 2007-2013 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file score.c Scoring functions to determine the audio file streaming order. */
18 static struct osl_table *score_table;
20 static int ptr_compare(const struct osl_object *obj1, const struct osl_object *obj2)
22 void *d1 = *(void **)obj1->data;
23 void *d2 = *(void **)obj2->data;
24 return NUM_COMPARE(d1, d2);
28 * Compare the score of two audio files
30 * \param obj1 Pointer to the first score object.
31 * \param obj2 Pointer to the second score object.
33 * This function first compares the score values as usual integers. If they compare as
34 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
35 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
37 * \sa osl_compare_function.
39 static int score_compare(const struct osl_object *obj1, const struct osl_object *obj2)
41 long d1 = *(long *)obj1->data;
42 long d2 = *(long *)obj2->data;
43 int ret = NUM_COMPARE(d2, d1);
47 return NUM_COMPARE(obj2->data, obj1->data);
51 * The score table consists of two columns: The \a aft_row column contains
52 * pointers to the rows of the audio file table, and the score column contains
53 * the current score of the audio file associated with that row.
55 enum score_table_columns {
56 /** The row of the audio file. */
60 /** This table has two columns */
64 static struct osl_column_description score_cols[] = {
65 [SCORECOL_AFT_ROW] = {
66 .storage_type = OSL_NO_STORAGE,
67 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE | OSL_DONT_FREE,
69 .compare_function = ptr_compare,
70 .data_size = sizeof(void *)
73 .storage_type = OSL_NO_STORAGE,
74 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
76 .compare_function = score_compare,
77 .data_size = sizeof(long)
81 static struct osl_table_description score_table_desc = {
83 .num_columns = NUM_SCORE_COLUMNS,
85 .column_descriptions = score_cols
89 * Compute the number of files in score table.
91 * \param num Result is returned here.
93 * \return Positive on success, negative on errors.
95 * \sa osl_get_num_rows().
97 int get_num_admissible_files(unsigned *num)
99 return osl(osl_get_num_rows(score_table, num));
103 * Get the score of the audio file associated with given row of the score table.
105 * \param score_row Pointer to the row in the score table.
106 * \param score Result is returned here on success.
108 * On errors (negative return value) the content of \a score is undefined.
110 * \return The return value of the underlying call to osl_get_object().
112 static int get_score_of_row(void *score_row, long *score)
114 struct osl_object obj;
115 int ret = osl(osl_get_object(score_table, score_row, SCORECOL_SCORE, &obj));
118 *score = *(long *)obj.data;
123 * Add an entry to the table of admissible files.
125 * \param aft_row The audio file to be added.
126 * \param score The score for this file.
128 * \return The return value of the underlying call to osl_add_row().
130 int score_add(const struct osl_row *aft_row, long score)
133 struct osl_object score_objs[NUM_SCORE_COLUMNS];
137 size = score_table_desc.column_descriptions[SCORECOL_AFT_ROW].data_size;
138 score_objs[SCORECOL_AFT_ROW].data = (struct osl_row *)aft_row;
139 score_objs[SCORECOL_AFT_ROW].size = size;
141 size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
142 score_objs[SCORECOL_SCORE].data = para_malloc(size);
143 score_objs[SCORECOL_SCORE].size = size;
144 *(long *)(score_objs[SCORECOL_SCORE].data) = score;
146 // PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
147 ret = osl(osl_add_row(score_table, score_objs));
149 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
150 free(score_objs[SCORECOL_SCORE].data);
155 static int get_nth_score(unsigned n, long *score)
158 int ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, n, &row));
162 return get_score_of_row(row, score);
166 * Replace a row of the score table.
168 * \param aft_row Determines the audio file to change.
169 * \param percent The position to re-insert the audio file.
171 * The percent parameter must be between \p 0 and 100 and. A value of zero
172 * means to re-insert the audio file into the score table with a score lower
173 * than any other admissible file.
175 * \return Positive on success, negative on errors. Possible errors: Errors
176 * returned by osl_get_row(), get_num_admissible_files(), osl_get_nth_row(),
177 * osl_get_object(), osl_update_object().
179 int score_update(const struct osl_row *aft_row, long percent)
184 struct osl_object obj = {.data = (struct osl_row *)aft_row,
185 .size = sizeof(aft_row)};
186 int ret = osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, &row));
188 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) /* not an error */
192 ret = get_num_admissible_files(&n);
195 new_pos = 1 + (n - 1) * percent / 100;
196 ret = get_nth_score(new_pos, &new_score);
200 obj.size = sizeof(long);
201 obj.data = para_malloc(obj.size);
202 *(long *)obj.data = new_score;
203 PARA_DEBUG_LOG("new score: %ld, rank %u/%u\n", new_score, new_pos, n);
204 return osl(osl_update_object(score_table, row, SCORECOL_SCORE, &obj));
208 * Given an admissible file, get the corresponding row in the aft and the score.
210 * \param score_row Determines the admissible file.
211 * \param score Result pointer.
212 * \param aft_row Result pointer.
214 * \return Negative on errors, positive on success. Possible errors: Errors
215 * returned by osl_get_object().
217 int get_score_and_aft_row(struct osl_row *score_row, long *score,
218 struct osl_row **aft_row)
220 struct osl_object obj;
221 int ret = get_score_of_row(score_row, score);
225 ret = osl(osl_get_object(score_table, score_row, SCORECOL_AFT_ROW, &obj));
232 static int get_score_row_from_aft_row(const struct osl_row *aft_row,
233 struct osl_row **score_row)
235 struct osl_object obj = {.data = (struct osl_row *)aft_row,
236 .size = sizeof(aft_row)};
237 return osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row));
241 * Loop over all files in the score table.
243 * \param data A pointer to arbitrary data.
244 * \param func Function to be called for each admissible file.
246 * \return The return value of the underlying call to osl_rbtree_loop().
248 * This is used for the ls command. The \a data parameter is passed as the
249 * second argument to \a func.
251 * \sa admissible_file_loop_reverse().
253 int admissible_file_loop(void *data, osl_rbtree_loop_func *func)
255 return osl(osl_rbtree_loop(score_table, SCORECOL_SCORE, data, func));
259 * Loop over all files in the score table in reverse order.
261 * \param data As in admissible_file_loop().
262 * \param func As in admissible_file_loop().
264 * \return Same return value as admissible_file_loop().
266 * \sa admissible_file_loop(), osl_rbtree_loop_reverse().
268 int admissible_file_loop_reverse(void *data, osl_rbtree_loop_func *func)
270 return osl(osl_rbtree_loop_reverse(score_table, SCORECOL_SCORE, data, func));
274 * Get the admissible audio file with highest score.
276 * \param aft_row Points to the row in the aft of the "best" audio file.
277 * \param score Highest score value in the score table.
279 * \return Positive on success, negative on errors. Possible errors: Errors
280 * returned by osl_rbtree_last_row(), osl_get_object().
282 int score_get_best(struct osl_row **aft_row, long *score)
285 struct osl_object obj;
286 int ret = osl(osl_rbtree_last_row(score_table, SCORECOL_SCORE, &row));
290 ret = osl(osl_get_object(score_table, row, SCORECOL_AFT_ROW, &obj));
294 return get_score_of_row(row, score);
298 * Remove an entry from the rbtree of admissible files.
300 * \param aft_row The file which is no longer admissible.
302 * \return Positive on success, negative on errors. Possible errors:
303 * Errors returned by osl_get_row() and osl_del_row().
305 * \sa score_add(), score_shutdown().
307 int score_delete(const struct osl_row *aft_row)
309 struct osl_row *score_row;
310 int ret = get_score_row_from_aft_row(aft_row, &score_row);
314 return osl(osl_del_row(score_table, score_row));
318 * Find out whether an audio file is contained in the score table.
320 * \param aft_row The row of the audio file table.
321 * \param rank Result pointer
323 * \return Positive, if \a aft_row belongs to the audio file table,
324 * zero if not, negative on errors. If \a aft_row was found, and \a rank
325 * is not \p NULL, the rank of \a aft_row is returned in \a rank.
327 int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank)
329 struct osl_row *score_row;
330 int ret = get_score_row_from_aft_row(aft_row, &score_row);
332 if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
338 ret = osl(osl_get_rank(score_table, score_row, SCORECOL_SCORE, rank));
344 /* Close the score table. */
345 static void score_close(void)
347 osl_close_table(score_table, OSL_FREE_VOLATILE);
352 * Open the score table.
356 * \return The return value of the underlying call to osl_open_table().
358 static int score_open(__a_unused const char *dir)
360 score_table_desc.dir = NULL; /* this table has only volatile columns */
361 return osl(osl_open_table(&score_table_desc, &score_table));
365 * Remove all entries from the score table, but keep the table open.
369 int clear_score_table(void)
372 return score_open(NULL);
375 static int score_event_handler(__a_unused enum afs_events event,
376 __a_unused struct para_buffer *pb, __a_unused void *data)
382 * Initialize the scoring subsystem.
384 * \param t The members of \a t are filled in by the function.
386 void score_init(struct afs_table *t)
388 t->name = score_table_desc.name;
389 t->open = score_open;
390 t->close = score_close;
391 t->event_handler = score_event_handler;