]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libsdl/lib/src/src/video/l4fb/SDL_l4fbvideowork.c
update
[l4.git] / l4 / pkg / libsdl / lib / src / src / video / l4fb / SDL_l4fbvideowork.c
1 #include <string.h>
2 #include <malloc.h>
3
4 #include "SDL.h"
5 #include "SDL_error.h"
6 #include "SDL_video.h"
7 #include "SDL_mouse.h"
8 #include "SDL_sysvideo.h"
9 #include "SDL_pixels_c.h"
10 #include "SDL_events_c.h"
11
12 #include "SDL_l4fbvideo.h"
13
14 #include <stdio.h>
15
16 double L4FB_video_scale_factor __attribute__ ((weak)) = 1.0;
17
18 static void FreeAndNull(void** ptr) {
19         if (ptr) {
20                 if (*ptr) {
21                         free(*ptr);
22                         *ptr = NULL;
23                 }
24         }
25 }
26
27 SDL_Surface *L4FB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags) {
28         char* apptitle = APPTITLE;
29         char* wintitle = WINTITLE;
30
31         printf("L4FB_SetVideoMode: %dx%d@%d:\n", width, height, bpp);
32         /*
33          * is requested resolution higher than the framebuffer's one?
34          */
35
36         if( width > (int)this->hidden->vvi.width
37                 || height > (int)this->hidden->vvi.height)
38         {
39                 return (NULL);
40         }
41         
42         /*
43          * calc the offsets for current resolution the center the image 
44          */
45         
46         this->hidden->x_offset = (this->hidden->vvi.width - width ) / 2;
47         this->hidden->y_offset = (this->hidden->vvi.height - height) / 2;
48
49         // make sure to have correct scale
50         if (L4FB_video_scale_factor<=0) L4FB_video_scale_factor = 1.0;
51         
52         // reject unsupported bpp
53         if (bpp != 16) return NULL;
54         // reject unsupported flags
55         if (flags & SDL_OPENGL) return NULL;
56
57         /* Allocate the new pixel format for the screen */
58         if ( ! SDL_ReallocFormat(current, bpp, 0, 0, 0, 0) ) {
59                 SDL_SetError("Couldn't allocate new pixel format for requested mode");
60                 return(NULL);
61         }
62
63         // if we were double buffered clear shadow buffer
64         if (current->flags & SDL_DOUBLEBUF) {
65                 FreeAndNull(current->pixels);
66         }
67
68         // init new window
69         if (this->wm_title) wintitle = this->wm_title;
70         if (this->wm_icon)  apptitle = this->wm_icon;
71
72
73
74 //      L4FB_SetCaption(this, wintitle, apptitle);
75
76         if (l4_is_invalid_cap(this->hidden->ev_ds) || !this->hidden->ev_ds)
77                 L4FB_InstallEventHandler(this);
78
79         /* Set up the new mode framebuffer */
80         current->w = width;
81         current->h = height;
82         current->pitch = SDL_CalculatePitch(current);
83         this->hidden->pitch =  current->pitch;
84         current->flags =  SDL_PREALLOC | SDL_ASYNCBLIT;
85
86
87 //      if (current->pixels)
88 //      {
89 //              /* TODO: ERROR HANDLING */
90 //      }
91
92 //      memset(current->pixels, 0, current->h*current->pitch);
93
94
95         /* since we want to center image in fb we allway have to use doublebuf... */
96
97         this->hidden->fb_start = this->hidden->fbmem_vaddr + this->hidden->vvi.buffer_offset;
98
99         current->pixels = malloc(current->h*current->pitch);
100
101         if (current->pixels) {
102                 // success!
103                 printf("allocated shadow buffer\n");
104                 this->hidden->pixels = current->pixels;
105                 memset(current->pixels, 0, current->h*current->pitch);
106         } else
107                 return NULL;
108
109
110         if (flags&SDL_DOUBLEBUF) {
111
112                 current->flags |= SDL_DOUBLEBUF|SDL_HWSURFACE;
113                 // try to alloc shadow buffer
114         }
115  
116         /* We're done */
117         return(current);
118 }
119
120 int L4FB_ToggleFullScreen(_THIS, int on) {
121         (void)this;
122         /* TODO: NOT IMPLENETED */
123
124         // remember window size for fullscreen toggle
125         
126         static int is_fs = 0;
127         
128
129         if((on)&&(!is_fs))
130         {
131                 /* TODO: set fs */
132         }
133         else if((!on)&&(is_fs))
134         {
135                 /* TODO: unset FS */
136         }
137         
138         return(0);
139 }
140
141 int L4FB_AllocHWSurface(_THIS, SDL_Surface *surface) {
142         (void)this;
143         (void)surface;
144         //INFO(LOG_Enter("");)
145         return(-1);
146 }
147 void L4FB_FreeHWSurface(_THIS, SDL_Surface *surface) {
148         (void)this;
149         (void)surface;
150         //INFO(LOG_Enter("");)
151         return;
152 }
153
154 int L4FB_LockHWSurface(_THIS, SDL_Surface *surface) {
155         (void)this;
156         (void)surface;
157 //      INFO(LOG_Enter("");)
158         return(0);
159 }
160 void L4FB_UnlockHWSurface(_THIS, SDL_Surface *surface) {
161         (void)this;
162         (void)surface;
163 //      INFO(LOG_Enter("");)
164         return;
165 }
166
167 void L4FB_UpdateRects(_THIS, int numrects, SDL_Rect *rects) {
168         int i;
169
170         l4re_video_view_info_t * vvi = &this->hidden->vvi;
171
172         for (i=0; i<numrects; i++) {
173                 int j;
174                 for (j=0; j<rects[i].h; j++) {
175                         void *dest = this->hidden->fb_start
176                                      + vvi->bytes_per_line
177                                                    *(this->hidden->y_offset+rects[i].y+j)
178                                                 + this->hidden->x_offset*vvi->pixel_info.bytes_per_pixel
179                                                 + rects[i].x*vvi->pixel_info.bytes_per_pixel;
180
181                         void *src = this->hidden->pixels
182                                 + (this->hidden->pitch
183                                                * (j+rects[i].y))
184                                 + rects[i].x * vvi->pixel_info.bytes_per_pixel;
185
186                         unsigned len = rects[i].w * vvi->pixel_info.bytes_per_pixel;
187                         memcpy(dest,src,len);
188                 }
189
190                 l4re_util_video_goos_fb_refresh(&this->hidden->goosfb, rects[i].x+this->hidden->x_offset, rects[i].y+this->hidden->y_offset, rects[i].w, rects[i].h);
191         }
192 }
193
194 int L4FB_FlipHWSurface(_THIS, SDL_Surface *surface) {
195         l4re_video_view_info_t * vvi = &this->hidden->vvi;
196
197         // copy buffer and update screen
198         int i;
199         /*
200          * we have to do it linewise because real screen size and surface size my differ
201          */
202         for(i=0; i<surface->h; i++) {
203                 /* calc dest addr */
204                 void *dest = this->hidden->fb_start
205                              + vvi->bytes_per_line
206                                            * (this->hidden->y_offset+i)
207                                          + this->hidden->x_offset * vvi->pixel_info.bytes_per_pixel; 
208
209                 memcpy(dest, surface->pixels+(surface->pitch * i), surface->pitch);
210         }
211
212         l4re_util_video_goos_fb_refresh(&this->hidden->goosfb, this->hidden->x_offset, this->hidden->y_offset, surface->w, surface->h);
213 //      printf("refresh\n");
214         return 0;
215 }
216
217 int L4FB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) {
218         (void)this;
219         (void)firstcolor;
220         (void)ncolors;
221         (void)colors;
222         /* return success */
223         return(1);
224 }
225
226 void L4FB_WarpWMCursor(_THIS, Uint16 x, Uint16 y) {
227         (void)this;
228         (void)x;
229         (void)y;
230         /* TODO: not implemented */
231 }
232
233 SDL_GrabMode L4FB_GrabInput(_THIS, SDL_GrabMode mode) {
234         (void)this;
235                 //(mode==SDL_GRAB_OFF?"no":"yes"));
236                 /*TODO:  not implemented */
237         return mode;
238 }