Archive

Archive for March, 2009

Debian 下 MacBook 触摸板设置

March 30th, 2009 1 comment

窃以为,MacBook 的触摸板是 Apple 人机界面中最不方便的一环,只有一个按键,手感也不如 IBM 的指点杆,没有鼠标情况下,操作非常不方便。

Linux 下的 MacBook 触摸板更是痛中之痛。Linux 下触摸板的驱动是 Synaptics,默认设置下,移动速度很慢,反应不灵敏;用两手指轻敲模拟右键的功能无法使用,不接外接鼠标就没有右键;用着简直抓狂。一直以来用 IBM 笔记本养成的在床上编程的习惯受到了严重挑战。

在新版本的 xorg 的中,可以自动发现硬件设备并自动配置,允许用户使用最小化的 xorg.conf 启动 X。其实就是一个空的 xorg.conf 也能启动。但该功能和现存的 Synaptics 配置方法相抵触,导致 google 上搜到的大部分配置方法都不能成功配置 Synaptics 的参数。

解决方法很简单:

  • 方法1:提供一个完整的,old fashion 的 xorg.conf 文件,尤其要包含 ServerLayout Section,其中要引用 Synaptics Section
  • 方法2:如果仍然希望使用 minimal xorg.conf,应该在 Synaptics Section加上 Option “CorePointer”

很显然,方法2要简单些。下面提供一个用方法2配置的 xorg.conf 文件,包含我的 Synaptics 配置。功能如下

  • 禁用 1 Finger Tap 功能
  • 2 Finger Tap 对应右键,3 Finger Tap 对应中键(按不出来),同时点击左上角区域代表中键
  • 触摸板右边和底边分别是垂直和水平滚轮
  • 其他是一些微调参数,主要为了让触摸板更灵敏

下面配置应该适用于 Debian Lenny/Squeeze/Sid,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Section "InputDevice"
    Identifier "Synaptics Touchpad"
    Driver "synaptics"
    Option "SHMConfig" "on"
    Option "CorePointer"
    Option "LeftEdge" "121"
    Option "RightEdge" "1094"
    Option "TopEdge" "80"
    Option "BottomEdge" "300"
    Option "FingerLow" "10"
    Option "FingerHigh" "15"
    Option "FingerPress" "256"
    Option "MaxTapTime" "300"
    Option "MaxTapMove" "220"
    Option "MaxDoubleTapTime" "180"
    Option "SingleTapTimeout" "180"
    Option "ClickTime" "100"
    Option "FastTaps" "1"
    Option "EmulateMidButtonTime" "75"
    Option "EmulateTwoFingerMinZ" "257"
    Option "VertScrollDelta" "5"
    Option "HorizScrollDelta" "5"
    Option "VertEdgeScroll" "1"
    Option "HorizEdgeScroll" "1"
    Option "CornerCoasting" "0"
    Option "VertTwoFingerScroll" "0"
    Option "HorizTwoFingerScroll" "0"
    Option "MinSpeed" "0.493"
    Option "MaxSpeed" "1.5544"
    Option "AccelFactor" "0.2"
    Option "TrackstickSpeed" "40"
    Option "EdgeMotionMinZ" "30"
    Option "EdgeMotionMaxZ" "160"
    Option "EdgeMotionMinSpeed" "1"
    Option "EdgeMotionMaxSpeed" "38"
    Option "EdgeMotionUseAlways" "0"
    Option "UpDownScrolling" "1"
    Option "LeftRightScrolling" "1"
    Option "UpDownScrollRepeat" "1"
    Option "LeftRightScrollRepeat" "1"
    Option "ScrollButtonRepeat" "100"
    Option "TouchpadOff" "0"
    Option "GuestMouseOff" "0"
    Option "LockedDrags" "0"
    Option "LockedDragTimeout" "5000"
    Option "RTCornerButton" "0"
    Option "RBCornerButton" "0"
    Option "LTCornerButton" "2"
    Option "LBCornerButton" "0"
    Option "TapButton1" "0"
    Option "TapButton2" "3"
    Option "TapButton3" "2"
    Option "CircularScrolling" "0"
    Option "CircScrollDelta" "0.1"
    Option "CircScrollTrigger" "0"
    Option "CircularPad" "0"
    Option "PalmDetect" "1"
    Option "PalmMinWidth" "10"
    Option "PalmMinZ" "200"
    Option "CoastingSpeed" "0"
    Option "PressureMotionMinZ" "30"
    Option "PressureMotionMaxZ" "160"
    Option "PressureMotionMinFactor" "1"
    Option "PressureMotionMaxFactor" "1"
    Option "GrabEventDevice" "1"
