diff options
Diffstat (limited to 'libwinnie/src/sdl')
-rw-r--r-- | libwinnie/src/sdl/event.cc | 56 | ||||
-rw-r--r-- | libwinnie/src/sdl/gfx.cc | 162 | ||||
-rw-r--r-- | libwinnie/src/sdl/keyboard.cc | 58 | ||||
-rw-r--r-- | libwinnie/src/sdl/mouse.cc | 127 |
4 files changed, 403 insertions, 0 deletions
diff --git a/libwinnie/src/sdl/event.cc b/libwinnie/src/sdl/event.cc new file mode 100644 index 0000000..685c323 --- /dev/null +++ b/libwinnie/src/sdl/event.cc @@ -0,0 +1,56 @@ +/* +winnie - an experimental window system + +Copyright (C) 2013 Eleni Maria Stea + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. + +Author: Eleni Maria Stea <elene.mst@gmail.com> +*/ + +#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); + default: + break; + } +} + +#endif // WINNIE_SDL diff --git a/libwinnie/src/sdl/gfx.cc b/libwinnie/src/sdl/gfx.cc new file mode 100644 index 0000000..7e098b9 --- /dev/null +++ b/libwinnie/src/sdl/gfx.cc @@ -0,0 +1,162 @@ +/* +winnie - an experimental window system + +Copyright (C) 2013 Eleni Maria Stea + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. + +Author: Eleni Maria Stea <elene.mst@gmail.com> +*/ + +#ifdef WINNIE_SDL +#include <stdio.h> +#include <stdlib.h> +#include <SDL/SDL.h> + +#include "gfx.h" +#include "shalloc.h" +#include "winnie.h" + +static SDL_Surface *fbsurf; + +struct Graphics { + Rect screen_rect; + Rect clipping_rect; + int color_depth; // bits per pixel + Pixmap *pixmap; +}; + +static Graphics *gfx; + +bool init_gfx() +{ + if(SDL_Init(SDL_INIT_VIDEO) == -1) { + fprintf(stderr, "failed to initialize SDL\n"); + return false; + } + + if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) { + return false; + } + + get_subsys()->graphics_offset = (int)((char*)gfx - (char*)get_pool()); + + Rect scr_rect(0, 0, 1024, 768); + gfx->screen_rect = scr_rect; + gfx->color_depth = 32; + + if(!(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); + + if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) { + fprintf(stderr, "Failed to allocate pixmap.\n"); + return false; + } + + gfx->pixmap->width = gfx->screen_rect.width; + gfx->pixmap->height = gfx->screen_rect.height; + + int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8; + if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) { + fprintf(stderr, "failed to allocate the pixmap framebuffer.\n"); + return false; + } + + set_clipping_rect(gfx->screen_rect); + + return true; +} + +void destroy_gfx() +{ + sh_free(gfx->pixmap->pixels); + gfx->pixmap->pixels = 0; + sh_free(gfx->pixmap); + sh_free(gfx); + SDL_Quit(); +} + +unsigned char *get_framebuffer() +{ + return gfx->pixmap->pixels; +} + +Pixmap *get_framebuffer_pixmap() +{ + return gfx->pixmap; +} + +Rect get_screen_size() +{ + return gfx->screen_rect; +} + +int get_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) +{ +} + +void gfx_update(const Rect &upd_rect) +{ + if(SDL_MUSTLOCK(fbsurf)) { + SDL_LockSurface(fbsurf); + } + + Rect rect = rect_intersection(upd_rect, gfx->screen_rect); + + unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4; + unsigned char *dptr = (unsigned char*)fbsurf->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4; + + for(int i=0; i<rect.height; i++) { + memcpy(dptr, sptr, rect.width * 4); + sptr += gfx->screen_rect.width * 4; + dptr += gfx->screen_rect.width * 4; + } + + if(SDL_MUSTLOCK(fbsurf)) { + SDL_UnlockSurface(fbsurf); + } + SDL_UpdateRect(fbsurf, rect.x, rect.y, rect.width, rect.height); +} + +void wait_vsync() +{ +} + +void get_rgb_order(int *r, int *g, int *b) +{ + *r = fbsurf->format->Rshift / 8; + *g = fbsurf->format->Gshift / 8; + *b = fbsurf->format->Bshift / 8; +} + +#endif // WINNIE_SDL diff --git a/libwinnie/src/sdl/keyboard.cc b/libwinnie/src/sdl/keyboard.cc new file mode 100644 index 0000000..2290213 --- /dev/null +++ b/libwinnie/src/sdl/keyboard.cc @@ -0,0 +1,58 @@ +/* +winnie - an experimental window system + +Copyright (C) 2013 Eleni Maria Stea + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. + +Author: Eleni Maria Stea <elene.mst@gmail.com> +*/ + +#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/libwinnie/src/sdl/mouse.cc b/libwinnie/src/sdl/mouse.cc new file mode 100644 index 0000000..3ba98a0 --- /dev/null +++ b/libwinnie/src/sdl/mouse.cc @@ -0,0 +1,127 @@ +/* +winnie - an experimental window system + +Copyright (C) 2013 Eleni Maria Stea + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. + +Author: Eleni Maria Stea <elene.mst@gmail.com> +*/ + +#ifdef WINNIE_SDL +#include <SDL/SDL.h> + +#include "mouse.h" +#include "shalloc.h" +#include "wm.h" +#include "window.h" +#include "winnie.h" + +extern SDL_Event sdl_event; + +struct Mouse { + int pointer_x; + int pointer_y; + int bnstate; +}; + +static Mouse *mouse; + +bool init_mouse() +{ + if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) { + return false; + } + get_subsys()->mouse_offset = (int)((char*)mouse - (char*)get_pool()); + + memset(mouse, 0, sizeof *mouse); + return true; +} + +void destroy_mouse() +{ + sh_free(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 *win; + if(!(win = wm->get_grab_window())) { + win = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y); + if(win) { + wm->set_focused_window(win); + } + else { + wm->set_focused_window(0); + } + } + + switch(sdl_event.type) { + case SDL_MOUSEMOTION: + mouse->pointer_x = sdl_event.motion.x; + mouse->pointer_y = sdl_event.motion.y; + if(win && (motion_callback = win->get_mouse_motion_callback())) { + Rect rect = win->get_absolute_rect(); + motion_callback(win, mouse->pointer_x - rect.x, mouse->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) { + mouse->bnstate |= 1 << bn; + } + else { + mouse->bnstate &= ~(1 << bn); + } + if(win && (button_callback = win->get_mouse_button_callback())) { + Rect rect = win->get_absolute_rect(); + button_callback(win, bn, sdl_event.button.state, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y); + } + } +} + +void get_pointer_pos(int *x, int *y) +{ + *x = mouse->pointer_x; + *y = mouse->pointer_y; +} + +int get_button_state() +{ + return mouse->bnstate; +} + +int get_button(int bn) +{ + if(bn < 0 || bn >= 3) { + return 0; + } + return (mouse->bnstate & (1 << bn)) != 0; +} +#endif // WINNIE_SDL |