Overview

The Kernel GUI System provides the foundation for graphical rendering in FreeWorld OS. It operates at the kernel level, directly accessing the VESA framebuffer to draw windows, UI components, and handle user input.

The system is implemented in x86-64 assembly for maximum performance and direct hardware access. It provides:

  • Direct framebuffer rendering (pixels, lines, rectangles, circles)
  • Window drawing with title bars, borders, and controls
  • Complete bitmap font rendering (all ASCII characters)
  • Dialog and message box system
  • UI components (buttons, labels, input fields) with full interaction
  • Mouse and keyboard event handling
  • Windows-style message loop
  • Window management (z-order, focus, procedures)
  • Window manipulation (drag, resize)
  • Double buffering for smooth rendering
  • Clipping regions for window content
  • Mouse cursor rendering

Architecture

The Kernel GUI System consists of several modules:

Renderer (kernel/gui/renderer.asm)

The core rendering engine that provides low-level graphics primitives:

  • gui_renderer_init() - Initializes framebuffer access
  • gui_clear_screen() - Clears screen with color
  • gui_draw_pixel() - Draws a single pixel
  • gui_draw_hline() - Draws horizontal line
  • gui_draw_vline() - Draws vertical line
  • gui_draw_rect() - Draws filled rectangle
  • gui_draw_rect_outline() - Draws rectangle outline
  • gui_blit() - Copies buffer to framebuffer

Window System (kernel/gui/window.asm)

Provides window drawing capabilities:

  • gui_draw_window() - Draws complete window (title bar, border, content)
  • gui_draw_window_title() - Draws window title bar

Window features:

  • Title bar with blue background (Windows 10 style)
  • Gray border (1 pixel)
  • Light gray background
  • Configurable position and size

Font Rendering (kernel/gui/font.asm)

Bitmap font system for text display:

  • gui_draw_char() - Draws single character
  • gui_draw_string() - Draws null-terminated string

Features:

  • 8x8 pixel bitmap font
  • ASCII character set (32-127)
  • Configurable text color

Dialog System (kernel/gui/dialog.asm)

Message boxes and dialogs:

  • gui_message_box() - Shows modal message box

Dialog types:

  • INFO - Information dialog
  • WARNING - Warning dialog
  • ERROR - Error dialog
  • QUESTION - Yes/No question dialog

UI Components (kernel/gui/components.asm)

Standard UI elements:

  • gui_draw_button() - Draws button (normal/hover/pressed states)
  • gui_draw_label() - Draws text label
  • gui_draw_input() - Draws text input field

Event System (kernel/gui/events.asm)

Handles user input events:

  • gui_process_mouse_event() - Processes mouse events
  • gui_process_keyboard_event() - Processes keyboard events
  • gui_hit_test() - Finds window at coordinates
  • gui_get_mouse_pos() - Gets current mouse position
  • gui_is_mouse_button_pressed() - Checks mouse button state

Message Loop (kernel/gui/messageloop.asm)

Windows-style message processing:

  • gui_post_message() - Posts message to queue
  • gui_get_message() - Retrieves message from queue
  • gui_message_loop() - Main message loop
  • gui_dispatch_message() - Dispatches message to window

Message types (Windows-compatible):

  • WM_PAINT (0x000F) - Window needs repainting
  • WM_MOUSEMOVE (0x0200) - Mouse moved
  • WM_LBUTTONDOWN (0x0201) - Left mouse button pressed
  • WM_LBUTTONUP (0x0202) - Left mouse button released
  • WM_KEYDOWN (0x0100) - Key pressed
  • WM_KEYUP (0x0101) - Key released
  • WM_CHAR (0x0102) - Character input
  • WM_CLOSE (0x0010) - Window close requested

Initialization (kernel/gui/gui_init.asm)

GUI subsystem initialization:

  • gui_init() - Initializes GUI subsystem
  • gui_draw_desktop() - Draws desktop background and taskbar
  • gui_start() - Starts GUI and enters message loop

Startup (kernel/gui/startup.asm)

Graphical environment startup:

  • gui_start_graphical_environment() - Main entry point for graphical boot
  • gui_show_welcome_window() - Shows welcome window on startup

Integration

The Kernel GUI System integrates with:

VESA Driver

The renderer uses VESA driver functions to access the framebuffer:

  • vesa_get_framebuffer_64() - Gets framebuffer address
  • vesa_get_width_64() - Gets screen width
  • vesa_get_height_64() - Gets screen height
  • vesa_get_bpp_64() - Gets bits per pixel
  • vesa_get_pitch_64() - Gets bytes per scanline

Kernel Initialization

The GUI system is initialized in kernel_subsystems_init_64():

call gui_init
call gui_start_graphical_environment

Node.js Integration

Once the filesystem and process execution are working, the GUI system will launch Node.js:

  • Loads /usr/lib/nodejs/bin/node from disk
  • Executes with /system/index.js as argument
  • Node.js then starts the high-level GUI framework

Rendering Context

The renderer maintains a global rendering context:

