Info-Tech

Lessons Learnt Transferring a GTK Utility from Wander to Ada

Summary

Introduction

This doc describes some of my solutions following the a success transition of a desktop terminal emulator program from Wander and Gtk 2 to Ada and Gtk 3. The rewrite took tell over a duration of 4 months starting in November 2021.

Caveat

I attain no longer philosophize to be an knowledgeable in one thing that follows. In on the present time and age (2022) programming is a ardour for me and the total work described right here used to be undertaken as a ‘spare time’ project. Here’s an ‘understanding piece’ – I’m no longer going to lend a hand up every remark with references!

Utility Description

My DASHER terminal emulators provide free, originate supply and popular emulations of the most incessantly broken-down kinds of terminal (D200 and D210) that had been related to Data Frequent minicomputers of the 1970s thru to the pause of the century.

Having every serial and telnet attend, the emulators will be related every to legacy hardware and popular emulations of that hardware.

The main on this series of terminal emulators used to be written in C++ with a Qt GUI, then came a Java version, and thirdly a Wander version using a fairly nice Gtk 2 kit (binding).

Why Port?

DasherG used to be first launched in 2019 nonetheless by the pause of 2021 it had already change into advanced to create and defend. This used to be basically since the GUI toolkit broken-down had change into unmaintained leading to main dependency difficulties. Also, Gtk 2 is nearing the pause of its life.

There seemed as if it would possibly per chance perchance be two choices: both bag a new GUI toolkit and reuse the guts of the code, or change to a language with higher GUI attend.

Unfortunately, Wander would no longer discover a correct ‘account’ relating to GUI libraries. There is rarely any formally-supported library and most likely the most higher-identified ones are both very idiosyncratic (‘opinionated’ in Wander parlance) or immature. I did discover a severe attempt with Fyne and even though or no longer it’s promising it suffers every of the aforementioned problems.

It seemed that switching to a language with higher GUI attend would make sense, and I had some new journey discovering out Ada and porting some non-GUI Wander code to Ada.

Also, it used to be firm to me that I wished a language, toolkit, and bindings which had been at bother of be stable for some years. Ada, Gtk 3 and GtkAda seem to fit the invoice well.

After following the evolution of Wander for numerous years I bear that the core developers are abandoning the premise of it being a in point of fact frequent motive language and transferring their point of curiosity to being an graceful web-services and products/netops domain-recount language.

Consequence

DasherA v0.11 written in Ada is believed to be functionally unbiased like DasherG v0.10 – with some minor enhancements. Efficiency and memory consumption of the 2 applications is similar.

DasherA is now easy to create and defend on popular Debian-basically based totally methods.

As frequent, a rewrite of the code (which had some legacy cruft left over from its previous incarnations) gave a probability to crimson meat up the structure of this map a shrimp.

Forward of starting, I guesstimated a timeframe of about six weeks for this, in point of fact it took four months. On the other hand, the time I in point of fact find on hand for initiatives akin to this varies wildly. I mediate this effort would find taken about four weeks if I was working on it paunchy-time.

What used to be Straightforward?

Program Logic

Quite loads of the exact terminal emulation common sense is contained the terminal sources in every DasherG and DasherA. A immediate glimpse and terminal.tear and Terminal.adb will point to that they’re strikingly identical.

Gtk 2 to Gtk 3

Even supposing DasherG broken-down a series of the functions that modified considerably between Gtk 2 and Gtk 3, the transfer used to be made great more uncomplicated because of the the swish frequent knowledge on hand on-line.

What used to be Sophisticated?

Documentation

The Wander developers and community find carried out a truly correct job of building graceful documentation amenities and practices into the language and packages (libraries) magnificent from its inception.

Unfortunately the identical can not be mentioned for Ada.

I mediate there are tools a shrimp unbiased like godoc, nonetheless there are several of them – every using diversified conventions for organising and having access to any generated documentation.

It used to be traumatic to come upon some Ada packages which gave the impression to find very correct documentation embedded within the code, nonetheless no directions relating to how to generate that documentation in a readable create.

Ada has been around for a truly very lengthy time; unfortunately it’s no longer the least bit times decided to the unwary reader to what version of the language on-line documentation is referring. This caught me out when I read someplace about the Ada Character form being both easiest-displayable characters, or seven-bit (I neglect which). This would possibly per chance well per chance had been appropriate in some broken-down version of Ada, nonetheless on the present time the Character form involves all 8-bit values. I spent some time working around a restriction that would no longer educate, then throwing away that work.

Equally, plenty of the Gtk examples and paperwork on-line in point of fact test with Gtk 2 – no longer Gtk 3 (or 4!).

Finding Libraries

Allied to the above point, it used to be no longer easy to search out the serial I/O, networking and other identical packages. There would no longer seem like a on the total agreed-upon central repository for such knowledge in Ada.

Examples

It used to be a precise war to search out priceless publicly on hand instance code for most likely the most packages broken-down in DasherA.

