Index: widget/public/nsIXRemoteClient.idl
===================================================================
RCS file: /cvsroot/mozilla/widget/public/nsIXRemoteClient.idl,v
retrieving revision 1.3
diff -u -p -6 -r1.3 nsIXRemoteClient.idl
--- widget/public/nsIXRemoteClient.idl	18 Apr 2004 22:00:14 -0000	1.3
+++ widget/public/nsIXRemoteClient.idl	1 Mar 2005 07:18:09 -0000
@@ -42,13 +42,13 @@
 [scriptable, uuid(0bafc924-1dd2-11b2-8345-b59762ae0df7)]
 interface nsIXRemoteClient : nsISupports
 {
   /**
    * Initializes the client
    */
-   void init();
+   void init(in string aDisplay);
 
   /**
    * Sends a command to a running instance.  If it returns false then
    * there is no running instance.
    *
    * @param aProgram This is the preferred program that we want to use
Index: widget/src/xremoteclient/XRemoteClient.cpp
===================================================================
RCS file: /cvsroot/mozilla/widget/src/xremoteclient/XRemoteClient.cpp,v
retrieving revision 1.16
diff -u -p -6 -r1.16 XRemoteClient.cpp
--- widget/src/xremoteclient/XRemoteClient.cpp	19 Feb 2005 16:11:58 -0000	1.16
+++ widget/src/xremoteclient/XRemoteClient.cpp	1 Mar 2005 07:18:19 -0000
@@ -106,20 +106,20 @@ static char *XAtomNames[] = {
   MOZILLA_PROFILE_PROP,
   MOZILLA_PROGRAM_PROP
 };
 static Atom XAtoms[ARRAY_LENGTH(XAtomNames)];
 
 NS_IMETHODIMP
-XRemoteClient::Init (void)
+XRemoteClient::Init (const char *aDisplay)
 {
   PR_LOG(sRemoteLm, PR_LOG_DEBUG, ("XRemoteClient::Init"));
 
   if (mInitialized)
     return NS_OK;
   // try to open the display
-  mDisplay = XOpenDisplay(0);
+  mDisplay = XOpenDisplay(aDisplay);
   if (!mDisplay)
     return NS_ERROR_FAILURE;
 
   // get our atoms
   XInternAtoms(mDisplay, XAtomNames, ARRAY_LENGTH(XAtomNames), False, XAtoms);
 
Index: widget/src/xremoteclient/mozilla-xremote-client.cpp
===================================================================
RCS file: /cvsroot/mozilla/widget/src/xremoteclient/mozilla-xremote-client.cpp,v
retrieving revision 1.5
diff -u -p -6 -r1.5 mozilla-xremote-client.cpp
--- widget/src/xremoteclient/mozilla-xremote-client.cpp	1 Dec 2004 22:27:00 -0000	1.5
+++ widget/src/xremoteclient/mozilla-xremote-client.cpp	1 Mar 2005 07:18:33 -0000
@@ -41,29 +41,42 @@
 #include <plgetopt.h>
 #ifdef MOZ_WIDGET_PHOTON
 #include "PhRemoteClient.h"
 #else
 #include "XRemoteClient.h"
 #endif
+#include <string.h>
 
 static void print_usage(void);
 
 int main(int argc, char **argv)
 {
   nsresult rv;
   XRemoteClient client;
   char *browser = 0;
   char *profile = 0;
   char *username = 0;
   char *command = 0;
+  char *display = 0;
+  PRInt32 i = 0;
 
   if (argc < 2) {
     print_usage();
     return 4;
   }
 
+  for (i=1; i < argc - 1; i++) {
+    if (strcmp(argv[i], "--display") == 0) {
+      display = argv[i + 1];
+      /* Now remove the --display parameters from the command line */
+      memmove(&argv[i], &argv[i + 2], sizeof(argv[i])*(argc - (i + 2)));
+      argc -= 2;
+      break;
+    }
+  }
+
   PLOptStatus os;
   PLOptState *opt = PL_CreateOptState(argc, argv, "ha:u:p:");
   while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
     if (PL_OPT_BAD == os) {
       print_usage();
       return 4;
@@ -87,13 +100,13 @@ int main(int argc, char **argv)
       command = strdup(opt->value);      
     default:
       break;
     }
   }
 
