2015-09-28 23:34:55 +00:00
|
|
|
/*
|
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
*
|
|
|
|
* 19.08.2015
|
|
|
|
*
|
|
|
|
* Command management functions for 4coder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2015-11-14 04:42:06 +00:00
|
|
|
typedef void (*Command_Function)(System_Functions *system,
|
|
|
|
struct Command_Data *command, struct Command_Binding binding);
|
2015-09-28 23:34:55 +00:00
|
|
|
|
|
|
|
struct Command_Binding{
|
|
|
|
Command_Function function;
|
|
|
|
Custom_Command_Function *custom;
|
|
|
|
i64 hash;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Command_Map{
|
2015-10-16 20:31:04 +00:00
|
|
|
Command_Map *parent;
|
2015-09-28 23:34:55 +00:00
|
|
|
Command_Binding vanilla_keyboard_default;
|
2015-10-16 20:31:04 +00:00
|
|
|
Command_Binding *commands;
|
2015-09-28 23:34:55 +00:00
|
|
|
i32 count, max;
|
|
|
|
};
|
|
|
|
|
|
|
|
internal void command_null(Command_Data *command);
|
|
|
|
|
|
|
|
internal i64
|
|
|
|
map_hash(u16 event_code, u8 modifiers){
|
|
|
|
i64 result = (event_code << 4) | modifiers;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:15:45 +00:00
|
|
|
internal b32
|
2015-09-28 23:34:55 +00:00
|
|
|
map_add(Command_Map *map, u16 event_code, u8 modifiers, Command_Function function, Custom_Command_Function *custom = 0){
|
|
|
|
Assert(map->count * 8 < map->max * 7);
|
|
|
|
Command_Binding bind;
|
|
|
|
bind.function = function;
|
|
|
|
bind.custom = custom;
|
|
|
|
bind.hash = map_hash(event_code, modifiers);
|
|
|
|
|
|
|
|
i32 max = map->max;
|
|
|
|
i32 index = bind.hash % max;
|
|
|
|
Command_Binding entry;
|
|
|
|
while ((entry = map->commands[index]).function && entry.hash != -1){
|
|
|
|
if (entry.hash == bind.hash){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
index = (index + 1) % max;
|
|
|
|
}
|
|
|
|
map->commands[index] = bind;
|
|
|
|
++map->count;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:15:45 +00:00
|
|
|
internal b32
|
2015-09-28 23:34:55 +00:00
|
|
|
map_find_entry(Command_Map *map, u16 event_code, u8 modifiers, i32 *index_out){
|
|
|
|
i64 hash = map_hash(event_code, modifiers);
|
|
|
|
i32 max = map->max;
|
|
|
|
i32 index = hash % map->max;
|
|
|
|
Command_Binding entry;
|
|
|
|
while ((entry = map->commands[index]).function){
|
|
|
|
if (entry.hash == hash){
|
|
|
|
*index_out = index;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
index = (index + 1) % max;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:15:45 +00:00
|
|
|
internal b32
|
2015-09-28 23:34:55 +00:00
|
|
|
map_find(Command_Map *map, u16 event_code, u8 modifiers, Command_Binding *bind_out){
|
2015-11-16 16:15:45 +00:00
|
|
|
b32 result;
|
2015-09-28 23:34:55 +00:00
|
|
|
i32 index;
|
|
|
|
result = map_find_entry(map, event_code, modifiers, &index);
|
|
|
|
if (result){
|
|
|
|
*bind_out = map->commands[index];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:15:45 +00:00
|
|
|
internal b32
|
2015-09-28 23:34:55 +00:00
|
|
|
map_drop(Command_Map *map, u16 event_code, u8 modifiers){
|
2015-11-16 16:15:45 +00:00
|
|
|
b32 result;
|
2015-09-28 23:34:55 +00:00
|
|
|
i32 index;
|
|
|
|
result = map_find_entry(map, event_code, modifiers, &index);
|
|
|
|
if (result){
|
|
|
|
map->commands[index].function = 0;
|
|
|
|
map->commands[index].hash = -1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
2015-10-16 20:31:04 +00:00
|
|
|
map_init(Command_Map *commands, Partition *part, i32 max, Command_Map *parent){
|
|
|
|
commands->parent = parent;
|
|
|
|
commands->commands = push_array(part, Command_Binding, max);
|
|
|
|
memset(commands->commands, 0, max*sizeof(*commands->commands));
|
2015-09-28 23:34:55 +00:00
|
|
|
commands->vanilla_keyboard_default = {};
|
2015-10-16 20:31:04 +00:00
|
|
|
commands->max = max;
|
2015-09-28 23:34:55 +00:00
|
|
|
commands->count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
|
|
|
map_get_vanilla_keyboard_default(Command_Map *map, u8 command, Command_Binding *bind_out){
|
|
|
|
if (command == MDFR_NONE){
|
|
|
|
*bind_out = map->vanilla_keyboard_default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Command_Binding
|
|
|
|
map_extract(Command_Map *map, Key_Single key){
|
|
|
|
Command_Binding bind = {};
|
|
|
|
|
|
|
|
u8 command = MDFR_NONE;
|
2015-10-28 01:45:03 +00:00
|
|
|
b32 ctrl = key.modifiers[CONTROL_KEY_CONTROL];
|
|
|
|
b32 alt = key.modifiers[CONTROL_KEY_ALT];
|
|
|
|
b32 shift = key.modifiers[CONTROL_KEY_SHIFT] && key.key.loose_keycode;
|
2015-09-28 23:34:55 +00:00
|
|
|
|
|
|
|
if (shift) command |= MDFR_SHIFT;
|
|
|
|
if (ctrl) command |= MDFR_CTRL;
|
|
|
|
if (alt) command |= MDFR_ALT;
|
|
|
|
|
|
|
|
u16 code = key.key.character_no_caps_lock;
|
2015-10-12 16:15:11 +00:00
|
|
|
if (code == 0) code = key.key.keycode;
|
|
|
|
map_find(map, code, command, &bind);
|
2015-09-28 23:34:55 +00:00
|
|
|
|
2015-10-12 16:15:11 +00:00
|
|
|
if (bind.function == 0 && key.key.character_no_caps_lock != 0){
|
|
|
|
map_get_vanilla_keyboard_default(map, command, &bind);
|
2015-09-28 23:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bind;
|
|
|
|
}
|
|
|
|
|
|
|
|
// BOTTOM
|
|
|
|
|