Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A workaround for X11 sharing by multiple users' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A workaround for X11 sharing by multiple users
Authored by: wgscott on Nov 05, '04 06:14:57PM
I had a fresh crack at this. I found that if the X11.app has been started, then you can use ps -x to find out what DISPLAY needs to be set to for that particular user. This is for zsh but it could be generalized...


       #---------------
	# X-windows DISPLAY
	#---------------
	
	# Set DISPLAY variable to allow X-windows programs to run from Terminal.app
	# Fast User Switching creates the problem of multiple simultaneous instances of
	# X11.app running and how to find the appropriate DISPLAY value to assign to each.
	# Currently this is only done for X11.app.  It should be done for XDarwin, etc.
	
	# {====>}
	
	# If the user has Apple's X11 installed, assume that the user wants to use it.
	# If it is not open, open it, and then try to find the appropriate DISPLAY value
	# consistent with multiple instances of X11 running simultaneously.
	
	# If the user doesn't have X11.app installed, the user probably uses a different     
	# X11 application. We then simply set DISPLAY to :0.0 if it is not already set.

    if [[ -z $DISPLAY ]]; then	
    
        # Find out if user has Apple's X11:
        
        # First attempt:
        X11APPDIR=$(/usr/bin/locate X11.app | head -1 )
        
        if [[ ! -z $X11APPDIR ]];then
            # If locate didn't work, try hunting for it in the usual places:
            if [[ -d /Applications/Utilities/X11.app ]]; then
                X11APPDIR=/Applications/Utilities/X11.app
            elif [[ -d /Applications/X11.app ]]; then
                X11APPDIR=/Applications/X11.app
            else
                X11APPDIR=''
            fi
        fi
         
            
        # If we found that the computer has X11.app, assume this is what the
        # user will want to use to start X11.
        
        if [[ -d $X11APPDIR ]];then 
        
            # First see if this user has started X11. If not, start it.
            # Then get the DISPLAY the output of the command "ps"
            # This guarantees we use the correct DISPLAY number.
            
            already_running_x11=$( /bin/ps -xwc | grep -v X11DO | grep X11 \
                                                | head -1 | awk '{print $NF}' )
            
            if [[ -z $already_running_x11 ]]; then
                /usr/bin/open -a X11; print "opening X11"; sleep 3
            fi
            
            x11_display_number=$( /bin/ps -xw -o command \
                         | /usr/bin/sed -n 's/.*\/X11.* \(:[0-9]\{1,2\}\)$/\1/p' )
                
            if [[ -n $x11_display_number ]];then
               export DISPLAY=$x11_display_number.0
               print "DISPLAY has been set to $DISPLAY "
            else
               print "Failed to read DISPLAY from ps"
               export DISPLAY=:0.0
               print "DISPLAY has been set to $DISPLAY "
               print "This may cause problems with Fast User Switching."
            fi
                    
        fi # [[ -d $X11APPDIR ]]
    
    else 
        # In the case of non-Apple X11, just set the display to :0.0
        export DISPLAY=:0.0
        print "DISPLAY has been set to $DISPLAY "
            
    fi # initial DISPLAY test
        




[ Reply to This | # ]