2016-02-20 21:36:16 +00:00
|
|
|
/*
|
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
*
|
|
|
|
* 19.08.2015
|
|
|
|
*
|
|
|
|
* Command management functions for 4coder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2017-02-12 06:01:01 +00:00
|
|
|
#define Command_Function_Sig(name) \
|
|
|
|
void (name)(System_Functions *system, struct Command_Data *command, struct Command_Binding binding)
|
2016-02-20 21:36:16 +00:00
|
|
|
|
2017-11-08 18:24:30 +00:00
|
|
|
typedef Command_Function_Sig(Command_Function);
|
2016-02-20 21:36:16 +00:00
|
|
|
|
|
|
|
struct Command_Binding{
|
2017-11-08 18:24:30 +00:00
|
|
|
Command_Function *function;
|
2016-02-27 20:23:52 +00:00
|
|
|
union{
|
|
|
|
Custom_Command_Function *custom;
|
|
|
|
u64 custom_id;
|
|
|
|
};
|
2017-02-17 22:03:19 +00:00
|
|
|
u64 hash;
|
2016-02-20 21:36:16 +00:00
|
|
|
};
|
2017-02-12 06:01:01 +00:00
|
|
|
static Command_Binding null_command_binding = {0};
|
2016-02-20 21:36:16 +00:00
|
|
|
|
|
|
|
struct Command_Map{
|
2017-11-08 18:24:30 +00:00
|
|
|
i32 parent;
|
2017-02-18 01:04:41 +00:00
|
|
|
Command_Binding vanilla_keyboard_default[8];
|
2016-02-20 21:36:16 +00:00
|
|
|
Command_Binding *commands;
|
2017-11-08 21:28:44 +00:00
|
|
|
i32 count, max;
|
|
|
|
void *real_beginning;
|
2016-02-20 21:36:16 +00:00
|
|
|
};
|
|
|
|
|
2017-11-08 18:24:30 +00:00
|
|
|
struct Mapping{
|
2017-11-08 21:28:44 +00:00
|
|
|
void *memory;
|
|
|
|
|
2017-11-08 18:24:30 +00:00
|
|
|
Command_Map map_top;
|
|
|
|
Command_Map map_file;
|
|
|
|
Command_Map map_ui;
|
|
|
|
|
|
|
|
i32 *map_id_table;
|
|
|
|
Command_Map *user_maps;
|
|
|
|
i32 user_map_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
internal i32
|
|
|
|
get_or_add_map_index(Mapping *mapping, i32 mapid){
|
|
|
|
i32 user_map_count = mapping->user_map_count;
|
|
|
|
i32 *map_id_table = mapping->map_id_table;
|
2017-11-08 21:28:44 +00:00
|
|
|
i32 result = 0;
|
|
|
|
for (; result < user_map_count; ++result){
|
2017-11-08 18:24:30 +00:00
|
|
|
if (map_id_table[result] == mapid){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (map_id_table[result] == -1){
|
|
|
|
map_id_table[result] = mapid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2017-11-08 21:28:44 +00:00
|
|
|
// HACK(allen): This seems busted, investigate.
|
2017-11-08 18:24:30 +00:00
|
|
|
internal i32
|
|
|
|
get_map_index(Mapping *mapping, i32 mapid){
|
|
|
|
i32 user_map_count = mapping->user_map_count;
|
|
|
|
i32 *map_id_table = mapping->map_id_table;
|
2017-11-08 21:28:44 +00:00
|
|
|
i32 result = 0;
|
|
|
|
for (; result < user_map_count; ++result){
|
2017-11-08 18:24:30 +00:00
|
|
|
if (map_id_table[result] == mapid){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (map_id_table[result] == 0){
|
|
|
|
result = user_map_count;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Command_Map*
|
|
|
|
get_map_base(Mapping *mapping, i32 mapid, b32 add){
|
|
|
|
Command_Map *map = 0;
|
|
|
|
if (mapid < mapid_global){
|
|
|
|
i32 map_index = 0;
|
|
|
|
if (add){
|
|
|
|
map_index = get_or_add_map_index(mapping, mapid);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
map_index = get_map_index(mapping, mapid);
|
|
|
|
}
|
|
|
|
if (map_index < mapping->user_map_count){
|
|
|
|
map = &mapping->user_maps[map_index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mapid == mapid_global){
|
|
|
|
map = &mapping->map_top;
|
|
|
|
}
|
|
|
|
else if (mapid == mapid_file){
|
|
|
|
map = &mapping->map_file;
|
|
|
|
}
|
|
|
|
else if (mapid == mapid_ui){
|
|
|
|
map = &mapping->map_ui;
|
|
|
|
}
|
|
|
|
return(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Command_Map*
|
|
|
|
get_or_add_map(Mapping *mapping, i32 mapid){
|
|
|
|
Command_Map *map = get_map_base(mapping, mapid, true);
|
|
|
|
return(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Command_Map*
|
|
|
|
get_map(Mapping *mapping, i32 mapid){
|
|
|
|
Command_Map *map = get_map_base(mapping, mapid, false);
|
|
|
|
return(map);
|
|
|
|
}
|
|
|
|
|
2017-02-17 22:03:19 +00:00
|
|
|
#define COMMAND_HASH_EMPTY 0
|
|
|
|
#define COMMAND_HASH_ERASED max_u64
|
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
internal void command_null(Command_Data *command);
|
|
|
|
|
2017-02-17 22:03:19 +00:00
|
|
|
internal u64
|
|
|
|
map_hash(Key_Code event_code, u8 modifiers){
|
|
|
|
u64 result = (event_code << 8) | modifiers;
|
|
|
|
return(result);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal b32
|
2017-11-08 18:24:30 +00:00
|
|
|
map_add(Command_Map *map, Key_Code event_code, u8 modifiers, Command_Function *function, Custom_Command_Function *custom = 0, b32 override_original = true){
|
2017-02-12 23:04:50 +00:00
|
|
|
b32 result = false;
|
2016-02-20 21:36:16 +00:00
|
|
|
Assert(map->count * 8 < map->max * 7);
|
2017-02-17 22:03:19 +00:00
|
|
|
u64 hash = map_hash(event_code, modifiers);
|
2016-02-20 21:36:16 +00:00
|
|
|
|
2017-02-17 22:03:19 +00:00
|
|
|
u32 max = map->max;
|
|
|
|
u32 index = hash % max;
|
|
|
|
Command_Binding entry = map->commands[index];
|
2017-11-09 00:12:14 +00:00
|
|
|
for (; entry.function != 0 && entry.hash != COMMAND_HASH_ERASED;){
|
2017-02-17 22:03:19 +00:00
|
|
|
if (entry.hash == hash){
|
|
|
|
result = true;
|
2017-02-12 23:04:50 +00:00
|
|
|
break;
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
index = (index + 1) % max;
|
2017-02-17 22:03:19 +00:00
|
|
|
entry = map->commands[index];
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
2017-02-17 22:03:19 +00:00
|
|
|
|
2017-02-12 23:04:50 +00:00
|
|
|
if (override_original || !result){
|
2017-11-09 00:12:14 +00:00
|
|
|
Command_Binding bind = {0};
|
2017-02-17 22:03:19 +00:00
|
|
|
bind.function = function;
|
|
|
|
bind.custom = custom;
|
|
|
|
bind.hash = hash;
|
2017-02-12 23:04:50 +00:00
|
|
|
map->commands[index] = bind;
|
2017-11-09 00:12:14 +00:00
|
|
|
if (!result){
|
|
|
|
++map->count;
|
|
|
|
}
|
2017-02-12 23:04:50 +00:00
|
|
|
}
|
2017-02-17 22:03:19 +00:00
|
|
|
|
2017-02-12 23:04:50 +00:00
|
|
|
return(result);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
2016-02-27 20:23:52 +00:00
|
|
|
inline b32
|
2017-11-08 18:24:30 +00:00
|
|
|
map_add(Command_Map *map, Key_Code event_code, u8 modifiers, Command_Function *function, u64 custom_id, b32 override_original = true){
|
2017-02-17 22:03:19 +00:00
|
|
|
return (map_add(map, event_code, modifiers, function, (Custom_Command_Function*)custom_id, override_original));
|
2016-02-27 20:23:52 +00:00
|
|
|
}
|
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
internal b32
|
2017-02-17 22:03:19 +00:00
|
|
|
map_find_entry(Command_Map *map, Key_Code event_code, u8 modifiers, u32 *index_out){
|
|
|
|
u64 hash = map_hash(event_code, modifiers);
|
|
|
|
u32 max = map->max;
|
|
|
|
u32 index = hash % max;
|
|
|
|
b32 result = false;
|
|
|
|
Command_Binding entry = map->commands[index];
|
2017-11-09 00:12:14 +00:00
|
|
|
for (; entry.function != 0;){
|
2016-02-20 21:36:16 +00:00
|
|
|
if (entry.hash == hash){
|
|
|
|
*index_out = index;
|
2017-02-17 22:03:19 +00:00
|
|
|
result = true;
|
|
|
|
break;
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
index = (index + 1) % max;
|
2017-02-17 22:03:19 +00:00
|
|
|
entry = map->commands[index];
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
2017-02-17 22:03:19 +00:00
|
|
|
return(result);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal b32
|
2017-02-17 22:03:19 +00:00
|
|
|
map_find(Command_Map *map, Key_Code event_code, u8 modifiers, Command_Binding *bind_out){
|
|
|
|
u32 index = 0;
|
|
|
|
b32 result = map_find_entry(map, event_code, modifiers, &index);
|
2016-02-20 21:36:16 +00:00
|
|
|
if (result){
|
|
|
|
*bind_out = map->commands[index];
|
|
|
|
}
|
2017-02-17 22:03:19 +00:00
|
|
|
return(result);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal b32
|
2017-02-17 22:03:19 +00:00
|
|
|
map_drop(Command_Map *map, Key_Code event_code, u8 modifiers){
|
|
|
|
u32 index = 0;
|
|
|
|
b32 result = map_find_entry(map, event_code, modifiers, &index);
|
2016-02-20 21:36:16 +00:00
|
|
|
if (result){
|
|
|
|
map->commands[index].function = 0;
|
2017-02-17 22:03:19 +00:00
|
|
|
map->commands[index].hash = COMMAND_HASH_ERASED;
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
2017-02-17 22:03:19 +00:00
|
|
|
return(result);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
2017-11-08 21:28:44 +00:00
|
|
|
map_init(Command_Map *map, Partition *part, i32 max, i32 parent){
|
|
|
|
Assert(max >= 6);
|
|
|
|
Assert(map->commands == 0);
|
|
|
|
map->parent = parent;
|
|
|
|
map->commands = push_array(part, Command_Binding, max);
|
|
|
|
map->count = 0;
|
|
|
|
map->max = max;
|
|
|
|
|
|
|
|
memset(map->commands, 0, max*sizeof(*map->commands));
|
|
|
|
memset(map->vanilla_keyboard_default, 0, sizeof(map->vanilla_keyboard_default));
|
2017-02-18 01:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal b32
|
|
|
|
map_get_modifiers_hash(u8 modifiers, u32 *hash_out){
|
|
|
|
b32 result = true;
|
|
|
|
u32 hash = 0;
|
|
|
|
if (modifiers & MDFR_SHIFT){
|
2017-11-09 00:12:14 +00:00
|
|
|
hash += MDFR_SHIFT;
|
2017-02-18 01:04:41 +00:00
|
|
|
}
|
|
|
|
if (modifiers & MDFR_CTRL){
|
2017-11-09 00:12:14 +00:00
|
|
|
hash += MDFR_CTRL;
|
|
|
|
}
|
|
|
|
if (modifiers & MDFR_CMND){
|
|
|
|
hash += MDFR_CMND;
|
2017-02-18 01:04:41 +00:00
|
|
|
}
|
|
|
|
if (modifiers & MDFR_ALT){
|
2017-11-09 00:12:14 +00:00
|
|
|
hash += MDFR_ALT;
|
2017-02-18 01:04:41 +00:00
|
|
|
}
|
|
|
|
*hash_out = hash;
|
|
|
|
return(result);
|
2016-05-19 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
internal void
|
2017-02-12 06:01:01 +00:00
|
|
|
map_get_vanilla_keyboard_default(Command_Map *map, u8 command, Command_Binding *bind_out){
|
2017-02-18 01:04:41 +00:00
|
|
|
u32 modifiers_hashed = 0;
|
|
|
|
if (map_get_modifiers_hash(command, &modifiers_hashed)){
|
|
|
|
*bind_out = map->vanilla_keyboard_default[modifiers_hashed];
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Command_Binding
|
|
|
|
map_extract(Command_Map *map, Key_Event_Data key){
|
2017-02-12 06:01:01 +00:00
|
|
|
Command_Binding bind = {0};
|
2016-02-20 21:36:16 +00:00
|
|
|
|
2017-11-07 21:35:26 +00:00
|
|
|
b32 ctrl = key.modifiers[MDFR_CONTROL_INDEX];
|
|
|
|
b32 alt = key.modifiers[MDFR_ALT_INDEX];
|
|
|
|
b32 command = key.modifiers[MDFR_COMMAND_INDEX];
|
|
|
|
b32 shift = key.modifiers[MDFR_SHIFT_INDEX];
|
2016-02-20 21:36:16 +00:00
|
|
|
|
2017-11-07 21:35:26 +00:00
|
|
|
u8 mod_flags = MDFR_NONE;
|
|
|
|
if (ctrl) mod_flags |= MDFR_CTRL;
|
|
|
|
if (alt) mod_flags |= MDFR_ALT;
|
2017-11-09 00:12:14 +00:00
|
|
|
if (command) mod_flags |= MDFR_CMND;
|
2017-11-07 21:35:26 +00:00
|
|
|
if (shift) mod_flags |= MDFR_SHIFT;
|
2017-02-12 06:01:01 +00:00
|
|
|
|
2017-02-17 22:03:19 +00:00
|
|
|
Key_Code code = key.character_no_caps_lock;
|
2016-02-20 21:36:16 +00:00
|
|
|
if (code == 0){
|
|
|
|
code = key.keycode;
|
2017-11-07 21:35:26 +00:00
|
|
|
map_find(map, code, mod_flags, &bind);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
if (code != '\n' && code != '\t' && code != ' '){
|
2017-11-07 21:35:26 +00:00
|
|
|
mod_flags &= ~(MDFR_SHIFT);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
2017-11-07 21:35:26 +00:00
|
|
|
map_find(map, code, mod_flags, &bind);
|
2016-02-20 21:36:16 +00:00
|
|
|
if (bind.function == 0){
|
2017-11-07 21:35:26 +00:00
|
|
|
map_get_vanilla_keyboard_default(map, mod_flags, &bind);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 20:23:52 +00:00
|
|
|
return(bind);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Command_Binding
|
2017-11-08 18:24:30 +00:00
|
|
|
map_extract_recursive(Mapping *mapping, i32 map_id, Key_Event_Data key){
|
|
|
|
Command_Map *map = get_map(mapping, map_id);
|
|
|
|
if (map == 0){
|
|
|
|
map = &mapping->map_top;
|
|
|
|
}
|
|
|
|
|
2017-02-17 22:03:19 +00:00
|
|
|
Command_Map *visited_maps[16] = {0};
|
2016-02-27 20:23:52 +00:00
|
|
|
i32 visited_top = 0;
|
|
|
|
|
2017-11-08 21:28:44 +00:00
|
|
|
Command_Binding cmd_bind = {0};
|
|
|
|
for (; map != 0; ){
|
2016-02-27 20:23:52 +00:00
|
|
|
cmd_bind = map_extract(map, key);
|
|
|
|
if (cmd_bind.function == 0){
|
|
|
|
if (visited_top < ArrayCount(visited_maps)){
|
|
|
|
visited_maps[visited_top++] = map;
|
2017-11-08 18:24:30 +00:00
|
|
|
map = get_map(mapping, map->parent);
|
2016-02-27 20:23:52 +00:00
|
|
|
for (i32 i = 0; i < visited_top; ++i){
|
|
|
|
if (map == visited_maps[i]){
|
|
|
|
map = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-17 22:03:19 +00:00
|
|
|
else{
|
|
|
|
map = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
map = 0;
|
2016-02-27 20:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(cmd_bind);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BOTTOM
|
|
|
|
|