Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1039054
  • 博文数量: 836
  • 博客积分: 43880
  • 博客等级: 大将
  • 技术积分: 5485
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-20 17:36
文章分类

全部博文(836)

文章存档

2011年(1)

2008年(835)

我的朋友

分类: LINUX

2008-08-20 18:26:13


安装了Linux后当然希望能进入XWindow,象在Windows一样方便地工作,于是大家都可能用过startx命令,也可能是直接在XWindow登录界面进入XWindow。 这两种方式的XWindow启动过程有没有区别呢?如果你没有做过个性化设置可能体会不到,但如果你设置过中文输入法,不管是用SCIM还是fcitx,可能都有过设置输入法为随XWindow启动的经验。 朋友们开始修改各种启动脚本,经常可能会修改到的方法有: 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 …… 总之,看起来是八仙过海各有各的神通了。这些方法有没有不同?为什么有时候明明设置好了,startx起来可以用了,但重启在XWindow管理界面登录后却不能用了?为什么有时从XWindow管理界面登录后可用,但startx后却用不了? 下面我们一起来分析一下吧。
#!/bin/sh
#
#/etc/X11/Xsession
#
#globalXsessionfile--usedbydisplaymanagersandxinit(startx)

#$Id:Xsession21862005-02-1107:11:05Zbranden$

set-e

PROGNAME=Xsession

message(){
#pretty-printmessagesofarbitrarylength;usexmessageifit
#isavailableand$DISPLAYisset
MESSAGE="$PROGNAME:$*"
echo"$MESSAGE"|fold-s-w${COLUMNS:-80}>&2
if[-n"$DISPLAY"]&&whichxmessage>/dev/null2>&1;then
echo"$MESSAGE"|fold-s-w${COLUMNS:-80}|xmessage-center-file-
fi
}

message_nonl(){
#pretty-printmessagesofarbitrarylength(notrailingnewline);use
#xmessageifitisavailableand$DISPLAYisset
MESSAGE="$PROGNAME:$*"
echo-n"$MESSAGE"|fold-s-w${COLUMNS:-80}>&2;
if[-n"$DISPLAY"]&&whichxmessage>/dev/null2>&1;then
echo-n"$MESSAGE"|fold-s-w${COLUMNS:-80}|xmessage-center-file-
fi
}

errormsg(){
#exitscriptwitherror
message"$*"
exit1
}

internal_errormsg(){
#exitscriptwitherror;essentiallya"THISSHOULDNEVERHAPPEN"message
#Onebigcalltomessage()forthesakeofxmessage;ifwehadtwothen
#theuserwouldhavedismissedtheerrorwewantreportedbeforeseeingthe
#requesttoreportit.
errormsg"$*"\
"Pleasereporttheinstalledversionofthe\"xfree86-common\""\
"packageandthecompletetextofthiserrormessageto"\
"."
}

#initializevariablesforusebyallsessionscripts

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

#attempttocreateanerrorfile;abortifwecannot
iftouch"$ERRFILE"2>/dev/null&&[-w"$ERRFILE"]&&
[!-L"$ERRFILE"];then
chmod600"$ERRFILE"
elifERRFILE=$(tempfile2>/dev/null);then
if!ln-sf"$ERRFILE""${TMPDIR:=/tmp}/xsession-$USER";then
message"warning:unabletosymlink\"$TMPDIR/xsession-$USER\"to"\
"\"$ERRFILE\";lookforsessionlog/errorsin"\
"\"$TMPDIR/xsession-$USER\"."
fi
else
errormsg"unabletocreateXsessionlog/errorfile;aborting."
fi

exec>>"$ERRFILE"2>&1

echo"$PROGNAME:Xsessionstartedfor$LOGNAMEat$(date)"

#sanitycheck;isoursessionscriptdirectorypresent?
if[!-d"$SYSSESSIONDIR"];then
errormsg"no\"$SYSSESSIONDIR\"directoryfound;aborting."
fi

