Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add desktop environment support using Termux X11 #49

Open
nglmercer opened this issue Oct 29, 2024 · 13 comments
Open

Add desktop environment support using Termux X11 #49

nglmercer opened this issue Oct 29, 2024 · 13 comments

Comments

@nglmercer
Copy link

I think it would be great to add support for running a desktop environment in the chroot distro using Termux X11..

Basic implementation would include:

  1. Add Termux X11 dependencies:

  2. Create a simple startup script:

Benefits:

  • Native graphical application support
  • Better user experience for desktop users
  • No need for VNC or external X servers
@jjkola
Copy link
Collaborator

jjkola commented Oct 29, 2024

Thanks for the idea. Just to clarify, do you mean that Termux will run X11 server, and the distro in the rootfs will connect to that?

@nglmercer
Copy link
Author

Yes, exactly!

@jjkola
Copy link
Collaborator

jjkola commented Oct 29, 2024

Unfortunately I only have one device which is rooted, and it is Android 7 device. Thus, I can't help with this as Termux X11 requires at least Android 8. Pull requests are welcome to address this case in the README file.

@kivinblue1
Copy link

kivinblue1 commented Nov 7, 2024

I think it would be great to add support for running a desktop environment in the chroot distro using Termux X11..

Basic implementation would include:

1. Add Termux X11 dependencies:

2. Create a simple startup script:

Benefits:

* Native graphical application support

* Better user experience for desktop users

* No need for VNC or external X servers

Unfortunately I only have one device which is rooted, and it is Android 7 device. Thus, I can't help with this as Termux X11 requires at least Android 8. Pull requests are welcome to address this case in the README file.

This is my own script for using termux-x11:

`echo "Creating Termux-X11 server process"

XDG_RUNTIME_DIR=${TMPDIR} termux-x11 :0 -ac &

#sleep 0.5
echo "Created"

echo "Mounting X-Server…"
su -c busybox mount --bind $PREFIX/tmp /data/local/chroot-distro/deb>
#bind for termux-x11 server in chroot mode

sleep 0.5
echo "Completed"

echo "Creating shm…"
su -c mkdir /dev/shm
#make shm folder in default android tmpfs /dev

sleep 0.3
echo "Completed"

echo "Fix chmod…"
su -c chmod 1777 /dev/shm

sleep 0.2
echo "Completed"
#any user can use /dev/shm

echo "Mounting to fix sudo…"
su -c busybox mount -o remount,dev,suid /data
#remount for suid usage under /data folder
sleep 0.5
echo "Completed... Login in to root..."
sleep 0.5

su -c chroot-distro login debian`

Then when you're inside chroot just type:
export DISPLAY=:0

dbus-launch --exit-with-session startxfce4 &

@nglmercer
Copy link
Author

It works but for some reason the process ends suddenly, gives some warnings with dbus, and I tried it on different devices anyway thanks for the contribution

@anaxonda
Copy link

anaxonda commented Nov 26, 2024

This does it for me for debian chroot. Ideas taken from https://github.com/LinuxDroidMaster/Termux-Desktops/blob/main/Documentation/chroot/debian_chroot.md, https://ivonblog.com/en-us/posts/termux-virglrenderer/#42-in-chroot:

#----------IN TERMUX--------------
#start virgl hardware acceleration
virgl_test_server_android &

#start pulseaudio server
pulseaudio --start --load="module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" --exit-idle-time=-1
pacmd load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1

#termux x11 variables
XDG_RUNTIME_DIR=${TMPDIR} termux-x11 :0 -ac &

#Mount Termux’s tmp to chroot’s tmp directory to get audio and acceleration
sudo busybox mount --bind $PREFIX/tmp /data/local/chroot-distro/debian/tmp

#login to chroot
su -c chroot-distro login debian

#----------IN CHROOT-----------

#make sure permissions are good for /tmp folder
sudo chmod -R 777 /tmp

#set display and audio variables, launch
export DISPLAY=:0 PULSE_SERVER=tcp:127.0.0.1:4713
dbus-launch --exit-with-session startxfce4 &
#open termux-x11 app

@jjkola to save an extra step it would for me be useful to set variables with the chroot-distro login command like for example how linuxdroidmaster is passing options in his script: busybox chroot $DEBIANPATH /bin/su - droidmaster -c 'export DISPLAY=:0 && export PULSE_SERVER=127.0.0.1 && dbus-launch --exit-with-session startxfce4'

Or, have an option to load a separate user config?

Edit: Disregard, my fault for not reading well, I see there is already an option to pass a separate command!

@sabamdarif
Copy link

This does it for me for debian chroot. Ideas taken from https://github.com/LinuxDroidMaster/Termux-Desktops/blob/main/Documentation/chroot/debian_chroot.md, https://ivonblog.com/en-us/posts/termux-virglrenderer/#42-in-chroot:

#----------IN TERMUX--------------
#start virgl hardware acceleration
virgl_test_server_android &

#start pulseaudio server
pulseaudio --start --load="module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" --exit-idle-time=-1
pacmd load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1

#termux x11 variables
XDG_RUNTIME_DIR=${TMPDIR} termux-x11 :0 -ac &

#Mount Termux’s tmp to chroot’s tmp directory to get audio and acceleration
sudo busybox mount --bind $PREFIX/tmp /data/local/chroot-distro/debian/tmp

#login to chroot
su -c chroot-distro login debian

#----------IN CHROOT-----------

#make sure permissions are good for /tmp folder
sudo chmod -R 777 /tmp

#set display and audio variables, launch
export DISPLAY=:0 PULSE_SERVER=tcp:127.0.0.1:4713
dbus-launch --exit-with-session startxfce4 &
#open termux-x11 app

@jjkola to save an extra step it would for me be useful to set variables with the chroot-distro login command like for example how linuxdroidmaster is passing options in his script: busybox chroot $DEBIANPATH /bin/su - droidmaster -c 'export DISPLAY=:0 && export PULSE_SERVER=127.0.0.1 && dbus-launch --exit-with-session startxfce4'

Or, have an option to load a separate user config?

Edit: Disregard, my fault for not reading well, I see there is already an option to pass a separate command!

it worked for me in both ubuntu and debain

@univers629
Copy link

Are there any tutorials on how to connect Termux:x11 to display graphics in Arch Linux running inside a chroot-distro?

@sabamdarif
Copy link

Are there any tutorials on how to connect Termux:x11 to display graphics in Arch Linux running inside a chroot-distro?

just follow the above tutorial, give termux root access and run then one by one inside termux

@pluseach
Copy link
Contributor

pluseach commented Jan 13, 2025

How to Set Up XFCE4 Desktop Environment with Termux-X11

Installing Dependencies Package

1: Install Termux-X11

2: Install Required Packages in Termux
First, open Termux and run these commands:

pkg install x11-repo
pkg install root-repo
pkg install tsu
pkg install ncurses-utils
pkg install termux-x11-nightly
pkg install pulseaudio
pkg install virglrenderer-android

