Kernel - Misc fixes and debug in VTerm/PTY, AxWin3 starts again
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / video.c
1 /*
2  * Acess2 GUI (AxWin) Version 3
3  * - By John Hodge (thePowersGang)
4  *
5  * video.c
6  * - Video methods
7  */
8 #include <common.h>
9 #include <acess/sys.h>
10 #include <acess/devices/pty.h>
11 #include <acess/devices/pty_cmds.h>
12 #include <image.h>
13 #include "resources/cursor.h"
14 #include <stdio.h>
15 #include <video.h>
16 #include <wm.h>
17 #include <string.h>
18
19 // === IMPORTS ===
20 extern int      giTerminalFD_Input;
21
22 // === PROTOTYPES ===
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);
28
29 // === GLOBALS ===
30  int    giVideo_CursorX;
31  int    giVideo_CursorY;
32 uint32_t        *gpScreenBuffer;
33  int    giVideo_FirstDirtyLine;
34  int    giVideo_LastDirtyLine;
35
36 // === CODE ===
37 void Video_Setup(void)
38 {
39          int    rv;
40         
41         // Open terminal
42         #if 0
43         giTerminalFD = _SysOpen(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
44         if( giTerminalFD == -1 )
45         {
46                 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
47                 exit(-1);
48         }
49         #else
50         giTerminalFD = 1;
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 )
56         {
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");
60                 exit(-1);
61         }
62         #endif
63
64         // TODO: Check terminal echoback that it does support graphical modes
65         // And/or have terminal flags fetchable by the client
66
67         // Set mode to video
68         Video_int_SetBufFmt(PTYBUFFMT_FB);      
69
70         // Get dimensions
71         struct ptydims dims;
72         rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_GETDIMS, &dims );
73         if( rv ) {
74                 perror("Can't get terminal dimensions (WTF?)");
75                 exit(-1);
76         }
77         giScreenWidth = dims.PW;
78         giScreenHeight = dims.PH;
79         _SysDebug("AxWin3 running at %ix%i", dims.PW, dims.PH);
80
81         giVideo_LastDirtyLine = giScreenHeight;
82         
83         // Create local framebuffer (back buffer)
84         gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
85
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 );
90 }
91
92 void Video_Update(void)
93 {
94         #if 0
95          int    ofs = giVideo_FirstDirtyLine*giScreenWidth;
96          int    size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
97         
98         if( giVideo_LastDirtyLine == 0 )        return; 
99
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;
108         #else
109         size_t  size = giScreenHeight * giScreenWidth;
110         Video_int_SetBufFmt(PTYBUFFMT_FB);
111         _SysWrite(giTerminalFD, gpScreenBuffer, size*4);
112         #endif
113 }
114
115 void Video_int_SetBufFmt(int NewFmt)
116 {
117         static int current_fmt;
118         
119         if( current_fmt == NewFmt )
120                 return ;
121         
122         struct ptymode mode = {.InputMode = 0, .OutputMode = NewFmt};
123         int rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_SETMODE, &mode );
124         if( rv ) {
125                 perror("Can't set PTY to framebuffer mode");
126                 exit(-1);
127         }
128         
129         current_fmt = NewFmt;
130 }
131
132 void Video_SetCursorPos(short X, short Y)
133 {
134         struct ptycmd_setcursorpos      cmd;
135         cmd.cmd = PTY2D_CMD_SETCURSORPOS;
136         cmd.x = giVideo_CursorX = X;
137         cmd.y = giVideo_CursorY = Y;
138
139         Video_int_SetBufFmt(PTYBUFFMT_2DCMD);   
140         _SysWrite(giTerminalFD, &cmd, sizeof(cmd));
141 }
142
143 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
144 {
145         uint32_t        *dest;
146          int    i;
147         
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;
153         
154         dest = gpScreenBuffer + Y * giScreenWidth + X;
155         while(H --)
156         {
157                 for( i = W; i --; dest ++ )     *dest = Colour;
158                 dest += giScreenWidth - W;
159         }
160 }
161
162 /**
163  * \brief Blit an entire buffer to the screen
164  * \note Assumes Pitch = 4*W
165  */
166 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
167 {
168         uint32_t        *buf;
169         short   drawW = W;
170
171         if( DstX >= giScreenWidth)      return ;
172         if( DstY >= giScreenHeight)     return ;
173         // Drawing oob to left/top, clip
174         if( DstX < 0 ) {
175                 Source += -DstX;
176                 drawW -= -DstX;
177                 DstX = 0;
178         }
179         if( DstY < 0 ) {
180                 Source += (-DstY)*W;
181                 H -= -DstY;
182                 DstY = 0;
183         }
184         if( drawW < 0 ) return ;
185         // Drawing oob to the right, clip
186         if( DstX + drawW > giScreenWidth ) {
187                 //int oldw = drawW;
188                 drawW = giScreenWidth - DstX;
189         }
190         if( DstY + H > giScreenHeight )
191                 H = giScreenHeight - DstY;
192
193         if( W <= 0 || H <= 0 )  return;
194
195         giVideo_FirstDirtyLine = MIN(DstY, giVideo_FirstDirtyLine);
196         giVideo_LastDirtyLine  = MAX(DstY+H, giVideo_LastDirtyLine);
197         
198         buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
199         if(drawW != giScreenWidth || W != giScreenWidth)
200         {
201                 while( H -- )
202                 {
203                         memcpy(buf, Source, drawW*4);
204                         Source += W;
205                         buf += giScreenWidth;
206                 }
207         }
208         else
209         {
210                 // Only valid if copying full scanlines on both ends
211                 memcpy(buf, Source, giScreenWidth*H*4);
212         }
213 }
214
215 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
216 {
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;
221         
222         ao = (_orig >> 24) & 0xFF;
223         ro = (_orig >> 16) & 0xFF;
224         go = (_orig >>  8) & 0xFF;
225         bo = (_orig >>  0) & 0xFF;
226         
227         an = (_new >> 24) & 0xFF;
228         rn = (_new >> 16) & 0xFF;
229         gn = (_new >>  8) & 0xFF;
230         bn = (_new >>  0) & 0xFF;
231
232         if( _alpha == 0x80 ) {
233                 ao = (ao + an) / 2;
234                 ro = (ro + rn) / 2;
235                 go = (go + gn) / 2;
236                 bo = (bo + bn) / 2;
237         }
238         else {
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;
243                 ao /= 255*2;
244                 ro /= 255*2;
245                 go /= 255*2;
246                 bo /= 255*2;
247         }
248
249         return (ao << 24) | (ro << 16) | (go << 8) | bo;
250 }
251

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