Merge branch 'master' of git://cadel.mutabah.net/acess2
[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
93         // Set cursor position and bitmap
94         {
95         Video_int_SetBufFmt(PTYBUFFMT_2DCMD);
96         struct ptycmd_header    hdr = {PTY2D_CMD_SETCURSORBMP,0,0};
97         size_t size = sizeof(hdr) + sizeof(cCursorBitmap) + cCursorBitmap.W*cCursorBitmap.H*4;
98         hdr.len_low = size / 4;
99         hdr.len_hi = size / (256*4);
100         _SysWrite(giTerminalFD, &hdr, sizeof(hdr));
101         _SysDebug("size = %i (%04x:%02x * 4)", size, hdr.len_hi, hdr.len_low);
102         _SysWrite(giTerminalFD, &cCursorBitmap, size-sizeof(hdr));
103         }
104         Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
105 }
106
107 void Video_Update(void)
108 {
109         #if 0
110          int    ofs = giVideo_FirstDirtyLine*giScreenWidth;
111          int    size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
112         
113         if( giVideo_LastDirtyLine == 0 )        return; 
114
115         _SysDebug("Video_Update - Updating lines %i to %i (0x%x+0x%x px)",
116                 giVideo_FirstDirtyLine, giVideo_LastDirtyLine, ofs, size);
117         _SysSeek(giTerminalFD, ofs*4, SEEK_SET);
118         _SysDebug("Video_Update - Sending FD %i %p 0x%x", giTerminalFD, gpScreenBuffer+ofs, size*4);
119         _SysWrite(giTerminalFD, gpScreenBuffer+ofs, size*4);
120         _SysDebug("Video_Update - Done");
121         giVideo_FirstDirtyLine = giScreenHeight;
122         giVideo_LastDirtyLine = 0;
123         #else
124         size_t  size = giScreenHeight * giScreenWidth;
125         Video_int_SetBufFmt(PTYBUFFMT_FB);
126         _SysWrite(giTerminalFD, gpScreenBuffer, size*4);
127         #endif
128 }
129
130 void Video_int_SetBufFmt(int NewFmt)
131 {
132         static int current_fmt;
133         
134         if( current_fmt == NewFmt )
135                 return ;
136         
137         struct ptymode mode = {.InputMode = 0, .OutputMode = NewFmt};
138         int rv = _SysIOCtl( giTerminalFD, PTY_IOCTL_SETMODE, &mode );
139         if( rv ) {
140                 perror("Can't set PTY to framebuffer mode");
141                 exit(-1);
142         }
143         
144         current_fmt = NewFmt;
145 }
146
147 void Video_SetCursorPos(short X, short Y)
148 {
149         struct ptycmd_setcursorpos      cmd;
150         cmd.hdr.cmd = PTY2D_CMD_SETCURSORPOS;
151         cmd.hdr.len_low = sizeof(cmd)/4;
152         cmd.hdr.len_hi = 0;
153         cmd.x = giVideo_CursorX = X;
154         cmd.y = giVideo_CursorY = Y;
155
156         Video_int_SetBufFmt(PTYBUFFMT_2DCMD);   
157         _SysWrite(giTerminalFD, &cmd, sizeof(cmd));
158 }
159
160 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
161 {
162         uint32_t        *dest;
163          int    i;
164         
165         if(X < 0 || Y < 0)      return;
166         if(W >= giScreenWidth)  return;
167         if(H >= giScreenHeight) return;
168         if(X + W >= giScreenWidth)      W = giScreenWidth - W;
169         if(Y + H >= giScreenHeight)     W = giScreenHeight - H;
170         
171         dest = gpScreenBuffer + Y * giScreenWidth + X;
172         while(H --)
173         {
174                 for( i = W; i --; dest ++ )     *dest = Colour;
175                 dest += giScreenWidth - W;
176         }
177 }
178
179 /**
180  * \brief Blit an entire buffer to the screen
181  * \note Assumes Pitch = 4*W
182  */
183 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
184 {
185         uint32_t        *buf;
186         short   drawW = W;
187
188         if( DstX >= giScreenWidth)      return ;
189         if( DstY >= giScreenHeight)     return ;
190         // Drawing oob to left/top, clip
191         if( DstX < 0 ) {
192                 Source += -DstX;
193                 drawW -= -DstX;
194                 DstX = 0;
195         }
196         if( DstY < 0 ) {
197                 Source += (-DstY)*W;
198                 H -= -DstY;
199                 DstY = 0;
200         }
201         if( drawW < 0 ) return ;
202         // Drawing oob to the right, clip
203         if( DstX + drawW > giScreenWidth ) {
204                 //int oldw = drawW;
205                 drawW = giScreenWidth - DstX;
206         }
207         if( DstY + H > giScreenHeight )
208                 H = giScreenHeight - DstY;
209
210         if( W <= 0 || H <= 0 )  return;
211
212         giVideo_FirstDirtyLine = MIN(DstY, giVideo_FirstDirtyLine);
213         giVideo_LastDirtyLine  = MAX(DstY+H, giVideo_LastDirtyLine);
214         
215         buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
216         if(drawW != giScreenWidth || W != giScreenWidth)
217         {
218                 while( H -- )
219                 {
220                         memcpy(buf, Source, drawW*4);
221                         Source += W;
222                         buf += giScreenWidth;
223                 }
224         }
225         else
226         {
227                 // Only valid if copying full scanlines on both ends
228                 memcpy(buf, Source, giScreenWidth*H*4);
229         }
230 }
231
232 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
233 {
234         uint16_t        ao,ro,go,bo;
235         uint16_t        an,rn,gn,bn;
236         if( _alpha == 0 )       return _orig;
237         if( _alpha == 255 )     return _new;
238         
239         ao = (_orig >> 24) & 0xFF;
240         ro = (_orig >> 16) & 0xFF;
241         go = (_orig >>  8) & 0xFF;
242         bo = (_orig >>  0) & 0xFF;
243         
244         an = (_new >> 24) & 0xFF;
245         rn = (_new >> 16) & 0xFF;
246         gn = (_new >>  8) & 0xFF;
247         bn = (_new >>  0) & 0xFF;
248
249         if( _alpha == 0x80 ) {
250                 ao = (ao + an) / 2;
251                 ro = (ro + rn) / 2;
252                 go = (go + gn) / 2;
253                 bo = (bo + bn) / 2;
254         }
255         else {
256                 ao = ao*(255-_alpha) + an*_alpha;
257                 ro = ro*(255-_alpha) + rn*_alpha;
258                 go = go*(255-_alpha) + gn*_alpha;
259                 bo = bo*(255-_alpha) + bn*_alpha;
260                 ao /= 255*2;
261                 ro /= 255*2;
262                 go /= 255*2;
263                 bo /= 255*2;
264         }
265
266         return (ao << 24) | (ro << 16) | (go << 8) | bo;
267 }
268

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