Split the c++ source files into separate source sets for shared, lib and curses

This commit is contained in:
Adam Murdoch
2013-12-12 12:21:44 +11:00
parent 87e120b73a
commit a6e825464e
5 changed files with 84 additions and 84 deletions

View File

@@ -1,200 +0,0 @@
/*
* Copyright 2012 Adam Murdoch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Curses functions
*/
#ifndef WIN32
#include "native.h"
#include "generic.h"
#include <unistd.h>
#include <stdlib.h>
#include <curses.h>
#include <term.h>
#define NORMAL_TEXT 0
#define BRIGHT_TEXT 1
#define FOREGROUND_COLOR 2
#define CURSOR_UP 3
#define CURSOR_DOWN 4
#define CURSOR_LEFT 5
#define CURSOR_RIGHT 6
#define CURSOR_START_LINE 7
#define CLEAR_END_OF_LINE 8
#ifdef SOLARIS
#define TERMINAL_CHAR_TYPE char
#else
#define TERMINAL_CHAR_TYPE int
#endif
int current_terminal = -1;
const char* terminal_capabilities[9];
int write_to_terminal(TERMINAL_CHAR_TYPE ch) {
write(current_terminal, &ch, 1);
}
const char* getcap(const char* capability) {
return tgetstr((char*)capability, NULL);
}
void write_capability(JNIEnv *env, const char* capability, jobject result) {
if (capability == NULL) {
mark_failed_with_message(env, "unknown terminal capability", result);
return;
}
if (tputs((char*)capability, 1, write_to_terminal) == ERR) {
mark_failed_with_message(env, "could not write to terminal", result);
return;
}
}
void write_param_capability(JNIEnv *env, const char* capability, int count, jobject result) {
if (capability == NULL) {
mark_failed_with_message(env, "unknown terminal capability", result);
return;
}
capability = tparm((char*)capability, count, 0, 0, 0, 0, 0, 0, 0, 0);
if (capability == NULL) {
mark_failed_with_message(env, "could not format terminal capability string", result);
return;
}
if (tputs((char*)capability, 1, write_to_terminal) == ERR) {
mark_failed_with_message(env, "could not write to terminal", result);
return;
}
}
JNIEXPORT jint JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_getVersion(JNIEnv *env, jclass target) {
return NATIVE_VERSION;
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_initTerminal(JNIEnv *env, jclass target, jint output, jobject capabilities, jobject result) {
if (!isatty(output+1)) {
mark_failed_with_message(env, "not a terminal", result);
return;
}
if (current_terminal < 0) {
char* termType = getenv("TERM");
if (termType == NULL) {
mark_failed_with_message(env, "$TERM not set", result);
return;
}
int retval = tgetent(NULL, termType);
if (retval != 1) {
mark_failed_with_message(env, "could not get termcap entry", result);
return;
}
jclass destClass = env->GetObjectClass(capabilities);
jfieldID field = env->GetFieldID(destClass, "terminalName", "Ljava/lang/String;");
jstring jtermType = char_to_java(env, termType, result);
env->SetObjectField(capabilities, field, jtermType);
// Text attributes
terminal_capabilities[NORMAL_TEXT] = getcap("me");
terminal_capabilities[BRIGHT_TEXT] = getcap("md");
field = env->GetFieldID(destClass, "textAttributes", "Z");
env->SetBooleanField(capabilities, field, terminal_capabilities[NORMAL_TEXT] != NULL && terminal_capabilities[BRIGHT_TEXT] != NULL);
// Colors
terminal_capabilities[FOREGROUND_COLOR] = getcap("AF");
field = env->GetFieldID(destClass, "colors", "Z");
env->SetBooleanField(capabilities, field, terminal_capabilities[FOREGROUND_COLOR] != NULL);
// Cursor motion
terminal_capabilities[CURSOR_UP] = getcap("up");
terminal_capabilities[CURSOR_DOWN] = getcap("do");
terminal_capabilities[CURSOR_LEFT] = getcap("le");
terminal_capabilities[CURSOR_RIGHT] = getcap("nd");
terminal_capabilities[CURSOR_START_LINE] = getcap("cr");
terminal_capabilities[CLEAR_END_OF_LINE] = getcap("ce");
field = env->GetFieldID(destClass, "cursorMotion", "Z");
env->SetBooleanField(capabilities, field, terminal_capabilities[CURSOR_UP] != NULL
&& terminal_capabilities[CURSOR_DOWN] != NULL
&& terminal_capabilities[CURSOR_RIGHT] != NULL
&& terminal_capabilities[CURSOR_LEFT] != NULL
&& terminal_capabilities[CURSOR_START_LINE] != NULL
&& terminal_capabilities[CLEAR_END_OF_LINE] != NULL);
}
current_terminal = output + 1;
if (terminal_capabilities[NORMAL_TEXT] != NULL) {
write_capability(env, terminal_capabilities[NORMAL_TEXT], result);
}
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_bold(JNIEnv *env, jclass target, jobject result) {
write_capability(env, terminal_capabilities[BRIGHT_TEXT], result);
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_reset(JNIEnv *env, jclass target, jobject result) {
if (terminal_capabilities[NORMAL_TEXT] != NULL) {
write_capability(env, terminal_capabilities[NORMAL_TEXT], result);
}
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_foreground(JNIEnv *env, jclass target, jint color, jobject result) {
write_param_capability(env, terminal_capabilities[FOREGROUND_COLOR], color, result);
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_up(JNIEnv *env, jclass target, jint count, jobject result) {
for (jint i = 0; i < count; i++) {
write_capability(env, terminal_capabilities[CURSOR_UP], result);
}
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_down(JNIEnv *env, jclass target, jint count, jobject result) {
for (jint i = 0; i < count; i++) {
write_capability(env, terminal_capabilities[CURSOR_DOWN], result);
}
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_left(JNIEnv *env, jclass target, jint count, jobject result) {
for (jint i = 0; i < count; i++) {
write_capability(env, terminal_capabilities[CURSOR_LEFT], result);
}
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_right(JNIEnv *env, jclass target, jint count, jobject result) {
for (jint i = 0; i < count; i++) {
write_capability(env, terminal_capabilities[CURSOR_RIGHT], result);
}
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_startLine(JNIEnv *env, jclass target, jobject result) {
write_capability(env, terminal_capabilities[CURSOR_START_LINE], result);
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_clearToEndOfLine(JNIEnv *env, jclass target, jobject result) {
write_capability(env, terminal_capabilities[CLEAR_END_OF_LINE], result);
}
#endif

View File

@@ -1,38 +0,0 @@
/*
* Copyright 2012 Adam Murdoch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Generic cross-platform functions.
*/
#include "native.h"
#include "generic.h"
void mark_failed_with_message(JNIEnv *env, const char* message, jobject result) {
mark_failed_with_code(env, message, 0, NULL, result);
}
void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, const char* error_code_message, jobject result) {
jclass destClass = env->GetObjectClass(result);
jmethodID method = env->GetMethodID(destClass, "failed", "(Ljava/lang/String;ILjava/lang/String;)V");
jstring message_str = env->NewStringUTF(message);
jstring error_code_str = error_code_message == NULL ? NULL : env->NewStringUTF(error_code_message);
env->CallVoidMethod(result, method, message_str, error_code, error_code_str);
}
JNIEXPORT jint JNICALL
Java_net_rubygrapefruit_platform_internal_jni_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) {
return NATIVE_VERSION;
}

View File

@@ -1,83 +0,0 @@
/*
* Copyright 2012 Adam Murdoch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* POSIX platform functions.
*/
#ifndef WIN32
#include "native.h"
#include "generic.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <wchar.h>
void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) {
const char * errno_message = NULL;
switch(errno) {
case ENOENT:
errno_message = "ENOENT";
break;
}
mark_failed_with_code(env, message, errno, errno_message, result);
}
char* java_to_char(JNIEnv *env, jstring string, jobject result) {
size_t stringLen = env->GetStringLength(string);
wchar_t* wideString = (wchar_t*)malloc(sizeof(wchar_t) * (stringLen+1));
const jchar* javaString = env->GetStringChars(string, NULL);
for (size_t i = 0; i < stringLen; i++) {
wideString[i] = javaString[i];
}
wideString[stringLen] = L'\0';
env->ReleaseStringChars(string, javaString);
size_t bytes = wcstombs(NULL, wideString, 0);
if (bytes < 0) {
mark_failed_with_message(env, "could not convert string to current locale", result);
free(wideString);
return NULL;
}
char* chars = (char*)malloc(bytes + 1);
wcstombs(chars, wideString, bytes+1);
free(wideString);
return chars;
}
jstring char_to_java(JNIEnv* env, const char* chars, jobject result) {
size_t bytes = strlen(chars);
wchar_t* wideString = (wchar_t*)malloc(sizeof(wchar_t) * (bytes+1));
if (mbstowcs(wideString, chars, bytes+1) < 0) {
mark_failed_with_message(env, "could not convert string from current locale", result);
free(wideString);
return NULL;
}
size_t stringLen = wcslen(wideString);
jchar* javaString = (jchar*)malloc(sizeof(jchar) * stringLen);
for (int i =0; i < stringLen; i++) {
javaString[i] = (jchar)wideString[i];
}
jstring string = env->NewString(javaString, stringLen);
free(wideString);
free(javaString);
return string;
}
#endif

View File

@@ -1,76 +0,0 @@
/*
* Copyright 2012 Adam Murdoch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __INCLUDE_GENERIC_H__
#define __INCLUDE_GENERIC_H__
#include <jni.h>
#ifdef __cplusplus
extern "C" {
#endif
#define NATIVE_VERSION 17
/*
* Marks the given result as failed, using the given error message
*/
extern void mark_failed_with_message(JNIEnv *env, const char* message, jobject result);
/*
* Marks the given result as failed, using the given error message and the current value of errno/GetLastError()
*/
extern void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result);
/*
* Marks the given result as failed, using the given error message and error code
*/
extern void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, const char* error_code_message, jobject result);
/*
* Converts the given Java string to a NULL terminated wchar_str. Should call free() when finished.
*
* Returns NULL on failure.
*/
extern wchar_t*
java_to_wchar(JNIEnv *env, jstring string, jobject result);
/*
* Converts the given wchar_t string to a Java string.
*
* Returns NULL on failure.
*/
extern jstring wchar_to_java(JNIEnv* env, const wchar_t* chars, size_t len, jobject result);
/*
* Converts the given Java string to a NULL terminated char string. Should call free() when finished.
*
* Returns NULL on failure.
*/
extern char* java_to_char(JNIEnv *env, jstring string, jobject result);
/*
* Converts the given NULL terminated char string to a Java string.
*
* Returns NULL on failure.
*/
extern jstring char_to_java(JNIEnv* env, const char* chars, jobject result);
#ifdef __cplusplus
}
#endif
#endif