shared memory: TODO fix client bug
authorEleni Maria Stea <elene.mst@gmail.com>
Mon, 1 Apr 2013 01:00:05 +0000 (04:00 +0300)
committerEleni Maria Stea <elene.mst@gmail.com>
Mon, 1 Apr 2013 01:00:05 +0000 (04:00 +0300)
20 files changed:
clock/Makefile [new file with mode: 0644]
libwinnie/src/fbdev/gfx.cc
libwinnie/src/fbdev/keyboard.cc
libwinnie/src/fbdev/mouse.cc
libwinnie/src/gfx.h
libwinnie/src/keyboard.h
libwinnie/src/mouse.h
libwinnie/src/sdl/gfx.cc
libwinnie/src/sdl/keyboard.cc
libwinnie/src/sdl/mouse.cc
libwinnie/src/shalloc.cc
libwinnie/src/shalloc.h
libwinnie/src/text.cc
libwinnie/src/text.h
libwinnie/src/winnie.cc
libwinnie/src/winnie.h
libwinnie/src/wm.cc
libwinnie/src/wm.h
winnie/Makefile
winnie/src/main.cc

diff --git a/clock/Makefile b/clock/Makefile
new file mode 100644 (file)
index 0000000..05167ac
--- /dev/null
@@ -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)
+
index ff86d9c..f98b398 100644 (file)
@@ -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;
index 22f129a..7ac1263 100644 (file)
@@ -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;
index 1fb2d55..8563da8 100644 (file)
@@ -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;
index e6d4b49..6d93bc0 100644 (file)
@@ -28,6 +28,9 @@ Author: Eleni Maria Stea <elene.mst@gmail.com>
 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();
 
index 548ddbb..4d55cba 100644 (file)
@@ -25,6 +25,9 @@ Author: Eleni Maria Stea <elene.mst@gmail.com>
 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();
 
index 6fbe711..9e55496 100644 (file)
@@ -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();
index 7e098b9..c15cd78 100644 (file)
@@ -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;
index 2290213..042bbbb 100644 (file)
@@ -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;
index 3ba98a0..9196a93 100644 (file)
@@ -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)
 {
 }
index 5d4715a..6595406 100644 (file)
@@ -21,8 +21,8 @@ Author: Eleni Maria Stea <elene.mst@gmail.com>
 
 #include <assert.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <stdint.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include <errno.h>
@@ -35,9 +35,6 @@ Author: Eleni Maria Stea <elene.mst@gmail.com>
 
 #include "shalloc.h"
 
-#define SHMNAME        "/winnie.shm"
-
-#define POOL_SIZE 16777216
 #define BLOCK_SIZE 512
 
 #define NUM_BLOCKS (POOL_SIZE / BLOCK_SIZE)
index bb0daf7..3518d53 100644 (file)
@@ -24,6 +24,9 @@ Author: Eleni Maria Stea <elene.mst@gmail.com>
 
 #include <cstring>
 
+#define POOL_SIZE 16777216
+#define SHMNAME "/winnie.shm"
+
 bool init_shared_memory();
 void destroy_shared_memory();
 
index 7dbae5d..3d6ab0c 100644 (file)
@@ -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) {
index a94388a..d71670f 100644 (file)
@@ -25,6 +25,9 @@ Author: Eleni Maria Stea <elene.mst@gmail.com>
 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);
index ffbe94d..121c7d6 100644 (file)
@@ -19,6 +19,13 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 Author: Eleni Maria Stea <elene.mst@gmail.com>
 */
 
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/mman.h>
 #include <sys/time.h>
 
 #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;
index 899357c..1c4f60c 100644 (file)
@@ -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();
index 621a23f..3d1f9f5 100644 (file)
@@ -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;
index 46f4857..ab8faa4 100644 (file)
@@ -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<Window*> windows;
index 64a9cf5..6065e37 100644 (file)
@@ -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
index 3c1c504..c6ad6f5 100644 (file)
@@ -25,10 +25,6 @@ Author: Eleni Maria Stea <elene.mst@gmail.com>
 
 #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();