X applications on Ubuntu Phone


All the X software

So yeah, Mir... Canonical's very own display server, to replace X server entirely in the future. Just that, all the great software currently existing, is made for X server. Not for Mir. Simple as that. Well, worry not. First of all, you can still put an executable that is written for X server on the phone. As long as it is ARM linux binary, it is a valid one on the Ubuntu Phone as well. True, you cannot just execute it and expect to work auto-magically, but there is quite a lot of ways you can make it work with some extra work. Generally there are 3 major ways to run X applications on the phone:
  1. Right on the phone via XMir
  2. Via a compatibility layer called Libertine
  3. Off the phone but onto the Desktop via SSH with X forwarding
  4. In the future directly: Ubuntu devs demo gtk apps running in Mir
XMir to the rescue
So Mir is a totally new display server somewhat based on Wayland, but it does come with a layer for compatibility with X. It is called XMir. It is said to be under active development, and expected to be enhanced over time, yet it is already shipped with Ubuntu Touch system on Ubuntu phones and tablets. XMir allows for running X application directly on the phone, inside a full screen XMir context. So you can put a binary compiled for X on the phone, create a launcher for it (.desktop file), add 2 special settings to it, so the phone knows to run it via XMir, and there you go! An X app running on the Ubuntu Phone with Mir!
If you already have your X app inside your phone, you need to make sure that the dedicated launcher contains these few important settings among the standard ones:

 X-Ubuntu-Touch=true
 X-Ubuntu-XMir-Enable=true
 Terminal=false
 Type=Application

Then put your launcher to ~/.local/share/applications/ as it does not need setting your root partition to writtable.
And if you need an OSK (on-screen keyboard) in your app, you need two important packages installed in your phone: maliit-inputcontext-gtk3 and maliit-inputcontext-gtk2:

 $ sudo mount -o remount,rw /
 $ sudo apt-get -q -y install maliit-inputcontext-gtk3 maliit-inputcontext-gtk2
 $ sudo mount -o remount,rw /

There are some limitations to this method though. Lots of X applications strongly depend on 3rd party libraries. Some are very standard libraries on Linux for Desktop, being part of default installations. But these will most likely not be present on Ubuntu Phone, especially if they are related to X stack. True, you could try to list every single dependency of your app, find it and install manually. But this can be hectic and maybe not even possible as some libraries might not be available for ARM. You could try to compile everything from source, but that could turn out even more hectic. Your best choice would be to go with this method when your app either doesn't depend on any extra libraries, or you are lucky that all extra libraries are supplied with the application.

Libertine to the rescue

Libertine is, after its info page, "a confined sandbox where you can install deb-packaged X11-based Ubuntu applications have have them run under Unity 8.". Well, you can actually install any X software in it, even not deb-packaged, but that is going into finer detail. To simply put it, Libertine is something you install on your Ubuntu phone, that will allow you to instrall and run X applications. Also, by installing Libertine, you will install a special Scope (you still need to enable it from the scopes list), which will show all your X applications installed inside Libertine as icons, that you just click on, and the corresponding app will just open on your phone via XMir, all automatically. Again, if you'd like to be more technical about it, the aforementioned scope would actually list not applications inside Libertine, but Launchers (.desktop files). So whatever you install inside Libertine, you also need to have a corresponding launcher to have it enlisted within Libertine's scope. 
Now, a bit more directly. But also more technically. Libertine is a container. Currently both chroot and LXC modes are supported. So the approach is to actually create a jail environment and install X software into that jail, then  have an integrated scope that lists apps from that jail and via clicking an icon, launches corresponding one (ie. sudo chroot ... some_app) inside XMir session. 
One characteristic of Libertine is, that the container is running exactly the same distribution of Ubuntu as the host. Libertine makers see this as an advantage for ensuring maximal compatibility. In my opinion, however, it gives far more elasticity if you could run anything you like inside a container. And you can, because your Ubuntu Phone is just a normal Ubuntu, and you can simply create your very own container (ie. using debootstrap) and put any distribution you like into it. You can also create launchers directly into your phone, so your contained app can be listed with native apps, rather then on a dedicated scope. Or, if you are scope developer, you can create your own scope to go with your own container. The bottom line is, if you are not the type of guy who likes DIY, you can just enjoy Libertine as a ready solution.

If you'd like to know about hand-crafting your own container and launching its X applications on the phone, be on the lookout for my future Article "Easy containers on Ubuntu Touch with qemu-debootstrap".

SSH with X Forwarding

This feature is absolutely mind-blowing. Just in case you are reading it without understanding what it means: X forwarding means, that after you connect from one Linux host to another, over SSH, you can run X application on a remote host in such a way, that it will render into your local screen, and will accept your locally connected mouse and keyboard as an input. Imagine you have Firefox installed inside your phone. Then you connect to your phone from your Ubuntu desktop over SSH. Then, inside the SSH session, you execute Firefox, and it appears on your Desktop! Just that, it actually is running inside your phone! a little bit like a remote desktop, just that instead of opening an entire remote desktop in a local window, you are only remotely displaying the application (all of its windows).