3: Set Up Desktop Environment (Assuming that you're already installed the Chroot Distro)
Inside your chroot environment, install XFCE4:

apt install xfce4
# Optional: Make sure to set up mpd.conf for music/audio server before running 'Audio Server'.
apt install mpd

Launch the Desktop Environment Easily

Screenshot_20250113-153820_Termux~4
Note: Make sure to run the script in Termux.

Save this script as "chroot-xfce.sh" as it provides user-friendly menu and use it to launch your XFCE4 desktop environment later:

  1. Create the script:
nano chroot-xfce.sh
  1. Copy and paste the provided code:
#!/bin/bash

# Set colors and styles
normal=$(tput sgr0)
highlight=$(tput bold; tput setaf 6)
logolight=$(tput bold; tput setaf 5)
title=$(tput bold; tput setaf 2)
error=$(tput setaf 1)
hide_cursor=$(tput civis)
show_cursor=$(tput cnorm)

# Menu options
options=("Audio Server" "Login CLI" "Login GUI")
selected=0
selected_distro_index=0
distro_selected=false

# Get Installed Distros
get_installed_distros() {
    installed_distros=()
    while read -r line; do
        if [[ "$line" =~ ^[A-Za-z].+[[:space:]]*:[[:space:]]*[a-z] ]]; then
            alias=$(echo "$line" | awk -F': ' '{print $2}')
            read -r next_line
            while [[ -n "$next_line" ]]; do
                if [[ "$next_line" == *"Installed: Yes"* ]]; then
                    installed_distros+=("$alias")
                    break
                fi
                read -r next_line
            done
        fi
    done < <(sudo chroot-distro list)
    
    if [ ${#installed_distros[@]} -eq 0 ]; then
        echo "No distributions installed."
        exit 1
    fi
}

# Format installed distros for display
format_installed_distros() {
    local distro_list
    distro_list=$(printf "%s " "${installed_distros[@]}")
    formatted_distro="Installed = [ ${distro_list%* } ]"
}

# ASCII art logo
logo="
  ____ _                     _     ____  _     _             
 / ___| |__  _ __ ___   ___ | |_  |  _ \(_)___| |_ _ __ ___  
| |   | '_ \| '__/ _ \ / _ \| __| | | | | / __| __| '__/ _ \ 
| |___| | | | | (_) | (_) | |_  | |_| | \__ \ |_| | | (_) |
 \____|_| |_|_|  \___/ \___/ \__| |____/|_|___/\__|_|  \___/
                              v1.1-final the.puer@discord"

# Tips array
infoa=("[Info] Use Up/Down to navigate."
      "[Info] Press Enter to select an option."
      "[Info] Use Home/End for quick navigation."
      "[Info] Press Esc or q or Ctrl+C to exit.")
infob=("[Info] Run Audio Server once before login as CLI.")

# Function to wait for user input to continue
wait_for_key_press() {
    echo -e "${normal}Press any key to continue..."
    read -r -n1 -s
}

# Function to draw the main menu
draw_menu() {
    clear
    echo -e "\n${logolight}${logo}"
    echo -e "${normal}Login to Distro"
    echo -e "${error}${formatted_distro}\n"
    echo -e "${normal}${infoa[RANDOM % ${#infoa[@]}]}"
    echo -e "${normal}${infob[RANDOM % ${#infob[@]}]}\n"

    echo -e "${title}-- Main Menu --${normal}"
    for i in "${!options[@]}"; do
        if [[ $i -eq $selected ]]; then
            printf " ${highlight}> ${options[$i]} ${normal}\n"
        else
            printf "   ${options[$i]}\n"
        fi
    done
    
    echo -e "\n${normal}(Use Up/Down arrows, Enter, Escape, q, Home, or End)"
}

# Function to draw the distro selection menu
draw_distro_menu() {
    clear
    echo -e "\n${logolight}${logo}"
    echo -e "${normal}Login to Distro"
    echo -e "${error}${formatted_distro}\n"
    echo -e "${normal}${infoa[RANDOM % ${#infoa[@]}]}"
    echo -e "${normal}${infob[RANDOM % ${#infob[@]}]}\n"

    echo -e "${title}-- Select Distro --${normal}"
    for i in "${!installed_distros[@]}"; do
        if [[ $i -eq $selected_distro_index ]]; then
            printf " ${highlight}> ${installed_distros[$i]} ${normal}\n"
        else
            printf "   ${installed_distros[$i]}\n"
        fi
    done
    
    echo -e "\n${normal}(Use Up/Down arrows, Enter to select, Escape or q to exit)"
}

# Function to select distro
select_distro() {
    selected_distro_index=0
    distro_selected=false
    
    draw_distro_menu
    
    while true; do
        stty -echo
        read -r -sN1 char
        case "$char" in
            $'\e')
                read -r -sN2 -t 0.1 char2
                case "$char2" in
                    '[A') # Up
                        selected_distro_index=$(( (selected_distro_index - 1 + ${#installed_distros[@]}) % ${#installed_distros[@]} ))
                        ;;
                    '[B') # Down
                        selected_distro_index=$(( (selected_distro_index + 1) % ${#installed_distros[@]} ))
                        ;;
                    '[H') # Home
                        selected_distro_index=0
                        ;;
                    '[F') # End
                        selected_distro_index=$((${#installed_distros[@]} - 1))
                        ;;
                    *) 
                        stty echo
                        echo -e "$show_cursor"
                        return 1
                        ;;
                esac
                ;;
            $'\n'|$'\r') # Enter
                selected_distro="${installed_distros[$selected_distro_index]}"
                distro_selected=true
                stty echo
                return 0
                ;;
            $'\x03'|'q') # Ctrl+C or q
                stty echo
                echo -e "$show_cursor"
                exit 0
                ;;
        esac
        draw_distro_menu
    done
}

# Function for the option
start_termux_server() {
    pkill -f com.termux.x11
    sudo pkill mpd
    killall -9 termux-x11 Xwayland pulseaudio virgl_test_server_android termux-wake-lock
    sudo fuser -k 4713/tcp
    sudo busybox mount --bind "$PREFIX/tmp" /data/local/chroot-distro/"$selected_distro"/tmp
    XDG_RUNTIME_DIR=${TMPDIR} termux-x11 :0 -ac &
    sleep 2
    pulseaudio --start --load="module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" --exit-idle-time=-1
    pacmd load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1
    virgl_test_server_android &
    sudo chmod -R 1777 /data/data/com.termux/files/usr/tmp
}

audio_server() {
    select_distro || return 1
    echo -e "\nRunning Audio Server for $selected_distro..."
    start_termux_server
    su -c "chroot-distro command $selected_distro \"export DISPLAY=:0 PULSE_SERVER=tcp:127.0.0.1:4713 && dbus-launch --exit-with-session && clear && mpd\""
}

login_cli() {
    select_distro || return 1
    echo -e "\nLogging in as CLI to $selected_distro..."
    read -rp "Enter username: " username
    sudo chroot-distro command "$selected_distro" "su -l $username"
}

login_gui() {
    select_distro || return 1
    echo -e "\nLogging in as GUI to $selected_distro..."
    read -rp "Enter username: " username
    start_termux_server
    am start --user 0 -n com.termux.x11/com.termux.x11.MainActivity
    su -c "chroot-distro command $selected_distro \"export DISPLAY=:0 PULSE_SERVER=tcp:127.0.0.1:4713 && dbus-launch --exit-with-session sudo -u $username startxfce4\""
}

# Initialize
get_installed_distros
format_installed_distros

# Hide cursor and draw initial menu
echo -e "$hide_cursor"
draw_menu

# Main loop
trap 'stty echo; echo -e "$show_cursor"; exit' EXIT SIGINT SIGTERM
while true; do
    stty -echo
    read -r -sN1 char
    case "$char" in
        $'\e')
            read -r -sN2 -t 0.1 char2
            case "$char2" in
                '[A') selected=$(( (selected - 1 + ${#options[@]}) % ${#options[@]} )) ;;
                '[B') selected=$(( (selected + 1) % ${#options[@]} )) ;;
                '[H') selected=0 ;;
                '[F') selected=$((${#options[@]} - 1)) ;;
                *) stty echo; echo -e "$show_cursor"; exit 0 ;;
            esac
            ;;
        $'\n'|$'\r')
            clear
            echo -e "${title}You selected: ${options[$selected]}${normal}"
            case "${options[$selected]}" in
                "Audio Server")
                    audio_server
                    echo -e "You can login as CLI now.\nSelect 'Login CLI' to login!\n"
                    wait_for_key_press
                    ;;
                "Login CLI")
                    su -c mount -o remount,dev,suid /data
                    login_cli
                    ;;
                "Login GUI")
                    su -c mount -o remount,dev,suid /data
                    login_gui
                    ;;
            esac
            stty echo; echo -e "$show_cursor"; exit 0
            ;;
        $'\x03'|'q')
            stty echo; echo -e "$show_cursor"; exit 0
            ;;
    esac

    stty echo
    draw_menu
done
  1. Save and make it executable:
chmod +x chroot-xfce.sh
  1. Run the script:
./chroot-xfce.sh
or
bash chroot-xfce.sh

Once completed, you'll have a fully functional XFCE4 desktop environment with audio capabilities running through Termux-X11.

@pluseach
Copy link
Contributor

@jjkola add this in readme?

pluseach added a commit to pluseach/chroot-distro that referenced this issue Jan 13, 2025
More Readable README and Adding Tutorial for Desktop Environment through Termux-X11 Magisk-Modules-Alt-Repo#49
@jjkola
Copy link
Collaborator

jjkola commented Jan 13, 2025

Thanks, I will try to review the changes as soon as possible.

@pluseach
Copy link
Contributor

Thanks, I will try to review the changes as soon as possible.

np, sure, do take your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants