toreonify's notes

Linux environment on the go

This year I found myself in a situation where I only had an Android smartphone and no personal computer. And I still wanted to do weird stuff.

This is where Termux comes in to play.

I won't be focusing on installation steps as they are well documented in Termux wiki and other popular resources. All of the scripts will be provided in the Git repository.

Termux

Termux is not only a terminal emulator, but a full Linux environment with it's own package repository. Because Android is not fully compatible with standard GNU/Linux, Termux community provides patches to libraries and applications.

GNU/Linux distribution

If you need a familiar environment like Debian or Fedora, you can have it. There are several choices:

  • Andronix
  • Linux Deploy (root only)
  • Custom scripts from community
  • proot-distro
  • cross-architecture proot-distro

The most compatible of them are Andronix and proot, later being from Termux developers themselves.

Cross-architecture proot allows you to install a distribution with different architecture than your device. For example, you can install x86 Debian on an ARM smartphone. It is not 100% stable and can cause errors when running complex applications.

I wanted to make everything work using only Termux packages, so I skipped proot and Andronix.

X11

For displaying user interface we will use Termux-X11, because it is the fastest and most convenient way to run graphical applications.

Please, forget about VNC... And XServer XSDL. They were great when they were the only options to run graphical interface.

Termux-X11 is a new and compact X11 server.

  • Runs fast, has vertical synchronization
  • Allows on the fly switch of the display orientation
  • Display is not hidden under the keyboard
  • Supports sound

https://github.com/termux/termux-x11

Notice how status bar of Thunar is above the keyboard. When you open the keyboard, size of the screen updates. That way you'll always see what you are writing and doing.

Thunar

Desktop environment

For desktop environment you can choose whatever you like. But, as a first choice I would stop on XFCE4 and i3 window manager. Why do we need separate window manager? Well, if your phone doesn't have a stylus, moving windows around a small screen with reasonable DPI would be a pain.

i3 allows you to stack/tab windows, filling all of the available space on the screen. That way you don't need to click and drag small borders. Just switch apps by using taskbar and select how windows are placed on the screen.

XFCE4 is lightweight but functional. It has support for theming, so you'll be able to customize it to your likings. It looks and feels a lot like a GNOME 2 desktop.

Configuring XFCE4 session

To use i3 as our main window manager, we need to disable XFWM4 and xfdesktop in the autostart of the default session.

xfdesktop conflicts with i3 and displays incorrectly. To replace background with a simple color, you can add this to your i3 configuration:

exec xsetroot -solid "#333333"

In the Settings menu find "Session and startup". Open the "Current Session" tab and remove xfwm4 and xfdesktop from the list. Click "Save session".

Before saving the session, close all of the unneeded programs. Otherwise, they will launch on the login too.

Go to "Application autostart" tab and click Add in the lower left corner. Fill the name and command as i3 and set Trigger to "on login".

Input methods

Termux-X11 allows you to use Android keyboard to input stuff. You can use on-screen keyboards in Linux itself, but a decent option like Onboard is not available in Termux, only in standard distributions. Though, by using keyboards that a meant to be used with programming or remote access in mind, you'll quickly abandon even the finest OSK.

Hacker's keyboard

Proven by years, nothing to add. Limited in customization, but very simple to use.

https://f-droid.org/packages/org.pocketworkstation.pckeyboard/

Unexpected keyboard

Very solid keyboard revolved around gestures to free up space for keys.

https://github.com/Julow/Unexpected-Keyboard

Accessing storage

By default, Termux adds links to some folders in internal storage. But, if you can look up the path in your favorite file explorer, access to storage folder opens up. With SD card it's even worse, because Termux links to its data folder.

In your file manager, path to the SD card may look like this:

/storage/73FA-1200/

After that, you can make a symbolic link to it in your home folder:

ln -s /storage/73FA-1200/ ~/sdcard

Scripts

To make launching your fancy X11 desktop easier, we'll make some scripts.

Launch X11

What we need to do to properly use X11:

  • Set environment variable for DISPLAY
  • Start termux-x11 and wait a few seconds
  • Start dbus-daemon and our DE
  • Write process ID of X11 server to file

Also, we will redirect output of X11 server and dbus to file, so it won't mess up your current terminal session.

As we are running on top of Android, we can use utility called am to send a message to switch to another app.

am start --user 0 -n com.termux.x11/com.termux.x11.MainActivity

DPI and interface scaling

In the script to run X11 you need to define DPI for i3 and most of the other apps to scale the interface. Adjust this value to your preferences.

Also, in XFCE4 you need to set DPI for GTK apps. It is configured in the Appearance section of Settings. On the tab Fonts you'll find "Custom DPI settings". For me, value of 136 was a good fit.

DISPLAY variable

DISPLAY variable is set in our script to run X11, but we also need to set in .bashrc file so that you can launch apps from Termux itself, not only from the UI.

export DISPLAY=:1

Return back to Termux or X11

As we did in script to launch X11, same applies to switching back to Termux:

am start --user 0 -n com.termux/com.termux.app.TermuxActivity

But, because of Android strict permissions you need to grant permission to draw over applications for Termux. Otherwise, it will not work. Only switching from Termux to X11 will work without this permission.

Enable OpenGL acceleration

By default, software 3D rendering is supported through LLVMpipe. This backend is only viable on powerful devices. But, you can install virglrenderer that uses native Android OpenGL libraries and accelerates 3D through your video chip.

To run it, simply install virglrenderer-android and run the server:

virgl_test_server_android &

For program to use this backend, add this environment variables before the command:

GALLIUM_DRIVER=virpipe MESA_GL_VERSION_OVERRIDE=4.0

Compatibility is on the better side, there are reports of running x86 games through Wine. I thought it would be fun to test something heavy. So, I build dhewm3 and, surprisingly, it worked first try. I wasn't been able to launch a full map, because my phone is somewhat weak, but the menu and test map with just a square room runs.

DOOM 3

I wouldn't use or recommend you to use this acceleration every time when in X11, so I didn't include it in the scripts. Desktop apps won't benefit that much from it. And if you need to work with 3D for a job – better to use something bigger and more powerful.

Check out this blog for more information on 3D acceleration: https://ivonblog.com/en-us/posts/termux-virglrenderer/

Conclusion

Is it any useful? Yes! I've written my previous article about assembly using only a smartphone. And I compiled and tested all of the examples in Termux using cross-proot with x86 C compiler.

Previously, I've used XServer XSDL to run QEMU and debug my assembly programs. That was painful. Even more painful was running QEMU in text mode in terminal, because I couldn't exit it. Keyboard input was captured to virtual machine and shortcuts to quit QEMU didn't work. They did if the capture did not occur.

I find it fascinating that SoCs in our smartphones are capable of running apps and games through layers of API abstractions and still be fast and usable. Next time, when someone says their phone is slow piece of sh... glass, remind them that even for a menu of a DOOM 3 twenty years ago, you would need a 500 dollar computer with decent specs. And it was hot, because CPU alone could draw up to 60 watts of power.

Nowadays it can run on a phone that is powered by a small battery and doesn't get over hotter than 40 degrees Celsius. That can fit in your pocket. Try to fit a midtower PC in your pocket ;)

Git repository with scripts: Bitbucket

Thoughts? Leave a comment