Class: X11::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/X11/display.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target = ENV['DISPLAY']) ⇒ Display

Open a connection to the specified display (numbered from 0) on the specified host



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/X11/display.rb', line 21

def initialize(target = ENV['DISPLAY'])
  # With no DISPLAY set (nil or empty), fall back to the conventional
  # default display ":0" rather than letting the regex below leave host
  # nil and crash on host.empty?.
  target = ":0" if target.nil? || target.empty?
  target =~ /^([\w.-]*):(\d+)(?:.(\d+))?$/
  host, display_id, _screen_id = $1, $2, $3
  family = nil

  @debug = ENV["PUREX_DEBUG"].to_s.strip == "true"
  
  if host.empty?
    @socket = UNIXSocket.new("/tmp/.X11-unix/X#{display_id}")
    family = :Local
    host = nil
  else
    @socket = TCPSocket.new(host,6000+display_id)
    family = :Internet
  end

  authorize(host, family, display_id)

  @requestseq = 1
  @rqueue = Queue.new       # Read but not returned events
  @wqueue = Queue.new
  @extensions = {}  # Known extensions
  @atoms = {}       # Interned atoms

  start_io
end

Instance Attribute Details

#socketObject

Returns the value of attribute socket.



18
19
20
# File 'lib/X11/display.rb', line 18

def socket
  @socket
end

Instance Method Details

#atom(name) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/X11/display.rb', line 327

def atom(name)
  return name if name.is_a?(Integer) # Allow atom(atom_integer_or_symbol)
  begin
    return Form::Atoms.const_get(name.to_sym) if Form::Atoms.const_defined?(name.to_sym)
  rescue
    # const_defined? will throw if name isn't a valid constant name, but
    # that's fine
  end
  name = name.to_sym
  intern_atom(false, name) if !@atoms[name]
  @atoms[name]
end

#atom_enum(val) ⇒ Object



460
461
462
# File 'lib/X11/display.rb', line 460

def atom_enum(val)
  open_enum(val, {cardinal: Form::CardinalAtom, atom: Form::AtomAtom, window: Form::WindowAtom}) || atom(val)
end

#change_gcObject



445
# File 'lib/X11/display.rb', line 445

def change_gc(...)      = write_request(Form::ChangeGC.new(...))

#change_property(mode, window, property, type, format, data) ⇒ Object



434
435
436
437
438
439
440
441
# File 'lib/X11/display.rb', line 434

def change_property(mode, window, property, type, format, data)
  property = atom(property.to_sym) if property.is_a?(Symbol) || property.is_a?(String)
  window_id = window.is_a?(X11::Window) ? window.wid : window

  mode = open_enum(mode, {replace: 0, prepend: 1, append: 2})
  type = atom_enum(type)
  write_request(Form::ChangeProperty.new(mode, window_id, property, type, format, data))
end

#change_save_setObject



446
# File 'lib/X11/display.rb', line 446

def change_save_set(...)= write_request(Form::ChangeSaveSet.new(...))

#change_window_attributes(wid, values: {}) ⇒ Object



317
318
319
320
321
322
323
# File 'lib/X11/display.rb', line 317

def change_window_attributes(wid,
  values: {})
  values = values.sort_by{_1[0]}
  mask =   values.inject(0) {|acc,v| (acc | v[0]) }
  values = values.map{_1[1]}
  write_request(Form::ChangeWindowAttributes.new(wid, mask, values))
end

#clear_area(*args) ⇒ Object



589
# File 'lib/X11/display.rb', line 589

def clear_area(*args)  = write_request(X11::Form::ClearArea.new(*args))

#client_message(window: default_root, type: :ClientMessage, format: 32, destination: default_root, mask: 0, data: [], propagate: true) ⇒ Object



573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/X11/display.rb', line 573

def client_message(window: default_root, type: :ClientMessage, format: 32, destination: default_root, mask: 0, data: [], propagate: true)
  f = {8 => "C20", 16 => "S10", 32 => "L5"}[format]
  # p f
  data = (Array(data).map{|item|atom(item)} + [0]*20).pack(f)
  event = Form::ClientMessage.new(
    format, 0, window, atom(type), data
  )
  event.code =33
  pp event
    
  send_event(propagate, destination, mask, event)
end

#closeObject



210
# File 'lib/X11/display.rb', line 210

