23ee1371b2c0880d1953ceee807158183f8a5cfd
[tpg/acess2.git] / Usermode / Applications / irc_src / pseudo_curses.c
1 /*
2  */
3 #include "pseudo_curses.h"
4 #include <stdbool.h>
5 #include <acess/sys.h>
6 #include <acess/devices/pty.h>
7 #include <stdio.h>
8
9 // === PROTOTYPES ===
10 bool    ACurses_GetDims_Acess(void);
11 bool    ACurses_GetDims_SerialTermHack(void);
12
13 // === GLOBALS ===
14  int    giTerminal_Width = 80;
15  int    giTerminal_Height = 25;
16
17 // === CODE ===
18 void ACurses_Init(void)
19 {
20         if( ACurses_GetDims_Acess() ) {
21         }
22         else if( ACurses_GetDims_SerialTermHack() ) {
23         }
24         else {
25                 _SysDebug("note: assuming 80x25, can't get terminal dimensions");
26                 giTerminal_Width = 80;
27                 giTerminal_Height = 25;
28         }
29 }
30
31 bool ACurses_GetDims_Acess(void)
32 {
33         if( _SysIOCtl(1, DRV_IOCTL_TYPE, NULL) != DRV_TYPE_TERMINAL )
34                 return false;
35         
36         struct ptydims  dims;
37         if( _SysIOCtl(1, PTY_IOCTL_GETDIMS, &dims) == -1 )
38                 return false;
39         giTerminal_Width = dims.W;
40         giTerminal_Height = dims.H;
41         return true;
42 }
43
44 bool ACurses_GetDims_SerialTermHack(void)
45 {
46         _SysDebug("ACurses_GetDims_SerialTermHack: Trying");
47         // Set cursor to 1000,1000 , request cursor position, reset cursor to 0,0
48         static const char req[] = "\033[1000;1000H\033[6n\033[H";
49         fflush(stdin);
50         _SysWrite(1, req, sizeof(req));
51         int64_t timeout = 1000;
52         fd_set fds;
53         FD_ZERO(&fds);
54         FD_SET(0, &fds);
55         _SysSelect(1, &fds, NULL, NULL, &timeout, 0);
56         if( !FD_ISSET(0, &fds) )
57                 return false;
58         
59         if( fgetc(stdin) != '\x1b' )
60                 return false;
61         if( fgetc(stdin) != '[' )
62                 return false;
63         if( fscanf(stdin, "%i;%i", &giTerminal_Width, &giTerminal_Height) != 2 )
64                 return false;
65         if( fgetc(stdin) != 'R' )
66                 return false;
67         
68         return true;
69 }
70
71 void SetCursorPos(int Row, int Col)
72 {
73         printf("\x1B[%i;%iH", Row, Col);
74 }
75

UCC git Repository :: git.ucc.asn.au