Fixed false positive on VESA cursor OOB checks
[tpg/acess2.git] / Usermode / Applications / axwin2_src / WM / video.c
1 /*
2  * Acess GUI (AxWin) Version 2
3  * By John Hodge (thePowersGang)
4  */
5 #include "common.h"
6 #include <acess/sys.h>
7 #include <acess/devices/terminal.h>
8 #include <image.h>
9
10 // === PROTOTYPES ===
11 void    Video_Setup(void);
12 void    Video_Update(void);
13 void    Video_FillRect(short X, short Y, short W, short H, uint32_t Color);
14
15 // === GLOBALS ===
16
17 // === CODE ===
18 void Video_Setup(void)
19 {
20          int    tmpInt;
21         
22         // Open terminal
23         giTerminalFD = open(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
24         if( giTerminalFD == -1 )
25         {
26                 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
27                 exit(-1);
28         }
29         
30         // Set width
31         tmpInt = giScreenWidth;
32         tmpInt = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, &tmpInt );
33         if(tmpInt != giScreenWidth)
34         {
35                 fprintf(stderr, "Warning: Selected width (%i) is invalid, clipped to %i\n",
36                         giScreenWidth, tmpInt);
37                 giScreenWidth = tmpInt;
38         }
39         
40         // Set height
41         tmpInt = giScreenHeight;
42         tmpInt = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, &tmpInt );
43         if(tmpInt != giScreenHeight)
44         {
45                 fprintf(stderr, "Warning: Selected height (%i) is invalid, clipped to %i\n",
46                         giScreenHeight, tmpInt);
47                 giScreenHeight = tmpInt;
48         }
49         
50         // Set mode to video
51         tmpInt = TERM_MODE_FB;
52         ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
53         
54         // Force VT8 to be shown
55         ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
56         
57         // Create local framebuffer (back buffer)
58         gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
59         memset32( gpScreenBuffer, 0x8888FF, giScreenWidth*giScreenHeight );
60         Video_Update();
61 }
62
63 void Video_Update(void)
64 {
65         //seek(giTerminalFD, 0, SEEK_SET);
66         seek(giTerminalFD, 0, 1);
67         write(giTerminalFD, giScreenWidth*giScreenHeight*4, gpScreenBuffer);
68 }
69
70 void Video_FillRect(short X, short Y, short W, short H, uint32_t Color)
71 {
72         uint32_t        *buf = gpScreenBuffer + Y*giScreenWidth + X;
73         
74         _SysDebug("Video_FillRect: (X=%i, Y=%i, W=%i, H=%i, Color=%08x)",
75                 X, Y, W, H, Color);
76         
77         if(W < 0 || X < 0 || X >= giScreenWidth)        return ;
78         if(X + W > giScreenWidth)       W = giScreenWidth - X;
79         
80         if(H < 0 || Y < 0 || Y >= giScreenHeight)       return ;
81         if(Y + H > giScreenHeight)      H = giScreenHeight - Y;
82         
83         while( H -- )
84         {
85                 memset32( buf, Color, W );
86                 buf += giScreenWidth;
87         }
88 }
89
90 void Video_DrawRect(short X, short Y, short W, short H, uint32_t Color)
91 {       
92         Video_FillRect(X, Y, W, 1, Color);
93         Video_FillRect(X, Y+H-1, W, 1, Color);
94         Video_FillRect(X, Y, 1, H, Color);
95         Video_FillRect(X+W-1, Y, 1, H, Color);
96 }
97
98 /**
99  * \brief Draw text to the screen
100  */
101 void Video_DrawText(short X, short Y, short W, short H, void *Font, int Point, uint32_t Color, char *Text)
102 {
103         // TODO!
104 }
105
106 /**
107  * \brief Draw an image to the screen
108  * \todo Maybe have support for an offset in the image
109  */
110 void Video_DrawImage(short X, short Y, short W, short H, tImage *Image)
111 {
112          int    x, y;
113         uint8_t *buf = (uint8_t *)(gpScreenBuffer + Y*giScreenWidth + X);
114         uint8_t *data = Image->Data;
115         
116         // Bounds Check
117         if( X >= giScreenWidth )        return ;
118         if( Y >= giScreenHeight )       return ;
119         
120         // Wrap to image size
121         if( W > Image->Width )  W = Image->Width;
122         if( H > Image->Height ) H = Image->Height;
123         
124         // Wrap to screen size
125         if( X + W > giScreenWidth )     W = giScreenWidth - X;
126         if( Y + H > giScreenHeight )    H = giScreenHeight - Y;
127         
128         switch( Image->Format )
129         {
130         case IMGFMT_BGRA:
131                 for( y = 0; y < H; y ++ )
132                 {
133                          int    r, g, b, a;     // New
134                          int    or, og, ob;     // Original
135                         for( x = 0; x < W; x ++ ) {
136                                 b = data[x*4+0]; g = data[x*4+1]; r = data[x*4+2]; a = data[x*4+3];
137                                 if( a == 0 )    continue;       // 100% transparent
138                                 ob = buf[x*4+0]; og = buf[x*4+1]; or = buf[x*4+2];
139                                 // Handle Alpha
140                                 switch(a)
141                                 {
142                                 // Transparent: Handled above
143                                 // Solid
144                                 case 0xFF:      break;
145                                 // Half
146                                 case 0x80:
147                                         r = (or + r) / 2;
148                                         g = (og + g) / 2;
149                                         b = (ob + b) / 2;
150                                         break;
151                                 // General
152                                 default:
153                                         r = (or * (255-a) + r * a) / 255;
154                                         g = (og * (255-a) + g * a) / 255;
155                                         b = (ob * (255-a) + b * a) / 255;
156                                         break;
157                                 }
158                                 buf[x*4+0] = b; buf[x*4+1] = g; buf[x*4+2] = r;
159                         }
160                         data += Image->Width * 4;
161                         buf += giScreenWidth * 4;
162                 }
163                 break;
164         
165         // RGB
166         case IMGFMT_RGB:
167                 for( y = 0; y < H; y ++ )
168                 {
169                         for( x = 0; x < W; x ++ ) {
170                                 buf[x*4+0] = data[x*3+2];       // Blue
171                                 buf[x*4+1] = data[x*3+1];       // Green
172                                 buf[x*4+2] = data[x*3+0];       // Red
173                         }
174                         data += W * 3;
175                         buf += giScreenWidth * 4;
176                 }
177                 break;
178         }
179 }

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