def close  = @rqueue.close

#configure_window(window, x: nil, y: nil, width: nil, height: nil, border_width: nil, sibling: nil, stack_mode: nil) ⇒ Object



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
# File 'lib/X11/display.rb', line 527

def configure_window(window, x: nil, y: nil, width: nil, height: nil,
  border_width: nil, sibling: nil, stack_mode: nil)

  values = []
  mask  = 0
  mask |= set_value(values, 0x001, x)
  mask |= set_value(values, 0x002, y)
  mask |= set_value(values, 0x004, width)
  mask |= set_value(values, 0x008, height)
  mask |= set_value(values, 0x010, border_width)
  mask |= set_value(values, 0x020, sibling)

  if stack_mode
    mask |= 0x040
    values << case stack_mode
              when :above then 0
              when :below then 1
              when :top_if then 2
              when :bottom_if then 3
              when :opposite then 4
              else raise "Unknown stack_mode #{stack_mode.inspect}"
              end
  end
  write_request(X11::Form::ConfigureWindow.new(window, mask, values))
end

#copy_area(*args) ⇒ Object



590
# File 'lib/X11/display.rb', line 590

def copy_area(*args)   = write_request(X11::Form::CopyArea.new(*args))

#create_colormap(alloc, window, visual) ⇒ Object



404
405
406
407
408
# File 'lib/X11/display.rb', line 404

def create_colormap(alloc, window, visual)
  mid = new_id
  write_request(Form::CreateColormap.new(alloc, mid, window, visual))
  mid
end

#create_gc(window, foreground: nil, background: nil, graphics_exposures: nil) ⇒ Object



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/X11/display.rb', line 554

def create_gc(window, foreground: nil, background: nil,
  graphics_exposures: nil
)
  mask = 0
  args = []

  # FIXME:
  # The rest can be found here:
  # https://tronche.com/gui/x/xlib/GC/manipulating.html#XGCValues
  mask |= set_value(args, 0x04, foreground)
  mask |= set_value(args, 0x08, background)
  mask |= set_value(args, 0x10000, graphics_exposures)
  
  gc = new_id
  write_request(X11::Form::CreateGC.new(gc, window, mask, args))
  gc
end

#create_pixmap(depth, drawable, w, h) ⇒ Object



598
599
600
# File 'lib/X11/display.rb', line 598

def create_pixmap(depth, drawable, w,h)
  new_id.tap{|pid| write_request(Form::CreatePixmap.new(depth, pid, drawable, w,h)) }
end

#create_window(x, y, w, h, values: {}, depth: 32, parent: nil, border_width: 0, wclass: X11::Form::InputOutput, visual: nil) ⇒ Object

Requests



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/X11/display.rb', line 289

def create_window(x,y,w,h,
  values: {},
  depth: 32, parent: nil, border_width: 0, wclass: X11::Form::InputOutput, visual: nil
)
  wid = new_id
  parent ||= default_root

  if visual.nil?
    visual = find_visual(0, depth).visual_id
  end

  values[X11::Form::CWColorMap] ||= create_colormap(0, parent, visual)

  values = values.sort_by{_1[0]}
  mask =   values.inject(0) {|acc,v| (acc | v[0]) }
  values = values.map{_1[1]}
  write_request(
    X11::Form::CreateWindow.new(
      depth, wid, parent,
      x,y,w,h,border_width, wclass, visual, mask, values)
  )
  return wid
end

#default_rootObject



286
# File 'lib/X11/display.rb', line 286

def default_root = screens.first.root

#destroy_window(window) ⇒ Object



362
# File 'lib/X11/display.rb', line 362

def destroy_window(window) = write_request(Form::DestroyWindow.new(window))

#display_infoObject



62
63
64
# File 'lib/X11/display.rb', line 62

def display_info
  @internal
end

#event_handler=(block) ⇒ Object



52
53
54
# File 'lib/X11/display.rb', line 52

def event_handler= block
  @event_handler= block
end

#find_visual(screen, depth, qlass = 4) ⇒ Object



281
282
283
284
# File 'lib/X11/display.rb', line 281

def find_visual(screen, depth, qlass = 4)
  self.display_info.screens[screen].depths.find{|d|
    d.depth == depth }.visuals.find{|v| v.qlass = qlass }
end

#flushObject



56
57
58
59
60
# File 'lib/X11/display.rb', line 56

