home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / ibm / AIX / hftUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  27.4 KB  |  1,134 lines

  1. /*
  2.  * $XConsortium: hftUtils.c,v 1.4 91/09/09 13:22:24 rws Exp $
  3.  *
  4.  * Copyright IBM Corporation 1987,1988,1989,1990,1991
  5.  *
  6.  * All Rights Reserved
  7.  *
  8.  * License to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted,
  10.  * provided that the above copyright notice appear in all copies and that
  11.  * both that copyright notice and this permission notice appear in
  12.  * supporting documentation, and that the name of IBM not be
  13.  * used in advertising or publicity pertaining to distribution of the
  14.  * software without specific, written prior permission.
  15.  *
  16.  * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS, AND 
  18.  * NONINFRINGEMENT OF THIRD PARTY RIGHTS, IN NO EVENT SHALL
  19.  * IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23.  * SOFTWARE.
  24.  *
  25. */
  26.  
  27. #include <sys/hft.h>
  28. #include <fcntl.h>
  29.  
  30. #include "hftUtils.h"
  31.  
  32. #include "aixError.h"
  33. #include "compiler.h"
  34. #include "ibmTrace.h"
  35.  
  36. #ifdef AIXV3
  37. #include "X_msg.h"
  38. #endif
  39.  
  40. #ifdef AIXEXTENSIONS
  41. #include "AIX.h"
  42. #include "AIXext.h"
  43. #endif
  44.  
  45. extern  int     hftQFD;
  46. extern  int     hftNeedsReset;
  47. extern  int     AIXDefaultDisplay ;
  48.  
  49. static  CurrentKeyClick = 0;    /* track current key click volume */
  50.  
  51. /***====================================================================***/
  52.  
  53.     /*
  54.      * Defines to simplify dealing with AIX.
  55.      */
  56.  
  57. #define HFWDCHARS(n)    (n)>>8&0xff, (n)&0xff
  58. #define FLWDCHARS(n)    (n)>>24&0xff,(n)>>16&0xff,(n)>>8&0xff,(n)&0xff
  59. #define VTD(n)  HFINTROESC, HFINTROLBR, HFINTROEX, FLWDCHARS(n)
  60.  
  61. #define HFTWRITE(s,m)   \
  62.     if (write(hftQFD,&s,sizeof(s))!=sizeof(s)) {\
  63.             ErrorF(m);\
  64.     }
  65.  
  66. #ifdef AIXV3
  67. #include <sys/termio.h>   /* posix line discipline stuff */
  68.  
  69. int
  70. hftPosix(fd)
  71. int fd;
  72. {
  73.     struct termios  getIO, putIO;
  74.  
  75.     TRACE((" hftPosix enter ok \n"));
  76.     if(tcgetattr(fd,&getIO)<0){
  77.             return -1 ;
  78.     }
  79.  
  80.     if(tcgetattr(fd,&putIO)<0){
  81.             return -1 ;
  82.     }
  83.  
  84.     putIO.c_oflag &= ~OPOST ;
  85.  
  86.     if(tcsetattr(fd,TCSANOW,&putIO)<0){
  87.             return -1 ;
  88.     }
  89.  
  90.     TRACE((" hftPosix exit ok \n"));
  91.     return 0 ;
  92. }
  93.  
  94. #endif
  95.  
  96. /***====================================================================***/
  97.  
  98.  
  99. static  struct  hfkled  hftProtoLED =
  100.     { VTD(sizeof(hftProtoLED)-3),  HFKLEDCH, HFKLEDCL, 2, 1 };
  101.  
  102. /*
  103.  * Turn all lock LED indicators on or off
  104.  */
  105.  
  106. SetLockLEDs (onoff)
  107. int       onoff;
  108. {
  109.     hftProtoLED.hf_ledselect = HFCAPSLOCK | HFSCROLLOCK | HFNUMLOCK;
  110.     hftProtoLED.hf_ledvalue = onoff;
  111.     HFTWRITE(hftProtoLED,"set all LEDs write failed\n");
  112. }
  113.  
  114. /*
  115.  * Turn caps lock LED indicator on or off
  116.  */
  117.  
  118. SetCapsLockLED (onoff)
  119. int       onoff;
  120. {
  121.     hftProtoLED.hf_ledselect = HFCAPSLOCK;
  122.     hftProtoLED.hf_ledvalue = ( onoff ? HFCAPSLOCK : 0 );
  123.     HFTWRITE(hftProtoLED,"set CapsLockLED write failed\n");
  124. }
  125.  
  126. /*
  127.  * Turn num lock LED indicator on or off
  128.  */
  129.  
  130. SetNumLockLED (onoff)
  131. int       onoff;
  132. {
  133.     hftProtoLED.hf_ledselect = HFNUMLOCK;
  134.     hftProtoLED.hf_ledvalue = ( onoff ? HFNUMLOCK : 0 );
  135.     HFTWRITE(hftProtoLED,"set NumLockLED write failed\n");
  136. }
  137.  
  138. /*
  139.  * Turn scroll lock LED indicator on or off
  140.  */
  141.  
  142. SetScrollLockLED (onoff)
  143. int       onoff;
  144. {
  145.     hftProtoLED.hf_ledselect = HFSCROLLOCK;
  146.     hftProtoLED.hf_ledvalue = ( onoff ? HFSCROLLOCK : 0 );
  147.     HFTWRITE(hftProtoLED,"set ScrollLockLED write failed\n");
  148. }
  149.  
  150. void
  151. hftSetLEDS(which,num)
  152. int which ;
  153. int num ;
  154. {
  155.     int onoff ;
  156.  
  157.     /* take a short cut to analyze the mask */
  158.  
  159.     onoff = 0 ;
  160.  
  161.     if (num & 1)
  162.        onoff |= HFCAPSLOCK ;
  163.  
  164.     if (num & 2 )
  165.        onoff |= HFNUMLOCK ;
  166.  
  167.     if (num & 4 )
  168.        onoff |= HFSCROLLOCK ;
  169.  
  170.     SetLockLEDs(onoff);
  171.  
  172.     /* who cares !! */
  173.  
  174. }
  175.  
  176. /***====================================================================***/
  177.  
  178.  
  179. static struct hfloth    hftProtoLocThresh =
  180.     { VTD(sizeof(hftProtoLocThresh)-3), HFLOTHCH, HFLOTHCL, 2, 1 };
  181.  
  182. void
  183. hftSetLocatorThresholds(horz,vert)
  184. unsigned        horz,vert;
  185. {
  186.     TRACE(("hftSetLocatorThresholds(0x%x,0x%x)\n",horz,vert));
  187.     hftProtoLocThresh.hf_hthresh[0]= (horz>>8)&0xff;
  188.     hftProtoLocThresh.hf_hthresh[1]= (horz&0xff);
  189.     hftProtoLocThresh.hf_vthresh[0]= (vert>>8)&0xff;
  190.     hftProtoLocThresh.hf_vthresh[1]= (vert&0xff);
  191.     HFTWRITE(hftProtoLocThresh,"set locator threshold write failed\n");
  192.     return;
  193. }
  194.  
  195. /***====================================================================***/
  196.  
  197. static struct hftdzone  hftProtoDZone =
  198.     { VTD(sizeof(hftProtoDZone)-3), HFTDZCH, HFTDZCL, 2, 1 };
  199.  
  200. void
  201. hftSetTableDeadZone(horz,vert)
  202. unsigned        horz,vert;
  203. {
  204.     TRACE(("hftSetTabletDeadZone(0x%x,0x%x)\n",horz,vert));
  205.     hftProtoDZone.hf_horizontal[0]= (horz>>8)&0xff;
  206.     hftProtoDZone.hf_horizontal[1]= (horz&0xff);
  207.     hftProtoDZone.hf_vertical[0]= (vert>>8)&0xff;
  208.     hftProtoDZone.hf_vertical[1]= (vert&0xff);
  209.     HFTWRITE(hftProtoDZone,"set tablet dead zone write failed\n");
  210.     return;
  211. }
  212.  
  213. /***====================================================================***/
  214.  
  215. static struct hfdial_lpfk       hftProtoSetLPFK =
  216.     { VTD(sizeof(hftProtoSetLPFK)-3), HFLPFKSCH, HFLPFKSCL, 2, 1 };
  217.  
  218. void
  219. hftSetLPFK(keys,flags)
  220. unsigned        keys,flags;
  221. {
  222.     TRACE(("hftSetLPFK(0x%x,0x%x)\n",keys,flags));
  223.     hftProtoSetLPFK.hf_mask.keys[0]= (keys>>24)&0xff;
  224.     hftProtoSetLPFK.hf_mask.keys[1]= (keys>>16)&0xff;
  225.     hftProtoSetLPFK.hf_mask.keys[2]= (keys>>8)&0xff;
  226.     hftProtoSetLPFK.hf_mask.keys[3]= (keys)&0xff;
  227.     hftProtoSetLPFK.hf_data2.lpfk.flags[0]=     (flags>>24)&0xff;
  228.     hftProtoSetLPFK.hf_data2.lpfk.flags[1]=     (flags>>16)&0xff;
  229.     hftProtoSetLPFK.hf_data2.lpfk.flags[2]=     (flags>>8)&0xff;
  230.     hftProtoSetLPFK.hf_data2.lpfk.flags[3]=     (flags)&0xff;
  231.     HFTWRITE(hftProtoSetLPFK,"set LPFK write failed\n");
  232.     return;
  233. }
  234.  
  235. /***====================================================================***/
  236.  
  237. static struct hfdial_lpfk       hftProtoSetGranularity =
  238.     { VTD(sizeof(hftProtoSetGranularity)-3), HFDIALSCH, HFDIALSCL, 2, 1 };
  239.  
  240. void
  241. hftSetDialGranularity(dials,settings)
  242. unsigned        dials;
  243. unsigned char   settings[16];
  244. {
  245. int     i;
  246.  
  247.     TRACE(("hftSetDialGranularity(0x%x,0x%x)\n",dials,settings));
  248.     hftProtoSetGranularity.hf_mask.dials[0]= (dials>>24)&0xff;
  249.     hftProtoSetGranularity.hf_mask.dials[1]= (dials>>16)&0xff;
  250.     hftProtoSetGranularity.hf_mask.dials[2]= (dials>>8)&0xff;
  251.     hftProtoSetGranularity.hf_mask.dials[3]= (dials)&0xff;
  252.     for (i=0;i<16;i++) {
  253.     hftProtoSetGranularity.hf_data2.granularity[i]= settings[i];
  254.     }
  255.     HFTWRITE(hftProtoSetGranularity,"set dial granularity write failed\n");
  256.     return;
  257. }
  258.  
  259. /***====================================================================***/
  260.  
  261. static  struct  hfrconf hftProtoReconfigure;
  262.  
  263. static struct hfsound   hftProtoSound =
  264.     { VTD(sizeof(hftProtoSound)-3), HFSOUNDCH, HFSOUNDCL, 2, 1, HFEXECALWAYS};
  265.  
  266. void
  267. hftSound(vol, duration,frequency)
  268. int             vol;
  269. unsigned        duration;
  270. unsigned        frequency;
  271. {
  272. int     real_vol;
  273.  
  274.      TRACE(("hftSound(%d,%d)\n",duration,frequency));
  275.  
  276.      real_vol = getVolume (vol);
  277.      if (real_vol == 0)
  278.     return;
  279.  
  280.      /* set bell volume */
  281.      if (real_vol != CurrentKeyClick) {
  282.     hftProtoReconfigure.hf_op= HFCHGVOLUME;
  283.     hftProtoReconfigure.hf_obj= real_vol;
  284.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  285.      }
  286.  
  287.      /* set bell duration and frequency, then ring bell */
  288.     hftProtoSound.hf_dur[0]= (duration>>8)&0xff;
  289.     hftProtoSound.hf_dur[1]= (duration&0xff);
  290.     hftProtoSound.hf_freq[0]= (frequency>>8)&0xff;
  291.     hftProtoSound.hf_freq[1]= (frequency&0xff);
  292.     HFTWRITE(hftProtoSound,"generate sound write failed\n");
  293.  
  294.      /* restore old value of keyclick */
  295.      if (real_vol != CurrentKeyClick) {
  296.     /*
  297.      * If the click is off, do not set the volume to 0
  298.      */
  299.     if (CurrentKeyClick != 0) {
  300.       hftProtoReconfigure.hf_obj= CurrentKeyClick;
  301.       ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  302.     }
  303.      }
  304.  
  305.      return;
  306. }
  307.  
  308.  
  309. /***====================================================================***/
  310.  
  311.  
  312. static int
  313. getVolume (volume)
  314. int     volume;
  315. {
  316.     /* volume range 0-100 */
  317.  
  318.     if ((volume >= 1) && (volume <= 33))
  319.         volume = 1;                 /* low */
  320.     else if ((volume >= 34) && (volume <= 66))
  321.         volume = 2;                 /* medium */
  322.     else if (volume >= 67)
  323.         volume = 3;                 /* high */
  324.  
  325.     return (volume);
  326. }
  327.  
  328.  
  329. /***====================================================================***/
  330.  
  331. void
  332. hftSetTypematicDelay(delay)
  333. unsigned        delay;
  334. {
  335.     TRACE(("hftSetTypematicDelay(%d)\n",delay));
  336.     hftProtoReconfigure.hf_op= HFCHGKBDDEL;
  337.     hftProtoReconfigure.hf_obj= delay;
  338.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  339.     return;
  340. }
  341.  
  342. /***====================================================================***/
  343.  
  344. void
  345. hftSetTypematicRate(rate)
  346. unsigned        rate;
  347. {
  348.     TRACE(("hftSetTypematicRate(%d)\n",rate));
  349.     hftProtoReconfigure.hf_op= HFCHGKBDRATE;
  350.     hftProtoReconfigure.hf_obj= rate;
  351.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  352.     return;
  353. }
  354.  
  355. /***====================================================================***/
  356.  
  357. void
  358. hftSetKeyClick(volume)
  359. int     volume;
  360. {
  361.  
  362.     TRACE(("hftSetKeyClick volume is %d hftQFD is %d\n",volume,hftQFD));
  363.  
  364.     if (volume != 0) {
  365.     hftProtoReconfigure.hf_op = HFCHGCLICK;
  366.     hftProtoReconfigure.hf_obj = 1;         /* turn click on */
  367.     ioctl(hftQFD, HFRCONF, &hftProtoReconfigure);
  368.     }
  369.  
  370.     hftProtoReconfigure.hf_obj = CurrentKeyClick = getVolume(volume);
  371.     hftProtoReconfigure.hf_op= HFCHGVOLUME;
  372.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  373.  
  374.     return;
  375. }
  376.  
  377. /***====================================================================***/
  378.  
  379. void
  380. hftSetLocatorSampleRate(rate)
  381. unsigned        rate;
  382. {
  383.     TRACE(("hftSetLocatorSampleRate(%d)\n",rate));
  384.  
  385. #ifdef AIXV3
  386. {
  387.     struct hfchgloc x;
  388.     x.hf_cmd = HFTRATE;
  389.     x.loc_value1 = rate;
  390.     ioctl(hftQFD,HFCHGLOC,&x);
  391. }
  392. #else
  393.     hftProtoReconfigure.hf_op= HFCHGLOCRATE;
  394.     hftProtoReconfigure.hf_obj= rate;
  395.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  396. #endif
  397.  
  398.     return;
  399. }
  400.  
  401. /***====================================================================***/
  402.  
  403. static struct hfqdevidc qdevidcmd =
  404.     { VTD(sizeof(qdevidcmd)-3), HFQDEVIDCH, HFQDEVIDCL };
  405.  
  406. static struct hfquery   query =    { qdevidcmd.hf_intro, sizeof( qdevidcmd ) };
  407. static struct hfrconf   reconfig = { HFSETDD };
  408.  
  409. static struct {
  410.             char    pad;
  411.             char    hf_esc;
  412.             char    hf_lbr;
  413.             char    hf_ex;
  414.             int     hf_len;
  415.             char    hf_typehi;
  416.             char    hf_typelo;
  417.             char    hf_sublen;
  418.             char    hf_subtype;
  419.             short   hf_numdev;
  420.             struct {
  421.                 short       hf_idhi,hf_idlo;
  422.                 short       hf_classhi,hf_classlo;
  423.             } hf_devices[HFT_MAXDEVICES];
  424. }  qDevIDResponse;
  425.  
  426. static  hftDeviceID hftDevices[HFT_MAXDEVICES];
  427. static int  number_of_devices = -1;
  428.  
  429. int
  430. hftQueryDeviceIDs(ppDevices)
  431. hftDeviceID     **ppDevices;
  432. {
  433. int     tempfd= -1;
  434. int     mustClose= FALSE;
  435.  
  436.  
  437.     TRACE(("hftQueryDeviceIDs(0x%x) hftQFD is %d \n",ppDevices,hftQFD));
  438.  
  439.     if( number_of_devices > 0 ){
  440.     TRACE(("using info from previous call\n"));
  441.     *ppDevices = hftDevices;
  442.     return(number_of_devices);
  443.     }
  444.  
  445.     if (hftQFD < 0) {
  446.     tempfd = open("/dev/tty",O_WRONLY);
  447.     if (tempfd<0) {
  448.         tempfd = open("/dev/console",O_WRONLY);
  449.     }
  450.     if (tempfd<0) {
  451.         tempfd = open("/dev/hft",O_WRONLY);
  452.     }
  453.     if (tempfd<0) {
  454.         ErrorF("Cannot open /dev/tty, /dev/console, or /dev/hft to query device ids\n");
  455.         return 0;
  456.     }
  457.     mustClose= TRUE;
  458.     }
  459.     else tempfd= hftQFD;
  460.  
  461.     query.hf_resp=      &qDevIDResponse.hf_esc;
  462.     query.hf_resplen=   sizeof(qDevIDResponse)-1;
  463.     qDevIDResponse.hf_len= sizeof( qDevIDResponse ) - 4;
  464.  
  465. /*
  466.     if tempfd is an hft then put into posix Posix
  467. */
  468.     if ( ioctl( tempfd, HFQUERY, &query ) ) {
  469.     if (mustClose)
  470.         close(tempfd);
  471.     mustClose = FALSE;
  472.     if( (tempfd = open("/dev/hft",O_WRONLY)) < 0 ){
  473. #ifdef AIXV3
  474.         aixErrMsg(M_MSG_8);
  475. #else
  476.         ErrorF( "AIX: unable to query device id\n" );
  477. #endif
  478.         return 0;
  479.     }
  480.     if ( ioctl( tempfd, HFQUERY, &query ) ) {
  481.         close(tempfd);
  482. #ifdef AIXV3
  483.         aixErrMsg(M_MSG_8);
  484. #else
  485.         ErrorF( "AIX: unable to query device id\n" );
  486. #endif
  487.         return 0;
  488.     }
  489.     close(tempfd);
  490.     }
  491.  
  492.     if (mustClose)
  493.     close(tempfd);
  494.  
  495.     MOVE( qDevIDResponse.hf_devices, hftDevices, sizeof(hftDevices) );
  496.     *ppDevices= hftDevices;
  497.  
  498.     number_of_devices = qDevIDResponse.hf_numdev;
  499.  
  500.     {
  501.     int  i;
  502.  
  503.     for ( i = 0;  i < number_of_devices;  ++i ) {
  504.         TRACE(("display %d is 0x%x\n", i, hftDevices[i].hftDevID));
  505.     }
  506.     }
  507.  
  508.     return(qDevIDResponse.hf_numdev);
  509.  
  510. }
  511.  
  512. /***====================================================================***/
  513.  
  514. static struct hfqhftc   qhft =      { VTD(6),
  515.                                   HFQHFTCH, HFQHFTCL } ;
  516.  
  517. static struct hfquery   ttquery =    { qhft.hf_intro, sizeof( qhft ) };
  518.  
  519. unsigned
  520. hftQueryCurrentDevice()
  521. {
  522. int             tempfd= -1;
  523. unsigned        *retval;
  524. volatile struct hfqhftr resp;
  525.  
  526.     TRACE(("hftQueryCurrentDevice() hftQFD is %d\n",hftQFD));
  527.  
  528.     if (hftQFD<0) {
  529.     tempfd = open("/dev/tty",O_WRONLY);
  530.     if (tempfd<0) {
  531.         ErrorF("Cannot open /dev/tty to query device id\n");
  532.         return 0;
  533.     }
  534.     }
  535.     else tempfd= hftQFD;
  536.  
  537.     ttquery.hf_resp    = resp.hf_intro;
  538.     ttquery.hf_resplen = sizeof( resp ) + 1 ;
  539.  
  540. /*
  541.     if tempfd is an hft then put into posix Posix
  542. */
  543.  
  544.     if ( ioctl( tempfd, HFQUERY, &ttquery ) ) {
  545.     ErrorF( "AIX: unable to query tty device id\n" );
  546.     if (tempfd!=hftQFD)
  547.         close(tempfd);
  548.     return 0;
  549.     }
  550.  
  551.     if (tempfd!=hftQFD)
  552.     close(tempfd);
  553.  
  554.     return resp.hf_phdevid ;
  555.  
  556. }
  557.  
  558. unsigned
  559. hftQueryDeviceID(fd)
  560.     int  fd;
  561. {
  562.     volatile struct hfqhftr  resp;
  563.  
  564.     TRACE(("hftQueryDeviceID(fd=%d)\n",fd));
  565.  
  566.     ttquery.hf_resp    = resp.hf_intro;
  567.     ttquery.hf_resplen = sizeof( resp ) + 1 ;
  568.  
  569.     if ( ioctl(fd, HFQUERY, &ttquery ) ) {
  570.     ErrorF( "AIX: unable to query tty device id\n" );
  571.     return 0;
  572.     }
  573.     TRACE(("hftQueryDeviceID(devid=0x%x)\n",resp.hf_phdevid));
  574.  
  575.     return resp.hf_phdevid ;
  576. }
  577.  
  578. volatile struct hfqhftr resp1;
  579. static struct hfquery   query1 =    { qhft.hf_intro, sizeof(qhft) } ;
  580.  
  581. int
  582. hftGetKeyboardID ()
  583. {
  584. int     tempfd;
  585.  
  586.     TRACE(("hftGetKeyboardID hftQFD is %d\n",hftQFD));
  587.  
  588.     query1.hf_resp    = resp1.hf_intro;
  589.     query1.hf_resplen = sizeof( resp1 ) + 1 ;
  590.  
  591.     tempfd= hftQFD; /* fix this */
  592.  
  593.     if ( ioctl( tempfd, HFQUERY, &query1 ) ) {
  594. #ifdef AIXV3
  595.     aixErrMsg(M_MSG_7);
  596. #else
  597.     ErrorF ("AIX: unable to querry current keyboard id\n");
  598. #endif
  599.     return (HFT_ILLEGAL_KEYBOARD);
  600.     }
  601.  
  602.     return (resp1.hf_phkbdid);
  603. }
  604.  
  605. /***====================================================================***/
  606.  
  607. int
  608. hftSetDefaultDisplay(idWanted)
  609. int     idWanted;
  610. {
  611. int              fd;
  612.  
  613.     TRACE(("hftSetDefaultDisplay(idWanted=0x%x)\n",idWanted));
  614.  
  615.     if (idWanted == 0) {
  616.     return TRUE;
  617.     }
  618.  
  619.     if (reconfig.hf_obj=hftHasAnAttached(idWanted)) {
  620.     if ((hftQueryDefaultDisplay()&HFT_DEVID_MASK)!=idWanted) {
  621.         fd= open("/dev/console",O_WRONLY);
  622.         if (fd<0) {
  623.             ErrorF("Unable to open /dev/console to set default display\n");
  624.             return FALSE;
  625.         }
  626. /*
  627.     if tempfd is an hft then put into posix Posix
  628. */
  629.         if ( ioctl( fd, HFRCONF, &reconfig ) ) {
  630.             ErrorF( "AIX: unable to change default display\n" );
  631.             close(fd);
  632.             return FALSE;
  633.         }
  634.         close (fd);
  635.     }
  636.     }
  637.     else {
  638.     ErrorF( "requested default display not attached\n" );
  639.     return FALSE;
  640.     }
  641.  
  642.     return TRUE;
  643. }
  644.  
  645. extern void ibmInfoMsg() ;
  646.  
  647. /***====================================================================***/
  648.  
  649. unsigned
  650. hftQueryDefaultDisplay()
  651. {
  652. hftDeviceID     *pDevices;
  653.  
  654.     TRACE(("hftQueryDefaultDisplay()\n"));
  655.  
  656.     if (hftQueryDeviceIDs(&pDevices)) {
  657.     return(pDevices[0].hftDevID&HFT_DEVID_MASK);
  658.     }
  659.     return(HFT_ILLEGAL_ID);
  660. }
  661.  
  662. /***====================================================================***/
  663.  
  664. int
  665. hftHasAnAttached(devId)
  666. unsigned int devId;
  667. {
  668. int              i;
  669. int              nDevices;
  670. hftDeviceID     *pDevices;
  671.  
  672.     TRACE(("hftHasAnAttached(0x%x)\n",devId));
  673.  
  674.     nDevices= hftQueryDeviceIDs(&pDevices);
  675.  
  676.     if (nDevices==0)
  677.     return(FALSE);
  678.  
  679.     for ( i = 0;  i < nDevices;  ++i ) {
  680.     if ( (pDevices[i].hftDevID&HFT_DEVID_MASK) == (devId&HFT_DEVID_MASK) ) {
  681.         return pDevices[i].hftDevID;
  682.     }
  683.     }
  684.  
  685.     return FALSE;
  686. }
  687.  
  688. int
  689. hftHasAnAttachedNumber(devId, devNum)
  690. unsigned int devId;
  691. unsigned int devNum;
  692. {
  693. int              i;
  694. int              nDevices;
  695. hftDeviceID     *pDevices;
  696.  
  697.     TRACE(("hftHasAnAttachedNumber(devId=0x%x, devNum=%d)\n",devId,devNum));
  698.  
  699.     nDevices= hftQueryDeviceIDs(&pDevices);
  700.  
  701.     if (nDevices==0)
  702.     return(FALSE);
  703.  
  704.     for ( i = 0;  i < nDevices;  ++i ) {
  705.     TRACE(("hftHasAnAttachedNumber(hftDevID=0x%08x)\n",pDevices[i].hftDevID));
  706.     TRACE(("hftHasAnAttachedNumber(   devID=0x%08x)\n",devId));
  707.     TRACE(("hftHasAnAttachedNumber(   devNum=0x%08x)\n",devNum));
  708.     if ( (pDevices[i].hftDevID&HFT_DEVID_MASK) == (devId&HFT_DEVID_MASK) /*&&*/
  709.     /*(pDevices[i].hftDevID&HFT_DEVNUM_MASK) == (devNum&HFT_DEVNUM_MASK)*/ )
  710.     {
  711.         return pDevices[i].hftDevID;
  712.     }
  713.     }
  714.  
  715.     TRACE(("hftHasAnAttachedNumber(returning FALSE)\n"));
  716.     return FALSE;
  717.  
  718. }
  719.  
  720. struct hfchgdsp  hftcd = {
  721.     '\033','[','x',0,0,0,sizeof(struct hfchgdsp)-3,
  722.     HFCHGDSPCH, HFCHGDSPCL, 2,0,
  723.     0,0,  0,0,0,0,0,0,0,0,  0,0,0,0,  0,0,0,0,0,0,0,0,0,0
  724. };
  725.  
  726.  
  727. int
  728. hftChangeVTToPhysicalDisplay(fd, device)
  729.     int  fd;
  730.     unsigned  device;
  731. {
  732.     TRACE(("hftChangeVTToPhysicalDisplay(fd=0%d,device=0x%x)\n",fd,device));
  733.  
  734.     hftcd.hf_mode[0] = HFNONDEF;
  735.     hftcd.hf_devid[0]= (device>>24)&0xff;
  736.     hftcd.hf_devid[1]= (device>>16)&0xff;
  737.     hftcd.hf_devid[2]= (device>>8)&0xff;
  738.     hftcd.hf_devid[3]= (device)&0xff;
  739.  
  740.     return( write( fd, &hftcd, sizeof(hftcd) ) == sizeof(hftcd) );
  741. }
  742.  
  743. int
  744. hftOpenOnDevice(device)
  745.     unsigned int  device;
  746. {
  747.     int  fd;
  748.  
  749.     TRACE(("hftOpenOnDevice(device=0x%x)\n",device));
  750.  
  751.     if( (fd=open("/dev/hft", O_RDWR)) < 0 ){
  752.     return -1;
  753.     }
  754.     hftPosix(fd);
  755.     if( hftChangeVTToPhysicalDisplay(fd, device) ){
  756.     return fd;
  757.     }
  758.     close(fd);
  759.  
  760.     return -1;
  761. }
  762.  
  763. void hftNoHotkeyVT();
  764.  
  765. void
  766. hftSetQFDOrNoHotkey(fd)
  767.     int  fd;
  768. {
  769.     TRACE(("hftSetQFDOrNoHotkey(fd=%d)\n",fd));
  770.  
  771.     if( hftQFD < 0 ){
  772.     /* use this fd for Q */
  773.     hftQFD = fd;
  774.     }
  775.     else
  776.     {
  777.     hftNoHotkeyVT(fd);
  778.     /* disable this fd from hotkey */
  779.     }
  780. }
  781.  
  782. /* need to declare as void before we use it */
  783. void hftActivateVT();
  784.  
  785. void
  786. hftManagerCommand(fd, hfcmd)
  787.     int  fd;
  788.     long  hfcmd;
  789. {
  790.     struct hftsmgrcmd  cmd;
  791.     struct hftqdresp  qdev;
  792.  
  793.     TRACE(("hftManagerCommand(fd=%d, hfcmd=0x%x)\n",fd,hfcmd));
  794.  
  795.     if( ioctl(fd, HFTQDEV, &qdev) == -1 ){
  796.     ErrorF("hftManagerCommand(fd, hfcmd): ioctl HFTQDEV failed");
  797.     return;
  798.     }
  799.  
  800.     cmd.hf_cmd = hfcmd;
  801.     cmd.hf_vtid = qdev.hf_vtid;
  802.     cmd.hf_vsid = 0L;
  803.  
  804.     if( ioctl(fd, HFTCSMGR, &cmd) == -1 ){
  805.     ErrorF("hftManagerCommand(fd, hfcmd): ioctl HFTCSMGR failed");
  806.     }
  807. }
  808.  
  809. void
  810. hftActivateVT(fd)
  811.     int  fd;
  812. {
  813.     TRACE(("hftActivateVT(fd=%d)\n",fd));
  814.     hftManagerCommand(fd, SMACT);
  815. }
  816.  
  817. void
  818. hftHideVT(fd)
  819.     int  fd;
  820. {
  821.     TRACE(("hftHideVT(fd=%d)\n",fd));
  822.     hftManagerCommand(fd, SMHIDE);
  823. }
  824.  
  825. void
  826. hftUnHideVT(fd)
  827.     int  fd;
  828. {
  829.     TRACE(("hftUnHideVT(fd=%d)\n",fd));
  830.     hftManagerCommand(fd, SMUNHIDE);
  831. }
  832.  
  833. void
  834. hftNoHotkeyVT(fd)
  835.     int  fd;
  836. {
  837.     TRACE(("hftNoHotkeyVT(fd=%d)\n",fd));
  838.     hftManagerCommand(fd, SMNOHOTKEY);
  839. }
  840.  
  841. struct hfqretractc  whichretract_command = {
  842.     '\033','[','x',0,0,0,sizeof(struct hfqretractc)-3,
  843.     HFQRETRACTCH, HFQRETRACTCL
  844. };
  845. struct hfqretractr  whichretract_response = {
  846.     '\033','[','x',0,0,0,sizeof(struct hfqretractr)-3,
  847.     HFQRETRACTRH, HFQRETRACTRL, 2,0,
  848.     0,0, 0,0,0,0
  849. };
  850. struct hfqretract   whichgotretract = {
  851.     &whichretract_command,
  852.     sizeof(struct hfqretractc),
  853.     &whichretract_response,
  854.     sizeof(struct hfqretractr)
  855. };
  856.  
  857. unsigned
  858. WhoIsGettingTheRetract()
  859. {
  860.     int  vtid;
  861.     unsigned  devid;
  862.  
  863.     TRACE(("WhoIsGettingTheRetract\n"));
  864.  
  865.     if ( ioctl( hftQFD, HFQUERY, &whichgotretract) ) {
  866.     ErrorF("AIX: unable to query who get the retract\n");
  867.     return;
  868.     }
  869.  
  870.     vtid = ((unsigned)whichretract_response.hf_vtid[0]) << 8;
  871.     vtid |= ((unsigned)whichretract_response.hf_vtid[1]);
  872.     devid = ((unsigned)whichretract_response.hf_devid[0]) << (3*8);
  873.     devid |= ((unsigned)whichretract_response.hf_devid[1]) << (2*8);
  874.     devid |= ((unsigned)whichretract_response.hf_devid[2]) << (1*8);
  875.     devid |= ((unsigned)whichretract_response.hf_devid[3]);
  876.  
  877.     TRACE(("WhoIsGettingTheRetract  vtid=0x%x  devid=0x%x\n", vtid, devid));
  878.  
  879.     return devid;
  880. }
  881.  
  882. #ifdef AIXEXTENSIONS
  883.  
  884. extern AIXInfoRec aixInfo;
  885. extern int aixTabletScaleX,aixTabletScaleY ;
  886.  
  887. #ifdef AIXV3
  888.   struct hftqdresp hfqdstuff ;
  889. #else
  890.   union
  891.   {
  892.     struct hfqdev   qdev;
  893.     struct hfqdresp qdresp;
  894.   } hfqdstuff;
  895. #endif
  896.  
  897. #ifdef AIXV3
  898. static struct hfqgraphdev qlocdev = { VTD( sizeof(qlocdev) - 3 ),
  899.                                   HFQTABLETCH, HFQTABLETCL };
  900. #else
  901. static struct hfqgraphdev qlocdev = { VTD( sizeof(qlocdev) - 3 ),
  902.                                   HFQLOCCH, HFQLOCCL };
  903. #endif
  904.  
  905. union {
  906.    struct hfqlocr loc;
  907.    char junk[sizeof(struct hfqlocr)+1];
  908. }   resp3;
  909.  
  910. static struct hfquery   query3 =    { qlocdev.hf_intro, sizeof(qlocdev),
  911.                                   resp3.junk, sizeof( resp3 ) };
  912. #ifdef AIXTABLET
  913. static struct hfqphdevc qphdev =    { VTD( sizeof(struct hfqphdevc) - 3 ),
  914.                                   HFQPDEVCH, HFQPDEVCL, 2, 0, FLWDCHARS(0) }
  915. ;
  916. static struct hfqphdevr phdevresp;
  917. static struct hfqphdev  phdevquery ={ &qphdev, sizeof(struct hfqphdevc),
  918.                                   &phdevresp, sizeof(struct hfqphdevr) };
  919. #endif AIXTABLET
  920.  
  921. void
  922. hftQueryHardwareConfig(device)
  923. unsigned        device;
  924. {
  925.  
  926.     TRACE(("hftQueryHardwareConfig(0x%x)\n",device));
  927.  
  928. #ifdef AIXV3
  929.     hfqdstuff.hf_vtid = 10 ;  /* check this value */
  930.     hfqdstuff.hf_dev = 20 ;  /* check this value */
  931.                          /* devname */
  932.     hfqdstuff.hf_numdisp = 1;
  933. #else
  934.     hfqdstuff.qdev.hf_qdrsvd = 0;
  935.     hfqdstuff.qdev.hf_qdopts = 2;
  936.     hfqdstuff.qdev.hf_qdlen =
  937.        sizeof(hfqdstuff) - sizeof(hfqdstuff.qdev);
  938. #endif
  939.  
  940. #ifdef AIXV3
  941.     if (ioctl(hftQFD, HFTQDEV, &hfqdstuff)) {
  942.     aixErrMsg(M_MSG_10);
  943.     }
  944.     else {
  945.     struct hfdial_lpfk r;      /* response structure of dials & lpfks */
  946.     struct hfqdial_lpfk q;     /* query structure of dials & lpfks */
  947.     struct hfqgraphdev hf_cmd; /* structure for hft command */
  948.  
  949.     aixInfo.kbdiodn = hfqdstuff.hf_kbddev ;
  950. #ifdef AIXTABLET
  951.     if ( ioctl(hftQFD , HFQUERY, &phdevquery ) ) {
  952.         long err_ret;
  953.         ioctl(hftQFD, HFQERROR, &err_ret);
  954.  
  955.         /* err_ret 1801 is response buffer overflow, ignore this error */
  956.         if (err_ret != 1801) {
  957.             ErrorF("AIX: unable to query physical device\n" );
  958.             ErrorF("exiting\n");
  959.         }
  960.     }
  961.     TRACE(("hftQueryHardwareConfig: locattr = %x\n",
  962.             phdevresp.hf_locattr[0]));
  963.  
  964.     if ( ! (phdevresp.hf_locattr[0] & HFLOCREL ) )
  965.         aixInfo.mouseiodn = 0 ;
  966.     else
  967.  
  968.     aixInfo.mouseiodn = hfqdstuff.hf_mousedev ;
  969.     TRACE(("MOUSEDEV : mouseiodn is %x\n",aixInfo.mouseiodn));
  970.  
  971.     if ( ! (phdevresp.hf_locattr[0] & HFLOCABS ) )
  972.         aixInfo.mouseiodn = 0 ;
  973.     else
  974.  
  975.     aixInfo.tabletiodn = hfqdstuff.hf_tabletdev ;
  976.     TRACE(("TABLETDEV : tabletiodn is %x\n",aixInfo.tabletiodn));
  977. #else
  978.     aixInfo.lociodn = hfqdstuff.hf_mousedev ;
  979.     TRACE(("MOUSEDEV : lociodn is %d\n",aixInfo.lociodn));
  980.     aixInfo.lociodn = hfqdstuff.hf_tabletdev ;
  981.     TRACE(("TABLETDEV : lociodn is %x\n",aixInfo.lociodn));
  982. #endif AIXTABLET
  983.  
  984.     hf_cmd.hf_intro[0] = HFINTROESC;
  985.     hf_cmd.hf_intro[1] = HFINTROLBR;
  986.     hf_cmd.hf_intro[2] = HFINTROEX;
  987.     hf_cmd.hf_intro[3] = (sizeof(struct hfqgraphdev) - 3) >> 24 & 0xff;
  988.     hf_cmd.hf_intro[4] = (sizeof(struct hfqgraphdev) - 3) >> 16 & 0xff;
  989.     hf_cmd.hf_intro[5] = (sizeof(struct hfqgraphdev) - 3) >> 8 & 0xff;
  990.     hf_cmd.hf_intro[6] = (sizeof(struct hfqgraphdev) - 3) & 0xff;
  991.  
  992.     /* Query for dials device */
  993.     hf_cmd.hf_intro[7] = HFQDIALSCH;
  994.     hf_cmd.hf_intro[8] = HFQDIALSCL;
  995.     q.hf_cmd = &hf_cmd;
  996.     q.hf_cmdlen = sizeof(struct hfqgraphdev);
  997.     q.hf_resp = &r;
  998.     q.hf_resplen = sizeof(struct hfdial_lpfk);
  999.     if (ioctl(hftQFD, HFQUERY, &q)) /* returns -1 if dials not available */
  1000.     {
  1001.         long error;
  1002.         ioctl(hftQFD, HFQERROR, &error);
  1003.         TRACE(("Query Dials: error code = %ld\n",error));
  1004.         aixInfo.dialiodn = 0;
  1005.     }
  1006.     else
  1007.  
  1008.     aixInfo.dialiodn = hfqdstuff.hf_dialsdev ;
  1009.  
  1010.     /* Query for lpfks device */
  1011.     hf_cmd.hf_intro[7] = HFQLPFKSCH;
  1012.     hf_cmd.hf_intro[8] = HFQLPFKSCL;
  1013.     q.hf_cmd = &hf_cmd;
  1014.     q.hf_cmdlen = sizeof(struct hfqgraphdev);
  1015.     q.hf_resp = &r;
  1016.     q.hf_resplen = sizeof(struct hfdial_lpfk);
  1017.     if (ioctl(hftQFD, HFQUERY, &q)) /* returns -1 if lpfks not available */
  1018.     {
  1019.         long error;
  1020.         ioctl(hftQFD, HFQERROR, &error);
  1021.         TRACE(("Query Dials: error code = %ld\n",error));
  1022.         aixInfo.lpfkiodn = 0;
  1023.     }
  1024.     else
  1025.  
  1026.     aixInfo.lpfkiodn = hfqdstuff.hf_lpfkdev ;
  1027.  
  1028.     TRACE(("DIALDEV : dialiodn is %x\n",aixInfo.dialiodn));
  1029.     TRACE(("LPFKDEV : lpfkiodn is %x\n",aixInfo.lpfkiodn));
  1030.     }
  1031.  
  1032. #else
  1033.     if (ioctl(hftQFD, HFQDEV, &hfqdstuff)) {
  1034.     ErrorF("AIX: unable to query hardware config\n" );
  1035.     ErrorF("exiting\n");
  1036.     }
  1037.     else {
  1038.     aixInfo.kbdiodn = hfqdstuff.qdresp.hf_keyiodn ;
  1039.     aixInfo.lociodn = hfqdstuff.qdresp.hf_lociodn ;
  1040.     aixInfo.dialiodn = hfqdstuff.qdresp.hf_dialsiodn ;
  1041.     aixInfo.lpfkiodn = hfqdstuff.qdresp.hf_lpfkiodn ;
  1042.     }
  1043. #endif
  1044.  
  1045.     aixInfo.dpsid =  1 ; /* we have dps */
  1046.  
  1047.     switch(device & 0xffff000) {
  1048.  
  1049.     case HFT_SKYWAY_ID:     aixInfo.displayid = XDEV_IBM_SKYWAY ;
  1050.                             break;
  1051.     case HFT_SKYMONO_ID:    aixInfo.displayid = XDEV_IBM_SKYMONO ;
  1052.                             break;
  1053.     case HFT_SABINE_ID :    aixInfo.displayid = XDEV_IBM_SABINE ;
  1054.                             break;
  1055.     case HFT_GEMINI_ID:     aixInfo.displayid = XDEV_IBM_GEMINI ;
  1056.                             break;
  1057.     case HFT_PEDER_ID:      aixInfo.displayid = XDEV_IBM_PEDER ;
  1058.                             break;
  1059.     default:                aixInfo.displayid = XDEV_IBM_GAI ;
  1060.     }
  1061.  
  1062. #ifdef AIXTABLET
  1063.     if ( ioctl(hftQFD , HFQUERY, &phdevquery ) ) {
  1064.     long err_ret;
  1065.     ioctl(hftQFD, HFQERROR, &err_ret);
  1066.  
  1067.     /* err_ret 1801 is response buffer overflow, ignore this error */
  1068.     if (err_ret != 1801) {
  1069.             ErrorF("AIX: unable to query physical device\n" );
  1070.             ErrorF("exiting\n");
  1071.     }
  1072.     }
  1073. #endif AIXTABLET
  1074.  
  1075. #ifdef AIXV3
  1076.     if ( ioctl(hftQFD , HFQUERY, &query3 ) ) {
  1077.     ErrorF("AIX: unable to query tablet\n" );
  1078.     ErrorF("exiting\n");
  1079.     }
  1080. #else
  1081.     if ( ioctl(hftQFD , HFQUERY, &query3 ) ) {
  1082.     ErrorF("AIX: unable to query locator\n" );
  1083.     ErrorF("exiting\n");
  1084.     }
  1085. #endif
  1086.  
  1087. #ifdef AIXV3
  1088. #ifdef AIXTABLET
  1089.     TRACE(("hftQueryHardwareConfig: locattr = %x\n",phdevresp.hf_locattr[0]));
  1090.     if ( phdevresp.hf_locattr[0] == HFLOCABS ) {
  1091. #else
  1092.     TRACE(("hftQueryHardwareConfig: devinfo = %x\n",resp3.loc.hf_devinfo[0]));
  1093.     if ( resp3.loc.hf_devinfo[0] & HFLOCABS ) {
  1094. #endif AIXTABLET
  1095.     /* scale factor = max_cnt * resolution / 1000 */
  1096.     int resolution;
  1097.     resolution = (resp3.loc.hf_resolution[2] << 8) +
  1098.                   resp3.loc.hf_resolution[3];
  1099.     aixTabletScaleX = ((resp3.loc.hf_horzmax_cnt[0] << 8)
  1100.                       + resp3.loc.hf_horzmax_cnt[1])
  1101.                       * resolution / 1000;
  1102.     aixTabletScaleY = ((resp3.loc.hf_vertmax_cnt[0] << 8)
  1103.                       + resp3.loc.hf_vertmax_cnt[1])
  1104.                       * resolution / 1000;
  1105. #else
  1106.    if ( resp3.loc.hf_devinfo[0] & HFLOCABS ) {
  1107.     aixTabletScaleX = (resp3.loc.hf_horzmax_cnt[0] << 8)
  1108.                       + resp3.loc.hf_horzmax_cnt[1];
  1109.     aixTabletScaleY = (resp3.loc.hf_vertmax_cnt[0] << 8)
  1110.                       + resp3.loc.hf_vertmax_cnt[1];
  1111. #endif AIXV3
  1112. #ifdef AIXTABLET
  1113.     if ( phdevresp.hf_locattr[0] == HFLOCREL ) {
  1114.         aixInfo.loctype = DEVMOUSE ;
  1115.         aixInfo.lociodn = aixInfo.mouseiodn ;
  1116.     }
  1117.     else {
  1118.         aixInfo.loctype = DEVTABLET ;
  1119.         aixInfo.lociodn = aixInfo.tabletiodn ;
  1120.     }
  1121. #else
  1122.     aixInfo.loctype = DEVTABLET ;
  1123. #endif AIXTABLET
  1124.     }
  1125.     else {
  1126.     aixInfo.loctype = DEVMOUSE ;
  1127.     }
  1128.     TRACE(("hftQueryHardwareConfig: loctype = %d\n",aixInfo.loctype));
  1129.     aixInfo.kbdid = hftGetKeyboardID();
  1130.     aixInfo.inputfd = hftQFD ;
  1131.     return;
  1132. }
  1133. #endif
  1134.