diff options
| author | Eleni Maria Stea <elene.mst@gmail.com> | 2013-02-23 01:22:10 +0200 |
|---|---|---|
| committer | Eleni Maria Stea <elene.mst@gmail.com> | 2013-02-23 01:22:10 +0200 |
| commit | 6dc42c0e7292d6d94de640ddd7de3ee5c808e9c2 (patch) | |
| tree | 638a8b4eaed9c1540618733c09e12f7c96140392 | |
| parent | 4047a2dc058e7e54e4ff95311fb556ae8eeeedb9 (diff) | |
added SDL backend for easier testing
| -rw-r--r-- | Makefile | 18 | ||||
| -rw-r--r-- | src/fbdev/event.cc (renamed from src/event.cc) | 2 | ||||
| -rw-r--r-- | src/fbdev/gfx.cc (renamed from src/gfx.cc) | 13 | ||||
| -rw-r--r-- | src/fbdev/keyboard.cc (renamed from src/keyboard.cc) | 4 | ||||
| -rw-r--r-- | src/fbdev/mouse.cc (renamed from src/mouse.cc) | 17 | ||||
| -rw-r--r-- | src/gfx.h | 2 | ||||
| -rw-r--r-- | src/main.cc | 2 | ||||
| -rw-r--r-- | src/mouse.h | 4 | ||||
| -rw-r--r-- | src/sdl/event.cc | 33 | ||||
| -rw-r--r-- | src/sdl/gfx.cc | 170 | ||||
| -rw-r--r-- | src/sdl/keyboard.cc | 37 | ||||
| -rw-r--r-- | src/sdl/mouse.cc | 89 | ||||
| -rw-r--r-- | src/window.cc | 6 | ||||
| -rw-r--r-- | src/window.h | 1 | ||||
| -rw-r--r-- | src/wm.cc | 19 |
15 files changed, 393 insertions, 24 deletions
@@ -1,16 +1,24 @@ -src = $(wildcard src/*.cc) +src = $(wildcard src/*.cc) $(wildcard src/fbdev/*.cc) $(wildcard src/sdl/*.cc) obj = $(src:.cc=.o) dep = $(obj:.o=.d) bin = winnie - dbg = -g opt = -O0 -#inc = +inc = -Isrc + +backend = SDL + +ifeq ($(backend), SDL) + def = -DWINNIE_SDL + libs = -lSDL +else + def = -DWINNIE_FBDEV +endif CXX = g++ -CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(inc) -#LDFLAGS = +CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(inc) $(def) +LDFLAGS = $(libs) $(bin): $(obj) $(CXX) -o $@ $(obj) $(LDFLAGS) diff --git a/src/event.cc b/src/fbdev/event.cc index a54fe7a..611ec3c 100644 --- a/src/event.cc +++ b/src/fbdev/event.cc @@ -1,3 +1,4 @@ +#ifdef WINNIE_FBDEV #include <stdio.h> #include <errno.h> @@ -35,3 +36,4 @@ void process_events() } } } +#endif // WINNIE_FBDEV diff --git a/src/gfx.cc b/src/fbdev/gfx.cc index 7d3595c..00e1881 100644 --- a/src/gfx.cc +++ b/src/fbdev/gfx.cc @@ -1,3 +1,4 @@ +#ifdef WINNIE_FBDEV #include <errno.h> #include <limits.h> #include <stdio.h> @@ -18,7 +19,7 @@ static unsigned char *framebuffer; static int dev_fd = -1; static Rect screen_rect; -static int color_depth; //bits per pixel +static int color_depth; // bits per pixel bool init_gfx() { @@ -207,9 +208,9 @@ void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_ int b = sptr[j * 4 + 2]; if(r != key_r || g != key_g || b != key_b) { - dptr[j * 4] = r; + dptr[j * 4] = b; dptr[j * 4 + 1] = g; - dptr[j * 4 + 2] = b; + dptr[j * 4 + 2] = r; } } @@ -217,3 +218,9 @@ void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_ dptr += dest_rect.width * 4; } } + +void gfx_update() +{ +} + +#endif // WINNIE_FBDEV diff --git a/src/keyboard.cc b/src/fbdev/keyboard.cc index 1b23e12..77f0bdd 100644 --- a/src/keyboard.cc +++ b/src/fbdev/keyboard.cc @@ -1,3 +1,4 @@ +#ifdef WINNIE_FBDEV #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -89,7 +90,7 @@ void process_keyboard_event() if(focused_win) { KeyboardFuncType keyb_callback = focused_win->get_keyboard_callback(); if(keyb_callback) { - keyb_callback(focused_win, key, true); + keyb_callback(focused_win, key, true); //TODO: true?? } } @@ -98,3 +99,4 @@ void process_keyboard_event() * - otherwise send keypress/release to focused window */ } +#endif // WINNIE_FBDEV diff --git a/src/mouse.cc b/src/fbdev/mouse.cc index b3f329b..5fd080b 100644 --- a/src/mouse.cc +++ b/src/fbdev/mouse.cc @@ -1,3 +1,4 @@ +#ifdef WINNIE_FBDEV #include <errno.h> #include <limits.h> #include <stdio.h> @@ -19,6 +20,7 @@ #define BN_RIGHT 2 #define BN_MIDDLE 4 +static int read_mouse(); static int dev_fd = -1; // file descriptor for /dev/psaux static Rect bounds; @@ -75,7 +77,6 @@ void process_mouse_event() } else { wm->set_focused_window(0); - return; } /* - send each pointer move and button press/release to the topmost window @@ -85,16 +86,16 @@ void process_mouse_event() int dx = pointer_x - prev_x; int dy = pointer_y - prev_y; - if(dx || dy) { + if((dx || dy) && top) { MouseMotionFuncType motion_callback = top->get_mouse_motion_callback(); if(motion_callback) { - Rect rect = top->get_rect(); + Rect rect = top->get_absolute_rect(); motion_callback(top, pointer_x - rect.x, pointer_y - rect.y); } } - MouseButtonFuncType button_callback = top->get_mouse_button_callback(); - if(button_callback && (bnstate != prev_state)) { + MouseButtonFuncType button_callback; + if((bnstate != prev_state) && top && (button_callback = top->get_mouse_button_callback())) { int num_bits = sizeof bnstate * CHAR_BIT; for(int i=0; i<num_bits; i++) { int s = (bnstate >> i) & 1; @@ -112,7 +113,7 @@ void get_pointer_pos(int *x, int *y) *y = pointer_y; } -int get_button_state(int bn) +int get_button_state() { return bnstate; } @@ -125,8 +126,7 @@ int get_button(int bn) return (bnstate & (1 << bn)) != 0; } - -int read_mouse() +static int read_mouse() { int rd; signed char state[3] = {0, 0, 0}; @@ -158,3 +158,4 @@ int read_mouse() return 0; } +#endif // WINNIE_FBDEV @@ -21,4 +21,6 @@ void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b); +void gfx_update(); + #endif //GFX_H_ diff --git a/src/main.cc b/src/main.cc index 6697506..8b36449 100644 --- a/src/main.cc +++ b/src/main.cc @@ -30,9 +30,11 @@ static void display(Window *win) { if(wm->get_focused_window() != win) { fill_rect(win->get_rect(), 106, 106, 250); + printf("drawing unfocused\n"); } else { fill_rect(win->get_rect(), 0, 0, 255); + printf("drawing FOCUSED\n"); } } diff --git a/src/mouse.h b/src/mouse.h index 271ee83..1770d7a 100644 --- a/src/mouse.h +++ b/src/mouse.h @@ -12,9 +12,7 @@ int get_mouse_fd(); void process_mouse_event(); void get_pointer_pos(int *x, int *y); -int get_button_state(int bn); +int get_button_state(); int get_button(int bn); -int read_mouse(); - #endif // MOUSE_H_ diff --git a/src/sdl/event.cc b/src/sdl/event.cc new file mode 100644 index 0000000..97c9b1e --- /dev/null +++ b/src/sdl/event.cc @@ -0,0 +1,33 @@ +#ifdef WINNIE_SDL +#include <stdlib.h> +#include <SDL/SDL.h> + +#include "event.h" +#include "keyboard.h" +#include "mouse.h" +#include "wm.h" + +SDL_Event sdl_event; +void process_events() +{ + wm->process_windows(); + if(!SDL_WaitEvent(&sdl_event)) { + return; + } + + switch(sdl_event.type) { + case SDL_KEYDOWN: + case SDL_KEYUP: + process_keyboard_event(); + break; + case SDL_MOUSEMOTION: + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + process_mouse_event(); + break; + case SDL_QUIT: + exit(0); + } +} + +#endif // WINNIE_SDL diff --git a/src/sdl/gfx.cc b/src/sdl/gfx.cc new file mode 100644 index 0000000..b757688 --- /dev/null +++ b/src/sdl/gfx.cc @@ -0,0 +1,170 @@ +#ifdef WINNIE_SDL +#include <stdio.h> +#include <stdlib.h> +#include <SDL/SDL.h> +#include "gfx.h" + +static SDL_Surface *fbsurf; + +static Rect screen_rect = {0, 0, 1024, 768}; +static int color_depth = 32; // bits per pixel + +bool init_gfx() +{ + if(SDL_Init(SDL_INIT_VIDEO) == -1) { + fprintf(stderr, "failed to initialize SDL\n"); + return false; + } + + if(!(fbsurf = SDL_SetVideoMode(screen_rect.width, screen_rect.height, color_depth, 0))) { + fprintf(stderr, "failed to set video mode\n"); + return false; + } + SDL_ShowCursor(0); + + return true; +} + +void destroy_gfx() +{ + SDL_Quit(); +} + +unsigned char *get_framebuffer() +{ + return (unsigned char*)fbsurf->pixels; +} + +Rect get_screen_size() +{ + return screen_rect; +} + +int get_color_depth() +{ + return color_depth; +} + +void clear_screen(int r, int g, int b) +{ + fill_rect(screen_rect, r, g, b); +} + +void fill_rect(const Rect &rect, int r, int g, int b) +{ + uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); + + SDL_Rect sdl_rect; + sdl_rect.x = rect.x; + sdl_rect.y = rect.y; + sdl_rect.w = rect.width; + sdl_rect.h = rect.height; + + SDL_FillRect(fbsurf, &sdl_rect, color); +} + +void set_cursor_visibility(bool visible) +{ +} + +void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, + const Rect &dest_rect, int dest_x, int dest_y) +{ + int width = src_rect.width; + int height = src_rect.height; + + int xoffs = dest_x - dest_rect.x; + if(xoffs < 0) { + dest_x = dest_rect.x; + width += xoffs; + } + + int yoffs = dest_y - dest_rect.y; + if(yoffs < 0) { + dest_y = dest_rect.y; + height += yoffs; + } + + int xend = dest_x + width; + if(xend >= dest_rect.width) { + width -= xend - dest_rect.width; + } + + int yend = dest_y + height; + if(yend >= dest_rect.height) { + height -= yend - dest_rect.height; + } + + if(width <= 0 || height <= 0) { + return; + } + + unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4; + unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4; + + for(int i=0; i<height; i++) { + memcpy(dptr, sptr, width * 4); + sptr += src_rect.width * 4; + dptr += dest_rect.width * 4; + } +} + +void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, + const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b) +{ + int width = src_rect.width; + int height = src_rect.height; + + int xoffs = dest_x - dest_rect.x; + if(xoffs < 0) { + dest_x = dest_rect.x; + width += xoffs; + } + + int yoffs = dest_y - dest_rect.y; + if(yoffs < 0) { + dest_y = dest_rect.y; + height += yoffs; + } + + int xend = dest_x + width; + if(xend >= dest_rect.width) { + width -= xend - dest_rect.width; + } + + int yend = dest_y + height; + if(yend >= dest_rect.height) { + height -= yend - dest_rect.height; + } + + if(width <= 0 || height <= 0) { + return; + } + + unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4; + unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4; + + for(int i=0; i<height; i++) { + for(int j=0; j<width; j++) { + int r = sptr[j * 4]; + int g = sptr[j * 4 + 1]; + int b = sptr[j * 4 + 2]; + + if(r != key_r || g != key_g || b != key_b) { + dptr[j * 4] = r; + dptr[j * 4 + 1] = g; + dptr[j * 4 + 2] = b; + } + } + + sptr += src_rect.width * 4; + dptr += dest_rect.width * 4; + } +} + +void gfx_update() +{ + SDL_UpdateRect(fbsurf, 0, 0, 0, 0); +} + +#endif // WINNIE_SDL diff --git a/src/sdl/keyboard.cc b/src/sdl/keyboard.cc new file mode 100644 index 0000000..d7f549d --- /dev/null +++ b/src/sdl/keyboard.cc @@ -0,0 +1,37 @@ +#ifdef WINNIE_SDL +#include <SDL/SDL.h> + +#include "keyboard.h" +#include "window.h" +#include "wm.h" + +extern SDL_Event sdl_event; + +bool init_keyboard() +{ + return true; +} + +void destroy_keyboard() +{ +} + +int get_keyboard_fd() +{ + return -1; +} + +void process_keyboard_event() +{ + int key = sdl_event.key.keysym.sym; + + Window *focused_win = wm->get_focused_window(); + if(focused_win) { + KeyboardFuncType keyb_callback = focused_win->get_keyboard_callback(); + if(keyb_callback) { + bool pressed = sdl_event.key.state == SDL_PRESSED; + keyb_callback(focused_win, key, pressed); + } + } +} +#endif // WINNIE_SDL diff --git a/src/sdl/mouse.cc b/src/sdl/mouse.cc new file mode 100644 index 0000000..5531707 --- /dev/null +++ b/src/sdl/mouse.cc @@ -0,0 +1,89 @@ +#ifdef WINNIE_SDL +#include <SDL/SDL.h> + +#include "mouse.h" +#include "window.h" +#include "wm.h" + +extern SDL_Event sdl_event; + +static int pointer_x, pointer_y; +static int bnstate; + +bool init_mouse() +{ + return true; +} + +void destroy_mouse() +{ +} + +void set_mouse_bounds(const Rect &rect) +{ +} + +int get_mouse_fd() +{ + return -1; +} + +void process_mouse_event() +{ + int bn; + MouseMotionFuncType motion_callback = 0; + MouseButtonFuncType button_callback = 0; + + Window *top = wm->get_window_at_pos(pointer_x, pointer_y); + + if(top) { + wm->set_focused_window(top); + } + else { + wm->set_focused_window(0); + } + + switch(sdl_event.type) { + case SDL_MOUSEMOTION: + pointer_x = sdl_event.motion.x; + pointer_y = sdl_event.motion.y; + if(top && (motion_callback = top->get_mouse_motion_callback())) { + Rect rect = top->get_absolute_rect(); + motion_callback(top, pointer_x - rect.x, pointer_y - rect.y); + } + break; + + case SDL_MOUSEBUTTONUP: + case SDL_MOUSEBUTTONDOWN: + bn = sdl_event.button.button - SDL_BUTTON_LEFT; + if(sdl_event.button.state == SDL_PRESSED) { + bnstate |= 1 << bn; + } + else { + bnstate &= ~(1 << bn); + } + if(top && (button_callback = top->get_mouse_button_callback())) { + button_callback(top, bn, sdl_event.button.state); + } + } +} + +void get_pointer_pos(int *x, int *y) +{ + *x = pointer_x; + *y = pointer_y; +} + +int get_button_state() +{ + return bnstate; +} + +int get_button(int bn) +{ + if(bn < 0 || bn >= 3) { + return 0; + } + return (bnstate & (1 << bn)) != 0; +} +#endif // WINNIE_SDL diff --git a/src/window.cc b/src/window.cc index 2a8b6f5..46528b7 100644 --- a/src/window.cc +++ b/src/window.cc @@ -7,6 +7,7 @@ Window::Window() { + parent = 0; title = 0; rect.x = rect.y = 0; rect.width = rect.height = 128; @@ -31,6 +32,11 @@ const Rect &Window::get_rect() const return rect; } +const Rect &Window::get_absolute_rect() const +{ + return rect; // TODO implement absolute rectangle thingy +} + bool Window::contains_point(int ptr_x, int ptr_y) { return ptr_x >= rect.x && ptr_x < rect.x + rect.width && diff --git a/src/window.h b/src/window.h index 1eebed0..3db8ec8 100644 --- a/src/window.h +++ b/src/window.h @@ -24,6 +24,7 @@ public: ~Window(); const Rect &get_rect() const; + const Rect &get_absolute_rect() const; bool contains_point(int ptr_x, int ptr_y); void move(int x, int y); @@ -130,6 +130,8 @@ void WindowManager::process_windows() Rect mouse_rect = {mouse_x, mouse_y, mouse_cursor.get_width(), mouse_cursor.get_height()}; invalidate_region(mouse_rect); + + gfx_update(); } void WindowManager::add_window(Window *win) @@ -163,6 +165,18 @@ void WindowManager::remove_window(Window *win) void WindowManager::set_focused_window(Window *win) { + if(win == focused_win) { + return; + } + + if(focused_win) { + // invalidate the frame (if any) + Window *parent = focused_win->get_parent(); + if(parent && parent != root_win) { + parent->invalidate(); + } + } + if(!win) { focused_win = 0; return; @@ -211,10 +225,7 @@ Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y) static void display(Window *win) { - if(win->get_managed()) { - fill_rect(win->get_rect(), 255, 211, 5); - win->draw(win->get_parent()->get_rect()); - } + fill_rect(win->get_rect(), 255, 211, 5); } static int prev_x = -1, prev_y; |