def flush
  while !@wqueue.empty?
    sleep(0.01)
  end
end

#free_pixmap(pixmap) ⇒ Object



602
603
604
# File 'lib/X11/display.rb', line 602

def free_pixmap(pixmap)
  write_request(Form::FreePixmap.new(pixmap))
end

#get_atom_name(atom) ⇒ Object



361
# File 'lib/X11/display.rb', line 361

def get_atom_name(atom)    = write_sync(Form::GetAtomName.new(atom), Form::AtomName)&.name

#get_geometry(drawable) ⇒ Object



363
# File 'lib/X11/display.rb', line 363

def get_geometry(drawable) = write_sync(Form::GetGeometry.new(drawable), Form::Geometry)

#get_input_focusObject



755
756
757
# File 'lib/X11/display.rb', line 755

def get_input_focus
  write_sync(Form::GetInputFocus.new, Form::GetInputFocusReply)
end

#get_keyboard_mapping(min_keycode = display_info.min_keycode, count = display_info.max_keycode - min_keycode) ⇒ Object



400
401
402
# File 'lib/X11/display.rb', line 400

def get_keyboard_mapping(min_keycode=display_info.min_keycode, count= display_info.max_keycode - min_keycode)
  write_sync(Form::GetKeyboardMapping.new(min_keycode, count), Form::GetKeyboardMappingReply)
end

#get_property(window, property, type, offset: 0, length: 4, delete: false) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/X11/display.rb', line 410

def get_property(window, property, type, offset: 0, length: 4, delete: false)
  property = atom(property)
  type     = atom_enum(type)
  window_id = window.is_a?(X11::Window) ? window.wid : window
  
  result = write_sync(Form::GetProperty.new(
    delete, window_id, property, type, offset, length
  ), Form::Property)

  if result && result.format != 0
    case result.format
    when 16
      result.value = result.value.unpack("v*")
      result.value = result.value.first if result.value.length == 1
    when 32
      result.value = result.value.unpack("V*")
      result.value = result.value.first if result.value.length == 1
    end
  elsif result
    result.value = nil
  end
  result
end

#get_selection_owner(selection) ⇒ Object

Get the current owner of a selection selection: the selection atom Returns: the window ID of the owner, or None (0) if there is no owner



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/X11/display.rb', line 384

def get_selection_owner(selection)
  # Convert selection to atom ID if necessary
  selection = atom(selection) if selection.is_a?(Symbol) || selection.is_a?(String)
  
  # Use the form-based approach for reading
  req = Form::GetSelectionOwner.new(selection)
  
  begin
    reply = write_sync(req, Form::SelectionOwner)
    reply ? reply.owner : 0
  rescue => e
    STDERR.puts "Error getting selection owner: #{e.message}" if @debug
    0  # Return 0 (None) on error
  end
end

#get_window_attributes(wid) ⇒ Object



313
314
315
# File 'lib/X11/display.rb', line 313

def get_window_attributes(wid)
  write_sync( Form::GetWindowAttributes.new(wid), Form::WindowAttributes )
end

#grab_button(owner_events, grab_window, event_mask, pointer_mode, keyboard_mode, confine_to, cursor, button, modifiers) ⇒ Object



492
493
494
495
496
497
498
499
500
# File 'lib/X11/display.rb', line 492

def grab_button(owner_events, grab_window, event_mask, pointer_mode,
  keyboard_mode, confine_to, cursor, button, modifiers)
  write_request(Form::GrabButton.new(
    owner_events, grab_window, event_mask,
    pointer_mode == :async ? 1 : 0,
    keyboard_mode == :async ? 1 : 0,
    confine_to.to_i, cursor.to_i, button, modifiers)
  )
end

#grab_key(owner_events, grab_window, modifiers, keycode, pointer_mode, keyboard_mode) ⇒ Object



481
482
483
484
485
486
487
488
489
490
# File 'lib/X11/display.rb', line 481

def grab_key(owner_events, grab_window, modifiers, keycode, pointer_mode, keyboard_mode)
  write_request(Form::GrabKey.new(
    owner_events,
    grab_window,
    modifiers,
    keycode,
    pointer_mode  == :async ? 1 : 0,
    keyboard_mode == :async ? 1 : 0
  ))
end

