- 論壇徽章:
- 0
|
作者:yuanshang 來源:賽迪網(wǎng)技術(shù)社區(qū) 發(fā)布時間:2006.12.13
安裝了Linux后當(dāng)然希望能進(jìn)入XWindow,象在Windows一樣方便地工作,于是大家都可能用過startx命令,也可能是直接在XWindow登錄界面進(jìn)入XWindow。
這兩種方式的XWindow啟動過程有沒有區(qū)別呢?如果你沒有做過個性化設(shè)置可能體會不到,但如果你設(shè)置過中文輸入法,不管是用SCIM還是fcitx,可能都有過設(shè)置輸入法為隨XWindow啟動的經(jīng)驗。
朋友們開始修改各種啟動腳本,經(jīng)常可能會修改到的方法有:
1、修改/etc/X11/xinit/xinitrc
2、修改/etc/X11/Xsession
3、在/etc/X11/Xsession.d目錄下增加一個自定義的腳本
4、修改$HOME/.xsession
5、修改$HOME/.xinitrc
6、修改/etc/X11/xdm/Xsession
……
總之,看起來是八仙過海各有各的神通了。這些方法有沒有不同?為什么有時候明明設(shè)置好了,startx起來可以用了,但重啟在XWindow管理界面登錄后卻不能用了?為什么有時從XWindow管理界面登錄后可用,但startx后卻用不了?
下面我們一起來分析一下吧。
#!/bin/sh
#
# /etc/X11/Xsession
#
# global Xsession file -- used by display managers and xinit (startx)
# $Id: Xsession 2186 2005-02-11 07:11:05Z branden $
set -e
PROGNAME=Xsession
message () {
# pretty-print messages of arbitrary length; use xmessage if it
# is available and $DISPLAY is set
MESSAGE="$PROGNAME: $*"
echo "$MESSAGE" | fold -s -w ${COLUMNS:-80} >&2
if [ -n "$DISPLAY" ] && which xmessage > /dev/null 2>&1; then
echo "$MESSAGE" | fold -s -w ${COLUMNS:-80} | xmessage -center -file -
fi
}
message_nonl () {
# pretty-print messages of arbitrary length (no trailing newline); use
# xmessage if it is available and $DISPLAY is set
MESSAGE="$PROGNAME: $*"
echo -n "$MESSAGE" | fold -s -w ${COLUMNS:-80} >&2;
if [ -n "$DISPLAY" ] && which xmessage > /dev/null 2>&1; then
echo -n "$MESSAGE" | fold -s -w ${COLUMNS:-80} | xmessage -center -file -
fi
}
errormsg () {
# exit script with error
message "$*"
exit 1
}
internal_errormsg () {
# exit script with error; essentially a "THIS SHOULD NEVER HAPPEN" message
# One big call to message() for the sake of xmessage; if we had two then
# the user would have dismissed the error we want reported before seeing the
# request to report it.
errormsg "$*" \
"Please report the installed version of the \"xfree86-common\"" \
"package and the complete text of this error message to" \
"."
}
# initialize variables for use by all session scripts
OPTIONFILE=/etc/X11/Xsession.options
SYSRESOURCES=/etc/X11/Xresources
USRRESOURCES=$HOME/.Xresources
SYSSESSIONDIR=/etc/X11/Xsession.d
USERXSESSION=$HOME/.xsession
ALTUSERXSESSION=$HOME/.Xsession
ERRFILE=$HOME/.xsession-errors
# attempt to create an error file; abort if we cannot
if touch "$ERRFILE" 2> /dev/null && [ -w "$ERRFILE" ] &&
[ ! -L "$ERRFILE" ]; then
chmod 600 "$ERRFILE"
elif ERRFILE=$(tempfile 2> /dev/null); then
if ! ln -sf "$ERRFILE" "${TMPDIR:=/tmp}/xsession-$USER"; then
message "warning: unable to symlink \"$TMPDIR/xsession-$USER\" to" \
"\"$ERRFILE\"; look for session log/errors in" \
"\"$TMPDIR/xsession-$USER\"."
fi
else
errormsg "unable to create X session log/error file; aborting."
fi
exec >>"$ERRFILE" 2>&1
echo "$PROGNAME: X session started for $LOGNAME at $(date)"
# sanity check; is our session script directory present?
if [ ! -d "$SYSSESSIONDIR" ]; then
errormsg "no \"$SYSSESSIONDIR\" directory found; aborting."
fi
# Attempt to create a file of non-zero length in /tmp; a full filesystem can
# cause mysterious X session failures. We do not use touch, :, or test -w
# because they won't actually create a file with contents. We also let standard
# error from tempfile and echo go to the error file to aid the user in
# determining what went wrong.
WRITE_TEST=$(tempfile)
if ! echo "*" >>"$WRITE_TEST"; then
message "warning: unable to write to ${WRITE_TEST%/*}; X session may exit" \
"with an error"
fi
rm -f "$WRITE_TEST"
# Use run-parts to source every file in the session directory; we source
# instead of executing so that the variables and functions defined above
# are available to the scripts, and so that they can pass variables to each
# other.
SESSIONFILES=$(run-parts --list $SYSSESSIONDIR)
if [ -n "$SESSIONFILES" ]; then
for SESSIONFILE in $SESSIONFILES; do
. $SESSIONFILE
done
fi
exit 0
此文件的第五行“# global Xsession file -- used by
display managers and xinit
(startx)”已經(jīng)說明/etc/X11/Xsession腳本是大家公用的,無論你是用XWindow管理器(比如kdm、gdm、xdm)進(jìn)入還
是通過命令行輸入startx(即xinit方式)進(jìn)入XWindow,都會調(diào)用此腳本程序。再看看這個腳本程序做了些什么吧。這一行
“SYSSESSIONDIR=/etc/X11/Xsession.d”定義了一個變量指向了目錄/etc/X11/Xsession.d,后面又出現(xiàn)
一段代碼:
SESSIONFILES=$(run-parts --list $SYSSESSIONDIR)
if [ -n "$SESSIONFILES" ]; then
for SESSIONFILE in $SESSIONFILES; do
. $SESSIONFILE
done
fi
顯然這里是搜索了目錄/etc/X11/Xsession.d里面所有的腳本并一一執(zhí)行完畢。
同時請注意有這么兩行:
USERXSESSION=$HOME/.xsession
ALTUSERXSESSION=$HOME/.Xsession
在這里并沒有看到使用這兩個變量,那么在這個腳本里定義來做什么?下面再看一下腳本/etc/X11/Xsession.d/50xfree86-common_determine-startup里的內(nèi)容,原文如下:
# $Id: 50xfree86-common_determine-startup 1437 2004-05-23 03:18:32Z branden $
# This file is sourced by Xsession(5), not executed.
# If no X session startup program was passed to the Xsession script as an
# argument (e.g., by the display manager), or if that program was not
# executable, fall back to looking for a user's custom X session script, if
# allowed by the options file.
if [ -z "$STARTUP" ]; then
if grep -qs ^allow-user-xsession "$OPTIONFILE"; then
for STARTUPFILE in "$USERXSESSION" "$ALTUSERXSESSION"; do
if [ -e "$STARTUPFILE" ]; then
if [ -x "$STARTUPFILE" ]; then
STARTUP="$STARTUPFILE"
else
STARTUP="sh $STARTUPFILE"
fi
break
fi
done
fi
fi
# If there is still nothing to use for a startup program, try the system
# default session manager, window manager, and terminal emulator.
if [ -z "$STARTUP" ]; then
if [ -x /usr/bin/x-session-manager ]; then
STARTUP=x-session-manager
elif [ -x /usr/bin/x-window-manager ]; then
STARTUP=x-window-manager
elif [ -x /usr/bin/x-terminal-emulator ]; then
STARTUP=x-terminal-emulator
fi
fi
# If we still have not found a startup program, give up.
if [ -z "$STARTUP" ]; then
ERRMSG="unable to start X session ---"
if grep -qs ^allow-user-xsession "$OPTIONFILE"; then
ERRMSG="$ERRMSG no \"$USERXSESSION\" file, no \"$ALTUSERXSESSION\" file,"
fi
errormsg "$ERRMSG no session managers, no window managers, and no terminal" \
"emulators found; aborting."
fi
# vim:set ai et sts=2 sw=2 tw=80:
這一行“# This file is sourced by Xsession(5), not executed.”證明腳本是被之前的
/etc/X11/Xsession調(diào)用的,而后面這一段:
for STARTUPFILE in "$USERXSESSION" "$ALTUSERXSESSION"; do
if [ -e "$STARTUPFILE" ]; then
if [ -x "$STARTUPFILE" ]; then
STARTUP="$STARTUPFILE"
else
STARTUP="sh $STARTUPFILE"
fi
break
fi
done
更證明了這一點。在這里腳本搜索用戶目錄$HOME下的.xsession或.Xsession腳本來執(zhí)行。
(在/etc/X11/Xsession.d目錄下還有一些腳本,這里就不再分析了,各位有興趣可以自己看看)
另外來看看/etc/X11/xinit/xinitrc這個腳本,其原文內(nèi)容如下:
##!/bin/sh
## $Xorg: xinitrc.cpp,v 1.3 2000/08/17 19:54:30 cpqbld Exp $
#
## /etc/X11/xinit/xinitrc
##
## global xinitrc file, used by all X sessions started by xinit (startx)
#
## invoke global X session script
. /etc/X11/Xsession
注意這一句“## global xinitrc file, used by all
X sessions started by xinit
(startx)”,這說明這個腳本只被xinit調(diào)用(即用戶輸入startx時)。而其執(zhí)行的最后還是執(zhí)行了/etc/X11/Xsession腳
本。而用戶如果用其它XWindow管理界面登錄系統(tǒng)時這個腳本里的內(nèi)容是不會被執(zhí)行的。最后再來看看/etc/X11/xdm/Xsession這個腳
本吧,其原文內(nèi)容如下:
#!/bin/sh
#
# $Xorg: Xsession,v 1.4 2000/08/17 19:54:17 cpqbld Exp $
#
#
#
#
# $XFree86: xc/programs/xdm/config/Xsession,v 1.3 2001/01/17 23:45:24 dawes Exp $
# invoke global X session script
. /etc/X11/Xsession
從內(nèi)容來看,也是最終執(zhí)行了/etc/X11/Xsession。
再看看/etc/X11里面的目錄結(jié)構(gòu):
X Xsession.d cursors gdm rstart xinit
XF86Config-4 Xsession.options default-display-manager lbxproxy sysconfig xkb
Xresources Xwrapper.config fonts proxymngr twm xserver
Xsession app-defaults fs rgb.txt xdm xsm
顯然,這里把xinit和xdm、twm及gdm是分開的,在xinit里執(zhí)行的腳本是不會
被xdm里的腳本調(diào)用的。從上面對代碼的分析,大家可以看到:其實XWindow執(zhí)行的關(guān)鍵腳本是/etc/X11/Xsession,這個腳本會去
/etc/X11/Xsession.d目錄里搜索腳本執(zhí)行,也會去$HOME/下搜索腳本運行,而其它目錄xinit、xdm等里面的腳本都會去調(diào)用
/etc/X11/Xsession這個腳本。
所以,如果你想把中文輸入法fcitx、SCIM等設(shè)置成隨Xwindow啟動,比如要加入如下腳本:
export LC_CTYPE=zh_CN.GBK
export XMODIFIERS="@im=fcitx"
export XIM=fcitx
export XIM_PROGRAM=fcitx
fcitx
那么可以用如下幾種方法:
1、加到/etc/X11/Xsession。這種方法對所有啟動方法都管理。
2、在/etc/X11/Xsession.d目錄里自己寫一個腳本,這種方法也對所有啟動方法都管理。
3、在$HOME目錄下生成.xsession或.Xsession腳本,這種方法也對當(dāng)前登錄用戶有效。
4、加到/etc/X11/xinit/xinitrc腳本里。這個方法只對xinit方式(即命令行輸入startx)管用。
5、加到/etc/X11/xdm/xsession腳本里,只對用xdm管理界面登錄的方式管用。
另外,關(guān)于如何設(shè)置中文輸入法,請參看我的文章《Linux中中文輸入法隨XWindow啟動的問題》,還有設(shè)置中文字體的應(yīng)用,可以參見我的文章《在Linux里使用Windows的TrueType字體》。
大家習(xí)慣了在Linux下生活了嗎?是否遇到過聽歌沒聲音的情況?看看《Linux下/dev/dsp設(shè)備文件的作用》吧
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u1/55468/showart_2161521.html |
|