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