1 /* Copyright (C) 1997 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file para.h global paraslash definitions */
5 #include "config.h"
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <sys/wait.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <time.h> /* time(), localtime() */
13 #include <unistd.h>
14 #include <errno.h>
15 #include <limits.h>
16 #include <stdarg.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <assert.h>
20 #include <stdbool.h>
21 #include <inttypes.h>
22 #include <sys/uio.h>
23 #include "gcc-compat.h"
25 /** used in various contexts */
26 #define MAXLINE 255
28 /** Compute the minimum of \a x and \a y. */
29 #define PARA_MIN(x, y) ({ \
30 typeof(x) _min1 = (x); \
31 typeof(y) _min2 = (y); \
32 (void) (&_min1 == &_min2); \
33 _min1 < _min2 ? _min1 : _min2; })
35 /** Compute the maximum of \a x and \a y. */
36 #define PARA_MAX(x, y) ({ \
37 typeof(x) _max1 = (x); \
38 typeof(y) _max2 = (y); \
39 (void) (&_max1 == &_max2); \
40 _max1 < _max2 ? _max2 : _max1; })
42 /** Compute the absolute value of \a x. */
43 #define PARA_ABS(x) ({ \
44 typeof(x) _x = (x); \
45 _x > 0? _x : -_x; })
48 extern __printf_2_3 void (*para_log)(int, const char*, ...);
49 /**
50 * Define a standard log function that always writes to stderr.
51 *
52 * \param funcname The name of the function to be defined.
53 *
54 * \param loglevel_barrier If the loglevel of the current message
55 * is less than that, the message is going to be ignored.
56 *
57 */
58 #define DEFINE_STDERR_LOGGER(funcname, loglevel_barrier) \
59 static __printf_2_3 void funcname(int ll, const char* fmt,...) \
60 { \
61 va_list argp; \
62 if (ll < loglevel_barrier) \
63 return; \
64 va_start(argp, fmt); \
65 vfprintf(stderr, fmt, argp); \
66 va_end(argp); \
67 }
68 /**
69 * Define the standard log function and activate it.
70 *
71 * \param loglevel_barrier See \ref DEFINE_STDERR_LOGGER.
72 */
73 #define INIT_STDERR_LOGGING(loglevel_barrier) \
74 DEFINE_STDERR_LOGGER(stderr_log, loglevel_barrier); \
75 __printf_2_3 void (*para_log)(int, const char*, ...) = stderr_log;
77 /** Sent by para_client to initiate the authentication procedure. */
78 #define AUTH_REQUEST_MSG "auth rsa "
80 /* exec */
81 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
83 /* time */
84 int tv_diff(const struct timeval *b, const struct timeval *a,
85 struct timeval *diff);
86 long unsigned tv2ms(const struct timeval *tv);
87 void tv_add(const struct timeval *a, const struct timeval *b,
88 struct timeval *sum);
89 void tv_scale(const unsigned long mult, const struct timeval *tv,
90 struct timeval *result);
91 void tv_divide(const unsigned long divisor, const struct timeval *tv,
92 struct timeval *result);
93 int tv_convex_combination(const long a, const struct timeval *tv1,
94 const long b, const struct timeval *tv2,
95 struct timeval *result);
96 void ms2tv(long unsigned n, struct timeval *tv);
97 void compute_chunk_time(long unsigned chunk_num,
98 struct timeval *chunk_tv, struct timeval *stream_start,
99 struct timeval *result);
100 struct timeval *clock_get_realtime(struct timeval *tv);
102 /**
103 * Return a random non-negative integer in an interval.
104 *
105 * \param max Determines maximal possible return value.
106 *
107 * \return An integer between zero and \p max - 1, inclusively.
108 */
109 _static_inline_ long int para_random(unsigned max)
110 {
111 return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
112 }
114 /**
115 * Simple sanity check for I/O vectors.
116 *
117 * \param iov Pointer to the I/O vector to check.
118 *
119 * \return True if \a iov points to a non-empty buffer.
120 */
121 _static_inline_ bool iov_valid(const struct iovec *iov)
122 {
123 return iov && iov->iov_len > 0 && iov->iov_base;
124 }
126 /** Round up x to next multiple of y. */
127 #define ROUND_UP(x, y) ({ \
128 const typeof(y) _divisor = y; \
129 ((x) + _divisor - 1) / _divisor * _divisor; })
131 /** Round down x to multiple of y. */
132 #define ROUND_DOWN(x, y) ({ \
133 const typeof(y) _divisor = y; \
134 (x) / _divisor * _divisor; })
136 /** Divide and round up to next integer. */
137 #define DIV_ROUND_UP(x, y) ({ \
138 typeof(y) _divisor = y; \
139 ((x) + _divisor - 1) / _divisor; })
141 /**
142 * Assert a build-time dependency, as an expression.
143 *
144 * \param cond The compile-time condition which must be true.
145 *
146 * Compilation will fail if the condition isn't true, or can't be evaluated by
147 * the compiler. This can be used in an expression: its value is "0".
148 *
149 * Taken from ccan.
150 */
151 #define EXPR_BUILD_ASSERT(cond) (sizeof(char [1 - 2 * !(cond)]) - 1)
153 /** &a[0] degrades to a pointer: a different type from an array */
154 #define _array_size_chk(arr) EXPR_BUILD_ASSERT(\
155 !__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0])))
156 /** Get the size of an array */
157 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
159 /**
160 * Wrapper for isspace.
161 * NetBSD needs this.
162 */
163 /*
164 * The values should be cast to an unsigned char first, then to int.
165 * Why? Because the isdigit (as do all other is/to functions/macros)
166 * expect a number from 0 upto and including 255 as their (int) argument.
167 * Because char is signed on most systems, casting it to int immediately
168 * gives the functions an argument between -128 and 127 (inclusive),
169 * which they will use as an array index, and which will thus fail
170 * horribly for characters which have their most significant bit set.
171 */
172 #define para_isspace(c) isspace((int)(unsigned char)(c))
174 /** Data that indicates an eof-condition for a fec-encoded stream. */
175 #define FEC_EOF_PACKET "\xec\x0d\xcc\xfe\0\0\0\0" \
176 "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0"
177 /** The number of bytes of the \a FEC_EOF_PACKET. */
178 #define FEC_EOF_PACKET_LEN 32
180 /** Used to avoid a shortcoming in vim's syntax highlighting. */
181 #define EMBRACE(...) { __VA_ARGS__}
183 /** A nice cup of STFU for Mr gcc. */
184 #define do_nothing do {/* nothing */} while (0)
186 /**
187 * The sample formats supported by paraslash.
188 *
189 * It may be determined by one of the following sources:
190 *
191 * 1. The decoding filter (para_audiod only). In this case, it is always
192 * \p SF_S16_LE which is the canonical format used within decoders.
193 *
194 * 2. The wav header (para_write only).
195 *
196 * 3. The --sample-format option of para_write.
197 */
198 #define SAMPLE_FORMATS \
199 SAMPLE_FORMAT(SF_S8, "8 bit signed"), \
200 SAMPLE_FORMAT(SF_U8, "8 bit unsigned"), \
201 SAMPLE_FORMAT(SF_S16_LE, "16 bit signed, little endian"), \
202 SAMPLE_FORMAT(SF_S16_BE, "16 bit signed, big endian"), \
203 SAMPLE_FORMAT(SF_U16_LE, "16 bit unsigned, little endian"), \
204 SAMPLE_FORMAT(SF_U16_BE, "16 bit unsigned, big endian"), \
206 /** \cond sample_format */
207 #define SAMPLE_FORMAT(a, b) a
208 enum sample_format {SAMPLE_FORMATS};
209 #undef SAMPLE_FORMAT
210 #define SAMPLE_FORMAT(a, b) b
211 /** \endcond sample_format */
213 /** Debug, Info, etc. */
214 enum loglevels {LOGLEVELS, NUM_LOGLEVELS};
216 #define PARA_DEBUG_LOG(f,...) para_log(LL_DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
217 #define PARA_INFO_LOG(f,...) para_log(LL_INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
218 #define PARA_NOTICE_LOG(f,...) para_log(LL_NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
219 #define PARA_WARNING_LOG(f,...) para_log(LL_WARNING, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
220 #define PARA_ERROR_LOG(f,...) para_log(LL_ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
221 #define PARA_CRIT_LOG(f,...) para_log(LL_CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
222 #define PARA_EMERG_LOG(f,...) para_log(LL_EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
224 #define STATUS_ITEMS \
225 STATUS_ITEM(basename) \
226 STATUS_ITEM(status) \
227 STATUS_ITEM(num_played) \
228 STATUS_ITEM(mtime) \
229 STATUS_ITEM(bitrate) \
230 STATUS_ITEM(frequency) \
231 STATUS_ITEM(file_size) \
232 STATUS_ITEM(status_flags) \
233 STATUS_ITEM(format) \
234 STATUS_ITEM(score) \
235 STATUS_ITEM(techinfo) \
236 STATUS_ITEM(afs_mode) \
237 STATUS_ITEM(attributes_txt) \
238 STATUS_ITEM(decoder_flags) \
239 STATUS_ITEM(audiod_status) \
240 STATUS_ITEM(play_time) \
241 STATUS_ITEM(attributes_bitmap) \
242 STATUS_ITEM(offset) \
243 STATUS_ITEM(seconds_total) \
244 STATUS_ITEM(stream_start) \
245 STATUS_ITEM(current_time) \
246 STATUS_ITEM(audiod_uptime) \
247 STATUS_ITEM(image_id) \
248 STATUS_ITEM(lyrics_id) \
249 STATUS_ITEM(duration) \
250 STATUS_ITEM(directory) \
251 STATUS_ITEM(lyrics_name) \
252 STATUS_ITEM(image_name) \
253 STATUS_ITEM(path) \
254 STATUS_ITEM(hash) \
255 STATUS_ITEM(channels) \
256 STATUS_ITEM(last_played) \
257 STATUS_ITEM(num_chunks) \
258 STATUS_ITEM(chunk_time) \
259 STATUS_ITEM(amplification) \
260 STATUS_ITEM(artist) \
261 STATUS_ITEM(title) \
262 STATUS_ITEM(year) \
263 STATUS_ITEM(album) \
264 STATUS_ITEM(comment) \
265 STATUS_ITEM(max_chunk_size) \
267 #define STATUS_ITEM(_name) SI_ ##_name,
268 enum status_items {STATUS_ITEMS NUM_STAT_ITEMS};
269 #undef STATUS_ITEM
270 #define STATUS_ITEM(_name) #_name,
272 extern const char *status_item_list[];
273 /** Loop over each status item. */
274 #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
275 int for_each_stat_item(char *item_buf, size_t num_bytes,
276 int (*item_handler)(int, char *));