shared memory: TODO fix client bug
[winnie] / libwinnie / src / winnie.cc
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;