mouse 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.markdown CHANGED
@@ -1,10 +1,24 @@
1
+ # 0.1.0 - The basic API
2
+
3
+ * Fixed animation timing (sleep(3) type coercion issue)
4
+
5
+ * Added `Mouse#click_down`
6
+ * Added `Mouse#click_up`
7
+ * Added `Mouse#click`
8
+ * Added `Mouse#secondary_click`
9
+ * Added `Mouse#arbitrary_click`
10
+ * Added `Mouse#middle_click`
11
+ * Added `Mouse#multi_click`
12
+ * Added `Mouse#double_click`
13
+ * Added `Mouse#triple_click`
14
+
1
15
  # 0.0.1 - Initial Release
2
16
 
3
17
  * CRuby and MacRuby compatible
4
18
 
5
- * Added Mouse.current_position
6
- * Added Mouse.move_to
7
- * Added Mouse.drag_to
8
- * Added Mouse.scroll
9
- * Added CGPoint on CRuby to mimic CGPoint in MacRuby
10
- * Added Array#to_point to mimic MacRuby allowing Arrays for structs
19
+ * Added `Mouse.current_position`
20
+ * Added `Mouse.move_to`
21
+ * Added `Mouse.drag_to`
22
+ * Added `Mouse.scroll`
23
+ * Added `CGPoint` on CRuby to mimic `CGPoint` in MacRuby
24
+ * Added `Array#to_point` to mimic MacRuby allowing Arrays for structs
data/README.markdown CHANGED
@@ -4,7 +4,9 @@ A port of mouse.rb from [AXElements](http://github.com/Marketcircle/AXElements),
4
4
  but cleaned up and rewritten in C to be more portable across languages and
5
5
  runtimes.
6
6
 
7
- Not much to see right now.
7
+ ## Examples
8
+
9
+ TODO :P
8
10
 
9
11
  ## Copyright
10
12
 
@@ -34,5 +36,3 @@ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34
36
  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35
37
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36
38
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
-
38
-
data/ext/mouse/mouse.c CHANGED
@@ -41,8 +41,8 @@ rb_mouse_unwrap_point(VALUE point)
41
41
  #endif
42
42
  }
43
43
 
44
- /***
45
- * Returns the current co-ordinates of the Mouse
44
+ /*
45
+ * Returns the current co-ordinates of the mouse cursor
46
46
  *
47
47
  * @return [CGPoint]
48
48
  */
@@ -53,8 +53,8 @@ rb_mouse_current_position(VALUE self)
53
53
  return CURRENT_POSITION;
54
54
  }
55
55
 
56
- /***
57
- * Move the cursor to the given co-ordinates
56
+ /*
57
+ * Move the mouse cursor to the given co-ordinates
58
58
  *
59
59
  * @param point [CGPoint,Array(Number,Number),#to_point]
60
60
  * @return [CGPoint]
@@ -67,8 +67,8 @@ rb_mouse_move_to(VALUE self, VALUE point)
67
67
  return CURRENT_POSITION;
68
68
  }
69
69
 
70
- /***
71
- * Drag the cursor to the given co-ordinates
70
+ /*
71
+ * Drag the mouse cursor to the given co-ordinates
72
72
  *
73
73
  * @param point [CGPoint,Array(Number,Number),#to_point]
74
74
  * @return [CGPoint]
@@ -81,11 +81,13 @@ rb_mouse_drag_to(VALUE self, VALUE point)
81
81
  return CURRENT_POSITION;
82
82
  }
83
83
 
84
- /***
84
+ /*
85
85
  * Generate `amount` scroll events at the current cursor position
86
86
  *
87
+ * Returns number of lines scrolled.
88
+ *
87
89
  * @param amount [Number]
88
- * @return [CGPoint]
90
+ * @return [Number]
89
91
  */
90
92
  static
91
93
  VALUE