render_context:
    .framebuffer: dq 0    ; Framebuffer address
    .width: dd 0          ; Screen width in pixels
    .height: dd 0         ; Screen height in pixels
    .bpp: dd 0            ; Bits per pixel
    .pitch: dd 0          ; Bytes per scanline

Color Format

Colors are stored in ARGB format (32-bit):

  • Bits 31-24: Alpha (0xFF = opaque)
  • Bits 23-16: Red
  • Bits 15-8: Green
  • Bits 7-0: Blue

Example colors:

  • 0xFFF0F0F0 - Light gray (window background)
  • 0xFF0078D4 - Blue (title bar)
  • 0xFF808080 - Gray (border)
  • 0xFFFFFFFF - White (text)
  • 0xFF000000 - Black (text)

Build System

The GUI system is built as part of the kernel with 19 modules:

GUI_OBJS = gui/renderer.o gui/window.o gui/font.o gui/font_complete.o \
           gui/dialog.o gui/components.o gui/events.o gui/messageloop.o \
           gui/window_manager.o gui/window_controls.o gui/cursor.o \
           gui/clipping.o gui/graphics_primitives.o gui/double_buffer.o \
           gui/window_manipulation.o gui/component_interaction.o \
           gui/driver_integration.o gui/gui_init.o gui/startup.o

All modules are compiled as 64-bit ELF objects and linked into the kernel.

Double Buffering

The double buffering system provides smooth rendering by drawing to an off-screen buffer and then swapping it with the front buffer. This eliminates flickering and provides a better user experience.

  • gui_init_double_buffer() - Initializes double buffering and allocates back buffer
  • gui_get_back_buffer() - Returns pointer to back buffer for rendering
  • gui_swap_buffers() - Swaps back buffer to front buffer (displays rendered content)
  • gui_is_double_buffer_enabled() - Checks if double buffering is active
  • gui_disable_double_buffer() - Disables double buffering and frees back buffer

Window Manipulation

Window manipulation allows users to move and resize windows interactively using the mouse.

  • gui_start_window_drag() - Starts window drag operation (move)
  • gui_update_window_drag() - Updates window position during drag
  • gui_stop_window_drag() - Stops window drag operation
  • gui_is_window_dragging() - Checks if window is currently being dragged
  • gui_hit_test_resize_handle() - Detects if mouse is over a window resize handle

Windows can be moved by clicking and dragging the title bar. Resize handles are located at the four corners of windows.

Component Interaction

Component interaction makes UI elements (buttons, inputs, etc.) actually functional and responsive to user input.

  • gui_handle_button_click() - Handles button click events
  • gui_set_button_state() - Sets button visual state (normal, hover, pressed)
  • gui_set_input_focus() - Sets focus to an input field
  • gui_process_component_keyboard() - Processes keyboard input for focused components
  • gui_hit_test_component() - Detects which component is at given coordinates

Components respond to mouse clicks and keyboard input, posting appropriate Windows messages (WM_COMMAND, WM_CHAR, etc.) to their parent windows.

Window Management (kernel/gui/window_manager.asm)

Complete window management system with z-order, focus, and window procedures:

  • gui_window_manager_init() - Initializes window manager
  • gui_create_window() - Creates a new window
  • gui_get_window() - Gets window by handle
  • gui_set_focus() - Sets window focus
  • gui_bring_to_front() - Brings window to front
  • gui_destroy_window() - Destroys a window
  • gui_find_window_at() - Finds window at coordinates
  • gui_draw_all_windows() - Draws all visible windows

Window Controls (kernel/gui/window_controls.asm)

Window control buttons (close, minimize, maximize):

  • gui_draw_window_controls() - Draws window control buttons
  • gui_hit_test_window_controls() - Detects which control button was clicked

Cursor Rendering (kernel/gui/cursor.asm)

Mouse cursor display system:

  • gui_draw_cursor() - Draws mouse cursor at position
  • gui_update_cursor() - Updates cursor position
  • gui_show_cursor() - Shows cursor
  • gui_hide_cursor() - Hides cursor
  • gui_get_cursor_pos() - Gets current cursor position

Clipping (kernel/gui/clipping.asm)

Clipping regions for window content:

  • gui_set_clip_region() - Sets clipping region
  • gui_clear_clip_region() - Clears clipping region
  • gui_is_point_clipped() - Checks if point is clipped
  • gui_clip_rect() - Clips rectangle to region

Graphics Primitives (kernel/gui/graphics_primitives.asm)

Additional graphics primitives:

  • gui_draw_circle() - Draws circle outline
  • gui_draw_circle_filled() - Draws filled circle
  • gui_draw_line() - Draws line using Bresenham's algorithm

Driver Integration (kernel/gui/driver_integration.asm)

Connects keyboard and mouse drivers to GUI event system:

  • gui_process_keyboard_input() - Processes keyboard input from driver
  • gui_process_mouse_input() - Processes mouse input from driver
  • gui_init_driver_integration() - Initializes driver integration

Future Enhancements

  • Transparency and alpha blending
  • Hardware acceleration support
  • Multiple graphics modes
  • Icon rendering system
  • Window animations
  • Theme support