#Attempttocreateafileofnon-zerolengthin/tmp;afullfilesystemcan
#causemysteriousXsessionfailures.Wedonotusetouch,:,ortest-w
#becausetheywon''tactuallycreateafilewithcontents.Wealsoletstandard
#errorfromtempfileandechogototheerrorfiletoaidtheuserin
#determiningwhatwentwrong.
WRITE_TEST=$(tempfile)
if!echo"*">>"$WRITE_TEST";then
message"warning:unabletowriteto${WRITE_TEST%/*};Xsessionmayexit"\
"withanerror"
fi
rm-f"$WRITE_TEST"

#Userun-partstosourceeveryfileinthesessiondirectory;wesource
#insteadofexecutingsothatthevariablesandfunctionsdefinedabove
#areavailabletothescripts,andsothattheycanpassvariablestoeach
#other.
SESSIONFILES=$(run-parts--list$SYSSESSIONDIR)
if[-n"$SESSIONFILES"];then
forSESSIONFILEin$SESSIONFILES;do
.$SESSIONFILE
done
fi

exit0
此文件的第五行“#globalXsessionfile--usedbydisplaymanagersandxinit(startx)”已经说明/etc/X11/Xsession脚本是大家公用的,无论你是用XWindow管理器(比如kdm、gdm、xdm)进入还是通过命令行输入startx(即xinit方式)进入XWindow,都会调用此脚本程序。再看看这个脚本程序做了些什么吧。这一行“SYSSESSIONDIR=/etc/X11/Xsession.d”定义了一个变量指向了目录/etc/X11/Xsession.d,后面又出现一段代码:
SESSIONFILES=$(run-parts--list$SYSSESSIONDIR)
if[-n"$SESSIONFILES"];then
forSESSIONFILEin$SESSIONFILES;do
.$SESSIONFILE
done
fi
显然这里是搜索了目录/etc/X11/Xsession.d里面所有的脚本并一一执行完毕。 同时请注意有这么两行:
USERXSESSION=$HOME/.xsession
ALTUSERXSESSION=$HOME/.Xsession
在这里并没有看到使用这两个变量,那么在这个脚本里定义来做什么?下面再看一下脚本/etc/X11/Xsession.d/50xfree86-common_determine-startup里的内容,原文如下:
#$Id:50xfree86-common_determine-startup14372004-05-2303:18:32Zbranden$

#ThisfileissourcedbyXsession(5),notexecuted.

#IfnoXsessionstartupprogramwaspassedtotheXsessionscriptasan
#argument(e.g.,bythedisplaymanager),orifthatprogramwasnot
#executable,fallbacktolookingforauser''scustomXsessionscript,if
#allowedbytheoptionsfile.
if[-z"$STARTUP"];then
ifgrep-qs^allow-user-xsession"$OPTIONFILE";then
forSTARTUPFILEin"$USERXSESSION""$ALTUSERXSESSION";do
if[-e"$STARTUPFILE"];then
if[-x"$STARTUPFILE"];then
STARTUP="$STARTUPFILE"
else
STARTUP="sh$STARTUPFILE"
fi
break
fi
done
fi
fi

#Ifthereisstillnothingtouseforastartupprogram,trythesystem
#defaultsessionmanager,windowmanager,andterminalemulator.
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

#Ifwestillhavenotfoundastartupprogram,giveup.
if[-z"$STARTUP"];then
ERRMSG="unabletostartXsession---"
ifgrep-qs^allow-user-xsession"$OPTIONFILE";then
ERRMSG="$ERRMSGno\"$USERXSESSION\"file,no\"$ALTUSERXSESSION\"file,"
fi
errormsg"$ERRMSGnosessionmanagers,nowindowmanagers,andnoterminal"\
"emulatorsfound;aborting."
fi

