mirror of
https://github.com/vidarh/ruby-x11
synced 2024-12-27 09:59:21 +01:00
This example is still awful and messy, but it's better than what was there... Needs chunky_png and skrift gem's installed
This commit is contained in:
parent
2d820a5600
commit
1af9524748
1 changed files with 140 additions and 66 deletions
202
example/test.rb
202
example/test.rb
|
@ -1,4 +1,5 @@
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
|
require 'skrift'
|
||||||
require 'bundler'
|
require 'bundler'
|
||||||
require 'chunky_png'
|
require 'chunky_png'
|
||||||
Bundler.setup(:default, :development)
|
Bundler.setup(:default, :development)
|
||||||
|
@ -8,95 +9,164 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
||||||
|
|
||||||
require 'X11'
|
require 'X11'
|
||||||
|
|
||||||
display = X11::Display.new
|
|
||||||
screen = display.screens.first
|
dpy = display = X11::Display.new
|
||||||
|
screen = dpy.screens.first
|
||||||
root = screen.root
|
root = screen.root
|
||||||
wid = display.new_id
|
wid = display.new_id
|
||||||
display.write_request(X11::Form::CreateWindow.new(
|
|
||||||
screen.root_depth,
|
dpy.create_window(
|
||||||
wid,
|
screen.root_depth, wid, root,
|
||||||
root,
|
0, 0, # x,y
|
||||||
0, #x
|
1000, 600, # w,h
|
||||||
0, #y
|
|
||||||
1000,#w
|
|
||||||
600,#h
|
|
||||||
0,
|
0,
|
||||||
X11::Form::InputOutput,
|
X11::Form::InputOutput,
|
||||||
X11::Form::CopyFromParent,
|
X11::Form::CopyFromParent,
|
||||||
X11::Form::CWBackPixel |
|
X11::Form::CWBackPixel |
|
||||||
X11::Form::CWEventMask,
|
X11::Form::CWEventMask,
|
||||||
[0xff8844, # RGB background
|
[0x0, # RGB background
|
||||||
X11::Form::SubstructureNotifyMask |
|
X11::Form::SubstructureNotifyMask |
|
||||||
# X11::Form::StructureNotifyMask | ## Move
|
X11::Form::StructureNotifyMask | ## Move
|
||||||
X11::Form::ExposureMask |
|
X11::Form::ExposureMask |
|
||||||
X11::Form::KeyPressMask |
|
X11::Form::KeyPressMask |
|
||||||
X11::Form::ButtonPressMask
|
X11::Form::ButtonPressMask
|
||||||
]
|
]
|
||||||
))
|
)
|
||||||
|
|
||||||
|
def set_window_opacity(dpy, wid, opacity)
|
||||||
|
dpy.change_property(
|
||||||
|
X11::Form::Replace,
|
||||||
|
wid, dpy.atom(:_NET_WM_WINDOW_OPACITY),
|
||||||
|
X11::Form::CardinalAtom, 32,
|
||||||
|
[(0xffffffff * opacity).to_i].pack("N").split(//).map(&:ord)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
set_window_opacity(dpy, wid, 0.8)
|
||||||
|
#p dpy.display_info
|
||||||
|
|
||||||
|
#reply = dpy.query_extension("XKEYBOARD")
|
||||||
|
|
||||||
|
$kmap = nil
|
||||||
|
def update_keymap(dpy)
|
||||||
|
reply = dpy.get_keyboard_mapping
|
||||||
|
|
||||||
|
|
||||||
|
if reply
|
||||||
|
$kmap = reply.keysyms.map do |c|
|
||||||
|
if c == 0
|
||||||
|
nil
|
||||||
|
elsif X11::KeySyms[c]
|
||||||
|
X11::KeySyms[c]
|
||||||
|
elsif c < 0x100
|
||||||
|
c.chr(Encoding::ISO_8859_1)
|
||||||
|
elsif c.between?(0xffb0, 0xffb9)
|
||||||
|
"KP_#{c-0xffb0}".to_sym
|
||||||
|
elsif c.between?(0xffbe, 0xffe0)
|
||||||
|
"F#{c-0xffbe+1}".to_sym
|
||||||
|
elsif c.between?(0xff08, 0xffff)
|
||||||
|
# FIIXME:
|
||||||
|
raise "keyboard_#{c.to_s(16)}".to_s
|
||||||
|
elsif c.between?(0x01000100, 0x0110FFFF)
|
||||||
|
(c-0x01000100).
|
||||||
|
chr(Encoding::UTF_32) rescue c.to_s(16)
|
||||||
|
else
|
||||||
|
raise "unknown_#{c.to_s(16)}"
|
||||||
|
end
|
||||||
|
end.each_slice(reply.keysyms_per_keycode).to_a
|
||||||
|
#ks = ks.map {|s| s.compact.sort_by{|x| x.to_s}.uniq }.to_a # This is for testing/ease of reading only
|
||||||
|
p $kmap[47-dpy.display_info.min_keycode]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def lookup_keysym(dpy, event)
|
||||||
|
update_keymap(dpy) if !$kmap
|
||||||
|
p $kmap[event.detail-dpy.display_info.min_keycode]
|
||||||
|
end
|
||||||
|
|
||||||
puts "Mapping"
|
puts "Mapping"
|
||||||
display.write_request(X11::Form::MapWindow.new(wid))
|
dpy.map_window(wid)
|
||||||
# Creating GC
|
|
||||||
gc = display.new_id
|
|
||||||
display.write_request(X11::Form::CreateGC.new(
|
|
||||||
gc, screen.root,
|
|
||||||
X11::Form::ForegroundMask,
|
|
||||||
[0xff0000, # RGB foreground
|
|
||||||
]
|
|
||||||
))
|
|
||||||
|
|
||||||
$gc2 = display.new_id
|
$gc = gc = dpy.create_gc(wid, foreground: 0xff0000)
|
||||||
display.write_request(X11::Form::CreateGC.new(
|
$gc2 = dpy.create_gc(wid,foreground: 0xffffff, background: 0x444444)
|
||||||
$gc2,
|
$gc3 = dpy.create_gc(wid)
|
||||||
screen.root,
|
|
||||||
X11::Form::ForegroundMask|X11::Form::BackgroundMask,
|
|
||||||
[0xffffff, # RGB foreground
|
|
||||||
0x444444,
|
|
||||||
]
|
|
||||||
))
|
|
||||||
|
|
||||||
$gc3 = display.new_id
|
|
||||||
display.write_request(X11::Form::CreateGC.new(
|
|
||||||
$gc3,
|
|
||||||
screen.root,
|
|
||||||
0, []
|
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
puts "Main loop"
|
puts "Main loop"
|
||||||
p gc
|
|
||||||
|
|
||||||
# This will wait for a reply
|
#p dpy.list_fonts(10, "*7x13*").names.map(&:to_s)
|
||||||
p display.write_sync(X11::Form::ListFonts.new(10, "*7x13*"),
|
|
||||||
X11::Form::ListFontsReply).names.map(&:to_s)
|
|
||||||
|
|
||||||
fid = display.new_id
|
fid = dpy.new_id
|
||||||
display.write_request(X11::Form::OpenFont.new(fid, "7x13"))
|
dpy.open_font(fid, "-misc-fixed-bold-r-normal--13-120-75-75-c-70-iso10646-1")
|
||||||
display.write_request(X11::Form::ChangeGC.new($gc2, X11::Form::FontMask, [fid]))
|
#"-bitstream-courier 10 pitch-medium-r-normal--0-0-0-0-m-0-iso10646-1")
|
||||||
|
dpy.change_gc($gc2, X11::Form::FontMask, [fid])
|
||||||
|
|
||||||
$png = ChunkyPNG::Image.from_file('genie.png')
|
$png = ChunkyPNG::Image.from_file('genie.png')
|
||||||
p $png.width
|
|
||||||
p $png.height
|
|
||||||
|
|
||||||
def redraw(display, wid, gc)
|
$data = ""
|
||||||
p [:redraw, gc]
|
|
||||||
display.write_request(X11::Form::PolyFillRectangle.new(
|
|
||||||
wid, gc,
|
|
||||||
[X11::Form::Rectangle.new(20,20, 60, 80)]
|
|
||||||
))
|
|
||||||
|
|
||||||
display.write_request(X11::Form::ClearArea.new( false, wid, 30, 30, 5, 5))
|
|
||||||
display.write_request(X11::Form::ImageText8.new(wid, $gc2, 30, 70, "Hello World"))
|
|
||||||
|
|
||||||
depth = 24
|
|
||||||
# FIXME: The colors are wrong
|
|
||||||
# pixels.pack("N*")
|
|
||||||
data = ""
|
|
||||||
$png.pixels.each do |px|
|
$png.pixels.each do |px|
|
||||||
str = [px].pack("N")
|
str = [px].pack("N")
|
||||||
data << str[2] << str[1] << str[0] << str[3]
|
if str[3] == "\x00"
|
||||||
|
$data << "\0\0\0\0".force_encoding("ASCII-8BIT")
|
||||||
|
else
|
||||||
|
$data << str[2] << str[1] << str[0] << str[3]
|
||||||
end
|
end
|
||||||
display.write_request(X11::Form::PutImage.new(
|
end
|
||||||
X11::Form::ZPixmap, wid, $gc2, $png.width, $png.height, 80, 80, 0, depth, data))
|
|
||||||
|
#$f = Font.load("/usr/share/fonts/truetype/tlwg/Umpush-BoldOblique.ttf")
|
||||||
|
$f = Font.load("/usr/share/fonts/truetype/tlwg/Garuda.ttf")
|
||||||
|
#$f = Font.load("resources/FiraGO-Regular.ttf")
|
||||||
|
|
||||||
|
$sft = SFT.new($f)
|
||||||
|
$sft.x_scale = 15
|
||||||
|
$sft.y_scale = 15
|
||||||
|
$glyphcache = {}
|
||||||
|
def render_glyph(display, wid, x,y, ch)
|
||||||
|
gid = $sft.lookup(ch.ord)
|
||||||
|
mtx = $sft.gmetrics(gid)
|
||||||
|
data = $glyphcache[gid]
|
||||||
|
if !data
|
||||||
|
img = Image.new(mtx.min_width, mtx.min_height) #(mtx .min_width + 3) & ~3, mtx.min_height)
|
||||||
|
if !$sft.render(gid, img)
|
||||||
|
raise "Unable to render #{gid}\n"
|
||||||
|
end
|
||||||
|
# p img
|
||||||
|
data = img.pixels.map {|px|
|
||||||
|
"\0\0"+px.chr+"\0" #+ "\0\0\0"
|
||||||
|
}.join.force_encoding("ASCII-8BIT")
|
||||||
|
$glyphcache[gid] = data
|
||||||
|
end
|
||||||
|
depth = 24
|
||||||
|
# p data
|
||||||
|
#p img
|
||||||
|
# p ch
|
||||||
|
display.put_image(
|
||||||
|
X11::Form::ZPixmap, wid, $gc2,
|
||||||
|
mtx.min_width,mtx.min_height,
|
||||||
|
x, y - mtx.y_offset, 0, depth, data
|
||||||
|
)
|
||||||
|
mtx.advance_width
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_str(display, wid, x,y, str)
|
||||||
|
str.each_byte do |ch|
|
||||||
|
off = render_glyph(display, wid, x, y, ch.chr)
|
||||||
|
x+= off
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def redraw(dpy, wid, gc)
|
||||||
|
dpy.poly_fill_rectangle(wid, gc, [X11::Form::Rectangle.new(20,20, 60, 80)])
|
||||||
|
dpy.clear_area(false, wid, 30, 30, 5, 5)
|
||||||
|
dpy.image_text16(wid, $gc2, 30, 70, "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ")
|
||||||
|
#"\u25f0\u25ef Hello World")
|
||||||
|
dpy.put_image(
|
||||||
|
X11::Form::ZPixmap, wid, $gc2,
|
||||||
|
$png.width, $png.height, 80, 120, 0, 24, $data
|
||||||
|
)
|
||||||
|
render_str(dpy, wid, 30,90, 'HelloWorld')
|
||||||
end
|
end
|
||||||
|
|
||||||
loop do
|
loop do
|
||||||
|
@ -104,5 +174,9 @@ loop do
|
||||||
if pkt
|
if pkt
|
||||||
p pkt
|
p pkt
|
||||||
redraw(display, wid, gc) if pkt.is_a?(X11::Form::Expose)
|
redraw(display, wid, gc) if pkt.is_a?(X11::Form::Expose)
|
||||||
|
|
||||||
|
if pkt.is_a?(X11::Form::KeyPress)
|
||||||
|
lookup_keysym(dpy,pkt)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue