win32console 1.0.8-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. data/Console-i386-mswin32.def +2 -0
  2. data/Console.cpp +1203 -0
  3. data/Console.rdoc +690 -0
  4. data/Console_ANSI.rdoc +302 -0
  5. data/HISTORY.txt +7 -0
  6. data/INSTALL.txt +18 -0
  7. data/README.txt +26 -0
  8. data/Rakefile +38 -0
  9. data/doc/classes/Win32.html +115 -0
  10. data/doc/classes/Win32/Console.html +650 -0
  11. data/doc/classes/Win32/Console.src/M000001.html +31 -0
  12. data/doc/classes/Win32/Console.src/M000002.html +23 -0
  13. data/doc/classes/Win32/Console.src/M000003.html +23 -0
  14. data/doc/classes/Win32/Console.src/M000004.html +27 -0
  15. data/doc/classes/Win32/Console.src/M000005.html +23 -0
  16. data/doc/classes/Win32/Console.src/M000006.html +28 -0
  17. data/doc/classes/Win32/Console.src/M000007.html +23 -0
  18. data/doc/classes/Win32/Console.src/M000008.html +24 -0
  19. data/doc/classes/Win32/Console.src/M000009.html +44 -0
  20. data/doc/classes/Win32/Console.src/M000010.html +23 -0
  21. data/doc/classes/Win32/Console.src/M000011.html +33 -0
  22. data/doc/classes/Win32/Console.src/M000012.html +26 -0
  23. data/doc/classes/Win32/Console.src/M000013.html +27 -0
  24. data/doc/classes/Win32/Console.src/M000014.html +28 -0
  25. data/doc/classes/Win32/Console.src/M000015.html +23 -0
  26. data/doc/classes/Win32/Console.src/M000016.html +23 -0
  27. data/doc/classes/Win32/Console.src/M000017.html +23 -0
  28. data/doc/classes/Win32/Console.src/M000018.html +29 -0
  29. data/doc/classes/Win32/Console.src/M000019.html +23 -0
  30. data/doc/classes/Win32/Console.src/M000020.html +23 -0
  31. data/doc/classes/Win32/Console.src/M000021.html +28 -0
  32. data/doc/classes/Win32/Console.src/M000022.html +23 -0
  33. data/doc/classes/Win32/Console.src/M000023.html +28 -0
  34. data/doc/classes/Win32/Console.src/M000024.html +35 -0
  35. data/doc/classes/Win32/Console.src/M000025.html +28 -0
  36. data/doc/classes/Win32/Console.src/M000026.html +28 -0
  37. data/doc/classes/Win32/Console.src/M000027.html +28 -0
  38. data/doc/classes/Win32/Console.src/M000028.html +31 -0
  39. data/doc/classes/Win32/Console.src/M000029.html +23 -0
  40. data/doc/classes/Win32/Console.src/M000030.html +23 -0
  41. data/doc/classes/Win32/Console.src/M000031.html +23 -0
  42. data/doc/classes/Win32/Console.src/M000032.html +27 -0
  43. data/doc/classes/Win32/Console.src/M000033.html +27 -0
  44. data/doc/classes/Win32/Console.src/M000034.html +25 -0
  45. data/doc/classes/Win32/Console/ANSI.html +103 -0
  46. data/doc/classes/Win32/Console/ANSI/IO.html +220 -0
  47. data/doc/classes/Win32/Console/ANSI/IO.src/M000035.html +32 -0
  48. data/doc/classes/Win32/Console/ANSI/IO.src/M000036.html +205 -0
  49. data/doc/classes/Win32/Console/ANSI/IO.src/M000037.html +40 -0
  50. data/doc/classes/Win32/Console/ANSI/IO.src/M000038.html +25 -0
  51. data/doc/classes/Win32/Console/API.html +758 -0
  52. data/doc/classes/Win32/Console/API.src/M000039.html +27 -0
  53. data/doc/classes/Win32/Console/API.src/M000040.html +27 -0
  54. data/doc/classes/Win32/Console/API.src/M000041.html +27 -0
  55. data/doc/classes/Win32/Console/API.src/M000042.html +32 -0
  56. data/doc/classes/Win32/Console/API.src/M000043.html +32 -0
  57. data/doc/classes/Win32/Console/API.src/M000044.html +28 -0
  58. data/doc/classes/Win32/Console/API.src/M000045.html +26 -0
  59. data/doc/classes/Win32/Console/API.src/M000046.html +26 -0
  60. data/doc/classes/Win32/Console/API.src/M000047.html +27 -0
  61. data/doc/classes/Win32/Console/API.src/M000048.html +30 -0
  62. data/doc/classes/Win32/Console/API.src/M000049.html +29 -0
  63. data/doc/classes/Win32/Console/API.src/M000050.html +27 -0
  64. data/doc/classes/Win32/Console/API.src/M000051.html +28 -0
  65. data/doc/classes/Win32/Console/API.src/M000052.html +30 -0
  66. data/doc/classes/Win32/Console/API.src/M000053.html +27 -0
  67. data/doc/classes/Win32/Console/API.src/M000054.html +29 -0
  68. data/doc/classes/Win32/Console/API.src/M000055.html +29 -0
  69. data/doc/classes/Win32/Console/API.src/M000056.html +28 -0
  70. data/doc/classes/Win32/Console/API.src/M000057.html +27 -0
  71. data/doc/classes/Win32/Console/API.src/M000058.html +47 -0
  72. data/doc/classes/Win32/Console/API.src/M000059.html +32 -0
  73. data/doc/classes/Win32/Console/API.src/M000060.html +47 -0
  74. data/doc/classes/Win32/Console/API.src/M000061.html +34 -0
  75. data/doc/classes/Win32/Console/API.src/M000062.html +32 -0
  76. data/doc/classes/Win32/Console/API.src/M000063.html +32 -0
  77. data/doc/classes/Win32/Console/API.src/M000064.html +35 -0
  78. data/doc/classes/Win32/Console/API.src/M000065.html +26 -0
  79. data/doc/classes/Win32/Console/API.src/M000066.html +27 -0
  80. data/doc/classes/Win32/Console/API.src/M000067.html +29 -0
  81. data/doc/classes/Win32/Console/API.src/M000068.html +27 -0
  82. data/doc/classes/Win32/Console/API.src/M000069.html +27 -0
  83. data/doc/classes/Win32/Console/API.src/M000070.html +28 -0
  84. data/doc/classes/Win32/Console/API.src/M000071.html +27 -0
  85. data/doc/classes/Win32/Console/API.src/M000072.html +26 -0
  86. data/doc/classes/Win32/Console/API.src/M000073.html +27 -0
  87. data/doc/classes/Win32/Console/API.src/M000074.html +31 -0
  88. data/doc/classes/Win32/Console/API.src/M000075.html +27 -0
  89. data/doc/classes/Win32/Console/API.src/M000076.html +32 -0
  90. data/doc/classes/Win32/Console/API.src/M000077.html +27 -0
  91. data/doc/classes/Win32/Console/API.src/M000078.html +32 -0
  92. data/doc/classes/Win32/Console/API.src/M000079.html +32 -0
  93. data/doc/classes/Win32/Console/API.src/M000080.html +32 -0
  94. data/doc/classes/Win32/Console/Constants.html +360 -0
  95. data/doc/created.rid +1 -0
  96. data/doc/files/Console_ANSI_rdoc.html +407 -0
  97. data/doc/files/Console_cpp.html +104 -0
  98. data/doc/files/Console_rdoc.html +964 -0
  99. data/doc/files/lib/Win32/Console/ANSI_rb.html +123 -0
  100. data/doc/files/lib/Win32/Console_rb.html +297 -0
  101. data/doc/fr_class_index.html +32 -0
  102. data/doc/fr_file_index.html +31 -0
  103. data/doc/fr_method_index.html +106 -0
  104. data/doc/index.html +24 -0
  105. data/doc/rdoc-style.css +172 -0
  106. data/lib/Console.so +0 -0
  107. data/lib/README_GEM.txt +64 -0
  108. data/lib/Term/ansicolor.rb +76 -0
  109. data/lib/Win32/Console.rb +970 -0
  110. data/lib/Win32/Console/ANSI.rb +305 -0
  111. data/lib/win32console.rb +3 -0
  112. data/test/test_cursor.rb +9 -0
  113. data/test/test_mouse.rb +6 -0
  114. data/test/test_readinput.rb +62 -0
  115. data/test/test_readoutput.rb +52 -0
  116. data/test/test_sendevent.rb +17 -0
  117. data/test/test_title.rb +14 -0
  118. data/test/test_write.rb +36 -0
  119. metadata +180 -0
