Git Product home page Git Product logo

flutter_pty's Introduction

flutter_pty

ci pub points

This is an experimental package to explore the possibilities of using native code to implement PTY instead of pure FFI and blocking isolates. It's expected to be more stable than the current implementation (pty).

Platform

Linux macOS Windows Android
✔️ ✔️ ✔️ ✔️

Quick start

import 'package:flutter_pty/flutter_pty.dart';

final pty = Pty.start('bash');

pty.output.listen((data) => ...);

pty.write(Utf8Encoder().convert('ls -al\n'));

pty.resize(30, 80);

pty.kill();

Project stucture

This template uses the following structure:

  • src: Contains the native source code, and a CmakeFile.txt file for building that source code into a dynamic library.

  • lib: Contains the Dart code that defines the API of the plugin, and which calls into the native code using dart:ffi.

  • platform folders (android, ios, windows, etc.): Contains the build files for building and bundling the native code library with the platform application.

Buidling and bundling native code

The pubspec.yaml specifies FFI plugins as follows:

  plugin:
    platforms:
      some_platform:
        ffiPlugin: true

This configuration invokes the native build for the various target platforms and bundles the binaries in Flutter applications using these FFI plugins.

This can be combined with dartPluginClass, such as when FFI is used for the implementation of one platform in a federated plugin:

  plugin:
    implements: some_other_plugin
    platforms:
      some_platform:
        dartPluginClass: SomeClass
        ffiPlugin: true

A plugin can have both FFI and method channels:

  plugin:
    platforms:
      some_platform:
        pluginClass: SomeName
        ffiPlugin: true

The native build systems that are invoked by FFI (and method channel) plugins are:

  • For Android: Gradle, which invokes the Android NDK for native builds.
    • See the documentation in android/build.gradle.
  • For iOS and MacOS: Xcode, via CocoaPods.
    • See the documentation in ios/flutter_pty.podspec.
    • See the documentation in macos/flutter_pty.podspec.
  • For Linux and Windows: CMake.
    • See the documentation in linux/CMakeLists.txt.
    • See the documentation in windows/CMakeLists.txt.

Binding to native code

To use the native code, bindings in Dart are needed. To avoid writing these by hand, they are generated from the header file (src/flutter_pty.h) by package:ffigen. Regenerate the bindings by running flutter pub run ffigen --config ffigen.yaml.

Invoking native code

Very short-running native functions can be directly invoked from any isolate. For example, see sum in lib/flutter_pty.dart.

Longer-running functions should be invoked on a helper isolate to avoid dropping frames in Flutter applications. For example, see sumAsync in lib/flutter_pty.dart.

Flutter help

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

flutter_pty's People

Contributors

devmil avatar mengyanshou avatar xtyxtyx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

flutter_pty's Issues

Dart project?

I get this:

C:/Users/jens/scoop/apps/dart/3.1.0/bin/dart.exe --enable-asserts C:\Users\jens\MyProjects\flutter-learn\dart-pty-test\bin\dart_pty_test.dart
Unhandled exception:
Invalid argument(s): Failed to load dynamic library 'flutter_pty.dll': error code 126
#0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:43)
#1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:22:12)
#2      _dylib.<anonymous closure> (package:flutter_pty/flutter_pty.dart:20:27)
#3      _dylib (package:flutter_pty/flutter_pty.dart:23:2)
#4      _dylib (package:flutter_pty/flutter_pty.dart)
#5      _bindings (package:flutter_pty/flutter_pty.dart:25:38)
#6      _bindings (package:flutter_pty/flutter_pty.dart)
#7      _init.<anonymous closure> (package:flutter_pty/flutter_pty.dart:28:10)
#8      _init (package:flutter_pty/flutter_pty.dart:29:2)
#9      _init (package:flutter_pty/flutter_pty.dart)
#10     _ensureInitialized (package:flutter_pty/flutter_pty.dart:32:7)
#11     new Pty.start (package:flutter_pty/flutter_pty.dart:57:5)
#12     main (file:///C:/Users/jens/MyProjects/flutter-learn/dart-pty-test/bin/dart_pty_test.dart:8:19)
#13     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:296:19)
#14     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189:12)

Process finished with exit code 255

with:

import 'dart:convert';

import 'package:flutter_pty/flutter_pty.dart';


void main() {
  final pty = Pty.start('powershell');

  pty.output.listen((event) { 
    print(utf8.decode(event));
  });


  pty.resize(30, 80);

  pty.kill();
}

Permission denied on android 11

I tried your library and used in app to run exe binary but always permission denied, I did chmod 777 but it doesn't work how to do it?

Windows OS conhost problem

I think you should use "ClosePseudoConsole" in "flutter_pty_win.c" , in function start_wait_exit_thread , here is the code:
static DWORD WINAPI wait_exit_thread(LPVOID arg)
{
WaitExitOptions *options = (WaitExitOptions *)arg;

DWORD exit_code = 0;

WaitForSingleObject(options->pid, INFINITE);

GetExitCodeProcess(options->pid, &exit_code);

CloseHandle(options->pid);
CloseHandle(options->hMutex);
ClosePseudoConsole(options->hpty);      // To close the PseudoConsole

Dart_PostInteger_DL(options->port, exit_code);

return 0;

}

static void start_wait_exit_thread(HANDLE pid, Dart_Port port, HANDLE mutex, HPCON hpty)
{
WaitExitOptions *options = malloc(sizeof(WaitExitOptions));

options->pid = pid;
options->port = port;
options->hMutex = mutex;
options->hpty = hpty;  //  add htpy

DWORD thread_id;

HANDLE thread = CreateThread(NULL, 0, wait_exit_thread, options, 0, &thread_id);

if (thread == NULL)
{
    free(options);
}

}

