clipboard 0.8.4 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,15 +15,19 @@ On Linux, you can choose, from which clipboard you want to +paste+, default is P
15
15
 
16
16
  +copy+ copies to all clipboards in Clipboard::CLIPBOARDS.
17
17
 
18
+ ==== Windows Encoding note
19
+ In you use 1.9, the Clipboard encoding will be translated into UTF-8 and then to your Encoding.default_external
20
+ If you use 1.8, it will fallback to CP850 encoding
21
+
18
22
  === Non-gem requirements
19
23
  * *Linux*: +xclip+ (you can install it on debian/ubuntu with <tt>sudo apt-get install xclip</tt>)
20
24
  * *Windows*: +clip+ (installed by default since Vista)
21
25
 
22
- === Bugs / TODO
23
- * Fix 1.9 and Encoding issues on Windows
24
- * Don't depend on +xclip+ ?
26
+ === Todo
27
+ * Don't depend on +clip+
28
+ * Don't depend on +xclip+
25
29
 
26
- Feel free to report bugs or to implement one of the TODO entries ;)
30
+ Feel free to report bugs or to implement one of the Todo entries ;)
27
31
 
28
32
  === Copyright
29
33
  Copyright (c) 2010 Jan Lelis, http://rbjl.net, released under the MIT license
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.4
1
+ 0.9.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{clipboard}
8
- s.version = "0.8.4"
8
+ s.version = "0.9.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jan Lelis"]
12
- s.date = %q{2010-10-03}
12
+ s.date = %q{2010-10-13}
13
13
  s.description = %q{Access the clipboard on all systems (Clipboard.copy & Clipboard.paste)}
14
14
  s.email = %q{mail@janlelis.de}
15
15
  s.extra_rdoc_files = [
@@ -1,4 +1,5 @@
1
1
  require 'zucker/os'
2
+ require 'zucker/version'
2
3
  require 'zucker/alias_for'
3
4
  require 'stringio'
4
5
  require File.expand_path '../../version', __FILE__
@@ -7,42 +8,66 @@ module Clipboard
7
8
  if OS.windows?
8
9
  WriteCommands = ['clip']
9
10
  CF_TEXT = 1
11
+ CF_UNICODETEXT = 13
10
12
 
11
- require 'Win32API'
12
- # init api handlers
13
- @open = Win32API.new("user32", "OpenClipboard",['L'],'L')
14
- @close = Win32API.new("user32", "CloseClipboard",[],'L')
15
- @empty = Win32API.new("user32", "EmptyClipboard",[],'L')
16
- @get = Win32API.new("user32", "GetClipboardData", ['L'], 'L')
17
- @lock = Win32API.new("kernel32", "GlobalLock", ['L'], 'P')
18
- @unlock = Win32API.new("kernel32", "GlobalUnlock", ['L'], 'L')
19
- instance_variables.each{ |handler|
20
- instance_variable_get(handler).instance_eval do
21
- alias [] call
22
- end
23
- }
13
+ # get ffi function handlers
14
+ require 'ffi'
24
15
 
16
+ module User32
17
+ extend FFI::Library
18
+ ffi_lib "user32"
19
+ ffi_convention :stdcall
20
+
21
+ attach_function :open, :OpenClipboard, [ :long ], :long
22
+ attach_function :close, :CloseClipboard, [ ], :long
23
+ attach_function :empty, :EmptyClipboard, [ ], :long
24
+ attach_function :get, :GetClipboardData, [ :long ], :long
25
+ end
26
+
27
+ module Kernel32
28
+ extend FFI::Library
29
+ ffi_lib 'kernel32'
30
+ ffi_convention :stdcall
31
+
32
+ attach_function :lock, :GlobalLock, [ :long ], :pointer
33
+ attach_function :unlock, :GlobalUnlock, [ :long ], :long
34
+ end
35
+
25
36
  # paste & clear
26
- # inspired by segment7.net and http://www.codeproject.com/KB/clipboard/archerclipboard1.aspx
27
- # does not work on 1.9, has probably something to do with utf8 strings ?
37
+ # see http://www.codeproject.com/KB/clipboard/archerclipboard1.aspx
28
38
  def self.paste(_=nil)
29
- data = ""
30
- if 0 != @open[ 0 ]
31
- hclip = @get[ CF_TEXT ]
32
- if 0 != hclip
33
- if 0 != data = @lock[ hclip ]
34
- @unlock[ hclip ]
39
+ ret = ""
40
+ if 0 != User32.open( 0 )
41
+ hclip = User32.get( CF_UNICODETEXT )
42
+ if hclip && 0 != hclip
43
+ pointer_to_data = Kernel32.lock( hclip )
44
+ data = ""
45
+ # Windows Unicode is ended by to null bytes, so get the whole string
46
+ current_byte = 0
47
+ until data.size >= 2 && data[-1].ord == 0 && data[-2].ord == 0
48
+ data << pointer_to_data.get_bytes( current_byte, 1 )
49
+ current_byte += 1
50
+ end
51
+ if RubyVersion >= 1.9
52
+ ret = data.chop.force_encoding("UTF-16LE").encode('UTF-8').encode(Encoding.default_external) # TODO check if direct translation is possible
53
+ else # 1.8: fallback to simple CP850 encoding
54
+ require 'iconv'
55
+ utf8 = Iconv.iconv( "UTF-8", "UTF-16LE", data.chop)[0]
56
+ ret = Iconv.iconv( "CP850", "UTF-8", utf8)[0]
57
+ end
58
+ if data && 0 != data
59
+ Kernel32.unlock( hclip )
35
60
  end
36
61
  end
37
- @close[]
62
+ User32.close( )
38
63
  end
39
- data || ""
64
+ ret || ""
40
65
  end
41
-
66
+
42
67
  def self.clear
43
- @open[0]
44
- @empty[]
45
- @close[]
68
+ User32.open( 0 )
69
+ User32.empty( )
70
+ User32.close( )
46
71
  paste
47
72
  end
48
73
  else #non-windows
data/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Clipboard
2
- VERSION = File.read File.expand_path '../VERSION', __FILE__
2
+ VERSION = File.read( File.expand_path('../VERSION', __FILE__)).chomp
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clipboard
3
3
  version: !ruby/object:Gem::Version
4
- hash: 55
4
+ hash: 59
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 8
9
- - 4
10
- version: 0.8.4
8
+ - 9
9
+ - 0
10
+ version: 0.9.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jan Lelis
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-03 00:00:00 +02:00
18
+ date: 2010-10-13 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency