2009-03-01 13:13:25 +00:00
|
|
|
/*
|
|
|
|
* GyS-TermIO v2.0 (for GySmail v3)
|
|
|
|
* a very small replacement of ncurses library
|
|
|
|
*
|
|
|
|
* copyright (C) 1999 A'rpi/ESP-team
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2005-11-14 00:30:37 +00:00
|
|
|
#include "config.h"
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2008-08-01 20:28:58 +00:00
|
|
|
//#define HAVE_TERMCAP
|
2012-04-06 13:58:39 +00:00
|
|
|
#if !defined(__MORPHOS__)
|
2008-07-30 12:01:30 +00:00
|
|
|
#define CONFIG_IOCTL
|
2002-03-24 01:09:08 +00:00
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
#define MAX_KEYS 64
|
|
|
|
#define BUF_LEN 256
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-02-25 14:05:12 +00:00
|
|
|
#include <signal.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_IOCTL
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
2001-12-03 15:22:03 +00:00
|
|
|
|
2001-11-19 17:03:38 +00:00
|
|
|
#ifdef HAVE_TERMIOS
|
2001-12-03 15:22:03 +00:00
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
|
#include <termios.h>
|
|
|
|
#endif
|
2001-12-03 15:30:06 +00:00
|
|
|
#ifdef HAVE_SYS_TERMIOS_H
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <sys/termios.h>
|
2001-11-19 17:03:38 +00:00
|
|
|
#endif
|
2001-12-03 15:22:03 +00:00
|
|
|
#endif
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <unistd.h>
|
2011-04-28 09:52:00 +00:00
|
|
|
#include <fcntl.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2012-01-13 06:59:21 +00:00
|
|
|
#include "bstr.h"
|
2007-08-25 04:28:08 +00:00
|
|
|
#include "mp_fifo.h"
|
2011-04-25 06:43:59 +00:00
|
|
|
#include "input/keycodes.h"
|
2008-04-29 11:52:45 +00:00
|
|
|
#include "getch2.h"
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-11-19 17:03:38 +00:00
|
|
|
#ifdef HAVE_TERMIOS
|
2001-02-24 20:28:24 +00:00
|
|
|
static struct termios tio_orig;
|
2001-11-19 17:03:38 +00:00
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
static int getch2_len=0;
|
2011-10-03 01:12:15 +00:00
|
|
|
static unsigned char getch2_buf[BUF_LEN];
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
int screen_width=80;
|
|
|
|
int screen_height=24;
|
2005-12-29 11:27:22 +00:00
|
|
|
char * erase_to_end_of_line = NULL;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int len;
|
|
|
|
int code;
|
|
|
|
char chars[8];
|
|
|
|
} keycode_st;
|
|
|
|
static keycode_st getch2_keys[MAX_KEYS];
|
|
|
|
static int getch2_key_db=0;
|
|
|
|
|
2008-08-01 20:28:58 +00:00
|
|
|
#ifdef HAVE_TERMCAP
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
#include <termcap.h>
|
|
|
|
#else
|
2008-12-03 23:01:03 +00:00
|
|
|
int tgetent(char *BUFFER, char *TERMTYPE);
|
|
|
|
int tgetnum(char *NAME);
|
|
|
|
int tgetflag(char *NAME);
|
|
|
|
char *tgetstr(char *NAME, char **AREA);
|
2001-02-24 20:28:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static char term_buffer[4096];
|
|
|
|
static char term_buffer2[4096];
|
|
|
|
static char *term_p=term_buffer2;
|
|
|
|
|
|
|
|
static void termcap_add(char *id,int code){
|
|
|
|
char *p=tgetstr(id,&term_p);
|
|
|
|
if(!p) return;
|
|
|
|
if(getch2_key_db>=MAX_KEYS) return;
|
|
|
|
getch2_keys[getch2_key_db].len=strlen(p);
|
|
|
|
strncpy(getch2_keys[getch2_key_db].chars,p,8);
|
|
|
|
getch2_keys[getch2_key_db].code=code;
|
|
|
|
++getch2_key_db;
|
|
|
|
/* printf("%s=%s\n",id,p); */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int success=0;
|
|
|
|
|
|
|
|
int load_termcap(char *termtype){
|
|
|
|
if(!termtype) termtype=getenv("TERM");
|
2003-01-11 12:32:24 +00:00
|
|
|
if(!termtype) termtype="unknown";
|
2001-02-24 20:28:24 +00:00
|
|
|
success=tgetent(term_buffer, termtype);
|
|
|
|
if(success<0){ printf("Could not access the 'termcap' data base.\n"); return 0; }
|
|
|
|
if(success==0){ printf("Terminal type `%s' is not defined.\n", termtype);return 0;}
|
|
|
|
|
|
|
|
screen_width=tgetnum("co");
|
|
|
|
screen_height=tgetnum("li");
|
|
|
|
if(screen_width<1 || screen_width>255) screen_width=80;
|
|
|
|
if(screen_height<1 || screen_height>255) screen_height=24;
|
2008-08-13 02:54:56 +00:00
|
|
|
erase_to_end_of_line= tgetstr("ce",&term_p);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
termcap_add("kP",KEY_PGUP);
|
|
|
|
termcap_add("kN",KEY_PGDWN);
|
|
|
|
termcap_add("kh",KEY_HOME);
|
|
|
|
termcap_add("kH",KEY_END);
|
|
|
|
termcap_add("kI",KEY_INS);
|
|
|
|
termcap_add("kD",KEY_DEL);
|
|
|
|
termcap_add("kb",KEY_BS);
|
|
|
|
termcap_add("kl",KEY_LEFT);
|
|
|
|
termcap_add("kd",KEY_DOWN);
|
|
|
|
termcap_add("ku",KEY_UP);
|
|
|
|
termcap_add("kr",KEY_RIGHT);
|
|
|
|
termcap_add("k0",KEY_F+0);
|
|
|
|
termcap_add("k1",KEY_F+1);
|
|
|
|
termcap_add("k2",KEY_F+2);
|
|
|
|
termcap_add("k3",KEY_F+3);
|
|
|
|
termcap_add("k4",KEY_F+4);
|
|
|
|
termcap_add("k5",KEY_F+5);
|
|
|
|
termcap_add("k6",KEY_F+6);
|
|
|
|
termcap_add("k7",KEY_F+7);
|
|
|
|
termcap_add("k8",KEY_F+8);
|
|
|
|
termcap_add("k9",KEY_F+9);
|
|
|
|
termcap_add("k;",KEY_F+10);
|
|
|
|
return getch2_key_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2006-02-09 14:08:03 +00:00
|
|
|
void get_screen_size(void){
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_IOCTL
|
2001-02-24 20:28:24 +00:00
|
|
|
struct winsize ws;
|
|
|
|
if (ioctl(0, TIOCGWINSZ, &ws) < 0 || !ws.ws_row || !ws.ws_col) return;
|
|
|
|
/* printf("Using IOCTL\n"); */
|
|
|
|
screen_width=ws.ws_col;
|
|
|
|
screen_height=ws.ws_row;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-04-06 11:24:26 +00:00
|
|
|
bool getch2(struct mp_fifo *fifo)
|
2007-08-25 04:28:08 +00:00
|
|
|
{
|
2007-08-25 04:28:09 +00:00
|
|
|
int retval = read(0, &getch2_buf[getch2_len], BUF_LEN-getch2_len);
|
2012-04-06 11:24:26 +00:00
|
|
|
/* Return false on EOF to stop running select() on the FD, as it'd
|
|
|
|
* trigger all the time. Note that it's possible to get temporary
|
|
|
|
* EOF on terminal if the user presses ctrl-d, but that shouldn't
|
|
|
|
* happen if the terminal state change done in getch2_enable()
|
|
|
|
* works.
|
|
|
|
*/
|
2007-08-25 04:28:08 +00:00
|
|
|
if (retval < 1)
|
2012-04-06 11:24:26 +00:00
|
|
|
return retval;
|
2007-08-25 04:28:09 +00:00
|
|
|
getch2_len += retval;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2007-08-25 04:28:08 +00:00
|
|
|
while (getch2_len > 0 && (getch2_len > 1 || getch2_buf[0] != 27)) {
|
2007-08-25 04:28:09 +00:00
|
|
|
int i, len, code;
|
|
|
|
|
|
|
|
/* First find in the TERMCAP database: */
|
|
|
|
for (i = 0; i < getch2_key_db; i++) {
|
|
|
|
if ((len = getch2_keys[i].len) <= getch2_len)
|
|
|
|
if(memcmp(getch2_keys[i].chars, getch2_buf, len) == 0) {
|
|
|
|
code = getch2_keys[i].code;
|
|
|
|
goto found;
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2007-08-25 04:28:09 +00:00
|
|
|
/* We always match some keypress here, with length 1 if nothing else.
|
|
|
|
* Since some of the cases explicitly test remaining buffer length
|
|
|
|
* having a keycode only partially read in the buffer could incorrectly
|
|
|
|
* use the first byte as an independent character.
|
|
|
|
* However the buffer is big enough that this shouldn't happen too
|
|
|
|
* easily, and it's been this way for years without many complaints.
|
|
|
|
* I see no simple fix as there's no easy test which would tell
|
|
|
|
* whether a string must be part of a longer keycode. */
|
|
|
|
len = 1;
|
|
|
|
code = getch2_buf[0];
|
|
|
|
/* Check the well-known codes... */
|
|
|
|
if (code != 27) {
|
|
|
|
if (code == 'A'-64) code = KEY_HOME;
|
|
|
|
else if (code == 'E'-64) code = KEY_END;
|
|
|
|
else if (code == 'D'-64) code = KEY_DEL;
|
|
|
|
else if (code == 'H'-64) code = KEY_BS;
|
|
|
|
else if (code == 'U'-64) code = KEY_PGUP;
|
|
|
|
else if (code == 'V'-64) code = KEY_PGDWN;
|
|
|
|
else if (code == 8 || code==127) code = KEY_BS;
|
|
|
|
else if (code == 10 || code==13) {
|
|
|
|
if (getch2_len > 1) {
|
|
|
|
int c = getch2_buf[1];
|
|
|
|
if ((c == 10 || c == 13) && (c != code))
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
code = KEY_ENTER;
|
2012-02-19 15:51:11 +00:00
|
|
|
} else {
|
|
|
|
int utf8len = bstr_parse_utf8_code_length(code);
|
|
|
|
if (utf8len > 0 && utf8len <= getch2_len) {
|
|
|
|
struct bstr s = { getch2_buf, utf8len };
|
|
|
|
int unicode = bstr_decode_utf8(s, NULL);
|
|
|
|
if (unicode > 0) {
|
|
|
|
len = utf8len;
|
|
|
|
code = unicode;
|
|
|
|
}
|
2012-01-13 06:59:21 +00:00
|
|
|
}
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2007-08-25 04:28:09 +00:00
|
|
|
else if (getch2_len > 1) {
|
|
|
|
int c = getch2_buf[1];
|
|
|
|
if (c == 27) {
|
|
|
|
code = KEY_ESC;
|
|
|
|
len = 2;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
code = c-'0'+KEY_F;
|
|
|
|
len = 2;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
if (getch2_len >= 4 && c == '[' && getch2_buf[2] == '[') {
|
|
|
|
int c = getch2_buf[3];
|
|
|
|
if (c >= 'A' && c < 'A'+12) {
|
|
|
|
code = KEY_F+1 + c-'A';
|
|
|
|
len = 4;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
2007-09-15 18:13:56 +00:00
|
|
|
if ((c == '[' || c == 'O') && getch2_len >= 3) {
|
2007-08-25 04:28:09 +00:00
|
|
|
int c = getch2_buf[2];
|
2012-01-14 13:09:26 +00:00
|
|
|
const int ctable[] = {
|
2007-08-25 04:28:09 +00:00
|
|
|
KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, 0,
|
|
|
|
KEY_END, KEY_PGDWN, KEY_HOME, KEY_PGUP, 0, 0, KEY_INS, 0, 0, 0,
|
|
|
|
KEY_F+1, KEY_F+2, KEY_F+3, KEY_F+4};
|
|
|
|
if (c >= 'A' && c <= 'S')
|
|
|
|
if (ctable[c - 'A']) {
|
|
|
|
code = ctable[c - 'A'];
|
|
|
|
len = 3;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (getch2_len >= 4 && c == '[' && getch2_buf[3] == '~') {
|
|
|
|
int c = getch2_buf[2];
|
|
|
|
const int ctable[8] = {KEY_HOME, KEY_INS, KEY_DEL, KEY_END, KEY_PGUP, KEY_PGDWN, KEY_HOME, KEY_END};
|
|
|
|
if (c >= '1' && c <= '8') {
|
|
|
|
code = ctable[c - '1'];
|
|
|
|
len = 4;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (getch2_len >= 5 && c == '[' && getch2_buf[4] == '~') {
|
|
|
|
int i = getch2_buf[2] - '0';
|
|
|
|
int j = getch2_buf[3] - '0';
|
|
|
|
if (i >= 0 && i <= 9 && j >= 0 && j <= 9) {
|
|
|
|
const short ftable[20] = {
|
|
|
|
11,12,13,14,15, 17,18,19,20,21,
|
|
|
|
23,24,25,26,28, 29,31,32,33,34 };
|
|
|
|
int a = i*10 + j;
|
|
|
|
for (i = 0; i < 20; i++)
|
|
|
|
if (ftable[i] == a) {
|
|
|
|
code = KEY_F+1 + i;
|
|
|
|
len = 5;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2007-08-25 04:28:09 +00:00
|
|
|
found:
|
|
|
|
getch2_len -= len;
|
|
|
|
for (i = 0; i < getch2_len; i++)
|
|
|
|
getch2_buf[i] = getch2_buf[len+i];
|
2008-04-29 12:12:19 +00:00
|
|
|
mplayer_put_key(fifo, code);
|
2007-08-25 04:28:08 +00:00
|
|
|
}
|
2012-04-06 11:24:26 +00:00
|
|
|
return true;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2012-02-25 14:05:12 +00:00
|
|
|
static volatile int getch2_status=0;
|
2001-08-22 19:43:23 +00:00
|
|
|
|
2012-02-25 14:05:12 +00:00
|
|
|
static void do_enable_getch2(void)
|
|
|
|
{
|
2001-11-19 17:03:38 +00:00
|
|
|
#ifdef HAVE_TERMIOS
|
2012-02-25 14:05:12 +00:00
|
|
|
struct termios tio_new;
|
|
|
|
tcgetattr(0,&tio_new);
|
2001-02-24 20:28:24 +00:00
|
|
|
tio_new.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
|
2012-09-17 09:12:51 +00:00
|
|
|
tio_new.c_cc[VMIN] = 1;
|
2001-02-24 20:28:24 +00:00
|
|
|
tio_new.c_cc[VTIME] = 0;
|
2001-06-05 18:40:44 +00:00
|
|
|
tcsetattr(0,TCSANOW,&tio_new);
|
2012-02-25 14:05:12 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void continue_sighandler(int signum)
|
|
|
|
{
|
|
|
|
if (getch2_status)
|
|
|
|
do_enable_getch2();
|
|
|
|
}
|
|
|
|
|
|
|
|
void getch2_enable(void){
|
|
|
|
#ifdef HAVE_TERMIOS
|
|
|
|
tcgetattr(0,&tio_orig);
|
|
|
|
do_enable_getch2();
|
2001-06-03 00:31:41 +00:00
|
|
|
#endif
|
2001-08-22 19:43:23 +00:00
|
|
|
getch2_status=1;
|
2012-02-25 14:05:12 +00:00
|
|
|
signal(SIGCONT,continue_sighandler);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2006-02-09 14:08:03 +00:00
|
|
|
void getch2_disable(void){
|
2001-08-22 19:43:23 +00:00
|
|
|
if(!getch2_status) return; // already disabled / never enabled
|
2012-02-25 14:05:12 +00:00
|
|
|
getch2_status=0;
|
2001-11-19 17:03:38 +00:00
|
|
|
#ifdef HAVE_TERMIOS
|
2001-06-05 18:40:44 +00:00
|
|
|
tcsetattr(0,TCSANOW,&tio_orig);
|
2001-06-03 00:31:41 +00:00
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|