-  rv = client.Init();
+  rv = client.Init(display);
   // failed to connect to the X server
   if (NS_FAILED(rv))
     return 1;
 
   // send the command - it doesn't get any easier than this
   PRBool success = PR_FALSE;
@@ -124,10 +137,11 @@ int main(int argc, char **argv)
   // else, everything is fine.
   return 0;
 }
 
 /* static */
 void print_usage(void) {
-  fprintf(stderr, "Usage: mozilla-xremote-client [-a firefox|thunderbird|mozilla|any]\n");
+  fprintf(stderr, "Usage: mozilla-xremote-client [--display DISPLAY]\n");
+  fprintf(stderr, "                              [-a firefox|thunderbird|mozilla|any]\n");
   fprintf(stderr, "                              [-u <username>]\n");
   fprintf(stderr, "                              [-p <profile>] COMMAND\n");
 }
Index: xpfe/bootstrap/nsAppRunner.cpp
===================================================================
RCS file: /cvsroot/mozilla/xpfe/bootstrap/nsAppRunner.cpp,v
retrieving revision 1.428
diff -u -p -6 -r1.428 nsAppRunner.cpp
--- xpfe/bootstrap/nsAppRunner.cpp	20 Dec 2004 21:53:33 -0000	1.428
+++ xpfe/bootstrap/nsAppRunner.cpp	1 Mar 2005 07:18:46 -0000
@@ -1436,13 +1436,13 @@ static nsresult DumpVersion(char *appnam
   return rv;
 }
 
 #ifdef MOZ_ENABLE_XREMOTE
 // use int here instead of a PR type since it will be returned
 // from main - just to keep types consistent
-static int HandleRemoteArguments(int argc, char* argv[], PRBool *aArgUsed)
+static int HandleRemoteArguments(int argc, char* argv[], char* display, PRBool *aArgUsed)
 {
   int i = 0;
 
   const char *remote = 0;
   const char *profile = 0;
   const char *program = 0;
@@ -1497,13 +1497,13 @@ static int HandleRemoteArguments(int arg
   nsCOMPtr<nsIXRemoteClient> client (do_CreateInstance(NS_XREMOTECLIENT_CONTRACTID));
   if (!client)
     return 1;
 
   nsresult rv;
   // try to init - connects to the X server and stuff
-  rv = client->Init();
+  rv = client->Init(display);
   if (NS_FAILED(rv)) {
     PR_fprintf(PR_STDERR, "Error: Failed to connect to X server.\n");
     return 1;
   }
 
   // Make sure to set a username if possible
@@ -1691,12 +1691,28 @@ int main(int argc, char* argv[])
       fprintf(stderr, "%s: XInitThreads failure.", argv[0]);
       exit(EXIT_FAILURE);
     }
   }
 #endif /* MOZ_X11 */
 
+  // save --display parameter for possible -remote handling
+  // (it will be lost after gtk_init)
+  char *display = 0;
+  char *param = 0;
+  for (int i=1; i<argc; i++) {
+    if ((PL_strncasecmp(argv[i], "-display", 7) == 0)
+        || (PL_strncasecmp(argv[i], "--display", 8) == 0)) {
+      if ((param = PL_strchr(argv[i], '=')) != NULL)
+        display = PL_strdup(++param);
+      else
+        display = PL_strdup(argv[i + 1]);
+      if (! *display)
+        display = nsnull;
+    }
+  }
+  
 #if defined(MOZ_WIDGET_GTK) || defined(MOZ_WIDGET_GTK2)
   // setup for private colormap.  Ideally we'd like to do this
   // in nsAppShell::Create, but we need to get in before gtk
   // has been initialized to make sure everything is running
   // consistently.
   for (i=1; i<argc; i++)
@@ -1793,13 +1809,13 @@ int main(int argc, char* argv[])
 #ifdef MOZ_ENABLE_XREMOTE
   // handle -remote now that xpcom is fired up
   int remoterv;
   PRBool argused = PR_FALSE;
   // argused will be true if someone tried to use a -remote flag.  We
   // always exit in that case.
