Class: WMCtrl
- Inherits:
-
Object
- Object
- WMCtrl
- Defined in:
- lib/wmctrl/wmctrl.rb,
lib/wmctrl/version.rb,
ext/wmctrl.c
Defined Under Namespace
Classes: DataHash, Desktop, Window
Constant Summary collapse
- VERSION =
"0.0.6"
Class Method Summary collapse
Instance Method Summary collapse
-
#action_window(wid, cmd, *args) ⇒ boolean
Manage windows.
-
#change_geometry(w, h) ⇒ true
Change geometry of all desktops.
-
#change_number_of_desktops(num) ⇒ true
Change number of desktops.
-
#change_viewport(x, y) ⇒ true
Change the viewport.
-
#desktops ⇒ Array
An array of WMCtrl::Desktop.
-
#get_window_data ⇒ Hash
Hash of specified window data.
-
#info ⇒ Hash
Get hash of information of window manager.
-
#list_desktops ⇒ Hash
Get list of information of desktops.
-
#list_windows(get_state = nil) ⇒ Hash
Get list of information of windows.
-
#showing_desktop(state) ⇒ true
Minimize windows to show desktop if the window manager implements "show the desktop" mode.
-
#supported ⇒ Array
Get an array of _NET_SUPPORTED property.
-
#switch_desktop(desktop_id) ⇒ Qtrue
Switch desktop.
-
#windows(*conditions) ⇒ Array
An array of WMCtrl::Window.
Class Method Details
.instance ⇒ Object
4 5 6 |
# File 'lib/wmctrl/wmctrl.rb', line 4 def self.instance @wmctrl ||= self.new end |
Instance Method Details
#action_window(wid, cmd, *args) ⇒ boolean
Manage windows.
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 |
# File 'ext/wmctrl.c', line 1216
static VALUE rb_wmctrl_action_window(int argc, VALUE *argv, VALUE self) {
Window wid;
int mode;
ID sym_id;
Display **ptr, *disp;
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
if (argc < 2) {
rb_raise(rb_eArgError, "Need more than one argument.");
}
wid = get_target_window(disp, argv[0]);
sym_id = SYM2ID(argv[1]);
if (sym_id == id_activate) {
mode = 'a';
} else if (sym_id == id_close) {
mode = 'c';
} else if (sym_id == id_move_resize) {
mode = 'e';
} else if (sym_id == id_change_state) {
mode = 'b';
} else if (sym_id == id_move_to_desktop) {
mode = 't';
} else if (sym_id == id_move_to_current) {
mode = 'R';
} else if (sym_id == id_set_title_long) {
mode = 'N';
} else if (sym_id == id_set_title_short) {
mode = 'I';
} else if (sym_id == id_set_title_both) {
mode = 'T';
} else {
rb_raise(rb_eStandardError, "Invalid argument of action_window.");
}
if (action_window(disp, wid, mode, argc - 2, (argv + 2))) {
return Qtrue;
} else {
return Qfalse;
}
}
|
#change_geometry(w, h) ⇒ true
Change geometry of all desktops. A window manager may ignore this request.
795 796 797 798 799 800 801 802 803 804 805 806 807 |
# File 'ext/wmctrl.c', line 795
static VALUE rb_wmctrl_change_geometry (VALUE self, VALUE xnum, VALUE ynum) {
long x, y;
Display **ptr, *disp;
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
x = NUM2LONG(xnum);
y = NUM2LONG(ynum);
if (x < 0 || y < 0) {
rb_raise(rb_eArgError, "Arguments must be nonnegative integers.");
}
client_msg(disp, DefaultRootWindow(disp), "_NET_DESKTOP_GEOMETRY", x, y, 0, 0, 0);
return Qtrue;
}
|
#change_number_of_desktops(num) ⇒ true
Change number of desktops.
818 819 820 821 822 823 824 825 826 827 828 829 |
# File 'ext/wmctrl.c', line 818
static VALUE rb_wmctrl_change_number_of_desktops (VALUE self, VALUE num) {
long n;
Display **ptr, *disp;
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
n = NUM2LONG(num);
if (n < 0) {
rb_raise(rb_eArgError, "An argument must be a nonnegative integer.");
}
client_msg(disp, DefaultRootWindow(disp), "_NET_NUMBER_OF_DESKTOPS", n, 0, 0, 0, 0);
return Qtrue;
}
|
#change_viewport(x, y) ⇒ true
Change the viewport. A window manager may ignore this request.
771 772 773 774 775 776 777 778 779 780 781 782 783 |
# File 'ext/wmctrl.c', line 771
static VALUE rb_wmctrl_change_viewport (VALUE self, VALUE xnum, VALUE ynum) {
long x, y;
Display **ptr, *disp;
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
x = NUM2LONG(xnum);
y = NUM2LONG(ynum);
if (x < 0 || y < 0) {
rb_raise(rb_eArgError, "Arguments must be nonnegative integers.");
}
client_msg(disp, DefaultRootWindow(disp), "_NET_DESKTOP_VIEWPORT", x, y, 0, 0, 0);
return Qtrue;
}
|
#desktops ⇒ Array
Returns An array of WMCtrl::Desktop.
147 148 149 |
# File 'lib/wmctrl/wmctrl.rb', line 147 def desktops list_desktops.map { |hash| WMCtrl::Desktop.new(hash) } end |
#get_window_data ⇒ Hash
Returns Hash of specified window data.
404 405 406 407 408 409 410 411 |
# File 'ext/wmctrl.c', line 404
static VALUE rb_wmctrl_get_window_data (VALUE self, VALUE win_id_obj) {
Display **ptr, *disp;
Window win_id;
win_id = (Window) NUM2LONG(win_id_obj);
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
return get_window_hash_data(win_id, disp, -1, TRUE);
}
|
#info ⇒ Hash
Get hash of information of window manager.
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 |
# File 'ext/wmctrl.c', line 667
static VALUE rb_wmctrl_info (VALUE self) {
Display **ptr, *disp;
Window *sup_window = NULL;
gchar *wm_name = NULL;
gchar *wm_class = NULL;
unsigned long *wm_pid = NULL;
unsigned long *showing_desktop = NULL;
gboolean name_is_utf8;
VALUE ret = rb_hash_new();
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
if (! (sup_window = (Window *)get_property(disp, DefaultRootWindow(disp),
XA_WINDOW, "_NET_SUPPORTING_WM_CHECK", NULL))) {
if (! (sup_window = (Window *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_WIN_SUPPORTING_WM_CHECK", NULL))) {
fputs("Cannot get window manager info properties.\n"
"(_NET_SUPPORTING_WM_CHECK or _WIN_SUPPORTING_WM_CHECK)\n", stderr);
/* return EXIT_FAILURE; */
return Qfalse;
}
}
/* WM_NAME */
name_is_utf8 = TRUE;
if (! (wm_name = get_property(disp, *sup_window,
XInternAtom(disp, "UTF8_STRING", False), "_NET_WM_NAME", NULL))) {
name_is_utf8 = FALSE;
if (! (wm_name = get_property(disp, *sup_window,
XA_STRING, "_NET_WM_NAME", NULL))) {
p_verbose("Cannot get name of the window manager (_NET_WM_NAME).\n");
}
}
if (wm_name) {
rb_hash_aset(ret, key_name, (name_is_utf8 ? RB_UTF8_STRING_NEW2(wm_name) : rb_str_new(wm_name, strlen(wm_name))));
}
/* WM_CLASS */
name_is_utf8 = TRUE;
if (! (wm_class = get_property(disp, *sup_window,
XInternAtom(disp, "UTF8_STRING", False), "WM_CLASS", NULL))) {
name_is_utf8 = FALSE;
if (! (wm_class = get_property(disp, *sup_window,
XA_STRING, "WM_CLASS", NULL))) {
p_verbose("Cannot get class of the window manager (WM_CLASS).\n");
}
}
if (wm_class) {
rb_hash_aset(ret, key_class, (name_is_utf8 ? RB_UTF8_STRING_NEW2(wm_class) : rb_str_new(wm_class, strlen(wm_class))));
}
/* WM_PID */
if (! (wm_pid = (unsigned long *)get_property(disp, *sup_window,
XA_CARDINAL, "_NET_WM_PID", NULL))) {
p_verbose("Cannot get pid of the window manager (_NET_WM_PID).\n");
}
/* _NET_SHOWING_DESKTOP */
if (! (showing_desktop = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_SHOWING_DESKTOP", NULL))) {
p_verbose("Cannot get the _NET_SHOWING_DESKTOP property.\n");
}
rb_hash_aset(ret, key_pid, (wm_pid ? UINT2NUM(*wm_pid) : Qnil));
rb_hash_aset(ret, key_showing_desktop,
(showing_desktop ? RB_UTF8_STRING_NEW2(*showing_desktop == 1 ? "ON" : "OFF") : Qnil));
g_free(sup_window);
g_free(wm_name);
g_free(wm_class);
g_free(wm_pid);
g_free(showing_desktop);
return ret;
}
|
#list_desktops ⇒ Hash
Get list of information of desktops.
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
# File 'ext/wmctrl.c', line 418
static VALUE rb_wmctrl_list_desktops (VALUE self) {
Display **ptr, *disp;
unsigned long *num_desktops = NULL;
unsigned long *cur_desktop = NULL;
unsigned long desktop_list_size = 0;
unsigned long *desktop_geometry = NULL;
unsigned long desktop_geometry_size = 0;
unsigned long *desktop_viewport = NULL;
unsigned long desktop_viewport_size = 0;
unsigned long *desktop_workarea = NULL;
unsigned long desktop_workarea_size = 0;
gchar *list = NULL;
unsigned int i;
unsigned int id;
Window root;
const gchar *error_message = NULL;
VALUE ret = Qnil;
VALUE *ret_arys;
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
root = DefaultRootWindow(disp);
if (! (num_desktops = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_NET_NUMBER_OF_DESKTOPS", NULL))) {
if (! (num_desktops = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_WIN_WORKSPACE_COUNT", NULL))) {
error_message = "Cannot get number of desktops properties. (_NET_NUMBER_OF_DESKTOPS or _WIN_WORKSPACE_COUNT)";
goto cleanup;
}
}
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_NET_CURRENT_DESKTOP", NULL))) {
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_WIN_WORKSPACE", NULL))) {
error_message = "Cannot get current desktop properties. (_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)";
goto cleanup;
}
}
if ((list = get_property(disp, root, XInternAtom(disp, "UTF8_STRING", False),
"_NET_DESKTOP_NAMES", &desktop_list_size)) == NULL) {
/* If charaset is not utf8 then there may be bugs here. */
if ((list = get_property(disp, root,
XA_STRING,
"_WIN_WORKSPACE_NAMES", &desktop_list_size)) == NULL) {
p_verbose("Cannot get desktop names properties. "
"(_NET_DESKTOP_NAMES or _WIN_WORKSPACE_NAMES)"
"\n");
/* ignore the error - list the desktops without names */
}
}
/* common size of all desktops */
if (! (desktop_geometry = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_DESKTOP_GEOMETRY", &desktop_geometry_size))) {
p_verbose("Cannot get common size of all desktops (_NET_DESKTOP_GEOMETRY).\n");
}
/* desktop viewport */
if (! (desktop_viewport = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_DESKTOP_VIEWPORT", &desktop_viewport_size))) {
p_verbose("Cannot get common size of all desktops (_NET_DESKTOP_VIEWPORT).\n");
}
/* desktop workarea */
if (! (desktop_workarea = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_WORKAREA", &desktop_workarea_size))) {
if (! (desktop_workarea = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_WIN_WORKAREA", &desktop_workarea_size))) {
p_verbose("Cannot get _NET_WORKAREA property.\n");
}
}
ret_arys = (VALUE *)g_malloc0(*num_desktops * sizeof(VALUE));
for (i = 0; i < *num_desktops; i++) {
ret_arys[i] = rb_hash_new();
rb_hash_aset(ret_arys[i], key_id, UINT2NUM(i));
if (i == *cur_desktop) {
rb_hash_aset(ret_arys[i], key_current, Qtrue);
} else {
rb_hash_aset(ret_arys[i], key_current, Qnil);
}
}
if (list) {
id = 0;
rb_hash_aset(ret_arys[id], key_title, RB_UTF8_STRING_NEW2(list));
id++;
for (i = 0; i < desktop_list_size; i++) {
if (list[i] == '\0') {
if (id >= *num_desktops) {
break;
}
rb_hash_aset(ret_arys[id], key_title, RB_UTF8_STRING_NEW2(list + i + 1));
id++;
}
}
}
/* prepare desktop geometry strings */
if (desktop_geometry && desktop_geometry_size > 0) {
if (desktop_geometry_size == 2 * sizeof(*desktop_geometry)) {
/* only one value - use it for all desktops */
p_verbose("WM provides _NET_DESKTOP_GEOMETRY value common for all desktops.\n");
for (i = 0; i < *num_desktops; i++) {
rb_hash_aset(ret_arys[i], key_geometry,
rb_ary_new3(2, INT2NUM(desktop_geometry[0]), INT2NUM(desktop_geometry[1])));
}
}
else {
/* seperate values for desktops of different size */
p_verbose("WM provides separate _NET_DESKTOP_GEOMETRY value for each desktop.\n");
for (i = 0; i < *num_desktops; i++) {
if (i < desktop_geometry_size / sizeof(*desktop_geometry) / 2) {
rb_hash_aset(ret_arys[i], key_geometry,
rb_ary_new3(2, INT2NUM(desktop_geometry[i*2]), INT2NUM(desktop_geometry[i*2+1])));
}
else {
rb_hash_aset(ret_arys[i], key_geometry, Qnil);
}
}
}
}
else {
for (i = 0; i < *num_desktops; i++) {
rb_hash_aset(ret_arys[i], key_geometry, Qnil);
}
}
/* prepare desktop viewport strings */
if (desktop_viewport && desktop_viewport_size > 0) {
if (desktop_viewport_size == 2 * sizeof(*desktop_viewport)) {
/* only one value - use it for current desktop */
p_verbose("WM provides _NET_DESKTOP_VIEWPORT value only for the current desktop.\n");
for (i = 0; i < *num_desktops; i++) {
if (i == *cur_desktop) {
rb_hash_aset(ret_arys[i], key_viewport,
rb_ary_new3(2, INT2NUM(desktop_viewport[0]), INT2NUM(desktop_viewport[1])));
}
else {
rb_hash_aset(ret_arys[i], key_viewport, Qnil);
}
}
}
else {
/* seperate values for each of desktops */
for (i = 0; i < *num_desktops; i++) {
if (i < desktop_viewport_size / sizeof(*desktop_viewport) / 2) {
rb_hash_aset(ret_arys[i], key_viewport,
rb_ary_new3(2, INT2NUM(desktop_viewport[i*2]), INT2NUM(desktop_viewport[i*2+1])));
}
else {
rb_hash_aset(ret_arys[i], key_viewport, Qnil);
}
}
}
}
else {
for (i = 0; i < *num_desktops; i++) {
rb_hash_aset(ret_arys[i], key_viewport, Qnil);
}
}
/* prepare desktop workarea strings */
if (desktop_workarea && desktop_workarea_size > 0) {
if (desktop_workarea_size == 4 * sizeof(*desktop_workarea)) {
/* only one value - use it for current desktop */
p_verbose("WM provides _NET_WORKAREA value only for the current desktop.\n");
for (i = 0; i < *num_desktops; i++) {
if (i == *cur_desktop) {
rb_hash_aset(ret_arys[i], key_workarea,
rb_ary_new3(4, INT2NUM(desktop_workarea[0]), INT2NUM(desktop_workarea[1]),
INT2NUM(desktop_workarea[2]), INT2NUM(desktop_workarea[3])));
}
else {
rb_hash_aset(ret_arys[i], key_workarea, Qnil);
}
}
}
else {
/* seperate values for each of desktops */
for (i = 0; i < *num_desktops; i++) {
if (i < desktop_workarea_size / sizeof(*desktop_workarea) / 4) {
rb_hash_aset(ret_arys[i], key_workarea,
rb_ary_new3(4, INT2NUM(desktop_workarea[i*4]), INT2NUM(desktop_workarea[i*4+1]),
INT2NUM(desktop_workarea[i*4+2]), INT2NUM(desktop_workarea[i*4+3])));
}
else {
rb_hash_aset(ret_arys[i], key_workarea, Qnil);
}
}
}
}
else {
for (i = 0; i < *num_desktops; i++) {
rb_hash_aset(ret_arys[i], key_workarea, Qnil);
}
}
ret = rb_ary_new4(*num_desktops, ret_arys);
p_verbose("Total number of desktops: %lu\n", *num_desktops);
p_verbose("Current desktop ID (counted from zero): %lu\n", *cur_desktop);
goto cleanup;
cleanup:
g_free(num_desktops);
g_free(cur_desktop);
g_free(desktop_geometry);
g_free(desktop_viewport);
g_free(desktop_workarea);
g_free(list);
if (error_message) {
rb_raise(rb_eStandardError, "%s", error_message);
}
return ret;
}
|
#list_windows(get_state = nil) ⇒ Hash
Get list of information of windows.
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'ext/wmctrl.c', line 372
static VALUE rb_wmctrl_list_windows (int argc, VALUE *argv, VALUE self) {
Display **ptr, *disp;
Window *client_list;
Window window_active;
unsigned long client_list_size;
unsigned int i;
int get_state;
VALUE get_state_obj, window_ary;
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
rb_scan_args(argc, argv, "01", &get_state_obj);
get_state = RTEST(get_state_obj);
if ((client_list = get_client_list(disp, &client_list_size)) == NULL) {
/* return EXIT_FAILURE; */
return Qfalse;
}
window_active = get_active_window(disp);
window_ary = rb_ary_new2(client_list_size);
for (i = 0; i < client_list_size / sizeof(Window); i++) {
rb_ary_push(window_ary, get_window_hash_data(client_list[i], disp, window_active, get_state));
}
g_free(client_list);
return window_ary;
}
|
#showing_desktop(state) ⇒ true
Minimize windows to show desktop if the window manager implements "show the desktop" mode.
753 754 755 756 757 758 759 |
# File 'ext/wmctrl.c', line 753
static VALUE rb_wmctrl_showing_desktop (VALUE self, VALUE state) {
Display **ptr, *disp;
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
client_msg(disp, DefaultRootWindow(disp), "_NET_SHOWING_DESKTOP", RTEST(state), 0, 0, 0, 0);
return Qtrue;
}
|
#supported ⇒ Array
Get an array of _NET_SUPPORTED property.
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 |
# File 'ext/wmctrl.c', line 1261
static VALUE rb_wmctrl_supported (VALUE self)
{
Atom *list;
unsigned long size;
unsigned int i;
gchar *prop_name;
VALUE ret;
Display **ptr, *disp;
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
if (!(list = (Atom *)get_property(disp, DefaultRootWindow(disp), XA_ATOM, "_NET_SUPPORTED", &size))) {
rb_raise(rb_eStandardError, "Cannot get _NET_SUPPORTED property.");
}
ret = rb_ary_new();
for (i = 0; i < size / sizeof(Atom); i++) {
prop_name = XGetAtomName(disp, list[i]);
rb_ary_push(ret, rb_str_new2(prop_name));
g_free(prop_name);
}
g_free(list);
return ret;
}
|
#switch_desktop(desktop_id) ⇒ Qtrue
Switch desktop.
648 649 650 651 652 653 654 655 656 657 658 659 660 |
# File 'ext/wmctrl.c', line 648
static VALUE rb_wmctrl_switch_desktop (VALUE self, VALUE desktop_id) {
int target;
Display **ptr, *disp;
Data_Get_Struct(self, Display*, ptr);
disp = *ptr;
target = FIX2INT(desktop_id);
if (target < 0) {
rb_raise(rb_eStandardError, "Invalid desktop ID: %d", target);
}
client_msg(disp, DefaultRootWindow(disp), "_NET_CURRENT_DESKTOP", (unsigned long)target, 0, 0, 0, 0);
return Qtrue;
}
|
#windows(*conditions) ⇒ Array
Returns An array of WMCtrl::Window.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/wmctrl/wmctrl.rb', line 153 def windows(*conditions) wins = list_windows(true).map { |hash| WMCtrl::Window.new(hash) } unless conditions.empty? wins_selected = [] conditions.each do |condition| wins_rest = [] wins.each do |win| if condition.all? { |k, v| v === win[k] } wins_selected << win else wins_rest << win end end wins = wins_rest end wins = wins_selected end wins end |