From 136bcf8da8b349e48ebd94ce899918efcbb54d5a Mon Sep 17 00:00:00 2001
From: Aron Xu <aron@debian.org>
Date: Sun, 5 Aug 2012 06:42:49 +0800
Subject: [PATCH] Alter the way of starting during Xsession startup

Traditionally im-swich and im-config start input method
framework in parallel with Xsession startup, this leaves
modern input method frameworks utilizing technologies
like dbus in an embrassing place, i.e. it's difficult to
obtain the dbus session address when the input method
framework is not initialized after dbus.

This patch alters the way of starting by introducing
im-launch, which is used to perform all necessary actions
during X session startup in a serial manner, ensuring all
needed facilities are ready before input method framework
gets launched.

Signed-off-by: Aron Xu <aron@debian.org>
---
 80im-config_launch |   34 +++++++-----------------
 debian/install     |    1 +
 debian/manpages    |    1 +
 im-config.8        |    1 +
 im-launch          |   75 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 im-launch.1        |   36 +++++++++++++++++++++++++
 6 files changed, 123 insertions(+), 25 deletions(-)
 create mode 100755 im-launch
 create mode 100644 im-launch.1

diff --git a/80im-config_launch b/80im-config_launch
index c628148..b8f8783 100644
--- a/80im-config_launch
+++ b/80im-config_launch
@@ -1,31 +1,15 @@
 #!/bin/sh
-# vim: set sts=4 expandtab: 
-# Copyright (C) 2010 Osamu Aoki <osamu@debian.org> 
+# vim: set sts=4 expandtab:
+# Copyright (C) 2010 Osamu Aoki <osamu@debian.org>
+# Copyright (C) 2012 Aron Xu <aron@debian.org>
 # GNU General Public License version 2 or later.
 #
 # Input Method for X, GNOME, KDE, ... see im-config(8).
 
-# If already tweaked, keep hands off :-)
-# If im-config is removed but not purged, keep hands off :-)
-if [ -z "$XMODIFIERS" ] && \
-   [ -z "$GTK_IM_MODULE" ] && \
-   [ -z "$QT_IM_MODULE" ] && \
-   [ -z "$QT4_IM_MODULE" ] && \
-   [ -z "$CLUTTER_IM_MODULE" ] && \
-   [ -r /usr/share/im-config/xinputrc.common ]; then
-    # initialize all im-config common functions and parameters
-    . /usr/share/im-config/xinputrc.common
-    # source the first found configuration file
-    if [ -r "$IM_CONFIG_XINPUTRC_USR" ]; then
-        . $IM_CONFIG_XINPUTRC_USR
-    elif [ -r "$IM_CONFIG_XINPUTRC_SYS" ]; then
-        . $IM_CONFIG_XINPUTRC_SYS
-    fi
-    # always export variables even for manual configuration.
-    export XMODIFIERS
-    export GTK_IM_MODULE
-    export QT_IM_MODULE
-    export QT4_IM_MODULE
-    export CLUTTER_IM_MODULE
-fi
+IMLAUNCH=/usr/bin/im-launch
 
+# Always run when installed to ensure variables are
+# set correctly no matter how the IM is configured.
+if [ -x "$IMLAUNCH" ]; then
+STARTUP="$IMLAUNCH -s -- $STARTUP"
+fi
diff --git a/debian/install b/debian/install
index 2d247a9..4866f47 100644
--- a/debian/install
+++ b/debian/install
@@ -2,6 +2,7 @@ default/im-config      etc/default
 80im-config_launch     etc/X11/Xsession.d
 xinputrc               etc/X11/xinit
 im-config              usr/bin
+im-launch              usr/bin
 im-config.desktop      usr/share/applications
 share/im-config.common usr/share/im-config
 share/xinputrc.common  usr/share/im-config
diff --git a/debian/manpages b/debian/manpages
index c2aab26..1f3c8c0 100644
--- a/debian/manpages
+++ b/debian/manpages
@@ -1 +1,2 @@
 im-config.8
+im-launch.1
diff --git a/im-config.8 b/im-config.8
index 5370e01..7dbed65 100644
--- a/im-config.8
+++ b/im-config.8
@@ -155,6 +155,7 @@ bug report to the im\-config package using reportbug(1) describing your
 resolution experience.
 
 .SH "SEE ALSO"
+.BR im-launch(1),
 .BR /usr/share/doc/im\-config/README.Debian.gz
 .SH AUTHOR
 This manual page was written by Osamu Aoki <osamu@debian.org>,
