aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkoekeishiya <aasvi93@hotmail.com>2017-08-24 13:31:19 +0200
committerkoekeishiya <aasvi93@hotmail.com>2017-08-24 13:31:19 +0200
commitfbce46ad22ecdbca7ab32729b9e4e5d4dc1e8016 (patch)
treed38231ecaa997f471f3cf3e271274675f6bae1f6
parent92bbb316bebe27d6daa7cc15e7ad33096208163b (diff)
downloadskhd-fbce46ad22ecdbca7ab32729b9e4e5d4dc1e8016.tar.gz
skhd-fbce46ad22ecdbca7ab32729b9e4e5d4dc1e8016.zip
#2 change how we lookup literals
-rw-r--r--src/locale.c69
-rw-r--r--src/locale.h3
-rw-r--r--src/parse.c70
-rw-r--r--src/tokenize.c12
-rw-r--r--src/tokenize.h33
5 files changed, 76 insertions, 111 deletions
diff --git a/src/locale.c b/src/locale.c
index e2cc86b..01de49d 100644
--- a/src/locale.c
+++ b/src/locale.c
@@ -6,14 +6,6 @@
#define internal static
#define local_persist static
-bool same_string(char *text, unsigned length, const char *match)
-{
- const char *at = match;
- unsigned index = 0;
- while(*at++ == text[index++] && index < length);
- return (*at == '\0' && index == length) ? true : false;
-}
-
internal CFStringRef
cfstring_from_keycode(CGKeyCode keycode)
{
@@ -76,64 +68,3 @@ uint32_t keycode_from_char(char key)
return keycode;
}
-
-uint32_t keycode_from_literal(char *key, unsigned length)
-{
- if(same_string(key, length, "return")) {
- return kVK_Return;
- } else if(same_string(key, length, "tab")) {
- return kVK_Tab;
- } else if(same_string(key, length, "space")) {
- return kVK_Space;
- } else if(same_string(key, length, "backspace")) {
- return kVK_Delete;
- } else if(same_string(key, length, "delete")) {
- return kVK_ForwardDelete;
- } else if(same_string(key, length, "escape")) {
- return kVK_Escape;
- } else if(same_string(key, length, "home")) {
- return kVK_Home;
- } else if(same_string(key, length, "end")) {
- return kVK_End;
- } else if(same_string(key, length, "pageup")) {
- return kVK_PageUp;
- } else if(same_string(key, length, "pagedown")) {
- return kVK_PageDown;
- } else if(same_string(key, length, "help")) {
- return kVK_Help;
- } else if(same_string(key, length, "left")) {
- return kVK_LeftArrow;
- } else if(same_string(key, length, "right")) {
- return kVK_RightArrow;
- } else if(same_string(key, length, "up")) {
- return kVK_UpArrow;
- } else if(same_string(key, length, "down")) {
- return kVK_DownArrow;
- } else if(same_string(key, length, "f1")) {
- return kVK_F1;
- } else if(same_string(key, length, "f2")) {
- return kVK_F2;
- } else if(same_string(key, length, "f3")) {
- return kVK_F3;
- } else if(same_string(key, length, "f4")) {
- return kVK_F4;
- } else if(same_string(key, length, "f5")) {
- return kVK_F5;
- } else if(same_string(key, length, "f6")) {
- return kVK_F6;
- } else if(same_string(key, length, "f7")) {
- return kVK_F7;
- } else if(same_string(key, length, "f8")) {
- return kVK_F8;
- } else if(same_string(key, length, "f9")) {
- return kVK_F9;
- } else if(same_string(key, length, "f10")) {
- return kVK_F10;
- } else if(same_string(key, length, "f11")) {
- return kVK_F11;
- } else if(same_string(key, length, "f12")) {
- return kVK_F12;
- } else {
- return 0;
- }
-}
diff --git a/src/locale.h b/src/locale.h
index 2662cee..7e30d1c 100644
--- a/src/locale.h
+++ b/src/locale.h
@@ -1,11 +1,8 @@
#ifndef SKHD_LOCALE_H
#define SKHD_LOCALE_H
-#include <stdbool.h>
#include <stdint.h>
uint32_t keycode_from_char(char key);
-uint32_t keycode_from_literal(char *key, unsigned length);
-bool same_string(char *text, unsigned length, const char *match);
#endif
diff --git a/src/parse.c b/src/parse.c
index 15faadd..11c3827 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -7,6 +7,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <stdbool.h>
#define internal static
@@ -31,6 +32,12 @@ read_file(const char *file)
return buffer;
}
+internal inline bool
+same_string(char *text, unsigned length, const char *match)
+{
+ return strncmp(text, match, length) == 0;
+}
+
internal char *
copy_string_count(char *s, int length)
{
@@ -73,22 +80,45 @@ parse_key(struct parser *parser)
{
uint32_t keycode;
struct token key = parser_previous(parser);
- if(key.length == 1) {
- keycode = keycode_from_char(*key.text);
- } else {
- keycode = keycode_from_literal(key.text, key.length);
- }
+ keycode = keycode_from_char(*key.text);
printf("\tkey: '%.*s' (0x%02x)\n", key.length, key.text, keycode);
return keycode;
}
-internal const char *modifier_flags_map[] =
+internal uint32_t literal_keycode_value[] =
{
- "alt", "lalt", "ralt",
- "shift", "lshift", "rshift",
- "cmd", "lcmd", "rcmd",
- "ctrl", "lctrl", "rctrl",
+ kVK_Return, kVK_Tab, kVK_Space,
+ kVK_Delete, kVK_ForwardDelete, kVK_Escape,
+ kVK_Home, kVK_End, kVK_PageUp,
+ kVK_PageDown, kVK_Help, kVK_LeftArrow,
+ kVK_RightArrow, kVK_UpArrow, kVK_DownArrow,
+ kVK_F1, kVK_F2, kVK_F3,
+ kVK_F4, kVK_F5, kVK_F6,
+ kVK_F7, kVK_F8, kVK_F9,
+ kVK_F10, kVK_F11, kVK_F12,
+ kVK_F13, kVK_F14, kVK_F15,
+ kVK_F16, kVK_F17, kVK_F18,
+ kVK_F19, kVK_F20,
};
+
+internal uint32_t
+parse_key_literal(struct parser *parser)
+{
+ uint32_t keycode = 0;
+ struct token key = parser_previous(parser);
+
+ // NOTE(koekeishiya): Might want to replace this mapping with a hashtable
+ for(int i = 0; i < array_count(literal_keycode_str); ++i) {
+ if(same_string(key.text, key.length, literal_keycode_str[i])) {
+ keycode = literal_keycode_value[i];
+ printf("\tkey: '%.*s' (%d)\n", key.length, key.text, keycode);
+ break;
+ }
+ }
+
+ return keycode;
+}
+
internal enum hotkey_flag modifier_flags_value[] =
{
Hotkey_Flag_Alt, Hotkey_Flag_LAlt, Hotkey_Flag_RAlt,
@@ -103,10 +133,11 @@ parse_modifier(struct parser *parser)
uint32_t flags = 0;
struct token modifier = parser_previous(parser);
- for(int i = 0; i < array_count(modifier_flags_map); ++i) {
- if(same_string(modifier.text, modifier.length, modifier_flags_map[i])) {
+ // NOTE(koekeishiya): Might want to replace this mapping with a hashtable
+ for(int i = 0; i < array_count(modifier_flags_str); ++i) {
+ if(same_string(modifier.text, modifier.length, modifier_flags_str[i])) {
flags |= modifier_flags_value[i];
- printf("\tmod: '%s'\n", modifier_flags_map[i]);
+ printf("\tmod: '%s'\n", modifier_flags_str[i]);
break;
}
}
@@ -115,7 +146,7 @@ parse_modifier(struct parser *parser)
if(parser_match(parser, Token_Modifier)) {
flags |= parse_modifier(parser);
} else {
- fprintf(stderr, "(#%d:%d) expected token 'Token_Modifier', but got '%.*s'\n",
+ fprintf(stderr, "(#%d:%d) expected modifier, but got '%.*s'\n",
parser->current_token.line, parser->current_token.cursor,
parser->current_token.length, parser->current_token.text);
parser->error = true;
@@ -145,7 +176,7 @@ parse_hotkey(struct parser *parser)
if(found_modifier) {
if(!parser_match(parser, Token_Dash)) {
- fprintf(stderr, "(#%d:%d) expected token '-', but got '%.*s'\n",
+ fprintf(stderr, "(#%d:%d) expected '-', but got '%.*s'\n",
parser->current_token.line, parser->current_token.cursor,
parser->current_token.length, parser->current_token.text);
parser->error = true;
@@ -157,8 +188,10 @@ parse_hotkey(struct parser *parser)
hotkey->key = parse_key(parser);
} else if(parser_match(parser, Token_Key_Hex)) {
hotkey->key = parse_key_hex(parser);
+ } else if(parser_match(parser, Token_Literal)) {
+ hotkey->key = parse_key_literal(parser);
} else {
- fprintf(stderr, "(#%d:%d) expected token 'Token_Key', but got '%.*s'\n",
+ fprintf(stderr, "(#%d:%d) expected key-literal, but got '%.*s'\n",
parser->current_token.line, parser->current_token.cursor,
parser->current_token.length, parser->current_token.text);
parser->error = true;
@@ -172,7 +205,7 @@ parse_hotkey(struct parser *parser)
if(parser_match(parser, Token_Command)) {
hotkey->command = parse_command(parser);
} else {
- fprintf(stderr, "(#%d:%d) expected token 'Token_Command', but got '%.*s'\n",
+ fprintf(stderr, "(#%d:%d) expected ':' followed by command, but got '%.*s'\n",
parser->current_token.line, parser->current_token.cursor,
parser->current_token.length, parser->current_token.text);
parser->error = true;
@@ -189,6 +222,7 @@ void parse_config(struct parser *parser, struct table *hotkey_map)
struct hotkey *hotkey;
while(!parser_eof(parser)) {
if((parser_check(parser, Token_Modifier)) ||
+ (parser_check(parser, Token_Literal)) ||
(parser_check(parser, Token_Key_Hex)) ||
(parser_check(parser, Token_Key))) {
hotkey = parse_hotkey(parser);
@@ -198,7 +232,7 @@ void parse_config(struct parser *parser, struct table *hotkey_map)
}
table_add(hotkey_map, hotkey, hotkey);
} else {
- fprintf(stderr, "(#%d:%d) expected token 'Token_Modifier', 'Token_Key_Hex' or 'Token_Key', but got '%.*s'\n",
+ fprintf(stderr, "(#%d:%d) expected modifier or key-literal, but got '%.*s'\n",
parser->current_token.line, parser->current_token.cursor,
parser->current_token.length, parser->current_token.text);
parser->error = true;
diff --git a/src/tokenize.c b/src/tokenize.c
index 333ee67..5f83084 100644
--- a/src/tokenize.c
+++ b/src/tokenize.c
@@ -1,6 +1,8 @@
#include "tokenize.h"
#define internal static
+#include <ctype.h>
+
internal int
token_equals(struct token token, const char *match)
{
@@ -81,15 +83,15 @@ resolve_identifier_type(struct token token)
return Token_Key;
}
- for(int i = 0; i < array_count(token_modifier_map); ++i) {
- if(token_equals(token, token_modifier_map[i])) {
+ for(int i = 0; i < array_count(modifier_flags_str); ++i) {
+ if(token_equals(token, modifier_flags_str[i])) {
return Token_Modifier;
}
}
- for(int i = 0; i < array_count(token_key_map); ++i) {
- if(token_equals(token, token_key_map[i])) {
- return Token_Key;
+ for(int i = 0; i < array_count(literal_keycode_str); ++i) {
+ if(token_equals(token, literal_keycode_str[i])) {
+ return Token_Literal;
}
}
diff --git a/src/tokenize.h b/src/tokenize.h
index 8b8ecf9..1c3141a 100644
--- a/src/tokenize.h
+++ b/src/tokenize.h
@@ -2,34 +2,35 @@
#define SKHD_TOKENIZE_H
#define array_count(a) (sizeof((a)) / sizeof(*(a)))
-static const char *token_modifier_map[] =
+static const char *modifier_flags_str[] =
{
- "lctrl", "ctrl", "rctrl",
- "lalt", "alt", "ralt",
- "lshift", "shift", "rshift",
- "lcmd", "cmd", "rcmd",
+ "alt", "lalt", "ralt",
+ "shift", "lshift", "rshift",
+ "cmd", "lcmd", "rcmd",
+ "ctrl", "lctrl", "rctrl",
};
-static const char *token_key_map[] =
+static const char *literal_keycode_str[] =
{
"return", "tab", "space",
"backspace", "delete", "escape",
- "capslock", "home", "end",
- "pageup", "pagedown", "help",
- "left", "right", "up",
- "down", "f1", "f2",
- "f3", "f4", "f5",
- "f6", "f7", "f8",
- "f9", "f10", "f11",
- "f12", "f13", "f14",
- "f15", "f16", "f17",
- "f18", "f19", "f20",
+ "home", "end", "pageup",
+ "pagedown", "help", "left",
+ "right", "up", "down",
+ "f1", "f2", "f3",
+ "f4", "f5", "f6",
+ "f7", "f8", "f9",
+ "f10", "f11", "f12",
+ "f13", "f14", "f15",
+ "f16", "f17", "f18",
+ "f19", "f20",
};
enum token_type
{
Token_Command,
Token_Modifier,
+ Token_Literal,
Token_Key_Hex,
Token_Key,