@@ -0,0 +1,2 @@
1
+ EXPORTS
2
+ Init_Console
data/Console.cpp ADDED
@@ -0,0 +1,1203 @@
1
+
2
+ #include "windows.h"
3
+ #include "ruby.h"
4
+
5
+ #ifdef WIN32
6
+ #define CONSOLE_EXPORT __declspec(dllexport)
7
+ #else
8
+ #error Not compiling on Windows
9
+ #endif
10
+
11
+ VALUE rb_mWin32;
12
+ VALUE rb_mConsole;
13
+ VALUE rb_mAPI;
14
+ VALUE rb_mConstants;
15
+
16
+ /* old RUBY_METHOD_FUNC() definition doesn't match to prototypes in those days. */
17
+ #ifndef ANYARGS
18
+ #undef RUBY_METHOD_FUNC
19
+ #define RUBY_METHOD_FUNC(func) ((VALUE (*)())func)
20
+ #endif
21
+
22
+
23
+ #define RB_DEF_S_METHOD(klass,method,func,argtype) \
24
+ rb_define_singleton_method(klass,method,RUBY_METHOD_FUNC(func), argtype)
25
+
26
+ #define RB_DEF_API_METHOD(name,argtype) \
27
+ RB_DEF_S_METHOD(rb_mAPI,#name,RUBY_METHOD_FUNC(rb_##name), argtype)
28
+
29
+ #define RB_DEF_METHOD(klass,method,func,argtype) \
30
+ rb_define_method(klass,method,RUBY_METHOD_FUNC(func), argtype)
31
+
32
+ VALUE
33
+ rb_getWin32Error()
34
+ {
35
+ DWORD e = GetLastError();
36
+ LPVOID lpMsgBuf;
37
+ if (!FormatMessage(
38
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
39
+ FORMAT_MESSAGE_FROM_SYSTEM |
40
+ FORMAT_MESSAGE_IGNORE_INSERTS,
41
+ NULL,
42
+ GetLastError(),
43
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
44
+ (LPTSTR) &lpMsgBuf,
45
+ 0,
46
+ NULL ))
47
+ {
48
+ // Handle the error.
49
+ return Qnil;
50
+ }
51
+
52
+ VALUE t = rb_str_new2( (LPCTSTR) lpMsgBuf );
53
+
54
+ // Free the buffer.
55
+ LocalFree( lpMsgBuf );
56
+
57
+ // Raise exception
58
+ rb_raise(rb_eRuntimeError, RSTRING(t)->ptr);
59
+ return Qnil;
60
+
61
+ }
62
+
63
+
64
+ extern "C"
65
+ {
66
+
67
+ static VALUE rb_GetStdHandle(VALUE self, VALUE handle)
68
+ {
69
+ unsigned long x;
70
+ if ( FIXNUM_P( handle ) )
71
+ {
72
+ x = NUM2ULONG( handle );
73
+ }
74
+ else
75
+ {
76
+ Check_Type( handle, T_BIGNUM );
77
+ x = rb_big2ulong(handle);
78
+ }
79
+ unsigned long h = PtrToUlong( GetStdHandle( x ) );
80
+ return ULONG2NUM(h);
81
+ }
82
+
83
+
84
+ static VALUE rb_AllocConsole(VALUE self)
85
+ {
86
+ if (AllocConsole()) return INT2FIX(1);
87
+ return rb_getWin32Error();
88
+ }
89
+
90
+
91
+ static VALUE rb_FreeConsole(VALUE self)
92
+ {
93
+ if (FreeConsole()) return INT2FIX(1);
94
+ return rb_getWin32Error();
95
+ }
96
+
97
+ static VALUE rb_GenerateConsoleCtrlEvent(VALUE self, VALUE event, VALUE pgid)
98
+ {
99
+ unsigned int e = NUM2UINT(event);
100
+ if ( e != CTRL_C_EVENT && e != CTRL_BREAK_EVENT )
101
+ rb_raise(rb_eArgError, "Wrong event: only CTRL_C_EVENT or "
102
+ "CTRL_BREAK_EVENT accepted.");
103
+ if ( GenerateConsoleCtrlEvent(e, NUM2UINT(pgid)) )
104
+ return INT2FIX(1);
105
+ return rb_getWin32Error();
106
+ }
107
+
108
+ static VALUE rb_GetConsoleMode(VALUE self, VALUE hConsoleOutput)
109
+ {
110
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
111
+ DWORD mode;
112
+ if (GetConsoleMode(handle,&mode))
113
+ return UINT2NUM(mode);
114
+ return rb_getWin32Error();
115
+ }
116
+
117
+ static VALUE rb_GetConsoleTitle(VALUE self)
118
+ {
119
+ char title[1024];
120
+ if (GetConsoleTitle((char*)&title,1024))
121
+ return rb_str_new2( title );
122
+ return rb_getWin32Error();
123
+ }
124
+
125
+
126
+
127
+
128
+ static VALUE rb_GetNumberOfConsoleMouseButtons( VALUE self )
129
+ {
130
+ DWORD mb;
131
+ if (GetNumberOfConsoleMouseButtons( &mb ))
132
+ return INT2FIX(mb);
133
+ return rb_getWin32Error();
134
+ }
135
+
136
+
137
+
138
+ static VALUE rb_GetNumberOfConsoleInputEvents( VALUE self, VALUE hConsoleOutput )
139
+ {
140
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
141
+ DWORD events;
142
+ if (GetNumberOfConsoleInputEvents(handle, &events))
143
+ return INT2FIX(events);
144
+ return rb_getWin32Error();
145
+ }
146
+
147
+
148
+ static VALUE
149
+ rb_CreateConsoleScreenBuffer( VALUE self, VALUE dwDesiredAccess,
150
+ VALUE dwShareMode, VALUE dwFlags )
151
+ {
152
+ if (CreateConsoleScreenBuffer( NUM2UINT(dwDesiredAccess),
153
+ NUM2UINT( dwShareMode),
154
+ NULL,
155
+ NUM2UINT( dwFlags),
156
+ NULL
157
+ ))
158
+ return INT2FIX(1);
159
+ return rb_getWin32Error();
160
+ }
161
+
162
+
163
+ static VALUE rb_GetConsoleCP( VALUE self )
164
+ {
165
+ unsigned int h = GetConsoleCP();
166
+ return UINT2NUM(h);
167
+ }
168
+
169
+ static VALUE rb_GetConsoleOutputCP( VALUE self )
170
+ {
171
+ unsigned int h = GetConsoleOutputCP();
172
+ return UINT2NUM(h);
173
+ }
174
+
175
+ static VALUE rb_SetConsoleMode( VALUE self, VALUE hConsoleOutput,
176
+ VALUE Mode )
177
+ {
178
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
179
+ if ( SetConsoleMode( handle, NUM2UINT( Mode ) ) )
180
+ return INT2FIX(1);
181
+ return rb_getWin32Error();
182
+ }
183
+
184
+ static VALUE rb_SetConsoleCP( VALUE self, VALUE wCodePageID )
185
+ {
186
+ if ( SetConsoleCP( NUM2UINT( wCodePageID ) ) )
187
+ return INT2FIX(1);
188
+ return rb_getWin32Error();
189
+ }
190
+
191
+ static VALUE rb_SetConsoleOutputCP( VALUE self, VALUE wCodePageID )
192
+ {
193
+ if ( SetConsoleOutputCP( NUM2UINT( wCodePageID ) ) )
194
+ return INT2FIX(1);
195
+ return rb_getWin32Error();
196
+ }
197
+
198
+ static VALUE rb_GetConsoleWindow( VALUE self )
199
+ {
200
+ unsigned long h = PtrToUlong( GetConsoleOutputCP() );
201
+ return ULONG2NUM(h);
202
+ }
203
+
204
+ static VALUE rb_WriteConsole( VALUE self, VALUE hConsoleOutput,
205
+ VALUE lpBuffer )
206
+ {
207
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
208
+ DWORD nNumberOfCharsToWrite = RSTRING(lpBuffer)->len;
209
+
210
+ DWORD lpNumberOfCharsWritten;
211
+
212
+ WriteConsole( handle, RSTRING(lpBuffer)->ptr,
213
+ nNumberOfCharsToWrite,
214
+ &lpNumberOfCharsWritten, NULL );
215
+ return UINT2NUM( lpNumberOfCharsWritten );
216
+ }
217
+
218
+
219
+ static VALUE rb_GetLargestConsoleWindowSize( VALUE self, VALUE hConsoleOutput )
220
+ {
221
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
222
+ COORD size = GetLargestConsoleWindowSize( handle);
223
+
224
+ VALUE ret = rb_ary_new();
225
+ rb_ary_push( ret, UINT2NUM( size.X ) );
226
+ rb_ary_push( ret, UINT2NUM( size.Y ) );
227
+ return ret;
228
+ }
229
+
230
+ static VALUE rb_GetConsoleCursorInfo( VALUE self, VALUE hConsoleOutput )
231
+ {
232
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
233
+
234
+ CONSOLE_CURSOR_INFO out;
235
+ if ( !GetConsoleCursorInfo( handle, &out ) )
236
+ return rb_getWin32Error();
237
+
238
+ VALUE ret = rb_ary_new();
239
+ rb_ary_push( ret, UINT2NUM( out.dwSize ) );
240
+ rb_ary_push( ret, UINT2NUM( out.bVisible ) );
241
+ return ret;
242
+ }
243
+
244
+ void rb_ParseEvent(VALUE ret, INPUT_RECORD& event )
245
+ {
246
+ switch(event.EventType) {
247
+ case KEY_EVENT:
248
+ {
249
+ KEY_EVENT_RECORD* kevent=(KEY_EVENT_RECORD *)&(event.Event);
250
+ rb_ary_push(ret, UINT2NUM(KEY_EVENT));
251
+ rb_ary_push(ret, UINT2NUM(kevent->bKeyDown));
252
+ rb_ary_push(ret, UINT2NUM(kevent->wRepeatCount));
253
+ rb_ary_push(ret, UINT2NUM(kevent->wVirtualKeyCode));
254
+ rb_ary_push(ret, UINT2NUM(kevent->wVirtualScanCode));
255
+ #ifdef UNICODE
256
+ rb_ary_push(ret, UINT2NUM(kevent->uChar.UnicodeChar));
257
+ #else
258
+ rb_ary_push(ret, UINT2NUM(kevent->uChar.AsciiChar));
259
+ #endif
260
+ rb_ary_push(ret, UINT2NUM(kevent->dwControlKeyState));
261
+ break;
262
+ }
263
+ case MOUSE_EVENT:
264
+ {
265
+ MOUSE_EVENT_RECORD * mevent=(MOUSE_EVENT_RECORD *)&(event.Event);
266
+ rb_ary_push(ret, UINT2NUM(MOUSE_EVENT) );
267
+ rb_ary_push(ret, UINT2NUM(mevent->dwMousePosition.X) );
268
+ rb_ary_push(ret, UINT2NUM(mevent->dwMousePosition.Y) );
269
+ rb_ary_push(ret, UINT2NUM(mevent->dwButtonState) );
270
+ rb_ary_push(ret, UINT2NUM(mevent->dwControlKeyState) );
271
+ rb_ary_push(ret, UINT2NUM(mevent->dwEventFlags) );
272
+ break;
273
+ }
274
+ case WINDOW_BUFFER_SIZE_EVENT:
275
+ {
276
+ WINDOW_BUFFER_SIZE_RECORD* wevent=
277
+ (WINDOW_BUFFER_SIZE_RECORD *)&(event.Event);
278
+ rb_ary_push(ret, UINT2NUM(WINDOW_BUFFER_SIZE_EVENT) );
279
+ rb_ary_push(ret, UINT2NUM(wevent->dwSize.X) );
280
+ rb_ary_push(ret, UINT2NUM(wevent->dwSize.Y) );
281
+ }
282
+ break;
283
+ case MENU_EVENT:
284
+ {
285
+ MENU_EVENT_RECORD* mevent= (MENU_EVENT_RECORD *)&(event.Event);
286
+ rb_ary_push(ret, UINT2NUM(MENU_EVENT) );
287
+ rb_ary_push(ret, UINT2NUM(mevent->dwCommandId) );
288
+ }
289
+ break;
290
+ case FOCUS_EVENT:
291
+ {
292
+ FOCUS_EVENT_RECORD* mevent= (FOCUS_EVENT_RECORD *)&(event.Event);
293
+ rb_ary_push(ret, UINT2NUM(FOCUS_EVENT) );
294
+ rb_ary_push(ret, UINT2NUM(mevent->bSetFocus) );
295
+ }
296
+ break;
297
+ }
298
+ }
299
+
300
+ static VALUE rb_PeekConsoleInput( VALUE self, VALUE hConsoleOutput )
301
+ {
302
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
303
+
304
+ DWORD nofread;
305
+ INPUT_RECORD event;
306
+ if (!PeekConsoleInput(handle,&event,1,&nofread))
307
+ return rb_getWin32Error();
308
+
309
+ VALUE ret = rb_ary_new();
310
+ rb_ParseEvent( ret, event );
311
+ return ret;
312
+ }
313
+
314
+ static VALUE rb_ReadConsole( VALUE self, VALUE hConsoleOutput,
315
+ VALUE buffer, VALUE numread )
316
+ {
317
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
318
+ DWORD nofread;
319
+ Check_Type( buffer, T_STRING );
320
+ int to_read = NUM2INT(numread);
321
+ if ( RSTRING(buffer)->len > to_read )
322
+ rb_raise(rb_eArgError, "String is too small to read that many characters.");
323
+ if (ReadConsole(handle,(void *)RSTRING(buffer)->ptr, to_read,
324
+ &nofread,NULL))
325
+ return UINT2NUM(nofread);
326
+ return rb_getWin32Error();
327
+ }
328
+
329
+ static VALUE rb_ReadConsoleInput( VALUE self, VALUE hConsoleOutput )
330
+ {
331
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
332
+ DWORD nofread;
333
+ INPUT_RECORD event;
334
+ if (!ReadConsoleInput(handle,&event,1,&nofread))
335
+ return rb_getWin32Error();
336
+
337
+ VALUE ret = rb_ary_new();
338
+ rb_ParseEvent( ret, event );
339
+ return ret;
340
+ }
341
+
342
+
343
+
344
+ static VALUE rb_ReadConsoleOutputCharacter( VALUE self, VALUE hConsoleOutput,
345
+ VALUE charbuf, VALUE len,
346
+ VALUE x, VALUE y )
347
+ {
348
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
349
+ COORD coords;
350
+ DWORD nofread;
351
+ coords.X= NUM2UINT( x );
352
+ coords.Y= NUM2UINT( y );
353
+ int l = NUM2INT(len);
354
+ if ( RSTRING(charbuf)->len < l*sizeof(TCHAR) )
355
+ rb_raise(rb_eArgError, "String is too small to read that many characters.");
356
+ if (ReadConsoleOutputCharacter(handle,RSTRING(charbuf)->ptr,l,
357
+ coords,&nofread))
358
+ return UINT2NUM( nofread );
359
+ return rb_getWin32Error();
360
+ }
361
+
362
+
363
+ static VALUE rb_ReadConsoleOutputAttribute( VALUE self, VALUE hConsoleOutput,
364
+ VALUE len, VALUE x, VALUE y )
365
+ {
366
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
367
+ COORD coords;
368
+ DWORD nofread;
369
+ unsigned short abuffer[80*999*sizeof(unsigned short)];
370
+ char cbuffer[80*999];
371
+ coords.X= NUM2UINT( x );
372
+ coords.Y= NUM2UINT( y );
373
+ if (ReadConsoleOutputAttribute(handle, abuffer, NUM2UINT(len),
374
+ coords,&nofread))
375
+ {
376
+ for(unsigned i=0;i<nofread;++i) {
377
+ cbuffer[i]=(char)abuffer[i];
378
+ }
379
+ return rb_str_new( cbuffer, nofread );
380
+ }
381
+ return rb_getWin32Error();
382
+ }
383
+
384
+
385
+ static VALUE rb_ReadConsoleOutput( VALUE self, VALUE hConsoleOutput,
386
+ VALUE buffer, VALUE srcwid, VALUE srcht,
387
+ VALUE startx, VALUE starty,
388
+ VALUE l, VALUE t, VALUE r, VALUE b )
389
+ {
390
+ COORD coords;
391
+ COORD size;
392
+ SMALL_RECT from;
393
+ size.X= NUM2UINT( srcwid );
394
+ size.Y= NUM2UINT( srcht );
395
+ coords.X= NUM2INT( startx );
396
+ coords.Y= NUM2INT( starty );
397
+ from.Left = NUM2INT( l );
398
+ from.Top = NUM2INT( t );
399
+ from.Right = NUM2INT( r );
400
+ from.Bottom = NUM2INT( b );
401
+ Check_Type( buffer, T_STRING );
402
+ if ( RSTRING(buffer)->len < (sizeof(CHAR_INFO)*size.X*size.Y) )
403
+ rb_raise(rb_eArgError, "string buffer is too small for reading that many characters.");
404
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
405
+ if (!ReadConsoleOutput(handle,(CHAR_INFO *)RSTRING(buffer)->ptr,size,coords,&from))
406
+ return rb_getWin32Error();
407
+
408
+ VALUE ret = rb_ary_new();
409
+ rb_ary_push( ret, INT2FIX(from.Left) );
410
+ rb_ary_push( ret, INT2FIX(from.Top) );
411
+ rb_ary_push( ret, INT2FIX(from.Right) );
412
+ rb_ary_push( ret, INT2FIX(from.Bottom) );
413
+ return ret;
414
+ }
415
+
416
+
417
+
418
+ static VALUE rb_GetConsoleScreenBufferInfo( VALUE self, VALUE hConsoleOutput )
419
+ {
420
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
421
+
422
+ CONSOLE_SCREEN_BUFFER_INFO out;
423
+
424
+ if ( !GetConsoleScreenBufferInfo( handle, &out ) )
425
+ return rb_getWin32Error();
426
+
427
+ VALUE ret = rb_ary_new();
428
+ rb_ary_push( ret, UINT2NUM( out.dwSize.X ) );
429
+ rb_ary_push( ret, UINT2NUM( out.dwSize.Y ) );
430
+ rb_ary_push( ret, UINT2NUM( out.dwCursorPosition.X ) );
431
+ rb_ary_push( ret, UINT2NUM( out.dwCursorPosition.Y ) );
432
+
433
+ rb_ary_push( ret, UINT2NUM( out.wAttributes ) );
434
+
435
+ rb_ary_push( ret, INT2FIX( out.srWindow.Left ) );
436
+ rb_ary_push( ret, INT2FIX( out.srWindow.Top ) );
437
+ rb_ary_push( ret, INT2FIX( out.srWindow.Right ) );
438
+ rb_ary_push( ret, INT2FIX( out.srWindow.Bottom ) );
439
+
440
+ rb_ary_push( ret, UINT2NUM( out.dwMaximumWindowSize.X ) );
441
+ rb_ary_push( ret, UINT2NUM( out.dwMaximumWindowSize.Y ) );
442
+
443
+
444
+ return ret;
445
+ }
446
+
447
+
448
+ #define strEQ(x,y) strcmp(x,y) == 0
449
+
450
+ DWORD c_constant(char *name)
451
+ {
452
+ switch (*name) {
453
+ case 'A':
454
+ break;
455
+ case 'B':
456
+ if (strEQ(name, "BACKGROUND_BLUE"))
457
+ #ifdef BACKGROUND_BLUE
458
+ return BACKGROUND_BLUE;
459
+ #else
460
+ goto not_there;
461
+ #endif
462
+ if (strEQ(name, "BACKGROUND_GREEN"))
463
+ #ifdef BACKGROUND_GREEN
464
+ return BACKGROUND_GREEN;
465
+ #else
466
+ goto not_there;
467
+ #endif
468
+ if (strEQ(name, "BACKGROUND_INTENSITY"))
469
+ #ifdef BACKGROUND_INTENSITY
470
+ return BACKGROUND_INTENSITY;
471
+ #else
472
+ goto not_there;
473
+ #endif
474
+ if (strEQ(name, "BACKGROUND_RED"))
475
+ #ifdef BACKGROUND_RED
476
+ return BACKGROUND_RED;
477
+ #else
478
+ goto not_there;
479
+ #endif
480
+ break;
481
+ case 'C':
482
+ if (strEQ(name, "CAPSLOCK_ON"))
483
+ #ifdef CAPSLOCK_ON
484
+ return CAPSLOCK_ON;
485
+ #else
486
+ goto not_there;
487
+ #endif
488
+ if (strEQ(name, "CONSOLE_TEXTMODE_BUFFER"))
489
+ #ifdef CONSOLE_TEXTMODE_BUFFER
490
+ return CONSOLE_TEXTMODE_BUFFER;
491
+ #else
492
+ goto not_there;
493
+ #endif
494
+ if (strEQ(name, "CTRL_BREAK_EVENT"))
495
+ #ifdef CTRL_BREAK_EVENT
496
+ return CTRL_BREAK_EVENT;
497
+ #else
498
+ goto not_there;
499
+ #endif
500
+ if (strEQ(name, "CTRL_C_EVENT"))
501
+ #ifdef CTRL_C_EVENT
502
+ return CTRL_C_EVENT;
503
+ #else
504
+ goto not_there;
505
+ #endif
506
+ break;
507
+
508
+ case 'D':
509
+ break;
510
+ case 'E':
511
+ if (strEQ(name, "ENABLE_ECHO_INPUT"))
512
+ #ifdef ENABLE_ECHO_INPUT
513
+ return ENABLE_ECHO_INPUT;
514
+ #else
515
+ goto not_there;
516
+ #endif
517
+ if (strEQ(name, "ENABLE_LINE_INPUT"))
518
+ #ifdef ENABLE_LINE_INPUT
519
+ return ENABLE_LINE_INPUT;
520
+ #else
521
+ goto not_there;
522
+ #endif
523
+ if (strEQ(name, "ENABLE_MOUSE_INPUT"))
524
+ #ifdef ENABLE_MOUSE_INPUT
525
+ return ENABLE_MOUSE_INPUT;
526
+ #else
527
+ goto not_there;
528
+ #endif
529
+ if (strEQ(name, "ENABLE_PROCESSED_INPUT"))
530
+ #ifdef ENABLE_PROCESSED_INPUT
531
+ return ENABLE_PROCESSED_INPUT;
532
+ #else
533
+ goto not_there;
534
+ #endif
535
+ if (strEQ(name, "ENABLE_PROCESSED_OUTPUT"))
536
+ #ifdef ENABLE_PROCESSED_OUTPUT
537
+ return ENABLE_PROCESSED_OUTPUT;
538
+ #else
539
+ goto not_there;
540
+ #endif
541
+ if (strEQ(name, "ENABLE_WINDOW_INPUT"))
542
+ #ifdef ENABLE_WINDOW_INPUT
543
+ return ENABLE_WINDOW_INPUT;
544
+ #else
545
+ goto not_there;
546
+ #endif
547
+ if (strEQ(name, "ENABLE_WRAP_AT_EOL_OUTPUT"))
548
+ #ifdef ENABLE_WRAP_AT_EOL_OUTPUT
549
+ return ENABLE_WRAP_AT_EOL_OUTPUT;
550
+ #else
551
+ goto not_there;
552
+ #endif
553
+ if (strEQ(name, "ENHANCED_KEY"))
554
+ #ifdef ENHANCED_KEY
555
+ return ENHANCED_KEY;
556
+ #else
557
+ goto not_there;
558
+ #endif
559
+ break;
560
+ case 'F':
561
+ if (strEQ(name, "FILE_SHARE_READ"))
562
+ #ifdef FILE_SHARE_READ
563
+ return FILE_SHARE_READ;
564
+ #else
565
+ goto not_there;
566
+ #endif
567
+ if (strEQ(name, "FILE_SHARE_WRITE"))
568
+ #ifdef FILE_SHARE_WRITE
569
+ return FILE_SHARE_WRITE;
570
+ #else
571
+ goto not_there;
572
+ #endif
573
+ if (strEQ(name, "FOREGROUND_BLUE"))
574
+ #ifdef FOREGROUND_BLUE
575
+ return FOREGROUND_BLUE;
576
+ #else
577
+ goto not_there;
578
+ #endif
579
+ if (strEQ(name, "FOREGROUND_GREEN"))
580
+ #ifdef FOREGROUND_GREEN
581
+ return FOREGROUND_GREEN;
582
+ #else
583
+ goto not_there;
584
+ #endif
585
+ if (strEQ(name, "FOREGROUND_INTENSITY"))
586
+ #ifdef FOREGROUND_INTENSITY
587
+ return FOREGROUND_INTENSITY;
588
+ #else
589
+ goto not_there;
590
+ #endif
591
+ if (strEQ(name, "FOREGROUND_RED"))
592
+ #ifdef FOREGROUND_RED
593
+ return FOREGROUND_RED;
594
+ #else
595
+ goto not_there;
596
+ #endif
597
+ break;
598
+ case 'G':
599
+ if (strEQ(name, "GENERIC_READ"))
600
+ #ifdef GENERIC_READ
601
+ return GENERIC_READ;
602
+ #else
603
+ goto not_there;
604
+ #endif
605
+ if (strEQ(name, "GENERIC_WRITE"))
606
+ #ifdef GENERIC_WRITE
607
+ return GENERIC_WRITE;
608
+ #else
609
+ goto not_there;
610
+ #endif
611
+ break;
612
+ case 'H':
613
+ break;
614
+ case 'I':
615
+ break;
616
+ case 'J':
617
+ break;
618
+ case 'K':
619
+ if (strEQ(name, "KEY_EVENT"))
620
+ #ifdef KEY_EVENT
621
+ return KEY_EVENT;
622
+ #else
623
+ goto not_there;
624
+ #endif
625
+ break;
626
+ case 'L':
627
+ if (strEQ(name, "LEFT_ALT_PRESSED"))
628
+ #ifdef LEFT_ALT_PRESSED
629
+ return LEFT_ALT_PRESSED;
630
+ #else
631
+ goto not_there;
632
+ #endif
633
+ if (strEQ(name, "LEFT_CTRL_PRESSED"))
634
+ #ifdef LEFT_CTRL_PRESSED
635
+ return LEFT_CTRL_PRESSED;
636
+ #else
637
+ goto not_there;
638
+ #endif
639
+ break;
640
+ case 'M':
641
+ break;
642
+ case 'N':
643
+ if (strEQ(name, "NUMLOCK_ON"))
644
+ #ifdef NUMLOCK_ON
645
+ return NUMLOCK_ON;
646
+ #else
647
+ goto not_there;
648
+ #endif
649
+ break;
650
+ case 'O':
651
+ break;
652
+ case 'P':
653
+ break;
654
+ case 'Q':
655
+ break;
656
+ case 'R':
657
+ if (strEQ(name, "RIGHT_ALT_PRESSED"))
658
+ #ifdef RIGHT_ALT_PRESSED
659
+ return RIGHT_ALT_PRESSED;
660
+ #else
661
+ goto not_there;
662
+ #endif
663
+ if (strEQ(name, "RIGHT_CTRL_PRESSED"))
664
+ #ifdef RIGHT_CTRL_PRESSED
665
+ return RIGHT_CTRL_PRESSED;
666
+ #else
667
+ goto not_there;
668
+ #endif
669
+ break;
670
+ case 'S':
671
+ if (strEQ(name, "SCROLLLOCK_ON"))
672
+ #ifdef SCROLLLOCK_ON
673
+ return SCROLLLOCK_ON;
674
+ #else
675
+ goto not_there;
676
+ #endif
677
+ if (strEQ(name, "SHIFT_PRESSED"))
678
+ #ifdef SHIFT_PRESSED
679
+ return SHIFT_PRESSED;
680
+ #else
681
+ goto not_there;
682
+ #endif
683
+ if (strEQ(name, "STD_ERROR_HANDLE"))
684
+ #ifdef STD_ERROR_HANDLE
685
+ return STD_ERROR_HANDLE;
686
+ #else
687
+ goto not_there;
688
+ #endif
689
+ if (strEQ(name, "STD_INPUT_HANDLE"))
690
+ #ifdef STD_INPUT_HANDLE
691
+ return STD_INPUT_HANDLE;
692
+ #else
693
+ goto not_there;
694
+ #endif
695
+ if (strEQ(name, "STD_OUTPUT_HANDLE"))
696
+ #ifdef STD_OUTPUT_HANDLE
697
+ return STD_OUTPUT_HANDLE;
698
+ #else
699
+ goto not_there;
700
+ #endif
701
+ break;
702
+ case 'T':
703
+ break;
704
+ case 'U':
705
+ break;
706
+ case 'V':
707
+ break;
708
+ case 'W':
709
+ break;
710
+ case 'X':
711
+ break;
712
+ case 'Y':
713
+ break;
714
+ case 'Z':
715
+ break;
716
+ }
717
+ rb_raise(rb_eArgError, "Not such constant.");
718
+ return 0;
719
+
720
+ not_there:
721
+ rb_raise(rb_eArgError, "Not defined.");
722
+ return 0;
723
+ }
724
+
725
+ VALUE rb_constant( VALUE self, VALUE name )
726
+ {
727
+ Check_Type( name, T_STRING );
728
+ return ULONG2NUM( c_constant( RSTRING(name)->ptr ) );
729
+ }
730
+
731
+
732
+ void define_constants()
733
+ {
734
+ #define DEF_SELF_CONST(NAME) \
735
+ rb_define_const(rb_mConstants, #NAME, ULONG2NUM( (ULONG)NAME ) );
736
+
737
+ DEF_SELF_CONST( STD_INPUT_HANDLE );
738
+ DEF_SELF_CONST( STD_OUTPUT_HANDLE );
739
+ DEF_SELF_CONST( STD_ERROR_HANDLE );
740
+ DEF_SELF_CONST( INVALID_HANDLE_VALUE );
741
+ DEF_SELF_CONST( GENERIC_READ );
742
+ DEF_SELF_CONST( GENERIC_WRITE );
743
+ DEF_SELF_CONST( FILE_SHARE_READ );
744
+ DEF_SELF_CONST( FILE_SHARE_WRITE );
745
+ DEF_SELF_CONST( CONSOLE_TEXTMODE_BUFFER );
746
+
747
+ DEF_SELF_CONST( FOREGROUND_BLUE );
748
+ DEF_SELF_CONST( FOREGROUND_GREEN );
749
+ DEF_SELF_CONST( FOREGROUND_RED );
750
+ DEF_SELF_CONST( FOREGROUND_INTENSITY );
751
+ DEF_SELF_CONST( BACKGROUND_BLUE );
752
+ DEF_SELF_CONST( BACKGROUND_GREEN );
753
+ DEF_SELF_CONST( BACKGROUND_RED );
754
+ DEF_SELF_CONST( BACKGROUND_INTENSITY );
755
+
756
+ DEF_SELF_CONST( ENABLE_PROCESSED_INPUT );
757
+ DEF_SELF_CONST( ENABLE_LINE_INPUT );
758
+ DEF_SELF_CONST( ENABLE_ECHO_INPUT );
759
+ DEF_SELF_CONST( ENABLE_WINDOW_INPUT );
760
+ DEF_SELF_CONST( ENABLE_MOUSE_INPUT );
761
+ DEF_SELF_CONST( ENABLE_PROCESSED_OUTPUT );
762
+ DEF_SELF_CONST( ENABLE_WRAP_AT_EOL_OUTPUT );
763
+
764
+ DEF_SELF_CONST( KEY_EVENT );
765
+ DEF_SELF_CONST( MOUSE_EVENT );
766
+ DEF_SELF_CONST( WINDOW_BUFFER_SIZE_EVENT );
767
+ DEF_SELF_CONST( MENU_EVENT );
768
+ DEF_SELF_CONST( FOCUS_EVENT );
769
+
770
+ DEF_SELF_CONST( CAPSLOCK_ON );
771
+ DEF_SELF_CONST( ENHANCED_KEY );
772
+ DEF_SELF_CONST( NUMLOCK_ON );
773
+ DEF_SELF_CONST( SHIFT_PRESSED );
774
+ DEF_SELF_CONST( LEFT_CTRL_PRESSED );
775
+ DEF_SELF_CONST( RIGHT_CTRL_PRESSED );
776
+ DEF_SELF_CONST( LEFT_ALT_PRESSED );
777
+ DEF_SELF_CONST( RIGHT_ALT_PRESSED );
778
+ DEF_SELF_CONST( SCROLLLOCK_ON );
779
+
780
+ DEF_SELF_CONST( MOUSE_WHEELED );
781
+ DEF_SELF_CONST( DOUBLE_CLICK );
782
+ DEF_SELF_CONST( MOUSE_MOVED );
783
+
784
+ DEF_SELF_CONST( FROM_LEFT_1ST_BUTTON_PRESSED );
785
+ DEF_SELF_CONST( FROM_LEFT_2ND_BUTTON_PRESSED );
786
+ DEF_SELF_CONST( FROM_LEFT_3RD_BUTTON_PRESSED );
787
+ DEF_SELF_CONST( FROM_LEFT_4TH_BUTTON_PRESSED );
788
+ DEF_SELF_CONST( RIGHTMOST_BUTTON_PRESSED );
789
+
790
+ DEF_SELF_CONST( CTRL_C_EVENT );
791
+ DEF_SELF_CONST( CTRL_BREAK_EVENT );
792
+ DEF_SELF_CONST( CTRL_CLOSE_EVENT );
793
+ DEF_SELF_CONST( CTRL_LOGOFF_EVENT );
794
+ DEF_SELF_CONST( CTRL_SHUTDOWN_EVENT );
795
+ }
796
+
797
+
798
+ VALUE
799
+ rb_FillConsoleOutputAttribute( VALUE self, VALUE hConsoleOutput,
800
+ VALUE wAttribute, VALUE nLength,
801
+ VALUE col, VALUE row )
802
+ {
803
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
804
+
805
+ COORD dwWriteCoord;
806
+ dwWriteCoord.X = NUM2UINT(col);
807
+ dwWriteCoord.Y = NUM2UINT(row);
808
+ DWORD numChars;
809
+ if (FillConsoleOutputAttribute( handle, NUM2UINT(wAttribute),
810
+ NUM2ULONG(nLength), dwWriteCoord,
811
+ &numChars ))
812
+ return ULONG2NUM(numChars);
813
+ return rb_getWin32Error();
814
+ }
815
+
816
+ VALUE
817
+ rb_SetConsoleScreenBufferSize( VALUE self, VALUE hConsoleOutput,
818
+ VALUE x, VALUE y )
819
+ {
820
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
821
+ COORD size;
822
+ size.X=NUM2UINT(x);
823
+ size.Y=NUM2UINT(y);
824
+ if (SetConsoleScreenBufferSize(handle, size))
825
+ return NUM2UINT(1);
826
+ return rb_getWin32Error();
827
+ }
828
+
829
+ VALUE
830
+ rb_SetConsoleTitle( VALUE self, VALUE title )
831
+ {
832
+ Check_Type( title, T_STRING );
833
+ if (SetConsoleTitle(RSTRING( title )->ptr))
834
+ return NUM2UINT(1);
835
+ return rb_getWin32Error();
836
+ }
837
+
838
+ VALUE
839
+ rb_SetStdHandle( VALUE self, VALUE fd, VALUE hConsoleOutput )
840
+ {
841
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
842
+ if (SetStdHandle(NUM2UINT(fd), handle))
843
+ return NUM2UINT(1);
844
+ return rb_getWin32Error();
845
+ }
846
+
847
+ VALUE
848
+ rb_SetConsoleWindowInfo( VALUE self, VALUE hConsoleOutput, VALUE bAbsolute,
849
+ VALUE left, VALUE top, VALUE right, VALUE bottom )
850
+ {
851
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
852
+
853
+ SMALL_RECT rect;
854
+ rect.Left = NUM2INT( left );
855
+ rect.Top = NUM2INT( top );
856
+ rect.Right = NUM2INT( right );
857
+ rect.Bottom = NUM2INT( bottom );
858
+ if (SetConsoleWindowInfo( handle, NUM2INT( bAbsolute ), &rect ))
859
+ return UINT2NUM(1);
860
+ return rb_getWin32Error();
861
+ }
862
+
863
+
864
+
865
+ VALUE
866
+ rb_SetConsoleCursorPosition( VALUE self, VALUE hConsoleOutput,
867
+ VALUE col, VALUE row )
868
+ {
869
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
870
+
871
+ COORD dwWriteCoord;
872
+ dwWriteCoord.X = NUM2UINT(col);
873
+ dwWriteCoord.Y = NUM2UINT(row);
874
+ // Cannot call rb_getWin32Error as this function fails when
875
+ // setting cursor to last column/row.
876
+ if ( !SetConsoleCursorPosition( handle, dwWriteCoord ) )
877
+ return Qnil;
878
+ return INT2FIX(1);
879
+ }
880
+
881
+ VALUE
882
+ rb_SetConsoleCursorInfo( VALUE self, VALUE hConsoleOutput,
883
+ VALUE size, VALUE visib )
884
+ {
885
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
886
+ CONSOLE_CURSOR_INFO c;
887
+ c.dwSize = NUM2UINT(size);
888
+ c.bVisible = NUM2UINT(visib);
889
+ if ( !SetConsoleCursorInfo( handle, &c ) )
890
+ return rb_getWin32Error();
891
+ return INT2FIX(1);
892
+ }
893
+
894
+
895
+
896
+ VALUE
897
+ rb_SetConsoleActiveScreenBuffer( VALUE self, VALUE hConsoleOutput )
898
+ {
899
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
900
+
901
+ if ( !SetConsoleActiveScreenBuffer( handle ) )
902
+ return rb_getWin32Error();
903
+ return INT2FIX(1);
904
+ }
905
+
906
+ VALUE
907
+ rb_SetConsoleTextAttribute( VALUE self, VALUE hConsoleOutput,
908
+ VALUE wAttributes )
909
+ {
910
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
911
+
912
+ if ( !SetConsoleTextAttribute( handle, NUM2UINT(wAttributes) ) )
913
+ return Qnil; // no getWin32Error to allow piping/redirecting
914
+ return INT2FIX(1);
915
+ }
916
+
917
+
918
+
919
+ VALUE
920
+ rb_ScrollConsoleScreenBuffer( VALUE self, VALUE hConsoleOutput, VALUE left1,
921
+ VALUE top1, VALUE right1, VALUE bottom1,
922
+ VALUE col, VALUE row, VALUE cChar, VALUE attr,
923
+ VALUE left2, VALUE top2, VALUE right2,
924
+ VALUE bottom2)
925
+ {
926
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
927
+
928
+ SMALL_RECT scroll, clip;
929
+ scroll.Left = NUM2INT( left1 );
930
+ scroll.Right = NUM2INT( right1 );
931
+ scroll.Top = NUM2INT( top1 );
932
+ scroll.Bottom = NUM2INT( bottom1 );
933
+ clip.Left = NUM2INT( left2 );
934
+ clip.Right = NUM2INT( right2 );
935
+ clip.Top = NUM2INT( top2 );
936
+ clip.Bottom = NUM2INT( bottom2 );
937
+ CHAR_INFO fill;
938
+ #ifdef UNICODE
939
+ fill.Char.UnicodeChar = NUM2CHR( cChar );
940
+ #else
941
+ fill.Char.AsciiChar = NUM2CHR( cChar );
942
+ #endif
943
+ fill.Attributes = NUM2INT(attr);
944
+ COORD origin;
945
+ origin.X = NUM2UINT( col );
946
+ origin.Y = NUM2UINT( row );
947
+
948
+ if ( ScrollConsoleScreenBuffer( handle, &scroll, &clip, origin,
949
+ &fill ) )
950
+ return INT2FIX(1);
951
+ return rb_getWin32Error();
952
+ }
953
+
954
+
955
+ VALUE
956
+ rb_FillConsoleOutputCharacter( VALUE self, VALUE hConsoleOutput,
957
+ VALUE cCharacter, VALUE nLength,
958
+ VALUE col, VALUE row )
959
+ {
960
+ HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
961
+
962
+ COORD dwWriteCoord;
963
+ dwWriteCoord.X = NUM2UINT(col);
964
+ dwWriteCoord.Y = NUM2UINT(row);
965
+ DWORD numChars;
966
+ if (FillConsoleOutputCharacter( handle, NUM2CHR(cCharacter),
967
+ NUM2ULONG(nLength), dwWriteCoord,
968
+ &numChars ))
969
+ return ULONG2NUM(numChars);
970
+ return rb_getWin32Error();
971
+ }
972
+
973
+
974
+ VALUE
975
+ rb_WriteConsoleInput(int argc, VALUE *argv, VALUE self)
976
+ {
977
+ if (argc < 3)
978
+ rb_raise(rb_eArgError, "Wrong number of arguments.");
979
+
980
+ HANDLE handle = ULongToPtr( NUM2ULONG( argv[0] ) );
981
+ WORD type = NUM2INT( argv[1] );
982
+ DWORD written;
983
+ INPUT_RECORD event;
984
+ event.EventType = type;
985
+ switch(event.EventType) {
986
+ case KEY_EVENT:
987
+ {
988
+ KEY_EVENT_RECORD* kevent=(KEY_EVENT_RECORD *)&(event.Event);
989
+ kevent->bKeyDown=(BOOL)NUM2UINT( argv[2] );
990
+ kevent->wRepeatCount=NUM2UINT( argv[3] );
991
+ kevent->wVirtualKeyCode=NUM2UINT( argv[4] );
992
+ kevent->wVirtualScanCode=NUM2UINT( argv[5] );
993
+ #ifdef UNICODE
994
+ if (argc < 7)
995
+ rb_raise(rb_eArgError, "Wrong number of arguments.");
996
+ kevent->uChar.UnicodeChar=NUM2UINT( argv[6] );
997
+ #else
998
+ if (argc < 8)
999
+ rb_raise(rb_eArgError, "Wrong number of arguments.");
1000
+ kevent->uChar.AsciiChar=NUM2UINT( argv[7] );
1001
+ #endif
1002
+ break;
1003
+ }
1004
+ case MOUSE_EVENT:
1005
+ {
1006
+ if (argc < 7)
1007
+ rb_raise(rb_eArgError, "Wrong number of arguments.");
1008
+
1009
+ MOUSE_EVENT_RECORD* mevent=(MOUSE_EVENT_RECORD *)&(event.Event);
1010
+ mevent->dwMousePosition.X=NUM2UINT( argv[2] );
1011
+ mevent->dwMousePosition.Y=NUM2UINT( argv[3] );
1012
+ mevent->dwButtonState=NUM2UINT( argv[4] );
1013
+ mevent->dwControlKeyState=NUM2UINT( argv[5] );
1014
+ mevent->dwEventFlags=NUM2UINT( argv[6] );
1015
+ break;
1016
+ }
1017
+ case WINDOW_BUFFER_SIZE_EVENT:
1018
+ {
1019
+ if (argc < 4)
1020
+ rb_raise(rb_eArgError, "Wrong number of arguments.");
1021
+ WINDOW_BUFFER_SIZE_RECORD* mevent=
1022
+ (WINDOW_BUFFER_SIZE_RECORD *)&(event.Event);
1023
+ mevent->dwSize.X = NUM2UINT( argv[2] );
1024
+ mevent->dwSize.Y = NUM2UINT( argv[3] );
1025
+ }
1026
+ break;
1027
+ case MENU_EVENT:
1028
+ {
1029
+ if (argc < 3)
1030
+ rb_raise(rb_eArgError, "Wrong number of arguments.");
1031
+ MENU_EVENT_RECORD* mevent= (MENU_EVENT_RECORD *)&(event.Event);
1032
+ mevent->dwCommandId = argv[2];
1033
+ }
1034
+ break;
1035
+ case FOCUS_EVENT:
1036
+ {
1037
+ if (argc < 3)
1038
+ rb_raise(rb_eArgError, "Wrong number of arguments.");
1039
+ FOCUS_EVENT_RECORD* mevent= (FOCUS_EVENT_RECORD *)&(event.Event);
1040
+ mevent->bSetFocus = NUM2UINT( argv[2] );
1041
+ }
1042
+ default:
1043
+ rb_raise( rb_eArgError, "Unknown type of event.");
1044
+ break;
1045
+ }
1046
+ if (WriteConsoleInput(handle,&event,1,&written))
1047
+ return INT2FIX(1);
1048
+ return rb_getWin32Error();
1049
+ }
1050
+
1051
+ VALUE
1052
+ rb_WriteConsoleOutput(VALUE self, VALUE h, VALUE buffer,
1053
+ VALUE srcwid, VALUE srcht, VALUE startx,
1054
+ VALUE starty, VALUE l, VALUE t, VALUE r, VALUE b)
1055
+ {
1056
+ COORD coords;
1057
+ COORD size;
1058
+ SMALL_RECT to;
1059
+
1060
+ HANDLE handle = ULongToPtr( NUM2ULONG( h ) );
1061
+ Check_Type( buffer, T_STRING );
1062
+ size.X=NUM2UINT( srcwid );
1063
+ size.Y=NUM2UINT( srcht );
1064
+ coords.X=NUM2INT( startx );
1065
+ coords.Y=NUM2INT( starty );
1066
+ to.Left = NUM2INT( l );
1067
+ to.Top = NUM2INT( t );
1068
+ to.Right = NUM2INT( r );
1069
+ to.Bottom = NUM2INT( b );
1070
+ if (WriteConsoleOutput(handle,(CHAR_INFO *)RSTRING(buffer)->ptr,
1071
+ size,coords,&to)) {
1072
+ VALUE ret = rb_ary_new();
1073
+ rb_ary_push( ret, INT2FIX( to.Left ) );
1074
+ rb_ary_push( ret, INT2FIX( to.Top ) );
1075
+ rb_ary_push( ret, INT2FIX( to.Right ) );
1076
+ rb_ary_push( ret, INT2FIX( to.Bottom ) );
1077
+ return ret;
1078
+ }
1079
+ return rb_getWin32Error();
1080
+ }
1081
+
1082
+
1083
+ VALUE
1084
+ rb_WriteConsoleOutputAttribute(VALUE self, VALUE h, VALUE s,
1085
+ VALUE x, VALUE y)
1086
+ {
1087
+
1088
+ HANDLE handle = ULongToPtr( NUM2ULONG( h ) );
1089
+ Check_Type( s, T_STRING );
1090
+
1091
+ unsigned short buffer[80*999*sizeof(unsigned short)];
1092
+ DWORD written;
1093
+ DWORD towrite = RSTRING(s)->len;
1094
+ for(unsigned i=0; i<towrite; i++) {
1095
+ buffer[i] = (unsigned short)(RSTRING(s)->ptr[i]);
1096
+ }
1097
+ COORD coords;
1098
+ coords.X=NUM2INT( x );
1099
+ coords.Y=NUM2INT( y );
1100
+ if (WriteConsoleOutputAttribute(handle,(const unsigned short *)&buffer,
1101
+ towrite,coords,&written)) {
1102
+ return UINT2NUM( written );
1103
+ }
1104
+ return rb_getWin32Error();
1105
+ }
1106
+
1107
+
1108
+ VALUE
1109
+ rb_WriteConsoleOutputCharacter(VALUE self, VALUE h, VALUE s,
1110
+ VALUE x, VALUE y)
1111
+ {
1112
+
1113
+ HANDLE handle = ULongToPtr( NUM2ULONG( h ) );
1114
+ Check_Type( s, T_STRING );
1115
+
1116
+ DWORD written;
1117
+ COORD coords;
1118
+ coords.X=NUM2INT( x );
1119
+ coords.Y=NUM2INT( y );
1120
+ if (WriteConsoleOutputCharacter(handle,(LPCTSTR)RSTRING(s)->ptr,
1121
+ RSTRING(s)->len,coords,&written)) {
1122
+ return UINT2NUM( written );
1123
+ }
1124
+ return rb_getWin32Error();
1125
+ }
1126
+
1127
+
1128
+ CONSOLE_EXPORT void
1129
+ Init_Console(void)
1130
+ {
1131
+ rb_mWin32 = rb_define_module("Win32");
1132
+ rb_define_const(rb_mKernel, "Win32", rb_mWin32);
1133
+
1134
+ rb_mConsole = rb_define_class_under(rb_mWin32, "Console", rb_cObject);
1135
+
1136
+ // Handle Constants
1137
+ rb_mConstants = rb_define_module_under(rb_mConsole,"Constants");
1138
+ define_constants();
1139
+
1140
+ // Handle API
1141
+ rb_mAPI = rb_define_class_under(rb_mConsole, "API", rb_cObject);
1142
+
1143
+ RB_DEF_API_METHOD(constant, 1); //OK
1144
+
1145
+ RB_DEF_API_METHOD(AllocConsole, 0);
1146
+
1147
+ RB_DEF_API_METHOD(CreateConsoleScreenBuffer, 3); //OK
1148
+
1149
+ RB_DEF_API_METHOD(FillConsoleOutputAttribute, 5); //OK
1150
+ RB_DEF_API_METHOD(FillConsoleOutputCharacter, 5); //OK
1151
+ // RB_DEF_API_METHOD(FillConsoleInputBuffer, 1); // Does not exist anymore
1152
+
1153
+ RB_DEF_API_METHOD(FreeConsole, 0);
1154
+
1155
+ RB_DEF_API_METHOD(GenerateConsoleCtrlEvent, 2);
1156
+
1157
+ RB_DEF_API_METHOD(GetConsoleCP, 0); //OK
1158
+ RB_DEF_API_METHOD(GetConsoleCursorInfo, 1); //OK
1159
+ RB_DEF_API_METHOD(GetConsoleMode, 1);
1160
+ RB_DEF_API_METHOD(GetConsoleOutputCP, 0);
1161
+ RB_DEF_API_METHOD(GetConsoleScreenBufferInfo, 1); //OK
1162
+ RB_DEF_API_METHOD(GetConsoleTitle, 0);
1163
+ RB_DEF_API_METHOD(GetConsoleWindow, 0);
1164
+ RB_DEF_API_METHOD(GetLargestConsoleWindowSize, 1);
1165
+ RB_DEF_API_METHOD(GetNumberOfConsoleInputEvents, 1);
1166
+ RB_DEF_API_METHOD(GetNumberOfConsoleMouseButtons, 0);
1167
+
1168
+ RB_DEF_API_METHOD(GetStdHandle, 1); //OK
1169
+
1170
+ RB_DEF_API_METHOD(PeekConsoleInput, 1); //OK
1171
+ RB_DEF_API_METHOD(ReadConsole, 3); //OK
1172
+ RB_DEF_API_METHOD(ReadConsoleInput, 1); //OK
1173
+
1174
+ RB_DEF_API_METHOD(ReadConsoleOutput, 10); // OK
1175
+ RB_DEF_API_METHOD(ReadConsoleOutputAttribute, 4); // OK
1176
+ RB_DEF_API_METHOD(ReadConsoleOutputCharacter, 5); // OK
1177
+
1178
+
1179
+ RB_DEF_API_METHOD(ScrollConsoleScreenBuffer, 13); //OK
1180
+
1181
+ RB_DEF_API_METHOD(SetConsoleActiveScreenBuffer, 1);
1182
+ RB_DEF_API_METHOD(SetConsoleCP, 1);
1183
+ RB_DEF_API_METHOD(SetConsoleCursorPosition, 3);
1184
+ RB_DEF_API_METHOD(SetConsoleCursorInfo, 3);
1185
+ RB_DEF_API_METHOD(SetConsoleMode, 2); //OK
1186
+ RB_DEF_API_METHOD(SetConsoleOutputCP, 1);
1187
+ RB_DEF_API_METHOD(SetConsoleScreenBufferSize, 3);
1188
+ RB_DEF_API_METHOD(SetConsoleTextAttribute, 2);
1189
+ RB_DEF_API_METHOD(SetConsoleTitle, 1); //OK
1190
+ RB_DEF_API_METHOD(SetConsoleWindowInfo, 6);
1191
+
1192
+ RB_DEF_API_METHOD(SetStdHandle, 2);
1193
+
1194
+ RB_DEF_API_METHOD(WriteConsole, 2);
1195
+
1196
+ RB_DEF_API_METHOD(WriteConsoleInput, -1);
1197
+ RB_DEF_API_METHOD(WriteConsoleOutput, 10);
1198
+ RB_DEF_API_METHOD(WriteConsoleOutputAttribute, 4);
1199
+ RB_DEF_API_METHOD(WriteConsoleOutputCharacter, 4);
1200
+
1201
+ }
1202
+
1203
+ }