首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过gir_ffi gem从i3ipc-glib访问i3-wm JSON响应obj

通过gir_ffi gem从i3ipc-glib访问i3-wm JSON响应obj
EN

Stack Overflow用户
提问于 2014-08-03 01:04:47
回答 1查看 144关注 0票数 0

我希望通过一个ruby脚本来控制我的i3窗口管理器。不再维护existing gem,建议您结合使用i3ipc-glib C库和gir_ffi来复制其功能。(GIR“基于GObject自省存储库系统提供的自省数据,在运行时为基于gir_ffi的库创建绑定”)。

因此,我可以成功地使用它来发送命令:

代码语言:javascript
复制
require 'gir_ffi'

namespace = 'i3ipc'
GirFFI.setup namespace

i3 = I3ipc::Connection.new(nil)
i3.command 'focus right'

然而,我没有收到i3 IPC documentation中概述的JSON回复。相反,我得到了一个GLib::Slist (doc)对象(在某些情况下是I3ipc::Con)。

代码语言:javascript
复制
2.1.2 :108 > command = i3.command('focus left')
 => #<GLib::SList:0x000000019f70d0 @struct=#<GLib::SList::Struct:0x000000019f7058>, @element_type=I3ipc::CommandReply>
2.1.2 :111 > command.class
 => GLib::SList
2.1.2 :112 > command.class.superclass
 => GirFFI::StructBase

2.1.2 :094 > workspaces = i3.get_workspaces
 => #<GLib::SList:0x00000001ab6ae8 @struct=#<GLib::SList::Struct:0x00000001ab6ac0>, @element_type=I3ipc::WorkspaceReply> 
2.1.2 :095 > workspaces.class
 => GLib::SList 

2.1.2 :035 > tree = i3.get_tree
 => #<I3ipc::Con:0x000000013c4a78 @struct=#<I3ipc::Con::Struct:0x000000013c51f8>> 
2.1.2 :036 > tree.class
 => I3ipc::Con 
2.1.2 :037 > tree.class.superclass
 => GObject::Object 
2.1.2 :038 > tree.class.superclass.superclass
 => GirFFI::ObjectBase 

如何访问原始的JSON响应?

EN

回答 1

Stack Overflow用户

发布于 2015-05-23 22:58:37

你可以尝试一个改进的i3ipc gem:

代码语言:javascript
复制
>> require 'i3ipc' #=> true
>> i3 = I3Ipc::Connection.new
=> #<I3Ipc::Connection:0x000000009fc068 @protocol=#  <I3Ipc::Protocol:0x000000009fc040 @socketpath="/run/user/1000/i3/ipc-socket.1043", @socket=#<UNIXSocket:fd 9>>>
>> command = i3.command('focus left') #=> [#<I3Ipc::Reply:0x007fbf90145940 @data={:success=>true}>]
>> command[0].to_h #=> {:success=>true}

>> workspaces = i3.workspaces
=> [#<I3Ipc::Reply:0x007fbf900e77f0 @data={:num=>1, :name=>"1 Browse", :visible=>true, :focused=>false, :rect=>#<I3Ipc::Reply:0x007fbf900ffd78 @data={:x=>1366, :y=>20, :width=>1920, :height=>1060}>, :output=>"VGA1", :urgent=>false}>, #<I3Ipc::Reply:0x007fbf900e6e68 @data={:num=>0, :name=>"0 Term", :visible=>true, :focused=>true, :rect=>#<I3Ipc::Reply:0x007fbf900e7458 @data={:x=>0, :y=>20, :width=>1366, :height=>748}>, :output=>"LVDS1", :urgent=>false}>]
>> workspaces[0].to_h #=> {:num=>1, :name=>"1 Browse", :visible=>true, :focused=>false, :rect=>{:x=>1366, :y=>20, :width=>1920, :height=>1060}, :output=>"VGA1", :urgent=>false}
>> workspaces[0].visible
>> workspaces[0].name #=> "1 Browse"

这实际上不是一个json,而是一个经过解析的散列。但是有一种方法可以使用低级类获取原始字符串响应:

代码语言:javascript
复制
>> protocol = I3Ipc::Protocol.new #=> #<I3Ipc::Protocol:0x007fbf9c08aee0 @socketpath="/run/user/1000/i3/ipc-socket.1043">
>> protocol.connect #=> #<UNIXSocket:fd 10>
# 1 means get_workspaces message
>> protocol.send(1) #=> 14
>> response = protocol.receive(1)
=> "[{\"num\":1,\"name\":\"1 Browse\",\"visible\":true,\"focused\":false,\"rect\":{\"x\":1366,\"y\":20,\"width\":1920,\"height\":1060},\"output\":\"VGA1\",\"urgent\":false},{\"num\":0,\"name\":\"0 Term\",\"visible\":true,\"focused\":true,\"rect\":{\"x\":0,\"y\":20,\"width\":1366,\"height\":748},\"output\":\"LVDS1\",\"urgent\":false}]"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25097454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档