fixed fbdev/*.cc to work with malloc
authorEleni Maria Stea <elene.mst@gmail.com>
Sun, 3 Mar 2013 20:01:04 +0000 (22:01 +0200)
committerEleni Maria Stea <elene.mst@gmail.com>
Sun, 3 Mar 2013 20:01:04 +0000 (22:01 +0200)
Makefile
src/fbdev/gfx.cc
src/fbdev/keyboard.cc
src/fbdev/mouse.cc

index 4242187..a132c3f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@ dbg = -g
 opt = -O0
 inc = -Isrc
 
-backend = SDL
+#backend = SDL
 
 ifeq ($(backend), SDL)
        def = -DWINNIE_SDL
index d0a8178..9984422 100644 (file)
@@ -30,6 +30,10 @@ static Graphics *gfx;
 
 bool init_gfx()
 {
+       if(!(gfx = (Graphics*)malloc(sizeof *gfx))) {
+               return false;
+       }
+
        gfx->dev_fd = -1;
 
        if((gfx->dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
@@ -45,10 +49,6 @@ bool init_gfx()
                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);
 
index 77f0bdd..625647f 100644 (file)
 #include "window.h"
 #include "wm.h"
 
-static int dev_fd = -1;
-static enum {RAW, CANONICAL} ttystate = CANONICAL;
+struct Keyboard {
+       int dev_fd;
+       enum {RAW, CANONICAL} ttystate;
+};
+
+static Keyboard *keyboard;
 
 bool init_keyboard()
 {
-       if((dev_fd = open("/dev/tty", O_RDWR)) == -1) {
+       if(!(keyboard = (Keyboard*)malloc(sizeof *keyboard))) {
+               return false;
+       }
+
+       keyboard->ttystate = keyboard->CANONICAL;
+       keyboard->dev_fd = -1;
+
+       if((keyboard->dev_fd = open("/dev/tty", O_RDWR)) == -1) {
                fprintf(stderr, "Cannot open /dev/tty : %s\n", strerror(errno));
                return false;
        }
 
        struct termios buf;
 
-       if(tcgetattr(dev_fd, &buf) < 0) {
+       if(tcgetattr(keyboard->dev_fd, &buf) < 0) {
                fprintf(stderr, "Cannot get the tty parameters : %s\n", strerror(errno));
                return false;
        }
@@ -36,11 +47,11 @@ bool init_keyboard()
        buf.c_cflag |= CS8;
        buf.c_oflag &= ~(OPOST);
 
-       if(tcsetattr(dev_fd, TCSAFLUSH, &buf) < 0) {
+       if(tcsetattr(keyboard->dev_fd, TCSAFLUSH, &buf) < 0) {
                return false;
        }
 
-       ttystate = RAW;
+       keyboard->ttystate = keyboard->RAW;
        return true;
 }
 
@@ -48,7 +59,7 @@ void destroy_keyboard()
 {
        struct termios buf;
 
-       if(tcgetattr(dev_fd, &buf) < 0) {
+       if(tcgetattr(keyboard->dev_fd, &buf) < 0) {
                fprintf(stderr, "Cannot get the tty parameters : %s\n", strerror(errno));
        }
 
@@ -58,27 +69,29 @@ void destroy_keyboard()
        buf.c_cflag &= CS8;
        buf.c_oflag |= (OPOST);
 
-       if(tcsetattr(dev_fd, TCSAFLUSH, &buf) < 0) {
+       if(tcsetattr(keyboard->dev_fd, TCSAFLUSH, &buf) < 0) {
                fprintf(stderr, "Cannot set the tty parameters : %s\n", strerror(errno));
        }
 
-       ttystate = CANONICAL;
+       keyboard->ttystate = keyboard->CANONICAL;
 
-       if(dev_fd != -1) {
-               close(dev_fd);
-               dev_fd = -1;
+       if(keyboard->dev_fd != -1) {
+               close(keyboard->dev_fd);
+               keyboard->dev_fd = -1;
        }
+
+       free(keyboard);
 }
 
 int get_keyboard_fd()
 {
-       return dev_fd;
+       return keyboard->dev_fd;
 }
 
 void process_keyboard_event()
 {
        char key;
-       if(read(dev_fd, &key, 1) < 1) {
+       if(read(keyboard->dev_fd, &key, 1) < 1) {
                return;
        }
 
index 61c5406..803e8e0 100644 (file)
 
 static int read_mouse();
 
-static int dev_fd = -1;        // file descriptor for /dev/psaux
-static Rect bounds;
-static int pointer_x, pointer_y;
-static int bnstate;
+struct Mouse {
+       int dev_fd;
+       Rect bounds;
+       int pointer_x;
+       int pointer_y;
+       int bnstate;
+};
+
+static Mouse *mouse;
 
 bool init_mouse()
 {
-       if((dev_fd = open("/dev/psaux", O_RDONLY | O_NONBLOCK)) == -1) {
+       if(!(mouse = (Mouse*)malloc(sizeof *mouse))) {
+               return false;
+       }
+
+       mouse->dev_fd = -1;
+
+       if((mouse->dev_fd = open("/dev/psaux", O_RDONLY | O_NONBLOCK)) == -1) {
                fprintf(stderr, "Cannot open /dev/psaux : %s\n", strerror(errno));
                return false;
        }
@@ -40,20 +51,21 @@ bool init_mouse()
 
 void destroy_mouse()
 {
-       if(dev_fd != -1) {
-               close(dev_fd);
-               dev_fd = -1;
+       if(mouse->dev_fd != -1) {
+               close(mouse->dev_fd);
+               mouse->dev_fd = -1;
        }
+       free(mouse);
 }
 
 void set_mouse_bounds(const Rect &rect)
 {
-       bounds = rect;
+       mouse->bounds = rect;
 }
 
 int get_mouse_fd()
 {
-       return dev_fd;
+       return mouse->dev_fd;
 }
 
 void process_mouse_event()
@@ -63,9 +75,9 @@ void process_mouse_event()
         *   read will return -1 when there are no more events instead of blocking).
         */
 
