xlib-objects 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 618e5374b429ba0d48824d30e13d755e4664408e
4
- data.tar.gz: 915a8a62603ae84bab9a479f6298a38a3417f3bd
3
+ metadata.gz: 22f4f88902d97b0d31c2610f90bfa14b2378571a
4
+ data.tar.gz: 0139801c5a443d34350d5ac3aeed21d87d58d3e4
5
5
  SHA512:
6
- metadata.gz: 25ee4211bff0abea0ae06c1e9d19501e21128580207afc41cfa049d7d1e5b79a9b261202025eed789746634c9266ff97f9c761aef65aeb7c94868f065d172740
7
- data.tar.gz: 02387a2ad113840131cf87501f433746e10e4eb9759e11c87953ce6df0f83fb4f3f8afcb2d665782c352f2f1c66bdc813555e4659e112eb77794fe474b4bafb7
6
+ metadata.gz: a409bdbcfbd75f89e9fb0fe4ac8a3b2c72790d9dbb857a0a7f38dec73efe4b00309291b8005bfb31e30efa58e865ade4c4dd035b0262312631f8365906a8471e
7
+ data.tar.gz: fcdabd70932efe480f6a8a18e98da2544de3bac50b52311ee8a706c35142df89082519d376da418c0e12af9d75e0733bbdea724c80be4caba26fb42a0fa522ed
data/lib/display.rb CHANGED
@@ -41,10 +41,20 @@ module XlibObj
41
41
  (0..@struct[:nscreens]-1).map{ |number| screen(number) }
42
42
  end
43
43
 
44
+ def focused_window
45
+ screens.reduce(nil){ |focused_window, s| focused_window or s.focused_window }
46
+ end
47
+
44
48
  def handle_events
45
49
  handle_event(next_event) while pending_events > 0
46
50
  end
47
51
 
52
+ def clipboard(type = :CLIPBOARD, &on_receive)
53
+ @internal_window ||= screen.root_window.create_window
54
+ @internal_window.request_selection(type: type, &on_receive)
55
+ self
56
+ end
57
+
48
58
  def on_error(&callback)
49
59
  @error_handler = if callback
50
60
  FFI::Function.new(:pointer, [:pointer, :pointer]) do |display_ptr, error_ptr|
@@ -85,7 +95,7 @@ module XlibObj
85
95
  end
86
96
 
87
97
  def handle_event(event)
88
- handling_window_id = event.event || event.parent || event.window
98
+ handling_window_id = event.event || event.parent || event.window || event.requestor
89
99
  handling_window = Window.new(self, handling_window_id)
90
100
  handling_window.handle(event)
91
101
  end
@@ -94,7 +104,7 @@ module XlibObj
94
104
  @struct[:screens] + number*Xlib::Screen.size
95
105
  end
96
106
 
97
- def screen(number)
107
+ def screen(number = 0)
98
108
  Screen.new(self, screen_pointer(number))
99
109
  end
100
110
  end
data/lib/error.rb CHANGED
@@ -72,7 +72,7 @@ module XlibObj
72
72
  if [Xlib::BadWindow, Xlib::BadPixmap, Xlib::BadCursor, Xlib::BadFont, Xlib::BadDrawable,
73
73
  Xlib::BadColor, Xlib::BadGC, Xlib::BadIDChoice].include?(code)
74
74
  Xlib.XGetErrorDatabaseText(@display.to_native, @display.name, "ResourceID",
75
- "ResourceID 0x%x", message, message_size)
75
+ "ResourceID %x", message, message_size)
76
76
  elsif code == Xlib::BadValue
77
77
  Xlib.XGetErrorDatabaseText(@display.to_native, @display.name, "Value", "Value 0x%x",
78
78
  message, message_size)
@@ -21,12 +21,12 @@ module XlibObj
21
21
  raise "The client message needs a type at least." unless @type
22
22
  @receiver = receiver
23
23
  Xlib.XSendEvent(@receiver.display.to_native, @receiver.to_native, false,
24
- Xlib::SubstructureNotifyMask | Xlib::SubstructureRedirectMask,
25
- to_native)
24
+ Xlib::SubstructureNotifyMask | Xlib::SubstructureRedirectMask, to_native)
26
25
  Xlib.XFlush(@receiver.display.to_native)
27
26
  end
28
27
 
29
28
  private
29
+
30
30
  def to_native
31
31
  event = Xlib::XEvent.new
32
32
  event[:xclient][:type] = Xlib::ClientMessage
data/lib/screen.rb CHANGED
@@ -40,6 +40,21 @@ module XlibObj
40
40
  @root_window ||= Window.new(@display, Xlib.XRootWindowOfScreen(to_native))
41
41
  end
42
42
 
43
+ def focused_window
44
+ window_ptr = FFI::MemoryPointer.new :Window
45
+ trash_ptr = FFI::MemoryPointer.new :int
46
+ Xlib.XGetInputFocus(@display.to_native, window_ptr, trash_ptr)
47
+ window = window_ptr.read_int
48
+
49
+ if window == Xlib::PointerRoot
50
+ root_window
51
+ elsif window == Xlib::None
52
+ nil
53
+ else
54
+ Window.new(@display, window)
55
+ end
56
+ end
57
+
43
58
  def crtcs
44
59
  (0..resources[:ncrtc]-1).map do |crtc_number|
45
60
  crtc_id(resources[:crtcs], crtc_number)
@@ -49,6 +64,7 @@ module XlibObj
49
64
  end
50
65
 
51
66
  private
67
+
52
68
  def resources
53
69
  unless @resources
