/*- * Copyright (c) 1997 Antti Kaipila * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include "cdplay.h" extern int term_fd,cd_fd,errno; extern char *message,*cd_device; extern struct termios old_ttyattr; int main(int argc, char **argv) { char cmd[1]; int repetivity; fd_set rset; struct timeval stime; /* Parse arguments */ cd_device=getenv("CDROM"); if (cd_device==NULL) cd_device=DEVICE; if(argc>1 && !strcmp(argv[1],"-d")) cd_device=argv[2]; else if(argc>1) print_help(); /* Initialise terminal thingys */ if((term_fd=open("/dev/tty",O_RDWR))<0) per(); if(init_term()<0) per(); /* Singnal stuff.. */ signal(SIGCONT,(sig_t) hs_cont); signal(SIGWINCH,(sig_t) hs_winch); signal(SIGTSTP, (sig_t) do_stop); /* I think this is something to do witch select() */ stime.tv_sec=1; stime.tv_usec=0; FD_ZERO(&rset); FD_SET(STDIN_FILENO,&rset); message="initialising cd"; print_head(); print_tail(); init_cd(); message="welcome!"; while(1){ if(select(1,&rset,(fd_set *) 0,(fd_set *) 0,&stime)>0) { getcommand(&cmd[0], &repetivity); if(cmd[0]=='p'){message="play";play_track(1);} else if(cmd[0]=='F'){message="next";next();} else if(cmd[0]=='R'){message="prev";prev();} else if(cmd[0]=='f'){message="ff";ff(repetivity);} else if(cmd[0]=='r'){message="rew";rew(repetivity);} else if(cmd[0]=='s'){message="stop";stop();} else if(cmd[0]=='E'){message="eject";eject();} else if(cmd[0]==' ') cdsoftpause(); else if(cmd[0]=='+'){message="more vol";more_vol();} else if(cmd[0]=='-'){message="less vol";less_vol();} else if(cmd[0]=='q'){message="bye!";do_exit(0);} else if(cmd[0]=='P') cdpause(); } update_cdinfo(); move_up(5); print_tail(); FD_ZERO(&rset); FD_SET(STDIN_FILENO,&rset); } exit(0); } void getcommand(char *command, int *repetivity) { static char stored_command; int n; fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); if (stored_command) { *command=stored_command; } else { n=read(STDIN_FILENO,command,1); if (n <= 0) { *command='\0'; *repetivity=0; return; } } *repetivity=1; while (read(STDIN_FILENO, &stored_command, 1) > 0) { if (stored_command == *command) { (*repetivity)++; } else { return; } } stored_command='\0'; return; } void hs_cont(void) { init_term(); print_head(); print_tail(); } void hs_winch(void) { check_ws(); } void print_help(void) { printf("cdplay %s\n",VERSION); printf("Usage: cdplay [-d device] [-h]\n"); printf(" -d device Set desired CD-ROM device [%s]\n",DEVICE); printf(" -h Print this help message\n"); exit(1); } void do_stop(void) { if(tcsetattr(term_fd,TCSAFLUSH,&old_ttyattr)<0) per(); if(kill(getpid(), SIGSTOP)<0) per(); } void do_exit(int n) { if(tcsetattr(term_fd,TCSAFLUSH,&old_ttyattr)<0) per(); exit(n); } void die(char *msg) { fprintf(stderr,"cdplay: %s\n",msg); exit(1); } void per(void) { message=strerror(errno); }