#grab_keyboard(grab_window, owner_events: true, pointer_mode: :async, keyboard_mode: :async, time: 0) ⇒ Object

Actively grab the keyboard for grab_window. Returns the reply (status 0 = Success). Modes default to async so key events flow normally to the focus.



504
505
506
507
508
509
510
511
512
# File 'lib/X11/display.rb', line 504

def grab_keyboard(grab_window, owner_events: true, pointer_mode: :async,
  keyboard_mode: :async, time: 0)
  write_sync(Form::GrabKeyboard.new(
    owner_events ? 1 : 0,
    grab_window, time,
    pointer_mode  == :async ? 1 : 0,
    keyboard_mode == :async ? 1 : 0
  ), Form::GrabKeyboardReply)
end

#image_text16(*args) ⇒ Object



592
# File 'lib/X11/display.rb', line 592

def image_text16(*args)= write_request(X11::Form::ImageText16.new(*args))

#image_text8(*args) ⇒ Object



591
# File 'lib/X11/display.rb', line 591

def image_text8(*args) = write_request(X11::Form::ImageText8.new(*args))

#intern_atom(flag, name) ⇒ Object



354
355
356
357
358
359
# File 'lib/X11/display.rb', line 354

def intern_atom(flag, name)
  reply = write_sync(Form::InternAtom.new(flag, name.to_s),Form::InternAtomReply)
  if reply
    @atoms[name.to_sym] = reply.atom
  end
end

#list_fontsObject



443
# File 'lib/X11/display.rb', line 443

def list_fonts(...)     = write_sync(Form::ListFonts.new(...), Form::ListFontsReply)

#major_opcode(name) ⇒ Object



346
347
348
349
350
351
352
# File 'lib/X11/display.rb', line 346

def major_opcode(name)
  if !@extensions[name]
    query_extension(name)
  end
  raise "No such extension '#{name}'" if !@extensions[name]
  @extensions[name][:major]
end

#map_windowObject



454
# File 'lib/X11/display.rb', line 454

def map_window(...)   = write_request(Form::MapWindow.new(...))

#new_idObject

The resource-id-mask contains a single contiguous set of bits (at least 18). The client allocates resource IDs for types WINDOW, PIXMAP, CURSOR, FONT, GCONTEXT, and COLORMAP by choosing a value with only some subset of these bits set and ORing it with resource-id-base.



78
79
80
81
82
83
# File 'lib/X11/display.rb', line 78

def new_id
  id = (@xid_next ||= 0)
  @xid_next += 1

  (id & @internal.resource_id_mask) | @internal.resource_id_base
end

#next_packetObject



208
# File 'lib/X11/display.rb', line 208

def next_packet = @rqueue.shift

#open_enum(val, map) ⇒ Object



469
# File 'lib/X11/display.rb', line 469

def open_enum(val, map) = (map[val].nil? ? val : map[val])

#open_fontObject



444
# File 'lib/X11/display.rb', line 444

def open_font(...)      = write_request(Form::OpenFont.new(...))

#peek_packetObject



207
# File 'lib/X11/display.rb', line 207

def peek_packet = !@rqueue.empty?

#poly_fill_rectangle(wid, gc, *rects) ⇒ Object



593
594
595
596
# File 'lib/X11/display.rb', line 593

def poly_fill_rectangle(wid, gc, *rects)
  rects = rects.map{|r| r.is_a?(Array) ? Form::Rectangle.new(*r) : r}
  write_request(X11::Form::PolyFillRectangle.new(wid, gc, rects))
end

#put_image(*args) ⇒ Object



588
# File 'lib/X11/display.rb', line 588

def put_image(*args)   = write_request(X11::Form::PutImage.new(*args))

#query_extension(name) ⇒ Object



340
341
342
343
344
# File 'lib/X11/display.rb', line 340

def query_extension(name)
  r = write_sync(Form::QueryExtension.new(name), Form::QueryExtensionReply)
  @extensions[name] = { major: r.major_opcode }
  r
end

#query_pointer(window) ⇒ Object



751
752
753
# File 'lib/X11/display.rb', line 751

def query_pointer(window)
  write_sync(Form::QueryPointer.new(window), Form::QueryPointerReply)
end

#query_treeObject



586
# File 'lib/X11/display.rb', line 586

def query_tree(...) = write_sync(X11::Form::QueryTree.new(...), X11::Form::QueryTreeReply)

