Debian Codegraph @

contrib/rplaytool-1.1/misc.c

raw symbols SCIP
1 /* $Id: misc.c,v 1.2 1998/08/13 06:13:19 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1995 Mark Boyns <[email protected]>
5 *
6 * This file is part of rplaytool.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "misc.h"
24 #include <sys/stat.h>
25 #include <xview/panel.h>
26 #include <xview/notice.h>
27 #include <xview/scrollbar.h>
28 #ifdef __STDC__
29 #include <stdarg.h>
30 #else
31 #include <varargs.h>
32 #endif
33 #include "rplaytool_ui.h"
34 #include "rptp.h"
35
36 #undef MIN
37 #undef MAX
38
39 #define MIN(a,b) ((a)<(b)?(a):(b))
40 #define MAX(a,b) ((a)>(b)?(a):(b))
41
42 /* Windows */
43 extern rplaytool_w_objects *Rplaytool_w;
44 extern rplaytool_s_objects *Rplaytool_s;
45 extern rplaytool_f_objects *Rplaytool_f;
46
47
48 PLAYLIST playlist[NUMBER_OF_PLAYLISTS];
49 int curr_playlist = 0;
50 int client_id = 0;
51 extern int initial_delay;
52
53 static unsigned short directory_bits[] =
54 {
55 #include "icons/directory.icon"
56 };
57 static unsigned short sound_bits[] =
58 {
59 #include "icons/sound.icon"
60 };
61 static unsigned short play_bits[] =
62 {
63 #include "icons/play.icon"
64 };
65 static unsigned short play1_bits[] =
66 {
67 #include "icons/play1.icon"
68 };
69 static unsigned short play2_bits[] =
70 {
71 #include "icons/play2.icon"
72 };
73 static unsigned short play3_bits[] =
74 {
75 #include "icons/play3.icon"
76 };
77 static unsigned short play4_bits[] =
78 {
79 #include "icons/play4.icon"
80 };
81 static unsigned short pause_bits[] =
82 {
83 #include "icons/pause.icon"
84 };
85 static unsigned short pause1_bits[] =
86 {
87 #include "icons/pause1.icon"
88 };
89 static unsigned short pause2_bits[] =
90 {
91 #include "icons/pause2.icon"
92 };
93 static unsigned short pause3_bits[] =
94 {
95 #include "icons/pause3.icon"
96 };
97 static unsigned short pause4_bits[] =
98 {
99 #include "icons/pause4.icon"
100 };
101 static unsigned short arrow_bits[] =
102 {
103 #include "icons/arrow.icon"
104 };
105 static unsigned short none_bits[] =
106 {
107 #include "icons/none.icon"
108 };
109 static unsigned short file_bits[] =
110 {
111 #include "icons/file.icon"
112 };
113 static unsigned short watch_bits[] =
114 {
115 #include "icons/watch.icon"
116 };
117
118 SOME_ICON icons[NUMBER_OF_ICONS] =
119 {
120 { NULL, play_bits },
121 { NULL, play1_bits },
122 { NULL, play2_bits },
123 { NULL, play3_bits },
124 { NULL, play4_bits },
125 { NULL, pause_bits },
126 { NULL, pause1_bits },
127 { NULL, pause2_bits },
128 { NULL, pause3_bits },
129 { NULL, pause4_bits },
130 { NULL, directory_bits },
131 { NULL, sound_bits },
132 { NULL, arrow_bits },
133 { NULL, none_bits },
134 { NULL, file_bits },
135 { NULL, watch_bits },
136 };
137
138 #ifdef __STDC__
139 int
140 notice (Xv_opaque panel, char *button1, char *button2, char *fmt, ...)
141 #else
142 int
143 notice (va_alist)
144 va_dcl
145 #endif
146 {
147 char message[1024];
148 Xv_notice nw;
149 int choice = 0;
150 va_list args;
151
152 #ifdef __STDC__
153 va_start (args, fmt);
154 #else
155 char *fmt, *button1, *button2;
156 Xv_opaque panel;
157 va_start (args);
158 panel = va_arg (args, Xv_opaque);
159 button1 = va_arg (args, char *);
160 button2 = va_arg (args, char *);
161 fmt = va_arg (args, char *);
162 #endif
163
164 vsprintf (message, fmt, args);
165
166 if (button1 && button2)
167 {
168 nw = xv_create (panel, NOTICE,
169 NOTICE_MESSAGE_STRINGS, message, NULL,
170 NOTICE_BUTTON, button1, 1,
171 NOTICE_BUTTON, button2, 2,
172 NOTICE_STATUS, &choice,
173 XV_SHOW, TRUE,
174 NULL);
175 }
176 else if (button1)
177 {
178 nw = xv_create (panel, NOTICE,
179 NOTICE_MESSAGE_STRINGS, message, NULL,
180 NOTICE_BUTTON, button1, 1,
181 NOTICE_STATUS, &choice,
182 XV_SHOW, TRUE,
183 NULL);
184 }
185 else
186 {
187 nw = xv_create (panel, NOTICE,
188 NOTICE_MESSAGE_STRINGS, message, NULL,
189 XV_SHOW, TRUE,
190 NULL);
191 }
192
193 xv_destroy_safe (nw);
194
195 return choice;
196 }
197
198 void
199 sort_list (list)
200 Xv_opaque list;
201 {
202 xv_set(list, PANEL_INACTIVE, TRUE, NULL);
203 xv_set(list, PANEL_LIST_SORT, PANEL_FORWARD, NULL);
204 xv_set(list, PANEL_INACTIVE, FALSE, NULL);
205 }
206
207 void
208 busy (frame, flag)
209 Xv_opaque frame;
210 int flag;
211 {
212 xv_set (frame, FRAME_BUSY, flag, NULL);
213 }
214
215 static INFO *
216 new_info ()
217 {
218 INFO *info = (INFO *) malloc (sizeof (INFO));
219
220 info->filename[0] = '\0';
221 info->name[0] = '\0';
222 info->volume = RPLAY_DEFAULT_VOLUME;
223 info->sample_rate = RPLAY_DEFAULT_SAMPLE_RATE;
224 info->delay = initial_delay; /* -1 or seconds */
225
226 return info;
227 }
228
229 /* Initialize a playlist. */
230 void
231 do_playlist_init (list)
232 int list;
233 {
234 playlist[list].id = -1;
235 playlist[list].size = 0;
236 playlist[list].curr = -1;
237 playlist[list].auto_play = 1;
238 playlist[list].filename[0] = '\0';
239 playlist[list].mtime = 0;
240 playlist[list].delay = -1;
241 }
242
243 /* Reset a playlist. */
244 void
245 do_playlist_reset (list)
246 int list;
247 {
248 playlist[list].id = -1;
249 playlist[list].size = 0;
250 playlist[list].curr = -1;
251 playlist[list].auto_play = 1;
252 /* playlist[list].filename[0] = '\0'; */
253 /* playlist[list].mtime = 0; */
254 /* playlist[list].delay = -1; */
255 }
256
257 void
258 playlist_update_icons (list)
259 int list;
260 {
261 int i;
262
263 xv_set (playlist[list].list, XV_SHOW, FALSE, NULL);
264
265 for (i = 0; i < playlist[list].size; i++)
266 {
267 xv_set (playlist[list].list,
268 PANEL_LIST_GLYPH, i, icons[ICON_NONE].image, NULL);
269 }
270
271 /* Delaying */
272 if (playlist[list].delay > 0)
273 {
274 xv_set (playlist[list].list,
275 PANEL_LIST_GLYPH, playlist[list].curr, icons[ICON_WATCH].image,
276 NULL);
277 }
278 /* Playing */
279 else if (playlist[list].curr >= 0 && playlist[list].id > 0)
280 {
281 xv_set (playlist[list].list,
282 PANEL_LIST_GLYPH, playlist[list].curr, icons[ICON_SOUND].image, NULL);
283 }
284 /* Not playing */
285 else if (playlist[list].curr >= 0 && playlist[list].curr < playlist[list].size)
286 {
287 xv_set (playlist[list].list,
288 PANEL_LIST_GLYPH, playlist[list].curr, icons[ICON_ARROW].image, NULL);
289 }
290 else
291 {
292 xv_set (playlist[list].list,
293 PANEL_LIST_GLYPH, 0, icons[ICON_ARROW].image, NULL);
294 }
295
296 xv_set (playlist[list].list, XV_SHOW, TRUE, NULL);
297 }
298
299
300 /* Add a sound to list at index. */
301 void
302 do_playlist_add (list, index, filename)
303 int list;
304 int index;
305 char *filename;
306 {
307 INFO *info;
308 char *p;
309
310 info = new_info ();
311 strcpy (info->filename, filename);
312 p = strrchr (info->filename, '/');
313 if (p)
314 {
315 strcpy (info->name, p+1);
316 }
317 else
318 {
319 strcpy (info->name, info->filename);
320 }
321
322 xv_set (playlist[list].list,
323 PANEL_LIST_INSERT, index,
324 PANEL_LIST_STRING, index, info->name,
325 PANEL_LIST_CLIENT_DATA, index, info,
326 NULL);
327
328 playlist[list].size++;
329 }
330
331 /* Append a sound to list .*/
332 void
333 do_playlist_append (list, filename)
334 int list;
335 char *filename;
336 {
337 do_playlist_add (list, playlist[list].size, filename);
338 playlist_update_icons (list);
339 }
340
341 void
342 do_playlist_move (list, from, to)
343 int list;
344 int from;
345 int to;
346 {
347 char from_name[RPTP_MAX_LINE], *from_data;
348 char to_name[RPTP_MAX_LINE], *to_data;
349
350 xv_set (playlist[list].list, XV_SHOW, FALSE, NULL);
351
352 strcpy (from_name, (char *) xv_get (playlist[list].list, PANEL_LIST_STRING, from));
353 from_data = (char *) xv_get (playlist[list].list, PANEL_LIST_CLIENT_DATA, from);
354
355 strcpy (to_name, (char *) xv_get (playlist[list].list, PANEL_LIST_STRING, to));
356 to_data = (char *) xv_get (playlist[list].list, PANEL_LIST_CLIENT_DATA, to);
357
358 xv_set (playlist[list].list,
359 PANEL_LIST_STRING, from, to_name,
360 PANEL_LIST_CLIENT_DATA, from, to_data,
361 NULL);
362
363 xv_set (playlist[list].list,
364 PANEL_LIST_STRING, to, from_name,
365 PANEL_LIST_CLIENT_DATA, to, from_data,
366 NULL);
367
368 playlist_update_icons (list);
369
370 xv_set (playlist[list].list, XV_SHOW, TRUE, NULL);
371 }
372
373 /* Delete all entries from a playlist. */
374 void
375 do_playlist_delete_all (list)
376 int list;
377 {
378 int i, n;
379
380 n = (int) xv_get (playlist[list].list, PANEL_LIST_NROWS);
381
382 for (i = 0; i < n; i++)
383 {
384 do_playlist_delete (list, 0);
385 }
386
387 do_playlist_reset (list);
388 }
389
390 /* Delete a single entry from a playlist. */
391 void
392 do_playlist_delete (list, index)
393 int list;
394 int index;
395 {
396 char *p;
397
398 p = (char *) xv_get (playlist[list].list, PANEL_LIST_CLIENT_DATA, index);
399 if (p)
400 {
401 free ((char *) p);
402 }
403
404 xv_set (playlist[list].list, PANEL_LIST_DELETE, index, NULL);
405
406 /* Update the size. */
407 playlist[curr_playlist].size--;
408
409 /* Stop it if it's playing. */
410 if (playlist[list].curr != -1 && index == playlist[list].curr)
411 {
412 do_playlist_stop (list);
413 playlist[list].curr--;
414 }
415 /* Update curr. */
416 else if (playlist[list].curr != -1 && index < playlist[list].curr)
417 {
418 playlist[list].curr--;
419 }
420 }
421
422 /* Stop the sound a playlist is playing. */
423 void
424 do_playlist_stop (list)
425 int list;
426 {
427 if (playlist[list].curr != -1 && playlist[list].id > 0)
428 {
429 char client_data[RPTP_MAX_LINE];
430
431 sprintf (client_data, "%d %d", client_id, list);
432 do_rptp_stop (playlist[list].id, client_data);
433
434 playlist[curr_playlist].id = -1;
435 }
436
437 if (playlist[curr_playlist].delay > 0)
438 {
439 playlist[curr_playlist].delay = -1;
440 }
441
442 playlist_update_icons (list);
443 }
444
445 void
446 do_playlist_load (list, fp)
447 int list;
448 FILE *fp;
449 {
450 char buf[BUFSIZ];
451 char *value;
452 INFO *info;
453 struct stat st;
454
455 while (fgets (buf, sizeof (buf), fp))
456 {
457 value = rptp_parse (buf, 0);
458 do_playlist_append (list, value);
459
460 info = (INFO *) xv_get (playlist[list].list, PANEL_LIST_CLIENT_DATA, playlist[list].size - 1);
461
462 value = rptp_parse (0, "volume");
463 if (value && *value)
464 {
465 info->volume = atoi (value);
466 }
467
468 value = rptp_parse (0, "sample-rate");
469 if (value && *value)
470 {
471 info->sample_rate = atoi (value);
472 }
473
474 value = rptp_parse (0, "name");
475 if (value && *value)
476 {
477 strcpy (info->name, value);
478 xv_set (playlist[list].list,
479 PANEL_LIST_STRING, playlist[list].size - 1, info->name,
480 NULL);
481 }
482
483 value = rptp_parse (0, "delay");
484 if (value && *value)
485 {
486 info->delay = atoi (value);
487 }
488 }
489
490 if (fstat (fileno (fp), &st) > 0)
491 {
492 playlist[list].mtime = st.st_mtime;
493 }
494
495 playlist_update_icons (list);
496 }
497
498 void
499 do_playlist_save (list, fp)
500 int list;
501 FILE *fp;
502 {
503 INFO *info;
504 int i;
505
506 for (i = 0; i < playlist[list].size; i++)
507 {
508 info = (INFO *) xv_get (playlist[list].list, PANEL_LIST_CLIENT_DATA, i);
509
510 fprintf (fp, "%s --name=\"%s\"", info->filename, info->name);
511
512 if (info->volume != RPLAY_DEFAULT_VOLUME)
513 {
514 fprintf (fp, " --volume=%d", info->volume);
515 }
516
517 if (info->sample_rate != RPLAY_DEFAULT_SAMPLE_RATE)
518 {
519 fprintf (fp, " --sample-rate=%d", info->sample_rate);
520 }
521
522 if (info->delay > 0)
523 {
524 fprintf (fp, " --delay=%d", info->delay);
525 }
526
527 fprintf (fp, "\n");
528 }
529 }
530
531 /* Scan all the playlist filenames to see if they have been modified.
532 If so, update the playlist with the new file contents. */
533 void
534 playlist_scan ()
535 {
536 int i, n;
537 struct stat st;
538 FILE *fp;
539 char buf[BUFSIZ];
540 char message[1024];
541 char filename[MAXPATHLEN];
542 INFO *info;
543 char was_playing[MAXPATHLEN];
544 int was_playing_id;
545 int was_playing_index;
546
547 for (i = 0; i < NUMBER_OF_PLAYLISTS; i++)
548 {
549 if (playlist[i].filename[0] != '\0')
550 {
551 stat (playlist[i].filename, &st);
552 if (st.st_mtime > playlist[i].mtime)
553 {
554 busy (Rplaytool_s->s, TRUE);
555
556 fp = fopen (playlist[i].filename, "r");
557 if (!fp)
558 {
559 fprintf (stderr, "Can't open `%s'\n", playlist[i].filename);
560 busy (Rplaytool_s->s, FALSE);
561 continue;
562 }
563
564 /* Save the sound that was playing. */
565 if (playlist[i].curr != -1)
566 {
567 info = (INFO *) xv_get (playlist[i].list, PANEL_LIST_CLIENT_DATA, playlist[i].curr);
568 strcpy (was_playing, info->filename);
569 was_playing_id = playlist[i].id;
570 was_playing_index = playlist[i].curr;
571 }
572 else
573 {
574 was_playing[0] = '\0';
575 was_playing_id = 0;
576 was_playing_index = 0;
577 }
578
579 /* Delete all sounds from this list. */
580 playlist[i].curr = -1; /* hack to prevent the stopping of the playing sound */
581 playlist[i].id = -1;
582 do_playlist_delete_all (i);
583
584 /* Load the new list. */
585 do_playlist_load (i, fp);
586 fclose (fp);
587
588 if (was_playing[0])
589 {
590 int new_position = -1;
591
592 /* Try to find the sound that was playing in the new list. */
593
594 /* First try the original position. */
595 n = (int) xv_get (playlist[i].list, PANEL_LIST_NROWS);
596 if (was_playing_index < n)
597 {
598 info = (INFO *) xv_get (playlist[i].list, PANEL_LIST_CLIENT_DATA, was_playing_index);
599 }
600 else
601 {
602 info = NULL;
603 }
604 if (info && strcmp (was_playing, info->filename) == 0)
605 {
606 new_position = was_playing_index;
607 }
608 /* Find closest match. */
609 else
610 {
611 int closest_before = -1;
612 int closest_after = -1;
613 int match_found = 0;
614
615 for (n = 0; n < playlist[i].size; n++)
616 {
617 info = (INFO *) xv_get (playlist[i].list, PANEL_LIST_CLIENT_DATA, n);
618 if (strcmp (was_playing, info->filename) == 0)
619 {
620 match_found++;
621
622 if (n < was_playing_index)
623 {
624 closest_before = n;
625 }
626 else if (n > was_playing_index)
627 {
628 closest_after = n;
629 break;
630 }
631 }
632 }
633
634 #if 0
635 printf ("match_found=%d prev=%d next=%d was=%d\n",
636 match_found, closest_before, closest_after, was_playing_index);
637 #endif
638
639 if (match_found)
640 {
641 if (closest_before >= 0 && closest_after >= 0)
642 {
643 int distance_before;
644 int distance_after;
645
646 distance_before = was_playing_index - closest_before;
647 distance_after = closest_after - was_playing_index;
648
649 /* If matches are equally distance, use the previous one.
650 Otherwise, use the closest one. */
651 new_position = distance_before <= distance_after
652 ? closest_before : closest_after;
653 }
654 else if (closest_before >= 0)
655 {
656 new_position = closest_before;
657 }
658 else
659 {
660 new_position = closest_after;
661 }
662 }
663 }
664
665 if (new_position == -1)
666 {
667 do_rptp_stop (was_playing_id, "");
668 playlist[i].curr = 0;
669 play_playlist (i, TRUE);
670 }
671 else
672 {
673 playlist[i].curr = new_position;
674 playlist[i].id = was_playing_id;
675 }
676 }
677
678 playlist[i].mtime = st.st_mtime;
679 playlist_update_icons (i);
680 busy (Rplaytool_s->s, FALSE);
681 }
682 }
683 }
684 }
685
686 void
687 play_playlist (list, use_delay)
688 int list;
689 int use_delay;
690 {
691 char client_data[32];
692 Scrollbar scrollbar;
693 INFO *info;
694
695 if ((int) xv_get (playlist[list].list, PANEL_LIST_NROWS) > 0
696 && playlist[list].curr >= 0
697 && playlist[list].curr < playlist[list].size)
698 {
699 int rows, view;
700
701 info = (INFO *) xv_get (playlist[list].list,
702 PANEL_LIST_CLIENT_DATA, playlist[list].curr);
703
704 /* See if the play should be delayed a while. */
705 if (use_delay && info->delay > 0)
706 {
707 /* It's already being delayed, interrupt it. */
708 if (playlist[list].delay > 0)
709 {
710 playlist[list].delay = -1;
711 }
712 else
713 {
714 playlist[list].delay = info->delay;
715 playlist_update_icons (list);
716 return;
717 }
718 }
719
720 /* Set `client-data' to the client_id and soundlist number. */
721 sprintf (client_data, "%d %d", client_id, list);
722
723 /* Play the sound */
724 do_rptp_play_info (info, client_data);
725
726 busy (Rplaytool_s->s, TRUE);
727
728 /* Auto-scroll */
729 rows = (int) xv_get (playlist[list].list, PANEL_LIST_DISPLAY_ROWS);
730 scrollbar = (Scrollbar) xv_get (playlist[list].list, PANEL_LIST_SCROLLBAR);
731 view = (int) xv_get (scrollbar, SCROLLBAR_VIEW_START);
732 if (playlist[list].curr >= view + rows)
733 {
734 xv_set (scrollbar, SCROLLBAR_VIEW_START, playlist[list].curr, NULL);
735 }
736 else if (playlist[list].curr < view)
737 {
738 view = playlist[list].curr - rows;
739 if (view < 0)
740 {
741 view = 0;
742 }
743 xv_set (scrollbar, SCROLLBAR_VIEW_START, view, NULL);
744 }
745 }
746 else if (playlist[list].curr >= playlist[list].size
747 || playlist[list].curr < 0)
748 {
749 playlist[list].curr = 0; /* rewind */
750 }
751
752 playlist_update_icons (list);
753 }