1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Miscellaneous helper API definitions
12 * Copyright (c) 2007 Michael Sevakis
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
24 #include "mpegplayer.h"
28 /* Initializes the cursor */
29 void stream_scan_init(struct stream_scan
*sk
)
31 dbuf_l2_init(&sk
->l2
);
34 /* Ensures direction is -1 or 1 and margin is properly initialized */
35 void stream_scan_normalize(struct stream_scan
*sk
)
39 sk
->dir
= SSCAN_FORWARD
;
44 sk
->dir
= SSCAN_REVERSE
;
49 /* Moves a scan cursor. If amount is positive, the increment is in the scan
50 * direction, otherwise opposite the scan direction */
51 void stream_scan_offset(struct stream_scan
*sk
, off_t by
)
53 off_t bydir
= by
*sk
->dir
;
60 void ts_to_hms(uint32_t pts
, struct hms
*hms
)
62 hms
->frac
= pts
% TS_SECOND
;
63 hms
->sec
= pts
/ TS_SECOND
;
64 hms
->min
= hms
->sec
/ 60;
65 hms
->hrs
= hms
->min
/ 60;
70 void hms_format(char *buf
, size_t bufsize
, struct hms
*hms
)
72 /* Only display hours if nonzero */
75 rb
->snprintf(buf
, bufsize
, "%u:%02u:%02u",
76 hms
->hrs
, hms
->min
, hms
->sec
);
80 rb
->snprintf(buf
, bufsize
, "%u:%02u",
86 uint32_t muldiv_uint32(uint32_t multiplicand
,
92 uint64_t prod
= (uint64_t)multiplier
*multiplicand
+ divisor
/2;
94 if ((uint32_t)(prod
>> 32) < divisor
)
95 return (uint32_t)(prod
/ divisor
);
97 else if (multiplicand
== 0 || multiplier
== 0)
99 return 0; /* 0/0 = 0 : yaya */
101 /* else (> 0) / 0 = UINT32_MAX */
103 return UINT32_MAX
; /* Saturate */
109 /* Does the list have any members? */
110 bool list_is_empty(void **list
)
112 return *list
== NULL
;
115 /* Is the item inserted into a particular list? */
116 bool list_is_member(void **list
, void *item
)
118 return *rb
->find_array_ptr(list
, item
) != NULL
;
121 /* Removes an item from a list - returns true if item was found
122 * and thus removed. */
123 bool list_remove_item(void **list
, void *item
)
125 return rb
->remove_array_ptr(list
, item
) != -1;
128 /* Adds a list item, insert last, if not already present. */
129 void list_add_item(void **list
, void *item
)
131 void **item_p
= rb
->find_array_ptr(list
, item
);
136 /* Clears the entire list. */
137 void list_clear_all(void **list
)
139 while (*list
!= NULL
)
143 /* Enumerate all items in the array, passing each item in turn to the
144 * callback as well as the data value. The current item may be safely
145 * removed. Other changes during enumeration are undefined. The callback
146 * may return 'false' to stop the enumeration early. */
147 void list_enum_items(void **list
,
148 list_enum_callback_t callback
,
158 if (callback
!= NULL
&& !callback(item
, data
))
162 list
++; /* Item still there */
167 /** System events **/
168 static long mpeg_sysevent_id
;
170 void mpeg_sysevent_clear(void)
172 mpeg_sysevent_id
= 0;
175 void mpeg_sysevent_set(void)
177 /* Nonzero and won't invoke anything in default event handler */
178 mpeg_sysevent_id
= ACTION_STD_CANCEL
;
181 long mpeg_sysevent(void)
183 return mpeg_sysevent_id
;
186 int mpeg_sysevent_callback(int btn
, const struct menu_item_ex
*menu
)
190 case SYS_USB_CONNECTED
:
192 mpeg_sysevent_id
= btn
;
193 return ACTION_STD_CANCEL
;
200 void mpeg_sysevent_handle(void)
202 long id
= mpeg_sysevent();
204 rb
->default_event_handler(id
);
210 int mpeg_button_get(int timeout
)
214 mpeg_sysevent_clear();
215 button
= timeout
== TIMEOUT_BLOCK
? rb
->button_get(true) :
216 rb
->button_get_w_tmo(timeout
);
217 return mpeg_sysevent_callback(button
, NULL
);