12
5
2011
13

Awesome 调节音量不再依赖 GNOME

之前一直在用 gnome-sound-applet 来调节音量。今天终于脱离了它。

GNOME 越来越臃肿了。今天系统出了点小问题,查看日志时再次看到 dbus 报怨 NetworkManager 没有运行的错误。我是直接用的 ArchLinux 的network服务连网的,根本不需要 NetworkManager 掺和,可是自己要用 Empathy,而它奇迹般地依赖 NetworkManager。。。虽然试了下强制卸载,最后只成功pacman -Rdd几个其它的包,没能在保证 Empathy 能用的前提下干掉 NetworkManager,心中多有不甘。于是开始打 gnome-sound-applet 的主意。

gnome-sound-applet 这东西是 gnome-control-center 的一部分,其依赖了 15M 左右的奇怪 GNOME 组件。这个 applet 也很鸡肋,本来是显示和调节音量用的,结果黑黑的图标在我的灰色 Awesome 面板上并不容易找到,花了良久才习惯。另外就是,该图标本身不提供任何信息,查看音量时需要把鼠标悬停过去,心中多有不爽。好在今天这些问题一并解决了。

参考来源是 Awesome Wiki 的这个页面,不过这里的代码太老了,不能用,自己参照rc.lua的其它部分以及 wiki 和 reference 修改之后才能用。最终的效果是这样子的:

Awesome widgets 截图

你应该很容易能猜到最右边的就是音量控制了,因为它前边有个八分音符符号“𝅘𝅥𝅮”。鼠标操作很简单:单击切换静音,上下滚动调节音量。静音时百分号会被红色的“M”取代。

贴代码:

