1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
#include "compositor.h"
#include "syscalls.h"
#include "kernel.h"
#include "kmalloc.h"
#include "log.h"
#include "vmem.h"
#include "asm_x86.h"
#include "mount.h"
#include "fd.h"
#include "lib/string/string.h"
#define MAX_WINDOWS 100
static uint32_t backbuffer[VESA_MAX_WIDTH*VESA_MAX_HEIGHT];
static uint32_t bgimage[VESA_MAX_WIDTH*VESA_MAX_HEIGHT];
static uint32_t *vmem=VMEM_FRAMEBUFFER;
struct window
{
int16_t posx;
int16_t posy;
uint16_t width;
uint16_t height;
uint32_t color;
uint16_t active;
uint32_t pid;
struct pdirectory *vmem;
};
struct window windows[MAX_WINDOWS];
static uint16_t next_window=0;
static uint16_t next_x=50;
static uint16_t next_y=50;
static uint16_t vesa_width;
static uint16_t vesa_height;
static uint16_t vesa_pitch;
static uint16_t mouse_x=100;
static uint16_t mouse_y=100;
static uint16_t mouse_k;
static bool skip_render;
static bool skip_render2;
static void put_pixel(int x,int y, uint32_t color)
{
vmem[y*vesa_width+x]=color;
}
static void put_win(struct window *win)
{
struct pdirectory* mydir;
if(win->vmem)
{
x86_cli();// do not reschedule us til ready!
mydir=x86_get_page_directory();
x86_set_page_directory(win->vmem);
}
uint32_t *user_vmem=VMEM_USER_FRAMEBUFFER;
uint32_t userx=0;
uint32_t usery=0;
// iterate over complete window area
for(uint16_t x=win->posx;x<win->posx+win->width;x++)
{
usery=0;
for(uint16_t y=win->posy;y<win->posy+win->height;y++)
{
if(!win->active)
{
uint8_t dst_b=backbuffer[y*vesa_width+x]&0xff;
uint8_t dst_g=(backbuffer[y*vesa_width+x]&0xff00)>>8;
uint8_t dst_r=(backbuffer[y*vesa_width+x]&0xff0000)>>16;
uint8_t src_b=win->color&0xff;
uint8_t src_g=(win->color&0xff00)>>8;
uint8_t src_r=(win->color&0xff0000)>>16;
uint16_t a;
if(win->vmem)
{
uint32_t col=0xff0000;
col=user_vmem[userx+usery*640];
src_b=col&0xff;
src_g=(col&0xff00)>>8;
src_r=(col&0xff0000)>>16;
a=0x44;
}
else
{
// just for testing
src_b=win->color&0xff;
src_g=(win->color&0xff00)>>8;
src_r=(win->color&0xff0000)>>16;
a=(0xff-((win->color&0xff000000)>>24));
}
//we encoded alpha in the highest octet. 1-opaque 0-transparent
//no premultiplied alpha. TODO: change maybe?
//
uint8_t out_r=((src_r*(0xff-a))>>8)+((a*dst_r)>>8);
uint8_t out_g=((src_g*(0xff-a))>>8)+((a*dst_g)>>8);
uint8_t out_b=((src_b*(0xff-a))>>8)+((a*dst_b)>>8);
backbuffer[y*vesa_width+x]=(out_r<<16)+(out_g<<8)+out_b;
}
else
{
if(win->vmem)
{
backbuffer[y*vesa_width+x]=(user_vmem[userx+usery*640]);
}
else
{
backbuffer[y*vesa_width+x]=0xffffff;
}
}
usery++;
}
userx++;
}
if(win->vmem)
{
x86_set_page_directory(mydir);
x86_sti();
}
//draw boundaries
for(uint16_t x=win->posx;x<win->posx+win->width;x++)
{
backbuffer[(win->posy)*vesa_width+x]=0xffffff; //TOP
backbuffer[(win->posy+win->height-1)*vesa_width+x]=0xffffff; //BOTTOM
//backbuffer[(win->posy+win->height-3)*vesa_width+x]=0xffffff; //BOTTOM
}
for(uint16_t y=win->posy;y<win->posy+win->height;y++)
{
backbuffer[(y)*vesa_width+(win->posx)]=0xffffff; //LEFT
backbuffer[(y)*vesa_width+win->posx+win->width-1]=0xffffff; //RIGHT
}
//
}
static void put_mouse()
{
if(mouse_k&1)
{
for(int i=-5;i<5;i++)
for(int j=-5;j<5;j++)
backbuffer[(mouse_y+j)*vesa_width+mouse_x+i]=0x00ff00;
}
else
{
for(int i=-5;i<5;i++)
for(int j=-5;j<5;j++)
backbuffer[(mouse_y+i)*vesa_width+mouse_x+j]=0xff0000;
}
}
void compositor_del_window(uint32_t addr)
{
for(int i=0;i<next_window;i++)
{
if(windows[i].vmem==(void *)addr)
{
if(i!=next_window-1)
{
windows[i]=windows[next_window-1];
}
next_window--;
}
}
}
void compositor_add_window(uint32_t addr,uint32_t pid)
{
klog("window added");
if (next_window>=MAX_WINDOWS)kpanic("max number of windows reached. increase MAX_WINDOWS");
windows[next_window]=windows[0];
windows[0].posx=next_x;
windows[0].posy=next_y;
next_x+=100;
next_y+=100;
windows[0].width=640;
windows[0].height=480;
windows[0].color=0xAAAA00AA;
windows[0].active=0;
windows[0].vmem=addr;
windows[0].pid=pid;
next_window++;
compositor_mouse_handle(200,200,0);
}
void compositor_init(uint16_t width, uint16_t height, uint16_t pitch)
{
vesa_width=width;
vesa_height=height;
klog("initialized composing window manager to %d x %d",width,height);
}
void compositor_wake()
{
skip_render=false;
}
void compositor_wake2()
{
skip_render2=false;
}
void compositor_swap_buffers()
{
if(skip_render||skip_render2)return;
skip_render=true;
skip_render2=true;
static bool first=true;
// if(!first)return;
// klog("swap");
// background
memcpy(backbuffer,bgimage,vesa_height*vesa_width*4);// TODO optimize? rects only too?
for(int i=next_window-1;i>=0;i--)
{
put_win(&windows[i]);
}
put_mouse();
memcpy(VMEM_FRAMEBUFFER,backbuffer,vesa_height*vesa_width*4);// TODO optimize? rects only too?
first=false;
}
void compositor_kb_handle(char c)
{
for(int i=0;i<MAX_WINDOWS;i++)
{
struct window *w=&windows[i];
if(w->active)
{
// klog("writing [%c] to window with pid [%d]",c,w->pid);
fd_write(get_fd(w->pid,0),c);
}
}
}
void compositor_mouse_handle(uint16_t x,uint16_t y, uint8_t key)
{
//klog("mouse: %d %d",x,y);
static int last_mouse_x;
static int last_mouse_y;
if(!(key&1))
{
last_mouse_x=x;
last_mouse_y=y;
}
mouse_x=x;
mouse_y=y;
mouse_k=key;
int active=-1;
for(int i=0;i<MAX_WINDOWS;i++)
{
struct window *w=&windows[i];
if(active==-1&&x>w->posx&&x<w->posx+w->width&&y>w->posy&&y<w->posy+w->height)
{
if(key&1)
{
w->posx-=last_mouse_x-mouse_x;
last_mouse_x=mouse_x;
w->posy-=last_mouse_y-mouse_y;
last_mouse_y=mouse_y;
if(w->posx<0)w->posx=0;
if(w->posy<0)w->posy=0;
if(w->posx+w->width>=1920)w->posx=1920-640;
if(w->posy+w->height>=1080)w->posy=1080-480;
}
w->active=true;
active=i;
}
else
{
w->active=false;
}
}
if(active!=-1&&active!=0&&(key&1))
{
struct window temp;
temp=windows[active];
windows[active]=windows[0];
windows[0]=temp;
}
}
void compositor_set_background(char *ppm_raw_filename)
{
klog("loading bg...");
fd bg=mount_file_open(ppm_raw_filename);
uint32_t i=0;
for(int y=0;y<vesa_width;y++)
{
for(int x=0;x<vesa_height;x++)
{
bgimage[i]=(fd_read(&bg)<<16)+(fd_read(&bg)<<8)+fd_read(&bg);
backbuffer[i]=bgimage[i];
i++;
}
}
klog("finished");
}
|