forked from stevedonovan/winapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winapi.c
2897 lines (2575 loc) · 85.4 KB
/
winapi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
A useful set of Windows API functions.
* Enumerating and accessing windows, including sending keys.
* Enumerating processes and querying their program name, memory used, etc.
* Reading and Writing to the Registry
* Copying and moving files, and showing drive information.
* Launching processes and opening documents.
* Monitoring filesystem changes.
@author Steve Donovan (steve.j.donovan@gmail.com)
@copyright 2011
@license MIT/X11
@module winapi
*/
#line 16 "winapi.l.c"
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
#include <string.h>
#ifdef __GNUC__
#include <winable.h> /* GNU GCC specific */
#endif
#include "Winnetwk.h"
#include <psapi.h>
#define WBUFF 2048
#define MAX_SHOW 100
#define THREAD_STACK_SIZE (1024 * 1024)
#define MAX_PROCESSES 1024
#define MAX_KEYS 512
#define FILE_BUFF_SIZE 2048
#define MAX_WATCH 20
#define MAX_WPATH 1024
#define TIMEOUT(timeout) timeout == 0 ? INFINITE : timeout
static wchar_t wbuff[WBUFF];
typedef LPCWSTR WStr;
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#ifdef WIN32
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
#if LUA_VERSION_NUM > 501
#define lua_objlen lua_rawlen
#endif
typedef const char *Str;
typedef const char *StrNil;
typedef int Int;
typedef double Number;
typedef int Boolean;
#line 43 "winapi.l.c"
#include "wutils.h"
static WStr wstring(Str text) {
return wstring_buff(text,wbuff,sizeof(wbuff));
}
/// Text encoding.
// @section encoding
/// set the current text encoding.
// @param e one of `CP_ACP` (Windows code page; default) and `CP_UTF8`
// @function set_encoding
static int l_set_encoding(lua_State *L) {
int e = luaL_checkinteger(L,1);
#line 56 "winapi.l.c"
set_encoding(e);
return 0;
}
/// get the current text encoding.
// @return either `CP_ACP` or `CP_UTF8`
// @function get_encoding
static int l_get_encoding(lua_State *L) {
lua_pushinteger(L, get_encoding());
return 1;
}
/// encode a string in another encoding.
// Note: currently there's a limit of about 2K on the string buffer.
// @param e_in `CP_ACP`, `CP_UTF8` or `CP_UTF16`
// @param e_out likewise
// @param text the string
// @function encode
static int l_encode(lua_State *L) {
int e_in = luaL_checkinteger(L,1);
int e_out = luaL_checkinteger(L,2);
const char *text = luaL_checklstring(L,3,NULL);
#line 75 "winapi.l.c"
int ce = get_encoding();
LPCWSTR ws;
if (e_in != -1) {
set_encoding(e_in);
ws = wstring(text);
} else {
ws = (LPCWSTR)text;
}
if (e_out != -1) {
set_encoding(e_out);
push_wstring(L,ws);
} else {
lua_pushlstring(L,(LPCSTR)ws,wcslen(ws)*sizeof(WCHAR));
}
set_encoding(ce);
return 1;
}
/// expand # unicode escapes in a string.
// @param text ASCII text with #XXXX, where XXXX is four hex digits. ## means # itself.
// @return text as UTF-8
// @see testu.lua
// @function utf8_expand
static int l_utf8_expand(lua_State *L) {
const char *text = luaL_checklstring(L,1,NULL);
#line 99 "winapi.l.c"
int len = strlen(text), i = 0, enc = get_encoding();
WCHAR wch;
LPWSTR P = wbuff;
if (len > sizeof(wbuff)) {
return push_error_msg(L,"string too big");
}
while (i <= len) {
if (text[i] == '#') {
++i;
if (text[i] == '#') {
wch = '#';
} else
if (len-i >= 4) {
char hexnum[5];
strncpy(hexnum,text+i,4);
hexnum[4] = '\0';
wch = strtol(hexnum,NULL,16);
i += 3;
} else {
return push_error_msg(L,"bad # escape");
}
} else {
wch = (WCHAR)text[i];
}
*P++ = wch;
++i;
}
*P++ = 0;
set_encoding(CP_UTF8);
push_wstring(L,wbuff);
set_encoding(enc);
return 1;
}
// forward reference to Process constructor
static int push_new_Process(lua_State *L,Int pid, HANDLE ph);
const DWORD_PTR WIN_NOACTIVATE = (DWORD_PTR)SWP_NOACTIVATE,
WIN_NOMOVE = (DWORD_PTR)SWP_NOMOVE,
WIN_NOSIZE = (DWORD_PTR)SWP_NOSIZE,
WIN_SHOWWINDOW = (DWORD_PTR)SWP_SHOWWINDOW,
WIN_NOZORDER = (DWORD_PTR)SWP_NOZORDER,
WIN_BOTTOM = (DWORD_PTR)HWND_BOTTOM,
WIN_NOTOPMOST = (DWORD_PTR)HWND_NOTOPMOST,
WIN_TOP = (DWORD_PTR)HWND_TOP,
WIN_TOPMOST = (DWORD_PTR)HWND_TOPMOST;
/// a class representing a Window.
// @type Window
#line 152 "winapi.l.c"
typedef struct {
HWND hwnd;
} Window;
#define Window_MT "Window"
Window * Window_arg(lua_State *L,int idx) {
Window *this = (Window *)luaL_checkudata(L,idx,Window_MT);
luaL_argcheck(L, this != NULL, idx, "Window expected");
return this;
}
static void Window_ctor(lua_State *L, Window *this, HWND h);
static int push_new_Window(lua_State *L,HWND h) {
Window *this = (Window *)lua_newuserdata(L,sizeof(Window));
luaL_getmetatable(L,Window_MT);
lua_setmetatable(L,-2);
Window_ctor(L,this,h);
return 1;
}
static void Window_ctor(lua_State *L, Window *this, HWND h) {
#line 153 "winapi.l.c"
this->hwnd = h;
}
static lua_State *sL;
static BOOL CALLBACK enum_callback(HWND hwnd,LPARAM data) {
push_ref(sL,(Ref)data);
push_new_Window(sL,hwnd);
lua_call(sL,1,0);
return TRUE;
}
/// the handle of this window.
// @function get_handle
static int l_Window_get_handle(lua_State *L) {
Window *this = Window_arg(L,1);
#line 168 "winapi.l.c"
lua_pushnumber(L,(DWORD_PTR)this->hwnd);
return 1;
}
/// get the window text.
// @function get_text
static int l_Window_get_text(lua_State *L) {
Window *this = Window_arg(L,1);
#line 175 "winapi.l.c"
GetWindowTextW(this->hwnd,wbuff,sizeof(wbuff));
return push_wstring(L,wbuff);
}
/// set the window text.
// @function set_text
static int l_Window_set_text(lua_State *L) {
Window *this = Window_arg(L,1);
const char *text = luaL_checklstring(L,2,NULL);
#line 182 "winapi.l.c"
SetWindowTextW(this->hwnd,wstring(text));
return 0;
}
/// change the visibility, state etc
// @param flags one of `SW_SHOW`, `SW_MAXIMIZE`, etc
// @function show
static int l_Window_show(lua_State *L) {
Window *this = Window_arg(L,1);
int flags = luaL_optinteger(L,2,SW_SHOW);
#line 190 "winapi.l.c"
ShowWindow(this->hwnd,flags);
return 0;
}
/// change the visibility without blocking.
// @param flags one of `SW_SHOW`, `SW_MAXIMIZE`, etc
// @function show_async
static int l_Window_show_async(lua_State *L) {
Window *this = Window_arg(L,1);
int flags = luaL_optinteger(L,2,SW_SHOW);
#line 198 "winapi.l.c"
ShowWindowAsync(this->hwnd,flags);
return 0;
}
/// get the position in pixels
// @return left position
// @return top position
// @function get_position
static int l_Window_get_position(lua_State *L) {
Window *this = Window_arg(L,1);
#line 207 "winapi.l.c"
RECT rect;
GetWindowRect(this->hwnd,&rect);
lua_pushinteger(L,rect.left);
lua_pushinteger(L,rect.top);
return 2;
}
/// get the bounds in pixels
// @return width
// @return height
// @function get_bounds
static int l_Window_get_bounds(lua_State *L) {
Window *this = Window_arg(L,1);
#line 219 "winapi.l.c"
RECT rect;
GetWindowRect(this->hwnd,&rect);
lua_pushinteger(L,rect.right - rect.left);
lua_pushinteger(L,rect.bottom - rect.top);
return 2;
}
/// is this window visible?
// @function is_visible
static int l_Window_is_visible(lua_State *L) {
Window *this = Window_arg(L,1);
#line 229 "winapi.l.c"
lua_pushboolean(L,IsWindowVisible(this->hwnd));
return 1;
}
/// destroy this window.
// @function destroy
static int l_Window_destroy(lua_State *L) {
Window *this = Window_arg(L,1);
#line 236 "winapi.l.c"
DestroyWindow(this->hwnd);
return 0;
}
/// resize this window.
// @param x0 left
// @param y0 top
// @param w width
// @param h height
// @function resize
static int l_Window_resize(lua_State *L) {
Window *this = Window_arg(L,1);
int x0 = luaL_checkinteger(L,2);
int y0 = luaL_checkinteger(L,3);
int w = luaL_checkinteger(L,4);
int h = luaL_checkinteger(L,5);
#line 247 "winapi.l.c"
MoveWindow(this->hwnd,x0,y0,w,h,TRUE);
return 0;
}
/// resize or move a window.
// see [API](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545%28v=vs.85%29.aspx)
// @param w window _handle_ to insert after, or one of:
// WINWIN_BOTTOM, WIN_NOTOPMOST, WIN_TOP (default), WIN_TOPMOST
// @param x0 left (ignore if flags has WIN_NOMOVE)
// @param y0 top
// @param w width (ignore if flags has WIN_NOSIZE)
// @param h height
// @param flags one of
// WIN_NOACTIVATE, WIN_NOMOVE, WIN_NOSIZE, WIN_SHOWWINDOW (default), WIN_NOZORDER
static int l_Window_set_pos(lua_State *L) {
Window *this = Window_arg(L,1);
int wafter = luaL_optinteger(L,2,WIN_TOP);
int x0 = luaL_checkinteger(L,3);
int y0 = luaL_checkinteger(L,4);
int w = luaL_checkinteger(L,5);
int h = luaL_checkinteger(L,6);
int flags = luaL_optinteger(L,7,WIN_SHOWWINDOW);
#line 262 "winapi.l.c"
SetWindowPos(this->hwnd,(HWND)(DWORD_PTR)wafter,x0,y0,w,h,flags);
return 0;
}
/// send a message.
// @param msg the message
// @param wparam
// @param lparam
// @return the result
// @function send_message
static int l_Window_send_message(lua_State *L) {
Window *this = Window_arg(L,1);
int msg = luaL_checkinteger(L,2);
double wparam = luaL_checknumber(L,3);
double lparam = luaL_checknumber(L,4);
#line 273 "winapi.l.c"
lua_pushinteger(L,SendMessage(this->hwnd,msg,(WPARAM)wparam,(LPARAM)lparam));
return 1;
}
/// send a message asynchronously.
// @param msg the message
// @param wparam
// @param lparam
// @return the result
// @function post_message
static int l_Window_post_message(lua_State *L) {
Window *this = Window_arg(L,1);
int msg = luaL_checkinteger(L,2);
double wparam = luaL_checknumber(L,3);
double lparam = luaL_checknumber(L,4);
#line 284 "winapi.l.c"
return push_bool(L,PostMessage(this->hwnd,msg,(WPARAM)wparam,(LPARAM)lparam));
}
/// enumerate all child windows.
// @param a callback which to receive each window object
// @function enum_children
static int l_Window_enum_children(lua_State *L) {
Window *this = Window_arg(L,1);
int callback = 2;
#line 292 "winapi.l.c"
Ref ref;
sL = L;
ref = make_ref(L,callback);
EnumChildWindows(this->hwnd,&enum_callback,ref);
release_ref(L,ref);
return 0;
}
/// get the parent window.
// @function get_parent
static int l_Window_get_parent(lua_State *L) {
Window *this = Window_arg(L,1);
#line 303 "winapi.l.c"
return push_new_Window(L,GetParent(this->hwnd));
}
/// get the name of the program owning this window.
// @function get_module_filename
static int l_Window_get_module_filename(lua_State *L) {
Window *this = Window_arg(L,1);
#line 309 "winapi.l.c"
int sz = GetWindowModuleFileNameW(this->hwnd,wbuff,sizeof(wbuff));
wbuff[sz] = 0;
return push_wstring(L,wbuff);
}
/// get the window class name.
// Useful to find all instances of a running program, when you
// know the class of the top level window.
// @function get_class_name
static int l_Window_get_class_name(lua_State *L) {
Window *this = Window_arg(L,1);
#line 319 "winapi.l.c"
static char buff[1024];
int n = GetClassName(this->hwnd,buff,sizeof(buff));
if (n > 0) {
lua_pushstring(L,buff);
return 1;
} else {
return push_error(L);
}
}
/// bring this window to the foreground.
// @function set_foreground
static int l_Window_set_foreground(lua_State *L) {
Window *this = Window_arg(L,1);
#line 332 "winapi.l.c"
lua_pushboolean(L,SetForegroundWindow(this->hwnd));
return 1;
}
/// get the associated process of this window
// @function get_process
static int l_Window_get_process(lua_State *L) {
Window *this = Window_arg(L,1);
#line 339 "winapi.l.c"
DWORD pid;
GetWindowThreadProcessId(this->hwnd,&pid);
return push_new_Process(L,pid,NULL);
}
/// this window as string (up to 100 chars).
// @function __tostring
static int l_Window___tostring(lua_State *L) {
Window *this = Window_arg(L,1);
#line 347 "winapi.l.c"
int ret;
int sz = GetWindowTextW(this->hwnd,wbuff,sizeof(wbuff));
if (sz > MAX_SHOW) {
wbuff[MAX_SHOW] = '\0';
}
ret = push_wstring(L,wbuff);
if (ret == 2) { // we had a conversion error
lua_pushliteral(L,"");
}
return 1;
}
static int l_Window___eq(lua_State *L) {
Window *this = Window_arg(L,1);
Window *other = Window_arg(L,2);
#line 360 "winapi.l.c"
lua_pushboolean(L,this->hwnd == other->hwnd);
return 1;
}
#line 364 "winapi.l.c"
static const struct luaL_Reg Window_methods [] = {
{"get_handle",l_Window_get_handle},
{"get_text",l_Window_get_text},
{"set_text",l_Window_set_text},
{"show",l_Window_show},
{"show_async",l_Window_show_async},
{"get_position",l_Window_get_position},
{"get_bounds",l_Window_get_bounds},
{"is_visible",l_Window_is_visible},
{"destroy",l_Window_destroy},
{"resize",l_Window_resize},
{"set_pos",l_Window_set_pos},
{"send_message",l_Window_send_message},
{"post_message",l_Window_post_message},
{"enum_children",l_Window_enum_children},
{"get_parent",l_Window_get_parent},
{"get_module_filename",l_Window_get_module_filename},
{"get_class_name",l_Window_get_class_name},
{"set_foreground",l_Window_set_foreground},
{"get_process",l_Window_get_process},
{"__tostring",l_Window___tostring},
{"__eq",l_Window___eq},
{NULL, NULL} /* sentinel */
};
static void Window_register (lua_State *L) {
luaL_newmetatable(L,Window_MT);
#if LUA_VERSION_NUM > 501
luaL_setfuncs(L,Window_methods,0);
#else
luaL_register(L,NULL,Window_methods);
#endif
lua_pushvalue(L,-1);
lua_setfield(L,-2,"__index");
lua_pop(L,1);
}
#line 366 "winapi.l.c"
/// Manipulating Windows.
// @section Windows
/// find a window based on classname and caption
// @param cname class name (may be nil)
// @param wname caption (may be nil)
// @return @{Window}
// @function find_window
static int l_find_window(lua_State *L) {
const char *cname = lua_tostring(L,1);
const char *wname = lua_tostring(L,2);
#line 375 "winapi.l.c"
HWND hwnd = FindWindow(cname,wname);
if (hwnd == NULL) {
return push_error(L);
} else {
return push_new_Window(L,hwnd);
}
}
/// makes a function that matches against window text
// @param text
// @function make_name_matcher
/// makes a function that matches against window class name
// @param text
// @function make_class_matcher
/// find a window using a condition function.
// @param match will return true when its argument is the desired window
// @return @{Window}
// @function find_window_ex
/// return all windows matching a condition.
// @param match will return true when its argument is the desired window
// @return a list of window objects
// @function find_all_windows
/// find a window matching the given text.
// @param text the pattern to match against the caption
// @return a window object.
// @function find_window_match
/// current foreground window.
// An example of setting the caption is @{caption.lua}
// @return @{Window}
// @function get_foreground_window
static int l_get_foreground_window(lua_State *L) {
return push_new_Window(L, GetForegroundWindow());
}
/// the desktop window.
// @usage winapi.get_desktop_window():get_bounds()
// @return @{Window}
// @function get_desktop_window
static int l_get_desktop_window(lua_State *L) {
return push_new_Window(L, GetDesktopWindow());
}
/// a Window object from a handle
// @param a Windows nandle
// @return @{Window}
// @function window_from_handle
static int l_window_from_handle(lua_State *L) {
int hwnd = luaL_checkinteger(L,1);
#line 427 "winapi.l.c"
return push_new_Window(L, (HWND)hwnd);
}
/// enumerate over all top-level windows.
// @param callback a function to receive each window object
// @function enum_windows
static int l_enum_windows(lua_State *L) {
int callback = 1;
#line 434 "winapi.l.c"
Ref ref;
sL = L;
ref = make_ref(L,callback);
EnumWindows(&enum_callback,ref);
release_ref(L,ref);
return 0;
}
/// route callback dispatch through a message window.
// You need to do this when using Winapi in a GUI application,
// since it ensures that Lua callbacks happen in the GUI thread.
// @function use_gui
static int l_use_gui(lua_State *L) {
make_message_window();
return 0;
}
static INPUT *add_input(INPUT *pi, WORD vkey, BOOL up) {
pi->type = INPUT_KEYBOARD;
pi->ki.dwFlags = up ? KEYEVENTF_KEYUP : 0;
pi->ki.wVk = vkey;
return pi+1;
}
// The Windows SendInput() is a low-level function, and you have to
// simulate things like uppercase directly. Repeated characters need
// an explicit 'key up' keystroke to work.
// see http://stackoverflow.com/questions/2167156/sendinput-isnt-sending-the-correct-shifted-characters
// this is a case where we have to convert the parameter directly, since
// it may be an integer (virtual key code) or string of characters.
/// send a string or virtual key to the active window.
// @{input.lua} shows launching a process, waiting for it to be
// ready, and sending it some keys
// @param text either a key (like winapi.VK_SHIFT) or a string
// @return number of keys sent, or nil if an error
// @return any error string
// @function send_to_window
static int l_send_to_window(lua_State *L) {
const char *text;
int vkey, len = MAX_KEYS;
UINT res;
SHORT last_vk = 0;
INPUT *input, *pi;
if (lua_isnumber(L,1)) {
INPUT inp;
ZeroMemory(&inp,sizeof(INPUT));
vkey = lua_tointeger(L,1);
add_input(&inp,vkey,lua_toboolean(L,2));
SendInput(1,&inp,sizeof(INPUT));
return 0;
} else {
text = lua_tostring(L,1);
if (text == NULL) {
return push_error_msg(L,"not a string or number");
}
}
input = (INPUT *)malloc(sizeof(INPUT)*len);
pi = input;
ZeroMemory(input, sizeof(INPUT)*len);
for(; *text; ++text) {
SHORT vk = VkKeyScan(*text);
if (last_vk == vk) {
pi = add_input(pi,last_vk & 0xFF,TRUE);
}
if (vk & 0x100) pi = add_input(pi,VK_SHIFT,FALSE);
pi = add_input(pi,vk & 0xFF,FALSE);
if (vk & 0x100) pi = add_input(pi,VK_SHIFT,TRUE);
last_vk = vk;
}
res = SendInput(((DWORD_PTR)pi-(DWORD_PTR)input)/sizeof(INPUT), input, sizeof(INPUT));
free(input);
if (res > 0) {
lua_pushinteger(L,res);
return 1;
} else {
return push_error(L);
}
return 0;
}
/// tile a group of windows.
// @param parent @{Window} (can use the desktop)
// @param horiz tile vertically by default
// @param kids a table of window objects
// @param bounds a bounds table (left,top,right,bottom) - can be nil
// @function tile_windows
static int l_tile_windows(lua_State *L) {
Window *parent = Window_arg(L,1);
int horiz = lua_toboolean(L,2);
int kids = 3;
int bounds = 4;
#line 522 "winapi.l.c"
RECT rt;
HWND *kids_arr;
int i,n_kids;
LPRECT lpRect = NULL;
if (! lua_isnoneornil(L,bounds)) {
lua_pushvalue(L,bounds);
lua_getfield(L,-1,"left"); rt.left=lua_tointeger(L,-1); lua_pop(L,1);
lua_getfield(L,-1,"top"); rt.top=lua_tointeger(L,-1); lua_pop(L,1);
lua_getfield(L,-1,"right"); rt.right=lua_tointeger(L,-1); lua_pop(L,1);
lua_getfield(L,-1,"bottom"); rt.bottom=lua_tointeger(L,-1); lua_pop(L,1);
lua_pop(L,1);
lpRect = &rt;
}
n_kids = lua_objlen(L,kids);
kids_arr = (HWND *)malloc(sizeof(HWND)*n_kids);
for (i = 0; i < n_kids; ++i) {
Window *w;
lua_rawgeti(L,kids,i+1);
w = Window_arg(L,-1);
kids_arr[i] = w->hwnd;
}
TileWindows(parent->hwnd,horiz ? MDITILE_HORIZONTAL : MDITILE_VERTICAL, lpRect, n_kids, kids_arr);
free(kids_arr);
return 0;
}
/// Miscellaneous functions.
// @section miscellaneous
static int push_new_File(lua_State *L,HANDLE hread, HANDLE hwrite);
/// sleep and use no processing time.
// @param millisec sleep period
// @function sleep
static int l_sleep(lua_State *L) {
int millisec = luaL_checkinteger(L,1);
#line 557 "winapi.l.c"
release_mutex();
Sleep(millisec);
lock_mutex();
return 0;
}
/// show a message box.
// @param caption for dialog
// @param msg the message
// @param btns (default 'ok') one of 'ok','ok-cancel','yes','yes-no',
// "abort-retry-ignore", "retry-cancel", "yes-no-cancel"
// @param icon (default 'information') one of 'information','question','warning','error'
// @return a string giving the pressed button: one of 'ok','yes','no','cancel',
// 'try','abort' and 'retry'
// @see message.lua
// @function show_message
static int l_show_message(lua_State *L) {
const char *caption = luaL_checklstring(L,1,NULL);
const char *msg = luaL_checklstring(L,2,NULL);
const char *btns = luaL_optlstring(L,3,"ok",NULL);
const char *icon = luaL_optlstring(L,4,"information",NULL);
#line 574 "winapi.l.c"
int res, type;
WCHAR capb [512];
type = mb_const(btns) | mb_const(icon);
wstring_buff(caption,capb,sizeof(capb));
res = MessageBoxW( NULL, wstring(msg), capb, type);
lua_pushstring(L,mb_result(res));
return 1;
}
/// make a beep sound.
// @param type (default 'ok'); one of 'information','question','warning','error'
// @function beep
static int l_beep(lua_State *L) {
const char *icon = luaL_optlstring(L,1,"ok",NULL);
#line 587 "winapi.l.c"
return push_bool(L, MessageBeep(mb_const(icon)));
}
/// copy a file.
// @param src source file
// @param dest destination file
// @param fail_if_exists if true, then cannot copy onto existing file
// @function copy_file
static int l_copy_file(lua_State *L) {
const char *src = luaL_checklstring(L,1,NULL);
const char *dest = luaL_checklstring(L,2,NULL);
int fail_if_exists = luaL_optinteger(L,3,0);
#line 596 "winapi.l.c"
return push_bool(L, CopyFile(src,dest,fail_if_exists));
}
/// output text to the system debugger.
// A uility such as [DebugView](http://technet.microsoft.com/en-us/sysinternals/bb896647)
// can show the output
// @param str text
// @function output_debug_string
static int l_output_debug_string(lua_State *L) {
const char *str = luaL_checklstring(L,1,NULL);
#line 605 "winapi.l.c"
OutputDebugString(str);
return 0;
}
/// move a file.
// @param src source file
// @param dest destination file
// @function move_file
static int l_move_file(lua_State *L) {
const char *src = luaL_checklstring(L,1,NULL);
const char *dest = luaL_checklstring(L,2,NULL);
#line 614 "winapi.l.c"
return push_bool(L, MoveFile(src,dest));
}
#define wconv(name) (name ? wstring_buff(name,w##name,sizeof(w##name)) : NULL)
/// execute a shell command.
// @param verb the action (e.g. 'open' or 'edit') can be nil.
// @param file the command
// @param parms any parameters (optional)
// @param dir the working directory (optional)
// @param show the window show flags (default is SW_SHOWNORMAL)
// @function shell_exec
static int l_shell_exec(lua_State *L) {
const char *verb = lua_tostring(L,1);
const char *file = luaL_checklstring(L,2,NULL);
const char *parms = lua_tostring(L,3);
const char *dir = lua_tostring(L,4);
int show = luaL_optinteger(L,5,SW_SHOWNORMAL);
#line 627 "winapi.l.c"
WCHAR wverb[128], wfile[MAX_WPATH], wdir[MAX_WPATH], wparms[MAX_WPATH];
int res = (DWORD_PTR)ShellExecuteW(NULL,wconv(verb),wconv(file),wconv(parms),wconv(dir),show) > 32;
return push_bool(L, res);
}
/// copy text onto the clipboard.
// @param text the text
// @function set_clipboard
static int l_set_clipboard(lua_State *L) {
const char *text = luaL_checklstring(L,1,NULL);
#line 636 "winapi.l.c"
HGLOBAL glob;
LPWSTR p;
int bufsize = 3*strlen(text);
if (! OpenClipboard(NULL)) {
return push_perror(L,"openclipboard");
}
EmptyClipboard();
glob = GlobalAlloc(GMEM_MOVEABLE, bufsize);
p = (LPWSTR)GlobalLock(glob);
wstring_buff(text,p,bufsize);
GlobalUnlock(glob);
if (SetClipboardData(CF_UNICODETEXT,glob) == NULL) {
CloseClipboard();
return push_error(L);
}
CloseClipboard();
return 0;
}
/// get the text on the clipboard.
// @return the text
// @function get_clipboard
static int l_get_clipboard(lua_State *L) {
HGLOBAL glob;
LPCWSTR p;
if (! OpenClipboard(NULL)) {
return push_perror(L,"openclipboard");
}
glob = GetClipboardData(CF_UNICODETEXT);
if (glob == NULL) {
CloseClipboard();
return push_error(L);
}
p = GlobalLock(glob);
push_wstring(L,p);
GlobalUnlock(glob);
CloseClipboard();
return 1;
}
/// open console i/o.
// @return @{File}
// @function get_console
static int l_get_console(lua_State *L) {
HANDLE w = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE r = GetStdHandle(STD_INPUT_HANDLE);
return push_new_File(L,r,w);
}
static int l_pipe(lua_State *L) {
HANDLE hRead, hWrite;
if (CreatePipe(&hRead,&hWrite,NULL,0) != 0) {
push_new_File(L,hRead,NULL);
push_new_File(L,NULL,hWrite);
return 2;
} else {
return push_error(L);
}
}
/// open a serial port for reading and writing.
// @param defn a string as used by the [mode command](http://technet.microsoft.com/en-us/library/cc732236%28WS.10%29.aspx)
// @return @{File}
// @function open_serial
static int l_open_serial(lua_State *L) {
const char *defn = luaL_checklstring(L,1,NULL);
#line 701 "winapi.l.c"
DCB dcb = {0};
char port[20];
HANDLE hSerial;
const char *p = defn;
char *q = port;
for (; *p != ' '; p++) {
*q++ = *p;
}
*q = '\0';
dcb.DCBlength = sizeof(dcb);
hSerial = CreateFile(port,GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hSerial == INVALID_HANDLE_VALUE) {
return push_perror(L,"createfile");
}
GetCommState(hSerial,&dcb);
if (! BuildCommDCB(defn,&dcb)) {
CloseHandle(hSerial);
return push_perror(L,"buildcom");
}
if (! SetCommState(hSerial,&dcb)) {
CloseHandle(hSerial);
return push_perror(L,"setcomm");
}
return push_new_File(L,hSerial,hSerial);
}
static int push_wait_result(lua_State *L, DWORD res) {
if (res == WAIT_OBJECT_0) {
lua_pushvalue(L,1);
lua_pushliteral(L,"OK");
return 2;
} else if (res == WAIT_TIMEOUT) {
lua_pushvalue(L,1);
lua_pushliteral(L,"TIMEOUT");
return 2;
} else {
return push_error(L);
}
}
static int wait_single(HANDLE h, int timeout) {
DWORD res;
release_mutex();
res = WaitForSingleObject (h, timeout);
lock_mutex();
return res;
}
static int push_wait(lua_State *L, HANDLE h, int timeout) {
return push_wait_result(L,wait_single(h,timeout));
}
static int push_wait_async(lua_State *L, HANDLE h, int timeout, int callback);
/// The Event class.
// @type Event
#line 761 "winapi.l.c"
typedef struct {
HANDLE hEvent;
} Event;
#define Event_MT "Event"
Event * Event_arg(lua_State *L,int idx) {
Event *this = (Event *)luaL_checkudata(L,idx,Event_MT);
luaL_argcheck(L, this != NULL, idx, "Event expected");
return this;
}
static void Event_ctor(lua_State *L, Event *this, HANDLE h);
static int push_new_Event(lua_State *L,HANDLE h) {
Event *this = (Event *)lua_newuserdata(L,sizeof(Event));
luaL_getmetatable(L,Event_MT);
lua_setmetatable(L,-2);
Event_ctor(L,this,h);
return 1;
}
static void Event_ctor(lua_State *L, Event *this, HANDLE h) {
#line 762 "winapi.l.c"
this->hEvent = h;
}
/// wait for this event to be signalled.
// @param timeout optional timeout in millisec; defaults to waiting indefinitely.
// @return this event object
// @return either "OK" or "TIMEOUT"
// @function wait
static int l_Event_wait(lua_State *L) {
Event *this = Event_arg(L,1);
int timeout = luaL_optinteger(L,2,0);
#line 771 "winapi.l.c"
return push_wait(L,this->hEvent, TIMEOUT(timeout));
}
/// run callback when this process is finished.
// @param callback the callback