Browse Source

Add some hotkey related code

Serj Kalichev 1 year ago
parent
commit
cbbafe9a35
7 changed files with 194 additions and 0 deletions
  1. 2 0
      klish/Makefile.am
  2. 27 0
      klish/ihotkey.h
  3. 1 0
      klish/ischeme/Makefile.am
  4. 79 0
      klish/ischeme/ihotkey.c
  5. 25 0
      klish/khotkey.h
  6. 1 0
      klish/kscheme/Makefile.am
  7. 59 0
      klish/kscheme/khotkey.c

+ 2 - 0
klish/Makefile.am

@@ -27,6 +27,7 @@ nobase_include_HEADERS += \
 	klish/kentry.h \
 	klish/kplugin.h \
 	klish/kaction.h \
+	klish/khotkey.h \
 	klish/ksym.h \
 	klish/kdb.h
 
@@ -35,6 +36,7 @@ nobase_include_HEADERS += \
 	klish/ischeme.h \
 	klish/iplugin.h \
 	klish/iaction.h \
+	klish/ihotkey.h \
 	klish/ientry.h
 
 # Session

+ 27 - 0
klish/ihotkey.h

@@ -0,0 +1,27 @@
+/** @file ihotkey.h
+ *
+ * @brief Klish scheme's "hotkey" entry
+ */
+
+#ifndef _klish_ihotkey_h
+#define _klish_ihotkey_h
+
+#include <faux/error.h>
+#include <klish/khotkey.h>
+
+typedef struct ihotkey_s {
+	char *key;
+	char *cmd;
+} ihotkey_t;
+
+
+C_DECL_BEGIN
+
+//bool_t ihotkey_parse(const ihotkey_t *info, khotkey_t *hotkey,
+//	faux_error_t *error);
+khotkey_t *ihotkey_load(const ihotkey_t *ihotkey, faux_error_t *error);
+char *ihotkey_deploy(const khotkey_t *khotkey, int level);
+
+C_DECL_END
+
+#endif // _klish_ihotkey_h

+ 1 - 0
klish/ischeme/Makefile.am

@@ -1,5 +1,6 @@
 libklish_la_SOURCES += \
 	klish/ischeme/iaction.c \
 	klish/ischeme/iplugin.c \
+	klish/ischeme/ihotkey.c \
 	klish/ischeme/ientry.c \
 	klish/ischeme/ischeme.c

+ 79 - 0
klish/ischeme/ihotkey.c

@@ -0,0 +1,79 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <faux/str.h>
+#include <faux/conv.h>
+#include <faux/error.h>
+#include <klish/khelper.h>
+#include <klish/khotkey.h>
+#include <klish/ihotkey.h>
+
+#define TAG "HOTKEY"
+
+/*
+bool_t ihotkey_parse(const ihotkey_t *info, khotkey_t *hotkey, faux_error_t *error)
+{
+	bool_t retcode = BOOL_TRUE;
+
+	if (!info)
+		return BOOL_FALSE;
+	if (!hotkey)
+		return BOOL_FALSE;
+
+	return retcode;
+}
+*/
+
+khotkey_t *ihotkey_load(const ihotkey_t *ihotkey, faux_error_t *error)
+{
+	khotkey_t *khotkey = NULL;
+
+	// Key [mandatory]
+	if (faux_str_is_empty(ihotkey->key)) {
+		faux_error_add(error, TAG": Empty 'key' attribute");
+		return NULL;
+	}
+
+	// Command [mandatory]
+	if (faux_str_is_empty(ihotkey->cmd)) {
+		faux_error_add(error, TAG": Empty 'cmd' attribute");
+		return NULL;
+	}
+
+	khotkey = khotkey_new(ihotkey->key, ihotkey->cmd);
+	if (!khotkey) {
+		faux_error_add(error, TAG": Can't create object");
+		return NULL;
+	}
+//	if (!ihotkey_parse(ihotkey, khotkey, error)) {
+//		khotkey_free(khotkey);
+//		return NULL;
+//	}
+
+	return khotkey;
+}
+
+
+char *ihotkey_deploy(const khotkey_t *khotkey, int level)
+{
+	char *str = NULL;
+	char *tmp = NULL;
+
+	if (!khotkey)
+		return NULL;
+
+	tmp = faux_str_sprintf("%*chotkey {\n", level, ' ');
+	faux_str_cat(&str, tmp);
+	faux_str_free(tmp);
+
+	attr2ctext(&str, "key", khotkey_key(khotkey), level + 1);
+	attr2ctext(&str, "cmd", khotkey_cmd(khotkey), level + 1);
+
+	tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
+	faux_str_cat(&str, tmp);
+	faux_str_free(tmp);
+
+	return str;
+}

+ 25 - 0
klish/khotkey.h

@@ -0,0 +1,25 @@
+/** @file khotkey.h
+ *
+ * @brief Klish scheme's "hotkey" entry
+ */
+
+#ifndef _klish_khotkey_h
+#define _klish_khotkey_h
+
+#include <faux/error.h>
+
+
+typedef struct khotkey_s khotkey_t;
+
+
+C_DECL_BEGIN
+
+khotkey_t *khotkey_new(const char *key, const char *cmd);
+void khotkey_free(khotkey_t *hotkey);
+
+const char *khotkey_key(const khotkey_t *hotkey);
+const char *khotkey_cmd(const khotkey_t *hotkey);
+
+C_DECL_END
+
+#endif // _klish_khotkey_h

+ 1 - 0
klish/kscheme/Makefile.am

@@ -3,6 +3,7 @@ libklish_la_SOURCES += \
 	klish/kscheme/ksym.c \
 	klish/kscheme/kplugin.c \
 	klish/kscheme/kaction.c \
+	klish/kscheme/khotkey.c \
 	klish/kscheme/kscheme.c \
 	klish/kscheme/kdb.c \
 	klish/kscheme/kentry.c

+ 59 - 0
klish/kscheme/khotkey.c

@@ -0,0 +1,59 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <faux/str.h>
+#include <klish/khelper.h>
+#include <klish/khotkey.h>
+
+
+struct khotkey_s {
+	char *key;
+	char *cmd;
+};
+
+
+// Simple methods
+
+// Key
+KGET_STR(hotkey, key);
+
+// Command
+KGET_STR(hotkey, cmd);
+
+
+khotkey_t *khotkey_new(const char *key, const char *cmd)
+{
+	khotkey_t *hotkey = NULL;
+
+	// Mandatory key
+	if (!key)
+		return NULL;
+	// Mandatory cmd
+	if (!cmd)
+		return NULL;
+
+	hotkey = faux_zmalloc(sizeof(*hotkey));
+	assert(hotkey);
+	if (!hotkey)
+		return NULL;
+
+	// Initialize
+	hotkey->key = faux_str_dup(key);
+	hotkey->cmd = faux_str_dup(cmd);
+
+	return hotkey;
+}
+
+
+void khotkey_free(khotkey_t *hotkey)
+{
+	if (!hotkey)
+		return;
+
+	faux_str_free(hotkey->key);
+	faux_str_free(hotkey->cmd);
+
+	faux_free(hotkey);
+}