Session: Load x11 libs when startup

This commit is contained in:
zt515
2017-11-30 23:31:51 +08:00
parent cdc1f1b3b0
commit f34e040e64
8 changed files with 128 additions and 25 deletions

View File

@ -9,6 +9,7 @@ import io.neoterm.component.eks.ExtraKeysComponent
import io.neoterm.component.font.FontComponent
import io.neoterm.component.pm.PackageComponent
import io.neoterm.component.script.UserScriptComponent
import io.neoterm.component.session.SessionComponent
import io.neoterm.frontend.logging.NLog
import io.neoterm.frontend.component.ComponentManager
@ -26,5 +27,6 @@ object NeoInitializer {
ComponentManager.registerComponent(ExtraKeysComponent::class.java)
ComponentManager.registerComponent(CompletionComponent::class.java)
ComponentManager.registerComponent(PackageComponent::class.java)
ComponentManager.registerComponent(SessionComponent::class.java)
}
}

View File

@ -0,0 +1,109 @@
package io.neoterm.component.session
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import io.neoterm.Globals
import io.neoterm.R
import io.neoterm.frontend.component.NeoComponent
import io.neoterm.frontend.logging.NLog
import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.frontend.preference.NeoTermPath
import io.neoterm.frontend.session.shell.ShellParameter
import io.neoterm.frontend.session.shell.ShellTermSession
import io.neoterm.frontend.session.xorg.XParameter
import io.neoterm.frontend.session.xorg.XSession
import io.neoterm.frontend.session.xorg.client.XSessionData
import io.neoterm.utils.TerminalUtils
/**
* @author kiva
*/
class SessionComponent : NeoComponent {
companion object {
private var IS_LIBRARIES_LOADED = false
private fun wrapLibraryName(libName: String): String {
return "lib$libName.so"
}
@SuppressLint("UnsafeDynamicallyLoadedCode")
private fun loadLibraries(): Boolean {
try {
if (Globals.NeedGles3) {
System.loadLibrary("GLESv3")
NLog.e("SessionComponent", "Loaded GLESv3 lib")
} else if (Globals.NeedGles2) {
System.loadLibrary("GLESv2")
NLog.e("SessionComponent", "Loaded GLESv2 lib")
}
} catch (e: UnsatisfiedLinkError) {
NLog.e("SessionComponent", "Cannot load GLESv3 or GLESv2 lib")
}
var result: Boolean
try {
Globals.XLIBS
.plus(Globals.XAPP_LIBS)
.forEach {
val soPath = "${NeoTermPath.LIB_PATH}/${wrapLibraryName(it)}"
NLog.e("SessionComponent", "Loading lib " + soPath)
try {
System.load(soPath)
} catch (error: UnsatisfiedLinkError) {
NLog.e("SessionComponent", "Error loading lib " + soPath
+ ", reason: " + error.localizedMessage)
result = false
}
}
result = true
} catch (ignore: UnsatisfiedLinkError) {
NLog.e("SessionComponent", ignore.localizedMessage)
result = false
}
return result
}
}
override fun onServiceInit() {
if (!IS_LIBRARIES_LOADED) {
synchronized(SessionComponent::class.java) {
if (!IS_LIBRARIES_LOADED) {
IS_LIBRARIES_LOADED = loadLibraries()
}
}
}
}
override fun onServiceDestroy() {
}
override fun onServiceObtained() {
}
fun createSession(context: Context, parameter: XParameter): XSession {
if (context is Activity) {
return XSession(context, XSessionData())
}
throw RuntimeException("Creating X sessions requires Activity, but got Context")
}
fun createSession(context: Context, parameter: ShellParameter): ShellTermSession {
val initCommand = parameter.initialCommand ?:
NeoPreference.loadString(R.string.key_general_initial_command, "")
val session = ShellTermSession.Builder()
.executablePath(parameter.executablePath)
.currentWorkingDirectory(parameter.cwd)
.callback(parameter.sessionCallback)
.systemShell(parameter.systemShell)
.envArray(parameter.env)
.argArray(parameter.arguments)
.create(context)
TerminalUtils.setupTerminalSession(session)
session.initialCommand = initCommand
return session
}
}

View File

