aboutsummaryrefslogtreecommitdiff
path: root/sys/libterm/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/libterm/input.c')
-rw-r--r--sys/libterm/input.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/sys/libterm/input.c b/sys/libterm/input.c
new file mode 100644
index 0000000..038e8df
--- /dev/null
+++ b/sys/libterm/input.c
@@ -0,0 +1,108 @@
+#include <u.h>
+#include <libn.h>
+#include <unibilium.h>
+
+#define iota(x) 1 << (x)
+
+// -----------------------------------------------------------------------
+// enums
+
+enum
+{
+ KeyRune, KeyFunc, KeySym,
+ KeyMouse, KeyPosition, KeyModeReport,
+ KeyDCS, KeyOSC, /* add other recognised types here */
+ KeyUnknownCSI = -1
+};
+
+enum KeyEvent
+{
+ EvNil, EvKey, EvEOF, EvAgain, EvErr,
+};
+
+enum
+{
+ SymUnknown = -1,
+ SymNone = 0,
+
+ /* special names in c0 */
+ SymBackspace, SymTab, SymEnter, SymEscape,
+
+ /* special names in g0 */
+ SymSpace, SymDel,
+
+ /* special keys */
+ SymUp, SymDown, SymLeft, SymRight, SymBegin, SymFind, SymInsert,
+ SymDelete, SymSelect, SymPageUp, SymPageDown, SymHome, SymEnd,
+
+ /* special keys from terminfo */
+ SymCancel, SymClear, SymClose, SymCommand, SymCopy, SymExit,
+ SymHelp, SymMark, SymMessage, SymMove, SymOpen, SymOptions,
+ SymPrint, SymRedo, SymReference, SymRefresh, SymReplace,
+ SymRestart, SymResume, SymSave, SymSuspend, SymUndo,
+
+ /* numeric keypad special keys */
+ SymKp0, SymKp1, SymKp2, SymKp3, SymKp4, SymKp5, SymKp6, SymKp7, SymKp8,
+ SymKp9, SymKpEnter, SymKpPlus, SymKpMinus, SymKpMul, SymKpDiv, SymKpComma,
+ SymKpDot, SymKpEq,
+
+ /* et cetera ad nauseum */
+ NumSyms
+};
+
+enum MouseEvent
+{
+ MouseNil, MousePress, MouseDrag, MouseRelease,
+};
+
+enum
+{
+ ModShift = iota(0),
+ ModAlt = iota(1),
+ ModCtrl = iota(2),
+};
+
+// -----------------------------------------------------------------------
+// types
+
+typedef struct Input Input;
+
+struct Input
+{
+ int fd;
+ int wait; /* in msec */
+ struct {
+ char *b, *c, bytes[1024];
+ } rbuf; /* read buffer */
+ struct {
+ char *b, *c, bytes[1024];
+ } ebuf; /* escape buffer */
+};
+
+// -----------------------------------------------------------------------
+// globals
+
+// -----------------------------------------------------------------------
+// utility functions
+
+// -----------------------------------------------------------------------
+// implementation
+
+Input *
+makeinput(int fd, int flags, unibi_term *info)
+{
+ int i;
+ Input *in;
+ char *e;
+
+ if (!(in = calloc(1, sizeof(in))))
+ panicf("out of memory");
+
+ in->fd = fd;
+ in->wait = 50; /* in msec */
+
+ /* initialize buffers */
+ in->rbuf.c = in->rbuf.b = in->rbuf.bytes;
+
+ return in;
+}