Class: WMCtrl

Inherits:
Object
  • Object
show all
Defined in:
lib/wmctrl/wmctrl.rb,
lib/wmctrl/version.rb,
ext/wmctrl.c

Defined Under Namespace

Classes: DataHash, Desktop, Window

Constant Summary collapse

NOT_USE_NET_MOVERESIZE_WINDOW =
-1
GRAVITY_DEFAULT =
0
GRAVITY_NORTH_WEST =
1
GRAVITY_NORTH =
2
GRAVITY_NORTH_EAST =
3
GRAVITY_WEST =
4
GRAVITY_CENTER =
5
GRAVITY_EAST =
6
GRAVITY_SOUTH_WEST =
7
GRAVITY_SOUTH =
8
GRAVITY_SOUTH_EAST =
9
GRAVITY_STATIC =
10
VERSION =
"0.0.7"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



17
18
19
# File 'lib/wmctrl/wmctrl.rb', line 17

def self.instance
  @wmctrl ||= self.new
end

Instance Method Details

#action_window(wid, : activate) ⇒ boolean #action_window(wid, : close) ⇒ boolean #action_window(wid, : move_resize, grav, x, y, w, h) ⇒ boolean #action_window(wid, : change_state, "add", prop1, prop2 = nil) ⇒ boolean #action_window(wid, : change_state, "remove", prop1, prop2 = nil) ⇒ boolean #action_window(wid, : change_state, "toggle", prop1, prop2 = nil) ⇒ boolean #action_window(wid, : move_to_desktop, desktop_id) ⇒ boolean #action_window(wid, : move_to_current) ⇒ boolean #action_window(wid, : set_title_long, str) ⇒ boolean #action_window(wid, : set_title_short, str) ⇒ boolean #action_window(wid, : set_title_both, str) ⇒ boolean

Note:

The arguments of prop1 and prop2 for :change_state command are strings that mean type of _NET_WM_STATE. For example, For example, we use "modal" or "MODAL" for _NET_WM_STATE_MODAL. See also http://standards.freedesktop.org/wm-spec/wm-spec-latest.html

Manage a window.

Overloads:

  • #action_window(wid, : activate) ⇒ boolean

    Activate a window.

    Parameters:

    • wid (Integer)

      Window ID

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.

  • #action_window(wid, : close) ⇒ boolean

    Close a window

    Parameters:

    • wid (Integer)

      Window ID

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.

  • #action_window(wid, : move_resize, grav, x, y, w, h) ⇒ boolean

    Returns true if succeeded. Otherwise, false.

    Parameters:

    • wid (Integer)

      Window ID

    • grav (Integer)

      Gravity

    • x (Integer)

      X coordinate

    • y (Integer)

      Y coordinate

    • w (Integer)

      Width

    • h (Integer)

      Height

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.

  • #action_window(wid, : change_state, "add", prop1, prop2 = nil) ⇒ boolean

    Returns true if succeeded. Otherwise, false.

    Parameters:

    • wid (Integer)

      Window ID

    • prop1 (String)

      String of _NET_WM_STATE type.

    • prop2 (String) (defaults to: nil)

      String of _NET_WM_STATE type.

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.

  • #action_window(wid, : change_state, "remove", prop1, prop2 = nil) ⇒ boolean

    Returns true if succeeded. Otherwise, false.

    Parameters:

    • wid (Integer)

      Window ID

    • prop1 (String)

      String of _NET_WM_STATE type.

    • prop2 (String) (defaults to: nil)

      String of _NET_WM_STATE type.

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.

  • #action_window(wid, : change_state, "toggle", prop1, prop2 = nil) ⇒ boolean

    Returns true if succeeded. Otherwise, false.

    Parameters:

    • wid (Integer)

      Window ID

    • prop1 (String)

      String of _NET_WM_STATE type.

    • prop2 (String) (defaults to: nil)

      String of _NET_WM_STATE type.

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.

  • #action_window(wid, : move_to_desktop, desktop_id) ⇒ boolean

    Returns true if succeeded. Otherwise, false.

    Parameters:

    • wid (Integer)

      Window ID

    • desktop_id (Integer)

      Desktop ID

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.

  • #action_window(wid, : move_to_current) ⇒ boolean

    Returns true if succeeded. Otherwise, false.

    Parameters:

    • wid (Integer)

      Window ID

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.

  • #action_window(wid, : set_title_long, str) ⇒ boolean

    Returns true if succeeded. Otherwise, false.

    Parameters:

    • wid (Integer)

      Window ID

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.

  • #action_window(wid, : set_title_short, str) ⇒ boolean

    Returns true if succeeded. Otherwise, false.

    Parameters:

    • wid (Integer)

      Window ID

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.

  • #action_window(wid, : set_title_both, str) ⇒ boolean

    Returns true if succeeded. Otherwise, false.

    Parameters:

    • wid (Integer)

      Window ID

    Returns:

    • (boolean)

      true if succeeded. Otherwise, false.



1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
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
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
# File 'ext/wmctrl.c', line 1200

