e1b0d73f739ea5684cb61656a0553be7b8343a2c
[paraslash.git] / para.h
1 /*
2 * Copyright (C) 1997-2006 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 /* some internal constants */
42 #define STRINGSIZE 4096
43 #define MAXLINE 255
44
45
46 #define PARA_MIN(a,b) ((a) < (b) ? (a) : (b))
47 #define PARA_MAX(a,b) ((a) > (b) ? (a) : (b))
48 #define PARA_ABS(a) ((a) > 0 ? (a) : -(a))
49
50 /* Loglevels */
51 #define DEBUG 1
52 #define INFO 2
53 #define NOTICE 3
54 #define WARNING 4
55 #define ERROR 5
56 #define CRIT 6
57 #define EMERG 7
58
59
60 #define COMPILE_TIME_LOGLEVEL 0
61 #if DEBUG > COMPILE_TIME_LOGLEVEL
62 #define PARA_DEBUG_LOG(f,...) para_log(DEBUG, "%s: " f, __FUNCTION__, __VA_ARGS__)
63 #else
64 #define PARA_DEBUG_LOG(...) do {;} while (0)
65 #endif
66
67 #if INFO > COMPILE_TIME_LOGLEVEL
68 #define PARA_INFO_LOG(f,...) para_log(INFO, "%s: " f, __FUNCTION__, __VA_ARGS__)
69 #else
70 #define PARA_INFO_LOG(...) do {;} while (0)
71 #endif
72
73 #if NOTICE > COMPILE_TIME_LOGLEVEL
74 #define PARA_NOTICE_LOG(f,...) para_log(NOTICE, "%s: " f, __FUNCTION__, __VA_ARGS__)
75 #else
76 #define PARA_NOTICE_LOG(...) do {;} while (0)
77 #endif
78
79 #if WARNING > COMPILE_TIME_LOGLEVEL
80 #define PARA_WARNING_LOG(f,...) para_log(WARNING, "%s: " f, __FUNCTION__, __VA_ARGS__)
81 #else
82 #define PARA_WARNING_LOG(...) do {;} while (0)
83 #endif
84
85 #if ERROR > COMPILE_TIME_LOGLEVEL
86 #define PARA_ERROR_LOG(f,...) para_log(ERROR, "%s: " f, __FUNCTION__, __VA_ARGS__)
87 #else
88 #define PARA_ERROR_LOG(...) do {;} while (0)
89 #endif
90
91 #if CRIT > COMPILE_TIME_LOGLEVEL
92 #define PARA_CRIT_LOG(f,...) para_log(CRIT, "%s: " f, __FUNCTION__, __VA_ARGS__)
93 #else
94 #define PARA_CRIT_LOG(...) do {;} while (0)
95 #endif
96
97 #if EMERG > COMPILE_TIME_LOGLEVEL
98 #define PARA_EMERG_LOG(f,...) para_log(EMERG, "%s: " f, __FUNCTION__, __VA_ARGS__)
99 #else
100 #define PARA_EMERG_LOG(...)
101 #endif
102
103 #define COPYRIGHT "Copyright (c) 1997-2006 by Andre Noll"
104
105 #define LICENSE "This is free software with ABSOLUTELY NO WARRANTY. " \
106 "See COPYING for details."
107
108 #define AWAITING_DATA_MSG "\nAwaiting Data."
109 #define PROCEED_MSG "\nProceed.\n"
110 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
111 #define EOC_MSG "\nEnd of Command."
112 #define CHALLENGE_RESPONSE_MSG "challenge_response:"
113
114 /* gui_common */
115 int para_open_audiod_pipe(char *);
116 int read_audiod_pipe(int, void (*)(char *));
117
118 /* exec */
119 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
120
121 /* signal */
122 int para_signal_init(void);
123 int para_install_sighandler(int);
124 void para_reap_children(void);
125 pid_t para_reap_child(void);
126 int para_next_signal(void);
127
128 /* time */
129 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
130 long unsigned tv2ms(const struct timeval*);
131 void d2tv(double, struct timeval*);
132 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
133 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
134 void tv_divide(const unsigned long div, const struct timeval *tv,
135 struct timeval *result);
136 int tv_convex_combination(const long a, const struct timeval *tv1,
137 const long b, const struct timeval *tv2,
138 struct timeval *result);
139 void ms2tv(const long unsigned n, struct timeval *tv);
140
141 /* stat */
142 enum {
143 SI_STATUS_BAR, SI_STATUS, SI_NUM_PLAYED,
144 SI_MTIME, SI_LENGTH_MIN, SI_LENGTH_SEC,
145 SI_FILE_SIZE, SI_STATUS_FLAGS, SI_FORMAT,
146 SI_SCORE, SI_AUDIO_INFO1, SI_AUDIO_INFO2,
147 SI_AUDIO_INFO3, SI_DBINFO1, SI_DBINFO2,
148 SI_DBINFO3, SI_DECODER_FLAGS, SI_AUDIOD_STATUS,
149 SI_PLAY_TIME, SI_UPTIME, SI_OFFSET,
150 SI_LENGTH, SI_STREAM_START, SI_CURRENT_TIME,
151 SI_AUDIOD_UPTIME, SI_SELECTOR, NUM_STAT_ITEMS
152 };
153
154 int stat_item_valid(const char *item);
155 int stat_line_valid(const char *);
156 void stat_client_write(char *msg, int itemnum);
157 int stat_client_add(int fd, long unsigned mask);
158 void dump_empty_status(void);
159 unsigned for_each_line(char *, int, void (*)(char *));
160
161 struct stat_item_data {
162 const char *prefix, *postfix;
163 unsigned x, y, len;
164 int fg, bg, align;
165 };
166
167 /* gui_theme */
168 struct gui_theme {
169 const char *name;
170 const char *author;
171 int sb_fg, sb_bg;
172 int cmd_fg, cmd_bg;
173 int output_fg, output_bg;
174 int msg_fg, msg_bg;
175 int err_msg_fg, err_msg_bg;
176 int welcome_fg, welcome_bg;
177 int sep_fg, sep_bg;
178 const char *sep_str;
179 int default_fg, default_bg;
180
181 int top_lines_default, top_lines_min;
182 int lines_min, cols_min;
183 struct stat_item_data data[NUM_STAT_ITEMS];
184 };
185
186 void init_theme(int i, struct gui_theme *);
187 void next_theme(struct gui_theme *);
188 void prev_theme(struct gui_theme *);
189 #define LEFT 1
190 #define RIGHT 2
191 #define CENTER 3
192
193
194 __printf_2_3 void para_log(int, const char*, ...);
195
196 /* taken from printf man page */
197 #define PARA_VSPRINTF(fmt, p) \
198 { \
199 int n, size = 100; \
200 p = para_malloc(size); \
201 while (1) { \
202 va_list ap; \
203 /* Try to print in the allocated space. */ \
204 va_start(ap, fmt); \
205 n = vsnprintf(p, size, fmt, ap); \
206 va_end(ap); \
207 /* If that worked, return the string. */ \
208 if (n > -1 && n < size) \
209 break; \
210 /* Else try again with more space. */ \
211 if (n > -1) /* glibc 2.1 */ \
212 size = n + 1; /* precisely what is needed */ \
213 else /* glibc 2.0 */ \
214 size *= 2; /* twice the old size */ \
215 p = para_realloc(p, size); \
216 } \
217 }