1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
|
#define load() /* nothing */
/*
* @(#)hardware.c 2.00 12/18/97
*
* Get information about a CD.
*/
#ifdef BOZO
static char *ident = "@(#)hardware.c 2.00 12/18/97";
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#ifdef __GLIBC__ /* glibc2 compatibility */
#include <sys/ustat.h>
#endif
# ifdef linux
# include <linux/cdrom.h>
# else
# include <sundev/srreg.h>
# endif /* linux */
#include "gcd.h"
struct cdinfo * read_toc(void);
void play_chunk( int start, int end );
extern struct play *playlist;
extern struct cdinfo thiscd, *cd;
int cd_fd = -1;
/*
* The minimum volume setting for the Sun and DEC CD-ROMs is 128 but for other
* machines this might not be the case.
*/
int min_volume = 128;
int max_volume = 255;
# ifdef linux
const char *cd_device = "/dev/cdrom\0";
# else
const char *cd_device = "/dev/rsr0\0";
# endif
extern int cur_track, cur_index, cur_lasttrack, cur_firsttrack, cur_pos_abs,
cur_frame, cur_pos_rel, cur_tracklen, cur_cdlen, cur_ntracks,
cur_nsections, cur_cdmode, cur_listno, cur_stopmode, exit_on_eject,
cur_balance;
extern char *cur_artist, *cur_cdname, *cur_trackname;
extern char cur_contd, cur_avoid;
/*
* read_toc()
*
* Read the table of contents from the CD. Return a pointer to a cdinfo
* struct containing the relevant information (minus artist/cdname/etc.)
* This is a static struct. Returns NULL if there was an error.
*
* XXX allocates one trackinfo too many.
*/
struct cdinfo *read_toc(void)
{
struct playlist *l;
struct cdrom_tochdr hdr;
struct cdrom_tocentry entry;
int i, pos;
if (cd_fd < 0)
return(NULL);
if (ioctl(cd_fd, CDROMREADTOCHDR, &hdr))
{
perror("readtochdr");
return (NULL);
}
thiscd.artist[0] = thiscd.cdname[0] = '\0';
thiscd.whichdb = thiscd.otherrc = thiscd.otherdb = NULL;
thiscd.length = 0;
thiscd.autoplay = thiscd.playmode = thiscd.volume = 0;
thiscd.ntracks = hdr.cdth_trk1;
if (thiscd.lists != NULL)
{
for (l = thiscd.lists; l->name != NULL; l++)
{
free(l->name);
free(l->list);
}
free(thiscd.lists);
thiscd.lists = NULL;
}
if (thiscd.trk != NULL)
free(thiscd.trk);
thiscd.trk = malloc((thiscd.ntracks + 1) * sizeof(struct trackinfo));
if (thiscd.trk == NULL)
{
perror("malloc");
return (NULL);
}
for (i = 0; i <= thiscd.ntracks; i++)
{
if (i == thiscd.ntracks)
entry.cdte_track = CDROM_LEADOUT;
else
entry.cdte_track = i + 1;
entry.cdte_format = CDROM_MSF;
if (ioctl(cd_fd, CDROMREADTOCENTRY, &entry))
{
perror("tocentry read");
return (NULL);
}
thiscd.trk[i].data =
thiscd.trk[i].avoid = entry.cdte_ctrl & CDROM_DATA_TRACK ?
1 : 0;
thiscd.trk[i].min = entry.cdte_addr.msf.minute;
thiscd.trk[i].sec = entry.cdte_addr.msf.second;
thiscd.trk[i].length = entry.cdte_addr.msf.minute * 60 +
entry.cdte_addr.msf.second;
thiscd.trk[i].start = thiscd.trk[i].length * 75 +
entry.cdte_addr.msf.frame;
thiscd.trk[i].songname =
thiscd.trk[i].otherrc =
thiscd.trk[i].otherdb = NULL;
thiscd.trk[i].contd = 0;
thiscd.trk[i].volume = 0;
thiscd.trk[i].track = i + 1;
thiscd.trk[i].section = 0;
}
/* Now compute actual track lengths. */
pos = thiscd.trk[0].length;
for (i = 0; i < thiscd.ntracks; i++)
{
thiscd.trk[i].length = thiscd.trk[i+1].length - pos;
pos = thiscd.trk[i+1].length;
if (thiscd.trk[i].data)
thiscd.trk[i].length = (thiscd.trk[i + 1].start -
thiscd.trk[i].start) * 2;
if (thiscd.trk[i].avoid)
strcpy(thiscd.trk[i].songname, "DATA TRACK");
}
thiscd.length = thiscd.trk[thiscd.ntracks].length;
/* Compute the cddb disc id */
thiscd.cddb_id = cddb_discid(thiscd.ntracks);
return (&thiscd);
}
/*
* cd_status()
*
* Return values:
*
* 0 No CD in drive.
* 1 CD in drive.
* 2 CD has just been inserted (TOC has been read)
* 3 CD has just been removed
*
* Updates cur_track, cur_pos_rel, cur_pos_abs and other variables.
*/
int cd_status()
{
char realname[MAXPATHLEN];
static int warned = 0;
struct cdrom_subchnl sc;
int ret = 1;
if (cd_fd < 0)
{
if ((cd_fd = open(cd_device, O_RDONLY|O_NONBLOCK)) < 0)
{
if (errno == EACCES)
{
if (!warned)
{
strcpy(realname, cd_device);
fprintf(stderr,
"As root, please run\n\nchmod 666 %s\n\n%s\n", realname,
"to give yourself permission to access the CD-ROM device.");
warned++;
}
}
else if (errno != ENXIO)
{
exit_smoothly();
perror(cd_device);
if (thiscd.trk != NULL)
free(thiscd.trk);
exit(1);
}
return (0);
}
cur_cdmode = 5;
}
if (warned)
{
warned = 0;
fprintf(stderr, "Thank you.\n");
}
sc.cdsc_format = CDROM_MSF;
if (ioctl(cd_fd, CDROMSUBCHNL, &sc))
{
cur_cdmode = 5;
cur_track = -1;
cur_cdlen = cur_tracklen = 1;
cur_pos_abs = cur_pos_rel = cur_frame = 0;
if (exit_on_eject)
exit(0);
return (0);
}
if (cur_cdmode == 5) /* CD has been ejected */
{
cur_pos_rel = cur_pos_abs = cur_frame = 0;
if ((cd = read_toc()) == NULL)
{
close(cd_fd);
cd_fd = -1;
if (exit_on_eject)
exit(0);
else
return (0);
}
cur_nsections = 0;
cur_ntracks = cd->ntracks;
cur_cdlen = cd->length;
load();
cur_artist = cd->artist;
cur_cdname = cd->cdname;
cur_cdmode = 4;
ret = 2;
}
switch (sc.cdsc_audiostatus) {
case CDROM_AUDIO_PLAY:
cur_cdmode = 1;
dopos:
cur_pos_abs = sc.cdsc_absaddr.msf.minute * 60 +
sc.cdsc_absaddr.msf.second;
cur_frame = cur_pos_abs * 75 + sc.cdsc_absaddr.msf.frame;
/* Only look up the current track number if necessary. */
if (cur_track < 1 || cur_frame < cd->trk[cur_track-1].start ||
cur_frame >= (cur_track >= cur_ntracks ?
(cur_cdlen + 1) * 75 :
cd->trk[cur_track].start))
{
cur_track = 0;
while (cur_track < cur_ntracks && cur_frame >=
cd->trk[cur_track].start)
cur_track++;
}
if (cur_track >= 1 && sc.cdsc_trk > cd->trk[cur_track-1].track)
cur_track++;
cur_index = sc.cdsc_ind;
doall:
if (cur_track >= 1 && cur_track <= cur_ntracks)
{
cur_trackname = cd->trk[cur_track-1].songname;
cur_avoid = cd->trk[cur_track-1].avoid;
cur_contd = cd->trk[cur_track-1].contd;
cur_pos_rel = (cur_frame -
cd->trk[cur_track-1].start) / 75;
if (cur_pos_rel < 0)
cur_pos_rel = -cur_pos_rel;
}
/* note: workbone requires playlist == NULL always! */
if (playlist != NULL && playlist[0].start)
{
cur_pos_abs -= cd->trk[playlist[cur_listno-1].
start - 1].start / 75;
cur_pos_abs += playlist[cur_listno-1].starttime;
}
if (cur_pos_abs < 0)
cur_pos_abs = cur_frame = 0;
if (cur_track < 1)
cur_tracklen = cd->length;
else
cur_tracklen = cd->trk[cur_track-1].length;
break;
case CDROM_AUDIO_PAUSED:
if (cur_cdmode == 1 || cur_cdmode == 3)
{
cur_cdmode = 3;
goto dopos;
}
else
cur_cdmode = 4;
goto doall;
case CDROM_AUDIO_COMPLETED:
cur_cdmode = 0; /* waiting for next track. */
break;
case CDROM_AUDIO_NO_STATUS:
cur_cdmode = 4;
cur_lasttrack = cur_firsttrack = -1;
goto doall;
}
return (ret);
}
#ifdef BOZO
/*
* scale_volume(vol, max)
*
* Return a volume value suitable for passing to the CD-ROM drive. "vol"
* is a volume slider setting; "max" is the slider's maximum value.
*
* On Sun and DEC CD-ROM drives, the amount of sound coming out the jack
* increases much faster toward the top end of the volume scale than it
* does at the bottom. To make up for this, we make the volume scale look
* sort of logarithmic (actually an upside-down inverse square curve) so
* that the volume value passed to the drive changes less and less as you
* approach the maximum slider setting. The actual formula looks like
*
* (max^2 - (max - vol)^2) * (max_volume - min_volume)
* v = --------------------------------------------------- + min_volume
* max^2
*
* If your system's volume settings aren't broken in this way, something
* like the following should work:
*
* return ((vol * (max_volume - min_volume)) / max + min_volume);
*/
scale_volume(vol, max)
int vol, max;
{
return ((max * max - (max - vol) * (max - vol)) *
(max_volume - min_volume) / (max * max) + min_volume);
}
/*
* unscale_volume(cd_vol, max)
*
* Given a value between min_volume and max_volume, return the volume slider
* value needed to achieve that value.
*
* Rather than perform floating-point calculations to reverse the above
* formula, we simply do a binary search of scale_volume()'s return values.
*/
unscale_volume(cd_vol, max)
int cd_vol, max;
{
int vol, incr, scaled;
for (vol = max / 2, incr = max / 4 + 1; incr; incr /= 2)
{
scaled = scale_volume(vol, max);
if (cd_vol == scaled)
break;
if (cd_vol < scaled)
vol -= incr;
else
vol += incr;
}
if (vol < 0)
vol = 0;
else if (vol > max)
vol = max;
return (vol);
}
/*
* cd_volume(vol, bal, max)
*
* Set the volume levels. "vol" and "bal" are the volume and balance knob
* settings, respectively. "max" is the maximum value of the volume knob
* (the balance knob is assumed to always go from 0 to 20.)
*/
void
cd_volume(vol, bal, max)
int vol, bal, max;
{
int left, right;
struct cdrom_volctrl v;
/*
* Set "left" and "right" to volume-slider values accounting for the
* balance setting.
*
* XXX - the maximum volume setting is assumed to be in the 20-30 range.
*/
if (bal < 9)
right = vol - (9 - bal) * 2;
else
right = vol;
if (bal > 11)
left = vol - (bal - 11) * 2;
else
left = vol;
/* Adjust the volume to make up for the CD-ROM drive's weirdness. */
left = scale_volume(left, max);
right = scale_volume(right, max);
v.channel0 = left < 0 ? 0 : left > 255 ? 255 : left;
v.channel1 = right < 0 ? 0 : right > 255 ? 255 : right;
if (cd_fd >= 0)
(void) ioctl(cd_fd, CDROMVOLCTRL, &v);
}
#endif /* BOZO */
/*
* pause_cd()
*
* Pause the CD, if it's in play mode. If it's already paused, go back to
* play mode.
*/
void
pause_cd()
{
if (cd_fd < 0) /* do nothing if there's no CD! */
return;
switch (cur_cdmode) {
case 1: /* playing */
cur_cdmode = 3;
ioctl(cd_fd, CDROMPAUSE);
break;
case 3: /* paused */
cur_cdmode = 1;
ioctl(cd_fd, CDROMRESUME);
}
}
/*
* stop_cd()
*
* Stop the CD if it's not already stopped.
*/
void
stop_cd()
{
if (cd_fd < 0)
return;
if (cur_cdmode != 4)
{
cur_lasttrack = cur_firsttrack = -1;
cur_cdmode = 4;
ioctl(cd_fd, CDROMSTOP);
}
}
/*
* play_chunk(start, end)
*
* Play the CD from one position to another (both in frames.)
*/
void
play_chunk(start, end)
int start, end;
{
struct cdrom_msf msf;
if (cd == NULL || cd_fd < 0)
return;
end--;
if (start >= end)
start = end-1;
msf.cdmsf_min0 = start / (60*75);
msf.cdmsf_sec0 = (start % (60*75)) / 75;
msf.cdmsf_frame0 = start % 75;
msf.cdmsf_min1 = end / (60*75);
msf.cdmsf_sec1 = (end % (60*75)) / 75;
msf.cdmsf_frame1 = end % 75;
#ifdef KICKSTART
if (ioctl(cd_fd, CDROMSTART))
{
perror("CDROMSTART");
return;
}
#endif
if (ioctl(cd_fd, CDROMPLAYMSF, &msf))
{
printf("play(%d,%d)\n",start,end);
printf("msf = %d:%d:%d %d:%d:%d\n",
msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
perror("CDROMPLAYMSF");
return;
}
}
/*
* play_cd(starttrack, pos, endtrack)
*
* Start playing the CD or jump to a new position. "pos" is in seconds,
* relative to start of track.
*/
void play_cd(int start, int pos, int end)
{
if (cd == NULL || cd_fd < 0)
return;
cur_firsttrack = start;
start--;
end--;
cur_lasttrack = end;
play_chunk(cd->trk[start].start + pos * 75, end >= cur_ntracks ?
cur_cdlen * 75 : cd->trk[end].start - 1);
}
#ifdef BOZO
/*
* Set the offset into the current track and play. -1 means end of track
* (i.e., go to next track.)
*/
void
play_from_pos(pos)
int pos;
{
if (pos == -1)
if (cd)
pos = cd->trk[cur_track - 1].length - 1;
if (cur_cdmode == 1)
play_cd(cur_track, pos, playlist[cur_listno-1].end);
}
#endif
/*
* Eject the current CD, if there is one, and set the mode to 5.
*
* Returns 0 on success, 1 if the CD couldn't be ejected, or 2 if the
* CD contains a mounted filesystem.
*/
int
eject_cd()
{
struct stat stbuf;
struct ustat ust;
if (cur_cdmode == 5) /* Already ejected! */
return (0);
if (fstat(cd_fd, &stbuf) != 0)
{
perror("fstat");
return (1);
}
/* Is this a mounted filesystem? */
if (ustat(stbuf.st_rdev, &ust) == 0)
return (2);
if (ioctl(cd_fd, CDROMEJECT))
{
perror("CDEJECT");
return (1);
}
if (exit_on_eject)
exit(0);
cur_track = -1;
cur_cdlen = cur_tracklen = 1;
cur_pos_abs = cur_pos_rel = cur_frame = 0;
cur_cdmode = 5;
return (0);
}
#ifdef BOZO
/* Try to keep the CD open all the time. This is run in a subprocess. */
void
keep_cd_open()
{
int fd;
struct flock fl;
extern end;
for (fd = 0; fd < 256; fd++)
close(fd);
if (fork())
exit(0);
if ((fd = open("/tmp/cd.lock", O_RDWR | O_CREAT, 0666)) < 0)
exit(0);
fl.l_type = F_WRLCK;
fl.l_whence = 0;
fl.l_start = 0;
fl.l_len = 0;
if (fcntl(fd, F_SETLK, &fl) < 0)
exit(0);
if (open(cd_device, O_RDONLY|O_NONBLOCK) >= 0)
{
brk(&end);
pause();
}
exit(0);
}
/*
* find_trkind(track, index)
*
* Start playing at a particular track and index, optionally using a particular
* frame as a starting position. Returns a frame number near the start of the
* index mark if successful, 0 if the track/index didn't exist.
*
* This is made significantly more tedious (though probably easier to port)
* by the fact that CDROMPLAYTRKIND doesn't work as advertised. The routine
* does a binary search of the track, terminating when the interval gets to
* around 10 frames or when the next track is encountered, at which point
* it's a fair bet the index in question doesn't exist.
*/
find_trkind(track, index, start)
int track, index, start;
{
int top = 0, bottom, current, interval, ret = 0, i;
if (cd == NULL || cd_fd < 0)
return;
for (i = 0; i < cur_ntracks; i++)
if (cd->trk[i].track == track)
break;
bottom = cd->trk[i].start;
for (; i < cur_ntracks; i++)
if (cd->trk[i].track > track)
break;
top = i == cur_ntracks ? (cd->length - 1) * 75 : cd->trk[i].start;
if (start > bottom && start < top)
bottom = start;
current = (top + bottom) / 2;
interval = (top - bottom) / 4;
do {
play_chunk(current, current + 75);
if (cd_status() != 1)
return (0);
while (cur_frame < current)
if (cd_status() != 1 || cur_cdmode != 1)
return (0);
else
susleep(1);
if (cd->trk[cur_track - 1].track > track)
break;
if (cur_index >= index)
{
ret = current;
current -= interval;
}
else
current += interval;
interval /= 2;
} while (interval > 2);
return (ret);
}
/*
* Simulate usleep() using select().
*/
susleep(usec)
int usec;
{
struct timeval tv;
timerclear(&tv);
tv.tv_sec = usec / 1000000;
tv.tv_usec = usec % 1000000;
return (select(0, NULL, NULL, NULL, &tv));
}
/*
* Read the initial volume from the drive, if available. Set cur_balance to
* the balance level (0-20, 10=centered) and return the proper setting for
* the volume knob.
*
* "max" is the maximum value of the volume knob.
*/
read_initial_volume(max)
int max;
{
int left, right;
/* Suns can't read the volume; oh well */
left = right = 255;
left = unscale_volume(left, max);
right = unscale_volume(right, max);
if (left < right)
{
cur_balance = (right - left) / 2 + 11;
if (cur_balance > 20)
cur_balance = 20;
return (right);
}
else if (left == right)
{
cur_balance = 10;
return (left);
}
else
{
cur_balance = (right - left) / 2 + 9;
if (cur_balance < 0)
cur_balance = 0;
return (left);
}
}
#endif
|