Class: RBGL::GUI::X11::RequestEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/rbgl/gui/x11/request_encoder.rb

Constant Summary collapse

EVENT_MASKS =
{
  exposure: 0x8000,
  key_press: 0x0001,
  key_release: 0x0002,
  button_press: 0x0004,
  button_release: 0x0008,
  pointer_motion: 0x0040,
  structure_notify: 0x020000
}.freeze
WINDOW_CLASSES =
{
  input_output: 1,
  input_only: 2
}.freeze
PROPERTY_MODES =
{
  replace: 0,
  prepend: 1,
  append: 2
}.freeze
IMAGE_FORMATS =
{
  bitmap: 0,
  xy_pixmap: 1,
  z_pixmap: 2
}.freeze

Instance Method Summary collapse

Instance Method Details

#change_property_data(window, property_atom, type_atom, data, mode: :replace, format: 8) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rbgl/gui/x11/request_encoder.rb', line 93

def change_property_data(window, property_atom, type_atom, data, mode: :replace, format: 8)
  data_bytes, value_count = pack_property_data(data, format)
  request = [
    window,
    property_atom,
    type_atom,
    format,
    value_count
  ].pack("VVVCV") + "\x00\x00\x00" + pad_to_4(data_bytes)

  [PROPERTY_MODES.fetch(mode, 0), request]
end

#create_gc_data(gc_id, drawable, values = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rbgl/gui/x11/request_encoder.rb', line 62

def create_gc_data(gc_id, drawable, values = {})
  mask = 0
  value_list = []

  if values[:foreground]
    mask |= 0x0004
    value_list << values[:foreground]
  end

  if values[:background]
    mask |= 0x0008
    value_list << values[:background]
  end

  [gc_id, drawable, mask].pack("VVV") + value_list.pack("V*")
end

#create_window_data(depth:, wid:, parent:, x:, y:, width:, height:, border_width:, window_class:, visual:, value_mask:, values:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rbgl/gui/x11/request_encoder.rb', line 34

def create_window_data(depth:, wid:, parent:, x:, y:, width:, height:,
                       border_width:, window_class:, visual:, value_mask:, values:)
  mask = 0
  value_list = []

  if value_mask.include?(:back_pixel)
    mask |= 0x0002
    value_list << values[:back_pixel]
  end

  if value_mask.include?(:event_mask)
    mask |= 0x0800
    value_list << encode_event_mask(values[:event_mask])
  end

  [
    depth,
    wid,
    parent,
    x, y,
    width, height,
    border_width,
    WINDOW_CLASSES.fetch(window_class, 0),
    visual,
    mask
  ].pack("CVVSSSSSVV") + value_list.pack("V*")
end

#intern_atom_data(name) ⇒ Object



106
107
108
# File 'lib/rbgl/gui/x11/request_encoder.rb', line 106

def intern_atom_data(name)
  [name.bytesize, 0].pack("vv") + pad_to_4(name)
end

#pack_property_data(data, format) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rbgl/gui/x11/request_encoder.rb', line 125

def pack_property_data(data, format)
  case format
  when 8
    bytes = data.to_s
    [bytes, bytes.bytesize]
  when 16
    values = Array(data)
    [values.pack("v*"), values.size]
  when 32
    values = Array(data)
    [values.pack("V*"), values.size]
  else
    raise ArgumentError, "Unsupported property format: #{format}"
  end
end

#pad_to_4(str) ⇒ Object



141
142
143
144
# File 'lib/rbgl/gui/x11/request_encoder.rb', line 141

def pad_to_4(str)
  padding = (4 - str.bytesize % 4) % 4
  str + ("\x00" * padding)
end

#put_image_data(format:, drawable:, gc:, width:, height:, dst_x:, dst_y:, depth:, data:) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rbgl/gui/x11/request_encoder.rb', line 79

def put_image_data(format:, drawable:, gc:, width:, height:, dst_x:, dst_y:, depth:, data:)
  format_byte = IMAGE_FORMATS.fetch(format, IMAGE_FORMATS[:z_pixmap])
  header = [
    drawable,
    gc,
    width, height,
    dst_x, dst_y,
    0,
    depth
  ].pack("VVvvvvCC") + "\x00\x00"

  [format_byte, header, data]
end

#request_packet(opcode, data, extra = 0) ⇒ Object



110
111
112
113
114
115
# File 'lib/rbgl/gui/x11/request_encoder.rb', line 110

def request_packet(opcode, data, extra = 0)
  length = (4 + data.bytesize + 3) / 4
  header = [opcode, extra, length].pack("CCv")
  padding = "\x00" * (length * 4 - 4 - data.bytesize)
  header + data + padding
end

#request_packet_with_data(opcode, extra, header_data, bulk_data) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/rbgl/gui/x11/request_encoder.rb', line 117

def request_packet_with_data(opcode, extra, header_data, bulk_data)
  total_data = header_data + bulk_data
  length = (4 + total_data.bytesize + 3) / 4
  header = [opcode, extra, length].pack("CCv")
  padding = "\x00" * (length * 4 - 4 - total_data.bytesize)
  header + total_data + padding
end