-       int prev_state = bnstate;
-       int prev_x = pointer_x;
-       int prev_y = pointer_y;
+       int prev_state = mouse->bnstate;
+       int prev_x = mouse->pointer_x;
+       int prev_y = mouse->pointer_y;
 
        if(read_mouse() == -1) {
                return;
@@ -73,7 +85,7 @@ void process_mouse_event()
 
        Window *top;
        if(!(top = wm->get_grab_window())) {
-               top = wm->get_window_at_pos(pointer_x, pointer_y);
+               top = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
                if(top) {
                        wm->set_focused_window(top);
                }
@@ -86,26 +98,26 @@ void process_mouse_event()
         *   with the pointer on it.
         */
 
-       int dx = pointer_x - prev_x;
-       int dy = pointer_y - prev_y;
+       int dx = mouse->pointer_x - prev_x;
+       int dy = mouse->pointer_y - prev_y;
 
        if((dx || dy) && top) {
                MouseMotionFuncType motion_callback = top->get_mouse_motion_callback();
                if(motion_callback) {
                        Rect rect = top->get_absolute_rect();
-                       motion_callback(top, pointer_x - rect.x, pointer_y - rect.y);
+                       motion_callback(top, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
                }
        }
 
        MouseButtonFuncType button_callback;
-       if((bnstate != prev_state) && top && (button_callback = top->get_mouse_button_callback())) {
-               int num_bits = sizeof bnstate * CHAR_BIT;
+       if((mouse->bnstate != prev_state) && top && (button_callback = top->get_mouse_button_callback())) {
+               int num_bits = sizeof mouse->bnstate * CHAR_BIT;
                for(int i=0; i<num_bits; i++) {
-                       int s = (bnstate >> i) & 1;
+                       int s = (mouse->bnstate >> i) & 1;
                        int prev_s = (prev_state >> i) & 1;
                        if(s != prev_s) {
                                Rect rect = top->get_absolute_rect();
-                               button_callback(top, i, s, pointer_x - rect.x, pointer_y - rect.y);
+                               button_callback(top, i, s, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
                        }
                }
        }
@@ -113,13 +125,13 @@ void process_mouse_event()
 
 void get_pointer_pos(int *x, int *y)
 {
-       *x = pointer_x;
-       *y = pointer_y;
+       *x = mouse->pointer_x;
+       *y = mouse->pointer_y;
 }
 
 int get_button_state()
 {
-       return bnstate;
+       return mouse->bnstate;
 }
 
 int get_button(int bn)
@@ -127,7 +139,7 @@ int get_button(int bn)
        if(bn < 0 || bn >= 3) {
                return 0;
        }
-       return (bnstate & (1 << bn)) != 0;
+       return (mouse->bnstate & (1 << bn)) != 0;
 }
 
 static int read_mouse()
@@ -135,29 +147,29 @@ static int read_mouse()
        int rd;
        signed char state[3] = {0, 0, 0};
 
-       if((rd = read(dev_fd, state, 3)) == -1) {
+       if((rd = read(mouse->dev_fd, state, 3)) == -1) {
                fprintf(stderr, "Unable to get mouse state : %s\n", strerror(errno));
                return -1;
        }
 
-       bnstate = state[0] & 7;
-       pointer_x += state[1];
-       pointer_y -= state[2];
+       mouse->bnstate = state[0] & 7;
+       mouse->pointer_x += state[1];
+       mouse->pointer_y -= state[2];
 
-       if(pointer_x < bounds.x) {
-               pointer_x = bounds.x;
+       if(mouse->pointer_x < mouse->bounds.x) {
+               mouse->pointer_x = mouse->bounds.x;
        }
 
-       if(pointer_y < bounds.y) {
-               pointer_y = bounds.y;
+       if(mouse->pointer_y < mouse->bounds.y) {
+               mouse->pointer_y = mouse->bounds.y;
        }
 
-       if(pointer_x > bounds.x + bounds.width - 1) {
-               pointer_x = bounds.x + bounds.width - 1;
+       if(mouse->pointer_x > mouse->bounds.x + mouse->bounds.width - 1) {
+               mouse->pointer_x = mouse->bounds.x + mouse->bounds.width - 1;
        }
 
-       if(pointer_y > bounds.y + bounds.height - 1) {
-               pointer_y = bounds.y + bounds.height - 1;
+       if(mouse->pointer_y > mouse->bounds.y + mouse->bounds.height - 1) {
+               mouse->pointer_y = mouse->bounds.y + mouse->bounds.height - 1;
        }
 
        return 0;