-  remoterv = HandleRemoteArguments(argc, argv, &argused);
+  remoterv = HandleRemoteArguments(argc, argv, display, &argused);
 
   if (argused) {
 #ifdef XPCOM_GLUE
     GRE_Shutdown();
 #else
     NS_ShutdownXPCOM(nsnull);
Index: browser/app/mozilla.in
===================================================================
RCS file: /cvsroot/mozilla/browser/app/mozilla.in,v
retrieving revision 1.11
diff -u -p -6 -r1.11 mozilla.in
--- browser/app/mozilla.in	1 Feb 2005 17:36:47 -0000	1.11
+++ browser/app/mozilla.in	1 Mar 2005 07:19:02 -0000
@@ -142,13 +142,13 @@ debugging=0
 MOZILLA_BIN="${progbase}-bin"
 
 # The following is to check for a currently running instance.
 # This is taken almost verbatim from the Mozilla RPM package's launch script.
 MOZ_CLIENT_PROGRAM="$dist_bin/mozilla-xremote-client"
 check_running() {
-    "${run_moz}" "$MOZ_CLIENT_PROGRAM" -a "${progbase}" 'ping()' 2>/dev/null >/dev/null
+    "${run_moz}" "$MOZ_CLIENT_PROGRAM" $X_DISPLAY -a "${progbase}" 'ping()' 2>/dev/null >/dev/null
     RETURN_VAL=$?
     if [ $RETURN_VAL -eq 0 ]; then
         echo 1
         return 1
     else
         echo 0
@@ -157,86 +157,77 @@ check_running() {
 }
 
 if [ "$OSTYPE" = "beos" ]; then
   mimeset -F "$MOZILLA_BIN"
 fi
 
-ALREADY_RUNNING=`check_running`
-
 ################################################################ Parse Arguments
 # If there's a command line argument but it doesn't begin with a -
 # it's probably a url.  Try to send it to a running instance.
 _USE_EXIST=0
-_optOne="$1"
-case "${_optOne}" in
-	-*) 
-		;;
-	*)
-		_USE_EXIST=1
-		;;
-esac
-
-_optLast=
-for i in "$@"; do 
-	_optLast="${i}"
-done #last arg
-
-if [ `expr "${_optLast}" : '.*:/.*'` -eq 0 -a \( -f "${_optLast}" -o -d "${_optLast}" \) ]; then
-	# Last argument seems to be a local file/directory
-	# Check, if it is absolutely specified (ie. /home/foo/file vs. ./file)
-	# If it is just "relatively" (./file) specified, make it absolutely
-	[ `expr "${_optLast}" : '/.*'` -eq 0 ] && _optLast="file://`pwd`/${_optLast}"
-fi
-################################################################ Parse Arguments
-
-########################################################################### Main
-if [ $ALREADY_RUNNING -eq 1 ]; then
-	# There's an instance already running. Use it.
-	# Any command line args passed in?
-	if [ $# -gt 0 ]; then
-		# There were "some" command line args.
-		if [ ${_USE_EXIST} -eq 1 ]; then
-			# We should use an existing instance, as _USE_EXIST=$_USE_EXIST=-1
-			_remote_cmd="openURL(${_optLast})"
-			"${run_moz}" "$MOZ_CLIENT_PROGRAM" -a "${progbase}" "${_remote_cmd}"
-			unset _remote_cmd
-			exit $?
-		fi
-	else
-		# No command line args. Open new window/tab
-		#exec "${run_moz}" "$MOZ_CLIENT_PROGRAM" -a "${progbase}" "xfeDoCommand(openBrowser)"
-		"${run_moz}" "$MOZ_CLIENT_PROGRAM" -a "${progbase}" "xfeDoCommand(openBrowser)"
-		exit $?
-	fi
-fi
-# Default action - no running instance or _USE_EXIST (${_USE_EXIST}) ! -eq 1
-########################################################################### Main
 
+# Parse command line now.
 while [ $# -gt 0 ]
 do
   case "$1" in
     -p | --pure | -pure)
       MOZILLA_BIN="${MOZILLA_BIN}.pure"
       shift
       ;;
     -g | --debug)
       script_args="$script_args -g"
       debugging=1
       shift
       ;;
