Usermode/libc - Fix strchr and strrchr behavior
[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 #include "include/lowlevel.h"
19
20 // === IMPORTS ===
21 extern int      giTerminalFD_Input;
22
23 // === PROTOTYPES ===
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);
29
30 // === GLOBALS ===
31  int    giVideo_CursorX;
32  int    giVideo_CursorY;
33 uint32_t        *gpScreenBuffer;
34  int    giVideo_FirstDirtyLine;
35  int    giVideo_LastDirtyLine;
36
37 // === CODE ===
38 void Video_Setup(void)
39 {
40          int    rv;
41         
42         // Open terminal
43         #if 0
44         giTerminalFD = _SysOpen(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
45         if( giTerminalFD == -1 )
46         {
47                 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
48                 exit(-1);
49         }
50         #else
51         giTerminalFD = 1;
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 )
57         {
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");
61                 exit(-1);
62         }
63         #endif
64
65         // TODO: Check terminal echoback that it does support graphical modes
66         // And/or have terminal flags fetchable by the client
67
68         // Set mode to video
69         Video_int_SetBufFmt(PTYBUFFMT_FB);      
70
71         // Get dimensions
72         struct ptydims dims;
73         rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_GETDIMS, &dims );
74         if( rv ) {
75                 perror("Can't get terminal dimensions (WTF?)");
76                 exit(-1);
77         }
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);
84                 exit(-1);
85         }
86         _SysDebug("AxWin3 running at %ix%i", dims.PW, dims.PH);
87
88         giVideo_LastDirtyLine = giScreenHeight;
89         
90         // Create local framebuffer (back buffer)
91         gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
92         //gpScreenBuffer = _SysMemMap( giTerminalFD, 0, giScreenWidth*giScreenHeight*4, NULL);
93
94         // Set cursor position and bitmap
95         {
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));
104         }
105         Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
106 }
107
108 void Video_Update(void)
109 {
110         #if 0
111          int    ofs = giVideo_FirstDirtyLine*giScreenWidth;
112          int    size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
113         
114         if( giVideo_LastDirtyLine == 0 )        return; 
115
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;
124         #else
125         size_t  size = giScreenHeight * giScreenWidth;
126         Video_int_SetBufFmt(PTYBUFFMT_FB);
127         _SysWrite(giTerminalFD, gpScreenBuffer, size*4);
128         #endif
129 }
130
131 void Video_int_SetBufFmt(int NewFmt)
132 {
133         static int current_fmt;
134         
135         if( current_fmt == NewFmt )
136                 return ;
137         
138         struct ptymode mode = {.InputMode = 0, .OutputMode = NewFmt};
139         int rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_SETMODE, &mode );
140         if( rv ) {
141                 perror("Can't set PTY to framebuffer mode");
142                 exit(-1);
143         }
144         
145         current_fmt = NewFmt;
146 }
147
148 void Video_SetCursorPos(short X, short Y)
149 {
150         struct ptycmd_setcursorpos      cmd;
151         cmd.hdr.cmd = PTY2D_CMD_SETCURSORPOS;
152         cmd.hdr.len_low = sizeof(cmd)/4;
153         cmd.hdr.len_hi = 0;
154         cmd.x = giVideo_CursorX = X;
155         cmd.y = giVideo_CursorY = Y;
156
157         Video_int_SetBufFmt(PTYBUFFMT_2DCMD);   
158         _SysWrite(giTerminalFD, &cmd, sizeof(cmd));
159 }
160
161 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
162 {
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;
169         
170         uint32_t        *dest = gpScreenBuffer + Y * giScreenWidth + X;
171         while(H --)
172         {
173                 for( int i = W; i --; dest ++ ) *dest = Colour;
174                 dest += giScreenWidth - W;
175         }
176 }
177
178 /**
179  * \brief Blit an entire buffer to the screen
180  * \note Assumes Pitch = 4*W
181  */
182 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
183 {
184         uint32_t        *buf;
185         short   drawW = W;
186
187         if( DstX >= giScreenWidth)      return ;
188         if( DstY >= giScreenHeight)     return ;
189         // Drawing oob to left/top, clip
190         if( DstX < 0 ) {
191                 Source += -DstX;
192                 drawW -= -DstX;
193                 DstX = 0;
194         }
195         if( DstY < 0 ) {
196                 Source += (-DstY)*W;
197                 H -= -DstY;
198                 DstY = 0;
199         }
200         if( drawW < 0 ) return ;
201         // Drawing oob to the right, clip
202         if( DstX + drawW > giScreenWidth ) {
203                 //int oldw = drawW;
204                 drawW = giScreenWidth - DstX;
205         }
206         if( DstY + H > giScreenHeight )
207                 H = giScreenHeight - DstY;
208
209         if( W <= 0 || H <= 0 )  return;
210
211         giVideo_FirstDirtyLine = MIN(DstY, giVideo_FirstDirtyLine);
212         giVideo_LastDirtyLine  = MAX(DstY+H, giVideo_LastDirtyLine);
213         
214         buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
215         if(drawW != giScreenWidth || W != giScreenWidth)
216         {
217                 while( H -- )
218                 {
219                         memcpy(buf, Source, drawW*4);
220                         Source += W;
221                         buf += giScreenWidth;
222                 }
223         }
224         else
225         {
226                 // Only valid if copying full scanlines on both ends
227                 memcpy(buf, Source, giScreenWidth*H*4);
228         }
229 }
230
231 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
232 {
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;
237         
238         ao = (_orig >> 24) & 0xFF;
239         ro = (_orig >> 16) & 0xFF;
240         go = (_orig >>  8) & 0xFF;
241         bo = (_orig >>  0) & 0xFF;
242         
243         an = (_new >> 24) & 0xFF;
244         rn = (_new >> 16) & 0xFF;
245         gn = (_new >>  8) & 0xFF;
246         bn = (_new >>  0) & 0xFF;
247
248         if( _alpha == 0x80 ) {
249                 ao = (ao + an) / 2;
250                 ro = (ro + rn) / 2;
251                 go = (go + gn) / 2;
252                 bo = (bo + bn) / 2;
253         }
254         else {
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;
259                 ao /= 255*2;
260                 ro /= 255*2;
261                 go /= 255*2;
262                 bo /= 255*2;
263         }
264
265         return (ao << 24) | (ro << 16) | (go << 8) | bo;
266 }
267

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