ruby-x11/lib/X11/display.rb

62 lines
1.7 KiB
Ruby
Raw Normal View History

2011-08-01 06:51:22 +02:00
module X11
class DisplayError < X11Error; end
class ConnectionError < DisplayError; end
class AuthorizationError < ConnectionError; end
2011-08-01 06:51:22 +02:00
class Display
# Open a connection to the specified display (numbered from 0) on the specified host
def initialize(target = ENV['DISPLAY'])
target =~ /^([\w.-]*):(\d+)(?:.(\d+))?$/
host, display_id, screen_id = $1, $2, $3
2012-03-09 06:52:42 +01:00
family = nil
2011-08-01 06:51:22 +02:00
if host.empty?
@socket = UNIXSocket.new("/tmp/.X11-unix/X#{display_id}")
family = :Local
host = nil
else
@socket = TCPSocket.new(host,6000+display_id)
2012-03-09 06:52:42 +01:00
family = :Internet
2011-08-01 06:51:22 +02:00
end
authorize(host, family, display_id)
end
private
def authorize(host, family, display_id)
auth_info = Auth.new.get_by_hostname(host||"localhost", family, display_id)
auth_name, auth_data = auth_info.address, auth_info.auth_data
2012-03-09 06:52:42 +01:00
handshake = Packet::ClientHandshake.create(
2011-08-01 06:51:22 +02:00
Protocol::BYTE_ORDER,
Protocol::MAJOR,
Protocol::MINOR,
auth_name.length,
auth_data.length,
auth_name,
auth_data
)
@socket.write(handshake)
2011-08-01 06:51:22 +02:00
case @socket.read(1).unpack("w").first
when X11::Auth::FAILED
len, major, minor, xlen = @socket.read(7).unpack("CSSS")
reason = @socket.read(xlen * 4)
reason = reason[0..len]
raise AuthorizationError, "Connection to server failed -- (version #{major}.#{minor}) #{reason}"
2011-08-01 06:51:22 +02:00
when X11::Auth::AUTHENTICATE
raise AuthorizationError, "Connection requires authentication"
2011-08-01 06:51:22 +02:00
when X11::Auth::SUCCESS
puts "CONNECTION SUCCESS"
2011-08-01 06:51:22 +02:00
else
raise AuthorizationError, "Received unknown opcode #{type}"
2011-08-01 06:51:22 +02:00
end
2011-08-01 06:51:22 +02:00
end
end
end