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

VERSION =
"0.0.5"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



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.

Examples:

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

Parameters:

  • wid

    Window ID

  • cmd (Symbol)

    Symbol of command

  • args (Array)

    Arguments for the command

Returns:

  • (boolean)

    true if succeeded. Otherwise, false.



1196
1197
1198
1199
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
# File 'ext/wmctrl.c', line 1196

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.

Parameters:

  • w (Ineteger)

    Width

  • h (Ineteger)

    Height

Returns:

  • (true)


775
776
777
778
779
780
781
782
783
784
785
786
787
# File 'ext/wmctrl.c', line 775

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)


798
799
800
801
802
803
804
805
806
807
808
809
# File 'ext/wmctrl.c', line 798

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)


751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'ext/wmctrl.c', line 751

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



106
107
108
# File 'lib/wmctrl/wmctrl.rb', line 106

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

#infoHash

Get hash of information of window manager.

Returns:

  • (Hash)


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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'ext/wmctrl.c', line 647

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.



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
# File 'ext/wmctrl.c', line 398

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.



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'ext/wmctrl.c', line 249

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++) {
    VALUE window_obj = rb_hash_new();
    gchar *title_utf8 = get_window_title(disp, client_list[i]); /* UTF8 */
    gchar *client_machine;
    gchar *class_out = get_window_class(disp, client_list[i]); /* UTF8 */
    unsigned long *desktop;

    rb_hash_aset(window_obj, key_id, INT2NUM(client_list[i]));
    rb_hash_aset(window_obj, key_title, (title_utf8 ? RB_UTF8_STRING_NEW2(title_utf8) : Qnil));
    rb_hash_aset(window_obj, key_class, (class_out ? RB_UTF8_STRING_NEW2(class_out) : Qnil));

    if (window_active == client_list[i]) {
      rb_hash_aset(window_obj, key_active, Qtrue);
    } else {
      rb_hash_aset(window_obj, key_active, Qnil);
    }

    /* desktop ID */
    if ((desktop = (unsigned long *)get_property(disp, client_list[i],
  						 XA_CARDINAL, "_NET_WM_DESKTOP", NULL)) == NULL) {
      desktop = (unsigned long *)get_property(disp, client_list[i], XA_CARDINAL, "_WIN_WORKSPACE", NULL);
    }
    /* special desktop ID -1 means "all desktops", so we
       have to convert the desktop value to signed long */
    rb_hash_aset(window_obj, key_desktop, INT2NUM(desktop ? (signed long)*desktop : 0));

    /* client machine */
    client_machine = get_property(disp, client_list[i], XA_STRING, "WM_CLIENT_MACHINE", NULL);
    rb_hash_aset(window_obj, key_client_machine, (client_machine ? RB_UTF8_STRING_NEW2(client_machine) : Qnil));

    if (get_state) {
      unsigned long *pid;
      int x, y, junkx, junky;
      unsigned long j;
      unsigned int wwidth, wheight, bw, depth;
      Window junkroot;
      unsigned long state_size;
      Atom *window_state;
      gchar *state_name;
      VALUE state_ary;
      Atom *extents;
      unsigned long extents_size;
      VALUE extents_ary;
      Atom *strut;
      unsigned long strut_size;
      VALUE strut_ary;

      /* pid */
      pid = (unsigned long *)get_property(disp, client_list[i], XA_CARDINAL, "_NET_WM_PID", NULL);
      rb_hash_aset(window_obj, key_pid, (pid ? ULONG2NUM(*pid) : Qnil));
      g_free(pid);

      /* geometry */
      XGetGeometry (disp, client_list[i], &junkroot, &junkx, &junky, &wwidth, &wheight, &bw, &depth);
      XTranslateCoordinates (disp, client_list[i], junkroot, -bw, -bw, &x, &y, &junkroot);

      rb_hash_aset(window_obj, key_geometry,
		   rb_ary_new3(4, INT2NUM(x), INT2NUM(y), INT2NUM(wwidth), INT2NUM(wheight)));

      /* state */
      if ((window_state = (Atom *)get_property(disp, client_list[i],
					       XA_ATOM, "_NET_WM_STATE", &state_size)) != NULL) {
	state_ary = rb_ary_new();
	for (j = 0; j < state_size / sizeof(Atom); j++) {
	  state_name = XGetAtomName(disp, window_state[j]);
	  rb_ary_push(state_ary, rb_str_new2(state_name));
	  g_free(state_name);
	}
	g_free(window_state);
      } else {
	state_ary = Qnil;
      }
      rb_hash_aset(window_obj, key_state, state_ary);

      /* frame extents */
      if ((extents = (unsigned long *)get_property(disp, client_list[i],
      						   XA_CARDINAL, "_NET_FRAME_EXTENTS", &extents_size)) != NULL) {
	extents_ary = rb_ary_new();
      	for (j = 0; j < extents_size / sizeof(unsigned long); j++) {
      	  rb_ary_push(extents_ary, ULONG2NUM(extents[j]));
      	}
	/* exterior frame */
	if (extents) {
	  rb_hash_aset(window_obj, key_exterior_frame,
		       rb_ary_new3(4, INT2NUM(x - (int)extents[0]), INT2NUM(y - (int)extents[2]),
				   INT2NUM(wwidth + (int)extents[0] + (int)extents[1]),
				   INT2NUM(wheight + (int)extents[2] + (int)extents[3])));
	}
      	g_free(extents);
      } else {
      	extents_ary = Qnil;
      }
      rb_hash_aset(window_obj, key_frame_extents, extents_ary);

      /* strut partial or strut */
      if ((strut = (unsigned long *)get_property(disp, client_list[i],
						 XA_CARDINAL, "_NET_WM_STRUT_PARTIAL", &strut_size)) == NULL) {
	strut = (unsigned long *)get_property(disp, client_list[i], XA_CARDINAL, "_NET_WM_STRUT", &strut_size);
      }
      if (strut) {
	strut_ary = rb_ary_new();
      	for (j = 0; j < strut_size / sizeof(unsigned long); j++) {
      	  rb_ary_push(strut_ary, ULONG2NUM(strut[j]));
      	}
      	g_free(strut);
      } else {
      	strut_ary = Qnil;
      }
      rb_hash_aset(window_obj, key_strut, strut_ary);
    }

    rb_ary_push(window_ary, window_obj);

    g_free(title_utf8);
    g_free(desktop);
    g_free(client_machine);
    g_free(class_out);
  }
  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)


733
734
735
736
737
738
739
# File 'ext/wmctrl.c', line 733

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.



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

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)


628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'ext/wmctrl.c', line 628

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.

Parameters:

  • conditions (Array)

    An array of condition hash. The values of hash are tested by the method ===.

Returns:

  • (Array)

    An array of WMCtrl::Window



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/wmctrl/wmctrl.rb', line 112

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