aboutsummaryrefslogtreecommitdiff
path: root/termkey.c
diff options
context:
space:
mode:
authorPaul LeoNerd Evans <leonerd@leonerd.org.uk>2012-01-26 10:06:05 +0000
committerPaul LeoNerd Evans <leonerd@leonerd.org.uk>2012-01-26 10:06:05 +0000
commitf9fe338e3ebfecc855dfd8a9b34e7ab3df4cd238 (patch)
tree17ee464b923adfcaa2173cafd5e59d5ce730351c /termkey.c
parent4cf544f26cc36b5ba6e2a6f150f7214ccecc31f7 (diff)
downloadtermo-f9fe338e3ebfecc855dfd8a9b34e7ab3df4cd238.tar.gz
termo-f9fe338e3ebfecc855dfd8a9b34e7ab3df4cd238.tar.xz
termo-f9fe338e3ebfecc855dfd8a9b34e7ab3df4cd238.zip
Split termkey_new() into alloc and initialise phases
Diffstat (limited to 'termkey.c')
-rw-r--r--termkey.c71
1 files changed, 41 insertions, 30 deletions
diff --git a/termkey.c b/termkey.c
index 1b203de..7e5a473 100644
--- a/termkey.c
+++ b/termkey.c
@@ -173,12 +173,47 @@ static const char *res2str(TermKeyResult res)
}
#endif
-/* We might expose this as public API one day, when the ideas are finalised.
- * As yet it isn't public, so keep it static
- */
-static TermKey *termkey_new_full(int fd, int flags, size_t buffsize, int waittime)
+static TermKey *termkey_alloc(void)
{
- TermKey *tk = malloc(sizeof(*tk));
+ TermKey *tk = malloc(sizeof(TermKey));
+ if(!tk)
+ return NULL;
+
+ /* Default all the object fields but don't allocate anything */
+
+ tk->fd = -1;
+ tk->flags = 0;
+ tk->canonflags = 0;
+
+ tk->buffer = NULL;
+ tk->buffstart = 0;
+ tk->buffcount = 0;
+ tk->buffsize = 256; /* bytes */
+
+ tk->restore_termios_valid = 0;
+
+ tk->waittime = 50; /* msec */
+
+ tk->is_closed = 0;
+
+ tk->nkeynames = 64;
+ tk->keynames = NULL;
+
+ for(int i = 0; i < 32; i++)
+ tk->c0[i].sym = TERMKEY_SYM_NONE;
+
+ tk->drivers = NULL;
+
+ tk->method.emit_codepoint = &emit_codepoint;
+ tk->method.peekkey_simple = &peekkey_simple;
+ tk->method.peekkey_mouse = &peekkey_mouse;
+
+ return tk;
+}
+
+TermKey *termkey_new(int fd, int flags)
+{
+ TermKey *tk = termkey_alloc();
if(!tk)
return NULL;
@@ -203,26 +238,14 @@ static TermKey *termkey_new_full(int fd, int flags, size_t buffsize, int waittim
tk->fd = fd;
tk->flags = flags;
- tk->canonflags = 0;
if(flags & TERMKEY_FLAG_SPACESYMBOL)
tk->canonflags |= TERMKEY_CANON_SPACESYMBOL;
- tk->buffer = malloc(buffsize);
+ tk->buffer = malloc(tk->buffsize);
if(!tk->buffer)
goto abort_free_tk;
- tk->buffstart = 0;
- tk->buffcount = 0;
- tk->buffsize = buffsize;
-
- tk->restore_termios_valid = 0;
-
- tk->waittime = waittime;
-
- tk->is_closed = 0;
-
- tk->nkeynames = 64;
tk->keynames = malloc(sizeof(tk->keynames[0]) * tk->nkeynames);
if(!tk->keynames)
goto abort_free_buffer;
@@ -231,13 +254,6 @@ static TermKey *termkey_new_full(int fd, int flags, size_t buffsize, int waittim
for(i = 0; i < tk->nkeynames; i++)
tk->keynames[i] = NULL;
- for(i = 0; i < 32; i++)
- tk->c0[i].sym = TERMKEY_SYM_NONE;
-
- tk->method.emit_codepoint = &emit_codepoint;
- tk->method.peekkey_simple = &peekkey_simple;
- tk->method.peekkey_mouse = &peekkey_mouse;
-
for(i = 0; keynames[i].name; i++)
if(termkey_register_keyname(tk, keynames[i].sym, keynames[i].name) == -1)
goto abort_free_keynames;
@@ -348,11 +364,6 @@ abort_free_tk:
return NULL;
}
-TermKey *termkey_new(int fd, int flags)
-{
- return termkey_new_full(fd, flags, 256, 50);
-}
-
void termkey_free(TermKey *tk)
{
free(tk->buffer); tk->buffer = NULL;