MODULE T17;

IMPORT IO, Strings, ASCII, NumberIO, termios, FIO, Delay, SplitV1, STextIO;


VAR
    cursorposStr : ARRAY [0..15] OF CHAR;
    resultStr    : ARRAY [0..15] OF CHAR;
    tmpString : ARRAY[0..10] OF CHAR;
    tmpString1: ARRAY[0..5] OF CHAR;
    tmpString2, cc : Strings.String1;
    x,y       : CARDINAL;
    i : CARDINAL;
    theTermios : termios.TERMIOS;
    TCSAFLUSH   : INTEGER;
    str : ARRAY [0..15] OF CHAR;
    c : CHAR;
    theStructure : SplitV1.Structure;


PROCEDURE Card2Str ( x : CARDINAL; VAR str : ARRAY OF CHAR);

BEGIN
IF x < 10 THEN
        NumberIO.CardToStr(x,1,str);
    ELSIF x < 100 THEN
        NumberIO.CardToStr(x,2,str);
    ELSE
        NumberIO.CardToStr(x,3,str);
    END;
END Card2Str;

BEGIN
    TCSAFLUSH := termios.tcsflush (); 
    theTermios := termios.InitTermios();
    IF termios.tcgetattr(FIO.StdIn, theTermios) = -1 THEN
        HALT
    END;
    IO.UnBufferedMode(0,TRUE);
    IO.UnBufferedMode(1,TRUE);

    (* very far positions to get the size of the window                   *)
    (* there is an ioctl() call in the libc which can get the same result *)
    (* but I didn't get it to wok for now                                 *)
    x := 45;
    y := 30;

    (* setting the cursor position *)
    cursorposStr := "";
    Strings.Concat(cursorposStr, ASCII.esc,cursorposStr);
    Strings.Concat(cursorposStr, "[",cursorposStr);
    tmpString := "";
    Strings.Concat(tmpString,cursorposStr, tmpString);
    tmpString1 := "";
    Card2Str(y,tmpString1);
    Strings.Concat(tmpString,tmpString1, tmpString);
    tmpString2[0] := ";";
    Strings.Concat(tmpString, tmpString2,tmpString);
    tmpString1 := "";
    Card2Str(x,tmpString1);
    Strings.Concat(tmpString,tmpString1, tmpString);
    tmpString2[0] := "H";
    Strings.Concat(tmpString, tmpString2,tmpString);
    FOR i := 0 TO Strings.Length(tmpString) DO
        IO.Write(tmpString[i])
    END;

        (* getting the cursor position *)

    (* sending request *)
    str := "";
    Strings.Concat(str,ASCII.esc,str);
    Strings.Concat(str,"[6n", str);
    (* sending request *)
    FOR i := 0 TO 3 DO
        IO.Write(str[i])
    END;
    (* getting answer *)
    str := "";
    REPEAT
        IO.Read(c);
        cc[0] := c;
        Strings.Concat(str,cc,str);
    UNTIL c = "R";

    (* displaying the retour , changing the escape to ? *)
    (* str[0] := "?";
    FOR i := 0 TO Strings.Length(str) DO
        IO.Write(str[i])
    END; *)

    (* extracting the coordinates *)
    Strings.Delete(str,0,2);
    Strings.Delete(str,Strings.Length(str) -1 ,1);
    SplitV1.SplitStr(str, ";", theStructure);
    NumberIO.StrToCard(theStructure[0].element,x);
    NumberIO.StrToCard(theStructure[1].element,y);

    (* Delay.Delay(2000); *)

    IO.BufferedMode(0,TRUE);
    IO.BufferedMode(1,TRUE);
    IF termios.tcsetattr(FIO.StdIn, TCSAFLUSH, theTermios) = -1 THEN
        HALT
    END;
    NumberIO.WriteCard(x, 5);
    NumberIO.WriteCard(y, 5);
    (* Delay.Delay(2000); *)
END T17.