If you already are familiar with X forwarding over SSH, here is something you might not have thought about: Although Ubuntu Phone runs on Mir and lacks X stack, but if you are connected over SSH with X forwarding, you are basically making your local X stack available remotely to your phone. So an X application installed inside your phone, suddenly gets X stack to run against. This is actually a HUGE deal! It means, that you can install and carry on you a ineptitude of software that you access in a remote fashion on a Linux Desktop at home or work. You travel from one location to another, and your entire software stack travels with you, in your phone. Once you get seated in front of your Desktop, you just connect witlessly, and run your programs off your phone onto your desktop's big screen monitor. Also, you get to use your desktop's mouse and keyboard, instead of trying your best with the phone's on-screen touch keyboard. This means, that you can enjoy applications that have UI not suitable for small (physically) screens or touch interaction with the user. Even though it is possible to run Thunderbird directly on the screen of your Ubuntu Phone, but it will be a hassle to use tiny buttons, read tiny fonts, scroll with tiny scrollbars, or type with the OSK (on-screen keyboard). But given that you do have Linux Desktop anyways, this is where you are consuming your apps. The magical detail is, that they are actually running inside your phone, which is traveling with you wherever you go!

Read more in my article Ubuntu Phone and X Forwarding over SSH as an ultimate Phone and Desktop integration.

Proof of concept

Time to show you some working example. Here, just for demo purpose, a VERY simple GTK application written in Lazarus. Actually, I designed and wrote this small demo in Lazarus running in my Ubuntu Phone, but I did it using SSH with X forwarding, for convenience, so I was working on my Ubuntu Desktop screen:

 I actually have Lazarus installed inside a chroot jail on the phone, which internally runs Ubuntu 14.04:
And then the Lazarus from my Ubuntu Phone came up on my Ubuntu Desktop screen:

 I even can compile and run the demo app over SSH session:
So as you can see, the demo is a simple form that prints out some metrics, and has a button, which if you click it, will show a message from the edit field above in a popup window.
Also notice, that the screen DPI is shown as 96, which is true for the screen I am using, but unfortunately the very same DPI is detected when I run this demo app on the phone's screen. This means that I will not be able to use Lazarus automatic HighDPI resizing. Perhaps it is the XMir that lies to the application about the actual DPI, but for now, we will not be taking care of that, as this is only a proof of concept that X app can run on the phone, and not discussion about UI.

Next step was to copy the binary file demo out from the container and directly under the Phone's ~/.local/xmir/demo/demo, a location I chose arbitrarily, you could choose whatever else.

We will also need a launcher if we want to be able to see the app icon among phone's apps, so here is how my ~/.local/xmir/demo/demo.desktop launcher looks like (I have it in the same folder but I will make a link):

 [Desktop Entry]
 Name=LazarusApp Demo
 Comment=Lazarus developed app, made to run via xmir
 X-Ubuntu-Touch=true
 X-Ubuntu-XMir-Enable=true
 Exec=/home/phablet/.local/xmir/demo/demo.sh /home/phablet/.local/xmir/demo/demo
 Icon=/home/phablet/.local/xmir/demo/demo.png
 Terminal=false

 Type=Application

As you probably noticed I do not execute the binary directly, but via a ~/.local/xmir/demo/demo.sh wrapper. The reason is, because in that wrapper I want to set up few environmental variables, that makes sure that the native OSK (on-screen keyboard) will work properly. Here is the content of the wrapper:



 export QT_IM_MOUDULE=maliitphablet

 export GTK_IM_MODULE=maliitphablet

 export QT_DEVICE_PIXEL_RATIO=

 exec $@


the variable QT_DEVICE_PIXEL_RATIO is something I found on lazarus forum, someone claimed it can mess the OSK if it was set to anything, so I just unset it as a precaution.

Now, I need to create a symbolic link of the launcher so it can go to a place where the phone looks for applications:

 $ ln -s /home/phablet/.local/xmir/demo/demo.desktop 
   /home/phablet/.local/share/applications/demo.desktop

now the app will be listed on the phone, in my case next to the Libertine icon (sometimes in order to refresh the Apps scope, you need to use Search feature and type in the name of your app):
and I can run it directly on the phone:
Even the OSK works well:

And this demo works so well, because as a Lazarus application, it is a self-contained executable, it needs not to link to any external libraries other than default X and GTK stacks which XMir emulates. As long as your own app is either self-contained, or you are able to supply on dependencies along, you will be same successful running it on the Ubuntu Phone!
Well, there are few drawbacks. One is that there is no windows manager when running X apps inside XMir. Perhaps this will get added in the future. For now this is being handled by Libertine, where matchbox window manager is being used. If you could install some lightweight X window manager inside the Ubuntu phone directly, then you should be also able to use it in XMir, but this is something I have not yet tried. If you are the developer, then things are easier, because you may create your own window decorations, and not depend on the system-supplied ones.
Another drawback is that you really only want X apps that are self-contained and can entirely be deployed somewhere under your home directory. Awfully lots of available X applications aren't like that. They depend on ability to install themselves and save their additional data files under various places of the filesystem outside of the home directory. On Ubuntu Touch devices, the root partition is both space-limited and read-only. Also, as of now, things you add to it, are not persistent between firmware updates. The things you keep under your home directory, however, are. Here again, Libertine has an advantage of actually keeping a chroot/LXC jail under home directory and then moving everything into that jail. But you are free to make your very own container, like debootstrap, and then you can deploy your app into there, while still being able to run it locally on the phone's screen via XMir. The difference is, however, that the chroot jail is isolated from your host, and thus, some things you won't be able to do, at least, not in an easy way. For example, if you run your app directly, you can, say, launch the native camera app programatically.

If you want to conduct your own experiment, then you can download files of this particular demo from my google drive here: demo-gtk-for-ubuntu-phone.7z

If you are interested in Lazarus development, stay on the lookout for my upcoming article Lazarus development for Ubuntu Phone.




Comments

Popular posts from this blog

Delphi VCL/FMX for Linux before the official platform support

How I use Linux to write software for multiple target platforms

Easy containers on Ubuntu Touch with qemu-debootstrap