AcessNative - Bugfixing
[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 <tpl_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         return 0;
43 }
44
45 Uint32 UI_GetAcessKeyFromSDL(SDLKey Sym, Uint16 Unicode)
46 {
47         Uint8   *keystate = SDL_GetKeyState(NULL);
48          int    shiftState = 0;
49         Uint32  ret = 0;
50         
51         if( keystate[SDLK_RSHIFT] || keystate[SDLK_LSHIFT] )
52                 shiftState = 1;
53         
54         // Fast return
55         if( gUI_Keymap[shiftState][Sym] )
56                 return gUI_Keymap[shiftState][Sym];
57         
58         // How nice of you, a unicode value
59         if( Unicode )
60         {
61                 ret = Unicode;
62         }
63         // Ok, we need to do work :(
64         else
65         {
66                 switch(Sym)
67                 {
68                 case SDLK_UP:   ret = KEY_UP;   break;
69                 case SDLK_DOWN: ret = KEY_DOWN; break;
70                 case SDLK_LEFT: ret = KEY_LEFT; break;
71                 case SDLK_RIGHT:ret = KEY_RIGHT;break;
72                 case SDLK_CAPSLOCK:     ret = KEY_CAPSLOCK;     break;
73                 case SDLK_F1:   ret = KEY_F1;   break;
74                 case SDLK_F2:   ret = KEY_F2;   break;
75                 case SDLK_F3:   ret = KEY_F3;   break;
76                 case SDLK_F4:   ret = KEY_F4;   break;
77                 case SDLK_F5:   ret = KEY_F5;   break;
78                 case SDLK_F6:   ret = KEY_F6;   break;
79                 case SDLK_F7:   ret = KEY_F7;   break;
80                 case SDLK_F8:   ret = KEY_F8;   break;
81                 case SDLK_F9:   ret = KEY_F9;   break;
82                 case SDLK_F10:  ret = KEY_F10;  break;
83                 case SDLK_F11:  ret = KEY_F11;  break;
84                 case SDLK_F12:  ret = KEY_F12;  break;
85                 default:
86                         printf("Unhandled key code %i\n", Sym);
87                         break;
88                 }
89         }
90         
91         gUI_Keymap[shiftState][Sym] = ret;
92         return ret;
93 }
94
95 int UI_MainThread(void *Unused)
96 {
97         SDL_Event       event;
98         Uint32  acess_sym;
99         
100         SDL_Init(SDL_INIT_VIDEO);
101         
102         // Set up video
103         gScreen = SDL_SetVideoMode(giUI_Width, giUI_Height, 32, 0);
104         if( !gScreen ) {
105                 fprintf(stderr, "Couldn't set %ix%i video mode: %s\n", giUI_Width, giUI_Height, SDL_GetError());
106                 SDL_Quit();
107                 exit(2);
108         }
109         SDL_WM_SetCaption("Acess2", "Acess2");
110         
111         giUI_Width = gScreen->w;
112         giUI_Height = gScreen->h;
113         giUI_Pitch = gScreen->pitch;
114         
115         SDL_EnableUNICODE(1);
116         
117         for( ;; )
118         {
119                 while(SDL_PollEvent(&event))
120                 {
121                         switch(event.type)
122                         {
123                         case SDL_QUIT:
124                                 AcessNative_Exit();
125                                 return 0;
126                                 
127                         case SDL_KEYDOWN:
128                                 acess_sym = UI_GetAcessKeyFromSDL(event.key.keysym.sym,
129                                         event.key.keysym.unicode);
130                                 
131                                 if( gUI_KeyboardCallback )
132                                         gUI_KeyboardCallback(acess_sym);
133                                 break;
134                         
135                         case SDL_KEYUP:
136                                 acess_sym = UI_GetAcessKeyFromSDL(event.key.keysym.sym,
137                                         event.key.keysym.unicode);
138                                 
139                                 if( gUI_KeyboardCallback )
140                                         gUI_KeyboardCallback(0x80000000|acess_sym);
141                                 break;
142                         
143                         default:
144                                 break;
145                         }
146                 }
147         }
148 }
149
150 void UI_BlitBitmap(int DstX, int DstY, int SrcW, int SrcH, Uint32 *Bitmap)
151 {
152         SDL_Surface     *tmp;
153         SDL_Rect        dstRect;
154         
155         tmp = SDL_CreateRGBSurfaceFrom(Bitmap, SrcW, SrcH, 32, SrcW*4,
156                 0xFF0000, 0x00FF00, 0x0000FF, 0xFF000000);
157         SDL_SetAlpha(tmp, 0, SDL_ALPHA_OPAQUE);
158         
159         dstRect.x = DstX;       dstRect.y = DstY;
160         
161         SDL_BlitSurface(tmp, NULL, gScreen, &dstRect);
162         
163         SDL_FreeSurface(tmp);
164 }
165
166 void UI_BlitFramebuffer(int DstX, int DstY, int SrcX, int SrcY, int W, int H)
167 {
168         SDL_Rect        srcRect;
169         SDL_Rect        dstRect;
170         
171         srcRect.x = SrcX;       srcRect.y = SrcY;
172         srcRect.w = W;  srcRect.h = H;
173         dstRect.x = DstX;       dstRect.y = DstY;
174         
175         SDL_BlitSurface(gScreen, &srcRect, gScreen, &dstRect); 
176 }
177
178 void UI_FillBitmap(int X, int Y, int W, int H, Uint32 Value)
179 {
180         SDL_Rect        dstRect;
181         
182         dstRect.x = X;  dstRect.y = Y;
183         dstRect.w = W;  dstRect.h = H;
184         
185         SDL_FillRect(gScreen, &dstRect, Value);
186 }
187
188 void UI_Redraw(void)
189 {
190         // No-Op, we're not using double buffering
191 }

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