Starting Qt Application On Startup for Embedded Linux

14,855

Solution 1

Have a look at /etc/X11/xinit/xinitrc (this may be different places on different systems) to see what files it sources. Generally, this will have an if..elif..else structure, so that only one initialization file is read, with $HOME/.Xclients prioritized then /etc/X11/xinit/Xclients. That's almost certainly where the terminal that appears comes from (I am presuming you do not have a desktop environment installed or anything).

Anyway, if you just want to run a single GUI app, create (or modify) an Xclients file like this:

#!/bin/sh

myGUIapp

This should be executable. It's pretty much a normal shell script, I believe, so you can have more stuff in there, although obviously not backgrounding a GUI app will block execution at that point.

[later addition]

Your installation doesn't have exactly those files, but it does have an /etc/X11/Xinit.d and if you look, I am sure those are short shell scripts and they are sourced from somewhere, probably one of the files in /etc/X11 -- Xsession, Xserver, or xserver-common. You might want to check if $XINITRC is defined in your environment; that will be a clue.

Your best bet is probably to just create a $HOME/.Xclients file (or as jofel mentions, $HOME/.xinitrc, which is probably more universal) and try it -- exactly that spelling and case, with a leading dot, and it should be set chmod 755 (the group and other permissions may not matter). Almost certainly this will be sourced from somewhere properly.

