AcessNative - Fixing for recent kernel changes
[tpg/acess2.git] / AcessNative / acesskernel_src / video.c
1 /*
2  * Acess2 Native Kernel
3  * 
4  * Video Driver
5  */
6 #define VERSION ((0<<8)|10)
7 #define DEBUG   0
8 #include <acess.h>
9 #include <vfs.h>
10 #include <fs_devfs.h>
11 #include <modules.h>
12 #include <api_drv_video.h>
13 #include "ui.h"
14
15 // === PROTOTYPES ===
16  int    Video_Install(char **Arguments);
17 Uint64  Video_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
18 Uint64  Video_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer);
19  int    Video_IOCtl(tVFS_Node *Node, int ID, void *Data);
20 // --- 2D Acceleration Functions --
21 void    Video_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour);
22 void    Video_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H);
23
24 // === GLOBALS ===
25 //MODULE_DEFINE(0, VERSION, NativeVideo, Video_Install, NULL, NULL);
26 tVFS_NodeType   gVideo_NodeType = {
27         .Read = Video_Read,
28         .Write = Video_Write,
29         .IOCtl = Video_IOCtl
30 };
31 tDevFS_Driver   gVideo_DriverStruct = {
32         NULL, "NativeVideo",
33         {.Type = &gVideo_NodeType}
34 };
35  int    giVideo_DriverID;
36  int    giVideo_CurrentFormat;
37 // --- 2D Video Stream Handlers ---
38 tDrvUtil_Video_2DHandlers       gVideo_2DFunctions = {
39         NULL,
40         Video_2D_Fill,
41         Video_2D_Blit
42 };
43
44 // === CODE ===
45 int Video_Install(char **Arguments)
46 {
47         // Install Device
48         giVideo_DriverID = DevFS_AddDevice( &gVideo_DriverStruct );
49         if(giVideo_DriverID == -1)
50                 return MODULE_ERR_MISC;
51         
52         return MODULE_ERR_OK;
53 }
54
55 /**
56  * \brief Read from framebuffer (unimplemented)
57  */
58 Uint64 Video_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
59 {
60         return 0;
61 }
62
63 /**
64  * \brief Write to the framebuffer
65  */
66 Uint64 Video_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer)
67 {
68          int    i;
69         ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
70
71         if(Buffer == NULL) {
72                 LEAVE('i', 0);
73                 return 0;
74         }
75         // Text Mode
76         switch( giVideo_CurrentFormat )
77         {
78         case VIDEO_BUFFMT_TEXT:
79                 {
80                 const tVT_Char  *chars = Buffer;
81                 // int  pitch = giUI_Pitch;
82                  int    widthInChars = giUI_Width/giVT_CharWidth;
83                  int    heightInChars = giUI_Height/giVT_CharHeight;
84                  int    x, y;
85                 Uint32  tmpBuf[giVT_CharHeight*giVT_CharWidth];
86                 
87                 Length /= sizeof(tVT_Char);
88                 Offset /= sizeof(tVT_Char);
89                 
90                 x = Offset % widthInChars;
91                 y = Offset / widthInChars;
92                 
93                 // Sanity Check
94                 if( Offset > (Uint64)(heightInChars*widthInChars) ) {
95                         Log_Notice("Video", "Offset (%i) > %i*%i (%i)", Offset,
96                                 heightInChars, widthInChars, heightInChars*widthInChars);
97                         LEAVE('i', 0);
98                         return 0;
99                 }
100                 if(y >= heightInChars) {
101                         LEAVE('i', 0);
102                         return 0;
103                 }
104                 
105                 // Clip to screen size
106                 if( (int)Offset + (int)Length > heightInChars*widthInChars ) {
107                         Log_Debug("Video", "%i + %i > %i*%i (%i)",
108                                 (int)Offset, (int)Length, heightInChars, widthInChars, heightInChars*widthInChars);
109                         Length = heightInChars*widthInChars - Offset;
110                         Log_Notice("Video", "Clipping write size to %i characters", (int)Length);
111                 }
112                 
113 //              Log_Debug("Video", "(%i,%i) %i chars", x, y, (int)Length);
114                 
115                 // Print characters
116                 for( i = 0; i < (int)Length; i++ )
117                 {
118                         if( chars->Ch )
119                         {
120 //                              Log_Debug("Video", "Render Char 0x%x in 0x%03x:%03x",
121 //                                      chars->Ch, chars->FGCol, chars->BGCol);
122                                 memset(tmpBuf, 0xFF, giVT_CharWidth*giVT_CharHeight*4);
123                                 VT_Font_Render(
124                                         chars->Ch,
125                                         tmpBuf, 32, giVT_CharWidth*4,
126                                         VT_Colour12to24(chars->BGCol),
127                                         VT_Colour12to24(chars->FGCol)
128                                         );
129                                 UI_BlitBitmap(
130                                         x*giVT_CharWidth, y*giVT_CharHeight,
131                                         giVT_CharWidth, giVT_CharHeight,
132                                         tmpBuf
133                                         );
134                         }
135                         else
136                         {
137                                 UI_FillBitmap(
138                                         x*giVT_CharWidth, y*giVT_CharHeight,
139                                         giVT_CharWidth, giVT_CharHeight,
140                                         VT_Colour12to24(chars->BGCol)
141                                         );
142                         }
143                         
144                         chars ++;
145                         x ++;
146                         if( x >= widthInChars ) {
147                                 x = 0;
148                                 y ++;
149                                 //dest += pitch*giVT_CharHeight;
150                         }
151                 }
152                 Length *= sizeof(tVT_Char);
153                 }
154                 break;
155         
156         case VIDEO_BUFFMT_FRAMEBUFFER:
157                 {
158                  int    startX, startY;
159                 
160                 if(giUI_Pitch*giUI_Height < Offset+Length)
161                 {
162                         Log_Warning("Video", "Video_Write - Framebuffer Overflow");
163                         LEAVE('i', 0);
164                         return 0;
165                 }
166                 
167                 LOG("buffer = %p", Buffer);
168                 
169                 startX = Offset % giUI_Width;
170                 startY = Offset / giUI_Width;
171                 
172                 if( Length + startX < giUI_Width )
173                 {
174                         // Single line
175                         UI_BlitBitmap(
176                                 startX, startY,
177                                 Length, 1,
178                                 Buffer);
179                 }
180                 else
181                 {
182                         // First scanline (partial or full)
183                         UI_BlitBitmap(
184                                 startX, startY,
185                                 giUI_Width - startX, 1,
186                                 Buffer);
187                         
188                         Length -= giUI_Width - startX;
189                         Buffer += giUI_Width - startX;
190                         
191                         // Middle Scanlines
192                         for( i = 0; i < Length / giUI_Height; i ++ )
193                         {
194                                 UI_BlitBitmap(
195                                         0, startY + i,
196                                         giUI_Width, 1,
197                                         Buffer);
198                                 Buffer += giUI_Width;
199                         }
200                         
201                         // Final scanline (partial)
202                         if( Length % giUI_Height )
203                         {
204                                 UI_BlitBitmap(
205                                         0, startY + i,
206                                         Length % giUI_Height, 1,
207                                         Buffer);
208                         }
209                 }
210                 }
211                 break;
212         
213         case VIDEO_BUFFMT_2DSTREAM:
214                 Length = DrvUtil_Video_2DStream(
215                         NULL,   // Single framebuffer, so Ent is unused
216                         Buffer, Length, &gVideo_2DFunctions, sizeof(gVideo_2DFunctions)
217                         );
218                 break;
219         
220         default:
221                 LEAVE('i', -1);
222                 return -1;
223         }
224         
225         // Tell the UI to blit
226         UI_Redraw();
227         
228         LEAVE('X', Length);
229         return Length;
230 }
231
232 const char * csaVIDEO_IOCTLS[] = {DRV_IOCTLNAMES, DRV_VIDEO_IOCTLNAMES, NULL};
233 /**
234  * \brief Handle messages to the device
235  */
236 int Video_IOCtl(tVFS_Node *Node, int ID, void *Data)
237 {
238          int    ret;
239         tVideo_IOCtl_Mode       *mode = Data;
240         switch(ID)
241         {
242         BASE_IOCTLS(DRV_TYPE_VIDEO, "NativeVideo", VERSION, csaVIDEO_IOCTLS);
243         
244         // Video mode control
245         // - We cheat, and only have one mode
246         case VIDEO_IOCTL_GETSETMODE:
247                 return 0;
248         case VIDEO_IOCTL_FINDMODE:
249         case VIDEO_IOCTL_MODEINFO:
250                 mode->id = 0;
251                 mode->width = giUI_Width;
252                 mode->height = giUI_Height;
253                 mode->bpp = 32;
254                 mode->flags = 0;
255                 return 0;
256         
257         // Buffer mode
258         case VIDEO_IOCTL_SETBUFFORMAT:
259                 ret = giVideo_CurrentFormat;
260                 if(Data) {
261                         giVideo_CurrentFormat = *(int*)Data;
262                 }
263                 return ret;
264         
265         #if 0
266         case VIDEO_IOCTL_SETCURSOR:     // Set cursor position
267                 #if !BLINKING_CURSOR
268                 if(giVideo_CursorX > 0)
269                         Video_FlipCursor(Node);
270                 #endif
271                 giVideo_CursorX = ((tVideo_IOCtl_Pos*)Data)->x;
272                 giVideo_CursorY = ((tVideo_IOCtl_Pos*)Data)->y;
273                 if(     giVideo_CursorX < 0 || giVesaCursorY < 0
274                 ||      giVideo_CursorX >= gpVesaCurMode->width/giVT_CharWidth
275                 ||      giVideo_CursorY >= gpVesaCurMode->height/giVT_CharHeight)
276                 {
277                         #if BLINKING_CURSOR
278                         if(giVesaCursorTimer != -1) {
279                                 Time_RemoveTimer(giVesaCursorTimer);
280                                 giVesaCursorTimer = -1;
281                         }
282                         #endif
283                         giVesaCursorX = -1;
284                         giVesaCursorY = -1;
285                 }
286                 else {
287                         #if BLINKING_CURSOR
288                 //      Log_Debug("VESA", "Updating timer %i?", giVesaCursorTimer);
289                         if(giVesaCursorTimer == -1)
290                                 giVesaCursorTimer = Time_CreateTimer(VESA_CURSOR_PERIOD, Vesa_FlipCursor, Node);
291                         #else
292                         Vesa_FlipCursor(Node);
293                         #endif
294                 }
295                 //Log_Debug("VESA", "Cursor position (%i,%i) Timer %i", giVesaCursorX, giVesaCursorY, giVesaCursorTimer);
296                 return 0;
297         #endif
298         }
299         return 0;
300 }
301
302 // --- 2D Acceleration Functions --
303 void Video_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour)
304 {
305         UI_FillBitmap(X, Y, W, H, Colour);
306 }
307
308 void Video_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H)
309 {
310         UI_BlitFramebuffer(DstX, DstY, SrcX, SrcY, W, H);
311 }

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