wmshutdown: Destroy dialog window before shutting down. This is especially useful...
[dockapps.git] / wmmp3 / main.c
blobec3bdaccc1b59e2554535abe295b60a6f8b9f827
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 "main.h"
16 #define B_STOP 0
17 #define B_PLAY 1
18 #define B_BACK 2
19 #define B_NEXT 3
20 #define B_PREV_DIR 4
21 #define B_RAND 5
22 #define B_NEXT_DIR 6
23 #define B_REPEAT 7
24 #define B_TITLE 8
26 void loadconfig();
27 void show_help();
28 void show_version();
29 int check_options(int argc, char *argv[]);
30 int handle_button_press(int x, int y);
31 void handle_button_release(int i);
33 struct coord {
34 int x;
35 int y;
36 int w;
37 int h;
40 /* from Steven Jorgensen */
41 void stripspace(char *s) {
42 char *t;
43 t = s + strlen(s) - 1;
44 while (t > s && isspace(*t)) {
45 t--;
47 t++;
48 *t = '\0';
51 int handle_button_press(int x, int y) {
52 int i;
54 i = CheckMouseRegion(x, y);
55 switch (i) {
56 case B_STOP:
57 button_down(i);
58 button_up(B_PLAY); /* raise play */
59 button_up(B_PREV_DIR); /* reactivate directory up/down */
60 button_up(B_NEXT_DIR);
61 stop();
62 break;
63 case B_PLAY:
64 button_down(i);
65 button_gray(B_PREV_DIR); /* gray out directory up/down */
66 button_gray(B_NEXT_DIR);
67 user_play();
68 break;
69 case B_BACK:
70 button_down(i);
71 back();
72 break;
73 case B_NEXT:
74 button_down(i);
75 next();
76 break;
77 case B_PREV_DIR:
78 dir_up(i);
79 break;
80 case B_RAND:
81 random_toggle(i);
82 break;
83 case B_NEXT_DIR:
84 dir_down(i);
85 break;
86 case B_REPEAT:
87 repeat_toggle(i);
88 break;
89 case B_TITLE:
90 turn_on_scroll();
91 break;
92 default:
93 fprintf(stderr, "unknown button pressed\n");
95 RedrawWindow();
97 return(i);
100 void handle_button_release(int i)
102 /* play stays down, don't mess with toggles */
103 /* ignore song title press too */
104 if (((i != B_PLAY) && (i != B_RAND)) &&
105 ((i != B_REPEAT) && (i != B_TITLE))) {
106 if (!is_playing()) {
107 button_up(i);
108 RedrawWindow();
109 } else {
110 /* don't undo gray of dir up/down */
111 if (((i == B_STOP) || (i == B_BACK)) || (i == B_NEXT)) {
112 button_up(i);
113 RedrawWindow();
119 void loadconfig()
121 struct passwd *pw;
122 char *config_filename;
123 FILE *fp;
125 errno = 0;
127 /* set defualts in case anything fails */
128 set_mpg123("/usr/local/bin/mpg123");
129 set_mp3ext(".mp3");
130 set_playlistext(".m3u");
132 pw = getpwuid(getuid());
133 /* don't forget about the string terminator... */
134 config_filename = (char *) malloc(sizeof(char) *
135 (strlen(pw->pw_dir) + 8));
136 sprintf(config_filename, "%s/.wmmp3", pw->pw_dir);
139 fp = fopen(config_filename, "r");
140 if (fp != NULL) {
141 char line[256];
142 char variable[256];
143 char value[256];
145 fgets(line, 256, fp);
146 while (!feof(fp)) {
147 if ((line[0] != '#') && (line[0] != '\n')) {
148 if (sscanf(line, " %s = %[^\n]", variable, value) < 2) {
149 fprintf(stderr, "Malformed line in config file: %s\n", line);
150 } else {
151 if (strcmp(variable, "mpg123") == 0) {
152 stripspace(value);
153 set_mpg123(value);
154 } else if (strcmp(variable, "mp3dir") == 0) {
155 stripspace(value);
156 add_mp3dir(value);
157 } else if (strcmp(variable, "mp3dirname") == 0) {
158 stripspace(value);
159 add_mp3dirname(value);
160 } else if (strcmp(variable, "mp3ext") == 0) {
161 stripspace(value);
162 set_mp3ext(value);
163 } else if (strcmp(variable, "playlistext") == 0) {
164 stripspace(value);
165 set_playlistext(value);
166 } else if (strcmp(variable, "alwaysscroll") == 0) {
167 stripspace(value);
168 set_alwaysscroll(value);
169 } else {
170 fprintf(stderr, "Unrecognized variable in config file: %s\n",
171 variable);
175 fgets(line, 256, fp);
179 fclose(fp);
180 } else {
181 fprintf(stderr, "open of %s failed: %s\n",
182 config_filename,
183 strerror(errno));
186 free(config_filename);
189 void show_help()
191 printf("wmmp3 -- by Patrick Crosby -- Version %s\n", VERSION);
192 printf("At this moment, there are no command line options that do\n");
193 printf("anything special:\n");
194 printf("\t-h,--help: print this message\n");
195 printf("\t-v,--version: print version info\n");
196 printf("All options are set in ~/.wmmp3. See sample.wmmp3 in the\n");
197 printf("distribution or http://dotfiles.com/software/ for more info.\n");
200 void show_version()
202 printf("wmmp3 -- by Patrick Crosby -- Version %s\n", VERSION);
205 int check_options(int argc, char *argv[])
207 int option_entered = 0;
208 int i;
211 for (i = 1; i < argc; i++) {
212 if (streq(argv[i], "-h")) {
213 option_entered = 1;
214 show_help();
216 else if (streq(argv[i], "--help")) {
217 option_entered = 1;
218 show_help();
220 else if (streq(argv[i], "-v")) {
221 option_entered = 1;
222 show_version();
224 else if (streq(argv[i], "--version")) {
225 option_entered = 1;
226 show_version();
230 return option_entered;
233 void main(int argc, char *argv[])
235 struct coord pos[] = {
236 {35, 34, 12, 11}, /* stop */
237 {46, 34, 12, 11}, /* play */
238 {35, 45, 12, 11}, /* back */
239 {46, 45, 12, 11}, /* next */
240 {6, 34, 12, 11}, /* prev_dir */
241 {17, 34, 12, 11}, /* random */
242 {6, 45, 12, 11}, /* next_dir */
243 {17, 45, 12, 11}, /* repeat */
244 {5, 18, 54, 12} /* song title */
247 char mask_bits[64 * 64];
248 int mask_width = 64;
249 int mask_height = 64;
250 XEvent Event;
251 int i;
252 int btn_num;
254 if (check_options(argc, argv)) {
255 exit(1);
258 loadconfig();
260 /* set up the display area for wmmp3 */
261 createXBMfromXPM(mask_bits, wmmp3_xpm, mask_width, mask_height);
262 openXwindow(argc, argv, wmmp3_xpm, mask_bits, mask_width, mask_height);
264 font_init();
265 draw_string("wmmp3", 5, 5);
268 i = 0;
269 for (i = 0; i < 9; i++)
270 AddMouseRegion(i, pos[i].x, pos[i].y,
271 pos[i].x + pos[i].w,
272 pos[i].y + pos[i].h);
274 /* event loop */
275 while (1) {
276 while (XPending(display)) {
277 XNextEvent(display, &Event);
278 switch (Event.type) {
279 case Expose:
280 RedrawWindow();
281 break;
282 case ClientMessage:
283 break;
284 case DestroyNotify:
285 XCloseDisplay(display);
286 exit(0);
287 case ButtonPress:
288 btn_num = handle_button_press(Event.xbutton.x,
289 Event.xbutton.y);
290 break;
291 case ButtonRelease:
292 handle_button_release(btn_num);
293 break;
297 usleep(50000);
299 exit(0);