home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xibm.zip / AIX / hftUtils.c < prev    next >
C/C++ Source or Header  |  1992-02-11  |  23KB  |  911 lines

  1. /*
  2.  * $Id: hftUtils.c,v 5.1 1992/02/12 00:21:36 jfc Exp $
  3.  *
  4.  * Copyright IBM Corporation 1987,1988,1989
  5.  *
  6.  * All Rights Reserved
  7.  *
  8.  * Permission 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 AND FITNESS, IN NO EVENT SHALL
  18.  * IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22.  * SOFTWARE.
  23.  *
  24. */
  25. #include <sys/hft.h>
  26. #include <sys/time.h>
  27. #include <fcntl.h>
  28.  
  29. #include "hftUtils.h"
  30.  
  31. #include "OScompiler.h"
  32. #include "ibmTrace.h"
  33. #include "ibmXhftMap.h"
  34.  
  35. extern    int    hftPending;
  36. extern    int    hftQFD;
  37. extern    int    hftNeedsReset;
  38. extern    int    AIXDefaultDisplay ;
  39.  
  40. static    CurrentKeyClick = 0;    /* track current key click volume */
  41.  
  42. /***====================================================================***/
  43.  
  44.     /*
  45.      * Defines to simplify dealing with AIX.
  46.      */
  47.  
  48. #define    HFWDCHARS(n)    (n)>>8&0xff, (n)&0xff
  49. #define    FLWDCHARS(n)    (n)>>24&0xff,(n)>>16&0xff,(n)>>8&0xff,(n)&0xff
  50. #define    VTD(n)    HFINTROESC, HFINTROLBR, HFINTROEX, FLWDCHARS(n)
  51.  
  52.  
  53. #define    HFTWRITE(s,m)    \
  54.     if (write(hftQFD,&s,sizeof(s))!=sizeof(s)) {\
  55.         ErrorF(m);\
  56.     }
  57.  
  58. /***====================================================================***/
  59. #ifdef AIXrt
  60. int    mpel_hft_id_list[]    = {HFT_MEGAPEL_ID, 0} ;
  61. int    apa16_hft_id_list[]   = {HFT_APA16_ID,   0} ;
  62. int    apa8_hft_id_list[]    = {HFT_APA8_ID,    0} ;
  63. int    apa8c_hft_id_list[]   = {HFT_APA8C_ID,   0} ;
  64. int    ega_hft_id_list[]     = {HFT_EGA_ID,     0} ;
  65. #else
  66. #ifdef AIXps2
  67. int    vga_hft_id_list[]     = {HFT_VGA_8503_ID,   HFT_VGA_8512_ID,
  68.                  HFT_VGA_8513_ID,   HFT_VGA_8514_ID,
  69.                  HFT_VGA_8507_ID,   HFT_VGA_8604_ID,   0} ;
  70. int    ibm8514_hft_id_list[] = {HFT_8514A_8503_ID, HFT_8514A_8512_ID,
  71.                  HFT_8514A_8513_ID, HFT_8514A_8514_ID,
  72.                  HFT_8514A_8507_ID, HFT_8514A_8604_ID, 0} ;
  73. #else
  74. ERROR!!! MUST BE RT/PC or PS/2 386
  75. #endif /* AIXps2 */
  76. #endif /* AIXrt */
  77.  
  78. static int getVolume();
  79.  
  80. int
  81. hftProbe( adapter )
  82.      unsigned adapter ;        /* adapter type   */
  83. {
  84.     int        *plist, *pcurrent ;
  85.     hftDeviceID *pDevices ;
  86.     int         nDevices ;
  87.     int         deviceID ;
  88.     int        i ;
  89.  
  90.     TRACE( ( "hftProbe(adapter=%d)\n", adapter ) ) ;
  91.  
  92.     switch ( adapter )
  93.     {
  94. #ifdef AIXrt
  95.       case MEGAPELxHFTid :
  96.     plist = mpel_hft_id_list ;
  97.     break ;
  98.       case APA16xHFTid :
  99.     plist = apa16_hft_id_list ;
  100.     break ;
  101.       case APA8xHFTid :
  102.     plist = apa8_hft_id_list ;
  103.     break ;
  104.       case EGAxHFTid :
  105.     plist = ega_hft_id_list ;
  106.     break ;
  107.       case APA8CxHFTid :
  108.     plist = apa8c_hft_id_list ;
  109.     break ;
  110. #endif /* AIXrt */
  111.  
  112. #ifdef AIXps2
  113.       case VGAxHFTid :
  114.     plist = vga_hft_id_list ;
  115.     break ;
  116.       case IBM8514xHFTid :
  117.     plist = ibm8514_hft_id_list ;
  118.     break ;
  119. #endif /* AIXps2 */
  120.  
  121.       default:
  122.     ErrorF( "hftProbe: unknown adapter=%d\n" ) ;
  123.     return -1 ;
  124.     }                /* End Switch */
  125.  
  126.     if ( !( nDevices = hftQueryDeviceIDs( &pDevices ) ) )
  127.     return -1 ;        /* Can't Find it ! Nothing Available ! */
  128.  
  129.     for (i = 0; i < nDevices ; i++ )
  130.     {
  131.     deviceID = pDevices[i].hftDevID & HFT_DEVID_MASK ;
  132.     for ( pcurrent = plist ; *pcurrent ; )
  133.     {
  134.         if ( *pcurrent++ == deviceID )
  135.         {
  136.         AIXDefaultDisplay = deviceID ;
  137.         /* We MUST have permissions on return !! */
  138. #ifndef AIXrt
  139.         /* This is the only place we hftInitQueue */
  140.         hftInitQueue( deviceID, FALSE ) ;
  141.         return hftQFD ; /* "Magic" number */
  142. #else
  143.         return MAX( hftQFD, 0 ) ; /* "Magic" number */
  144. #endif
  145.         }
  146.     }
  147.     }
  148.     /* Didn't Find it ! */
  149.     return -1 ;
  150. }
  151.  
  152. /***====================================================================***/
  153.  
  154. static    struct    hfkled    hftProtoLED =
  155.     { VTD(sizeof(hftProtoLED)-3),  HFKLEDCH, HFKLEDCL, 2, 0 };
  156.  
  157. /*
  158.  * Turn all lock LED indicators on or off
  159.  */
  160.  
  161. SetLockLEDs (onoff)
  162.      int onoff;
  163. {
  164.     hftProtoLED.hf_ledselect = HFCAPSLOCK | HFSCROLLOCK | HFNUMLOCK;
  165.     hftProtoLED.hf_ledvalue = onoff;
  166.     HFTWRITE(hftProtoLED,"set all LEDs write failed\n");
  167. }
  168.  
  169. /*
  170.  * Turn caps lock LED indicator on or off
  171.  */
  172.  
  173. void
  174. SetCapsLockLED (onoff)
  175.      int onoff;
  176. {
  177.     hftProtoLED.hf_ledselect = HFCAPSLOCK;
  178.     hftProtoLED.hf_ledvalue = ( onoff ? HFCAPSLOCK : 0 );
  179.     HFTWRITE(hftProtoLED,"set CapsLockLED write failed\n");
  180.     return ;
  181. }
  182.  
  183. /*
  184.  * Turn num lock LED indicator on or off
  185.  */
  186. void
  187. SetNumLockLED (onoff)
  188.      int onoff;
  189. {
  190.     hftProtoLED.hf_ledselect = HFNUMLOCK;
  191.     hftProtoLED.hf_ledvalue = ( onoff ? HFNUMLOCK : 0 );
  192.     HFTWRITE(hftProtoLED,"set NumLockLED write failed\n");
  193.     return ;
  194. }
  195.  
  196. /*
  197.  * Turn scroll lock LED indicator on or off
  198.  */
  199. void
  200. SetScrollLockLED (onoff)
  201.      int onoff;
  202. {
  203.     hftProtoLED.hf_ledselect = HFSCROLLOCK;
  204.     hftProtoLED.hf_ledvalue = ( onoff ? HFSCROLLOCK : 0 );
  205.     HFTWRITE(hftProtoLED,"set ScrollLockLED write failed\n");
  206.     return ;
  207. }
  208.  
  209. void
  210. hftSetLEDS(which,num)
  211.      int which ;
  212.      int num ;
  213. {
  214.     /* take a short cut to analyze the mask */
  215.     if (num & 1)
  216.     SetCapsLockLED(1);
  217.     else
  218.     SetCapsLockLED(0);
  219.  
  220.     if (num & 2 )
  221.     SetNumLockLED(1);
  222.     else
  223.     SetNumLockLED(0);
  224.  
  225.     if (num & 4 )
  226.     SetScrollLockLED(1);
  227.     else
  228.     SetScrollLockLED(0);
  229.  
  230.     /* who cares !! */
  231.     return ;
  232. }
  233.  
  234. /***====================================================================***/
  235.  
  236. static struct hfloth    hftProtoLocThresh =
  237.     { VTD(sizeof(hftProtoLocThresh)-3), HFLOTHCH, HFLOTHCL, 2, 1 };
  238.  
  239. void
  240. hftSetLocatorThresholds(horz,vert)
  241.      unsigned    horz,vert;
  242. {
  243.     TRACE(("hftSetLocatorThresholds(0x%x,0x%x)\n",horz,vert));
  244.     hftProtoLocThresh.hf_hthresh[0]= (horz>>8)&0xff;
  245.     hftProtoLocThresh.hf_hthresh[1]= (horz&0xff);
  246.     hftProtoLocThresh.hf_vthresh[0]= (vert>>8)&0xff;
  247.     hftProtoLocThresh.hf_vthresh[1]= (vert&0xff);
  248.     HFTWRITE(hftProtoLocThresh,"set locator threshold write failed\n");
  249.     return;
  250. }
  251.  
  252. /***====================================================================***/
  253.  
  254. static struct hftdzone    hftProtoDZone =
  255.     { VTD(sizeof(hftProtoDZone)-3), HFTDZCH, HFTDZCL, 2, 1 };
  256.  
  257. void
  258. hftSetTableDeadZone(horz,vert)
  259.      unsigned    horz,vert;
  260. {
  261.     TRACE(("hftSetTabletDeadZone(0x%x,0x%x)\n",horz,vert));
  262.     hftProtoDZone.hf_horizontal[0]= (horz>>8)&0xff;
  263.     hftProtoDZone.hf_horizontal[1]= (horz&0xff);
  264.     hftProtoDZone.hf_vertical[0]= (vert>>8)&0xff;
  265.     hftProtoDZone.hf_vertical[1]= (vert&0xff);
  266.     HFTWRITE(hftProtoDZone,"set tablet dead zone write failed\n");
  267.     return;
  268. }
  269.  
  270. /***====================================================================***/
  271.  
  272. static struct hfdial_lpfk    hftProtoSetLPFK =
  273.     { VTD(sizeof(hftProtoSetLPFK)-3), HFLPFKSCH, HFLPFKSCL, 2, 1 };
  274.  
  275. void
  276. hftSetLPFK(keys,flags)
  277.      unsigned    keys,flags;
  278. {
  279.     TRACE(("hftSetLPFK(0x%x,0x%x)\n",keys,flags));
  280.     hftProtoSetLPFK.hf_mask.keys[0]= (keys>>24)&0xff;
  281.     hftProtoSetLPFK.hf_mask.keys[1]= (keys>>16)&0xff;
  282.     hftProtoSetLPFK.hf_mask.keys[2]= (keys>>8)&0xff;
  283.     hftProtoSetLPFK.hf_mask.keys[3]= (keys)&0xff;
  284.     hftProtoSetLPFK.hf_data2.lpfk.flags[0]=    (flags>>24)&0xff;
  285.     hftProtoSetLPFK.hf_data2.lpfk.flags[1]=    (flags>>16)&0xff;
  286.     hftProtoSetLPFK.hf_data2.lpfk.flags[2]=    (flags>>8)&0xff;
  287.     hftProtoSetLPFK.hf_data2.lpfk.flags[3]=    (flags)&0xff;
  288.     HFTWRITE(hftProtoSetLPFK,"set LPFK write failed\n");
  289.     return;
  290. }
  291.  
  292. /***====================================================================***/
  293.  
  294. static struct hfdial_lpfk    hftProtoSetGranularity =
  295.     { VTD(sizeof(hftProtoSetGranularity)-3), HFDIALSCH, HFDIALSCL, 2, 1 };
  296.  
  297. void
  298. hftSetDialGranularity(dials,settings)
  299.      unsigned        dials;
  300.      unsigned char    settings[16];
  301. {
  302.     int    i;
  303.  
  304.     TRACE(("hftSetDialGranularity(0x%x,0x%x)\n",dials,settings));
  305.     hftProtoSetGranularity.hf_mask.dials[0]= (dials>>24)&0xff;
  306.     hftProtoSetGranularity.hf_mask.dials[1]= (dials>>16)&0xff;
  307.     hftProtoSetGranularity.hf_mask.dials[2]= (dials>>8)&0xff;
  308.     hftProtoSetGranularity.hf_mask.dials[3]= (dials)&0xff;
  309.     for (i=0;i<16;i++)
  310.     hftProtoSetGranularity.hf_data2.granularity[i]=    settings[i];
  311.  
  312.     HFTWRITE(hftProtoSetGranularity,"set dial granularity write failed\n");
  313.     return;
  314. }
  315.  
  316. /***====================================================================***/
  317.  
  318. static    struct    hfrconf    hftProtoReconfigure;
  319.  
  320. static struct hfsound    hftProtoSound =
  321.     { VTD(sizeof(hftProtoSound)-3), HFSOUNDCH, HFSOUNDCL, 2, 1, HFEXECALWAYS};
  322.  
  323. void
  324. hftSound(vol, duration,frequency)
  325.      int    vol;
  326.      unsigned    duration;
  327.      unsigned    frequency;
  328. {
  329.     int    real_vol;
  330.  
  331.     TRACE(("hftSound(%d,%d)\n",duration,frequency));
  332.  
  333.     real_vol = getVolume (vol);
  334.     if (real_vol == 0)
  335.     return;
  336.  
  337.     /* set bell volume */
  338.     if (real_vol != CurrentKeyClick)
  339.     {
  340.     hftProtoReconfigure.hf_op= HFCHGVOLUME;
  341.     hftProtoReconfigure.hf_obj= real_vol;
  342.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  343.     }
  344.  
  345.     /* set bell duration and frequency, then ring bell */
  346.     hftProtoSound.hf_dur[0]= (duration>>8)&0xff;
  347.     hftProtoSound.hf_dur[1]= (duration&0xff);
  348.     hftProtoSound.hf_freq[0]= (frequency>>8)&0xff;
  349.     hftProtoSound.hf_freq[1]= (frequency&0xff);
  350.     HFTWRITE(hftProtoSound,"generate sound write failed\n");
  351.  
  352.     /* restore old value of keyclick */
  353.     if (real_vol != CurrentKeyClick)
  354.     {
  355.     hftProtoReconfigure.hf_obj= CurrentKeyClick;
  356.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  357.     }
  358.  
  359.     return;
  360. }
  361.  
  362. /***====================================================================***/
  363.  
  364. static int
  365. getVolume (volume)
  366.      int    volume;
  367. {
  368.     /* volume range 0-100 */
  369.  
  370.     if ((volume >= 1) && (volume <= 33))
  371.     volume = 1;        /* low */
  372.     else if ((volume >= 34) && (volume <= 66))
  373.     volume = 2;        /* medium */
  374.     else if (volume >= 67)
  375.     volume = 3;        /* high */
  376.  
  377.     return (volume);
  378. }
  379.  
  380. /***====================================================================***/
  381.  
  382. void
  383. hftSetTypematicDelay(delay)
  384.      unsigned    delay;
  385. {
  386.     TRACE(("hftSetTypematicDelay(%d)\n",delay));
  387.     hftProtoReconfigure.hf_op= HFCHGKBDDEL;
  388.     hftProtoReconfigure.hf_obj= delay;
  389.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  390.     return;
  391. }
  392.  
  393. /***====================================================================***/
  394.  
  395. void
  396. hftSetTypematicRate(rate)
  397.      unsigned    rate;
  398. {
  399.     TRACE(("hftSetTypematicRate(%d)\n",rate));
  400.     hftProtoReconfigure.hf_op= HFCHGKBDRATE;
  401.     hftProtoReconfigure.hf_obj= rate;
  402.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  403.     return;
  404. }
  405.  
  406. /***====================================================================***/
  407.  
  408. void
  409. hftSetKeyClick(volume)
  410.      int    volume;
  411. {
  412.  
  413.     TRACE(("hftSetKeyClick(%d)\n",volume));
  414.  
  415.     if (volume != 0)
  416.     {
  417.     hftProtoReconfigure.hf_op = HFCHGCLICK;
  418.     hftProtoReconfigure.hf_obj = 1;        /* turn click on */
  419.     ioctl(hftQFD, HFRCONF, &hftProtoReconfigure);
  420.     }
  421.  
  422.     hftProtoReconfigure.hf_obj = CurrentKeyClick = getVolume(volume);
  423.     hftProtoReconfigure.hf_op= HFCHGVOLUME;
  424.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  425.     return;
  426. }
  427.  
  428. /***====================================================================***/
  429.  
  430. void
  431. hftSetLocatorSampleRate(rate)
  432.      unsigned    rate;
  433. {
  434.     TRACE(("hftSetLocatorSampleRate(%d)\n",rate));
  435.     hftProtoReconfigure.hf_op= HFCHGLOCRATE;
  436.     hftProtoReconfigure.hf_obj= rate;
  437.     ioctl(hftQFD,HFRCONF,&hftProtoReconfigure);
  438.     return;
  439. }
  440.  
  441. /***====================================================================***/
  442.  
  443. volatile char hftQr[12] = {
  444.     0, VTD(0), HFQDEVIDRH, HFQDEVIDRL, 2, 1
  445. };
  446.  
  447. static struct hfqdevidc qdevidcmd =
  448.     { VTD(sizeof(qdevidcmd)-3), HFQDEVIDCH, HFQDEVIDCL };
  449.  
  450. static struct hfquery   query =    { qdevidcmd.hf_intro, sizeof( qdevidcmd ) };
  451. static struct hfquery   tquery =   { qdevidcmd.hf_intro, sizeof( qdevidcmd ) };
  452. static struct hfrconf   reconfig = { HFSETDD };
  453.  
  454. static struct {
  455.         char    pad;
  456.         char    hf_esc;
  457.         char    hf_lbr;
  458.         char    hf_ex;
  459.         int     hf_len;
  460.         char    hf_typehi;
  461.         char    hf_typelo;
  462.         char    hf_sublen;
  463.         char    hf_subtype;
  464.         short    hf_numdev;
  465.         struct {
  466.             short    hf_idhi,hf_idlo;
  467.             short    hf_classhi,hf_classlo;
  468.         } hf_devices[HFT_MAXDEVICES];
  469. }  qDevIDResponse;
  470.  
  471. static    hftDeviceID hftDevices[HFT_MAXDEVICES];
  472.  
  473. int
  474. hftQueryDeviceIDs(ppDevices)
  475.      hftDeviceID    **ppDevices;
  476. {
  477.     int i;
  478.     int    tempfd= -1;
  479.     int    mustClose= FALSE;
  480.  
  481.     TRACE(("hftQueryDeviceIDs(0x%x)\n",ppDevices));
  482.  
  483.     if (hftQFD < 0)
  484.     {
  485.     if ( ( tempfd = open("/dev/console",O_WRONLY) ) < 0 )
  486.     {
  487.         ErrorF("Cannot open /dev/console to query device ids\n");
  488.         return 0;
  489.     }
  490.     mustClose= TRUE;
  491.     }
  492.     else
  493.     tempfd= hftQFD;
  494.  
  495.     query.hf_resp=    &qDevIDResponse.hf_esc;
  496.     query.hf_resplen=    sizeof(qDevIDResponse)-1;
  497.     qDevIDResponse.hf_len= sizeof( qDevIDResponse ) - 4;
  498.  
  499.     if ( ioctl( tempfd, HFQUERY, &query ) )
  500.     {
  501.     /*    ErrorF( "unable to query display device ids\n" );*/
  502.     if (mustClose)
  503.         close(tempfd);
  504.     return 0;
  505.     }
  506.     if (mustClose)
  507.     close(tempfd);
  508.  
  509.     MOVE( qDevIDResponse.hf_devices, hftDevices, sizeof(hftDevices) ) ;
  510.  
  511.     for (i=0; i < HFT_MAXDEVICES; i++)
  512.     {
  513.     hftDevices[i].hftDevID    = HF_INT((char*)&hftDevices[i].hftDevID);
  514.     hftDevices[i].hftDevClass = HF_INT((char*)&hftDevices[i].hftDevClass);
  515.     }
  516.  
  517.     *ppDevices= hftDevices;
  518.  
  519.     return MIN(qDevIDResponse.hf_numdev,HFT_MAXDEVICES) ;
  520. }
  521.  
  522. static struct hfqhftc qhft = { 0x1b,'[','x',(6)>>24, (6)>>16, (6)>>8, (6),
  523.                    0x01, 0x45 };
  524. static struct hfquery   ttquery = { qhft.hf_intro, sizeof( qhft ) };
  525.  
  526. unsigned
  527. hftQueryCurrentDevice()
  528. {
  529.     int        tempfd= -1;
  530.     unsigned    *retval;
  531.     volatile struct hfqhftr resp;
  532.  
  533.     TRACE(("hftQueryCurrentDevice()\n"));
  534.  
  535.     if (hftQFD<0)
  536.     {
  537.     tempfd = open("/dev/tty",O_WRONLY);
  538.     if (tempfd<0)
  539.     {
  540.         tempfd = open("/dev/hft",O_WRONLY);
  541.         if (tempfd<0)
  542.         {
  543.         ErrorF("Cannot open /dev/tty to query device id\n");
  544.         return 0;
  545.         }
  546.     }
  547.     }
  548.     else
  549.     tempfd= hftQFD;
  550.  
  551.     ttquery.hf_resp    = resp.hf_intro;
  552.     ttquery.hf_resplen = sizeof( resp ) + 1 ;
  553.  
  554.     if ( ioctl( tempfd, HFQUERY, &ttquery ) )
  555.     {
  556.     ErrorF( "AIX: unable to query tty device id\n" );
  557.     resp.hf_phdevid = 0 ;
  558.     if (tempfd != hftQFD)
  559.         close(tempfd);
  560.     }
  561.  
  562.     if (tempfd!=hftQFD)
  563.     close(tempfd);
  564.  
  565.     return resp.hf_phdevid ;
  566. }
  567.  
  568. /***====================================================================***/
  569.  
  570. static struct hfqhftc   qhft_kb =   { VTD(6),
  571.                       HFQHFTCH, HFQHFTCL } ;
  572.  
  573. volatile struct hfqhftr resp1;
  574.  
  575. static struct hfquery   query1 =    { qhft.hf_intro,
  576.                       sizeof( qhft_kb ) };
  577. int
  578. hftGetKeyboardID ()
  579. {
  580.     int    tempfd;
  581.  
  582.     TRACE(("hftGetKeyboardID(0x%x)\n"));
  583.  
  584.     query1.hf_resp    = resp1.hf_intro;
  585.     query1.hf_resplen = sizeof( resp1 ) + 1 ;
  586.  
  587.     tempfd= hftQFD;
  588.     if ( ioctl( tempfd, HFQUERY, &query1 ) )
  589.     {
  590.     ErrorF("AIX: unable to query current keyboard");
  591.     return HFT_ILLEGAL_KEYBOARD ;
  592.     }
  593.     return resp1.hf_phkbdid ;
  594. }
  595.  
  596. /***====================================================================***/
  597.  
  598. int
  599. hftSetDefaultDisplay(idWanted)
  600.      int    idWanted;
  601. {
  602.     int             fd;
  603.  
  604.     TRACE(("hftSetDefaultDisplay(idWanted)\n",idWanted));
  605.     if ( !idWanted )
  606.     return TRUE;
  607.  
  608.     if (reconfig.hf_obj=hftHasAnAttached(idWanted))
  609.     {
  610.     if ((hftQueryDefaultDisplay()&HFT_DEVID_MASK)!=idWanted)
  611.     {
  612.         fd= open("/dev/console",O_WRONLY);
  613.         if (fd<0) {
  614.         ErrorF("Unable to open /dev/console to set default display\n");
  615.         return FALSE;
  616.         }
  617.         if ( ioctl( fd, HFRCONF, &reconfig ) )
  618.         {
  619.         ErrorF( "unable to change default display\n" );
  620.         (void) close(fd);
  621.         return FALSE;
  622.         }
  623.         close(fd) ;
  624.     }
  625.     }
  626.     else
  627.     {
  628.     ErrorF( "requested default display not attached\n" );
  629.     return FALSE;
  630.     }
  631.     return TRUE;
  632. }
  633.  
  634. extern void ibmInfoMsg() ;
  635.  
  636. /***====================================================================***/
  637.  
  638. unsigned
  639. hftQueryDefaultDisplay()
  640. {
  641.     hftDeviceID *pDevices ;
  642.  
  643.     TRACE( ( "hftQueryDefaultDisplay()\n" ) ) ;
  644.     return hftQueryDeviceIDs( &pDevices ) 
  645.     ? (pDevices[0].hftDevID & HFT_DEVID_MASK) : HFT_ILLEGAL_ID ;
  646. }
  647.  
  648. /***====================================================================***/
  649.  
  650. int
  651. hftHasAnAttached(devId)
  652.      unsigned int devId;
  653. {
  654.     int             i;
  655.     int         nDevices;
  656.     hftDeviceID    *pDevices;
  657.  
  658.     TRACE(("hftHasAnAttached(0x%x)\n",devId));
  659.     nDevices= hftQueryDeviceIDs(&pDevices);
  660.  
  661.     if (nDevices==0)
  662.     return(FALSE);
  663.  
  664.     for ( i = 0;  i < nDevices;  ++i )
  665.     {
  666.     if ( (pDevices[i].hftDevID&HFT_DEVID_MASK) == (devId&HFT_DEVID_MASK) )
  667.         return pDevices[i].hftDevID;
  668.     }
  669.     return FALSE;
  670. }
  671.  
  672. /***====================================================================***/
  673.  
  674. static    struct    hfchgdsp    changeDisplay = {
  675.     VTD(sizeof(changeDisplay)), HFCHGDSPCH, HFCHGDSPCL, 2, 0,
  676.     HFNONDEF, 0 };
  677.  
  678. void
  679. hftChangePhysicalDisplay(device)
  680.      unsigned    device;
  681. {
  682.     TRACE(("hftChangePhysicalDisplay(0x%x)\n",device));
  683.     if (hftQFD>=0)
  684.     {
  685.     changeDisplay.hf_devid[0]= (device>>24)&0xff;
  686.     changeDisplay.hf_devid[1]= (device>>16)&0xff;
  687.     changeDisplay.hf_devid[2]= (device>>8)&0xff;
  688.     changeDisplay.hf_devid[3]= (device)&0xff;
  689.     write( hftQFD, &changeDisplay, sizeof changeDisplay );
  690.     }
  691.     return;
  692. }
  693.  
  694. /***====================================================================***/
  695.  
  696. int
  697. hftFindAScreen(device)
  698.      unsigned    device;
  699. {
  700.     unsigned     oldDefault,thisDevice;
  701.     char    *thisTtyName;
  702.  
  703.     TRACE(("hftFindAScreen()\n"));
  704.     if (hftQFD<0)
  705.     {
  706.     thisTtyName= (char *)ttyname(2);
  707.     thisDevice= hftQueryCurrentDevice();
  708.     if (((device!=0)&&(device!=thisDevice)) || !thisTtyName)
  709.     {
  710.         oldDefault= hftQueryDefaultDisplay();
  711.         if (oldDefault!=device)
  712.         hftSetDefaultDisplay(device);
  713.         if ((hftQFD= open("/dev/hft",O_RDWR)) < 0)
  714.         {
  715.         ErrorF("error opening /dev/hft\nexiting\n");
  716.         exit(1);
  717.         }
  718.         if (oldDefault!=device)
  719.         hftSetDefaultDisplay(oldDefault);
  720.  
  721.         /* 
  722.          * This is so that you don't go into hft wait when not starting
  723.          * X on the console.  (jsw)
  724.          */
  725.         hftNeedsReset= TRUE;
  726.     }
  727.     else if ((strncmp(thisTtyName,"/dev/hft/",9) == 0) ||
  728.          (strcmp(thisTtyName,"/dev/console") == 0))
  729.     {
  730.         hftQFD= 2;
  731.         hftNeedsReset= TRUE;
  732.     }
  733.  
  734.     /* 10/9/88 (ef) -- need to close stdout so that "unix:0" (or    */
  735.     /*    some other number) is flushed to xinit. sigh.        */
  736.     /* close(1) ; no nore sigh ... */
  737.  
  738.     /* Actually, we still need to close stdout because the later    */
  739.     /*      write to hftQFD in hftQueue.c won't otherwise work with */
  740.     /*      some update levels of AIX 2.2.1.  Sigh once more ...    */
  741.     close(1);
  742.     /* Michael Brantley, ONCS, Rice University, Houston, Texas      */
  743.     }
  744.     return(hftQFD);
  745. }
  746.  
  747. /***====================================================================***/
  748.  
  749. /*
  750.  * BlockHandler is called just before the server goes into select
  751.  * forever (effectively),  WakeupHandler is called when select
  752.  * returns (times out or is interrupted).
  753.  *
  754.  * Normally, the hft interrupt knocks us out of select, but not
  755.  * back to Dispatch() -- only to WaitForSomething().  WaitForSomething()
  756.  * does *not* check device input -- it loops back around to the block
  757.  * handler and select.  It doesn't return to dispatch until a client
  758.  * does something.  The WakeupHandler only needs to dispatch input
  759.  * events when we're waiting for clients in WaitForSomething().
  760.  *
  761.  * We need to set a timeout if input is pending, because
  762.  * hftDispatchEvents wants to be called periodically if it has
  763.  * a pending timeout, and it won't be generating any interrupts to
  764.  * blow the select away.
  765.  *
  766.  * waittime is used to get a reasonable amount of time for timeouts
  767.  * in the hft queue.  The value of the timeout below was derived
  768.  * experimentally -- I played with the length of the timeout
  769.  * until the mouse felt "right."
  770.  */
  771.  
  772. static    int    handleInput;
  773.  
  774. void
  775. hftBlockHandler(scrNdx,blockData,ppTimeout,pReadmask)
  776.      int        scrNdx;
  777.      char        *blockData;
  778.      struct timeval    **ppTimeout;
  779.      unsigned        *pReadmask;
  780. {
  781.     static struct timeval waittime = { 0, 40000 }; /* {sec, usec} */
  782.  
  783.     TRACE(("hftBlockHandler()\n"));
  784.  
  785.     if (hftPending)
  786.     {
  787.     *ppTimeout= &waittime;
  788.     handleInput= 1;
  789.     }
  790.     else
  791.     {
  792.     hftInterruptAlways();
  793.     handleInput= 0;
  794.     }
  795.     return;
  796. }
  797.  
  798. /***====================================================================***/
  799.  
  800. void
  801. hftWakeupHandler(scrNdx,blockData,pTimeout,pReadmask)
  802.      int        scrNdx;
  803.      char        *blockData;
  804.      struct timeval    *pTimeout;
  805.      unsigned        *pReadmask;
  806. {
  807.     TRACE(("hftWakeupHandler()\n"));
  808.     if (handleInput)
  809.     hftDispatchEvents();
  810.  
  811.     hftInterruptOnFirst();
  812.     return;
  813. }
  814.  
  815. #ifdef AIXEXTENSIONS
  816. #include "AIX.h"
  817. #include "AIXext.h"
  818. #include "hftUtils.h"
  819.  
  820. extern AIXInfoRec aixInfo;
  821. extern int aixTabletScaleX,aixTabletScaleY ;
  822.  
  823. union
  824. {
  825.     struct hfqdev   qdev;
  826.     struct hfqdresp qdresp;
  827. } hfqdstuff;
  828.  
  829. static struct hfqhftc   qhft =      { VTD(6),
  830.                       HFQHFTCH, HFQHFTCL } ;
  831. volatile struct hfqhftr resp1;
  832.  
  833. static struct hfquery   query1 =    { qhft.hf_intro,
  834.                       sizeof( qhft ) };
  835. static struct hfqgraphdev qlocdev = { VTD( sizeof(qlocdev) - 3 ),
  836.                       HFQLOCCH, HFQLOCCL };
  837. union {
  838.    struct hfqlocr loc;
  839.    char junk[sizeof(struct hfqlocr)+1];
  840. }   resp3;
  841.  
  842. static struct hfquery   query3 =    { qlocdev.hf_intro, sizeof(qlocdev),
  843.                       resp3.junk, sizeof( resp3 ) };
  844.  
  845. void
  846. hftQueryHardwareConfig(device)
  847.      unsigned    device;
  848. {
  849.  
  850.     TRACE(("hftQueryHardwareConfig(0x%x)\n",device));
  851.  
  852.     hfqdstuff.qdev.hf_qdrsvd = 0;
  853.     hfqdstuff.qdev.hf_qdopts = 2;
  854.     hfqdstuff.qdev.hf_qdlen =
  855.     sizeof(hfqdstuff) - sizeof(hfqdstuff.qdev);
  856.  
  857.     if (ioctl(hftQFD, HFQDEV, &hfqdstuff))
  858.     {
  859.     ErrorF("Fatal!      unable to query hardware config\n" );
  860.     ErrorF("            exiting\n");
  861.     exit(1);
  862.     }
  863.     else
  864.     {
  865.     aixInfo.kbdiodn = hfqdstuff.qdresp.hf_keyiodn ;
  866.     aixInfo.lociodn = hfqdstuff.qdresp.hf_lociodn ;
  867.     aixInfo.dialiodn = hfqdstuff.qdresp.hf_dialsiodn ;
  868.     aixInfo.lpfkiodn = hfqdstuff.qdresp.hf_lpfkiodn ;
  869.     }
  870.  
  871.     aixInfo.vrmid =  device & 0xffff0000 ;
  872.  
  873.     switch(aixInfo.vrmid)
  874.     {
  875.       case HFT_APA8_ID:    aixInfo.displayid = XDEV_IBM_APA8 ;
  876.     break;
  877.       case HFT_APA8C_ID:    aixInfo.displayid = XDEV_IBM_APA8C ;
  878.     break;
  879.       case HFT_APA16_ID:    aixInfo.displayid = XDEV_IBM_APA16 ;
  880.     break;
  881.       case HFT_MEGAPEL_ID:    aixInfo.displayid = XDEV_IBM_MPEL ;
  882.     break;
  883.       default:        aixInfo.displayid = XDEV_IBM_GSL ;
  884.     }
  885.  
  886.     if ( ioctl(hftQFD , HFQUERY, &query3 ) )
  887.     {
  888.     ErrorF("Fatal!      unable to query locator\n" );
  889.     ErrorF("            exiting\n");
  890.     }
  891.  
  892.     if ( resp3.loc.hf_devinfo[0] & HFLOCABS )
  893.     {
  894.     aixTabletScaleX = (resp3.loc.hf_horzmax_cnt[0] << 8)
  895.         + resp3.loc.hf_horzmax_cnt[1];
  896.     aixTabletScaleY = (resp3.loc.hf_vertmax_cnt[0] << 8)
  897.         + resp3.loc.hf_vertmax_cnt[1];
  898.     aixInfo.loctype = DEVTABLET ;
  899.     }
  900.     else
  901.     {
  902.     aixInfo.loctype = DEVMOUSE ;
  903.     }
  904.  
  905.     aixInfo.kbdid = hftGetKeyboardID();
  906.     aixInfo.inputfd = hftQFD ;
  907.     return;
  908. }
  909.  
  910. #endif
  911.