home *** CD-ROM | disk | FTP | other *** search
/ M.u.C.S. Disc 2000 / MUCS2000.iso / sound / mp2 / src / gem / mp2event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-08  |  24.6 KB  |  1,138 lines

  1. #include <tos.h>
  2. #include <vdi.h>
  3. #include <aes.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #include "aesextra.h"
  8.  
  9. #include "mp2audio.h"
  10. #include "mp2wind.h"
  11. #include "mp2info.h"
  12.  
  13. /* Functions in this module */
  14. void main_event_loop(void);
  15. void bg_event_loop(void);
  16. void fg_event_loop(void);
  17. int ev2_loop(WINDFORM *, int, int);
  18. int checkhang(void);
  19. void toggle_object(WINDFORM *,int,int);
  20. void fg_init(WINDFORM *);
  21. int find_windform(int );
  22. int handle_message(int *);
  23. int do_dragdrop(int *, int);
  24. void update_objects(WINDFORM *,int,int, int *);
  25. int key2button(int);
  26. int do_formstuff(int);
  27. long calc_time(void);
  28. void update_time(void);
  29. void setfilename(char *);
  30.  
  31. /* global variables */
  32. int fgbg,closed_acc=0,file_was_open,looping=0,replay_pause=0;
  33. long total_time;
  34. char *app_name="MP2AUDIO";
  35.  
  36. /* global variable from mp2init.c */
  37. extern int buf_init,first_init;
  38. extern long first_buf,o_filepos;
  39.  
  40. /* global variables from mp2file.c */
  41. extern char path[512], filename[512];
  42. extern long filesize,filepos,bufferpos;
  43.  
  44. /* global variables from mp2audio.c */
  45. extern int time_slice, count_dir;
  46. extern int fd,replay,quit,file_open;
  47. extern long buffer,left;
  48. extern int acc_id,app_id;
  49. extern WINDFORM windforms[5];
  50. #ifdef STATIC_BUFFER
  51. extern char buffer_mem[(long)BLOCK_SIZE];
  52. #else
  53. extern char *buffer_mem;
  54. #endif
  55.  
  56. /* global variable from mp2info.c */
  57. extern MP2INFO mp2info;
  58.  
  59. /* Functions from mp2init.c */
  60. extern void init_replay(void);
  61. extern void continue_replay(void);
  62.  
  63. /* Functions from mp2exit.c */
  64. extern void exit_replay(void);
  65. extern void pause_replay(void);
  66.  
  67. /* Functions from mp2file.c */
  68. extern int open_file(void);
  69. extern int reopen_file(void);
  70. extern void reset_file(int fd);
  71. extern void close_file(int fd);
  72. extern void load(int q);
  73. extern int next_song(char *, char *, int);
  74.  
  75. /* Functions from mp2info.c */
  76. extern int getmp2info(int);
  77. extern void show_mp2_error(int);
  78.  
  79. #define FG 1
  80. #define BG 0
  81.  
  82. #define CLOSE_WIND 21
  83. #define SWITCH_WIND 42
  84.  
  85. #define max(a,b) ((a)>(b)?(a):(b))
  86. #define min(a,b) ((a)<(b)?(a):(b))
  87.  
  88. typedef struct { int x1,y1,x2,y2; } CORDS2;
  89.  
  90. void main_event_loop()
  91. {
  92.     fgbg=(_app?FG:BG);
  93.     
  94.     while(!quit)
  95.     {
  96.         if(closed_acc)
  97.         {
  98.             evnt_timer(1000,0);
  99. #ifdef DEBUG
  100.             form_alert(1,"[1][ACC reopened][Ok]");
  101. #endif
  102.             fg_init(&windforms[WIND_CTRL]);
  103.             if(file_was_open)
  104.                 fd=reopen_file();
  105.             closed_acc=0;
  106.         }
  107.         if(fgbg==FG)
  108.             fg_event_loop();
  109.         else if(fgbg==BG)
  110.             bg_event_loop();
  111.     }
  112.  
  113. }
  114.  
  115. /* No form window is open */
  116. void bg_event_loop()
  117. {
  118.     int x,y,kstate,key,clicks,event,state;
  119.     int pipe[8];
  120.  
  121.  
  122. #ifdef DEBUG
  123.     form_alert(1,"[1][ACC bg][Ok]");
  124. #endif
  125.     do {
  126.         event = evnt_multi( MU_MESAG | MU_TIMER,
  127.                             2, 0x1, 1,
  128.                             0, 0, 0, 0, 0,
  129.                             0, 0, 0, 0, 0,
  130.                             pipe,
  131.                             time_slice, 0,
  132.                             &x, &y, &state, &kstate, &key, &clicks );
  133.  
  134.         if (event & MU_TIMER)
  135.             if (replay)
  136.             {
  137.                 if(first_init)
  138.                     checkhang();
  139.                 load(1);
  140.                 update_time();
  141.             }
  142.  
  143.         if (event & MU_MESAG)
  144.         {
  145.             handle_message(pipe); /* no window to handle */
  146.         }
  147.  
  148.             
  149.     } while ((fgbg==BG) && !closed_acc);
  150. }
  151.  
  152. /* Form window open */
  153. void fg_event_loop()
  154. {
  155.     int x,y,kstate,key,clicks,event,state;
  156.     int pipe[8],obj_id;
  157.     int fgexit=0,tmph,th;
  158.     
  159.     wind_open(windforms[WIND_CTRL].whandle,
  160.         windforms[WIND_CTRL].wind.x,windforms[WIND_CTRL].wind.y,
  161.         windforms[WIND_CTRL].wind.w,windforms[WIND_CTRL].wind.h);
  162.     windforms[WIND_CTRL].wind_open=1;
  163.  
  164. #ifdef DEBUG
  165.     form_alert(1,"[1][ACC fg][Ok]");
  166. #endif
  167.     do {
  168.         event = evnt_multi( MU_MESAG | MU_TIMER | MU_BUTTON | MU_KEYBD,
  169.                             1, 0x3, 0x1,
  170.                             0, 0, 0, 0, 0,
  171.                             0, 0, 0, 0, 0,
  172.                             pipe,
  173.                             time_slice, 0,
  174.                             &x, &y, &state, &kstate, &key, &clicks );
  175.  
  176.         if (event & MU_TIMER)
  177.             if (replay)
  178.             {
  179.                 if(first_init)
  180.                     checkhang();
  181.                 load(1);
  182.                 update_time();
  183.             }
  184.  
  185.         if(event & MU_KEYBD)
  186.         {
  187.             obj_id=key2button(key);
  188.             if(obj_id < 0)
  189.             {
  190.                 switch(-obj_id)
  191.                 {
  192.                     case CLOSE_WIND:
  193.                         wind_get(0, WF_TOP, &pipe[3]);
  194.                         pipe[0]=WM_CLOSED;
  195.                         fgexit=handle_message(pipe);
  196.                         break;
  197.                     case SWITCH_WIND:
  198.                         wind_get(0, WF_TOP, &tmph);
  199.                         /* find handle of next open window */
  200.                         th=tmph;
  201.                         while(((th=find_windform(th)+1)%N_WINDS)!=tmph)
  202.                         {
  203.                             if(windforms[th].wind_open)
  204.                             {
  205.                                 wind_set(windforms[th].whandle,WF_TOP);
  206.                                 break;
  207.                             }
  208.                         }
  209.                     default:
  210.                         break;
  211.                 }
  212.             }
  213.             else
  214.                 do_formstuff(obj_id);
  215.         }
  216.  
  217.         if(!fgexit)
  218.         {
  219.             if (event & MU_MESAG)
  220.                 fgexit=handle_message(pipe);
  221.     
  222.             if (event & MU_BUTTON)
  223.             {
  224.                 if(replay || replay_pause)
  225.                 {
  226.                     windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_flags |= KEEPSELECT;
  227.                     windforms[WIND_CTRL].formtree[CTRL_FF].ob_flags |= KEEPSELECT;
  228.                 }
  229.                 else
  230.                 {
  231.                     windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_flags &= ~KEEPSELECT;
  232.                     windforms[WIND_CTRL].formtree[CTRL_FF].ob_flags &= ~KEEPSELECT;
  233.                 }
  234.                 if(wind_find(x,y) == windforms[WIND_CTRL].whandle)
  235.                     if(ev2_loop(&windforms[WIND_CTRL],x,y))
  236.                         event=0;
  237.             }
  238.         }
  239.         
  240.         if(_app)
  241.             quit=fgexit;
  242.         else
  243.             if(fgexit)
  244.                 fgbg=BG;
  245.  
  246.     } while ((replay || fgbg==FG) && !fgexit);
  247. }
  248.  
  249. int ev2_loop(WINDFORM *wind,int mx,int my)
  250. {
  251.     int x,y,kstate,key,clicks,event,state,org_state;
  252.     int obj_id,ev2exit,fgexit=0;
  253.     int pipe[8];
  254.     CORDS t;
  255.  
  256.     if((obj_id=objc_find(wind->formtree,CTRL_FIRST,1,mx,my))>=0)
  257.     {
  258.         if(wind->formtree[obj_id].ob_flags & SELECTABLE)
  259.         {
  260.             org_state=wind->formtree[obj_id].ob_state & SELECTED;
  261.             toggle_object(wind,obj_id,TOGGLE);
  262.  
  263.             objc_offset(wind->formtree,obj_id,&t.x,&t.y);
  264.             t.w=wind->formtree[obj_id].ob_width;
  265.             t.h=wind->formtree[obj_id].ob_height;
  266.             ev2exit=0;
  267.             do {
  268.                 event = evnt_multi( MU_MESAG | MU_TIMER | MU_BUTTON | MU_M1 | MU_M2,
  269.                                     1, 0x1, 0x0,
  270.                                     0, t.x, t.y, t.w, t.h,
  271.                                     1, t.x, t.y, t.w, t.h,
  272.                                     pipe,
  273.                                     time_slice, 0,
  274.                                     &x, &y, &state, &kstate, &key, &clicks );
  275.  
  276.                 if (event & MU_TIMER)
  277.                     if (replay)
  278.                     {
  279.                         if(first_init)
  280.                             checkhang();
  281.                         load(1);
  282.                         update_time();
  283.                     }
  284.             
  285.                 if (event & MU_MESAG)
  286.                     fgexit=handle_message(pipe);
  287.  
  288.                 if (event & MU_M1) /* Enter area */
  289.                 {
  290.                     if(org_state==(wind->formtree[obj_id].ob_state & SELECTED))
  291.                         toggle_object(wind,obj_id,TOGGLE);
  292.                 }
  293.                 if (event & MU_M2) /* Leave area */
  294.                 {
  295.                     if(org_state!=(wind->formtree[obj_id].ob_state & SELECTED))
  296.                         toggle_object(wind,obj_id,TOGGLE);
  297.                 }
  298.             
  299.                 if (event & MU_BUTTON)
  300.                 {
  301.                     if(obj_id==objc_find(wind->formtree,CTRL_FIRST,1,x,y))
  302.                     {
  303.                         if(wind->formtree[obj_id].ob_flags & KEEPSELECT)
  304.                         {
  305.                             if(org_state && (wind->formtree[obj_id].ob_state & SELECTED))
  306.                                 toggle_object(wind,obj_id,SET_NORMAL);
  307.                             if(!org_state && !(wind->formtree[obj_id].ob_state & SELECTED))
  308.                                 toggle_object(wind,obj_id,SET_SELECTED);
  309.                         }
  310.                         else if(wind->formtree[obj_id].ob_state & SELECTED)
  311.                             toggle_object(wind,obj_id,SET_NORMAL);
  312.  
  313.                         fgexit=do_formstuff(obj_id);
  314.                     }
  315.                     else
  316.                     {
  317.                         objc_change(wind->formtree,obj_id,0,wind->form.x,
  318.                             wind->form.y,wind->form.w,wind->form.h,org_state,0);
  319.                         update_objects(wind,obj_id,1,0);
  320.                     }
  321.                     ev2exit=1;
  322.                 }
  323.  
  324.         
  325.             } while (((replay || fgbg==FG) && !fgexit) && !ev2exit);
  326.             event=0;
  327.             return 1;
  328.         }
  329.     }
  330.     return 0;
  331. }        
  332.  
  333. int checkhang()
  334. {
  335.     long ptr[4];
  336.     
  337.     first_init=0;
  338.     
  339.     buffptr(ptr);
  340.  
  341. #ifdef DEBUG    
  342.     fprintf(stderr,"first_buf=%lx ptr[0]=%lx\n",first_buf,ptr[0]);
  343. #endif
  344.  
  345.     if(ptr[0]-first_buf < 512)
  346.     {
  347. #ifdef DEBUG
  348.         fprintf(stderr,"init failed, trying again!\n");
  349.         fprintf(stderr,"first_buf=%lx ptr[0]=%lx\n",first_buf,ptr[0]);
  350. #endif
  351.         Fseek(o_filepos,fd,0);
  352.         filepos = o_filepos;
  353.         init_replay();
  354.         return 1;
  355.     }
  356.     return 0;
  357. }
  358.  
  359. void toggle_object(WINDFORM *wind,int obj_id,int mode)
  360. {
  361.     if((mode==SET_NORMAL) || 
  362.         ((mode==TOGGLE) && (wind->formtree[obj_id].ob_state & SELECTED)))
  363.     {
  364.         objc_change(wind->formtree,obj_id,0,wind->form.x,
  365.             wind->form.y,wind->form.w,wind->form.h,
  366.             NORMAL,0);
  367.         wind->formtree[wind->formtree[obj_id].ob_head].ob_x--;
  368.         wind->formtree[wind->formtree[obj_id].ob_head].ob_y--;
  369.     }
  370.     else if((mode==SET_SELECTED) || 
  371.         ((mode==TOGGLE) && !(wind->formtree[obj_id].ob_state & SELECTED)))
  372.     {
  373.         objc_change(wind->formtree,obj_id,0,wind->form.x,
  374.             wind->form.y,wind->form.w,wind->form.h,
  375.             SELECTED,0);
  376.         wind->formtree[wind->formtree[obj_id].ob_head].ob_x++;
  377.         wind->formtree[wind->formtree[obj_id].ob_head].ob_y++;
  378.     }
  379.     update_objects(wind,obj_id,1,0);
  380. }
  381.  
  382. void fg_init(WINDFORM *wind)
  383. {
  384.     form_center(wind->formtree,&wind->form.x,&wind->form.y,
  385.         &wind->form.w,&wind->form.h);
  386.     wind_calc(WC_BORDER,wind->windkind,wind->form.x,wind->form.y,
  387.         wind->form.w,wind->form.h,&wind->wind.x,&wind->wind.y,
  388.         &wind->wind.w,&wind->wind.h);
  389.     wind->whandle=wind_create(wind->windkind,wind->wind.x,wind->wind.y,
  390.         wind->wind.w,wind->wind.h);
  391.     wind_set(wind->whandle,WF_NAME,wind->wind_title);
  392.  
  393.     if(wind == &windforms[WIND_CTRL])
  394.         wind_set(wind->whandle,WF_BEVENT,1,0,0,0); /* untoppable */
  395.     
  396.     wind_calc(WC_WORK,wind->windkind,wind->wind.x,wind->wind.y,
  397.         wind->wind.w,wind->wind.h,&wind->form.x,&wind->form.y,
  398.         &wind->form.w,&wind->form.h);
  399.     wind->formtree[wind->firstobj].ob_x=wind->form.x;
  400.     wind->formtree[wind->firstobj].ob_y=wind->form.y;
  401.     wind->formtree[wind->firstobj].ob_width=wind->form.w;
  402.     wind->formtree[wind->firstobj].ob_height=wind->form.h;
  403. }
  404.  
  405. int handle_message(int pipe[8])
  406. {
  407.     static int first_open=0;
  408.     int wnr,error; /* ,avmsg[8]; */
  409.     char *vamsg,*o;
  410.  
  411. #ifdef DEBUG
  412.     char tmp[128];
  413. #endif
  414.  
  415.     switch (pipe[0]) {
  416.         case AC_OPEN:
  417.             if (pipe[4] == acc_id)
  418.             {
  419.                 if(first_open)
  420.                 {
  421.                     if(windforms[WIND_CTRL].wind_open)
  422.                         wind_set(windforms[WIND_CTRL].whandle,WF_TOP);
  423.                     else
  424.                         fgbg=FG;
  425.                 }
  426.                 else
  427.                 {
  428.                     fg_init(&windforms[WIND_CTRL]);
  429.                     fgbg=FG;
  430.                     first_open=1;
  431.                 }
  432.             }
  433. #ifdef DEBUG
  434.                 form_alert(1,"[1][Got AC_OPEN][Ok]");
  435. #endif
  436.             break;
  437.         case AC_CLOSE:
  438. /*            if (pipe[4] == acc_id) */
  439.  
  440.             {
  441. #ifdef DEBUG
  442.                 sprintf(tmp,"[1][Got AC_CLOSE|ACC id: %d|pipe4: %d][Ok]",acc_id,pipe[4]);
  443.                 form_alert(1,tmp);
  444. #endif
  445.  
  446.                 if(windforms[WIND_CTRL].wind_open)
  447.                 {
  448.                     wind_close(windforms[WIND_CTRL].whandle);
  449.                     wind_delete(windforms[WIND_CTRL].whandle);
  450.                     windforms[WIND_CTRL].wind_open=0;
  451.                 }
  452.                 if(windforms[WIND_INFO].wind_open)
  453.                 {
  454.                     wind_close(windforms[WIND_INFO].whandle);
  455.                     wind_delete(windforms[WIND_INFO].whandle);
  456.                     windforms[WIND_INFO].wind_open=0;
  457.                 }
  458. #ifdef DEBUG
  459.                 sprintf(tmp,"[1][Filepos: %ld][Ok]",filepos);
  460.                 form_alert(1,tmp);
  461. #endif
  462.                 file_was_open=file_open;
  463.                 if(file_open)
  464.                     close_file(fd);
  465.                 closed_acc=1;
  466.                 return 1;
  467.             }
  468. /*            break; */
  469.             
  470.         case AP_TERM:
  471.             quit=1;
  472.             return 1;
  473. /*            switch(pipe[5])
  474.             {
  475.                 case AP_RESCHG:
  476.                     printf("Got AP_RESCHG!\n");
  477.                 break;
  478.                 case AP_TERM:
  479.                     printf("Got AP_TERM!");
  480.                 break;
  481.                 default:
  482.                     printf("Got unknown AP_TERM!");
  483.             }
  484. */
  485. /*            break; */
  486.         case RESCHG_COMPLETED:
  487. /*            printf("Got RESCHG_COMPLETED!"); */
  488.             break;
  489.         
  490.         case WM_REDRAW:
  491.             if((wnr=find_windform(pipe[3]))>=0)
  492.                 update_objects(&windforms[wnr],windforms[wnr].firstobj,
  493.                     windforms[wnr].objdepth,pipe);
  494.             break;
  495.         case WM_MOVED:
  496.             if((wnr=find_windform(pipe[3]))>=0)
  497.             {
  498.                 wind_set(windforms[wnr].whandle,WF_CURRXYWH,pipe[4],pipe[5],pipe[6],pipe[7]);
  499.                 windforms[wnr].wind.x=pipe[4];
  500.                 windforms[wnr].wind.y=pipe[5];
  501.                 windforms[wnr].wind.w=pipe[6];
  502.                 windforms[wnr].wind.h=pipe[7];
  503.                 wind_calc(WC_WORK,windforms[wnr].windkind,
  504.                     windforms[wnr].wind.x,windforms[wnr].wind.y,
  505.                     windforms[wnr].wind.w,windforms[wnr].wind.h,
  506.                     &windforms[wnr].form.x,&windforms[wnr].form.y,
  507.                     &windforms[wnr].form.w,&windforms[wnr].form.h);
  508.                 windforms[wnr].formtree[windforms[wnr].firstobj].ob_x=windforms[wnr].form.x;
  509.                 windforms[wnr].formtree[windforms[wnr].firstobj].ob_y=windforms[wnr].form.y;
  510.                 windforms[wnr].formtree[windforms[wnr].firstobj].ob_width=windforms[wnr].form.w;
  511.                 windforms[wnr].formtree[windforms[wnr].firstobj].ob_height=windforms[wnr].form.h;
  512.                 if(replay)
  513.                     update_time();
  514.             }
  515.             break;
  516.         case WM_CLOSED:
  517.             if((wnr=find_windform(pipe[3]))>=0)
  518.             {
  519.                 wind_close(windforms[wnr].whandle);
  520.                 windforms[wnr].wind_open=0;
  521.                 if(wnr==WIND_CTRL)
  522.                 {
  523.                     if(windforms[WIND_INFO].wind_open)
  524.                     {
  525.                         wind_close(windforms[WIND_INFO].whandle);
  526.                         windforms[WIND_INFO].wind_open=0;
  527.                     }
  528.                     return 1;
  529.                 }
  530.                 else
  531.                     wind_delete(windforms[wnr].whandle);
  532.             }
  533.             break;
  534.         case WM_TOPPED:
  535.             if((wnr=find_windform(pipe[3]))>=0)
  536.                 wind_set(pipe[3],WF_TOP);
  537.             break;
  538.         case AP_DRAGDROP:
  539.             if(wind_find(pipe[4],pipe[5]) == windforms[WIND_CTRL].whandle)
  540.             {
  541.                 if(do_dragdrop(pipe,DD_OK))
  542.                 {
  543.                     exit_replay();
  544.  
  545.                     if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED)
  546.                         toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL);
  547.                     Dsp_Hf1(0);                
  548.                     if(windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_state & SELECTED)
  549.                         toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL);
  550.                     replay_pause=0;
  551.  
  552.                     filepos=0;
  553.                     fd=reopen_file();
  554.                     filesize=Fseek(0L,fd,2);
  555.                     Fseek(0L,fd,0);
  556.                     if((error=getmp2info(fd))==MP2_NOERR)
  557.                     {
  558.                         update_time();
  559.                         setfilename(filename);
  560.                         if(!(pipe[6] & K_ALT))
  561.                             init_replay();
  562.                     }
  563.                     else
  564.                     {
  565.                         exit_replay();
  566.                         show_mp2_error(error);
  567.                         close_file(fd);
  568.                         strcpy(windforms[WIND_CTRL].wind_title,"MPEG");
  569.                         wind_set(windforms[WIND_CTRL].whandle,WF_NAME,
  570.                             windforms[WIND_CTRL].wind_title);
  571.                         setfilename("MPEGFILE");
  572.                     }
  573.                 }
  574.             }
  575.             else
  576.             {
  577.                 do_dragdrop(pipe,DD_NAK);
  578.             }
  579.             break;
  580.  
  581.         case VA_START:
  582.             vamsg=*((char **)&pipe[3]);
  583.             strcpy(path,vamsg);
  584.  
  585. /* This *should* be sent to the application
  586.     which sent the VA_START msg, but it seems
  587.     to hang Thing doing it. */
  588. /*
  589.             avmsg[0]=AV_STARTED;
  590.             avmsg[1]=app_id;
  591.             avmsg[2]=0;
  592.             avmsg[3]=pipe[3];
  593.             avmsg[4]=pipe[4];
  594.             appl_write(pipe[1],5*2,avmsg);
  595. */
  596.             if((o=strchr(path,' '))!=NULL)
  597.                 o[0]='\0';
  598.             o=strrchr(path,'\\');
  599.             strcpy(filename,&o[1]);
  600.             o=strrchr(path,'\\');
  601.             strncpy(o,"\\*.MP?",6);
  602.             o[6] = '\0';
  603.  
  604.  
  605.             exit_replay();
  606.  
  607.             if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED)
  608.                 toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL);
  609.             Dsp_Hf1(0);                
  610.             if(windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_state & SELECTED)
  611.                 toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL);
  612.             replay_pause=0;
  613.  
  614.             filepos=0;
  615.             fd=reopen_file();
  616.             filesize=Fseek(0L,fd,2);
  617.             Fseek(0L,fd,0);
  618.             if((error=getmp2info(fd))==MP2_NOERR)
  619.             {
  620.                 update_time();
  621.                 setfilename(filename);
  622.                 init_replay();
  623.             }
  624.             else
  625.             {
  626.                 exit_replay();
  627.                 show_mp2_error(error);
  628.                 close_file(fd);
  629.                 strcpy(windforms[WIND_CTRL].wind_title,"MPEG");
  630.                 wind_set(windforms[WIND_CTRL].whandle,WF_NAME,
  631.                     windforms[WIND_CTRL].wind_title);
  632.                 setfilename("MPEGFILE");
  633.             }
  634.             
  635.             break;
  636.         default:
  637. #ifdef DEBUG
  638.             sprintf(tmp,"[1][Unimplemented| message: %d][Ok]",pipe[0]);
  639.             form_alert(1,tmp);
  640. #endif
  641.             break;
  642.     }
  643.     return 0;
  644. }
  645.  
  646. int do_dragdrop(int pipe[8], int mode)
  647. {
  648.     char misc[512],*o;
  649.     char dd_name[32],dd_cmsg;
  650.     int i,dd_pipe,headsize,again=1,ret=0;
  651.     long datasize,err;
  652.     
  653.     strcpy(dd_name,"U:\\PIPE\\DRAGDROP.");
  654.     strcat(dd_name,(char *)&pipe[7]);
  655.     
  656.     if((err=Fopen(dd_name,FO_RW)) >= 0)
  657.     {
  658.         dd_pipe=(int)err;
  659.  
  660.         dd_cmsg=(char)mode;
  661.         Fwrite(dd_pipe,1,&dd_cmsg);
  662.         if(mode==DD_OK)
  663.         {
  664.             strcpy(misc,"ARGS");
  665.             for(i=(int)strlen(misc) ; i<32 ; i++)
  666.                 misc[i] = '\0';
  667.             Fwrite(dd_pipe,32,misc);
  668.             
  669.             while(again)
  670.             {
  671.                 Fread(dd_pipe,2,&headsize);
  672.                 
  673.                 if(headsize > 0)
  674.                 {
  675.                     Fread(dd_pipe,headsize,misc);
  676.                     if(!strncmp(misc,"ARGS",4))
  677.                     {
  678.                         datasize = *((long *)&misc[4]);
  679.  
  680.                         dd_cmsg=DD_OK;
  681.                         Fwrite(dd_pipe,1,&dd_cmsg);
  682.                         Fread(dd_pipe,datasize,misc);
  683.     
  684.                         if((o=strchr(misc,' '))!=NULL)
  685.                             o[0]='\0';
  686.                         o=strrchr(misc,'\\');
  687.                         strcpy(path,misc);
  688.                         strcpy(filename,&o[1]);
  689.                         o=strrchr(path,'\\');
  690.                         strncpy(o,"\\*.MP?",6);
  691.                         o[6] = '\0';
  692.  
  693.                         again=0;
  694.                         ret=1;
  695.                     }
  696.                     else
  697.                     {
  698.                         dd_cmsg=DD_EXT;
  699.                         Fwrite(dd_pipe,1,&dd_cmsg);
  700.                         again=1;
  701.                     }
  702.                 }
  703.                 else
  704.                     again=0;
  705.             }
  706.         }
  707.         /* mode==DD_NAK do nothing*/
  708.  
  709.         Fclose(dd_pipe);
  710.     }
  711.     
  712.     return ret;
  713. }
  714.  
  715. int find_windform(int whandle)
  716. {
  717.     /* Do this more general later perhaps */
  718.  
  719.     if(whandle==windforms[WIND_CTRL].whandle)
  720.         return WIND_CTRL;
  721.     if(whandle==windforms[WIND_INFO].whandle)
  722.         return WIND_INFO;
  723.     return -1;
  724. }
  725.  
  726. void update_objects(WINDFORM *wind,int obj_id,int depth, int pipe[8])
  727. {
  728.     CORDS2 r,u,o;
  729.     CORDS t;
  730.  
  731.     if(pipe)
  732.     {
  733.         o.x1=pipe[4];
  734.         o.y1=pipe[5];
  735.         o.x2=pipe[4]+pipe[6]-1;
  736.         o.y2=pipe[5]+pipe[7]-1;
  737.     }
  738.  
  739.     graf_mouse(M_OFF,0);
  740.     wind_update(BEG_UPDATE);
  741.     wind_get(wind->whandle,WF_FIRSTXYWH,&t.x,&t.y,&t.w,&t.h);
  742.     while(t.w || t.h)
  743.     {
  744.         if(pipe)
  745.         {
  746.             r.x1=t.x;                r.y1=t.y;
  747.             r.x2=t.x+t.w-1;        r.y2=t.y+t.h-1;
  748.             u.x1=max(r.x1,o.x1);    u.y1=max(r.y1,o.y1);
  749.             u.x2=min(r.x2,o.x2);    u.y2=min(r.y2,o.y2);
  750.         }
  751.         else
  752.         {
  753.             u.x1=t.x;                u.y1=t.y;
  754.             u.x2=t.x+t.w-1;        u.y2=t.y+t.h-1;
  755.         }
  756.         
  757.         if((u.x2>=u.x1) && (u.y2>=u.y1)) 
  758.             objc_draw(wind->formtree,obj_id,depth,
  759.                 u.x1, u.y1, u.x2-u.x1+1, u.y2-u.y1+1);
  760.         wind_get(wind->whandle,WF_NEXTXYWH,&t.x,&t.y,&t.w,&t.h);
  761.     }
  762.     wind_update(END_UPDATE);
  763.     graf_mouse(M_ON,0);
  764. }
  765.  
  766. int key2button(int key)
  767. {
  768.     int ac,sc;
  769.     
  770.     ac=key & 0xff;            /* extract ascii code */
  771.     sc=(key>>8) & 0xff;    /* extract scan code (not the best) */
  772.     sc=sc;                    /* dummy */
  773.  
  774.     switch(ac)
  775.     {
  776.         case 0x09:            /* C-i = info */
  777.             return CTRL_INFO;
  778.         case 0x12:            /* C-r = repeat on/off */
  779.             toggle_object(&windforms[WIND_CTRL],CTRL_LOOP,TOGGLE);
  780.             return CTRL_LOOP;
  781. /* C-w hangs the desktop when run as accessory */
  782.         case 0x17:            /* C-w = switch window */
  783.             if(_app)
  784.                 return -SWITCH_WIND;
  785.             else
  786.                 return 0;
  787.         case 0x15:            /* C-u = close window */
  788.             return -CLOSE_WIND;
  789.         case 0x0f:            /* C-o = open new file */
  790.             return CTRL_LOAD;
  791.         case 0x0c:            /* C-l = (load and) play */
  792.             return CTRL_PLAY;
  793.         case 0x13:            /* C-s = stop */
  794.             return CTRL_STOP;
  795.         case 0x10:            /* C-p = pause */
  796.             if(replay_pause)
  797.                 toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL);
  798.             else if(replay)
  799.                 toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_SELECTED);
  800.             return CTRL_PAUSE;
  801.         case 0x06:            /* C-f = next song */
  802.             return CTRL_NEXT;
  803.         case 0x02:            /* C-b = previous song */
  804.             return CTRL_PREV;
  805.         case 0x16:            /* C-v = Fast Forward */
  806.             toggle_object(&windforms[WIND_CTRL],CTRL_FF,TOGGLE);
  807.             return CTRL_FF;
  808.         case 0x14:            /* C-t = count up/down */
  809.             toggle_object(&windforms[WIND_CTRL],CTRL_TIME,TOGGLE);
  810.             return CTRL_TIME;
  811.         case ' ':            /* SPC = play/stop */
  812.             if(replay)
  813.                 return CTRL_STOP;
  814.             else
  815.                 return CTRL_PLAY;
  816.         default:
  817.             return 0;
  818.     }
  819. }
  820.  
  821. int do_formstuff(int obj_id)
  822. {
  823.     int tfd;
  824.  
  825.     switch(obj_id)
  826.     {
  827.         case CTRL_STOP:
  828.             if(Dsp_Hf1(-1))    /* If fast forwarding */
  829.             {
  830.                 if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED)
  831.                     toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL);
  832.                 Dsp_Hf1(0);                
  833.             }
  834.  
  835.             if(replay || replay_pause)
  836.             {
  837.                 if(windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_state & SELECTED)
  838.                     toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL);
  839.                 replay_pause=0;
  840.                 exit_replay();
  841.                 update_time();
  842.             }
  843.             break;
  844.         case CTRL_PLAY:
  845.             if(replay_pause || Dsp_Hf1(-1))
  846.             {
  847.                 if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED)
  848.                     toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL);
  849.                 Dsp_Hf1(0);                
  850.  
  851.                 if(windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_state & SELECTED)
  852.                     toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL);
  853.                 continue_replay();
  854.                 replay_pause=0;
  855.             }
  856.             else
  857.             {
  858.                 if(!replay && file_open)
  859.                 {
  860.                     replay_pause=0;
  861.                     init_replay();
  862.                 }
  863.                 else
  864.                 {
  865.                     if((tfd = open_file()) > 0)
  866.                     {
  867.                         int error;
  868.                         
  869.                         fd=tfd;
  870.                         if((error=getmp2info(fd))==MP2_NOERR)
  871.                         {
  872.                             replay_pause=0;
  873.                             update_time();
  874.                             setfilename(filename);
  875.                             init_replay();
  876.                         }
  877.                         else
  878.                         {
  879.                             show_mp2_error(error);
  880.                             close_file(fd);
  881.                             strcpy(windforms[WIND_CTRL].wind_title,"MPEG");
  882.                             wind_set(windforms[WIND_CTRL].whandle,WF_NAME,
  883.                                 windforms[WIND_CTRL].wind_title);
  884.                             setfilename("MPEGFILE");
  885.                         }
  886.     
  887.                     }
  888.                 }
  889.             }
  890.             break;
  891.         case CTRL_PAUSE:
  892.  
  893.             if(replay_pause)
  894.             {
  895.                 continue_replay();
  896.                 replay_pause=0;
  897.             }
  898.             else
  899.             {
  900.                 if(replay)
  901.                 {
  902.                     pause_replay();
  903.                     replay_pause=1;
  904.                 }
  905.             }
  906.  
  907.             break;
  908.         case CTRL_LOAD:
  909.             if((tfd = open_file()) > 0)
  910.             {
  911.                 int error;
  912.             
  913.                 if(windforms[WIND_CTRL].formtree[CTRL_PAUSE].ob_state & SELECTED)
  914.                     toggle_object(&windforms[WIND_CTRL],CTRL_PAUSE,SET_NORMAL);
  915.                 replay_pause=0;
  916.  
  917.                 if(Dsp_Hf1(-1))    /* If fast forwarding */
  918.                 {
  919.                     if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED)
  920.                         toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL);
  921.                     Dsp_Hf1(0);                
  922.                 }
  923.  
  924.                 fd=tfd;
  925.                 if((error=getmp2info(tfd))==MP2_NOERR)
  926.                 {
  927.                     update_time();
  928.                     setfilename(filename);
  929.                 }
  930.                 else
  931.                 {
  932.                     show_mp2_error(error);
  933.                     close_file(fd);
  934.                     strcpy(windforms[WIND_CTRL].wind_title,"MPEG");
  935.                     wind_set(windforms[WIND_CTRL].whandle,WF_NAME,
  936.                         windforms[WIND_CTRL].wind_title);
  937.                     setfilename("MPEGFILE");
  938.                 }
  939.             }
  940.             break;
  941.         case CTRL_LOOP:
  942.             looping=(windforms[WIND_CTRL].formtree[CTRL_LOOP].ob_state & SELECTED);
  943.             break;
  944.         case CTRL_INFO:
  945.             /* Open info window */
  946.             if(windforms[WIND_INFO].wind_open)
  947.                 wind_set(windforms[WIND_INFO].whandle,WF_TOP);
  948.             else
  949.             {
  950.                 fg_init(&windforms[WIND_INFO]);
  951.                 wind_open(windforms[WIND_INFO].whandle,
  952.                     windforms[WIND_INFO].wind.x,windforms[WIND_INFO].wind.y,
  953.                     windforms[WIND_INFO].wind.w,windforms[WIND_INFO].wind.h);
  954.                 windforms[WIND_INFO].wind_open=1;
  955.             }
  956.             break;
  957.         case CTRL_NEXT:
  958.             if(file_open)
  959.             {
  960.                 int o_replay,o_pause;
  961.             
  962.                 if(Dsp_Hf1(-1))    /* If fast forwarding */
  963.                 {
  964.                     if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED)
  965.                         toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL);
  966.                     Dsp_Hf1(0);                
  967.                 }
  968.  
  969.                 o_replay = replay;
  970.                 o_pause = replay_pause;
  971.                 exit_replay();
  972.                 while((tfd=next_song(path, filename, 1))!=0)
  973.                 {
  974.                     if(getmp2info(tfd)!=MP2_NOERR)
  975.                     {
  976.                         close_file(tfd);
  977.                     } 
  978.                     else
  979.                     {
  980.                         if(file_open)
  981.                             close_file(fd);
  982.                         fd = tfd;
  983.                         file_open=1;
  984.                         update_time();
  985.                         setfilename(filename);
  986.                         replay = o_replay;
  987.                         replay_pause = o_pause;
  988.                         if(replay)
  989.                             init_replay();
  990.                         if(replay_pause)
  991.                         {
  992.                             init_replay();
  993.                             pause_replay();
  994.                         }
  995.                         break;
  996.                     }
  997.                 }
  998.                 if(!tfd)
  999.                 {
  1000.                     update_time();
  1001.                 }
  1002.             }
  1003.             break;
  1004.         case CTRL_PREV:
  1005.             if(file_open)
  1006.             {
  1007.                 int o_replay,o_pause;
  1008.                 
  1009.                 if(Dsp_Hf1(-1))    /* If fast forwarding */
  1010.                 {
  1011.                     if(windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED)
  1012.                         toggle_object(&windforms[WIND_CTRL],CTRL_FF,SET_NORMAL);
  1013.                     Dsp_Hf1(0);                
  1014.                 }
  1015.  
  1016.                 o_replay = replay;
  1017.                 o_pause = replay_pause;
  1018.  
  1019.                 /* if more than or one second of the song
  1020.                     has elapsed, go to the beginning of
  1021.                     of the same song! */
  1022.                 if(calc_time() > 0)
  1023.                 {
  1024.                     reset_file(fd);
  1025.                     update_time();
  1026.                     replay = o_replay;
  1027.                     replay_pause = o_pause;
  1028.                     if(replay)
  1029.                         init_replay();
  1030.                     if(replay_pause)
  1031.                     {
  1032.                         init_replay();
  1033.                         pause_replay();
  1034.                     }
  1035.                 }
  1036.                 else
  1037.                 {
  1038.                     exit_replay();
  1039.                     while((tfd=next_song(path, filename, 0))!=0)
  1040.                     {
  1041.                         if(getmp2info(tfd)!=MP2_NOERR)
  1042.                         {
  1043.                             close_file(tfd);
  1044.                         }
  1045.                         else
  1046.                         {
  1047.                             if(file_open)
  1048.                                 close_file(fd);
  1049.                             fd = tfd;
  1050.                             file_open=1;
  1051.                             update_time();
  1052.                             setfilename(filename);
  1053.                             replay = o_replay;
  1054.                             replay_pause = o_pause;
  1055.                             if(replay)
  1056.                                 init_replay();
  1057.                             if(replay_pause)
  1058.                             {
  1059.                                 init_replay();
  1060.                                 pause_replay();
  1061.                             }
  1062.                             break;
  1063.                         }
  1064.                     }
  1065.                     if(!tfd)
  1066.                     {
  1067.                         update_time();
  1068.                     }
  1069.                 }
  1070.             }
  1071.             break;
  1072.         case CTRL_FF:
  1073.             if (windforms[WIND_CTRL].formtree[CTRL_FF].ob_state & SELECTED)
  1074.                 Dsp_Hf1(1);
  1075.             else
  1076.                 Dsp_Hf1(0);
  1077.             break;
  1078.         case CTRL_TIME:
  1079.             if(windforms[WIND_CTRL].formtree[CTRL_TIME].ob_state & SELECTED)
  1080.                 count_dir = 0; /* count up */
  1081.             else
  1082.                 count_dir = 1; /* count down */
  1083.             if(file_open)
  1084.                 update_time();
  1085.             break;
  1086.         default:
  1087.             break;
  1088.     }
  1089.     return 0;
  1090. }
  1091.  
  1092. long calc_time()
  1093. {
  1094.     long ptr[4];
  1095.  
  1096.     if(buf_init)
  1097.         buffptr(ptr);
  1098.     else
  1099.         ptr[0]=(long)buffer_mem;    
  1100.  
  1101.     return (bufferpos+ptr[0]-(long)buffer_mem)/(mp2info.bitrate/8);
  1102. }
  1103.  
  1104. void update_time()
  1105. {
  1106.     long time;
  1107.     char tmp[64];
  1108.     
  1109.     if(count_dir)
  1110.         time = total_time - calc_time();
  1111.     else
  1112.         time = calc_time();
  1113.         
  1114.     sprintf(tmp,"%02ld:%02ld",time/60,time%60);
  1115.     if(strcmp(tmp,windforms[WIND_CTRL].wind_title))
  1116.     {
  1117.         strcpy(windforms[WIND_CTRL].wind_title,tmp);
  1118.         wind_set(windforms[WIND_CTRL].whandle,WF_NAME,
  1119.             windforms[WIND_CTRL].wind_title);
  1120.     }
  1121. }
  1122.  
  1123. void setfilename(char *fname)
  1124. {
  1125.     char *t,*text;
  1126.     
  1127.     text = &windforms[WIND_CTRL].formtree[CTRL_FILENAME].ob_spec.
  1128.         tedinfo->te_ptext[6];
  1129.     
  1130.     strncpy(text, fname, 8);
  1131.     text[8] = '\0';
  1132.     if((t=strchr(text, '.')) != NULL)
  1133.         *t = '\0';
  1134.  
  1135.     update_objects(&windforms[WIND_CTRL],CTRL_FNAME_BOX,0,0);
  1136.     update_objects(&windforms[WIND_CTRL],CTRL_FILENAME,0,0);
  1137. }
  1138.