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
WAIT_CHANGE_STACKING_ORDER =
0.01
VERSION =
"0.0.8"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.display(dpy = nil) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/wmctrl/wmctrl.rb', line 24

def self.display(dpy = nil)
  dpy ||= ENV["DISPLAY"]
  unless display = @wmctrl[dpy]
    display = self.new(dpy)
    @wmctrl[dpy] = display
  end
  display
end

.instanceObject



19
20
21
22
# File 'lib/wmctrl/wmctrl.rb', line 19

def self.instance
  STDERR.puts "WMCtrl.instance is deprecated. Please use WMCtrl.display alternatively."
  self.display
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.



1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
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
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
# File 'ext/wmctrl.c', line 1249

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)


860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'ext/wmctrl.c', line 860

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)


883
884
885
886
887
888
889
890
891
892
893
894
# File 'ext/wmctrl.c', line 883

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)


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

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

#client_msg(win_id_obj, msg_obj, data0_obj, data1_obj, data2_obj, data3_obj, data4_obj) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/wmctrl.c', line 133

static VALUE rb_client_msg(VALUE self, VALUE win_id_obj, VALUE msg_obj, VALUE data0_obj, VALUE data1_obj, VALUE data2_obj, VALUE data3_obj, VALUE data4_obj)
{
  Display **ptr, *disp;
  Window win_id;
  char *msg;
  unsigned long data0, data1, data2, data3, data4;
  win_id = (Window) NUM2LONG(win_id_obj);
  Data_Get_Struct(self, Display*, ptr);
  disp = *ptr;
  msg = StringValuePtr(msg_obj);
  data0 = (Window) NUM2ULONG(data0_obj);
  data1 = (Window) NUM2ULONG(data1_obj);
  data2 = (Window) NUM2ULONG(data2_obj);
  data3 = (Window) NUM2ULONG(data3_obj);
  data4 = (Window) NUM2ULONG(data4_obj);
  client_msg(disp, win_id, msg, data0, data1, data2, data3, data4);
  return Qtrue;
}

#desktopsArray

Returns An array of WMCtrl::Desktop.

Returns:

  • (Array)

    An array of WMCtrl::Desktop



203
204
205
# File 'lib/wmctrl/wmctrl.rb', line 203

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

#get_display_nameString

Get display name.

Returns:

  • (String)

    A name of display.



98
99
100
101
102
103
104
# File 'ext/wmctrl.c', line 98

static VALUE rb_wmctrl_get_display_name (VALUE self) {
  char *display_name;
  Display **ptr;
  Data_Get_Struct(self, Display*, ptr);
  display_name = DisplayString(*ptr);
  return(RB_UTF8_STRING_NEW2(display_name));
}

#get_window_dataHash

Returns Hash of specified window data.

Returns:

  • (Hash)

    Hash of specified window data



467
468
469
470
471
472
473
474
475
476
# File 'ext/wmctrl.c', line 467

static VALUE rb_wmctrl_get_window_data (VALUE self, VALUE win_id_obj) {
  Display **ptr, *disp;
  Window win_id;
  VALUE display_name;
  win_id = (Window) NUM2LONG(win_id_obj);
  Data_Get_Struct(self, Display*, ptr);
  disp = *ptr;
  display_name = rb_wmctrl_get_display_name(self);
  return get_window_hash_data(win_id, disp, -1, TRUE, display_name);
}

#infoHash

Get hash of information of window manager.

Returns:

  • (Hash)


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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'ext/wmctrl.c', line 732

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.



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
654
655
656
657
658
659
660
661
662
663
664
665
666
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
# File 'ext/wmctrl.c', line 483

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, stacking_order = 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.



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
# File 'ext/wmctrl.c', line 433

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, stacking_order;
  VALUE get_state_obj, stacking_order_obj, window_ary, display_name;
  Data_Get_Struct(self, Display*, ptr);
  disp = *ptr;
  rb_scan_args(argc, argv, "02", &get_state_obj, &stacking_order_obj);
  get_state = RTEST(get_state_obj);
  stacking_order = RTEST(stacking_order_obj);

  if ((client_list = get_client_list(disp, &client_list_size, stacking_order)) == NULL) {
    /* return EXIT_FAILURE; */
    return Qfalse;
  }

  window_active = get_active_window(disp);
  window_ary = rb_ary_new2(client_list_size);
  display_name = rb_wmctrl_get_display_name(self);

  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, display_name));
  }
  g_free(client_list);

  return window_ary;
}

#restack(wins_bottom_to_top, opts = {}) ⇒ Object

Parameters:

  • opts (Hash) (defaults to: {})

    Options

Options Hash (opts):

  • :waiting_time (Float)

    Time to wait restack request

  • :display (String)

    Display name



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/wmctrl/wmctrl.rb', line 253

def restack(wins_bottom_to_top, opts = {})
  win_upper = nil
  waiting_time = opts[:sleep] || WAIT_CHANGE_STACKING_ORDER
  wins_bottom_to_top.reverse.each do |win|
    next if win.sticky?
    if win_upper
      # 1 means below
      WMCtrl.display(opts[:display]).client_msg(win.id, "_NET_RESTACK_WINDOW", 2, win_upper.id, 1, 0, 0)
      # If there is no sleep, change the stacking order fails.
      # Can we use _NET_WM_SYNC_REQUEST?
      sleep(waiting_time)
    end
    win_upper = win
  end
end

#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)


818
819
820
821
822
823
824
# File 'ext/wmctrl.c', line 818

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.



1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
# File 'ext/wmctrl.c', line 1326

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)


713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'ext/wmctrl.c', line 713

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, opts = {}, &block) ⇒ 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.

  • opts (Hash)

    Options

Yields:

Returns:

  • (Array)

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/wmctrl/wmctrl.rb', line 218

def windows(*conditions, &block)
  cond_last = conditions.last || {}
  if cond_last[:order] && (cond_last[:order] == :stacking)
    conditions.pop
    wins = list_windows(true, true)
  else
    wins = list_windows(true, nil)
  end
  wins.map! { |h| WMCtrl::Window.new(h) }
  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