Added X11:Window class

This commit is contained in:
Vidar Hokstad 2024-01-13 17:04:10 +00:00
parent ce8bf9c38c
commit 44f312554e
3 changed files with 64 additions and 23 deletions

View file

@ -14,7 +14,7 @@ dpy = display = X11::Display.new
screen = dpy.screens.first screen = dpy.screens.first
root = screen.root root = screen.root
wid = dpy.create_window( window = X11::Window.create(dpy,
0, 0, # x,y 0, 0, # x,y
1000, 600, # w,h 1000, 600, # w,h
# FIXME: WTH isn't depth: 32 working here? # FIXME: WTH isn't depth: 32 working here?
@ -32,17 +32,18 @@ wid = dpy.create_window(
#dpy.next_packet #dpy.next_packet
#exit(0) #exit(0)
def set_window_opacity(dpy, wid, opacity)
dpy.change_property( def set_window_opacity(window, opacity)
window.change_property(
:replace, :replace,
wid, "_NET_WM_WINDOW_OPACITY", "_NET_WM_WINDOW_OPACITY",
:cardinal, 32, :cardinal, 32,
[(0xffffffff * opacity).to_i].pack("V").unpack("C*") [(0xffffffff * opacity).to_i].pack("V").unpack("C*")
) )
end end
set_window_opacity(dpy, wid, 0.8) set_window_opacity(window, 0.8)
#p dpy.display_info #p dpy.display_info
@ -87,11 +88,11 @@ def lookup_keysym(dpy, event)
end end
puts "Mapping" puts "Mapping"
dpy.map_window(wid) window.map
$gc = gc = dpy.create_gc(wid, foreground: 0xff0000) $gc = gc = window.create_gc(foreground: 0xff0000)
$gc2 = dpy.create_gc(wid,foreground: 0xffffff, background: 0x444444) $gc2 = window.create_gc(foreground: 0xffffff, background: 0x444444)
$gc3 = dpy.create_gc(wid) $gc3 = window.create_gc
puts "Main loop" puts "Main loop"
@ -123,7 +124,7 @@ $sft = SFT.new($f)
$sft.x_scale = 15 $sft.x_scale = 15
$sft.y_scale = 15 $sft.y_scale = 15
$glyphcache = {} $glyphcache = {}
def render_glyph(display, wid, x,y, ch) def render_glyph(window, x,y, ch)
gid = $sft.lookup(ch.ord) gid = $sft.lookup(ch.ord)
mtx = $sft.gmetrics(gid) mtx = $sft.gmetrics(gid)
data = $glyphcache[gid] data = $glyphcache[gid]
@ -135,45 +136,45 @@ def render_glyph(display, wid, x,y, ch)
# p img # p img
data = img.pixels.map {|px| data = img.pixels.map {|px|
"\0\0"+px.chr+"\0" #+ "\0\0\0" "\0\0"+px.chr+"\0" #+ "\0\0\0"
}.join.force_encoding("ASCII-8BIT") }.join.b
$glyphcache[gid] = data $glyphcache[gid] = data
end end
depth = 24 depth = 24
# p data # p data
#p img #p img
# p ch # p ch
display.put_image( window.put_image(
:ZPixmap, wid, $gc2, :ZPixmap, $gc2,
mtx.min_width,mtx.min_height, mtx.min_width,mtx.min_height,
x, y - mtx.y_offset, 0, depth, data x, y - mtx.y_offset, 0, depth, data
) )
mtx.advance_width mtx.advance_width
end end
def render_str(display, wid, x,y, str) def render_str(window, x,y, str)
str.each_byte do |ch| str.each_byte do |ch|
off = render_glyph(display, wid, x, y, ch.chr) off = render_glyph(window, x, y, ch.chr)
x+= off x+= off
end end
end end
def redraw(dpy, wid, gc) def redraw(window, gc)
dpy.poly_fill_rectangle(wid, gc, [20,20, 60, 80]) window.poly_fill_rectangle(gc, [20,20, 60, 80])
dpy.clear_area(false, wid, 30, 30, 5, 5) window.clear_area(false, 30, 30, 5, 5)
dpy.image_text16(wid, $gc2, 30, 70, "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ") window.image_text16($gc2, 30, 70, "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ")
#"\u25f0\u25ef Hello World") #"\u25f0\u25ef Hello World")
dpy.put_image( window.put_image(
:ZPixmap, wid, $gc2, :ZPixmap, $gc2,
$png.width, $png.height, 80, 120, 0, 24, $data $png.width, $png.height, 80, 120, 0, 24, $data
) )
render_str(dpy, wid, 30,90, 'HelloWorld') render_str(window, 30,90, 'HelloWorld')
end end
loop do loop do
pkt = display.next_packet pkt = display.next_packet
if pkt if pkt
p pkt p pkt
redraw(display, wid, gc) if pkt.is_a?(X11::Form::Expose) redraw(window, gc) if pkt.is_a?(X11::Form::Expose)
if pkt.is_a?(X11::Form::KeyPress) if pkt.is_a?(X11::Form::KeyPress)
lookup_keysym(dpy,pkt) lookup_keysym(dpy,pkt)

View file

@ -9,4 +9,5 @@ require_relative './X11/display'
require_relative './X11/screen' require_relative './X11/screen'
require_relative './X11/type' require_relative './X11/type'
require_relative './X11/form' require_relative './X11/form'
require_relative './X11/window'
require_relative './X11/keysyms' require_relative './X11/keysyms'

39
lib/X11/window.rb Normal file
View file

@ -0,0 +1,39 @@
module X11
# A simple helper class that makes requests where the
# target object is a specific window a bit more convenient
class Window
attr_reader :dpy, :wid
def initialize(dpy,wid)
@dpy, @wid = dpy,wid
end
def self.create(dpy, ...)
wid = dpy.create_window(...)
Window.new(dpy,wid)
end
def query_tree = dpy.query_tree(@wid)
def map = dpy.map_window(@wid)
def unmap = dpy.unmap_window(@wid)
def destroy = dpy.destroy_window(@wid)
def get_geometry = dpy.get_geometry(@wid)
def configure(...) = dpy.configure_window(@wid, ...)
def get_property(...) = dpy.get_property(@wid,...)
def grab_key(arg, ...) = dpy.grab_key(arg, @wid, ...)
def grab_button(arg,...) = dpy.grab_button(arg, @wid, ...)
def change_property(mode, ...) = dpy.change_property(mode, @wid, ...)
def set_input_focus(mode) = dpy.set_input_focus(mode, @wid)
def select_input(...) = dpy.select_input(@wid,...)
def get_window_attributes(...) = dpy.get_window_attributes(@wid,...)
def change_attributes(...) = dpy.change_window_attributes(@wid,...)
def image_text16(...) = dpy.image_text16(@wid, ...)
def clear_area(arg, ...) = dpy.clear_area(arg, @wid, ...)
def poly_fill_rectangle(...) = dpy.poly_fill_rectangle(@wid, ...)
def put_image(type, ...) = dpy.put_image(type, @wid, ...)
def create_gc(...) = dpy.create_gc(@wid, ...)
end
end