From c2dd9675d6af30ab5129519325bac68491087cc1 Mon Sep 17 00:00:00 2001 From: Paul LeoNerd Evans Date: Sun, 16 Nov 2008 12:28:38 +0000 Subject: Store major and minor version number in Makefile; substitute #defines in termkey.h at build time --- .bzrignore | 1 + Makefile | 12 ++++- termkey.h | 162 --------------------------------------------------------- termkey.h.in | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+), 164 deletions(-) create mode 100644 .bzrignore delete mode 100644 termkey.h create mode 100644 termkey.h.in diff --git a/.bzrignore b/.bzrignore new file mode 100644 index 0000000..b5bdf38 --- /dev/null +++ b/.bzrignore @@ -0,0 +1 @@ +termkey.h diff --git a/Makefile b/Makefile index a24a716..97441a1 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,10 @@ CFLAGS?= CFLAGS_DEBUG= -SONAME=libtermkey.so.0 +VERSION_MAJOR=0 +VERSION_MINOR=0 + +SONAME=libtermkey.so.$(VERSION_MAJOR) PREFIX=/usr/local LIBDIR=$(PREFIX)/lib @@ -15,7 +18,12 @@ ifeq ($(DEBUG),1) CFLAGS_DEBUG=-ggdb -DDEBUG endif -all: demo demo-async doc +all: termkey.h demo demo-async doc + +termkey.h: termkey.h.in Makefile + sed -e 's/@@VERSION_MAJOR@@/$(VERSION_MAJOR)/g' \ + -e 's/@@VERSION_MINOR@@/$(VERSION_MINOR)/g' \ + $< >$@ demo: libtermkey.so demo.c $(CC) $(CFLAGS) $(CFLAGS_DEBUG) -o $@ $^ diff --git a/termkey.h b/termkey.h deleted file mode 100644 index 4227518..0000000 --- a/termkey.h +++ /dev/null @@ -1,162 +0,0 @@ -#ifndef GUARD_TERMKEY_H_ -#define GUARD_TERMKEY_H_ - -#include -#include - -typedef enum { - TERMKEY_SYM_UNKNOWN = -1, - TERMKEY_SYM_NONE = 0, - - // Special names in C0 - TERMKEY_SYM_BACKSPACE, - TERMKEY_SYM_TAB, - TERMKEY_SYM_ENTER, - TERMKEY_SYM_ESCAPE, - - // Special names in G0 - TERMKEY_SYM_SPACE, - TERMKEY_SYM_DEL, - - // Special keys - TERMKEY_SYM_UP, - TERMKEY_SYM_DOWN, - TERMKEY_SYM_LEFT, - TERMKEY_SYM_RIGHT, - TERMKEY_SYM_BEGIN, - TERMKEY_SYM_FIND, - TERMKEY_SYM_INSERT, - TERMKEY_SYM_DELETE, - TERMKEY_SYM_SELECT, - TERMKEY_SYM_PAGEUP, - TERMKEY_SYM_PAGEDOWN, - TERMKEY_SYM_HOME, - TERMKEY_SYM_END, - - // Special keys from terminfo - TERMKEY_SYM_CANCEL, - TERMKEY_SYM_CLEAR, - TERMKEY_SYM_CLOSE, - TERMKEY_SYM_COMMAND, - TERMKEY_SYM_COPY, - TERMKEY_SYM_EXIT, - TERMKEY_SYM_HELP, - TERMKEY_SYM_MARK, - TERMKEY_SYM_MESSAGE, - TERMKEY_SYM_MOVE, - TERMKEY_SYM_OPEN, - TERMKEY_SYM_OPTIONS, - TERMKEY_SYM_PRINT, - TERMKEY_SYM_REDO, - TERMKEY_SYM_REFERENCE, - TERMKEY_SYM_REFRESH, - TERMKEY_SYM_REPLACE, - TERMKEY_SYM_RESTART, - TERMKEY_SYM_RESUME, - TERMKEY_SYM_SAVE, - TERMKEY_SYM_SUSPEND, - TERMKEY_SYM_UNDO, - - // Numeric keypad special keys - TERMKEY_SYM_KP0, - TERMKEY_SYM_KP1, - TERMKEY_SYM_KP2, - TERMKEY_SYM_KP3, - TERMKEY_SYM_KP4, - TERMKEY_SYM_KP5, - TERMKEY_SYM_KP6, - TERMKEY_SYM_KP7, - TERMKEY_SYM_KP8, - TERMKEY_SYM_KP9, - TERMKEY_SYM_KPENTER, - TERMKEY_SYM_KPPLUS, - TERMKEY_SYM_KPMINUS, - TERMKEY_SYM_KPMULT, - TERMKEY_SYM_KPDIV, - TERMKEY_SYM_KPCOMMA, - TERMKEY_SYM_KPPERIOD, - TERMKEY_SYM_KPEQUALS, - - // et cetera ad nauseum -} termkey_sym; - -typedef enum { - TERMKEY_TYPE_UNICODE, - TERMKEY_TYPE_FUNCTION, - TERMKEY_TYPE_KEYSYM -} termkey_type; - -typedef enum { - TERMKEY_RES_NONE, - TERMKEY_RES_KEY, - TERMKEY_RES_EOF, - TERMKEY_RES_AGAIN, -} termkey_result; - -enum { - TERMKEY_KEYMOD_SHIFT = 1 << 0, - TERMKEY_KEYMOD_ALT = 1 << 1, - TERMKEY_KEYMOD_CTRL = 1 << 2, -}; - -typedef int termkey_keysym; - -typedef struct { - termkey_type type; - union { - long codepoint; // TERMKEY_TYPE_UNICODE - int number; // TERMKEY_TYPE_FUNCTION - termkey_keysym sym; // TERMKEY_TYPE_KEYSYM - } code; - int modifiers; - - /* Any Unicode character can be UTF-8 encoded in no more than 6 bytes, plus - * terminating NUL */ - char utf8[7]; -} termkey_key; - -typedef struct termkey termkey_t; - -enum { - TERMKEY_FLAG_NOINTERPRET = 1 << 0, // Do not interpret C0//G1 codes if possible - TERMKEY_FLAG_CONVERTKP = 1 << 1, // Convert KP codes to regular keypresses - TERMKEY_FLAG_RAW = 1 << 2, // Input is raw bytes, not UTF-8 - TERMKEY_FLAG_UTF8 = 1 << 3, // Input is definitely UTF-8 - TERMKEY_FLAG_NOTERMIOS = 1 << 4, // Do not make initial termios calls on construction -}; - -termkey_t *termkey_new(int fd, int flags); -void termkey_free(termkey_t *tk); -void termkey_destroy(termkey_t *tk); - -int termkey_get_flags(termkey_t *tk); -void termkey_set_flags(termkey_t *tk, int newflags); - -void termkey_set_waittime(termkey_t *tk, int msec); -int termkey_get_waittime(termkey_t *tk); - -termkey_result termkey_getkey(termkey_t *tk, termkey_key *key); -termkey_result termkey_getkey_force(termkey_t *tk, termkey_key *key); -termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key); - -void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen); - -termkey_result termkey_advisereadable(termkey_t *tk); - -termkey_keysym termkey_register_keyname(termkey_t *tk, termkey_keysym sym, const char *name); -const char *termkey_get_keyname(termkey_t *tk, termkey_keysym sym); - -typedef enum { - TERMKEY_FORMAT_LONGMOD = 1 << 0, // Shift-... instead of S-... - TERMKEY_FORMAT_CARETCTRL = 1 << 1, // ^X instead of C-X - TERMKEY_FORMAT_ALTISMETA = 1 << 2, // Meta- or M- instead of Alt- or A- - TERMKEY_FORMAT_WRAPBRACKET = 1 << 3, // Wrap special keys in brackets like -} termkey_format; - -// Some useful combinations - -#define TERMKEY_FORMAT_VIM (TERMKEY_FORMAT_ALTISMETA|TERMKEY_FORMAT_WRAPBRACKET) - -size_t termkey_snprint_key(termkey_t *tk, char *buffer, size_t len, termkey_key *key, termkey_format format); - -#endif diff --git a/termkey.h.in b/termkey.h.in new file mode 100644 index 0000000..4849923 --- /dev/null +++ b/termkey.h.in @@ -0,0 +1,165 @@ +#ifndef GUARD_TERMKEY_H_ +#define GUARD_TERMKEY_H_ + +#include +#include + +#define TERMKEY_VERSION_MAJOR @@VERSION_MAJOR@@ +#define TERMKEY_VERSION_MINOR @@VERSION_MINOR@@ + +typedef enum { + TERMKEY_SYM_UNKNOWN = -1, + TERMKEY_SYM_NONE = 0, + + // Special names in C0 + TERMKEY_SYM_BACKSPACE, + TERMKEY_SYM_TAB, + TERMKEY_SYM_ENTER, + TERMKEY_SYM_ESCAPE, + + // Special names in G0 + TERMKEY_SYM_SPACE, + TERMKEY_SYM_DEL, + + // Special keys + TERMKEY_SYM_UP, + TERMKEY_SYM_DOWN, + TERMKEY_SYM_LEFT, + TERMKEY_SYM_RIGHT, + TERMKEY_SYM_BEGIN, + TERMKEY_SYM_FIND, + TERMKEY_SYM_INSERT, + TERMKEY_SYM_DELETE, + TERMKEY_SYM_SELECT, + TERMKEY_SYM_PAGEUP, + TERMKEY_SYM_PAGEDOWN, + TERMKEY_SYM_HOME, + TERMKEY_SYM_END, + + // Special keys from terminfo + TERMKEY_SYM_CANCEL, + TERMKEY_SYM_CLEAR, + TERMKEY_SYM_CLOSE, + TERMKEY_SYM_COMMAND, + TERMKEY_SYM_COPY, + TERMKEY_SYM_EXIT, + TERMKEY_SYM_HELP, + TERMKEY_SYM_MARK, + TERMKEY_SYM_MESSAGE, + TERMKEY_SYM_MOVE, + TERMKEY_SYM_OPEN, + TERMKEY_SYM_OPTIONS, + TERMKEY_SYM_PRINT, + TERMKEY_SYM_REDO, + TERMKEY_SYM_REFERENCE, + TERMKEY_SYM_REFRESH, + TERMKEY_SYM_REPLACE, + TERMKEY_SYM_RESTART, + TERMKEY_SYM_RESUME, + TERMKEY_SYM_SAVE, + TERMKEY_SYM_SUSPEND, + TERMKEY_SYM_UNDO, + + // Numeric keypad special keys + TERMKEY_SYM_KP0, + TERMKEY_SYM_KP1, + TERMKEY_SYM_KP2, + TERMKEY_SYM_KP3, + TERMKEY_SYM_KP4, + TERMKEY_SYM_KP5, + TERMKEY_SYM_KP6, + TERMKEY_SYM_KP7, + TERMKEY_SYM_KP8, + TERMKEY_SYM_KP9, + TERMKEY_SYM_KPENTER, + TERMKEY_SYM_KPPLUS, + TERMKEY_SYM_KPMINUS, + TERMKEY_SYM_KPMULT, + TERMKEY_SYM_KPDIV, + TERMKEY_SYM_KPCOMMA, + TERMKEY_SYM_KPPERIOD, + TERMKEY_SYM_KPEQUALS, + + // et cetera ad nauseum +} termkey_sym; + +typedef enum { + TERMKEY_TYPE_UNICODE, + TERMKEY_TYPE_FUNCTION, + TERMKEY_TYPE_KEYSYM +} termkey_type; + +typedef enum { + TERMKEY_RES_NONE, + TERMKEY_RES_KEY, + TERMKEY_RES_EOF, + TERMKEY_RES_AGAIN, +} termkey_result; + +enum { + TERMKEY_KEYMOD_SHIFT = 1 << 0, + TERMKEY_KEYMOD_ALT = 1 << 1, + TERMKEY_KEYMOD_CTRL = 1 << 2, +}; + +typedef int termkey_keysym; + +typedef struct { + termkey_type type; + union { + long codepoint; // TERMKEY_TYPE_UNICODE + int number; // TERMKEY_TYPE_FUNCTION + termkey_keysym sym; // TERMKEY_TYPE_KEYSYM + } code; + int modifiers; + + /* Any Unicode character can be UTF-8 encoded in no more than 6 bytes, plus + * terminating NUL */ + char utf8[7]; +} termkey_key; + +typedef struct termkey termkey_t; + +enum { + TERMKEY_FLAG_NOINTERPRET = 1 << 0, // Do not interpret C0//G1 codes if possible + TERMKEY_FLAG_CONVERTKP = 1 << 1, // Convert KP codes to regular keypresses + TERMKEY_FLAG_RAW = 1 << 2, // Input is raw bytes, not UTF-8 + TERMKEY_FLAG_UTF8 = 1 << 3, // Input is definitely UTF-8 + TERMKEY_FLAG_NOTERMIOS = 1 << 4, // Do not make initial termios calls on construction +}; + +termkey_t *termkey_new(int fd, int flags); +void termkey_free(termkey_t *tk); +void termkey_destroy(termkey_t *tk); + +int termkey_get_flags(termkey_t *tk); +void termkey_set_flags(termkey_t *tk, int newflags); + +void termkey_set_waittime(termkey_t *tk, int msec); +int termkey_get_waittime(termkey_t *tk); + +termkey_result termkey_getkey(termkey_t *tk, termkey_key *key); +termkey_result termkey_getkey_force(termkey_t *tk, termkey_key *key); +termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key); + +void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen); + +termkey_result termkey_advisereadable(termkey_t *tk); + +termkey_keysym termkey_register_keyname(termkey_t *tk, termkey_keysym sym, const char *name); +const char *termkey_get_keyname(termkey_t *tk, termkey_keysym sym); + +typedef enum { + TERMKEY_FORMAT_LONGMOD = 1 << 0, // Shift-... instead of S-... + TERMKEY_FORMAT_CARETCTRL = 1 << 1, // ^X instead of C-X + TERMKEY_FORMAT_ALTISMETA = 1 << 2, // Meta- or M- instead of Alt- or A- + TERMKEY_FORMAT_WRAPBRACKET = 1 << 3, // Wrap special keys in brackets like +} termkey_format; + +// Some useful combinations + +#define TERMKEY_FORMAT_VIM (TERMKEY_FORMAT_ALTISMETA|TERMKEY_FORMAT_WRAPBRACKET) + +size_t termkey_snprint_key(termkey_t *tk, char *buffer, size_t len, termkey_key *key, termkey_format format); + +#endif -- cgit v1.2.3-70-g09d2