wmclockmon: update `getbool` not to modify its argument
[dockapps.git] / wmmp3 / main.c
blobbeca147ff38704818dbaae6892d046bf3b72dcc4
1 /*
2 * wmmp3
3 * Copyright (c)1999 Patrick Crosby <xb@dotfiles.com>.
4 * This software covered by the GPL. See COPYING file for details.
6 * main.c
8 * This is the main body of the application. Handles all initialization
9 * plus the X event loop.
11 * $Id: main.c,v 1.11 1999/10/08 22:21:32 pcrosby Exp $
14 #include <ctype.h>
15 #include "main.h"
17 #define B_STOP 0
18 #define B_PLAY 1
19 #define B_BACK 2
20 #define B_NEXT 3
21 #define B_PREV_DIR 4
22 #define B_RAND 5
23 #define B_NEXT_DIR 6
24 #define B_REPEAT 7
25 #define B_TITLE 8
27 void loadconfig();
28 void show_help();
29 void show_version();
30 int check_options(int argc, char *argv[]);
31 int handle_button_press(int x, int y);
32 void handle_button_release(int i);
34 struct coord {
35 int x;
36 int y;
37 int w;
38 int h;
41 /* from Steven Jorgensen */
42 void stripspace(char *s) {
43 char *t;
44 t = s + strlen(s) - 1;
45 while (t > s && isspace(*t)) {
46 t--;
48 t++;
49 *t = '\0';
52 int handle_button_press(int x, int y) {
53 int i;
55 i = CheckMouseRegion(x, y);
56 switch (i) {
57 case B_STOP:
58 button_down(i);
59 button_up(B_PLAY); /* raise play */
60 button_up(B_PREV_DIR); /* reactivate directory up/down */
61 button_up(B_NEXT_DIR);
62 stop();
63 break;
64 case B_PLAY:
65 button_down(i);
66 button_gray(B_PREV_DIR); /* gray out directory up/down */
67 button_gray(B_NEXT_DIR);
68 user_play();
69 break;
70 case B_BACK:
71 button_down(i);
72 back();
73 break;
74 case B_NEXT:
75 button_down(i);
76 next();
77 break;
78 case B_PREV_DIR:
79 dir_up(i);
80 break;
81 case B_RAND:
82 random_toggle(i);
83 break;
84 case B_NEXT_DIR:
85 dir_down(i);
86 break;
87 case B_REPEAT:
88 repeat_toggle(i);
89 break;
90 case B_TITLE:
91 turn_on_scroll();
92 break;
93 default:
94 fprintf(stderr, "unknown button pressed\n");
96 RedrawWindow();
98 return(i);
101 void handle_button_release(int i)
103 /* play stays down, don't mess with toggles */
104 /* ignore song title press too */
105 if (((i != B_PLAY) && (i != B_RAND)) &&
106 ((i != B_REPEAT) && (i != B_TITLE))) {
107 if (!is_playing()) {
108 button_up(i);
109 RedrawWindow();
110 } else {
111 /* don't undo gray of dir up/down */
112 if (((i == B_STOP) || (i == B_BACK)) || (i == B_NEXT)) {
113 button_up(i);
114 RedrawWindow();
120 void loadconfig()
122 struct passwd *pw;
123 char *config_filename;
124 FILE *fp;
126 errno = 0;
128 /* set defualts in case anything fails */
129 set_mpg123("/usr/local/bin/mpg123");
130 set_mp3ext(".mp3");
131 set_playlistext(".m3u");
133 pw = getpwuid(getuid());
134 /* don't forget about the string terminator... */
135 config_filename = (char *) malloc(sizeof(char) *
136 (strlen(pw->pw_dir) + 8));
137 sprintf(config_filename, "%s/.wmmp3", pw->pw_dir);
140 fp = fopen(config_filename, "r");
141 if (fp != NULL) {
142 char line[256];
143 char variable[256];
144 char value[256];
146 fgets(line, 256, fp);
147 while (!feof(fp)) {
148 if ((line[0] != '#') && (line[0] != '\n')) {
149 if (sscanf(line, " %s = %[^\n]", variable, value) < 2) {
150 fprintf(stderr, "Malformed line in config file: %s\n", line);
151 } else {
152 if (strcmp(variable, "mpg123") == 0) {
153 stripspace(value);
154 set_mpg123(value);
155 } else if (strcmp(variable, "mp3dir") == 0) {
156 stripspace(value);
157 add_mp3dir(value);
158 } else if (strcmp(variable, "mp3dirname") == 0) {
159 stripspace(value);
160 add_mp3dirname(value);
161 } else if (strcmp(variable, "mp3ext") == 0) {
162 stripspace(value);
163 set_mp3ext(value);
164 } else if (strcmp(variable, "playlistext") == 0) {
165 stripspace(value);
166 set_playlistext(value);
167 } else if (strcmp(variable, "alwaysscroll") == 0) {
168 stripspace(value);
169 set_alwaysscroll(value);
170 } else {
171 fprintf(stderr, "Unrecognized variable in config file: %s\n",
172 variable);
176 fgets(line, 256, fp);
180 fclose(fp);
181 } else {
182 fprintf(stderr, "open of %s failed: %s\n",
183 config_filename,
184 strerror(errno));
187 free(config_filename);
190 void show_help()
192 printf("wmmp3 -- by Patrick Crosby -- Version %s\n", VERSION);
193 printf("At this moment, there are no command line options that do\n");
194 printf("anything special:\n");
195 printf("\t-h,--help: print this message\n");
196 printf("\t-v,--version: print version info\n");
197 printf("All options are set in ~/.wmmp3. See sample.wmmp3 in the\n");
198 printf("distribution or http://dotfiles.com/software/ for more info.\n");
201 void show_version()
203 printf("wmmp3 -- by Patrick Crosby -- Version %s\n", VERSION);
206 int check_options(int argc, char *argv[])
208 int option_entered = 0;
209 int i;
212 for (i = 1; i < argc; i++) {
213 if (streq(argv[i], "-h")) {
214 option_entered = 1;
215 show_help();
217 else if (streq(argv[i], "--help")) {
218 option_entered = 1;
219 show_help();
221 else if (streq(argv[i], "-v")) {
222 option_entered = 1;
223 show_version();
225 else if (streq(argv[i], "--version")) {
226 option_entered = 1;
227 show_version();
231 return option_entered;
234 void main(int argc, char *argv[])
236 struct coord pos[] = {
237 {35, 34, 12, 11}, /* stop */
238 {46, 34, 12, 11}, /* play */
239 {35, 45, 12, 11}, /* back */
240 {46, 45, 12, 11}, /* next */
241 {6, 34, 12, 11}, /* prev_dir */
242 {17, 34, 12, 11}, /* random */
243 {6, 45, 12, 11}, /* next_dir */
244 {17, 45, 12, 11}, /* repeat */
245 {5, 18, 54, 12} /* song title */
248 char mask_bits[64 * 64];
249 int mask_width = 64;
250 int mask_height = 64;
251 XEvent Event;
252 int i;
253 int btn_num;
255 if (check_options(argc, argv)) {
256 exit(1);
259 loadconfig();
261 /* set up the display area for wmmp3 */
262 createXBMfromXPM(mask_bits, wmmp3_xpm, mask_width, mask_height);
263 openXwindow(argc, argv, wmmp3_xpm, mask_bits, mask_width, mask_height);
265 font_init();
266 draw_string("wmmp3", 5, 5);
269 i = 0;
270 for (i = 0; i < 9; i++)
271 AddMouseRegion(i, pos[i].x, pos[i].y,
272 pos[i].x + pos[i].w,
273 pos[i].y + pos[i].h);
275 /* event loop */
276 while (1) {
277 while (XPending(display)) {
278 XNextEvent(display, &Event);
279 switch (Event.type) {
280 case Expose:
281 RedrawWindow();
282 break;
283 case ClientMessage:
284 break;
285 case DestroyNotify:
286 XCloseDisplay(display);
287 exit(0);
288 case ButtonPress:
289 btn_num = handle_button_press(Event.xbutton.x,
290 Event.xbutton.y);
291 break;
292 case ButtonRelease:
293 handle_button_release(btn_num);
294 break;
298 usleep(50000);
300 exit(0);