This commit is contained in:
Alvin-Zilverstand committed 2026-10-08 23:36:34 +02:00
1 parent 78dd619293
commit 6dc841f956
109 files changed
+18192 -1907

No files matched your search

+80 -7
View File
@@ -29,7 +29,8 @@ unbuffered keys, converts Enter to newline, and treats Ctrl+D as EOF.
Ctrl+Z is an ordinary control character: shell EOF, full-screen editor undo.
Redirected input uses getchar. `bob_terminal()` (trap 9) returns 1 only when
both input/output are native Windows console handles; otherwise 0. Shell
history, completion and editing are guest code in `kernel/input.c`. ANSI key
history, completion and editing are guest code in `kernel/input.c`; the shell's
`history` command lists the same four recent entries. ANSI key
sequences in redirected regression fixtures exercise the same guest actions.
`bob_columns()` (trap 10) reports the console buffer width, or 80 as fallback.
The shell scrolls a long input line horizontally to keep redraw within it.
@@ -74,9 +75,10 @@ returning its slot or -1 for a bad name, length or a full table. Kind 0 is text;
kind 1 is machine code, kind 2 is resident bob32 C code, and kind 3 is a packed
native B32K v2 image. Its copy appends a zero word, which is not part of length.
Names hold at most 23 characters. bob16 retains eight fixed 511-word slots;
bob32 packs files into a shared 4,096-word content region. Text files remain
limited to 511 words; resident programs and native images can use the remaining
shared capacity (up to 4,095 words per file). Valid slot indices are 0..7.
bob32 packs files into a protected 8,192-word content region at `0x1E000`,
between the kernel and supervised-app regions. Text files remain limited to 511
words; resident programs and native images can use the remaining shared capacity
(up to 8,191 words per file). Valid slot indices are 0..7.
These are internal kernel APIs.
Failed size/name validation does not overwrite an existing file. Edits start
@@ -131,16 +133,87 @@ kernel code; ordinary shell programs use bob_run for recovery and write checks.
See [the OS guide](../C_OS.md) for commands, examples and C subset limits.
In bob32 mode, `import32 HOST_PATH NAME` validates and packs a B32K v2 image
into a guest RAM file; `run NAME` loads and executes it. B32S version 2
snapshots preserve packed files; version 1 fixed-slot snapshots remain readable
and are converted to the packed layout on restore. `run32 HOST_PATH` remains
into a guest RAM file; `run NAME` loads and executes it. B32S version 3
snapshots preserve the expanded filesystem; versions 1 and 2 remain readable
and are migrated to the current storage layout on restore. `run32 HOST_PATH` remains
available for direct host-path launches.
Native C applications can include `bob_string.h` for small app-side string
and parsing helpers. It provides bounded integer formatting, status-returning
decimal/hex parsing, and in-place quoted tokenization without depending on the
kernel's private runtime functions.
`bob_native.h` is an optional convenience include for the common native API
surface: core `bob.h` calls, strings, app-managed memory, filesystem, process,
time, and input events. Graphics and GUI remain opt-in via `bob_gfx.h` and
`bob_gui.h` so text-only apps do not pull in widget code.
`bob_memory.h` provides a caller-owned word arena. Keep an integer buffer and a
used-word counter, request word counts with `bob_arena_alloc`, inspect
used/remaining capacity, and reset the counter when its allocations are no longer
needed. Exhaustion and invalid sizes return null; no OS-global heap is reserved.
Native apps may define `main(int argc, char **argv)`; when launched by guest
filename, `argv[0]` is that filename and `argc` includes it. `run app first
"two words"` groups quoted words; backslash escapes the following character
and `""` supplies an empty argument.
`bob_fs.h` exposes the version 1 native-app filesystem service through
`bob_file_list`, `bob_file_read`, `bob_file_write`, `bob_file_delete`,
`bob_file_read_words`, `bob_file_write_words`, and `bob_file_kind`. Listing
returns kind and word-size metadata; `bob_file_kind` queries the kind directly
by name. Text reads/writes handle up to 511 characters. Word-file
helpers preserve binary values for bob16 and bob32 program kinds. Native B32K
images can be read as words, but must use the validated `import32` path to enter
the filesystem as runnable apps. Applications pass buffers in their own memory,
while the emulator validates and copies data to keep kernel tables private.
Files remain in the guest RAM filesystem and follow the normal B32S save/restore
flow. B32S v3 stores the expanded payload; v1 fixed-slot and v2 packed snapshots
remain readable.
`bob_system_info` reports the service version, app address-space size, mapped
pages, and filesystem slot/word usage through the same versioned boundary.
`bob_process.h` adds `bob_app_run(name, arguments, &exit_code)` through version 1
service operation 6. It launches a stored kind-3 B32K image with shell-style
quoted arguments, preserves the suspended caller's sparse application memory,
and separates launch errors from the child's exit code. Nested launches are
limited to eight active app frames. The API returns -1 when the app name is
missing, -2 for a non-native file, -4 for invalid data, and -5 when memory is
unavailable. The `launcher` app lists stored native images and can run one with
`run launcher APP [ARG ...]`.
`bob_time.h` exposes version 1 time operation 7. `bob_time_utc_seconds(words)`
fills two 32-bit words with the low and high halves of UTC seconds since the
Unix epoch and returns 2. It returns a negative status for an invalid request.
`bob_gfx.h` uses that OS boundary for an 80x25 cell framebuffer: enter/query,
clear, pixel, filled rectangle, line, text, transparent bitmap blit, present,
and leave. Each cell carries a 16-color foreground. Draw calls update a back
buffer; present renders it, and supervised app exit closes an unclosed canvas.
`bob_gfx_blit_color` accepts transparent row-major cells with an ASCII character
in bits 0..7 and a foreground color in bits 8..11; invalid cell values reject
the whole call before drawing.
`bob_font.h` adds an opt-in 5x7 bitmap font helper. `bob_gfx_bitmap_text`
rasterizes uppercase letters, digits, space, and common punctuation into the
canvas via the colored bitmap service; lowercase letters map to uppercase and
unsupported characters use `?`.
`bob_event.h` provides `bob_event_wait` and nonblocking `bob_event_poll` with
four-word events. Poll returns 1 for an event or 0 when the console queue is
empty; redirected input is left for the blocking wait and is not peeked.
Version 1 reports typed characters, special-key presses, key releases, mouse
movement and mouse button transitions when the Windows console provides those
records. Mouse coordinates are console-cell coordinates; redirected input
remains character based.
`bob_event_queue.h` adds an app-owned FIFO for those same four-word events.
Initialize a two-word `{head,count}` state, allocate four storage words per
queue slot, then use `bob_event_queue_push`/`pop`; push returns 0 when full
without overwriting older events, and pop returns 0 when empty. Invalid
capacity/state returns -1. The queue is optional and requires no OS memory.
`bob_wm.h` adds a small reusable window manager over caller-provided fixed
arrays. Initialize it with a window limit and screen size, then use create,
destroy, focus, raise, move, show/hide, and invalidate operations. Dispatch
returns a recipient ID, routes keys to the focused visible window, raises the
topmost clicked window, captures title-bar drags, and supplies window-local
mouse coordinates. `bob_wm_begin_draw` starts a redraw pass and repeated
`bob_wm_next_dirty` calls yield visible windows bottom-to-top; geometry and
stacking changes request a full redraw, while invalidation also marks any
overlapping windows so they repaint correctly. The manager stores no app-specific
titles or callbacks; applications keep their own state and draw their own
contents through `bob_gui.h` and `bob_gfx.h`.
+2
View File
@@ -10,6 +10,8 @@ int bob_run(int entry);
int bob_run_image(const char *path);
int bob_import_image(int *descriptor);
int bob_run_native(const int *descriptor);
int bob_os_service(int *request);
int bob_fs_register(int *descriptor);
int bob_snapshot(int *descriptor, int operation);
int bob_key(void);
int bob_terminal(void);
+37
View File
@@ -0,0 +1,37 @@
#ifndef BOB_EVENT_H
#define BOB_EVENT_H
#include "bob.h"
#define BOB_EVENT_API_VERSION 1
#define BOB_EVENT_KEY_DOWN 1
#define BOB_EVENT_CHAR 2
#define BOB_EVENT_KEY_UP 3
#define BOB_EVENT_MOUSE_MOVE 4
#define BOB_EVENT_MOUSE_BUTTON_DOWN 5
#define BOB_EVENT_MOUSE_BUTTON_UP 6
#define BOB_EVENT_MOUSE_WHEEL 7
#define BOB_EVENT_POLL 28
#define BOB_MOUSE_BUTTON_LEFT 1
#define BOB_MOUSE_BUTTON_RIGHT 2
#define BOB_MOUSE_BUTTON_MIDDLE 4
/* Four words: type, key/character/buttons/wheel delta, x, y. Blocks until input or EOF. */
int bob_event_wait(int *event) {
int request[7];
request[0]=(BOB_EVENT_API_VERSION << 16) | 26;
request[1]=(int)event;request[2]=4;
request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
/* Returns 1 with an event, 0 when no console event is available, negative on bad input. */
int bob_event_poll(int *event) {
int request[7];
request[0]=(BOB_EVENT_API_VERSION << 16) | BOB_EVENT_POLL;
request[1]=(int)event;request[2]=4;
request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
#endif
+36
View File
@@ -0,0 +1,36 @@
#ifndef BOB_EVENT_QUEUE_H
#define BOB_EVENT_QUEUE_H
#include "bob_event.h"
/* Caller-owned FIFO state is head,count; storage holds capacity four-word events. */
void bob_event_queue_init(int *state) {
if(!state)return;
state[0]=0;state[1]=0;
}
/* Push returns 1 on success, 0 if full, and -1 for invalid state or arguments. */
int bob_event_queue_push(int *state,int *storage,int capacity,const int *event) {
int tail;
if(!state||!storage||!event||capacity<1||state[0]<0||state[0]>=capacity||
state[1]<0||state[1]>capacity)return -1;
if(state[1]==capacity)return 0;
tail=(state[0]+state[1])%capacity;
for(int i=0;i<4;i++)storage[tail*4+i]=event[i];
state[1]++;
return 1;
}
/* Pop returns 1 with an event, 0 when empty, and -1 for invalid state or arguments. */
int bob_event_queue_pop(int *state,int *storage,int capacity,int *event) {
int head;
if(!state||!storage||!event||capacity<1||state[0]<0||state[0]>=capacity||
state[1]<0||state[1]>capacity)return -1;
if(!state[1])return 0;
head=state[0];
for(int i=0;i<4;i++)event[i]=storage[head*4+i];
state[0]=(head+1)%capacity;state[1]--;
return 1;
}
#endif
+59
View File
@@ -0,0 +1,59 @@
#ifndef BOB_FONT_H
#define BOB_FONT_H
#include "bob_gfx.h"
/* 5x7 column bitmaps: blank, A-Z, 0-9, ?, !, -, .; lowercase maps to uppercase. */
static int bob_font5x7[41][5]={
{0,0,0,0,0},
{0x7e,0x11,0x11,0x11,0x7e},{0x7f,0x49,0x49,0x49,0x36},
{0x3e,0x41,0x41,0x41,0x22},{0x7f,0x41,0x41,0x22,0x1c},
{0x7f,0x49,0x49,0x49,0x41},{0x7f,0x09,0x09,0x09,0x01},
{0x3e,0x41,0x49,0x49,0x7a},{0x7f,0x08,0x08,0x08,0x7f},
{0x00,0x41,0x7f,0x41,0x00},{0x20,0x40,0x41,0x3f,0x01},
{0x7f,0x08,0x14,0x22,0x41},{0x7f,0x40,0x40,0x40,0x40},
{0x7f,0x02,0x0c,0x02,0x7f},{0x7f,0x04,0x08,0x10,0x7f},
{0x3e,0x41,0x41,0x41,0x3e},{0x7f,0x09,0x09,0x09,0x06},
{0x3e,0x41,0x51,0x21,0x5e},{0x7f,0x09,0x19,0x29,0x46},
{0x46,0x49,0x49,0x49,0x31},{0x01,0x01,0x7f,0x01,0x01},
{0x3f,0x40,0x40,0x40,0x3f},{0x1f,0x20,0x40,0x20,0x1f},
{0x3f,0x40,0x38,0x40,0x3f},{0x63,0x14,0x08,0x14,0x63},
{0x07,0x08,0x70,0x08,0x07},{0x61,0x51,0x49,0x45,0x43},
{0x3e,0x51,0x49,0x45,0x3e},{0x00,0x42,0x7f,0x40,0x00},
{0x62,0x51,0x49,0x49,0x46},{0x22,0x41,0x49,0x49,0x36},
{0x18,0x14,0x12,0x7f,0x10},{0x2f,0x49,0x49,0x49,0x31},
{0x3e,0x49,0x49,0x49,0x32},{0x01,0x71,0x09,0x05,0x03},
{0x36,0x49,0x49,0x49,0x36},{0x26,0x49,0x49,0x49,0x3e},
{0x02,0x01,0x51,0x09,0x06},{0x00,0x00,0x5f,0x00,0x00},
{0x08,0x08,0x08,0x08,0x08},{0x00,0x60,0x60,0x00,0x00}
};
/* Draws uppercase 5x7 glyphs at cell-pixel resolution; clips at the right edge. */
int bob_gfx_bitmap_text(int x,int y,const char *text,int color) {
int bitmap[560];
int length=0,width,max_chars,character,glyph,cell;
if(x<0||y<0||x>=80||y+7>25||color<0||color>15)return -1;
max_chars=(80-x+1)/6;
while(text[length]&&length<max_chars)length++;
if(!length)return 0;
width=length*6-1;
for(int i=0;i<width*7;i++)bitmap[i]=0;
for(int i=0;i<length;i++) {
character=text[i];
if(character>='a'&&character<='z')character-=32;
if(character==' ')glyph=0;
else if(character>='A'&&character<='Z')glyph=1+character-'A';
else if(character>='0'&&character<='9')glyph=27+character-'0';
else if(character=='!')glyph=38;
else if(character=='-')glyph=39;
else if(character=='.')glyph=40;
else glyph=37;
for(int column=0;column<5;column++)for(int row=0;row<7;row++) {
cell=(bob_font5x7[glyph][column]&(1<<row))?('#'|(color<<8)):0;
bitmap[row*width+i*6+column]=cell;
}
}
return bob_gfx_blit_color(x,y,width,7,bitmap);
}
#endif
+88
View File
@@ -0,0 +1,88 @@
#ifndef BOB_FS_H
#define BOB_FS_H
#include "bob.h"
/* Stable request protocol used by the bob32 filesystem service (trap 15). */
#define BOB_FS_LIST 1
#define BOB_FS_READ 2
#define BOB_FS_WRITE 3
#define BOB_FS_DELETE 4
#define BOB_FS_SYSTEM_INFO 5
#define BOB_FS_RUN_APP 6
#define BOB_FS_WRITE_WORDS 8
#define BOB_FS_READ_WORDS 9
#define BOB_FS_KIND 10
#define BOB_FS_API_VERSION 1
#define BOB_FS_REQUEST(operation) ((BOB_FS_API_VERSION << 16) | (operation))
#define BOB_FILE_KIND_TEXT 0
#define BOB_FILE_KIND_BOB16_PROGRAM 1
#define BOB_FILE_KIND_BOB32_PROGRAM 2
#define BOB_FILE_KIND_NATIVE_APP 3
/* LIST returns the file kind (0..3), or -1 at end. length receives word size. */
int bob_file_list(int index, char *name, int capacity, int *length) {
int request[7];
request[0]=BOB_FS_REQUEST(BOB_FS_LIST);request[1]=index;request[2]=(int)name;
request[3]=capacity;request[4]=(int)length;request[5]=0;request[6]=0;
return bob_os_service(request)-1;
}
/* READ returns text length, or a negative value for missing/invalid input. */
int bob_file_read(const char *name, char *buffer, int capacity) {
int request[7];
request[0]=BOB_FS_REQUEST(BOB_FS_READ);request[1]=(int)name;request[2]=(int)buffer;
request[3]=capacity;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
/* WRITE creates/replaces a text file. Length is measured in characters. */
int bob_file_write(const char *name, const char *text, int length) {
int request[7];
request[0]=BOB_FS_REQUEST(BOB_FS_WRITE);request[1]=(int)name;request[2]=(int)text;
request[3]=length;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
/* Store raw bob16/bob32 program words (kinds 1/2); native images require import. */
int bob_file_write_words(const char *name, int *words, int length, int kind) {
int request[7];
request[0]=BOB_FS_REQUEST(BOB_FS_WRITE_WORDS);request[1]=(int)name;
request[2]=(int)words;request[3]=length;request[4]=kind;
request[5]=0;request[6]=0;
return bob_os_service(request);
}
/* Read raw program/native image words into an application-owned buffer. */
int bob_file_read_words(const char *name, int *words, int capacity) {
int request[7];
request[0]=BOB_FS_REQUEST(BOB_FS_READ_WORDS);request[1]=(int)name;
request[2]=(int)words;request[3]=capacity;request[4]=0;
request[5]=0;request[6]=0;
return bob_os_service(request);
}
/* Return a file kind (0..3) by name, or -1 when it does not exist. */
int bob_file_kind(const char *name) {
int request[7];
request[0]=BOB_FS_REQUEST(BOB_FS_KIND);request[1]=(int)name;
request[2]=0;request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
int bob_file_delete(const char *name) {
int request[7];
request[0]=BOB_FS_REQUEST(BOB_FS_DELETE);request[1]=(int)name;request[2]=0;
request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
/* Fills version, app-address-space size, mapped pages and filesystem usage. */
int bob_system_info(int *values, int capacity) {
int request[7];
request[0]=BOB_FS_REQUEST(BOB_FS_SYSTEM_INFO);request[1]=(int)values;
request[2]=capacity;request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
#endif
+89
View File
@@ -0,0 +1,89 @@
#ifndef BOB_GFX_H
#define BOB_GFX_H
#include "bob.h"
/* Version 1 is an 80x25 character-cell canvas; one cell is one pixel. */
#define BOB_GFX_VERSION 1
#define BOB_GFX_REQUEST(operation) ((BOB_GFX_VERSION << 16) | (operation))
int bob_gfx_enter(void) {
int request[7];
request[0]=BOB_GFX_REQUEST(16);
request[1]=0;request[2]=0;request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
int bob_gfx_resolution(int *values) {
int request[7];
request[0]=BOB_GFX_REQUEST(17);request[1]=(int)values;request[2]=2;
request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
int bob_gfx_pixel(int x,int y,int character,int color) {
int request[7];
request[0]=BOB_GFX_REQUEST(18);request[1]=x;request[2]=y;
request[3]=character;request[4]=color;request[5]=0;request[6]=0;
return bob_os_service(request);
}
int bob_gfx_fill_rect(int x,int y,int width,int height,int character,int color) {
int request[7];
request[0]=BOB_GFX_REQUEST(19);request[1]=x;request[2]=y;
request[3]=width;request[4]=height;request[5]=character;request[6]=color;
return bob_os_service(request);
}
int bob_gfx_line(int x0,int y0,int x1,int y1,int character,int color) {
int request[7];
request[0]=BOB_GFX_REQUEST(20);request[1]=x0;request[2]=y0;
request[3]=x1;request[4]=y1;request[5]=character;request[6]=color;
return bob_os_service(request);
}
int bob_gfx_text(int x,int y,const char *text,int color) {
int request[7];
request[0]=BOB_GFX_REQUEST(21);request[1]=x;request[2]=y;
request[3]=(int)text;request[4]=color;request[5]=0;request[6]=0;
return bob_os_service(request);
}
/* Bitmap cells are zero-transparent ASCII characters, row-major. */
int bob_gfx_blit(int x,int y,int width,int height,const char *bitmap,int color) {
int request[7];
request[0]=BOB_GFX_REQUEST(22);request[1]=x;request[2]=y;
request[3]=width;request[4]=height;request[5]=(int)bitmap;request[6]=color;
return bob_os_service(request);
}
/* Each word stores character in bits 0..7 and color in bits 8..11; zero is transparent. */
int bob_gfx_blit_color(int x,int y,int width,int height,const int *bitmap) {
int request[7];
request[0]=BOB_GFX_REQUEST(27);request[1]=x;request[2]=y;
request[3]=width;request[4]=height;request[5]=(int)bitmap;request[6]=0;
return bob_os_service(request);
}
int bob_gfx_clear(int character,int color) {
int request[7];
request[0]=BOB_GFX_REQUEST(23);request[1]=character;request[2]=color;
request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
int bob_gfx_present(void) {
int request[7];
request[0]=BOB_GFX_REQUEST(24);
request[1]=0;request[2]=0;request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
int bob_gfx_leave(void) {
int request[7];
request[0]=BOB_GFX_REQUEST(25);
request[1]=0;request[2]=0;request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
#endif
+352
View File
@@ -0,0 +1,352 @@
#ifndef BOB_GUI_H
#define BOB_GUI_H
#include "bob_gfx.h"
#include "bob_event.h"
#include "bob_wm.h"
/* Rectangles are x, y, width, height; drag state is active, offset-x, offset-y. */
int bob_gui_window_hit(int x,int y,const int *rect) {
return x>=rect[0]&&y>=rect[1]&&x<rect[0]+rect[2]&&y<rect[1]+rect[3];
}
/* Rectangles are packed x,y,width,height; order is bottom-to-top window IDs. */
int bob_gui_window_at(int x,int y,const int *rectangles,const int *order,int count) {
for(int i=count-1;i>=0;i--) {
int id=order[i];
if(id>=0&&id<count&&bob_gui_window_hit(x,y,rectangles+id*4))return id;
}
return -1;
}
int bob_gui_raise_window(int *order,int count,int id) {
int index=-1;
for(int i=0;i<count;i++)if(order[i]==id){index=i;break;}
if(index<0)return 0;
for(int i=index;i<count-1;i++)order[i]=order[i+1];
order[count-1]=id;return 1;
}
int bob_gui_drag_start(const int *rect,int *drag,int x,int y) {
if(x<rect[0]||x>=rect[0]+rect[2]||y!=rect[1])return 0;
drag[0]=1;drag[1]=x-rect[0];drag[2]=y-rect[1];return 1;
}
int bob_gui_drag_update(int *rect,int *drag,int x,int y,int screen_width,int screen_height) {
int max_x,max_y;
if(!drag[0])return 0;
max_x=screen_width-rect[2];max_y=screen_height-rect[3];
if(max_x<0)max_x=0;if(max_y<0)max_y=0;
rect[0]=x-drag[1];rect[1]=y-drag[2];
if(rect[0]<0)rect[0]=0;if(rect[0]>max_x)rect[0]=max_x;
if(rect[1]<0)rect[1]=0;if(rect[1]>max_y)rect[1]=max_y;
return 1;
}
int bob_gui_drag_stop(int *drag) {
int was_dragging=drag[0];drag[0]=0;return was_dragging;
}
/* Button state is hovered, pressed; returns one only for an inside release. */
int bob_gui_button_event(const int *rect,int *state,const int *event) {
int inside=bob_gui_window_hit(event[2],event[3],rect);
if(event[0]==BOB_EVENT_MOUSE_MOVE)state[0]=inside;
else if(event[0]==BOB_EVENT_MOUSE_BUTTON_DOWN) {
state[0]=inside;state[1]=inside&&((event[1]&BOB_MOUSE_BUTTON_LEFT)!=0);
} else if(event[0]==BOB_EVENT_MOUSE_BUTTON_UP) {
state[0]=inside;
if(state[1]&&inside&&!(event[1]&BOB_MOUSE_BUTTON_LEFT)){state[1]=0;return 1;}
state[1]=0;
}
return 0;
}
int bob_gui_button_draw(int x,int y,int width,int height,const char *label,
int hovered,int pressed) {
int color=pressed?14:(hovered?11:7);
int label_length=0,label_start=0,label_x,label_width;
if(width<3||height<3)return -1;
while(label[label_length])label_length++;
bob_gfx_fill_rect(x,y,width,height,' ',color);
bob_gfx_line(x+1,y,x+width-2,y,pressed?'=':'-',color);
bob_gfx_line(x+1,y+height-1,x+width-2,y+height-1,pressed?'=':'-',color);
bob_gfx_line(x,y+1,x,y+height-2,'|',color);
bob_gfx_line(x+width-1,y+1,x+width-1,y+height-2,'|',color);
bob_gfx_pixel(x,y,'+',color);bob_gfx_pixel(x+width-1,y,'+',color);
bob_gfx_pixel(x,y+height-1,'+',color);bob_gfx_pixel(x+width-1,y+height-1,'+',color);
label_width=width-2;
if(label_length>label_width){label_start=(label_length-label_width)/2;label_length=label_width;}
label_x=x+1+(label_width-label_length)/2;
for(int i=0;i<label_length;i++)bob_gfx_pixel(label_x+i,y+height/2,label[label_start+i],15);
return 0;
}
/* Draws a one-row-per-item popup; items is a fixed-stride label array. */
int bob_gui_menu_draw(const int *rect,const char *items,int count,int stride,int selected) {
int label_width=rect[2]-4;
char label[80];
if(count<1||stride<1||rect[2]<6||rect[2]>80||rect[3]!=count+2||
rect[0]<0||rect[1]<0||rect[0]+rect[2]>80||rect[1]+rect[3]>25)return -1;
bob_gfx_fill_rect(rect[0],rect[1],rect[2],rect[3],' ',7);
bob_gfx_line(rect[0]+1,rect[1],rect[0]+rect[2]-2,rect[1],'-',7);
bob_gfx_line(rect[0]+1,rect[1]+rect[3]-1,rect[0]+rect[2]-2,rect[1]+rect[3]-1,'-',7);
bob_gfx_line(rect[0],rect[1]+1,rect[0],rect[1]+rect[3]-2,'|',7);
bob_gfx_line(rect[0]+rect[2]-1,rect[1]+1,rect[0]+rect[2]-1,rect[1]+rect[3]-2,'|',7);
bob_gfx_pixel(rect[0],rect[1],'+',7);bob_gfx_pixel(rect[0]+rect[2]-1,rect[1],'+',7);
bob_gfx_pixel(rect[0],rect[1]+rect[3]-1,'+',7);bob_gfx_pixel(rect[0]+rect[2]-1,rect[1]+rect[3]-1,'+',7);
for(int row=0;row<count;row++) {
int color=row==selected?4:7;int length=0;const char *source=items+row*stride;
bob_gfx_fill_rect(rect[0]+1,rect[1]+1+row,rect[2]-2,1,' ',color);
bob_gfx_pixel(rect[0]+1,rect[1]+1+row,row==selected?'>':' ',color);
while(length<label_width&&length<stride&&source[length]){label[length]=source[length];length++;}
label[length]=0;bob_gfx_text(rect[0]+2,rect[1]+1+row,label,row==selected?15:8);
}
return 0;
}
/* Places a popup below its anchor and clamps it inside the screen rectangle. */
int bob_gui_menu_place(int *rect,int anchor_x,int anchor_y,int width,int count,
int screen_width,int screen_height) {
int height=count+2;
if(width<6||count<1||screen_width<width||screen_height<height)return -1;
rect[0]=anchor_x;rect[1]=anchor_y;rect[2]=width;rect[3]=height;
if(rect[0]+width>screen_width)rect[0]=screen_width-width;
if(rect[0]<0)rect[0]=0;
if(rect[1]+height>screen_height)rect[1]=screen_height-height;
if(rect[1]<0)rect[1]=0;
return 0;
}
/* Returns the activated row, -1 for no action, or -2 when Escape closes it. */
int bob_gui_menu_event(const int *rect,int count,int *state,const int *event) {
int row=-1;
if(count<1||rect[3]!=count+2)return -1;
if(event[2]>=rect[0]+1&&event[2]<rect[0]+rect[2]-1&&
event[3]>=rect[1]+1&&event[3]<rect[1]+1+count)row=event[3]-rect[1]-1;
if(event[0]==BOB_EVENT_MOUSE_MOVE)state[0]=row;
else if(event[0]==BOB_EVENT_MOUSE_BUTTON_DOWN) {
state[0]=row;state[1]=row>=0&&(event[1]&BOB_MOUSE_BUTTON_LEFT)?row:-1;
} else if(event[0]==BOB_EVENT_MOUSE_BUTTON_UP) {
int activated=row>=0&&state[1]==row&&!(event[1]&BOB_MOUSE_BUTTON_LEFT)?row:-1;
state[0]=row;state[1]=-1;return activated;
} else if(event[0]==BOB_EVENT_KEY_DOWN) {
if(event[1]==256)state[0]=state[0]<=0?count-1:state[0]-1;
else if(event[1]==257)state[0]=state[0<0||state[0]>=count-1?0:state[0]+1];
} else if(event[0]==BOB_EVENT_CHAR) {
if(event[1]==27)return -2;
if((event[1]==10||event[1]==13)&&state[0]>=0&&state[0]<count)return state[0];
}
return -1;
}
/* Text state is focused, caret index, horizontal scroll offset. */
int bob_gui_text_field_init(char *buffer,int *state,int capacity) {
int length=0;
if(capacity<1)return -1;
while(length<capacity-1&&buffer[length])length++;
state[0]=0;state[1]=length;state[2]=0;return length;
}
int bob_gui_text_field_event(const int *rect,char *buffer,int *state,int capacity,const int *event) {
int length=0,visible,changed=0;
while(length<capacity-1&&buffer[length])length++;
visible=rect[2]-2;
if(visible<1||capacity<1)return 0;
if(event[0]==BOB_EVENT_MOUSE_BUTTON_DOWN) {
state[0]=bob_gui_window_hit(event[2],event[3],rect);
if(state[0]) {
state[1]=state[2]+event[2]-rect[0]-1;
if(state[1]<0)state[1]=0;if(state[1]>length)state[1]=length;
}
} else if(state[0]&&event[0]==BOB_EVENT_CHAR) {
if(event[1]>=32&&event[1]<127&&length<capacity-1) {
for(int i=length;i>state[1];i--)buffer[i]=buffer[i-1];
buffer[state[1]++]=event[1];buffer[++length]=0;changed=1;
} else if((event[1]==8||event[1]==127)&&state[1]>0) {
for(int i=state[1]-1;i<length;i++)buffer[i]=buffer[i+1];
state[1]--;length--;changed=1;
}
} else if(state[0]&&event[0]==BOB_EVENT_KEY_DOWN) {
if(event[1]==258&&state[1]>0)state[1]--;
else if(event[1]==259&&state[1]<length)state[1]++;
else if(event[1]==260)state[1]=0;
else if(event[1]==261)state[1]=length;
else if(event[1]==262&&state[1]<length) {
for(int i=state[1];i<length;i++)buffer[i]=buffer[i+1];
length--;changed=1;
}
}
if(state[1]<state[2])state[2]=state[1];
if(state[1]>=state[2]+visible)state[2]=state[1]-visible+1;
return changed;
}
int bob_gui_text_field_draw(const int *rect,const char *buffer,const int *state) {
int visible=rect[2]-2;
char shown[80];
int i=0;
if(rect[2]<3||rect[2]>80||rect[3]<3)return -1;
bob_gfx_fill_rect(rect[0],rect[1],rect[2],rect[3],' ',state[0]?15:8);
bob_gfx_line(rect[0]+1,rect[1],rect[0]+rect[2]-2,rect[1],'-',state[0]?15:8);
bob_gfx_line(rect[0]+1,rect[1]+rect[3]-1,rect[0]+rect[2]-2,rect[1]+rect[3]-1,'-',state[0]?15:8);
bob_gfx_line(rect[0],rect[1]+1,rect[0],rect[1]+rect[3]-2,'|',state[0]?15:8);
bob_gfx_line(rect[0]+rect[2]-1,rect[1]+1,rect[0]+rect[2]-1,rect[1]+rect[3]-2,'|',state[0]?15:8);
for(i=0;i<visible&&buffer[state[2]+i];i++)shown[i]=buffer[state[2]+i];
shown[i]=0;bob_gfx_text(rect[0]+1,rect[1]+1,shown,15);
if(state[0]&&state[1]-state[2]>=0&&state[1]-state[2]<visible)
bob_gfx_pixel(rect[0]+1+state[1]-state[2],rect[1]+1,'|',14);
return 0;
}
/* Text-area state is focused, byte cursor, first visible line, reserved. */
int bob_gui_text_area_init(char *buffer,int *state,int capacity) {
int length=0;
if(capacity<1)return -1;
while(length<capacity-1&&buffer[length]) {
length++;
}
state[0]=0;state[1]=length;state[2]=0;state[3]=0;
return length;
}
static int bob_gui_text_area_insert(char *buffer,int *length,int *cursor,int capacity,char value) {
if(*length>=capacity-1)return 0;
for(int i=*length;i>*cursor;i--)buffer[i]=buffer[i-1];
buffer[*cursor]=value;(*cursor)++;(*length)++;buffer[*length]=0;
return 1;
}
static void bob_gui_text_area_position(const char *buffer,int cursor,int *line,int *column) {
*line=0;*column=0;
for(int i=0;i<cursor;i++) {
if(buffer[i]=='\n'){(*line)++;*column=0;}
else (*column)++;
}
}
static void bob_gui_text_area_scroll(const char *buffer,int cursor,int *state,const int *rect) {
int line,column,visible=rect[3]-2;
bob_gui_text_area_position(buffer,cursor,&line,&column);
if(line<state[2])state[2]=line;
if(line>=state[2]+visible)state[2]=line-visible+1;
}
int bob_gui_text_area_reveal(const char *buffer,int *state,const int *rect) {
bob_gui_text_area_scroll(buffer,state[1],state,rect);
return 0;
}
int bob_gui_text_area_event(const int *rect,char *buffer,int *state,int capacity,const int *event) {
int length=0,changed=0,line,column,start,end,next_start,next_end,target,visible,total_lines,max_scroll,old_scroll;
while(length<capacity-1&&buffer[length])length++;
if(capacity<1||rect[2]<3||rect[3]<3)return 0;
if(event[0]==BOB_EVENT_MOUSE_WHEEL&&bob_gui_window_hit(event[2],event[3],rect)) {
state[0]=1;old_scroll=state[2];visible=rect[3]-2;total_lines=1;
for(int i=0;i<length;i++)if(buffer[i]=='\n')total_lines++;
max_scroll=total_lines-visible;if(max_scroll<0)max_scroll=0;
if(event[1]>0&&state[2]>0)state[2]--;
else if(event[1]<0&&state[2]<max_scroll)state[2]++;
return state[2]!=old_scroll;
} else if(event[0]==BOB_EVENT_MOUSE_BUTTON_DOWN) {
state[0]=bob_gui_window_hit(event[2],event[3],rect);
if(state[0]) {
target=state[2]+event[3]-rect[1]-1;
column=event[2]-rect[0]-1;
start=0;line=0;
while(start<length&&line<target)if(buffer[start++]=='\n')line++;
while(start<length&&buffer[start]!='\n'&&column>0){start++;column--;}
state[1]=start;
}
} else if(state[0]&&event[0]==BOB_EVENT_CHAR) {
if(event[1]==8||event[1]==127) {
if(state[1]>0) {
for(int i=state[1]-1;i<length;i++)buffer[i]=buffer[i+1];
state[1]--;length--;changed=1;
}
} else if(event[1]==10||event[1]==13) {
changed=bob_gui_text_area_insert(buffer,&length,&state[1],capacity,'\n');
} else if(event[1]>=32&&event[1]<127) {
changed=bob_gui_text_area_insert(buffer,&length,&state[1],capacity,(char)event[1]);
}
} else if(state[0]&&event[0]==BOB_EVENT_KEY_DOWN) {
bob_gui_text_area_position(buffer,state[1],&line,&column);
if(event[1]==258&&state[1]>0)state[1]--;
else if(event[1]==259&&state[1]<length)state[1]++;
else if(event[1]==260) {
start=state[1];while(start>0&&buffer[start-1]!='\n')start--;state[1]=start;
} else if(event[1]==261) {
end=state[1];while(end<length&&buffer[end]!='\n')end++;state[1]=end;
} else if(event[1]==262&&state[1]<length) {
for(int i=state[1];i<length;i++)buffer[i]=buffer[i+1];
length--;changed=1;
} else if(event[1]==256||event[1]==257) {
start=state[1];while(start>0&&buffer[start-1]!='\n')start--;
end=state[1];while(end<length&&buffer[end]!='\n')end++;
if(event[1]==256&&start>0) {
next_end=start-1;next_start=next_end;
while(next_start>0&&buffer[next_start-1]!='\n')next_start--;
target=next_start+column;if(target>next_end)target=next_end;state[1]=target;
} else if(event[1]==257&&end<length) {
next_start=end+1;next_end=next_start;
while(next_end<length&&buffer[next_end]!='\n')next_end++;
target=next_start+column;if(target>next_end)target=next_end;state[1]=target;
}
}
}
bob_gui_text_area_scroll(buffer,state[1],state,rect);
return changed;
}
int bob_gui_text_area_draw(const int *rect,const char *buffer,const int *state) {
int width=rect[2]-2,height=rect[3]-2,length=0,line=0,column=0;
int position=0,row,row_start,row_end,offset,shown_length,cursor_line,cursor_column;
char shown[80];
if(rect[2]<3||rect[2]>80||rect[3]<3)return -1;
while(buffer[length])length++;
bob_gfx_fill_rect(rect[0],rect[1],rect[2],rect[3],' ',state[0]?15:8);
bob_gfx_line(rect[0]+1,rect[1],rect[0]+rect[2]-2,rect[1],'-',state[0]?15:8);
bob_gfx_line(rect[0]+1,rect[1]+rect[3]-1,rect[0]+rect[2]-2,rect[1]+rect[3]-1,'-',state[0]?15:8);
bob_gfx_line(rect[0],rect[1]+1,rect[0],rect[1]+rect[3]-2,'|',state[0]?15:8);
bob_gfx_line(rect[0]+rect[2]-1,rect[1]+1,rect[0]+rect[2]-1,rect[1]+rect[3]-2,'|',state[0]?15:8);
bob_gui_text_area_position(buffer,state[1],&cursor_line,&cursor_column);
while(position<length&&line<state[2])if(buffer[position++]=='\n')line++;
for(row=0;row<height&&position<=length;row++) {
row_start=position;row_end=position;
while(row_end<length&&buffer[row_end]!='\n')row_end++;
offset=0;
if(line==cursor_line&&cursor_column>=width)offset=cursor_column-width+1;
shown_length=0;
while(shown_length<width&&row_start+offset+shown_length<row_end) {
shown[shown_length]=buffer[row_start+offset+shown_length];shown_length++;
}
shown[shown_length]=0;
bob_gfx_text(rect[0]+1,rect[1]+1+row,shown,15);
if(state[0]&&line==cursor_line&&cursor_column>=offset&&cursor_column-offset<width)
bob_gfx_pixel(rect[0]+1+cursor_column-offset,rect[1]+1+row,'|',14);
if(row_end>=length)break;
position=row_end+1;line++;
}
return 0;
}
/* Reusable character-cell window frame and content area. */
int bob_gui_window(int x,int y,int width,int height,const char *title,
const char *body,const char *message,int focused) {
int edge=focused?14:8;
int title_color=focused?15:7;
if(width<4||height<4)return -1;
bob_gfx_fill_rect(x,y,width,height,' ',edge);
bob_gfx_line(x+1,y,x+width-2,y,'-',edge);
bob_gfx_line(x+1,y+height-1,x+width-2,y+height-1,'-',edge);
bob_gfx_line(x,y+1,x,y+height-2,'|',edge);
bob_gfx_line(x+width-1,y+1,x+width-1,y+height-2,'|',edge);
bob_gfx_pixel(x,y,'+',edge);bob_gfx_pixel(x+width-1,y,'+',edge);
bob_gfx_pixel(x,y+height-1,'+',edge);bob_gfx_pixel(x+width-1,y+height-1,'+',edge);
bob_gfx_fill_rect(x+1,y+1,width-2,1,' ',focused?4:8);
bob_gfx_text(x+2,y+1,title,title_color);
bob_gfx_text(x+2,y+3,body,edge);
if(message)bob_gfx_text(x+2,y+height-2,message,focused?10:7);
return 0;
}
#endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef BOB_MEMORY_H
#define BOB_MEMORY_H
/* Caller-owned word arena; allocation sizes are target words, not host bytes. */
int bob_arena_reset(int *used) { if(!used)return -1;*used=0;return 0; }
int *bob_arena_alloc(int *storage,int capacity,int *used,int words) {
int *result;
if(!storage||!used||capacity<=0||words<=0||*used<0||*used>capacity)return 0;
if(words>capacity-*used)return 0;
result=storage+*used;*used+=words;return result;
}
int bob_arena_used(int *used) { if(!used||*used<0)return -1;return *used; }
int bob_arena_remaining(int capacity,int *used) {
if(!used||capacity<0||*used<0||*used>capacity)return -1;
return capacity-*used;
}
#endif
+15
View File
@@ -0,0 +1,15 @@
#ifndef BOB_NATIVE_H
#define BOB_NATIVE_H
/* Common bob32 application APIs. Include bob_gfx.h/bob_gui.h only when needed. */
#include "bob.h"
#include "bob_string.h"
#include "bob_memory.h"
#include "bob_fs.h"
#include "bob_system.h"
#include "bob_process.h"
#include "bob_time.h"
#include "bob_event.h"
#include "bob_event_queue.h"
#endif
+14
View File
@@ -0,0 +1,14 @@
#ifndef BOB_PROCESS_H
#define BOB_PROCESS_H
#include "bob_fs.h"
/* Run a stored native bob32 app; returns service status and writes child status. */
int bob_app_run(const char *name,const char *arguments,int *exit_code) {
int request[7];
request[0]=BOB_FS_REQUEST(BOB_FS_RUN_APP);request[1]=(int)name;request[2]=(int)arguments;
request[3]=(int)exit_code;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
#endif
+21
View File
@@ -0,0 +1,21 @@
#ifndef BOB_SYSTEM_H
#define BOB_SYSTEM_H
#include "bob.h"
#define BOB_SYSTEM_API_VERSION 1
#define BOB_SYSTEM_CAPABILITIES 11
#define BOB_SYSTEM_DEVICE_CELL_FRAMEBUFFER 1
#define BOB_SYSTEM_INPUT_KEYBOARD 1
#define BOB_SYSTEM_INPUT_MOUSE 2
/* Returns version, device flags, cell width/height, and input flags. */
int bob_system_capabilities(int *values,int capacity) {
int request[7];
request[0]=(BOB_SYSTEM_API_VERSION << 16) | BOB_SYSTEM_CAPABILITIES;
request[1]=(int)values;request[2]=capacity;
request[3]=0;request[4]=0;request[5]=0;request[6]=0;
return bob_os_service(request);
}
#endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef BOB_TIME_H
#define BOB_TIME_H
#include "bob.h"
#define BOB_TIME_API_VERSION 1
#define BOB_TIME_GET_UTC_SECONDS 7
/* Writes low/high 32-bit words of UTC seconds since 1970-01-01. */
int bob_time_utc_seconds(int *words) {
int request[7];
request[0]=(BOB_TIME_API_VERSION << 16) | BOB_TIME_GET_UTC_SECONDS;
request[1]=(int)words;request[2]=2;request[3]=0;request[4]=0;
request[5]=0;request[6]=0;
return bob_os_service(request);
}
#endif
+193
View File
@@ -0,0 +1,193 @@
#ifndef BOB_WM_H
#define BOB_WM_H
#include "bob_event.h"
/* App-owned window records contain x,y,w,h,visible,dirty,allocated. */
#define BOB_WM_MAX_WINDOWS 8
#define BOB_WM_WINDOW_WORDS 7
#define BOB_WM_STATE_WORDS 10
/* State contains max,count,focus,drag,offset-x,offset-y,screen-w,screen-h,
full-dirty,draw-cursor. Z-order is an app-owned bottom-to-top ID array. */
static int bob_wm_window_valid(const int *state,const int *windows,int id) {
return state&&windows&&id>=0&&id<state[0]&&windows[id*BOB_WM_WINDOW_WORDS+6];
}
static int bob_wm_intersects(const int *a,const int *b) {
return a[0]<b[0]+b[2]&&b[0]<a[0]+a[2]&&a[1]<b[1]+b[3]&&b[1]<a[1]+a[3];
}
static void bob_wm_clamp(int *window,const int *state) {
int max_x=state[6]-window[2],max_y=state[7]-window[3];
if(max_x<0)max_x=0;if(max_y<0)max_y=0;
if(window[0]<0)window[0]=0;if(window[0]>max_x)window[0]=max_x;
if(window[1]<0)window[1]=0;if(window[1]>max_y)window[1]=max_y;
}
int bob_wm_init(int *state,int *windows,int *order,int maximum,int screen_width,int screen_height) {
if(!state||!windows||!order||maximum<1||maximum>BOB_WM_MAX_WINDOWS||
screen_width<1||screen_height<1)return -1;
for(int i=0;i<BOB_WM_STATE_WORDS;i++)state[i]=0;
state[0]=maximum;state[2]=-1;state[3]=-1;state[6]=screen_width;state[7]=screen_height;state[8]=1;
for(int i=0;i<maximum*BOB_WM_WINDOW_WORDS;i++)windows[i]=0;
for(int i=0;i<maximum;i++)order[i]=-1;
return 0;
}
static void bob_wm_mark_all(int *state,int *windows,const int *order) {
for(int i=0;i<state[1];i++) {
int id=order[i];
if(bob_wm_window_valid(state,windows,id)&&windows[id*BOB_WM_WINDOW_WORDS+4])
windows[id*BOB_WM_WINDOW_WORDS+5]=1;
}
state[8]=1;
}
int bob_wm_create(int *state,int *windows,int *order,int x,int y,int width,int height) {
int id=-1;
if(!state||!windows||!order||width<4||height<3||width>state[6]||height>state[7])return -1;
for(int i=0;i<state[0];i++)if(!windows[i*BOB_WM_WINDOW_WORDS+6]){id=i;break;}
if(id<0||state[1]>=state[0])return -1;
int *window=windows+id*BOB_WM_WINDOW_WORDS;
window[0]=x;window[1]=y;window[2]=width;window[3]=height;
window[4]=1;window[5]=1;window[6]=1;bob_wm_clamp(window,state);
order[state[1]++]=id;state[2]=id;state[3]=-1;
for(int i=0;i<state[1]-1;i++)windows[order[i]*BOB_WM_WINDOW_WORDS+5]=1;
state[8]=1;return id;
}
int bob_wm_raise(int *state,int *windows,int *order,int id) {
int index=-1;
if(!bob_wm_window_valid(state,windows,id)||!windows[id*BOB_WM_WINDOW_WORDS+4])return -1;
for(int i=0;i<state[1];i++)if(order[i]==id){index=i;break;}
if(index<0)return -1;
if(index==state[1]-1)return 0;
for(int i=index;i<state[1]-1;i++)order[i]=order[i+1];
order[state[1]-1]=id;bob_wm_mark_all(state,windows,order);return 1;
}
int bob_wm_focus(int *state,int *windows,int *order,int id) {
int old;
if(!bob_wm_window_valid(state,windows,id)||!windows[id*BOB_WM_WINDOW_WORDS+4])return -1;
old=state[2];
bob_wm_raise(state,windows,order,id);
if(old>=0&&old<state[0])windows[old*BOB_WM_WINDOW_WORDS+5]=1;
state[2]=id;windows[id*BOB_WM_WINDOW_WORDS+5]=1;
return 0;
}
int bob_wm_move(int *state,int *windows,const int *order,int id,int x,int y) {
int *window;
if(!bob_wm_window_valid(state,windows,id))return -1;
window=windows+id*BOB_WM_WINDOW_WORDS;
window[0]=x;window[1]=y;bob_wm_clamp(window,state);
bob_wm_mark_all(state,windows,order);return 0;
}
int bob_wm_show(int *state,int *windows,int *order,int id,int visible) {
int *window;
if(!bob_wm_window_valid(state,windows,id))return -1;
window=windows+id*BOB_WM_WINDOW_WORDS;visible=visible!=0;
if(window[4]==visible)return 0;
window[4]=visible;
if(visible)bob_wm_focus(state,windows,order,id);
else if(state[3]==id)state[3]=-1;
else if(state[2]==id) {
state[2]=-1;
for(int i=state[1]-1;i>=0;i--)if(windows[order[i]*BOB_WM_WINDOW_WORDS+4]){state[2]=order[i];break;}
if(state[2]>=0)windows[state[2]*BOB_WM_WINDOW_WORDS+5]=1;
}
bob_wm_mark_all(state,windows,order);return 1;
}
int bob_wm_destroy(int *state,int *windows,int *order,int id) {
int index=-1,was_focused;
if(!bob_wm_window_valid(state,windows,id))return -1;
for(int i=0;i<state[1];i++)if(order[i]==id){index=i;break;}
if(index<0)return -1;
was_focused=state[2]==id;
if(state[3]==id)state[3]=-1;
for(int i=index;i<state[1]-1;i++)order[i]=order[i+1];
order[--state[1]]=-1;
for(int i=0;i<BOB_WM_WINDOW_WORDS;i++)windows[id*BOB_WM_WINDOW_WORDS+i]=0;
if(was_focused) {
state[2]=-1;
for(int i=state[1]-1;i>=0;i--)if(windows[order[i]*BOB_WM_WINDOW_WORDS+4]){state[2]=order[i];break;}
}
if(state[2]>=0)windows[state[2]*BOB_WM_WINDOW_WORDS+5]=1;
bob_wm_mark_all(state,windows,order);return 0;
}
int bob_wm_window_at(const int *state,const int *windows,const int *order,int x,int y) {
if(!state||!windows||!order)return -1;
for(int i=state[1]-1;i>=0;i--) {
int id=order[i];int *window=(int *)(windows+id*BOB_WM_WINDOW_WORDS);
if(bob_wm_window_valid(state,windows,id)&&window[4]&&
x>=window[0]&&y>=window[1]&&x<window[0]+window[2]&&y<window[1]+window[3])return id;
}
return -1;
}
/* Returns a recipient ID or -1; routed mouse coordinates become window-local. */
int bob_wm_dispatch(int *state,int *windows,int *order,const int *event,int *routed) {
int type,target=-1,x,y;
if(!state||!windows||!order||!event||!routed)return -1;
type=event[0];for(int i=0;i<4;i++)routed[i]=event[i];
if(type==BOB_EVENT_KEY_DOWN||type==BOB_EVENT_KEY_UP||type==BOB_EVENT_CHAR) {
target=state[2];
if(!bob_wm_window_valid(state,windows,target)||!windows[target*BOB_WM_WINDOW_WORDS+4])return -1;
return target;
}
if(type!=BOB_EVENT_MOUSE_MOVE&&type!=BOB_EVENT_MOUSE_BUTTON_DOWN&&
type!=BOB_EVENT_MOUSE_BUTTON_UP&&type!=BOB_EVENT_MOUSE_WHEEL)return -1;
x=event[2];y=event[3];
if(type==BOB_EVENT_MOUSE_MOVE&&bob_wm_window_valid(state,windows,state[3])) {
target=state[3];bob_wm_move(state,windows,order,target,x-state[4],y-state[5]);
} else if(type==BOB_EVENT_MOUSE_BUTTON_UP&&bob_wm_window_valid(state,windows,state[3])) {
target=state[3];bob_wm_move(state,windows,order,target,x-state[4],y-state[5]);state[3]=-1;
} else {
target=bob_wm_window_at(state,windows,order,x,y);
if(type==BOB_EVENT_MOUSE_BUTTON_DOWN&&target>=0) {
bob_wm_focus(state,windows,order,target);
int *window=windows+target*BOB_WM_WINDOW_WORDS;
if((event[1]&BOB_MOUSE_BUTTON_LEFT)&&y==window[1]) {
state[3]=target;state[4]=x-window[0];state[5]=y-window[1];
}
}
}
if(target<0)return -1;
routed[2]=x-windows[target*BOB_WM_WINDOW_WORDS];
routed[3]=y-windows[target*BOB_WM_WINDOW_WORDS+1];
return target;
}
void bob_wm_invalidate(int *state,int *windows,int *order,int id) {
if(!bob_wm_window_valid(state,windows,id))return;
windows[id*BOB_WM_WINDOW_WORDS+5]=1;
for(int i=0;i<state[1];i++) {
int other=order[i];
if(other==id)continue;
if(bob_wm_window_valid(state,windows,other)&&windows[other*BOB_WM_WINDOW_WORDS+4]&&
bob_wm_intersects(windows+id*BOB_WM_WINDOW_WORDS,windows+other*BOB_WM_WINDOW_WORDS))
windows[other*BOB_WM_WINDOW_WORDS+5]=1;
}
}
/* Prepare a redraw pass and return dirty visible IDs in bottom-to-top order. */
int bob_wm_begin_draw(int *state,int *windows,const int *order) {
if(!state||!windows||!order)return -1;
if(state[8])bob_wm_mark_all(state,windows,order);
state[8]=0;state[9]=0;return 0;
}
int bob_wm_next_dirty(int *state,int *windows,const int *order) {
if(!state||!windows||!order)return -1;
while(state[9]<state[1]) {
int id=order[state[9]++];int *window=windows+id*BOB_WM_WINDOW_WORDS;
if(bob_wm_window_valid(state,windows,id)&&window[4]&&window[5]){window[5]=0;return id;}
}
return -1;
}
#endif
+17 -10
View File
@@ -3,6 +3,14 @@ int file_lengths[8];
int file_kinds[8];
int file_used[8];
void file_service_register(void) {
int descriptor[5];
descriptor[0] = 0; descriptor[1] = (int)file_names;
descriptor[2] = (int)file_lengths; descriptor[3] = (int)file_kinds;
descriptor[4] = (int)file_used;
bob_fs_register(descriptor);
}
void file_snapshot(int operation) {
int descriptor[4]; int result;
descriptor[0] = (int)file_names; descriptor[1] = (int)file_lengths;
@@ -26,14 +34,13 @@ void file_snapshot(int operation) {
}
char *file_name(int slot) { return file_names + slot * NAME_WORDS; }
/* The upper 4096 words are reserved for file contents, above the kernel stack.
Supervised programs cannot write this region. */
/* Native files occupy protected memory between the kernel and app regions. */
int *file_content(int slot) {
#ifdef BOBC_WIDE
int i; int offset;
offset = 0;
for (i = 0; i < slot; i++) if (file_used[i]) offset += file_lengths[i] + 1;
return (int *)0xf000 + offset;
return (int *)WIDE_FILE_BASE + offset;
#else
return (int *)0xf000 + slot * FILE_WORDS;
#endif
@@ -101,7 +108,7 @@ int file_write(char *name, void *data, int length, int kind) {
/* Compact first so variable-sized files never leave unusable gaps. */
for (i = 0; i < FILE_COUNT; i++) if (file_used[i] && i != slot) {
int *source; int *destination; int j;
source = (int *)0xf000; destination = (int *)0xf000;
source = (int *)WIDE_FILE_BASE; destination = (int *)WIDE_FILE_BASE;
for (j = 0; j < i; j++) if (file_used[j] && j != slot) destination += file_lengths[j] + 1;
for (j = 0; j < i; j++) if (file_used[j]) source += file_lengths[j] + 1;
if (source != destination && destination < source)
@@ -112,15 +119,15 @@ int file_write(char *name, void *data, int length, int kind) {
for (i = 0; i < slot; i++) if (file_used[i] && i != slot) targetOffset += file_lengths[i] + 1;
{ int total = 0;
for (i = 0; i < FILE_COUNT; i++) if (file_used[i] && i != slot) total += file_lengths[i] + 1;
for (i = total - 1; i >= targetOffset; i--) ((int *)0xf000)[i + length + 1] = ((int *)0xf000)[i];
for (i = total - 1; i >= targetOffset; i--) ((int *)WIDE_FILE_BASE)[i + length + 1] = ((int *)WIDE_FILE_BASE)[i];
if (sourceSlot >= 0) {
int sourceOffset = 0;
for (i = 0; i < sourceSlot; i++) if (file_used[i] && i != slot) sourceOffset += file_lengths[i] + 1;
if (sourceSlot > slot) sourceOffset += length + 1;
data = (int *)0xf000 + sourceOffset;
data = (int *)WIDE_FILE_BASE + sourceOffset;
}
memcpy((int *)0xf000 + targetOffset, data, length);
((int *)0xf000)[targetOffset + length] = 0;
memcpy((int *)WIDE_FILE_BASE + targetOffset, data, length);
((int *)WIDE_FILE_BASE)[targetOffset + length] = 0;
file_lengths[slot] = length; file_kinds[slot] = kind;
}
return slot;
@@ -150,7 +157,7 @@ void file_list(void) {
if (!count) println("No files.");
print("Used "); print_dec(count);
#ifdef BOBC_WIDE
println("/8 slots; shared 4096-word storage. Text/resident files max 511; native apps use remaining space.");
println("/8 slots; shared 8192-word storage. Text files max 511; native apps use remaining space.");
#else
println("/8 slots; limit 511 words per file. delete frees a slot.");
#endif
@@ -167,7 +174,7 @@ void file_manage(char *operation, char *name, char *destination) {
for (i = 0; i < FILE_COUNT; i++) if (file_used[i]) {
if (i == slot) { sourceOffset += file_lengths[i] + 1; continue; }
if (sourceOffset != destinationOffset)
for (j = 0; j <= file_lengths[i]; j++) ((int *)0xf000)[destinationOffset + j] = ((int *)0xf000)[sourceOffset + j];
for (j = 0; j <= file_lengths[i]; j++) ((int *)WIDE_FILE_BASE)[destinationOffset + j] = ((int *)WIDE_FILE_BASE)[sourceOffset + j];
sourceOffset += file_lengths[i] + 1; destinationOffset += file_lengths[i] + 1;
}
#endif
+19 -7
View File
@@ -1,10 +1,20 @@
/* Native shell editing; history is stored before command tokenization. */
#define shell_history ((char *)0xe400)
#define SHELL_HISTORY_LIMIT 4
#define SHELL_HISTORY_ENTRY_SIZE 128
int history_count;
int input_length;
int input_cursor;
int input_echo;
void shell_history_list(void) {
int i;
for (i = 0; i < history_count; i++) {
print_dec(i + 1); print(" ");
println(shell_history + i * SHELL_HISTORY_ENTRY_SIZE);
}
}
void input_redraw(void) {
int i; int width; int start; int end;
if (!input_echo) return;
@@ -45,9 +55,9 @@ void input_complete(void) {
if (input_cursor != input_length) return;
if (!start) {
#ifdef BOBC_WIDE
words = "help echo clear mem halt list ls dir read write edit load run run32 cc go copy rename delete alloc peek poke save restore";
words = "help echo clear mem halt history list ls dir read write edit load run run32 cc go copy rename delete alloc peek poke save restore";
#else
words = "help echo clear mem halt list ls dir read write edit load run cc go copy rename delete alloc peek poke save restore";
words = "help echo clear mem halt history list ls dir read write edit load run cc go copy rename delete alloc peek poke save restore";
#endif
while (*words) {
length = 0;
@@ -90,7 +100,8 @@ int shell_read_line(void) {
if (key == 256 && browsing) browsing--;
if (key == 257 && browsing < history_count) browsing++;
if (browsing == history_count) memcpy(command_line, draft, strlen(draft) + 1);
else memcpy(command_line, shell_history + browsing * 128, strlen(shell_history + browsing * 128) + 1);
else memcpy(command_line, shell_history + browsing * SHELL_HISTORY_ENTRY_SIZE,
strlen(shell_history + browsing * SHELL_HISTORY_ENTRY_SIZE) + 1);
input_length = strlen(command_line); input_cursor = input_length;
} else if (key == 258) { if (input_cursor) input_cursor--; }
else if (key == 259) { if (input_cursor < input_length) input_cursor++; }
@@ -118,11 +129,12 @@ int shell_read_line(void) {
if (truncated) return -1;
if (key == -1 && !input_length) return -2;
if (input_length && (history_count == 0 || strcmp(command_line, shell_history + (history_count - 1) * 128))) {
if (history_count == 4) {
for (i = 0; i < 384; i++) shell_history[i] = shell_history[i + 128];
history_count = 3;
if (history_count == SHELL_HISTORY_LIMIT) {
for (i = 0; i < (SHELL_HISTORY_LIMIT - 1) * SHELL_HISTORY_ENTRY_SIZE; i++)
shell_history[i] = shell_history[i + SHELL_HISTORY_ENTRY_SIZE];
history_count = SHELL_HISTORY_LIMIT - 1;
}
memcpy(shell_history + history_count * 128, command_line, input_length + 1); history_count++;
memcpy(shell_history + history_count * SHELL_HISTORY_ENTRY_SIZE, command_line, input_length + 1); history_count++;
}
return input_length;
}
+3
View File
@@ -8,6 +8,9 @@
int main(void) {
int length; char *sample;
#ifdef BOBC_WIDE
file_service_register();
#endif
println("bob!");
#ifdef BOBC_WIDE
println("bob32 OS: help COMMAND for usage; go bob.c runs the example.");
+4 -1
View File
@@ -14,10 +14,12 @@
#endif
#define WIDE_PROGRAM_BASE 0x20000
#define WIDE_VARIABLE_BASE 0xf0000
#define WIDE_FILE_BASE 0x1e000
#define WIDE_FILE_WORDS 8192
#define FILE_COUNT 8
#define TEXT_WORDS 512
#if BOBC_WIDE
#define FILE_WORDS 4096
#define FILE_WORDS WIDE_FILE_WORDS
#else
#define FILE_WORDS 512
#endif
@@ -28,6 +30,7 @@ int file_find(char *name);
int file_prepare_native(char *name);
char *file_name(int slot);
int *file_content(int slot);
void file_service_register(void);
#if BOBC_WIDE
void memcpy(void *dst, void *src, int count);
void memset(void *dst, int value, int count);
+7 -2
View File
@@ -7,6 +7,7 @@ int editor_undo_length;
int editor_has_undo;
char *arguments;
void nano_edit(char *name);
void shell_history_list(void);
char *next_arg(void) {
char *start;
@@ -270,7 +271,7 @@ void memory_status(void) {
print("Programs "); print_hex(PROGRAM_BASE); print(".."); print_hex(0xbfff); println("");
#endif
#ifdef BOBC_WIDE
println("Files: 8 slots, 4096 words total; text files up to 511 words; native apps share remaining space.");
println("Files: 8 slots, 8192 words total; text files up to 511 words; native apps share remaining space.");
#else
println("Files: 8 slots, 511 words each; stack above 0xE600.");
#endif
@@ -309,6 +310,7 @@ void command_help(char *name) {
else if (strcmp(name, "alloc") == 0 || strcmp(name, "mem") == 0)
println("alloc WORDS: allocate/zero heap words, print start address; no free. Example: alloc 16. mem shows remaining heap and memory regions.");
else if (strcmp(name, "echo") == 0) println("echo TEXT: print text. Example: echo bob!");
else if (strcmp(name, "history") == 0) println("history: list the four most recent nonempty shell commands from this session.");
else if (strcmp(name, "clear") == 0) println("clear: clear an ANSI-capable terminal; does not remove files.");
else if (strcmp(name, "halt") == 0) println("halt: stop emulator. Unsaved RAM files and allocations are lost; use save before quitting.");
else if (strcmp(name, "help") == 0) println("help: list commands. help COMMAND: show usage and an example. Try help edit or help go.");
@@ -326,7 +328,7 @@ void SHELL_COMMAND_BODY(char *line) {
if (strcmp(command, "help") == 0) {
name = next_arg();
if (*name) { command_help(name); return; }
println("help | echo TEXT | clear | mem | halt");
println("help | echo TEXT | history | clear | mem | halt");
println("peek ADDRESS | poke ADDRESS VALUE | alloc WORDS");
println("list / ls / dir | read NAME | write NAME TEXT | edit NAME");
println("copy OLD NEW | rename OLD NEW | delete NAME (permanent in RAM)");
@@ -396,6 +398,9 @@ void SHELL_COMMAND_BODY(char *line) {
*pointer = value;
}
print_hex(address); print(": "); print_hex(*pointer); print(" ("); print_dec(*pointer); println(")");
} else if (strcmp(command, "history") == 0) {
if (*next_arg()) println("Usage: history");
else shell_history_list();
} else println("Unknown command. Type help.");
}
#ifdef BOBC_WIDE