Skip to content

Commit

Permalink
Merge branch 'session-cleanup' of github.com:Alexander--/Android-Term…
Browse files Browse the repository at this point in the history
…inal-Emulator into Alexander---session-cleanup
  • Loading branch information
jackpal committed Apr 14, 2015
2 parents 6a6e116 + 8a8c017 commit e68b375
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 237 deletions.
16 changes: 16 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
task wrapper (type:Wrapper) {
gradleVersion = '2.2.1'
distributionUrl = 'https://services.gradle.org/distributions/gradle-2.2.1-all.zip'
}

buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.0'
}
}

Expand All @@ -13,3 +20,12 @@ allprojects {
jcenter()
}
}

subprojects {
def androidHome

if ((androidHome = System.env.'ANDROID_HOME')
&& (androidHome = androidHome as File).exists()
&& androidHome.canWrite())
apply plugin: 'android-sdk-manager'
}
16 changes: 15 additions & 1 deletion term/src/main/java/jackpal/androidterm/BoundSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import android.text.TextUtils;
import jackpal.androidterm.util.TermSettings;

public class BoundSession extends GenericTermSession {
class BoundSession extends GenericTermSession {
private final String issuerTitle;

private boolean fullyInitialized;

BoundSession(ParcelFileDescriptor ptmxFd, TermSettings settings, String issuerTitle) {
super(ptmxFd, settings, true);

Expand All @@ -24,4 +26,16 @@ public String getTitle() {
? issuerTitle
: issuerTitle + " — " + extraTitle;
}

@Override
public void initializeEmulator(int columns, int rows) {
super.initializeEmulator(columns, rows);

fullyInitialized = true;
}

@Override
boolean isFailFast() {
return !fullyInitialized;
}
}
84 changes: 2 additions & 82 deletions term/src/main/java/jackpal/androidterm/Exec.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,88 +36,8 @@ public class Exec
System.loadLibrary("jackpal-androidterm5");
}

private static Field descriptorField;
static native void setPtyWindowSizeInternal(int fd, int row, int col, int xpixel, int ypixel) throws IOException;

private static void cacheDescField() throws NoSuchFieldException {
if (descriptorField != null)
return;

descriptorField = FileDescriptor.class.getDeclaredField("descriptor");
descriptorField.setAccessible(true);
}

private static int getIntFd(ParcelFileDescriptor parcelFd) throws IOException {
if (Build.VERSION.SDK_INT >= 12)
return FdHelperHoneycomb.getFd(parcelFd);
else {
try {
cacheDescField();

return descriptorField.getInt(parcelFd.getFileDescriptor());
} catch (Exception e) {
throw new IOException("Unable to obtain file descriptor on this OS version: " + e.getMessage());
}
}
}

/**
* Set the widow size for a given pty. Allows programs
* connected to the pty learn how large their screen is.
*/
public static void setPtyWindowSize(ParcelFileDescriptor fd, int row, int col, int xpixel, int ypixel) {
// If the tty goes away too quickly, this may get called after it's descriptor is closed
if (!fd.getFileDescriptor().valid())
return;

try {
setPtyWindowSizeInternal(getIntFd(fd), row, col, xpixel, ypixel);
} catch (IOException e) {
// pretend that everything is ok...
Log.e("exec", "Failed to set window size due to " + e.getMessage());
}
}

/**
* Set or clear UTF-8 mode for a given pty. Used by the terminal driver
* to implement correct erase behavior in cooked mode (Linux >= 2.6.4).
*/
public static void setPtyUTF8Mode(ParcelFileDescriptor fd, boolean utf8Mode) {
// If the tty goes away too quickly, this may get called after it's descriptor is closed
if (!fd.getFileDescriptor().valid())
return;

try {
setPtyUTF8ModeInternal(getIntFd(fd), utf8Mode);
} catch (IOException e) {
// pretend that everything is ok...
Log.e("exec", "Failed to set UTF mode due to " + e.getMessage());
}
}

/**
* Close a given file descriptor.
*/
public static void close(ParcelFileDescriptor fd) {
try {
fd.close();
} catch (IOException e) {
// ok
}
}

/**
* Send SIGHUP to a process group, SIGHUP notifies a terminal client, that the terminal have been disconnected,
* and usually results in client's death, unless it's process is a daemon or have been somehow else detached
* from the terminal (for example, by the "nohup" utility).
*/
public static void hangupProcessGroup(int processId) {
TermExec.sendSignal(-processId, 1);
}

private static native void setPtyWindowSizeInternal(int fd, int row, int col, int xpixel, int ypixel)
throws IOException;

private static native void setPtyUTF8ModeInternal(int fd, boolean utf8Mode)
throws IOException;
static native void setPtyUTF8ModeInternal(int fd, boolean utf8Mode) throws IOException;
}

97 changes: 91 additions & 6 deletions term/src/main/java/jackpal/androidterm/GenericTermSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package jackpal.androidterm;

import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;