EndSection
Categories: Apple, 开源软件 Tags: ,

Debian 下编译静态链接的 ffmpeg 版本

March 24th, 2009 1 comment

ffmpeg 是一款开源视频编码转换工具,最近刚刚发布 0.5 版本。ffmpeg 支持 h264,mpeg4,aac,mp3,等多种视频音频编码格式,同时支持 avi,mp4,mov,flv 等多种 container 格式。

Debian 中源中自带 ffmpeg,由于版权问题,只支持很少的几种编解码器,功能非常弱,可用性比较差。Debian Multimedia 项目弥补了这一缺陷,提供了预编译好的支持各种常用格式的 ffmpeg。

由于 Linux 发行版对于动态链接库的使用情有独钟,喜欢用链接到 Shared Object(so)库的方式编译各种软件,Debian 自然也不例外。而 ffmpeg 这款博览各家之长的软件拥有众多的库依赖关系,因此不能单独安装。不同发行版之间,甚至相同发行版的不同版本号之间,ffmpeg 的安装包都是不兼容的。安装起来非常不方便。

花了一周末的时间,使用 Debian 成功编译出静态链接的 ffmpeg 版本,得到脚本如下。下载后在 Debian 中直接运行即可。我在当前 Debian Multimedia 的 ffmpeg-0.5.svn20090318,和 Sid,Squeeze,Lenny 上测试该脚本成功。如果软件下载速度太慢,请自行修改脚本中的 Debian 源 URL。

另外,我用 Intel C Compiler 编译了 ffmpeg 的静态版本,只能运行在 Cuo 2 Duo 之后的芯片上。简单测试了一下,mpeg4 编码大概能比 gcc 版本快 10% 左右。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#/bin/sh
cat > /etc/apt/sources.list.d/build-static-ffmpeg.list <<EOF
deb http://debian.ustc.edu.cn/debian sid main contrib non-free
deb-src http://debian.ustc.edu.cn/debian sid main contrib non-free
 
deb http://debian.ustc.edu.cn/debian-multimedia sid main
deb-src http://debian.ustc.edu.cn/debian-multimedia sid main
EOF
apt-get update
apt-get -t sid install debian-multimedia-keyring debian-archive-keyring
 
mkdir -p nvidia-glx-dev/DEBIAN
cat > nvidia-glx-dev/DEBIAN/control <<EOF
Package: nvidia-glx-dev
Priority: optional
Maintainer: nobody@example.com
Architecture: all
Version: 180.29
Description: dummy package for nvidia-glx-dev
 dummy package for nvidia-glx-dev
EOF
 
dpkg-deb -b nvidia-glx-dev
dpkg -i nvidia-glx-dev.deb 
 
apt-get -t sid build-dep ffmpeg || ( apt-get -t sid install libsdl1.2-dev && apt-get -t sid build-dep ffmpeg )
apt-get -t sid install libusb-1.0-0-dev
 
apt-get -t sid source ffmpeg
 
cd ffmpeg-*/
 
cat > static_build <<EOF
debian/rules clean
debian/rules patch
 
