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 _SysDebug("AxWin3 running at %ix%i", dims.PW, dims.PH);
81 giVideo_LastDirtyLine = giScreenHeight;
83 // Create local framebuffer (back buffer)
84 gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
86 // Set cursor position and bitmap
87 // TODO: This will require using the 2DCMD buffer format
88 //_SysIOCtl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
89 Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
92 void Video_Update(void)
95 int ofs = giVideo_FirstDirtyLine*giScreenWidth;
96 int size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
98 if( giVideo_LastDirtyLine == 0 ) return;
100 _SysDebug("Video_Update - Updating lines %i to %i (0x%x+0x%x px)",
101 giVideo_FirstDirtyLine, giVideo_LastDirtyLine, ofs, size);
102 _SysSeek(giTerminalFD, ofs*4, SEEK_SET);
103 _SysDebug("Video_Update - Sending FD %i %p 0x%x", giTerminalFD, gpScreenBuffer+ofs, size*4);
104 _SysWrite(giTerminalFD, gpScreenBuffer+ofs, size*4);
105 _SysDebug("Video_Update - Done");
106 giVideo_FirstDirtyLine = giScreenHeight;
107 giVideo_LastDirtyLine = 0;
109 size_t size = giScreenHeight * giScreenWidth;
110 Video_int_SetBufFmt(PTYBUFFMT_FB);
111 _SysWrite(giTerminalFD, gpScreenBuffer, size*4);
115 void Video_int_SetBufFmt(int NewFmt)
117 static int current_fmt;
119 if( current_fmt == NewFmt )
122 struct ptymode mode = {.InputMode = 0, .OutputMode = NewFmt};
123 int rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_SETMODE, &mode );
125 perror("Can't set PTY to framebuffer mode");
129 current_fmt = NewFmt;
132 void Video_SetCursorPos(short X, short Y)
134 struct ptycmd_setcursorpos cmd;
135 cmd.cmd = PTY2D_CMD_SETCURSORPOS;
136 cmd.x = giVideo_CursorX = X;
137 cmd.y = giVideo_CursorY = Y;
139 Video_int_SetBufFmt(PTYBUFFMT_2DCMD);
140 _SysWrite(giTerminalFD, &cmd, sizeof(cmd));
143 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
148 if(X < 0 || Y < 0) return;
149 if(W >= giScreenWidth) return;
150 if(H >= giScreenHeight) return;
151 if(X + W >= giScreenWidth) W = giScreenWidth - W;
152 if(Y + H >= giScreenHeight) W = giScreenHeight - H;
154 dest = gpScreenBuffer + Y * giScreenWidth + X;
157 for( i = W; i --; dest ++ ) *dest = Colour;
158 dest += giScreenWidth - W;
163 * \brief Blit an entire buffer to the screen
164 * \note Assumes Pitch = 4*W
166 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
171 if( DstX >= giScreenWidth) return ;
172 if( DstY >= giScreenHeight) return ;
173 // Drawing oob to left/top, clip
184 if( drawW < 0 ) return ;
185 // Drawing oob to the right, clip
186 if( DstX + drawW > giScreenWidth ) {
188 drawW = giScreenWidth - DstX;
190 if( DstY + H > giScreenHeight )
191 H = giScreenHeight - DstY;
193 if( W <= 0 || H <= 0 ) return;
195 giVideo_FirstDirtyLine = MIN(DstY, giVideo_FirstDirtyLine);
196 giVideo_LastDirtyLine = MAX(DstY+H, giVideo_LastDirtyLine);
198 buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
199 if(drawW != giScreenWidth || W != giScreenWidth)
203 memcpy(buf, Source, drawW*4);
205 buf += giScreenWidth;
210 // Only valid if copying full scanlines on both ends
211 memcpy(buf, Source, giScreenWidth*H*4);
215 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
217 uint16_t ao,ro,go,bo;
218 uint16_t an,rn,gn,bn;
219 if( _alpha == 0 ) return _orig;
220 if( _alpha == 255 ) return _new;
222 ao = (_orig >> 24) & 0xFF;
223 ro = (_orig >> 16) & 0xFF;
224 go = (_orig >> 8) & 0xFF;
225 bo = (_orig >> 0) & 0xFF;
227 an = (_new >> 24) & 0xFF;
228 rn = (_new >> 16) & 0xFF;
229 gn = (_new >> 8) & 0xFF;
230 bn = (_new >> 0) & 0xFF;
232 if( _alpha == 0x80 ) {
239 ao = ao*(255-_alpha) + an*_alpha;
240 ro = ro*(255-_alpha) + rn*_alpha;
241 go = go*(255-_alpha) + gn*_alpha;
242 bo = bo*(255-_alpha) + bn*_alpha;
249 return (ao << 24) | (ro << 16) | (go << 8) | bo;