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

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