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

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