├── .gitignore ├── .gitmodules ├── .vscode └── launch.json ├── LICENSE ├── README.md └── src ├── AvalonStudio.TerminalEmulator.sln ├── AvalonStudio.TerminalEmulator ├── .gitignore ├── App.xaml ├── App.xaml.cs ├── Assets │ └── avalonia-logo.ico ├── AvalonStudio.TerminalEmulator.csproj ├── Program.cs ├── ViewLocator.cs ├── ViewModels │ ├── TerminalViewModel.cs │ └── ViewModelBase.cs ├── Views │ ├── MainWindow.xaml │ └── MainWindow.xaml.cs └── nuget.config └── AvalonStudio.Terminals ├── AvalonStudio.Terminals.csproj ├── IPsuedoTerminal.cs ├── IPsuedoTerminalProvider.cs ├── ProcessConnection.cs ├── Unix ├── Native.cs ├── UnixPsuedoTerminal.cs └── UnixPsuedoTerminalProvider.cs └── Win32 ├── Win32PsuedoTerminal.cs └── Win32PsuedoTerminalProvider.cs /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "VtNetCore.Avalonia"] 2 | path = VtNetCore.Avalonia 3 | url = https://github.com/VitalElement/VtNetCore.Avalonia.git 4 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": ".NET Core Launch (console)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "program": "${workspaceFolder}/src/AvalonStudio.TerminalEmulator/bin/Debug/netcoreapp2.2/AvalonStudio.TerminalEmulator.dll", 12 | "args": [], 13 | "cwd": "${workspaceFolder}", 14 | "console": "internalConsole", 15 | "stopAtEntry": false, 16 | "internalConsoleOptions": "openOnSessionStart" 17 | }, 18 | { 19 | "name": ".NET Core Launch (web)", 20 | "type": "coreclr", 21 | "request": "launch", 22 | "preLaunchTask": "build", 23 | "program": "${workspaceFolder}/bin/Debug//.dll", 24 | "args": [], 25 | "cwd": "${workspaceFolder}", 26 | "stopAtEntry": false, 27 | "internalConsoleOptions": "openOnSessionStart", 28 | "launchBrowser": { 29 | "enabled": true, 30 | "args": "${auto-detect-url}", 31 | "windows": { 32 | "command": "cmd.exe", 33 | "args": "/C start ${auto-detect-url}" 34 | }, 35 | "osx": { 36 | "command": "open" 37 | }, 38 | "linux": { 39 | "command": "xdg-open" 40 | } 41 | }, 42 | "env": { 43 | "ASPNETCORE_ENVIRONMENT": "Development" 44 | }, 45 | "sourceFileMap": { 46 | "/Views": "${workspaceFolder}/Views" 47 | } 48 | }, 49 | { 50 | "name": ".NET Core Attach", 51 | "type": "coreclr", 52 | "request": "attach", 53 | "processId": "${command:pickProcess}" 54 | } 55 | ] 56 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 danwalmsley 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Avalonia.TerminalEmulator 2 | Avalonia control that allows embedding applications inside an emulated terminal. 3 | -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio Version 16 3 | VisualStudioVersion = 16.0.28714.193 4 | MinimumVisualStudioVersion = 10.0.40219.1 5 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AvalonStudio.TerminalEmulator", "AvalonStudio.TerminalEmulator\AvalonStudio.TerminalEmulator.csproj", "{6B06A72B-8D3E-4BEA-AFB6-09DC684ECB34}" 6 | EndProject 7 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AvalonStudio.Terminals", "AvalonStudio.Terminals\AvalonStudio.Terminals.csproj", "{1E2C4335-B0A4-474E-9FF6-B0244A93C24F}" 8 | EndProject 9 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VtNetCore.Avalonia", "..\VtNetCore.Avalonia\VtNetCore.Avalonia\VtNetCore.Avalonia.csproj", "{BD1DD93A-4733-4871-B2CD-3082D48FEECF}" 10 | EndProject 11 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VtNetCore", "..\VtNetCore.Avalonia\VtNetCore\VtNetCore\VtNetCore.csproj", "{BE99F07E-1E17-4B8D-9D54-AA330D93539B}" 12 | EndProject 13 | Global 14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 15 | Debug|Any CPU = Debug|Any CPU 16 | Release|Any CPU = Release|Any CPU 17 | EndGlobalSection 18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 19 | {6B06A72B-8D3E-4BEA-AFB6-09DC684ECB34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 20 | {6B06A72B-8D3E-4BEA-AFB6-09DC684ECB34}.Debug|Any CPU.Build.0 = Debug|Any CPU 21 | {6B06A72B-8D3E-4BEA-AFB6-09DC684ECB34}.Release|Any CPU.ActiveCfg = Release|Any CPU 22 | {6B06A72B-8D3E-4BEA-AFB6-09DC684ECB34}.Release|Any CPU.Build.0 = Release|Any CPU 23 | {1E2C4335-B0A4-474E-9FF6-B0244A93C24F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 24 | {1E2C4335-B0A4-474E-9FF6-B0244A93C24F}.Debug|Any CPU.Build.0 = Debug|Any CPU 25 | {1E2C4335-B0A4-474E-9FF6-B0244A93C24F}.Release|Any CPU.ActiveCfg = Release|Any CPU 26 | {1E2C4335-B0A4-474E-9FF6-B0244A93C24F}.Release|Any CPU.Build.0 = Release|Any CPU 27 | {BD1DD93A-4733-4871-B2CD-3082D48FEECF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 28 | {BD1DD93A-4733-4871-B2CD-3082D48FEECF}.Debug|Any CPU.Build.0 = Debug|Any CPU 29 | {BD1DD93A-4733-4871-B2CD-3082D48FEECF}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {BD1DD93A-4733-4871-B2CD-3082D48FEECF}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {BE99F07E-1E17-4B8D-9D54-AA330D93539B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {BE99F07E-1E17-4B8D-9D54-AA330D93539B}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {BE99F07E-1E17-4B8D-9D54-AA330D93539B}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {BE99F07E-1E17-4B8D-9D54-AA330D93539B}.Release|Any CPU.Build.0 = Release|Any CPU 35 | EndGlobalSection 36 | GlobalSection(SolutionProperties) = preSolution 37 | HideSolutionNode = FALSE 38 | EndGlobalSection 39 | GlobalSection(ExtensibilityGlobals) = postSolution 40 | SolutionGuid = {8C23CD4A-1E35-4F1E-9738-8F66249A2699} 41 | EndGlobalSection 42 | GlobalSection(AvalonStudioProperties) = preSolution 43 | StartupItem = {D34BE5A0-269E-46D5-BE32-590AE7146EE1} 44 | EndGlobalSection 45 | EndGlobal 46 | -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | ## Visual Studio Code specific files and folder 7 | .vscode/* 8 | !.vscode/settings.json 9 | !.vscode/tasks.json 10 | !.vscode/launch.json 11 | !.vscode/extensions.jsons 12 | 13 | # User-specific files 14 | *.suo 15 | *.user 16 | *.userosscache 17 | *.sln.docstates 18 | 19 | # User-specific files (MonoDevelop/Xamarin Studio) 20 | *.userprefs 21 | 22 | # Build results 23 | [Dd]ebug/ 24 | [Dd]ebugPublic/ 25 | [Rr]elease/ 26 | [Rr]eleases/ 27 | x64/ 28 | x86/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUNIT 47 | *.VisualState.xml 48 | TestResult.xml 49 | 50 | # Build Results of an ATL Project 51 | [Dd]ebugPS/ 52 | [Rr]eleasePS/ 53 | dlldata.c 54 | 55 | # Benchmark Results 56 | BenchmarkDotNet.Artifacts/ 57 | 58 | # .NET Core 59 | project.lock.json 60 | project.fragment.lock.json 61 | artifacts/ 62 | **/Properties/launchSettings.json 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_i.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *.log 88 | *.vspscc 89 | *.vssscc 90 | .builds 91 | *.pidb 92 | *.svclog 93 | *.scc 94 | 95 | # Chutzpah Test files 96 | _Chutzpah* 97 | 98 | # Visual C++ cache files 99 | ipch/ 100 | *.aps 101 | *.ncb 102 | *.opendb 103 | *.opensdf 104 | *.sdf 105 | *.cachefile 106 | *.VC.db 107 | *.VC.VC.opendb 108 | 109 | # Visual Studio profiler 110 | *.psess 111 | *.vsp 112 | *.vspx 113 | *.sap 114 | 115 | # Visual Studio Trace Files 116 | *.e2e 117 | 118 | # TFS 2012 Local Workspace 119 | $tf/ 120 | 121 | # Guidance Automation Toolkit 122 | *.gpState 123 | 124 | # ReSharper is a .NET coding add-in 125 | _ReSharper*/ 126 | *.[Rr]e[Ss]harper 127 | *.DotSettings.user 128 | 129 | # JustCode is a .NET coding add-in 130 | .JustCode 131 | 132 | # TeamCity is a build add-in 133 | _TeamCity* 134 | 135 | # DotCover is a Code Coverage Tool 136 | *.dotCover 137 | 138 | # AxoCover is a Code Coverage Tool 139 | .axoCover/* 140 | !.axoCover/settings.json 141 | 142 | # Visual Studio code coverage results 143 | *.coverage 144 | *.coveragexml 145 | 146 | # NCrunch 147 | _NCrunch_* 148 | .*crunch*.local.xml 149 | nCrunchTemp_* 150 | 151 | # MightyMoose 152 | *.mm.* 153 | AutoTest.Net/ 154 | 155 | # Web workbench (sass) 156 | .sass-cache/ 157 | 158 | # Installshield output folder 159 | [Ee]xpress/ 160 | 161 | # DocProject is a documentation generator add-in 162 | DocProject/buildhelp/ 163 | DocProject/Help/*.HxT 164 | DocProject/Help/*.HxC 165 | DocProject/Help/*.hhc 166 | DocProject/Help/*.hhk 167 | DocProject/Help/*.hhp 168 | DocProject/Help/Html2 169 | DocProject/Help/html 170 | 171 | # Click-Once directory 172 | publish/ 173 | 174 | # Publish Web Output 175 | *.[Pp]ublish.xml 176 | *.azurePubxml 177 | # Note: Comment the next line if you want to checkin your web deploy settings, 178 | # but database connection strings (with potential passwords) will be unencrypted 179 | *.pubxml 180 | *.publishproj 181 | 182 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 183 | # checkin your Azure Web App publish settings, but sensitive information contained 184 | # in these scripts will be unencrypted 185 | PublishScripts/ 186 | 187 | # NuGet Packages 188 | *.nupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | 214 | # Visual Studio cache files 215 | # files ending in .cache can be ignored 216 | *.[Cc]ache 217 | # but keep track of directories ending in .cache 218 | !*.[Cc]ache/ 219 | 220 | # Others 221 | ClientBin/ 222 | ~$* 223 | *~ 224 | *.dbmdl 225 | *.dbproj.schemaview 226 | *.jfm 227 | *.pfx 228 | *.publishsettings 229 | orleans.codegen.cs 230 | 231 | # Including strong name files can present a security risk 232 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 233 | #*.snk 234 | 235 | # Since there are multiple workflows, uncomment next line to ignore bower_components 236 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 237 | #bower_components/ 238 | 239 | # RIA/Silverlight projects 240 | Generated_Code/ 241 | 242 | # Backup & report files from converting an old project file 243 | # to a newer Visual Studio version. Backup files are not needed, 244 | # because we have git ;-) 245 | _UpgradeReport_Files/ 246 | Backup*/ 247 | UpgradeLog*.XML 248 | UpgradeLog*.htm 249 | ServiceFabricBackup/ 250 | *.rptproj.bak 251 | 252 | # SQL Server files 253 | *.mdf 254 | *.ldf 255 | *.ndf 256 | 257 | # Business Intelligence projects 258 | *.rdl.data 259 | *.bim.layout 260 | *.bim_*.settings 261 | *.rptproj.rsuser 262 | 263 | # Microsoft Fakes 264 | FakesAssemblies/ 265 | 266 | # GhostDoc plugin setting file 267 | *.GhostDoc.xml 268 | 269 | # Node.js Tools for Visual Studio 270 | .ntvs_analysis.dat 271 | node_modules/ 272 | 273 | # Visual Studio 6 build log 274 | *.plg 275 | 276 | # Visual Studio 6 workspace options file 277 | *.opt 278 | 279 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 280 | *.vbw 281 | 282 | # Visual Studio LightSwitch build output 283 | **/*.HTMLClient/GeneratedArtifacts 284 | **/*.DesktopClient/GeneratedArtifacts 285 | **/*.DesktopClient/ModelManifest.xml 286 | **/*.Server/GeneratedArtifacts 287 | **/*.Server/ModelManifest.xml 288 | _Pvt_Extensions 289 | 290 | # Paket dependency manager 291 | .paket/paket.exe 292 | paket-files/ 293 | 294 | # FAKE - F# Make 295 | .fake/ 296 | 297 | # JetBrains Rider 298 | .idea/ 299 | *.sln.iml 300 | 301 | # CodeRush 302 | .cr/ 303 | 304 | # Python Tools for Visual Studio (PTVS) 305 | __pycache__/ 306 | *.pyc 307 | 308 | # Cake - Uncomment if you are using it 309 | # tools/** 310 | # !tools/packages.config 311 | 312 | # Tabs Studio 313 | *.tss 314 | 315 | # Telerik's JustMock configuration file 316 | *.jmconfig 317 | 318 | # BizTalk build output 319 | *.btp.cs 320 | *.btm.cs 321 | *.odx.cs 322 | *.xsd.cs 323 | 324 | # OpenCover UI analysis results 325 | OpenCover/ 326 | 327 | # Azure Stream Analytics local run output 328 | ASALocalRun/ 329 | 330 | # MSBuild Binary and Structured Log 331 | *.binlog 332 | 333 | # NVidia Nsight GPU debugger configuration file 334 | *.nvuser 335 | 336 | # MFractors (Xamarin productivity tool) working folder 337 | .mfractor/ 338 | -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/App.xaml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using Avalonia; 2 | using Avalonia.Markup.Xaml; 3 | 4 | namespace AvalonStudio.TerminalEmulator 5 | { 6 | public class App : Application 7 | { 8 | public override void Initialize() 9 | { 10 | AvaloniaXamlLoader.Load(this); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/Assets/avalonia-logo.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VitalElement/AvalonStudio.TerminalEmulator/c8ad965266185d6919cb0489b23401fcdd947ef7/src/AvalonStudio.TerminalEmulator/Assets/avalonia-logo.ico -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/AvalonStudio.TerminalEmulator.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | Exe 4 | netcoreapp2.2 5 | 6 | 7 | 8 | %(Filename) 9 | 10 | 11 | Designer 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | all 20 | runtime; build; native; contentfiles; analyzers; buildtransitive 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Avalonia; 3 | using Avalonia.Logging.Serilog; 4 | using AvalonStudio.TerminalEmulator.ViewModels; 5 | using AvalonStudio.TerminalEmulator.Views; 6 | using AvalonStudio.Terminals.Unix; 7 | 8 | namespace AvalonStudio.TerminalEmulator 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | UnixPsuedoTerminal.Trampoline(args); 15 | BuildAvaloniaApp().Start(() => new TerminalViewModel()); 16 | } 17 | 18 | public static AppBuilder BuildAvaloniaApp() 19 | => AppBuilder.Configure() 20 | .UsePlatformDetect() 21 | .UseReactiveUI() 22 | .LogToDebug(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/ViewLocator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) The Avalonia Project. All rights reserved. 2 | // Licensed under the MIT license. See licence.md file in the project root for full license information. 3 | 4 | using System; 5 | using Avalonia.Controls; 6 | using Avalonia.Controls.Templates; 7 | using AvalonStudio.TerminalEmulator.ViewModels; 8 | 9 | namespace AvalonStudio.TerminalEmulator 10 | { 11 | public class ViewLocator : IDataTemplate 12 | { 13 | public bool SupportsRecycling => false; 14 | 15 | public IControl Build(object data) 16 | { 17 | var name = data.GetType().FullName.Replace("ViewModel", "View"); 18 | var type = Type.GetType(name); 19 | 20 | if (type != null) 21 | { 22 | return (Control)Activator.CreateInstance(type); 23 | } 24 | else 25 | { 26 | return new TextBlock { Text = "Not Found: " + name }; 27 | } 28 | } 29 | 30 | public bool Match(object data) 31 | { 32 | return data is ViewModelBase; 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/ViewModels/TerminalViewModel.cs: -------------------------------------------------------------------------------- 1 | using Avalonia.Threading; 2 | using AvalonStudio.Terminals; 3 | using AvalonStudio.Terminals.Unix; 4 | using AvalonStudio.Terminals.Win32; 5 | using ReactiveUI; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.IO; 9 | using System.Runtime.InteropServices; 10 | using VtNetCore.Avalonia; 11 | 12 | namespace AvalonStudio.TerminalEmulator.ViewModels 13 | { 14 | public class TerminalViewModel : ReactiveObject 15 | { 16 | private IConnection _connection; 17 | private bool _terminalVisible; 18 | private object _createLock = new object(); 19 | static IPsuedoTerminalProvider s_provider = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? new Win32PsuedoTerminalProvider() : new UnixPsuedoTerminalProvider() as IPsuedoTerminalProvider; 20 | 21 | public TerminalViewModel() 22 | { 23 | Dispatcher.UIThread.Post(() => 24 | { 25 | CreateConnection(); 26 | }); 27 | } 28 | 29 | 30 | private void CreateConnection(string workingDirectory = null) 31 | { 32 | lock (_createLock) 33 | { 34 | if (workingDirectory == null) 35 | { 36 | workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); 37 | } 38 | 39 | CloseConnection(); 40 | 41 | var args = new List(); 42 | 43 | if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) 44 | { 45 | args.Add("-l"); 46 | } 47 | 48 | var shellExecutable = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Environment.ExpandEnvironmentVariables("%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe") : "/bin/bash"; 49 | 50 | if (shellExecutable != null) 51 | { 52 | var terminal = s_provider.Create(80, 32, workingDirectory, null, shellExecutable, args.ToArray()); 53 | 54 | Connection = new PsuedoTerminalConnection(terminal); 55 | 56 | TerminalVisible = true; 57 | 58 | Connection.Closed += Connection_Closed; 59 | } 60 | } 61 | } 62 | 63 | private void CloseConnection() 64 | { 65 | if (Connection != null) 66 | { 67 | System.Console.WriteLine("Closing Terminal"); 68 | Connection.Closed -= Connection_Closed; 69 | Connection.Disconnect(); 70 | Connection = null; 71 | } 72 | } 73 | 74 | private void Connection_Closed(object sender, System.EventArgs e) 75 | { 76 | (sender as IConnection).Closed -= Connection_Closed; 77 | TerminalVisible = false; 78 | } 79 | 80 | public IConnection Connection 81 | { 82 | get { return _connection; } 83 | set { this.RaiseAndSetIfChanged(ref _connection, value); } 84 | } 85 | 86 | public bool TerminalVisible 87 | { 88 | get { return _terminalVisible; } 89 | set { this.RaiseAndSetIfChanged(ref _terminalVisible, value); } 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/ViewModels/ViewModelBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using ReactiveUI; 5 | 6 | namespace AvalonStudio.TerminalEmulator.ViewModels 7 | { 8 | public class ViewModelBase : ReactiveObject 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/Views/MainWindow.xaml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/Views/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using Avalonia; 2 | using Avalonia.Controls; 3 | using Avalonia.Markup.Xaml; 4 | 5 | namespace AvalonStudio.TerminalEmulator.Views 6 | { 7 | public class MainWindow : Window 8 | { 9 | public MainWindow() 10 | { 11 | InitializeComponent(); 12 | } 13 | 14 | private void InitializeComponent() 15 | { 16 | AvaloniaXamlLoader.Load(this); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/AvalonStudio.TerminalEmulator/nuget.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/AvalonStudio.Terminals/AvalonStudio.Terminals.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | true 9 | 10 | 11 | 12 | true 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/AvalonStudio.Terminals/IPsuedoTerminal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Threading.Tasks; 4 | 5 | namespace AvalonStudio.Terminals 6 | { 7 | public interface IPsuedoTerminal : IDisposable 8 | { 9 | void SetSize(int columns, int rows); 10 | 11 | Task WriteAsync(byte[] buffer, int offset, int count); 12 | 13 | Task ReadAsync(byte[] buffer, int offset, int count); 14 | 15 | Process Process { get; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/AvalonStudio.Terminals/IPsuedoTerminalProvider.cs: -------------------------------------------------------------------------------- 1 | namespace AvalonStudio.Terminals 2 | { 3 | public interface IPsuedoTerminalProvider 4 | { 5 | IPsuedoTerminal Create(int columns, int rows, string initialDirectory, string environment, string command, params string[] arguments); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/AvalonStudio.Terminals/ProcessConnection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reactive.Concurrency; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using VtNetCore.Avalonia; 6 | 7 | namespace AvalonStudio.Terminals 8 | { 9 | public class PsuedoTerminalConnection : IConnection, IDisposable 10 | { 11 | private CancellationTokenSource _cancellationSource; 12 | private bool _isConnected = false; 13 | private IPsuedoTerminal _terminal; 14 | 15 | public PsuedoTerminalConnection(IPsuedoTerminal terminal) 16 | { 17 | _terminal = terminal; 18 | } 19 | 20 | public bool IsConnected => _isConnected; 21 | 22 | public event EventHandler DataReceived; 23 | 24 | public event EventHandler Closed; 25 | 26 | public bool Connect() 27 | { 28 | _cancellationSource = new CancellationTokenSource(); 29 | 30 | Task.Run(async () => 31 | { 32 | var data = new byte[4096]; 33 | 34 | while (!_cancellationSource.IsCancellationRequested) 35 | { 36 | var bytesReceived = await _terminal.ReadAsync(data, 0, data.Length); 37 | 38 | if (bytesReceived > 0) 39 | { 40 | var receviedData = new byte[bytesReceived]; 41 | 42 | Buffer.BlockCopy(data, 0, receviedData, 0, bytesReceived); 43 | 44 | DataReceived?.Invoke(this, new DataReceivedEventArgs { Data = receviedData }); 45 | } 46 | 47 | await Task.Delay(5); 48 | } 49 | }, _cancellationSource.Token); 50 | 51 | _isConnected = true; 52 | 53 | _terminal.Process.EnableRaisingEvents = true; 54 | _terminal.Process.Exited += Process_Exited; 55 | 56 | return _isConnected; 57 | } 58 | 59 | private void Process_Exited(object sender, EventArgs e) 60 | { 61 | _terminal.Process.Exited -= Process_Exited; 62 | 63 | Closed?.Invoke(this, EventArgs.Empty); 64 | } 65 | 66 | public void Disconnect() 67 | { 68 | _cancellationSource?.Cancel(); 69 | _terminal?.Dispose(); 70 | } 71 | 72 | public void SendData(byte[] data) 73 | { 74 | _terminal.WriteAsync(data, 0, data.Length); 75 | } 76 | 77 | public void SetTerminalWindowSize(int columns, int rows, int width, int height) 78 | { 79 | _terminal?.SetSize(columns, rows); 80 | } 81 | 82 | public void Dispose() 83 | { 84 | _cancellationSource?.Cancel(); 85 | 86 | Disconnect(); 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/AvalonStudio.Terminals/Unix/Native.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace AvalonStudio.Terminals.Unix 5 | { 6 | internal class NativeDelegates 7 | { 8 | [DllImport("libdl.so.2", EntryPoint = "dlopen")] 9 | private static extern IntPtr dlopen_lin(string path, int flags); 10 | 11 | [DllImport("libdl.so.2", EntryPoint = "dlsym")] 12 | private static extern IntPtr dlsym_lin(IntPtr handle, string symbol); 13 | 14 | [DllImport("libSystem.dylib", EntryPoint = "dlopen")] 15 | private static extern IntPtr dlopen_mac(string path, int flags); 16 | 17 | [DllImport("libSystem.dylib", EntryPoint = "dlsym")] 18 | private static extern IntPtr dlsym_mac(IntPtr handle, string symbol); 19 | 20 | public static T GetProc() 21 | { 22 | if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) 23 | { 24 | var dl = dlopen_mac("libSystem.dylib", 2); 25 | 26 | var name = typeof(T).Name; 27 | var a = dlsym_mac(dl, name); 28 | return Marshal.GetDelegateForFunctionPointer(a); 29 | } 30 | else 31 | { 32 | var dl = dlopen_lin("libc.6.so", 2); 33 | var a = dlsym_lin(dl, typeof(T).Name); 34 | return Marshal.GetDelegateForFunctionPointer(a); 35 | } 36 | } 37 | 38 | public static T GetProc(string function) 39 | { 40 | if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) 41 | { 42 | var dl = dlopen_mac("libSystem.dylib", 2); 43 | var a = dlsym_mac(dl, function); 44 | return Marshal.GetDelegateForFunctionPointer(a); 45 | } 46 | else 47 | { 48 | var dl = dlopen_lin("libc.6.so", 2); 49 | var a = dlsym_lin(dl, function); 50 | return Marshal.GetDelegateForFunctionPointer(a); 51 | } 52 | } 53 | 54 | public delegate void dup2(int oldfd, int newfd); 55 | public delegate int fork(); 56 | public delegate void setsid(); 57 | public delegate int ioctl(int fd, UInt64 ctl, IntPtr arg); 58 | public delegate void close(int fd); 59 | public delegate int open([MarshalAs(UnmanagedType.LPStr)] string file, int flags); 60 | public delegate int chdir([MarshalAs(UnmanagedType.LPStr)] string path); 61 | public delegate IntPtr ptsname(int fd); 62 | public delegate int grantpt(int fd); 63 | public delegate int unlockpt(int fd); 64 | public unsafe delegate void execve([MarshalAs(UnmanagedType.LPStr)]string path, [MarshalAs(UnmanagedType.LPArray)]string[] argv, [MarshalAs(UnmanagedType.LPArray)]string[] envp); 65 | public delegate int read(int fd, IntPtr buffer, int length); 66 | public delegate int write(int fd, IntPtr buffer, int length); 67 | public delegate void free(IntPtr ptr); 68 | public delegate int pipe(IntPtr[] fds); 69 | public delegate int setpgid(int pid, int pgid); 70 | public delegate int posix_spawn_file_actions_adddup2(IntPtr file_actions, int fildes, int newfildes); 71 | public delegate int posix_spawn_file_actions_addclose(IntPtr file_actions, int fildes); 72 | public delegate int posix_spawn_file_actions_init(IntPtr file_actions); 73 | public delegate int posix_spawnattr_init(IntPtr attributes); 74 | public delegate int posix_spawnp(out IntPtr pid, string path, IntPtr fileActions, IntPtr attrib, string[] argv, string[] envp); 75 | public delegate int dup(int fd); 76 | public delegate void _exit(int code); 77 | public delegate int getdtablesize(); 78 | } 79 | 80 | internal static class Native 81 | { 82 | public const int O_RDONLY = 0x0000; 83 | public const int O_WRONLY = 0x0001; 84 | public const int O_RDWR = 0x0002; 85 | public const int O_ACCMODE = 0x0003; 86 | 87 | public const int O_CREAT = 0x0100; /* second byte, away from DOS bits */ 88 | public const int O_EXCL = 0x0200; 89 | public const int O_NOCTTY = 0x0400; 90 | public const int O_TRUNC = 0x0800; 91 | public const int O_APPEND = 0x1000; 92 | public const int O_NONBLOCK = 0x2000; 93 | public static readonly ulong TIOCSWINSZ = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x80087467 : 0x5414; 94 | 95 | public const int _SC_OPEN_MAX = 5; 96 | 97 | public const int EAGAIN = 11; /* Try again */ 98 | 99 | public const int EINTR = 4; /* Interrupted system call */ 100 | 101 | public const int ENOENT = 2; 102 | 103 | public static readonly ulong TIOCSCTTY = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? (ulong)0x20007484 : 0x540E; 104 | 105 | [StructLayout(LayoutKind.Sequential, Pack = 1)] 106 | public struct winsize 107 | { 108 | public ushort ws_row; /* rows, in characters */ 109 | public ushort ws_col; /* columns, in characters */ 110 | public ushort ws_xpixel; /* horizontal size, pixels */ 111 | public ushort ws_ypixel; /* vertical size, pixels */ 112 | }; 113 | 114 | public static NativeDelegates.open open = NativeDelegates.GetProc(); 115 | public static NativeDelegates.chdir chdir = NativeDelegates.GetProc(); 116 | public static NativeDelegates.write write = NativeDelegates.GetProc(); 117 | public static NativeDelegates.grantpt grantpt = NativeDelegates.GetProc(); 118 | public static NativeDelegates.unlockpt unlockpt = NativeDelegates.GetProc(); 119 | public static NativeDelegates.ptsname ptsname = NativeDelegates.GetProc(); 120 | public static NativeDelegates.posix_spawn_file_actions_init posix_spawn_file_actions_init = NativeDelegates.GetProc(); 121 | public static NativeDelegates.posix_spawn_file_actions_adddup2 posix_spawn_file_actions_adddup2 = NativeDelegates.GetProc(); 122 | public static NativeDelegates.posix_spawn_file_actions_addclose posix_spawn_file_actions_addclose = NativeDelegates.GetProc(); 123 | public static NativeDelegates.posix_spawnattr_init posix_spawnattr_init = NativeDelegates.GetProc(); 124 | public static NativeDelegates.posix_spawnp posix_spawnp = NativeDelegates.GetProc(); 125 | public static NativeDelegates.dup dup = NativeDelegates.GetProc(); 126 | public static NativeDelegates.setsid setsid = NativeDelegates.GetProc(); 127 | public static NativeDelegates.ioctl ioctl = NativeDelegates.GetProc(); 128 | public static NativeDelegates.execve execve = NativeDelegates.GetProc(); 129 | 130 | public static IntPtr StructToPtr(object obj) 131 | { 132 | var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(obj)); 133 | Marshal.StructureToPtr(obj, ptr, false); 134 | return ptr; 135 | } 136 | 137 | 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/AvalonStudio.Terminals/Unix/UnixPsuedoTerminal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Runtime.InteropServices; 7 | using System.Threading.Tasks; 8 | 9 | namespace AvalonStudio.Terminals.Unix 10 | { 11 | public class UnixPsuedoTerminal : IPsuedoTerminal 12 | { 13 | private int _handle; 14 | private int _cfg; 15 | private Stream _stdin = null; 16 | private Stream _stdout = null; 17 | private Process _process; 18 | private bool _isDisposed = false; 19 | 20 | public UnixPsuedoTerminal(Process process, int handle, int cfg, Stream stdin, Stream stdout) 21 | { 22 | _process = process; 23 | 24 | _handle = handle; 25 | _stdin = stdin; 26 | _stdout = stdout; 27 | 28 | _cfg = cfg; 29 | } 30 | 31 | public static void Trampoline(string[] args) 32 | { 33 | if (args.Length > 2 && args[0] == "--trampoline") 34 | { 35 | Native.setsid(); 36 | Native.ioctl(0, Native.TIOCSCTTY, IntPtr.Zero); 37 | Native.chdir(args[1]); 38 | 39 | var envVars = new List(); 40 | var env = Environment.GetEnvironmentVariables(); 41 | 42 | foreach (var variable in env.Keys) 43 | { 44 | if (variable.ToString() != "TERM") 45 | { 46 | envVars.Add($"{variable}={env[variable]}"); 47 | } 48 | } 49 | 50 | envVars.Add("TERM=xterm-256color"); 51 | envVars.Add(null); 52 | 53 | var argsArray = args.Skip(3).ToList(); 54 | argsArray.Add(null); 55 | 56 | Native.execve(args[2], argsArray.ToArray(), envVars.ToArray()); 57 | } 58 | else 59 | { 60 | return; 61 | } 62 | } 63 | 64 | public void Dispose() 65 | { 66 | if (!_isDisposed) 67 | { 68 | _isDisposed = true; 69 | _stdin?.Dispose(); 70 | _stdout?.Dispose(); 71 | 72 | // TODO close file descriptors and terminate processes? 73 | } 74 | } 75 | 76 | public async Task ReadAsync(byte[] buffer, int offset, int count) 77 | { 78 | return await _stdout.ReadAsync(buffer, offset, count); 79 | } 80 | 81 | public async Task WriteAsync(byte[] buffer, int offset, int count) 82 | { 83 | if (buffer.Length == 1 && buffer[0] == 10) 84 | { 85 | buffer[0] = 13; 86 | } 87 | 88 | await Task.Run(() => 89 | { 90 | var buf = Marshal.AllocHGlobal(count); 91 | Marshal.Copy(buffer, offset, buf, count); 92 | Native.write(_cfg, buf, count); 93 | 94 | Marshal.FreeHGlobal(buf); 95 | }); 96 | } 97 | 98 | public void SetSize(int columns, int rows) 99 | { 100 | Native.winsize size = new Native.winsize(); 101 | int ret; 102 | size.ws_row = (ushort)(rows > 0 ? rows : 24); 103 | size.ws_col = (ushort)(columns > 0 ? columns : 80); 104 | 105 | var ptr = Native.StructToPtr(size); 106 | 107 | ret = Native.ioctl(_cfg, Native.TIOCSWINSZ, ptr); 108 | 109 | Marshal.FreeHGlobal(ptr); 110 | 111 | var error = Marshal.GetLastWin32Error(); 112 | } 113 | 114 | public struct winsize 115 | { 116 | public ushort ws_row; /* rows, in characters */ 117 | public ushort ws_col; /* columns, in characters */ 118 | public ushort ws_xpixel; /* horizontal size, pixels */ 119 | public ushort ws_ypixel; /* vertical size, pixels */ 120 | }; 121 | 122 | public Process Process => _process; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/AvalonStudio.Terminals/Unix/UnixPsuedoTerminalProvider.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Win32.SafeHandles; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Diagnostics; 5 | using System.IO; 6 | using System.Runtime.InteropServices; 7 | 8 | namespace AvalonStudio.Terminals.Unix 9 | { 10 | public class UnixPsuedoTerminalProvider : IPsuedoTerminalProvider 11 | { 12 | public IPsuedoTerminal Create(int columns, int rows, string initialDirectory, string environment, string command, params string[] arguments) 13 | { 14 | var fdm = Native.open("/dev/ptmx", Native.O_RDWR | Native.O_NOCTTY); 15 | 16 | var res = Native.grantpt(fdm); 17 | res = Native.unlockpt(fdm); 18 | 19 | var namePtr = Native.ptsname(fdm); 20 | var name = Marshal.PtrToStringAnsi(namePtr); 21 | var fds = Native.open(name, (int)Native.O_RDWR); 22 | 23 | var fileActions = Marshal.AllocHGlobal(1024); 24 | Native.posix_spawn_file_actions_init(fileActions); 25 | res = Native.posix_spawn_file_actions_adddup2(fileActions, (int)fds, 0); 26 | res = Native.posix_spawn_file_actions_adddup2(fileActions, (int)fds, 1); 27 | res = Native.posix_spawn_file_actions_adddup2(fileActions, (int)fds, 2); 28 | res = Native.posix_spawn_file_actions_addclose(fileActions, (int)fdm); 29 | res = Native.posix_spawn_file_actions_addclose(fileActions, (int)fds); 30 | 31 | 32 | var attributes = Marshal.AllocHGlobal(1024); 33 | res = Native.posix_spawnattr_init(attributes); 34 | 35 | var envVars = new List(); 36 | var env = Environment.GetEnvironmentVariables(); 37 | 38 | foreach(var variable in env.Keys) 39 | { 40 | if(variable.ToString() != "TERM") 41 | { 42 | envVars.Add($"{variable}={env[variable]}"); 43 | } 44 | } 45 | 46 | envVars.Add("TERM=xterm-256color"); 47 | envVars.Add(null); 48 | 49 | var path = System.Reflection.Assembly.GetEntryAssembly().Location; 50 | var argsArray = new List { "dotnet", path, "--trampoline", initialDirectory, command }; 51 | argsArray.AddRange(arguments); 52 | argsArray.Add(null); 53 | 54 | res = Native.posix_spawnp(out var pid, "dotnet", fileActions, attributes, argsArray.ToArray(), envVars.ToArray()); 55 | 56 | var stdin = Native.dup(fdm); 57 | var process = Process.GetProcessById((int)pid); 58 | return new UnixPsuedoTerminal(process, fds, stdin, new FileStream(new SafeFileHandle(new IntPtr(stdin), true), FileAccess.Write), new FileStream(new SafeFileHandle(new IntPtr(fdm), true), FileAccess.Read)); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/AvalonStudio.Terminals/Win32/Win32PsuedoTerminal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.IO; 4 | using System.Threading.Tasks; 5 | using static winpty.WinPty; 6 | 7 | namespace AvalonStudio.Terminals.Win32 8 | { 9 | public class Win32PsuedoTerminal : IPsuedoTerminal 10 | { 11 | private IntPtr _handle = IntPtr.Zero; 12 | private IntPtr _err = IntPtr.Zero; 13 | private IntPtr _cfg = IntPtr.Zero; 14 | private IntPtr _spawnCfg = IntPtr.Zero; 15 | private Stream _stdin = null; 16 | private Stream _stdout = null; 17 | private Process _process; 18 | private bool _isDisposed = false; 19 | 20 | public Win32PsuedoTerminal(Process process, IntPtr handle, IntPtr cfg, IntPtr spawnCfg, IntPtr err, Stream stdin, Stream stdout) 21 | { 22 | _process = process; 23 | 24 | _handle = handle; 25 | _stdin = stdin; 26 | _stdout = stdout; 27 | 28 | _cfg = cfg; 29 | _spawnCfg = spawnCfg; 30 | _err = err; 31 | } 32 | 33 | public void Dispose() 34 | { 35 | if (!_isDisposed) 36 | { 37 | _isDisposed = true; 38 | _stdin?.Dispose(); 39 | _stdout?.Dispose(); 40 | winpty_config_free(_cfg); 41 | winpty_spawn_config_free(_spawnCfg); 42 | winpty_error_free(_err); 43 | winpty_free(_handle); 44 | } 45 | } 46 | 47 | public async Task ReadAsync(byte[] buffer, int offset, int count) 48 | { 49 | return await _stdout.ReadAsync(buffer, offset, count); 50 | } 51 | 52 | public async Task WriteAsync(byte[] buffer, int offset, int count) 53 | { 54 | if (buffer.Length == 1 && buffer[0] == 10) 55 | { 56 | buffer[0] = 13; 57 | } 58 | 59 | await _stdin.WriteAsync(buffer, offset, count); 60 | } 61 | 62 | public void SetSize(int columns, int rows) 63 | { 64 | if (_cfg != IntPtr.Zero && columns >= 1 && rows >= 1) 65 | { 66 | winpty_set_size(_handle, columns, rows, out _err); 67 | } 68 | } 69 | 70 | public Process Process => _process; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/AvalonStudio.Terminals/Win32/Win32PsuedoTerminalProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.IO; 4 | using System.IO.Pipes; 5 | using System.Runtime.InteropServices; 6 | using static winpty.WinPty; 7 | 8 | namespace AvalonStudio.Terminals.Win32 9 | { 10 | public class Win32PsuedoTerminalProvider : IPsuedoTerminalProvider 11 | { 12 | private static IntPtr TryGetHandle(Process p) 13 | { 14 | var result = IntPtr.Zero; 15 | 16 | try 17 | { 18 | result = p.Handle; 19 | } 20 | catch (Exception e) 21 | { 22 | 23 | } 24 | 25 | return result; 26 | } 27 | 28 | public IPsuedoTerminal Create(int columns, int rows, string initialDirectory, string environment, string command, params string[] arguments) 29 | { 30 | var cfg = winpty_config_new(WINPTY_FLAG_COLOR_ESCAPES, out IntPtr err); 31 | winpty_config_set_initial_size(cfg, columns, rows); 32 | 33 | var handle = winpty_open(cfg, out err); 34 | 35 | if (err != IntPtr.Zero) 36 | { 37 | System.Console.WriteLine(winpty_error_code(err)); 38 | return null; 39 | } 40 | 41 | string exe = command; 42 | string args = String.Join(" ", arguments); 43 | string cwd = initialDirectory; 44 | 45 | var spawnCfg = winpty_spawn_config_new(WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN, exe, args, cwd, environment, out err); 46 | if (err != IntPtr.Zero) 47 | { 48 | System.Console.WriteLine(winpty_error_code(err)); 49 | return null; 50 | } 51 | 52 | var stdin = CreatePipe(winpty_conin_name(handle), PipeDirection.Out); 53 | var stdout = CreatePipe(winpty_conout_name(handle), PipeDirection.In); 54 | 55 | if (!winpty_spawn(handle, spawnCfg, out IntPtr process, out IntPtr thread, out int procError, out err)) 56 | { 57 | System.Console.WriteLine(winpty_error_code(err)); 58 | return null; 59 | } 60 | 61 | var id = GetProcessId(process); 62 | 63 | var terminalProcess = Process.GetProcessById(id); 64 | 65 | return new Win32PsuedoTerminal(terminalProcess, handle, cfg, spawnCfg, err, stdin, stdout); 66 | } 67 | 68 | [DllImport("kernel32.dll")] 69 | static extern int GetProcessId(IntPtr handle); 70 | 71 | private Stream CreatePipe(string pipeName, PipeDirection direction) 72 | { 73 | string serverName = "."; 74 | 75 | if (pipeName.StartsWith("\\")) 76 | { 77 | int slash3 = pipeName.IndexOf('\\', 2); 78 | 79 | if (slash3 != -1) 80 | { 81 | serverName = pipeName.Substring(2, slash3 - 2); 82 | } 83 | 84 | int slash4 = pipeName.IndexOf('\\', slash3 + 1); 85 | 86 | if (slash4 != -1) 87 | { 88 | pipeName = pipeName.Substring(slash4 + 1); 89 | } 90 | } 91 | 92 | var pipe = new NamedPipeClientStream(serverName, pipeName, direction); 93 | 94 | pipe.Connect(); 95 | 96 | return pipe; 97 | } 98 | } 99 | } 100 | --------------------------------------------------------------------------------