app platform split

This commit is contained in:
Allen Webster 2015-11-13 23:42:06 -05:00
parent febbe1775f
commit 95104afd37
19 changed files with 1143 additions and 3883 deletions

View File

@ -223,9 +223,7 @@ struct Buffer_Summary{
const char *buffer_name;
int file_cursor_pos;
int is_lexed;
int map_id;
};
@ -263,6 +261,11 @@ struct Application_Links{
Get_Active_Buffer_Function *get_active_buffer;
};
struct Config_API{
Get_Binding_Data_Function *get_bindings;
Set_Extra_Font_Function *set_extra_font;
};
// NOTE(allen): definitions for the buffer that communicates to 4ed.exe
enum Binding_Unit_Type{

293
4ed.cpp
View File

@ -75,6 +75,8 @@ struct App_Vars{
App_State state;
App_State_Resizing resizing;
Panel *prev_mouse_panel;
Config_API config_api;
};
internal i32
@ -141,8 +143,8 @@ globalvar Application_Links app_links;
#define REQ_COLOR_VIEW(n) Color_View *n = view_to_color_view(command->view); if (!n) return
#define REQ_DBG_VIEW(n) Debug_View *n = view_to_debug_view(command->view); if (!n) return
#define COMMAND_DECL(n) internal void command_##n(Command_Data *command, Command_Binding binding)
#define COMPOSE_DECL(n) internal void n(Command_Data *command, Command_Binding binding)
#define COMMAND_DECL(n) internal void command_##n(System_Functions *system, Command_Data *command, Command_Binding binding)
#define COMPOSE_DECL(n) internal void n(System_Functions *system, Command_Data *command, Command_Binding binding)
struct Command_Data{
Mem_Options *mem;
@ -159,6 +161,7 @@ struct Command_Data{
Key_Single key;
Partition part;
System_Functions *system;
};
struct Command_Parameter{
@ -222,7 +225,7 @@ COMMAND_DECL(write_character){
i32 pos;
pos = view->cursor.pos;
i32 next_cursor_pos = view->cursor.pos + string.size;
view_replace_range(mem, view, layout, pos, pos, (u8*)string.str, string.size, next_cursor_pos);
view_replace_range(system, mem, view, layout, pos, pos, (u8*)string.str, string.size, next_cursor_pos);
view_cursor_move(view, next_cursor_pos);
if (view->mark >= pos) view->mark += string.size;
file->cursor_pos = view->cursor.pos;
@ -454,7 +457,7 @@ COMMAND_DECL(copy){
Range range = get_range(view->cursor.pos, view->mark);
if (range.start < range.end){
clipboard_copy(&mem->general, working_set, range, file);
clipboard_copy(system, &mem->general, working_set, range, file);
}
}
@ -470,11 +473,11 @@ COMMAND_DECL(cut){
if (range.start < range.end){
i32 next_cursor_pos = range.start;
clipboard_copy(&mem->general, working_set, range, file);
view_replace_range(mem, view, layout, range.start, range.end, 0, 0, next_cursor_pos);
clipboard_copy(system, &mem->general, working_set, range, file);
view_replace_range(system, mem, view, layout, range.start, range.end, 0, 0, next_cursor_pos);
view->mark = range.start;
view_measure_wraps(&mem->general, view);
view_measure_wraps(system, &mem->general, view);
view_cursor_move(view, next_cursor_pos);
}
}
@ -494,7 +497,7 @@ COMMAND_DECL(paste){
i32 pos_left = view->cursor.pos;
i32 next_cursor_pos = pos_left+src->size;
view_replace_range(mem, view, layout, pos_left, pos_left, (u8*)src->str, src->size, next_cursor_pos);
view_replace_range(system, mem, view, layout, pos_left, pos_left, (u8*)src->str, src->size, next_cursor_pos);
view_cursor_move(view, next_cursor_pos);
view->mark = pos_left;
@ -528,7 +531,8 @@ COMMAND_DECL(paste_next){
Range range = get_range(view->mark, view->cursor.pos);
String *src = working_set_clipboard_roll_down(working_set);
i32 next_cursor_pos = range.start+src->size;
view_replace_range(mem, view, layout, range.start, range.end,
view_replace_range(system,
mem, view, layout, range.start, range.end,
(u8*)src->str, src->size, next_cursor_pos);
view_cursor_move(view, next_cursor_pos);
@ -548,7 +552,7 @@ COMMAND_DECL(paste_next){
}
}
else{
command_paste(command, binding);
command_paste(system, command, binding);
}
}
@ -562,8 +566,8 @@ COMMAND_DECL(delete_range){
Range range = get_range(view->cursor.pos, view->mark);
if (range.start < range.end){
i32 next_cursor_pos = range.start;
view_replace_range(mem, view, layout, range.start, range.end, 0, 0, next_cursor_pos);
view_measure_wraps(&mem->general, view);
view_replace_range(system, mem, view, layout, range.start, range.end, 0, 0, next_cursor_pos);
view_measure_wraps(system, &mem->general, view);
view_cursor_move(view, next_cursor_pos);
view->mark = range.start;
}
@ -586,7 +590,7 @@ COMMAND_DECL(undo){
USE_LAYOUT(layout);
USE_MEM(mem);
view_undo(mem, layout, view);
view_undo(system, mem, layout, view);
}
COMMAND_DECL(redo){
@ -596,7 +600,7 @@ COMMAND_DECL(redo){
USE_LAYOUT(layout);
USE_MEM(mem);
view_redo(mem, layout, view);
view_redo(system, mem, layout, view);
}
COMMAND_DECL(increase_rewind_speed){
@ -642,7 +646,7 @@ COMMAND_DECL(history_backward){
USE_LAYOUT(layout);
USE_MEM(mem);
view_history_step(mem, layout, view, hist_backward);
view_history_step(system, mem, layout, view, hist_backward);
}
COMMAND_DECL(history_forward){
@ -652,7 +656,7 @@ COMMAND_DECL(history_forward){
USE_LAYOUT(layout);
USE_MEM(mem);
view_history_step(mem, layout, view, hist_forward);
view_history_step(system, mem, layout, view, hist_forward);
}
COMMAND_DECL(save_history){
@ -661,7 +665,7 @@ COMMAND_DECL(save_history){
REQ_FILE(file, view);
USE_MEM(mem);
file_dump_history(mem, file, "history_data.hst");
file_dump_history(system, mem, file, "history_data.hst");
}
COMMAND_DECL(interactive_new){
@ -675,11 +679,12 @@ COMMAND_DECL(interactive_new){
USE_DELAY(delay);
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_minor(new_view, panel, live_set);
view_replace_minor(system, new_view, panel, live_set);
new_view->map = &vars->map_ui;
Interactive_View *int_view =
interactive_view_init(new_view, &vars->hot_directory, style,
interactive_view_init(system,
new_view, &vars->hot_directory, style,
working_set, delay);
int_view->interaction = INTV_SYS_FILE_LIST;
int_view->action = INTV_NEW;
@ -687,7 +692,8 @@ COMMAND_DECL(interactive_new){
}
internal File_View*
app_open_file(App_Vars *vars, Mem_Options *mem, Panel *panel,
app_open_file(System_Functions *system,
App_Vars *vars, Mem_Options *mem, Panel *panel,
Working_Set *working_set, String *string, Style *style,
Live_Views *live_set, Command_Data *command_data){
File_View *result = 0;
@ -699,7 +705,7 @@ app_open_file(App_Vars *vars, Mem_Options *mem, Panel *panel,
Get_File_Result file = working_set_get_available_file(working_set);
if (file.file){
file_get_dummy(file.file);
created_file = file_create(mem, file.file, (u8*)string->str, style->font);
created_file = file_create(system, mem, file.file, string->str, style->font);
table_add(&working_set->table, file.file->source_path, file.index);
if (created_file){
target_file = file.file;
@ -710,7 +716,7 @@ app_open_file(App_Vars *vars, Mem_Options *mem, Panel *panel,
if (target_file){
View *new_view = live_set_alloc_view(live_set, &vars->mem);
view_replace_major(new_view, panel, live_set);
view_replace_major(system, new_view, panel, live_set);
File_View *file_view = file_view_init(new_view, &vars->layout);
result = file_view;
@ -722,7 +728,8 @@ app_open_file(App_Vars *vars, Mem_Options *mem, Panel *panel,
Temp_Memory temp = begin_temp_memory(&vars->mem.part);
command_data->part = partition_sub_part(&vars->mem.part, 16 << 10);
view_set_file(file_view, target_file, style,
view_set_file(system,
file_view, target_file, style,
vars->hooks[hook_open_file], command_data, app_links);
command_data->part = old_part;
@ -734,7 +741,7 @@ app_open_file(App_Vars *vars, Mem_Options *mem, Panel *panel,
#if BUFFER_EXPERIMENT_SCALPEL <= 0
if (created_file && target_file->tokens_exist)
file_first_lex_parallel(&mem->general, target_file);
file_first_lex_parallel(system, &mem->general, target_file);
#endif
}
@ -771,17 +778,19 @@ COMMAND_DECL(interactive_open){
if (filename){
String string = make_string(filename, filename_len);
if (app_open_file(vars, mem, panel, working_set,
if (app_open_file(system,
vars, mem, panel, working_set,
&string, style, live_set, command)) made_file = 1;
}
if (!made_file){
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_minor(new_view, panel, live_set);
view_replace_minor(system, new_view, panel, live_set);
new_view->map = &vars->map_ui;
Interactive_View *int_view =
interactive_view_init(new_view, &vars->hot_directory, style,
interactive_view_init(system,
new_view, &vars->hot_directory, style,
working_set, delay);
int_view->interaction = INTV_SYS_FILE_LIST;
int_view->action = INTV_OPEN;
@ -803,21 +812,23 @@ COMMAND_DECL(reopen){
USE_VARS(vars);
Editing_File temp_file;
if (file_create(mem, &temp_file, (u8*)make_c_str(file->source_path), style->font)){
file_close(&mem->general, file);
if (file_create(system,
mem, &temp_file, make_c_str(file->source_path), style->font)){
file_close(system, &mem->general, file);
*file = temp_file;
file->source_path.str = file->source_path_;
file->live_name.str = file->live_name_;
#if BUFFER_EXPERIMENT_SCALPEL <= 0
if (file->tokens_exist)
file_first_lex_parallel(&mem->general, file);
file_first_lex_parallel(system, &mem->general, file);
#endif
Partition old_part = command->part;
Temp_Memory temp = begin_temp_memory(&vars->mem.part);
command->part = partition_sub_part(&vars->mem.part, 16 << 10);
view_set_file(view, file, style,
view_set_file(system,
view, file, style,
vars->hooks[hook_open_file], command, app_links);
command->part = old_part;
@ -830,7 +841,7 @@ COMMAND_DECL(reopen){
View *current_view_ = current_panel->view;
File_View *current_view = view_to_file_view(current_view_);
if (current_view && current_view != view && current_view->file == file){
view_set_file(current_view, current_view->file, style, 0, command, app_links);
view_set_file(system, current_view, current_view->file, style, 0, command, app_links);
}
}
}
@ -844,7 +855,7 @@ COMMAND_DECL(save){
String *file_path = &file->source_path;
if (file_path->size > 0){
file_save(&mem->part, file, file_path->str);
file_save(system, &mem->part, file, file_path->str);
}
}
@ -859,11 +870,12 @@ COMMAND_DECL(interactive_save_as){
USE_DELAY(delay);
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_minor(new_view, panel, live_set);
view_replace_minor(system, new_view, panel, live_set);
new_view->map = &vars->map_ui;
Interactive_View *int_view =
interactive_view_init(new_view, &vars->hot_directory, style,
interactive_view_init(system,
new_view, &vars->hot_directory, style,
working_set, delay);
int_view->interaction = INTV_SYS_FILE_LIST;
int_view->action = INTV_SAVE_AS;
@ -892,11 +904,12 @@ COMMAND_DECL(interactive_switch_buffer){
USE_DELAY(delay);
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_minor(new_view, panel, live_set);
view_replace_minor(system, new_view, panel, live_set);
new_view->map = &vars->map_ui;
Interactive_View *int_view =
interactive_view_init(new_view, &vars->hot_directory, style,
interactive_view_init(system,
new_view, &vars->hot_directory, style,
working_set, delay);
int_view->interaction = INTV_LIVE_FILE_LIST;
int_view->action = INTV_SWITCH;
@ -914,11 +927,12 @@ COMMAND_DECL(interactive_kill_buffer){
USE_DELAY(delay);
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_minor(new_view, panel, live_set);
view_replace_minor(system, new_view, panel, live_set);
new_view->map = &vars->map_ui;
Interactive_View *int_view =
interactive_view_init(new_view, &vars->hot_directory, style,
interactive_view_init(system,
new_view, &vars->hot_directory, style,
working_set, delay);
int_view->interaction = INTV_LIVE_FILE_LIST;
int_view->action = INTV_KILL;
@ -969,16 +983,17 @@ COMMAND_DECL(toggle_tokens){
USE_MEM(mem);
if (file->tokens_exist){
file_kill_tokens(&mem->general, file);
file_kill_tokens(system, &mem->general, file);
}
else{
file_first_lex_parallel(&mem->general, file);
file_first_lex_parallel(system, &mem->general, file);
}
#endif
}
internal void
case_change_range(Mem_Options *mem, File_View *view, Editing_File *file,
case_change_range(System_Functions *system,
Mem_Options *mem, File_View *view, Editing_File *file,
u8 a, u8 z, u8 char_delta){
#if BUFFER_EXPERIMENT_SCALPEL <= 0
Range range = get_range(view->cursor.pos, view->mark);
@ -990,7 +1005,7 @@ case_change_range(Mem_Options *mem, File_View *view, Editing_File *file,
step.edit.len = range.end - range.start;
if (file->still_lexing)
system_cancel_job(BACKGROUND_THREADS, file->lex_job);
system->cancel_job(BACKGROUND_THREADS, file->lex_job);
file_update_history_before_edit(mem, file, step, 0, hist_normal);
@ -1002,7 +1017,7 @@ case_change_range(Mem_Options *mem, File_View *view, Editing_File *file,
}
if (file->token_stack.tokens)
file_relex_parallel(mem, file, range.start, range.end, 0);
file_relex_parallel(system, mem, file, range.start, range.end, 0);
}
#endif
}
@ -1012,7 +1027,7 @@ COMMAND_DECL(to_uppercase){
REQ_FILE_VIEW(view);
REQ_FILE(file, view);
USE_MEM(mem);
case_change_range(mem, view, file, 'a', 'z', (u8)('A' - 'a'));
case_change_range(system, mem, view, file, 'a', 'z', (u8)('A' - 'a'));
}
COMMAND_DECL(to_lowercase){
@ -1020,7 +1035,7 @@ COMMAND_DECL(to_lowercase){
REQ_FILE_VIEW(view);
REQ_FILE(file, view);
USE_MEM(mem);
case_change_range(mem, view, file, 'A', 'Z', (u8)('a' - 'A'));
case_change_range(system, mem, view, file, 'A', 'Z', (u8)('a' - 'A'));
}
COMMAND_DECL(clean_all_lines){
@ -1030,7 +1045,7 @@ COMMAND_DECL(clean_all_lines){
USE_LAYOUT(layout);
USE_MEM(mem);
view_clean_whitespace(mem, view, layout);
view_clean_whitespace(system, mem, view, layout);
}
COMMAND_DECL(eol_dosify){
@ -1039,7 +1054,7 @@ COMMAND_DECL(eol_dosify){
REQ_FILE(file, view);
file->dos_write_mode = 1;
file->last_4ed_edit_time = system_get_now();
file->last_4ed_edit_time = system->time_stamp_now();
}
COMMAND_DECL(eol_nixify){
@ -1048,7 +1063,7 @@ COMMAND_DECL(eol_nixify){
REQ_FILE(file, view);
file->dos_write_mode = 0;
file->last_4ed_edit_time = system_get_now();
file->last_4ed_edit_time = system->time_stamp_now();
}
COMMAND_DECL(auto_tab){
@ -1075,7 +1090,7 @@ COMMAND_DECL(auto_tab_range){
if (file->token_stack.tokens && file->tokens_complete){
Range range = get_range(view->cursor.pos, view->mark);
view_auto_tab_tokens(mem, view, layout, range.start, range.end, 1);
view_auto_tab_tokens(system, mem, view, layout, range.start, range.end, 1);
}
}
@ -1088,7 +1103,7 @@ COMMAND_DECL(auto_tab_line_at_cursor){
if (file->token_stack.tokens && file->tokens_complete){
i32 pos = view->cursor.pos;
view_auto_tab_tokens(mem, view, layout, pos, pos, 0);
view_auto_tab_tokens(system, mem, view, layout, pos, pos, 0);
}
}
@ -1100,7 +1115,7 @@ COMMAND_DECL(auto_tab_whole_file){
USE_MEM(mem);
if (file->token_stack.tokens && file->tokens_complete){
view_auto_tab_tokens(mem, view, layout, 0, buffer_size(&file->buffer), 1);
view_auto_tab_tokens(system, mem, view, layout, 0, buffer_size(&file->buffer), 1);
}
}
@ -1164,7 +1179,7 @@ COMMAND_DECL(close_panel){
if (layout->panel_count > 1){
if (view){
live_set_free_view(command->live_set, view);
live_set_free_view(system, command->live_set, view);
panel->view = 0;
}
@ -1265,7 +1280,7 @@ COMMAND_DECL(delete){
Assert(shift > 0);
i32 next_cursor_pos = start;
view_replace_range(mem, view, layout, start, end, 0, 0, next_cursor_pos);
view_replace_range(system, mem, view, layout, start, end, 0, 0, next_cursor_pos);
view_cursor_move(view, next_cursor_pos);
if (view->mark >= end) view->mark -= shift;
}
@ -1289,7 +1304,7 @@ COMMAND_DECL(backspace){
Assert(shift > 0);
i32 next_cursor_pos = view->cursor.pos - shift;
view_replace_range(mem, view, layout, start, end, 0, 0, next_cursor_pos);
view_replace_range(system, mem, view, layout, start, end, 0, 0, next_cursor_pos);
view_cursor_move(view, next_cursor_pos);
if (view->mark >= end) view->mark -= shift;
}
@ -1371,9 +1386,10 @@ COMMAND_DECL(page_up){
}
inline void
open_theme_options(App_Vars *vars, Live_Views *live_set, Mem_Options *mem, Panel *panel){
open_theme_options(System_Functions *system,
App_Vars *vars, Live_Views *live_set, Mem_Options *mem, Panel *panel){
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_minor(new_view, panel, live_set);
view_replace_minor(system, new_view, panel, live_set);
new_view->map = &vars->map_ui;
Color_View *color_view = color_view_init(new_view, &vars->working_set);
@ -1392,7 +1408,7 @@ COMMAND_DECL(open_color_tweaker){
USE_MEM(mem);
USE_PANEL(panel);
open_theme_options(vars, live_set, mem, panel);
open_theme_options(system, vars, live_set, mem, panel);
}
COMMAND_DECL(open_menu){
@ -1405,7 +1421,7 @@ COMMAND_DECL(open_menu){
USE_STYLE(style);
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_minor(new_view, panel, live_set);
view_replace_minor(system, new_view, panel, live_set);
new_view->map = &vars->map_ui;
Menu_View *menu_view = menu_view_init(new_view, style, working_set, &vars->delay);
@ -1422,7 +1438,7 @@ COMMAND_DECL(open_debug_view){
USE_MEM(mem);
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_major(new_view, panel, live_set);
view_replace_major(system, new_view, panel, live_set);
new_view->map = &vars->map_debug;
Debug_View *debug_view = debug_view_init(new_view);
@ -1442,16 +1458,6 @@ COMMAND_DECL(debug_os_events){
view->mode = DBG_OS_EVENTS;
}
COMMAND_DECL(debug_profile){
ProfileMomentFunction();
REQ_DBG_VIEW(view);
view->mode = DBG_PROFILE;
}
COMMAND_DECL(pause_unpause_profile){
ProfileMomentFunction();
INTERNAL_updating_profile = !INTERNAL_updating_profile;
}
#endif
COMMAND_DECL(close_minor_view){
@ -1460,7 +1466,7 @@ COMMAND_DECL(close_minor_view){
USE_PANEL(panel);
USE_LIVE_SET(live_set);
view_remove_minor(panel, live_set);
view_remove_minor(system, panel, live_set);
}
COMMAND_DECL(cursor_mark_swap){
@ -1495,10 +1501,10 @@ COMMAND_DECL(set_settings){
#if BUFFER_EXPERIMENT_SCALPEL <= 0
int v = dynamic_to_bool(&param->param.value);
if (file->tokens_exist){
if (!v) file_kill_tokens(&mem->general, file);
if (!v) file_kill_tokens(system, &mem->general, file);
}
else{
if (v) file_first_lex_parallel(&mem->general, file);
if (v) file_first_lex_parallel(system, &mem->general, file);
}
#endif
}break;
@ -1575,20 +1581,21 @@ COMMAND_DECL(build){
}
if (file){
file_create_super_locked(mem, file, (u8*)buffer_name, style->font);
file_create_super_locked(system, mem, file, buffer_name, style->font);
table_add(&working_set->table, file->live_name, index);
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_major(new_view, panel, live_set);
view_replace_major(system, new_view, panel, live_set);
File_View *file_view = file_view_init(new_view, &vars->layout);
view_set_file(file_view, file, style,
view_set_file(system,
file_view, file, style,
vars->hooks[hook_open_file], command, app_links);
new_view->map = app_get_map(vars, file->base_map_id);
i32 i = vars->cli_processes.count++;
CLI_Process *proc = vars->cli_processes.procs + i;
if (!system_cli_call(path, script, &proc->cli)){
if (!system->cli_call(path, script, &proc->cli)){
--vars->cli_processes.count;
}
proc->out_file = file;
@ -1618,15 +1625,15 @@ update_command_data(App_Vars *vars, Command_Data *cmd){
command_data.screen_height = cmd->screen_height;
command_data.key = cmd->key;
command_data.part = cmd->part;
command_data.system = cmd->system;
*cmd = command_data;
}
COMPOSE_DECL(compose_write_auto_tab_line){
command_write_character(command, binding);
command_write_character(system, command, binding);
update_command_data(command->vars, command);
command_auto_tab_line_at_cursor(command, binding);
command_auto_tab_line_at_cursor(system, command, binding);
update_command_data(command->vars, command);
}
@ -1638,7 +1645,7 @@ extern "C"{
Command_Function function = command_table[command_id];
Command_Binding binding;
binding.function = function;
if (function) function(cmd, binding);
if (function) function(cmd->system, cmd, binding);
update_command_data(cmd->vars, cmd);
}
@ -1715,7 +1722,6 @@ setup_debug_commands(Command_Map *commands, Partition *part, Key_Codes *codes, C
map_add(commands, 'm', MDFR_NONE, command_debug_memory);
map_add(commands, 'o', MDFR_NONE, command_debug_os_events);
map_add(commands, 'p', MDFR_NONE, command_debug_profile);
}
#endif
@ -1804,7 +1810,6 @@ setup_top_commands(Command_Map *commands, Partition *part, Key_Codes *codes, Com
#if FRED_INTERNAL
map_add(commands, 'd', MDFR_ALT, command_open_debug_view);
map_add(commands, 'p', MDFR_CTRL | MDFR_ALT, command_pause_unpause_profile);
#endif
map_add(commands, 'p', MDFR_CTRL, command_open_panel_vsplit);
@ -2201,9 +2206,10 @@ app_hardcode_styles(App_Vars *vars){
}
internal bool32
app_load_font(Font *font, char *filename, i32 size, void *memory,
app_load_font(System_Functions *system,
Font *font, char *filename, i32 size, void *memory,
i32 *used, i32 tab_width, String name){
if (font_load(font, filename, size, memory, font_predict_size(size), used, tab_width)){
if (font_load(system, font, filename, size, memory, font_predict_size(size), used, tab_width)){
font->loaded = 1;
font->name_[ArrayCount(font->name_)-1] = 0;
font->name = make_string(font->name_, 0, ArrayCount(font->name_)-1);
@ -2256,15 +2262,14 @@ HOOK_SIG(default_open_file_hook){
app.clear_parameters(cmd_context);
}
internal bool32
app_init(Thread_Context *thread, Application_Memory *memory,
Key_Codes *loose_codes, Clipboard_Contents clipboard){
external App_Init_Sig(app_init){
app_links_init();
Partition _partition = partition_open(memory->vars_memory, memory->vars_memory_size);
App_Vars *vars = push_struct(&_partition, App_Vars);
Assert(vars);
*vars = {};
vars->config_api = api;
vars->mem.part = _partition;
Partition *partition = &vars->mem.part;
general_memory_open(&vars->mem.general, memory->target_memory, memory->target_memory_size);
@ -2319,14 +2324,14 @@ app_init(Thread_Context *thread, Application_Memory *memory,
setup_command_table();
Command_Map *global = &vars->map_top;
if (TEMP.get_bindings){
if (vars->config_api.get_bindings){
i32 size = partition_remaining(partition);
void *data = partition_current(partition);
// TODO(allen): Use a giant bubble of general memory for this.
// So that it doesn't interfere with the command maps as they allocate
// their own memory.
i32 wanted_size = TEMP.get_bindings(data, size, loose_codes);
i32 wanted_size = vars->config_api.get_bindings(data, size, loose_codes);
bool32 did_top = 0;
bool32 did_file = 0;
@ -2458,41 +2463,47 @@ app_init(Thread_Context *thread, Application_Memory *memory,
i32 memory_used;
memory_used = 0;
app_load_font(vars->fonts.fonts + font_count++, "liberation-mono.ttf", 17,
app_load_font(system,
vars->fonts.fonts + font_count++, "liberation-mono.ttf", 17,
partition_current(partition),
&memory_used, 4, make_lit_string("liberation mono"));
push_block(partition, memory_used);
memory_used = 0;
app_load_font(vars->fonts.fonts + font_count++, "LiberationSans-Regular.ttf", 17,
app_load_font(system,
vars->fonts.fonts + font_count++, "LiberationSans-Regular.ttf", 17,
partition_current(partition),
&memory_used, 4, make_lit_string("liberation sans"));
push_block(partition, memory_used);
memory_used = 0;
app_load_font(vars->fonts.fonts + font_count++, "Hack-Regular.ttf", 17,
app_load_font(system,
vars->fonts.fonts + font_count++, "Hack-Regular.ttf", 17,
partition_current(partition),
&memory_used, 4, make_lit_string("hack"));
push_block(partition, memory_used);
memory_used = 0;
app_load_font(vars->fonts.fonts + font_count++, "CutiveMono-Regular.ttf", 17,
app_load_font(system,
vars->fonts.fonts + font_count++, "CutiveMono-Regular.ttf", 17,
partition_current(partition),
&memory_used, 4, make_lit_string("cutive mono"));
push_block(partition, memory_used);
memory_used = 0;
app_load_font(vars->fonts.fonts + font_count++, "Inconsolata-Regular.ttf", 17,
app_load_font(system,
vars->fonts.fonts + font_count++, "Inconsolata-Regular.ttf", 17,
partition_current(partition),
&memory_used, 4, make_lit_string("inconsolata"));
push_block(partition, memory_used);
if (TEMP.set_extra_font){
if (vars->config_api.set_extra_font){
Extra_Font extra;
extra.size = 17;
TEMP.set_extra_font(&extra);
vars->config_api.set_extra_font(&extra);
memory_used = 0;
if (app_load_font(vars->fonts.fonts + font_count, extra.file_name, extra.size,
if (app_load_font(system,
vars->fonts.fonts + font_count, extra.file_name, extra.size,
partition_current(partition),
&memory_used, 4, make_string_slowly(extra.font_name))){
++font_count;
@ -2545,7 +2556,7 @@ app_init(Thread_Context *thread, Application_Memory *memory,
panel_init(&panels[0]);
vars->hot_dir_base = make_fixed_width_string(vars->hot_dir_base_);
hot_directory_init(&vars->hot_directory, vars->hot_dir_base);
hot_directory_init(system, &vars->hot_directory, vars->hot_dir_base);
vars->mini_str = make_string((char*)vars->mini_buffer, 0, 512);
@ -2559,14 +2570,7 @@ app_init(Thread_Context *thread, Application_Memory *memory,
return 1;
}
internal Application_Step_Result
app_step(Thread_Context *thread, Key_Codes *codes,
Key_Input_Data *input, Mouse_State *mouse,
bool32 time_step, Render_Target *target,
Application_Memory *memory,
Clipboard_Contents clipboard,
bool32 first_step, bool32 force_redraw){
external App_Step_Sig(app_step){
ProfileStart(OS_syncing);
Application_Step_Result app_result = {};
app_result.redraw = force_redraw;
@ -2592,7 +2596,7 @@ app_step(Thread_Context *thread, Key_Codes *codes,
if (!file->is_dummy){
Time_Stamp time_stamp;
time_stamp = system_file_time_stamp((u8*)make_c_str(file->source_path));
time_stamp = system->file_time_stamp(make_c_str(file->source_path));
if (time_stamp.success){
file->last_sys_write_time = time_stamp.time;
@ -2616,8 +2620,8 @@ app_step(Thread_Context *thread, Key_Codes *codes,
Editing_File *out_file = proc->out_file;
i32 new_cursor = out_file->cursor_pos;
for (system_cli_begin_update(&proc->cli);
system_cli_update_step(&proc->cli, dest, max, &amount);){
for (system->cli_begin_update(&proc->cli);
system->cli_update_step(&proc->cli, dest, max, &amount);){
if (out_file){
amount = eol_in_place_convert_in(dest, amount);
Edit_Spec spec = {};
@ -2628,14 +2632,14 @@ app_step(Thread_Context *thread, Key_Codes *codes,
spec.step.pre_pos = new_cursor;
spec.step.post_pos = spec.step.edit.start + amount;
spec.str = (u8*)dest;
file_do_single_edit(&vars->mem, out_file,
file_do_single_edit(system, &vars->mem, out_file,
&vars->layout, spec, hist_normal);
app_result.redraw = 1;
new_cursor = spec.step.post_pos;
}
}
if (system_cli_end_update(&proc->cli)){
if (system->cli_end_update(&proc->cli)){
*proc = vars->cli_processes.procs[--count];
--i;
}
@ -2676,7 +2680,7 @@ app_step(Thread_Context *thread, Key_Codes *codes,
if (!view_->is_active) continue;
File_View *view = view_to_file_view(view_);
if (!view) continue;
view_measure_wraps(&vars->mem.general, view);
view_measure_wraps(system, &vars->mem.general, view);
view->cursor = view_compute_cursor_from_pos(view, view->cursor.pos);
}
app_result.redraw = 1;
@ -2926,6 +2930,7 @@ app_step(Thread_Context *thread, Key_Codes *codes,
command_data.vars = vars;
command_data.screen_width = target->width;
command_data.screen_height = target->height;
command_data.system = system;
Temp_Memory param_stack_temp = begin_temp_memory(&vars->mem.part);
command_data.part = partition_sub_part(&vars->mem.part, 16 << 10);
@ -2973,12 +2978,12 @@ app_step(Thread_Context *thread, Key_Codes *codes,
Handle_Command_Function *handle_command = 0;
if (view) handle_command = view->handle_command;
if (handle_command){
handle_command(view, &command_data, cmd, key, codes);
handle_command(system, view, &command_data, cmd, key, codes);
app_result.redraw = 1;
}
else{
if (cmd.function){
cmd.function(&command_data, cmd);
cmd.function(system, &command_data, cmd);
app_result.redraw = 1;
}
}
@ -3025,7 +3030,7 @@ app_step(Thread_Context *thread, Key_Codes *codes,
if (panel == mouse_panel){
input.mouse = mouse_data;
}
if (view_->do_view(thread, view_, panel->inner, active_view,
if (view_->do_view(system, thread, view_, panel->inner, active_view,
VMSG_STEP, 0, &input, &active_input)){
app_result.redraw = 1;
}
@ -3054,7 +3059,8 @@ app_step(Thread_Context *thread, Key_Codes *codes,
case DACT_OPEN:
{
command_data.view = (View*)
app_open_file(vars, mem, panel, working_set, string, style, live_set, &command_data);
app_open_file(system,
vars, mem, panel, working_set, string, style, live_set, &command_data);
}break;
case DACT_SAVE_AS:
@ -3066,7 +3072,7 @@ app_step(Thread_Context *thread, Key_Codes *codes,
if (fview){
Editing_File *file = fview->file;
if (file && !file->is_dummy){
file_save_and_set_names(&vars->mem.part, file, string->str);
file_save_and_set_names(system, &vars->mem.part, file, string->str);
}
}
}break;
@ -3075,26 +3081,27 @@ app_step(Thread_Context *thread, Key_Codes *codes,
{
Editing_File *file = working_set_lookup_file(working_set, *string);
if (!file->is_dummy){
file_save(&vars->mem.part, file, file->source_path.str);
file_save(system, &vars->mem.part, file, file->source_path.str);
}
}break;
case DACT_NEW:
{
Get_File_Result file = working_set_get_available_file(working_set);
file_create_empty(mem, file.file, (u8*)string->str, style->font);
file_create_empty(system, mem, file.file, string->str, style->font);
table_add(&working_set->table, file.file->source_path, file.index);
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_major(new_view, panel, live_set);
view_replace_major(system, new_view, panel, live_set);
File_View *file_view = file_view_init(new_view, &vars->layout);
command_data.view = (View*)file_view;
view_set_file(file_view, file.file, style,
view_set_file(system,
file_view, file.file, style,
vars->hooks[hook_open_file], &command_data, app_links);
new_view->map = app_get_map(vars, file.file->base_map_id);
#if BUFFER_EXPERIMENT_SCALPEL <= 0
if (file.file->tokens_exist) file_first_lex_parallel(general, file.file);
if (file.file->tokens_exist) file_first_lex_parallel(system, general, file.file);
#endif
}break;
@ -3103,12 +3110,13 @@ app_step(Thread_Context *thread, Key_Codes *codes,
Editing_File *file = working_set_lookup_file(working_set, *string);
if (file){
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_major(new_view, panel, live_set);
view_replace_major(system, new_view, panel, live_set);
File_View *file_view = file_view_init(new_view, &vars->layout);
command_data.view = (View*)file_view;
view_set_file(file_view, file, style,
view_set_file(system,
file_view, file, style,
vars->hooks[hook_open_file], &command_data, app_links);
new_view->map = app_get_map(vars, file->base_map_id);
@ -3120,7 +3128,7 @@ app_step(Thread_Context *thread, Key_Codes *codes,
Editing_File *file = working_set_lookup_file(working_set, *string);
if (file){
table_remove(&working_set->table, file->source_path);
kill_file(general, file, live_set, &vars->layout);
kill_file(system, general, file, live_set, &vars->layout);
}
}break;
@ -3133,18 +3141,19 @@ app_step(Thread_Context *thread, Key_Codes *codes,
case SYNC_GOOD:
{
table_remove(&working_set->table, file->source_path);
kill_file(general, file, live_set, &vars->layout);
view_remove_minor(panel, live_set);
kill_file(system, general, file, live_set, &vars->layout);
view_remove_minor(system, panel, live_set);
}break;
case SYNC_UNSAVED:
{
View *new_view = live_set_alloc_view(live_set, mem);
view_replace_minor(new_view, panel, live_set);
view_replace_minor(system, new_view, panel, live_set);
new_view->map = &vars->map_ui;
Interactive_View *int_view =
interactive_view_init(new_view, &vars->hot_directory, style,
interactive_view_init(system,
new_view, &vars->hot_directory, style,
working_set, &vars->delay);
int_view->interaction = INTV_SURE_TO_KILL_INTER;
int_view->action = INTV_SURE_TO_KILL;
@ -3159,17 +3168,17 @@ app_step(Thread_Context *thread, Key_Codes *codes,
case DACT_CLOSE_MINOR:
{
view_remove_minor(panel, live_set);
view_remove_minor(system, panel, live_set);
}break;
case DACT_CLOSE_MAJOR:
{
view_remove_major(panel, live_set);
view_remove_major(system, panel, live_set);
}break;
case DACT_THEME_OPTIONS:
{
open_theme_options(vars, live_set, mem, panel);
open_theme_options(system, vars, live_set, mem, panel);
}break;
}
}
@ -3189,11 +3198,13 @@ app_step(Thread_Context *thread, Key_Codes *codes,
prev.x1 != inner.x1 || prev.y1 != inner.y1){
View *view = panel->view;
if (view){
view->do_view(thread, view, inner, active_view,
view->do_view(system,
thread, view, inner, active_view,
VMSG_RESIZE, 0, &dead_input, &active_input);
view = (view->is_minor)?view->major:0;
if (view){
view->do_view(thread, view, inner, active_view,
view->do_view(system,
thread, view, inner, active_view,
VMSG_RESIZE, 0, &dead_input, &active_input);
}
}
@ -3211,7 +3222,8 @@ app_step(Thread_Context *thread, Key_Codes *codes,
Editing_File *file = vars->working_set.files;
for (i32 i = vars->working_set.file_index_count; i > 0; --i, ++file){
if (buffer_good(&file->buffer) && !file->is_dummy){
file_measure_starts_widths(&vars->mem.general, &file->buffer, vars->style.font);
file_measure_starts_widths(system,
&vars->mem.general, &file->buffer, vars->style.font);
}
}
@ -3219,11 +3231,13 @@ app_step(Thread_Context *thread, Key_Codes *codes,
for (i32 panel_i = vars->layout.panel_count; panel_i > 0; --panel_i, ++panel){
View *view = panel->view;
if (view){
view->do_view(thread, view, panel->inner, active_view,
view->do_view(system,
thread, view, panel->inner, active_view,
VMSG_STYLE_CHANGE, 0, &dead_input, &active_input);
view = (view->is_minor)?view->major:0;
if (view){
view->do_view(thread, view, panel->inner, active_view,
view->do_view(system,
thread, view, panel->inner, active_view,
VMSG_STYLE_CHANGE, 0, &dead_input, &active_input);
}
}
@ -3253,7 +3267,8 @@ app_step(Thread_Context *thread, Key_Codes *codes,
if (view_){
Assert(view_->do_view);
draw_push_clip(target, panel->inner);
view_->do_view(thread, view_, panel->inner, active_view,
view_->do_view(system,
thread, view_, panel->inner, active_view,
VMSG_DRAW, target, &dead_input, &active_input);
draw_pop_clip(target);
}

288
4ed.h
View File

@ -3,249 +3,15 @@
*
* 12.12.2014
*
* Application Layer for project codename "4ed"
* Application Layer for 4coder
*
*/
// TOP
#ifndef FRED_H
#define FRED_H
struct Partition{
u8 *base;
i32 pos, max;
};
inline Partition
partition_open(void *memory, i32 size){
Partition partition;
partition.base = (u8*)memory;;
partition.pos = 0;
partition.max = size;
return partition;
}
inline void*
partition_allocate(Partition *data, i32 size){
void *ret = 0;
if (size > 0 && data->pos + size <= data->max){
ret = data->base + data->pos;
data->pos += size;
}
return ret;
}
inline void
partition_align(Partition *data, u32 boundary){
data->pos = (data->pos + (boundary - 1)) & (~boundary);
}
inline void*
partition_current(Partition *data){
return data->base + data->pos;
}
inline i32
partition_remaining(Partition *data){
return data->max - data->pos;
}
inline Partition
partition_sub_part(Partition *data, i32 size){
Partition result = {};
void *d = partition_allocate(data, size);
if (d) result = partition_open(d, size);
return result;
}
#define push_struct(part, T) (T*)partition_allocate(part, sizeof(T))
#define push_array(part, T, size) (T*)partition_allocate(part, sizeof(T)*(size))
#define push_block(part, size) partition_allocate(part, size)
enum Memory_Bubble_Flag{
MEM_BUBBLE_USED = 0x1,
MEM_BUBBLE_DEBUG = 0xD3000000,
MEM_BUBBLE_SYS_DEBUG = 0x5D000000,
MEM_BUBBLE_DEBUG_MASK = 0xFF000000
};
struct Bubble{
Bubble *prev;
Bubble *next;
u32 flags;
i32 size;
u32 type;
u32 _unused_;
};
struct General_Memory{
Bubble sentinel;
};
inline void
insert_bubble(Bubble *prev, Bubble *bubble){
bubble->prev = prev;
bubble->next = prev->next;
bubble->prev->next = bubble;
bubble->next->prev = bubble;
}
inline void
remove_bubble(Bubble *bubble){
bubble->prev->next = bubble->next;
bubble->next->prev = bubble->prev;
}
#if FRED_INTERNAL
#define MEM_BUBBLE_FLAG_INIT MEM_BUBBLE_DEBUG
#else
#define MEM_BUBBLE_FLAG_INIT 0
#endif
internal void
general_memory_open(General_Memory *general, void *memory, i32 size){
general->sentinel.prev = &general->sentinel;
general->sentinel.next = &general->sentinel;
general->sentinel.flags = MEM_BUBBLE_USED;
general->sentinel.size = 0;
Bubble *first = (Bubble*)memory;
first->flags = (u32)MEM_BUBBLE_FLAG_INIT;
first->size = size - sizeof(Bubble);
insert_bubble(&general->sentinel, first);
}
internal void
general_memory_check(General_Memory *general){
Bubble *sentinel = &general->sentinel;
for (Bubble *bubble = sentinel->next;
bubble != sentinel;
bubble = bubble->next){
Assert(bubble);
Bubble *next = bubble->next;
Assert(bubble == next->prev);
if (next != sentinel){
Assert(bubble->next > bubble);
Assert(bubble > bubble->prev);
char *end_ptr = (char*)(bubble + 1) + bubble->size;
char *next_ptr = (char*)next;
Assert(end_ptr == next_ptr);
}
}
}
#define BUBBLE_MIN_SIZE 1024
internal void
general_memory_attempt_split(Bubble *bubble, i32 wanted_size){
i32 remaining_size = bubble->size - wanted_size;
if (remaining_size >= BUBBLE_MIN_SIZE){
bubble->size = wanted_size;
Bubble *new_bubble = (Bubble*)((u8*)(bubble + 1) + wanted_size);
new_bubble->flags = (u32)MEM_BUBBLE_FLAG_INIT;
new_bubble->size = remaining_size - sizeof(Bubble);
insert_bubble(bubble, new_bubble);
}
}
internal void*
general_memory_allocate(General_Memory *general, i32 size, u32 type = 0){
void *result = 0;
for (Bubble *bubble = general->sentinel.next;
bubble != &general->sentinel;
bubble = bubble->next){
if (!(bubble->flags & MEM_BUBBLE_USED)){
if (bubble->size >= size){
result = bubble + 1;
bubble->flags |= MEM_BUBBLE_USED;
bubble->type = type;
general_memory_attempt_split(bubble, size);
break;
}
}
}
return result;
}
inline void
general_memory_do_merge(Bubble *left, Bubble *right){
Assert(left->next == right);
Assert(right->prev == left);
left->size += sizeof(Bubble) + right->size;
remove_bubble(right);
}
inline void
general_memory_attempt_merge(Bubble *left, Bubble *right){
if (!(left->flags & MEM_BUBBLE_USED) &&
!(right->flags & MEM_BUBBLE_USED)){
general_memory_do_merge(left, right);
}
}
internal void
general_memory_free(General_Memory *general, void *memory){
Bubble *bubble = ((Bubble*)memory) - 1;
Assert((!FRED_INTERNAL) || (bubble->flags & MEM_BUBBLE_DEBUG_MASK) == MEM_BUBBLE_DEBUG);
bubble->flags &= ~MEM_BUBBLE_USED;
bubble->type = 0;
Bubble *prev, *next;
prev = bubble->prev;
next = bubble->next;
general_memory_attempt_merge(bubble, next);
general_memory_attempt_merge(prev, bubble);
}
internal void*
general_memory_reallocate(General_Memory *general, void *old, i32 old_size, i32 size, u32 type = 0){
void *result = old;
Bubble *bubble = ((Bubble*)old) - 1;
bubble->type = type;
Assert((!FRED_INTERNAL) || (bubble->flags & MEM_BUBBLE_DEBUG_MASK) == MEM_BUBBLE_DEBUG);
i32 additional_space = size - bubble->size;
if (additional_space > 0){
Bubble *next = bubble->next;
if (!(next->flags & MEM_BUBBLE_USED) &&
next->size + sizeof(Bubble) >= additional_space){
general_memory_do_merge(bubble, next);
general_memory_attempt_split(bubble, size);
}
else{
result = general_memory_allocate(general, size, type);
if (old_size) memcpy(result, old, old_size);
general_memory_free(general, old);
}
}
return result;
}
inline void*
general_memory_reallocate_nocopy(General_Memory *general, void *old, i32 size, u32 type = 0){
return general_memory_reallocate(general, old, 0, size, type);
}
struct Temp_Memory{
Partition *part;
i32 pos;
};
internal Temp_Memory
begin_temp_memory(Partition *data){
Temp_Memory result;
result.part = data;
result.pos = data->pos;
return result;
}
internal void
end_temp_memory(Temp_Memory temp){
temp.part->pos = temp.pos;
}
struct Mem_Options{
Partition part;
General_Memory general;
};
#if SOFTWARE_RENDER
struct Render_Target{
void *pixel_data;
@ -289,17 +55,13 @@ struct Key_Event_Data{
};
struct Key_Input_Data{
// NOTE(allen): keycodes here
Key_Event_Data press[KEY_INPUT_BUFFER_SIZE];
Key_Event_Data hold[KEY_INPUT_BUFFER_SIZE];
i32 press_count;
i32 hold_count;
// NOTE(allen):
// true when the key is held down
// false when the key is not held down
bool8 control_keys[CONTROL_KEY_COUNT];
bool8 caps_lock;
b8 control_keys[CONTROL_KEY_COUNT];
b8 caps_lock;
};
struct Key_Summary{
@ -310,7 +72,7 @@ struct Key_Summary{
struct Key_Single{
Key_Event_Data key;
bool8 *modifiers;
b8 *modifiers;
};
inline Key_Single
@ -354,11 +116,13 @@ struct Clipboard_Contents{
struct Thread_Context;
internal bool32
app_init(Thread_Context *thread,
Application_Memory *memory,
Key_Codes *lose_codes,
Clipboard_Contents clipboard);
#define App_Init_Sig(name) \
b32 name(System_Functions *system, \
Thread_Context *thread, Application_Memory *memory, \
Key_Codes *loose_codes, Clipboard_Contents clipboard, \
Config_API api)
typedef App_Init_Sig(App_Init);
enum Application_Mouse_Cursor{
APP_MOUSE_CURSOR_DEFAULT,
@ -375,13 +139,23 @@ struct Application_Step_Result{
bool32 redraw;
};
internal Application_Step_Result
app_step(Thread_Context *thread,
Key_Codes *codes,
Key_Input_Data *input, Mouse_State *state,
bool32 time_step, Render_Target *target,
Application_Memory *memory,
Clipboard_Contents clipboard,
bool32 first_step, bool32 force_redraw);
#define App_Step_Sig(name) Application_Step_Result \
name(System_Functions *system, \
Thread_Context *thread, Key_Codes *codes, \
Key_Input_Data *input, Mouse_State *mouse, \
b32 time_step, Render_Target *target, \
Application_Memory *memory, \
Clipboard_Contents clipboard, \
b32 first_step, b32 force_redraw)
typedef App_Step_Sig(App_Step);
struct App_Functions{
App_Init *init;
App_Step *step;
};
#endif
// BOTTOM

93
4ed_app_target.cpp Normal file
View File

@ -0,0 +1,93 @@
/*
* Mr. 4th Dimention - Allen Webster
*
* 13.11.2014
*
* Application layer build target
*
*/
// TOP
#ifdef FRED_NOT_PACKAGE
#define FRED_INTERNAL 1
#define FRED_SLOW 1
#define FRED_PRINT_DEBUG 1
#define FRED_PRINT_DEBUG_FILE_LINE 0
#define FRED_PROFILING 1
#define FRED_PROFILING_OS 0
#define FRED_FULL_ERRORS 0
#else
#define FRED_SLOW 0
#define FRED_INTERNAL 0
#define FRED_PRINT_DEBUG 0
#define FRED_PRINT_DEBUG_FILE_LINE 0
#define FRED_PROFILING 0
#define FRED_PROFILING_OS 0
#define FRED_FULL_ERRORS 0
#endif
#define SOFTWARE_RENDER 0
#if FRED_INTERNAL == 0
#undef FRED_PRINT_DEBUG
#define FRED_PRINT_DEBUG 0
#undef FRED_PROFILING
#define FRED_PROFILING 0
#undef FRED_PROFILING_OS
#define FRED_PROFILING_OS 0
#endif
#if FRED_PRINT_DEBUG == 0
#undef FRED_PRINT_DEBUG_FILE_LINE
#define FRED_PRINT_DEBUG_FILE_LINE 0
#undef FRED_PRINT_DEBUG_FILE_LINE
#define FRED_PROFILING_OS 0
#endif
#define FPS 30
#define FRAME_TIME (1000000 / FPS)
#define BUFFER_EXPERIMENT_SCALPEL 0
#include "4ed_meta.h"
#include "4cpp_types.h"
#define FCPP_STRING_IMPLEMENTATION
#include "4cpp_string.h"
#include "4ed_mem.cpp"
#include "4ed_math.cpp"
#include "4coder_custom.h"
#include "4ed_system.h"
#include "4ed.h"
#include "4ed_rendering.h"
#include <windows.h>
#include <GL/gl.h>
#include "4ed_internal.h"
#define FCPP_LEXER_IMPLEMENTATION
#include "4cpp_lexer.h"
#include "4ed_rendering.cpp"
#include "4ed_command.cpp"
#include "4ed_layout.cpp"
#include "4ed_style.cpp"
#include "4ed_file_view.cpp"
#include "4ed_color_view.cpp"
#include "4ed_interactive_view.cpp"
#include "4ed_menu_view.cpp"
#include "4ed_debug_view.cpp"
#include "4ed.cpp"
// BOTTOM

View File

@ -1272,7 +1272,7 @@ do_style_preview(Library_UI *ui, Style *style, i32 toggle = -1){
}
internal bool32
do_main_file_box(UI_State *state, UI_Layout *layout, Hot_Directory *hot_directory, char *end = 0){
do_main_file_box(System_Functions *system, UI_State *state, UI_Layout *layout, Hot_Directory *hot_directory, char *end = 0){
bool32 result = 0;
Style *style = state->style;
Font *font = style->font;
@ -1280,7 +1280,7 @@ do_main_file_box(UI_State *state, UI_Layout *layout, Hot_Directory *hot_director
String *string = &hot_directory->string;
if (state->input_stage){
if (ui_do_file_field_input(state, hot_directory)){
if (ui_do_file_field_input(system, state, hot_directory)){
result = 1;
}
}
@ -1300,14 +1300,14 @@ do_main_file_box(UI_State *state, UI_Layout *layout, Hot_Directory *hot_director
}
internal bool32
do_main_string_box(UI_State *state, UI_Layout *layout, String *string){
do_main_string_box(System_Functions *system, UI_State *state, UI_Layout *layout, String *string){
bool32 result = 0;
Style *style = state->style;
Font *font = style->font;
i32_Rect box = layout_rect(layout, font->height + 2);
if (state->input_stage){
if (ui_do_line_field_input(state, string)){
if (ui_do_line_field_input(system, state, string)){
result = 1;
}
}
@ -1398,13 +1398,14 @@ do_file_option(i32 id, UI_State *state, UI_Layout *layout, String filename, bool
return result;
}
internal bool32
do_file_list_box(UI_State *state, UI_Layout *layout, Hot_Directory *hot_dir, bool32 has_filter,
internal b32
do_file_list_box(System_Functions *system,
UI_State *state, UI_Layout *layout, Hot_Directory *hot_dir, bool32 has_filter,
bool32 *new_dir, bool32 *selected, char *end){
bool32 result = 0;
File_List *files = &hot_dir->file_list;
if (do_main_file_box(state, layout, hot_dir, end)){
if (do_main_file_box(system, state, layout, hot_dir, end)){
*selected = 1;
terminate_with_null(&hot_dir->string);
}
@ -1473,12 +1474,12 @@ do_file_list_box(UI_State *state, UI_Layout *layout, Hot_Directory *hot_dir, boo
return result;
}
internal bool32
do_live_file_list_box(UI_State *state, UI_Layout *layout, Working_Set *working_set,
internal b32
do_live_file_list_box(System_Functions *system, UI_State *state, UI_Layout *layout, Working_Set *working_set,
String *string, bool32 *selected){
bool32 result = 0;
if (do_main_string_box(state, layout, string)){
if (do_main_string_box(system, state, layout, string)){
*selected = 1;
terminate_with_null(string);
}
@ -1518,7 +1519,8 @@ do_live_file_list_box(UI_State *state, UI_Layout *layout, Working_Set *working_s
}
internal i32
step_draw_library(Color_View *color_view, i32_Rect rect, View_Message message,
step_draw_library(System_Functions *system,
Color_View *color_view, i32_Rect rect, View_Message message,
Render_Target *target, Input_Summary *user_input){
i32 result = 0;
@ -1565,12 +1567,12 @@ step_draw_library(Color_View *color_view, i32_Rect rect, View_Message message,
if (do_button(-3, &ui.state, &ui.layout, "Import", 2)){
color_view->mode = CV_MODE_IMPORT_FILE;
hot_directory_clean_end(color_view->hot_directory);
hot_directory_reload(color_view->hot_directory, color_view->working_set);
hot_directory_reload(system, color_view->hot_directory, color_view->working_set);
}
if (do_button(-4, &ui.state, &ui.layout, "Export", 2)){
color_view->mode = CV_MODE_EXPORT;
hot_directory_clean_end(color_view->hot_directory);
hot_directory_reload(color_view->hot_directory, color_view->working_set);
hot_directory_reload(system, color_view->hot_directory, color_view->working_set);
memset(color_view->import_export_check, 0, sizeof(color_view->import_export_check));
}
@ -1603,13 +1605,14 @@ step_draw_library(Color_View *color_view, i32_Rect rect, View_Message message,
}
bool32 new_dir = 0;
if (do_file_list_box(&ui.state, &ui.layout, ui.hot_directory, color_view->p4c_only,
if (do_file_list_box(system,
&ui.state, &ui.layout, ui.hot_directory, color_view->p4c_only,
&new_dir, &file_selected, 0)){
result = 1;
}
if (new_dir){
hot_directory_reload(ui.hot_directory, ui.state.working_set);
hot_directory_reload(system, ui.hot_directory, ui.state.working_set);
}
if (file_selected){
memset(&color_view->inspecting_styles, 0, sizeof(Style_Library));
@ -1617,7 +1620,8 @@ step_draw_library(Color_View *color_view, i32_Rect rect, View_Message message,
Style *styles = color_view->inspecting_styles.styles;
i32 count, max;
max = ArrayCount(color_view->inspecting_styles.styles);
if (style_library_import((u8*)color_view->hot_directory->string.str,
if (style_library_import(system,
color_view->hot_directory->string.str,
ui.fonts, styles, max, &count)){
color_view->mode = CV_MODE_IMPORT;
}
@ -1645,19 +1649,20 @@ step_draw_library(Color_View *color_view, i32_Rect rect, View_Message message,
}
bool32 new_dir = 0;
if (do_file_list_box(&ui.state, &ui.layout, ui.hot_directory, 1,
if (do_file_list_box(system,
&ui.state, &ui.layout, ui.hot_directory, 1,
&new_dir, &file_selected, ".p4c")){
result = 1;
}
if (new_dir){
hot_directory_reload(ui.hot_directory, ui.state.working_set);
hot_directory_reload(system,
ui.hot_directory, ui.state.working_set);
}
if (file_selected){
i32 count = ui.styles->count;
// TODO(allen): pass the transient memory in here
Style **styles = (Style**)
system_get_memory(sizeof(Style*)*count);
Style **styles = (Style**)system->get_memory(sizeof(Style*)*count);
Style *style = ui.styles->styles;
bool8 *export_check = color_view->import_export_check;
i32 export_count = 0;
@ -1666,13 +1671,13 @@ step_draw_library(Color_View *color_view, i32_Rect rect, View_Message message,
styles[export_count++] = style;
}
}
char *mem = (char*)system_get_memory(ui.hot_directory->string.size + 5);
char *mem = (char*)system->get_memory(ui.hot_directory->string.size + 5);
String str = make_string(mem, 0, ui.hot_directory->string.size + 5);
copy(&str, ui.hot_directory->string);
append(&str, make_lit_string(".p4c"));
style_library_export((u8*)str.str, styles, export_count);
system_free_memory(mem);
system_free_memory(styles);
style_library_export(system, str.str, styles, export_count);
system->free_memory(mem);
system->free_memory(styles);
color_view->mode = CV_MODE_LIBRARY;
}
}break;
@ -1757,11 +1762,11 @@ DO_VIEW_SIG(do_color_view){
switch (message){
case VMSG_STEP:
{
result = step_draw_library(color_view, rect, message, target, user_input);
result = step_draw_library(system, color_view, rect, message, target, user_input);
}break;
case VMSG_DRAW:
{
step_draw_library(color_view, rect, message, target, user_input);
step_draw_library(system, color_view, rect, message, target, user_input);
}break;
}break;

View File

@ -9,7 +9,8 @@
// TOP
typedef void (*Command_Function)(struct Command_Data *command, struct Command_Binding binding);
typedef void (*Command_Function)(System_Functions *system,
struct Command_Data *command, struct Command_Binding binding);
struct Command_Binding{
Command_Function function;

View File

@ -12,8 +12,7 @@
enum Debug_Mode{
DBG_MEMORY,
DBG_OS_EVENTS,
DBG_PROFILE
DBG_OS_EVENTS
};
struct Dbg_Past_Key{
@ -94,10 +93,10 @@ draw_general_memory(Debug_View *view, i32_Rect rect, Render_Target *target, i32
}
internal i32
draw_system_memory(Debug_View *view, i32_Rect rect, Render_Target *target, i32 y){
draw_system_memory(System_Functions *system, Debug_View *view, i32_Rect rect, Render_Target *target, i32 y){
Font *font = view->font;
i32 y_advance = font->height;
Bubble *sentinel = INTERNAL_system_sentinel();
Bubble *sentinel = system->internal_sentinel();
for (Bubble *bubble = sentinel->next;
bubble != sentinel;
@ -124,10 +123,11 @@ draw_system_memory(Debug_View *view, i32_Rect rect, Render_Target *target, i32 y
}
internal void
draw_background_threads(Debug_View *view, i32_Rect rect, Render_Target *target){
draw_background_threads(System_Functions *system,
Debug_View *view, i32_Rect rect, Render_Target *target){
i32 pending;
bool8 running[4];
INTERNAL_get_thread_states(BACKGROUND_THREADS, running, &pending);
system->internal_get_thread_states(BACKGROUND_THREADS, running, &pending);
i32 box_size = 30;
@ -292,152 +292,8 @@ draw_os_events(Debug_View *view, i32_Rect rect, Render_Target *target,
}
internal i32
draw_profile_frame(Render_Target *target, Profile_Frame *frame,
i32 x, i32 top, i32 bottom, i32 goal, Input_Summary *active_input){
i32 result = -1;
persist u32 colors[] = {
0x80C06000,
0x8000C060,
0x806000C0,
0x8060C000,
0x800060C0,
0x80C00060,
};
persist i32 color_max = ArrayCount(colors);
Mouse_Summary *mouse = &active_input->mouse;
i32 count = frame->events.count;
Debug_Event *events = frame->events.e;
i32 i;
for (i = 0; i < count && events[i].type == DBGEV_START; ++i){
i64 start = events[i++].time;
i64 end = events[i].time;
real32 rtop = unlerp(0, (real32)end, FRAME_TIME);
real32 rbot = unlerp(0, (real32)start, FRAME_TIME);
rtop = lerp((real32)bottom, rtop, (real32)goal);
rbot = lerp((real32)bottom, rbot, (real32)goal);
i32_Rect r = i32R(x, (i32)rtop, x+5, (i32)rbot);
draw_rectangle(target, r, colors[events[i].event_index % color_max]);
if (hit_check(mouse->mx, mouse->my, r)) result = (i - 1);
}
{
real32 rtop = unlerp(0, (real32)frame->dbg_procing_end, FRAME_TIME);
real32 rbot = unlerp(0, (real32)frame->dbg_procing_start, FRAME_TIME);
rtop = lerp((real32)bottom, rtop, (real32)goal);
rbot = lerp((real32)bottom, rbot, (real32)goal);
i32_Rect r = i32R(x, (i32)rtop, x+5, (i32)rbot);
draw_rectangle(target, r, 0xFF808080);
}
for (; i < count; ++i){
Assert(events[i].type == DBGEV_MOMENT);
real32 ry = unlerp(0, (real32)events[i].time, FRAME_TIME);
ry = lerp((real32)bottom, ry, (real32)goal);
i32_Rect r = i32R(x-1, (i32)ry, x+6, (i32)ry+1);
draw_rectangle(target, r, 0xFFFFFFFF);
if (hit_check(mouse->mx, mouse->my, r)) result = i;
}
return result;
}
internal void
draw_profile(Debug_View *view, i32_Rect rect, Render_Target *target, Input_Summary *active_input){
i32 j = (INTERNAL_frame_index % 30);
i32 event_index = -1;
i32 frame_index = -1;
i32 target_time = (rect.y0 + rect.y1)/2;
i32 x = rect.x0;
for (i32 i = 0; i < PAST_PROFILE_COUNT; ++i){
Profile_Frame *frame = past_frames + j;
i32 s = draw_profile_frame(target, frame, x, rect.y0, rect.y1, target_time, active_input);
if (s != -1){
event_index = s;
frame_index = j;
}
x += 10;
j = ((j+1) % PAST_PROFILE_COUNT);
}
draw_rectangle(target, i32R(rect.x0, target_time, rect.x1, target_time + 1), 0xFFFFFFFF);
char c[200];
if (frame_index != -1){
Profile_Frame *frame = past_frames + frame_index;
Debug_Event *events = frame->events.e;
Debug_Event *event = events + event_index;
Font *font = view->font;
u32 color = 0xFFFFFFFF;
i32 x, y;
x = rect.x0;
y = rect.y0;
String s = make_fixed_width_string(c);
append(&s, event->name);
append(&s, ": ");
Assert(event->type == DBGEV_START || event->type == DBGEV_MOMENT);
if (event->type == DBGEV_START){
Debug_Event *next_event = event + 1;
Assert(next_event->type == DBGEV_END);
append_int_to_str((i32)(next_event->time - event->time), &s);
}
else{
append_int_to_str((i32)event->time, &s);
}
terminate_with_null(&s);
draw_string(target, font, c, x, y, color);
y += font->height;
if (frame->first_key != -1){
Dbg_Past_Key *key = view->past_keys + frame->first_key;
Dbg_Past_Key *end_key = view->past_keys + ArrayCount(view->past_keys);
while (key->frame_index == frame->index){
draw_key_event(view, target, key,
x, y, 0xFFFFFFFF, 0xFF808080);
y += font->height;
++key;
if (key == end_key) key = view->past_keys;
}
}
i32 count = frame->events.count;
for (i32 i = 0; i < count; ++i){
if (events[i].type == DBGEV_START) ++i;
else{
s = make_fixed_width_string(c);
append(&s, events[i].name);
append(&s, ": ");
append_int_to_str((i32)events[i].time, &s);
terminate_with_null(&s);
draw_string(target, font, c, x, y, color);
y += font->height;
}
}
}
}
internal i32
draw_debug_view(Debug_View *view, i32_Rect rect, Render_Target *target,
draw_debug_view(System_Functions *system,
Debug_View *view, i32_Rect rect, Render_Target *target,
Input_Summary *active_input){
i32 result = 0;
@ -450,17 +306,12 @@ draw_debug_view(Debug_View *view, i32_Rect rect, Render_Target *target,
y = draw_general_memory(view, rect, target, y);
draw_rectangle(target, i32R(rect.x0, y, rect.x1, y+2), 0xFF222222);
y += 2;
y = draw_system_memory(view, rect, target, y);
y = draw_system_memory(system, view, rect, target, y);
}break;
case DBG_OS_EVENTS:
{
draw_os_events(view, rect, target, active_input);
}break;
case DBG_PROFILE:
{
draw_background_threads(view, rect, target);
draw_profile(view, rect, target, active_input);
}break;
}
return result;
}
@ -472,7 +323,6 @@ step_debug_view(Debug_View *view, i32_Rect rect, Render_Target *target,
bool8 *modifiers = active_input->keys.modifiers;
for (i32 i = 0; i < active_input->keys.count; ++i){
i32 this_index = view->past_key_pos;
Dbg_Past_Key *past_key = view->past_keys + view->past_key_pos;
++view->past_key_pos;
view->past_key_pos = view->past_key_pos % max_past;
@ -482,15 +332,14 @@ step_debug_view(Debug_View *view, i32_Rect rect, Render_Target *target,
past_key->modifiers[1] = modifiers[1];
past_key->modifiers[2] = modifiers[2];
if (INTERNAL_updating_profile){
#if 0
i32 this_index = view->past_key_pos;
past_key->frame_index = INTERNAL_frame_index;
if (profile_frame.first_key == -1){
profile_frame.first_key = this_index;
}
}
else{
#endif
past_key->frame_index = -1;
}
if (view->past_key_count < max_past) ++view->past_key_count;
}
@ -509,7 +358,7 @@ DO_VIEW_SIG(do_debug_view){
case VMSG_RESIZE: break;
case VMSG_STYLE_CHANGE: break;
case VMSG_STEP: step_debug_view(debug_view, rect, target, active_input); result = 1; break;
case VMSG_DRAW: draw_debug_view(debug_view, rect, target, active_input); break;
case VMSG_DRAW: draw_debug_view(system, debug_view, rect, target, active_input); break;
case VMSG_FREE: break;
}

View File

@ -270,13 +270,13 @@ struct Hot_Directory{
};
internal void
hot_directory_init(Hot_Directory *hot_directory, String base){
hot_directory_init(System_Functions *system, Hot_Directory *hot_directory, String base){
hot_directory->string = base;
hot_directory->string.str[255] = 0;
i32 dir_size = system_get_working_directory((u8*)hot_directory->string.str,
i32 dir_size = system->get_current_directory(hot_directory->string.str,
hot_directory->string.memory_size);
if (dir_size <= 0){
dir_size = system_get_easy_directory((u8*)hot_directory->string.str);
dir_size = system->get_easy_directory(hot_directory->string.str);
}
hot_directory->string.size = dir_size;
append(&hot_directory->string, "\\");
@ -323,22 +323,23 @@ hot_directory_fixup(Hot_Directory *hot_directory, Working_Set *working_set){
}
inline void
hot_directory_set(Hot_Directory *hot_directory, String str, Working_Set *working_set){
hot_directory_set(System_Functions *system,
Hot_Directory *hot_directory, String str, Working_Set *working_set){
bool32 success = copy_checked(&hot_directory->string, str);
terminate_with_null(&hot_directory->string);
if (success){
system_free_file_list(hot_directory->file_list);
hot_directory->file_list = system_get_files(str);
system->free_file_list(hot_directory->file_list);
hot_directory->file_list = system->get_file_list(str);
}
hot_directory_fixup(hot_directory, working_set);
}
inline void
hot_directory_reload(Hot_Directory *hot_directory, Working_Set *working_set){
hot_directory_reload(System_Functions *system, Hot_Directory *hot_directory, Working_Set *working_set){
if (hot_directory->file_list.block){
system_free_file_list(hot_directory->file_list);
system->free_file_list(hot_directory->file_list);
}
hot_directory->file_list = system_get_files(hot_directory->string);
hot_directory->file_list = system->get_file_list(hot_directory->string);
hot_directory_fixup(hot_directory, working_set);
}
@ -412,7 +413,8 @@ struct Single_Line_Mode{
};
internal Single_Line_Input_Step
app_single_line_input_core(Key_Codes *codes, Working_Set *working_set,
app_single_line_input_core(System_Functions *system,
Key_Codes *codes, Working_Set *working_set,
Key_Single key, Single_Line_Mode mode){
Single_Line_Input_Step result = {};
@ -431,7 +433,7 @@ app_single_line_input_core(Key_Codes *codes, Working_Set *working_set,
if (char_is_slash(end_character)){
mode.string->size = reverse_seek_slash(*mode.string) + 1;
mode.string->str[mode.string->size] = 0;
hot_directory_set(mode.hot_directory, *mode.string, working_set);
hot_directory_set(system, mode.hot_directory, *mode.string, working_set);
}
else{
mode.string->str[mode.string->size] = 0;
@ -462,7 +464,7 @@ app_single_line_input_core(Key_Codes *codes, Working_Set *working_set,
if (match.filename.str){
if (match.is_folder){
set_last_folder(mode.string, match.filename);
hot_directory_set(mode.hot_directory, *mode.string, working_set);
hot_directory_set(system, mode.hot_directory, *mode.string, working_set);
result.hit_newline = 0;
}
else{
@ -490,7 +492,7 @@ app_single_line_input_core(Key_Codes *codes, Working_Set *working_set,
mode.string->size++;
mode.string->str[mode.string->size] = 0;
if (mode.type == SINGLE_LINE_FILE && char_is_slash(new_character)){
hot_directory_set(mode.hot_directory, *mode.string, working_set);
hot_directory_set(system, mode.hot_directory, *mode.string, working_set);
}
result.made_a_change = 1;
}
@ -505,15 +507,17 @@ app_single_line_input_core(Key_Codes *codes, Working_Set *working_set,
}
inline Single_Line_Input_Step
app_single_line_input_step(Key_Codes *codes, Key_Single key, String *string){
app_single_line_input_step(System_Functions *system,
Key_Codes *codes, Key_Single key, String *string){
Single_Line_Mode mode = {};
mode.type = SINGLE_LINE_STRING;
mode.string = string;
return app_single_line_input_core(codes, 0, key, mode);
return app_single_line_input_core(system, codes, 0, key, mode);
}
inline Single_Line_Input_Step
app_single_file_input_step(Key_Codes *codes, Working_Set *working_set, Key_Single key,
app_single_file_input_step(System_Functions *system,
Key_Codes *codes, Working_Set *working_set, Key_Single key,
String *string, Hot_Directory *hot_directory,
bool32 fast_folder_select){
Single_Line_Mode mode = {};
@ -521,11 +525,12 @@ app_single_file_input_step(Key_Codes *codes, Working_Set *working_set, Key_Singl
mode.string = string;
mode.hot_directory = hot_directory;
mode.fast_folder_select = fast_folder_select;
return app_single_line_input_core(codes, working_set, key, mode);
return app_single_line_input_core(system, codes, working_set, key, mode);
}
inline Single_Line_Input_Step
app_single_number_input_step(Key_Codes *codes, Key_Single key, String *string){
app_single_number_input_step(System_Functions *system,
Key_Codes *codes, Key_Single key, String *string){
Single_Line_Input_Step result = {};
Single_Line_Mode mode = {};
mode.type = SINGLE_LINE_STRING;
@ -533,7 +538,7 @@ app_single_number_input_step(Key_Codes *codes, Key_Single key, String *string){
char c = (char)key.key.character;
if (c == 0 || c == '\n' || char_is_numeric(c))
result = app_single_line_input_core(codes, 0, key, mode);
result = app_single_line_input_core(system, codes, 0, key, mode);
return result;
}
@ -930,7 +935,8 @@ ui_do_text_field_input(UI_State *state, String *str){
}
internal bool32
ui_do_file_field_input(UI_State *state, Hot_Directory *hot_dir){
ui_do_file_field_input(System_Functions *system,
UI_State *state, Hot_Directory *hot_dir){
bool32 result = 0;
Key_Summary *keys = state->keys;
for (i32 key_i = 0; key_i < keys->count; ++key_i){
@ -938,21 +944,22 @@ ui_do_file_field_input(UI_State *state, Hot_Directory *hot_dir){
String *str = &hot_dir->string;
terminate_with_null(str);
Single_Line_Input_Step step =
app_single_file_input_step(state->codes, state->working_set, key, str, hot_dir, 1);
app_single_file_input_step(system, state->codes, state->working_set, key, str, hot_dir, 1);
if (step.hit_newline || step.hit_ctrl_newline) result = 1;
}
return result;
}
internal bool32
ui_do_line_field_input(UI_State *state, String *string){
ui_do_line_field_input(System_Functions *system,
UI_State *state, String *string){
bool32 result = 0;
Key_Summary *keys = state->keys;
for (i32 key_i = 0; key_i < keys->count; ++key_i){
Key_Single key = get_single_key(keys, key_i);
terminate_with_null(string);
Single_Line_Input_Step step =
app_single_line_input_step(state->codes, key, string);
app_single_line_input_step(system, state->codes, key, string);
if (step.hit_newline || step.hit_ctrl_newline) result = 1;
}
return result;
@ -1136,8 +1143,8 @@ file_set_name(Editing_File *file, char *filename){
}
inline void
file_synchronize_times(Editing_File *file, u8 *filename){
Time_Stamp stamp = system_file_time_stamp(filename);
file_synchronize_times(System_Functions *system, Editing_File *file, char *filename){
Time_Stamp stamp = system->file_time_stamp(filename);
if (stamp.success){
file->last_4ed_write_time = stamp.time;
file->last_4ed_edit_time = stamp.time;
@ -1146,7 +1153,7 @@ file_synchronize_times(Editing_File *file, u8 *filename){
}
internal b32
file_save(Partition *part, Editing_File *file, char *filename){
file_save(System_Functions *system, Partition *part, Editing_File *file, char *filename){
b32 result = 0;
#if BUFFER_EXPERIMENT_SCALPEL <= 3
Temp_Memory temp = begin_temp_memory(part);
@ -1154,25 +1161,26 @@ file_save(Partition *part, Editing_File *file, char *filename){
if (file->dos_write_mode){
char *data = push_array(part, char, max);
i32 size = buffer_convert_out(&file->buffer, data, max);
result = system_save_file((u8*)filename, data, size);
result = system->save_file(filename, data, size);
}
else{
char *data = push_array(part, char, max);
i32 size = buffer_size(&file->buffer);
Assert(size <= max);
buffer_stringify(&file->buffer, 0, size, data);
result = system_save_file((u8*)filename, data, size);
result = system->save_file(filename, data, size);
}
end_temp_memory(temp);
file_synchronize_times(file, (u8*)filename);
file_synchronize_times(system, file, filename);
#endif
return result;
}
inline bool32
file_save_and_set_names(Partition *part, Editing_File *file, char *filename){
bool32 result = 0;
if (file_save(part, file, filename)){
inline b32
file_save_and_set_names(System_Functions *system,
Partition *part, Editing_File *file, char *filename){
b32 result = 0;
if (file_save(system, part, file, filename)){
result = 1;
file_set_name(file, filename);
}
@ -1222,7 +1230,8 @@ file_grow_starts_as_needed(General_Memory *general, Buffer_Type *buffer, i32 add
}
internal void
file_measure_starts_widths(General_Memory *general, Buffer_Type *buffer, Font *font){
file_measure_starts_widths(System_Functions *system,
General_Memory *general, Buffer_Type *buffer, Font *font){
ProfileMomentFunction();
if (!buffer->line_starts){
i32 max = buffer->line_max = Kbytes(1);
@ -1304,7 +1313,8 @@ file_measure_starts(General_Memory *general, Buffer_Type *buffer){
#endif
internal void
file_remeasure_starts(General_Memory *general, Buffer_Type *buffer,
file_remeasure_starts(System_Functions *system,
General_Memory *general, Buffer_Type *buffer,
i32 line_start, i32 line_end, i32 line_shift,
i32 character_shift){
#if BUFFER_EXPERIMENT_SCALPEL <= 3
@ -1362,7 +1372,8 @@ file_measure_widths(General_Memory *general, Buffer_Type *buffer, Font *font){
#endif
internal void
file_remeasure_widths(General_Memory *general, Buffer_Type *buffer, Font *font,
file_remeasure_widths(System_Functions *system,
General_Memory *general, Buffer_Type *buffer, Font *font,
i32 line_start, i32 line_end, i32 line_shift){
#if BUFFER_EXPERIMENT_SCALPEL <= 3
ProfileMomentFunction();
@ -1406,7 +1417,8 @@ view_compute_lowest_line(File_View *view){
}
internal void
view_measure_wraps(General_Memory *general, File_View *view){
view_measure_wraps(System_Functions *system,
General_Memory *general, File_View *view){
#if BUFFER_EXPERIMENT_SCALPEL <= 3
ProfileMomentFunction();
Buffer_Type *buffer;
@ -1443,7 +1455,8 @@ alloc_for_buffer(void *context, int *size){
}
internal void
file_create_from_string(Mem_Options *mem, Editing_File *file, u8 *filename, Font *font, String val, b32 super_locked = 0){
file_create_from_string(System_Functions *system,
Mem_Options *mem, Editing_File *file, char *filename, Font *font, String val, b32 super_locked = 0){
*file = {};
General_Memory *general = &mem->general;
#if BUFFER_EXPERIMENT_SCALPEL <= 3
@ -1460,6 +1473,7 @@ file_create_from_string(Mem_Options *mem, Editing_File *file, u8 *filename, Font
i32 scratch_size = partition_remaining(part);
Assert(scratch_size > 0);
i32 init_success = buffer_end_init(&init, part->base + part->pos, scratch_size);
AllowLocal(init_success);
Assert(init_success);
#endif
@ -1469,9 +1483,9 @@ file_create_from_string(Mem_Options *mem, Editing_File *file, u8 *filename, Font
file->base_map_id = mapid_file;
file->font = font;
file_synchronize_times(file, filename);
file_synchronize_times(system, file, filename);
file_measure_starts_widths(general, &file->buffer, font);
file_measure_starts_widths(system, general, &file->buffer, font);
file->super_locked = super_locked;
if (!super_locked){
@ -1503,33 +1517,33 @@ file_create_from_string(Mem_Options *mem, Editing_File *file, u8 *filename, Font
}
internal bool32
file_create(Mem_Options *mem, Editing_File *file, u8 *filename, Font *font){
file_create(System_Functions *system, Mem_Options *mem, Editing_File *file, char *filename, Font *font){
bool32 result = 0;
File_Data raw_file = system_load_file(filename);
File_Data raw_file = system->load_file(filename);
if (raw_file.data){
result = 1;
String val = make_string((char*)raw_file.data, raw_file.size);
file_create_from_string(mem, file, filename, font, val);
system_free_file(raw_file);
file_create_from_string(system, mem, file, filename, font, val);
system->free_file(raw_file);
}
return result;
}
internal b32
file_create_empty(Mem_Options *mem, Editing_File *file, u8 *filename, Font *font){
file_create_empty(System_Functions *system, Mem_Options *mem, Editing_File *file, char *filename, Font *font){
b32 result = 1;
String empty_str = {};
file_create_from_string(mem, file, filename, font, empty_str);
file_create_from_string(system, mem, file, filename, font, empty_str);
return result;
}
internal b32
file_create_super_locked(Mem_Options *mem, Editing_File *file, u8 *filename, Font *font){
file_create_super_locked(System_Functions *system, Mem_Options *mem, Editing_File *file, char *filename, Font *font){
b32 result = 1;
String empty_str = {};
file_create_from_string(mem, file, filename, font, empty_str, 1);
file_create_from_string(system, mem, file, filename, font, empty_str, 1);
return result;
}
@ -1559,9 +1573,9 @@ working_set_get_available_file(Working_Set *working_set){
}
internal void
file_close(General_Memory *general, Editing_File *file){
file_close(System_Functions *system, General_Memory *general, Editing_File *file){
if (file->still_lexing){
system_cancel_job(BACKGROUND_THREADS, file->lex_job);
system->cancel_job(BACKGROUND_THREADS, file->lex_job);
}
if (file->token_stack.tokens){
general_memory_free(general, file->token_stack.tokens);
@ -1605,7 +1619,7 @@ struct Shift_Information{
#if BUFFER_EXPERIMENT_SCALPEL <= 0
internal
JOB_CALLBACK(job_full_lex){
Job_Callback(job_full_lex){
Editing_File *file = (Editing_File*)data[0];
General_Memory *general = (General_Memory*)data[1];
@ -1621,14 +1635,14 @@ JOB_CALLBACK(job_full_lex){
Cpp_Lex_Data status;
status = cpp_lex_file_nonalloc(cpp_file, &tokens);
while (!status.complete){
system_grow_thread_memory(memory);
system->grow_thread_memory(memory);
tokens.tokens = (Cpp_Token*)memory->data;
tokens.max_count = memory->size / sizeof(Cpp_Token);
status = cpp_lex_file_nonalloc(cpp_file, &tokens, status);
}
i32 new_max = LargeRoundUp(tokens.count, Kbytes(1));
system_aquire_lock(FRAME_LOCK);
system->acquire_lock(FRAME_LOCK);
if (file->token_stack.tokens){
file->token_stack.tokens = (Cpp_Token*)
general_memory_reallocate_nocopy(general, file->token_stack.tokens, new_max*sizeof(Cpp_Token), BUBBLE_TOKENS);
@ -1637,7 +1651,7 @@ JOB_CALLBACK(job_full_lex){
file->token_stack.tokens = (Cpp_Token*)
general_memory_allocate(general, new_max*sizeof(Cpp_Token), BUBBLE_TOKENS);
}
system_release_lock(FRAME_LOCK);
system->release_lock(FRAME_LOCK);
i32 copy_amount = Kbytes(8);
i32 uncoppied = tokens.count*sizeof(Cpp_Token);
@ -1647,9 +1661,9 @@ JOB_CALLBACK(job_full_lex){
u8 *src = (u8*)tokens.tokens;
while (uncoppied > 0){
system_aquire_lock(FRAME_LOCK);
system->acquire_lock(FRAME_LOCK);
memcpy(dest, src, copy_amount);
system_release_lock(FRAME_LOCK);
system->release_lock(FRAME_LOCK);
dest += copy_amount;
src += copy_amount;
uncoppied -= copy_amount;
@ -1658,7 +1672,7 @@ JOB_CALLBACK(job_full_lex){
file->token_stack.count = tokens.count;
file->token_stack.max_count = new_max;
system_force_redraw();
system->force_redraw();
// NOTE(allen): These are outside the locked section because I don't
// think getting these out of order will cause critical bugs, and I
@ -1669,9 +1683,10 @@ JOB_CALLBACK(job_full_lex){
#endif
internal void
file_kill_tokens(General_Memory *general, Editing_File *file){
file_kill_tokens(System_Functions *system,
General_Memory *general, Editing_File *file){
if (file->still_lexing){
system_cancel_job(BACKGROUND_THREADS, file->lex_job);
system->cancel_job(BACKGROUND_THREADS, file->lex_job);
}
if (file->token_stack.tokens){
general_memory_free(general, file->token_stack.tokens);
@ -1683,7 +1698,8 @@ file_kill_tokens(General_Memory *general, Editing_File *file){
#if BUFFER_EXPERIMENT_SCALPEL <= 0
internal void
file_first_lex_parallel(General_Memory *general, Editing_File *file){
file_first_lex_parallel(System_Functions *system,
General_Memory *general, Editing_File *file){
Assert(file->token_stack.tokens == 0);
file->tokens_complete = 0;
@ -1695,20 +1711,21 @@ file_first_lex_parallel(General_Memory *general, Editing_File *file){
job.data[0] = file;
job.data[1] = general;
job.memory_request = Kbytes(64);
file->lex_job = system_post_job(BACKGROUND_THREADS, job);
file->lex_job = system->post_job(BACKGROUND_THREADS, job);
}
internal void
file_relex_parallel(Mem_Options *mem, Editing_File *file,
file_relex_parallel(System_Functions *system,
Mem_Options *mem, Editing_File *file,
i32 start_i, i32 end_i, i32 amount){
General_Memory *general = &mem->general;
Partition *part = &mem->part;
if (file->token_stack.tokens == 0){
file_first_lex_parallel(general, file);
file_first_lex_parallel(system, general, file);
return;
}
bool32 inline_lex = !file->still_lexing;
b32 inline_lex = !file->still_lexing;
if (inline_lex){
Cpp_File cpp_file;
cpp_file.data = file->buffer.data;
@ -1779,7 +1796,7 @@ file_relex_parallel(Mem_Options *mem, Editing_File *file,
job.data[0] = file;
job.data[1] = general;
job.memory_request = Kbytes(64);
file->lex_job = system_post_job(BACKGROUND_THREADS, job);
file->lex_job = system->post_job(BACKGROUND_THREADS, job);
}
}
#endif
@ -2182,7 +2199,8 @@ view_get_cursor_y(File_View *view){
}
internal void
view_set_file(File_View *view, Editing_File *file, Style *style,
view_set_file(System_Functions *system,
File_View *view, Editing_File *file, Style *style,
Custom_Command_Function *open_hook, void *cmd_context, Application_Links app){
Panel *panel = view->view_base.panel;
view->file = file;
@ -2194,7 +2212,7 @@ view_set_file(File_View *view, Editing_File *file, Style *style,
view->font_advance = font->advance;
view->font_height = font->height;
view_measure_wraps(general, view);
view_measure_wraps(system, general, view);
view->cursor = {};
view->cursor = view_compute_cursor_from_pos(view, file->cursor_pos);
@ -2526,20 +2544,22 @@ debug_step_match(Edit_Step a, Edit_Step b){
}
inline void
file_pre_edit_maintenance(Editing_File *file){
file_pre_edit_maintenance(System_Functions *system,
Editing_File *file){
if (file->still_lexing)
system_cancel_job(BACKGROUND_THREADS, file->lex_job);
file->last_4ed_edit_time = system_get_now();
system->cancel_job(BACKGROUND_THREADS, file->lex_job);
file->last_4ed_edit_time = system->time_stamp_now();
}
internal void
file_do_single_edit(Mem_Options *mem, Editing_File *file,
file_do_single_edit(System_Functions *system,
Mem_Options *mem, Editing_File *file,
Editing_Layout *layout, Edit_Spec spec, History_Mode history_mode){
ProfileMomentFunction();
// NOTE(allen): fixing stuff beforewards????
file_update_history_before_edit(mem, file, spec.step, spec.str, history_mode);
file_pre_edit_maintenance(file);
file_pre_edit_maintenance(system, file);
// NOTE(allen): actual text replacement
#if BUFFER_EXPERIMENT_SCALPEL <= 3
@ -2580,15 +2600,15 @@ file_do_single_edit(Mem_Options *mem, Editing_File *file,
i32 new_line_count = buffer_count_newlines(&file->buffer, start, start+str_len);
i32 line_shift = new_line_count - replaced_line_count;
file_remeasure_starts(general, &file->buffer, line_start, line_end, line_shift, shift_amount);
file_remeasure_widths(general, &file->buffer, file->font, line_start, line_end, line_shift);
file_remeasure_starts(system, general, &file->buffer, line_start, line_end, line_shift, shift_amount);
file_remeasure_widths(system, general, &file->buffer, file->font, line_start, line_end, line_shift);
i32 panel_count = layout->panel_count;
Panel *current_panel = layout->panels;
for (i32 i = 0; i < panel_count; ++i, ++current_panel){
File_View *current_view = view_to_file_view(current_panel->view);
if (current_view && current_view->file == file){
view_measure_wraps(general, current_view);
view_measure_wraps(system, general, current_view);
}
}
#endif
@ -2596,7 +2616,7 @@ file_do_single_edit(Mem_Options *mem, Editing_File *file,
#if BUFFER_EXPERIMENT_SCALPEL <= 0
// NOTE(allen): fixing stuff afterwards
if (file->tokens_exist)
file_relex_parallel(mem, file, start, end, shift_amount);
file_relex_parallel(system, mem, file, start, end, shift_amount);
#endif
#if BUFFER_EXPERIMENT_SCALPEL <= 3
@ -2609,7 +2629,7 @@ file_do_single_edit(Mem_Options *mem, Editing_File *file,
for (i32 i = 0; i < panel_count; ++i, ++current_panel){
File_View *current_view = view_to_file_view(current_panel->view);
if (current_view && current_view->file == file){
view_measure_wraps(general, current_view);
view_measure_wraps(system, general, current_view);
write_cursor_with_index(cursors, &cursor_count, current_view->cursor.pos);
write_cursor_with_index(cursors, &cursor_count, current_view->mark);
}
@ -2638,7 +2658,8 @@ file_do_single_edit(Mem_Options *mem, Editing_File *file,
}
internal void
view_do_white_batch_edit(Mem_Options *mem, File_View *view, Editing_File *file,
view_do_white_batch_edit(System_Functions *system,
Mem_Options *mem, File_View *view, Editing_File *file,
Editing_Layout *layout, Edit_Spec spec, History_Mode history_mode){
if (view->locked) return;
#if BUFFER_EXPERIMENT_SCALPEL <= 3
@ -2648,7 +2669,7 @@ view_do_white_batch_edit(Mem_Options *mem, File_View *view, Editing_File *file,
// NOTE(allen): fixing stuff beforewards????
Assert(spec.str == 0);
file_update_history_before_edit(mem, file, spec.step, 0, history_mode);
file_pre_edit_maintenance(file);
file_pre_edit_maintenance(system, file);
// NOTE(allen): actual text replacement
General_Memory *general = &mem->general;
@ -2720,7 +2741,7 @@ view_do_white_batch_edit(Mem_Options *mem, File_View *view, Editing_File *file,
for (i32 i = 0; i < panel_count; ++i, ++current_panel){
File_View *current_view = view_to_file_view(current_panel->view);
if (current_view && current_view->file == file){
view_measure_wraps(general, current_view);
view_measure_wraps(system, general, current_view);
write_cursor_with_index(cursors, &cursor_count, current_view->cursor.pos);
write_cursor_with_index(cursors, &cursor_count, current_view->mark);
}
@ -2748,7 +2769,8 @@ view_do_white_batch_edit(Mem_Options *mem, File_View *view, Editing_File *file,
}
inline void
view_replace_range(Mem_Options *mem, File_View *view, Editing_Layout *layout,
view_replace_range(System_Functions *system,
Mem_Options *mem, File_View *view, Editing_Layout *layout,
i32 start, i32 end, u8 *str, i32 len, i32 next_cursor){
if (view->locked) return;
Edit_Spec spec = {};
@ -2759,7 +2781,7 @@ view_replace_range(Mem_Options *mem, File_View *view, Editing_Layout *layout,
spec.step.pre_pos = view->cursor.pos;
spec.step.post_pos = next_cursor;
spec.str = str;
file_do_single_edit(mem, view->file, layout, spec, hist_normal);
file_do_single_edit(system, mem, view->file, layout, spec, hist_normal);
}
inline void
@ -2772,7 +2794,8 @@ view_post_paste_effect(File_View *view, i32 ticks, i32 start, i32 size, u32 colo
}
internal void
view_undo_redo(Mem_Options *mem, Editing_Layout *layout, File_View *view, Editing_File *file,
view_undo_redo(System_Functions *system,
Mem_Options *mem, Editing_Layout *layout, File_View *view, Editing_File *file,
Edit_Stack *stack, Edit_Type expected_type){
if (view->locked) return;
if (file && stack->edit_count > 0){
@ -2787,7 +2810,7 @@ view_undo_redo(Mem_Options *mem, Editing_Layout *layout, File_View *view, Editin
spec.step.edit.str_start = 0;
spec.str = stack->strings + step.edit.str_start;
file_do_single_edit(mem, file, layout, spec, hist_normal);
file_do_single_edit(system, mem, file, layout, spec, hist_normal);
if (expected_type == ED_UNDO) view_cursor_move(view, step.pre_pos);
else view_cursor_move(view, step.post_pos);
@ -2798,21 +2821,21 @@ view_undo_redo(Mem_Options *mem, Editing_Layout *layout, File_View *view, Editin
}
else{
TentativeAssert(spec.step.special_type == 1);
view_do_white_batch_edit(mem, view, file, layout, spec, hist_normal);
view_do_white_batch_edit(system, mem, view, file, layout, spec, hist_normal);
}
}
}
inline void
view_undo(Mem_Options *mem, Editing_Layout *layout, File_View *view){
view_undo(System_Functions *system, Mem_Options *mem, Editing_Layout *layout, File_View *view){
Editing_File *file = view->file;
view_undo_redo(mem, layout, view, file, &file->undo.undo, ED_UNDO);
view_undo_redo(system, mem, layout, view, file, &file->undo.undo, ED_UNDO);
}
inline void
view_redo(Mem_Options *mem, Editing_Layout *layout, File_View *view){
view_redo(System_Functions *system, Mem_Options *mem, Editing_Layout *layout, File_View *view){
Editing_File *file = view->file;
view_undo_redo(mem, layout, view, file, &file->undo.redo, ED_REDO);
view_undo_redo(system, mem, layout, view, file, &file->undo.redo, ED_REDO);
}
inline u8*
@ -2822,7 +2845,7 @@ write_data(u8 *ptr, void *x, i32 size){
}
internal void
file_dump_history(Mem_Options *mem, Editing_File *file, char *filename){
file_dump_history(System_Functions *system, Mem_Options *mem, Editing_File *file, char *filename){
if (!file->undo.undo.edits) return;
i32 size = 0;
@ -2871,18 +2894,18 @@ file_dump_history(Mem_Options *mem, Editing_File *file, char *filename){
curs = write_data(curs, file->undo.children.strings, file->undo.children.size);
Assert((i32)(curs - data) == size);
system_save_file((u8*)filename, data, size);
system->save_file(filename, data, size);
}
}
internal void
view_history_step(Mem_Options *mem, Editing_Layout *layout, File_View *view, History_Mode history_mode){
view_history_step(System_Functions *system, Mem_Options *mem, Editing_Layout *layout, File_View *view, History_Mode history_mode){
if (view->locked) return;
Assert(history_mode != hist_normal);
Editing_File *file = view->file;
bool32 do_history_step = 0;
b32 do_history_step = 0;
Edit_Step step = {};
if (history_mode == hist_backward){
if (file->undo.edit_history_cursor > 0){
@ -2908,7 +2931,7 @@ view_history_step(Mem_Options *mem, Editing_Layout *layout, File_View *view, His
spec.step.edit.str_start = 0;
spec.str = file->undo.history.strings + step.edit.str_start;
file_do_single_edit(mem, file, layout, spec, history_mode);
file_do_single_edit(system, mem, file, layout, spec, history_mode);
switch (spec.step.type){
case ED_NORMAL:
@ -2925,7 +2948,7 @@ view_history_step(Mem_Options *mem, Editing_Layout *layout, File_View *view, His
}
else{
TentativeAssert(spec.step.special_type == 1);
view_do_white_batch_edit(mem, view, file, layout, spec, history_mode);
view_do_white_batch_edit(system, mem, view, file, layout, spec, history_mode);
}
}
}
@ -3063,13 +3086,13 @@ working_set_lookup_file(Working_Set *working_set, String string){
}
internal void
clipboard_copy(General_Memory *general, Working_Set *working, Range range, Editing_File *file){
clipboard_copy(System_Functions *system, General_Memory *general, Working_Set *working, Range range, Editing_File *file){
#if BUFFER_EXPERIMENT_SCALPEL <= 3
i32 size = range.end - range.start;
String *dest = working_set_next_clipboard_string(general, working, size);
buffer_stringify(&file->buffer, range.start, range.end, dest->str);
dest->size = size;
system_post_clipboard(*dest);
system->post_clipboard(*dest);
#endif
}
@ -3111,7 +3134,7 @@ file_compute_whitespace_edit(Mem_Options *mem, Editing_File *file, i32 cursor_po
}
internal void
view_clean_whitespace(Mem_Options *mem, File_View *view, Editing_Layout *layout){
view_clean_whitespace(System_Functions *system, Mem_Options *mem, File_View *view, Editing_Layout *layout){
#if BUFFER_EXPERIMENT_SCALPEL <= 3
Editing_File *file = view->file;
Assert(file && !file->is_dummy);
@ -3162,7 +3185,7 @@ view_clean_whitespace(Mem_Options *mem, File_View *view, Editing_Layout *layout)
file_compute_whitespace_edit(mem, file, view->cursor.pos, edits, str_base, str_size,
inverse_array, inv_str, part->max - part->pos, edit_count);
view_do_white_batch_edit(mem, view, file, layout, spec, hist_normal);
view_do_white_batch_edit(system, mem, view, file, layout, spec, hist_normal);
}
end_temp_memory(temp);
@ -3170,7 +3193,8 @@ view_clean_whitespace(Mem_Options *mem, File_View *view, Editing_Layout *layout)
}
internal void
view_auto_tab_tokens(Mem_Options *mem, File_View *view, Editing_Layout *layout,
view_auto_tab_tokens(System_Functions *system,
Mem_Options *mem, File_View *view, Editing_Layout *layout,
i32 start, i32 end, b32 empty_blank_lines){
#if BUFFER_EXPERIMENT_SCALPEL <= 0
Editing_File *file = view->file;
@ -3351,7 +3375,7 @@ view_auto_tab_tokens(Mem_Options *mem, File_View *view, Editing_Layout *layout,
file_compute_whitespace_edit(mem, file, view->cursor.pos, edits, str_base, str_size,
inverse_array, inv_str, part->max - part->pos, edit_count);
view_do_white_batch_edit(mem, view, file, layout, spec, hist_normal);
view_do_white_batch_edit(system, mem, view, file, layout, spec, hist_normal);
}
end_temp_memory(temp);
@ -3493,10 +3517,10 @@ view_compute_max_target_y(File_View *view){
}
internal void
remeasure_file_view(View *view_, i32_Rect rect){
remeasure_file_view(System_Functions *system, View *view_, i32_Rect rect){
File_View *view = (File_View*)view_;
Relative_Scrolling relative = view_get_relative_scrolling(view);
view_measure_wraps(&view->view_base.mem->general, view);
view_measure_wraps(system, &view->view_base.mem->general, view);
view_cursor_move(view, view->cursor.pos);
view->preferred_x = view_get_cursor_x(view);
view_set_relative_scrolling(view, relative);
@ -3696,7 +3720,7 @@ do_undo_slider(Widget_ID wid, UI_State *state, UI_Layout *layout, i32 max, i32 v
}
internal i32
step_file_view(Thread_Context *thread, View *view_, i32_Rect rect,
step_file_view(System_Functions *system, Thread_Context *thread, View *view_, i32_Rect rect,
bool32 is_active, Input_Summary *user_input){
view_->mouse_cursor_type = APP_MOUSE_CURSOR_IBEAM;
i32 result = 0;
@ -3829,11 +3853,11 @@ step_file_view(Thread_Context *thread, View *view_, i32_Rect rect,
view->rewind_amount = 0;
for (i32 i = 0; i < scrub_max && new_count < undo_count; ++i){
view_undo(view_->mem, view->layout, view);
view_undo(system, view_->mem, view->layout, view);
--undo_count;
}
for (i32 i = 0; i < scrub_max && new_count > undo_count; ++i){
view_redo(view_->mem, view->layout, view);
view_redo(system, view_->mem, view->layout, view);
++undo_count;
}
}
@ -3857,10 +3881,10 @@ step_file_view(Thread_Context *thread, View *view_, i32_Rect rect,
i32 count = file->undo.edit_history_cursor;
if (do_undo_slider(wid, &state, &layout, mid, count, &file->undo, &new_count)){
for (i32 i = 0; i < scrub_max && new_count < count; ++i){
view_history_step(view_->mem, view->layout, view, hist_backward);
view_history_step(system, view_->mem, view->layout, view, hist_backward);
}
for (i32 i = 0; i < scrub_max && new_count > count; ++i){
view_history_step(view_->mem, view->layout, view, hist_forward);
view_history_step(system, view_->mem, view->layout, view, hist_forward);
}
}
}
@ -3882,11 +3906,11 @@ step_file_view(Thread_Context *thread, View *view_, i32_Rect rect,
i32 rewind_max = view->rewind_max;
view->rewind_amount += view->rewind_speed;
for (i32 i = 0; view->rewind_amount <= -1.f; ++i, view->rewind_amount += 1.f){
if (i < rewind_max) view_undo(view_->mem, view->layout, view);
if (i < rewind_max) view_undo(system, view_->mem, view->layout, view);
}
for (i32 i = 0; view->rewind_amount >= 1.f; ++i, view->rewind_amount -= 1.f){
if (i < rewind_max) view_redo(view_->mem, view->layout, view);
if (i < rewind_max) view_redo(system, view_->mem, view->layout, view);
}
if (view->rewind_speed < 0.f && undo_count == 0){
@ -4177,7 +4201,8 @@ draw_file_view(Thread_Context *thread, View *view_, i32_Rect rect, bool32 is_act
}
internal void
kill_file(General_Memory *general, Editing_File *file, Live_Views *live_set, Editing_Layout *layout){
kill_file(System_Functions *system,
General_Memory *general, Editing_File *file, Live_Views *live_set, Editing_Layout *layout){
i32 panel_count = layout->panel_count;
Panel *panels = layout->panels, *panel;
panel = panels;
@ -4189,21 +4214,21 @@ kill_file(General_Memory *general, Editing_File *file, Live_Views *live_set, Edi
if (view->is_minor) to_kill = view->major;
File_View *fview = view_to_file_view(to_kill);
if (fview && fview->file == file){
live_set_free_view(live_set, &fview->view_base);
live_set_free_view(system, live_set, &fview->view_base);
if (to_kill == view) panel->view = 0;
else view->major = 0;
}
}
++panel;
}
file_close(general, file);
file_close(system, general, file);
file_get_dummy(file);
}
internal void
command_search(Command_Data*,Command_Binding);
command_search(System_Functions*,Command_Data*,Command_Binding);
internal void
command_rsearch(Command_Data*,Command_Binding);
command_rsearch(System_Functions*,Command_Data*,Command_Binding);
internal
HANDLE_COMMAND_SIG(handle_command_file_view){
@ -4216,7 +4241,7 @@ HANDLE_COMMAND_SIG(handle_command_file_view){
case FWIDG_TIMELINES:
{
file_view->next_mode = {};
if (binding.function) binding.function(command, binding);
if (binding.function) binding.function(system, command, binding);
file_view->mode = file_view->next_mode;
if (key.key.keycode == codes->esc)
@ -4228,7 +4253,7 @@ HANDLE_COMMAND_SIG(handle_command_file_view){
#if BUFFER_EXPERIMENT_SCALPEL <= 3
String *string = &file_view->isearch.str;
Single_Line_Input_Step result =
app_single_line_input_step(codes, key, string);
app_single_line_input_step(system, codes, key, string);
if (result.made_a_change ||
binding.function == command_search ||
@ -4309,7 +4334,7 @@ HANDLE_COMMAND_SIG(handle_command_file_view){
{
String *string = &file_view->gotoline.str;
Single_Line_Input_Step result =
app_single_number_input_step(codes, key, string);
app_single_number_input_step(system, codes, key, string);
if (result.hit_newline || result.hit_ctrl_newline){
i32 line_number = str_to_int(*string);
@ -4344,7 +4369,7 @@ DO_VIEW_SIG(do_file_view){
case VMSG_RESIZE:
case VMSG_STYLE_CHANGE:
{
remeasure_file_view(view, rect);
remeasure_file_view(system, view, rect);
}break;
case VMSG_DRAW:
{
@ -4352,7 +4377,7 @@ DO_VIEW_SIG(do_file_view){
}break;
case VMSG_STEP:
{
result = step_file_view(thread, view, rect, (view == active), user_input);
result = step_file_view(system, thread, view, rect, (view == active), user_input);
}break;
case VMSG_FREE:
{

View File

@ -97,7 +97,8 @@ interactive_view_complete(Interactive_View *view){
}
internal i32
step_draw_int_view(Interactive_View *view, Render_Target *target, i32_Rect rect,
step_draw_int_view(System_Functions *system,
Interactive_View *view, Render_Target *target, i32_Rect rect,
Input_Summary *user_input, bool32 input_stage){
i32 result = 0;
@ -115,17 +116,19 @@ step_draw_int_view(Interactive_View *view, Render_Target *target, i32_Rect rect,
switch (view->interaction){
case INTV_SYS_FILE_LIST:
if (do_file_list_box(&state, &layout, view->hot_directory, 0,
if (do_file_list_box(system,
&state, &layout, view->hot_directory, 0,
&new_dir, &complete, 0)){
result = 1;
}
if (new_dir){
hot_directory_reload(view->hot_directory, view->working_set);
hot_directory_reload(system,
view->hot_directory, view->working_set);
}
break;
case INTV_LIVE_FILE_LIST:
if (do_live_file_list_box(&state, &layout, view->working_set, &view->dest, &complete)){
if (do_live_file_list_box(system, &state, &layout, view->working_set, &view->dest, &complete)){
result = 1;
}
break;
@ -191,7 +194,7 @@ DO_VIEW_SIG(do_interactive_view){
Interactive_View *int_view = (Interactive_View*)view;
switch (message){
case VMSG_STEP: case VMSG_DRAW:
result = step_draw_int_view(int_view, target, rect, user_input, (message == VMSG_STEP));
result = step_draw_int_view(system, int_view, target, rect, user_input, (message == VMSG_STEP));
break;
}
@ -199,14 +202,14 @@ DO_VIEW_SIG(do_interactive_view){
}
internal Interactive_View*
interactive_view_init(View *view, Hot_Directory *hot_dir, Style *style,
interactive_view_init(System_Functions *system, View *view, Hot_Directory *hot_dir, Style *style,
Working_Set *working_set, Delay *delay){
Interactive_View *result = (Interactive_View*)view;
view->type = VIEW_TYPE_INTERACTIVE;
view->do_view = do_interactive_view;
result->hot_directory = hot_dir;
hot_directory_clean_end(hot_dir);
hot_directory_reload(hot_dir, working_set);
hot_directory_reload(system, hot_dir, working_set);
result->query = make_fixed_width_string(result->query_);
result->dest = make_fixed_width_string(result->dest_);
result->style = style;

View File

@ -7,178 +7,32 @@
*
*/
/*
* Profiling
*/
// TOP
#if FRED_INTERNAL == 1
enum Debug_Event_Type{
DBGEV_START,
DBGEV_END,
DBGEV_MOMENT,
// never below this
DBGEV_COUNT
};
#define ProfileStart_(name, start, counter, hit, thread, n, c)
struct Debug_Event{
i64 time;
char *name;
Debug_Event_Type type;
i32 which_hit;
i32 event_index;
i32 thread_index;
};
#define ProfileEnd_(name, start, counter, hit, thread)
struct Debug_Event_Array{
volatile u32 count;
Debug_Event e[4096];
};
#define ProfileMoment_(name, counter, thread)
struct Profile_Frame{
Debug_Event_Array events;
i32 dbg_procing_start;
i32 dbg_procing_end;
i32 index;
i32 first_key;
};
#if 0
Profile_Frame profile_frame;
#define ProfileStart(name) char *_pname_##name; i64 _pstart_##name; \
i32 _pcounter_##name; u32 _phit_##name; \
ProfileStart_(_pname_##name, _pstart_##name, _pcounter_##name, \
_phit_##name, system->thread_get_id(thread), \
#name, __COUNTER__)
#define PAST_PROFILE_COUNT 30
Profile_Frame past_frames[PAST_PROFILE_COUNT];
extern const i32 INTERNAL_event_index_count;
extern u32 INTERNAL_event_hits[];
i64 INTERNAL_frame_start_time;
bool32 INTERNAL_collecting_events;
inline u32
post_debug_event(char *name, Debug_Event_Type type, i32 event_index, i32 thread_index, u32 which_hit){
u32 result = 0;
if (INTERNAL_collecting_events){
u32 index =
InterlockedIncrement(&profile_frame.events.count);
--index;
Assert(index < ArrayCount(profile_frame.events.e));
Debug_Event ev;
ev.time = system_time() - INTERNAL_frame_start_time;
ev.name = name;
ev.type = type;
ev.event_index = event_index;
ev.thread_index = thread_index;
if (type == DBGEV_END){
ev.which_hit = which_hit;
}
else{
ev.which_hit = InterlockedIncrement(INTERNAL_event_hits + event_index) - 1;
}
profile_frame.events.e[index] = ev;
result = ev.which_hit;
}
return result;
}
internal u32
quick_partition(Debug_Event *es, u32 start, u32 pivot){
Debug_Event *p = es + pivot;
i32 m = (start + pivot) >> 1;
Swap(*p, es[m]);
i32 pn = p->thread_index;
i32 pe = p->event_index;
i32 ph = p->which_hit;
i32 pt = p->type;
for (u32 i = start; i < pivot; ++i){
Debug_Event *e = es + i;
bool32 smaller = 0;
if (e->thread_index < pn) smaller = 1;
else if (e->thread_index == pn){
if (e->type != DBGEV_MOMENT && pt == DBGEV_MOMENT) smaller = 1;
else if (e->type != DBGEV_MOMENT){
if (e->event_index < pe) smaller = 1;
else if (e->event_index == pe){
if (e->which_hit < ph) smaller = 1;
else if (e->which_hit == ph){
if (e->type < pt) smaller = 1;
}
}
}
else if (pt == DBGEV_MOMENT){
if (e->time < p->time) smaller = 1;
}
}
if (smaller){
Swap(*e, es[start]);
++start;
}
}
Swap(*p, es[start]);
return start;
}
internal void
quick_sort(Debug_Event *e, u32 start, u32 pivot){
u32 mid = quick_partition(e, start, pivot);
if (start + 1 < mid) quick_sort(e, start, mid - 1);
if (mid + 1 < pivot) quick_sort(e, mid + 1, pivot);
}
inline void
sort(Debug_Event_Array *events){
quick_sort(events->e, 0, events->count - 1);
}
globalvar i32 INTERNAL_frame_index;
globalvar bool32 INTERNAL_updating_profile;
#define ProfileStart_(name, start, counter, hit, thread, n, c)\
name = n; counter = c; start = system_time(); hit = post_debug_event(n, DBGEV_START, counter, thread, 0)
#define ProfileEnd_(name, start, counter, hit, thread) post_debug_event(name, DBGEV_END, counter, thread, hit)
#define ProfileMoment_(name, counter, thread) post_debug_event(name, DBGEV_MOMENT, counter, thread, 0)
struct INTERNAL_Profile_Block{
char *name;
i64 start;
i32 counter;
i32 thread;
i32 hit;
INTERNAL_Profile_Block(char *n, i32 c, i32 t){
ProfileStart_(name, start, counter, hit, t, n, c);
thread = t;
}
~INTERNAL_Profile_Block(){
ProfileEnd_(name, start, counter, hit, thread);
}
};
#define ProfileBlock(name, thread) INTERNAL_Profile_Block name(#name, __COUNTER__, thread)
#define ProfileBlockFunction() INTERNAL_Profile_Block name(__FUNCTION__, __COUNTER__, 0)
#define ProfileStart(name) char *_pname_##name; i64 _pstart_##name; i32 _pcounter_##name; u32 _phit_##name; \
ProfileStart_(_pname_##name, _pstart_##name, _pcounter_##name, _phit_##name, system_thread_get_id(thread), #name, __COUNTER__)
#define ProfileEnd(name) ProfileEnd_(_pname_##name, _pstart_##name, _pcounter_##name, _phit_##name, system_thread_get_id(thread))
#define ProfileEnd(name) ProfileEnd_(_pname_##name, _pstart_##name, \
_pcounter_##name, _phit_##name, \
system->thread_get_id(thread))
#define ProfileMoment(name, thread) ProfileMoment_(#name, __COUNTER__, thread)
#define ProfileMomentFunction() ProfileMoment_(__FUNCTION__, __COUNTER__, 0)
#else
#define ProfileBlock(name)
#define ProfileStart(name)
#define ProfileEnd(name)
#define ProfileMoment(name)
@ -186,3 +40,21 @@ struct INTERNAL_Profile_Block{
#endif
struct Sys_Bubble : public Bubble{
i32 line_number;
char *file_name;
};
#else
#define ProfileStart(name)
#define ProfileEnd(name)
#define ProfileMoment(name)
#define ProfileMomentFunction()
#endif
// BOTTOM

View File

@ -38,14 +38,19 @@ enum View_Message{
};
struct View;
#define DO_VIEW_SIG(name)\
i32 (name)(Thread_Context *thread, View *view, i32_Rect rect, View *active,\
View_Message message, Render_Target *target, Input_Summary *user_input, Input_Summary *active_input)
#define DO_VIEW_SIG(name) \
i32 (name)(System_Functions *system, Thread_Context *thread, \
View *view, i32_Rect rect, View *active, \
View_Message message, Render_Target *target, \
Input_Summary *user_input, Input_Summary *active_input)
typedef DO_VIEW_SIG(Do_View_Function);
#define HANDLE_COMMAND_SIG(name)\
void (name)(View *view, Command_Data *command, Command_Binding binding,\
#define HANDLE_COMMAND_SIG(name) \
void (name)(System_Functions *system, View *view, \
Command_Data *command, Command_Binding binding, \
Key_Single key, Key_Codes *codes)
typedef HANDLE_COMMAND_SIG(Handle_Command_Function);
// TODO(allen): this shouldn't exist
@ -186,9 +191,9 @@ live_set_alloc_view(Live_Views *live_set, Mem_Options *mem){
}
inline void
live_set_free_view(Live_Views *live_set, View *view){
live_set_free_view(System_Functions *system, Live_Views *live_set, View *view){
Assert(live_set->count > 0);
view->do_view(0, view, {}, 0, VMSG_FREE, 0, {}, 0);
view->do_view(system, 0, view, {}, 0, VMSG_FREE, 0, {}, 0);
view->next_free = live_set->free_view;
live_set->free_view = view;
--live_set->count;
@ -196,13 +201,13 @@ live_set_free_view(Live_Views *live_set, View *view){
}
inline void
view_replace_major(View *new_view, Panel *panel, Live_Views *live_set){
view_replace_major(System_Functions *system, View *new_view, Panel *panel, Live_Views *live_set){
View *view = panel->view;
if (view){
if (view->is_minor && view->major){
live_set_free_view(live_set, view->major);
live_set_free_view(system, live_set, view->major);
}
live_set_free_view(live_set, view);
live_set_free_view(system, live_set, view);
}
new_view->panel = panel;
new_view->minor = 0;
@ -210,13 +215,13 @@ view_replace_major(View *new_view, Panel *panel, Live_Views *live_set){
}
inline void
view_replace_minor(View *new_view, Panel *panel, Live_Views *live_set){
view_replace_minor(System_Functions *system, View *new_view, Panel *panel, Live_Views *live_set){
View *view = panel->view;
new_view->is_minor = 1;
if (view){
if (view->is_minor){
new_view->major = view->major;
live_set_free_view(live_set, view);
live_set_free_view(system, live_set, view);
}
else{
new_view->major = view;
@ -231,40 +236,40 @@ view_replace_minor(View *new_view, Panel *panel, Live_Views *live_set){
}
inline void
view_remove_major(Panel *panel, Live_Views *live_set){
view_remove_major(System_Functions *system, Panel *panel, Live_Views *live_set){
View *view = panel->view;
if (view){
if (view->is_minor && view->major){
live_set_free_view(live_set, view->major);
live_set_free_view(system, live_set, view->major);
}
live_set_free_view(live_set, view);
live_set_free_view(system, live_set, view);
}
panel->view = 0;
}
inline void
view_remove_major_leave_minor(Panel *panel, Live_Views *live_set){
view_remove_major_leave_minor(System_Functions *system, Panel *panel, Live_Views *live_set){
View *view = panel->view;
if (view){
if (view->is_minor && view->major){
live_set_free_view(live_set, view->major);
live_set_free_view(system, live_set, view->major);
view->major = 0;
}
else{
live_set_free_view(live_set, view);
live_set_free_view(system, live_set, view);
panel->view = 0;
}
}
}
inline void
view_remove_minor(Panel *panel, Live_Views *live_set){
view_remove_minor(System_Functions *system, Panel *panel, Live_Views *live_set){
View *view = panel->view;
View *major = 0;
if (view){
if (view->is_minor){
major = view->major;
live_set_free_view(live_set, view);
live_set_free_view(system, live_set, view);
}
}
panel->view = major;
@ -272,10 +277,10 @@ view_remove_minor(Panel *panel, Live_Views *live_set){
}
inline void
view_remove(Panel *panel, Live_Views *live_set){
view_remove(System_Functions *system, Panel *panel, Live_Views *live_set){
View *view = panel->view;
if (view->is_minor) view_remove_minor(panel, live_set);
else view_remove_major(panel, live_set);
if (view->is_minor) view_remove_minor(system, panel, live_set);
else view_remove_major(system, panel, live_set);
}
struct Divider_And_ID{

253
4ed_mem.cpp Normal file
View File

@ -0,0 +1,253 @@
/*
* Mr. 4th Dimention - Allen Webster
*
* 13.11.2015
*
* Memory utils for 4coder
*
*/
// TOP
struct Partition{
u8 *base;
i32 pos, max;
};
inline Partition
partition_open(void *memory, i32 size){
Partition partition;
partition.base = (u8*)memory;;
partition.pos = 0;
partition.max = size;
return partition;
}
inline void*
partition_allocate(Partition *data, i32 size){
void *ret = 0;
if (size > 0 && data->pos + size <= data->max){
ret = data->base + data->pos;
data->pos += size;
}
return ret;
}
inline void
partition_align(Partition *data, u32 boundary){
data->pos = (data->pos + (boundary - 1)) & (~boundary);
}
inline void*
partition_current(Partition *data){
return data->base + data->pos;
}
inline i32
partition_remaining(Partition *data){
return data->max - data->pos;
}
inline Partition
partition_sub_part(Partition *data, i32 size){
Partition result = {};
void *d = partition_allocate(data, size);
if (d) result = partition_open(d, size);
return result;
}
#define push_struct(part, T) (T*)partition_allocate(part, sizeof(T))
#define push_array(part, T, size) (T*)partition_allocate(part, sizeof(T)*(size))
#define push_block(part, size) partition_allocate(part, size)
enum Memory_Bubble_Flag{
MEM_BUBBLE_USED = 0x1,
MEM_BUBBLE_DEBUG = 0xD3000000,
MEM_BUBBLE_SYS_DEBUG = 0x5D000000,
MEM_BUBBLE_DEBUG_MASK = 0xFF000000
};
struct Bubble{
Bubble *prev;
Bubble *next;
u32 flags;
i32 size;
u32 type;
u32 _unused_;
};
struct General_Memory{
Bubble sentinel;
};
inline void
insert_bubble(Bubble *prev, Bubble *bubble){
bubble->prev = prev;
bubble->next = prev->next;
bubble->prev->next = bubble;
bubble->next->prev = bubble;
}
inline void
remove_bubble(Bubble *bubble){
bubble->prev->next = bubble->next;
bubble->next->prev = bubble->prev;
}
#if FRED_INTERNAL
#define MEM_BUBBLE_FLAG_INIT MEM_BUBBLE_DEBUG
#else
#define MEM_BUBBLE_FLAG_INIT 0
#endif
internal void
general_memory_open(General_Memory *general, void *memory, i32 size){
general->sentinel.prev = &general->sentinel;
general->sentinel.next = &general->sentinel;
general->sentinel.flags = MEM_BUBBLE_USED;
general->sentinel.size = 0;
Bubble *first = (Bubble*)memory;
first->flags = (u32)MEM_BUBBLE_FLAG_INIT;
first->size = size - sizeof(Bubble);
insert_bubble(&general->sentinel, first);
}
internal void
general_memory_check(General_Memory *general){
Bubble *sentinel = &general->sentinel;
for (Bubble *bubble = sentinel->next;
bubble != sentinel;
bubble = bubble->next){
Assert(bubble);
Bubble *next = bubble->next;
Assert(bubble == next->prev);
if (next != sentinel){
Assert(bubble->next > bubble);
Assert(bubble > bubble->prev);
char *end_ptr = (char*)(bubble + 1) + bubble->size;
char *next_ptr = (char*)next;
AllowLocal(end_ptr);
AllowLocal(next_ptr);
Assert(end_ptr == next_ptr);
}
}
}
#define BUBBLE_MIN_SIZE 1024
internal void
general_memory_attempt_split(Bubble *bubble, i32 wanted_size){
i32 remaining_size = bubble->size - wanted_size;
if (remaining_size >= BUBBLE_MIN_SIZE){
bubble->size = wanted_size;
Bubble *new_bubble = (Bubble*)((u8*)(bubble + 1) + wanted_size);
new_bubble->flags = (u32)MEM_BUBBLE_FLAG_INIT;
new_bubble->size = remaining_size - sizeof(Bubble);
insert_bubble(bubble, new_bubble);
}
}
internal void*
general_memory_allocate(General_Memory *general, i32 size, u32 type = 0){
void *result = 0;
for (Bubble *bubble = general->sentinel.next;
bubble != &general->sentinel;
bubble = bubble->next){
if (!(bubble->flags & MEM_BUBBLE_USED)){
if (bubble->size >= size){
result = bubble + 1;
bubble->flags |= MEM_BUBBLE_USED;
bubble->type = type;
general_memory_attempt_split(bubble, size);
break;
}
}
}
return result;
}
inline void
general_memory_do_merge(Bubble *left, Bubble *right){
Assert(left->next == right);
Assert(right->prev == left);
left->size += sizeof(Bubble) + right->size;
remove_bubble(right);
}
inline void
general_memory_attempt_merge(Bubble *left, Bubble *right){
if (!(left->flags & MEM_BUBBLE_USED) &&
!(right->flags & MEM_BUBBLE_USED)){
general_memory_do_merge(left, right);
}
}
internal void
general_memory_free(General_Memory *general, void *memory){
Bubble *bubble = ((Bubble*)memory) - 1;
Assert((!FRED_INTERNAL) || (bubble->flags & MEM_BUBBLE_DEBUG_MASK) == MEM_BUBBLE_DEBUG);
bubble->flags &= ~MEM_BUBBLE_USED;
bubble->type = 0;
Bubble *prev, *next;
prev = bubble->prev;
next = bubble->next;
general_memory_attempt_merge(bubble, next);
general_memory_attempt_merge(prev, bubble);
}
internal void*
general_memory_reallocate(General_Memory *general, void *old, i32 old_size, i32 size, u32 type = 0){
void *result = old;
Bubble *bubble = ((Bubble*)old) - 1;
bubble->type = type;
Assert((!FRED_INTERNAL) || (bubble->flags & MEM_BUBBLE_DEBUG_MASK) == MEM_BUBBLE_DEBUG);
i32 additional_space = size - bubble->size;
if (additional_space > 0){
Bubble *next = bubble->next;
if (!(next->flags & MEM_BUBBLE_USED) &&
next->size + sizeof(Bubble) >= additional_space){
general_memory_do_merge(bubble, next);
general_memory_attempt_split(bubble, size);
}
else{
result = general_memory_allocate(general, size, type);
if (old_size) memcpy(result, old, old_size);
general_memory_free(general, old);
}
}
return result;
}
inline void*
general_memory_reallocate_nocopy(General_Memory *general, void *old, i32 size, u32 type = 0){
return general_memory_reallocate(general, old, 0, size, type);
}
struct Temp_Memory{
Partition *part;
i32 pos;
};
internal Temp_Memory
begin_temp_memory(Partition *data){
Temp_Memory result;
result.part = data;
result.pos = data->pos;
return result;
}
internal void
end_temp_memory(Temp_Memory temp){
temp.part->pos = temp.pos;
}
struct Mem_Options{
Partition part;
General_Memory general;
};
// BOTTOM

View File

@ -33,6 +33,7 @@ typedef double real64;
typedef float f32;
typedef double f64;
#define external extern "C"
#define internal static
#define globalvar static
#define persist static

View File

@ -669,12 +669,13 @@ font_predict_size(i32 pt_size){
}
internal i32
font_load(Font *font_out, char *filename, i32 pt_size,
font_load(System_Functions *system,
Font *font_out, char *filename, i32 pt_size,
void *font_block, i32 font_block_size,
i32 *memory_used_out, i32 tab_width){
i32 result = 1;
File_Data file;
file = system_load_file((u8*)filename);
file = system->load_file(filename);
if (!file.data){
result = 0;
@ -749,7 +750,7 @@ font_load(Font *font_out, char *filename, i32 pt_size,
}
font_out->advance = max_advance - 1;
}
system_free_file(file);
system->free_file(file);
}
return result;

View File

@ -475,10 +475,11 @@ style_format_for_use(Font_Set *fonts, Style *out, Style_File_Format_v3 *style){
}
internal bool32
style_library_import(u8 *filename, Font_Set *fonts, Style *out, i32 max,
style_library_import(System_Functions *system,
char *filename, Font_Set *fonts, Style *out, i32 max,
i32 *count_opt, i32 *total_opt = 0){
bool32 result = 1;
File_Data file = system_load_file(filename);
File_Data file = system->load_file(filename);
if (!file.data){
result = 0;
}
@ -538,7 +539,7 @@ style_library_import(u8 *filename, Font_Set *fonts, Style *out, i32 max,
}
early_exit:
system_free_file(file);
system->free_file(file);
}
return result;
@ -594,10 +595,10 @@ style_format_for_file(Style *style, Style_File_Format *out){
}
internal void
style_library_export(u8 *filename, Style **styles, i32 count){
style_library_export(System_Functions *system, char *filename, Style **styles, i32 count){
i32 size = count*(sizeof(Style_File_Format) + STAG_COUNT*sizeof(Style_Color_Specifier)) +
sizeof(P4C_Page_Header) + sizeof(Style_Page_Header);
void *data = system_get_memory(size);
void *data = system->get_memory(size);
void *cursor = data;
{
@ -619,8 +620,8 @@ style_library_export(u8 *filename, Style **styles, i32 count){
for (i32 i = 0; i < count; ++i){
out = style_format_for_file(*in++, out);
}
system_save_file(filename, data, size);
system_free_memory(data);
system->save_file(filename, data, size);
system->free_memory(data);
}
// BOTTOM

View File

@ -7,10 +7,14 @@
*
*/
struct Plat_Handle{ u64 d[2]; };
// TOP
struct System_Functions;
struct Plat_Handle{
u32 d[4];
};
// TODO(allen): This should either be a String or it should be improved
// to handle 64-bit sized files. Staying in this state, however, is unacceptable.
struct File_Data{
void *data;
u32 size;
@ -18,65 +22,60 @@ struct File_Data{
struct Time_Stamp{
u64 time;
bool32 success;
b32 success;
};
internal File_Data
system_load_file(u8 *filename);
#define Sys_Load_File_Sig(name) File_Data name(char *filename)
typedef Sys_Load_File_Sig(System_Load_File);
internal bool32
system_save_file(u8 *filename, void *data, i32 size);
#define Sys_Save_File_Sig(name) i32 name(char *filename, void *data, i32 size)
typedef Sys_Save_File_Sig(System_Save_File);
internal Time_Stamp
system_file_time_stamp(u8 *filename);
#define Sys_File_Time_Stamp_Sig(name) Time_Stamp name(char *filename)
typedef Sys_File_Time_Stamp_Sig(System_File_Time_Stamp);
internal u64
system_get_now();
#define Sys_Time_Stamp_Now_Sig(name) u64 name()
typedef Sys_Time_Stamp_Now_Sig(System_Time_Stamp_Now);
internal void
system_free_file(File_Data data);
#define Sys_Free_File_Sig(name) void name(File_Data file)
typedef Sys_Free_File_Sig(System_Free_File);
internal void
system_fatal_error(u8 *message);
#define Sys_Get_Current_Directory_Sig(name) i32 name(char *out, i32 max)
typedef Sys_Get_Current_Directory_Sig(System_Get_Current_Directory);
internal i32
system_get_working_directory(u8 *destination, i32 max_size);
internal i32
system_get_easy_directory(u8 *destination);
#define Sys_Get_Easy_Directory_Sig(name) i32 name(char *destination)
typedef Sys_Get_Easy_Directory_Sig(System_Get_Easy_Directory);
struct File_Info{
String filename;
bool32 folder;
//bool32 loaded;
b32 folder;
};
struct File_List{
File_Info *infos;
i32 count;
void *block;
i32 count;
};
internal File_List
system_get_files(String directory);
#define Sys_Get_File_List_Sig(name) File_List name(String directory)
typedef Sys_Get_File_List_Sig(System_Get_File_List);
internal void
system_free_file_list(File_List list);
#define Sys_Free_File_List_Sig(name) void name(File_List list)
typedef Sys_Free_File_List_Sig(System_Free_File_List);
internal void*
system_get_memory_(i32 size, i32 line_number, char *file_name);
#define Sys_Get_Memory_Sig(name) void* name(i32 size, i32 line_number, char *filename)
typedef Sys_Get_Memory_Sig(System_Get_Memory);
#define system_get_memory(size) system_get_memory_(size, __LINE__, __FILE__)
#define get_memory(size) get_memory_full(size, __LINE__, __FILE__)
internal void
system_free_memory(void *block);
#define Sys_Free_Memory_Sig(name) void name(void *block);
typedef Sys_Free_Memory_Sig(System_Free_Memory);
internal void
system_post_clipboard(String str);
#define Sys_Post_Clipboard_Sig(name) void name(String str);
typedef Sys_Post_Clipboard_Sig(System_Post_Clipboard);
internal i64
system_time();
#define Sys_Time_Sig(name) i64 name()
typedef Sys_Time_Sig(System_Time);
struct CLI_Handles{
Plat_Handle proc;
@ -85,55 +84,20 @@ struct CLI_Handles{
u32 scratch_space[4];
};
internal b32
system_cli_call(char *path, char *script_name, CLI_Handles *cli_out);
#define Sys_CLI_Call_Sig(name) b32 name(char *path, char *script, CLI_Handles *cli)
typedef Sys_CLI_Call_Sig(System_CLI_Call);
internal void
system_cli_begin_update(CLI_Handles *cli);
#define Sys_CLI_Begin_Update_Sig(name) void name(CLI_Handles *cli)
typedef Sys_CLI_Begin_Update_Sig(System_CLI_Begin_Update);
internal b32
system_cli_update_step(CLI_Handles *cli, char *dest, u32 max, u32 *amount);
#define Sys_CLI_Update_Step_Sig(name) b32 name(CLI_Handles *cli, char *dest, u32 max, u32 *amount)
typedef Sys_CLI_Update_Step_Sig(System_CLI_Update_Step);
internal b32
system_cli_end_update(CLI_Handles *cli);
#define Sys_CLI_End_Update_Sig(name) b32 name(CLI_Handles *cli)
typedef Sys_CLI_End_Update_Sig(System_CLI_End_Update);
struct Thread_Context;
struct Thread_Memory{
void *data;
i32 size;
i32 id;
};
internal u32
system_thread_get_id(Thread_Context *thread);
internal u32
system_thread_current_job_id(Thread_Context *thread);
enum Thread_Group_ID{
BACKGROUND_THREADS,
THREAD_GROUP_COUNT
};
#define JOB_CALLBACK(name) void name(Thread_Context *thread, Thread_Memory *memory, void *data[2])
typedef JOB_CALLBACK(Job_Callback);
struct Job_Data{
Job_Callback *callback;
void *data[2];
i32 memory_request;
};
internal u32
system_post_job(Thread_Group_ID group_id, Job_Data job);
internal void
system_cancel_job(Thread_Group_ID group_id, u32 job_id);
internal bool32
system_job_is_pending(Thread_Group_ID group_id, u32 job_id);
enum Lock_ID{
FRAME_LOCK,
CANCEL_LOCK0,
@ -147,29 +111,97 @@ enum Lock_ID{
LOCK_COUNT
};
internal void
system_aquire_lock(Lock_ID id);
enum Thread_Group_ID{
BACKGROUND_THREADS,
THREAD_GROUP_COUNT
};
internal void
system_release_lock(Lock_ID id);
struct Thread_Memory{
void *data;
i32 size;
i32 id;
};
internal void
system_aquire_lock(i32 id);
#define Job_Callback(name) void name(System_Functions *system, Thread_Context *thread, Thread_Memory *memory, void *data[2])
typedef Job_Callback(Job_Callback);
internal void
system_release_lock(i32 id);
struct Job_Data{
Job_Callback *callback;
void *data[2];
i32 memory_request;
};
internal void
system_grow_thread_memory(Thread_Memory *memory);
#define Sys_Thread_Get_ID_Sig(name) u32 name(Thread_Context *thread)
typedef Sys_Thread_Get_ID_Sig(System_Thread_Get_ID);
internal void
system_force_redraw();
#define Sys_Thread_Current_Job_ID_Sig(name) u32 name(Thread_Context *thread)
typedef Sys_Thread_Current_Job_ID_Sig(System_Thread_Current_Job_ID);
#if FRED_INTERNAL
internal Bubble*
INTERNAL_system_sentinel();
#define Sys_Post_Job_Sig(name) u32 name(Thread_Group_ID id, Job_Data job)
typedef Sys_Post_Job_Sig(System_Post_Job);
internal void
INTERNAL_get_thread_states(Thread_Group_ID id, bool8 *running, i32 *pending);
#endif
#define Sys_Cancel_Job_Sig(name) void name(Thread_Group_ID id, u32 job_id)
typedef Sys_Cancel_Job_Sig(System_Cancel_Job);
#define Sys_Job_Is_Pending_Sig(name) b32 name(Thread_Group_ID id, u32 job_id)
typedef Sys_Job_Is_Pending_Sig(System_Job_Is_Pending);
#define Sys_Grow_Thread_Memory_Sig(name) void name(Thread_Memory *memory)
typedef Sys_Grow_Thread_Memory_Sig(System_Grow_Thread_Memory);
#define Sys_Acquire_Lock_Sig(name) void name(i32 id)
typedef Sys_Acquire_Lock_Sig(System_Acquire_Lock);
#define Sys_Release_Lock_Sig(name) void name(i32 id)
typedef Sys_Release_Lock_Sig(System_Release_Lock);
#define Sys_Force_Redraw_Sig(name) void name()
typedef Sys_Force_Redraw_Sig(System_Force_Redraw);
#define INTERNAL_Sys_Sentinel_Sig(name) Bubble* name()
typedef INTERNAL_Sys_Sentinel_Sig(INTERNAL_System_Sentinel);
#define INTERNAL_Sys_Get_Thread_States_Sig(name) void name(Thread_Group_ID id, b8 *running, i32 *pending)
typedef INTERNAL_Sys_Get_Thread_States_Sig(INTERNAL_System_Get_Thread_States);
struct System_Functions{
System_Load_File *load_file;
System_Save_File *save_file;
System_File_Time_Stamp *file_time_stamp;
System_Time_Stamp_Now *time_stamp_now;
System_Free_File *free_file;
System_Get_Current_Directory *get_current_directory;
System_Get_Easy_Directory *get_easy_directory;
System_Get_File_List *get_file_list;
System_Free_File_List *free_file_list;
System_Get_Memory *get_memory_full;
System_Free_Memory *free_memory;
System_Post_Clipboard *post_clipboard;
System_Time *time;
System_CLI_Call *cli_call;
System_CLI_Begin_Update *cli_begin_update;
System_CLI_Update_Step *cli_update_step;
System_CLI_End_Update *cli_end_update;
System_Thread_Get_ID *thread_get_id;
System_Thread_Current_Job_ID *thread_current_job_id;
System_Post_Job *post_job;
System_Cancel_Job *cancel_job;
System_Job_Is_Pending *job_is_pending;
System_Grow_Thread_Memory *grow_thread_memory;
System_Acquire_Lock *acquire_lock;
System_Release_Lock *release_lock;
System_Force_Redraw *force_redraw;
INTERNAL_System_Sentinel *internal_sentinel;
INTERNAL_System_Get_Thread_States *internal_get_thread_states;
};
// BOTTOM

View File

@ -1178,8 +1178,8 @@ void batch_edit_test(Stats_Log *log, Buffer_Set *buffers, int test_repitions, fl
Sample_Machine machine;
machine = begin_machine(test_repitions, &scratch, &scratch_size);
printf("batch size: %d\n", replay->replay.count);
log_data_item(log, litstr("batch-size"), replay->replay.count);
printf("batch size: %d\n", batch_size);
log_data_item(log, litstr("batch-size"), batch_size);
int i;
for (i = 0; i < test_repitions; ++i){
start(&machine);
@ -1396,20 +1396,13 @@ int main(int argc, char **argv){
{
char word[] = "not-going-to-find-this";
int word_len = sizeof(word) - 1;
word_seek_test(&log, &buffers, reps, 0, word, word_len, scratch, scratch_size, &word_seek);
}
{
char word[] = "return";
int word_len = sizeof(word) - 1;
word_seek_test(&log, &buffers, reps, 1, word, word_len, scratch, scratch_size, &word_seek);
printf("average normal word seek:\n");
print_record(word_seek.expected);
printf("\n");
}
}
log_end_section(&log);
@ -1463,7 +1456,7 @@ int main(int argc, char **argv){
character_stride = 10;
}
else{
character_stride = (character_stride / batch_size);
character_stride /= batch_size;
}
int p, curs;

File diff suppressed because it is too large Load Diff

View File

@ -8,10 +8,6 @@
*/
// TOP
// TODO(allen):
//
// Fix the OwnDC thing.
//
#ifdef FRED_NOT_PACKAGE
@ -58,8 +54,6 @@
#define FPS 30
#define FRAME_TIME (1000000 / FPS)
#define BUFFER_EXPERIMENT_SCALPEL 0
#include "4ed_meta.h"
#define FCPP_FORBID_MALLOC
@ -67,32 +61,25 @@
#include "4cpp_types.h"
#define FCPP_STRING_IMPLEMENTATION
#include "4cpp_string.h"
#define FCPP_LEXER_IMPLEMENTATION
#include "4cpp_lexer.h"
#include "4ed_mem.cpp"
#include "4ed_math.cpp"
#include "4coder_custom.h"
#include "4ed.h"
#include "4ed_system.h"
#include "4ed.h"
#include "4ed_rendering.h"
struct TEMP_BACKDOOR{
Get_Binding_Data_Function *get_bindings;
Set_Extra_Font_Function *set_extra_font;
} TEMP;
#if FRED_INTERNAL
struct Sys_Bubble : public Bubble{
i32 line_number;
char *file_name;
};
#endif
Config_API config_api;
#include <windows.h>
#include <GL/gl.h>
#include "4ed_internal.h"
#if 0
#define FCPP_LEXER_IMPLEMENTATION
#include "4cpp_lexer.h"
#include "4ed_rendering.cpp"
#include "4ed_command.cpp"
#include "4ed_layout.cpp"
@ -103,6 +90,8 @@ struct Sys_Bubble : public Bubble{
#include "4ed_menu_view.cpp"
#include "4ed_debug_view.cpp"
#include "4ed.cpp"
#endif
#include "4ed_keyboard.cpp"
struct Full_Job_Data{
@ -179,26 +168,70 @@ struct Win32_Vars{
Thread_Memory *thread_memory;
HMODULE custom;
HMODULE app_code;
i64 performance_frequency;
i64 start_pcount;
System_Functions *system;
App_Functions app;
#if FRED_INTERNAL
Sys_Bubble internal_bubble;
#endif
};
globalvar Win32_Vars win32vars;
globalvar Application_Memory win32memory;
internal void
_OutDbgStr(u8 *msg){
OutputDebugString((char*)msg);
internal Bubble*
INTERNAL_system_sentinel(){
return (&win32vars.internal_bubble);
}
internal void*
system_get_memory_(i32 size, i32 line_number, char *file_name){
void *ptr = 0;
#if FRED_INTERNAL
ptr = VirtualAlloc(0, size + sizeof(Sys_Bubble), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
Sys_Bubble *bubble = (Sys_Bubble*)ptr;
bubble->flags = MEM_BUBBLE_SYS_DEBUG;
bubble->line_number = line_number;
bubble->file_name = file_name;
bubble->size = size;
WaitForSingleObject(win32vars.DEBUG_sysmem_lock, INFINITE);
insert_bubble(&win32vars.internal_bubble, bubble);
ReleaseSemaphore(win32vars.DEBUG_sysmem_lock, 1, 0);
ptr = bubble + 1;
#else
ptr = VirtualAlloc(0, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
#endif
return ptr;
}
#define system_get_memory(size) system_get_memory_(size, __LINE__, __FILE__)
internal void
system_fatal_error(u8 *message){
MessageBox(0, (char*)message, "4ed Error", MB_OK|MB_ICONERROR);
system_free_memory(void *block){
if (block){
#if FRED_INTERNAL
Sys_Bubble *bubble = ((Sys_Bubble*)block) - 1;
Assert((bubble->flags & MEM_BUBBLE_DEBUG_MASK) == MEM_BUBBLE_SYS_DEBUG);
WaitForSingleObject(win32vars.DEBUG_sysmem_lock, INFINITE);
remove_bubble(bubble);
ReleaseSemaphore(win32vars.DEBUG_sysmem_lock, 1, 0);
VirtualFree(bubble, 0, MEM_RELEASE);
#else
VirtualFree(block, 0, MEM_RELEASE);
#endif
}
}
internal File_Data
system_load_file(u8 *filename){
system_load_file(char *filename){
File_Data result = {};
HANDLE file;
file = CreateFile((char*)filename, GENERIC_READ, 0, 0,
@ -238,8 +271,8 @@ system_load_file(u8 *filename){
return result;
}
internal bool32
system_save_file(u8 *filename, void *data, i32 size){
internal b32
system_save_file(char *filename, void *data, i32 size){
HANDLE file;
file = CreateFile((char*)filename, GENERIC_WRITE, 0, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
@ -262,7 +295,7 @@ system_save_file(u8 *filename, void *data, i32 size){
}
internal Time_Stamp
system_file_time_stamp(u8 *filename){
system_file_time_stamp(char *filename){
Time_Stamp result;
result = {};
@ -279,7 +312,7 @@ system_file_time_stamp(u8 *filename){
}
internal u64
system_get_now(){
system_time_stamp_now(){
u64 result;
SYSTEMTIME sys_now;
FILETIME file_now;
@ -289,13 +322,23 @@ system_get_now(){
return result;
}
internal i64
system_time(){
i64 result = 0;
LARGE_INTEGER time;
if (QueryPerformanceCounter(&time)){
result = (i64)(time.QuadPart - win32vars.start_pcount) * 1000000 / win32vars.performance_frequency;
}
return result;
}
internal void
system_free_file(File_Data data){
system_free_memory(data.data);
}
internal i32
system_get_working_directory(u8 *destination, i32 max_size){
system_get_current_directory(char *destination, i32 max_size){
DWORD required = GetCurrentDirectory(0, 0);
if ((i32) required > max_size){
// TODO(allen): WHAT NOW? Not enough space in destination for
@ -307,7 +350,7 @@ system_get_working_directory(u8 *destination, i32 max_size){
}
internal i32
system_get_easy_directory(u8 *destination){
system_get_easy_directory(char *destination){
persist char easydir[] = "C:\\";
for (i32 i = 0; i < ArrayCount(easydir); ++i){
destination[i] = easydir[i];
@ -316,7 +359,7 @@ system_get_easy_directory(u8 *destination){
}
internal File_List
system_get_files(String directory){
system_get_file_list(String directory){
File_List result = {};
if (directory.size > 0){
@ -393,63 +436,6 @@ system_free_file_list(File_List list){
system_free_memory(list.block);
}
#if FRED_INTERNAL
Sys_Bubble INTERNAL_sentinel;
internal Bubble*
INTERNAL_system_sentinel(){
return &INTERNAL_sentinel;
}
#endif
internal void*
system_get_memory_(i32 size, i32 line_number, char *file_name){
void *ptr = 0;
#if FRED_INTERNAL
ptr = VirtualAlloc(0, size + sizeof(Sys_Bubble), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
Sys_Bubble *bubble = (Sys_Bubble*)ptr;
bubble->flags = MEM_BUBBLE_SYS_DEBUG;
bubble->line_number = line_number;
bubble->file_name = file_name;
bubble->size = size;
WaitForSingleObject(win32vars.DEBUG_sysmem_lock, INFINITE);
insert_bubble(&INTERNAL_sentinel, bubble);
ReleaseSemaphore(win32vars.DEBUG_sysmem_lock, 1, 0);
ptr = bubble + 1;
#else
ptr = VirtualAlloc(0, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
#endif
return ptr;
}
internal void
system_free_memory(void *block){
if (block){
#if FRED_INTERNAL
Sys_Bubble *bubble = ((Sys_Bubble*)block) - 1;
Assert((bubble->flags & MEM_BUBBLE_DEBUG_MASK) == MEM_BUBBLE_SYS_DEBUG);
WaitForSingleObject(win32vars.DEBUG_sysmem_lock, INFINITE);
remove_bubble(bubble);
ReleaseSemaphore(win32vars.DEBUG_sysmem_lock, 1, 0);
VirtualFree(bubble, 0, MEM_RELEASE);
#else
VirtualFree(block, 0, MEM_RELEASE);
#endif
}
}
internal i64
system_time(){
i64 result = 0;
LARGE_INTEGER time;
if (QueryPerformanceCounter(&time)){
result = (i64)(time.QuadPart - win32vars.start_pcount) * 1000000 / win32vars.performance_frequency;
}
return result;
}
// TODO(allen): Probably best to just drop all system functions here again.
internal void
system_post_clipboard(String str){
@ -484,7 +470,9 @@ Win32RedrawScreen(HDC hdc){
win32vars.bmp_info.bmiHeader.biHeight =
-win32vars.bmp_info.bmiHeader.biHeight;
}
#else
internal void
Win32RedrawScreen(HDC hdc){
glFlush();
@ -534,7 +522,6 @@ Win32KeyboardHandle(bool8 current_state, bool8 previous_state, WPARAM wParam){
internal LRESULT
Win32Callback(HWND hwnd, UINT uMsg,
WPARAM wParam, LPARAM lParam){
LRESULT result = {};
switch (uMsg){
case WM_MENUCHAR:
@ -625,7 +612,6 @@ Win32Callback(HWND hwnd, UINT uMsg,
win32vars.true_pixel_size = new_height*new_pitch;
if (!win32vars.pixel_data){
FatalError("Failure allocating new screen memory");
win32vars.keep_playing = 0;
}
}
@ -652,16 +638,11 @@ Win32Callback(HWND hwnd, UINT uMsg,
HDC hdc = BeginPaint(hwnd, &ps);
Clipboard_Contents empty_contents = {};
#if FRED_INTERNAL
INTERNAL_collecting_events = 0;
#endif
app_step(&win32vars.main_thread,
&win32vars.key_codes,
win32vars.app.step(win32vars.system,
&win32vars.main_thread, &win32vars.key_codes,
&win32vars.previous_data, &win32vars.mouse,
0, &win32vars.target, &win32memory, empty_contents, 0, 1);
#if FRED_INTERNAL
INTERNAL_collecting_events = 1;
#endif
Win32RedrawScreen(hdc);
EndPaint(hwnd, &ps);
@ -726,7 +707,7 @@ ThreadProc(LPVOID lpParameter){
thread_memory->size = new_size;
}
}
full_job->job.callback(thread, thread_memory, full_job->job.data);
full_job->job.callback(win32vars.system, thread, thread_memory, full_job->job.data);
full_job->running_thread = 0;
thread->running = 0;
}
@ -796,6 +777,16 @@ system_post_job(Thread_Group_ID group_id, Job_Data job){
return result;
}
internal void
system_acquire_lock(i32 id){
WaitForSingleObject(win32vars.locks[id], INFINITE);
}
internal void
system_release_lock(i32 id){
ReleaseSemaphore(win32vars.locks[id], 1, 0);
}
internal void
system_cancel_job(Thread_Group_ID group_id, u32 job_id){
Work_Queue *queue = win32vars.queues + group_id;
@ -815,7 +806,7 @@ system_cancel_job(Thread_Group_ID group_id, u32 job_id){
0, THREAD_NOT_ASSIGNED);
if (thread_id != THREAD_NOT_ASSIGNED){
system_aquire_lock(CANCEL_LOCK0 + thread_id - 1);
system_acquire_lock(CANCEL_LOCK0 + thread_id - 1);
thread = group->threads + thread_id - 1;
TerminateThread(thread->handle, 0);
u32 creation_flag = 0;
@ -831,29 +822,9 @@ system_job_is_pending(Thread_Group_ID group_id, u32 job_id){
return Win32JobIsPending(queue, job_id);
}
internal void
system_aquire_lock(Lock_ID id){
WaitForSingleObject(win32vars.locks[id], INFINITE);
}
internal void
system_release_lock(Lock_ID id){
ReleaseSemaphore(win32vars.locks[id], 1, 0);
}
internal void
system_aquire_lock(i32 id){
WaitForSingleObject(win32vars.locks[id], INFINITE);
}
internal void
system_release_lock(i32 id){
ReleaseSemaphore(win32vars.locks[id], 1, 0);
}
internal void
system_grow_thread_memory(Thread_Memory *memory){
system_aquire_lock(CANCEL_LOCK0 + memory->id - 1);
system_acquire_lock(CANCEL_LOCK0 + memory->id - 1);
void *old_data = memory->data;
i32 old_size = memory->size;
i32 new_size = LargeRoundUp(memory->size*2, Kbytes(4));
@ -1012,13 +983,80 @@ system_cli_end_update(CLI_Handles *cli){
return close_me;
}
internal b32
Win32LoadAppCode(){
b32 result = 0;
win32vars.app_code = LoadLibraryA("4ed_app.dll");
if (win32vars.app_code){
result = 1;
win32vars.app.init = (App_Init*)
GetProcAddress(win32vars.app_code, "app_init");
win32vars.app.step = (App_Step*)
GetProcAddress(win32vars.app_code, "app_step");
}
return result;
}
internal void
Win32LoadSystemCode(){
win32vars.system->load_file = system_load_file;
win32vars.system->save_file = system_save_file;
win32vars.system->file_time_stamp = system_file_time_stamp;
win32vars.system->time_stamp_now = system_time_stamp_now;
win32vars.system->free_file = system_free_file;
win32vars.system->get_current_directory = system_get_current_directory;
win32vars.system->get_easy_directory = system_get_easy_directory;
win32vars.system->get_file_list = system_get_file_list;
win32vars.system->free_file_list = system_free_file_list;
win32vars.system->get_memory_full = system_get_memory_;
win32vars.system->free_memory = system_free_memory;
win32vars.system->post_clipboard = system_post_clipboard;
win32vars.system->time = system_time;
win32vars.system->cli_call = system_cli_call;
win32vars.system->cli_begin_update = system_cli_begin_update;
win32vars.system->cli_update_step = system_cli_update_step;
win32vars.system->cli_end_update = system_cli_end_update;
win32vars.system->thread_get_id = system_thread_get_id;
win32vars.system->thread_current_job_id = system_thread_current_job_id;
win32vars.system->post_job = system_post_job;
win32vars.system->cancel_job = system_cancel_job;
win32vars.system->job_is_pending = system_job_is_pending;
win32vars.system->grow_thread_memory = system_grow_thread_memory;
win32vars.system->acquire_lock = system_acquire_lock;
win32vars.system->release_lock = system_release_lock;
win32vars.system->force_redraw = system_force_redraw;
win32vars.system->internal_sentinel = INTERNAL_system_sentinel;
win32vars.system->internal_get_thread_states = INTERNAL_get_thread_states;
}
int
WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow){
win32vars = {};
TEMP = {};
config_api = {};
if (!Win32LoadAppCode()){
// TODO(allen): Failed to load app code, serious problem.
return 99;
}
System_Functions system_;
System_Functions *system = &system_;
win32vars.system = system;
Win32LoadSystemCode();
LARGE_INTEGER lpf;
QueryPerformanceFrequency(&lpf);
win32vars.performance_frequency = lpf.QuadPart;
@ -1026,14 +1064,9 @@ WinMain(HINSTANCE hInstance,
win32vars.start_pcount = lpf.QuadPart;
#if FRED_INTERNAL
memset(INTERNAL_event_hits, 0, INTERNAL_event_index_count * sizeof(u32));
INTERNAL_frame_index = 0;
INTERNAL_updating_profile = 1;
INTERNAL_collecting_events = 1;
INTERNAL_sentinel.next = &INTERNAL_sentinel;
INTERNAL_sentinel.prev = &INTERNAL_sentinel;
INTERNAL_sentinel.flags = MEM_BUBBLE_SYS_DEBUG;
win32vars.internal_bubble.next = &win32vars.internal_bubble;
win32vars.internal_bubble.prev = &win32vars.internal_bubble;
win32vars.internal_bubble.flags = MEM_BUBBLE_SYS_DEBUG;
#endif
keycode_init(&win32vars.key_codes, &win32vars.loose_codes);
@ -1041,10 +1074,10 @@ WinMain(HINSTANCE hInstance,
#ifdef FRED_SUPER
win32vars.custom = LoadLibraryA("4coder_custom.dll");
if (win32vars.custom){
TEMP.get_bindings = (Get_Binding_Data_Function*)
config_api.get_bindings = (Get_Binding_Data_Function*)
GetProcAddress(win32vars.custom, "get_bindings");
TEMP.set_extra_font = (Set_Extra_Font_Function*)
config_api.set_extra_font = (Set_Extra_Font_Function*)
GetProcAddress(win32vars.custom, "set_extra_font");
}
#endif
@ -1092,8 +1125,6 @@ WinMain(HINSTANCE hInstance,
window_class.lpszClassName = "4coder-win32-wndclass";
if (!RegisterClass(&window_class)){
// TODO(allen): diagnostics
FatalError("Failed to create window class");
return 1;
}
@ -1122,8 +1153,6 @@ WinMain(HINSTANCE hInstance,
0, 0, hInstance, 0);
if (!window_handle){
// TODO(allen): diagnostics
FatalError("Failed to create window");
return 2;
}
@ -1152,9 +1181,9 @@ WinMain(HINSTANCE hInstance,
win32vars.pixel_data = system_get_memory(win32vars.true_pixel_size);
if (!win32vars.pixel_data){
FatalError("Failure allocating screen memory");
return 3;
}
#else
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
@ -1215,7 +1244,6 @@ WinMain(HINSTANCE hInstance,
PAGE_READWRITE);
if (!win32memory.vars_memory){
FatalError("Failure allocating application memory");
return 4;
}
@ -1228,7 +1256,7 @@ WinMain(HINSTANCE hInstance,
win32vars.next_clipboard_is_self = 0;
if (win32vars.clipboard_sequence == 0){
FatalError("Failure to access platform's clipboard");
// TODO(allen): diagnostics
}
}
else{
@ -1248,63 +1276,36 @@ WinMain(HINSTANCE hInstance,
}
}
if (!app_init(&win32vars.main_thread,
if (!win32vars.app.init(win32vars.system, &win32vars.main_thread,
&win32memory, &win32vars.key_codes,
win32vars.clipboard_contents)){
win32vars.clipboard_contents, config_api)){
return 5;
}
win32vars.keep_playing = 1;
timeBeginPeriod(1);
system_aquire_lock(FRAME_LOCK);
system_acquire_lock(FRAME_LOCK);
Thread_Context *thread = &win32vars.main_thread;
AllowLocal(thread);
bool32 first = 1;
i64 timer_start = system_time();
while (win32vars.keep_playing){
#if FRED_INTERNAL
i64 dbg_procing_start = system_time();
if (!first){
if (INTERNAL_updating_profile){
i32 j = (INTERNAL_frame_index % 30);
Profile_Frame *frame = past_frames + j;
sort(&profile_frame.events);
frame->events.count = profile_frame.events.count;
memcpy(frame->events.e, profile_frame.events.e, sizeof(Debug_Event)*profile_frame.events.count);
past_frames[j].dbg_procing_start = profile_frame.dbg_procing_start;
past_frames[j].dbg_procing_end = profile_frame.dbg_procing_end;
past_frames[j].index = profile_frame.index;
past_frames[j].first_key = profile_frame.first_key;
++INTERNAL_frame_index;
if (INTERNAL_frame_index < 0){
INTERNAL_frame_index = ((INTERNAL_frame_index - 1) % 30) + 1;
}
memset(INTERNAL_event_hits, 0, INTERNAL_event_index_count * sizeof(u32));
}
}
profile_frame.events.count = 0;
profile_frame.first_key = -1;
profile_frame.index = INTERNAL_frame_index;
INTERNAL_frame_start_time = timer_start;
profile_frame.dbg_procing_start = (i32)(dbg_procing_start - INTERNAL_frame_start_time);
profile_frame.dbg_procing_end = (i32)(system_time() - INTERNAL_frame_start_time);
#endif
ProfileStart(OS_input);
win32vars.previous_data = win32vars.input_data;
win32vars.input_data.press_count = 0;
win32vars.input_data.hold_count = 0;
win32vars.input_data.caps_lock = GetKeyState(VK_CAPITAL) & 0x1;
win32vars.input_data.control_keys[CONTROL_KEY_SHIFT] = (GetKeyState(VK_SHIFT) & 0x0100) >> 8;
win32vars.input_data.control_keys[CONTROL_KEY_CONTROL] = (GetKeyState(VK_CONTROL) & 0x0100) >> 8;
win32vars.input_data.control_keys[CONTROL_KEY_ALT] = (GetKeyState(VK_MENU) & 0x0100) >> 8;
win32vars.input_data.control_keys[CONTROL_KEY_SHIFT] =
(GetKeyState(VK_SHIFT) & 0x0100) >> 8;
win32vars.input_data.control_keys[CONTROL_KEY_CONTROL] =
(GetKeyState(VK_CONTROL) & 0x0100) >> 8;
win32vars.input_data.control_keys[CONTROL_KEY_ALT] =
(GetKeyState(VK_MENU) & 0x0100) >> 8;
win32vars.mouse.left_button_prev = win32vars.mouse.left_button;
win32vars.mouse.right_button_prev = win32vars.mouse.right_button;
@ -1391,8 +1392,8 @@ WinMain(HINSTANCE hInstance,
ProfileEnd(OS_input);
Application_Step_Result result =
app_step(&win32vars.main_thread,
&win32vars.key_codes,
win32vars.app.step(win32vars.system,
&win32vars.main_thread, &win32vars.key_codes,
&win32vars.input_data, &win32vars.mouse,
1, &win32vars.target,
&win32memory,
@ -1428,7 +1429,7 @@ WinMain(HINSTANCE hInstance,
if (samount > 0) Sleep(samount);
timer_end = system_time();
}
system_aquire_lock(FRAME_LOCK);
system_acquire_lock(FRAME_LOCK);
timer_start = system_time();
ProfileEnd(frame_sleep);
}
@ -1436,10 +1437,6 @@ WinMain(HINSTANCE hInstance,
return 0;
}
#if FRED_INTERNAL
const i32 INTERNAL_event_index_count = __COUNTER__;
u32 INTERNAL_event_hits[__COUNTER__];
#endif
// BOTTOM