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 );
92 //gpScreenBuffer = _SysMemMap( giTerminalFD, 0, giScreenWidth*giScreenHeight*4, NULL);
94 // Set cursor position and bitmap
96 Video_int_SetBufFmt(PTYBUFFMT_2DCMD);
97 struct ptycmd_header hdr = {PTY2D_CMD_SETCURSORBMP,0,0};
98 size_t size = sizeof(hdr) + sizeof(cCursorBitmap) + cCursorBitmap.W*cCursorBitmap.H*4;
99 hdr.len_low = size / 4;
100 hdr.len_hi = size / (256*4);
101 _SysWrite(giTerminalFD, &hdr, sizeof(hdr));
102 _SysDebug("size = %i (%04x:%02x * 4)", size, hdr.len_hi, hdr.len_low);
103 _SysWrite(giTerminalFD, &cCursorBitmap, size-sizeof(hdr));
105 Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
108 void Video_Update(void)
111 int ofs = giVideo_FirstDirtyLine*giScreenWidth;
112 int size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
114 if( giVideo_LastDirtyLine == 0 ) return;
116 _SysDebug("Video_Update - Updating lines %i to %i (0x%x+0x%x px)",
117 giVideo_FirstDirtyLine, giVideo_LastDirtyLine, ofs, size);
118 _SysSeek(giTerminalFD, ofs*4, SEEK_SET);
119 _SysDebug("Video_Update - Sending FD %i %p 0x%x", giTerminalFD, gpScreenBuffer+ofs, size*4);
120 _SysWrite(giTerminalFD, gpScreenBuffer+ofs, size*4);
121 _SysDebug("Video_Update - Done");
122 giVideo_FirstDirtyLine = giScreenHeight;
123 giVideo_LastDirtyLine = 0;
125 size_t size = giScreenHeight * giScreenWidth;
126 Video_int_SetBufFmt(PTYBUFFMT_FB);
127 _SysWrite(giTerminalFD, gpScreenBuffer, size*4);
131 void Video_int_SetBufFmt(int NewFmt)
133 static int current_fmt;
135 if( current_fmt == NewFmt )
138 struct ptymode mode = {.InputMode = 0, .OutputMode = NewFmt};
139 int rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_SETMODE, &mode );
141 perror("Can't set PTY to framebuffer mode");
145 current_fmt = NewFmt;
148 void Video_SetCursorPos(short X, short Y)
150 struct ptycmd_setcursorpos cmd;
151 cmd.hdr.cmd = PTY2D_CMD_SETCURSORPOS;
152 cmd.hdr.len_low = sizeof(cmd)/4;
154 cmd.x = giVideo_CursorX = X;
155 cmd.y = giVideo_CursorY = Y;
157 Video_int_SetBufFmt(PTYBUFFMT_2DCMD);
158 _SysWrite(giTerminalFD, &cmd, sizeof(cmd));
161 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
163 if(X < 0 || Y < 0) return;
164 if(W < 0 || H < 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 uint32_t *dest = gpScreenBuffer + Y * giScreenWidth + X;
173 for( int 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;