OSDN Git Service

Delay allocation of the transcript until after we know the terminal's size
authorSteven Luo <steven+android@steven676.net>
Mon, 23 Jan 2012 09:40:17 +0000 (01:40 -0800)
committerJack Palevich <jackpal@google.com>
Wed, 25 Jan 2012 21:29:43 +0000 (13:29 -0800)
During the modularization for by the multisession patches, the lazy
initialization which used to ensure that the transcript wasn't allocated
until its final size was known was lost.  This results in the transcript
always being allocated at least twice for a newly created session
(unless the final width of the window is exactly 80 columns).

To prevent this, delay allocation of the transcript in TermSession
until after the size is set with a call to updateSize().  This requires
some changes in the EmulatorView's initialization code to ensure that
the TranscriptScreen and TerminalEmulator objects aren't used before
they're allocated.

Signed-off-by: Jack Palevich <jackpal@google.com>
src/jackpal/androidterm/EmulatorView.java
src/jackpal/androidterm/session/TermSession.java

index 8fb4f16..ba28883 100644 (file)
@@ -105,7 +105,7 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
     /**
      * Text size. Zero means 4 x 8 font.
      */
-    private int mTextSize;
+    private int mTextSize = 10;
 
     private int mCursorStyle;
     private int mCursorBlink;
@@ -113,13 +113,13 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
     /**
      * Foreground color.
      */
-    private int mForeground;
+    private int mForeground = TermSettings.WHITE;
     private int mForegroundIndex;
 
     /**
      * Background color.
      */
-    private int mBackground;
+    private int mBackground = TermSettings.BLACK;
     private int mBackgroundIndex;
 
     /**
@@ -619,10 +619,11 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
         setFocusable(true);
         setFocusableInTouchMode(true);
 
-        initialize(session);
-        session.setUpdateCallback(mUpdateNotify);
+        mTermSession = session;
         // XXX We should really be able to fetch this from within TermSession
         session.setProcessExitMessage(context.getString(R.string.process_exit_message));
+
+        mKeyListener = new TermKeyListener(session);
     }
 
     public void setWindowSizeCallback(WindowSizeCallback callback) {
@@ -650,20 +651,16 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
 
     /**
      * Call this to initialize the view.
-     *
-     * @param session The terminal session this view will be displaying
      */
-    private void initialize(TermSession session) {
-        mTermSession = session;
-        mTranscriptScreen = session.getTranscriptScreen();
-        mEmulator = session.getEmulator();
+    private void initialize() {
+        TermSession session = mTermSession;
 
-        mKeyListener = new TermKeyListener(session);
-        mTextSize = 10;
-        mForeground = TermSettings.WHITE;
-        mBackground = TermSettings.BLACK;
         updateText();
 
+        mTranscriptScreen = session.getTranscriptScreen();
+        mEmulator = session.getEmulator();
+        session.setUpdateCallback(mUpdateNotify);
+
         requestFocus();
     }
 
@@ -966,8 +963,10 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
         if (!mKnownSize) {
             mKnownSize = true;
+            initialize();
+        } else {
+            updateSize(false);
         }
-        updateSize(false);
     }
 
     private void updateSize(int w, int h) {
@@ -1034,6 +1033,12 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
             requestLayout();
             mRedoLayout = false;
         }
+
+        if (mEmulator == null) {
+            // Not ready yet
+            return;
+        }
+
         int w = getWidth();
         int h = getHeight();
 
index b115fe1..d6cb632 100644 (file)
@@ -51,6 +51,8 @@ public class TermSession {
     private FileDescriptor mTermFd;
     private FileOutputStream mTermOut;
     private FileInputStream mTermIn;
+    private String mInitialCommand;
+    private Thread mWatcherThread;
 
     private TranscriptScreen mTranscriptScreen;
     private TerminalEmulator mEmulator;
@@ -65,9 +67,6 @@ public class TermSession {
 
     private String mProcessExitMessage;
 
-    private static final int DEFAULT_COLUMNS = 80;
-    private static final int DEFAULT_ROWS = 24;
-
     // Number of rows in the transcript
     private static final int TRANSCRIPT_ROWS = 10000;
 
@@ -107,14 +106,9 @@ public class TermSession {
         mProcId = processId[0];
         mTermOut = new FileOutputStream(mTermFd);
         mTermIn = new FileInputStream(mTermFd);
+        mInitialCommand = initialCommand;
 
-        int[] colorScheme = settings.getColorScheme();
-        mTranscriptScreen = new TranscriptScreen(DEFAULT_COLUMNS, TRANSCRIPT_ROWS, DEFAULT_ROWS, colorScheme[0], colorScheme[2]);
-        mEmulator = new TerminalEmulator(settings, mTranscriptScreen, DEFAULT_COLUMNS, DEFAULT_ROWS, mTermOut);
-
-        mIsRunning = true;
-
-        Thread watcher = new Thread() {
+        mWatcherThread = new Thread() {
              @Override
              public void run() {
                 Log.i(TermDebug.LOG_TAG, "waiting for: " + mProcId);
@@ -123,8 +117,7 @@ public class TermSession {
                 mMsgHandler.sendMessage(mMsgHandler.obtainMessage(PROCESS_EXITED, result));
              }
         };
-        watcher.setName("Process watcher");
-        watcher.start();
+        mWatcherThread.setName("Process watcher");
 
         mWriteCharBuffer = CharBuffer.allocate(2);
         mWriteByteBuffer = ByteBuffer.allocate(4);
@@ -157,9 +150,19 @@ public class TermSession {
             }
         };
         mPollingThread.setName("Input reader");
+    }
+
+    private void initializeEmulator(int columns, int rows) {
+        TermSettings settings = mSettings;
+        int[] colorScheme = settings.getColorScheme();
+        mTranscriptScreen = new TranscriptScreen(columns, TRANSCRIPT_ROWS, rows, colorScheme[0], colorScheme[2]);
+        mEmulator = new TerminalEmulator(settings, mTranscriptScreen, columns, rows, mTermOut);
+
+        mIsRunning = true;
+        mWatcherThread.start();
         mPollingThread.start();
 
-        sendInitialCommand(initialCommand);
+        sendInitialCommand(mInitialCommand);
     }
 
     private void sendInitialCommand(String initialCommand) {
@@ -277,7 +280,12 @@ public class TermSession {
     public void updateSize(int columns, int rows) {
         // Inform the attached pty of our new size:
         Exec.setPtyWindowSize(mTermFd, rows, columns, 0, 0);
-        mEmulator.updateSize(columns, rows);
+
+        if (mEmulator == null) {
+            initializeEmulator(columns, rows);
+        } else {
+            mEmulator.updateSize(columns, rows);
+        }
     }
 
     public String getTranscriptText() {
@@ -303,6 +311,11 @@ public class TermSession {
 
     public void updatePrefs(TermSettings settings) {
         mSettings = settings;
+        if (mEmulator == null) {
+            // Not initialized yet, we'll pick up the settings then
+            return;
+        }
+
         mEmulator.updatePrefs(settings);
 
         int[] colorScheme = settings.getColorScheme();