#include "settings.h" #include "paging.h" #include "window.h" #include "drive.h" #include "panic.h" #include "task.h" #include "util.h" #include "pmap.h" #include "elf.h" #include "log.h" #include "vbe.h" #define MAX_WINDOWS 64 #define ACTION_BUFFER_PAGES 1 #define AB_END(buf) (buf + (ACTION_BUFFER_PAGES * 4096) / sizeof(struct window_action)) #define GSC(x, y) (x / 4 + y / 4 >= 256 ? 255 : x / 4 + y / 4) #define BACKGROUND(x, y) ((struct pixel){.r = GSC(x / 2, y / 3), .g = GSC(x, y), .b = GSC(x, y / 2)}) static struct pixel border_color; #define MOVE_BY 5 static struct window { const volatile struct pixel *pixel_buffer_pma; uint16_t width; uint16_t height; uint16_t xpos; uint16_t ypos; struct window_action *action_buffer; struct window_action *next_action_read; struct window_action *next_action_write; struct window *above; struct window *below; struct task_state *from_task; } windows[MAX_WINDOWS]; static struct window *bottom_window = 0; static struct window *top_window = 0; static struct pixel *buffer; static uint32_t buffer_pages; static uint32_t pix_clear_mask; static void load_mode_params() { if (buffer) free_pages(buffer, buffer_pages); const uint32_t fb_len = VBE_MODE_INFO->width * VBE_MODE_INFO->height; buffer_pages = (fb_len * sizeof(struct pixel) - 1) / 4096 + 1; buffer = allocate_kernel_pages(buffer_pages); if (!buffer) PANIC("could not allocate intermediate framebuffer"); #define MASK(offset, length) ((~((1 << offset) - 1)) - (offset + length == 32 ? 0 : (~((1 << (offset + length)) - 1)))) pix_clear_mask = ~( MASK(VBE_MODE_INFO->red_off, VBE_MODE_INFO->red_len) | MASK(VBE_MODE_INFO->green_off, VBE_MODE_INFO->green_len) | MASK(VBE_MODE_INFO->blue_off, VBE_MODE_INFO->blue_len) | MASK(VBE_MODE_INFO->alpha_off, VBE_MODE_INFO->alpha_len) ); } static void blit() { void *row = VBE_MODE_INFO->frame_buf; const struct pixel *from = buffer; for (uint16_t y = 0; y < VBE_MODE_INFO->height; ++y) { uint32_t *out_pix = row; for (uint16_t x = 0; x < VBE_MODE_INFO->width; ++x) { *out_pix &= pix_clear_mask; *out_pix |= (from->r >> (8 - VBE_MODE_INFO->red_len)) << VBE_MODE_INFO->red_off; *out_pix |= (from->g >> (8 - VBE_MODE_INFO->green_len)) << VBE_MODE_INFO->green_off; *out_pix |= (from->b >> (8 - VBE_MODE_INFO->blue_len)) << VBE_MODE_INFO->blue_off; out_pix = (uint32_t *)((void *)out_pix + VBE_MODE_INFO->bpp / 8); ++from; } row += VBE_MODE_INFO->pitch; } } #define POWER_OFF_MESSAGE_PITCH 45 #define POWER_OFF_MESSAGE_ROWS 28 static const bool power_off_message[] = { 1,0,0,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0,1, 1,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,1, 0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,1,0,1,1,0,0,1,0,0,0,1,0,0,0,1,0,1,0, 0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,1,0,0,1,1,1,1,1,0,0,0,1,0,1,0, 0,0,1,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,0,0, 0,0,1,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,0,0, 0,0,1,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,0,0, 0,0,1,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,0,0, 0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; #define POWER_OFF_SCALE 3 void show_shutdown() { for (uint32_t i = 0; i < VBE_MODE_INFO->height * VBE_MODE_INFO->width; ++i) buffer[i] = (struct pixel){.r = 0, .g = 0, .b = 0}; const uint16_t x_pad = (VBE_MODE_INFO->width - POWER_OFF_MESSAGE_PITCH * POWER_OFF_SCALE) / 2; const uint16_t y_pad = (VBE_MODE_INFO->height - POWER_OFF_MESSAGE_ROWS * POWER_OFF_SCALE) / 2; for (uint8_t r = 0; r < POWER_OFF_MESSAGE_ROWS; ++r) for (uint8_t i = 0; i < POWER_OFF_MESSAGE_PITCH; ++i) if (power_off_message[r * POWER_OFF_MESSAGE_PITCH + i]) { const uint32_t o = x_pad + i * POWER_OFF_SCALE + (y_pad + r * POWER_OFF_SCALE) * VBE_MODE_INFO->width; for (uint8_t y = 0; y < POWER_OFF_SCALE; ++y) for (uint8_t x = 0; x < POWER_OFF_SCALE; ++x) buffer[o + x + y * VBE_MODE_INFO->width] = (struct pixel){.r = 255, .g = 255, .b = 255}; } blit(); } static inline void draw_hz_line(uint16_t y, uint16_t xs, uint16_t xm, struct pixel color) { if (y >= VBE_MODE_INFO->height) return; if (xs >= VBE_MODE_INFO->width) xs = 0; if (xm > VBE_MODE_INFO->width) xm = VBE_MODE_INFO->width; for (uint16_t x = xs; x < xm; ++x) buffer[y * VBE_MODE_INFO->width + x] = color; } static inline void draw_vt_line(uint16_t x, uint16_t ys, uint16_t ym, struct pixel color) { if (x >= VBE_MODE_INFO->width) return; if (ys >= VBE_MODE_INFO->height) ys = 0; if (ym > VBE_MODE_INFO->height) ym = VBE_MODE_INFO->height; for (uint16_t y = ys; y < ym; ++y) buffer[y * VBE_MODE_INFO->width + x] = color; } static void paint_and_above(const struct window *w) { switch_to_kernel_cr3(); for (const struct window *i = w; i; i = i->above) { const uint16_t ys = i->ypos < VBE_MODE_INFO->height ? 0 : -i->ypos; const uint16_t xs = i->xpos < VBE_MODE_INFO->width ? 0 : -i->xpos; const uint16_t ym = ((i->ypos + i->height) & 0xffff) <= VBE_MODE_INFO->height ? i->height : VBE_MODE_INFO->height - i->ypos; const uint16_t xm = ((i->xpos + i->width) & 0xffff) <= VBE_MODE_INFO->width ? i->width : VBE_MODE_INFO->width - i->xpos; //logf(LOG_INFO, "y: %n .. %n", ys, ym - 1); //logf(LOG_INFO, "x: %n .. %n", xs, xm - 1); for (uint16_t y = ys; y < ym; ++y) for (uint16_t x = xs; x < xm; ++x) buffer[((y + i->ypos) & 0xffff) * VBE_MODE_INFO->width + ((x + i->xpos) & 0xffff)] = i->pixel_buffer_pma[y * i->width + x]; draw_hz_line(i->ypos - 2, i->xpos - 2, i->xpos + i->width + 2, border_color); draw_hz_line(i->ypos - 1, i->xpos - 2, i->xpos + i->width + 2, border_color); draw_hz_line(i->ypos + i->height, i->xpos - 2, i->xpos + i->width + 2, border_color); draw_hz_line(i->ypos + i->height + 1, i->xpos - 2, i->xpos + i->width + 2, border_color); draw_vt_line(i->xpos - 2, i->ypos, i->ypos + i->height, border_color); draw_vt_line(i->xpos - 1, i->ypos, i->ypos + i->height, border_color); draw_vt_line(i->xpos + i->width, i->ypos, i->ypos + i->height, border_color); draw_vt_line(i->xpos + i->width + 1, i->ypos, i->ypos + i->height, border_color); } blit(); switch_to_task_cr3(); } static void paint_bg() { for (uint16_t y = 0; y < VBE_MODE_INFO->height; ++y) for (uint16_t x = 0; x < VBE_MODE_INFO->width; ++x) buffer[y * VBE_MODE_INFO->width + x] = BACKGROUND(x, y); } static void paint_all() { paint_bg(); paint_and_above(bottom_window); } struct window *new_window(uint16_t width, uint16_t height, const struct pixel *pixel_buffer) { if (!pixel_buffer) { logf(LOG_WARN, "Refusing to create window with null pixel buffer for task %s.", active_task->name); return 0; } struct window *w; for (uint8_t i = 0; i < MAX_WINDOWS; ++i) if (!windows[i].pixel_buffer_pma) { w = &windows[i]; goto got_window; } return 0; got_window: w->pixel_buffer_pma = vma_to_pma(active_task->page_directory, pixel_buffer); w->width = width; w->height = height; w->xpos = (VBE_MODE_INFO->width / 2) - (width / 2); w->ypos = (VBE_MODE_INFO->height / 2) - (height / 2); struct window_action *const ab = allocate_kernel_pages(ACTION_BUFFER_PAGES); w->action_buffer = ab; w->next_action_read = ab; w->next_action_write = ab; if (top_window) top_window->above = w; else bottom_window = w; w->above = 0; w->below = top_window; top_window = w; w->from_task = active_task; paint_and_above(w); return w; } static void del_no_paint(struct window *w) { if (w == top_window) top_window = w->below; if (w == bottom_window) bottom_window = w->above; if (w->below) w->below->above = w->above; if (w->above) w->above->below = w->below; free_pages(w->action_buffer, ACTION_BUFFER_PAGES); w->pixel_buffer_pma = 0; } void del_window(struct window *w) { del_no_paint(w); paint_all(); } void delete_any_windows_from(struct task_state *tstate) { bool need_to_paint = false; for (struct window *w = windows; w < windows + MAX_WINDOWS; ++w) if (w->pixel_buffer_pma && (w->from_task == tstate)) { del_no_paint(w); need_to_paint = true; } if (need_to_paint) paint_all(); } void resize_window(struct window *w, uint16_t width, uint16_t height, const struct pixel *pixel_buffer) { const bool smaller = (width < w->width) || (height < w->height); w->width = width; w->height = height; reassign_pixel_buffer(w, pixel_buffer); if (smaller) paint_all(); else paint_and_above(w); } void reassign_pixel_buffer(struct window *w, const struct pixel *pixel_buffer) { w->pixel_buffer_pma = vma_to_pma(active_task->page_directory, pixel_buffer); } void push_window_paint(const struct window *w) { paint_and_above(w); } struct window_action next_window_action(struct window *w) { if (w->next_action_write == w->next_action_read) return (struct window_action){.action_type = NONE}; const struct window_action *const action = w->next_action_read; if (++(w->next_action_read) >= AB_END(w->action_buffer)) w->next_action_read = w->action_buffer; return *action; } static void send_action(struct window *w, struct window_action packet) { struct window_action *next_next = w->next_action_write + 1; if (next_next >= AB_END(w->action_buffer)) next_next = w->action_buffer; if (next_next != w->next_action_read) { *(w->next_action_write) = packet; w->next_action_write = next_next; unwait(w->from_task, (struct wait){.mode = WINDOW_ACTION}); } } #define RUN_COMMAND_FILE "sys/winspace.rc" enum wm_action { WM_SHUFFLE_UP, WM_SHUFFLE_DOWN, WM_MOVE_LEFT, WM_MOVE_RIGHT, WM_MOVE_UP, WM_MOVE_DOWN, WM_RUN_COMMAND, N_WM_ACTIONS }; static struct key_packet keybinds[] = { {.key_id = KEY_PAGE_DOWN, .modifiers = WINS}, {.key_id = KEY_PAGE_UP, .modifiers = WINS}, {.key_id = KEY_LEFT_ARROW, .modifiers = WINS}, {.key_id = KEY_RIGHT_ARROW, .modifiers = WINS}, {.key_id = KEY_UP_ARROW, .modifiers = WINS}, {.key_id = KEY_DOWN_ARROW, .modifiers = WINS}, {.key_id = KEY_SPACE, .modifiers = WINS} }; static inline bool fuzzy_key_match(struct key_packet t, struct key_packet a) { if (t.key_id != a.key_id) return false; if (((t.modifiers & SHIFTS) == SHIFTS) && (a.modifiers & SHIFTS)) a.modifiers |= SHIFTS; if (((t.modifiers & CTRLS) == CTRLS) && (a.modifiers & CTRLS)) a.modifiers |= CTRLS; if (((t.modifiers & ALTS) == ALTS) && (a.modifiers & ALTS)) a.modifiers |= ALTS; if (((t.modifiers & WINS) == WINS) && (a.modifiers & WINS)) a.modifiers |= WINS; return a.modifiers == t.modifiers; } static char *run_command; static char *run_command_pass = ""; void init_win() { if (!try_get_color_setting("wm-border-color", &border_color)) PANIC("could not load window border color setting."); buffer = 0; load_mode_params(); paint_bg(); blit(); const file_id_t fid = drives->get_file(drives, RUN_COMMAND_FILE); if (!fid) PANIC("Couldn't open " RUN_COMMAND_FILE "."); const uint32_t len = drives->get_file_length(drives, fid); run_command = allocate_kernel_pages(len / 4096 + 1); if (!run_command) PANIC("Couldn't make buffer for " RUN_COMMAND_FILE " contents."); fmcpy(run_command, drives, fid, 0, len); run_command[len] = '\0'; drives->free_file(drives, fid); for (char *c = run_command; *c; ++c) if (*c == ' ') { *c = '\0'; run_command_pass = c + 1; break; } } void on_action(struct window_action packet) { //logf(LOG_INFO, "Window action, top window = 0x%d from %s.", top_window, top_window->from_task->name); if (packet.action_type == NOT_READY) return; if (packet.action_type == KEY_DOWN) for (uint8_t i = 0; i < N_WM_ACTIONS; ++i) if (fuzzy_key_match(keybinds[i], packet.as_key)) { switch_to_kernel_cr3(); struct window *old_top, *old_bottom; switch (i) { case WM_SHUFFLE_UP: if (!top_window || !top_window->below) break; old_top = top_window; old_bottom = bottom_window; top_window = old_top->below; top_window->above = 0; old_top->below = 0; old_top->above = old_bottom; old_bottom->below = old_top; bottom_window = old_top; paint_and_above(bottom_window->above); break; case WM_SHUFFLE_DOWN: if (!top_window || !top_window->below) break; old_top = top_window; old_bottom = bottom_window; bottom_window = old_bottom->above; bottom_window->below = 0; old_bottom->above = 0; old_bottom->below = old_top; old_top->above = old_bottom; top_window = old_bottom; paint_and_above(top_window); break; case WM_MOVE_LEFT: if (top_window) { top_window->xpos -= MOVE_BY; paint_all(); } break; case WM_MOVE_RIGHT: if (top_window) { top_window->xpos += MOVE_BY; paint_all(); } break; case WM_MOVE_UP: if (top_window) { top_window->ypos -= MOVE_BY; paint_all(); } break; case WM_MOVE_DOWN: if (top_window) { top_window->ypos += MOVE_BY; paint_all(); } break; case WM_RUN_COMMAND: if (!try_elf_run(drives, run_command, run_command_pass, 0)) PANIC("Couldn't run program listed in " RUN_COMMAND_FILE "."); } switch_to_task_cr3(); return; } if (top_window) send_action(top_window, packet); }