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
|
#include "compositor.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"
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
{
uint16_t posx;
uint16_t posy;
uint16_t width;
uint16_t height;
uint32_t color;
uint16_t active;
struct pdirectory *vmem;
};
static uint16_t next_window=0;
static uint16_t next_x=50;
static uint16_t next_y=50;
#define MAX_WINDOWS 100
struct window windows[MAX_WINDOWS];
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 void put_pixel(int x,int y, uint32_t color)
{
vmem[y*vesa_width+x]=color;
}
static void put_rect(struct window *win)
{
struct pdirectory* mydir=x86_get_page_directory();
if(win->vmem)
{
x86_set_page_directory(win->vmem);
}
uint32_t *user_vmem=VMEM_USER_FRAMEBUFFER;
uint32_t userx=0;
uint32_t usery=0;
for(uint16_t x=win->posx;x<win->posx+win->width;x++)
{
backbuffer[(win->posy-1)*vesa_width+x]=0xffffff;
backbuffer[(win->posy+win->height)*vesa_width+x]=0xffffff;
}
for(uint16_t x=win->posx;x<win->posx+win->width;x++)
{
usery=0;
userx++;
for(uint16_t y=win->posy;y<win->posy+win->height;y++)
{
usery++;
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;
//we encoded alpha in the highest octet. 1-opaque 0-transparent
//user premultiplied alpha please!
uint16_t a=(0xff-((win->color&0xff000000)>>24));
uint8_t out_r=src_r+((a*dst_r)>>8);
uint8_t out_g=src_g+((a*dst_g)>>8);
uint8_t out_b=src_b+((a*dst_b)>>8);
if(win->vmem)
{
if(userx<640&&usery<480)backbuffer[y*vesa_width+x]=user_vmem[userx+usery*640];
}
else
{
backbuffer[y*vesa_width+x]=(out_r<<16)+(out_g<<8)+out_b;
}
}
else
{
if(win->vmem)
{
if(userx<640&&usery<480)backbuffer[y*vesa_width+x]=~(user_vmem[userx+usery*640]);
}
else
{
backbuffer[y*vesa_width+x]=0xffffff;
}
}
}
}
if(win->vmem)
{
x86_set_page_directory(mydir);
}
}
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_add_window(uint32_t addr)
{
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;
next_window++;
}
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_swap_buffers()
{
static bool first=true;
// if(!first)return;
// klog("swap");
memcpy(backbuffer,bgimage,vesa_height*vesa_width*4);// TODO optimize? rects only too?
for(int i=next_window-1;i>=0;i--)
{
put_rect(&windows[i]);
}
put_mouse();
memcpy(VMEM_FRAMEBUFFER,backbuffer,vesa_height*vesa_width*4);// TODO optimize? rects only too?
first=false;
}
void compositor_mouse_handle(uint16_t x,uint16_t y, uint8_t key)
{
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;
}
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");
}
|