diff options
author | arpi_esp <arpi_esp@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2001-02-24 20:28:24 +0000 |
---|---|---|
committer | arpi_esp <arpi_esp@b3059339-0415-0410-9bf9-f77b7e298cf2> | 2001-02-24 20:28:24 +0000 |
commit | d34041569e71fc9bd772354e94dc9d16061072a5 (patch) | |
tree | 8f481cae1c70f32d1756fbe5f39000577b73042d /linux | |
parent | e95a95ece09bac96bdfd37322f96c6f57ef79ebc (diff) | |
download | mpv-d34041569e71fc9bd772354e94dc9d16061072a5.tar.bz2 mpv-d34041569e71fc9bd772354e94dc9d16061072a5.tar.xz |
Initial revision
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@2 b3059339-0415-0410-9bf9-f77b7e298cf2
Diffstat (limited to 'linux')
-rw-r--r-- | linux/getch2.c | 215 | ||||
-rw-r--r-- | linux/getch2.h | 20 | ||||
-rw-r--r-- | linux/keycodes.h | 36 | ||||
-rw-r--r-- | linux/shmem.c | 83 | ||||
-rw-r--r-- | linux/shmem.h | 4 | ||||
-rw-r--r-- | linux/timer-lx.c | 49 | ||||
-rw-r--r-- | linux/timer.h | 6 |
7 files changed, 413 insertions, 0 deletions
diff --git a/linux/getch2.c b/linux/getch2.c new file mode 100644 index 0000000000..628d225170 --- /dev/null +++ b/linux/getch2.c @@ -0,0 +1,215 @@ +/* GyS-TermIO v2.0 (for GySmail v3) (C) 1999 A'rpi/ESP-team */ + +#include "../config.h" + +//#define USE_TERMCAP +#define USE_IOCTL + +#define MAX_KEYS 64 +#define BUF_LEN 256 + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <sys/types.h> +#ifdef USE_IOCTL +#include <sys/ioctl.h> +#endif +#include <sys/termios.h> +#include <unistd.h> + +#include "keycodes.h" + +static struct termios tio_orig; +static int getch2_len=0; +static char getch2_buf[BUF_LEN]; + +int screen_width=80; +int screen_height=24; + +typedef struct { + int len; + int code; + char chars[8]; +} keycode_st; +static keycode_st getch2_keys[MAX_KEYS]; +static int getch2_key_db=0; + +#ifdef USE_TERMCAP + +#if 0 +#include <termcap.h> +#else + extern int tgetent (char *BUFFER, char *TERMTYPE); + extern int tgetnum (char *NAME); + extern int tgetflag (char *NAME); + extern char *tgetstr (char *NAME, char **AREA); +#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"); + 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; + + 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 + +void get_screen_size(){ +#ifdef USE_IOCTL + 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 +} + +int getch2(int time){ + int len=0; + int code=0; + int i=0; + + while(!getch2_len || (getch2_len==1 && getch2_buf[0]==27)){ + fd_set rfds; + struct timeval tv; + int retval; + /* Watch stdin (fd 0) to see when it has input. */ + FD_ZERO(&rfds); FD_SET(0,&rfds); + /* Wait up to 'time' microseconds. */ + tv.tv_sec=time/1000; tv.tv_usec = (time%1000)*1000; + retval=select(1, &rfds, NULL, NULL, &tv); + if(!retval) return -1; + /* Data is available now. */ + retval=read(0,&getch2_buf[getch2_len],BUF_LEN-getch2_len); + if(retval<1) return -1; + getch2_len+=retval; + } + + /* 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; + } + } + len=1;code=getch2_buf[0]; + /* Check the well-known codes... */ + if(code!=27){ + if(code=='A'-64){ code=KEY_HOME; goto found;} + if(code=='E'-64){ code=KEY_END; goto found;} + if(code=='D'-64){ code=KEY_DEL; goto found;} + if(code=='H'-64){ code=KEY_BS; goto found;} + if(code=='U'-64){ code=KEY_PGUP; goto found;} + if(code=='V'-64){ code=KEY_PGDWN; goto found;} + if(code==8 || code==127){ code=KEY_BS; goto found;} + if(code==10 || code==13){ + if(getch2_len>1){ + int c=getch2_buf[1]; + if(c==10 || c==13) if(c!=code) len=2; + } + code=KEY_ENTER; + goto found; + } + } 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;} + } + if(c=='[' || c=='O') if(getch2_len>=3){ + int c=getch2_buf[2]; + static short int ctable[]={ 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]; + 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){ + static short int 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;} + } + } + } +found: + if((getch2_len-=len)>0){ + int i; + for(i=0;i<getch2_len;i++) getch2_buf[i]=getch2_buf[len+i]; + } + return code; +} + +void getch2_enable(){ +struct termios tio_new; + ioctl(0,TCGETS,&tio_orig); /* tcgetattr(0,&tio_orig); */ + tio_new=tio_orig; + tio_new.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */ + tio_new.c_cc[VMIN] = 1; + tio_new.c_cc[VTIME] = 0; + ioctl(0,TCSETS,&tio_new); /* tcsetattr(0,TCSANOW,&tio_new); */ +} + +void getch2_disable(){ + ioctl(0,TCSETS,&tio_orig); /* tcsetattr(0,TCSANOW,&tio_orig); */ +} + diff --git a/linux/getch2.h b/linux/getch2.h new file mode 100644 index 0000000000..016de5ae9e --- /dev/null +++ b/linux/getch2.h @@ -0,0 +1,20 @@ +/* GyS-TermIO v2.0 (for GySmail v3) (C) 1999 A'rpi/ESP-team */ +/* a very small replacement of ncurses library */ + +/* Screen size. Initialized by load_termcap() and get_screen_size() */ +extern int screen_width; +extern int screen_height; + +/* Get screen-size using IOCTL call. */ +extern void get_screen_size(); + +/* Load key definitions from the TERMCAP database. 'termtype' can be NULL */ +extern int load_termcap(char *termtype); + +/* Enable and disable STDIN line-buffering */ +extern void getch2_enable(); +extern void getch2_disable(); + +/* Read a character or a special key code (see keycodes.h) */ +extern int getch2(int halfdelay_time); + diff --git a/linux/keycodes.h b/linux/keycodes.h new file mode 100644 index 0000000000..e0b963535c --- /dev/null +++ b/linux/keycodes.h @@ -0,0 +1,36 @@ +/* KEY code definitions for GyS-TermIO v2.0 (C) 1999 A'rpi/ESP-team */ + +#define KEY_ENTER 13 +#define KEY_TAB 9 + +#define KEY_BASE 0x100 + +/* Function keys */ +#define KEY_F (KEY_BASE+64) + +/* Control keys */ +#define KEY_CTRL (KEY_BASE) +#define KEY_BACKSPACE (KEY_CTRL+0) +#define KEY_DELETE (KEY_CTRL+1) +#define KEY_INSERT (KEY_CTRL+2) +#define KEY_HOME (KEY_CTRL+3) +#define KEY_END (KEY_CTRL+4) +#define KEY_PAGE_UP (KEY_CTRL+5) +#define KEY_PAGE_DOWN (KEY_CTRL+6) +#define KEY_ESC (KEY_CTRL+7) + +/* Control keys short name */ +#define KEY_BS KEY_BACKSPACE +#define KEY_DEL KEY_DELETE +#define KEY_INS KEY_INSERT +#define KEY_PGUP KEY_PAGE_UP +#define KEY_PGDOWN KEY_PAGE_DOWN +#define KEY_PGDWN KEY_PAGE_DOWN + +/* Cursor movement */ +#define KEY_CRSR (KEY_BASE+16) +#define KEY_RIGHT (KEY_CRSR+0) +#define KEY_LEFT (KEY_CRSR+1) +#define KEY_DOWN (KEY_CRSR+2) +#define KEY_UP (KEY_CRSR+3) + diff --git a/linux/shmem.c b/linux/shmem.c new file mode 100644 index 0000000000..7563175fb0 --- /dev/null +++ b/linux/shmem.c @@ -0,0 +1,83 @@ +/* + * shmem.c - Shared memory allocation + * + * based on mpg123's xfermem.c by + * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de> + * Sun Apr 6 02:26:26 MET DST 1997 + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/time.h> +#include <sys/uio.h> +#include <sys/mman.h> +#include <sys/socket.h> +#include <fcntl.h> + +#ifdef AIX +#include <sys/select.h> +#endif + +#include <sys/ipc.h> +#include <sys/shm.h> + +extern int errno; + +#if defined(MAP_ANONYMOUS) && !defined(MAP_ANON) +#define MAP_ANON MAP_ANONYMOUS +#endif + +static int shmem_type=0; + +void* shmem_alloc(int size){ +void* p; +int devzero; +while(1){ + switch(shmem_type){ + case 0: // ========= MAP_ANON|MAP_SHARED ========== + p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0); + if(p==MAP_FAILED) break; // failed + printf("shmem: %d bytes allocated using mmap anon (%X)\n",size,p); + return p; + case 1: // ========= MAP_SHARED + /dev/zero ========== + if ((devzero = open("/dev/zero", O_RDWR, 0)) == -1) break; + p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0); + if(p==MAP_FAILED) break; // failed + printf("shmem: %d bytes allocated using mmap /dev/zero (%X)\n",size,p); + return p; + case 2: { // ========= shmget() ========== + struct shmid_ds shmemds; + int shmemid; + if ((shmemid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600)) == -1) break; + if ((int)(p = shmat(shmemid, 0, 0)) == -1){ + perror ("shmat()"); + shmctl (shmemid, IPC_RMID, &shmemds); + break; + } + if (shmctl(shmemid, IPC_RMID, &shmemds) == -1) { + perror ("shmctl()"); + if (shmdt(p) == -1) perror ("shmdt()"); + break; + } + printf("shmem: %d bytes allocated using shmget() & shmat() (%X)\n",size,p); + return p; + } + default: + printf("FATAL: Cannot alloate %d bytes shared memory :(\n",size); + return NULL; + } + ++shmem_type; +} +} + +void shmem_free(void* p){ + switch(shmem_type){ + case 2: + if (shmdt(p) == -1) perror ("shmdt()"); + break; + } +} diff --git a/linux/shmem.h b/linux/shmem.h new file mode 100644 index 0000000000..58ceee4e44 --- /dev/null +++ b/linux/shmem.h @@ -0,0 +1,4 @@ + +void* shmem_alloc(int size); +void shmem_free(void* p); + diff --git a/linux/timer-lx.c b/linux/timer-lx.c new file mode 100644 index 0000000000..0460aaa024 --- /dev/null +++ b/linux/timer-lx.c @@ -0,0 +1,49 @@ +// Precise timer routines for LINUX (C) LGB & A'rpi/ASTRAL + +#include <unistd.h> +#include <sys/time.h> + +// Returns current time in seconds +unsigned int GetTimer(){ + struct timeval tv; + struct timezone tz; +// float s; + gettimeofday(&tv,&tz); +// s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec; + return (tv.tv_sec*1000000+tv.tv_usec); +} + +// Returns current time in microseconds +int uGetTimer(){ + struct timeval tv; + struct timezone tz; + gettimeofday(&tv,&tz); + return (int)(tv.tv_usec+1000000*tv.tv_sec); +} + +static unsigned int RelativeTime=0; + +// Returns time spent between now and last call in seconds +float GetRelativeTime(){ +unsigned int t,r; + t=GetTimer(); +// t*=16;printf("time=%ud\n",t); + r=t-RelativeTime; + RelativeTime=t; + return (float)r * 0.000001F; +} + +// Initialize timer, must be called at least once at start +void InitTimer(){ + GetRelativeTime(); +} + + +#if 0 +void main(){ + float t=0; + InitTimer(); + while(1){ t+=GetRelativeTime();printf("time= %10.6f\r",t);fflush(stdout); } +} +#endif + diff --git a/linux/timer.h b/linux/timer.h new file mode 100644 index 0000000000..8348a6cf7b --- /dev/null +++ b/linux/timer.h @@ -0,0 +1,6 @@ + +void InitTimer(); +int GetTimer(); +//int uGetTimer(); +float GetRelativeTime(); + |