Difference between revisions of "Running program in separate X display"

From ArchWiki
Jump to: navigation, search
(merge request)
(13 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
[[Category:X Server]]
 +
{{Merge|Gaming#Starting_games_in_a_separate_X_server|same topic}}
 +
 
It may often be handy to run some program in separate X display. I.e. 3D games for gaining performance, or making alt+tab-style switching possible with ctrl+alt+[F7-F12].
 
It may often be handy to run some program in separate X display. I.e. 3D games for gaining performance, or making alt+tab-style switching possible with ctrl+alt+[F7-F12].
  
Line 11: Line 14:
 
  #!/bin/bash
 
  #!/bin/bash
 
   
 
   
  #test if file exists, otherwise exit
+
  # calculate first available VT
  which $* 2> /dev/null
+
  LVT=`fgconsole --next-available`
  if [ "$?" != "0" ] ; then
+
  echo "No such executable"
+
# calculate first usable display
 +
XNUM="-1"
 +
SOCK="something"
 +
while [ ! -z "$SOCK" ]
 +
do
 +
XNUM=$(( $XNUM + 1 ))
 +
SOCK=$(ls -A -1 /tmp/.X11-unix | grep "X$XNUM" )
 +
done
 +
NEWDISP=":$XNUM"
 +
 +
  if [ ! -z "$*" ]
 +
# generate exec line if arguments are given
 +
then
 +
# test if executable exists
 +
  if [ ! -x "$(which $1 2> /dev/null)" ]  
 +
then
 +
  echo "$1: No such executable!"
 +
# if running from X, display zenity dialog:
 +
[ -z "$DISPLAY" ] || zenity --error --text="$1: No such executable!" 2> /dev/null
 
  exit 127
 
  exit 127
 
  fi
 
  fi
 +
# generate exec line
 +
EXECL="$(which $1)"
 +
shift 1
 +
EXECL="$EXECL $*"
 +
EXECPH=""
 
   
 
   
  # use this display number
+
  # prepare to start new X sessions if no arguments passed
  NEWDISP=":10"
+
  else
 +
EXECL=""
 +
EXECPH="New X session"
 +
fi
 
   
 
   
  # this flag is used for indicating unaccessible or broken DRI.
+
  # if runing from X, display zenity dialog:
  # it is set, for example, when using fglrx+compiz-manager
+
[ -z "$DISPLAY" ] || zenity --question --title "Launch?" --text="Will launch \"$EXECL$EXECPH\"
 +
 +
tty$LVT
 +
X display $NEWDISP
 +
 +
Continue?" 2> /dev/null || exit 1
 +
 +
echo "Will launch \"$EXECL$EXECPH\" on tty$LVT, X display $NEWDISP..."
 +
[ -z "$DISPLAY" ] && sleep 1s
 +
 +
 +
# This flag is used for indication of unaccessible or broken DRI.
 +
  # It is set, for example, when using fglrx+compiz-manager.
 
  # Some games will refuse to start if it is set.
 
  # Some games will refuse to start if it is set.
  # unsetting it should not do any harm, and can be useful.
+
  # Unsetting it should not do any harm, and can be useful.
 
  unset LIBGL_ALWAYS_INDIRECT
 
  unset LIBGL_ALWAYS_INDIRECT
 
   
 
   
  #main execution line with automatic full path retrieval.
+
##########################################################
  xinit `which $*` -- $NEWDISP -nolisten tcp -br &
+
  # main execution
 +
  xinit $EXECL -- $NEWDISP vt$LVT -nolisten tcp -br &
 +
##########################################################
 
   
 
   
  #then we set useful parameters
+
  # set our new dislplay
 
  DISPLAY=$NEWDISP
 
  DISPLAY=$NEWDISP
  sleep 1s
+
# wait some time for X to load
 +
  sleep 4s
 
   
 
   
  # disable annoying non-linear mouse acceleration
+
# then we set useful parameters
 +
 +
  # disable non-linear mouse acceleration
 
  xset m 1 0
 
  xset m 1 0
 +
 
  # disable hard-coded default 10m screensaver
 
  # disable hard-coded default 10m screensaver
 
  xset s 0 0
 
  xset s 0 0
 +
 +
# while X is running, persistently do something useful
 +
# i.e. prevent apps from enabling mouse acceleration and screensaver
 +
while xset -q > /dev/null
 +
do
 +
xset m 1 0
 +
xset s 0 0
 +
sleep 3s
 +
done
 +
 +
 +
wait
 +
sleep 5
  
 
General principle: set variables before xinit line, and xset parameters after.
 
General principle: set variables before xinit line, and xset parameters after.
 
+
Save it as /usr/bin/xrun
+
Save it as /usr/bin/xrun or better as /usr/local/bin/xrun (if you have /usr/local/bin/ in your $PATH)
 
+
 
After that launching app in separate X display should be as easy as typing
 
After that launching app in separate X display should be as easy as typing
 
  xrun appname
 
  xrun appname
  
=== Advanced script ===
+
'''remove lines containing zenity, if you do not have zenity installed.'''
  
This script utilizes zenity to show error and info:
 
  
#!/bin/bash
 
 
#test if file exists, otherwise exit
 
which $* 2> /dev/null
 
if [ "$?" != "0" ] ; then
 
echo "No such executable!"
 
zenity --error --text="No such executable!" 2> /dev/null
 
exit 127
 
fi
 
 
# use this vt
 
LVT=`fgconsole --next-available`
 
#LVT=7
 
 
echo "Will launch \"$*\" on tty$LVT..."
 
zenity --question --title "Launch?" --text="Will launch \"$*\" on tty$LVT. Continue?" 2> /dev/null || exit 1
 
 
# use this display number
 
NEWDISP=":10"
 
 
# this flag is used for indicating unaccessible or broken DRI.
 
# it is set, for example, when using fglrx+compiz-manager
 
# Some games will refuse to start if it is set.
 
# unsetting it should not do any harm, and can be useful.
 
unset LIBGL_ALWAYS_INDIRECT
 
 
#main execution line with automatic full path retrieval.
 
xinit `which $*` -- $NEWDISP vt$LVT -nolisten tcp -br &
 
 
#then we set useful parameters
 
DISPLAY=$NEWDISP
 
sleep 1s
 
 
# disable non-linear mouse acceleration
 
xset m 1 0
 
# disable hard-coded default 10m screensaver
 
xset s 0 0
 
  
--[[User:Psycho zs|Psycho zs]] 07:00, 3 October 2010 (EDT)
+
== Permission problems ==
 +
See [[General Troubleshooting#Session permissions]] for details.

Revision as of 20:19, 19 December 2013

Merge-arrows-2.pngThis article or section is a candidate for merging with Gaming#Starting_games_in_a_separate_X_server.Merge-arrows-2.png

Notes: same topic (Discuss in Talk:Running program in separate X display#)

It may often be handy to run some program in separate X display. I.e. 3D games for gaining performance, or making alt+tab-style switching possible with ctrl+alt+[F7-F12].

The simplest way would be:

xinit appname -- :10

Basic script

But if you have ~/.xinitrc in place, you should use full path to app to override it. Also there can be some environment variables and X options to pass. So the smart way would be to pack it all into a wrapper:

#!/bin/bash

# calculate first available VT
LVT=`fgconsole --next-available`

# calculate first usable display
XNUM="-1"
SOCK="something"
while [ ! -z "$SOCK" ]
do
XNUM=$(( $XNUM + 1 ))
SOCK=$(ls -A -1 /tmp/.X11-unix | grep "X$XNUM" )
done
NEWDISP=":$XNUM"

if [ ! -z "$*" ]
# generate exec line if arguments are given
then
# test if executable exists
 if [ ! -x "$(which $1 2> /dev/null)" ] 
then
echo "$1: No such executable!"
# if running from X, display zenity dialog:
[ -z "$DISPLAY" ] || zenity --error --text="$1: No such executable!" 2> /dev/null
exit 127
fi
# generate exec line
EXECL="$(which $1)"
shift 1
EXECL="$EXECL $*"
EXECPH=""

# prepare to start new X sessions if no arguments passed
else
EXECL=""
EXECPH="New X session"
fi

# if runing from X, display zenity dialog:
[ -z "$DISPLAY" ] || zenity --question --title "Launch?" --text="Will launch \"$EXECL$EXECPH\"

tty$LVT
X display $NEWDISP

Continue?" 2> /dev/null || exit 1

echo "Will launch \"$EXECL$EXECPH\" on tty$LVT, X display $NEWDISP..."
[ -z "$DISPLAY" ] && sleep 1s


# This flag is used for indication of unaccessible or broken DRI.
# It is set, for example, when using fglrx+compiz-manager.
# Some games will refuse to start if it is set.
# Unsetting it should not do any harm, and can be useful.
unset LIBGL_ALWAYS_INDIRECT

##########################################################
# main execution
xinit $EXECL -- $NEWDISP vt$LVT -nolisten tcp -br &
##########################################################

# set our new dislplay
DISPLAY=$NEWDISP
# wait some time for X to load
sleep 4s

# then we set useful parameters

# disable non-linear mouse acceleration
xset m 1 0

# disable hard-coded default 10m screensaver
xset s 0 0

# while X is running, persistently do something useful
# i.e. prevent apps from enabling mouse acceleration and screensaver
while xset -q > /dev/null
do 
xset m 1 0
xset s 0 0
sleep 3s
done


wait
sleep 5

General principle: set variables before xinit line, and xset parameters after.

Save it as /usr/bin/xrun or better as /usr/local/bin/xrun (if you have /usr/local/bin/ in your $PATH)

After that launching app in separate X display should be as easy as typing

xrun appname

remove lines containing zenity, if you do not have zenity installed.


Permission problems

See General Troubleshooting#Session permissions for details.