|
| 1 | +/* |
| 2 | + * This file is part of Cosmic IDE. |
| 3 | + * Cosmic IDE is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. |
| 4 | + * Cosmic IDE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 5 | + * You should have received a copy of the GNU General Public License along with Cosmic IDE. If not, see <https://www.gnu.org/licenses/>. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.cosmicide.fragment |
| 9 | + |
| 10 | +import android.os.Bundle |
| 11 | +import android.os.Environment |
| 12 | +import android.util.Log |
| 13 | +import android.view.KeyEvent |
| 14 | +import android.view.MotionEvent |
| 15 | +import android.view.View |
| 16 | +import androidx.core.content.res.ResourcesCompat |
| 17 | +import androidx.lifecycle.lifecycleScope |
| 18 | +import com.blankj.utilcode.util.ClipboardUtils |
| 19 | +import com.blankj.utilcode.util.KeyboardUtils |
| 20 | +import com.termux.terminal.TerminalEmulator |
| 21 | +import com.termux.terminal.TerminalSession |
| 22 | +import com.termux.terminal.TerminalSessionClient |
| 23 | +import com.termux.view.TerminalRenderer |
| 24 | +import com.termux.view.TerminalView |
| 25 | +import com.termux.view.TerminalViewClient |
| 26 | +import kotlinx.coroutines.launch |
| 27 | +import org.cosmicide.R |
| 28 | +import org.cosmicide.common.BaseBindingFragment |
| 29 | +import org.cosmicide.databinding.FragmentTerminalBinding |
| 30 | +import org.cosmicide.project.Project |
| 31 | +import org.cosmicide.util.ProjectHandler |
| 32 | +import java.io.File |
| 33 | + |
| 34 | +/** |
| 35 | + * A fragment for displaying information about the compilation process. |
| 36 | + */ |
| 37 | +class TerminalFragment : BaseBindingFragment<FragmentTerminalBinding>() { |
| 38 | + val project: Project? = ProjectHandler.getProject() |
| 39 | + |
| 40 | + override fun getViewBinding() = FragmentTerminalBinding.inflate(layoutInflater) |
| 41 | + |
| 42 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 43 | + super.onViewCreated(view, savedInstanceState) |
| 44 | + |
| 45 | + binding.terminalView.attachSession(getTerminalSession()) |
| 46 | + binding.terminalView.setTerminalViewClient(TerminalClient(binding.terminalView)) |
| 47 | + binding.terminalView.mRenderer = |
| 48 | + TerminalRenderer(28, ResourcesCompat.getFont(requireContext(), R.font.noto_sans_mono)!!) |
| 49 | + binding.terminalView.requestFocus() |
| 50 | + KeyboardUtils.showSoftInput(binding.terminalView) |
| 51 | + } |
| 52 | + |
| 53 | + private fun getTerminalSession(): TerminalSession { |
| 54 | + val cwd = project?.root?.absolutePath |
| 55 | + ?: if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) { |
| 56 | + Environment.getExternalStorageDirectory().absolutePath |
| 57 | + } else { |
| 58 | + requireContext().filesDir.absolutePath |
| 59 | + } |
| 60 | + var shell = "/bin/sh" |
| 61 | + |
| 62 | + if (File(shell).exists().not()) { |
| 63 | + shell = "/system/bin/sh" |
| 64 | + } |
| 65 | + |
| 66 | + return TerminalSession( |
| 67 | + shell, |
| 68 | + cwd, |
| 69 | + arrayOf<String>(), |
| 70 | + arrayOf(), |
| 71 | + TerminalEmulator.DEFAULT_TERMINAL_TRANSCRIPT_ROWS, |
| 72 | + getTermSessionClient() |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + private fun getTermSessionClient(): TerminalSessionClient { |
| 77 | + return object : TerminalSessionClient { |
| 78 | + override fun onTextChanged(changedSession: TerminalSession) { |
| 79 | + lifecycleScope.launch { |
| 80 | + binding.terminalView.onScreenUpdated() |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + override fun onTitleChanged(updatedSession: TerminalSession) {} |
| 85 | + |
| 86 | + override fun onSessionFinished(finishedSession: TerminalSession) { |
| 87 | + lifecycleScope.launch { |
| 88 | + binding.terminalView.let { |
| 89 | + KeyboardUtils.hideSoftInput(it) |
| 90 | + it.mTermSession?.finishIfRunning() |
| 91 | + } |
| 92 | + requireActivity().supportFragmentManager.popBackStack() |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + override fun onCopyTextToClipboard(session: TerminalSession, text: String?) { |
| 97 | + ClipboardUtils.copyText(text) |
| 98 | + } |
| 99 | + |
| 100 | + override fun onPasteTextFromClipboard(session: TerminalSession?) { |
| 101 | + lifecycleScope.launch { |
| 102 | + val clip = ClipboardUtils.getText().toString() |
| 103 | + if (clip.trim { it <= ' ' } |
| 104 | + .isNotEmpty() && binding.terminalView.mEmulator != null) { |
| 105 | + binding.terminalView.mEmulator.paste(clip) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + override fun onBell(session: TerminalSession) {} |
| 111 | + |
| 112 | + override fun onColorsChanged(changedSession: TerminalSession) {} |
| 113 | + |
| 114 | + override fun onTerminalCursorStateChange(state: Boolean) {} |
| 115 | + override fun setTerminalShellPid(session: TerminalSession, pid: Int) { |
| 116 | + Log.d("TerminalFragment", "setTerminalShellPid: $pid") |
| 117 | + } |
| 118 | + |
| 119 | + override fun getTerminalCursorStyle(): Int { |
| 120 | + return TerminalEmulator.TERMINAL_CURSOR_STYLE_UNDERLINE |
| 121 | + } |
| 122 | + |
| 123 | + override fun logError(tag: String?, message: String?) { |
| 124 | + if (message != null) { |
| 125 | + Log.e(tag, message) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + override fun logWarn(tag: String?, message: String?) { |
| 130 | + if (message != null) { |
| 131 | + Log.w(tag, message) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + override fun logInfo(tag: String?, message: String?) { |
| 136 | + if (message != null) { |
| 137 | + Log.i(tag, message) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + override fun logDebug(tag: String?, message: String?) { |
| 142 | + if (message != null) { |
| 143 | + Log.d(tag, message) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + override fun logVerbose(tag: String?, message: String?) { |
| 148 | + if (message != null) { |
| 149 | + Log.v(tag, message) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + override fun logStackTraceWithMessage( |
| 154 | + tag: String?, |
| 155 | + message: String?, |
| 156 | + e: Exception? |
| 157 | + ) { |
| 158 | + Log.e(tag, message + "\n" + Log.getStackTraceString(e)) |
| 159 | + } |
| 160 | + |
| 161 | + override fun logStackTrace(tag: String?, e: Exception?) { |
| 162 | + Log.e(tag, Log.getStackTraceString(e)) |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + class TerminalClient(val terminal: TerminalView) : TerminalViewClient { |
| 169 | + override fun logError(tag: String?, message: String?) { |
| 170 | + if (message != null) { |
| 171 | + Log.e(tag, message) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + override fun logWarn(tag: String?, message: String?) { |
| 176 | + if (message != null) { |
| 177 | + Log.w(tag, message) |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + override fun logInfo(tag: String?, message: String?) { |
| 182 | + if (message != null) { |
| 183 | + Log.i(tag, message) |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + override fun logDebug(tag: String?, message: String?) { |
| 188 | + if (message != null) { |
| 189 | + Log.d(tag, message) |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + override fun logVerbose(tag: String?, message: String?) { |
| 194 | + if (message != null) { |
| 195 | + Log.v(tag, message) |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + override fun logStackTraceWithMessage( |
| 200 | + tag: String?, |
| 201 | + message: String?, |
| 202 | + e: Exception? |
| 203 | + ) { |
| 204 | + Log.e(tag, message + "\n" + Log.getStackTraceString(e)) |
| 205 | + } |
| 206 | + |
| 207 | + override fun logStackTrace(tag: String?, e: Exception?) { |
| 208 | + Log.e(tag, Log.getStackTraceString(e)) |
| 209 | + } |
| 210 | + |
| 211 | + override fun onScale(scale: Float): Float { |
| 212 | + return scale |
| 213 | + } |
| 214 | + |
| 215 | + override fun onSingleTapUp(e: MotionEvent?) { |
| 216 | + if (terminal.mTermSession.isRunning) { |
| 217 | + terminal.requestFocus() |
| 218 | + KeyboardUtils.showSoftInput(terminal) |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + override fun shouldBackButtonBeMappedToEscape(): Boolean { |
| 223 | + return false |
| 224 | + } |
| 225 | + |
| 226 | + override fun shouldEnforceCharBasedInput(): Boolean { |
| 227 | + return true |
| 228 | + } |
| 229 | + |
| 230 | + override fun shouldUseCtrlSpaceWorkaround(): Boolean { |
| 231 | + return false |
| 232 | + } |
| 233 | + |
| 234 | + override fun isTerminalViewSelected(): Boolean { |
| 235 | + return true |
| 236 | + } |
| 237 | + |
| 238 | + override fun copyModeChanged(copyMode: Boolean) {} |
| 239 | + |
| 240 | + override fun onKeyDown(keyCode: Int, e: KeyEvent?, session: TerminalSession?): Boolean { |
| 241 | + return false |
| 242 | + } |
| 243 | + |
| 244 | + override fun onKeyUp(keyCode: Int, e: KeyEvent?): Boolean { |
| 245 | + if (keyCode == KeyEvent.KEYCODE_BACK) { |
| 246 | + if (terminal.mTermSession.isRunning) { |
| 247 | + terminal.mTermSession.finishIfRunning() |
| 248 | + } |
| 249 | + return true |
| 250 | + } |
| 251 | + return false |
| 252 | + } |
| 253 | + |
| 254 | + override fun onLongPress(event: MotionEvent?): Boolean { |
| 255 | + return false |
| 256 | + } |
| 257 | + |
| 258 | + override fun readControlKey(): Boolean { |
| 259 | + return false |
| 260 | + } |
| 261 | + |
| 262 | + override fun readAltKey(): Boolean { |
| 263 | + return false |
| 264 | + } |
| 265 | + |
| 266 | + override fun readShiftKey(): Boolean { |
| 267 | + return false |
| 268 | + } |
| 269 | + |
| 270 | + override fun readFnKey(): Boolean { |
| 271 | + return false |
| 272 | + } |
| 273 | + |
| 274 | + override fun onCodePoint( |
| 275 | + codePoint: Int, |
| 276 | + ctrlDown: Boolean, |
| 277 | + session: TerminalSession? |
| 278 | + ): Boolean { |
| 279 | + return false |
| 280 | + } |
| 281 | + |
| 282 | + override fun onEmulatorSet() {} |
| 283 | + } |
| 284 | +} |
0 commit comments