casting in fbdev
[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
100         subsys = (Subsys*)pool;
101
102         if(!client_open_gfx(pool, subsys->graphics_offset)) {
103                 fprintf(stderr, "Failed to open graphics.\n");
104                 return false;
105         }
106
107         if(!client_open_keyboard(pool, subsys->keyboard_offset)) {
108                 fprintf(stderr, "Failed to open keyboard.\n");
109                 return false;
110         }
111
112         if(!client_open_mouse(pool, subsys->mouse_offset)) {
113                 fprintf(stderr, "Failed to open mouse.\n");
114                 return false;
115         }
116
117         if(!client_open_text(pool, subsys->text_offset)) {
118                 fprintf(stderr, "Failed to open text.\n");
119                 return false;
120         }
121
122         if(!client_open_wm(pool, subsys->wm_offset)) {
123                 fprintf(stderr, "Failed to open the window manager.\n");
124                 return false;
125         }
126
127         return true;
128 }
129
130 void winnie_close()
131 {
132         client_close_gfx();
133         client_close_keyboard();
134         client_close_mouse();
135         client_close_text();
136         client_close_wm();
137
138         if(munmap(pool, POOL_SIZE) == -1) {
139                 fprintf(stderr, "Failed to unmap shared memory: %s\n", strerror(errno));
140         }
141 }
142
143 long winnie_get_time()
144 {
145         static struct timeval init_tv;
146         struct timeval tv;
147
148         gettimeofday(&tv, 0);
149
150         if(!tv.tv_sec && !tv.tv_usec) {
151                 init_tv = tv;
152                 return 0;
153         }
154
155         return (tv.tv_usec - init_tv.tv_usec) / 1000 + (tv.tv_sec - init_tv.tv_sec) * 1000;
156 }
157
158 Subsys *get_subsys()
159 {
160         return subsys;
161 }