import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
Expand All @@ -35,10 +37,14 @@
* A terminal session, consisting of a TerminalEmulator, a TranscriptScreen,
* and the I/O streams used to talk to the process.
*/
public class GenericTermSession extends TermSession {
class GenericTermSession extends TermSession {
//** Set to true to force into 80 x 24 for testing with vttest. */
private static final boolean VTTEST_MODE = false;

private static Field descriptorField;

private final long createdAt;

// A cookie which uniquely identifies this session.
private String mHandle;

Expand All @@ -53,7 +59,7 @@ public class GenericTermSession extends TermSession {

private UpdateCallback mUTF8ModeNotify = new UpdateCallback() {
public void onUpdate() {
Exec.setPtyUTF8Mode(mTermFd, getUTF8Mode());
setPtyUTF8Mode(getUTF8Mode());
}
};

Expand All @@ -62,6 +68,8 @@ public void onUpdate() {

this.mTermFd = mTermFd;

this.createdAt = System.currentTimeMillis();

updatePrefs(settings);
}

Expand All @@ -79,7 +87,7 @@ public void initializeEmulator(int columns, int rows) {
}
super.initializeEmulator(columns, rows);

Exec.setPtyUTF8Mode(mTermFd, getUTF8Mode());
setPtyUTF8Mode(getUTF8Mode());
setUTF8ModeUpdateCallback(mUTF8ModeNotify);
}

Expand All @@ -90,7 +98,7 @@ public void updateSize(int columns, int rows) {
rows = 24;
}
// Inform the attached pty of our new size:
Exec.setPtyWindowSize(mTermFd, rows, columns, 0, 0);
setPtyWindowSize(rows, columns, 0, 0);
super.updateSize(columns, rows);
}

Expand All @@ -117,7 +125,12 @@ protected void onProcessExit() {

@Override
public void finish() {
Exec.close(mTermFd);
try {
mTermFd.close();
} catch (IOException e) {
// ok
}

super.finish();
}

Expand All @@ -130,7 +143,7 @@ public void finish() {
* unset or an empty string.
*/
public String getTitle(String defaultTitle) {
String title = super.getTitle();
String title = getTitle();
if (title != null && title.length() > 0) {
return title;
} else {
Expand All @@ -148,4 +161,76 @@ public void setHandle(String handle) {
public String getHandle() {
return mHandle;
}

@Override
public String toString() {
return getClass().getSimpleName() + '(' + createdAt + ',' + mHandle + ')';
}

/**
* Set the widow size for a given pty. Allows programs
* connected to the pty learn how large their screen is.
*/
void setPtyWindowSize(int row, int col, int xpixel, int ypixel) {
// If the tty goes away too quickly, this may get called after it's descriptor is closed
if (!mTermFd.getFileDescriptor().valid())
return;

try {
Exec.setPtyWindowSizeInternal(getIntFd(mTermFd), row, col, xpixel, ypixel);
} catch (IOException e) {
Log.e("exec", "Failed to set window size: " + e.getMessage());

if (isFailFast())
throw new IllegalStateException(e);
}
}

/**
* Set or clear UTF-8 mode for a given pty. Used by the terminal driver
* to implement correct erase behavior in cooked mode (Linux >= 2.6.4).
*/
void setPtyUTF8Mode(boolean utf8Mode) {
// If the tty goes away too quickly, this may get called after it's descriptor is closed
if (!mTermFd.getFileDescriptor().valid())
return;

try {
Exec.setPtyUTF8ModeInternal(getIntFd(mTermFd), utf8Mode);
} catch (IOException e) {
Log.e("exec", "Failed to set UTF mode: " + e.getMessage());

if (isFailFast())
throw new IllegalStateException(e);
}
}

/**
* @return true, if failing to operate on file descriptor deserves an exception (never the case for ATE own shell)
*/
boolean isFailFast() {
return false;
}

private static void cacheDescField() throws NoSuchFieldException {
if (descriptorField != null)
return;

descriptorField = FileDescriptor.class.getDeclaredField("descriptor");
descriptorField.setAccessible(true);
}

private static int getIntFd(ParcelFileDescriptor parcelFd) throws IOException {
if (Build.VERSION.SDK_INT >= 12)
return FdHelperHoneycomb.getFd(parcelFd);
else {
try {
cacheDescField();

return descriptorField.getInt(parcelFd.getFileDescriptor());
} catch (Exception e) {
throw new IOException("Unable to obtain file descriptor on this OS version: " + e.getMessage());
}
}
}
}
11 changes: 10 additions & 1 deletion term/src/main/java/jackpal/androidterm/ShellTermSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,16 @@ private void onProcessExit(int result) {

@Override
public void finish() {
Exec.hangupProcessGroup(mProcId);
hangupProcessGroup();
super.finish();
}

/**
* Send SIGHUP to a process group, SIGHUP notifies a terminal client, that the terminal have been disconnected,
* and usually results in client's death, unless it's process is a daemon or have been somehow else detached
* from the terminal (for example, by the "nohup" utility).
*/
void hangupProcessGroup() {
TermExec.sendSignal(-mProcId, 1);
}
}
Loading

0 comments on commit e68b375

Please sign in to comment.