AcessNative - Fixing build
[tpg/acess2.git] / AcessNative / acesskernel_src / ui_sdl.c
1 /*
2  * Acess2 Native Kernel
3  * 
4  * SDL User Interface
5  */
6 #include <SDL/SDL.h>
7 #define const
8 #include "ui.h"
9 #undef const
10 #include <api_drv_keyboard.h>
11
12 // === IMPORTS ===
13 extern void     AcessNative_Exit(void);
14
15 // === PROTOTYPES ===
16  int    UI_Initialise(int MaxWidth, int MaxHeight);
17  int    UI_MainThread(void *Unused);
18 void    UI_BlitBitmap(int DstX, int DstY, int SrcW, int SrcH, Uint32 *Bitmap);
19 void    UI_BlitFramebuffer(int DstX, int DstY, int SrcX, int SrcY, int W, int H);
20 void    UI_FillBitmap(int X, int Y, int W, int H, Uint32 Value);
21 void    UI_Redraw(void);
22
23 // === GLOBALS ===
24 SDL_Surface     *gScreen;
25 SDL_Thread      *gInputThread;
26  int    giUI_Width = 0;
27  int    giUI_Height = 0;
28  int    giUI_Pitch = 0;
29 tUI_KeybardCallback     gUI_KeyboardCallback;
30 Uint32  gUI_Keymap[2][SDLK_LAST];       // Upper/Lower case
31
32 // === FUNCTIONS ===
33 int UI_Initialise(int MaxWidth, int MaxHeight)
34 {       
35         // Changed when the video mode is set
36         giUI_Width = MaxWidth;
37         giUI_Height = MaxHeight;
38         
39         // Start main thread
40         gInputThread = SDL_CreateThread(UI_MainThread, NULL);
41         
42         while(gScreen == NULL)
43                 SDL_Delay(10);
44         
45         return 0;
46 }
47
48 Uint32 UI_GetAcessKeyFromSDL(SDLKey Sym, Uint16 Unicode)
49 {
50         Uint8   *keystate = SDL_GetKeyState(NULL);
51          int    shiftState = 0;
52         Uint32  ret = 0;
53         
54         if( keystate[SDLK_RSHIFT] || keystate[SDLK_LSHIFT] )
55                 shiftState = 1;
56         
57         // Fast return
58         if( gUI_Keymap[shiftState][Sym] )
59                 return gUI_Keymap[shiftState][Sym];
60
61         // Enter key on acess returns \n, but SDL returns \r
62         if( Sym == SDLK_RETURN )
63                 Unicode = '\n';
64         
65         // How nice of you, a unicode value
66         if( Unicode )
67         {
68                 ret = Unicode;
69         }
70         // Ok, we need to do work :(
71         else
72         {
73                 switch(Sym)
74                 {
75                 case SDLK_UP:   ret = KEY_UP;   break;
76                 case SDLK_DOWN: ret = KEY_DOWN; break;
77                 case SDLK_LEFT: ret = KEY_LEFT; break;
78                 case SDLK_RIGHT:ret = KEY_RIGHT;break;
79                 case SDLK_CAPSLOCK:     ret = KEY_CAPSLOCK;     break;
80                 case SDLK_F1:   ret = KEY_F1;   break;
81                 case SDLK_F2:   ret = KEY_F2;   break;
82                 case SDLK_F3:   ret = KEY_F3;   break;
83                 case SDLK_F4:   ret = KEY_F4;   break;
84                 case SDLK_F5:   ret = KEY_F5;   break;
85                 case SDLK_F6:   ret = KEY_F6;   break;
86                 case SDLK_F7:   ret = KEY_F7;   break;
87                 case SDLK_F8:   ret = KEY_F8;   break;
88                 case SDLK_F9:   ret = KEY_F9;   break;
89                 case SDLK_F10:  ret = KEY_F10;  break;
90                 case SDLK_F11:  ret = KEY_F11;  break;
91                 case SDLK_F12:  ret = KEY_F12;  break;
92                 case SDLK_RETURN:       ret = '\n';     break;
93                 default:
94                         printf("Unhandled key code %i\n", Sym);
95                         break;
96                 }
97         }
98         
99         gUI_Keymap[shiftState][Sym] = ret;
100         return ret;
101 }
102
103 int UI_MainThread(void *Unused)
104 {
105         SDL_Event       event;
106         Uint32  acess_sym;
107         
108         SDL_Init(SDL_INIT_VIDEO);
109         
110         // Set up video
111         gScreen = SDL_SetVideoMode(giUI_Width, giUI_Height, 32, 0);
112         if( !gScreen ) {
113                 fprintf(stderr, "Couldn't set %ix%i video mode: %s\n", giUI_Width, giUI_Height, SDL_GetError());
114                 SDL_Quit();
115                 exit(2);
116         }
117         SDL_WM_SetCaption("Acess2", "Acess2");
118         
119         giUI_Width = gScreen->w;
120         giUI_Height = gScreen->h;
121         giUI_Pitch = gScreen->pitch;
122         
123         SDL_EnableUNICODE(1);
124         
125         for( ;; )
126         {
127                 while(SDL_PollEvent(&event))
128                 {
129                         switch(event.type)
130                         {
131                         case SDL_QUIT:
132                                 AcessNative_Exit();
133                                 return 0;
134                                 
135                         case SDL_KEYDOWN:
136                                 acess_sym = UI_GetAcessKeyFromSDL(event.key.keysym.sym,
137                                         event.key.keysym.unicode);
138                                 
139                                 if( gUI_KeyboardCallback )
140                                         gUI_KeyboardCallback(acess_sym);
141                                 break;
142                         
143                         case SDL_KEYUP:
144                                 acess_sym = UI_GetAcessKeyFromSDL(event.key.keysym.sym,
145                                         event.key.keysym.unicode);
146                                 
147                                 if( gUI_KeyboardCallback )
148                                         gUI_KeyboardCallback(0x80000000|acess_sym);
149                                 break;
150                         
151                         default:
152                                 break;
153                         }
154                 }
155         }
156 }
157
158 void UI_BlitBitmap(int DstX, int DstY, int SrcW, int SrcH, Uint32 *Bitmap)
159 {
160         SDL_Surface     *tmp;
161         SDL_Rect        dstRect;
162         
163 //      printf("UI_BlitBitmap: Blit to (%i,%i) from %p (%ix%i 32bpp bitmap)\n",
164 //              DstX, DstY, Bitmap, SrcW, SrcH);
165         
166         tmp = SDL_CreateRGBSurfaceFrom(Bitmap, SrcW, SrcH, 32, SrcW*4,
167                 0xFF0000, 0x00FF00, 0x0000FF, 0xFF000000);
168         SDL_SetAlpha(tmp, 0, SDL_ALPHA_OPAQUE);
169         
170         dstRect.x = DstX;       dstRect.y = DstY;
171         dstRect.w = -1; dstRect.h = -1;
172         
173         SDL_BlitSurface(tmp, NULL, gScreen, &dstRect);
174         //SDL_BlitSurface(tmp, NULL, gScreen, NULL);
175         
176         SDL_FreeSurface(tmp);
177 //      SDL_Flip(gScreen);
178 }
179
180 void UI_BlitFramebuffer(int DstX, int DstY, int SrcX, int SrcY, int W, int H)
181 {
182         SDL_Rect        srcRect;
183         SDL_Rect        dstRect;
184         
185         srcRect.x = SrcX;       srcRect.y = SrcY;
186         srcRect.w = W;  srcRect.h = H;
187         dstRect.x = DstX;       dstRect.y = DstY;
188         
189         SDL_BlitSurface(gScreen, &srcRect, gScreen, &dstRect); 
190 }
191
192 void UI_FillBitmap(int X, int Y, int W, int H, Uint32 Value)
193 {
194         SDL_Rect        dstRect;
195         
196         dstRect.x = X;  dstRect.y = Y;
197         dstRect.w = W;  dstRect.h = H;
198         
199 //      printf("UI_FillBitmap: gScreen = %p\n", gScreen);
200         SDL_FillRect(gScreen, &dstRect, Value);
201 }
202
203 void UI_Redraw(void)
204 {
205         // TODO: Keep track of changed rectangle
206         SDL_UpdateRect(gScreen, 0, 0, giUI_Width, giUI_Height);
207 }

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