cdplay.c revision 1.27 1 1.27 christos /* $NetBSD: cdplay.c,v 1.27 2003/09/12 00:39:38 christos Exp $ */
2 1.1 ad
3 1.1 ad /*
4 1.15 ad * Copyright (c) 1999, 2000, 2001 Andrew Doran.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * Redistribution and use in source and binary forms, with or without
8 1.1 ad * modification, are permitted provided that the following conditions
9 1.1 ad * are met:
10 1.1 ad * 1. Redistributions of source code must retain the above copyright
11 1.1 ad * notice, this list of conditions and the following disclaimer.
12 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 ad * notice, this list of conditions and the following disclaimer in the
14 1.1 ad * documentation and/or other materials provided with the distribution.
15 1.1 ad *
16 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 ad * SUCH DAMAGE.
27 1.1 ad *
28 1.1 ad */
29 1.1 ad
30 1.1 ad /*
31 1.15 ad * Compact Disc Control Utility, originally by Serge V. Vakulenko
32 1.15 ad * <vak (at) cronyx.ru>. First appeared in FreeBSD under the guise of
33 1.15 ad * cdcontrol(1). Based on the non-X based CD player by Jean-Marc
34 1.15 ad * Zucconi and Andrey A. Chernov. Fixed and further modified on
35 1.15 ad * by Jukka Ukkonen <jau (at) funet.fi>. Lots of fixes and improvements
36 1.15 ad * made subsequently by The NetBSD Project.
37 1.1 ad *
38 1.6 ad * from FreeBSD: cdcontrol.c,v 1.17.2.1 1999/01/31 15:36:01 billf Exp
39 1.1 ad */
40 1.1 ad
41 1.1 ad #include <sys/cdefs.h>
42 1.1 ad #ifndef lint
43 1.27 christos __RCSID("$NetBSD: cdplay.c,v 1.27 2003/09/12 00:39:38 christos Exp $");
44 1.1 ad #endif /* not lint */
45 1.19 thorpej
46 1.19 thorpej #include <sys/types.h>
47 1.1 ad
48 1.5 ad #include <sys/endian.h>
49 1.5 ad #include <sys/ioctl.h>
50 1.5 ad #include <sys/file.h>
51 1.5 ad #include <sys/cdio.h>
52 1.5 ad
53 1.22 is #include <assert.h>
54 1.22 is
55 1.1 ad #include <ctype.h>
56 1.1 ad #include <err.h>
57 1.1 ad #include <errno.h>
58 1.4 msaitoh #include <histedit.h>
59 1.21 is #include <limits.h>
60 1.16 ad #include <signal.h>
61 1.1 ad #include <stdio.h>
62 1.1 ad #include <stdlib.h>
63 1.1 ad #include <string.h>
64 1.1 ad #include <unistd.h>
65 1.11 lukem #include <util.h>
66 1.1 ad
67 1.15 ad enum cmd {
68 1.16 ad CMD_CLOSE,
69 1.15 ad CMD_EJECT,
70 1.15 ad CMD_HELP,
71 1.15 ad CMD_INFO,
72 1.16 ad CMD_NEXT,
73 1.15 ad CMD_PAUSE,
74 1.15 ad CMD_PLAY,
75 1.16 ad CMD_PREV,
76 1.15 ad CMD_QUIT,
77 1.16 ad CMD_RESET,
78 1.15 ad CMD_RESUME,
79 1.16 ad CMD_SET,
80 1.16 ad CMD_SHUFFLE,
81 1.16 ad CMD_SKIP,
82 1.16 ad CMD_STATUS,
83 1.15 ad CMD_STOP,
84 1.15 ad CMD_VOLUME,
85 1.15 ad };
86 1.1 ad
87 1.1 ad struct cmdtab {
88 1.15 ad enum cmd command;
89 1.15 ad const char *name;
90 1.15 ad unsigned int min;
91 1.15 ad const char *args;
92 1.15 ad } const cmdtab[] = {
93 1.16 ad { CMD_HELP, "?", 1, 0 },
94 1.16 ad { CMD_CLOSE, "close", 1, NULL },
95 1.16 ad { CMD_EJECT, "eject", 1, NULL },
96 1.16 ad { CMD_HELP, "help", 1, NULL },
97 1.16 ad { CMD_INFO, "info", 1, NULL },
98 1.16 ad { CMD_NEXT, "next", 1, NULL },
99 1.16 ad { CMD_PAUSE, "pause", 2, NULL },
100 1.16 ad { CMD_PLAY, "play", 1, "min1:sec1[.fram1] [min2:sec2[.fram2]]" },
101 1.16 ad { CMD_PLAY, "play", 1, "track1[.index1] [track2[.index2]]" },
102 1.16 ad { CMD_PLAY, "play", 1, "tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]" },
103 1.16 ad { CMD_PLAY, "play", 1, "[#block [len]]" },
104 1.16 ad { CMD_PREV, "prev", 2, NULL },
105 1.16 ad { CMD_QUIT, "quit", 1, NULL },
106 1.16 ad { CMD_RESET, "reset", 4, NULL },
107 1.16 ad { CMD_RESUME, "resume", 4, NULL },
108 1.16 ad { CMD_SET, "set", 2, "msf | lba" },
109 1.16 ad { CMD_SHUFFLE, "shuffle", 2, NULL },
110 1.16 ad { CMD_SKIP, "skip", 2, NULL },
111 1.16 ad { CMD_STATUS, "status", 3, NULL },
112 1.16 ad { CMD_STOP, "stop", 3, NULL },
113 1.16 ad { CMD_VOLUME, "volume", 1, "<l> <r>|left|right|mute|mono|stereo" },
114 1.1 ad };
115 1.1 ad
116 1.23 simonb #define CD_MAX_TRACK 99 /* largest 2 digit BCD number */
117 1.23 simonb
118 1.23 simonb struct cd_toc_entry toc_buffer[CD_MAX_TRACK + 1];
119 1.1 ad
120 1.1 ad const char *cdname;
121 1.1 ad int fd = -1;
122 1.1 ad int msf = 1;
123 1.16 ad int shuffle;
124 1.16 ad int interactive = 1;
125 1.16 ad struct itimerval itv_timer;
126 1.1 ad
127 1.4 msaitoh History *hist;
128 1.4 msaitoh HistEvent he;
129 1.4 msaitoh EditLine *elptr;
130 1.4 msaitoh
131 1.15 ad int get_vol(int *, int *);
132 1.16 ad int get_status(int *, int *, int *, int *, int *);
133 1.15 ad void help(void);
134 1.15 ad int info(const char *);
135 1.15 ad void lba2msf(u_long, u_int *, u_int *, u_int *);
136 1.15 ad int main(int, char **);
137 1.15 ad u_int msf2lba(u_int, u_int, u_int);
138 1.15 ad int opencd(void);
139 1.15 ad const char *parse(char *, int *);
140 1.16 ad int play(const char *, int);
141 1.15 ad int play_blocks(int, int);
142 1.15 ad int play_msf(int, int, int, int, int, int);
143 1.15 ad int play_track(int, int, int, int);
144 1.15 ad int print_status(const char *);
145 1.23 simonb void print_track(struct cd_toc_entry *);
146 1.15 ad const char *prompt(void);
147 1.15 ad int read_toc_entrys(int);
148 1.15 ad int run(int, const char *);
149 1.15 ad int setvol(int, int);
150 1.27 christos void sig_timer(int);
151 1.16 ad int skip(int, int);
152 1.15 ad const char *strstatus(int);
153 1.15 ad void usage(void);
154 1.1 ad
155 1.22 is void toc2msf(u_int, u_int *, u_int *, u_int *);
156 1.22 is int toc2lba(u_int);
157 1.22 is void addmsf(u_int *, u_int *, u_int *, u_int, u_int, u_int);
158 1.22 is
159 1.15 ad int
160 1.15 ad main(int argc, char **argv)
161 1.1 ad {
162 1.15 ad const char *arg;
163 1.15 ad char buf[80], *p;
164 1.5 ad static char defdev[16];
165 1.15 ad int cmd, len, c;
166 1.4 msaitoh char *line;
167 1.4 msaitoh const char *elline;
168 1.16 ad int scratch, rv;
169 1.16 ad struct sigaction sa_timer;
170 1.1 ad
171 1.1 ad cdname = getenv("MUSIC_CD");
172 1.15 ad if (cdname == NULL)
173 1.1 ad cdname = getenv("CD_DRIVE");
174 1.15 ad if (cdname == NULL)
175 1.1 ad cdname = getenv("DISC");
176 1.15 ad if (cdname == NULL)
177 1.1 ad cdname = getenv("CDPLAY");
178 1.1 ad
179 1.15 ad while ((c = getopt(argc, argv, "f:h")) != -1)
180 1.15 ad switch (c) {
181 1.1 ad case 'f':
182 1.1 ad cdname = optarg;
183 1.1 ad continue;
184 1.1 ad case 'h':
185 1.1 ad default:
186 1.1 ad usage();
187 1.15 ad /* NOTREACHED */
188 1.1 ad }
189 1.1 ad argc -= optind;
190 1.1 ad argv += optind;
191 1.1 ad
192 1.18 ad if (argc > 0 && strcasecmp(*argv, "help") == 0)
193 1.1 ad usage();
194 1.1 ad
195 1.15 ad if (cdname == NULL) {
196 1.25 itojun snprintf(defdev, sizeof(defdev), "cd0%c",
197 1.25 itojun 'a' + getrawpartition());
198 1.1 ad cdname = defdev;
199 1.1 ad }
200 1.15 ad
201 1.3 ad opencd();
202 1.16 ad srandom(time(NULL));
203 1.16 ad
204 1.1 ad if (argc > 0) {
205 1.16 ad interactive = 0;
206 1.15 ad for (p = buf; argc-- > 0; argv++) {
207 1.1 ad len = strlen(*argv);
208 1.1 ad
209 1.1 ad if (p + len >= buf + sizeof(buf) - 1)
210 1.1 ad usage();
211 1.1 ad if (p > buf)
212 1.1 ad *p++ = ' ';
213 1.1 ad
214 1.26 itojun strlcpy(p, *argv, sizeof(buf) - (p - buf));
215 1.1 ad p += len;
216 1.1 ad }
217 1.15 ad *p = '\0';
218 1.1 ad arg = parse(buf, &cmd);
219 1.1 ad return (run(cmd, arg));
220 1.1 ad }
221 1.15 ad
222 1.1 ad printf("Type `?' for command list\n\n");
223 1.1 ad
224 1.4 msaitoh hist = history_init();
225 1.4 msaitoh history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
226 1.12 cgd elptr = el_init(getprogname(), stdin, stdout, stderr);
227 1.4 msaitoh el_set(elptr, EL_EDITOR, "emacs");
228 1.4 msaitoh el_set(elptr, EL_PROMPT, prompt);
229 1.4 msaitoh el_set(elptr, EL_HIST, history, hist);
230 1.4 msaitoh el_source(elptr, NULL);
231 1.4 msaitoh
232 1.16 ad sigemptyset(&sa_timer.sa_mask);
233 1.27 christos sa_timer.sa_handler = sig_timer;
234 1.16 ad sa_timer.sa_flags = SA_RESTART;
235 1.16 ad if ((rv = sigaction(SIGALRM, &sa_timer, NULL)) < 0)
236 1.16 ad err(EXIT_FAILURE, "sigaction()");
237 1.16 ad
238 1.1 ad for (;;) {
239 1.4 msaitoh line = NULL;
240 1.4 msaitoh do {
241 1.4 msaitoh if (((elline = el_gets(elptr, &scratch)) != NULL)
242 1.4 msaitoh && (scratch != 0)){
243 1.4 msaitoh history(hist, &he, H_ENTER, elline);
244 1.4 msaitoh line = strdup(elline);
245 1.4 msaitoh arg = parse(line, &cmd);
246 1.4 msaitoh } else {
247 1.4 msaitoh cmd = CMD_QUIT;
248 1.4 msaitoh fprintf(stderr, "\r\n");
249 1.4 msaitoh arg = 0;
250 1.4 msaitoh break;
251 1.4 msaitoh }
252 1.15 ad } while (arg == NULL);
253 1.4 msaitoh
254 1.1 ad if (run(cmd, arg) < 0) {
255 1.15 ad if (fd != -1)
256 1.15 ad close(fd);
257 1.1 ad fd = -1;
258 1.1 ad }
259 1.1 ad fflush(stdout);
260 1.4 msaitoh if (line != NULL)
261 1.4 msaitoh free(line);
262 1.1 ad }
263 1.15 ad
264 1.4 msaitoh el_end(elptr);
265 1.4 msaitoh history_end(hist);
266 1.15 ad exit(EXIT_SUCCESS);
267 1.15 ad /* NOTREACHED */
268 1.1 ad }
269 1.1 ad
270 1.15 ad void
271 1.15 ad usage(void)
272 1.1 ad {
273 1.1 ad
274 1.15 ad fprintf(stderr, "usage: cdplay [-f device] [command ...]\n");
275 1.15 ad exit(EXIT_FAILURE);
276 1.15 ad /* NOTREACHED */
277 1.15 ad }
278 1.15 ad
279 1.15 ad void
280 1.15 ad help(void)
281 1.15 ad {
282 1.15 ad const struct cmdtab *c, *mc;
283 1.15 ad const char *s;
284 1.15 ad int i, n;
285 1.15 ad
286 1.15 ad mc = cmdtab + sizeof(cmdtab) / sizeof(cmdtab[0]);
287 1.15 ad for (c = cmdtab; c < mc; c++) {
288 1.15 ad for (i = c->min, s = c->name; *s != '\0'; s++, i--) {
289 1.15 ad n = (i > 0 ? toupper(*s) : *s);
290 1.15 ad putchar(n);
291 1.5 ad }
292 1.15 ad if (c->args != NULL)
293 1.15 ad printf(" %s", c->args);
294 1.15 ad putchar('\n');
295 1.15 ad }
296 1.15 ad printf(
297 1.15 ad "\nThe word \"play\" is not required for the play commands.\n"
298 1.15 ad "The plain target address is taken as a synonym for play.\n");
299 1.15 ad }
300 1.15 ad
301 1.15 ad int
302 1.15 ad run(int cmd, const char *arg)
303 1.15 ad {
304 1.15 ad int l, r, rv;
305 1.15 ad
306 1.15 ad if (cmd == CMD_QUIT) {
307 1.16 ad close(fd);
308 1.15 ad exit(EXIT_SUCCESS);
309 1.15 ad /* NOTREACHED */
310 1.5 ad }
311 1.15 ad
312 1.5 ad if (fd < 0 && !opencd())
313 1.5 ad return (0);
314 1.15 ad
315 1.5 ad switch (cmd) {
316 1.1 ad case CMD_INFO:
317 1.15 ad rv = info(arg);
318 1.15 ad break;
319 1.1 ad
320 1.1 ad case CMD_STATUS:
321 1.15 ad rv = print_status(arg);
322 1.15 ad break;
323 1.1 ad
324 1.1 ad case CMD_PAUSE:
325 1.15 ad if ((rv = ioctl(fd, CDIOCPAUSE)) < 0)
326 1.15 ad warn("ioctl(CDIOCPAUSE)");
327 1.15 ad break;
328 1.1 ad
329 1.1 ad case CMD_RESUME:
330 1.15 ad if ((rv = ioctl(fd, CDIOCRESUME)) < 0)
331 1.15 ad warn("ioctl(CDIOCRESUME)");
332 1.15 ad break;
333 1.1 ad
334 1.1 ad case CMD_STOP:
335 1.15 ad if ((rv = ioctl(fd, CDIOCSTOP)) < 0)
336 1.15 ad warn("ioctl(CDIOCSTOP)");
337 1.15 ad if (ioctl(fd, CDIOCALLOW) < 0)
338 1.15 ad warn("ioctl(CDIOCALLOW)");
339 1.15 ad break;
340 1.1 ad
341 1.1 ad case CMD_RESET:
342 1.15 ad if ((rv = ioctl(fd, CDIOCRESET)) >= 0) {
343 1.15 ad close(fd);
344 1.15 ad fd = -1;
345 1.15 ad } else
346 1.15 ad warn("ioctl(CDIOCRESET)");
347 1.1 ad return (0);
348 1.1 ad
349 1.1 ad case CMD_EJECT:
350 1.16 ad if (shuffle)
351 1.16 ad run(CMD_SHUFFLE, NULL);
352 1.15 ad if (ioctl(fd, CDIOCALLOW) < 0)
353 1.15 ad warn("ioctl(CDIOCALLOW)");
354 1.15 ad if ((rv = ioctl(fd, CDIOCEJECT)) < 0)
355 1.15 ad warn("ioctl(CDIOCEJECT)");
356 1.15 ad break;
357 1.1 ad
358 1.1 ad case CMD_CLOSE:
359 1.3 ad ioctl(fd, CDIOCALLOW);
360 1.15 ad if ((rv = ioctl(fd, CDIOCCLOSE)) >= 0) {
361 1.15 ad close(fd);
362 1.15 ad fd = -1;
363 1.15 ad } else
364 1.15 ad warn("ioctl(CDIOCCLOSE)");
365 1.15 ad break;
366 1.1 ad
367 1.1 ad case CMD_PLAY:
368 1.1 ad while (isspace(*arg))
369 1.1 ad arg++;
370 1.16 ad rv = play(arg, 1);
371 1.15 ad break;
372 1.1 ad
373 1.13 gmcgarry case CMD_PREV:
374 1.16 ad rv = skip(-1, 1);
375 1.15 ad break;
376 1.13 gmcgarry
377 1.13 gmcgarry case CMD_NEXT:
378 1.16 ad rv = skip(1, 1);
379 1.16 ad break;
380 1.16 ad
381 1.16 ad case CMD_SHUFFLE:
382 1.16 ad if (interactive == 0)
383 1.16 ad errx(EXIT_FAILURE,
384 1.16 ad "`shuffle' valid only in interactive mode");
385 1.16 ad if (shuffle == 0) {
386 1.16 ad itv_timer.it_interval.tv_sec = 1;
387 1.16 ad itv_timer.it_interval.tv_usec = 0;
388 1.16 ad itv_timer.it_value.tv_sec = 1;
389 1.16 ad itv_timer.it_value.tv_usec = 0;
390 1.16 ad if (setitimer(ITIMER_REAL, &itv_timer, NULL) == 0) {
391 1.16 ad shuffle = 1;
392 1.16 ad skip(0, 1);
393 1.16 ad }
394 1.16 ad } else {
395 1.16 ad itv_timer.it_interval.tv_sec = 0;
396 1.16 ad itv_timer.it_interval.tv_usec = 0;
397 1.16 ad itv_timer.it_value.tv_sec = 0;
398 1.16 ad itv_timer.it_value.tv_usec = 0;
399 1.16 ad if (setitimer(ITIMER_REAL, &itv_timer, NULL) == 0)
400 1.16 ad shuffle = 0;
401 1.16 ad }
402 1.16 ad printf("shuffle play:\t%s\n", shuffle ? "on" : "off");
403 1.16 ad rv = 0;
404 1.16 ad break;
405 1.16 ad
406 1.16 ad case CMD_SKIP:
407 1.16 ad if (!interactive)
408 1.16 ad errx(EXIT_FAILURE,
409 1.16 ad "`skip' valid only in interactive mode");
410 1.16 ad if (!shuffle)
411 1.16 ad warnx("`skip' valid only in shuffle mode");
412 1.16 ad else
413 1.16 ad skip(0, 1);
414 1.15 ad break;
415 1.15 ad
416 1.1 ad case CMD_SET:
417 1.15 ad if (strcasecmp(arg, "msf") == 0)
418 1.1 ad msf = 1;
419 1.15 ad else if (strcasecmp(arg, "lba") == 0)
420 1.5 ad msf = 0;
421 1.1 ad else
422 1.5 ad warnx("invalid command arguments");
423 1.15 ad break;
424 1.1 ad
425 1.1 ad case CMD_VOLUME:
426 1.15 ad if (strncasecmp(arg, "left", strlen(arg)) == 0)
427 1.15 ad rv = ioctl(fd, CDIOCSETLEFT);
428 1.15 ad else if (strncasecmp(arg, "right", strlen(arg)) == 0)
429 1.15 ad rv = ioctl(fd, CDIOCSETRIGHT);
430 1.15 ad else if (strncasecmp(arg, "mono", strlen(arg)) == 0)
431 1.15 ad rv = ioctl(fd, CDIOCSETMONO);
432 1.15 ad else if (strncasecmp(arg, "stereo", strlen(arg)) == 0)
433 1.15 ad rv = ioctl(fd, CDIOCSETSTEREO);
434 1.15 ad else if (strncasecmp(arg, "mute", strlen(arg)) == 0)
435 1.15 ad rv = ioctl(fd, CDIOCSETMUTE);
436 1.15 ad else {
437 1.15 ad rv = 0;
438 1.15 ad if (sscanf(arg, "%d %d", &l, &r) != 2) {
439 1.15 ad if (sscanf(arg, "%d", &l) == 1)
440 1.15 ad r = l;
441 1.15 ad else {
442 1.15 ad warnx("invalid command arguments");
443 1.15 ad break;
444 1.15 ad }
445 1.9 abs }
446 1.15 ad rv = setvol(l, r);
447 1.1 ad }
448 1.15 ad break;
449 1.15 ad
450 1.1 ad case CMD_HELP:
451 1.1 ad default:
452 1.1 ad help();
453 1.15 ad rv = 0;
454 1.15 ad break;
455 1.15 ad }
456 1.1 ad
457 1.15 ad return (rv);
458 1.1 ad }
459 1.1 ad
460 1.15 ad int
461 1.16 ad play(const char *arg, int fromuser)
462 1.1 ad {
463 1.22 is int rv, n, start, end, istart, iend, blk, len, relend;
464 1.15 ad u_int tr1, tr2, m1, m2, s1, s2, f1, f2, tm, ts, tf;
465 1.1 ad struct ioc_toc_header h;
466 1.15 ad
467 1.16 ad if (shuffle && fromuser) {
468 1.17 ad warnx("`play' not valid in shuffle mode");
469 1.16 ad return (0);
470 1.16 ad }
471 1.16 ad
472 1.15 ad if ((rv = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
473 1.15 ad warn("ioctl(CDIOREADTOCHEADER)");
474 1.15 ad return (rv);
475 1.15 ad }
476 1.1 ad
477 1.2 ad end = 0;
478 1.2 ad istart = iend = 1;
479 1.1 ad n = h.ending_track - h.starting_track + 1;
480 1.15 ad rv = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
481 1.15 ad if (rv < 0)
482 1.15 ad return (rv);
483 1.1 ad
484 1.15 ad if (arg == NULL || *arg == '\0') {
485 1.1 ad /* Play the whole disc */
486 1.16 ad return (play_track(h.starting_track, 1, h.ending_track, 99));
487 1.1 ad }
488 1.15 ad
489 1.15 ad if (strchr(arg, '#') != NULL) {
490 1.1 ad /* Play block #blk [ len ] */
491 1.3 ad len = 0;
492 1.1 ad
493 1.1 ad if (2 != sscanf(arg, "#%d%d", &blk, &len) &&
494 1.1 ad 1 != sscanf(arg, "#%d", &blk))
495 1.1 ad goto Clean_up;
496 1.1 ad
497 1.1 ad if (len == 0) {
498 1.22 is len = toc2lba(n);
499 1.1 ad }
500 1.3 ad return (play_blocks(blk, len));
501 1.1 ad }
502 1.15 ad
503 1.15 ad if (strchr(arg, ':') != NULL) {
504 1.1 ad /*
505 1.1 ad * Play MSF m1:s1 [ .f1 ] [ m2:s2 [ .f2 ] ]
506 1.1 ad *
507 1.1 ad * Will now also undestand timed addresses relative
508 1.1 ad * to the beginning of a track in the form...
509 1.1 ad *
510 1.1 ad * tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]
511 1.1 ad */
512 1.22 is relend = 1;
513 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
514 1.15 ad if (8 == sscanf(arg, "%d %d:%d.%d %d %d:%d.%d", &tr1, &m1,
515 1.3 ad &s1, &f1, &tr2, &m2, &s2, &f2))
516 1.1 ad goto Play_Relative_Addresses;
517 1.1 ad
518 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
519 1.15 ad if (7 == sscanf(arg, "%d %d:%d %d %d:%d.%d", &tr1, &m1, &s1,
520 1.3 ad &tr2, &m2, &s2, &f2))
521 1.1 ad goto Play_Relative_Addresses;
522 1.1 ad
523 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
524 1.15 ad if (7 == sscanf(arg, "%d %d:%d.%d %d %d:%d", &tr1, &m1, &s1,
525 1.3 ad &f1, &tr2, &m2, &s2))
526 1.1 ad goto Play_Relative_Addresses;
527 1.1 ad
528 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
529 1.15 ad if (7 == sscanf(arg, "%d %d:%d.%d %d:%d.%d", &tr1, &m1, &s1,
530 1.3 ad &f1, &m2, &s2, &f2))
531 1.1 ad goto Play_Relative_Addresses;
532 1.1 ad
533 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
534 1.3 ad if (6 == sscanf(arg, "%d %d:%d.%d %d:%d", &tr1, &m1, &s1, &f1,
535 1.3 ad &m2, &s2))
536 1.1 ad goto Play_Relative_Addresses;
537 1.1 ad
538 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
539 1.15 ad if (6 == sscanf(arg, "%d %d:%d %d:%d.%d", &tr1, &m1, &s1, &m2,
540 1.3 ad &s2, &f2))
541 1.1 ad goto Play_Relative_Addresses;
542 1.1 ad
543 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
544 1.15 ad if (6 == sscanf(arg, "%d %d:%d.%d %d %d", &tr1, &m1, &s1, &f1,
545 1.3 ad &tr2, &m2))
546 1.1 ad goto Play_Relative_Addresses;
547 1.1 ad
548 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
549 1.22 is if (6 == sscanf(arg, "%d %d:%d %d %d:%d", &tr1, &m1, &s1, &tr2,
550 1.22 is &m2, &s2))
551 1.22 is goto Play_Relative_Addresses;
552 1.22 is
553 1.22 is tr2 = m2 = s2 = f2 = f1 = 0;
554 1.15 ad if (5 == sscanf(arg, "%d %d:%d %d:%d", &tr1, &m1, &s1, &m2,
555 1.3 ad &s2))
556 1.1 ad goto Play_Relative_Addresses;
557 1.1 ad
558 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
559 1.15 ad if (5 == sscanf(arg, "%d %d:%d %d %d", &tr1, &m1, &s1, &tr2,
560 1.3 ad &m2))
561 1.1 ad goto Play_Relative_Addresses;
562 1.1 ad
563 1.22 is relend=0;
564 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
565 1.15 ad if (5 == sscanf(arg, "%d %d:%d.%d %d", &tr1, &m1, &s1, &f1,
566 1.3 ad &tr2))
567 1.1 ad goto Play_Relative_Addresses;
568 1.1 ad
569 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
570 1.1 ad if (4 == sscanf(arg, "%d %d:%d %d", &tr1, &m1, &s1, &tr2))
571 1.1 ad goto Play_Relative_Addresses;
572 1.1 ad
573 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
574 1.1 ad if (4 == sscanf(arg, "%d %d:%d.%d", &tr1, &m1, &s1, &f1))
575 1.1 ad goto Play_Relative_Addresses;
576 1.1 ad
577 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
578 1.1 ad if (3 == sscanf(arg, "%d %d:%d", &tr1, &m1, &s1))
579 1.1 ad goto Play_Relative_Addresses;
580 1.1 ad
581 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
582 1.1 ad goto Try_Absolute_Timed_Addresses;
583 1.1 ad
584 1.1 ad Play_Relative_Addresses:
585 1.1 ad if (!tr1)
586 1.1 ad tr1 = 1;
587 1.2 ad else if (tr1 > n)
588 1.2 ad tr1 = n;
589 1.1 ad
590 1.22 is toc2msf(tr1-1, &tm, &ts, &tf);
591 1.22 is addmsf(&m1, &s1, &f1, tm, ts, tf);
592 1.22 is
593 1.22 is toc2msf(tr1, &tm, &ts, &tf);
594 1.22 is
595 1.15 ad if ((m1 > tm) || ((m1 == tm) && ((s1 > ts) || ((s1 == ts) &&
596 1.2 ad (f1 > tf))))) {
597 1.20 itojun warnx("Track %d is not that long.", tr1);
598 1.1 ad return (0);
599 1.1 ad }
600 1.22 is tr1--; /* XXXXX ???? */
601 1.1 ad
602 1.1 ad
603 1.1 ad if (!tr2) {
604 1.22 is if (relend) {
605 1.1 ad tr2 = tr1;
606 1.22 is
607 1.22 is addmsf(&m2, &s2, &f2, m1, s1, f1);
608 1.1 ad } else {
609 1.1 ad tr2 = n;
610 1.22 is
611 1.22 is toc2msf(n, &m2, &s2, &f2);
612 1.1 ad }
613 1.1 ad } else {
614 1.1 ad if (tr2 > n) {
615 1.1 ad tr2 = n;
616 1.1 ad m2 = s2 = f2 = 0;
617 1.1 ad } else {
618 1.22 is if (relend)
619 1.1 ad tr2--;
620 1.22 is
621 1.22 is toc2msf(tr2, &tm, &ts, &tf);
622 1.22 is addmsf(&m2, &s2, &f2, tm, ts, tf);
623 1.1 ad }
624 1.1 ad }
625 1.1 ad
626 1.22 is toc2msf(n, &tm, &ts, &tf);
627 1.2 ad
628 1.15 ad if ((tr2 < n) && ((m2 > tm) || ((m2 == tm) && ((s2 > ts) ||
629 1.2 ad ((s2 == ts) && (f2 > tf)))))) {
630 1.20 itojun warnx("The playing time of the disc is not that long.");
631 1.1 ad return (0);
632 1.1 ad }
633 1.2 ad
634 1.1 ad return (play_msf(m1, s1, f1, m2, s2, f2));
635 1.1 ad
636 1.1 ad Try_Absolute_Timed_Addresses:
637 1.21 is m2 = UINT_MAX;
638 1.21 is
639 1.1 ad if (6 != sscanf(arg, "%d:%d.%d%d:%d.%d",
640 1.1 ad &m1, &s1, &f1, &m2, &s2, &f2) &&
641 1.1 ad 5 != sscanf(arg, "%d:%d.%d%d:%d", &m1, &s1, &f1, &m2, &s2) &&
642 1.1 ad 5 != sscanf(arg, "%d:%d%d:%d.%d", &m1, &s1, &m2, &s2, &f2) &&
643 1.1 ad 3 != sscanf(arg, "%d:%d.%d", &m1, &s1, &f1) &&
644 1.1 ad 4 != sscanf(arg, "%d:%d%d:%d", &m1, &s1, &m2, &s2) &&
645 1.1 ad 2 != sscanf(arg, "%d:%d", &m1, &s1))
646 1.1 ad goto Clean_up;
647 1.1 ad
648 1.21 is if (m2 == UINT_MAX) {
649 1.1 ad if (msf) {
650 1.1 ad m2 = toc_buffer[n].addr.msf.minute;
651 1.1 ad s2 = toc_buffer[n].addr.msf.second;
652 1.1 ad f2 = toc_buffer[n].addr.msf.frame;
653 1.1 ad } else {
654 1.5 ad lba2msf(be32toh(toc_buffer[n].addr.lba),
655 1.1 ad &tm, &ts, &tf);
656 1.1 ad m2 = tm;
657 1.1 ad s2 = ts;
658 1.1 ad f2 = tf;
659 1.1 ad }
660 1.1 ad }
661 1.3 ad return (play_msf(m1, s1, f1, m2, s2, f2));
662 1.1 ad }
663 1.15 ad
664 1.1 ad /*
665 1.1 ad * Play track trk1 [ .idx1 ] [ trk2 [ .idx2 ] ]
666 1.1 ad */
667 1.1 ad if (4 != sscanf(arg, "%d.%d%d.%d", &start, &istart, &end, &iend) &&
668 1.1 ad 3 != sscanf(arg, "%d.%d%d", &start, &istart, &end) &&
669 1.1 ad 3 != sscanf(arg, "%d%d.%d", &start, &end, &iend) &&
670 1.1 ad 2 != sscanf(arg, "%d.%d", &start, &istart) &&
671 1.1 ad 2 != sscanf(arg, "%d%d", &start, &end) &&
672 1.1 ad 1 != sscanf(arg, "%d", &start))
673 1.1 ad goto Clean_up;
674 1.1 ad
675 1.1 ad if (end == 0)
676 1.1 ad end = n;
677 1.1 ad return (play_track(start, istart, end, iend));
678 1.1 ad
679 1.1 ad Clean_up:
680 1.1 ad warnx("invalid command arguments");
681 1.1 ad return (0);
682 1.13 gmcgarry }
683 1.13 gmcgarry
684 1.16 ad void
685 1.27 christos sig_timer(int sig)
686 1.16 ad {
687 1.16 ad sigset_t anymore;
688 1.16 ad
689 1.16 ad sigpending(&anymore);
690 1.16 ad if (sigismember(&anymore, SIGALRM))
691 1.16 ad return;
692 1.16 ad setitimer(ITIMER_REAL, &itv_timer, NULL);
693 1.16 ad if (fd != -1)
694 1.16 ad skip(0, 0);
695 1.16 ad }
696 1.16 ad
697 1.13 gmcgarry int
698 1.16 ad skip(int dir, int fromuser)
699 1.13 gmcgarry {
700 1.15 ad char str[16];
701 1.16 ad int rv, trk, idx, m, s, f;
702 1.13 gmcgarry struct ioc_toc_header h;
703 1.15 ad
704 1.15 ad if ((rv = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
705 1.15 ad warn("ioctl(CDIOREADTOCHEADER)");
706 1.15 ad return (rv);
707 1.15 ad }
708 1.16 ad if ((rv = get_status(&trk, &idx, &m, &s, &f)) < 0)
709 1.15 ad return (rv);
710 1.16 ad
711 1.16 ad if (dir == 0) {
712 1.16 ad if (fromuser || (rv != CD_AS_PLAY_IN_PROGRESS &&
713 1.16 ad rv != CD_AS_PLAY_PAUSED))
714 1.16 ad trk = h.starting_track +
715 1.16 ad random() % (h.ending_track - h.starting_track + 1);
716 1.16 ad else
717 1.16 ad return (0);
718 1.16 ad } else {
719 1.16 ad trk += dir;
720 1.16 ad if (trk > h.ending_track)
721 1.16 ad trk = h.starting_track;
722 1.16 ad else if(trk < h.starting_track)
723 1.16 ad trk = h.ending_track;
724 1.16 ad }
725 1.16 ad
726 1.16 ad if (shuffle)
727 1.25 itojun snprintf(str, sizeof(str), "%d %d", trk, trk);
728 1.16 ad else
729 1.25 itojun snprintf(str, sizeof(str), "%d", trk);
730 1.16 ad
731 1.16 ad return (play(str, 0));
732 1.1 ad }
733 1.1 ad
734 1.15 ad const char *
735 1.15 ad strstatus(int sts)
736 1.1 ad {
737 1.15 ad const char *str;
738 1.2 ad
739 1.1 ad switch (sts) {
740 1.15 ad case CD_AS_AUDIO_INVALID:
741 1.15 ad str = "invalid";
742 1.15 ad break;
743 1.15 ad case CD_AS_PLAY_IN_PROGRESS:
744 1.15 ad str = "playing";
745 1.15 ad break;
746 1.15 ad case CD_AS_PLAY_PAUSED:
747 1.15 ad str = "paused";
748 1.15 ad break;
749 1.15 ad case CD_AS_PLAY_COMPLETED:
750 1.15 ad str = "completed";
751 1.15 ad break;
752 1.15 ad case CD_AS_PLAY_ERROR:
753 1.15 ad str = "error";
754 1.15 ad break;
755 1.15 ad case CD_AS_NO_STATUS:
756 1.15 ad str = "not playing";
757 1.15 ad break;
758 1.1 ad default:
759 1.15 ad str = "<unknown>";
760 1.15 ad break;
761 1.1 ad }
762 1.15 ad
763 1.15 ad return (str);
764 1.1 ad }
765 1.1 ad
766 1.15 ad int
767 1.15 ad print_status(const char *arg)
768 1.1 ad {
769 1.2 ad struct cd_sub_channel_info data;
770 1.1 ad struct ioc_read_subchannel ss;
771 1.16 ad int rv, trk, idx, m, s, f;
772 1.2 ad struct ioc_vol v;
773 1.1 ad
774 1.16 ad if ((rv = get_status(&trk, &idx, &m, &s, &f)) >= 0) {
775 1.16 ad printf("audio status:\t%s\n", strstatus(rv));
776 1.15 ad printf("current track:\t%d\n", trk);
777 1.16 ad printf("current index:\t%d\n", idx);
778 1.15 ad printf("position:\t%d:%02d.%02d\n", m, s, f);
779 1.15 ad } else
780 1.15 ad printf("audio status:\tno info available\n");
781 1.15 ad
782 1.16 ad printf("shuffle play:\t%s\n", shuffle ? "on" : "off");
783 1.16 ad
784 1.15 ad bzero(&ss, sizeof(ss));
785 1.15 ad ss.data = &data;
786 1.15 ad ss.data_len = sizeof(data);
787 1.15 ad ss.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
788 1.15 ad ss.data_format = CD_MEDIA_CATALOG;
789 1.15 ad
790 1.16 ad if (ioctl(fd, CDIOCREADSUBCHANNEL, (char *)&ss) >= 0) {
791 1.15 ad printf("media catalog:\t%sactive",
792 1.15 ad ss.data->what.media_catalog.mc_valid ? "" : "in");
793 1.15 ad if (ss.data->what.media_catalog.mc_valid &&
794 1.15 ad ss.data->what.media_catalog.mc_number[0])
795 1.15 ad printf(" (%.15s)",
796 1.15 ad ss.data->what.media_catalog.mc_number);
797 1.15 ad putchar('\n');
798 1.15 ad } else
799 1.15 ad printf("media catalog:\tnone\n");
800 1.15 ad
801 1.16 ad if (ioctl(fd, CDIOCGETVOL, &v) >= 0) {
802 1.15 ad printf("left volume:\t%d\n", v.vol[0]);
803 1.15 ad printf("right volume:\t%d\n", v.vol[1]);
804 1.15 ad } else {
805 1.15 ad printf("left volume:\tnot available\n");
806 1.15 ad printf("right volume:\tnot available\n");
807 1.1 ad }
808 1.15 ad
809 1.16 ad ; return (0);
810 1.1 ad }
811 1.1 ad
812 1.15 ad int
813 1.15 ad info(const char *arg)
814 1.1 ad {
815 1.1 ad struct ioc_toc_header h;
816 1.1 ad int rc, i, n;
817 1.1 ad
818 1.1 ad if ((rc = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
819 1.15 ad warn("ioctl(CDIOREADTOCHEADER)");
820 1.1 ad return (rc);
821 1.1 ad }
822 1.1 ad
823 1.1 ad n = h.ending_track - h.starting_track + 1;
824 1.1 ad rc = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
825 1.1 ad if (rc < 0)
826 1.1 ad return (rc);
827 1.1 ad
828 1.23 simonb printf("track start duration block length type\n");
829 1.23 simonb printf("--------------------------------------------------\n");
830 1.1 ad
831 1.1 ad for (i = 0; i < n; i++) {
832 1.1 ad printf("%5d ", toc_buffer[i].track);
833 1.23 simonb print_track(toc_buffer + i);
834 1.1 ad }
835 1.24 matt printf(" - "); /* Lead-out area */
836 1.23 simonb print_track(toc_buffer + n);
837 1.1 ad return (0);
838 1.1 ad }
839 1.1 ad
840 1.15 ad void
841 1.15 ad lba2msf(u_long lba, u_int *m, u_int *s, u_int *f)
842 1.1 ad {
843 1.15 ad
844 1.1 ad lba += 150; /* block start offset */
845 1.1 ad lba &= 0xffffff; /* negative lbas use only 24 bits */
846 1.1 ad *m = lba / (60 * 75);
847 1.1 ad lba %= (60 * 75);
848 1.1 ad *s = lba / 75;
849 1.1 ad *f = lba % 75;
850 1.1 ad }
851 1.1 ad
852 1.15 ad u_int
853 1.15 ad msf2lba(u_int m, u_int s, u_int f)
854 1.1 ad {
855 1.2 ad
856 1.1 ad return (((m * 60) + s) * 75 + f) - 150;
857 1.1 ad }
858 1.1 ad
859 1.15 ad void
860 1.23 simonb print_track(struct cd_toc_entry *e)
861 1.1 ad {
862 1.1 ad int block, next, len;
863 1.15 ad u_int m, s, f;
864 1.1 ad
865 1.1 ad if (msf) {
866 1.1 ad /* Print track start */
867 1.1 ad printf("%2d:%02d.%02d ", e->addr.msf.minute,
868 1.1 ad e->addr.msf.second, e->addr.msf.frame);
869 1.1 ad
870 1.1 ad block = msf2lba(e->addr.msf.minute, e->addr.msf.second,
871 1.1 ad e->addr.msf.frame);
872 1.1 ad } else {
873 1.7 ad block = e->addr.lba;
874 1.1 ad lba2msf(block, &m, &s, &f);
875 1.1 ad /* Print track start */
876 1.1 ad printf("%2d:%02d.%02d ", m, s, f);
877 1.1 ad }
878 1.23 simonb if (e->track > CD_MAX_TRACK) {
879 1.23 simonb /* lead-out area -- print block */
880 1.23 simonb printf(" - %6d - lead-out\n", block);
881 1.1 ad return;
882 1.1 ad }
883 1.1 ad if (msf)
884 1.1 ad next = msf2lba(e[1].addr.msf.minute, e[1].addr.msf.second,
885 1.1 ad e[1].addr.msf.frame);
886 1.1 ad else
887 1.7 ad next = e[1].addr.lba;
888 1.1 ad len = next - block;
889 1.1 ad lba2msf(len, &m, &s, &f);
890 1.1 ad
891 1.1 ad /* Print duration, block, length, type */
892 1.23 simonb printf("%2d:%02d.%02d %6d %6d %8s\n", m, s, f, block, len,
893 1.1 ad (e->control & 4) ? "data" : "audio");
894 1.1 ad }
895 1.1 ad
896 1.15 ad int
897 1.15 ad play_track(int tstart, int istart, int tend, int iend)
898 1.1 ad {
899 1.1 ad struct ioc_play_track t;
900 1.15 ad int rv;
901 1.1 ad
902 1.1 ad t.start_track = tstart;
903 1.1 ad t.start_index = istart;
904 1.1 ad t.end_track = tend;
905 1.1 ad t.end_index = iend;
906 1.1 ad
907 1.15 ad if ((rv = ioctl(fd, CDIOCPLAYTRACKS, &t)) < 0)
908 1.15 ad warn("ioctl(CDIOCPLAYTRACKS)");
909 1.15 ad return (rv);
910 1.1 ad }
911 1.1 ad
912 1.15 ad int
913 1.15 ad play_blocks(int blk, int len)
914 1.1 ad {
915 1.1 ad struct ioc_play_blocks t;
916 1.15 ad int rv;
917 1.1 ad
918 1.1 ad t.blk = blk;
919 1.1 ad t.len = len;
920 1.1 ad
921 1.15 ad if ((rv = ioctl(fd, CDIOCPLAYBLOCKS, &t)) < 0)
922 1.15 ad warn("ioctl(CDIOCPLAYBLOCKS");
923 1.15 ad return (rv);
924 1.1 ad }
925 1.1 ad
926 1.15 ad int
927 1.15 ad setvol(int left, int right)
928 1.1 ad {
929 1.1 ad struct ioc_vol v;
930 1.15 ad int rv;
931 1.1 ad
932 1.1 ad v.vol[0] = left;
933 1.1 ad v.vol[1] = right;
934 1.1 ad v.vol[2] = 0;
935 1.1 ad v.vol[3] = 0;
936 1.1 ad
937 1.15 ad if ((rv = ioctl(fd, CDIOCSETVOL, &v)) < 0)
938 1.15 ad warn("ioctl(CDIOCSETVOL)");
939 1.15 ad return (rv);
940 1.1 ad }
941 1.1 ad
942 1.15 ad int
943 1.15 ad read_toc_entrys(int len)
944 1.1 ad {
945 1.1 ad struct ioc_read_toc_entry t;
946 1.15 ad int rv;
947 1.1 ad
948 1.1 ad t.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
949 1.1 ad t.starting_track = 0;
950 1.1 ad t.data_len = len;
951 1.1 ad t.data = toc_buffer;
952 1.1 ad
953 1.15 ad if ((rv = ioctl(fd, CDIOREADTOCENTRYS, &t)) < 0)
954 1.15 ad warn("ioctl(CDIOREADTOCENTRYS)");
955 1.15 ad return (rv);
956 1.1 ad }
957 1.1 ad
958 1.15 ad int
959 1.15 ad play_msf(int start_m, int start_s, int start_f, int end_m, int end_s,
960 1.15 ad int end_f)
961 1.1 ad {
962 1.1 ad struct ioc_play_msf a;
963 1.15 ad int rv;
964 1.1 ad
965 1.1 ad a.start_m = start_m;
966 1.1 ad a.start_s = start_s;
967 1.1 ad a.start_f = start_f;
968 1.1 ad a.end_m = end_m;
969 1.1 ad a.end_s = end_s;
970 1.1 ad a.end_f = end_f;
971 1.1 ad
972 1.15 ad if ((rv = ioctl(fd, CDIOCPLAYMSF, &a)) < 0)
973 1.15 ad warn("ioctl(CDIOREADTOCENTRYS)");
974 1.15 ad return (rv);
975 1.1 ad }
976 1.1 ad
977 1.15 ad int
978 1.16 ad get_status(int *trk, int *idx, int *min, int *sec, int *frame)
979 1.1 ad {
980 1.1 ad struct ioc_read_subchannel s;
981 1.1 ad struct cd_sub_channel_info data;
982 1.15 ad u_int mm, ss, ff;
983 1.15 ad int rv;
984 1.1 ad
985 1.1 ad bzero(&s, sizeof(s));
986 1.1 ad s.data = &data;
987 1.1 ad s.data_len = sizeof(data);
988 1.1 ad s.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
989 1.1 ad s.data_format = CD_CURRENT_POSITION;
990 1.1 ad
991 1.15 ad if ((rv = ioctl(fd, CDIOCREADSUBCHANNEL, &s)) < 0) {
992 1.15 ad warn("ioctl(CDIOCREADSUBCHANNEL)");
993 1.15 ad return (rv);
994 1.15 ad }
995 1.1 ad
996 1.1 ad *trk = s.data->what.position.track_number;
997 1.16 ad *idx = s.data->what.position.index_number;
998 1.1 ad if (msf) {
999 1.1 ad *min = s.data->what.position.reladdr.msf.minute;
1000 1.1 ad *sec = s.data->what.position.reladdr.msf.second;
1001 1.1 ad *frame = s.data->what.position.reladdr.msf.frame;
1002 1.1 ad } else {
1003 1.15 ad lba2msf(be32toh(s.data->what.position.reladdr.lba), &mm,
1004 1.15 ad &ss, &ff);
1005 1.1 ad *min = mm;
1006 1.1 ad *sec = ss;
1007 1.1 ad *frame = ff;
1008 1.1 ad }
1009 1.1 ad
1010 1.3 ad return (s.data->header.audio_status);
1011 1.1 ad }
1012 1.1 ad
1013 1.15 ad const char *
1014 1.15 ad prompt(void)
1015 1.1 ad {
1016 1.15 ad
1017 1.4 msaitoh return ("cdplay> ");
1018 1.1 ad }
1019 1.1 ad
1020 1.15 ad const char *
1021 1.15 ad parse(char *buf, int *cmd)
1022 1.1 ad {
1023 1.15 ad const struct cmdtab *c, *mc;
1024 1.3 ad char *p, *q;
1025 1.1 ad int len;
1026 1.1 ad
1027 1.1 ad for (p = buf; isspace(*p); p++)
1028 1.1 ad continue;
1029 1.1 ad
1030 1.1 ad if (isdigit(*p) || (p[0] == '#' && isdigit(p[1]))) {
1031 1.1 ad *cmd = CMD_PLAY;
1032 1.1 ad return (p);
1033 1.1 ad }
1034 1.3 ad
1035 1.15 ad for (buf = p; *p != '\0' && !isspace(*p); p++)
1036 1.1 ad continue;
1037 1.1 ad
1038 1.15 ad if ((len = p - buf) == 0)
1039 1.1 ad return (0);
1040 1.1 ad
1041 1.15 ad if (*p != '\0') { /* It must be a spacing character! */
1042 1.1 ad *p++ = 0;
1043 1.15 ad for (q = p; *q != '\0' && *q != '\n' && *q != '\r'; q++)
1044 1.1 ad continue;
1045 1.1 ad *q = 0;
1046 1.1 ad }
1047 1.3 ad
1048 1.1 ad *cmd = -1;
1049 1.3 ad
1050 1.15 ad mc = cmdtab + sizeof(cmdtab) / sizeof(cmdtab[0]);
1051 1.15 ad for (c = cmdtab; c < mc; c++) {
1052 1.1 ad /* Is it an exact match? */
1053 1.15 ad if (strcasecmp(buf, c->name) == 0) {
1054 1.1 ad *cmd = c->command;
1055 1.1 ad break;
1056 1.1 ad }
1057 1.1 ad /* Try short hand forms then... */
1058 1.15 ad if (len >= c->min && strncasecmp(buf, c->name, len) == 0) {
1059 1.1 ad if (*cmd != -1 && *cmd != c->command) {
1060 1.1 ad warnx("ambiguous command");
1061 1.1 ad return (0);
1062 1.1 ad }
1063 1.1 ad *cmd = c->command;
1064 1.1 ad }
1065 1.1 ad }
1066 1.1 ad
1067 1.1 ad if (*cmd == -1) {
1068 1.1 ad warnx("invalid command, enter ``help'' for commands");
1069 1.1 ad return (0);
1070 1.1 ad }
1071 1.3 ad
1072 1.1 ad while (isspace(*p))
1073 1.1 ad p++;
1074 1.3 ad return (p);
1075 1.1 ad }
1076 1.1 ad
1077 1.15 ad int
1078 1.15 ad opencd(void)
1079 1.1 ad {
1080 1.1 ad char devbuf[80];
1081 1.1 ad
1082 1.1 ad if (fd > -1)
1083 1.1 ad return (1);
1084 1.1 ad
1085 1.11 lukem fd = opendisk(cdname, O_RDONLY, devbuf, sizeof(devbuf), 0);
1086 1.1 ad if (fd < 0) {
1087 1.1 ad if (errno == ENXIO) {
1088 1.15 ad /*
1089 1.15 ad * ENXIO has an overloaded meaning here. The
1090 1.15 ad * original "Device not configured" should be
1091 1.15 ad * interpreted as "No disc in drive %s".
1092 1.15 ad */
1093 1.1 ad warnx("no disc in drive %s", devbuf);
1094 1.1 ad return (0);
1095 1.1 ad }
1096 1.15 ad err(EXIT_FAILURE, "%s", devbuf);
1097 1.1 ad }
1098 1.3 ad
1099 1.1 ad return (1);
1100 1.22 is }
1101 1.22 is
1102 1.22 is void
1103 1.22 is toc2msf(u_int i, u_int *m, u_int *s, u_int *f)
1104 1.22 is {
1105 1.22 is struct cd_toc_entry *ctep;
1106 1.22 is
1107 1.22 is assert(i >= 0);
1108 1.23 simonb assert(i <= CD_MAX_TRACK);
1109 1.22 is
1110 1.22 is ctep = &toc_buffer[i];
1111 1.22 is
1112 1.22 is if (msf) {
1113 1.22 is *m = ctep->addr.msf.minute;
1114 1.22 is *s = ctep->addr.msf.second;
1115 1.22 is *f = ctep->addr.msf.frame;
1116 1.22 is } else {
1117 1.22 is lba2msf(be32toh(ctep->addr.lba), m, s, f);
1118 1.22 is }
1119 1.22 is }
1120 1.22 is
1121 1.22 is int
1122 1.22 is toc2lba(u_int i)
1123 1.22 is {
1124 1.22 is struct cd_toc_entry *ctep;
1125 1.22 is
1126 1.22 is assert(i > 0);
1127 1.23 simonb assert(i <= CD_MAX_TRACK);
1128 1.22 is
1129 1.22 is ctep = &toc_buffer[i-1];
1130 1.22 is
1131 1.22 is if (msf) {
1132 1.22 is return msf2lba(
1133 1.22 is ctep->addr.msf.minute,
1134 1.22 is ctep->addr.msf.second,
1135 1.22 is ctep->addr.msf.frame);
1136 1.22 is } else {
1137 1.22 is return be32toh(ctep->addr.lba);
1138 1.22 is }
1139 1.22 is }
1140 1.22 is
1141 1.22 is void
1142 1.22 is addmsf(u_int *m, u_int *s, u_int *f, u_int m2, u_int s2, u_int f2)
1143 1.22 is {
1144 1.22 is *f += f2;
1145 1.22 is if (*f > 75) {
1146 1.22 is *s += *f / 75;
1147 1.22 is *f %= 75;
1148 1.22 is }
1149 1.22 is
1150 1.22 is *s += s2;
1151 1.22 is if (*s > 60) {
1152 1.22 is *m += *s / 60;
1153 1.22 is *s %= 60;
1154 1.22 is }
1155 1.22 is
1156 1.22 is *m += m2;
1157 1.1 ad }
1158