mac-robot 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "bundler"
11
+ gem "jeweler", "~> 1.8.3"
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.3)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.6.5)
12
+ rake (0.9.2.2)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+ rspec (2.8.0)
16
+ rspec-core (~> 2.8.0)
17
+ rspec-expectations (~> 2.8.0)
18
+ rspec-mocks (~> 2.8.0)
19
+ rspec-core (2.8.0)
20
+ rspec-expectations (2.8.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.8.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler
29
+ jeweler (~> 1.8.3)
30
+ rspec (~> 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 youpy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,33 @@
1
+ = mac-robot
2
+
3
+ A Library to Automate User Interactions
4
+
5
+ == Synopsis
6
+
7
+ require 'mac-robot'
8
+
9
+ robot = Mac::Robot.new
10
+
11
+ === Automate Mouse
12
+
13
+ robot.mouse_press
14
+ robot.mouse_move(250, 250)
15
+ robot.mouse_release
16
+
17
+ === Post Notification
18
+
19
+ == Contributing to mac-robot
20
+
21
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
22
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
23
+ * Fork the project.
24
+ * Start a feature/bugfix branch.
25
+ * Commit and push until you are happy with your contribution.
26
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
27
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
28
+
29
+ == Copyright
30
+
31
+ Copyright (c) 2012 youpy. See LICENSE.txt for
32
+ further details.
33
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "mac-robot"
18
+ gem.homepage = "http://github.com/youpy/mac-robot"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A Library to Automate User Interactions}
21
+ gem.description = %Q{A Library to Automate User Interactions}
22
+ gem.email = "youpy@buycheapviagraonlinenow.com"
23
+ gem.authors = ["youpy"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ # rule to build the extension: this says
29
+ # that the extension should be rebuilt
30
+ # after any change to the files in ext
31
+ ext_names = %w/event_dispatcher/
32
+ ext_names.each do |ext_name|
33
+ file "lib/#{ext_name}.bundle" =>
34
+ Dir.glob("ext/#{ext_name}/*{.rb,.m}") do
35
+ Dir.chdir("ext/#{ext_name}") do
36
+ # this does essentially the same thing
37
+ # as what RubyGems does
38
+ ruby "extconf.rb"
39
+ sh "make"
40
+ end
41
+ cp "ext/#{ext_name}/#{ext_name}.bundle", "lib/"
42
+ end
43
+
44
+ task :spec => "lib/#{ext_name}.bundle"
45
+ end
46
+
47
+ require 'rspec/core'
48
+ require 'rspec/core/rake_task'
49
+ RSpec::Core::RakeTask.new(:spec) do |spec|
50
+ spec.pattern = FileList['spec/**/*_spec.rb']
51
+ end
52
+
53
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,2 @@
1
+ #include <ruby.h>
2
+ #import <Foundation/Foundation.h>
@@ -0,0 +1,89 @@
1
+ #import "event_dispatcher.h"
2
+
3
+ static VALUE rb_cEventDispatcher;
4
+
5
+ struct EventDispatcherObject {};
6
+
7
+ void cEventDispatcher_free(void *ptr) {
8
+ free(ptr);
9
+ }
10
+
11
+ VALUE createInstance() {
12
+ struct EventDispatcherObject *obj;
13
+
14
+ obj = malloc(sizeof(struct EventDispatcherObject));
15
+
16
+ return Data_Wrap_Struct(rb_cEventDispatcher, 0, cEventDispatcher_free, obj);
17
+ }
18
+
19
+ CGEventType getEventTypeFromValue(VALUE value)
20
+ {
21
+ const char *typeStr;
22
+ CGEventType type;
23
+
24
+ typeStr = StringValuePtr(value);
25
+
26
+ if(strcmp(typeStr, "mouse_down") == 0) {
27
+ type = kCGEventLeftMouseDown;
28
+ } else if(strcmp(typeStr, "mouse_up") == 0) {
29
+ type = kCGEventLeftMouseUp;
30
+ } else if(strcmp(typeStr, "mouse_move") == 0) {
31
+ type = kCGEventMouseMoved;
32
+ } else {
33
+ type = kCGEventNull;
34
+ }
35
+
36
+ return type;
37
+ }
38
+
39
+ CGMouseButton getMouseButtonFromValue(VALUE value)
40
+ {
41
+ // http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html
42
+ //
43
+ // enum _CGMouseButton {
44
+ // kCGMouseButtonLeft = 0,
45
+ // kCGMouseButtonRight = 1,
46
+ // kCGMouseButtonCenter = 2
47
+ // };
48
+ // typedef uint32_t CGMouseButton;
49
+ //
50
+ return (CGMouseButton)NUM2INT(value);
51
+ }
52
+
53
+ static VALUE cEventDispatcher_new(int argc, VALUE *argv, VALUE klass)
54
+ {
55
+ VALUE obj;
56
+
57
+ obj = createInstance();
58
+
59
+ return obj;
60
+ }
61
+
62
+ static VALUE cEventDispatcher_dispatchMouseEvent(int argc, VALUE *argv, VALUE self)
63
+ {
64
+ VALUE x, y, button, type;
65
+ CGEventRef event;
66
+
67
+ rb_scan_args(argc, argv, "4", &x, &y, &button, &type);
68
+
69
+ event = CGEventCreateMouseEvent(
70
+ NULL,
71
+ getEventTypeFromValue(rb_funcall(type, rb_intern("to_s"), 0)),
72
+ CGPointMake(NUM2INT(x), NUM2INT(y)),
73
+ getMouseButtonFromValue(button));
74
+
75
+ CGEventPost(kCGHIDEventTap, event);
76
+ CFRelease(event);
77
+
78
+ return Qnil;
79
+ }
80
+
81
+ void Init_event_dispatcher(void){
82
+ VALUE rb_mMac, rb_cRobot;
83
+
84
+ rb_mMac = rb_define_module("Mac");
85
+ rb_cRobot = rb_define_class_under(rb_mMac, "Robot", rb_cObject);
86
+ rb_cEventDispatcher = rb_define_class_under(rb_cRobot, "EventDispatcher", rb_cObject);
87
+ rb_define_singleton_method(rb_cEventDispatcher, "new", cEventDispatcher_new, -1);
88
+ rb_define_method(rb_cEventDispatcher, "dispatchMouseEvent", cEventDispatcher_dispatchMouseEvent, -1);
89
+ }
@@ -0,0 +1,14 @@
1
+ require "mkmf"
2
+
3
+ # Name your extension
4
+ extension_name = 'event_dispatcher'
5
+
6
+ # Set your target name
7
+ dir_config(extension_name)
8
+
9
+ $LDFLAGS += ' -framework ApplicationServices'
10
+
11
+ have_header(extension_name)
12
+
13
+ # Do the work
14
+ create_makefile(extension_name)
data/lib/mac-robot.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'event_dispatcher'
2
+
3
+ module Mac
4
+ class Robot
5
+ attr_reader :x, :y
6
+
7
+ BUTTONS = {
8
+ :left => 0,
9
+ :right => 1,
10
+ :center => 2
11
+ }
12
+
13
+ def initialize
14
+ @x = 0
15
+ @y = 0
16
+ @state = :mouse_up
17
+ end
18
+
19
+ def mouse_press(button = :left)
20
+ mouse_event(button, :mouse_down)
21
+ @state = :mouse_down
22
+ end
23
+
24
+ def mouse_release(button = :left)
25
+ mouse_event(button, :mouse_up)
26
+ @state = :mouse_up
27
+ end
28
+
29
+ def mouse_move(x, y)
30
+ @x = x
31
+ @y = y
32
+
33
+ mouse_event(:left, :mouse_move)
34
+ end
35
+
36
+ private
37
+
38
+ def mouse_event(button, type)
39
+ dispatcher = EventDispatcher.new
40
+ dispatcher.dispatchMouseEvent(@x, @y, BUTTONS[button], type)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Mac::Robot do
4
+ subject do
5
+ Mac::Robot.new
6
+ end
7
+
8
+ describe 'mouse event' do
9
+ # how to test???
10
+ it 'should simulate mouse event' do
11
+ subject.mouse_press(:left)
12
+ subject.mouse_move(250, 250)
13
+ subject.mouse_release(:left)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'mac-robot'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mac-robot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - youpy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70330368082100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70330368082100
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70330368081540 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70330368081540
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &70330368080980 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.8.3
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70330368080980
47
+ description: A Library to Automate User Interactions
48
+ email: youpy@buycheapviagraonlinenow.com
49
+ executables: []
50
+ extensions:
51
+ - ext/event_dispatcher/extconf.rb
52
+ extra_rdoc_files:
53
+ - LICENSE.txt
54
+ - README.rdoc
55
+ files:
56
+ - .document
57
+ - .rspec
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE.txt
61
+ - README.rdoc
62
+ - Rakefile
63
+ - VERSION
64
+ - ext/event_dispatcher/event_dispatcher.h
65
+ - ext/event_dispatcher/event_dispatcher.m
66
+ - ext/event_dispatcher/extconf.rb
67
+ - lib/mac-robot.rb
68
+ - spec/mac-robot_spec.rb
69
+ - spec/spec_helper.rb
70
+ homepage: http://github.com/youpy/mac-robot
71
+ licenses:
72
+ - MIT
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
84
+ - 0
85
+ hash: -1500423455916059717
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.10
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: A Library to Automate User Interactions
98
+ test_files: []