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

From ArchWiki
Jump to: navigation, search
(Advanced script)
Line 11: Line 11:
 
  #!/bin/bash
 
  #!/bin/bash
 
   
 
   
  #test if file exists, otherwise exit
+
  # use this vt:
  which $* 2> /dev/null
+
  # first available VT
  if [ "$?" != "0" ] ; then
+
  LVT=`fgconsole --next-available`
  echo "No such executable"
+
  #LVT=7
exit 127
+
fi
+
 
   
 
   
 
  # use this display number
 
  # use this display number
  NEWDISP=":10"
+
  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 -nolisten tcp -br &
 
 
#then we set useful parameters
 
DISPLAY=$NEWDISP
 
sleep 1s
 
 
# disable annoying non-linear mouse acceleration
 
xset m 1 0
 
# disable hard-coded default 10m screensaver
 
xset s 0 0
 
 
General principle: set variables before xinit line, and xset parameters after.
 
 
Save it as /usr/bin/xrun
 
 
After that launching app in separate X display should be as easy as typing
 
xrun appname
 
 
=== Advanced script ===
 
 
This script utilizes zenity to show error and info, useful for launchers and run dialogs:
 
 
#!/bin/bash
 
 
   
 
   
  #test if file exists, otherwise exit
+
  # test if executable exists
  which $* 2> /dev/null  
+
  [ -x "$(which $* 2> /dev/null)" ]
 
  if [ "$?" != "0" ] ; then
 
  if [ "$?" != "0" ] ; then
 
  echo "No such executable!"
 
  echo "No such executable!"
  zenity --error --text="No such executable!" 2> /dev/null
+
  # if running from X, display zenity dialog:
 +
[ -z "$DISPLAY" ] || zenity --error --text="No such executable!" 2> /dev/null
 
  exit 127
 
  exit 127
 
  fi
 
  fi
 
   
 
   
  # use this vt
+
  # if runing from X, display zenity dialog:
  LVT=`fgconsole --next-available`
+
  [ -z "$DISPLAY" ] || zenity --question --title "Launch?" --text="Will launch \"$*\" on tty$LVT. Continue?" 2> /dev/null || exit 1
#LVT=7
+
 
   
 
   
 
  echo "Will launch \"$*\" on tty$LVT..."
 
  echo "Will launch \"$*\" on tty$LVT..."
  zenity --question --title "Launch?" --text="Will launch \"$*\" on tty$LVT. Continue?" 2> /dev/null || exit 1
+
  [ -z "$DISPLAY" ] && sleep 1s
 
   
 
   
# use this display number
 
NEWDISP=":10"
 
 
   
 
   
 
  # this flag is used for indicating unaccessible or broken DRI.  
 
  # this flag is used for indicating unaccessible or broken DRI.  
Line 76: Line 42:
 
  unset LIBGL_ALWAYS_INDIRECT
 
  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
+
  ##########################################################
 +
# main execution line with automatic full path retrieval.
 +
xinit "$(which $* 2> /dev/null)" -- $NEWDISP vt$LVT -nolisten tcp -br &
 +
##########################################################
 +
 +
# set our new dislplay
 
  DISPLAY=$NEWDISP
 
  DISPLAY=$NEWDISP
  sleep 1s
+
# wait some timefor X to load
 +
  sleep 4s
 +
 +
# then we set useful parameters
 
   
 
   
 
  # disable non-linear mouse acceleration
 
  # 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.
 +
 +
Save it as /usr/bin/xrun
 +
 +
After that launching app in separate X display should be as easy as typing
 +
xrun appname
 +
 +
remove lines with zenity, if you do not have zenity installed.
  
--[[User:Psycho zs|Psycho zs]] 07:00, 3 October 2010 (EDT)
+
--[[User:Psycho zs|Psycho zs]] 13:02, 18 December 2010 (EST)

Revision as of 18:02, 18 December 2010

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

# use this vt:
# first available VT
LVT=`fgconsole --next-available`
#LVT=7

# use this display number
NEWDISP=":10" 


# test if executable exists
[ -x "$(which $* 2> /dev/null)" ] 
if [ "$?" != "0" ] ; then
echo "No such executable!"
# if running from X, display zenity dialog:
[ -z "$DISPLAY" ] || zenity --error --text="No such executable!" 2> /dev/null
exit 127
fi

# if runing from X, display zenity dialog:
[ -z "$DISPLAY" ] || zenity --question --title "Launch?" --text="Will launch \"$*\" on tty$LVT. Continue?" 2> /dev/null || exit 1

echo "Will launch \"$*\" on tty$LVT..."
[ -z "$DISPLAY" ] && sleep 1s


# 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 $* 2> /dev/null)" -- $NEWDISP vt$LVT -nolisten tcp -br &
##########################################################

# set our new dislplay
DISPLAY=$NEWDISP
# wait some timefor 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

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

xrun appname

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

--Psycho zs 13:02, 18 December 2010 (EST)