54
70
  resources_ptr = Xlib.XRRGetScreenResources(@display.to_native,
data/lib/window.rb CHANGED
@@ -20,8 +20,7 @@ module XlibObj
20
20
 
21
21
  def attribute(name)
22
22
  attributes = Xlib::WindowAttributes.new
23
- Xlib.XGetWindowAttributes(@display.to_native, @to_native, attributes.
24
- pointer)
23
+ Xlib.XGetWindowAttributes(@display.to_native, @to_native, attributes.pointer)
25
24
  attributes[name.to_sym]
26
25
  rescue
27
26
  nil
@@ -43,22 +42,25 @@ module XlibObj
43
42
  Property.new(self, name).set(value)
44
43
  end
45
44
 
45
+ def delete_property(name)
46
+ Property.new(self, name).delete
47
+ end
48
+
46
49
  def absolute_position
47
50
  x_abs = FFI::MemoryPointer.new :int
48
51
  y_abs = FFI::MemoryPointer.new :int
49
52
  child = FFI::MemoryPointer.new :Window
50
53
  root_win = screen.root_window
51
54
 
52
- Xlib.XTranslateCoordinates(@display.to_native, @to_native,
53
- root_win.to_native, 0, 0, x_abs, y_abs, child)
55
+ Xlib.XTranslateCoordinates(@display.to_native, @to_native, root_win.to_native, 0, 0, x_abs,
56
+ y_abs, child)
54
57
 
55
58
  { x: x_abs.read_int, y: y_abs.read_int }
56
59
  end
57
60
 
58
61
  # Commands
59
62
  def move_resize(x, y, width, height)
60
- Xlib.XMoveResizeWindow(@display.to_native, @to_native, x, y, width,
61
- height)
63
+ Xlib.XMoveResizeWindow(@display.to_native, @to_native, x, y, width, height)
62
64
  Xlib.XFlush(@display.to_native)
63
65
  self
64
66
  end
@@ -87,6 +89,12 @@ module XlibObj
87
89
  self
88
90
  end
89
91
 
92
+ def focus
93
+ Xlib.XSetInputFocus(@display.to_native, @to_native, Xlib::RevertToParent, Xlib::CurrentTime)
94
+ Xlib.XFlush(@display.to_native)
95
+ self
96
+ end
97
+
90
98
  def on(mask, type, &callback)
91
99
  @event_handler.on(mask, type, &callback)
92
100
  end
@@ -103,6 +111,43 @@ module XlibObj
103
111
 
104
112
  def send_to_itself(type, data = nil, subject = nil)
105
113
  Event::ClientMessage.new(type, data, subject).send_to(self)
114
+ self
115
+ end
116
+
117
+ def request_selection(type: :PRIMARY, format: :UTF8_STRING, property: :XSEL_DATA, &on_receive)
118
+ # will only receive the selection notify event, if the window has been created by the process
119
+ # running this very code
120
+ selection_handler = on(:no_event, :selection_notify) do |event|
121
+ next if Atom.new(@display, event.selection).name != type
122
+
123
+ if event.property == Xlib::None
124
+ selection = nil
125
+ else
126
+ selection = property(event.property)
127
+ delete_property(event.property)
128
+ end
129
+ on_receive.call(selection, type)
130
+ off(:no_event, :selection_notify, selection_handler)
131
+ end
132
+
133
+ type_atom = Atom.new(@display, type)
134
+ property_atom = Atom.new(@display, property)
135
+ format_atom = Atom.new(@display, format)
136
+ Xlib.XConvertSelection(@display.to_native, type_atom.to_native, format_atom.to_native,
137
+ property_atom.to_native, @to_native, Xlib::CurrentTime)
138
+ Xlib.XFlush(@display.to_native)
139
+ self
140
+ end
141
+
142
+ def create_window
143
+ black = Xlib.XBlackPixel(@display.to_native, 0)
144
+ win_id = Xlib.XCreateSimpleWindow(@display.to_native, to_native, 0, 0, 1, 1, 0, black, black)
145
+ XlibObj::Window.new(@display, win_id)
146
+ end
147
+
148
+ def destroy
149
+ @event_handler.destroy
150
+ Xlib.XDestroyWindow(@display.to_native, to_native)
106
151
  end
107
152
  end
108
153
  end
@@ -15,6 +15,10 @@ module XlibObj
15
15
  @handlers[display] ||= {}
16
16
  @handlers[display][window_id] ||= new(display, window_id)
17
17
  end
18
+
19
+ def remove(display, window_id)
20
+ @handlers[display].delete(window_id) if @handlers and @handlers[display]
21
+ end
18
22
  end
19
23
 
20
24
  def initialize(display, window_id)
@@ -46,6 +50,10 @@ module XlibObj
46
50
  end
47
51
  end
48
52
 
53
+ def destroy
54
+ self.class.remove(@display, @window_id)
55
+ end
56
+
49
57
  private
50
58
  def add_event_mask(mask)
51
59
  check_mask(mask)
@@ -91,7 +99,7 @@ module XlibObj
91
99
  end
92
100
 
93
101
  def check_mask(mask)
94
- if normalize_mask(mask).zero? && normalize_rr_mask(mask).zero?
102
+ if XlibObj::Event::MASK[mask].nil? && XlibObj::Event::RR_MASK[mask].nil?
95
103
  raise("Unknown event mask #{mask}.")
96
104
  end
97
105
  end
@@ -73,6 +73,10 @@ module XlibObj
73
73
  self
74
74
  end
75
75
 
76
+ def delete
77
+ Xlib.XDeleteProperty(@window.display.to_native, @window.to_native, @atom.to_native)
78
+ end
79
+
76
80
  private
77
81
  def read_bytes(pointer, width, count)
78
82
  pointer.read_pointer.read_string(count*native_width(width))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xlib-objects
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Aue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-13 00:00:00.000000000 Z
11
+ date: 2015-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xlib