home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / ibm / AIX / aixWrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-16  |  9.0 KB  |  455 lines

  1. /*
  2.  * $XConsortium: aixWrap.c,v 1.3 91/07/16 13:01:36 jap 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.  
  29. #define NEED_EVENTS
  30. #define NEED_REPLIES
  31. #include "X.h"
  32. #include "Xproto.h"
  33. #include "miscstruct.h"
  34. #include "scrnintstr.h"
  35. #include "cursorstr.h"
  36. #include "aixInput.h"
  37. #include "input.h"
  38. #include "inputstr.h"
  39.  
  40. #include "compiler.h"
  41.  
  42. #include "ibmIO.h"
  43. #include "ibmMouse.h"
  44. #include "ibmKeybd.h"
  45. #include "ibmScreen.h"
  46. #include "ibmTrace.h"
  47.  
  48. #include "OSio.h"
  49. #include "hftQueue.h"
  50.  
  51.  
  52. #define FIELDWIDTH    (10)
  53. #define FIELDHEIGHT    (10)
  54.  
  55. typedef struct {
  56.     int  x,y;
  57.     int  connected;
  58. } wrap_entry;
  59.  
  60. static wrap_entry  entry_list[MAXSCREENS];
  61.  
  62. static int  field[FIELDWIDTH][FIELDHEIGHT];
  63.  
  64. static int  need_to_init = 1;
  65.  
  66. static void
  67. aixInitWrap()
  68. {
  69.     int  i,x,y;
  70.  
  71.     if( ! need_to_init ){
  72.     ErrorF("aix Wrap already initialized\n");
  73.     exit(0);
  74.     }
  75.  
  76.     for(i=0;i<MAXSCREENS;i++){
  77.     entry_list[i].x = -1;
  78.     }
  79.     for(x=0;x<FIELDWIDTH;x++){
  80.     for(y=0;y<FIELDHEIGHT;y++){
  81.         field[x][y] = -1;
  82.     }
  83.     }
  84.  
  85.     need_to_init = 0;
  86. }
  87.  
  88. /*
  89.     Return the screen index of the next screen.
  90.     Return -1 if there is no next screen.  (Stick the cursor
  91.     on the screen edge.)
  92.     Return the same index if wrap to the other side of the
  93.     same screen.
  94. */
  95. int
  96. aixWrapUp(index)
  97.     int  index;
  98. {
  99.     int  x, y;
  100.  
  101.     if( need_to_init ){
  102.     ErrorF("aix Wrap not initialized\n");
  103.     exit(0);
  104.     }
  105.  
  106.     x = entry_list[index].x;
  107.     y = entry_list[index].y;
  108.     y++;
  109.     while( y < FIELDHEIGHT ){
  110.     if( field[x][y] >= 0 )
  111.         return(field[x][y]);
  112.     y++;
  113.     }
  114.     if( ! ibmYWrapScreen )
  115.     return -1;
  116.     y = 0;
  117.     while( y < entry_list[index].y ){
  118.     if( field[x][y] >= 0 )
  119.         return(field[x][y]);
  120.     y++;
  121.     }
  122.     return(index);
  123. }
  124.  
  125. /*
  126.     Return the screen index of the next screen.
  127.     Return -1 if there is no next screen.  (Stick the cursor
  128.     on the screen edge.)
  129.     Return the same index if wrap to the other side of the
  130.     same screen.
  131. */
  132. int
  133. aixWrapDown(index)
  134.     int  index;
  135. {
  136.     int  x, y;
  137.  
  138.     if( need_to_init ){
  139.     ErrorF("aix Wrap not initialized\n");
  140.     exit(0);
  141.     }
  142.  
  143.     x = entry_list[index].x;
  144.     y = entry_list[index].y;
  145.     y--;
  146.     while( y >= 0 ){
  147.     if( field[x][y] >= 0 )
  148.         return(field[x][y]);
  149.     y--;
  150.     }
  151.     if( ! ibmYWrapScreen )
  152.     return -1;
  153.     y = FIELDHEIGHT-1;
  154.     while( y > entry_list[index].y ){
  155.     if( field[x][y] >= 0 )
  156.         return(field[x][y]);
  157.     y--;
  158.     }
  159.     return(index);
  160. }
  161.  
  162. /*
  163.     Return the screen index of the next screen.
  164.     Return -1 if there is no next screen.  (Stick the cursor
  165.     on the screen edge.)
  166.     Return the same index if wrap to the other side of the
  167.     same screen.
  168. */
  169. int
  170. aixWrapLeft(index)
  171.     int  index;
  172. {
  173.     int  x, y;
  174.  
  175.     if( need_to_init ){
  176.     ErrorF("aix Wrap not initialized\n");
  177.     exit(0);
  178.     }
  179.  
  180.     x = entry_list[index].x;
  181.     y = entry_list[index].y;
  182.     x--;
  183.     while( x >= 0 ){
  184.     if( field[x][y] >= 0 )
  185.         return(field[x][y]);
  186.     x--;
  187.     }
  188.     if( ! ibmXWrapScreen )
  189.     return -1;
  190.     x = FIELDWIDTH-1;
  191.     while( x > entry_list[index].x ){
  192.     if( field[x][y] >= 0 )
  193.         return(field[x][y]);
  194.     x--;
  195.     }
  196.     return(index);
  197. }
  198.  
  199. /*
  200.     Return the screen index of the next screen.
  201.     Return -1 if there is no next screen.  (Stick the cursor
  202.     on the screen edge.)
  203.     Return the same index if wrap to the other side of the
  204.     same screen.
  205. */
  206. int
  207. aixWrapRight(index)
  208.     int  index;
  209. {
  210.     int  x, y;
  211.  
  212.     if( need_to_init ){
  213.     ErrorF("aix Wrap not initialized\n");
  214.     exit(0);
  215.     }
  216.  
  217.     x = entry_list[index].x;
  218.     y = entry_list[index].y;
  219.     x++;
  220.     while( x < FIELDWIDTH ){
  221.     if( field[x][y] >= 0 )
  222.         return(field[x][y]);
  223.     x++;
  224.     }
  225.     if( ! ibmXWrapScreen )
  226.     return -1;
  227.     x = 0;
  228.     while( x < entry_list[index].x ){
  229.     if( field[x][y] >= 0 )
  230.         return(field[x][y]);
  231.     x++;
  232.     }
  233.     return(index);
  234. }
  235.  
  236. void
  237. aixPutScreenAt(index,x,y)
  238.     int  index, x, y;
  239. {
  240.     if( need_to_init )
  241.     aixInitWrap();
  242.  
  243.     if( entry_list[index].x >= 0 ){
  244.     ErrorF("aixPutScreenAt() screen %d already has a postion %d %d\n",field[x][y],entry_list[index].x,entry_list[index].y);
  245.     return;
  246.     }
  247.  
  248.     if( field[x][y] < 0 ){
  249.     field[x][y] = index;
  250.     entry_list[index].x = x;
  251.     entry_list[index].y = y;
  252.     }
  253.     else{
  254.     ErrorF("aixPutScreenAt() Position %d %d already in use by screen %d\n",x,y,field[x][y]);
  255.     while( field[x][y] >= 0 ){
  256.         x++;
  257.         if( x > 9 ){
  258.         x = 0;
  259.         y++;
  260.         if( y > 9 ){
  261.             y = 0;
  262.         }
  263.         }
  264.     }
  265.     field[x][y] = index;
  266.     entry_list[index].x = x;
  267.     entry_list[index].y = y;
  268.     ErrorF("aixPutScreenAt() moved %d to position %d %d instead.\n",index,x,y);
  269.     }
  270. }
  271.  
  272. static int
  273. isConnected(i)
  274.     int  i;
  275. {
  276.     int  nexti;
  277.  
  278.     if( entry_list[i].connected == -1 ){
  279.     return(FALSE);
  280.     }
  281.     if( entry_list[i].connected )
  282.     return(TRUE);
  283.     entry_list[i].connected = -1;
  284.  
  285.     if( (nexti=aixWrapLeft(i)) >= 0 ){
  286.     if( isConnected(nexti) )
  287.         return(TRUE);
  288.     }
  289.     if( (nexti=aixWrapRight(i)) >= 0 ){
  290.     if( isConnected(nexti) )
  291.         return(TRUE);
  292.     }
  293.     if( (nexti=aixWrapUp(i)) >= 0 ){
  294.     if( isConnected(nexti) )
  295.         return(TRUE);
  296.     }
  297.     if( (nexti=aixWrapDown(i)) >= 0 ){
  298.     if( isConnected(nexti) )
  299.         return(TRUE);
  300.     }
  301.     entry_list[i].connected = 0;
  302.     return(FALSE);
  303. }
  304.  
  305. static int  connectedrow[FIELDHEIGHT];
  306. static int  connectcount;
  307.  
  308. static void
  309. markConnected(i)
  310.     int  i;
  311. {
  312.     int  nexti;
  313.  
  314.     if( entry_list[i].connected )
  315.     return;
  316.  
  317.     entry_list[i].connected = TRUE;
  318.     connectcount++;
  319.     connectedrow[entry_list[i].y] = TRUE;
  320.  
  321.     if( (nexti=aixWrapLeft(i)) >= 0 ){
  322.     markConnected(nexti);
  323.     }
  324.     if( (nexti=aixWrapRight(i)) >= 0 ){
  325.     markConnected(nexti);
  326.     }
  327.     if( (nexti=aixWrapUp(i)) >= 0 ){
  328.     markConnected(nexti);
  329.     }
  330.     if( (nexti=aixWrapDown(i)) >= 0 ){
  331.     markConnected(nexti);
  332.     }
  333. }
  334. /*
  335.     Make sure all screens can be reached.
  336. */
  337. void
  338. aixConnectScreens()
  339. {
  340.     int  i,x,y, screencount;
  341.     int  x2,y2;
  342.  
  343.     if( need_to_init ){
  344.     ErrorF("aix Wrap not initialized\n");
  345.     exit(0);
  346.     }
  347.  
  348.     screencount = 0;
  349.     for(i=0;i<MAXSCREENS;i++){
  350.     if( entry_list[i].x >= 0 ){
  351.         screencount++;
  352.     }
  353.     }
  354.     for(x=0;x<FIELDWIDTH;x++){
  355.     for(y=0;y<FIELDHEIGHT;y++){
  356.         if( field[x][y] >= 0 ){
  357.         }
  358.     }
  359.     }
  360.     
  361.     if( screencount < 2 )
  362.     return;
  363.  
  364.     for(y=0;y<FIELDHEIGHT;y++){
  365.     connectedrow[y] = FALSE;
  366.     }
  367.     for(i=0;i<MAXSCREENS;i++){
  368.     entry_list[i].connected = FALSE;
  369.     }
  370.     connectcount = 0;
  371.  
  372. /*
  373.     Mark the first one as connected, then
  374.     find the screens connected to it.
  375. */
  376.     for(i=0;i<MAXSCREENS;i++){
  377.     if( entry_list[i].x >= 0 ){
  378.         markConnected(i);
  379.         break;
  380.     }
  381.     }
  382.  
  383.     if( connectcount == screencount ){
  384.     TRACE(("screens are connected\n"));
  385.     return;
  386.     }
  387.  
  388.     TRACE(("some screens are not connected\n"));
  389.  
  390. /*
  391.     Re-connect by moving screens down, or up.
  392. */
  393.     for(;i<MAXSCREENS;i++){
  394.     if( entry_list[i].x >= 0 && !(entry_list[i].connected) ){
  395.         /* see if some other change has made this connected */
  396.         if( isConnected(i) ){
  397.         markConnected(i);
  398.         }
  399.         else{
  400.             /* try to move it down */
  401.         x2 = entry_list[i].x;
  402.         for(y2=entry_list[i].y-1; y2 >=0 ; y2--){
  403.             if( field[x2][y2] >= 0 ){
  404.             /* this spot already taken */
  405.             break;
  406.             }
  407.             if( connectedrow[y2] ){
  408.             /* move screen to this row */
  409.             TRACE(("moving screen %d from (%d,%d) to (%d,%d)\n",i,entry_list[i].x,entry_list[i].y,x2,y2));
  410.             field[x2][y2] = i;
  411.             field[entry_list[i].x][entry_list[i].y] = -1;
  412.             entry_list[i].x = x2;
  413.             entry_list[i].y = y2;
  414.             markConnected(i);
  415.             break;
  416.             }
  417.         }
  418.  
  419.             /* we were able to move it down */
  420.         if( entry_list[i].connected ){
  421.             continue;
  422.         }
  423.  
  424.             /* can't move down, so try to move it up */
  425.         for(y2=entry_list[i].y+1; y2 < FIELDHEIGHT; y2++){
  426.             if( field[x2][y2] >= 0 ){
  427.             /* this spot already taken */
  428.             break;
  429.             }
  430.             if( connectedrow[y2] ){
  431.             /* move screen to this row */
  432.             TRACE(("moving screen %d from (%d,%d) to (%d,%d)\n",i,entry_list[i].x,entry_list[i],y,x2,y2));
  433.             field[x2][y2] = i;
  434.             field[entry_list[i].x][entry_list[i].y] = -1;
  435.             entry_list[i].x = x2;
  436.             entry_list[i].y = y2;
  437.             markConnected(i);
  438.             break;
  439.             }
  440.         }
  441.  
  442.         TRACE(("couldn't reconnect screen %d by moving it\n",i));
  443.  
  444.         }
  445.     }
  446.     }
  447.  
  448.     if( connectcount == screencount ){
  449.     TRACE(("screens are connected\n"));
  450.     return;
  451.     }
  452.  
  453.     TRACE(("some screens couldn't be connected\n"));
  454. }
  455.