@ -56,7 +56,7 @@ class TerminalDialog(val context: Context) {
.arguments(arguments)
.callback(terminalSessionCallback)
.systemShell(false)
terminalSession = TerminalUtils.createShellSession(context, parameter)
terminalSession = TerminalUtils.createSession(context, parameter)
if (terminalSession is ShellTermSession) {
(terminalSession as ShellTermSession).exitPrompt = context.getString(R.string.process_exit_prompt_press_back)
}

View File

@ -11,6 +11,7 @@ object NeoTermPath {
const val USR_PATH = "$ROOT_PATH/usr"
const val HOME_PATH = "$ROOT_PATH/home"
const val APT_BIN_PATH = "$USR_PATH/bin/apt"
const val LIB_PATH = "$USR_PATH/lib"
const val CUSTOM_PATH = "$HOME_PATH/.neoterm"
const val NEOTERM_SHELL_PATH = "$CUSTOM_PATH/shell"

View File

@ -24,13 +24,7 @@ import java.util.*
* @author kiva
*/
class XSession private constructor(private val mActivity: Activity, private val sessionData: XSessionData) : NeoXorgViewClient {
companion object {
fun createSession(activity: Activity, parameter: XParameter): XSession {
return XSession(activity, XSessionData())
}
}
class XSession constructor(private val mActivity: Activity, private val sessionData: XSessionData) : NeoXorgViewClient {
var mSessionName = "";
init {

View File

@ -77,7 +77,7 @@ class NeoTermService : Service() {
get() = mXSessions
fun createTermSession(parameter: ShellParameter): TerminalSession {
val session = TerminalUtils.createShellSession(this, parameter)
val session = TerminalUtils.createSession(this, parameter)
mTerminalSessions.add(session)
updateNotification()
return session
@ -93,7 +93,7 @@ class NeoTermService : Service() {
}
fun createXSession(activity: Activity, parameter: XParameter): XSession {
val session = XSession.createSession(activity, parameter)
val session = TerminalUtils.createSession(activity, parameter)
mXSessions.add(session)
updateNotification()
return session

View File

@ -46,7 +46,7 @@ open class BaseCustomizeActivity : AppCompatActivity() {
.callback(sessionCallback)
.systemShell(false)
session = TerminalUtils.createShellSession(this, parameter)
session = TerminalUtils.createSession(this, parameter)
terminalView.attachSession(session)
}

View File

@ -1,13 +1,17 @@
package io.neoterm.utils
import android.app.Activity
import android.content.Context
import io.neoterm.R
import io.neoterm.backend.TerminalSession
import io.neoterm.component.font.FontComponent
import io.neoterm.component.session.SessionComponent
import io.neoterm.frontend.component.ComponentManager
import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.frontend.session.shell.ShellParameter
import io.neoterm.frontend.session.shell.ShellTermSession
import io.neoterm.frontend.session.xorg.XParameter
import io.neoterm.frontend.session.xorg.XSession
import io.neoterm.frontend.terminal.TerminalView
import io.neoterm.frontend.terminal.TerminalViewClient
import io.neoterm.frontend.terminal.eks.ExtraKeysView
@ -36,21 +40,14 @@ object TerminalUtils {
fun setupTerminalSession(session: TerminalSession?) {
}
fun createShellSession(context: Context, parameter: ShellParameter): TerminalSession {
val initCommand = parameter.initialCommand ?:
NeoPreference.loadString(R.string.key_general_initial_command, "")
fun createSession(context: Context, parameter: ShellParameter): TerminalSession {
val sessionComponent = ComponentManager.getComponent<SessionComponent>()
return sessionComponent.createSession(context, parameter)
}
val session = ShellTermSession.Builder()
.executablePath(parameter.executablePath)
.currentWorkingDirectory(parameter.cwd)
.callback(parameter.sessionCallback)
.systemShell(parameter.systemShell)
.envArray(parameter.env)
.argArray(parameter.arguments)
.create(context)
setupTerminalSession(session)
session.initialCommand = initCommand
return session
fun createSession(activity: Activity, parameter: XParameter) : XSession {
val sessionComponent = ComponentManager.getComponent<SessionComponent>()
return sessionComponent.createSession(activity, parameter)
}
fun escapeString(s: String?): String {