home *** CD-ROM | disk | FTP | other *** search
/ M.u.C.S. Disc 2000 / MUCS2000.iso / anwend / qed453 / src / av.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-20  |  10.3 KB  |  499 lines

  1. #include "global.h"
  2. #include "av.h"
  3. #include "comm.h"
  4. #include "dd.h"
  5. #include "options.h"
  6. #include "poslist.h"
  7. #include "rsc.h"
  8. #include "window.h"
  9.  
  10. /* exportierte Variablen *****************************************************/
  11. int    av_shell_id = -1,                /* ID des Desktops */
  12.         av_shell_status = 0;            /* Welche AV_* kann Desktop */
  13.  
  14. /* lokale Variablen **********************************************************/
  15.  
  16. /* Hilfe-System **************************************************************/
  17.  
  18. #ifndef AC_HELP
  19. #define AC_HELP        1025
  20. #endif
  21.  
  22. /*
  23.  * Generiert aus einem Pfadnamen den GEM-Namen (max. acht Zeichen, ggf.
  24.  * mit ' ' aufgefüllt) und überprüft ihn auf ST-GUIDE.
  25. */
  26. static bool make_name(char *longname, char *shortname, bool must_stg)
  27. {
  28.     int    len, i;
  29.     char    *p;
  30.  
  31.     if (longname[0] == EOS)
  32.     {
  33.         note(1, 0, NOHELP);
  34.         strcpy(shortname, "");
  35.         return FALSE;
  36.     }
  37.     else
  38.     {
  39.         file_name(longname, shortname, FALSE);
  40.         str_toupper(shortname);
  41.         p = strrchr(shortname, '.');                /* Extension abschneiden */
  42.         *p = '\0';
  43.         len = (int) strlen(shortname);
  44.         for (i = len; i < 8; i++)
  45.             strcat(shortname, " ");
  46.         if (must_stg && (strcmp(shortname, "ST-GUIDE") != 0))
  47.         {
  48.             note(1, 0, NOSTG);
  49.             return FALSE;
  50.         }
  51.         else
  52.             return TRUE;
  53.     }
  54. }
  55.  
  56.  
  57. /*
  58.  * Liefert die app_id des übergebenen Programmes zurück. Falls es noch
  59.  * nicht läuft, wird es mit arg gestartet.
  60. */
  61. static int get_id(char* name, char *path, char *arg, bool *started)
  62. {
  63.     int        ret = -1;
  64.     char        *p, help[128] = "";
  65.     bool    s = FALSE;
  66.  
  67.     if (name[0] != EOS)
  68.     {
  69.         ret = appl_find(name);
  70.         if (ret < 0)                            /* ggf. als Programm nachstarten */
  71.         {
  72.             if (arg != NULL)
  73.             {
  74.                 strcpy(help, " ");
  75.                 strcat(help, arg);
  76.                 help[0] = (char)strlen(help);
  77.             }
  78.             p = strrchr(path, '.');
  79.             if (file_exists(path) && (strnicmp(p, ".app", 3) == 0 || strncmp(p, ".prg", 3) == 0))
  80.             {
  81.                 if (gl_gem >= 0x400)
  82.                 {
  83.                     ret = shel_write(1, 1, 1, path, help);
  84.                     s = (ret > 0);
  85.                 }
  86.                 else if (gl_magx)
  87.                 {
  88.                     ret = shel_write(1, 1, 100, path, help);
  89.                     s = (ret > 0);
  90.                 }
  91.                 else
  92.                 {
  93.                     note(1, 0, HELPSTART);
  94.                     ret = -1;
  95.                 }
  96.             }
  97.             else
  98.             {
  99.                 snote(1, 0, HELPNOTF, helpprog);
  100.                 ret = - 1;
  101.             }
  102.         }
  103.     }
  104.     if (started != NULL)
  105.         *started = s;
  106.     return ret;
  107. }
  108.  
  109. static void send_help(int id, char *str)
  110. {
  111.     if (debug_level & DBG_AV)
  112.         debug("send_help: '%s' to %d\n", str, id);
  113.     memset(msgbuff, 0, (int)sizeof(msgbuff));
  114.     msgbuff[0] = AC_HELP;
  115.     msgbuff[1] = gl_apid;
  116.     strcpy(global_str1, str);
  117.     *(char **)(msgbuff + 3) = global_str1;
  118.     send_msg(id);
  119. }
  120.  
  121. bool call_help(char *str)
  122. {
  123.     int        help_id;
  124.     FILENAME    name;
  125.  
  126.     wake_mouse();
  127.     if (make_name(helpprog, name, FALSE))
  128.     {
  129.         help_id = get_id(name, helpprog, NULL, NULL);
  130.         if (help_id > 0)
  131.         {
  132.             send_help(help_id, str);
  133.             return TRUE;
  134.         }
  135.     }
  136.     return FALSE;
  137. }
  138.  
  139. bool call_hyp(char *data)
  140. {
  141.     int        stg_id;
  142.     char         help[80];
  143.     FILENAME name;
  144.     bool        started;
  145.  
  146.     if (helpprog[0] == EOS)
  147.     {
  148.         note(1, 0, NOHELP);
  149.         return FALSE;
  150.     }
  151.     wake_mouse();
  152.     if (make_name(helpprog, name, TRUE))
  153.     {
  154.         strcpy(help, "*:\\qed.hyp ");
  155.         strcat(help, data);
  156.  
  157.         stg_id = get_id(name, helpprog, help, &started);
  158.         if (debug_level & DBG_AV)
  159.             debug("call_hyp: '%s' to %d\n", data, stg_id);
  160.         if (stg_id > 0 && !started)            /* nicht gestartet, da es bereits lief! */
  161.             send_vastart(stg_id, help);        /* -> also Message schicken */
  162.         return (stg_id > 0);
  163.     }
  164.     return FALSE;
  165. }
  166.  
  167.  
  168. static void get_my_name(char *my_name)
  169. {
  170.     char    str[12];
  171.     int    d, i, id;
  172.  
  173.     if ((appl_xgetinfo(4, &d, &d, &i, &d)) && (i == 1))    /* gibts appl_search? */
  174.     {
  175.         i = appl_search(0, str, &d, &id);
  176.         while (i != 0)
  177.         {
  178.             if (id == gl_apid)
  179.                 break;
  180.             i = appl_search( 1, str, &d, &id);
  181.         }
  182.     }
  183.     if (i != 0)
  184.     {
  185.         strcpy(my_name, str);
  186.         for (i = (int)strlen(my_name); i < 8; i++)
  187.             strcat(my_name, " ");
  188.     }
  189.     else
  190.         strcpy(my_name, "");
  191.     if    (debug_level & DBG_AV)
  192.         debug("get_my_name()= %s.\n", my_name);
  193. }
  194.  
  195. /* AV-Protokoll **************************************************************/
  196.  
  197. static char    *glob_data = NULL;
  198.  
  199. static void send_avprot(void)
  200. {
  201.     if (av_shell_id >= 0)
  202.     {
  203.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  204.         msgbuff[0] = AV_PROTOKOLL;
  205.         msgbuff[1] = gl_apid;
  206.         msgbuff[3] = (2|16);        /* VA_START, Quoting */
  207.         get_my_name(global_str1);
  208.         *(char **) (msgbuff + 6) = global_str1;
  209.         send_msg(av_shell_id);
  210.         if (debug_level & DBG_AV)
  211.             debug("AV_PROTOKOLL\n");
  212.     }
  213. }
  214.  
  215. static void send_avexit(void)
  216. {
  217.     if ((av_shell_id >= 0) && (av_shell_status & 1024))
  218.     {
  219.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  220.         msgbuff[0] = AV_EXIT;
  221.         msgbuff[1] = gl_apid;
  222.         msgbuff[3] = gl_apid;
  223.         send_msg(av_shell_id);
  224.         if (debug_level & DBG_AV)
  225.             debug("AV_EXIT\n");
  226.     }
  227. }
  228.  
  229. bool send_avkey(int ks, int kr)
  230. {
  231.     bool    b = FALSE;
  232.  
  233.     if ((av_shell_id >= 0) && (av_shell_status & 1))
  234.     {
  235.         wake_mouse();
  236.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  237.         msgbuff[0] = AV_SENDKEY;
  238.         msgbuff[1] = gl_apid;
  239.         msgbuff[3] = ks;
  240.         msgbuff[4] = kr;
  241.         b = send_msg(av_shell_id);
  242.         if (debug_level & DBG_AV)
  243.             debug("AV_SENDKEY (%d,%d)\n", ks, kr);
  244.     }
  245.     return b;
  246. }
  247.  
  248. void send_avwinopen(int handle)
  249. {
  250.     if ((av_shell_id >= 0) && (wind_cycle))    /* Fensterwechsel global */
  251.     {
  252.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  253.         msgbuff[0] = AV_ACCWINDOPEN;
  254.         msgbuff[1] = gl_apid;
  255.         msgbuff[3] = handle;
  256.         send_msg(av_shell_id);
  257.         if (debug_level & DBG_AV)
  258.             debug("AV_ACCWINDOPEN (%d)\n", handle);
  259.     }
  260. }
  261.  
  262. void send_avwinclose(int handle)
  263. {
  264.     if ((av_shell_id >= 0) && (wind_cycle))    /* Fensterwechsel global */
  265.     {
  266.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  267.         msgbuff[0] = AV_ACCWINDCLOSED;
  268.         msgbuff[1] = gl_apid;
  269.         msgbuff[3] = handle;
  270.         send_msg(av_shell_id);
  271.         if (debug_level & DBG_AV)
  272.             debug("AV_ACCWINDCLOSED (%d)\n", handle);
  273.     }
  274. }
  275.  
  276. void send_avdrag(int wh, int m_x, int m_y, int kstate, int data_type)
  277. {
  278.     if ((av_shell_id >= 0) && (av_shell_status & 512))
  279.     {
  280.         int    app, d;
  281.         
  282.         wind_get(wh, WF_OWNER, &app, &d, &d, &d);
  283.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  284.         msgbuff[0] = AV_DRAG_ON_WINDOW;
  285.         msgbuff[1] = gl_apid;
  286.         msgbuff[3] = m_x;
  287.         msgbuff[4] = m_y;
  288.         msgbuff[5] = kstate;
  289.  
  290.         switch (data_type)
  291.         {
  292.             case DRAGDROP_PATH :        /* ein Name */
  293.                 if (debug_level & DBG_AV)
  294.                     debug("AV_DRAG_TO_WIN single file\n");
  295.                 strcpy(global_str1, drag_filename);
  296.                 *(char **) (msgbuff + 6) = global_str1;
  297.                 break;
  298.  
  299.             case DRAGDROP_DATA :        /* mehrere Namen */
  300.                 if (debug_level & DBG_AV)
  301.                     debug("AV_DRAG_TO_WIN multible files\n");
  302.                 glob_data = malloc_global(drag_data_size + 1);
  303.                 if (glob_data != NULL)
  304.                 {
  305.                     strcpy(glob_data, drag_data);
  306.                     *(char **) (msgbuff + 6) = glob_data;
  307.                 }
  308.                 break;
  309.         }
  310.         send_msg(av_shell_id);
  311.     }
  312. }
  313.  
  314. int check_avobj(int x, int y)
  315. {
  316.     int    ret = 0;
  317.         
  318.     if (av_shell_id >= 0)
  319.     {
  320.         memset(msgbuff, 0, (int)sizeof(msgbuff));
  321.         msgbuff[0] = AV_WHAT_IZIT;
  322.         msgbuff[3] = x;
  323.         msgbuff[4] = y;
  324.         
  325.         send_msg(av_shell_id);
  326.         evnt_mesag(msgbuff);
  327.         if (msgbuff[0] == VA_THAT_IZIT)
  328.         {
  329.             ret = msgbuff[4];
  330.             if (debug_level & DBG_AV)
  331.                 debug("VA_THAT_IZIT(%d, %d): %d\n", x, y, ret);
  332.         }
  333.     }
  334.     return ret;
  335. }
  336.  
  337. static void send_avstarted(int id, int m3, int m4)
  338. {
  339.     memset(msgbuff, 0, (int)sizeof(msgbuff));
  340.     msgbuff[0] = AV_STARTED;
  341.     msgbuff[3] = m3;
  342.     msgbuff[4] = m4;
  343.     if (debug_level & DBG_AV)
  344.         debug("AV_STARTED (%d)\n", id);
  345.     send_msg(id);
  346. }
  347.  
  348. /*
  349.  * Zerlegt VA_START-Arg in einzelne Dateinamen.
  350.  * Quoting wird korrekt ausgewertet ('arg 1', 'arg''s')
  351. */
  352. static bool parse_vaarg(POSENTRY **list, char *arg)
  353. {
  354.     int        i, j, len;
  355.     PATH        filename;
  356.     bool    in_quote = FALSE;
  357.     
  358.     len = (int)strlen(arg);
  359.     if (len > 0)
  360.     {
  361.         i = 0;
  362.         j = 0;
  363.         strcpy(filename, "");
  364.         while (i < len)
  365.         {
  366.             if (arg[i] == '\'')
  367.             {
  368.                 i++;
  369.                 if (in_quote)
  370.                 {
  371.                     if (arg[i] == '\'')                    /* Doppeltes ' */
  372.                     {
  373.                         filename[j++] = '\'';
  374.                         i++;
  375.                     }
  376.                     else
  377.                         in_quote = FALSE;
  378.                 }
  379.                 else
  380.                     in_quote = TRUE;
  381.             }
  382.             if ((arg[i] != ' ') || ((arg[i] == ' ' && in_quote)))
  383.                 filename[j++] = arg[i++];
  384.             else
  385.             {
  386.                 filename[j++] = '\0';
  387.                 insert_poslist(list, filename, 0, 0);
  388.                 j = 0;
  389.                 i++; 
  390.             }
  391.         }
  392.         if (j > 0)
  393.         {
  394.             filename[j++] = '\0';
  395.             insert_poslist(list, filename, 0, 0);
  396.         }
  397.         return TRUE;
  398.     }
  399.     return FALSE;
  400. }
  401.  
  402.  
  403. void handle_av(int msgbuff[])
  404. {
  405.     char        *str_p, *arg;
  406.     int        kstate, d;
  407.     POSENTRY    *va_list = NULL;
  408.  
  409.     switch (msgbuff[0])
  410.     {
  411.         case VA_START :
  412.             if (all_iconified)
  413.                 all_uniconify(NULL, NULL);
  414.             str_p = *(char **)(msgbuff + 3);
  415.             if (str_p != NULL)
  416.             {
  417.                 arg = strdup(str_p);
  418.                 send_avstarted(msgbuff[1], msgbuff[3], msgbuff[4]);
  419.  
  420.                 if ((debug_level & DBG_AV) || (debug_level & DBG_ARG))
  421.                     debug("VA_START %s\n", arg);
  422.  
  423.                 if (strlen(arg) > 0 && parse_vaarg(&va_list, arg))
  424.                 {
  425.                     if (debug_level & DBG_AV)
  426.                         debug("va_arg= %s\n", str_p);
  427.                     open_poslist(va_list);
  428.                     delete_poslist(&va_list);
  429.                 }
  430.                 free(arg);
  431.             }
  432.             break;
  433.  
  434.         case VA_PROTOSTATUS :
  435.             if (debug_level & DBG_AV)
  436.                 debug("VA_PROTSTATUS %u\n", (unsigned short)msgbuff[3]);
  437.             av_shell_status = msgbuff[3];
  438.             if (wind_cycle && !(av_shell_status & 64))
  439.                 wind_cycle = FALSE;            /* glob. Fensterwechsel abschalten */
  440.             break;
  441.  
  442.         case VA_DRAG_COMPLETE :
  443.             if (debug_level & DBG_AV)
  444.                 debug("VA_DRAG_COMPLETE.\n");
  445.             if (glob_data != NULL)
  446.             {
  447.                 free(glob_data);
  448.                 glob_data = NULL;
  449.             }
  450.             break;
  451.  
  452.         case VA_DRAGACCWIND :                /* bei D&D mit glob. Fensterwechsel */
  453.             str_p = *(char **)(msgbuff + 6);
  454.             if (str_p != NULL)
  455.             {
  456.                 graf_mkstate(&d, &d, &d, &kstate);
  457.                 handle_avdd(msgbuff[3], kstate, str_p);
  458.             }
  459.             break;
  460.  
  461.         case AV_SENDKEY :
  462.             if (debug_level & DBG_AV)
  463.                 debug("AV_SENDKEY von %d: %d, %d\n", msgbuff[1], msgbuff[3], msgbuff[4]);
  464.             if ((msgbuff[3] == 4) && (msgbuff[4] == 0x1117))    /* ^W */
  465.                 cycle_window();
  466.             break;
  467.             
  468.         default:
  469.             break;
  470.     }
  471. }
  472.  
  473. void init_av(void)
  474. {
  475.     int    i;
  476.     char    name[9], *p;
  477.  
  478.     p = getenv("AVSERVER");
  479.     if (p != NULL)
  480.     {
  481.         strncpy(name, p, 8);
  482.         name[8] = EOS;
  483.         for (i = (int)strlen(name); i < 8; i++)
  484.             strcat(name, " ");
  485.         i = appl_find(name);
  486.         if (i >= 0)
  487.             av_shell_id = i;
  488.     }
  489.     if (debug_level & DBG_AV)
  490.         debug("av_shell_id= %d\n", av_shell_id);
  491.  
  492.     send_avprot();
  493. }
  494.  
  495. void term_av(void)
  496. {
  497.     send_avexit();
  498. }
  499.