win32console 1.2.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,342 @@
1
+
2
+ # The actual api to access windows functions
3
+ module Win32
4
+ class Console
5
+ class API
6
+ require 'Win32API'
7
+
8
+ class << self
9
+
10
+ def constant(t)
11
+ begin
12
+ return Win32::Console::Constants.const_get(t)
13
+ rescue
14
+ return nil
15
+ end
16
+ end
17
+
18
+ def AllocConsole()
19
+ @AllocConsole ||= Win32API.new( "kernel32", "AllocConsole", [], 'l' )
20
+ @AllocConsole.call()
21
+ end
22
+
23
+ def CreateConsoleScreenBuffer( dwDesiredAccess, dwShareMode, dwFlags )
24
+ @CreateConsoleScreenBuffer ||= Win32API.new( "kernel32", "CreateConsoleScreenBuffer", ['l', 'l', 'p', 'l', 'p'], 'l' )
25
+ @CreateConsoleScreenBuffer.call( dwDesiredAccess, dwShareMode, nil, dwFlags, nil )
26
+ end
27
+
28
+ def FillConsoleOutputAttribute( hConsoleOutput, wAttribute, nLength, col, row )
29
+ @FillConsoleOutputAttribute ||= Win32API.new( "kernel32", "FillConsoleOutputAttribute", ['l', 'i', 'l', 'l', 'p'], 'l' )
30
+ dwWriteCoord = (row << 16) + col
31
+ lpNumberOfAttrsWritten = ' ' * 4
32
+ @FillConsoleOutputAttribute.call( hConsoleOutput, wAttribute, nLength, dwWriteCoord, lpNumberOfAttrsWritten )
33
+ return lpNumberOfAttrsWritten.unpack('L')
34
+ end
35
+
36
+ def FillConsoleOutputCharacter( hConsoleOutput, cCharacter, nLength, col, row )
37
+ @FillConsoleOutputCharacter ||= Win32API.new( "kernel32", "FillConsoleOutputCharacter", ['l', 'i', 'l', 'l', 'p'], 'l' )
38
+ dwWriteCoord = (row << 16) + col
39
+ lpNumberOfAttrsWritten = ' ' * 4
40
+ @FillConsoleOutputCharacter.call( hConsoleOutput, cCharacter, nLength, dwWriteCoord, lpNumberOfAttrsWritten )
41
+ return lpNumberOfAttrsWritten.unpack('L')
42
+ end
43
+
44
+ def FlushConsoleInputBuffer( hConsoleInput )
45
+ @FlushConsoleInputBuffer ||= Win32API.new( "kernel32", "FillConsoleInputBuffer", ['l'], 'l' )
46
+ @FlushConsoleInputBuffer.call( hConsoleInput )
47
+ end
48
+
49
+ def FreeConsole()
50
+ @FreeConsole ||= Win32API.new( "kernel32", "FreeConsole", [], 'l' )
51
+ @FreeConsole.call()
52
+ end
53
+
54
+ def GenerateConsoleCtrlEvent( dwCtrlEvent, dwProcessGroupId )
55
+ @GenerateConsoleCtrlEvent ||= Win32API.new( "kernel32", "GenerateConsoleCtrlEvent", ['l', 'l'], 'l' )
56
+ @GenerateConsoleCtrlEvent.call( dwCtrlEvent, dwProcessGroupId )
57
+ end
58
+
59
+ def GetConsoleCP()
60
+ @GetConsoleCP ||= Win32API.new( "kernel32", "GetConsoleCP", [], 'l' )
61
+ @GetConsoleCP.call()
62
+ end
63
+
64
+ def GetConsoleCursorInfo( hConsoleOutput )
65
+ @GetConsoleCursorInfo ||= Win32API.new( "kernel32", "GetConsoleCursorInfo", ['l', 'p'], 'l' )
66
+ lpConsoleCursorInfo = ' ' * 8
67
+ @GetConsoleCursorInfo.call( hConsoleOutput, lpConsoleCursorInfo )
68
+ return lpConsoleCursorInfo.unpack('LL')
69
+ end
70
+
71
+ def GetConsoleMode( hConsoleHandle )
72
+ @GetConsoleMode ||= Win32API.new( "kernel32", "GetConsoleMode", ['l', 'p'], 'l' )
73
+ lpMode = ' ' * 4
74
+ @GetConsoleMode.call( hConsoleHandle, lpMode )
75
+ return lpMode.unpack('L').first
76
+ end
77
+
78
+ def GetConsoleOutputCP()
79
+ @GetConsoleOutputCP ||= Win32API.new( "kernel32", "GetConsoleOutputCP", [], 'l' )
80
+ @GetConsoleOutputCP.call()
81
+ end
82
+
83
+ def GetConsoleScreenBufferInfo( hConsoleOutput )
84
+ @GetConsoleScreenBufferInfo ||= Win32API.new( "kernel32", "GetConsoleScreenBufferInfo", ['l', 'p'], 'l' )
85
+ lpBuffer = ' ' * 22
86
+ @GetConsoleScreenBufferInfo.call( hConsoleOutput, lpBuffer )
87
+ return lpBuffer.unpack('SSSSSssssSS')
88
+ end
89
+
90
+ def GetConsoleTitle()
91
+ @GetConsoleTitle ||= Win32API.new( "kernel32", "GetConsoleTitle", ['p', 'l'], 'l' )
92
+ nSize = 120
93
+ lpConsoleTitle = ' ' * nSize
94
+ @GetConsoleTitle.call( lpConsoleTitle, nSize )
95
+ return lpConsoleTitle.strip
96
+ end
97
+
98
+ def GetConsoleWindow()
99
+ @GetConsoleWindow ||= Win32API.new( "kernel32", "GetConsoleWindow",[], 'l' )
100
+ @GetConsoleWindow.call()
101
+ end
102
+
103
+ def GetLargestConsoleWindowSize( hConsoleOutput )
104
+ @GetLargestConsoleWindowSize ||= Win32API.new( "kernel32", "GetLargestConsoleWindowSize", ['l'], 'l' )
105
+ coord = @GetLargestConsoleWindowSize.call( hConsoleOutput )
106
+ x = coord >> 16
107
+ y = coord & 0x0000ffff
108
+ return [x,y]
109
+ end
110
+
111
+ def GetNumberOfConsoleInputEvents( hConsoleInput )
112
+ @GetNumberOfConsoleInputEvents ||= Win32API.new( "kernel32", "GetNumberOfConsoleInputEvents", ['l', 'p'], 'l' )
113
+ lpcNumberOfEvents = 0
114
+ @GetNumberOfConsoleInputEvents.call( hConsoleInput, lpcNumberOfEvents )
115
+ return lpcNumberOfEvents
116
+ end
117
+
118
+ def GetNumberOfConsoleMouseButtons( )
119
+ @GetNumberOfConsoleMouseButtons ||= Win32API.new( "kernel32", "GetNumberOfConsoleMouseButtons", ['p'], 'l' )
120
+ lpNumberOfMouseButtons = 0
121
+ @GetNumberOfConsoleMouseButtons.call( lpNumberOfMouseButtons )
122
+ return lpNumberOfMouseButtons
123
+ end
124
+
125
+ def GetStdHandle( nStdHandle )
126
+ @GetStdHandle ||= Win32API.new( "kernel32", "GetStdHandle", ['l'], 'l' )
127
+ @GetStdHandle.call( nStdHandle )
128
+ end
129
+
130
+ # <<HandlerRoutine>> : This is not an actual API function, just a concept description in the SDK.
131
+
132
+ def PeekConsoleInput( hConsoleInput )
133
+ @PeekConsoleInput ||= Win32API.new( "kernel32", "PeekConsoleInput", ['l', 'p', 'l', 'p'], 'l' )
134
+ lpNumberOfEventsRead = ' ' * 4
135
+ lpBuffer = ' ' * 20
136
+ nLength = 20
137
+ @PeekConsoleInput.call( hConsoleInput, lpBuffer, nLength, lpNumberOfEventsRead )
138
+ type = lpBuffer.unpack('s')[0]
139
+
140
+ case type
141
+ when KEY_EVENT
142
+ return lpBuffer.unpack('sSSSSCS')
143
+ when MOUSE_EVENT
144
+ return lpBuffer.unpack('sSSSS')
145
+ when WINDOW_BUFFER_SIZE_EVENT
146
+ return lpBuffer.unpack('sS')
147
+ when MENU_EVENT
148
+ return lpBuffer.unpack('sS')
149
+ when FOCUS_EVENT
150
+ return lpBuffer.unpack('sS')
151
+ else
152
+ return []
153
+ end
154
+ end
155
+
156
+ def ReadConsole( hConsoleInput, lpBuffer, nNumberOfCharsToRead )
157
+ @ReadConsole ||= Win32API.new( "kernel32", "ReadConsole", ['l', 'p', 'l', 'p', 'p'], 'l' )
158
+ lpBuffer = ' ' * nNumberOfCharsToRead unless lpBuffer
159
+ lpNumberOfCharsRead = ' ' * 4
160
+ lpReserved = ' ' * 4
161
+ @ReadConsole.call( hConsoleInput, lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead, lpReserved )
162
+ return lpNumberOfCharsRead.unpack('L')
163
+ end
164
+
165
+ def ReadConsoleInput( hConsoleInput )
166
+ @ReadConsoleInput ||= Win32API.new( "kernel32", "ReadConsoleInput", ['l', 'p', 'l', 'p'], 'l' )
167
+ lpNumberOfEventsRead = ' ' * 4
168
+ lpBuffer = ' ' * 20
169
+ nLength = 20
170
+ @ReadConsoleInput.call( hConsoleInput, lpBuffer, nLength,
171
+ lpNumberOfEventsRead )
172
+ type = lpBuffer.unpack('s')[0]
173
+
174
+ case type
175
+ when KEY_EVENT
176
+ return lpBuffer.unpack('sSSSSCS')
177
+ when MOUSE_EVENT
178
+ return lpBuffer.unpack('sSSSS')
179
+ when WINDOW_BUFFER_SIZE_EVENT
180
+ return lpBuffer.unpack('sS')
181
+ when MENU_EVENT
182
+ return lpBuffer.unpack('sS')
183
+ when FOCUS_EVENT
184
+ return lpBuffer.unpack('sS')
185
+ else
186
+ return []
187
+ end
188
+ end
189
+
190
+ def ReadConsoleOutput( hConsoleOutput, lpBuffer, cols, rows, bufx, bufy, left, top, right, bottom )
191
+ @ReadConsoleOutput ||= Win32API.new( "kernel32", "ReadConsoleOutput", ['l', 'p', 'l', 'l', 'p'], 'l' )
192
+ dwBufferSize = cols * rows * 4
193
+ lpBuffer = ' ' * dwBufferSize
194
+ dwBufferCoord = (bufy << 16) + bufx
195
+ lpReadRegion = [ left, top, right, bottom ].pack('ssss')
196
+ @ReadConsoleOutput.call( hConsoleOutput, lpBuffer, dwBufferSize,
197
+ dwBufferCoord, lpReadRegion )
198
+ end
199
+
200
+ def ReadConsoleOutputAttribute( hConsoleOutput, nLength, col, row )
201
+ @ReadConsoleOutputAttribute ||= Win32API.new( "kernel32", "ReadConsoleOutputAttribute", ['l', 'p', 'l', 'l', 'p'], 'l' )
202
+ lpAttribute = ' ' * nLength
203
+ dwReadCoord = (row << 16) + col
204
+ lpNumberOfAttrsRead = ' ' * 4
205
+ @ReadConsoleOutputAttribute.call( hConsoleOutput, lpAttribute, nLength, dwReadCoord, lpNumberOfAttrsRead )
206
+ return lpAttribute
207
+ end
208
+
209
+ def ReadConsoleOutputCharacter( hConsoleOutput, lpCharacter, nLength, col, row )
210
+ @ReadConsoleOutputCharacter ||= Win32API.new( "kernel32", "ReadConsoleOutputCharacter", ['l', 'p', 'l', 'l', 'p'], 'l' )
211
+ dwReadCoord = (row << 16) + col
212
+ lpNumberOfCharsRead = ' ' * 4
213
+ @ReadConsoleOutputCharacter.call( hConsoleOutput, lpCharacter, nLength, dwReadCoord, lpNumberOfCharsRead )
214
+ return lpNumberOfCharsRead.unpack('L')
215
+ end
216
+
217
+ def ScrollConsoleScreenBuffer( hConsoleOutput, left1, top1, right1, bottom1,col, row, char, attr, left2, top2, right2, bottom2 )
218
+ @ScrollConsoleScreenBuffer ||= Win32API.new( "kernel32", "ScrollConsoleScreenBuffer", ['l', 'p', 'p', 'l', 'p'], 'l' )
219
+ lpScrollRectangle = [left1, top1, right1, bottom1].pack('ssss')
220
+ lpClipRectangle = [left2, top2, right2, bottom2].pack('ssss')
221
+ dwDestinationOrigin = (row << 16) + col
222
+ lpFill = [char, attr].pack('ss')
223
+ @ScrollConsoleScreenBuffer.call( hConsoleOutput, lpScrollRectangle, lpClipRectangle, dwDestinationOrigin, lpFill )
224
+ end
225
+
226
+ def SetConsoleActiveScreenBuffer( hConsoleOutput )
227
+ @SetConsoleActiveScreenBuffer ||= Win32API.new( "kernel32", "SetConsoleActiveScreenBuffer", ['l'], 'l' )
228
+ @SetConsoleActiveScreenBuffer.call( hConsoleOutput )
229
+ end
230
+
231
+ # <<SetConsoleCtrlHandler>>: Will probably not be implemented.
232
+
233
+ def SetConsoleCP( wCodePageID )
234
+ @SetConsoleCP ||= Win32API.new( "kernel32", "SetConsoleCP", ['l'], 'l' )
235
+ @SetConsoleCP.call( wCodePageID )
236
+ end
237
+
238
+ def SetConsoleCursorInfo( hConsoleOutput, col, row )
239
+ @SetConsoleCursorInfo ||= Win32API.new( "kernel32", "SetConsoleCursorInfo", ['l', 'p'], 'l' )
240
+ lpConsoleCursorInfo = [size,visi].pack('LL')
241
+ @SetConsoleCursorInfo.call( hConsoleOutput, lpConsoleCursorInfo )
242
+ end
243
+
244
+ def SetConsoleCursorPosition( hConsoleOutput, col, row )
245
+ @SetConsoleCursorPosition ||= Win32API.new( "kernel32", "SetConsoleCursorPosition", ['l', 'p'], 'l' )
246
+ dwCursorPosition = (row << 16) + col
247
+ @SetConsoleCursorPosition.call( hConsoleOutput, dwCursorPosition )
248
+ end
249
+
250
+ def SetConsoleMode( hConsoleHandle, lpMode )
251
+ @SetConsoleMode ||= Win32API.new( "kernel32", "SetConsoleMode", ['l', 'p'], 'l' )
252
+ @SetConsoleMode.call( hConsoleHandle, lpMode )
253
+ end
254
+
255
+ def SetConsoleOutputCP( wCodePageID )
256
+ @SetConsoleOutputCP ||= Win32API.new( "kernel32", "GetConsoleOutputCP", ['l'], 'l' )
257
+ @SetConsoleOutputCP.call( wCodePageID )
258
+ end
259
+
260
+ def SetConsoleScreenBufferSize( hConsoleOutput, col, row )
261
+ @SetConsoleScreenBufferSize ||= Win32API.new( "kernel32", "SetConsoleScreenBufferSize", ['l', 'l'], 'l' )
262
+ dwSize = (row << 16) + col
263
+ @SetConsoleScreenBufferSize.call( hConsoleOutput, dwSize )
264
+ end
265
+
266
+ def SetConsoleTextAttribute( hConsoleOutput, wAttributes )
267
+ @SetConsoleTextAttribute ||= Win32API.new( "kernel32", "SetConsoleTextAttribute", ['l', 'i'], 'l' )
268
+ @SetConsoleTextAttribute.call( hConsoleOutput, wAttributes )
269
+ end
270
+
271
+ def SetConsoleTitle( lpConsoleTitle )
272
+ @SetConsoleTitle ||= Win32API.new( "kernel32", "SetConsoleTitle", ['p'], 'l' )
273
+ @SetConsoleTitle.call( lpConsoleTitle )
274
+ end
275
+
276
+ def SetConsoleWindowInfo( hConsoleOutput, bAbsolute, left, top, right, bottom )
277
+ @SetConsoleWindowInfo ||= Win32API.new( "kernel32", "SetConsoleWindowInfo", ['l', 'l', 'p'], 'l' )
278
+ lpConsoleWindow = [ left, top, right, bottom ].pack('ssss')
279
+ @SetConsoleWindowInfo.call( hConsoleOutput, bAbsolute, lpConsoleWindow )
280
+ end
281
+
282
+ def SetStdHandle( nStdHandle, hHandle )
283
+ @SetStdHandle ||= Win32API.new( "kernel32", "SetStdHandle", ['l', 'l'], 'l' )
284
+ @SetStdHandle.call( nStdHandle, hHandle )
285
+ end
286
+
287
+ def WriteConsole( hConsoleOutput, lpBuffer )
288
+ @WriteConsole ||= Win32API.new( "kernel32", "WriteConsole", ['l', 'p', 'l', 'p', 'p'], 'l' )
289
+ nNumberOfCharsToWrite = lpBuffer.length()
290
+ lpNumberOfCharsWritten = ' ' * 4
291
+ lpReserved = ' ' * 4
292
+ @WriteConsole.call( hConsoleOutput, lpBuffer, nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReserved )
293
+ return lpNumberOfCharsWritten
294
+ end
295
+
296
+ def WriteFile( hConsoleOutput, lpBuffer )
297
+ @WriteFile ||= Win32API.new( "kernel32", "WriteFile", ['l', 'p', 'l', 'p', 'p'], 'l' )
298
+ nNumberOfBytesToWrite = lpBuffer.length()
299
+ lpNumberOfBytesWritten = ' ' * 4
300
+ lpReserved = nil
301
+ @WriteFile.call( hConsoleOutput, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpReserved )
302
+ return lpNumberOfBytesWritten.unpack('L')
303
+ end
304
+
305
+ def WriteConsoleInput( hConsoleInput, lpBuffer )
306
+ @WriteConsoleInput ||= Win32API.new( "kernel32", "WriteConsoleInput", ['l', 'p', 'l', 'p'], 'l' )
307
+ @WriteConsoleInput.call( hConsoleInput, lpBuffer, nLength, lpNumberOfEventsWritten )
308
+ end
309
+
310
+ # @@ Todo: Test this
311
+ def WriteConsoleOutput( hConsoleOutput, buffer, cols, rows, bufx, bufy, left, top, right, bottom )
312
+ @WriteConsoleOutput ||= Win32API.new( "kernel32", "WriteConsoleOutput", ['l', 'p', 'l', 'l', 'p'], 'l' )
313
+ lpBuffer = buffer.flatten.pack('ss' * buffer.length() * 2)
314
+ dwBufferSize = (buffer.length() << 16) + 2
315
+ dwBufferCoord = (row << 16) + col
316
+ lpWriteRegion = [ left, top, right, bottom ].pack('ssss')
317
+ @WriteConsoleOutput.call( hConsoleOutput, lpBuffer, dwBufferSize, dwBufferCoord, lpWriteRegion )
318
+ end
319
+
320
+ def WriteConsoleOutputAttribute( hConsoleOutput, lpAttribute, col, row )
321
+ @WriteConsoleOutputAttribute ||= Win32API.new( "kernel32", "WriteConsoleOutputAttribute", ['l', 'p', 'l', 'l', 'p'], 'l' )
322
+ nLength = lpAttribute.length()
323
+ dwWriteCoord = (row << 16) + col
324
+ lpNumberOfAttrsWritten = ' ' * 4
325
+ @WriteConsoleOutputAttribute.call( hConsoleOutput, lpAttribute, nLength, dwWriteCoord, lpNumberOfAttrsWritten )
326
+ return lpNumberOfAttrsWritten.unpack('L')
327
+ end
328
+
329
+ def WriteConsoleOutputCharacter( hConsoleOutput, lpCharacter, col, row )
330
+ @WriteConsoleOutputCharacter ||= Win32API.new( "kernel32", "WriteConsoleOutputCharacter", ['l', 'p', 'l', 'l', 'p'], 'l' )
331
+ nLength = lpCharacter.length()
332
+ dwWriteCoord = (row << 16) + col
333
+ lpNumberOfCharsWritten = ' ' * 4
334
+ @WriteConsoleOutputCharacter.call( hConsoleOutput, lpCharacter, nLength, dwWriteCoord, lpNumberOfCharsWritten )
335
+ return lpNumberOfCharsWritten.unpack('L')
336
+ end
337
+
338
+ end
339
+ end
340
+ end
341
+
342
+ end
@@ -0,0 +1,66 @@
1
+ # The WINDOWS constants
2
+
3
+ module Win32
4
+ class Console
5
+ module Constants
6
+ STD_INPUT_HANDLE = 0xFFFFFFF6
7
+ STD_OUTPUT_HANDLE = 0xFFFFFFF5
8
+ STD_ERROR_HANDLE = 0xFFFFFFF4
9
+ INVALID_HANDLE_VALUE = 0xFFFFFFFF
10
+ GENERIC_READ = 0x80000000
11
+ GENERIC_WRITE = 0x40000000
12
+ FILE_SHARE_READ = 0x00000001
13
+ FILE_SHARE_WRITE = 0x00000002
14
+ CONSOLE_TEXTMODE_BUFFER = 0x00000001
15
+
16
+ FOREGROUND_BLUE = 0x0001
17
+ FOREGROUND_GREEN = 0x0002
18
+ FOREGROUND_RED = 0x0004
19
+ FOREGROUND_INTENSITY = 0x0008
20
+ BACKGROUND_BLUE = 0x0010
21
+ BACKGROUND_GREEN = 0x0020
22
+ BACKGROUND_RED = 0x0040
23
+ BACKGROUND_INTENSITY = 0x0080
24
+
25
+ ENABLE_PROCESSED_INPUT = 0x0001
26
+ ENABLE_LINE_INPUT = 0x0002
27
+ ENABLE_ECHO_INPUT = 0x0004
28
+ ENABLE_WINDOW_INPUT = 0x0008
29
+ ENABLE_MOUSE_INPUT = 0x0010
30
+ ENABLE_PROCESSED_OUTPUT = 0x0001
31
+ ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002
32
+
33
+ KEY_EVENT = 0x0001
34
+ MOUSE_EVENT = 0x0002
35
+ WINDOW_BUFFER_SIZE_EVENT = 0x0004
36
+ MENU_EVENT = 0x0008
37
+ FOCUS_EVENT = 0x0010
38
+
39
+ CAPSLOCK_ON = 0x0080
40
+ ENHANCED_KEY = 0x0100
41
+ NUMLOCK_ON = 0x0020
42
+ SHIFT_PRESSED = 0x0010
43
+ LEFT_CTRL_PRESSED = 0x0008
44
+ RIGHT_CTRL_PRESSED = 0x0004
45
+ LEFT_ALT_PRESSED = 0x0002
46
+ RIGHT_ALT_PRESSED = 0x0001
47
+ SCROLLLOCK_ON = 0x0040
48
+
49
+ MOUSE_WHEELED = 0x0004
50
+ DOUBLE_CLICK = 0x0002
51
+ MOUSE_MOVED = 0x0001
52
+
53
+ FROM_LEFT_1ST_BUTTON_PRESSED = 0x0001
54
+ FROM_LEFT_2ND_BUTTON_PRESSED = 0x0004
55
+ FROM_LEFT_3RD_BUTTON_PRESSED = 0x0008
56
+ FROM_LEFT_4TH_BUTTON_PRESSED = 0x0010
57
+ RIGHTMOST_BUTTON_PRESSED = 0x0002
58
+
59
+ CTRL_C_EVENT = 0x0000
60
+ CTRL_BREAK_EVENT = 0x0001
61
+ CTRL_CLOSE_EVENT = 0x0002
62
+ CTRL_LOGOFF_EVENT = 0x0005
63
+ CTRL_SHUTDOWN_EVENT = 0x0006
64
+ end
65
+ end
66
+ end