It’s salutary to examine the series of code examples on rosettacode.org for the 2 languages. At the time of writing there are 1460 Wander examples and 865 Ada ones; surprising when one considers how fairly recently Wander used to be introduced.

A identical account applies to GitHub – basically based totally on their API there are 967876 Wander repositories and exact 4535 Ada ones as of eighth March 2022.

Swap of Mindset

I’d characterise Wander as a easy language, and Ada as rich one.

On the other hand, Wander’s obvious simplicity hides inevitable complexity when doing one thing non-trivial with Goroutines. Contrariwise, Ada’s complexity can blind one to the simplicity of such things as protected kinds and job entry functions.

Both Wander and Ada seem to me to amass an agnostic come to object-oriented programming. In every languages there are cases must you will be in a location to inquire your self whether a form is de facto an object. When transferring code over be decided about this, and persist along with your resolution! (There is a shrimp work well-known in DasherA removing some pointless ‘objects’.)

Also, look the Channels allotment below.

Debugging tasks

Urgh! I wish to investigate this extra; gdb appears to be like to be fairly insufficient on this regard as soon as a pair of tasks are alive to (cf. the swish delve debugger for Wander).

Traps for the Unwary

Continuously make certain to are discovering out up-to-date, decent documentation – must that you just would possibly per chance well bag it.

Rewrite, don’t Translate

When confronted with a form of code to transfer it’s tempting to attempt to translate between languages on a token-by-token foundation. Here’s rarely ever a correct advice. Purchase a step lend a hand, find in solutions what every kit, form or func does, then reimplement it using Ada idioms. (I got higher at this as this project progressed, I must silent revisit most likely the most well-known code that used to be ported.)

GtkAda and Initiatives

It appears that any time you employ ANY Glib, Gdk, or Gtk entity, you MUST both be in a callback, or be inside of a Gdk.Threads.Enter; ... Gdk.Threads.Leave; block. This involves such harmless-seeming things as Glib.Error, Gdk.Forms.Keysyms, and even Glib.Guint.

Failure to glimpse the above rule results in very unfamiliar behaviour – most continuously leading to a wreck. Any error messages produced would possibly per chance perchance no longer be priceless in pointing you to the underlying keep off.

I mediate the above moreover applies to entities referred to true thru Ada elaboration.

Goroutines vs. Initiatives

Goroutines are so light-weight and straightforward to make use of that it’s total to overuse them in Wander code. I guess that in GNAT Ada, tasks are implemented as O.S. threads – so they are less light-weight.

When transferring code from Wander to Ada the least bit times find in solutions whether a role is de facto wanted. Using fewer Goroutines/tasks would possibly per chance perchance well end result in extra purposeful code – despite the seductiveness of Wander’s amenities.

Having written this, I’m in a position to now take a look on the least one final job in DasherA that must most likely be rewritten as a protected form.

It’s miles a relentless surprise to me that GNAT Ada appears to be like to lack easy-to-use equivalents to Wander’s flee detector and deadlock reporting. For a language that areas so great emphasis on tasking, the lack of these popular tools feels fancy an infinite omission.

Channels

I wasted time recreating Wander-fancy channels in Ada. Things grew to change into great extra purposeful when I had the realisation that Wander’s channels are in point of fact surprisingly analogous to Ada’s job entries.

Error Handling

I mediate it’s broadly acknowledged that error coping with is a broken-down jam in Wander. Unfortunately this would possibly per chance occasionally per chance end result in habits which would possibly per chance perchance be laborious to shake off, even when given the unbiased correct thing about exceptions in Ada. Code is at bother of pray to be continuously restructured after you find got worked out your exception coping with strategy.

Pleasures and Disappointments

(These items matter to me as a ardour programmer: it’s far fundamental to me that programming would no longer change into a trial.)

Pleasures

  • Straightforward compiler and GUI toolkit installation on Mint Linux using frequent kit administration
  • Factual job attend a watch on in Ada
  • Like a flash create cases (feels virtually about as immediate as Wander)
  • Factual performance – even prior to any optimisation
  • Language attend in VS Code
  • Very unbiased correct (C-basically based totally) documentation and tutorials for Gtk 3
  • Data constructions are a joy in Ada – the higher-constrained kinds exact feel ‘magnificent’
  • Safe kinds are loads more uncomplicated to handle than mutexes in Wander.

Disappointments

  • Documentation…
  • Desiring to find /usr/lib/gcc/x86_64-linux-gnu/9/adainclude/ and /usr/portion/ada/adainclude/gtkada/ originate the least bit cases to consult the .adverts files in these directories
  • Lack of precise-world GtkAda code examples
  • Uncomfortable Ada job debugging below gdb
  • Lack of deadlock and flee condition detection (they are constructed-in with Wander)

License: Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)

©2022 Steve Merrony

Content Protection by DMCA.com

Back to top button