+    --display)
+      X_DISPLAY="--display $2"
+      moreargs="$moreargs \"$1\" \"$2\""
+      shift 2
+      ;;
     -d | --debugger)
       script_args="$script_args -d $2"
       shift 2
       ;;
     *)
+      _optLast=$1
+      if [ `expr "${_optLast}" : '.*:/.*'` -eq 0 -a \( -f "${_optLast}" -o -d "${_optLast}" \) ]; then
+        # Last argument seems to be a local file/directory
+        # Check, if it is absolutely specified (ie. /home/foo/file vs. ./file)
+        # If it is just "relatively" (./file) specified, make it absolutely
+        [ `expr "${_optLast}" : '/.*'` -eq 0 ] && _optLast="file://`pwd`/${_optLast}"
+      fi
+      _USE_EXIST=1
       moreargs="$moreargs \"$1\""
       shift 1
       ;;
   esac
 done
 
+# Check whether an instance is running, now that applicable
+# command line options have been passed
+ALREADY_RUNNING=`check_running`
+
+
+########################################################################### Main
+if [ $ALREADY_RUNNING -eq 1 ]; then
+        # There's an instance already running. Use it.
+        if [ ${_USE_EXIST} -eq 1 ]; then
+                _remote_cmd="openURL(${_optLast})"
+                "${run_moz}" "$MOZ_CLIENT_PROGRAM" $X_DISPLAY -a "${progbase}" "${_remote_cmd}"
+                unset _remote_cmd
+                exit $?
+        else
+                # No command line args. Open new window/tab
+                #exec "${run_moz}" "$MOZ_CLIENT_PROGRAM" -a "${progbase}" "xfeDoCommand(openBrowser)"
+                "${run_moz}" "$MOZ_CLIENT_PROGRAM" $X_DISPLAY -a "${progbase}" "xfeDoCommand(openBrowser)"
+                exit $?
+        fi
+fi
+# Default action - no running instance or _USE_EXIST (${_USE_EXIST}) ! -eq 1
+########################################################################### Main
+
 export MRE_HOME
 eval "set -- $moreargs"
 
 ## Start addon scripts
 moz_pis_startstop_scripts "start"
 
