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

+111
View File
@@ -0,0 +1,111 @@
# Native bob32 applications
Build an application from the repository root:
```text
gcc -E -P -nostdinc -undef -I kernel apps/notes.c -o build/notes.i
build/bobcc build/notes.i build/notes.basm build/notes.b32 --wide-app
```
Use the same two build commands with `apps/graphics_demo.c` and the
`build/graphics_demo` output names to build the interactive canvas example.
Import it once as `graphics`, then run `run graphics`; it waits for a key and
returns to the shell.
Build `apps/gui_demo.c` the same way to try the desktop prototype. The larger
editor starts with `new.txt` and `bob!` in a filename field and multiline text
area. Click either field to place the caret, or use Tab to cycle through the
filename, text-area, and file-browser focus. In the browser, select a text file
and press `e` to load it; Ctrl+S saves the text back to the guest filesystem.
Enter inserts a newline. Escape returns to the shell. On a Windows console,
click a window to raise and focus it or drag its title bar to move it.
`kernel/bob_wm.h` is the shared window manager used by the GUI demo. It owns
fixed-capacity window records, focus, stacking, hit testing, title-bar drag
capture, event routing, screen clamping, and dirty redraw tracking. Apps provide
the manager's fixed storage arrays and use `bob_wm_dispatch` to route an event;
the returned window ID identifies its recipient, and mouse coordinates in the
routed event are window-local. Call `bob_wm_invalidate` when application content
changes, then draw IDs returned by `bob_wm_next_dirty` in stacking order.
`kernel/bob_gui.h` contains reusable window-frame, button, menu, single-line
text-field, and multiline text-area widgets. Drawn windows have framed borders
and a distinct title strip.
The `bob_gui_button_draw` and `bob_gui_button_event` helpers provide a reusable
clickable button with hover and pressed states. The demo button focuses Window
B on a left click, and the bob32 test suite exercises hit-testing, dragging,
left-button activation, and release-outside behavior with synthetic events.
Overlapping-window hit-testing and raising are also tested.
The text-field helpers support click-to-position, insertion, Backspace/Delete,
arrow/Home/End navigation, caret drawing, and horizontal scrolling. The text
area supports multiline insertion, vertical movement with column preservation,
click-to-position, caret visibility, and mouse-wheel scrolling. Native bob32
fixtures check text and cursor state, including wheel-scroll bounds.
The popup menu has fixed-stride labels, keyboard selection, Enter/Escape, and
mouse hover/click activation. Window B opens it from the File title area or
with `m`; its actions open the selected text file, start delete confirmation,
or close the menu. Popup placement follows Window B when it is dragged and
clamps the menu inside the 80x25 screen; widget tests cover edge and invalid
screen placements.
Window B lists guest filesystem entries and previews the selected text file;
`n`/`p` and mouse clicks change the selection, while Up/Down or `u`/`d` scroll
the preview. On Windows, the mouse wheel scrolls the editor or preview and
changes file selection when over the list. Press `x` to open an
on-screen Yes/Cancel prompt; click a button
or press `y`/`n` to confirm or cancel.
The GUI integration test creates a long `gui.txt`, scrolls beyond the initial
preview, checks a unique trailing marker, confirms deletion, and verifies the
file can no longer be read. It also creates `new.txt` from the GUI, snapshots
the guest filesystem, restores it in a second emulator process, and reads the
saved contents with `cat`. A separate integration test loads a two-line text
file, edits and saves it through the GUI, then checks the saved newline and text
with `cat`.
Start the bob32 OS, import the image, and use it from the guest filesystem:
```text
import32 build/notes.b32 notes
run notes put journal "bob! a saved note"
run notes edit journal "bob! updated note"
run notes add journal "another line"
run notes show journal
ls
save
```
`notes edit NAME [TEXT]` creates or replaces a text note, while
`notes add NAME [TEXT]` appends a line (or prompts for one). `notes` protects
binary and executable files from note-specific commands. It also supports
`run notes delete journal`. The `echo` app prints its
arguments, `ls` lists guest files and their kinds/sizes, and `cat FILE` displays
a text file. `sysinfo` reports application memory, filesystem usage, display
dimensions, and keyboard/mouse availability. Import
each image once; the application and its data are preserved by B32S snapshots.
`cat FILE` preserves existing trailing newlines and only adds a line break when
nonempty file contents have no final newline.
Build `apps/launcher.c` with the same commands to list stored native apps or
start one with arguments. For example, `run launcher echo "a saved note"`
starts `echo` as a child app and reports its exit code. Native apps can call
`bob_app_run(name, arguments, &exit_code)` from `bob_process.h` directly.
The GUI demo also has a small application-launcher panel: focus Window B and
press `a`, choose a stored app with `n`/`p` or the arrow keys, and press `r` or
click Launch. The desktop resumes and displays the child exit status.
Press `?` for a bitmap-font help overlay with the desktop shortcuts; Escape or
its Close button returns to the desktop. Launcher and help panels clear the
desktop canvas behind them so their contents remain readable.
`graphics_demo` uses the version 1 `bob_gfx.h` canvas API. Its first backend is
an 80x25 character-cell framebuffer with colors, filled rectangles, lines,
text, transparent bitmap cells (including per-cell colors), and explicit presentation. The same app can
later target a pixel renderer without changing its draw calls.
Include `bob_font.h` when an app needs the opt-in 5x7 bitmap text renderer.
The demo waits through `bob_event.h`, a blocking event interface that currently
normalizes typed characters, special-key presses/releases, mouse movement, and
mouse-button transitions when the Windows console provides those records.
Apps use `bob_fs.h` for guest file operations and `bob_string.h` for string and
number helpers. The filesystem service has a versioned request protocol and
keeps guest RAM tables private from application code.
`bob_event.h` provides blocking wait and nonblocking poll operations, while
`bob_event_queue.h` provides an optional caller-owned FIFO when an app needs to
buffer or defer events. A full queue reports overflow without dropping an
earlier event.
+4
View File
@@ -0,0 +1,4 @@
int main(void) {
bob64_app_write("bob!", 4);
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
#include "../bob64/app.h"
#define DISPLAY_LIMIT_WIDTH 1024u
#define DISPLAY_LIMIT_HEIGHT 768u
static u32 pixels[DISPLAY_LIMIT_WIDTH*DISPLAY_LIMIT_HEIGHT];
s64 bob64_app_main(const BOB64_APP_STARTUP *startup) {
u32 width,height;
BOB64_EVENT event;
if(!startup||startup->AbiVersion!=BOB64_APP_ABI_VERSION||
bob64_app_query_abi()!=BOB64_SYSCALL_ABI_VERSION||
bob64_app_get_display(&width,&height))return -1;
if(!width||!height||width>DISPLAY_LIMIT_WIDTH||height>DISPLAY_LIMIT_HEIGHT)
return -2;
for(u32 y=0;y<height;y++)for(u32 x=0;x<width;x++) {
u32 red=(x*180u)/width;
u32 green=(y*120u)/height;
pixels[(usize)y*width+x]=(red<<16)|(green<<8)|32u;
}
if(bob64_app_present(pixels,width,height)!=(s64)((u64)width*height))return -3;
if(bob64_app_write("bob!",4)!=4)return -4;
for(;;) {
if(bob64_app_wait_event(&event))return -5;
if(event.Type==BOB64_EVENT_KEY_DOWN&&event.Character==0x1b)break;
}
return 0;
}
+521
View File
@@ -0,0 +1,521 @@
#include "../bob64/app.h"
#include "../bob64/editor.h"
#include "../bob64/gfx.h"
#include "../bob64/window.h"
#define BOB64_GUI_MAX_WIDTH 1280u
#define BOB64_GUI_MAX_HEIGHT 800u
#define BOB64_GUI_MAX_WINDOWS 4u
#define BOB64_GUI_FILENAME_CAPACITY 64u
#define BOB64_GUI_SURFACE_PIXEL_CAPACITY 1048576u
static u32 pixels[BOB64_GUI_SURFACE_PIXEL_CAPACITY];
static BOB64_GFX graphics[BOB64_GUI_MAX_WINDOWS];
static struct {usize Offset,Bytes;u8 Used;} surface_allocations[BOB64_GUI_MAX_WINDOWS];
static BOB64_WINDOW_MANAGER manager;
static BOB64_WINDOW windows[BOB64_GUI_MAX_WINDOWS];
static BOB64_WINDOW_HANDLE order[BOB64_GUI_MAX_WINDOWS];
static char titles[BOB64_GUI_MAX_WINDOWS][16]={"Files","Editor","Window 3","Window 4"};
static char filename[BOB64_GUI_FILENAME_CAPACITY];
static char editor_status[32];
static BOB64_EDITOR editor;
static BOB64_WINDOW_HANDLE editor_window;
static BOB64_WINDOW_HANDLE files_window;
static u32 screen_width,screen_height;
static BOB64_FILE_INFO file_entries[BOB64_SYSCALL_MAX_FILES];
static u32 file_count,selected_file,file_scroll;
static int editor_save_allowed;
static int editor_exit_armed;
static int editor_open_armed;
static int file_delete_armed;
static usize gui_length(const char *text,usize limit) {
usize length=0;
if(!text)return limit;
while(length<limit&&text[length])length++;
return length;
}
static void gui_copy(char *destination,const char *source,usize length) {
for(usize i=0;i<length;i++)destination[i]=source[i];
destination[length]=0;
}
static BOB64_GFX *gui_graphics_for_handle(BOB64_WINDOW_HANDLE handle) {
for(u32 i=0;i<BOB64_GUI_MAX_WINDOWS;i++)
if(windows[i].Handle==handle)return &graphics[i];
return 0;
}
static u32 *gui_surface_allocate(u32 slot,u32 width,u32 height) {
usize bytes,candidate=0;
if(slot>=BOB64_GUI_MAX_WINDOWS||surface_allocations[slot].Used||!width||!height||
(u64)width*height>BOB64_GUI_SURFACE_PIXEL_CAPACITY)return 0;
bytes=(usize)width*height*sizeof(u32);
for(;;) {
usize next=sizeof(pixels);
int next_slot=-1;
for(u32 i=0;i<BOB64_GUI_MAX_WINDOWS;i++)
if(surface_allocations[i].Used&&surface_allocations[i].Offset>=candidate&&
surface_allocations[i].Offset<next) {
next=surface_allocations[i].Offset;next_slot=(int)i;
}
if(bytes<=next-candidate)break;
if(next_slot<0)return 0;
candidate=surface_allocations[next_slot].Offset+
surface_allocations[next_slot].Bytes;
if(candidate>=sizeof(pixels))return 0;
}
if(candidate>sizeof(pixels)||bytes>sizeof(pixels)-candidate)return 0;
surface_allocations[slot].Offset=candidate;
surface_allocations[slot].Bytes=bytes;
surface_allocations[slot].Used=1;
return pixels+candidate/sizeof(u32);
}
static void gui_surface_release(u32 slot) {
if(slot>=BOB64_GUI_MAX_WINDOWS)return;
surface_allocations[slot].Offset=0;surface_allocations[slot].Bytes=0;
surface_allocations[slot].Used=0;
}
static BOB64_WINDOW_HANDLE gui_create_window(s32 x,s32 y,u32 width,u32 height,
const char *title) {
BOB64_WINDOW_HANDLE kernel_handle;
BOB64_WINDOW_HANDLE app_handle;
BOB64_WINDOW *window;
u32 slot=0;
u32 *surface;
while(slot<BOB64_GUI_MAX_WINDOWS&&windows[slot].Handle)slot++;
if(slot>=BOB64_GUI_MAX_WINDOWS||!(surface=gui_surface_allocate(slot,width,height)))
return 0;
kernel_handle=bob64_app_window_create(x,y,width,height);
if(!kernel_handle||(s64)kernel_handle<0) {
gui_surface_release(slot);return 0;
}
manager.NextHandle=kernel_handle;
app_handle=bob64_wm_create(&manager,x,y,width,height,(void *)title);
if(app_handle!=kernel_handle) {
if(app_handle)bob64_wm_destroy(&manager,app_handle);
bob64_app_window_destroy(kernel_handle);gui_surface_release(slot);return 0;
}
window=bob64_wm_find(&manager,app_handle);
slot=(u32)(window-windows);
graphics[slot].Pixels=surface;graphics[slot].Width=width;
graphics[slot].Height=height;
graphics[slot].Capacity=(usize)BOB64_GUI_MAX_WIDTH*BOB64_GUI_MAX_HEIGHT;
return app_handle;
}
static int gui_destroy_window(BOB64_WINDOW_HANDLE handle) {
u32 slot=0;
while(slot<BOB64_GUI_MAX_WINDOWS&&windows[slot].Handle!=handle)slot++;
if(slot>=BOB64_GUI_MAX_WINDOWS)return -1;
if(bob64_app_window_destroy(handle))return -1;
if(bob64_wm_destroy(&manager,handle))return -1;
gui_surface_release(slot);
return 0;
}
static void gui_focus_window(BOB64_WINDOW_HANDLE handle) {
if(bob64_wm_focus(&manager,handle)||bob64_app_window_focus(handle))
gui_copy(editor_status,"FOCUS FAILED",sizeof("FOCUS FAILED"));
}
static int gui_load_file(const BOB64_APP_STARTUP *startup) {
const char *requested="notes.txt";
usize length=sizeof("notes.txt")-1;
s64 result;
if(startup->ArgumentCount>1) {
requested=startup->Arguments[1];
length=gui_length(requested,BOB64_GUI_FILENAME_CAPACITY);
if(!length||length>=BOB64_GUI_FILENAME_CAPACITY)return -1;
}
for(usize i=0;i<length;i++) {
char value=requested[i];
if(!((value>='a'&&value<='z')||(value>='A'&&value<='Z')||
(value>='0'&&value<='9')||value=='.'||value=='_'||value=='-'))return -1;
}
gui_copy(filename,requested,length);
result=bob64_app_read_file(filename,length,editor.Text,BOB64_EDITOR_CAPACITY);
if(result==-2) {
bob64_editor_init(&editor,0,0);
gui_copy(editor_status,"NEW FILE",sizeof("NEW FILE"));
editor_save_allowed=1;
} else if(result==-28) {
bob64_editor_init(&editor,0,0);
gui_copy(editor_status,"FILE TOO LARGE",sizeof("FILE TOO LARGE"));
editor_save_allowed=0;
} else if(result<0) {
return -1;
} else {
bob64_editor_init(&editor,editor.Text,(u32)result);
gui_copy(editor_status,"LOADED",sizeof("LOADED"));
editor_save_allowed=1;
}
return 0;
}
static void gui_refresh_files(void) {
s64 result=bob64_app_list_files(file_entries,BOB64_SYSCALL_MAX_FILES);
if(result<0) {
file_count=0;
gui_copy(editor_status,result==-28?"TOO MANY FILES":"LIST FAILED",
result==-28?sizeof("TOO MANY FILES"):sizeof("LIST FAILED"));
return;
}
file_count=(u32)result;
if(selected_file>=file_count)selected_file=file_count?file_count-1:0;
if(file_scroll>=file_count)file_scroll=file_count?file_count-1:0;
}
static void gui_reveal_selected_file(void) {
BOB64_WINDOW *window=bob64_wm_find(&manager,files_window);
u32 rows=window&&window->Height>58?(window->Height-58)/14:1;
if(!rows)rows=1;
if(selected_file<file_scroll)file_scroll=selected_file;
else if(selected_file>=file_scroll+rows)file_scroll=selected_file-rows+1;
}
static int gui_name_equal(const char *left,const char *right) {
while(*left&&*right&&*left==*right){left++;right++;}
return !*left&&!*right;
}
static void gui_open_selected_file(void) {
s64 result;
if(selected_file>=file_count) {
gui_copy(editor_status,"NO FILE SELECTED",sizeof("NO FILE SELECTED"));return;
}
const char *name=file_entries[selected_file].Name;
if(editor.Dirty&&!editor_open_armed) {
gui_copy(editor_status,"OPEN AGAIN TO DISCARD",sizeof("OPEN AGAIN TO DISCARD"));
editor_open_armed=1;return;
}
if(gui_name_equal(name,filename)) {
gui_focus_window(editor_window);
gui_copy(editor_status,"CURRENT FILE",sizeof("CURRENT FILE"));
editor_open_armed=0;return;
}
result=bob64_app_read_file(name,gui_length(name,BOB64_GUI_FILENAME_CAPACITY),
editor.Text,BOB64_EDITOR_CAPACITY);
if(result==-28) {
gui_copy(editor_status,"FILE TOO LARGE",sizeof("FILE TOO LARGE"));
editor_save_allowed=0;return;
}
if(result<0) {
gui_copy(editor_status,"OPEN FAILED",sizeof("OPEN FAILED"));return;
}
gui_copy(filename,name,gui_length(name,BOB64_GUI_FILENAME_CAPACITY));
bob64_editor_init(&editor,editor.Text,(u32)result);
editor_save_allowed=1;editor_open_armed=0;
gui_copy(editor_status,"LOADED",sizeof("LOADED"));
gui_focus_window(editor_window);
}
static void gui_delete_selected_file(void) {
const char *name;
s64 result;
if(selected_file>=file_count) {
gui_copy(editor_status,"NO FILE SELECTED",sizeof("NO FILE SELECTED"));return;
}
name=file_entries[selected_file].Name;
if(gui_name_equal(name,filename)) {
file_delete_armed=0;
gui_copy(editor_status,"SAVE OR OPEN OTHER FILE",sizeof("SAVE OR OPEN OTHER FILE"));
return;
}
if(!file_delete_armed) {
file_delete_armed=1;
gui_copy(editor_status,"D AGAIN TO DELETE",sizeof("D AGAIN TO DELETE"));
return;
}
result=bob64_app_delete_file(name,gui_length(name,BOB64_GUI_FILENAME_CAPACITY));
file_delete_armed=0;
if(result)gui_copy(editor_status,"DELETE FAILED",sizeof("DELETE FAILED"));
else {gui_refresh_files();gui_copy(editor_status,"DELETED",sizeof("DELETED"));}
}
static void gui_draw_window(const BOB64_WINDOW *window,int focused) {
BOB64_WINDOW local=*window;
BOB64_GFX *graphics;
local.X=0;local.Y=0;window=&local;
graphics=gui_graphics_for_handle(window->Handle);
if(!graphics)return;
const char *title=(const char *)window->Context;
u32 border=focused?0x00a4c8ffu:0x006d8095u;
u32 title_bar=focused?0x00335d8cu:0x00334050u;
bob64_gfx_fill_rect(graphics,window->X,window->Y,window->Width,
window->Height,0x0009111bu);
bob64_gfx_fill_rect(graphics,window->X+2,window->Y+2,window->Width-4,
window->Height-4,0x001a2a3cu);
bob64_gfx_fill_rect(graphics,window->X,window->Y,window->Width,
BOB64_WINDOW_TITLE_BAR_HEIGHT,title_bar);
bob64_gfx_line(graphics,window->X,window->Y,
window->X+(s32)window->Width-1,window->Y,border);
bob64_gfx_line(graphics,window->X,window->Y+(s32)window->Height-1,
window->X+(s32)window->Width-1,
window->Y+(s32)window->Height-1,border);
bob64_gfx_line(graphics,window->X,window->Y,window->X,
window->Y+(s32)window->Height-1,border);
bob64_gfx_line(graphics,window->X+(s32)window->Width-1,window->Y,
window->X+(s32)window->Width-1,
window->Y+(s32)window->Height-1,border);
bob64_gfx_text(graphics,window->X+12,window->Y+8,title,0x00ffffffu,1);
if(window->Handle==editor_window) {
u32 columns=(window->Width-24)/6;
u32 rows=(window->Height-76)/10;
u32 column=0,row=0;
u32 cursor_row,cursor_column;
s32 text_x=window->X+12,text_y=window->Y+46;
int caret_visible=0;
if(columns==0)columns=1;
bob64_editor_visual_position(&editor,columns,&cursor_row,&cursor_column);
(void)cursor_column;
if(cursor_row<editor.ScrollRow)editor.ScrollRow=cursor_row;
else if(rows&&cursor_row>=editor.ScrollRow+rows)
editor.ScrollRow=cursor_row-rows+1;
bob64_gfx_text(graphics,window->X+12,window->Y+30,filename,
0x009eb3c7u,1);
for(u32 i=0;i<=editor.Length;i++) {
if(column>=columns&&(i==editor.Length||editor.Text[i]!='\n')) {
row++;column=0;
}
if(i==editor.Cursor) {
s32 caret_x=text_x+(s32)(column*6);
s32 caret_y=text_y+(s32)((row-editor.ScrollRow)*10);
if(row>=editor.ScrollRow&&row<editor.ScrollRow+rows)
bob64_gfx_line(graphics,caret_x,caret_y,
caret_x,caret_y+7,0x00ffffffu);
caret_visible=1;
}
if(i==editor.Length)break;
if(editor.Text[i]=='\n') {
row++;column=0;
continue;
}
if(row>=editor.ScrollRow&&row<editor.ScrollRow+rows) {
char character[2]={editor.Text[i],0};
bob64_gfx_text(graphics,text_x+(s32)(column*6),
text_y+(s32)((row-editor.ScrollRow)*10),character,
0x00e2ebf5u,1);
}
column++;
}
if(!caret_visible&&row>=editor.ScrollRow&&row<editor.ScrollRow+rows)
bob64_gfx_line(graphics,text_x+(s32)(column*6),
text_y+(s32)((row-editor.ScrollRow)*10),
text_x+(s32)(column*6),text_y+(s32)((row-editor.ScrollRow)*10)+7,
0x00ffffffu);
bob64_gfx_line(graphics,window->X+8,
window->Y+(s32)window->Height-22,
window->X+(s32)window->Width-8,
window->Y+(s32)window->Height-22,0x0044576au);
bob64_gfx_text(graphics,window->X+12,
window->Y+(s32)window->Height-16,editor_status,0x008fc8ffu,1);
bob64_gfx_text(graphics,window->X+(s32)window->Width/2,
window->Y+(s32)window->Height-16,"CTRL S SAVE",0x009eb3c7u,1);
} else if(window->Handle==files_window) {
u32 rows=window->Height>58?(window->Height-58)/14:0;
bob64_gfx_text(graphics,window->X+10,window->Y+31,
"UP DOWN OPEN R REFRESH",0x009eb3c7u,1);
bob64_gfx_text(graphics,window->X+10,window->Y+41,
"D TWICE DELETE",0x009eb3c7u,1);
bob64_gfx_text(graphics,window->X+10,window->Y+49,editor_status,
0x008fc8ffu,1);
for(u32 row=0;row<rows&&file_scroll+row<file_count;row++) {
u32 index=file_scroll+row;
s32 y=window->Y+56+(s32)(row*14);
if(index==selected_file)
bob64_gfx_fill_rect(graphics,window->X+6,y-2,
window->Width-12,12,0x00335d8cu);
bob64_gfx_text(graphics,window->X+10,y,
file_entries[index].Name,index==selected_file?
0x00ffffffu:0x00dbe8f4u,1);
}
if(!file_count)bob64_gfx_text(graphics,window->X+10,window->Y+60,
"NO FILES",0x008fc8ffu,1);
} else if(window->Handle==manager.Focused) {
bob64_gfx_text(graphics,window->X+14,window->Y+46,
"BOB64 DESKTOP",0x007ed6ffu,1);
bob64_gfx_text(graphics,window->X+14,window->Y+66,
"RUN DESKTOP B64E FILE",0x00dbe8f4u,1);
bob64_gfx_text(graphics,window->X+14,window->Y+84,
"TAB SWITCH WINDOWS",0x00dbe8f4u,1);
bob64_gfx_text(graphics,window->X+14,window->Y+102,
"ESC RETURN TO SHELL",0x00dbe8f4u,1);
bob64_gfx_text(graphics,window->X+14,window->Y+120,
"N NEW X CLOSE OTHER",0x00dbe8f4u,1);
} else {
bob64_gfx_text(graphics,window->X+14,window->Y+46,
"DRAG WINDOW TITLE",0x00dbe8f4u,1);
}
}
static int gui_draw(s32 cursor_x,s32 cursor_y) {
for(u32 i=0;i<manager.Count;i++) {
BOB64_WINDOW *window=bob64_wm_find(&manager,manager.ZOrder[i]);
BOB64_GFX *surface;
if(!window||!(window->Flags&BOB64_WINDOW_VISIBLE))continue;
surface=gui_graphics_for_handle(window->Handle);
if(!surface)continue;
bob64_gfx_fill_rect(surface,0,0,surface->Width,surface->Height,
0x000d1723u);
for(u32 y=0;y<surface->Height;y+=4)
bob64_gfx_line(surface,0,(s32)y,(s32)surface->Width-1,(s32)y,
0x00132130u);
gui_draw_window(window,window->Handle==manager.Focused);
if(cursor_x>=window->X&&cursor_y>=window->Y&&
(u32)(cursor_x-window->X)<window->Width&&
(u32)(cursor_y-window->Y)<window->Height) {
s32 x=cursor_x-window->X,y=cursor_y-window->Y;
bob64_gfx_line(surface,x-5,y,x+5,y,0x00000000u);
bob64_gfx_line(surface,x,y-5,x,y+5,0x00000000u);
bob64_gfx_line(surface,x-3,y,x+3,y,0x00ffffffu);
bob64_gfx_line(surface,x,y-3,x,y+3,0x00ffffffu);
}
if(bob64_app_window_present(window->Handle,surface->Pixels,
surface->Width,surface->Height)!=(s64)((u64)surface->Width*surface->Height))
return -1;
}
return 0;
}
static void gui_save_editor(void) {
if(!editor_save_allowed) {
gui_copy(editor_status,"FILE TOO LARGE",sizeof("FILE TOO LARGE"));
return;
}
s64 result=bob64_app_write_file(filename,gui_length(filename,
BOB64_GUI_FILENAME_CAPACITY),editor.Text,editor.Length);
if(result==(s64)editor.Length) {
editor.Dirty=0;gui_copy(editor_status,"SAVED",sizeof("SAVED"));
} else gui_copy(editor_status,"SAVE FAILED",sizeof("SAVE FAILED"));
}
static void gui_editor_place_cursor(u32 target_row,u32 target_column,u32 columns) {
u32 row=0,column=0;
if(!columns)columns=1;
target_row+=editor.ScrollRow;
for(u32 i=0;i<=editor.Length;i++) {
if(column>=columns&&(i==editor.Length||editor.Text[i]!='\n')) {
row++;column=0;
}
if(row==target_row&&column>=target_column) {
editor.Cursor=i;return;
}
if(i==editor.Length)break;
if(editor.Text[i]=='\n') {
if(row==target_row){editor.Cursor=i;return;}
row++;column=0;continue;
}
column++;
}
editor.Cursor=editor.Length;
}
static void gui_focus_next(void) {
if(!manager.Count)return;
u32 start=0;
for(u32 i=0;i<manager.Count;i++)if(manager.ZOrder[i]==manager.Focused) {
start=i+1;break;
}
for(u32 offset=0;offset<manager.Count;offset++) {
BOB64_WINDOW_HANDLE handle=manager.ZOrder[(start+offset)%manager.Count];
BOB64_WINDOW *window=bob64_wm_find(&manager,handle);
if(window&&(window->Flags&BOB64_WINDOW_VISIBLE)) {
gui_focus_window(handle);return;
}
}
}
static void gui_key(const BOB64_EVENT *event) {
if(event->Type!=BOB64_EVENT_KEY_DOWN)return;
if(event->Character=='\t')gui_focus_next();
else if(manager.Focused==files_window) {
if(event->Key==(BOB64_EVENT_KEY_EXTENDED|0x48)) {
if(selected_file)selected_file--;
gui_reveal_selected_file();
file_delete_armed=editor_open_armed=0;
} else if(event->Key==(BOB64_EVENT_KEY_EXTENDED|0x50)) {
if(selected_file+1<file_count)selected_file++;
gui_reveal_selected_file();
file_delete_armed=editor_open_armed=0;
} else if(event->Character=='r'||event->Character=='R') {
gui_refresh_files();file_delete_armed=editor_open_armed=0;
} else if(event->Character=='o'||event->Character=='O'||
event->Character=='\n')gui_open_selected_file();
else if(event->Character=='d'||event->Character=='D')gui_delete_selected_file();
else file_delete_armed=editor_open_armed=0;
}
else if(event->Character=='n'&&manager.Count<BOB64_GUI_MAX_WINDOWS) {
u32 index=manager.Count;
BOB64_WINDOW_HANDLE handle=gui_create_window(
(s32)(screen_width/5+index*24),(s32)(screen_height/5+index*20),
screen_width/3,screen_height/3,titles[index]);
if(!handle)gui_copy(editor_status,"WINDOW CREATE FAILED",
sizeof("WINDOW CREATE FAILED"));
} else if(event->Character=='x'&&manager.Count>1&&manager.Focused!=editor_window) {
if(gui_destroy_window(manager.Focused))
gui_copy(editor_status,"WINDOW CLOSE FAILED",sizeof("WINDOW CLOSE FAILED"));
}
else if(manager.Focused==editor_window) {
if((event->Modifiers&BOB64_EVENT_MOD_CONTROL)&&
(event->Character=='s'||event->Character=='S'))gui_save_editor();
else bob64_editor_handle_key(&editor,event);
}
}
s64 bob64_app_main(const BOB64_APP_STARTUP *startup) {
BOB64_EVENT event;
BOB64_EVENT routed;
BOB64_EVENT screen_event;
s32 cursor_x,cursor_y;
if(!startup||startup->AbiVersion!=BOB64_APP_ABI_VERSION||
bob64_app_query_abi()!=BOB64_SYSCALL_ABI_VERSION)return -1;
if(bob64_app_write("bob!",4)!=4)return -3;
if(bob64_app_get_display(&screen_width,&screen_height))return -6;
if(screen_width<480||screen_height<300||screen_width>BOB64_GUI_MAX_WIDTH||
screen_height>BOB64_GUI_MAX_HEIGHT)return -7;
if(bob64_wm_init(&manager,windows,BOB64_GUI_MAX_WINDOWS,order,
screen_width,screen_height))return -8;
if(gui_load_file(startup))return -9;
gui_refresh_files();
cursor_x=(s32)(screen_width/2);cursor_y=(s32)(screen_height/2);
if(!gui_create_window(8,24,screen_width/3,screen_height-32,titles[0])||
!gui_create_window((s32)(screen_width/3+16),24,
screen_width-screen_width/3-24,screen_height-32,titles[1]))return -2;
files_window=manager.ZOrder[0];
editor_window=manager.Focused;
for(;;) {
if(gui_draw(cursor_x,cursor_y))return -4;
if(bob64_app_wait_event(&event))return -5;
if(event.Type==BOB64_EVENT_KEY_DOWN&&event.Character==0x1b) {
if(editor.Dirty&&!editor_exit_armed) {
gui_copy(editor_status,"ESC AGAIN TO QUIT",sizeof("ESC AGAIN TO QUIT"));
editor_exit_armed=1;continue;
}
break;
}
if(event.Type==BOB64_EVENT_KEY_DOWN)editor_exit_armed=0;
if(event.Type==BOB64_EVENT_MOUSE_MOVE||event.Type==BOB64_EVENT_MOUSE_BUTTON||
event.Type==BOB64_EVENT_MOUSE_WHEEL)
cursor_x=event.ScreenX,cursor_y=event.ScreenY;
screen_event=event;screen_event.X=event.ScreenX;screen_event.Y=event.ScreenY;
bob64_wm_dispatch(&manager,&screen_event,&routed);
if(event.Type==BOB64_EVENT_MOUSE_BUTTON&&(event.Buttons&1u)&&
manager.Focused==files_window&&routed.X>=8&&routed.Y>=56) {
u32 row=(u32)(routed.Y-56)/14;
if(file_scroll+row<file_count)selected_file=file_scroll+row;
}
if(event.Type==BOB64_EVENT_MOUSE_BUTTON&&(event.Buttons&1u)&&
manager.Focused==editor_window&&routed.X>=12&&routed.Y>=46) {
BOB64_WINDOW *window=bob64_wm_find(&manager,editor_window);
if(window)gui_editor_place_cursor((u32)(routed.Y-46)/10,
(u32)(routed.X-12)/6,(window->Width-24)/6);
}
gui_key(&event);
if(editor.Dirty)gui_copy(editor_status,"UNSAVED",sizeof("UNSAVED"));
}
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
#include "../bob64/app.h"
#include "../bob64/libc.h"
s64 bob64_app_main(const BOB64_APP_STARTUP *startup) {
static const char message[]="bob!";
static const char file_name[]="bob-app.txt";
static const char stream_name[]="bob-stream.txt";
static const char file_data[]="written by a C app";
char file_copy[32];
if(!startup||startup->AbiVersion!=BOB64_APP_ABI_VERSION)return -1;
if(bob64_app_query_abi()!=BOB64_SYSCALL_ABI_VERSION)return -2;
u64 ticks_before=bob64_app_ticks();
if(bob64_app_write(message,sizeof(message)-1)!=(s64)(sizeof(message)-1))return -3;
if(bob64_app_write_file(file_name,sizeof(file_name)-1,file_data,
sizeof(file_data)-1)!=(s64)(sizeof(file_data)-1))return -4;
s64 copied=bob64_app_read_file(file_name,sizeof(file_name)-1,file_copy,
sizeof(file_copy));
if(copied!=(s64)(sizeof(file_data)-1))return -5;
if(bob64_memcmp(file_copy,file_data,sizeof(file_data)-1))return -6;
s64 handle=bob64_app_file_open(stream_name,sizeof(stream_name)-1,
BOB64_FILE_OPEN_READ|BOB64_FILE_OPEN_WRITE|BOB64_FILE_OPEN_CREATE|
BOB64_FILE_OPEN_TRUNCATE);
if(handle<=0)return -7;
if(bob64_app_file_write(handle,file_data,sizeof(file_data)-1)!=
(s64)(sizeof(file_data)-1))return -8;
if(bob64_app_file_seek(handle,0)!=0)return -9;
copied=bob64_app_file_read(handle,file_copy,sizeof(file_copy));
if(copied!=(s64)(sizeof(file_data)-1)||
bob64_memcmp(file_copy,file_data,sizeof(file_data)-1))return -10;
if(bob64_app_file_close(handle))return -11;
if(bob64_app_ticks()<ticks_before)return -12;
return 0;
}
+14
View File
@@ -0,0 +1,14 @@
#include "bob.h"
#include "bob_fs.h"
int main(int argc, char **argv) {
char text[512];
int length;
int i;
if (argc != 2) { bob_puts("Usage: cat FILE"); return 1; }
length = bob_file_read(argv[1], text, 512);
if (length < 0) { bob_puts("Cannot read text file."); return 1; }
for (i = 0; i < length; i++) bob_putc(text[i]);
if(length>0&&text[length-1]!='\n')bob_putc('\n');
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
#include "bob.h"
static void write_text(const char *text) {
while (*text) bob_putc(*text++);
}
int main(int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
if (i > 1) bob_putc(' ');
write_text(argv[i]);
}
bob_putc('\n');
return 0;
}
+31
View File
@@ -0,0 +1,31 @@
#include "bob.h"
#include "bob_gfx.h"
#include "bob_event.h"
int main(void) {
int resolution[2];
int event[4];
char bitmap[8] = {'/', '-', '\\', '|', '\\', '-', '/', '|'};
if (bob_gfx_enter()) return 1;
if (bob_gfx_resolution(resolution) != 2 || resolution[0] != 80 || resolution[1] != 25) {
bob_gfx_leave();
return 2;
}
bob_gfx_clear(' ', 0);
bob_gfx_fill_rect(1, 1, 78, 23, '.', 1);
bob_gfx_fill_rect(2, 2, 76, 3, ' ', 4);
bob_gfx_text(4, 3, "BOB32 GRAPHICS DEMO", 15);
bob_gfx_line(3, 7, 76, 7, '=', 14);
bob_gfx_pixel(40, 16, '*', 10);
bob_gfx_fill_rect(8, 10, 12, 5, '#', 2);
bob_gfx_blit(28, 10, 4, 2, bitmap, 12);
bob_gfx_text(4, 19, "Press any key to return to the shell", 15);
if (bob_gfx_present()) { bob_gfx_leave(); return 3; }
if (bob_event_wait(event) != 1 || event[0] != BOB_EVENT_CHAR || event[1] != 'x') {
bob_gfx_leave();
return 4;
}
if (bob_gfx_leave()) return 5;
bob_puts("bob!");
return 0;
}
+462
View File
@@ -0,0 +1,462 @@
#include "bob.h"
#include "bob_gfx.h"
#include "bob_font.h"
#include "bob_event.h"
#include "bob_gui.h"
#include "bob_fs.h"
#include "bob_process.h"
#include "bob_string.h"
static void gui_select_file(int row,const int *slots,int count,int *selected,
char *status,char *preview,int *preview_length,int *preview_scroll) {
int size,kind;char name[24],size_text[12];
if(row<0||row>=count)return;
*selected=row;
kind=bob_file_list(slots[row],name,24,&size);
bob_format_int(size,size_text,12);
bob_strcpy(status,name);bob_strcat(status," ");bob_strcat(status,size_text);
preview[0]=0;*preview_length=-1;*preview_scroll=0;
if(kind==0)*preview_length=bob_file_read(name,preview,512);
}
static int gui_preview_offset(const char *preview,int length,int lines) {
int position=0,column;
while(lines>0&&position<length) {
column=0;
while(column<34&&position<length&&preview[position]!='\n'){position++;column++;}
if(position<length&&preview[position]=='\n')position++;
lines--;
}
return position;
}
static int gui_delete_selected(int row,const int *slots,int count,char *name) {
int size,kind;
if(row<0||row>=count)return -1;
kind=bob_file_list(slots[row],name,24,&size);
if(kind!=0)return -1;
return bob_file_delete(name);
}
static void gui_wm_sync_rects(int *rects,const int *managed) {
for(int id=0;id<2;id++)for(int i=0;i<4;i++)
rects[id*4+i]=managed[id*BOB_WM_WINDOW_WORDS+i];
}
static void gui_place_editor_widgets(int *windows,int *name_field,int *text_area,int *button) {
int *rect=windows;
name_field[0]=rect[0]+2;name_field[1]=rect[1]+5;
text_area[0]=rect[0]+2;text_area[1]=rect[1]+9;
button[0]=rect[0]+3;button[1]=rect[1]+16;
}
static int gui_open_selected_text(int row,const int *slots,int count,char *name,
char *filename,char *text,int *text_state,const int *text_area,
int *wm_state,int *wm_windows,int *wm_order,char *status) {
int size,kind,length;
if(row<0||row>=count){bob_strcpy(status,"Select a text file.");return 0;}
kind=bob_file_list(slots[row],name,24,&size);
if(kind!=BOB_FILE_KIND_TEXT){bob_strcpy(status,"Text files only.");return 0;}
length=bob_file_read(name,text,512);
if(length<0){bob_strcpy(status,"Could not read file.");return 0;}
bob_strcpy(filename,name);
bob_gui_text_area_init(text,text_state,512);
bob_gui_text_area_reveal(text,text_state,text_area);
text_state[0]=1;bob_wm_focus(wm_state,wm_windows,wm_order,0);
bob_strcpy(status,"Editing selected file.");
return 1;
}
static void gui_launch_app(int row,const char *names,int count,char *message) {
char number[12];
int child_status=-1,status;
if(row<0||row>=count){bob_strcpy(message,"No app selected.");return;}
status=bob_app_run(names+row*24,"",&child_status);
if(status==-1)bob_strcpy(message,"App not found.");
else if(status==-2)bob_strcpy(message,"Not a native app.");
else if(status==-4)bob_strcpy(message,"Invalid app image.");
else if(status==-5)bob_strcpy(message,"Not enough memory.");
else if(status<0)bob_strcpy(message,"App launch failed.");
else {
bob_strcpy(message,"App exit ");bob_format_int(child_status,number,12);
bob_strcat(message,number);
}
}
int main(void) {
int event[4];
int mouse_x=-1;
int mouse_y=-1;
int wm_state[10];
int wm_windows[14];
int wm_order[2];
int routed_event[4];
int target_window=-1;
int redraw_full=0;
int overlay_was_open=0;
int draw_id=-1;
int windows[8];
int button[4]={6,19,31,3};
int button_state[2]={0,0};
int delete_yes[4]={43,17,13,3};
int delete_no[4]={59,17,13,3};
int delete_yes_state[2]={0,0};
int delete_no_state[2]={0,0};
int name_field[4]={5,8,33,3};
int text_area[4]={5,12,33,7};
int name_state[3];
int text_state[4];
int length_b=0;
int file_slots[8];
char app_names[192];
int app_count=0;
int app_selected=0;
int launcher_open=0;
int help_open=0;
int help_close[4]={32,18,16,3};
int help_close_state[2]={0,0};
int launch_button[4]={42,18,16,3};
int close_button[4]={22,18,14,3};
int launch_button_state[2]={0,0};
int close_button_state[2]={0,0};
int file_count=0;
int selected_file=-1;
int selection_initialized=0;
int file_scroll=0;
int delete_confirm=0;
int menu_open=0;
int menu_rect[4]={43,7,30,5};
int menu_state[2]={0,-1};
int file_size;
int file_kind;
int preview_length=-1;
int preview_scroll=0;
int preview_row,preview_column,preview_position;
char preview[512];
char preview_line[35];
char file_name[24];
char file_name_text[24]="new.txt";
char file_text[512]="bob!";
char message_b[32]="";
char menu_items[72];
int running=1;
bob_strcpy(message_b,"N/P select; E edit; X delete");
bob_strcpy(menu_items,"Open selected text");
bob_strcpy(menu_items+24,"Delete selected");
bob_strcpy(menu_items+48,"Close menu");
bob_gui_text_field_init(file_name_text,name_state,24);
bob_gui_text_area_init(file_text,text_state,512);
name_state[0]=1;
if(bob_wm_init(wm_state,wm_windows,wm_order,2,80,25))return 1;
if(bob_wm_create(wm_state,wm_windows,wm_order,3,3,37,20)!=0||
bob_wm_create(wm_state,wm_windows,wm_order,41,3,37,20)!=1)return 1;
bob_wm_focus(wm_state,wm_windows,wm_order,0);
if(bob_gfx_enter())return 1;
while(running) {
gui_wm_sync_rects(windows,wm_windows);
gui_place_editor_widgets(windows,name_field,text_area,button);
bob_gui_menu_place(menu_rect,windows[4]+2,windows[5]+4,30,3,80,25);
file_count=0;
app_count=0;
for(int i=0;i<8;i++) {
file_kind=bob_file_list(i,file_name,24,&file_size);
if(file_kind>=0)file_slots[file_count++]=i;
if(file_kind==BOB_FILE_KIND_NATIVE_APP) {
bob_strcpy(app_names+app_count*24,file_name);app_count++;
}
}
if(app_selected>=app_count)app_selected=app_count?app_count-1:0;
if(!selection_initialized&&file_count>0) {
gui_select_file(0,file_slots,file_count,&selected_file,message_b,preview,&preview_length,&preview_scroll);
selection_initialized=1;
}
if(selected_file>=file_count) {
selected_file=-1;preview_length=-1;preview_scroll=0;delete_confirm=0;
bob_strcpy(message_b,"Selection changed.");
}
if(file_scroll>file_count-6)file_scroll=file_count>6?file_count-6:0;
if(selected_file>=0) {
if(selected_file<file_scroll)file_scroll=selected_file;
if(selected_file>=file_scroll+6)file_scroll=selected_file-5;
}
if(menu_open||launcher_open||help_open||overlay_was_open)wm_state[8]=1;
redraw_full=wm_state[8];
bob_wm_begin_draw(wm_state,wm_windows,wm_order);
if(redraw_full) {
bob_gfx_clear('.',1);
bob_gfx_text(2,0,"BOB32 DESKTOP ?: HELP TAB: FOCUS CTRL+S: SAVE ESC: EXIT",15);
}
while((draw_id=bob_wm_next_dirty(wm_state,wm_windows,wm_order))>=0) {
int id=draw_id;int *rect=windows+id*4;
if(id==0)bob_gui_window(rect[0],rect[1],rect[2],rect[3],"Text editor",
"E opens text. Tab changes focus.","Ctrl+S saves; Enter starts a new line.",wm_state[2]==0);
else {
bob_gui_window(rect[0],rect[1],rect[2],rect[3],"Window B","File menu: M or click File",message_b,wm_state[2]==1);
for(int row=0;row<6&&file_scroll+row<file_count;row++) {
int slot=file_slots[file_scroll+row];
bob_file_list(slot,file_name,24,&file_size);
bob_gfx_text(rect[0]+2,rect[1]+4+row,
file_scroll+row==selected_file?"> ":" ",wm_state[2]==1?15:8);
bob_gfx_text(rect[0]+4,rect[1]+4+row,file_name,wm_state[2]==1?15:7);
}
if(delete_confirm) {
bob_gfx_text(rect[0]+2,rect[1]+11,"Delete selected text file?",wm_state[2]==1?14:8);
delete_yes[0]=rect[0]+2;delete_yes[1]=rect[1]+14;
delete_no[0]=rect[0]+19;delete_no[1]=rect[1]+14;
bob_gui_button_draw(delete_yes[0],delete_yes[1],delete_yes[2],delete_yes[3],"Yes, delete",delete_yes_state[0],delete_yes_state[1]);
bob_gui_button_draw(delete_no[0],delete_no[1],delete_no[2],delete_no[3],"Cancel",delete_no_state[0],delete_no_state[1]);
} else {
bob_gfx_text(rect[0]+2,rect[1]+11,"Preview:",wm_state[2]==1?14:8);
if(preview_length<0)bob_gfx_text(rect[0]+2,rect[1]+12,"No text preview.",wm_state[2]==1?7:8);
preview_position=gui_preview_offset(preview,preview_length,preview_scroll);
for(preview_row=0;preview_row<6&&preview_position<preview_length;preview_row++) {
preview_column=0;
while(preview_column<34&&preview_position<preview_length&&preview[preview_position]!='\n')
preview_line[preview_column++]=preview[preview_position++];
if(preview_position<preview_length&&preview[preview_position]=='\n')preview_position++;
preview_line[preview_column]=0;
bob_gfx_text(rect[0]+2,rect[1]+12+preview_row,preview_line,wm_state[2]==1?15:7);
}
}
}
if(id==0) {
gui_place_editor_widgets(windows,name_field,text_area,button);
bob_gfx_text(rect[0]+2,rect[1]+4,"File name",wm_state[2]==0?14:8);
bob_gui_text_field_draw(name_field,file_name_text,name_state);
bob_gfx_text(rect[0]+2,rect[1]+8,"Text",wm_state[2]==0?14:8);
bob_gui_text_area_draw(text_area,file_text,text_state);
bob_gui_button_draw(button[0],button[1],button[2],button[3],"Save file",button_state[0],button_state[1]);
}
}
if(menu_open)bob_gui_menu_draw(menu_rect,menu_items,3,24,menu_state[0]);
if(launcher_open||help_open)bob_gfx_fill_rect(0,1,80,24,'.',1);
if(launcher_open) {
bob_gui_window(18,3,44,19,"Application launcher",
"N/P or arrows select; R runs; Esc closes.","",1);
for(int row=0;row<app_count&&row<8;row++) {
int icon='A'+((row%14+1)<<8);
if(row==app_selected)bob_gfx_fill_rect(21,7+row,38,1,' ',1);
bob_gfx_blit_color(22,7+row,1,1,&icon);
bob_gfx_text(25,7+row,app_names+row*24,row==app_selected?15:7);
}
bob_gui_button_draw(close_button[0],close_button[1],close_button[2],close_button[3],"Close",close_button_state[0],close_button_state[1]);
bob_gui_button_draw(launch_button[0],launch_button[1],launch_button[2],launch_button[3],"Launch",launch_button_state[0],launch_button_state[1]);
}
if(help_open) {
bob_gui_window(18,3,44,19,"Desktop help","","",1);
bob_gfx_bitmap_text(28,6,"HELP",14);
bob_gfx_text(23,14,"Tab moves through fields and files.",15);
bob_gfx_text(23,15,"E edits a selected text file; X deletes.",15);
bob_gfx_text(23,16,"Ctrl+S saves; Esc closes this help.",15);
bob_gfx_text(23,17,"Wheel scrolls text; list wheel selects.",15);
bob_gui_button_draw(help_close[0],help_close[1],help_close[2],help_close[3],"Close",help_close_state[0],help_close_state[1]);
}
overlay_was_open=menu_open||launcher_open||help_open;
bob_gfx_present();
if(bob_event_wait(event)!=1)break;
if(menu_open) {
int action=bob_gui_menu_event(menu_rect,3,menu_state,event);
if(action==-2)menu_open=0;
else if(event[0]==BOB_EVENT_MOUSE_BUTTON_DOWN&&
!bob_gui_window_hit(event[2],event[3],menu_rect))menu_open=0;
else if(action==0) {
gui_open_selected_text(selected_file,file_slots,file_count,file_name,
file_name_text,file_text,text_state,text_area,wm_state,wm_windows,wm_order,message_b);
menu_open=0;
} else if(action==1) {
int size,kind;
if(selected_file>=0&&selected_file<file_count) {
kind=bob_file_list(file_slots[selected_file],file_name,24,&size);
if(kind==BOB_FILE_KIND_TEXT){delete_confirm=1;bob_strcpy(message_b,"Delete? Y/N");}
else bob_strcpy(message_b,"Text files only.");
} else bob_strcpy(message_b,"No file selected.");
menu_open=0;
} else if(action==2)menu_open=0;
continue;
}
if(help_open) {
if(event[0]==BOB_EVENT_MOUSE_MOVE)bob_gui_button_event(help_close,help_close_state,event);
else if(event[0]==BOB_EVENT_MOUSE_BUTTON_DOWN)bob_gui_button_event(help_close,help_close_state,event);
else if(event[0]==BOB_EVENT_MOUSE_BUTTON_UP) {
if(bob_gui_button_event(help_close,help_close_state,event))help_open=0;
} else if(event[0]==BOB_EVENT_CHAR||event[0]==BOB_EVENT_KEY_DOWN)help_open=0;
continue;
}
if(launcher_open) {
if(event[0]==BOB_EVENT_MOUSE_MOVE) {
bob_gui_button_event(close_button,close_button_state,event);
bob_gui_button_event(launch_button,launch_button_state,event);
} else if(event[0]==BOB_EVENT_MOUSE_BUTTON_DOWN) {
int row=event[3]-7;
if(event[2]>=21&&event[2]<59&&row>=0&&row<app_count&&row<8)app_selected=row;
bob_gui_button_event(close_button,close_button_state,event);
bob_gui_button_event(launch_button,launch_button_state,event);
} else if(event[0]==BOB_EVENT_MOUSE_BUTTON_UP) {
int close=bob_gui_button_event(close_button,close_button_state,event);
int launch=bob_gui_button_event(launch_button,launch_button_state,event);
if(close)launcher_open=0;
else if(launch){gui_launch_app(app_selected,app_names,app_count,message_b);launcher_open=0;}
} else if(event[0]==BOB_EVENT_KEY_DOWN) {
if(event[1]==256&&app_selected>0)app_selected--;
else if(event[1]==257&&app_selected+1<app_count&&app_selected<7)app_selected++;
} else if(event[0]==BOB_EVENT_CHAR) {
if(event[1]==27)launcher_open=0;
else if((event[1]=='n'||event[1]=='j')&&app_selected+1<app_count&&app_selected<7)app_selected++;
else if((event[1]=='p'||event[1]=='k')&&app_selected>0)app_selected--;
else if(event[1]=='r'){gui_launch_app(app_selected,app_names,app_count,message_b);launcher_open=0;}
}
continue;
}
if(event[0]==BOB_EVENT_CHAR&&event[1]==27){running=0;break;}
target_window=bob_wm_dispatch(wm_state,wm_windows,wm_order,event,routed_event);
if(target_window<0)continue;
for(int i=0;i<4;i++)event[i]=routed_event[i];
gui_wm_sync_rects(windows,wm_windows);
gui_place_editor_widgets(windows,name_field,text_area,button);
bob_wm_invalidate(wm_state,wm_windows,wm_order,target_window);
if(event[0]==BOB_EVENT_MOUSE_MOVE||event[0]==BOB_EVENT_MOUSE_BUTTON_DOWN||
event[0]==BOB_EVENT_MOUSE_BUTTON_UP||event[0]==BOB_EVENT_MOUSE_WHEEL) {
event[2]+=windows[target_window*4];event[3]+=windows[target_window*4+1];
}
if(event[0]==BOB_EVENT_MOUSE_MOVE) {
mouse_x=event[2];mouse_y=event[3];
if(wm_state[2]==0)bob_gui_button_event(button,button_state,event);
else button_state[0]=0;
if(wm_state[2]==1&&delete_confirm) {
bob_gui_button_event(delete_yes,delete_yes_state,event);
bob_gui_button_event(delete_no,delete_no_state,event);
} else {delete_yes_state[0]=0;delete_no_state[0]=0;}
continue;
}
if(event[0]==BOB_EVENT_MOUSE_WHEEL) {
mouse_x=event[2];mouse_y=event[3];
if(wm_state[2]==0&&bob_gui_window_hit(mouse_x,mouse_y,text_area)) {
name_state[0]=0;bob_wm_focus(wm_state,wm_windows,wm_order,0);
bob_gui_text_area_event(text_area,file_text,text_state,512,event);
} else if(wm_state[2]==1&&bob_gui_window_hit(mouse_x,mouse_y,windows+4)) {
if(mouse_y>=windows[5]+4&&mouse_y<windows[5]+10) {
int row=event[1]>0?selected_file-1:selected_file+1;
if(row>=0&&row<file_count)
gui_select_file(row,file_slots,file_count,&selected_file,message_b,preview,&preview_length,&preview_scroll);
} else if(!delete_confirm&&mouse_y>=windows[5]+11&&mouse_y<windows[5]+18) {
if(event[1]>0&&preview_scroll>0)preview_scroll--;
else if(event[1]<0&&gui_preview_offset(preview,preview_length,preview_scroll+1)<preview_length)preview_scroll++;
}
}
continue;
}
if(event[0]==BOB_EVENT_MOUSE_BUTTON_DOWN) {
int hit=target_window;
mouse_x=event[2];mouse_y=event[3];
if(hit==1&&mouse_y==windows[5]+3&&mouse_x>=windows[4]+2&&mouse_x<windows[4]+6) {
menu_open=1;menu_state[0]=0;menu_state[1]=-1;continue;
}
if(hit==1&&mouse_y>=windows[5]+4&&mouse_y<windows[5]+10) {
int row=file_scroll+mouse_y-(windows[5]+4);
if(row<file_count) {
delete_confirm=0;
gui_select_file(row,file_slots,file_count,&selected_file,message_b,preview,&preview_length,&preview_scroll);
}
}
if(wm_state[2]==1&&delete_confirm) {
bob_gui_button_event(delete_yes,delete_yes_state,event);
bob_gui_button_event(delete_no,delete_no_state,event);
}
if(wm_state[2]==0) {
bob_gui_button_event(button,button_state,event);
bob_gui_text_field_event(name_field,file_name_text,name_state,24,event);
bob_gui_text_area_event(text_area,file_text,text_state,512,event);
} else {button_state[0]=0;button_state[1]=0;name_state[0]=0;text_state[0]=0;}
continue;
}
if(event[0]==BOB_EVENT_MOUSE_BUTTON_UP) {
if(delete_confirm&&wm_state[2]==1&&bob_gui_button_event(delete_yes,delete_yes_state,event)) {
if(gui_delete_selected(selected_file,file_slots,file_count,file_name)==0) {
selected_file=-1;selection_initialized=1;preview_length=-1;preview_scroll=0;
bob_strcpy(message_b,"Deleted.");
} else bob_strcpy(message_b,"Delete failed.");
delete_confirm=0;
} else if(delete_confirm&&wm_state[2]==1&&bob_gui_button_event(delete_no,delete_no_state,event)) {
delete_confirm=0;
if(selected_file>=0)gui_select_file(selected_file,file_slots,file_count,&selected_file,message_b,preview,&preview_length,&preview_scroll);
}
if(wm_state[2]==0&&bob_gui_button_event(button,button_state,event)) {
if(bob_file_write(file_name_text,file_text,bob_strlen(file_text))==0) {
bob_strcpy(message_b,"Saved file.");selected_file=-1;selection_initialized=0;
} else bob_strcpy(message_b,"Save failed; check name.");
}
continue;
}
if(event[0]==BOB_EVENT_KEY_DOWN) {
if(wm_state[2]==0) {
if(name_state[0])bob_gui_text_field_event(name_field,file_name_text,name_state,24,event);
else if(text_state[0])bob_gui_text_area_event(text_area,file_text,text_state,512,event);
}
else if(event[1]==256&&preview_scroll>0)preview_scroll--;
else if(event[1]==257&&gui_preview_offset(preview,preview_length,preview_scroll+1)<preview_length)preview_scroll++;
continue;
}
if(event[0]!=BOB_EVENT_CHAR)continue;
if(event[1]==27)running=0;
else if(event[1]=='\t') {
if(wm_state[2]==0&&name_state[0]){name_state[0]=0;text_state[0]=1;text_state[1]=bob_strlen(file_text);}
else if(wm_state[2]==0&&text_state[0]){text_state[0]=0;bob_wm_focus(wm_state,wm_windows,wm_order,1);}
else {bob_wm_focus(wm_state,wm_windows,wm_order,0);name_state[0]=1;name_state[1]=bob_strlen(file_name_text);text_state[0]=0;}
}
else if(event[1]=='?') {
help_open=1;help_close_state[0]=0;help_close_state[1]=0;
}
else if(wm_state[2]==1&&event[1]=='a') {
launcher_open=1;app_selected=0;launch_button_state[0]=0;launch_button_state[1]=0;
close_button_state[0]=0;close_button_state[1]=0;
bob_strcpy(message_b,app_count?"Choose an app.":"No stored apps.");
}
else if(wm_state[2]==1&&event[1]=='m') {menu_open=1;menu_state[0]=0;menu_state[1]=-1;}
else if(event[1]==19) {
if(bob_file_write(file_name_text,file_text,bob_strlen(file_text))==0) {
bob_strcpy(message_b,"Saved file.");selected_file=-1;selection_initialized=0;
} else bob_strcpy(message_b,"Save failed; check name.");
}
else if(wm_state[2]==1&&event[1]=='e') {
gui_open_selected_text(selected_file,file_slots,file_count,file_name,
file_name_text,file_text,text_state,text_area,wm_state,wm_windows,wm_order,message_b);
}
else if(wm_state[2]==1&&delete_confirm&&event[1]=='y') {
if(gui_delete_selected(selected_file,file_slots,file_count,file_name)==0) {
selected_file=-1;selection_initialized=1;preview_length=-1;preview_scroll=0;
bob_strcpy(message_b,"Deleted.");
} else bob_strcpy(message_b,"Delete failed.");
delete_confirm=0;
} else if(wm_state[2]==1&&delete_confirm&&event[1]=='n') {
delete_confirm=0;
if(selected_file>=0)gui_select_file(selected_file,file_slots,file_count,&selected_file,message_b,preview,&preview_length,&preview_scroll);
bob_strcpy(message_b,"Delete cancelled.");
} else if(wm_state[2]==1&&delete_confirm) {
}
else if(wm_state[2]==1&&event[1]=='n'&&selected_file+1<file_count) {
gui_select_file(selected_file+1,file_slots,file_count,&selected_file,message_b,preview,&preview_length,&preview_scroll);
} else if(wm_state[2]==1&&event[1]=='p'&&selected_file>0) {
gui_select_file(selected_file-1,file_slots,file_count,&selected_file,message_b,preview,&preview_length,&preview_scroll);
} else if(wm_state[2]==1&&event[1]=='x'&&selected_file>=0) {
int selected_size,selected_kind;
selected_kind=bob_file_list(file_slots[selected_file],file_name,24,&selected_size);
if(selected_kind==0){delete_confirm=1;delete_yes_state[0]=0;delete_yes_state[1]=0;delete_no_state[0]=0;delete_no_state[1]=0;bob_strcpy(message_b,"Delete? Y/N");}
else bob_strcpy(message_b,"Text files only.");
} else if(wm_state[2]==1&&event[1]=='u'&&preview_scroll>0)preview_scroll--;
else if(wm_state[2]==1&&event[1]=='d'&&gui_preview_offset(preview,preview_length,preview_scroll+1)<preview_length)preview_scroll++;
else if(wm_state[2]==0) {
if(name_state[0])bob_gui_text_field_event(name_field,file_name_text,name_state,24,event);
else bob_gui_text_area_event(text_area,file_text,text_state,512,event);
}
else if(event[1]==8||event[1]==127) {
if(wm_state[2]==1&&length_b>0)message_b[--length_b]=0;
} else if(event[1]>=32&&event[1]<127) {
if(wm_state[2]==1&&length_b<30) {
message_b[length_b++]=event[1];message_b[length_b]=0;
}
}
}
if(bob_gfx_leave())return 2;
bob_puts("bob!");
return 0;
}
+51
View File
@@ -0,0 +1,51 @@
#include "bob.h"
#include "bob_fs.h"
#include "bob_process.h"
#include "bob_string.h"
static int append(char *buffer,int *length,char value) {
if(*length>=126)return 0;
buffer[(*length)++]=value;buffer[*length]=0;return 1;
}
static void print_number(int value) {
char digits[12];int count=0;
if(value<0){bob_putc('-');value=-value;}
do {digits[count++]=(char)('0'+value%10);value/=10;} while(value&&count<11);
while(count)bob_putc(digits[--count]);
bob_putc('\n');
}
int main(int argc,char **argv) {
int i,size,kind,length=0,status,child_status=-1;
char name[24],arguments[128];
arguments[0]=0;
if(argc<2) {
bob_puts("Native apps:");
for(i=0;i<8;i++) {
kind=bob_file_list(i,name,24,&size);
if(kind==3){bob_puts(name);bob_putc('\n');}
}
bob_puts("Usage: run launcher APP [ARG ...]");
return 0;
}
for(i=2;i<argc;i++) {
int j;
if(i>2&&!append(arguments,&length,' '))return 1;
if(!append(arguments,&length,'"'))return 1;
for(j=0;argv[i][j];j++) {
if((argv[i][j]=='"'||argv[i][j]=='\\')&&!append(arguments,&length,'\\'))return 1;
if(!append(arguments,&length,argv[i][j]))return 1;
}
if(!append(arguments,&length,'"'))return 1;
}
status=bob_app_run(argv[1],arguments,&child_status);
if(status==-1)bob_puts("App not found.");
else if(status==-2)bob_puts("File is not a native bob32 app.");
else if(status==-4)bob_puts("Invalid app or arguments.");
else if(status==-5)bob_puts("Not enough emulator memory to launch app.");
else if(status==-3)bob_puts("App timed out.");
else if(status<-5)bob_puts("App could not be launched.");
else {bob_puts("Child exit:");print_number(child_status);}
return status<0?1:child_status;
}
+29
View File
@@ -0,0 +1,29 @@
#include "bob.h"
#include "bob_fs.h"
#include "bob_string.h"
static void write_text(const char *text) {
while (*text) bob_putc(*text++);
}
int main(void) {
char name[24];
char number[12];
int size;
int kind;
int i;
for (i = 0; i < 8; i++) {
kind = bob_file_list(i, name, 24, &size);
if (kind < 0) continue;
write_text(name);
bob_putc(' ');
bob_format_int(size, number, 12);
write_text(number);
bob_putc(' ');
if (kind == 0) write_text("text");
else if (kind == 3) write_text("native app");
else write_text("program");
bob_putc('\n');
}
return 0;
}
+103
View File
@@ -0,0 +1,103 @@
#include "bob.h"
#include "bob_fs.h"
static int same_text(const char *left,const char *right) {
int i=0;while(left[i]&&left[i]==right[i])i++;
return left[i]==right[i];
}
/* 1=text note, 0=missing, -1=name belongs to a non-text file. */
static int note_kind(const char *wanted) {
int kind=bob_file_kind(wanted);
if(kind<0)return 0;
return kind==BOB_FILE_KIND_TEXT?1:-1;
}
static int join_arguments(char *text,int *length,int argc,char **argv,int start) {
*length=0;
for(int i=start;i<argc;i++) {
if(i>start) {
if(*length>=511)return 0;
text[(*length)++]=' ';
}
for(int j=0;argv[i][j];j++) {
if(*length>=511)return 0;
text[(*length)++]=argv[i][j];
}
}
text[*length]=0;
return 1;
}
int main(int argc, char **argv) {
char text[512];
int length;
int i;
if (argc < 2) {
bob_puts("Usage: notes show NAME | put/edit NAME [TEXT] | add NAME [TEXT] | delete NAME");
return 1;
}
if (same_text(argv[1], "show") && argc == 3) {
if(note_kind(argv[2])<0){bob_puts("Not a text note.");return 1;}
length = bob_file_read(argv[2], text, 512);
if (length < 0) { bob_puts("Note not found."); return 1; }
for (i = 0; i < length; i++) bob_putc(text[i]);
bob_putc('\n');
return 0;
}
if ((same_text(argv[1], "put")||same_text(argv[1], "edit")) && argc >= 3) {
if(note_kind(argv[2])<0){bob_puts("Not a text note.");return 1;}
if (argc == 3) {
bob_puts("Note text:");
bob_gets(text, 512);
length=0;while(text[length])length++;
} else if(!join_arguments(text,&length,argc,argv,3)) {
bob_puts("Note is too long (511 characters maximum).");return 1;
}
if (bob_file_write(argv[2], text, length)) { bob_puts("Cannot save note."); return 1; }
bob_puts("Saved.");
return 0;
}
if (same_text(argv[1], "add") && argc >= 3) {
int kind=note_kind(argv[2]);
if(kind<0){bob_puts("Not a text note.");return 1;}
if(kind) {
length=bob_file_read(argv[2],text,512);
if(length<0){bob_puts("Cannot read note.");return 1;}
} else {length=0;text[0]=0;}
if(argc==3) {
if(length&&text[length-1]!='\n') {
if(length>=511){bob_puts("Note is too long (511 characters maximum).");return 1;}
text[length++]='\n';
}
bob_puts("Add text:");bob_gets(text+length,512-length);
while(text[length])length++;
} else {
if(length&&text[length-1]!='\n') {
if(length>=511){bob_puts("Note is too long (511 characters maximum).");return 1;}
text[length++]='\n';
}
for(i=3;i<argc;i++) {
if(i>3) {
if(length>=511){bob_puts("Note is too long (511 characters maximum).");return 1;}
text[length++]=' ';
}
for(int j=0;argv[i][j];j++) {
if(length>=511){bob_puts("Note is too long (511 characters maximum).");return 1;}
text[length++]=argv[i][j];
}
}
text[length]=0;
}
if(bob_file_write(argv[2],text,length)){bob_puts("Cannot save note.");return 1;}
bob_puts("Saved.");return 0;
}
if (same_text(argv[1], "delete") && argc == 3) {
if(note_kind(argv[2])!=1){bob_puts("Not a text note.");return 1;}
if (bob_file_delete(argv[2])) { bob_puts("Note not found."); return 1; }
bob_puts("Deleted.");
return 0;
}
bob_puts("Usage: notes show NAME | put/edit NAME [TEXT] | add NAME [TEXT] | delete NAME");
return 1;
}
+50
View File
@@ -0,0 +1,50 @@
#include "bob.h"
#include "bob_fs.h"
#include "bob_system.h"
#include "bob_string.h"
static void write_text(const char *text) {
while (*text) bob_putc(*text++);
}
static void show_value(const char *label, int value) {
char number[12];
write_text(label);
bob_format_int(value, number, 12);
write_text(number);
bob_putc('\n');
}
int main(void) {
int info[6];
int capabilities[5];
if (bob_system_info(info, 6) != 6 || info[0] != BOB_FS_API_VERSION) {
bob_puts("System information service unavailable.");
return 1;
}
if (bob_system_capabilities(capabilities, 4) != -1) {
bob_puts("System capability validation failed.");
return 2;
}
if (bob_system_capabilities(capabilities, 5) != 5 ||
capabilities[0] != BOB_SYSTEM_API_VERSION) {
bob_puts("System capabilities unavailable.");
return 3;
}
bob_puts("bob32 system information");
show_value("System API version: ", capabilities[0]);
bob_puts((capabilities[1] & BOB_SYSTEM_DEVICE_CELL_FRAMEBUFFER) ?
"Cell framebuffer: available" : "Cell framebuffer: unavailable");
show_value("Display width (cells): ", capabilities[2]);
show_value("Display height (cells): ", capabilities[3]);
bob_puts((capabilities[4] & BOB_SYSTEM_INPUT_KEYBOARD) ?
"Keyboard input: available" : "Keyboard input: unavailable");
bob_puts((capabilities[4] & BOB_SYSTEM_INPUT_MOUSE) ?
"Mouse input: available" : "Mouse input: unavailable");
show_value("Application address space (words): ", info[1]);
show_value("Mapped application pages: ", info[2]);
show_value("Guest files: ", info[3]);
show_value("Filesystem words used: ", info[4]);
show_value("Filesystem words free: ", info[5]);
return 0;
}