#include <errno.h>
#include <limits.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
-static unsigned char *framebuffer;
-static int dev_fd = -1;
+struct Graphics {
+ unsigned char *framebuffer;
+ int dev_fd;
+ Rect screen_rect;
+ Rect clipping_rect;
+ int color_depth;
+ Pixmap *pixmap;
+};
-static Rect screen_rect;
-
-static int color_depth; // bits per pixel
-
-static Pixmap *pixmap;
+static Graphics *gfx;
bool init_gfx()
{
- if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
+ gfx->dev_fd = -1;
+
+ if((gfx->dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
return false;
}
fb_var_screeninfo sinfo;
- if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
- close(dev_fd);
- dev_fd = -1;
+ if(ioctl(gfx->dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
+ close(gfx->dev_fd);
+ gfx->dev_fd = -1;
fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
return false;
}
+ if(!(gfx = (Graphics*)malloc(sizeof *gfx))) {
+ return false;
+ }
+
printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
- screen_rect.x = screen_rect.y = 0;
- screen_rect.width = sinfo.xres_virtual;
- screen_rect.height = sinfo.yres_virtual;
- color_depth = sinfo.bits_per_pixel;
+ gfx->screen_rect.x = gfx->screen_rect.y = 0;
+ gfx->screen_rect.width = sinfo.xres_virtual;
+ gfx->screen_rect.height = sinfo.yres_virtual;
+ gfx->color_depth = sinfo.bits_per_pixel;
- set_clipping_rect(screen_rect);
+ set_clipping_rect(gfx->screen_rect);
- int sz = FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth);
- framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
+ int sz = FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth);
+ gfx->framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, gfx->dev_fd, 0);
- if(framebuffer == (void*)-1) {
- close(dev_fd);
- dev_fd = -1;
+ if(gfx->framebuffer == (void*)-1) {
+ close(gfx->dev_fd);
+ gfx->dev_fd = -1;
fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
return false;
}
// TODO: uncomment when I find how to use intelfb instead of i915 GRRRR.-
fb_vblank vblank;
- if(ioctl(dev_fd, FBIOGET_VBLANK, &vblank) == -1) {
+ if(ioctl(gfx->dev_fd, FBIOGET_VBLANK, &vblank) == -1) {
// fprintf(stderr, "FBIOGET_VBLANK error: %s\n", strerror(errno));
}
/*
}
*/
- pixmap = new Pixmap;
- pixmap->width = screen_rect.width;
- pixmap->height = screen_rect.height;
- pixmap->pixels = framebuffer;
+ gfx->pixmap = new Pixmap;
+ gfx->pixmap->width = gfx->screen_rect.width;
+ gfx->pixmap->height = gfx->screen_rect.height;
+ gfx->pixmap->pixels = gfx->framebuffer;
return true;
}
{
clear_screen(0, 0, 0);
- if(dev_fd != -1) {
- close(dev_fd);
+ if(gfx->dev_fd != -1) {
+ close(gfx->dev_fd);
}
- dev_fd = -1;
+ gfx->dev_fd = -1;
- munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth));
- framebuffer = 0;
+ munmap(gfx->framebuffer, FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth));
+ gfx->framebuffer = 0;
- pixmap->pixels = 0;
+ gfx->pixmap->pixels = 0;
+
+ free(gfx);
}
unsigned char *get_framebuffer()
{
- return framebuffer;
+ return gfx->framebuffer;
}
Pixmap *get_framebuffer_pixmap()
{
- return pixmap;
+ return gfx->pixmap;
}
Rect get_screen_size()
{
- return screen_rect;
+ return gfx->screen_rect;
}
int get_color_depth()
{
- return color_depth;
+ return gfx->color_depth;
+}
+
+void set_clipping_rect(const Rect &rect)
+{
+ gfx->clipping_rect = rect_intersection(rect, get_screen_size());
}
+const Rect &get_clipping_rect()
+{
+ return gfx->clipping_rect;
+}
void set_cursor_visibility(bool visible)
{
fb_cursor curs;
curs.enable = visible ? 1 : 0;
- if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
+ if(ioctl(gfx->dev_fd, FBIO_CURSOR, &curs) == -1) {
fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
}
}
void wait_vsync()
{
unsigned long arg = 0;
- if(ioctl(dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
+ if(ioctl(gfx->dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
// printf("ioctl error %s\n", strerror(errno));
}
}
#include "geom.h"
#include "gfx.h"
-static Rect clipping_rect;
-
-void set_clipping_rect(const Rect &rect)
-{
- clipping_rect = rect_intersection(rect, get_screen_size());
-}
-
-const Rect &get_clipping_rect()
-{
- return clipping_rect;
-}
-
void clear_screen(int r, int g, int b)
{
Rect screen_rect = get_screen_size();
{
Rect drect = rect;
Rect screen_rect = get_screen_size();
+ Rect clipping_rect = get_clipping_rect();
if(drect.x < clipping_rect.x) {
drect.width -= clipping_rect.x - drect.x;
break;
case SDL_QUIT:
exit(0);
+ default:
+ break;
}
}
#include <SDL/SDL.h>
#include "gfx.h"
-static SDL_Surface *fbsurf;
+struct Graphics {
+ SDL_Surface *fbsurf;
+ Rect screen_rect;
+ Rect clipping_rect;
+ int color_depth; // bits per pixel
+ Pixmap *pixmap;
+};
-static Rect screen_rect = {0, 0, 1024, 768};
-static Rect clipping_rect;
-
-static int color_depth = 32; // bits per pixel
-
-static Pixmap *pixmap;
+static Graphics *gfx;
bool init_gfx()
{
return false;
}
- if(!(fbsurf = SDL_SetVideoMode(screen_rect.width, screen_rect.height, color_depth, 0))) {
+ if(!(gfx = (Graphics*)malloc(sizeof *gfx))) {
+ return false;
+ }
+
+ Rect scr_rect = {0, 0, 1024, 768};
+ gfx->screen_rect = scr_rect;
+ gfx->color_depth = 32;
+
+ if(!(gfx->fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) {
fprintf(stderr, "failed to set video mode\n");
return false;
}
SDL_ShowCursor(0);
- pixmap = new Pixmap;
- pixmap->width = screen_rect.width;
- pixmap->height = screen_rect.height;
- pixmap->pixels = (unsigned char*)fbsurf->pixels;
+ gfx->pixmap = new Pixmap;
+ gfx->pixmap->width = gfx->screen_rect.width;
+ gfx->pixmap->height = gfx->screen_rect.height;
+ gfx->pixmap->pixels = (unsigned char*)gfx->fbsurf->pixels;
- set_clipping_rect(screen_rect);
+ set_clipping_rect(gfx->screen_rect);
return true;
}
void destroy_gfx()
{
- pixmap->pixels = 0;
- delete pixmap;
+ gfx->pixmap->pixels = 0;
+ delete gfx->pixmap;
+ free(gfx);
SDL_Quit();
}
unsigned char *get_framebuffer()
{
- return (unsigned char*)fbsurf->pixels;
+ return (unsigned char*)gfx->fbsurf->pixels;
}
Pixmap *get_framebuffer_pixmap()
{
- return pixmap;
+ return gfx->pixmap;
}
Rect get_screen_size()
{
- return screen_rect;
+ return gfx->screen_rect;
}
int get_color_depth()
{
- return color_depth;
+ return gfx->color_depth;
}
/*void set_clipping_rect(const Rect &rect)
{
- clipping_rect = rect_intersection(rect, screen_rect);
+ gfx->clipping_rect = rect_intersection(rect, gfx->screen_rect);
SDL_Rect sdl_rect;
- sdl_rect.x = clipping_rect.x;
- sdl_rect.y = clipping_rect.y;
- sdl_rect.w = clipping_rect.width;
- sdl_rect.h = clipping_rect.height;
+ sdl_rect.x = gfx->clipping_rect.x;
+ sdl_rect.y = gfx->clipping_rect.y;
+ sdl_rect.w = gfx->clipping_recvoid fill_rect(const Rect &rect, int r, int g, int b)
+{
+ Rect drect = rect;
+ Rect screen_rect = get_screen_size();
+
+ if(drect.x < clipping_rect.x) {
+ drect.width -= clipping_rect.x - drect.x;
+ drect.x = clipping_rect.x;
+ }
+
+ if(drect.y < clipping_rect.y) {
+ drect.height -= clipping_rect.y - drect.y;
+ drect.y = clipping_rect.y;
+ }
+
+ if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) {
+ drect.width = clipping_rect.width + clipping_rect.x - drect.x;
+ }
- SDL_SetClipRect(fbsurf, &sdl_rect);
+ if(drect.y + drect.height >= clipping_rect.y + clipping_rect.height) {
+ drect.height = clipping_rect.height + clipping_rect.y - drect.y;
+ }
+
+ unsigned char *fb = get_framebuffer() + (drect.x + screen_rect.width * drect.y) * 4;
+ for(int i=0; i<drect.height; i++) {
+ for(int j=0; j<drect.width; j++) {
+ fb[j * 4] = b;
+ fb[j * 4 + 1] = g;
+ fb[j * 4 + 2] = r;
+ }
+ fb += screen_rect.width * 4;
+ }
+}
+t.width;
+ sdl_rect.h = gfx->clipping_rect.height;
+
+ SDL_SetClipRect(gfx->fbsurf, &sdl_rect);
}
const Rect &get_clipping_rect()
{
- return clipping_rect;
+ return gfx->clipping_rect;
}
void clear_screen(int r, int g, int b)
{
- fill_rect(screen_rect, r, g, b);
+ fill_rect(gfx->screen_rect, r, g, b);
}
void fill_rect(const Rect &rect, int r, int g, int b)
sdl_rect.w = rect.width;
sdl_rect.h = rect.height;
- SDL_FillRect(fbsurf, &sdl_rect, color);
+ SDL_FillRect(gfx->fbsurf, &sdl_rect, color);
}*/
+void set_clipping_rect(const Rect &rect)
+{
+ gfx->clipping_rect = rect_intersection(rect, get_screen_size());
+}
+
+const Rect &get_clipping_rect()
+{
+ return gfx->clipping_rect;
+}
+
+
void set_cursor_visibility(bool visible)
{
}
void gfx_update()
{
- SDL_UpdateRect(fbsurf, 0, 0, 0, 0);
+ SDL_UpdateRect(gfx->fbsurf, 0, 0, 0, 0);
}
void wait_vsync()
static int draw_glyph(Pixmap *pixmap, int x, int y, char c);
-static FT_Library ft_lib;
-static FT_Face ft_face;
+struct Text {
+ FT_Library ft_lib;
+ FT_Face ft_face;
+ int text_x, text_y;
+ int text_color[3];
+};
-static int text_x, text_y;
-static int text_color[3] = {255, 255, 255};
+Text *text;
bool init_text()
{
- if(FT_Init_FreeType(&ft_lib)) {
+ if(!(text = (Text*)malloc(sizeof *text))) {
+ return false;
+ }
+
+ if(FT_Init_FreeType(&text->ft_lib)) {
fprintf(stderr, "Failed to initialize the FreeType library!\n");
return false;
}
- if(FT_New_Face(ft_lib, FONT_PATH, 0, &ft_face)) {
+ if(FT_New_Face(text->ft_lib, FONT_PATH, 0, &text->ft_face)) {
fprintf(stderr, "Failed to load font: %s\n", FONT_PATH);
return false;
}
- if(FT_Set_Char_Size(ft_face, 0, FONT_SIZE * 64, DPI, DPI)) {
+ if(FT_Set_Char_Size(text->ft_face, 0, FONT_SIZE * 64, DPI, DPI)) {
fprintf(stderr, "Failed to set font size\n");
return false;
}
+ set_text_color(255, 255, 255);
+
return true;
}
}
while(*txt != 0) {
- text_x += draw_glyph(pixmap, text_x, text_y, *txt);
+ text->text_x += draw_glyph(pixmap, text->text_x, text->text_y, *txt);
txt++;
}
}
void set_text_position(int x, int y)
{
- text_x = x;
- text_y = y;
+ text->text_x = x;
+ text->text_y = y;
}
void set_text_color(int r, int g, int b)
{
- text_color[0] = r;
- text_color[1] = g;
- text_color[2] = b;
+ text->text_color[0] = r;
+ text->text_color[1] = g;
+ text->text_color[2] = b;
}
static int draw_glyph(Pixmap *pixmap, int x, int y, char c)
{
- if(FT_Load_Char(ft_face, c, FT_LOAD_RENDER)) {
+ if(FT_Load_Char(text->ft_face, c, FT_LOAD_RENDER)) {
return 0;
}
- x += ft_face->glyph->bitmap_left;
- y -= ft_face->glyph->bitmap_top;
+ x += text->ft_face->glyph->bitmap_left;
+ y -= text->ft_face->glyph->bitmap_top;
- FT_Bitmap *ft_bmp = &ft_face->glyph->bitmap;
+ FT_Bitmap *ft_bmp = &text->ft_face->glyph->bitmap;
unsigned char *bmp_ptr = ft_bmp->buffer;
unsigned char *pxm_ptr = pixmap->get_image() + (pixmap->get_width() * y + x) * 4;
if(bmp_ptr[j] && dest_x >= clipping_rect.x) {
int a = (int)bmp_ptr[j];
- pxm_ptr[4 * j] = (a * text_color[0] + pxm_ptr[4 * j] * (255 - a)) / 255;
- pxm_ptr[4 * j + 1] = (a * text_color[1] + pxm_ptr[4 * j + 1] * (255 - a)) / 255;
- pxm_ptr[4 * j + 2] = (a * text_color[2] + pxm_ptr[4 * j + 2] * (255 - a)) / 255;
+ pxm_ptr[4 * j] = (a * text->text_color[0] + pxm_ptr[4 * j] * (255 - a)) / 255;
+ pxm_ptr[4 * j + 1] = (a * text->text_color[1] + pxm_ptr[4 * j + 1] * (255 - a)) / 255;
+ pxm_ptr[4 * j + 2] = (a * text->text_color[2] + pxm_ptr[4 * j + 2] * (255 - a)) / 255;
}
}
}
bmp_ptr += ft_bmp->pitch;
}
- return ft_face->glyph->advance.x >> 6;
+ return text->ft_face->glyph->advance.x >> 6;
}
return false;
}
+ if(!init_window_manager()) {
+ return false;
+ }
+
if(!init_keyboard()) {
return false;
}
#include "window.h"
WindowManager *wm;
-static WindowManager wminst;
static void display(Window *win);
static void mouse(Window *win, int bn, bool pressed, int x, int y);
static void motion(Window *win, int x, int y);
+bool init_window_manager()
+{
+ if(!(wm = new WindowManager)) {
+ return false;
+ }
+
+ return true;
+}
+
+void destroy_window_manager()
+{
+ delete wm;
+}
+
void WindowManager::create_frame(Window *win)
{
Window *frame = new Window;
root_win->move(0, 0);
root_win->set_managed(false);
+ grab_win = 0;
focused_win = 0;
bg_color[0] = 210;
class Window;
+bool init_window_manager();
+void destroy_window_manager();
+
class WindowManager {
private:
std::list<Window*> windows;