Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added X11Client#window_children

  • Loading branch information...
commit 96292df6ad4245f3ada78648abea33576ecaadf8 1 parent 9932efd
@ayanko authored
View
1  ext/x11_client_ext/x11_client_ext.c
@@ -26,6 +26,7 @@ void Init_x11_client_ext() {
rb_define_method(cX11Client, "stop", X11Client_stop, 0);
rb_define_method(cX11Client, "root_window_id", X11Client_root_window_id, 0);
+ rb_define_method(cX11Client, "window_children_ids", X11Client_window_children_ids, 1);
rb_define_method(cX11Client, "get_window", X11Client_get_window, 1);
rb_define_method(cX11Client, "mousemove", X11Client_mousemove, 2);
View
23 ext/x11_client_ext/x11_client_window.c
@@ -53,6 +53,29 @@ VALUE X11Client_get_window(VALUE self, VALUE window_id) {
return result;
}
+VALUE X11Client_window_children_ids(VALUE self, VALUE window_id) {
+ Window window = NUM2LONG(window_id);
+ Window dummy;
+ Window *children;
+ unsigned int i, nchildren;
+
+ X11Client *client;
+ Data_Get_Struct(self, X11Client, client);
+
+ XSetErrorHandler(X11Client_IgnoreBadWindowHandler);
+
+ VALUE children_ids = rb_ary_new();
+ if (XQueryTree(client->display, window, &dummy, &dummy, &children, &nchildren)) {
+ for (i = 0; i < nchildren; i++) {
+ Window child_id = children[i];
+ rb_funcall(children_ids, rb_intern("push"), 1, LONG2FIX(child_id));
+ }
+ }
+
+ XSetErrorHandler(NULL);
+ return children_ids;
+}
+
VALUE X11Client_window_state(XWindowAttributes attributes) {
VALUE state = Qnil;
switch(attributes.map_state) {
View
3  ext/x11_client_ext/x11_client_window.h
@@ -4,7 +4,8 @@ VALUE X11Client_root_window_id(VALUE self);
// returns window attributes for specified window id
VALUE X11Client_get_window(VALUE self, VALUE window_id);
-//VALUE X11Client_window_children_ids(VALUE self, VALUE window);
+// return children window ids for specified window id
+VALUE X11Client_window_children_ids(VALUE self, VALUE window_id);
// get window state
VALUE X11Client_window_state(XWindowAttributes attributes);
View
23 lib/x11_client.rb
@@ -1,9 +1,32 @@
require "x11_client_ext"
class X11Client
+ def window_children(window_id)
+ window_children_ids(window_id).map do |child_id|
+ get_window(child_id)
+ end.compact
+ end
+
+ def window_descendants(window_id)
+ windows = []
+ ids = [window_id]
+ until ids.empty?
+ window = get_window(ids.shift)
+ windows << window
+ ids += window_children_ids(window['id'])
+ end
+ windows
+ end
def root_window
get_window(root_window_id)
end
+ def root_window_children
+ window_children(root_window_id)
+ end
+
+ def root_window_descendants
+ window_descendants(root_window_id)
+ end
end
View
23 spec/x11_client_spec.rb
@@ -5,6 +5,15 @@
let(:root_window_id) { `xwininfo -root -int | grep "Window id:"`.scan(/\d+/).first.to_i }
+ let(:root_window_children_ids) do
+ ids = []
+ `xwininfo -root -int -children`.split("\n").each do |line|
+ next if line =~ /\d+\s+children:/
+ line =~ /^\s*(\d+)/ and ids.push($1.to_i)
+ end
+ ids
+ end
+
describe "#root_window_id" do
it "should be root window integer" do
root_window_id.should_not be_zero
@@ -22,4 +31,18 @@
window['y'].should == 0
end
end
+
+ describe "#window_children" do
+ it "should return empty array if window does not exists" do
+ client.window_children(0).should == []
+ end
+
+ it "should return root window children array for roor window" do
+ root_window_children_ids.should_not be_empty
+ children = client.window_children(root_window_id)
+
+ children.should be_kind_of(Array)
+ children.map { |child| child['id'] }.sort.should == root_window_children_ids.sort
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.