You can put files in /etc/X11/Xinit.d yourself, but doing it for this purpose is not a good idea because yours should run last and block further execution. So have a look at the scripts in /etc/X11 (again: Xsession, etc., they don't have a .sh suffix) and try to figure out in what order they all chain together. It is also likely that somewhere one of them checks for an Xclients file, eg via something like

if [ -x /some/path/Xclients ]; then

$HOME may also be used, and .xinitrc. Which is why creating at least one of these variations should work (write the file and move around/rename it if at first you don't succeed). To summarize: prime candidates for the name: .xinitrx and .Xclients, in either $HOME or /etc/X11/, but if in the later, ditch the leading dot.

Solution 2

Firstly, it's not necessarily obvious that "my Qt application needs the X server to be running to do anything" - I have successfully run Qt5 applications with the linuxfb backend from upstart. If you can do that, you'll get faster startup and lower memory use than going via X. On the other hand, you might want it to start X11 client programs, or need a window manager, so it's not for everyone.

Assuming for now that your application does need an X server, look at the nodm display manager. This is designed for embedded applications; I use it for my MythTV frontend. It's much cleaner than meddling with the scripts in /etc/X11/, and gives you the ability to have different setups for different users, which may be handy during development.

It's pretty easy to use; on a Debian install, just edit /etc/default/nodm to enable nodm and specify the required username. Other defaults are probably okay.

Then give that user a suitable ~/.xinitrc - in the simplest case, you could make it a symlink to your application. But it's more likely to be a short /bin/sh script that maybe sets a background, installs a keymap, starts a screensaver, and any other niceties you fancy before exec myapplication.

Hint - replace the last line with exec xterm whilst you're working on it - that can be pretty useful for trying things out.

Solution 3

You can add the command to the $HOME/.xinitrc of the user which starts the X server.

Need to be an executable $HOME/.xinitrc shell script (first line #!/bin/sh and chmod +x $HOME/.xinitrc).

Share:
14,855

Related videos on Youtube

sj755
Author by

sj755

Updated on September 18, 2022

Comments

  • sj755
    sj755 over 1 year

    I'm trying to get a Qt application to launch immediately after booting up. When booted, the Linux image does nothing more than launch an X server and a terminal. It also has the cron daemon running in the background. Obviously, my Qt application needs the X server to be running to do anything.

    I've seen a similar question for Red Hat and SUSE Linux.

    However, I don't see this working for my image.

    I'm wondering if there is a standard way in Linux/UNIX to make a GUI application start immediately after the X Server.


    [sj755@localhost X11]$ tree /etc/X11/
    /etc/X11/
    |-- functions
    |-- Xdefaults
    |-- Xinit
    |-- Xinit.d
    |   |-- 01xrandr
    |   |-- 11zaurus
    |   |-- 12keymap
    |   |-- 40xmodmap
    |   |-- 50setdpi
    |   |-- 55xScreenSaver
    |   |-- 60xXDefaults
    |   |-- 89xTs_Calibrate
    |   `-- 98keymap-fixup
    |-- xmodmap
    |   |-- a716.xmodmap
    |   |-- collie.xmodmap
    |   |-- default.xmodmap
    |   |-- h1910.xmodmap
    |   |-- h2200.xmodmap
    |   |-- h6300.xmodmap
    |   |-- hx4700.xmodmap
    |   |-- keyboardless.xmodmap
    |   |-- omap5912osk.xmodmap
    |   |-- poodle.xmodmap
    |   |-- shepherd.xmodmap
    |   |-- simpad.xmodmap
    |   |-- slcXXXX.xmodmap
    |   |-- xmodmap-invert
    |   |-- xmodmap-left
    |   |-- xmodmap-portrait
    |   `-- xmodmap-right
    |-- xorg.conf
    |-- Xserver
    |-- xserver-common
    |-- Xsession
    `-- Xsession.d
        |-- 60xXDefaults
        |-- 89xdgautostart
        `-- 90xXWindowManager
    
    3 directories, 36 files
    

    root@devboard:~# cat /etc/X11/Xsession.d/90xXWindowManager
    if [ -x $HOME/.Xsession ]; then
        exec $HOME/.Xsession
    elif [ -x /usr/bin/x-session-manager ]; then
        exec /usr/bin/x-session-manager
    else
        exec /usr/bin/x-window-manager
    fi
    

    #!/bin/sh
    #
    # Very simple session manager for Mini X
    #
    
    # Uncomment below to enable parsing of debian menu entrys
    # export MB_USE_DEB_MENUS=1 
    
    if [ -e $HOME/.mini_x/session ]
    then
    exec $HOME/.mini_x/session
    fi
    
    if [ -e /etc/mini_x/session ]
    then
    exec /etc/mini_x/session
    fi
    
    MINI_X_SESSION_DIR=/etc/mini_x/session.d
    if [ -d "$MINI_X_SESSION_DIR" ]; then
        # Execute session file on behalf of file owner
        find $MINI_X_SESSION_DIR -type f | while read SESSIONFILE; do
            set +e
            USERNAME=`stat -c %U $SESSIONFILE`
            # Using su rather than sudo as latest 1.8.1 cause failure [YOCTO #1211]
    #       su -l -c '$SESSIONFILE&' $USERNAME
            sudo -b -i -u $USERNAME $SESSIONFILE&
            set -e
        done
    fi
    
    # This resolution is big enough for hob2's max window size.
    xrandr -s 1024x768
    
    # Default files to run if $HOME/.mini_x/session or /etc/mini_x/session
    # don't exist. 
    
    matchbox-terminal&
    exec matchbox-window-manager
    
  • sj755
    sj755 almost 11 years
    I've updated my question, showing the /etc/X11 directory tree. I don't see an xinitrc file.
  • goldilocks
    goldilocks almost 11 years
    @sj755: Okay, read what I've added after "later addition".
  • sj755
    sj755 almost 11 years
    I found a file of interest. I've posted its contents. It seems to execute a file called $HOME/.Xsessions. I tried putting my script in that. It seems I broke the X Server. Luckily it's only an embedded Linux device and I can quickly recreate the image.
  • sj755
    sj755 almost 11 years
    Also, .xinitrc and .Xclients did not work.
  • goldilocks
    goldilocks almost 11 years
    :( $HOME/.Xsessions looks to be it to me. I'm not sure what you mean by "broke" the X Server -- do you have logging available? If so check /var/log/Xorg.0.log.
  • sj755
    sj755 almost 11 years
    I took a look at x-session-manager. It turns out to be a symbolic link to a script called usr/bin/mini-x-session. I posted its contents in a third edit. It seems that the X server is started here, followed by the terminal that I see on startup. I'm going to see if I can modify the script to run my own application.
  • goldilocks
    goldilocks almost 11 years
    @sj755: That's exactly right. Try removing/commenting out matchbox-terminal& and replacing matchbox-window-manager with your app. You may not need a window manager running in order to get your QT code to fullscreen the app, so that is worth trying to save a bit of RAM (this will also mean the window can't be manipulated by the user, if there is any interaction applicable). Otherwise do with &, like the terminal.
  • sj755
    sj755 almost 11 years
    It works. The matchbox-window-manager appears to be necessary. Also, my application won't respond to mouse clicks until I click a key on the keyboard. Anyways, I appreciate the responsiveness and help.
  • Lennart Rolland
    Lennart Rolland over 7 years
    nodm is the way to go IMO. We use this for large deployments of kiosk applications and it works a charm. Also solid advice on .xinitrc