#read_error(data) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/X11/display.rb', line 85

def read_error data
  error = Form::Error.from_packet(StringIO.new(data))
  # FIXME: Maybe make this configurable, as it means potentially
  # keeping along really heavy requests. or alternative purge them
  # more aggressively also when there are no errors, as otherwise
  # the growth might be unbounded
  error.request = @requests[error.sequence_number]
  @requests.keys.find_all{|s| s <= error.sequence_number}.each do |s|
    @requests.delete(s)
  end
  STDERR.puts "ERROR: #{error.inspect}"
  error
end

#read_event(type, data, event_class) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/X11/display.rb', line 107

def read_event type, data, event_class
  io = StringIO.new(data)
  case type
  # 0 is error, not handled here
  # 1 is reply, not handled here
  when 2  then return Form::KeyPress.from_packet(io)
  when 3  then return Form::KeyRelease.from_packet(io)
  when 4  then return Form::ButtonPress.from_packet(io)
  when 5  then return Form::ButtonRelease.from_packet(io)
  when 6  then return Form::MotionNotify.from_packet(io)
  when 7  then return Form::EnterNotify.from_packet(io)
  when 8  then return Form::LeaveNotify.from_packet(io)
  when 9  then return Form::FocusIn.from_packet(io)
  when 10 then return Form::FocusOut.from_packet(io)
  # FIXME 11: KeymapNotify
  when 12 then return Form::Expose.from_packet(io)
  # FIXME 13: GraphicsExposure
  when 14 then return Form::NoExposure.from_packet(io)
  # FIXME: 15: VisibilityNotify
  when 16 then return Form::CreateNotify.from_packet(io)
  when 17 then return Form::DestroyNotify.from_packet(io)
  when 18 then return Form::UnmapNotify.from_packet(io)
  when 19 then return Form::MapNotify.from_packet(io)
  when 20 then return Form::MapRequest.from_packet(io)
  when 21 then return Form::ReparentNotify.from_packet(io)
  when 22 then return Form::ConfigureNotify.from_packet(io)
  when 23 then return Form::ConfigureRequest.from_packet(io)
  # FIXME: 24: GravityNotify
  # FIXME: 25: ResizeRequest
  # FIXME: 26: CirculateNotify
  # FIXME: 27: CirculateRequest
  when 28 then return Form::PropertyNotify.from_packet(io)
  # FIXME: 29: SelectionClear
  # FIXME: 30: SelectionRequest
  # FIXME: 31: SelectionNotify
  # FIXME: 32: ColormapNotify
  when 33 then return Form::ClientMessage.from_packet(io)
  # FIXME: 34: MappingNotify
  else
    STDERR.puts "FIXME: Event: #{type}"
    STDERR.puts "EVENT: #{data.inspect}"
    data
  end
end

#read_full_packet(len = 32) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/X11/display.rb', line 152

def read_full_packet(len = 32)
  data = @socket.read(32)
  return nil if data.nil?
  while data.length < 32
    IO.select([@socket],nil,nil,0.001)
    data.concat(@socket.read_nonblock(32 - data.length))
  end
  return data
end

#read_packetObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/X11/display.rb', line 162

def read_packet
  data = read_full_packet(32)
  return nil if data.nil?

  # FIXME: Make it configurable.
  @requests.keys.find_all{|s| s <= @requestseq - 50}.each do |s|
    @requests.delete(s)
  end

  # FIXME: What is bit 8 for? Synthentic?
  type = data.unpack("C").first & 0x7f
  case type
  when 0 then read_error(data)
  when 1 then read_reply(data)
  when 2..34 then read_event(type, data, nil)
  else
    raise ProtocolError, "Unsupported reply type: #{type} #{data.inspect}"
  end
end

#read_reply(data) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/X11/display.rb', line 99

def read_reply data
  len = data.unpack("@4L")[0]
  extra = len > 0 ? @socket.read(len*4) : ""
  #STDERR.puts "REPLY: #{data.inspect}"
  #STDERR.puts "EXTRA: #{extra.inspect}"
  data + extra
end

#render_add_glyphs(glyphset, glyphids, glyphinfos, data) ⇒ Object



684
685
686
687
# File 'lib/X11/display.rb', line 684

