Module: RWin::API

Defined in:
ext/rwin/rw_api.c,
lib/wrb/gmem.rb,
lib/rwin.rb

Defined Under Namespace

Classes: BITMAPINFO, CStruct, DEVMODE, IMAGEINFO, LOGBRUSH, LOGFONT, LOGPEN, MENUITEMINFO, POINT, RECT, SIZE

Constant Summary collapse

TypeAliases =
{
  # Base
  'B'             => 'B',
  'C'             => 'C',
  'H'             => 'H',
  'W'             => 'W',
  'L'             => 'L',
  'P'             => 'P',
  'S'             => 'S',
  'U'             => 'U',
  'T'             => RWin::UNICODE ? 'W' : 'C',
  'c'             => 'c', # Not encoded string for char*
  'Q'             => 'Q', # X64 ? ULONGLONG : ULONG
  'I'             => 'I', # X64 ? LONGLONG : LONG
  
  # Common
  'byte'          => 'B',
  'char'          => 'c', # Not encoded string for char*
  'wchar_t'       => 'W',
  'unsignedchar'  => 'B',
  'short'         => 'H',
  'int'           => 'L',
  'uint'          => 'U',
  'unsigned'      => 'U',
  'unsignedint'   => 'U',
  'long'          => 'L',
  'unsignedlong'  => 'U',
  'void'          => 'L',
  
  # Windows
  'CHAR'          => 'C', # Encoded string for CHAR*
  'UCHAR'         => 'B',
  'BYTE'          => 'B',
  'WCHAR'         => 'W',
  'SHORT'         => 'H',
  'WORD'          => 'H',
  'ATOM'          => 'L',
  'INT'           => 'L',
  'UINT'          => 'U',
  'BOOL'          => 'L',
  'LONG'          => 'L',
  'ULONG'         => 'U',
  'DWORD'         => 'U',
  'HICON'         => 'U',
  'COLORREF'      => 'U',
  'LPARAM'        => 'Q',
  'WPARAM'        => 'Q',
  'VOID*'         => 'P',
  'LPSTR'         => 'S',
  'LPWSTR'        => 'S',
  'LPTSTR'        => 'S',
  'LPCSTR'        => 'S',
  'LPCWSTR'       => 'S',
  'LPCTSTR'       => 'S',
  'TCHAR'         => RWin::UNICODE ? 'W' : 'C',
  'HANDLE'        => 'Q',
  'HINSTANCE'     => 'Q',
  'HWND'          => 'Q',
  'HDC'           => 'Q',
  'HBITMAP'       => 'Q',
  'ULONG_PTR'     => 'Q', # for X64
  'UINT_PTR'      => 'Q', # for X64
  'DWORD_PTR'     => 'Q', # for X64
  'LONG_PTR'      => 'I', # for X64
  'INT_PTR'       => 'I' # for X64
}
TypeLength =
{
  'B' => 1,
  'C' => 1,
  'H' => 2,
  'W' => 2,
  'L' => 4,
  'U' => 4,
  'P' => RWin::X64 ? 8 : 4,
  'S' => RWin::X64 ? 8 : 4,
  'Q' => RWin::X64 ? 8 : 4,
  'I' => RWin::X64 ? 8 : 4
}
@@__nullchar_w_ =
"\0\0".force_encoding(Encoding::UTF_16LE)
@@__nullregexp_w_ =
Regexp.new("\0*$".encode!(Encoding::UTF_16LE))
@@__crregexp_w_ =
Regexp.new("\r".encode!(Encoding::UTF_16LE))
@@__lfregexp_w_ =
Regexp.new("\n".encode!(Encoding::UTF_16LE))
@@__crlfchar_w_ =
"\r\n".encode!(Encoding::UTF_16LE)
@@_klasses_ =

RWin::API.defstruct( definittions…)

[]

Class Method Summary collapse

Class Method Details

._TObject



348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/rwin.rb', line 348

def encode_output(s)
  str = s.to_s
  enc = RWin::Application.external_encoding
  if str.encoding == Encoding::ASCII_8BIT
    str.force_encoding(Encoding::UTF_16LE)
  end
  r = str.encode(enc)
  if enc == Encoding::UTF_16LE
    r.gsub(@@__nullregexp_w_, '')
  else
    r.gsub(/\0*$/, '')
  end
end

._XObject



349
350
351
352
353
354
355
# File 'lib/rwin.rb', line 349

def encode_default(s)
  str = s.to_s
  if str.encoding == Encoding::ASCII_8BIT
    str.force_encoding(Encoding::UTF_16LE)
  end
  str.encode(Encoding.default_external).gsub(/\0*$/, '')
end

.adjust_internal_lf_code(s) ⇒ Object



295
296
297
298
299
300
301
302
# File 'lib/rwin.rb', line 295

def adjust_internal_lf_code(s)
  str = s.to_s
  if str.encoding==Encoding::UTF_16LE
    str.gsub(@@__crregexp_w_ , '').gsub(@@__lfregexp_w_, @@__crlfchar_w_)
  else
    str.gsub(/\r/, '').gsub(/\n/, "\r\n")
  end
end

.adjust_output_lf_code(s) ⇒ Object



304
305
306
307
308
309
310
311
# File 'lib/rwin.rb', line 304

def adjust_output_lf_code(s)
  str = s.to_s
  if str.encoding==Encoding::UTF_16LE
    str.gsub(@@__crregexp_w_, '')
  else
    str.gsub(/\r/, '')
  end
end

