Class: X11::Form::BaseForm
Direct Known Subclasses
ChangeGC, ChangeProperty, ChangeSaveSet, ChangeWindowAttributes, ClearArea, ClientHandshake, ConfigureWindow, CopyArea, CreateColormap, CreateGC, CreatePixmap, CreateWindow, DepthInfo, DestroyWindow, DirectFormat, DisplayInfo, Error, Event, FormatInfo, FreePixmap, Geometry, GetAtomName, GetGeometry, GetKeyboardMapping, GetProperty, GetWindowAttributes, GlyphElt32, GlyphInfo, GrabButton, GrabKey, ImageText16, ImageText8, InternAtom, ListFonts, ListFontsReply, MapWindow, OpenFont, PictDepth, PictFormInfo, PictScreen, PictVisual, PolyFillRectangle, Property, PutImage, QueryExtension, QueryPointer, QueryTree, QueryTreeReply, Rectangle, ReparentWindow, Reply, ScreenInfo, SendEvent, Str, UnmapWindow, VisualInfo, WindowAttributes, XRenderAddGlyphs, XRenderColor, XRenderCompositeGlyphs32, XRenderCreateGlyphSet, XRenderCreatePicture, XRenderCreateSolidFill, XRenderFillRectangles, XRenderFreePicture, XRenderQueryPictFormats, XRenderQueryVersion
Defined Under Namespace
Classes: Field
Constant Summary
Constants included from Type
Type::BackingStore, Type::BitGravity, Type::Bitmask, Type::Colormap, Type::Colornum, Type::Cursor, Type::Drawable, Type::Font, Type::Fontable, Type::Gcontext, Type::KeyCode, Type::Keysym, Type::Mask, Type::Pixmap, Type::Signifigance, Type::Timestamp, Type::VisualID, Type::WinGravity, Type::Window
Class Method Summary collapse
- .field(name, type_klass, type = nil, value: nil) ⇒ Object
- .fields ⇒ Object
-
.from_packet(socket) ⇒ Object
FIXME: Doing small reads from socket is a bad idea, and the protocol provides length fields that makes it unnecessary.
- .structs ⇒ Object
- .unused(size) ⇒ Object
Instance Method Summary collapse
-
#initialize(*params) ⇒ BaseForm
constructor
initialize field accessors.
- #to_packet(dpy) ⇒ Object
Constructor Details
#initialize(*params) ⇒ BaseForm
initialize field accessors
37 38 39 40 41 42 43 44 45 |
# File 'lib/X11/form.rb', line 37 def initialize(*params) self.class.fields.each do |f| if !f.value param = params.shift #p [f,param] instance_variable_set("@#{f.name}", param) end end end |
Class Method Details
.field(name, type_klass, type = nil, value: nil) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/X11/form.rb', line 156 def field(name, type_klass, type = nil, value: nil) # name, type_klass, type = args class_eval do if value && value.respond_to?(:call) define_method(name.to_sym) { value.call(self) } else attr_accessor name end end s = Field.new s.name = name s.type = (type == nil ? :field : type) s.type_klass = type_klass s.value = value @structs ||= [] @structs << s end |
.fields ⇒ Object
181 182 183 |
# File 'lib/X11/form.rb', line 181 def fields super+Array(@structs).dup.delete_if{|s| s.type == :unused or s.type == :length or s.type == :format_length} end |
.from_packet(socket) ⇒ Object
FIXME: Doing small reads from socket is a bad idea, and the protocol provides length fields that makes it unnecessary.
102 103 104 105 106 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 151 152 |
# File 'lib/X11/form.rb', line 102 def from_packet(socket) # fetch class level instance variable holding defined fields form = new lengths = {} structs.each do |s| case s.type when :field val = if s.type_klass.superclass == BaseForm s.type_klass.from_packet(socket) else s.type_klass.unpack( socket.read(s.type_klass.size) ) end form.instance_variable_set("@#{s.name}", val) when :unused sz = s.size.respond_to?(:call) ? s.size.call(self) : s.size socket.read(sz) when :length size = s.type_klass.unpack( socket.read(s.type_klass.size) ) lengths[s.name] = size when :format_length size = s.type_klass.unpack( socket.read(s.type_klass.size) ) lengths[s.name] = case form.format when 8 then size when 16 then size*2 when 32 then size*4 else 0 end when :string len = lengths[s.name] val = s.type_klass.unpack(socket, len) form.instance_variable_set("@#{s.name}", val) when :list len = lengths[s.name] if len val = len.times.collect do s.type_klass.from_packet(socket) end else val = [] while ob = s.type_klass.from_packet(socket) val << ob end end form.instance_variable_set("@#{s.name}", val) end end return form end |
.structs ⇒ Object
96 97 98 |
# File 'lib/X11/form.rb', line 96 def structs superclass.structs + Array(@structs) #instance_variable_get("@structs")) end |
Instance Method Details
#to_packet(dpy) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/X11/form.rb', line 47 def to_packet(dpy) # fetch class level instance variable holding defined fields structs = self.class.structs packet = structs.map do |s| # fetch value of field set in initialization value = s.type == :unused ? nil : instance_variable_get("@#{s.name}") case s.type when :field if s.value if s.value.respond_to?(:call) value = s.value.call(self) else value = s.value end end #p [s,value] if value.is_a?(BaseForm) v = value.to_packet(dpy) else #p [s,value] v = s.type_klass.pack(value, dpy) end #p v v when :unused sz = s.size.respond_to?(:call) ? s.size.call(self) : s.size "\x00" * sz when :length, :format_length #p [s,value] #p [value.size] s.type_klass.pack(value.size, dpy) when :string s.type_klass.pack(value, dpy) when :list Array(value).collect do |obj| if obj.is_a?(BaseForm) obj.to_packet(dpy) else s.type_klass.pack(obj, dpy) end end end end.join end |