-- {{{2 Volume Control
volume_cardid  = 0
volume_channel = "Master"
function volume (mode, widget)
  if mode == "update" then
    local fd = io.popen("amixer -c " .. volume_cardid .. " -- sget " .. volume_channel)
    local status = fd:read("*all")
    fd:close()

    local volume = string.match(status, "(%d?%d?%d)%%")
    volume = string.format("% 3d", volume)

    status = string.match(status, "%[(o[^%]]*)%]")

    if string.find(status, "on", 1, true) then
      volume = '𝅘𝅥𝅮' .. volume .. "%"
    else
      volume = '𝅘𝅥𝅮' .. volume .. '<span color="red">M</span>'
    end
    widget.text = volume
  elseif mode == "up" then
    io.popen("amixer -q -c " .. volume_cardid .. " sset " .. volume_channel .. " 5%+"):read("*all")
    volume("update", widget)
  elseif mode == "down" then
    io.popen("amixer -q -c " .. volume_cardid .. " sset " .. volume_channel .. " 5%-"):read("*all")
    volume("update", widget)
  else
    io.popen("amixer -c " .. volume_cardid .. " sset " .. volume_channel .. " toggle"):read("*all")
    volume("update", widget)
  end
end
volume_clock = timer({ timeout = 10 })
volume_clock:add_signal("timeout", function () volume("update", tb_volume) end)
volume_clock:start()

tb_volume = widget({ type = "textbox", name = "tb_volume", align = "right" })
tb_volume.width = 35
tb_volume:buttons(awful.util.table.join(
  awful.button({ }, 4, function () volume("up", tb_volume) end),
  awful.button({ }, 5, function () volume("down", tb_volume) end),
  awful.button({ }, 1, function () volume("mute", tb_volume) end)
))
volume("update", tb_volume)

记得把 tb_volume 加到 wibox 里去。

这里是我的整个 Awesome 配置。

2013年3月11日更新:Awesome 3.5 版本语法变化较大,请到我的 github 上查看相关代码。

Category: Linux | Tags: arch awesome gnome Lua linux
6
26
2011
3

使用 LD_PRELOAD 进行文件读写重定向

缘起

换Arch后,我把首选字体改成了这样:

  <match target="pattern">
    <test qual="any" name="family">
      <string>monospace</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>DejaVu Sans Mono</string>
      <string>文泉驿等宽正黑</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>serif</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>DejaVu Serif</string>
      <string>文泉驿正黑</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>sans-serif</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>DejaVu Sans</string>
      <string>文泉驿正黑</string>
    </edit>
  </match>

也就是说,DejaVu 的字体优先于文泉驿的,因为我觉得 DejaVu 的英文字体比文泉驿的好看。这一般没什么问题,除了某些PDF文件,不知道怎么搞的,不嵌入字体也就算了,字体名也奇奇怪怪弄得 evince 不认识。因为太奇怪,并且 evince 显示得更奇怪,我也不好像 KaiTi_GB2312→KaiTi 这样做字体替换了(其实应该用别名的,但当时不会)。想到一个很简单的办法是,让中文字体拥有更高的优先级。实践证明这样做有效。但是,我不希望为了几个乱码的PDF而更改我的全局字体配置。于是,基于 LD_PRELOAD 的解决方案出来了。

LD_PRELOAD 是什么?

LD_PRELOAD 是 Linux 动态链接器认识的一个环境变量(不知道其他系统是否支持)。我看的资料是这个Fun with LD_PRELOAD。原理很简单,使用自己的函数覆盖掉其它动态链接库的。因为 libc 对系统调用都有一个 wrapper,所以正常的程序都会“被骗”的。proxychains 这个让其它程序使用 socks/https 代理的程序的原理就是这个。

我的 hack

我想对指定程序(evince)重定向~/.fonts.conf的读取,所以准备覆盖open这个系统调用。按照从那个PDF的链接中找到的源码的方式,写出了最初的代码。结果用cat测试就失败了——原来还有个 manpage 没说的open64。继续测试,又发现对vi写文件时不起作用。于是加上了第三个要覆盖的函数creat

程序写好后,觉得光重定向一个文件的读写不好玩,稍稍扩展一下,支持配置就强大了。本来想学那个PDF提到的netjail一样用环境变量的。但又觉得不好设计,而且 C 语言真的写起来太麻烦了。于是想到了用 Lua 来配置。后来还想到用 Python 的,但很可惜,段错误了。

#include<stdarg.h>
#include<dlfcn.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<unistd.h>

#include<lua.h>
#include<lualib.h>
#include<lauxlib.h>

static int lib_initialized = 0;
static int (*orig_open)(const char*, int, mode_t) = 0;
static int (*orig_open64)(const char*, int, mode_t) = 0;
static int (*orig_creat)(const char*, mode_t) = 0;
static lua_State *L = NULL;
void lib_init();

void die(char *fmt, ...) {
  va_list args;
  va_start(args, fmt);
  vfprintf(stderr, fmt, args);
  va_end(args);
  fprintf(stderr, "\n");
  fflush(stderr);
  exit(-1);
}

static char* redirect(const char* file){
  int ret;
  const char *new;
  lua_getglobal(L, "redirect");
  lua_pushstring(L, file);
  ret = lua_pcall(L, 1, 1, 0);
  if(ret){
    fprintf(stderr, "取得重定向文件路径时出错了: %s\n", lua_tostring(L, -1));
    lua_pop(L, 1); /* 错误信息 */
    return (char*)file;
  }else{
    new = lua_tostring(L, -1);
    lua_pop(L, 1); /* 返回值 */
  }
  return (char*)new;
}

int open(const char* file, int flags, mode_t mode) {
  lib_init();
  file = redirect(file);
  return orig_open(file, flags, mode);
}

int open64(const char* file, int flags, mode_t mode) {
  lib_init();
  file = redirect(file);
  return orig_open64(file, flags, mode);
}

int creat(const char* file, mode_t mode) {
  lib_init();
  file = redirect(file);
  return orig_creat(file, mode);
}

void lib_init() {
  void *libhdl;
  char *dlerr;

  if (lib_initialized) return;

  if (!(libhdl=dlopen("libc.so.6", RTLD_LAZY)))
    die("Failed to patch library calls: %s", dlerror());

  orig_open = dlsym(libhdl, "open");
  if ((dlerr=dlerror()) != NULL)
    die("Failed to patch open() library call: %s", dlerr);

  orig_open64 = dlsym(libhdl, "open64");
  if ((dlerr=dlerror()) != NULL)
    die("Failed to patch open64() library call: %s", dlerr);

  orig_creat = dlsym(libhdl, "creat");
  if ((dlerr=dlerror()) != NULL)
    die("Failed to patch creat() library call: %s", dlerr);

  int ret;
  L = luaL_newstate();
  luaL_openlibs(L);
  char config[PATH_MAX];
  strcpy(config, getenv("HOME"));
  strcat(config, "/.openredir.lua");
  ret = luaL_dofile(L, config);
  if(ret){
    die("Error run ~/.openredir.lua");
  }
  lua_getglobal(L, "redirect");
  if(!lua_isfunction(L,-1)){
    die("Error run 'redirect' function in openredir.lua");
  }
  lua_pop(L, 1);

  lib_initialized = 1;
}

Makefile 如下:

CC=gcc
CFLAGS=-g -Wall -I/usr/include/lua5.1
LDFLAGS=-ldl -llua

.PHONY: all clean

all: openredir.so

openredir.so: openredir.o
    gcc -shared $< -o $@ $(LDFLAGS)

clean:
    -rm *.o

下面这个是配置文件,要放到~/.openredir.lua才行。

redirect = function(path)
  -- TODO use absolute and normalized path
  io.stderr:write('打开文件 ' .. path .. '\n')
  if path == '/' then
    return '/etc/issue.tty1'
  elseif path == '/home/lilydjwg/.fonts.conf' then
    return '/home/lilydjwg/tmpfs/fonts.conf'
  else
    return path
  end
end

示例:

>>> LD_PRELOAD=./openredir.so cat /
打开文件 /
Arch Linux \r  (\n) (\l)

不过使用时在LD_PRELOAD中最好使用绝对路径,因为某些程序会chdir()到其它地方去的。

最后吐槽下,Lua 里把路径转成 normalize 过的绝对路径都没有现成的函数,囧。。。


2014年11月23日更新:openredir 已经放在 GitHub 上了。

Category: Linux | Tags: C代码 fontconfig linux Lua
11
26
2010
8

在Win编译带Lua支持的GVIM

虽然已经在Linux下待了这么长时间,可是,还是有很多时候,我不得不面对难用的Windows。而在Linux下一直自己编译Vim的我,今天在得知刘春棍的行动的支持下,终于鼓足了勇气,下载安装MinGW,编译了GVIM。

在Windows下编译GVIM和Linux下不同,不能用./configure来配置,要到src目录下,修改这下面的Make_ming.mak文件,改好后直接make -f Make_ming.mak编译。

Win下编译东西花的时间长就不说了。我编译了好几遍。Python支持没问题,设定好路径就OK,Ruby我是为编译GVIM专门装的,除了静态编译时会报错外,还有个很严重的问题,导致我最后放弃了+ruby。这个后面再说。Lua我下的是最新的5.2版,一个lua5_2_work2_Win32_bin.zip包含了exe和dll文件,另一个lua5_2_work2_Win32_mingw4_lib.zip包含了头文件和一个 .a 文件。我最开始以为和ruby一样只能动态载入,于是设置了DYNAMIC_LUA=yes,结果编译是成功了,但载入失败:

luaL_typerror无法加载

我用cg搜索了下,luaL_typerror被 #define 成了luaL_typeerror,这看来是5.2改的。我手动在if_lua.c里把这个给改了,结果发现,还有其它函数也是类似的情况,比如lua_call等。于是我尝试静态编进去试试,竟然成功了!

然而,喜悦总是短暂的。我以前只知道ruby支持有问题,没想到这次问题更严重了,:edit命令都用不了:

当然,一开始我还不能肯定是ruby支持的问题,但当编译了不带ruby支持的版本后,这个奇异的问题就没有了。

Vim7.3对其他语言的支持的问题真是多啊:Python3的中文支持有问题,ruby的支持也有问题,要支持Lua5.2还得自己改源代码。。。

不管怎么说,Lua静态编译进去了我还是很高兴的。虽然在没装Lua的机器上没那些Lua的库的,但至少在Vimscript外又多了一种可用的语言了。

最后,想下载我编译的 Windows 32 版 vim 的请到这里来。

Category: Vim | Tags: vim windows Lua
11
7
2010
5

编译Awesome时出现module 'luadoc' not found错误的解决

今天在Arch上安装awesome,不料官方源里没有,说是因为需要cairo-xcb这个包,但它也不在源里。郁闷地使用yaourt安装。依赖N多东西。过程中竟然要卸载我的finch啊什么的好多包。果断按下N键。重新yaourt。依赖安好了,才注意到awesome使用的是cmake。这东西我在尝试csync时用过。

cmake到一半,停下来了:

[ 61%] Built target generated_icons
lua: /usr/bin/luadoc:7: module 'luadoc' not found:
	no field package.preload['luadoc']
	no file '/home/lilydjwg/scripts/lua/luadoc.lua'
	no file './luadoc.so'
	no file '/usr/lib/lua/5.1/luadoc.so'
	no file '/usr/lib/lua/5.1/loadall.so'
stack traceback:
	[C]: in function 'require'
	/usr/bin/luadoc:7: in main chunk
	[C]: ?
make[3]: *** [CMakeFiles/luadoc] 错误 1
make[2]: *** [CMakeFiles/luadoc.dir/all] 错误 2
make[1]: *** [all] 错误 2
make: *** [cmake-build] 错误 2
    正在放弃...
==> ERROR: Makepkg was unable to build awesome.

刚刚明明装了luadoc的,现在竟然没找到。。。再仔细一看,坏了,是luadoc没找到它自己的模块。。。我对lua可不比python,可以说除了那种学过编程的人都能看懂的部分外其它的完全不懂。Google过,完全没有我要找的东西。也在Ubuntu下装了luadoc,结果一样的问题。在《Lua程序设计》里看了下lua找模块的方法,然后只好胡乱尝试设置LUA_PATH变量了。辛苦的试错过程就不多说,找到的解决办法是在PKGBUILDcmake前边加上

export LUA_PATH='/usr/share/lua/5.1/?/init.lua;/usr/share/lua/5.1/?.lua'

随后编译成功。太晚了,不测试了,先睡觉去。

Category: Linux | Tags: arch awesome Lua

| Theme: Aeros 2.0 by TheBuckmaker.com