.callbackadress(arg = nil, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/rwin.rb', line 131

def self.callbackadress(arg=nil, &block)
  if block_given?
    RWin::API.bindCallbackProc(Proc.new(&block))
  elsif arg.is_a?(Proc)
    RWin::API.bindCallbackProc(arg)
  else
    raise ArgumentError, "Neither Proc nor Block", caller(1)
  end
end

.defstruct(*args) ⇒ Object



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/rwin.rb', line 588

def self.defstruct(*args)
  if args[0]==true || args[0]==nil || args[0]==false || args[0]==:pack
    need_pack = args.shift
    need_pack = true if need_pack
  else
    need_pack = nil;
  end
#    dpp need_pack
  if args[0].class == Class || args[0].class == Module || args[0].class == Object
    mname = args.shift.name + '::DUMMY'
  elsif  (kls = @@_klasses_.pop) &&
    mname = kls.to_s
  end
  if mname && !mname.empty?
    ar = mname.split('::')
    ar.pop
    modules = ar.inject([]){|ar, s| ar << ar.last.to_s + '::' + s}.map{|i| eval(i)}
    modules.reverse!
  end
  args = args[0].split(/\s*[,;]\s*/) if args.size == 1
  
  if args.size == 1 && args[0].match(/^([A-Z]?\w*)\s*\[\s*\]/) # variable length array
    newclass = Class.new(CStruct){
      klass, b_type, name, len, a_size = parse_arg(args[0], modules)
#        self.def_members b_type+'1', nil, klass ? [klass] : nil
      deftemplate(b_type+'1', klass ? [klass] : nil)
    }
  else
    newclass = Class.new(CStruct){
      members = []
      classes = []
      offset = 0
      template = ""
      btypes = ""
      maxbytes = 1
      args.each do |arg|
        klass, b_type, name, len, a_size = parse_arg(arg, modules)
#          dpp klass, b_type, name, len, a_size
        b_size = TypeLength[b_type] #;dpp [maxbytes, b_size]
        maxbytes = b_size if b_size && maxbytes < b_size
        if a_size == 0                          # member except array
          btypes = b_type
          offset += len
        elsif a_size >= 1     # Array
          btypes = b_type + a_size.to_s
          offset += len * a_size
        else
          raise ArgumentError, "illegal data size: #{a_size}", caller(1)
        end
        members << name.to_s
        classes << klass
        klass = nil
        template.concat btypes
      end # each
      members = members==[""] ? nil : members
      classes = classes==[nil] ? nil : classes
      # self.def_members(template, members, classes, need_pack)
      deftemplate(template, classes && classes.compact, need_pack)
      defmembers(*members)
    } # Class.new
  end
  @@_klasses_ << newclass
  newclass
end

.defun(*args) ⇒ Object



181
# File 'lib/rwin.rb', line 181

def self.defun(*args) defun_core(args, nil); end

.defun_ubf(*args) ⇒ Object



182
# File 'lib/rwin.rb', line 182

def self.defun_ubf(*args) defun_core(args, true); end

.encode_default(s) ⇒ Object



332
333
334
335
336
337
338
# File 'lib/rwin.rb', line 332

def encode_default(s)
  str = s.to_s
  if str.encoding == Encoding::ASCII_8BIT
    str.force_encoding(Encoding::UTF_16LE)
  end
  str.encode(Encoding.default_external).gsub(/\0*$/, '')
end

.encode_internal(s) ⇒ Object



313
314
315
316
# File 'lib/rwin.rb', line 313

def encode_internal(s)
  str = s.to_s
  str.encode(Encoding::UTF_16LE).gsub(@@__nullregexp_w_, '').concat(@@__nullchar_w_)
end

.encode_output(s) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/rwin.rb', line 318

def encode_output(s)
  str = s.to_s
  enc = RWin::Application.external_encoding
  if str.encoding == Encoding::ASCII_8BIT
    str.force_encoding(Encoding::UTF_16LE)
  end
  r = str.encode(enc)
  if enc == Encoding::UTF_16LE
    r.gsub(@@__nullregexp_w_, '')
  else
    r.gsub(/\0*$/, '')
  end
end

.encode_wide(str) ⇒ Object



340
# File 'lib/rwin.rb', line 340

def encode_wide(str) encode_internal(str); end

.encoding2bin(str) ⇒ Object



342
# File 'lib/rwin.rb', line 342

def encoding2bin(str) str.force_encoding(Encoding::ASCII_8BIT); end

.encoding2default(str) ⇒ Object



344
345
346
# File 'lib/rwin.rb', line 344

def encoding2default(str)
  str.force_encoding(Encoding.default_external)
end

.int2uint(i) ⇒ Object



369
370
371
# File 'lib/rwin.rb', line 369

def int2uint(i)
  i = i & 0xffffffff_ffffffff
end

.lasterrorObject



283
284
285
286
287
# File 'lib/rwin.rb', line 283

def self.lasterror()
  buff = TCHAR[256]
  len = FormatMessage(0x1000, 0, RWin::API.GetLastError, 0, buff, buff.size, nil)
  buff.to_s
end

.loadlib(*args) ⇒ Object



127
128
129
# File 'lib/rwin.rb', line 127

def self.loadlib(*args)
  set_dlls(*args)
end

.typealias(newtype, oldtype) ⇒ Object



351
352
353
354
355
356
357
358
# File 'lib/rwin.rb', line 351

def typealias(newtype, oldtype)
  if btypes = TypeAliases[oldtype]
    raise TypeError,"template `P' not allowed, change type directly", caller(1) if btypes=='P'
    TypeAliases[newtype] = btypes
  else
    raise ArgumentError, "#{oldtype} is not registered", caller(1)
  end
end

.uint2int(u) ⇒ Object



365
366
367
368
# File 'lib/rwin.rb', line 365

def uint2int(u)
  u = u & 0xffffffff_ffffffff
  u > 0x7fffffff_ffffffff ? u - 0x100000000_00000000 : u
end