cdplay.c revision 1.29 1 1.29 dsl /* $NetBSD: cdplay.c,v 1.29 2004/10/30 17:08:12 dsl 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.29 dsl __RCSID("$NetBSD: cdplay.c,v 1.29 2004/10/30 17:08:12 dsl 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
203 1.1 ad if (argc > 0) {
204 1.16 ad interactive = 0;
205 1.15 ad for (p = buf; argc-- > 0; argv++) {
206 1.1 ad len = strlen(*argv);
207 1.1 ad
208 1.1 ad if (p + len >= buf + sizeof(buf) - 1)
209 1.1 ad usage();
210 1.1 ad if (p > buf)
211 1.1 ad *p++ = ' ';
212 1.1 ad
213 1.26 itojun strlcpy(p, *argv, sizeof(buf) - (p - buf));
214 1.1 ad p += len;
215 1.1 ad }
216 1.15 ad *p = '\0';
217 1.1 ad arg = parse(buf, &cmd);
218 1.1 ad return (run(cmd, arg));
219 1.1 ad }
220 1.15 ad
221 1.1 ad printf("Type `?' for command list\n\n");
222 1.1 ad
223 1.4 msaitoh hist = history_init();
224 1.4 msaitoh history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */
225 1.12 cgd elptr = el_init(getprogname(), stdin, stdout, stderr);
226 1.4 msaitoh el_set(elptr, EL_EDITOR, "emacs");
227 1.4 msaitoh el_set(elptr, EL_PROMPT, prompt);
228 1.4 msaitoh el_set(elptr, EL_HIST, history, hist);
229 1.4 msaitoh el_source(elptr, NULL);
230 1.4 msaitoh
231 1.16 ad sigemptyset(&sa_timer.sa_mask);
232 1.27 christos sa_timer.sa_handler = sig_timer;
233 1.16 ad sa_timer.sa_flags = SA_RESTART;
234 1.16 ad if ((rv = sigaction(SIGALRM, &sa_timer, NULL)) < 0)
235 1.16 ad err(EXIT_FAILURE, "sigaction()");
236 1.16 ad
237 1.1 ad for (;;) {
238 1.4 msaitoh line = NULL;
239 1.4 msaitoh do {
240 1.4 msaitoh if (((elline = el_gets(elptr, &scratch)) != NULL)
241 1.4 msaitoh && (scratch != 0)){
242 1.4 msaitoh history(hist, &he, H_ENTER, elline);
243 1.4 msaitoh line = strdup(elline);
244 1.4 msaitoh arg = parse(line, &cmd);
245 1.4 msaitoh } else {
246 1.4 msaitoh cmd = CMD_QUIT;
247 1.4 msaitoh fprintf(stderr, "\r\n");
248 1.4 msaitoh arg = 0;
249 1.4 msaitoh break;
250 1.4 msaitoh }
251 1.15 ad } while (arg == NULL);
252 1.4 msaitoh
253 1.1 ad if (run(cmd, arg) < 0) {
254 1.15 ad if (fd != -1)
255 1.15 ad close(fd);
256 1.1 ad fd = -1;
257 1.1 ad }
258 1.1 ad fflush(stdout);
259 1.4 msaitoh if (line != NULL)
260 1.4 msaitoh free(line);
261 1.1 ad }
262 1.15 ad
263 1.4 msaitoh el_end(elptr);
264 1.4 msaitoh history_end(hist);
265 1.15 ad exit(EXIT_SUCCESS);
266 1.15 ad /* NOTREACHED */
267 1.1 ad }
268 1.1 ad
269 1.15 ad void
270 1.15 ad usage(void)
271 1.1 ad {
272 1.1 ad
273 1.15 ad fprintf(stderr, "usage: cdplay [-f device] [command ...]\n");
274 1.15 ad exit(EXIT_FAILURE);
275 1.15 ad /* NOTREACHED */
276 1.15 ad }
277 1.15 ad
278 1.15 ad void
279 1.15 ad help(void)
280 1.15 ad {
281 1.15 ad const struct cmdtab *c, *mc;
282 1.15 ad const char *s;
283 1.15 ad int i, n;
284 1.15 ad
285 1.15 ad mc = cmdtab + sizeof(cmdtab) / sizeof(cmdtab[0]);
286 1.15 ad for (c = cmdtab; c < mc; c++) {
287 1.15 ad for (i = c->min, s = c->name; *s != '\0'; s++, i--) {
288 1.29 dsl n = (i > 0 ? toupper((unsigned char)*s) : *s);
289 1.15 ad putchar(n);
290 1.5 ad }
291 1.15 ad if (c->args != NULL)
292 1.15 ad printf(" %s", c->args);
293 1.15 ad putchar('\n');
294 1.15 ad }
295 1.15 ad printf(
296 1.15 ad "\nThe word \"play\" is not required for the play commands.\n"
297 1.15 ad "The plain target address is taken as a synonym for play.\n");
298 1.15 ad }
299 1.15 ad
300 1.15 ad int
301 1.15 ad run(int cmd, const char *arg)
302 1.15 ad {
303 1.15 ad int l, r, rv;
304 1.15 ad
305 1.15 ad if (cmd == CMD_QUIT) {
306 1.16 ad close(fd);
307 1.15 ad exit(EXIT_SUCCESS);
308 1.15 ad /* NOTREACHED */
309 1.5 ad }
310 1.15 ad
311 1.5 ad if (fd < 0 && !opencd())
312 1.5 ad return (0);
313 1.15 ad
314 1.5 ad switch (cmd) {
315 1.1 ad case CMD_INFO:
316 1.15 ad rv = info(arg);
317 1.15 ad break;
318 1.1 ad
319 1.1 ad case CMD_STATUS:
320 1.15 ad rv = print_status(arg);
321 1.15 ad break;
322 1.1 ad
323 1.1 ad case CMD_PAUSE:
324 1.15 ad if ((rv = ioctl(fd, CDIOCPAUSE)) < 0)
325 1.15 ad warn("ioctl(CDIOCPAUSE)");
326 1.15 ad break;
327 1.1 ad
328 1.1 ad case CMD_RESUME:
329 1.15 ad if ((rv = ioctl(fd, CDIOCRESUME)) < 0)
330 1.15 ad warn("ioctl(CDIOCRESUME)");
331 1.15 ad break;
332 1.1 ad
333 1.1 ad case CMD_STOP:
334 1.15 ad if ((rv = ioctl(fd, CDIOCSTOP)) < 0)
335 1.15 ad warn("ioctl(CDIOCSTOP)");
336 1.15 ad if (ioctl(fd, CDIOCALLOW) < 0)
337 1.15 ad warn("ioctl(CDIOCALLOW)");
338 1.15 ad break;
339 1.1 ad
340 1.1 ad case CMD_RESET:
341 1.15 ad if ((rv = ioctl(fd, CDIOCRESET)) >= 0) {
342 1.15 ad close(fd);
343 1.15 ad fd = -1;
344 1.15 ad } else
345 1.15 ad warn("ioctl(CDIOCRESET)");
346 1.1 ad return (0);
347 1.1 ad
348 1.1 ad case CMD_EJECT:
349 1.16 ad if (shuffle)
350 1.16 ad run(CMD_SHUFFLE, NULL);
351 1.15 ad if (ioctl(fd, CDIOCALLOW) < 0)
352 1.15 ad warn("ioctl(CDIOCALLOW)");
353 1.15 ad if ((rv = ioctl(fd, CDIOCEJECT)) < 0)
354 1.15 ad warn("ioctl(CDIOCEJECT)");
355 1.15 ad break;
356 1.1 ad
357 1.1 ad case CMD_CLOSE:
358 1.3 ad ioctl(fd, CDIOCALLOW);
359 1.15 ad if ((rv = ioctl(fd, CDIOCCLOSE)) >= 0) {
360 1.15 ad close(fd);
361 1.15 ad fd = -1;
362 1.15 ad } else
363 1.15 ad warn("ioctl(CDIOCCLOSE)");
364 1.15 ad break;
365 1.1 ad
366 1.1 ad case CMD_PLAY:
367 1.29 dsl while (isspace((unsigned char)*arg))
368 1.1 ad arg++;
369 1.16 ad rv = play(arg, 1);
370 1.15 ad break;
371 1.1 ad
372 1.13 gmcgarry case CMD_PREV:
373 1.16 ad rv = skip(-1, 1);
374 1.15 ad break;
375 1.13 gmcgarry
376 1.13 gmcgarry case CMD_NEXT:
377 1.16 ad rv = skip(1, 1);
378 1.16 ad break;
379 1.16 ad
380 1.16 ad case CMD_SHUFFLE:
381 1.16 ad if (interactive == 0)
382 1.16 ad errx(EXIT_FAILURE,
383 1.16 ad "`shuffle' valid only in interactive mode");
384 1.16 ad if (shuffle == 0) {
385 1.16 ad itv_timer.it_interval.tv_sec = 1;
386 1.16 ad itv_timer.it_interval.tv_usec = 0;
387 1.16 ad itv_timer.it_value.tv_sec = 1;
388 1.16 ad itv_timer.it_value.tv_usec = 0;
389 1.16 ad if (setitimer(ITIMER_REAL, &itv_timer, NULL) == 0) {
390 1.16 ad shuffle = 1;
391 1.16 ad skip(0, 1);
392 1.16 ad }
393 1.16 ad } else {
394 1.16 ad itv_timer.it_interval.tv_sec = 0;
395 1.16 ad itv_timer.it_interval.tv_usec = 0;
396 1.16 ad itv_timer.it_value.tv_sec = 0;
397 1.16 ad itv_timer.it_value.tv_usec = 0;
398 1.16 ad if (setitimer(ITIMER_REAL, &itv_timer, NULL) == 0)
399 1.16 ad shuffle = 0;
400 1.16 ad }
401 1.16 ad printf("shuffle play:\t%s\n", shuffle ? "on" : "off");
402 1.16 ad rv = 0;
403 1.16 ad break;
404 1.16 ad
405 1.16 ad case CMD_SKIP:
406 1.16 ad if (!interactive)
407 1.16 ad errx(EXIT_FAILURE,
408 1.16 ad "`skip' valid only in interactive mode");
409 1.16 ad if (!shuffle)
410 1.16 ad warnx("`skip' valid only in shuffle mode");
411 1.16 ad else
412 1.16 ad skip(0, 1);
413 1.15 ad break;
414 1.15 ad
415 1.1 ad case CMD_SET:
416 1.15 ad if (strcasecmp(arg, "msf") == 0)
417 1.1 ad msf = 1;
418 1.15 ad else if (strcasecmp(arg, "lba") == 0)
419 1.5 ad msf = 0;
420 1.1 ad else
421 1.5 ad warnx("invalid command arguments");
422 1.15 ad break;
423 1.1 ad
424 1.1 ad case CMD_VOLUME:
425 1.15 ad if (strncasecmp(arg, "left", strlen(arg)) == 0)
426 1.15 ad rv = ioctl(fd, CDIOCSETLEFT);
427 1.15 ad else if (strncasecmp(arg, "right", strlen(arg)) == 0)
428 1.15 ad rv = ioctl(fd, CDIOCSETRIGHT);
429 1.15 ad else if (strncasecmp(arg, "mono", strlen(arg)) == 0)
430 1.15 ad rv = ioctl(fd, CDIOCSETMONO);
431 1.15 ad else if (strncasecmp(arg, "stereo", strlen(arg)) == 0)
432 1.15 ad rv = ioctl(fd, CDIOCSETSTEREO);
433 1.15 ad else if (strncasecmp(arg, "mute", strlen(arg)) == 0)
434 1.15 ad rv = ioctl(fd, CDIOCSETMUTE);
435 1.15 ad else {
436 1.15 ad rv = 0;
437 1.15 ad if (sscanf(arg, "%d %d", &l, &r) != 2) {
438 1.15 ad if (sscanf(arg, "%d", &l) == 1)
439 1.15 ad r = l;
440 1.15 ad else {
441 1.15 ad warnx("invalid command arguments");
442 1.15 ad break;
443 1.15 ad }
444 1.9 abs }
445 1.15 ad rv = setvol(l, r);
446 1.1 ad }
447 1.15 ad break;
448 1.15 ad
449 1.1 ad case CMD_HELP:
450 1.1 ad default:
451 1.1 ad help();
452 1.15 ad rv = 0;
453 1.15 ad break;
454 1.15 ad }
455 1.1 ad
456 1.15 ad return (rv);
457 1.1 ad }
458 1.1 ad
459 1.15 ad int
460 1.16 ad play(const char *arg, int fromuser)
461 1.1 ad {
462 1.22 is int rv, n, start, end, istart, iend, blk, len, relend;
463 1.15 ad u_int tr1, tr2, m1, m2, s1, s2, f1, f2, tm, ts, tf;
464 1.1 ad struct ioc_toc_header h;
465 1.15 ad
466 1.16 ad if (shuffle && fromuser) {
467 1.17 ad warnx("`play' not valid in shuffle mode");
468 1.16 ad return (0);
469 1.16 ad }
470 1.16 ad
471 1.15 ad if ((rv = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
472 1.15 ad warn("ioctl(CDIOREADTOCHEADER)");
473 1.15 ad return (rv);
474 1.15 ad }
475 1.1 ad
476 1.2 ad end = 0;
477 1.2 ad istart = iend = 1;
478 1.1 ad n = h.ending_track - h.starting_track + 1;
479 1.15 ad rv = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
480 1.15 ad if (rv < 0)
481 1.15 ad return (rv);
482 1.1 ad
483 1.15 ad if (arg == NULL || *arg == '\0') {
484 1.1 ad /* Play the whole disc */
485 1.16 ad return (play_track(h.starting_track, 1, h.ending_track, 99));
486 1.1 ad }
487 1.15 ad
488 1.15 ad if (strchr(arg, '#') != NULL) {
489 1.1 ad /* Play block #blk [ len ] */
490 1.3 ad len = 0;
491 1.1 ad
492 1.1 ad if (2 != sscanf(arg, "#%d%d", &blk, &len) &&
493 1.1 ad 1 != sscanf(arg, "#%d", &blk))
494 1.1 ad goto Clean_up;
495 1.1 ad
496 1.1 ad if (len == 0) {
497 1.22 is len = toc2lba(n);
498 1.1 ad }
499 1.3 ad return (play_blocks(blk, len));
500 1.1 ad }
501 1.15 ad
502 1.15 ad if (strchr(arg, ':') != NULL) {
503 1.1 ad /*
504 1.1 ad * Play MSF m1:s1 [ .f1 ] [ m2:s2 [ .f2 ] ]
505 1.1 ad *
506 1.1 ad * Will now also undestand timed addresses relative
507 1.1 ad * to the beginning of a track in the form...
508 1.1 ad *
509 1.1 ad * tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]
510 1.1 ad */
511 1.22 is relend = 1;
512 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
513 1.15 ad if (8 == sscanf(arg, "%d %d:%d.%d %d %d:%d.%d", &tr1, &m1,
514 1.3 ad &s1, &f1, &tr2, &m2, &s2, &f2))
515 1.1 ad goto Play_Relative_Addresses;
516 1.1 ad
517 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
518 1.15 ad if (7 == sscanf(arg, "%d %d:%d %d %d:%d.%d", &tr1, &m1, &s1,
519 1.3 ad &tr2, &m2, &s2, &f2))
520 1.1 ad goto Play_Relative_Addresses;
521 1.1 ad
522 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
523 1.15 ad if (7 == sscanf(arg, "%d %d:%d.%d %d %d:%d", &tr1, &m1, &s1,
524 1.3 ad &f1, &tr2, &m2, &s2))
525 1.1 ad goto Play_Relative_Addresses;
526 1.1 ad
527 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
528 1.15 ad if (7 == sscanf(arg, "%d %d:%d.%d %d:%d.%d", &tr1, &m1, &s1,
529 1.3 ad &f1, &m2, &s2, &f2))
530 1.1 ad goto Play_Relative_Addresses;
531 1.1 ad
532 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
533 1.3 ad if (6 == sscanf(arg, "%d %d:%d.%d %d:%d", &tr1, &m1, &s1, &f1,
534 1.3 ad &m2, &s2))
535 1.1 ad goto Play_Relative_Addresses;
536 1.1 ad
537 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
538 1.15 ad if (6 == sscanf(arg, "%d %d:%d %d:%d.%d", &tr1, &m1, &s1, &m2,
539 1.3 ad &s2, &f2))
540 1.1 ad goto Play_Relative_Addresses;
541 1.1 ad
542 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
543 1.15 ad if (6 == sscanf(arg, "%d %d:%d.%d %d %d", &tr1, &m1, &s1, &f1,
544 1.3 ad &tr2, &m2))
545 1.1 ad goto Play_Relative_Addresses;
546 1.1 ad
547 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
548 1.22 is if (6 == sscanf(arg, "%d %d:%d %d %d:%d", &tr1, &m1, &s1, &tr2,
549 1.22 is &m2, &s2))
550 1.22 is goto Play_Relative_Addresses;
551 1.22 is
552 1.22 is tr2 = m2 = s2 = f2 = f1 = 0;
553 1.15 ad if (5 == sscanf(arg, "%d %d:%d %d:%d", &tr1, &m1, &s1, &m2,
554 1.3 ad &s2))
555 1.1 ad goto Play_Relative_Addresses;
556 1.1 ad
557 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
558 1.15 ad if (5 == sscanf(arg, "%d %d:%d %d %d", &tr1, &m1, &s1, &tr2,
559 1.3 ad &m2))
560 1.1 ad goto Play_Relative_Addresses;
561 1.1 ad
562 1.22 is relend=0;
563 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
564 1.15 ad if (5 == sscanf(arg, "%d %d:%d.%d %d", &tr1, &m1, &s1, &f1,
565 1.3 ad &tr2))
566 1.1 ad goto Play_Relative_Addresses;
567 1.1 ad
568 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
569 1.1 ad if (4 == sscanf(arg, "%d %d:%d %d", &tr1, &m1, &s1, &tr2))
570 1.1 ad goto Play_Relative_Addresses;
571 1.1 ad
572 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
573 1.1 ad if (4 == sscanf(arg, "%d %d:%d.%d", &tr1, &m1, &s1, &f1))
574 1.1 ad goto Play_Relative_Addresses;
575 1.1 ad
576 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
577 1.1 ad if (3 == sscanf(arg, "%d %d:%d", &tr1, &m1, &s1))
578 1.1 ad goto Play_Relative_Addresses;
579 1.1 ad
580 1.1 ad tr2 = m2 = s2 = f2 = f1 = 0;
581 1.1 ad goto Try_Absolute_Timed_Addresses;
582 1.1 ad
583 1.1 ad Play_Relative_Addresses:
584 1.1 ad if (!tr1)
585 1.1 ad tr1 = 1;
586 1.2 ad else if (tr1 > n)
587 1.2 ad tr1 = n;
588 1.1 ad
589 1.22 is toc2msf(tr1-1, &tm, &ts, &tf);
590 1.22 is addmsf(&m1, &s1, &f1, tm, ts, tf);
591 1.22 is
592 1.22 is toc2msf(tr1, &tm, &ts, &tf);
593 1.22 is
594 1.15 ad if ((m1 > tm) || ((m1 == tm) && ((s1 > ts) || ((s1 == ts) &&
595 1.2 ad (f1 > tf))))) {
596 1.20 itojun warnx("Track %d is not that long.", tr1);
597 1.1 ad return (0);
598 1.1 ad }
599 1.22 is tr1--; /* XXXXX ???? */
600 1.1 ad
601 1.1 ad
602 1.1 ad if (!tr2) {
603 1.22 is if (relend) {
604 1.1 ad tr2 = tr1;
605 1.22 is
606 1.22 is addmsf(&m2, &s2, &f2, m1, s1, f1);
607 1.1 ad } else {
608 1.1 ad tr2 = n;
609 1.22 is
610 1.22 is toc2msf(n, &m2, &s2, &f2);
611 1.1 ad }
612 1.1 ad } else {
613 1.1 ad if (tr2 > n) {
614 1.1 ad tr2 = n;
615 1.1 ad m2 = s2 = f2 = 0;
616 1.1 ad } else {
617 1.22 is if (relend)
618 1.1 ad tr2--;
619 1.22 is
620 1.22 is toc2msf(tr2, &tm, &ts, &tf);
621 1.22 is addmsf(&m2, &s2, &f2, tm, ts, tf);
622 1.1 ad }
623 1.1 ad }
624 1.1 ad
625 1.22 is toc2msf(n, &tm, &ts, &tf);
626 1.2 ad
627 1.15 ad if ((tr2 < n) && ((m2 > tm) || ((m2 == tm) && ((s2 > ts) ||
628 1.2 ad ((s2 == ts) && (f2 > tf)))))) {
629 1.20 itojun warnx("The playing time of the disc is not that long.");
630 1.1 ad return (0);
631 1.1 ad }
632 1.2 ad
633 1.1 ad return (play_msf(m1, s1, f1, m2, s2, f2));
634 1.1 ad
635 1.1 ad Try_Absolute_Timed_Addresses:
636 1.21 is m2 = UINT_MAX;
637 1.21 is
638 1.1 ad if (6 != sscanf(arg, "%d:%d.%d%d:%d.%d",
639 1.1 ad &m1, &s1, &f1, &m2, &s2, &f2) &&
640 1.1 ad 5 != sscanf(arg, "%d:%d.%d%d:%d", &m1, &s1, &f1, &m2, &s2) &&
641 1.1 ad 5 != sscanf(arg, "%d:%d%d:%d.%d", &m1, &s1, &m2, &s2, &f2) &&
642 1.1 ad 3 != sscanf(arg, "%d:%d.%d", &m1, &s1, &f1) &&
643 1.1 ad 4 != sscanf(arg, "%d:%d%d:%d", &m1, &s1, &m2, &s2) &&
644 1.1 ad 2 != sscanf(arg, "%d:%d", &m1, &s1))
645 1.1 ad goto Clean_up;
646 1.1 ad
647 1.21 is if (m2 == UINT_MAX) {
648 1.1 ad if (msf) {
649 1.1 ad m2 = toc_buffer[n].addr.msf.minute;
650 1.1 ad s2 = toc_buffer[n].addr.msf.second;
651 1.1 ad f2 = toc_buffer[n].addr.msf.frame;
652 1.1 ad } else {
653 1.5 ad lba2msf(be32toh(toc_buffer[n].addr.lba),
654 1.1 ad &tm, &ts, &tf);
655 1.1 ad m2 = tm;
656 1.1 ad s2 = ts;
657 1.1 ad f2 = tf;
658 1.1 ad }
659 1.1 ad }
660 1.3 ad return (play_msf(m1, s1, f1, m2, s2, f2));
661 1.1 ad }
662 1.15 ad
663 1.1 ad /*
664 1.1 ad * Play track trk1 [ .idx1 ] [ trk2 [ .idx2 ] ]
665 1.1 ad */
666 1.1 ad if (4 != sscanf(arg, "%d.%d%d.%d", &start, &istart, &end, &iend) &&
667 1.1 ad 3 != sscanf(arg, "%d.%d%d", &start, &istart, &end) &&
668 1.1 ad 3 != sscanf(arg, "%d%d.%d", &start, &end, &iend) &&
669 1.1 ad 2 != sscanf(arg, "%d.%d", &start, &istart) &&
670 1.1 ad 2 != sscanf(arg, "%d%d", &start, &end) &&
671 1.1 ad 1 != sscanf(arg, "%d", &start))
672 1.1 ad goto Clean_up;
673 1.1 ad
674 1.1 ad if (end == 0)
675 1.1 ad end = n;
676 1.1 ad return (play_track(start, istart, end, iend));
677 1.1 ad
678 1.1 ad Clean_up:
679 1.1 ad warnx("invalid command arguments");
680 1.1 ad return (0);
681 1.13 gmcgarry }
682 1.13 gmcgarry
683 1.16 ad void
684 1.27 christos sig_timer(int sig)
685 1.16 ad {
686 1.16 ad sigset_t anymore;
687 1.16 ad
688 1.16 ad sigpending(&anymore);
689 1.16 ad if (sigismember(&anymore, SIGALRM))
690 1.16 ad return;
691 1.16 ad setitimer(ITIMER_REAL, &itv_timer, NULL);
692 1.16 ad if (fd != -1)
693 1.16 ad skip(0, 0);
694 1.16 ad }
695 1.16 ad
696 1.13 gmcgarry int
697 1.16 ad skip(int dir, int fromuser)
698 1.13 gmcgarry {
699 1.15 ad char str[16];
700 1.16 ad int rv, trk, idx, m, s, f;
701 1.13 gmcgarry struct ioc_toc_header h;
702 1.15 ad
703 1.15 ad if ((rv = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
704 1.15 ad warn("ioctl(CDIOREADTOCHEADER)");
705 1.15 ad return (rv);
706 1.15 ad }
707 1.16 ad if ((rv = get_status(&trk, &idx, &m, &s, &f)) < 0)
708 1.15 ad return (rv);
709 1.16 ad
710 1.16 ad if (dir == 0) {
711 1.16 ad if (fromuser || (rv != CD_AS_PLAY_IN_PROGRESS &&
712 1.16 ad rv != CD_AS_PLAY_PAUSED))
713 1.16 ad trk = h.starting_track +
714 1.28 itojun arc4random() % (h.ending_track - h.starting_track + 1);
715 1.16 ad else
716 1.16 ad return (0);
717 1.16 ad } else {
718 1.16 ad trk += dir;
719 1.16 ad if (trk > h.ending_track)
720 1.16 ad trk = h.starting_track;
721 1.16 ad else if(trk < h.starting_track)
722 1.16 ad trk = h.ending_track;
723 1.16 ad }
724 1.16 ad
725 1.16 ad if (shuffle)
726 1.25 itojun snprintf(str, sizeof(str), "%d %d", trk, trk);
727 1.16 ad else
728 1.25 itojun snprintf(str, sizeof(str), "%d", trk);
729 1.16 ad
730 1.16 ad return (play(str, 0));
731 1.1 ad }
732 1.1 ad
733 1.15 ad const char *
734 1.15 ad strstatus(int sts)
735 1.1 ad {
736 1.15 ad const char *str;
737 1.2 ad
738 1.1 ad switch (sts) {
739 1.15 ad case CD_AS_AUDIO_INVALID:
740 1.15 ad str = "invalid";
741 1.15 ad break;
742 1.15 ad case CD_AS_PLAY_IN_PROGRESS:
743 1.15 ad str = "playing";
744 1.15 ad break;
745 1.15 ad case CD_AS_PLAY_PAUSED:
746 1.15 ad str = "paused";
747 1.15 ad break;
748 1.15 ad case CD_AS_PLAY_COMPLETED:
749 1.15 ad str = "completed";
750 1.15 ad break;
751 1.15 ad case CD_AS_PLAY_ERROR:
752 1.15 ad str = "error";
753 1.15 ad break;
754 1.15 ad case CD_AS_NO_STATUS:
755 1.15 ad str = "not playing";
756 1.15 ad break;
757 1.1 ad default:
758 1.15 ad str = "<unknown>";
759 1.15 ad break;
760 1.1 ad }
761 1.15 ad
762 1.15 ad return (str);
763 1.1 ad }
764 1.1 ad
765 1.15 ad int
766 1.15 ad print_status(const char *arg)
767 1.1 ad {
768 1.2 ad struct cd_sub_channel_info data;
769 1.1 ad struct ioc_read_subchannel ss;
770 1.16 ad int rv, trk, idx, m, s, f;
771 1.2 ad struct ioc_vol v;
772 1.1 ad
773 1.16 ad if ((rv = get_status(&trk, &idx, &m, &s, &f)) >= 0) {
774 1.16 ad printf("audio status:\t%s\n", strstatus(rv));
775 1.15 ad printf("current track:\t%d\n", trk);
776 1.16 ad printf("current index:\t%d\n", idx);
777 1.15 ad printf("position:\t%d:%02d.%02d\n", m, s, f);
778 1.15 ad } else
779 1.15 ad printf("audio status:\tno info available\n");
780 1.15 ad
781 1.16 ad printf("shuffle play:\t%s\n", shuffle ? "on" : "off");
782 1.16 ad
783 1.15 ad bzero(&ss, sizeof(ss));
784 1.15 ad ss.data = &data;
785 1.15 ad ss.data_len = sizeof(data);
786 1.15 ad ss.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
787 1.15 ad ss.data_format = CD_MEDIA_CATALOG;
788 1.15 ad
789 1.16 ad if (ioctl(fd, CDIOCREADSUBCHANNEL, (char *)&ss) >= 0) {
790 1.15 ad printf("media catalog:\t%sactive",
791 1.15 ad ss.data->what.media_catalog.mc_valid ? "" : "in");
792 1.15 ad if (ss.data->what.media_catalog.mc_valid &&
793 1.15 ad ss.data->what.media_catalog.mc_number[0])
794 1.15 ad printf(" (%.15s)",
795 1.15 ad ss.data->what.media_catalog.mc_number);
796 1.15 ad putchar('\n');
797 1.15 ad } else
798 1.15 ad printf("media catalog:\tnone\n");
799 1.15 ad
800 1.16 ad if (ioctl(fd, CDIOCGETVOL, &v) >= 0) {
801 1.15 ad printf("left volume:\t%d\n", v.vol[0]);
802 1.15 ad printf("right volume:\t%d\n", v.vol[1]);
803 1.15 ad } else {
804 1.15 ad printf("left volume:\tnot available\n");
805 1.15 ad printf("right volume:\tnot available\n");
806 1.1 ad }
807 1.15 ad
808 1.16 ad ; return (0);
809 1.1 ad }
810 1.1 ad
811 1.15 ad int
812 1.15 ad info(const char *arg)
813 1.1 ad {
814 1.1 ad struct ioc_toc_header h;
815 1.1 ad int rc, i, n;
816 1.1 ad
817 1.1 ad if ((rc = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
818 1.15 ad warn("ioctl(CDIOREADTOCHEADER)");
819 1.1 ad return (rc);
820 1.1 ad }
821 1.1 ad
822 1.1 ad n = h.ending_track - h.starting_track + 1;
823 1.1 ad rc = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
824 1.1 ad if (rc < 0)
825 1.1 ad return (rc);
826 1.1 ad
827 1.23 simonb printf("track start duration block length type\n");
828 1.23 simonb printf("--------------------------------------------------\n");
829 1.1 ad
830 1.1 ad for (i = 0; i < n; i++) {
831 1.1 ad printf("%5d ", toc_buffer[i].track);
832 1.23 simonb print_track(toc_buffer + i);
833 1.1 ad }
834 1.24 matt printf(" - "); /* Lead-out area */
835 1.23 simonb print_track(toc_buffer + n);
836 1.1 ad return (0);
837 1.1 ad }
838 1.1 ad
839 1.15 ad void
840 1.15 ad lba2msf(u_long lba, u_int *m, u_int *s, u_int *f)
841 1.1 ad {
842 1.15 ad
843 1.1 ad lba += 150; /* block start offset */
844 1.1 ad lba &= 0xffffff; /* negative lbas use only 24 bits */
845 1.1 ad *m = lba / (60 * 75);
846 1.1 ad lba %= (60 * 75);
847 1.1 ad *s = lba / 75;
848 1.1 ad *f = lba % 75;
849 1.1 ad }
850 1.1 ad
851 1.15 ad u_int
852 1.15 ad msf2lba(u_int m, u_int s, u_int f)
853 1.1 ad {
854 1.2 ad
855 1.1 ad return (((m * 60) + s) * 75 + f) - 150;
856 1.1 ad }
857 1.1 ad
858 1.15 ad void
859 1.23 simonb print_track(struct cd_toc_entry *e)
860 1.1 ad {
861 1.1 ad int block, next, len;
862 1.15 ad u_int m, s, f;
863 1.1 ad
864 1.1 ad if (msf) {
865 1.1 ad /* Print track start */
866 1.1 ad printf("%2d:%02d.%02d ", e->addr.msf.minute,
867 1.1 ad e->addr.msf.second, e->addr.msf.frame);
868 1.1 ad
869 1.1 ad block = msf2lba(e->addr.msf.minute, e->addr.msf.second,
870 1.1 ad e->addr.msf.frame);
871 1.1 ad } else {
872 1.7 ad block = e->addr.lba;
873 1.1 ad lba2msf(block, &m, &s, &f);
874 1.1 ad /* Print track start */
875 1.1 ad printf("%2d:%02d.%02d ", m, s, f);
876 1.1 ad }
877 1.23 simonb if (e->track > CD_MAX_TRACK) {
878 1.23 simonb /* lead-out area -- print block */
879 1.23 simonb printf(" - %6d - lead-out\n", block);
880 1.1 ad return;
881 1.1 ad }
882 1.1 ad if (msf)
883 1.1 ad next = msf2lba(e[1].addr.msf.minute, e[1].addr.msf.second,
884 1.1 ad e[1].addr.msf.frame);
885 1.1 ad else
886 1.7 ad next = e[1].addr.lba;
887 1.1 ad len = next - block;
888 1.1 ad lba2msf(len, &m, &s, &f);
889 1.1 ad
890 1.1 ad /* Print duration, block, length, type */
891 1.23 simonb printf("%2d:%02d.%02d %6d %6d %8s\n", m, s, f, block, len,
892 1.1 ad (e->control & 4) ? "data" : "audio");
893 1.1 ad }
894 1.1 ad
895 1.15 ad int
896 1.15 ad play_track(int tstart, int istart, int tend, int iend)
897 1.1 ad {
898 1.1 ad struct ioc_play_track t;
899 1.15 ad int rv;
900 1.1 ad
901 1.1 ad t.start_track = tstart;
902 1.1 ad t.start_index = istart;
903 1.1 ad t.end_track = tend;
904 1.1 ad t.end_index = iend;
905 1.1 ad
906 1.15 ad if ((rv = ioctl(fd, CDIOCPLAYTRACKS, &t)) < 0)
907 1.15 ad warn("ioctl(CDIOCPLAYTRACKS)");
908 1.15 ad return (rv);
909 1.1 ad }
910 1.1 ad
911 1.15 ad int
912 1.15 ad play_blocks(int blk, int len)
913 1.1 ad {
914 1.1 ad struct ioc_play_blocks t;
915 1.15 ad int rv;
916 1.1 ad
917 1.1 ad t.blk = blk;
918 1.1 ad t.len = len;
919 1.1 ad
920 1.15 ad if ((rv = ioctl(fd, CDIOCPLAYBLOCKS, &t)) < 0)
921 1.15 ad warn("ioctl(CDIOCPLAYBLOCKS");
922 1.15 ad return (rv);
923 1.1 ad }
924 1.1 ad
925 1.15 ad int
926 1.15 ad setvol(int left, int right)
927 1.1 ad {
928 1.1 ad struct ioc_vol v;
929 1.15 ad int rv;
930 1.1 ad
931 1.1 ad v.vol[0] = left;
932 1.1 ad v.vol[1] = right;
933 1.1 ad v.vol[2] = 0;
934 1.1 ad v.vol[3] = 0;
935 1.1 ad
936 1.15 ad if ((rv = ioctl(fd, CDIOCSETVOL, &v)) < 0)
937 1.15 ad warn("ioctl(CDIOCSETVOL)");
938 1.15 ad return (rv);
939 1.1 ad }
940 1.1 ad
941 1.15 ad int
942 1.15 ad read_toc_entrys(int len)
943 1.1 ad {
944 1.1 ad struct ioc_read_toc_entry t;
945 1.15 ad int rv;
946 1.1 ad
947 1.1 ad t.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
948 1.1 ad t.starting_track = 0;
949 1.1 ad t.data_len = len;
950 1.1 ad t.data = toc_buffer;
951 1.1 ad
952 1.15 ad if ((rv = ioctl(fd, CDIOREADTOCENTRYS, &t)) < 0)
953 1.15 ad warn("ioctl(CDIOREADTOCENTRYS)");
954 1.15 ad return (rv);
955 1.1 ad }
956 1.1 ad
957 1.15 ad int
958 1.15 ad play_msf(int start_m, int start_s, int start_f, int end_m, int end_s,
959 1.15 ad int end_f)
960 1.1 ad {
961 1.1 ad struct ioc_play_msf a;
962 1.15 ad int rv;
963 1.1 ad
964 1.1 ad a.start_m = start_m;
965 1.1 ad a.start_s = start_s;
966 1.1 ad a.start_f = start_f;
967 1.1 ad a.end_m = end_m;
968 1.1 ad a.end_s = end_s;
969 1.1 ad a.end_f = end_f;
970 1.1 ad
971 1.15 ad if ((rv = ioctl(fd, CDIOCPLAYMSF, &a)) < 0)
972 1.15 ad warn("ioctl(CDIOREADTOCENTRYS)");
973 1.15 ad return (rv);
974 1.1 ad }
975 1.1 ad
976 1.15 ad int
977 1.16 ad get_status(int *trk, int *idx, int *min, int *sec, int *frame)
978 1.1 ad {
979 1.1 ad struct ioc_read_subchannel s;
980 1.1 ad struct cd_sub_channel_info data;
981 1.15 ad u_int mm, ss, ff;
982 1.15 ad int rv;
983 1.1 ad
984 1.1 ad bzero(&s, sizeof(s));
985 1.1 ad s.data = &data;
986 1.1 ad s.data_len = sizeof(data);
987 1.1 ad s.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
988 1.1 ad s.data_format = CD_CURRENT_POSITION;
989 1.1 ad
990 1.15 ad if ((rv = ioctl(fd, CDIOCREADSUBCHANNEL, &s)) < 0) {
991 1.15 ad warn("ioctl(CDIOCREADSUBCHANNEL)");
992 1.15 ad return (rv);
993 1.15 ad }
994 1.1 ad
995 1.1 ad *trk = s.data->what.position.track_number;
996 1.16 ad *idx = s.data->what.position.index_number;
997 1.1 ad if (msf) {
998 1.1 ad *min = s.data->what.position.reladdr.msf.minute;
999 1.1 ad *sec = s.data->what.position.reladdr.msf.second;
1000 1.1 ad *frame = s.data->what.position.reladdr.msf.frame;
1001 1.1 ad } else {
1002 1.15 ad lba2msf(be32toh(s.data->what.position.reladdr.lba), &mm,
1003 1.15 ad &ss, &ff);
1004 1.1 ad *min = mm;
1005 1.1 ad *sec = ss;
1006 1.1 ad *frame = ff;
1007 1.1 ad }
1008 1.1 ad
1009 1.3 ad return (s.data->header.audio_status);
1010 1.1 ad }
1011 1.1 ad
1012 1.15 ad const char *
1013 1.15 ad prompt(void)
1014 1.1 ad {
1015 1.15 ad
1016 1.4 msaitoh return ("cdplay> ");
1017 1.1 ad }
1018 1.1 ad
1019 1.15 ad const char *
1020 1.15 ad parse(char *buf, int *cmd)
1021 1.1 ad {
1022 1.15 ad const struct cmdtab *c, *mc;
1023 1.3 ad char *p, *q;
1024 1.1 ad int len;
1025 1.1 ad
1026 1.29 dsl for (p = buf; isspace((unsigned char)*p); p++)
1027 1.1 ad continue;
1028 1.1 ad
1029 1.29 dsl if (isdigit((unsigned char)*p) || (p[0] == '#' && isdigit((unsigned char)p[1]))) {
1030 1.1 ad *cmd = CMD_PLAY;
1031 1.1 ad return (p);
1032 1.1 ad }
1033 1.3 ad
1034 1.29 dsl for (buf = p; *p != '\0' && !isspace((unsigned char)*p); p++)
1035 1.1 ad continue;
1036 1.1 ad
1037 1.15 ad if ((len = p - buf) == 0)
1038 1.1 ad return (0);
1039 1.1 ad
1040 1.15 ad if (*p != '\0') { /* It must be a spacing character! */
1041 1.1 ad *p++ = 0;
1042 1.15 ad for (q = p; *q != '\0' && *q != '\n' && *q != '\r'; q++)
1043 1.1 ad continue;
1044 1.1 ad *q = 0;
1045 1.1 ad }
1046 1.3 ad
1047 1.1 ad *cmd = -1;
1048 1.3 ad
1049 1.15 ad mc = cmdtab + sizeof(cmdtab) / sizeof(cmdtab[0]);
1050 1.15 ad for (c = cmdtab; c < mc; c++) {
1051 1.1 ad /* Is it an exact match? */
1052 1.15 ad if (strcasecmp(buf, c->name) == 0) {
1053 1.1 ad *cmd = c->command;
1054 1.1 ad break;
1055 1.1 ad }
1056 1.1 ad /* Try short hand forms then... */
1057 1.15 ad if (len >= c->min && strncasecmp(buf, c->name, len) == 0) {
1058 1.1 ad if (*cmd != -1 && *cmd != c->command) {
1059 1.1 ad warnx("ambiguous command");
1060 1.1 ad return (0);
1061 1.1 ad }
1062 1.1 ad *cmd = c->command;
1063 1.1 ad }
1064 1.1 ad }
1065 1.1 ad
1066 1.1 ad if (*cmd == -1) {
1067 1.1 ad warnx("invalid command, enter ``help'' for commands");
1068 1.1 ad return (0);
1069 1.1 ad }
1070 1.3 ad
1071 1.29 dsl while (isspace((unsigned char)*p))
1072 1.1 ad p++;
1073 1.3 ad return (p);
1074 1.1 ad }
1075 1.1 ad
1076 1.15 ad int
1077 1.15 ad opencd(void)
1078 1.1 ad {
1079 1.1 ad char devbuf[80];
1080 1.1 ad
1081 1.1 ad if (fd > -1)
1082 1.1 ad return (1);
1083 1.1 ad
1084 1.11 lukem fd = opendisk(cdname, O_RDONLY, devbuf, sizeof(devbuf), 0);
1085 1.1 ad if (fd < 0) {
1086 1.1 ad if (errno == ENXIO) {
1087 1.15 ad /*
1088 1.15 ad * ENXIO has an overloaded meaning here. The
1089 1.15 ad * original "Device not configured" should be
1090 1.15 ad * interpreted as "No disc in drive %s".
1091 1.15 ad */
1092 1.1 ad warnx("no disc in drive %s", devbuf);
1093 1.1 ad return (0);
1094 1.1 ad }
1095 1.15 ad err(EXIT_FAILURE, "%s", devbuf);
1096 1.1 ad }
1097 1.3 ad
1098 1.1 ad return (1);
1099 1.22 is }
1100 1.22 is
1101 1.22 is void
1102 1.22 is toc2msf(u_int i, u_int *m, u_int *s, u_int *f)
1103 1.22 is {
1104 1.22 is struct cd_toc_entry *ctep;
1105 1.22 is
1106 1.22 is assert(i >= 0);
1107 1.23 simonb assert(i <= CD_MAX_TRACK);
1108 1.22 is
1109 1.22 is ctep = &toc_buffer[i];
1110 1.22 is
1111 1.22 is if (msf) {
1112 1.22 is *m = ctep->addr.msf.minute;
1113 1.22 is *s = ctep->addr.msf.second;
1114 1.22 is *f = ctep->addr.msf.frame;
1115 1.22 is } else {
1116 1.22 is lba2msf(be32toh(ctep->addr.lba), m, s, f);
1117 1.22 is }
1118 1.22 is }
1119 1.22 is
1120 1.22 is int
1121 1.22 is toc2lba(u_int i)
1122 1.22 is {
1123 1.22 is struct cd_toc_entry *ctep;
1124 1.22 is
1125 1.22 is assert(i > 0);
1126 1.23 simonb assert(i <= CD_MAX_TRACK);
1127 1.22 is
1128 1.22 is ctep = &toc_buffer[i-1];
1129 1.22 is
1130 1.22 is if (msf) {
1131 1.22 is return msf2lba(
1132 1.22 is ctep->addr.msf.minute,
1133 1.22 is ctep->addr.msf.second,
1134 1.22 is ctep->addr.msf.frame);
1135 1.22 is } else {
1136 1.22 is return be32toh(ctep->addr.lba);
1137 1.22 is }
1138 1.22 is }
1139 1.22 is
1140 1.22 is void
1141 1.22 is addmsf(u_int *m, u_int *s, u_int *f, u_int m2, u_int s2, u_int f2)
1142 1.22 is {
1143 1.22 is *f += f2;
1144 1.22 is if (*f > 75) {
1145 1.22 is *s += *f / 75;
1146 1.22 is *f %= 75;
1147 1.22 is }
1148 1.22 is
1149 1.22 is *s += s2;
1150 1.22 is if (*s > 60) {
1151 1.22 is *m += *s / 60;
1152 1.22 is *s %= 60;
1153 1.22 is }
1154 1.22 is
1155 1.22 is *m += m2;
1156 1.1 ad }
1157