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"
20 extern int giTerminalFD_Input;
23 void Video_Setup(void);
24 void Video_int_SetBufFmt(int NewFmt);
25 void Video_SetCursorPos(short X, short Y);
26 void Video_Update(void);
27 void Video_FillRect(int X, int Y, int W, int H, uint32_t Color);
32 uint32_t *gpScreenBuffer;
33 int giVideo_FirstDirtyLine;
34 int giVideo_LastDirtyLine;
37 void Video_Setup(void)
43 giTerminalFD = _SysOpen(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
44 if( giTerminalFD == -1 )
46 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
51 giTerminalFD_Input = 0;
52 // Check that the console is a PTY
53 // - _SysIOCtl(..., 0, NULL) returns the type, which should be 2
54 rv = _SysIOCtl(1, DRV_IOCTL_TYPE, NULL);
55 if( rv != DRV_TYPE_TERMINAL )
57 fprintf(stderr, "stdout is not a PTY, can't start (%i exp, %i got)\n",
58 DRV_TYPE_TERMINAL, rv);
59 _SysDebug("stdout is not an PTY, can't start");
64 // TODO: Check terminal echoback that it does support graphical modes
65 // And/or have terminal flags fetchable by the client
68 Video_int_SetBufFmt(PTYBUFFMT_FB);
72 rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_GETDIMS, &dims );
74 perror("Can't get terminal dimensions (WTF?)");
77 giScreenWidth = dims.PW;
78 giScreenHeight = dims.PH;
79 if( giScreenWidth < 640 || giScreenHeight < 480 ) {
80 Video_int_SetBufFmt(PTYBUFFMT_TEXT);
81 _SysDebug("Resoltion too small, 640x480 reqd but %ix%i avail",
82 giScreenWidth, giScreenHeight);
85 _SysDebug("AxWin3 running at %ix%i", dims.PW, dims.PH);
87 giVideo_LastDirtyLine = giScreenHeight;
89 // Create local framebuffer (back buffer)
90 gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
92 // Set cursor position and bitmap
94 Video_int_SetBufFmt(PTYBUFFMT_2DCMD);
95 struct ptycmd_header hdr = {PTY2D_CMD_SETCURSORBMP,0,0};
96 size_t size = sizeof(hdr) + sizeof(cCursorBitmap) + cCursorBitmap.W*cCursorBitmap.H*4;
97 hdr.len_low = size / 4;
98 hdr.len_hi = size / (256*4);
99 _SysWrite(giTerminalFD, &hdr, sizeof(hdr));
100 _SysDebug("size = %i (%04x:%02x * 4)", size, hdr.len_hi, hdr.len_low);
101 _SysWrite(giTerminalFD, &cCursorBitmap, size-sizeof(hdr));
103 Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
106 void Video_Update(void)
109 int ofs = giVideo_FirstDirtyLine*giScreenWidth;
110 int size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
112 if( giVideo_LastDirtyLine == 0 ) return;
114 _SysDebug("Video_Update - Updating lines %i to %i (0x%x+0x%x px)",
115 giVideo_FirstDirtyLine, giVideo_LastDirtyLine, ofs, size);
116 _SysSeek(giTerminalFD, ofs*4, SEEK_SET);
117 _SysDebug("Video_Update - Sending FD %i %p 0x%x", giTerminalFD, gpScreenBuffer+ofs, size*4);
118 _SysWrite(giTerminalFD, gpScreenBuffer+ofs, size*4);
119 _SysDebug("Video_Update - Done");
120 giVideo_FirstDirtyLine = giScreenHeight;
121 giVideo_LastDirtyLine = 0;
123 size_t size = giScreenHeight * giScreenWidth;
124 Video_int_SetBufFmt(PTYBUFFMT_FB);
125 _SysWrite(giTerminalFD, gpScreenBuffer, size*4);
129 void Video_int_SetBufFmt(int NewFmt)
131 static int current_fmt;
133 if( current_fmt == NewFmt )
136 struct ptymode mode = {.InputMode = 0, .OutputMode = NewFmt};
137 int rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_SETMODE, &mode );
139 perror("Can't set PTY to framebuffer mode");
143 current_fmt = NewFmt;
146 void Video_SetCursorPos(short X, short Y)
148 struct ptycmd_setcursorpos cmd;
149 cmd.hdr.cmd = PTY2D_CMD_SETCURSORPOS;
150 cmd.hdr.len_low = sizeof(cmd)/4;
152 cmd.x = giVideo_CursorX = X;
153 cmd.y = giVideo_CursorY = Y;
155 Video_int_SetBufFmt(PTYBUFFMT_2DCMD);
156 _SysWrite(giTerminalFD, &cmd, sizeof(cmd));
159 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
164 if(X < 0 || Y < 0) return;
165 if(W >= giScreenWidth) return;
166 if(H >= giScreenHeight) return;
167 if(X + W >= giScreenWidth) W = giScreenWidth - W;
168 if(Y + H >= giScreenHeight) W = giScreenHeight - H;
170 dest = gpScreenBuffer + Y * giScreenWidth + X;
173 for( i = W; i --; dest ++ ) *dest = Colour;
174 dest += giScreenWidth - W;
179 * \brief Blit an entire buffer to the screen
180 * \note Assumes Pitch = 4*W
182 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
187 if( DstX >= giScreenWidth) return ;
188 if( DstY >= giScreenHeight) return ;
189 // Drawing oob to left/top, clip
200 if( drawW < 0 ) return ;
201 // Drawing oob to the right, clip
202 if( DstX + drawW > giScreenWidth ) {
204 drawW = giScreenWidth - DstX;
206 if( DstY + H > giScreenHeight )
207 H = giScreenHeight - DstY;
209 if( W <= 0 || H <= 0 ) return;
211 giVideo_FirstDirtyLine = MIN(DstY, giVideo_FirstDirtyLine);
212 giVideo_LastDirtyLine = MAX(DstY+H, giVideo_LastDirtyLine);
214 buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
215 if(drawW != giScreenWidth || W != giScreenWidth)
219 memcpy(buf, Source, drawW*4);
221 buf += giScreenWidth;
226 // Only valid if copying full scanlines on both ends
227 memcpy(buf, Source, giScreenWidth*H*4);
231 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
233 uint16_t ao,ro,go,bo;
234 uint16_t an,rn,gn,bn;
235 if( _alpha == 0 ) return _orig;
236 if( _alpha == 255 ) return _new;
238 ao = (_orig >> 24) & 0xFF;
239 ro = (_orig >> 16) & 0xFF;
240 go = (_orig >> 8) & 0xFF;
241 bo = (_orig >> 0) & 0xFF;
243 an = (_new >> 24) & 0xFF;
244 rn = (_new >> 16) & 0xFF;
245 gn = (_new >> 8) & 0xFF;
246 bn = (_new >> 0) & 0xFF;
248 if( _alpha == 0x80 ) {
255 ao = ao*(255-_alpha) + an*_alpha;
256 ro = ro*(255-_alpha) + rn*_alpha;
257 go = go*(255-_alpha) + gn*_alpha;
258 bo = bo*(255-_alpha) + bn*_alpha;
265 return (ao << 24) | (ro << 16) | (go << 8) | bo;