2 * Acess2 GUI (AxWin) Version 3
3 * - By John Hodge (thePowersGang)
10 #include <acess/devices/pty.h>
11 #include <acess/devices/pty_cmds.h>
13 #include "resources/cursor.h"
18 #include "include/lowlevel.h"
21 extern int giTerminalFD_Input;
24 void Video_Setup(void);
25 void Video_int_SetBufFmt(int NewFmt);
26 void Video_SetCursorPos(short X, short Y);
27 void Video_Update(void);
28 void Video_FillRect(int X, int Y, int W, int H, uint32_t Color);
33 uint32_t *gpScreenBuffer;
34 int giVideo_FirstDirtyLine;
35 int giVideo_LastDirtyLine;
38 void Video_Setup(void)
44 giTerminalFD = _SysOpen(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
45 if( giTerminalFD == -1 )
47 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
52 giTerminalFD_Input = 0;
53 // Check that the console is a PTY
54 // - _SysIOCtl(..., 0, NULL) returns the type, which should be 2
55 rv = _SysIOCtl(1, DRV_IOCTL_TYPE, NULL);
56 if( rv != DRV_TYPE_TERMINAL )
58 fprintf(stderr, "stdout is not a PTY, can't start (%i exp, %i got)\n",
59 DRV_TYPE_TERMINAL, rv);
60 _SysDebug("stdout is not an PTY, can't start");
65 // TODO: Check terminal echoback that it does support graphical modes
66 // And/or have terminal flags fetchable by the client
69 Video_int_SetBufFmt(PTYBUFFMT_FB);
73 rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_GETDIMS, &dims );
75 perror("Can't get terminal dimensions (WTF?)");
78 giScreenWidth = dims.PW;
79 giScreenHeight = dims.PH;
80 if( giScreenWidth < 640 || giScreenHeight < 480 ) {
81 Video_int_SetBufFmt(PTYBUFFMT_TEXT);
82 _SysDebug("Resoltion too small, 640x480 reqd but %ix%i avail",
83 giScreenWidth, giScreenHeight);
86 _SysDebug("AxWin3 running at %ix%i", dims.PW, dims.PH);
88 giVideo_LastDirtyLine = giScreenHeight;
90 // Create local framebuffer (back buffer)
91 gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
93 // Set cursor position and bitmap
95 Video_int_SetBufFmt(PTYBUFFMT_2DCMD);
96 struct ptycmd_header hdr = {PTY2D_CMD_SETCURSORBMP,0,0};
97 size_t size = sizeof(hdr) + sizeof(cCursorBitmap) + cCursorBitmap.W*cCursorBitmap.H*4;
98 hdr.len_low = size / 4;
99 hdr.len_hi = size / (256*4);
100 _SysWrite(giTerminalFD, &hdr, sizeof(hdr));
101 _SysDebug("size = %i (%04x:%02x * 4)", size, hdr.len_hi, hdr.len_low);
102 _SysWrite(giTerminalFD, &cCursorBitmap, size-sizeof(hdr));
104 Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
107 void Video_Update(void)
110 int ofs = giVideo_FirstDirtyLine*giScreenWidth;
111 int size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
113 if( giVideo_LastDirtyLine == 0 ) return;
115 _SysDebug("Video_Update - Updating lines %i to %i (0x%x+0x%x px)",
116 giVideo_FirstDirtyLine, giVideo_LastDirtyLine, ofs, size);
117 _SysSeek(giTerminalFD, ofs*4, SEEK_SET);
118 _SysDebug("Video_Update - Sending FD %i %p 0x%x", giTerminalFD, gpScreenBuffer+ofs, size*4);
119 _SysWrite(giTerminalFD, gpScreenBuffer+ofs, size*4);
120 _SysDebug("Video_Update - Done");
121 giVideo_FirstDirtyLine = giScreenHeight;
122 giVideo_LastDirtyLine = 0;
124 size_t size = giScreenHeight * giScreenWidth;
125 Video_int_SetBufFmt(PTYBUFFMT_FB);
126 _SysWrite(giTerminalFD, gpScreenBuffer, size*4);
130 void Video_int_SetBufFmt(int NewFmt)
132 static int current_fmt;
134 if( current_fmt == NewFmt )
137 struct ptymode mode = {.InputMode = 0, .OutputMode = NewFmt};
138 int rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_SETMODE, &mode );
140 perror("Can't set PTY to framebuffer mode");
144 current_fmt = NewFmt;
147 void Video_SetCursorPos(short X, short Y)
149 struct ptycmd_setcursorpos cmd;
150 cmd.hdr.cmd = PTY2D_CMD_SETCURSORPOS;
151 cmd.hdr.len_low = sizeof(cmd)/4;
153 cmd.x = giVideo_CursorX = X;
154 cmd.y = giVideo_CursorY = Y;
156 Video_int_SetBufFmt(PTYBUFFMT_2DCMD);
157 _SysWrite(giTerminalFD, &cmd, sizeof(cmd));
160 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
165 if(X < 0 || Y < 0) return;
166 if(W >= giScreenWidth) return;
167 if(H >= giScreenHeight) return;
168 if(X + W >= giScreenWidth) W = giScreenWidth - W;
169 if(Y + H >= giScreenHeight) W = giScreenHeight - H;
171 dest = gpScreenBuffer + Y * giScreenWidth + X;
174 for( i = W; i --; dest ++ ) *dest = Colour;
175 dest += giScreenWidth - W;
180 * \brief Blit an entire buffer to the screen
181 * \note Assumes Pitch = 4*W
183 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
188 if( DstX >= giScreenWidth) return ;
189 if( DstY >= giScreenHeight) return ;
190 // Drawing oob to left/top, clip
201 if( drawW < 0 ) return ;
202 // Drawing oob to the right, clip
203 if( DstX + drawW > giScreenWidth ) {
205 drawW = giScreenWidth - DstX;
207 if( DstY + H > giScreenHeight )
208 H = giScreenHeight - DstY;
210 if( W <= 0 || H <= 0 ) return;
212 giVideo_FirstDirtyLine = MIN(DstY, giVideo_FirstDirtyLine);
213 giVideo_LastDirtyLine = MAX(DstY+H, giVideo_LastDirtyLine);
215 buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
216 if(drawW != giScreenWidth || W != giScreenWidth)
220 memcpy(buf, Source, drawW*4);
222 buf += giScreenWidth;
227 // Only valid if copying full scanlines on both ends
228 memcpy(buf, Source, giScreenWidth*H*4);
232 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
234 uint16_t ao,ro,go,bo;
235 uint16_t an,rn,gn,bn;
236 if( _alpha == 0 ) return _orig;
237 if( _alpha == 255 ) return _new;
239 ao = (_orig >> 24) & 0xFF;
240 ro = (_orig >> 16) & 0xFF;
241 go = (_orig >> 8) & 0xFF;
242 bo = (_orig >> 0) & 0xFF;
244 an = (_new >> 24) & 0xFF;
245 rn = (_new >> 16) & 0xFF;
246 gn = (_new >> 8) & 0xFF;
247 bn = (_new >> 0) & 0xFF;
249 if( _alpha == 0x80 ) {
256 ao = ao*(255-_alpha) + an*_alpha;
257 ro = ro*(255-_alpha) + rn*_alpha;
258 go = go*(255-_alpha) + gn*_alpha;
259 bo = bo*(255-_alpha) + bn*_alpha;
266 return (ao << 24) | (ro << 16) | (go << 8) | bo;