libConsole/getch.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:12 +0200
changeset 52 1fa566e9d13f
parent 49 77094ec383cf
permissions -rw-r--r--
getch name conflict on windows platform * ::getch works for c++ in db_bridge, not here. committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
#include <stdio.h>
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
#include <jni.h>
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
#include <Console.h>
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
#ifdef WIN32
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <conio.h>
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
52
1fa566e9d13f getch name conflict on windows platform
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 49
diff changeset
     8
int cross_getch ()
49
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
{
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
    int ch = -1;
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
    while (!kbhit ()) {
52
1fa566e9d13f getch name conflict on windows platform
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 49
diff changeset
    13
        ch = getch ();
49
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
    }
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
    return ch;
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
}
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
#else
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
#include <termios.h>
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
52
1fa566e9d13f getch name conflict on windows platform
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 49
diff changeset
    22
int cross_getch ()
49
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
{
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    int ch = -1, fd = 0;
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    struct termios neu, alt;
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    fd = fileno (stdin);
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    tcgetattr (fd, &alt);
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    neu = alt;
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    neu.c_lflag &= ~(ICANON | ECHO);
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    tcsetattr (fd, TCSANOW, &neu);
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    ch = getchar ();
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    tcsetattr (fd, TCSANOW, &alt);
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    return ch;
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
}
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
#endif
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
JNIEXPORT jint JNICALL Java_Console_getch (JNIEnv * env, jclass lass)
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
{
52
1fa566e9d13f getch name conflict on windows platform
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 49
diff changeset
    40
    return cross_getch ();
49
77094ec383cf libConsole: a java native interface example
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
}