Index: mail/app/mozilla.in
===================================================================
RCS file: /cvsroot/mozilla/mail/app/mozilla.in,v
retrieving revision 1.4
diff -u -p -6 -r1.4 mozilla.in
--- mail/app/mozilla.in	1 Feb 2005 18:04:15 -0000	1.4
+++ mail/app/mozilla.in	1 Mar 2005 19:54:38 -0000
@@ -141,13 +141,13 @@ debugging=0
 MOZILLA_BIN="${progbase}-bin"
 
 # The following is to check for a currently running instance.
 # This is taken almost verbatim from the Mozilla RPM package's launch script.
 MOZ_CLIENT_PROGRAM="$dist_bin/mozilla-xremote-client"
 check_running() {
-    "${run_moz}" "$MOZ_CLIENT_PROGRAM" -a "${progbase}" 'ping()' 2>/dev/null >/dev/null
+    "${run_moz}" "$MOZ_CLIENT_PROGRAM" $X_DISPLAY -a "${progbase}" 'ping()' 2>/dev/null >/dev/null
     RETURN_VAL=$?
     if [ $RETURN_VAL -eq 0 ]; then
         echo 1
         return 1
     else
         echo 0
@@ -156,98 +156,80 @@ check_running() {
 }
 
 if [ "$OSTYPE" = "beos" ]; then
   mimeset -F "$MOZILLA_BIN"
 fi
 
-ALREADY_RUNNING=`check_running`
-
 ################################################################ Parse Arguments
 # If there's a command line argument but it doesn't begin with a -
 # it's probably a url.  Try to send it to a running instance.
 _USE_EXIST=0
 _NEW_WINDOW=
-_optOne="$1"
-case "${_optOne}" in
-	-*) 
-		;;
-	*)
-		_USE_EXIST=1
-		;;
-esac
-
-_optOthers=
-_optLast=
-for i in "$@"; do 
-	_optLast="${i}"
-done #last arg
-
-for i in "$@"; do
-	[ $i = ${_optLast} ] && break
-	_optOthers="${_optOthers} ${i}"
-done #others arg
-
-#???: needs check if othersopt begin with -* ?
-if [ `expr "${_optLast}" : '.*:/.*'` -eq 0 -a \( -f "${_optLast}" -o -d "${_optLast}" \) ]; then
-	# Last argument seems to be a local file/directory
-	# Check, if it is absolutely specified (ie. /home/foo/file vs. ./file)
-	# If it is just "relatively" (./file) specified, make it absolutely
-	[ `expr "${_optLast}" : '/.*'` -eq 0 ] && _optLast="file://`pwd`/${_optLast}"
-elif [ `expr "${_optLast}" : '.*:/.*'` -gt 0 -o -n "${_optOthers}" ]; then #???? like before...
-	_NEW_WINDOW=1
-fi
-################################################################ Parse Arguments
-
-########################################################################### Main
-if [ $ALREADY_RUNNING -eq 1 ]; then
-	# There's an instance already running. Use it.
-	# Any command line args passed in?
-	if [ $# -gt 0 ]; then
-		# There were "some" command line args.
-		if [ ${_USE_EXIST} -eq 1 ]; then
-			# We should use an existing instance, as _USE_EXIST=$_USE_EXIST=-1
-			_open_type="window"
-			#_open_type="tab"
-			_remote_cmd="openURL(${_optLast} , new-${_open_type})"
-			"${run_moz}" "$MOZ_CLIENT_PROGRAM" -a "${progbase}" "${_remote_cmd}"
-			unset _remote_cmd _open_type
-			exit $?
-		fi
-	else
-		# No command line args. Open new window/tab
-		#exec "${run_moz}" "$MOZ_CLIENT_PROGRAM" -a "${progbase}" "xfeDoCommand(openBrowser)"
-		"${run_moz}" "$MOZ_CLIENT_PROGRAM" -a "${progbase}" "xfeDoCommand(openInbox)"
-		exit $?
-	fi
-fi
-# Default action - no running instance or _USE_EXIST (${_USE_EXIST}) ! -eq 1
-########################################################################### Main
 
+# Parse command line now.
 while [ $# -gt 0 ]
 do
   case "$1" in
     -p | --pure | -pure)
       MOZILLA_BIN="${MOZILLA_BIN}.pure"
       shift
       ;;
     -g | --debug)
       script_args="$script_args -g"
       debugging=1
       shift
       ;;
+    --display)
+      X_DISPLAY="--display $2"
+      moreargs="$moreargs \"$1\" \"$2\""
+      shift 2
+      ;;
     -d | --debugger)
       script_args="$script_args -d $2"
       shift 2
       ;;
     *)
+      _optLast=$1
+      _optOthers=
+      if [ `expr "${_optLast}" : '.*:/.*'` -eq 0 -a \( -f "${_optLast}" -o -d "${_optLast}" \) ]; then
+         # Last argument seems to be a local file/directory
+         # Check, if it is absolutely specified (ie. /home/foo/file vs. ./file)
+         # If it is just "relatively" (./file) specified, make it absolutely
+         [ `expr "${_optLast}" : '/.*'` -eq 0 ] && _optLast="file://`pwd`/${_optLast}"
+      elif [ `expr "${_optLast}" : '.*:/.*'` -gt 0 -o -n "${_optOthers}" ]; then #???? like before...
+         _NEW_WINDOW=1
+      fi
       moreargs="$moreargs \"$1\""
       shift 1
       ;;
   esac
 done
 