static VALUE rb_wmctrl_action_window(int argc, VALUE *argv, VALUE self) {
  Window win;
  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.");
  }
  win = get_target_window(disp, argv[0]);
  sym_id = SYM2ID(argv[1]);
  argc = argc - 2;
  argv = argv + 2;
  p_verbose("Using window: 0x%.8lx\n", win);
  if (sym_id == id_activate) {
    if (argc == 0) {
      return activate_window(disp, win, TRUE);
    }
  } else if (sym_id == id_close) {
    if (argc == 0) {
      return close_window(disp, win);
    }
  } else if (sym_id == id_move_resize) {
    /* resize/move the window around the desktop => -r -e */
    if (argc == 5) {
      int grav, x, y, w, h;
      grav = FIX2INT(argv[0]);
      x = FIX2INT(argv[1]);
      y = FIX2INT(argv[2]);
      w = FIX2INT(argv[3]);
      h = FIX2INT(argv[4]);
      return window_move_resize(disp, win, grav, x, y, w, h);
    }
  } else if (sym_id == id_change_state) {
    /* change state of a window => -r -b */
    if (argc == 2 || argc == 3) {
      const char *action, *prop1, *prop2;
      action = StringValuePtr(argv[0]);
      prop1 = StringValuePtr(argv[1]);
      if (argc == 3) {
	prop2 = StringValuePtr(argv[2]);
      } else {
	prop2 = NULL;
      }
      return change_window_state(disp, win, action, prop1, prop2);
    }
  } else if (sym_id == id_move_to_desktop) {
    /* move the window to the specified desktop => -r -t */
    if (argc == 1) {
      if (window_to_desktop(disp, win, FIX2INT(argv[0]))) {
        return Qtrue;
      }
      return Qfalse;
    }
  } else if (sym_id == id_move_to_current) {
    /* move the window to the current desktop and activate it => -r */
    if (argc == 0) {
      if (window_to_desktop(disp, win, -1)) {
	usleep(100000);
        /* 100 ms - make sure the WM has enough time to move the window, before we activate it */
	return activate_window(disp, win, FALSE);
      }
      return Qfalse;
    }
  } else if (sym_id == id_set_title_long || sym_id == id_set_title_short || sym_id == id_set_title_both) {
    if (argc == 1) {
      return window_set_title(disp, win, StringValuePtr(argv[0]), sym_id);
    }
  }
  rb_raise(rb_eStandardError, "Invalid argument of action_window.");
}

#change_geometry(w, h) ⇒ true

Change geometry of all desktops. A window manager may ignore this request.

Parameters:

  • w (Ineteger)

    Width

  • h (Ineteger)

    Height

Returns:

  • (true)


811
812
813
814
815
816
817
818
819
820
821
822
823
# File 'ext/wmctrl.c', line 811

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.

Parameters:

  • num (Integer)

    Number of desktops.

Returns:

  • (true)


834
835
836
837
838
839
840
841
842
843
844
845
# File 'ext/wmctrl.c', line 834

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.

Parameters:

  • x (Integer)

    Offset of top position

  • y (Integer)

    Offset of left position

Returns:

  • (true)


787
788
789
790
791
792
793
794
795
796
797
798
799
# File 'ext/wmctrl.c', line 787

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;
}

#desktopsArray

Returns An array of WMCtrl::Desktop.

Returns:

  • (Array)

    An array of WMCtrl::Desktop



190
191
192
# File 'lib/wmctrl/wmctrl.rb', line 190

def desktops
  list_desktops.map { |hash| WMCtrl::Desktop.new(hash) }
end

#get_window_dataHash

Returns Hash of specified window data.

Returns:

  • (Hash)

    Hash of specified window data



420
421
422
423
424
425
426
427
# File 'ext/wmctrl.c', line 420

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);
}

#infoHash

Get hash of information of window manager.

Returns:

  • (Hash)


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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'ext/wmctrl.c', line 683

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_desktopsHash

Get list of information of desktops.

Returns:

  • (Hash)

    An array of hashes of desktop information.



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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'ext/wmctrl.c', line 434

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.

Parameters:

  • get_state (Boolean)

    If the value is true then we get some properties at the same time

Returns:

  • (Hash)

    An array of hashes of window information.



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'ext/wmctrl.c', line 388

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.

Parameters:

  • state (boolean)

    We set true if we want to turn on showing desktop mode.

Returns:

  • (true)


769
770
771
772
773
774
775
# File 'ext/wmctrl.c', line 769

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;
}

#supportedArray

Get an array of _NET_SUPPORTED property.

Returns:

  • (Array)

    An array of strings.



1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
# File 'ext/wmctrl.c', line 1277

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.

Parameters:

  • desktop_id (Integer)

    ID number of desktop.

Returns:

  • (Qtrue)


664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'ext/wmctrl.c', line 664

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) {|WMCtrl::Window| ... } ⇒ Array

Returns An array of WMCtrl::Window including windows matched by the conditions and the block.

Parameters:

  • conditions (Array)

    An array of condition hash. The keys of the hash are keys of WMCtrl::Window#[]. The values of the hash are tested by the method ===. Keys of each condition are combined by logical product and all conditions are combined by logical sum.

Yields:

Returns:

  • (Array)

    An array of WMCtrl::Window including windows matched by the conditions and the block.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/wmctrl/wmctrl.rb', line 202

def windows(*conditions, &block)
  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
  if block_given?
    wins.delete_if { |win| !yield(win) }
  end
  wins
end