diff --git a/im-launch b/im-launch
new file mode 100755
index 0000000..b36a97d
--- /dev/null
+++ b/im-launch
@@ -0,0 +1,75 @@
+#!/bin/sh
+# vim: set sts=4 expandtab:
+# Copyright (C) 2010 Osamu Aoki <osamu@debian.org>
+# Copyright (C) 2012 Aron Xu <aron@debian.org>
+# GNU General Public License version 2 or later.
+#
+# Initialize input Method for X, GNOME, KDE, ...
+# see im-launch(1) and im-config(8).
+
+NAME=`basename $0`
+OPTIONS=`getopt -n "$NAME" -o sf -- "$@"`
+if [ $? != 0 ]; then exit 1 ; fi
+
+eval set -- "$OPTIONS"
+
+EXIT_SESSION=0
+FORCE=0
+while [ $1 ]; do
+    case $1 in
+        -s)
+            EXIT_SESSION=1
+            shift ;;
+        -f)
+            FORCE=1
+            shift ;;
+        --)
+            shift
+            break ;;
+        *)
+            EXIT_SESSION=0
+            FORCE=0
+            break ;;
+    esac
+done
+
+# Run with care, prevent users from accidentally execute the script and
+# break their current running X session.
+if [ $EXIT_SESSION = 0 ] && [ $FORCE = 0 ]; then
+    echo 'im-launch should be called in Xsession script or forced,'
+    echo 'see im-launch(1) and im-config(8) for details.'
+    exit 1
+fi
+
+# Execute all subsequent commands first
+$@ &
+
+# If already tweaked, keep hands off :-)
+# If im-config is removed but not purged, keep hands off :-)
+if [ -z "$XMODIFIERS" ] && \
+   [ -z "$GTK_IM_MODULE" ] && \
+   [ -z "$QT_IM_MODULE" ] && \
+   [ -z "$QT4_IM_MODULE" ] && \
+   [ -z "$CLUTTER_IM_MODULE" ] && \
+   [ -r /usr/share/im-config/xinputrc.common ]; then
+    # initialize all im-config common functions and parameters
+    . /usr/share/im-config/xinputrc.common
+    # source the first found configuration file
+    if [ -r "$IM_CONFIG_XINPUTRC_USR" ]; then
+        . $IM_CONFIG_XINPUTRC_USR
+    elif [ -r "$IM_CONFIG_XINPUTRC_SYS" ]; then
+        . $IM_CONFIG_XINPUTRC_SYS
+    fi
+    # always export variables even for manual configuration.
+    export XMODIFIERS
+    export GTK_IM_MODULE
+    export QT_IM_MODULE
+    export QT4_IM_MODULE
+    export CLUTTER_IM_MODULE
+fi
+
+# Keep running at backgroup after startup with minial footprint when
+# called by Xsession script or the exact X session may vanish.
+if [ $EXIT_SESSION = 1 ]; then
+    while true; do read -p > /dev/null; done
+fi
\ No newline at end of file
diff --git a/im-launch.1 b/im-launch.1
new file mode 100644
index 0000000..8bf4e77
--- /dev/null
+++ b/im-launch.1
@@ -0,0 +1,36 @@
+.TH IM\-LAUNCH 1
+.\" NAME should be all caps, SECTION should be 1-8, maybe w/ subsection
+.\" other parms are allowed: see man(7), man(1)
+.SH NAME
+im\-launch \- Utility to start input method for X sessions
+.SH SYNOPSIS
+.TP
+.B im\-launch [OPTION] \-\- [PROGRAM] [ARGS...]
+
+.SH OPTIONS
+.TP 8
+.B \-s
+Run by Xsession script through Xsession.d/80im-config_launch,
+effectively make this program a part of X session startup process.
+After execution the program keeps running at background untill the
+X session exits. This is the only designed purpose of the program.
+.TP 8
+.B \-f
+Force to program to run even not called by Xsession script and exit
+immediately after execution. This may break the input method of
+your current X session(s).
+
+.SH "TROUBLESHOOT"
+If you have any problem, see the first part of \fB~/.xsession-errors\fP and
+look for the cause.  If you are running \fBim\-config\fP while Debian (sid)
+system is undergoing a major library transition, you may need to manually set
+up \fB~/.xinputrc\fP with adjusted library version number etc.  Please file a
+bug report to the im\-config package using reportbug(1) describing your
+resolution experience.
+
+.SH "SEE ALSO"
+.BR im-config(8),
+.BR /usr/share/doc/im\-config/README.Debian.gz
+.SH AUTHOR
+This manual page was written by Aron Xu <aron@debian.org>,
+for the Debian GNU/Linux system (but may be used by others).
-- 
1.7.9.5