CFLAGS="-Wall -g"
CC="ccache gcc"
NCPUS=\$(getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
 
./configure \
	--disable-ffplay --disable-ffserver --disable-shared --enable-static \
	--extra-libs="-static -ldirac_encoder -ldirac_decoder -lstdc++ -lspeex -lvorbis -lxvidcore -ldc1394 -lraw1394 -lusb-1.0 -lm -lasound -ldl" \
	--prefix=/usr --extra-cflags="\$CFLAGS" --cc="\$CC" \
	--libdir=\\\${prefix}/lib --shlibdir=\\\${prefix}/lib --bindir=\\\${prefix}/bin \
	--incdir=\\\${prefix}/include/ffmpeg --enable-libmp3lame \
	--enable-gpl --enable-libfaad --mandir=\\\${prefix}/share/man --enable-libvorbis \
	--enable-pthreads --enable-libfaac --enable-libxvid --enable-postproc \
	--enable-libamr-nb --enable-libamr-wb --enable-x11grab --enable-libgsm \
	--enable-libx264 --enable-libtheora --enable-libdc1394 \
	--enable-nonfree --disable-stripping --enable-avfilter --enable-libdirac \
	--disable-decoder=libdirac --enable-libschroedinger --disable-encoder=libschroedinger \
	--enable-libspeex --enable-avfilter-lavf --disable-mmx \
	--disable-altivec --disable-armv5te --disable-armv6 --disable-vis
 
echo "#define FFMPEG_VERSION \"SVN-r18029-static\"" > version.h
 
make -j \$NCPUS && echo Build success || echo build failed
strip ffmpeg
 
./ffmpeg -version
cp ffmpeg ..
EOF
 
. static_build 
echo Type ". static_build" under source code dir to rebuild
Categories: 开源软件 Tags: , ,

用 syslinux 引导 Windows

March 22nd, 2009 2 comments

FAT 分区的引导扇区(Boot Sector)的功能是将 Windows XP 的 ntldr 文件,或 Windows Vista 的 bootmgr 文件加载到内存适当位置,由此启动 Windows。

Linux 下可以创建 Windows 的 FAT 文件系统,但是不能创建 FAT 文件系统中的 Boot Sector,也就是不能创建可以引导的 FAT 分区,因而 Linux 下无法创建可以引导的 Windows 磁盘。

不过可以曲线救国,通过 syslinux 来引导 Windows。 首先,将 syslinux 安装到 Windows 分区设备上。

syslinux /dev/sdXY

然后,将 syslinux 自带的 chain.c32 文件拷贝到 Windows 分区上。如果是 Debian,这一文件在 /usr/lib/syslinux/ 目录下。

接着,在 Windows 分区上创建 syslinux.cfg 文件

default 1
timeout 0
label 1
  kernel chain.c32
  append hd0,1 ntldr=ntldr

chain.c32 接受的第一个参数代表启动第1块硬盘的第1个分区,并装载分区上的 ntldr 文件。用该配置文件可以引导 Windows XP。如果需要引导 Windows Vista,需要修改成下述内容:

default 1
timeout 0
label 1
  kernel chain.c32
  append hd0,1 ntldr=bootmgr

chain.32 接受的参数说明如下

  • option “swap” to support swapping of BIOS drive numbers. This is necessary to boot certain operating systems (DOS, Windows) from a secondary drive.
  • option “file=” to support loading a boot file from the SYSLINUX filesystem instead of loading the boot sector from the drive.
  • option “seg=” to control the load location.
  • option “ntldr=” as a shorthand for “seg=0×2000 file=”; use this to load one of WinNT’s loaders
  • options “freedos=” and “msdos=”/”pcdos=” as shorthands for “seg=0×60 file=” and “seg=0×70 file=” respectively; use this to load FreeDOS’s kernel.sys, MS-DOS’s io.sys or PC-DOS’s ibmbio.sys.
Categories: 开源软件 Tags: , ,

Linux 下读写 FAT 属性信息

March 22nd, 2009 Comments off

FAT 是 Windows 下的文件系统格式。每个 FAT 下的文件都包含 ARHIVE,SYSTEM,HIDDEN,READ-ONLY 等属性。而 Linux 下面缺乏读写 FAT 属性信息的命令,难以读出,或者设置这些 FAT 属性。

而实际上 Linux 内核的 vfat 模块包含相应的 ioctl 命令,可以修改文件的属性。在 <linux/msdos_fs.h> 定义如下:

#define FAT_IOCTL_GET_ATTRIBUTES	_IOR('r', 0x10, __u32)
#define FAT_IOCTL_SET_ATTRIBUTES	_IOW('r', 0x11, __u32)

上述两个 ioctl 命令接受一个 int 类型,该 int 值的不同 bit 位,按照下面的定义表示不同的文件属性:

#define ATTR_RO		1	/* read-only */
#define ATTR_HIDDEN	2	/* hidden */
#define ATTR_SYS	4	/* system */
#define ATTR_VOLUME	8	/* volume label */
#define ATTR_DIR	16	/* directory */
#define ATTR_ARCH	32	/* archived */

由于没有发现有命令可以利用上述 ioctl 接口,操作 FAT 文件的属性,我自己写了一个 attrib.py 文件,代码在最后。该命令接收两个参数,说明如下

Usage: ./attrib.py dir_or_file dump|load

下面的命令将 /mnt/fat 目录下所有子目录和文件的属性输出到文件 attributes.dump 文件中

./attrib.py /mnt/fat dump > attributes.dump

./attrib.py dump 输出的部分内容如下:

A..SHR     ./ntldr
A..SHR     ./NTDETECT.COM
A..SHR     ./boot.ini
A..SHR     ./IO.SYS
A..SHR     ./MSDOS.SYS
.D..H.     ./WINDOWS/inf
.D.S.R     ./WINDOWS/Fonts
.D.SH.     ./WINDOWS/Installer
.D.S..     ./WINDOWS/Tasks
.D.S..     ./WINDOWS/Downloaded Program Files
...SH.     ./WINDOWS/winnt.bmp
...SH.     ./WINDOWS/winnt256.bmp
A...HR     ./WINDOWS/WindowsShell.Manifest
A..S..     ./WINDOWS/bootstat.dat

下面的命令将根据 attributes.dump 文件中包含的属性信息,给 /mnt/fat 目录下所有子目录和文件设置相应的属性

./attrib.py /mnt/fat load < attributes.dump

下面是 attrib.py 的源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python
import os
import sys
import fcntl
import posixpath
 
ATTR_ARCH = 0x20
ATTR_DIR = 0x10
ATTR_VOLUME = 0x08
ATTR_SYSTEM = 0x04
ATTR_HIDDEN = 0x02
ATTR_RO = 0x01
 
AttrBitName = {
    ATTR_ARCH: 'A',
    ATTR_DIR: 'D',
    ATTR_VOLUME: 'V',
    ATTR_SYSTEM: 'S',
    ATTR_HIDDEN: 'H',
    ATTR_RO: 'R',
}
 
AttrBits = AttrBitName.keys()
AttrBits.sort()
AttrBits.reverse()
 
FAT_GETATTR_IOCTL = 0x80047210
FAT_SETATTR_IOCTL = 0x40047211
 
def fat_getattr(filename):
  fd = os.open(filename, os.O_RDONLY)
  try:
    word = fcntl.ioctl(fd, FAT_GETATTR_IOCTL, '    ')
  except:
    os.close(fd)
    raise
  os.close(fd)
  return ord(word[0])
 
def fat_setattr(filename, attr):
  fd = os.open(filename, os.O_RDONLY)
  word = '%s\0\0\0' % chr(attr)
  try:
    fcntl.ioctl(fd, FAT_SETATTR_IOCTL, word)
  except:
    os.close(fd)
    raise
  os.close(fd)
 
def fat_attrstr(attr):
  bit_to_name = lambda bit: bit &amp; attr and AttrBitName[bit] or '.'
  return ''.join(map(bit_to_name, AttrBits))
 
def fat_strattr(str):
  str_to_bin = lambda x: x[0] == x[1] and '1' or '0'
  bin = ''.join(map(str_to_bin, zip('ADVSHR', str)))
  return int(bin, 2)
 
def fat_dumpattr():
  for cur, dirs, files in os.walk('.'):
    items = dirs + files
    for item in items:
      try:
        filename = '%s/%s' % (cur, item)
        attr = fat_getattr(filename)
        str = fat_attrstr(attr)
        print '%s     %s' % (str, filename)
      except Exception, e:
        print e
 
def fat_loadattr():
  for line in sys.stdin.readlines():
    str = line[:6]
    file = line[6:].strip()
    if posixpath.exists(file):
      newattr = fat_strattr(str)
      oldattr = fat_getattr(file)
      if newattr == oldattr:
        print 'ignore same attribute %s[0x%02x]: %s' % (str, newattr, file)
      else:
        oldstr = fat_attrstr(oldattr)
        fat_setattr(file, newattr)
        print '-%s[0x%02X] +%s[0x%02X] %s' % (oldstr, oldattr, str, newattr, file)
    else:
      print 'ignore not existing file: %s' % file
 
def usage():
  print 'Usage: %s dir_or_file dump|load' % sys.argv[0]
  exit(1)
 
if len(sys.argv) != 3:
  usage()
 
target_dir = sys.argv[1]
old_dir = os.getcwd()
try:
  os.chdir(target_dir)
except:
  print 'Bad directory: %s' % target_dir
  usage()
 
 
op = sys.argv[2]
if op == 'dump':
  fat_dumpattr()
elif op == 'load':
  fat_loadattr()
else:
  print 'Bad option: %s' % op
  usage()
 
os.chdir(old_dir)
Categories: 开源软件 Tags: ,

git remove file from revision history

March 18th, 2009 Comments off

最近学习 git,真是非常强大,非常不好用的软件。

一个常见的需求:有一个文件,包含敏感信息,不小心加进git库里面去了。希望从源码库的所有历史记录中删除该文件的所有版本。

其他的源码管理系统也会有类似的功能。如果是 CVS,就到源码库目录下直接删除 RCS 文件。如果是 Subversion,则是用 svnadmin dump 之后,用 import filter 再倒入。

git-filter-branch 命令包含各种回溯修改 revision history 的功能,经过尝试,获得心得若干:

  • 首先,git 的命令可扩展性很强,但对初学者来说,要记忆的命令太长,像看天书;
  • 其次,git 往往一个命令完成不了任务,需要多个命令配合,但文档对此语焉不详,初学者无所适从;
  • 最后,也是最难的,git 中概念太多,名字非常不直观,从传统源码管理的层次理解起来台阶太高。

为此,写了几个 bash function,封装 git 的上述功能。用法如下

  • git-rewrite-commits exclude_files_list:将文件从 reivision history 中删除,可指定要删除的目录;
  • git-prune-commits:上面的命令执行之后,可能出现空的 commit 对象,用这个命令可以清空这类对象;
  • git-rewrite-msg regex:regex 是可被 sed 接受的正则表达式,用该命令修改所有的 commit 注释;

如果直接使用 filter-branch 从 git 历史中删除文件,会将工作拷贝中的文件也删除。而如果只是想把清除 git 库中的敏感文件,用户可能希望保留工作拷贝中的这些文件。使用 git-rewrite-commits 可以起到保留要删除的文件的效果。

另外,由于 git 是分布式源码管理,用这种做法因此光删除一个库中的文件历史并不够。为了避免下游的库将删除后的敏感文件又 push 回来,应该对 git 库的所有副本执行相同的清除操作,这样才保险。

附:相关 bash 脚本,加入 .bashrc 中生效

function git-remove-refs()
{
  if [ -z $@ ] ; then
    echo "Usage: git-remove-refs refs_name"
    return
  fi
  git for-each-ref --format="%(refname)" refs/$1 | xargs -I Ref bash -c "echo removing git ref: Ref; git update-ref -d Ref"
}
 
function git-backup-files()
{
  FILES=`git ls-files $@`
  for File in $FILES
  do
    echo save backup for $File
  done
  GIT_BACKUP_TMP=`tempfile`.tar.gz
  tar czf $GIT_BACKUP_TMP $FILES
  echo backup file saved at $GIT_BACKUP_TMP
}
 
function git-restore-files()
{
  if [ -f $GIT_BACKUP_TMP ] ; then
    echo backup file $GIT_BACKUP_TMP restored
    tar xzf $GIT_BACKUP_TMP
    unset GIT_BACKUP_TMP
  fi
}
 
function git-rewrite-commits()
{
  if [ -z $@ ] ; then
    echo "Usage: git-rewrite-commits files_to_remove"
    return
  fi
  git-backup-files $@
  git filter-branch --index-filter "git ls-files -z $@ | git update-index --remove -z --stdin"
  git-remove-refs original
  git-restore-files
}
 
function git-prune-commits()
{
  if [ ! -z $@ ] ; then
    echo "Usage: git-prune-commits"
    return
  fi
  git filter-branch --prune-empty
  git-remove-refs original
}
 
function git-rewrite-msg()
{
  if [ -z $@ ] ; then
    echo "Usage: git-rewrite-msg regex_pattern"
    return
  fi
  git filter-branch --msg-filter "sed -e \"$1\""
  git-remove-refs original
}
Categories: 乱七八糟 Tags: ,

Office 2007 Mime Types

March 9th, 2009 Comments off

Office 2007编辑的openxml格式的文件,后缀为.docx, pptx, xlsx,这些文件放到Apache2服务器上会认成zip格式,IE另存这类文件的时候会把扩展名都换成zip,非常不方便。

这是因为系统中没有记录openxml格式文件正确的Mime类型。在Debian和Ubuntu系统下,可以直接编辑/etc/mime.types文件,在最后增加以下内容:

application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx
application/vnd.openxmlformats-officedocument.presentationml.template potx
application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsx
application/vnd.openxmlformats-officedocument.spreadsheetml.template xltx

编辑完成之后,重启Apache即可。

Categories: 开源软件 Tags: , ,