def render_add_glyphs(glyphset, glyphids, glyphinfos, data)
  write_request(X11::Form::XRenderAddGlyphs.new(render_opcode,
    glyphset, Array(glyphids), Array(glyphinfos), data))
end

#render_composite_glyphs32(op, src, dst, fmt, glyphset, srcx, srcy, *elts) ⇒ Object



695
696
697
698
699
700
701
702
703
# File 'lib/X11/display.rb', line 695

def render_composite_glyphs32(op, src, dst, fmt, glyphset, srcx,srcy, *elts)
  write_request(X11::Form::XRenderCompositeGlyphs32.new(
    render_opcode,
    op, src, dst, fmt,
    glyphset,
    srcx, srcy,
    elts.map {|e| e.is_a?(Array) ? Form::GlyphElt32.new(*e) : e }
  ))
end

#render_create_glyph_set(format) ⇒ Object



677
678
679
680
681
682
# File 'lib/X11/display.rb', line 677

def render_create_glyph_set(format)
  glyphset = new_id
  write_request(X11::Form::XRenderCreateGlyphSet.new(
    major_opcode("RENDER"),glyphset, format))
  glyphset
end

#render_create_picture(drawable, format, vmask = 0, vlist = []) ⇒ Object



620
621
622
623
624
625
# File 'lib/X11/display.rb', line 620

def render_create_picture(drawable, format, vmask=0, vlist=[])
  pid = new_id
  write_request(X11::Form::XRenderCreatePicture.new(
     render_opcode, pid, drawable, format, vmask, vlist))
  pid
end

#render_create_solid_fill(*color) ⇒ Object



705
706
707
708
709
710
711
712
713
714
# File 'lib/X11/display.rb', line 705

def render_create_solid_fill(*color)
  if color.length == 1 && color.is_a?(Form::XRenderColor)
    color = color[0]
  else
    color = Form::XRenderColor.new(*color)
  end
  fill = new_id
  write_request(Form::XRenderCreateSolidFill.new(render_opcode,fill,color))
  fill
end

#render_fill_rectangles(op, dst, color, rects) ⇒ Object



689
690
691
692
693
# File 'lib/X11/display.rb', line 689

def render_fill_rectangles(op, dst, color, rects)
  color = Form::XRenderColor.new(*color) if color.is_a?(Array)
  rects = rects.map{|r| r.is_a?(Array) ? Form::Rectangle.new(*r) : r}
  write_request(Form::XRenderFillRectangles.new(render_opcode, op, dst, color, rects))
end

#render_find_standard_format(sym) ⇒ Object



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
# File 'lib/X11/display.rb', line 643

def render_find_standard_format(sym)
  # A pox be on the people who made this necessary

  formats = render_query_pict_formats

  case sym
  when :a8
    @a8 ||= formats.formats.find do |f|
      f.type  == 1 &&
      f.depth == 8 &&
      f.direct.alpha_mask == 255
    end
  when :rgb24
    @rgb24 ||= formats.formats.find do |f|
      f.type         == 1  &&
      f.depth        == 24 &&
      f.direct.red   == 16 &&
      f.direct.green == 8  &&
      f.direct.blue  == 0
    end
  when :argb24
    @argb24 ||= formats.formats.find do |f|
      f.type         == 1  &&
      f.depth        == 32 &&
      f.direct.alpha == 24 &&
      f.direct.red   == 16 &&
      f.direct.green == 8  &&
      f.direct.blue  == 0
    end
  else
    raise "Unsupported format (a4/a1 by omission)"
  end
end

#render_find_visual_format(visual) ⇒ Object



634
635
636
637
638
639
640
641
# File 'lib/X11/display.rb', line 634

def render_find_visual_format(visual)
  # FIXME.
  render_query_pict_formats.screens.map do |s|
    s.depths.map do |d|
      d.visuals.map {|v| v.visual == visual ? v : nil }
    end
  end.flatten.compact.first&.format
end

#render_free_picture(picture) ⇒ Object



716
717
718
# File 'lib/X11/display.rb', line 716

def render_free_picture(picture)
  write_request(Form::XRenderFreePicture.new(render_opcode, picture))
end

#render_opcodeObject

XRender



608
609
610
611
612
613
614
615
616
617
618
# File 'lib/X11/display.rb', line 608