@@ -93,24 +95,198 @@ rb_mouse_scroll(VALUE self, VALUE amount)
93
95
  {
94
96
  amount = rb_funcall(amount, sel_to_i, 0);
95
97
  mouse_scroll(NUM2SIZET(amount));
98
+ return amount;
99
+ }
100
+
101
+ /*
102
+ * Generate the down click part of a click event at the current position
103
+ *
104
+ * This might be useful in concert with {Mouse.click_up} if you want
105
+ * to inject some behaviour between the down and up click events.
106
+ *
107
+ * @return [CGPoint]
108
+ */
109
+ static
110
+ VALUE
111
+ rb_mouse_click_down(VALUE self)
112
+ {
113
+ mouse_click_down();
114
+ return CURRENT_POSITION;
115
+ }
116
+
117
+ /*
118
+ * Generate the up click part of a click event at the current position
119
+ *
120
+ * This might be useful in concert with {Mouse.click_down} if you want
121
+ * to inject some behaviour between the down and up click events.
122
+ *
123
+ * @return [CGPoint]
124
+ */
125
+ static
126
+ VALUE
127
+ rb_mouse_click_up(VALUE self)
128
+ {
129
+ mouse_click_up();
130
+ return CURRENT_POSITION;
131
+ }
132
+
133
+ /*
134
+ * Generate a regular click event at the current mouse position
135
+ *
136
+ * @return [CGPoint]
137
+ */
138
+ static
139
+ VALUE
140
+ rb_mouse_click(VALUE self)
141
+ {
142
+ mouse_click();
143
+ return CURRENT_POSITION;
144
+ }
145
+
146
+ /*
147
+ * Generate a secondary click at the current mouse position
148
+ *
149
+ * Secondary click is often referred to as "right click".
150
+ *
151
+ * @return [CGPoint]
152
+ */
153
+ static
154
+ VALUE
155
+ rb_mouse_secondary_click(VALUE self)
156
+ {
157
+ mouse_secondary_click();
158
+ return CURRENT_POSITION;
159
+ }
160
+
161
+ /*
162
+ * Generate a click using an arbitrary mouse button at the current position
163
+ *
164
+ * Numbers are used to map the mouse buttons. At the time of writing,
165
+ * the documented values are:
166
+ *
167
+ * - `kCGMouseButtonLeft = 0`
168
+ * - `kCGMouseButtonRight = 1`
169
+ * - `kCGMouseButtonCenter = 2`
170
+ *
171
+ * And the rest are not documented! Though they should be easy enough
172
+ * to figure out. See the `CGMouseButton` enum in the reference
173
+ * documentation for the most up to date list.
174
+ *
175
+ * @param button [Number,#to_i]
176
+ * @return [CGPoint]
177
+ */
178
+ static
179
+ VALUE
180
+ rb_mouse_arbitrary_click(VALUE self, VALUE button)
181
+ {
182
+ button = rb_funcall(button, sel_to_i, 0);
183
+ mouse_arbitrary_click(NUM2UINT(button));
184
+ return CURRENT_POSITION;
185
+ }
186
+
187
+ /*
188
+ * Generate a click event for the middle mouse button at the current position
189
+ *
190
+ * It doesn't matter if you don't have a middle mouse button.
191
+ *
192
+ * @return [CGPoint]
193
+ */
194
+ static
195
+ VALUE
196
+ rb_mouse_middle_click(VALUE self)
197
+ {
198
+ mouse_middle_click();
199
+ return CURRENT_POSITION;
200
+ }
201
+
202
+ /*
203
+ * Generate a multi-click event at the current mouse position
204
+ *
205
+ * Unlike {Mouse.double_click} and {Mouse.triple_click} this will generate
206
+ * a single event with the given number of clicks.
207
+ *
208
+ * @param num_clicks [Number,#to_i]
209
+ * @return [CGPoint]
210
+ */
211
+ static
212
+ VALUE
213
+ rb_mouse_multi_click(VALUE self, VALUE num_clicks)
214
+ {
215
+ // TODO: there has got to be a more idiomatic way to do this coercion
216
+ num_clicks = rb_funcall(num_clicks, sel_to_i, 0);
217
+ mouse_multi_click(NUM2SIZET(num_clicks));
218
+ return CURRENT_POSITION;
219
+ }
220
+
221
+ /*
222
+ * Perform a double click at the given mouse position
223
+ *
224
+ * Implemented by first generating a single click, and then a double click.,
225
+ * Apps seem to respond more consistently to this behaviour since that is
226
+ * how a human would have to generate a double click event.
227
+ *
228
+ * @return [CGPoint]
229
+ */
230
+ static
231
+ VALUE
232
+ rb_mouse_double_click(VALUE self)
233
+ {
234
+ mouse_double_click();
235
+ return CURRENT_POSITION;
236
+ }
237
+
238
+ /*
239
+ * Perform a triple click at the given mouse position
240
+ *
241
+ * Implemented by first generating a single click, then a double click,
242
+ * and finally a triple click. Apps seem to respond more consistently to
243
+ * this behaviour since that is how a human would have to generate a
244
+ * triple click event.
245
+ *
246
+ * @return [CGPoint]
247
+ */
248
+ static
249
+ VALUE
250
+ rb_mouse_triple_click(VALUE self)
251
+ {
252
+ mouse_triple_click();
96
253
  return CURRENT_POSITION;
97
254
  }