+# Check whether an instance is running, now that applicable
+# command line options have been passed
+ALREADY_RUNNING=`check_running`
+
+
+########################################################################### Main
+if [ $ALREADY_RUNNING -eq 1 ]; then
+        # There's an instance already running. Use it.
+        if [ ${_USE_EXIST} -eq 1 ]; then
+                _remote_cmd="openURL(${_optLast})"
+                "${run_moz}" "$MOZ_CLIENT_PROGRAM" $X_DISPLAY -a "${progbase}" "${_remote_cmd}"
+                unset _remote_cmd
+                exit $?
+        else
+                # No command line args. Open new window/tab
+                #exec "${run_moz}" "$MOZ_CLIENT_PROGRAM" -a "${progbase}" "xfeDoCommand(openBrowser)"
+                "${run_moz}" "$MOZ_CLIENT_PROGRAM" $X_DISPLAY -a "${progbase}" "xfeDoCommand(openInbox)"
+                exit $?
+        fi
+fi
+# Default action - no running instance or _USE_EXIST (${_USE_EXIST}) ! -eq 1
+########################################################################### Main
+
 export MRE_HOME
 eval "set -- $moreargs"
 
 ## Start addon scripts
 moz_pis_startstop_scripts "start"
 
Index: toolkit/xre/nsAppRunner.cpp
===================================================================
RCS file: /cvsroot/mozilla/toolkit/xre/nsAppRunner.cpp,v
retrieving revision 1.77
diff -u -p -4 -r1.77 nsAppRunner.cpp
--- toolkit/xre/nsAppRunner.cpp	28 Mar 2005 19:36:24 -0000	1.77
+++ toolkit/xre/nsAppRunner.cpp	29 Mar 2005 19:28:58 -0000
@@ -874,9 +874,9 @@ DumpVersion()
 #ifdef MOZ_ENABLE_XREMOTE
 // use int here instead of a PR type since it will be returned
 // from main - just to keep types consistent
 static int
-HandleRemoteArgument(const char* remote)
+HandleRemoteArgument(const char* remote, const char* display)
 {
   nsresult rv;
   ArgResult ar;
 
@@ -916,9 +916,9 @@ HandleRemoteArgument(const char* remote)
     nsCOMPtr<nsIXRemoteClient> client (do_CreateInstance(NS_XREMOTECLIENT_CONTRACTID));
     NS_ENSURE_TRUE(client, 1);
 
     // try to init - connects to the X server and stuff
-    rv = client->Init();
+    rv = client->Init(display);
     if (NS_FAILED(rv)) {
       PR_fprintf(PR_STDERR, "Error: Failed to connect to X server.\n");
       return 1;
     }
@@ -1557,8 +1557,9 @@ typedef void (*_g_set_application_name_f
 
 int
 XRE_main(int argc, char* argv[], const nsXREAppData* aAppData)
 {
+  ArgResult ar;
   nsresult rv;
   NS_TIMELINE_MARK("enter main");
 
 #if defined(DEBUG) && defined(XP_WIN32)
@@ -1675,8 +1676,17 @@ XRE_main(int argc, char* argv[], const n
       }
     }
     return 0;
   }
+  
+  // save display argument here to reserve it for -remote
+  // handling (it will be removed from gtk_init)
+  const char *display = nsnull;
+  ar = CheckArg("display", &display);
+  if (ar == ARG_BAD) {
+    PR_fprintf(PR_STDERR, "Error: argument --display requires a display number\n");
+    return 1;
+  }
 
 #if defined(MOZ_WIDGET_GTK) || defined(MOZ_WIDGET_GTK2)
   // setup for private colormap.  Ideally we'd like to do this
   // in nsAppShell::Create, but we need to get in before gtk
@@ -1759,15 +1769,15 @@ XRE_main(int argc, char* argv[], const n
 #ifdef MOZ_ENABLE_XREMOTE
   // handle -remote now that xpcom is fired up
 
   const char* xremotearg;
-  ArgResult ar = CheckArg("remote", &xremotearg);
+  ar = CheckArg("remote", &xremotearg);
   if (ar == ARG_BAD) {
     PR_fprintf(PR_STDERR, "Error: -remote requires an argument\n");
     return 1;
   }
   if (ar) {
-    return HandleRemoteArgument(xremotearg);
+    return HandleRemoteArgument(xremotearg, display);
   }
 #endif
 
   nsCOMPtr<nsIProfileLock> profileLock;
