121c7d6c4515746266ab09b1e733bd7897eaed40
[winnie] / libwinnie / src / winnie.cc
1 /*
2 winnie - an experimental window system
3
4 Copyright (C) 2013 Eleni Maria Stea
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
20 */
21
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/mman.h>
29 #include <sys/time.h>
30
31 #include "keyboard.h"
32 #include "mouse.h"
33 #include "shalloc.h"
34 #include "winnie.h"
35
36 static Subsys *subsys;
37
38 bool winnie_init()
39 {
40         if(!init_shared_memory()) {
41                 return false;
42         }
43
44         if(!(subsys = (Subsys*)sh_malloc(sizeof *subsys))) {
45                 return false;
46         }
47
48         if(!init_gfx()) {
49                 return false;
50         }
51
52         if(!init_window_manager()) {
53                 return false;
54         }
55
56         if(!init_keyboard()) {
57                 return false;
58         }
59
60         if(!init_mouse()) {
61                 return false;
62         }
63
64         if(!init_text()) {
65                 return false;
66         }
67
68         wm->invalidate_region(get_screen_size());
69         return true;
70 }
71
72 void winnie_shutdown()
73 {
74         destroy_gfx();
75         destroy_keyboard();
76         destroy_mouse();
77         destroy_text();
78         destroy_window_manager();
79
80         sh_free(subsys);
81
82         destroy_shared_memory();
83 }
84
85 static int fd;
86 static void *pool;
87
88 bool winnie_open()
89 {
90         if(((fd = shm_open(SHMNAME, O_RDWR, S_IRWXU)) == -1)) {
91                 fprintf(stderr, "Failed to open shared memory: %s\n", strerror(errno));
92                 return false;
93         }
94
95         if((pool = mmap(0, POOL_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == (void*)-1) {
96                 fprintf(stderr, "Failed to map shared memory: %s\n", strerror(errno));
97                 return false;
98         }
99         shm_unlink(SHMNAME);
100
101         subsys = (Subsys*)pool;
102
103         if(!client_open_gfx(pool, subsys->graphics_offset)) {
104                 fprintf(stderr, "Failed to open graphics.\n");
105                 return false;
106         }
107
108         if(!client_open_keyboard(pool, subsys->keyboard_offset)) {
109                 fprintf(stderr, "Failed to open keyboard.\n");
110                 return false;
111         }
112
113         if(!client_open_mouse(pool, subsys->mouse_offset)) {
114                 fprintf(stderr, "Failed to open mouse.\n");
115                 return false;
116         }
117
118         if(!client_open_text(pool, subsys->text_offset)) {
119                 fprintf(stderr, "Failed to open text.\n");
120                 return false;
121         }
122
123         if(!client_open_wm(pool, subsys->wm_offset)) {
124                 fprintf(stderr, "Failed to open the window manager.\n");
125                 return false;
126         }
127
128         return true;
129 }
130
131 void winnie_close()
132 {
133         client_close_gfx();
134         client_close_keyboard();
135         client_close_mouse();
136         client_close_text();
137         client_close_wm();
138
139         if(munmap(pool, POOL_SIZE) == -1) {
140                 fprintf(stderr, "Failed to unmap shared memory: %s\n", strerror(errno));
141         }
142 }
143
144 long winnie_get_time()
145 {
146         static struct timeval init_tv;
147         struct timeval tv;
148
149         gettimeofday(&tv, 0);
150
151         if(!tv.tv_sec && !tv.tv_usec) {
152                 init_tv = tv;
153                 return 0;
154         }
155
156         return (tv.tv_usec - init_tv.tv_usec) / 1000 + (tv.tv_sec - init_tv.tv_sec) * 1000;
157 }
158
159 Subsys *get_subsys()
160 {
161         return subsys;
162 }