From 87255f9ad8bb7f8aea8b8c073e869b9a53c07322 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Mon, 1 Apr 2013 04:00:05 +0300 Subject: [PATCH] shared memory: TODO fix client bug --- clock/Makefile | 34 ++++++++++++++++++++ libwinnie/src/fbdev/gfx.cc | 10 ++++++ libwinnie/src/fbdev/keyboard.cc | 10 ++++++ libwinnie/src/fbdev/mouse.cc | 10 ++++++ libwinnie/src/gfx.h | 3 ++ libwinnie/src/keyboard.h | 3 ++ libwinnie/src/mouse.h | 3 ++ libwinnie/src/sdl/gfx.cc | 10 ++++++ libwinnie/src/sdl/keyboard.cc | 9 ++++++ libwinnie/src/sdl/mouse.cc | 10 ++++++ libwinnie/src/shalloc.cc | 5 +-- libwinnie/src/shalloc.h | 3 ++ libwinnie/src/text.cc | 10 ++++++ libwinnie/src/text.h | 3 ++ libwinnie/src/winnie.cc | 66 +++++++++++++++++++++++++++++++++++++++ libwinnie/src/winnie.h | 3 ++ libwinnie/src/wm.cc | 11 +++++++ libwinnie/src/wm.h | 3 ++ winnie/Makefile | 2 +- winnie/src/main.cc | 48 ---------------------------- 20 files changed, 203 insertions(+), 53 deletions(-) create mode 100644 clock/Makefile diff --git a/clock/Makefile b/clock/Makefile new file mode 100644 index 0000000..05167ac --- /dev/null +++ b/clock/Makefile @@ -0,0 +1,34 @@ +src = $(wildcard src/*.cc) $(wildcard src/fbdev/*.cc) $(wildcard src/sdl/*.cc) +obj = $(src:.cc=.o) +dep = $(obj:.o=.d) +bin = clock + +dbg = -g +opt = -O0 +inc = -Isrc -I../libwinnie/src + +backend = SDL + +ifeq ($(backend), SDL) + def = -DWINNIE_SDL + libs = -lSDL +else + def = -DWINNIE_FBDEV +endif + +CXX = g++ +CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(inc) $(def) `freetype-config --cflags` +LDFLAGS = -L../libwinnie/ $(libs) `freetype-config --libs` -lrt -lwinnie + +$(bin): $(obj) + $(CXX) -o $@ $(obj) $(LDFLAGS) -Wl,-rpath=../libwinnie + +-include $(dep) + +%.d: %.cc + @$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@ + +.PHONY: clean +clean: + rm -f $(obj) $(bin) $(dep) + diff --git a/libwinnie/src/fbdev/gfx.cc b/libwinnie/src/fbdev/gfx.cc index ff86d9c..f98b398 100644 --- a/libwinnie/src/fbdev/gfx.cc +++ b/libwinnie/src/fbdev/gfx.cc @@ -150,6 +150,16 @@ void destroy_gfx() sh_free(gfx); } +bool client_open_gfx(void *smem_start, int offset) +{ + gfx = (unsigned char*)smem_start + offset; + return true; +} + +void client_close_gfx() +{ +} + unsigned char *get_framebuffer() { return gfx->pixmap->pixels; diff --git a/libwinnie/src/fbdev/keyboard.cc b/libwinnie/src/fbdev/keyboard.cc index 22f129a..7ac1263 100644 --- a/libwinnie/src/fbdev/keyboard.cc +++ b/libwinnie/src/fbdev/keyboard.cc @@ -108,6 +108,16 @@ void destroy_keyboard() sh_free(keyboard); } +bool client_open_keyboard(void *smem_start, int offset) +{ + keyboard = (unsigned char*)smem_start + offset; + return true; +} + +void client_close_keyboard() +{ +} + int get_keyboard_fd() { return keyboard->dev_fd; diff --git a/libwinnie/src/fbdev/mouse.cc b/libwinnie/src/fbdev/mouse.cc index 1fb2d55..8563da8 100644 --- a/libwinnie/src/fbdev/mouse.cc +++ b/libwinnie/src/fbdev/mouse.cc @@ -83,6 +83,16 @@ void destroy_mouse() sh_free(mouse); } +bool client_open_mouse(void *smem_start, int offset) +{ + mouse = (unsigned char*)smem_start + offset; + return true; +} + +void client_close_mouse() +{ +} + void set_mouse_bounds(const Rect &rect) { mouse->bounds = rect; diff --git a/libwinnie/src/gfx.h b/libwinnie/src/gfx.h index e6d4b49..6d93bc0 100644 --- a/libwinnie/src/gfx.h +++ b/libwinnie/src/gfx.h @@ -28,6 +28,9 @@ Author: Eleni Maria Stea bool init_gfx(); void destroy_gfx(); +bool client_open_gfx(void *smem_start, int offset); +void client_close_gfx(); + unsigned char *get_framebuffer(); Pixmap *get_framebuffer_pixmap(); diff --git a/libwinnie/src/keyboard.h b/libwinnie/src/keyboard.h index 548ddbb..4d55cba 100644 --- a/libwinnie/src/keyboard.h +++ b/libwinnie/src/keyboard.h @@ -25,6 +25,9 @@ Author: Eleni Maria Stea bool init_keyboard(); void destroy_keyboard(); +bool client_open_keyboard(void *smem_start, int offset); +void client_close_keyboard(); + int get_keyboard_fd(); void process_keyboard_event(); diff --git a/libwinnie/src/mouse.h b/libwinnie/src/mouse.h index 6fbe711..9e55496 100644 --- a/libwinnie/src/mouse.h +++ b/libwinnie/src/mouse.h @@ -27,6 +27,9 @@ struct Rect; bool init_mouse(); void destroy_mouse(); +bool client_open_mouse(void *smem_start, int offset); +void client_close_mouse(); + void set_mouse_bounds(const Rect &rect); int get_mouse_fd(); diff --git a/libwinnie/src/sdl/gfx.cc b/libwinnie/src/sdl/gfx.cc index 7e098b9..c15cd78 100644 --- a/libwinnie/src/sdl/gfx.cc +++ b/libwinnie/src/sdl/gfx.cc @@ -90,6 +90,16 @@ void destroy_gfx() SDL_Quit(); } +bool client_open_gfx(void *smem_start, int offset) +{ + gfx = (Graphics*)((unsigned char*)smem_start + offset); + return true; +} + +void client_close_gfx() +{ +} + unsigned char *get_framebuffer() { return gfx->pixmap->pixels; diff --git a/libwinnie/src/sdl/keyboard.cc b/libwinnie/src/sdl/keyboard.cc index 2290213..042bbbb 100644 --- a/libwinnie/src/sdl/keyboard.cc +++ b/libwinnie/src/sdl/keyboard.cc @@ -37,6 +37,15 @@ void destroy_keyboard() { } +bool client_open_keyboard(void *smem_start, int offset) +{ + return true; +} + +void client_close_keyboard() +{ +} + int get_keyboard_fd() { return -1; diff --git a/libwinnie/src/sdl/mouse.cc b/libwinnie/src/sdl/mouse.cc index 3ba98a0..9196a93 100644 --- a/libwinnie/src/sdl/mouse.cc +++ b/libwinnie/src/sdl/mouse.cc @@ -54,6 +54,16 @@ void destroy_mouse() sh_free(mouse); } +bool client_open_mouse(void *smem_start, int offset) +{ + mouse = (Mouse*)((unsigned char*)smem_start + offset); + return true; +} + +void client_close_mouse() +{ +} + void set_mouse_bounds(const Rect &rect) { } diff --git a/libwinnie/src/shalloc.cc b/libwinnie/src/shalloc.cc index 5d4715a..6595406 100644 --- a/libwinnie/src/shalloc.cc +++ b/libwinnie/src/shalloc.cc @@ -21,8 +21,8 @@ Author: Eleni Maria Stea #include #include -#include #include +#include #include #include @@ -35,9 +35,6 @@ Author: Eleni Maria Stea #include "shalloc.h" -#define SHMNAME "/winnie.shm" - -#define POOL_SIZE 16777216 #define BLOCK_SIZE 512 #define NUM_BLOCKS (POOL_SIZE / BLOCK_SIZE) diff --git a/libwinnie/src/shalloc.h b/libwinnie/src/shalloc.h index bb0daf7..3518d53 100644 --- a/libwinnie/src/shalloc.h +++ b/libwinnie/src/shalloc.h @@ -24,6 +24,9 @@ Author: Eleni Maria Stea #include +#define POOL_SIZE 16777216 +#define SHMNAME "/winnie.shm" + bool init_shared_memory(); void destroy_shared_memory(); diff --git a/libwinnie/src/text.cc b/libwinnie/src/text.cc index 7dbae5d..3d6ab0c 100644 --- a/libwinnie/src/text.cc +++ b/libwinnie/src/text.cc @@ -75,6 +75,16 @@ void destroy_text() sh_free(text); } +bool client_open_text(void *smem_start, int offset) +{ + text = (Text*)((unsigned char*)smem_start + offset); + return true; +} + +void client_close_text() +{ +} + void draw_text(const char *txt, Pixmap *pixmap) { if(!pixmap) { diff --git a/libwinnie/src/text.h b/libwinnie/src/text.h index a94388a..d71670f 100644 --- a/libwinnie/src/text.h +++ b/libwinnie/src/text.h @@ -25,6 +25,9 @@ Author: Eleni Maria Stea bool init_text(); void destroy_text(); +bool client_open_text(void *smem_start, int offset); +void client_close_text(); + void draw_text(const char *txt, Pixmap *pixmap = 0); void set_text_position(int x, int y); void set_text_color(int r, int g, int b); diff --git a/libwinnie/src/winnie.cc b/libwinnie/src/winnie.cc index ffbe94d..121c7d6 100644 --- a/libwinnie/src/winnie.cc +++ b/libwinnie/src/winnie.cc @@ -19,6 +19,13 @@ along with this program. If not, see . Author: Eleni Maria Stea */ +#include +#include +#include + +#include +#include +#include #include #include "keyboard.h" @@ -75,6 +82,65 @@ void winnie_shutdown() destroy_shared_memory(); } +static int fd; +static void *pool; + +bool winnie_open() +{ + if(((fd = shm_open(SHMNAME, O_RDWR, S_IRWXU)) == -1)) { + fprintf(stderr, "Failed to open shared memory: %s\n", strerror(errno)); + return false; + } + + if((pool = mmap(0, POOL_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == (void*)-1) { + fprintf(stderr, "Failed to map shared memory: %s\n", strerror(errno)); + return false; + } + shm_unlink(SHMNAME); + + subsys = (Subsys*)pool; + + if(!client_open_gfx(pool, subsys->graphics_offset)) { + fprintf(stderr, "Failed to open graphics.\n"); + return false; + } + + if(!client_open_keyboard(pool, subsys->keyboard_offset)) { + fprintf(stderr, "Failed to open keyboard.\n"); + return false; + } + + if(!client_open_mouse(pool, subsys->mouse_offset)) { + fprintf(stderr, "Failed to open mouse.\n"); + return false; + } + + if(!client_open_text(pool, subsys->text_offset)) { + fprintf(stderr, "Failed to open text.\n"); + return false; + } + + if(!client_open_wm(pool, subsys->wm_offset)) { + fprintf(stderr, "Failed to open the window manager.\n"); + return false; + } + + return true; +} + +void winnie_close() +{ + client_close_gfx(); + client_close_keyboard(); + client_close_mouse(); + client_close_text(); + client_close_wm(); + + if(munmap(pool, POOL_SIZE) == -1) { + fprintf(stderr, "Failed to unmap shared memory: %s\n", strerror(errno)); + } +} + long winnie_get_time() { static struct timeval init_tv; diff --git a/libwinnie/src/winnie.h b/libwinnie/src/winnie.h index 899357c..1c4f60c 100644 --- a/libwinnie/src/winnie.h +++ b/libwinnie/src/winnie.h @@ -42,6 +42,9 @@ struct Subsys { bool winnie_init(); void winnie_shutdown(); +bool winnie_open(); +void winnie_close(); + long winnie_get_time(); Subsys *get_subsys(); diff --git a/libwinnie/src/wm.cc b/libwinnie/src/wm.cc index 621a23f..3d1f9f5 100644 --- a/libwinnie/src/wm.cc +++ b/libwinnie/src/wm.cc @@ -61,6 +61,17 @@ void destroy_window_manager() sh_free(wm); } + +bool client_open_wm(void *smem_start, int offset) +{ + wm = (WindowManager*) ((unsigned char*)smem_start + offset); + return true; +} + +void client_close_wm() +{ +} + void WindowManager::create_frame(Window *win) { Window *frame = new Window; diff --git a/libwinnie/src/wm.h b/libwinnie/src/wm.h index 46f4857..ab8faa4 100644 --- a/libwinnie/src/wm.h +++ b/libwinnie/src/wm.h @@ -33,6 +33,9 @@ class Window; bool init_window_manager(); void destroy_window_manager(); +bool client_open_wm(void *smem_start, int offset); +void client_close_wm(); + class WindowManager { private: std::list windows; diff --git a/winnie/Makefile b/winnie/Makefile index 64a9cf5..6065e37 100644 --- a/winnie/Makefile +++ b/winnie/Makefile @@ -18,7 +18,7 @@ endif CXX = g++ CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(inc) $(def) `freetype-config --cflags` -LDFLAGS = -L../libwinnie/ $(libs) `freetype-config --libs` -lrt -lwinnie +LDFLAGS = -L../libwinnie $(libs) `freetype-config --libs` -lrt -lwinnie $(bin): $(obj) $(CXX) -o $@ $(obj) $(LDFLAGS) -Wl,-rpath=../libwinnie diff --git a/winnie/src/main.cc b/winnie/src/main.cc index 3c1c504..c6ad6f5 100644 --- a/winnie/src/main.cc +++ b/winnie/src/main.cc @@ -25,10 +25,6 @@ Author: Eleni Maria Stea #include "winnie.h" -static void display(Window *win); -static void keyboard(Window *win, int key, bool pressed); -static void button(Window *win, int bn, bool pressed, int x, int y); -static void motion(Window *win, int x, int y); static void cleanup(); int main() @@ -39,27 +35,6 @@ int main() atexit(cleanup); - Window *win1 = new Window; - win1->set_title("Clipping the win title"); - win1->move(200, 100); - win1->resize(200, 300); - win1->set_display_callback(display); - win1->set_keyboard_callback(keyboard); - win1->set_mouse_button_callback(button); - win1->set_mouse_motion_callback(motion); - - Window *win2 = new Window; - win2->set_title("window 2"); - win2->move(300, 100); - win2->resize(200, 300); - win2->set_display_callback(display); - win2->set_keyboard_callback(keyboard); - win2->set_mouse_button_callback(button); - win2->set_mouse_motion_callback(motion); - - wm->add_window(win1); - wm->add_window(win2); - Pixmap bg; if(!(bg.load("data/bg.ppm"))) { fprintf(stderr, "failed to load pixmap\n"); @@ -72,29 +47,6 @@ int main() } } -static void display(Window *win) -{ - fill_rect(win->get_absolute_rect(), 128, 128, 128); -} - -static void keyboard(Window *win, int key, bool pressed) -{ - switch(key) { - case 'q': - exit(0); - } -} - -static void button(Window *win, int bn, bool pressed, int x, int y) -{ - printf("WINDOW(%p) button %d %s\n", (void*)win, bn, pressed ? "press" : "release"); -} - -static void motion(Window *win, int x, int y) -{ - printf("WINDOW(%p) motion %d %d\n", (void*)win, x, y); -} - static void cleanup() { winnie_shutdown(); -- 1.7.10.4