shared memory: TODO fix client bug
[winnie] / libwinnie / src / sdl / gfx.cc
1 /*
2 winnie - an experimental window system
3
4 Copyright (C) 2013 Eleni Maria Stea
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
20 */
21
22 #ifdef WINNIE_SDL
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <SDL/SDL.h>
26
27 #include "gfx.h"
28 #include "shalloc.h"
29 #include "winnie.h"
30
31 static SDL_Surface *fbsurf;
32
33 struct Graphics {
34         Rect screen_rect;
35         Rect clipping_rect;
36         int color_depth; // bits per pixel
37         Pixmap *pixmap;
38 };
39
40 static Graphics *gfx;
41
42 bool init_gfx()
43 {
44         if(SDL_Init(SDL_INIT_VIDEO) == -1) {
45                 fprintf(stderr, "failed to initialize SDL\n");
46                 return false;
47         }
48
49         if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
50                 return false;
51         }
52
53         get_subsys()->graphics_offset = (int)((char*)gfx - (char*)get_pool());
54
55         Rect scr_rect(0, 0, 1024, 768);
56         gfx->screen_rect = scr_rect;
57         gfx->color_depth = 32;
58
59         if(!(fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) {
60                 fprintf(stderr, "Failed to set video mode\n");
61                 return false;
62         }
63         SDL_ShowCursor(0);
64
65         if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
66                 fprintf(stderr, "Failed to allocate pixmap.\n");
67                 return false;
68         }
69
70         gfx->pixmap->width = gfx->screen_rect.width;
71         gfx->pixmap->height = gfx->screen_rect.height;
72
73         int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8;
74         if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) {
75                 fprintf(stderr, "failed to allocate the pixmap framebuffer.\n");
76                 return false;
77         }
78
79         set_clipping_rect(gfx->screen_rect);
80
81         return true;
82 }
83
84 void destroy_gfx()
85 {
86         sh_free(gfx->pixmap->pixels);
87         gfx->pixmap->pixels = 0;
88         sh_free(gfx->pixmap);
89         sh_free(gfx);
90         SDL_Quit();
91 }
92
93 bool client_open_gfx(void *smem_start, int offset)
94 {
95         gfx = (Graphics*)((unsigned char*)smem_start + offset);
96         return true;
97 }
98
99 void client_close_gfx()
100 {
101 }
102
103 unsigned char *get_framebuffer()
104 {
105         return gfx->pixmap->pixels;
106 }
107
108 Pixmap *get_framebuffer_pixmap()
109 {
110         return gfx->pixmap;
111 }
112
113 Rect get_screen_size()
114 {
115         return gfx->screen_rect;
116 }
117
118 int get_color_depth()
119 {
120         return gfx->color_depth;
121 }
122
123 void set_clipping_rect(const Rect &rect)
124 {
125         gfx->clipping_rect = rect_intersection(rect, get_screen_size());
126 }
127
128 const Rect &get_clipping_rect()
129 {
130         return gfx->clipping_rect;
131 }
132
133
134 void set_cursor_visibility(bool visible)
135 {
136 }
137
138 void gfx_update(const Rect &upd_rect)
139 {
140         if(SDL_MUSTLOCK(fbsurf)) {
141                 SDL_LockSurface(fbsurf);
142         }
143
144         Rect rect = rect_intersection(upd_rect, gfx->screen_rect);
145
146         unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
147         unsigned char *dptr = (unsigned char*)fbsurf->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
148
149         for(int i=0; i<rect.height; i++) {
150                 memcpy(dptr, sptr, rect.width * 4);
151                 sptr += gfx->screen_rect.width * 4;
152                 dptr += gfx->screen_rect.width * 4;
153         }
154
155         if(SDL_MUSTLOCK(fbsurf)) {
156                 SDL_UnlockSurface(fbsurf);
157         }
158         SDL_UpdateRect(fbsurf, rect.x, rect.y, rect.width, rect.height);
159 }
160
161 void wait_vsync()
162 {
163 }
164
165 void get_rgb_order(int *r, int *g, int *b)
166 {
167         *r = fbsurf->format->Rshift / 8;
168         *g = fbsurf->format->Gshift / 8;
169         *b = fbsurf->format->Bshift / 8;
170 }
171
172 #endif // WINNIE_SDL