2 * Copyright (C) 2007 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. */
15 static void *score_table;
17 static int ptr_compare(const struct osl_object *obj1, const struct osl_object *obj2)
19 void *d1 = *(void **)obj1->data;
20 void *d2 = *(void **)obj2->data;
21 return NUM_COMPARE(d1, d2);
25 * Compare the score of two audio files
27 * \param obj1 Pointer to the first score object.
28 * \param obj2 Pointer to the second score object.
30 * This function first compares the score values as usual integers. If they compare as
31 * equal, the addresss of \a obj1 and \a obj2 are compared. So this compare function
32 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
34 * \sa osl_compare_function.
36 static int score_compare(const struct osl_object *obj1, const struct osl_object *obj2)
38 int d1 = *(int*)obj1->data;
39 int d2 = *(int*)obj2->data;
40 int ret = NUM_COMPARE(d2, d1);
44 return NUM_COMPARE(obj2->data, obj1->data);
48 * The score table consists of two colums: The \a aft_row column contains
49 * pointers to the rows of the audio file table, and the score column contains
50 * the current score of the audio file associated with that row.
52 enum score_table_columns {
53 /** The row of the audio file. */
57 /** This table has two columns */
61 static struct osl_column_description score_cols[] = {
62 [SCORECOL_AFT_ROW] = {
63 .storage_type = OSL_NO_STORAGE,
64 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
66 .compare_function = ptr_compare,
67 .data_size = sizeof(void *)
70 .storage_type = OSL_NO_STORAGE,
71 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
73 .compare_function = score_compare,
74 .data_size = sizeof(long)
78 static struct osl_table_description score_table_desc = {
80 .num_columns = NUM_SCORE_COLUMNS,
82 .column_descriptions = score_cols
86 * Compute the number of files in score table.
88 * \param num Result is returned here.
90 * \return Positive on success, negative on errors.
92 * \sa osl_get_num_rows().
94 int get_num_admissible_files(unsigned *num)
96 return osl_get_num_rows(score_table, num);
100 * Get the score of the audio file associated with given row of the score table.
102 * \param score_row Pointer to the row in the score table.
103 * \param score Result is returned here on success.
105 * On errors (negative return value) the content of \a score is undefined.
107 * \return The return value of the underlying call to osl_get_object().
109 static int get_score_of_row(void *score_row, long *score)
111 struct osl_object obj;
112 int ret = osl_get_object(score_table, score_row, SCORECOL_SCORE, &obj);
115 *score = *(long *)obj.data;
120 * Add an entry to the table of admissible files.
122 * \param aft_row The audio file to be added.
123 * \param score The score for this file.
125 * \return The return value of the underlying call to osl_add_row().
127 int score_add(const struct osl_row *aft_row, long score)
130 struct osl_object score_objs[NUM_SCORE_COLUMNS];
134 size = score_table_desc.column_descriptions[SCORECOL_AFT_ROW].data_size;
135 score_objs[SCORECOL_AFT_ROW].data = para_malloc(size);
136 score_objs[SCORECOL_AFT_ROW].size = size;
137 *(const void **)(score_objs[SCORECOL_AFT_ROW].data) = aft_row;
139 size = score_table_desc.column_descriptions[SCORECOL_SCORE].data_size;
140 score_objs[SCORECOL_SCORE].data = para_malloc(size);
141 score_objs[SCORECOL_SCORE].size = size;
142 *(int *)(score_objs[SCORECOL_SCORE].data) = score;
144 // PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
145 ret = osl_add_row(score_table, score_objs);
147 PARA_ERROR_LOG("failed to add row: %d\n", ret);
148 free(score_objs[SCORECOL_AFT_ROW].data);
149 free(score_objs[SCORECOL_SCORE].data);
154 static int get_nth_score(unsigned n, long *score)
157 int ret = osl_get_nth_row(score_table, SCORECOL_SCORE, n, &row);
161 return get_score_of_row(row, score);
165 * Replace a row of the score table.
167 * \param aft_row Determines the audio file to change.
168 * \param percent The position to re-insert the audio file.
170 * The percent paramenter must be between \p 0 and 100 and. A value of zero
171 * means to re-insert the audio file into the score table with a score lower
172 * than any other admissible file.
174 * \return Positive on success, negative on errors. Possible errors: Errors
175 * returned by osl_get_row(), get_num_admissible_files(), osl_get_nth_row(),
176 * osl_get_object(), osl_update_object().
178 int score_update(const struct osl_row *aft_row, long percent)
183 struct osl_object obj = {.data = &aft_row, .size = sizeof(void **)};
184 int ret = osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, &row);
188 ret = get_num_admissible_files(&n);
191 new_pos = 1 + (n - 1) * percent / 100;
192 ret = get_nth_score(new_pos, &new_score);
196 obj.size = sizeof(long);
197 obj.data = para_malloc(obj.size);
198 *(long *)obj.data = new_score;
199 PARA_DEBUG_LOG("new score: %ld, position: %u/%u\n", new_score,
201 return osl_update_object(score_table, row, SCORECOL_SCORE, &obj);
205 * Given an admissible file, get the corresponding row in the aft and the score.
207 * \param score_row Determines the admissible file.
208 * \param score Result pointer.
209 * \param aft_row Result pointer.
211 * \return Negative on errors, positive on success. Possible errors: Errors
212 * returned by osl_get_object().
214 int get_score_and_aft_row(struct osl_row *score_row, long *score,
215 struct osl_row **aft_row)
217 struct osl_object obj;
218 int ret = get_score_of_row(score_row, score);
222 ret = osl_get_object(score_table, score_row, SCORECOL_AFT_ROW, &obj);
225 *aft_row = *(void **)obj.data;
229 static int get_score_row_from_aft_row(const struct osl_row *aft_row,
230 struct osl_row **score_row)
232 struct osl_object obj = {.data = &aft_row, .size = sizeof(void **)};
233 return osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row);
238 * Loop over all files in the score table.
240 * \param data A pointer to arbitrary data.
241 * \param func Function to be called for each admissible file.
243 * \return The return value of the underlying call to osl_rbtree_loop().
245 * This is used for the ls command. The \a data parameter is passed as the
246 * second argument to \a func.
248 * \sa admissible_file_loop_reverse().
250 int admissible_file_loop(void *data, osl_rbtree_loop_func *func)
252 return osl_rbtree_loop(score_table, SCORECOL_SCORE, data, func);
256 * Loop over all files in the score table in reverse order.
258 * \param data As in admissible_file_loop().
259 * \param func As in admissible_file_loop().
261 * \return Same return value as admissible_file_loop().
263 * \sa admissible_file_loop(), osl_rbtree_loop_reverse().
265 int admissible_file_loop_reverse(void *data, osl_rbtree_loop_func *func)
267 return osl_rbtree_loop_reverse(score_table, SCORECOL_SCORE, data, func);
271 * Get the admissible audio file with highest score.
273 * \param aft_row Points to the row in the aft of the "best" audio file.
274 * \param score Highest score value in the score table.
276 * \return Positive on success, negative on errors. Possible errors: Errors
277 * returned by osl_rbtree_last_row(), osl_get_object().
279 int score_get_best(struct osl_row **aft_row, long *score)
282 struct osl_object obj;
283 int ret = osl_rbtree_last_row(score_table, SCORECOL_SCORE, &row);
287 ret = osl_get_object(score_table, row, SCORECOL_AFT_ROW, &obj);
290 *aft_row = *(void **)obj.data;
291 return get_score_of_row(row, score);
295 * Remove an entry from the rbtree of admissible files.
297 * \param aft_row The file which is no longer admissible.
299 * \return Positive on success, negative on errors. Possible errors:
300 * Errors returned by osl_get_row() and osl_del_row().
302 * \sa score_add(), score_shutdown().
304 int score_delete(const struct osl_row *aft_row)
306 struct osl_row *score_row;
307 int ret = get_score_row_from_aft_row(aft_row, &score_row);
311 return osl_del_row(score_table, score_row);
315 * Find out whether an audio file is contained in the score table.
317 * \param aft_row The row of the audio file table.
319 * \return Positive, if \a aft_row belongs to the audio file table,
320 * zero if not, negative on errors.
322 int row_belongs_to_score_table(const struct osl_row *aft_row)
324 struct osl_row *score_row;
325 int ret = get_score_row_from_aft_row(aft_row, &score_row);
328 if (ret == -E_RB_KEY_NOT_FOUND)
334 * Close the score table.
336 * \param flags Options passed to osl_close_table().
340 void score_shutdown(enum osl_close_flags flags)
342 osl_close_table(score_table, flags | OSL_FREE_VOLATILE);
347 * Init the scoring subsystem.
349 * \param ti Gets filled in by the function.
350 * \param db The database directory.
352 * \return The return value of the underlying call to osl_open_table().
354 * \sa score_shutdown().
356 int score_init(struct table_info *ti, const char *db)
358 score_table_desc.dir = db;
359 ti->desc = &score_table_desc;
360 ti->flags = TBLFLAG_SKIP_CREATE;
361 int ret = osl_open_table(ti->desc, &ti->table);
363 score_table = ti->table;