?login_element?

Subversion Repositories NedoOS

Rev

Rev 390 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <signal.h>
  6.  
  7. #include "psg.h"
  8. #include "net.h"
  9. #include "play.h"
  10. #include "args.h"
  11. #include "global.h"
  12. #include "ififo.h"
  13.  
  14.  
  15.  
  16. struct psg_file   * psg;
  17. struct frame_list * frames;
  18. int sock;
  19. int sock_set;
  20.  
  21. void signal_handler(int);
  22.  
  23.  
  24. int main(int argc, char ** argv)
  25. {
  26.         psg    = NULL;
  27.         frames = NULL;
  28.  
  29.         sock     = (-1);
  30.         sock_set =   0;
  31.  
  32.        
  33.  
  34.  
  35.  
  36.         // parse arguments
  37.         if( argc<3 )
  38.         {
  39. ERRARGS:        fprintf(stderr,"usage: psgplay <ZX host address> <filename.psg> {options}\n");
  40.                 fprintf(stderr," --prebuf N : how many frames to send ahead of time (default is 100 (2 seconds), 0 means as many as possible)\n");
  41.                 fprintf(stderr," --syncchk  : instructs the program to send SYNCREQ and check SYNCRPLY packets\n");
  42.                 fprintf(stderr," --framechk : instructs the program to check FRAMESYNC packets\n");
  43.                 exit(1);
  44.         }
  45.  
  46.         init_global();
  47.  
  48.         if( argc>3 && !parse_args(3,argc,argv) ) goto ERRARGS;
  49.         if( g.prebuf < 0 ) goto ERRARGS;
  50.  
  51.  
  52. #ifdef DEBUG
  53. printf("DEBUG: %s, prebuf=%d, testsync=%d\n",__PRETTY_FUNCTION__,g.prebuf,g.syncchk);
  54. #endif
  55.  
  56.  
  57.  
  58.         // load PSG file
  59.         psg = load_psg_file(argv[2]);
  60.         //
  61.         frames = build_psg_frames(psg);
  62.  
  63.  
  64.  
  65.         // init network (required for nedoVindOvS)
  66.         net_init();
  67.  
  68.  
  69.         // resolve address
  70.         struct in_addr a = net_resolve(argv[1]);
  71.  
  72.         // connect to the AY server
  73.         sock = net_connect(a); 
  74.         sock_set = 1;
  75.  
  76.         // set signal handler that shuts up connection when process is terminated intentionally
  77.  
  78. #ifndef _WIN32
  79.         signal(SIGHUP,  &signal_handler);
  80.         signal(SIGQUIT, &signal_handler);
  81. #endif
  82.         signal(SIGINT,  &signal_handler);
  83.         signal(SIGABRT, &signal_handler);
  84.         signal(SIGTERM, &signal_handler);
  85.  
  86.  
  87.         // play it!
  88.         ififo_init();
  89.         play_tune(sock,frames);
  90.         ififo_free();
  91.  
  92.  
  93.         net_disconnect(sock);
  94.  
  95.  
  96.  
  97.  
  98.         free_psg_frames(frames);
  99.         free_psg_file(psg);
  100.        
  101.         net_dispose();
  102.  
  103.         return 0;
  104. }
  105.  
  106.  
  107.  
  108. void signal_handler(int num)
  109. {
  110.         if( sock_set )
  111.         { // try shut up remote AY by sending lots of ZX<< SHUTUP
  112.                 uint8_t a[100]; // must be greater than any other packet size
  113.                 memset(a,0,sizeof(a));
  114.                 send(sock, (void*)&a, sizeof(a), 0);
  115.  
  116.                 net_disconnect(sock);
  117.         }
  118.  
  119.         if( frames ) free_psg_frames(frames);
  120.  
  121.         if( psg ) free_psg_file(psg);
  122.  
  123.         net_dispose();
  124.         exit(1);
  125. }
  126.  
  127.