Class: Wmadd::X::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/wmadd/x.rb

Instance Method Summary collapse

Constructor Details

#initialize(display_obj, id) ⇒ Window

Returns a new instance of Window.



441
442
443
444
445
# File 'lib/wmadd/x.rb', line 441

def initialize(display_obj, id)
  @display_obj = display_obj
  @display = display_obj.display
  @window = id
end

Instance Method Details

#active_windowObject



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/wmadd/x.rb', line 610

def active_window
  net_active_window = X11::XInternAtom(@display, '_NET_ACTIVE_WINDOW', 1)

  actual_type = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  actual_format = X11::Int.malloc(Fiddle::RUBY_FREE)
  nitems = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  bytes = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  prop = X11::Pointer.malloc(Fiddle::RUBY_FREE)
  X11::XGetWindowProperty(@display, @window, net_active_window, 0, ~0, 0, ANY_PROPERTY_TYPE, actual_type, actual_format, nitems, bytes, prop)
  raise "invalid format: #{actual_format.n}" unless actual_format.n == 32
  raise "invalid nitems: #{nitems.n}" unless nitems.n > 0

  win = X11::UnsignedLong.new(prop.ptr).n
  return nil if win == 0
  Window.new(@display_obj, win)
ensure
  X11::XFree(prop.ptr) unless prop.ptr.null?
end

#change_property_atom(property, mode, data) ⇒ Object



559
560
561
562
563
# File 'lib/wmadd/x.rb', line 559

def change_property_atom(property, mode, data)
  prop_ = X11::XInternAtom(@display, property.to_s, 0)
  data_ = [X11::XInternAtom(@display, data.to_s, 0)].pack('L!')
  X11::XChangeProperty(@display, @window, prop_, X::XA_ATOM, 32, mode, data_, 1)
end

#change_property_cardinal(property, mode, data) ⇒ Object



565
566
567
568
569
570
# File 'lib/wmadd/x.rb', line 565

def change_property_cardinal(property, mode, data)
  prop_ = X11::XInternAtom(@display, property.to_s, 0)
  data_ = X11::UnsignedLong.malloc
  data_.n = data
  X11::XChangeProperty(@display, @window, prop_, X::XA_CARDINAL, 32, mode, data_, 1)
end

#class_hintObject



463
464
465
466
467
# File 'lib/wmadd/x.rb', line 463

def class_hint
  class_hint = X11::XClassHint.malloc(Fiddle::RUBY_FREE)
  ret = X11::XGetClassHint(@display, @window, class_hint)
  ret == 0 ? nil : ClassHint.new(name: class_hint.name.to_s, class_name: class_hint.class_name.to_s)
end

#clearObject



587
588
589
# File 'lib/wmadd/x.rb', line 587

def clear
  X11::XClearWindow(@display, @window)
end

#create_gcObject



582
583
584
585
# File 'lib/wmadd/x.rb', line 582

def create_gc
  gc = X11::XCreateGC(@display, @window, 0, nil)
  GC.new(@display, @window, gc)
end

#create_simple_window(x, y, width, height, border_width, border, background) ⇒ Object



538
539
540
541
# File 'lib/wmadd/x.rb', line 538

def create_simple_window(x, y, width, height, border_width, border, background)
  win = X11::XCreateSimpleWindow(@display, @window, x, y, width, height, border_width, border, background)
  Window.new(@display_obj, win)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


459
460
461
# File 'lib/wmadd/x.rb', line 459

def eql?(other)
  id == other.id
end

#geometryObject



469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/wmadd/x.rb', line 469

def geometry
  root = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  x = X11::Int.malloc(Fiddle::RUBY_FREE)
  y = X11::Int.malloc(Fiddle::RUBY_FREE)
  width = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  height = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  border_width = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  depth = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)

  X11::XGetGeometry(@display, @window, root, x, y, width, height, border_width, depth)
  Geometry.new(x: x.n, y: y.n, width: width.n, height: height.n, border_width: border_width.n, depth: depth.n)
end

#hashObject



455
456
457
# File 'lib/wmadd/x.rb', line 455

def hash
  id.hash
end

#idObject



451
452
453
# File 'lib/wmadd/x.rb', line 451

def id
  @window
end

#inspectObject



447
448
449
# File 'lib/wmadd/x.rb', line 447

def inspect
  "#<#{self.class.name}: 0x#{id.to_s(16)}: #{class_hint&.class_name}: #{wm_name}>"
end

#map_windowObject



547
548
549
# File 'lib/wmadd/x.rb', line 547

def map_window
  X11::XMapWindow(@display, @window)