def render_opcode
  return @render_opcode if @render_opcode
  @render_opcode = major_opcode("RENDER")
  if @render_opcode
    @render_version = write_sync(X11::Form::XRenderQueryVersion.new(
      @render_opcode,0,11),
      X11::Form::XRenderQueryVersionReply
    )
  end
  @render_opcode
end

#render_query_pict_formatsObject



627
628
629
630
631
632
# File 'lib/X11/display.rb', line 627

def render_query_pict_formats
  @render_formats ||= write_sync(
    Form::XRenderQueryPictFormats.new(render_opcode),
    Form::XRenderQueryPictFormatsReply
  )
end

#reparent_window(window, parent, x, y, save: true) ⇒ Object



448
449
450
451
452
# File 'lib/X11/display.rb', line 448

def reparent_window(window, parent, x, y, save: true)
  # You so almost always want this that it should've been a single request
  change_save_set(0, window) if save
  write_request(Form::ReparentWindow.new(window, parent, x,y))
end

#runObject



273
274
275
276
277
278
279
# File 'lib/X11/display.rb', line 273

def run
  loop do
    pkt = next_packet
    return if !pkt
    yield(pkt)
  end
end

#screensObject



66
67
68
69
70
# File 'lib/X11/display.rb', line 66

def screens
  @internal.screens.map do |s|
    Screen.new(self, s)
  end
end

#select_input(w, events) ⇒ Object



325
# File 'lib/X11/display.rb', line 325

def select_input(w, events) = change_window_attributes(w, values: {Form::CWEventMask => events})

#send_eventObject



572
# File 'lib/X11/display.rb', line 572

def send_event(...) = write_request(Form::SendEvent.new(...))

#set_input_focus(revert_to, focus, time = :now) ⇒ Object



471
472
473
474
475
476
477
478
479
# File 'lib/X11/display.rb', line 471

def set_input_focus(revert_to, focus, time=:now)
  # FIXME: This is an experiment.
  # Upside: Simpler. Downside: Doesn't work server-side.
  # Probably a bad idea.
  revert_to = open_enum(revert_to, {none: 0, pointer_root: 1, parent: 2})
  focus     = open_enum(focus,     {none: 0, pointer_root: 1 })
  time      = open_enum(time,      {current_time: 0, now: 0})
  write_packet(u8(42,revert_to), u16(3), window(focus), u32(time))
end

#set_selection_owner(selection, owner, time = 0) ⇒ Object

Set the owner of a selection selection: the selection atom owner: the window ID of the new owner, or None (0) to indicate no owner time: the server time when ownership should take effect, or CurrentTime (0)



369
370
371
372
373
374
375
376
377
378
379
# File 'lib/X11/display.rb', line 369

def set_selection_owner(selection, owner, time = 0)
  # Convert selection to atom ID if necessary
  selection = atom(selection) if selection.is_a?(Symbol) || selection.is_a?(String)
  owner = owner || 0  # Allow nil for owner to mean None (0)
  
  # Create and send the SetSelectionOwner request using the Form
  req = Form::SetSelectionOwner.new(owner, selection, time)
  write_request(req)
  
  true # Always returns true; check get_selection_owner to verify
end

#set_value(values, mask, x) ⇒ Object



518
519
520
521
522
523
524
525
# File 'lib/X11/display.rb', line 518

def set_value(values, mask, x)
  if x
    values << x
    mask
  else
    0
  end
end

#start_ioObject



212
213
214
215
216
217
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/X11/display.rb', line 212

def start_io
  @replies ||= {}
  @requests ||= {}
  # Read thread.
  # FIXME: Drop the select.
  rt = Thread.new do
    while pkt = read_packet
      #STDERR.puts "read: #{pkt.inspect}"
      if !pkt
        sleep 0.1
      elsif pkt.is_a?(String)
        # This is a reply. We need the sequence number.
        #
        seq = pkt.unpack1("@2S")
        STDERR.puts "  - seq= #{seq}" if @debug
        STDERR.puts @replies.inspect if @debug
        if @replies[seq]
          q = @replies.delete(seq)
          STDERR.puts "  - reply to #{q}" if @debug
          q << pkt
        end
      elsif pkt.is_a?(X11::Form::Error)
        if @replies[pkt.sequence_number]
          q = @replies.delete(pkt.sequence_number)
          q << pkt
        else
          @rqueue << pkt
        end
      else
        @rqueue << pkt
      end
    end
    @rqueue.close
    @replies.values.each(&:close)
  end

  # Write thread
  wt = Thread.new do
    while msg = @wqueue.shift
      ob, q, data = *msg
      @requests[@requestseq] = ob
      @replies[@requestseq] = q if q
      @requestseq = (@requestseq + 1) % 65536
      @socket.write(data)
    end
  end

  at_exit do
    flush
    @rqueue.close
    @wqueue.close
    # We kill this because it may be stuck in a read
    # we'll never care about
    Thread.kill(rt)
    
    # We wait for this to finish because otherwise we may
    # lose side-effects
    wt.join
  end