98
255
 
99
256
  void
100
257
  Init_mouse()
101
258
  {
259
+ // on either supported Ruby, this should be defined by now
102
260
  rb_cCGPoint = rb_const_get(rb_cObject, rb_intern("CGPoint"));
103
- sel_x = rb_intern("x");
104
- sel_y = rb_intern("y");
261
+
262
+ sel_x = rb_intern("x");
263
+ sel_y = rb_intern("y");
105
264
  sel_to_point = rb_intern("to_point");
106
265
  sel_to_i = rb_intern("to_i");
107
- sel_new = rb_intern("new");
266
+ sel_new = rb_intern("new");
108
267
 
268
+ /*
269
+ * Document-module: Mouse
270
+ *
271
+ * Herp Derp TODO
272
+ *
273
+ * [Reference](http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html)
274
+ */
109
275
  rb_mMouse = rb_define_module("Mouse");
276
+
110
277
  rb_funcall(rb_mMouse, rb_intern("extend"), 1, rb_mMouse);
111
278
 
112
279
  rb_define_method(rb_mMouse, "current_position", rb_mouse_current_position, 0);
113
280
  rb_define_method(rb_mMouse, "move_to", rb_mouse_move_to, 1);
114
281
  rb_define_method(rb_mMouse, "drag_to", rb_mouse_drag_to, 1);
115
282
  rb_define_method(rb_mMouse, "scroll", rb_mouse_scroll, 1);
283
+ rb_define_method(rb_mMouse, "click_down", rb_mouse_click_down, 0);
284
+ rb_define_method(rb_mMouse, "click_up", rb_mouse_click_up, 0);
285
+ rb_define_method(rb_mMouse, "click", rb_mouse_click, 0);
286
+ rb_define_method(rb_mMouse, "secondary_click", rb_mouse_secondary_click, 0);
287
+ rb_define_method(rb_mMouse, "arbitrary_click", rb_mouse_arbitrary_click, 1);
288
+ rb_define_method(rb_mMouse, "middle_click", rb_mouse_middle_click, 0);
289
+ rb_define_method(rb_mMouse, "multi_click", rb_mouse_multi_click, 1);
290
+ rb_define_method(rb_mMouse, "double_click", rb_mouse_double_click, 0);
291
+ rb_define_method(rb_mMouse, "triple_click", rb_mouse_triple_click, 0);
116
292
  }
data/ext/mouse/mouser.c CHANGED
@@ -9,8 +9,8 @@
9
9
  #include <ApplicationServices/ApplicationServices.h>
10
10
  #include "mouser.h"
11
11
 