end

#move_window(x, y) ⇒ Object



543
544
545
# File 'lib/wmadd/x.rb', line 543

def move_window(x, y)
  X11::XMoveWindow(@display, @window, x, y)
end

#parentObject



513
514
515
516
517
518
519
520
521
522
# File 'lib/wmadd/x.rb', line 513

def parent
  root = ' '*8
  parent = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  nchildren = X11::Int.malloc(Fiddle::RUBY_FREE)
  children_p = X11::Pointer.malloc(Fiddle::RUBY_FREE)
  ret = X11::XQueryTree(@display, @window, root, parent, children_p, nchildren)
  return if ret == 0

  Window.new(@display_obj, parent.n)
end

#query_treeObject



524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/wmadd/x.rb', line 524

def query_tree
  root = ' '*8
  parent = ' '*8
  nchildren = X11::Int.malloc(Fiddle::RUBY_FREE)
  children_p = X11::Pointer.malloc(Fiddle::RUBY_FREE)
  ret = X11::XQueryTree(@display, @window, root, parent, children_p, nchildren)
  return if ret == 0

  nchildren.n.times.map do |i|
    win = children_p.ptr[X11::Window.size * i, X11::Window.size].unpack1('L!')
    Window.new(@display_obj, win)
  end
end

#screenObject



578
579
580
# File 'lib/wmadd/x.rb', line 578

def screen
  X11::Screen.new(window_attributes.screen)
end

#select_input(mask) ⇒ Object



509
510
511
# File 'lib/wmadd/x.rb', line 509

def select_input(mask)
  X11::XSelectInput(@display, @window, mask)
end

#unmap_subwindowsObject



555
556
557
# File 'lib/wmadd/x.rb', line 555

def unmap_subwindows
  X11::XUnmapSubwindows(@display, @window)
end

#unmap_windowObject



551
552
553
# File 'lib/wmadd/x.rb', line 551

def unmap_window
  X11::XUnmapWindow(@display, @window)
end

#window_attributesObject



572
573
574
575
576
# File 'lib/wmadd/x.rb', line 572

def window_attributes
  attr = X11::XWindowAttributes.malloc(Fiddle::RUBY_FREE)
  X11::XGetWindowAttributes(@display, @window, attr)
  WindowAttributes.new(**attr.to_h)
end

#window_typeObject



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/wmadd/x.rb', line 591

def window_type
  net_wm_window_type = X11::XInternAtom(@display, '_NET_WM_WINDOW_TYPE', 0)

  property = net_wm_window_type
  actual_type = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  actual_format = X11::Int.malloc(Fiddle::RUBY_FREE)
  nitems = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  bytes = X11::UnsignedLong.malloc(Fiddle::RUBY_FREE)
  prop = X11::Pointer.malloc(Fiddle::RUBY_FREE)

  X11::XGetWindowProperty(@display, @window, property, 0, ~0, 0, ANY_PROPERTY_TYPE, actual_type, actual_format, nitems, bytes, prop)
  return nil if actual_type.n != XA_ATOM
  raise "invalid format: #{actual_format.n}" unless actual_format.n == 32
  type = X11::UnsignedLong.new(prop.ptr)
  @display_obj.wm_window_types[type.n]
ensure
  X11::XFree(prop.ptr) unless prop.ptr.null?
end

#wm_hintsObject



482
483
484
485
486
487
488
# File 'lib/wmadd/x.rb', line 482

def wm_hints
  hints = X11::XGetWMHints(@display, @window)
  return nil if hints.null?
  WMHints.new(**X11::XWMHints.new(hints).to_h)
ensure
  X11::XFree(hints) unless hints.null?
end

#wm_nameObject



496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/wmadd/x.rb', line 496

def wm_name
  prop = X11::XTextProperty.malloc(Fiddle::RUBY_FREE)
  text_list = X11::Pointer.malloc(Fiddle::RUBY_FREE)
  X11::XGetWMName(@display, @window, prop)
  X11::Xutf8TextPropertyToTextList(@display, prop, text_list, ' '*8)
  ptr = text_list.ptr
  if ptr.null?
    nil
  else
    ptr.ptr.to_s.force_encoding('utf-8')
  end
end

#wm_normal_hintsObject



490
491
492
493
494
# File 'lib/wmadd/x.rb', line 490

def wm_normal_hints
  hints = X11::XSizeHints.malloc(Fiddle::RUBY_FREE)
  ret = X11::XGetWMNormalHints(@display, @window, hints, ' '*8)
  ret == 0 ? nil : SizeHints.new(**hints.to_h)
end