movewin 1.2 → 1.3

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: a193b21672c8447a18c0d52b7d8c46acc72e4848
4
- data.tar.gz: 110afbc6ef126e64dc154b0a70ebd2f21d6fb837
3
+ metadata.gz: 186d90a92e2cba238cb35a3dd4c0ca998ae39c32
4
+ data.tar.gz: dbbebd61d13940f5e749697f81c0b121b9a57e5f
5
5
  SHA512:
6
- metadata.gz: 8e0f530fe1b99eaa8d1b98c7b49b6f2125937856859280f351d757b261ae60b5170393f7b07f48540a1d790cf7aa12b45318c1728d6818a7ee2d6c1138dc2160
7
- data.tar.gz: 1eba09edc9672467643e5b3ab1cb4e0aebe77f4712038cba21253d6bf9c31511b3e2d57048b8b97d828ea297cfc23dde9fd875889d0c2913f5166c92393de6ea
6
+ metadata.gz: 07394cd3f53cb282b2a606a46fc9802c5e773422f69a15b722cdf41aeea9e780ebb9d427e4ec95a1b22cf5feeb6cd582f6419a6ff39a8cf53ffbbc8868747ecd
7
+ data.tar.gz: 0cd496ec1c9c9d462db9c483d7908f04fc5a4313fe55cfa6dbd7cfe705e660345c40e2de79261c8cf81963d98551439af189828ddcf905f379279ef468742175
@@ -67,6 +67,10 @@ void StoreWindows(CFDictionaryRef cgWindow, void *rb_ary_ptr);
67
67
  /* Callback for Data_Wrap_Struct(), frees internal refs with CFRelease() */
68
68
  void MW_Window_destroy(void *ref);
69
69
 
70
+ /* Check if VALUE is number or has to_i() method; get number as integer */
71
+ static bool canGetNumber(VALUE v);
72
+ static int getNumber(VALUE v);
73
+
70
74
  /* Copy contents of a CFStringRef into a Ruby string */
71
75
  static VALUE convertStringRef(CFStringRef str);
72
76
 
@@ -188,11 +192,11 @@ VALUE MW_Window_set_position(VALUE self, VALUE args) {
188
192
  args = rb_ary_entry(args, 0);
189
193
  }
190
194
  if( RARRAY_LEN(args) == 2 &&
191
- TYPE(rb_ary_entry(args, 0)) == T_FIXNUM &&
192
- TYPE(rb_ary_entry(args, 1)) == T_FIXNUM )
195
+ canGetNumber(rb_ary_entry(args, 0)) &&
196
+ canGetNumber(rb_ary_entry(args, 1)) )
193
197
  {
194
- position.x = NUM2INT(rb_ary_entry(args, 0));
195
- position.y = NUM2INT(rb_ary_entry(args, 1));
198
+ position.x = getNumber(rb_ary_entry(args, 0));
199
+ position.y = getNumber(rb_ary_entry(args, 1));
196
200
  } else {
197
201
  rb_raise(rb_eArgError, "wrong number of arguments");
198
202
  }
@@ -211,11 +215,11 @@ VALUE MW_Window_set_size(VALUE self, VALUE args) {
211
215
  args = rb_ary_entry(args, 0);
212
216
  }
213
217
  if( RARRAY_LEN(args) == 2 &&
214
- TYPE(rb_ary_entry(args, 0)) == T_FIXNUM &&
215
- TYPE(rb_ary_entry(args, 1)) == T_FIXNUM )
218
+ canGetNumber(rb_ary_entry(args, 0)) &&
219
+ canGetNumber(rb_ary_entry(args, 1)) )
216
220
  {
217
- size.width = NUM2INT(rb_ary_entry(args, 0));
218
- size.height = NUM2INT(rb_ary_entry(args, 1));
221
+ size.width = getNumber(rb_ary_entry(args, 0));
222
+ size.height = getNumber(rb_ary_entry(args, 1));
219
223
  } else {
220
224
  rb_raise(rb_eArgError, "wrong number of arguments");
221
225
  }
@@ -253,7 +257,8 @@ void StoreWindows(CFDictionaryRef cgWindow, void *rb_ary_ptr) {
253
257
  axWindow = AXWindowFromCGWindow(cgWindow, i);
254
258
  if(!axWindow) break;
255
259
  mwWindow = (MW_Window *)malloc(sizeof(MW_Window));
256
- mwWindow->cgWindow = cgWindow;
260
+ mwWindow->cgWindow =
261
+ CFDictionaryCreateCopy(kCFAllocatorDefault, cgWindow);
257
262
  mwWindow->axWindow = axWindow;
258
263
  wrappedMwWindow = Data_Wrap_Struct(
259
264
  MW_WindowClass, NULL, MW_Window_destroy, (void *)mwWindow
@@ -272,6 +277,28 @@ void MW_Window_destroy(void *ref) {
272
277
  free(mwWindow);
273
278
  }
274
279
 
280
+ /* Return true if a VALUE is a number, or has a to_i() method */
281
+ static bool canGetNumber(VALUE v) {
282
+ ID rb_respond_to = rb_intern("respond_to?");
283
+ ID rb_to_i = rb_intern("to_i");
284
+
285
+ return TYPE(v) == T_FIXNUM ||
286
+ rb_funcall(v, rb_respond_to, 1, ID2SYM(rb_to_i)) == Qtrue;
287
+ }
288
+
289
+ /* Return an integer for a VALUE, calling to_i() first if needed */
290
+ static int getNumber(VALUE v) {
291
+ ID rb_to_i = rb_intern("to_i");
292
+
293
+ if(TYPE(v) == T_FIXNUM) {
294
+ return NUM2INT(v);
295
+ } else if(canGetNumber(v)) {
296
+ return NUM2INT(rb_funcall(v, rb_to_i, 0));
297
+ } else {
298
+ return 0;
299
+ }
300
+ }
301
+
275
302
  /* Given a CFStringRef, copy its contents into a Ruby string */
276
303
  static VALUE convertStringRef(CFStringRef str) {
277
304
  CFRange range;
@@ -94,6 +94,7 @@ int EnumerateWindows(
94
94
  if(windowName) free(windowName);
95
95
  if(appName) free(appName);
96
96
  }
97
+ CFRelease(windowList);
97
98
  if(subPattern != pattern) free(subPattern);
98
99
 
99
100
  return count;
@@ -219,6 +220,7 @@ AXUIElementRef AXWindowFromCGWindow(CFDictionaryRef window, int minIdx) {
219
220
  matchIdx++;
220
221
  }
221
222
  }
223
+ CFRelease(app);
222
224
 
223
225
  return foundAppWindow;
224
226
  }
data/lib/movewin.rb CHANGED
@@ -36,7 +36,7 @@
36
36
  require 'movewin/movewin_ext'
37
37
 
38
38
  module MoveWin
39
- VERSION = '1.2'
39
+ VERSION = '1.3'
40
40
 
41
41
  # Individual accessors for display size components
42
42
  def self.display_width; MoveWin.display_size[0]; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: movewin
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.2"
4
+ version: "1.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Ho
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2014-09-06 00:00:00 Z
12
+ date: 2014-09-08 00:00:00 Z
13
13
  dependencies: []
14
14
 
15
15
  description: List and move OS X windows from Ruby via the OS X accessibility APIs.