Hybrid native/web Android app with dwindows and Free Pascal

19 Apr 2024

Coming back to dwindows after a (long) break, I've now implemented an Android hybrid native/web demo/exercise app in Free Pascal, using a webview widget for the user interface, with bi-directional Javascript ↔ Pascal in-process RPC.

The main body of the code loads in-app static content packaged into the APK file, then binds the names HostSayHello and HostExit for callback to Pascal from Javascript. Said Pascal code can in turn invoke Javascript to manipulate the DOM.

  webwidget := dw_html_new(1001); // 1001 is an identifier
  if webwidget <> nil then
    begin
      dw_box_pack_start(mainwindow, webwidget, 0, 0, DW_TRUE, DW_TRUE, 0);
      dw_html_url(webwidget, 'file:///android_asset/index.html');
      dw_html_javascript_add(webwidget, 'HostSayHello');
      dw_html_javascript_add(webwidget, 'HostExit');
      dw_signal_connect(webwidget, DW_SIGNAL_HTML_MESSAGE, @html_message_callback, nil);
    end;

Here's an animated GIF of the app running in the Android emulator on my laptop:


Free Pascal Android hybrid native/web app with dwindows

Tags: Android, dwindows, Pascal

dwindows with Free Pascal for iOS and Android

30 Dec 2022

Dynamic Windows, aka dwindows, is a cross-platform GUI application toolkit with a C API. dwindows works with GTK2, GTK3, Windows API, OS/2 Presentation Manager, Cocoa (macOS and iOS), and Android.

Having a C API makes it possible to build bindings for other programming languages, like Pascal. Free Pascal already powers Lazarus, a rapid application development (RAD) tool for building cross-platform GUI applications in Pascal. There is a large overlap between the platforms covered by dwindows and Lazarus, with the notable exception of iOS.

Free Pascal is able to produce binaries for many processor architectures, including for the ARM CPU used in Android and iOS devices. For Lazarus, there is the excellent LAMW - the Lazarus Android Module Wizard - which extends Lazarus for building Android apps with native look-and-feel.

What Free Pascal and Lazarus don't have currently is a good iOS story. And here the C API of dwindows offers possibilities. Perhaps, it becomes feasible to build Android and iOS apps using a common Pascal code base for most of the required functionality.

At the moment, I've put together the littlest Pascal demo mobile app for dwindows. Here's a screenshot, Android phone on the left, iPhone on the right.

Pascal Hello World on Android and iOS with dwindows

The code putting up the message box is the same:

procedure dwmain(argc: Integer; argv: PPChar); cdecl;
begin
  dw_init(TRUE, argc, argv);
  dw_messagebox('Free Pascal + dwindows', DW_MB_OK or DW_MB_INFORMATION, 'Hello from Pascal, courtesy of dwindows!');
  dw_exit(0);
end;

Scaffolding is different: on Android, the Pascal program is built as a shared library, dwindows is another shared library, while on iOS the Pascal program becomes an executable and dwindows is a framework.

My next step is to build a demo with more GUI widgets.

Beyond the short term, as I see it, the best approach to building Android/iOS apps with a common Pascal code base is to use HTML/CSS through the web widget, like what my fpwebview does for the desktop platforms. (Indeed, a recurring question for webview is whether it works for Android and iOS.)

Using, say, mORmot to do 'server-side rendering' fully, the app requires minimal to zero Javascript on the front end. Concerns about latency, state management, etc. that motivated the creation of fancy Javascript front-end frameworks go away as the app is effectively a single-user single-browser tab on-device web app.

Of course, the modern mobile phone is a powerful device, and a mobile app's requirements could go far beyond simply showing stuff on screen and accepting input via touch and virtual keyboard. To build fully fledged iOS apps with Free Pascal, there is probably no shortcut to building Pascal interfaces to the platform APIs for cameras, audio I/O, accelerometer, GPS, etc.

Tags: Android, dwindows, iOS, Pascal

CogDroid

17 Mar 2013

Recently I bought an Android tablet, the 7" Yuandao N70HD. The tablet runs Jelly Bean, and comes "almost-rooted", meaning root is readily accessible, but not fully enabled. Searching the web reveals the existence of Windows EXEs and DOS batch files that enable full root access on the device. Inspecting one of these batch files shows that it simply copies certain native executables from the Android SDK to the device.

Now, perhaps root is not necessary to run CogDroid. :-) Anyhow, here's a screenshot of the tablet showing CogDroid running the image that powers this website. VNC access into the image works too.

Tags: Android, CogVM, tablet