12
- static const double FPS = 120.0;
13
- static const double QUANTUM = 1 / 120; // Should be (1 / FPS), but gcc sucks
12
+ static const uint_t FPS = 120;
13
+ static const uint_t QUANTUM = 1000000 / 120; // should be FPS, but GCC sucks
14
14
  static const double DEFAULT_DURATION = 0.2; // seconds
15
15
 
16
16
  #define NEW_EVENT(type,point,button) CGEventCreateMouseEvent(nil,type,point,button)
@@ -35,9 +35,9 @@ static const double DEFAULT_DURATION = 0.2; // seconds
35
35
 
36
36
  static
37
37
  void
38
- mouse_sleep(size_t quanta)
38
+ mouse_sleep(uint_t quanta)
39
39
  {
40
- sleep(quanta * QUANTUM);
40
+ usleep(quanta * QUANTUM);
41
41
  }
42
42
 
43
43
  CGPoint
@@ -175,7 +175,7 @@ mouse_scroll(size_t amount)
175
175
 
176
176
 
177
177
  void
178
- mouse_click_down3(CGPoint point, size_t sleep_quanta)
178
+ mouse_click_down3(CGPoint point, uint_t sleep_quanta)
179
179
  {
180
180
  POSTRELEASE(NEW_EVENT(kCGEventLeftMouseDown, point, kCGMouseButtonLeft));
181
181
  mouse_sleep(sleep_quanta);
@@ -223,7 +223,7 @@ mouse_click()
223
223
 
224
224
 
225
225
  void
226
- mouse_secondary_click3(CGPoint point, size_t sleep_quanta)
226
+ mouse_secondary_click3(CGPoint point, uint_t sleep_quanta)
227
227
  {
228
228
  CGEventRef base_event = NEW_EVENT(
229
229
  kCGEventRightMouseDown,
@@ -252,7 +252,7 @@ mouse_secondary_click()
252
252
 
253
253
 
254
254
  void
255
- mouse_arbitrary_click3(CGEventMouseSubtype button, CGPoint point, size_t sleep_quanta)
255
+ mouse_arbitrary_click3(CGEventMouseSubtype button, CGPoint point, uint_t sleep_quanta)
256
256
  {
257
257
  CGEventRef base_event = NEW_EVENT(
258
258
  kCGEventOtherMouseDown,
data/ext/mouse/mouser.h CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
  #include <ApplicationServices/ApplicationServices.h>
10
10
 
11
+ typedef unsigned int uint_t;
12
+
11
13
  enum MouseMovementUnit {
12
14
  kMouseScrollByLine = kCGScrollEventUnitLine,
13
15
  // TODO: might not be real pixels, might be Cocoa co-ords, need to investigate
@@ -29,7 +31,7 @@ void mouse_scroll3(size_t amount, enum MouseMovementUnit units, double duration)
29
31
 
30
32
  void mouse_click_down();
31
33
  void mouse_click_down2(CGPoint point);
32
- void mouse_click_down3(CGPoint point, size_t sleep_quanta);
34
+ void mouse_click_down3(CGPoint point, uint_t sleep_quanta);
33
35
 
34
36
  void mouse_click_up();
35
37
  void mouse_click_up2(CGPoint point);
@@ -39,11 +41,11 @@ void mouse_click2(CGPoint point);
39
41
 
40
42
  void mouse_secondary_click();
41
43
  void mouse_secondary_click2(CGPoint point);
42
- void mouse_secondary_click3(CGPoint point, size_t sleep_quanta);
44
+ void mouse_secondary_click3(CGPoint point, uint_t sleep_quanta);
43
45
 
44
46
  void mouse_arbitrary_click(CGEventMouseSubtype button);
45
47
  void mouse_arbitrary_click2(CGEventMouseSubtype button, CGPoint point);
46
- void mouse_arbitrary_click3(CGEventMouseSubtype button, CGPoint point, size_t sleep_quanta);
48
+ void mouse_arbitrary_click3(CGEventMouseSubtype button, CGPoint point, uint_t sleep_quanta);
47
49
 
48
50
  void mouse_middle_click();
49
51
  void mouse_middle_click2(CGPoint point);
data/lib/mouse.rb CHANGED
@@ -1,12 +1,22 @@
1
1
  if RUBY_ENGINE == 'macruby'
2
2
 
3
+ # A workaround that guarantees that `CGPoint` is defined
3
4
  framework '/System/Library/Frameworks/CoreGraphics.framework'
4
5
 
5
6
  else
6
7
 
7
8
  ##
8
- #
9
+ # A structure that contains a point in a two-dimensional coordinate system
9
10
  class CGPoint < Struct.new(:x, :y)
11
+
12
+ # @!attribute [rw] x
13
+ # The `x` co-ordinate of the screen point
14
+ # @return [Number]
15
+
16
+ # @!attribute [rw] y
17
+ # The `y` co-ordinate of the screen point
18
+ # @return [Number]
19
+
10
20
  ##
11
21
  # Return a nice string representation of the point
12
22
  #
@@ -16,13 +26,11 @@ else
16
26
  def inspect
17
27
  "#<CGPoint x=#{self[:x]} y=#{self[:y]}>"
18
28
  end
29
+
19
30
  end
20
31
 
21
32
  end
22
33
 
23
-
24
- ##
25
- # Mouse extensions to `CGPoint`
26
34
  class CGPoint
27
35
  ##
28
36
  # Returns the receiver, since the receiver is already a {CGPoint}
data/lib/mouse/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Mouse
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
4
4
 
metadata CHANGED
@@ -1,19 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mouse
3
3
  version: !ruby/object:Gem::Version
4
+ version: 0.1.0
4
5
  prerelease:
5
- version: 0.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mark Rada
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-02 00:00:00 Z
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yard
16
- prerelease: false
17
16
  requirement: !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
@@ -21,6 +20,7 @@ dependencies:
21
20
  - !ruby/object:Gem::Version
22
21
  version: 0.8.3
23
22
  type: :development
23
+ prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
@@ -29,7 +29,6 @@ dependencies:
29
29
  version: 0.8.3
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: kramdown
32
- prerelease: false
33
32
  requirement: !ruby/object:Gem::Requirement
34
33
  none: false
35
34
  requirements:
@@ -37,6 +36,7 @@ dependencies:
37
36
  - !ruby/object:Gem::Version
38
37
  version: 0.14.1
39
38
  type: :development
39
+ prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
@@ -45,7 +45,6 @@ dependencies:
45
45
  version: 0.14.1
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: rake-compiler
48
- prerelease: false
49
48
  requirement: !ruby/object:Gem::Requirement
50
49
  none: false
51
50
  requirements:
@@ -53,13 +52,15 @@ dependencies:
53
52
  - !ruby/object:Gem::Version
54
53
  version: 0.8.1
55
54
  type: :development
55
+ prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: 0.8.1
62
- description: 'mouse is a rich, high level wrapper around OS X CGEvent APIs that allow
62
+ description: ! 'mouse is a rich, high level wrapper around OS X CGEvent APIs that
63
+ allow
63
64
 
64
65
  programmatic manipulation of the mouse cursor.
65
66
 
@@ -94,15 +95,21 @@ require_paths:
94
95
  required_ruby_version: !ruby/object:Gem::Requirement
95
96
  none: false
96
97
  requirements:
97
- - - '>='
98
+ - - ! '>='
98
99
  - !ruby/object:Gem::Version
99
- version: "0"
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: 1024147063034037924
100
104
  required_rubygems_version: !ruby/object:Gem::Requirement
101
105
  none: false
102
106
  requirements:
103
- - - '>='
107
+ - - ! '>='
104
108
  - !ruby/object:Gem::Version
105
- version: "0"
109
+ version: '0'
110
+ segments:
111
+ - 0
112
+ hash: 1024147063034037924
106
113
  requirements: []
107
114
  rubyforge_project:
108
115
  rubygems_version: 1.8.24