home *** CD-ROM | disk | FTP | other *** search
/ M.u.C.S. Disc 2000 / MUCS2000.iso / sound / mp2_099 / src / gem / mp2event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  23.7 KB  |  1,104 lines

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