Kernel/PTYs - Add initial dims/mode to PTY_Create
[tpg/acess2.git] / KernelLand / Kernel / drv / serial.c
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * drv/serial.c
6  * - Common serial port code
7  */
8 #include <acess.h>
9 #include <modules.h>
10 #include <fs_devfs.h>
11 #include <drv_serial.h>
12 #include <drv_pty.h>
13 #include <debug_hooks.h>
14
15 // === TYPES ===
16 struct sSerialPort
17 {
18         tPTY    *PTY;
19         tSerial_OutFcn  OutputFcn;
20         void    *OutHandle;
21 };
22
23 // === PROTOTYPES ===
24  int    Serial_Install(char **Arguments);
25 //tSerialPort   *Serial_CreatePort( tSerial_OutFcn output, void *handle );
26 //void  Serial_ByteReceived(tSerialPort *Port, char Ch);
27 void    Serial_int_PTYOutput(void *Handle, size_t Length, const void *Buffer);
28  int    Serial_int_PTYSetArrib(void *Handle, const struct ptymode *Mode);
29  int    Serial_int_PTYSetDims(void *Handle, const struct ptydims *Dims);
30 void    Serial_int_OutputDebug(void *unused, char ch);
31
32 // === GLOBALS ===
33 MODULE_DEFINE(0, 0x100, Serial, Serial_Install, NULL, "PTY", NULL);
34 tSerialPort     *gSerial_KernelDebugPort;
35
36 // === CODE ===
37 int Serial_Install(char **Arguments)
38 {
39         gSerial_KernelDebugPort = Serial_CreatePort( Serial_int_OutputDebug, NULL );
40         return 0;
41 }
42
43 tSerialPort *Serial_CreatePort(tSerial_OutFcn output, void *handle)
44 {
45         tSerialPort     *ret = malloc( sizeof(tSerialPort) );
46         // Automatically indexed
47         struct ptydims dims = {
48                 .W = 80, .H = 25,
49                 .PW = 0, .PH = 0
50         };
51         struct ptymode mode = {
52                 .OutputMode = PTYBUFFMT_TEXT,
53                 .InputMode = PTYIMODE_CANON|PTYIMODE_ECHO
54         };
55         ret->PTY = PTY_Create("serial#", ret,
56                 Serial_int_PTYOutput, Serial_int_PTYSetDims, Serial_int_PTYSetArrib,
57                 &dims, &mode
58                 );
59         ret->OutputFcn = output;
60         ret->OutHandle = handle;
61         return ret;
62 }
63
64 void Serial_ByteReceived(tSerialPort *Port, char Ch)
65 {
66         if( !Port )
67                 return ;
68         if( Port == gSerial_KernelDebugPort )
69         {
70                 static int serial_debug_mode = 0;
71                 // Kernel serial debug hooks.
72                 if( serial_debug_mode )
73                 {
74                         switch(Ch)
75                         {
76                         case 'p':
77                                 Threads_Dump();
78                                 break;
79                         case 'h':
80                                 Heap_Dump();
81                                 break;
82                         case 'X'-'A'+1:
83                                 PTY_SendInput(Port->PTY, &Ch, 1);
84                                 break;
85                         }
86                         serial_debug_mode = 0;
87                         return ;
88                 }
89                 else if( Ch == 'X'-'A'+1 )
90                 {
91                         serial_debug_mode = 1;
92                         return ;
93                 }
94                 else
95                 {
96                         // Fall
97                 }
98         }
99         if( Ch == '\r' )
100                 Ch = '\n';
101         PTY_SendInput(Port->PTY, &Ch, 1);
102 }
103
104 void Serial_int_PTYOutput(void *Handle, size_t Length, const void *Buffer)
105 {
106         tSerialPort     *Port = Handle;
107         const char      *buf = Buffer;
108         for( int i = 0; i < Length; i ++ )
109                 Port->OutputFcn( Port->OutHandle, *buf++ );
110 }
111 int Serial_int_PTYSetArrib(void *Handle, const struct ptymode *Mode)
112 {
113         return 0;
114 }
115 int Serial_int_PTYSetDims(void *Handle, const struct ptydims *Dims)
116 {
117         return 0;
118 }
119
120 void Serial_int_OutputDebug(void *unused, char ch)
121 {
122         Debug_PutCharDebug(ch);
123 }
124

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