神奇的ncurses

139 views
Skip to first unread message

lumin

unread,
Jan 8, 2015, 8:36:24 AM1/8/15
to xidian...@googlegroups.com
一直感觉htop这个程序不对劲,很久以前发现这家伙在终端里可以用鼠标操作
(几年前发现aptitude这个CLI程序响应鼠标事件)
最近发现,htop在tty下同样可以借助gpm来响应鼠标事件,甚为神奇。

为了跟踪这样的超级牛力从何而来,先找htop的alternative看看依赖 : top
值得注意的是top不支持鼠标事件。
$ which top | xargs ldd
linux-vdso.so.1 (0x00007fff153fc000)
libprocps.so.3 => /lib/x86_64-linux-gnu/libprocps.so.3
(0x00007ff28c0f1000)
libncurses.so.5 => /lib/x86_64-linux-gnu/libncurses.so.5
(0x00007ff28becc000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5
(0x00007ff28bca1000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff28ba9d000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff28b6f4000)
/lib64/ld-linux-x86-64.so.2 (0x00007ff28c336000)

再看htop,进行对照
$ which htop | xargs ldd
linux-vdso.so.1 (0x00007fffb5d5e000)
libncursesw.so.5 => /lib/x86_64-linux-gnu/libncursesw.so.5
(0x00007f484720c000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5
(0x00007f4846fe2000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4846ce0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4846937000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4846733000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4847463000)

对比之后发现二者链接的库的区别
libncursesw.so.5 <- htop
libncurses.so.5 <- top
后缀的w的含义:
This package contains the shared libraries necessary to run programs
compiled with ncursesw, which includes support for wide characters.
也就是说后缀w不区分鼠标事件接口。

虽然从这些信息上还不能推断鼠标相应功能是谁提供,但是ncurses库有很大嫌疑。
libc, libm linux-vdso, ld-linux-x86-64.可以直接排除。
剩下的嫌疑文件用dpkg -S及。。。。。。结果显示

1. libtinfo 是ncurses的一部分。
2. libdl 是libc的一部分

另外,
3. libc根本不鸟CLI
4. 特殊.so文件更不会与此功能有关

综上,可能的结果就只有一个了:
ncurses提供了让CLI响应鼠标事件的接口。
太神奇。

参看软件包(debian):
libncursesw5*
libncurses5*

Zhang Cheng

unread,
Jan 8, 2015, 9:58:54 AM1/8/15
to xidian...@googlegroups.com
支持鼠标不是ncurses干的事情。不过我没有研究过具体是怎么实现的,但是以下事实可以证明不是ncurses干的。

* vim也支持鼠标(:h 'mouse'),vim并没有链接ncurses库
* ssh远程主机之后,在远程主机上运行vim,同样支持鼠标
* 开screen/tmux之后,再开vim仍然支持鼠标 

在开了ssh/screen之后,鼠标的动作相当于由 term程序 传递给 ssh/screen,ssh/screen再传递到远程主机的应用上。



--
您收到此邮件是因为您订阅了“西电开源社区”邮件列表。
要向此邮件列表发帖,请发送电子邮件至 xidian...@googlegroups.com
要取消订阅,请发送电子邮件至 xidian_linux+unsubscribe@googlegroups.com
请通过 https://groups.google.com/group/xidian_linux?hl=zh-CN 访问此网上论坛。
通过 [ipv6 enabled] http://xdlinux.info/http://xdl.in/
    [ipv4 only] http://linux.xidian.edu.cn/
     [手机]:http://m.xdlinux.info/   访问西电开源社区。
--- 您收到此邮件是因为您订阅了 Google 网上论坛的“西电开源社区邮件列表”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到xidian_linux+unsubscribe@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/d/optout



--
Cheng,
Best Regards

Zhang Cheng

unread,
Jan 8, 2015, 10:04:27 AM1/8/15
to xidian...@googlegroups.com
刚才看了一下,ncurse确实可以处理鼠标,见这里:http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/mouse.html

也就是说,vim应该自己实现了鼠标的支持。
--
Cheng,
Best Regards

lumin

unread,
Jan 8, 2015, 9:07:46 PM1/8/15
to xidian...@googlegroups.com, steph...@gmail.com
或许 vim 自己处理了一些东西吧。不喜欢别人的东西自己造个轮子(或者说特殊定制/优化)在开源/自由软件圈子里很正常。
我进一步找了一些证据:

1. libtinfo.so 的确是 ncurses库的一员
$ strings libtinfo.so.5 | sort | uniq | nl | grep -i mouse
   342    get_mouse
   643    key_mouse
   644    KEY_MOUSE
   952    mouse_info
  1184    req_mouse_pos
1.1 apt show libtinfo5 (debian)
Package: libtinfo5
Source: ncurses (5.9+20140913-1)         <------ [证据]
Description: shared low-level terminfo library for terminal handling
 The ncurses library routines are a terminal-independent method of
 updating character screens with reasonable optimization.
 .
 This package contains the shared low-level terminfo library.
而vim ldd结果中有libtinfo (jessie的官方编译vim)

2. 安装了ncurses 的 dev包之后搜索头文件,关键字mouse
$ grep -i mouse -r /usr/include | cut -d ':' -f 1 | sort | uniq
./curses.h
./cursesw.h
./dialog.h
./dlg_keys.h
./linux/adb.h
./linux/hid.h
./linux/input.h
./linux/sysctl.h
./linux/tiocl.h
./linux/tty.h
./term.h
./x86_64-linux-gnu/bits/ioctl-types.h

3. 这一段摘自 /usr/include/ncurses.h
1525 /* mouse interface */
1526
1527 #if NCURSES_MOUSE_VERSION > 1
1528 #define NCURSES_MOUSE_MASK(b,m) ((m) << (((b) - 1) * 5))
1529 #else
1530 #define NCURSES_MOUSE_MASK(b,m) ((m) << (((b) - 1) * 6))
1531 #endif
1532
1533 #define NCURSES_BUTTON_RELEASED 001L
1534 #define NCURSES_BUTTON_PRESSED  002L
1535 #define NCURSES_BUTTON_CLICKED  004L
1536 #define NCURSES_DOUBLE_CLICKED  010L
1537 #define NCURSES_TRIPLE_CLICKED  020L
1538 #define NCURSES_RESERVED_EVENT  040L
1539
1540 /* event masks */
1541 #define BUTTON1_RELEASED    NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_RELEASED)
1542 #define BUTTON1_PRESSED     NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_PRESSED)
1543 #define BUTTON1_CLICKED     NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_CLICKED)
1544 #define BUTTON1_DOUBLE_CLICKED  NCURSES_MOUSE_MASK(1, NCURSES_DOUBLE_CLICKED)
1545 #define BUTTON1_TRIPLE_CLICKED  NCURSES_MOUSE_MASK(1, NCURSES_TRIPLE_CLICKED)
1546
1547 #define BUTTON2_RELEASED    NCURSES_MOUSE_MASK(2, NCURSES_BUTTON_RELEASED)
1548 #define BUTTON2_PRESSED     NCURSES_MOUSE_MASK(2, NCURSES_BUTTON_PRESSED)
1549 #define BUTTON2_CLICKED     NCURSES_MOUSE_MASK(2, NCURSES_BUTTON_CLICKED)
1550 #define BUTTON2_DOUBLE_CLICKED  NCURSES_MOUSE_MASK(2, NCURSES_DOUBLE_CLICKED)


真相究竟是什么呢?

On 08/01/15 14:58, Zhang Cheng wrote:
支 持鼠标不是ncurses干的事情。不过我没有研究过具体是怎么实现的,但是以下事实可以证明不是ncurses干的。

* vim也支持鼠标(:h 'mouse'),vim并没有链接ncurses库
* ssh远程主机之后,在远程主机上运行vim,同样支持鼠标
* 开screen/tmux之后,再开vim仍然支持鼠标 

在 开了ssh/screen之后,鼠标的动作相当于由 term程序 传递给 ssh/screen,ssh/screen再传递到远程主机的应用上。

2015-01-08 21:35 GMT+08:00 lumin <cdlum...@163.com>:
一直感觉htop这个 程序不对劲,很久以前发现这家伙在终端里可以用鼠标操作
要取消订阅,请发送电子邮件至 xidian_linux...@googlegroups.com

请通过 https://groups.google.com/group/xidian_linux?hl=zh-CN 访问此网上论坛。
通过 [ipv6 enabled] http://xdlinux.info/http://xdl.in/
    [ipv4 only] http://linux.xidian.edu.cn/
     [手机]:http://m.xdlinux.info/   访问西电开源社区。
--- 您收到此邮件是因为您订阅了 Google 网上论坛的“西电开源社区邮件列表”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到xidian_linux...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/d/optout



--
Cheng,
Best Regards
--
您收到此邮件是因为您订阅了“西电开源社区”邮件列表。
要向此邮件列表发帖,请发送电子邮件至 xidian...@googlegroups.com
要取消订阅,请发送电子邮件至 xidian_linux...@googlegroups.com

请通过 https://groups.google.com/group/xidian_linux?hl=zh-CN 访问此网上论坛。
通过 [ipv6 enabled] http://xdlinux.info/http://xdl.in/
[ipv4 only] http://linux.xidian.edu.cn/
[手机]:http://m.xdlinux.info/
访问西电开源社区。
---
您收到此邮件是因为您订阅了Google网上论坛上的“西电开源社区邮件列表”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到xidian_linux...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

曹丰宇

unread,
Jan 8, 2015, 9:10:30 PM1/8/15
to xidian...@googlegroups.com, steph...@gmail.com
我也放一些找到的东西吧:

http://www.reddit.com/r/linux/comments/1ad51v/til_i_can_control_htop_using_the_mouse_in_a/
Theoretically any ncurses program can have mouse support


也就是说至少htop是通过ncurses处理鼠标事件的
Reply all
Reply to author
Forward
0 new messages