#vim:setaietsts=2sw=2tw=80:
这一行“#ThisfileissourcedbyXsession(5),notexecuted.”证明脚本是被之前的 /etc/X11/Xsession调用的,而后面这一段:
forSTARTUPFILEin"$USERXSESSION""$ALTUSERXSESSION";do
if[-e"$STARTUPFILE"];then
if[-x"$STARTUPFILE"];then
STARTUP="$STARTUPFILE"
else
STARTUP="sh$STARTUPFILE"
fi
break
fi
done
更证明了这一点。在这里脚本搜索用户目录$HOME下的.xsession或.Xsession脚本来执行。 (在/etc/X11/Xsession.d目录下还有一些脚本,这里就不再分析了,各位有兴趣可以自己看看) 另外来看看/etc/X11/xinit/xinitrc这个脚本,其原文内容如下:
##!/bin/sh
##$Xorg:xinitrc.cpp,v1.32000/08/1719:54:30cpqbldExp$
#
##/etc/X11/xinit/xinitrc
##
##globalxinitrcfile,usedbyallXsessionsstartedbyxinit(startx)
#
##invokeglobalXsessionscript

./etc/X11/Xsession
注意这一句“##globalxinitrcfile,usedbyallXsessionsstartedbyxinit(startx)”,这说明这个脚本只被xinit调用(即用户输入startx时)。而其执行的最后还是执行了/etc/X11/Xsession脚本。而用户如果用其它XWindow管理界面登录系统时这个脚本里的内容是不会被执行的。最后再来看看/etc/X11/xdm/Xsession这个脚本吧,其原文内容如下:
#!/bin/sh
#
#$Xorg:Xsession,v1.42000/08/1719:54:17cpqbldExp$
#
#
#
#
#$XFree86:xc/programs/xdm/config/Xsession,v1.32001/01/1723:45:24dawesExp$

#invokeglobalXsessionscript

./etc/X11/Xsession
从内容来看,也是最终执行了/etc/X11/Xsession。 再看看/etc/X11里面的目录结构:
XXsession.dcursorsgdmrstartxinit
XF86Config-4Xsession.optionsdefault-display-managerlbxproxysysconfigxkb
XresourcesXwrapper.configfontsproxymngrtwmxserver
Xsessionapp-defaultsfsrgb.txtxdmxsm
显然,这里把xinit和xdm、twm及gdm是分开的,在xinit里执行的脚本是不会被xdm里的脚本调用的。从上面对代码的分析,大家可以看到:其实XWindow执行的关键脚本是/etc/X11/Xsession,这个脚本会去/etc/X11/Xsession.d目录里搜索脚本执行,也会去$HOME/下搜索脚本运行,而其它目录xinit、xdm等里面的脚本都会去调用/etc/X11/Xsession这个脚本。 所以,如果你想把中文输入法fcitx、SCIM等设置成随Xwindow启动,比如要加入如下脚本:
exportLC_CTYPE=zh_CN.GBK
exportXMODIFIERS="@im=fcitx"
exportXIM=fcitx
exportXIM_PROGRAM=fcitx
fcitx
那么可以用如下几种方法: 1、加到/etc/X11/Xsession。这种方法对所有启动方法都管理。 2、在/etc/X11/Xsession.d目录里自己写一个脚本,这种方法也对所有启动方法都管理。 3、在$HOME目录下生成.xsession或.Xsession脚本,这种方法也对当前登录用户有效。 4、加到/etc/X11/xinit/xinitrc脚本里。这个方法只对xinit方式(即命令行输入startx)管用。 5、加到/etc/X11/xdm/xsession脚本里,只对用xdm管理界面登录的方式管用。 另外,关于如何设置中文输入法,请参看我的文章《Linux中中文输入法随XWindow启动的问题》,还有设置中文字体的应用,可以参见我的文章《在Linux里使用Windows的TrueType字体》。 大家习惯了在Linux下生活了吗?是否遇到过听歌没声音的情况?看看《Linux下/dev/dsp设备文件的作用》吧 (T115)


下载本文示例代码
阅读(519) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~