home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xyzext.zip / xyz / lib / Xxyz.c < prev   
C/C++ Source or Header  |  1992-07-18  |  9KB  |  390 lines

  1.  
  2. /*
  3.  * XamineYourZerver extension library routines
  4.  *
  5.  * $Revision$
  6.  */
  7.  
  8. #include <X11/Xlibint.h>
  9. #include "xyzstr.h"
  10. #include <X11/extensions/Xext.h>
  11. #include "extutil.h"
  12.  
  13. static XExtensionInfo *xyz_info;
  14. static char *xyz_extension_name = XAMINE_YOUR_ZERVER_NAME;
  15.  
  16. #define XYZ_CheckExtension(dpy,i,val) \
  17.    XextCheckExtension(dpy, i, xyz_extension_name, val)
  18.  
  19. /*****************************************************************************
  20.  *                                                                           *
  21.  *                         private utility routines                          *
  22.  *                                                                           *
  23.  *****************************************************************************/
  24.  
  25. static int close_display();
  26. static /* const */ XExtensionHooks xyz_extension_hooks = {
  27.     NULL,                               /* create_gc */
  28.     NULL,                               /* copy_gc */
  29.     NULL,                               /* flush_gc */
  30.     NULL,                               /* free_gc */
  31.     NULL,                               /* create_font */
  32.     NULL,                               /* free_font */
  33.     close_display,                      /* close_display */
  34.     NULL,                               /* wire_to_event */
  35.     NULL,                               /* event_to_wire */
  36.     NULL,                               /* error */
  37.     NULL                                /* error_string */
  38. };
  39.  
  40. static XEXT_GENERATE_FIND_DISPLAY(find_display, xyz_info, xyz_extension_name,
  41.    &xyz_extension_hooks, XYZ_NumberEvents, NULL)
  42.  
  43. static XEXT_GENERATE_CLOSE_DISPLAY(close_display, xyz_info)
  44.  
  45. /*****************************************************************************
  46.  *                                                                           *
  47.  *                  public Xamine Your Zerver routines                       *
  48.  *                                                                           *
  49.  *****************************************************************************/
  50.  
  51. Bool
  52. XYZ_QueryExtension(dpy)
  53. Display *dpy;
  54. {
  55.    XExtDisplayInfo *info = find_display(dpy);
  56.  
  57.    if(XextHasExtension(info)) {
  58.       return True;
  59.    } else {
  60.       return False;
  61.    }
  62. }
  63.  
  64. Status
  65. XYZ_Instrument(dpy, instrument)
  66. Display *dpy;
  67. int instrument;
  68. {
  69.    XExtDisplayInfo *info = find_display(dpy);
  70.    xXYZ_InstrumentReq *req;
  71.  
  72.    XYZ_CheckExtension(dpy, info, 0);
  73.  
  74.    LockDisplay(dpy);
  75.    GetReq(XYZ_Instrument, req);
  76.    req->reqType = info->codes->major_opcode;
  77.    req->xyzReqType = X_XYZ_Instrument;
  78.    req->instrument = instrument;
  79.    UnlockDisplay(dpy);
  80.    SyncHandle();
  81.    return 1;
  82. }
  83.  
  84. Status
  85. XYZ_Trace(dpy, trace)
  86. Display *dpy;
  87. int trace;
  88. {
  89.    XExtDisplayInfo *info = find_display(dpy);
  90.    xXYZ_TraceReq *req;
  91.  
  92.    XYZ_CheckExtension(dpy, info, 0);
  93.  
  94.    LockDisplay(dpy);
  95.    GetReq(XYZ_Trace, req);
  96.    req->reqType = info->codes->major_opcode;
  97.    req->xyzReqType = X_XYZ_Trace;
  98.    req->trace = trace;
  99.    UnlockDisplay(dpy);
  100.    SyncHandle();
  101.    return 1;
  102. }
  103.  
  104. Status
  105. XYZ_SetCurTraceLevel(dpy, tracelevel)
  106. Display *dpy;
  107. int tracelevel;
  108. {
  109.    XExtDisplayInfo *info = find_display(dpy);
  110.    xXYZ_SetCurTraceLevelReq *req;
  111.  
  112.    XYZ_CheckExtension(dpy, info, 0);
  113.  
  114.    LockDisplay(dpy);
  115.    GetReq(XYZ_SetCurTraceLevel, req);
  116.    req->reqType = info->codes->major_opcode;
  117.    req->xyzReqType = X_XYZ_SetCurTraceLevel;
  118.    req->tracelevel = tracelevel;
  119.    UnlockDisplay(dpy);
  120.    SyncHandle();
  121.    return 1;
  122. }
  123.  
  124. Status
  125. XYZ_QueryState(dpy, instrument, trace, tracelevel, status)
  126. Display *dpy;
  127. int *instrument;
  128. int *trace;
  129. int *tracelevel;
  130. int *status;
  131. {
  132.    XExtDisplayInfo *info = find_display(dpy);
  133.    xXYZ_QueryStateReq *req;
  134.    xXYZ_QueryStateReply rep;
  135.  
  136.    XYZ_CheckExtension(dpy, info, 0);
  137.  
  138.    LockDisplay(dpy);
  139.    GetReq(XYZ_QueryState, req);
  140.    req->reqType = info->codes->major_opcode;
  141.    req->xyzReqType = X_XYZ_QueryState;
  142.    if(!_XReply(dpy, &rep, 0, xFalse)) {
  143.       UnlockDisplay(dpy);
  144.       SyncHandle();
  145.       return 0;
  146.    }
  147.    *instrument = rep.instrument;
  148.    *trace = rep.trace;
  149.    *tracelevel = rep.tracelevel;
  150.    *status = rep.status;
  151.    UnlockDisplay(dpy);
  152.    SyncHandle();
  153.    return 1;
  154. }
  155.  
  156. Status
  157. XYZ_GetTag(dpy, tagname, value, tracelevel)
  158. Display *dpy;
  159. char *tagname;
  160. int *value;
  161. int *tracelevel;
  162. {
  163.    XExtDisplayInfo *info = find_display(dpy);
  164.    xXYZ_GetTagReq *req;
  165.    xXYZ_GetTagReply rep;
  166.    int nChars;
  167.  
  168.    XYZ_CheckExtension(dpy, info, 0);
  169.  
  170.    nChars = strlen(tagname);
  171.    LockDisplay(dpy);
  172.    GetReq(XYZ_GetTag, req);
  173.    req->reqType = info->codes->major_opcode;
  174.    req->xyzReqType = X_XYZ_GetTag;
  175.    req->length += (nChars + 3) >> 2;
  176.    req->nChars = nChars;
  177.    _XSend(dpy, tagname, nChars);
  178.    if(!_XReply(dpy, &rep, 0, xFalse)) {
  179.       UnlockDisplay(dpy);
  180.       SyncHandle();
  181.       return 0;
  182.    }
  183.  
  184.    *value = rep.value;
  185.    *tracelevel = rep.tracelevel;
  186.  
  187.    UnlockDisplay(dpy);
  188.    SyncHandle();
  189.    return 1;
  190. }
  191.  
  192. Status
  193. XYZ_SetValue(dpy, tagname, value)
  194. Display *dpy;
  195. char *tagname;
  196. int value;
  197. {
  198.    XExtDisplayInfo *info = find_display(dpy);
  199.    xXYZ_SetValueReq *req;
  200.    int nChars;
  201.  
  202.    XYZ_CheckExtension(dpy, info, 0);
  203.  
  204.    nChars = strlen(tagname);
  205.    LockDisplay(dpy);
  206.    GetReq(XYZ_SetValue, req);
  207.    req->reqType = info->codes->major_opcode;
  208.    req->xyzReqType = X_XYZ_SetValue;
  209.    req->length += (nChars + 3) >> 2;
  210.    req->value = value;
  211.    req->nChars = nChars;
  212.    Data(dpy, tagname, nChars);
  213.  
  214.    UnlockDisplay(dpy);
  215.    SyncHandle();
  216.    return 1;
  217. }
  218.  
  219. Status
  220. XYZ_SetTraceLevel(dpy, tagname, tracelevel)
  221. Display *dpy;
  222. char *tagname;
  223. int tracelevel;
  224. {
  225.    XExtDisplayInfo *info = find_display(dpy);
  226.    xXYZ_SetTraceLevelReq *req;
  227.    int nChars;
  228.  
  229.    XYZ_CheckExtension(dpy, info, 0);
  230.  
  231.    nChars = strlen(tagname);
  232.    LockDisplay(dpy);
  233.    GetReq(XYZ_SetTraceLevel, req);
  234.    req->reqType = info->codes->major_opcode;
  235.    req->xyzReqType = X_XYZ_SetTraceLevel;
  236.    req->length += (nChars + 3) >> 2;
  237.    req->tracelevel = tracelevel;
  238.    req->nChars = nChars;
  239.    Data(dpy, tagname, nChars);
  240.  
  241.    UnlockDisplay(dpy);
  242.    SyncHandle();
  243.    return 1;
  244. }
  245.  
  246. XYZ_value *
  247. XYZ_ListValues(dpy, npats, pats, patlens, maxtags, total, returned)
  248. Display *dpy;
  249. int npats;
  250. char **pats;
  251. int *patlens;
  252. int maxtags;
  253. int *total;
  254. int *returned;
  255. {
  256.    XExtDisplayInfo *info = find_display(dpy);
  257.    xXYZ_ListValuesReq *req;
  258.    xXYZ_ListValuesReply rep;
  259.    CARD16 nChars;
  260.    char *data;
  261.    char *valptr;
  262.    int rlen;
  263.    int length;
  264.    XYZ_value *mem;
  265.    XYZ_value *vals;
  266.    int i;
  267.    int space;
  268.  
  269.    XYZ_CheckExtension(dpy, info, 0);
  270.  
  271.    LockDisplay(dpy);
  272.    GetReq(XYZ_ListValues, req);
  273.    req->reqType = info->codes->major_opcode;
  274.    req->xyzReqType = X_XYZ_ListValues;
  275.    for(i=0;i<npats;i++) {
  276.       req->length += (patlens[i] + 2 + 3) >> 2;
  277.    }
  278.    req->npats = npats;
  279.    req->maxtags = maxtags;
  280.  
  281.    for(i=0;i<npats;i++) {
  282.       nChars = patlens[i];
  283.       /* the size for BufAlloc is careful to be 4-byte aligned */
  284.       space = (2 + nChars + 3) & ~3;
  285.       BufAlloc(char *, data, space);
  286.       *((CARD16 *) data) = nChars;
  287.       bcopy(pats[i], data + 2, patlens[i]);
  288.    }
  289.  
  290.    if(!_XReply(dpy, &rep, 0, xFalse)) {
  291.       /* error receiving reply */
  292.       UnlockDisplay(dpy);
  293.       SyncHandle();
  294.       return NULL;
  295.    }
  296.   
  297.    *total = -1;
  298.    *returned = -1;
  299.  
  300.    rlen = rep.length << 2;
  301.    if(rep.returned > 0) {
  302.       data = (char *) Xmalloc((unsigned) rlen);
  303.       mem = (XYZ_value *) Xmalloc((rep.returned + 1) * sizeof(XYZ_value));
  304.       mem[0].tagname = data;
  305.       vals = &mem[1];
  306.  
  307.       if((!data) || (!vals)) {
  308.      /* memory allocation failure */
  309.      if(data) Xfree(data);
  310.      if(vals) Xfree((char *) mem);
  311.      _XEatData(dpy, (unsigned long) rlen);
  312.      UnlockDisplay(dpy);
  313.      SyncHandle();
  314.      return NULL;
  315.       }
  316.  
  317.       _XReadPad(dpy, data, rlen);
  318.       for(i=0;i<rep.returned;i++) {
  319.      /* grab length of tag name */
  320.      length = *((unsigned short *) data);
  321.      /* advance past tag name length */
  322.      data += sizeof(unsigned short);
  323.      /* save pointer to tag name */
  324.      vals[i].tagname = data;
  325.      /* make sure read value from aligned boundary */
  326.      valptr = (char *)((int)(data + length + 3) & ~3);
  327.      /* save tag value */
  328.      vals[i].value = *((int *)valptr);
  329.      /* null terminate tag name */
  330.      data[length] = '\0';
  331.      /* advance to start of next name/value pair*/
  332.      data= valptr + sizeof(int);
  333.       }
  334.    } else {
  335.       vals = NULL;
  336.    }
  337.    *total = rep.total;
  338.    *returned = rep.returned;
  339.  
  340.    UnlockDisplay(dpy);
  341.    SyncHandle();
  342.    return vals;
  343. }
  344.  
  345. void
  346. XYZ_FreeValueList(values_list)
  347. XYZ_value *values_list;
  348. {
  349.    values_list--;
  350.    Xfree(values_list[0].tagname);
  351.    Xfree(values_list);
  352. }
  353.  
  354. Status
  355. XYZ_ResetValues(dpy)
  356. Display *dpy;
  357. {
  358.    XExtDisplayInfo *info = find_display(dpy);
  359.    xXYZ_ResetValuesReq *req;
  360.  
  361.    XYZ_CheckExtension(dpy, info, 0);
  362.  
  363.    LockDisplay(dpy);
  364.    GetReq(XYZ_ResetValues, req);
  365.    req->reqType = info->codes->major_opcode;
  366.    req->xyzReqType = X_XYZ_ResetValues;
  367.    UnlockDisplay(dpy);
  368.    SyncHandle();
  369.    return 1;
  370. }
  371.  
  372. Status
  373. XYZ_ResetTraceLevels(dpy)
  374. Display *dpy;
  375. {
  376.    XExtDisplayInfo *info = find_display(dpy);
  377.    xXYZ_ResetTraceLevelsReq *req;
  378.  
  379.    XYZ_CheckExtension(dpy, info, 0);
  380.  
  381.    LockDisplay(dpy);
  382.    GetReq(XYZ_ResetTraceLevels, req);
  383.    req->reqType = info->codes->major_opcode;
  384.    req->xyzReqType = X_XYZ_ResetTraceLevels;
  385.    UnlockDisplay(dpy);
  386.    SyncHandle();
  387.    return 1;
  388. }
  389.  
  390.