end

#u16(*args) ⇒ Object



458
# File 'lib/X11/display.rb', line 458

def u16(*args) = args.pack("v*")

#u32(*args) ⇒ Object



459
# File 'lib/X11/display.rb', line 459

def u32(*args) = args.pack("V*")

#u8(*args) ⇒ Object



457
# File 'lib/X11/display.rb', line 457

def u8(*args)  = args.pack("c*")

#ungrab_keyboard(time = 0) ⇒ Object



514
515
516
# File 'lib/X11/display.rb', line 514

def ungrab_keyboard(time = 0)
  write_request(Form::UngrabKeyboard.new(time))
end

#unmap_windowObject



455
# File 'lib/X11/display.rb', line 455

def unmap_window(...) = write_request(Form::UnmapWindow.new(...))

#window(*args) ⇒ Object



464
465
466
467
# File 'lib/X11/display.rb', line 464

def window(*args)
  args.each {|a| raise "Window expected" if a.nil? }
  u32(*args)
end

#write_packet(*args) ⇒ Object



182
183
184
185
186
# File 'lib/X11/display.rb', line 182

def write_packet(*args)
  pkt = args.join
  pkt[2..3] = u16(pkt.length/4)
  @wqueue << [nil,nil,pkt]
end

#write_request(ob) ⇒ Object



188
189
190
191
192
193
# File 'lib/X11/display.rb', line 188

def write_request ob
  data = ob.to_packet(self) if ob.respond_to?(:to_packet)
  raise "BAD LENGTH for #{ob.inspect} (#{ob.request_length.to_i*4} ! #{data.size} " if ob.request_length && ob.request_length.to_i*4 != data.size
  STDERR.puts "write_req: #{ob.inspect}" if @debug
  @wqueue << [ob,nil,data]
end

#write_sync(ob, reply = nil) ⇒ Object

Raises:



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/X11/display.rb', line 195

def write_sync(ob, reply=nil)
  data = ob.to_packet(self) if ob.respond_to?(:to_packet)
  q = Queue.new
  @wqueue << [ob,q,data]
  STDERR.puts "write_sync_req: #{ob.inspect}" if @debug
  pkt = q.shift
  STDERR.puts "write_sync_rep: #{pkt.inspect}" if @debug
  raise(X11::Error.new(pkt)) if pkt.is_a?(X11::Form::Error)
  return pkt if !pkt.is_a?(String)
  reply ? reply.from_packet(StringIO.new(pkt)) : pkt
end

#xinerama_is_activeObject



736
737
738
739
740
741
742
# File 'lib/X11/display.rb', line 736

def xinerama_is_active
  result = write_sync(
    X11::Form::XineramaIsActive.new(xinerama_opcode),
    X11::Form::XineramaIsActiveReply
  )
  result.state != 0
end

#xinerama_opcodeObject

Xinerama extension



722
723
724
725
726
# File 'lib/X11/display.rb', line 722

def xinerama_opcode
  return @xinerama_opcode if @xinerama_opcode
  @xinerama_opcode = major_opcode("XINERAMA")
  @xinerama_opcode
end

#xinerama_query_screensObject



744
745
746
747
748
749
# File 'lib/X11/display.rb', line 744

def xinerama_query_screens
  write_sync(
    X11::Form::XineramaQueryScreens.new(xinerama_opcode),
    X11::Form::XineramaQueryScreensReply
  )
end

#xinerama_query_versionObject



728
729
730
731
732
733
734
# File 'lib/X11/display.rb', line 728

def xinerama_query_version
  return @xinerama_version if @xinerama_version
  @xinerama_version = write_sync(
    X11::Form::XineramaQueryVersion.new(xinerama_opcode),
    X11::Form::XineramaQueryVersionReply
  )
end