ee7134de86624db46a14eab74693e7dbed56ee5d
[paraslash.git] / para.h
1 /*
2  * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file para.h global paraslash definitions */
20
21 #include "config.h"
22
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <sys/wait.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h> /* time(), localtime() */
29 #include <unistd.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdarg.h>
33 #include <ctype.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <sys/socket.h>
37 #include <sys/un.h> /* needed by create_pf_socket */
38 #include <string.h>
39 #include "gcc-compat.h"
40
41 /** used in various contexts */
42 #define MAXLINE 255
43
44
45 #define PARA_MIN(a,b) ((a) < (b) ? (a) : (b))
46 #define PARA_MAX(a,b) ((a) > (b) ? (a) : (b))
47 #define PARA_ABS(a) ((a) > 0 ? (a) : -(a))
48
49 /* Loglevels */
50 #define DEBUG 1
51 #define INFO  2
52 #define NOTICE 3
53 #define WARNING 4
54 #define ERROR 5
55 #define CRIT 6
56 #define EMERG 7
57
58
59 #define COMPILE_TIME_LOGLEVEL 0
60 #if DEBUG > COMPILE_TIME_LOGLEVEL
61 #define PARA_DEBUG_LOG(f,...) para_log(DEBUG, "%s: " f, __FUNCTION__, __VA_ARGS__)
62 #else
63 #define PARA_DEBUG_LOG(...) do {;} while (0)
64 #endif
65
66 #if INFO > COMPILE_TIME_LOGLEVEL
67 #define PARA_INFO_LOG(f,...) para_log(INFO, "%s: " f, __FUNCTION__, __VA_ARGS__)
68 #else
69 #define PARA_INFO_LOG(...) do {;} while (0)
70 #endif
71
72 #if NOTICE > COMPILE_TIME_LOGLEVEL
73 #define PARA_NOTICE_LOG(f,...) para_log(NOTICE, "%s: " f, __FUNCTION__, __VA_ARGS__)
74 #else
75 #define PARA_NOTICE_LOG(...) do {;} while (0)
76 #endif
77
78 #if WARNING > COMPILE_TIME_LOGLEVEL
79 #define PARA_WARNING_LOG(f,...) para_log(WARNING, "%s: " f, __FUNCTION__, __VA_ARGS__)
80 #else
81 #define PARA_WARNING_LOG(...) do {;} while (0)
82 #endif
83
84 #if ERROR > COMPILE_TIME_LOGLEVEL
85 #define PARA_ERROR_LOG(f,...) para_log(ERROR, "%s: " f, __FUNCTION__, __VA_ARGS__)
86 #else
87 #define PARA_ERROR_LOG(...) do {;} while (0)
88 #endif
89
90 #if CRIT > COMPILE_TIME_LOGLEVEL
91 #define PARA_CRIT_LOG(f,...) para_log(CRIT, "%s: " f, __FUNCTION__, __VA_ARGS__)
92 #else
93 #define PARA_CRIT_LOG(...) do {;} while (0)
94 #endif
95
96 #if EMERG > COMPILE_TIME_LOGLEVEL
97 #define PARA_EMERG_LOG(f,...) para_log(EMERG, "%s: " f, __FUNCTION__, __VA_ARGS__)
98 #else
99 #define PARA_EMERG_LOG(...)
100 #endif
101
102 /**
103  * define a standard log function that always writes to stderr
104  *
105  * \param loglevel_barier If the loglevel of the current message
106  * is less than that, the message is going to be ignored.
107  *
108  */
109 #define INIT_STDERR_LOGGING(loglevel_barrier) \
110         __printf_2_3 void para_log(int ll, const char* fmt,...) \
111         { \
112                 va_list argp; \
113                 if (ll < loglevel_barrier) \
114                         return; \
115                 va_start(argp, fmt); \
116                 vfprintf(stderr, fmt, argp); \
117                 va_end(argp); \
118         }
119
120
121 #define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION "\n" \
122         "Copyright (C) 2007 Andre Noll\n" \
123         "This is free software with ABSOLUTELY NO WARRANTY." \
124         " See COPYING for details.\n" \
125         "Written by Andre Noll.\n" \
126         "Report bugs to <maan@systemlinux.org>.\n"
127
128 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
129         if (_args_info_struct.version_given) { \
130                 printf("%s", VERSION_TEXT(_prefix)); \
131                 exit(EXIT_SUCCESS); \
132         }
133
134 #define AWAITING_DATA_MSG "\nAwaiting Data."
135 #define PROCEED_MSG "\nProceed.\n"
136 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
137 #define EOC_MSG "\nEnd of Command."
138 #define CHALLENGE_RESPONSE_MSG "challenge_response:"
139
140 /* gui_common */
141 int para_open_audiod_pipe(char *);
142 int read_audiod_pipe(int, void (*)(char *));
143
144 /* exec */
145 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
146
147 /* time */
148 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
149 long unsigned tv2ms(const struct timeval*);
150 void d2tv(double, struct timeval*);
151 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
152 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
153 void tv_divide(const unsigned long divisor, const struct timeval *tv,
154         struct timeval *result);
155 int tv_convex_combination(const long a, const struct timeval *tv1,
156                 const long b, const struct timeval *tv2,
157                 struct timeval *result);
158 void ms2tv(const long unsigned n, struct timeval *tv);
159
160 /* stat */
161 enum {
162         SI_STATUS_BAR,          SI_STATUS,              SI_NUM_PLAYED,
163         SI_MTIME,               SI_LENGTH_MIN,          SI_LENGTH_SEC,
164         SI_FILE_SIZE,           SI_STATUS_FLAGS,        SI_FORMAT,
165         SI_SCORE,               SI_AUDIO_INFO1,         SI_AUDIO_INFO2,
166         SI_AUDIO_INFO3,         SI_DBINFO1,             SI_DBINFO2,
167         SI_DBINFO3,             SI_DECODER_FLAGS,       SI_AUDIOD_STATUS,
168         SI_PLAY_TIME,           SI_UPTIME,              SI_OFFSET,
169         SI_LENGTH,              SI_STREAM_START,        SI_CURRENT_TIME,
170         SI_AUDIOD_UPTIME,       SI_SELECTOR,            NUM_STAT_ITEMS
171 };
172
173 int stat_item_valid(const char *item);
174 int stat_line_valid(const char *);
175 void stat_client_write(char *msg, int itemnum);
176 int stat_client_add(int fd, long unsigned mask);
177 unsigned for_each_line(char *, int, void (*)(char *));
178 #define FOR_EACH_STAT_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
179
180 struct stat_item_data {
181         const char *prefix, *postfix;
182         unsigned x, y, len;
183         int fg, bg, align;
184 };
185
186 /* gui_theme */
187 struct gui_theme {
188         const char *name;
189         const char *author;
190         int sb_fg, sb_bg;
191         int cmd_fg, cmd_bg;
192         int output_fg, output_bg;
193         int msg_fg, msg_bg;
194         int err_msg_fg, err_msg_bg;
195         int welcome_fg, welcome_bg;
196         int sep_fg, sep_bg;
197         const char *sep_str;
198         int default_fg, default_bg;
199
200         int top_lines_default, top_lines_min;
201         int lines_min, cols_min;
202         struct stat_item_data data[NUM_STAT_ITEMS];
203 };
204
205 void init_theme(int i, struct gui_theme *);
206 void next_theme(struct gui_theme *);
207 void prev_theme(struct gui_theme *);
208 #define LEFT 1
209 #define RIGHT 2
210 #define CENTER 3
211
212
213 __printf_2_3 void para_log(int, const char*, ...);
214
215 /* taken from printf man page */
216 #define PARA_VSPRINTF(fmt, p) \
217 { \
218         int n, size = 100; \
219         p = para_malloc(size); \
220         while (1) { \
221                 va_list ap; \
222                 /* Try to print in the allocated space. */ \
223                 va_start(ap, fmt); \
224                 n = vsnprintf(p, size, fmt, ap); \
225                 va_end(ap); \
226                 /* If that worked, return the string. */ \
227                 if (n > -1 && n < size) \
228                         break; \
229                 /* Else try again with more space. */ \
230                 if (n > -1) /* glibc 2.1 */ \
231                         size = n + 1; /* precisely what is needed */ \
232                 else /* glibc 2.0 */ \
233                         size *= 2; /* twice the old size */ \
234                 p = para_realloc(p, size); \
235         } \
236 }