SayoriOS 0.3.3

window.c
1 #include "desktop/window.h"
2 #include "desktop/render.h"
3 #include "io/ports.h"
4 #include "mem/vmm.h"
5 #include "../lib/libvector/include/vector.h"
6
7 size_t window_global_id = 0;
8 size_t window_count = 0;
9
10 vector_t* windows = 0;
11
12 Window_t* _window_new(char* title) {
13 Window_t* win = kcalloc(1, sizeof(Window_t));
14
15 win->id = window_global_id++;
16 win->width = 10;
17 win->height = 10;
18 win->x = 10;
19 win->y = 10;
20 win->title = title;
21 win->state = NOT_INITIALIZED;
22 win->closable = true;
23 win->minimizable = true;
24 win->with_title_bar = true;
25 win->canvas_bgcolor = WINDOW_CANVAS_COLOR;
26 win->widgets = vector_new();
27
28 qemu_log("Bare window create ok");
29
30 return win;
31 }
32
33 vector_t * get_window_list() {
34 return windows;
35 }
36
37 size_t get_window_count() {
38 return window_count;
39 }
40
41 void window_add_widget(Window_t* window, Widget_t* widget) {
42 qemu_log("Widget count start: %d", window->widgets->size);
43
44 vector_push_back(window->widgets, (size_t) widget);
45
46 qemu_log("Widget count end: %d", window->widgets->size);
47 qemu_log("Added widget %x to window %x", widget, window);
48 }
49
50 void window_remove_widget(Window_t* window, Widget_t* widget) {
51 for (size_t i = 0; i < window->widgets->size; i++) {
52 if(window->widgets->data[i]) {
53 if((Widget_t*)window->widgets->data[i] == widget) {
54 vector_erase_nth(window->widgets, i);
55 }
56 }
57 }
58 }
59
60 void _window_add_to_list(Window_t* window) {
61 if(!windows) {
62 qemu_log("First time creating windows array!!!");
63 windows = vector_new();
64 }
65
66 qemu_log("Expanding windows array to %d elements", window_count + 1);
67 vector_push_back(windows, (size_t) window);
68
69 window_count++;
70 }
71
72 void _window_remove_from_list(Window_t* window) {
73 qemu_log("Window count: %d", window_count);
74 qemu_log("Removing window with id: %d; Address: %x", window->id, window);
75 for (size_t i = 0; i < window_count; i++) {
76 if(WINDOW(i)) {
77 if(WINDOW(i)->id == window->id) {
78 qemu_log("Found that window at index: %d.", i);
79 qemu_log("Total widgets in window: %d", window->widgets->size);
80 for (size_t j = 0; j < window->widgets->size; j++) {
81 qemu_log("|- Removing widget: %x", window->widgets[j]);
82 qemu_log("|--- Destroyer at: %x", ((Widget_t *)(window->widgets->data[j]))->destroyer);
83 destroy_widget((Widget_t *) window->widgets->data[j]);
84 }
85 vector_destroy(window->widgets);
86
87 window_count--;
88 qemu_log("Now, %d windows in the system", window_count);
89
90 vector_erase_nth(windows, i);
91
92 if(window_count == 0) {
93 qemu_log("ZEROING WINDOWS ARRAY");
94 vector_destroy(windows);
95 }
96
97 qemu_log("Ok!!!");
98 break;
99 }
100 }
101 }
102 }
103
104 Window_t* window_new(char* title) {
105 Window_t* instance = _window_new(title); // Allocate is here
106 instance->state = HIDDEN;
107
108 qemu_log("Created bare window");
109 _window_add_to_list(instance); // Allocate is here (many)
110
111 return instance;
112 }
113
114 // Returns true if window in list false otherwise
115 bool window_is_in_list(Window_t* win) {
116 for(size_t i = 0; i < windows->size; i++) {
117 if (WINDOW(i) == win) {
118 return true;
119 }
120 }
121
122 return false;
123 }
124
125 void window_destroy(Window_t* win) {
126 qemu_log(" ========> Reached window_destroy()");
127 if(!windows) {
128 qemu_err("No array!");
129 return;
130 }
131 if(!win) {
132 qemu_log("Window address is 0, not destroying it");
133 return;
134 }
135
136 if(!window_is_in_list(win)) {
137 qemu_warn("Window not in window list so, please destroy it manually!");
138 return;
139 }
140
141 if(win->state != NOT_INITIALIZED) {
142 if(win->on_close)
143 win->on_close(win);
144
145 _window_remove_from_list(win); // Reallocate is here
146 } else {
147 qemu_warn("Window is not initialized, not destroying it");
148 }
149
150 qemu_log("Freeing all its data at: %x", win);
151
152 kfree(win);
153
154 qemu_log("Ok");
155 }
156
157 void destroy_all_windows() {
158 qemu_warn("DESTROYING ALL WINDOWS KNOWN TO WINDOW MANAGER!!!");
159 while(window_count > 0) {
160 qemu_log("%d windows remaining...", window_count);
161 window_destroy(WINDOW(window_count - 1));
162 }
163
164 windows = 0;
165 qemu_log("DESTROYED ALL WINDOWS!");
166 }
167
168 void window_send_signal(Window_t* window, WindowSignal_t signal, void* data) {
169 qemu_log("Got signal on window (id %d): %s (data at %x)", window->id,
170 (signal == WINDOW_CLOSE ?
171 "WINDOW_CLOSE":
172 (signal == WINDOW_CLICK ?
173 "WINDOW_CLICK":
174 (signal == WINDOW_MINIMIZE ?
175 "WINDOW_MINIMIZE":"UNKNOWN"
176 )
177 )
178 ), data
179 );
180
181 if(signal == WINDOW_CLICK) {
182 Coordinates_t* coords = (Coordinates_t*)data;
183
184 coords->x -= window->x;
185 coords->y -= window->y;
186
187 qemu_log("Click on: X: %d; Y: %d", coords->x, coords->y);
188 qemu_log("Widget count: %d", window->widgets->size);
189
190 for (size_t i = 0; i < window->widgets->size; i++) {
191 Widget_t* wgt = (Widget_t *) window->widgets->data[i];
192 // qemu_log("Getting over widget: at %x", wgt);
193 // qemu_log("X: %d; Y: %d; W: %d; H: %d;", wgt->x, wgt->y, wgt->width, wgt->height);
194
195 if(wgt && point_in_rect(
196 coords->x,
197 coords->y,
198 wgt->x,
199 wgt->y,
200 (ssize_t)wgt->width,
201 (ssize_t)wgt->height
202 )) {
203 widget_notify(window, wgt, WIDGET_CLICK, coords);
204 qemu_log("Notified for click");
205 break;
206 }
207 }
208 }
209
210 qemu_log("Func end");
211 }
212
213 // For debugging purposes only!!!
214 void log_window_manager_state() {
215 qemu_log("===============[WINDOW MANAGER STATE]===================");
216 qemu_log("Window next global ID to be specified to next window: %d", window_global_id);
217 qemu_log("Window count: %d", window_count);
218 qemu_log("Address of windows array: %x", windows);
219
220 for(size_t i = 0; i < window_count; i++) {
221 qemu_log("Window #%d (%x): ID: %d", i, windows[i], WINDOW(i)->id);
222 if(WINDOW(i) != 0) {
223 qemu_log("Widgets:");
224 for (size_t j = 0; j < WINDOW(i)->widgets->size; j++) {
225 qemu_log("|- Widget #%d: at %x", j, WINDOW(i)->widgets[j]);
226 }
227
228 }
229 }
230
231 qemu_log("========================================================");
232 }
Coordinates
Definition: pointutils.h:5
Widget
Definition: widget.h:12
Window
Definition: window.h:25
vector
Definition: vector.h:7