If not close the Pseudo , when close the port , there is a conhost process can not be kill.

not working smoothly on windows. can't ping google.com and use ffmpeg. need help

hello friends, I have a project which contains a windows terminal (CMD).

The following packages I use:
https://pub.dev/packages/flutter_pty
https://pub.dev/packages/xterm

However, there are several obstacles including:
1.⁠ ⁠can't ping google.com (gambar 1)
gambar 1
2.⁠ ⁠can't stream YouTube via ffmpeg (gambar 2)
gambar 2

out of curiosity I tried to clone the example project from the packages I was using
https://github.com/TerminalStudio/flutter_pty
without changing anything, but the result is the same (gambar 3)
gambar 3

anyone can help?

Windows Powershell Error

When I use "powershell.exe" instead of "cmd.exe", an error is reported.

Internal Windows PowerShell error. Loading managed Windows PowerShell failed with error 8009001d

Even if I start with cmd.exe and enter "powershell" in it, the same error is reported

My Code:

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_pty/flutter_pty.dart';
import 'package:xterm/xterm.dart';

class TerminalTabView extends StatefulWidget {
  const TerminalTabView({super.key});

  @override
  State<TerminalTabView> createState() => _TerminalTabViewState();
}

class _TerminalTabViewState extends State<TerminalTabView> {
  final TerminalController _terminalController = TerminalController();
  late final Pty _pty;
  late final Terminal _terminal = Terminal(maxLines: 10000);

  @override
  void initState() {
    WidgetsBinding.instance.endOfFrame.then(
      (_) {
        if (mounted) {
          _startPty();
        }
      },
    );
  }

  void _startPty() {
    _pty = Pty.start(
      shell,
      columns: _terminal.viewWidth,
      rows: _terminal.viewHeight,
    );

    _pty.output
        .cast<List<int>>()
        .transform(const Utf8Decoder())
        .listen(_terminal.write);

    _pty.exitCode.then((code) {
      _terminal.write('the process exited with exit code $code');
    });

    _terminal.onOutput = (data) {
      _pty.write(const Utf8Encoder().convert(data));
    };

    _terminal.onResize = (w, h, pw, ph) {
      _pty.resize(h, w);
    };
  }

  String get shell {
    if (Platform.isMacOS || Platform.isLinux) {
      return Platform.environment['SHELL'] ?? 'bash';
    }
    if (Platform.isWindows) {
      return 'cmd.exe';
    }
    return 'sh';
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: const Color.fromRGBO(30, 30, 30, 1.0),
      child: Padding(
        padding: const EdgeInsets.all(20),
        child: TerminalView(
          _terminal,
          controller: _terminalController,
          autofocus: true,
          onSecondaryTapDown: (details, offset) async {},
        ),
      ),
    );
  }
}

Errors on Linux

First, there seems to be a typo in CMakeLists.txt which calls for "flutter_pty.cc" but the file is called "flutter_pty.c"

After fixing that, building on Linux fails with a missing library:

> flutter run -d linux
Launching lib/main.dart on Linux in debug mode...
/home/user/Development/flutter_pty/example/linux/flutter/ephemeral/.plugin_symlinks/flutter_pty/src/flutter_pty.h:11:10: fatal error: 'util.h' file not found
Building Linux application...                                           
Exception: Build process failed

Can't get input to render using xterm + flutter_pty

Hi there,

I was excited to discover xterm and flutter_pty today. Thanks for all of your work.

I'm trying to connect xterm and flutter_pty and I'm having a difficult time getting the input to render to the screen. I'm guessing I haven't implemented LocalTerminalBackend#out correctly.

Also, wherever I try to call pty.output.listen I get the following error:

Exception has occurred.
StateError (Bad state: Stream has already been listened to.)

Here's my code:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_pty/flutter_pty.dart';
import 'package:xterm/flutter.dart';
import 'package:xterm/xterm.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'xterm / flutter_pty demo',
      home: LocalTerminal(),
    );
  }
}

class LocalTerminalBackend implements TerminalBackend {
  LocalTerminalBackend();

  final pty = Pty.start('bash');

  @override
  void init() {
    // NOOP
  }

  @override
  void write(String input) => pty.write(const Utf8Encoder().convert(input));

  @override
  Stream<String> get out =>
      pty.output.map((data) => String.fromCharCodes(data));

  @override
  void resize(int width, int height, int pixelWidth, int pixelHeight) =>
      pty.resize(width, height);

  @override
  void ackProcessed() {
    // NOOP
  }

  @override
  Future<int> get exitCode => pty.exitCode;

  @override
  void terminate() => pty.kill();
}

class LocalTerminal extends StatefulWidget {
  const LocalTerminal({Key? key}) : super(key: key);

  @override
  _LocalTerminalState createState() => _LocalTerminalState();
}

class _LocalTerminalState extends State<LocalTerminal> {
  late Terminal terminal;

  @override
  void initState() {
    super.initState();
    terminal = Terminal(maxLines: 10000, backend: LocalTerminalBackend());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: TerminalView(
          terminal: terminal,
        ),
      ),
    );
  }
}

I'm sure I'm doing something wrong. Any help getting this working is greatly appreciated. Thanks!

AGP8 mandates build.gradle to add namespace

Add the following code in android{} of build.gradle

// Conditional for compatibility with AGP <4.2.
if (project.android.hasProperty("namespace")) {
    namespace 'com.example.flutter_pty'
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.