Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Add support for Windows
Browse files Browse the repository at this point in the history
Ported termkey for windows:

- A new CFLAG HAVE_TERMIOS controls the use of termios.h. If it is not
  available TERMKEY_FLAG_NOTERMIOS is ignored. By default it is set for
  the Makefile.
- The termkey_waitkey() function is not implemented in windows, since there
  is no poll().
  • Loading branch information
equalsraf committed May 3, 2017
1 parent 713dcc2 commit 578fe18
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Makefile
Expand Up @@ -12,6 +12,11 @@ endif

override CFLAGS +=-Wall -std=c99

HAVE_TERMIOS ?= 1
ifeq ($(HAVE_TERMIOS),1)
override CFLAGS += -DHAVE_TERMIOS
endif

ifeq ($(DEBUG),1)
override CFLAGS +=-ggdb -DDEBUG
endif
Expand Down
8 changes: 7 additions & 1 deletion driver-ti.c
Expand Up @@ -17,7 +17,9 @@
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifndef _WIN32
# include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>

Expand Down Expand Up @@ -338,8 +340,10 @@ static int start_driver(TermKey *tk, void *info)
if(fstat(tk->fd, &statbuf) == -1)
return 0;

#ifndef _WIN32
if(S_ISFIFO(statbuf.st_mode))
return 1;
#endif

// Can't call putp or tputs because they suck and don't give us fd control
len = strlen(start_string);
Expand Down Expand Up @@ -367,8 +371,10 @@ static int stop_driver(TermKey *tk, void *info)
if(fstat(tk->fd, &statbuf) == -1)
return 0;

#ifndef _WIN32
if(S_ISFIFO(statbuf.st_mode))
return 1;
#endif

/* The terminfo database will contain keys in application cursor key mode.
* We may need to enable that mode
Expand Down
11 changes: 10 additions & 1 deletion termkey-internal.h
Expand Up @@ -4,7 +4,14 @@
#include "termkey.h"

#include <stdint.h>
#include <termios.h>
#ifdef HAVE_TERMIOS
# include <termios.h>
#endif

#ifdef _MSC_VER
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif

struct TermKeyDriver
{
Expand Down Expand Up @@ -41,8 +48,10 @@ struct TermKey {
size_t hightide; /* Position beyond buffstart at which peekkey() should next start
* normally 0, but see also termkey_interpret_csi */

#ifdef HAVE_TERMIOS
struct termios restore_termios;
char restore_termios_valid;
#endif

TermKey_Terminfo_Getstr_Hook *ti_getstr_hook;
void *ti_getstr_hook_data;
Expand Down
22 changes: 18 additions & 4 deletions termkey.c
Expand Up @@ -3,14 +3,20 @@

#include <ctype.h>
#include <errno.h>
#include <poll.h>
#include <unistd.h>
#ifndef _WIN32
# include <poll.h>
# include <unistd.h>
# include <strings.h>
#endif
#include <string.h>
#include <strings.h>

#include <stdio.h>

#define strcaseeq(a,b) (strcasecmp(a,b) == 0)
#ifdef _MSC_VER
# define strcaseeq(a,b) (_stricmp(a,b) == 0)
#else
# define strcaseeq(a,b) (strcasecmp(a,b) == 0)
#endif

void termkey_check_version(int major, int minor)
{
Expand Down Expand Up @@ -282,7 +288,9 @@ static TermKey *termkey_alloc(void)
tk->buffsize = 256; /* bytes */
tk->hightide = 0;

#ifdef HAVE_TERMIOS
tk->restore_termios_valid = 0;
#endif

tk->ti_getstr_hook = NULL;
tk->ti_getstr_hook_data = NULL;
Expand Down Expand Up @@ -483,6 +491,7 @@ int termkey_start(TermKey *tk)
if(tk->is_started)
return 1;

#ifdef HAVE_TERMIOS
if(tk->fd != -1 && !(tk->flags & TERMKEY_FLAG_NOTERMIOS)) {
struct termios termios;
if(tcgetattr(tk->fd, &termios) == 0) {
Expand Down Expand Up @@ -517,6 +526,7 @@ int termkey_start(TermKey *tk)
tcsetattr(tk->fd, TCSANOW, &termios);
}
}
#endif

struct TermKeyDriverNode *p;
for(p = tk->drivers; p; p = p->next)
Expand All @@ -542,8 +552,10 @@ int termkey_stop(TermKey *tk)
if(p->driver->stop_driver)
(*p->driver->stop_driver)(tk, p->info);

#ifdef HAVE_TERMIOS
if(tk->restore_termios_valid)
tcsetattr(tk->fd, TCSANOW, &tk->restore_termios);
#endif

tk->is_started = 0;

Expand Down Expand Up @@ -1046,6 +1058,7 @@ TermKeyResult termkey_getkey_force(TermKey *tk, TermKeyKey *key)
return ret;
}

#ifndef _WIN32
TermKeyResult termkey_waitkey(TermKey *tk, TermKeyKey *key)
{
if(tk->fd == -1) {
Expand Down Expand Up @@ -1105,6 +1118,7 @@ TermKeyResult termkey_waitkey(TermKey *tk, TermKeyKey *key)

/* UNREACHABLE */
}
#endif

TermKeyResult termkey_advisereadable(TermKey *tk)
{
Expand Down
2 changes: 2 additions & 0 deletions termkey.h.in
Expand Up @@ -197,7 +197,9 @@ void termkey_canonicalise(TermKey *tk, TermKeyKey *key);

TermKeyResult termkey_getkey(TermKey *tk, TermKeyKey *key);
TermKeyResult termkey_getkey_force(TermKey *tk, TermKeyKey *key);
#ifndef _WIN32
TermKeyResult termkey_waitkey(TermKey *tk, TermKeyKey *key);
#endif

TermKeyResult termkey_advisereadable(TermKey *tk);

Expand Down

0 comments on commit 578fe18

Please sign in to comment.