Skip to content

Commit 1e0b12c

Browse files
committed
exports
1 parent 1eeb448 commit 1e0b12c

10 files changed

+196
-18
lines changed

ahkmingw_win.depend

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,29 @@
2020
<ctype.h>
2121
<olectl.h>
2222

23-
1251585311 l:\source\ahkmingw\globaldata.h
23+
1252480327 l:\source\ahkmingw\globaldata.h
2424
"hook.h"
2525
"clipboard.h"
2626
"script.h"
2727
"os_version.h"
2828

29-
1251608817 l:\source\ahkmingw\hook.h
29+
1252480977 l:\source\ahkmingw\hook.h
3030
"stdafx.h"
3131
"hotkey.h"
3232

3333
1251585311 l:\source\ahkmingw\hotkey.h
3434
"keyboard_mouse.h"
3535
"script.h"
3636

37-
1251585311 l:\source\ahkmingw\keyboard_mouse.h
37+
1252480327 l:\source\ahkmingw\keyboard_mouse.h
3838
"defines.h"
3939

4040
1252041773 l:\source\ahkmingw\defines.h
4141
"stdafx.h"
4242
<pshpack1.h>
4343
<pshpack4.h>
4444

45-
1252477430 l:\source\ahkmingw\script.h
45+
1252480977 l:\source\ahkmingw\script.h
4646
"stdafx.h"
4747
"defines.h"
4848
"SimpleHeap.h"
@@ -68,7 +68,7 @@
6868
"stdafx.h"
6969
"defines.h"
7070

71-
1252041773 l:\source\ahkmingw\util.h
71+
1252480327 l:\source\ahkmingw\util.h
7272
"stdafx.h"
7373
"defines.h"
7474

@@ -80,7 +80,7 @@
8080

8181
1251585311 l:\source\ahkmingw\lib\exearc_read.h
8282

83-
1252475065 l:\source\ahkmingw\exports.h
83+
1252480326 l:\source\ahkmingw\exports.h
8484

8585
1251585311 l:\source\ahkmingw\os_version.h
8686
<windows.h>

exports.cpp

+143
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,56 @@ EXPORT VarSizeType ahkgetvar(char *name, char *output)
99
Var *ahkvar = g_script.FindOrAddVar(name);
1010
return ahkvar->Get(output); // var.getText() added in V1.
1111
}
12+
EXPORT int ahkassign(char *name, char *value) // ahkwine 0.1
13+
{
14+
Var *var;
15+
if ( !(var = g_script.FindOrAddVar(name, strlen(name))) )
16+
return -1; // Realistically should never happen.
17+
var->Assign(value);
18+
return 0; // success
19+
}
20+
EXPORT unsigned int ahkFindFunc(char *funcname)
21+
{
22+
return (unsigned int)g_script.FindFunc(funcname);
23+
}
24+
25+
void BIF_FindFunc(ExprTokenType &aResultToken, ExprTokenType *aParam[], int aParamCount) // Added in Nv8.
26+
{
27+
// Set default return value in case of early return.
28+
aResultToken.symbol = SYM_INTEGER ;
29+
aResultToken.marker = "";
30+
// Get the first arg, which is the string used as the source of the extraction. Call it "findfunc" for clarity.
31+
char funcname_buf[MAX_NUMBER_SIZE]; // A separate buf because aResultToken.buf is sometimes used to store the result.
32+
char *funcname = ExprTokenToString(*aParam[0], funcname_buf); // Remember that aResultToken.buf is part of a union, though in this case there's no danger of overwriting it since our result will always be of STRING type (not int or float).
33+
int funcname_length = (int)EXPR_TOKEN_LENGTH(aParam[0], funcname);
34+
aResultToken.value_int64 = (__int64)ahkFindFunc(funcname);
35+
return;
36+
}
37+
EXPORT int ahkLabel(char *aLabelName)
38+
{
39+
Label *aLabel = g_script.FindLabel(aLabelName) ;
40+
if (aLabel)
41+
{
42+
PostMessage(g_hWnd, AHK_EXECUTE_LABEL, (LPARAM)aLabel, (LPARAM)aLabel);
43+
return 0;
44+
}
45+
else
46+
return -1;
47+
}
48+
49+
EXPORT int ahkKey(char *keyname) // WPARAM key, PKBDLLHOOKSTRUCT event
50+
{
51+
// modLR_type modifiersLR_now = -; // sSendMode ? sEventModifiersLR : GetModifierLRState();
52+
// SetModifierLRState((modifiersLR_now | MOD_LALT) & ~(MOD_RALT | MOD_LCONTROL | MOD_RCONTROL | MOD_LSHIFT | MOD_RSHIFT)
53+
// , modifiersLR_now, NULL, false // Pass false because there's no need to disguise the down-event of LALT.
54+
// , true, KEY_IGNORE); // Pass true so that any release of RALT is disguised (Win is never released here).
1255

56+
sc_type aSC = TextToSC(keyname);
57+
vk_type aVK = TextToVK(keyname);
58+
// KeyEvent(KEYDOWNANDUP, aVK, 0, 0, false, 0);
59+
keybd_event((byte)aVK, (byte)aSC, 0, 0);
60+
return 0;
61+
}
1362

1463
// Naveen: v6 addFile()
1564
// Todo: support for #Directives, and proper treatment of mIsReadytoExecute
@@ -67,3 +116,97 @@ void BIF_Import(ExprTokenType &aResultToken, ExprTokenType *aParam[], int aParam
67116
#endif
68117
return;
69118
}
119+
120+
EXPORT int ahkFunction(char *func, char *param1, char *param2, char *param3, char *param4)
121+
{
122+
Func *aFunc = g_script.FindFunc(func) ;
123+
if (aFunc)
124+
{
125+
g_script.mTempFunc = aFunc ;
126+
PostMessage(g_hWnd, AHK_EXECUTE_FUNCTION, (WPARAM)param1, (LPARAM)param2);
127+
return 0;
128+
}
129+
else
130+
return -1;
131+
}
132+
133+
bool callFunc(WPARAM awParam, LPARAM alParam)
134+
{
135+
Func &func = *(Func *)g_script.mTempFunc ;
136+
if (!INTERRUPTIBLE_IN_EMERGENCY)
137+
return false;
138+
139+
if (g_nThreads >= g_MaxThreadsTotal)
140+
// Below: Only a subset of ACT_IS_ALWAYS_ALLOWED is done here because:
141+
// 1) The omitted action types seem too obscure to grant always-run permission for msg-monitor events.
142+
// 2) Reduction in code size.
143+
if (func.mJumpToLine->mActionType != ACT_EXITAPP && func.mJumpToLine->mActionType != ACT_RELOAD)
144+
return false;
145+
146+
// Need to check if backup is needed in case script explicitly called the function rather than using
147+
// it solely as a callback. UPDATE: And now that max_instances is supported, also need it for that.
148+
// See ExpandExpression() for detailed comments about the following section.
149+
VarBkp *var_backup = NULL; // If needed, it will hold an array of VarBkp objects.
150+
int var_backup_count; // The number of items in the above array.
151+
if (func.mInstances > 0) // Backup is needed.
152+
if (!Var::BackupFunctionVars(func, var_backup, var_backup_count)) // Out of memory.
153+
return false;
154+
// Since we're in the middle of processing messages, and since out-of-memory is so rare,
155+
// it seems justifiable not to have any error reporting and instead just avoid launching
156+
// the new thread.
157+
158+
// Since above didn't return, the launch of the new thread is now considered unavoidable.
159+
160+
// See MsgSleep() for comments about the following section.
161+
char ErrorLevel_saved[ERRORLEVEL_SAVED_SIZE];
162+
strlcpy(ErrorLevel_saved, g_ErrorLevel->Contents(), sizeof(ErrorLevel_saved));
163+
164+
global_struct global_saved;
165+
CopyMemory(&global_saved, &g, sizeof(global_struct));
166+
167+
InitNewThread(0, false, true, func.mJumpToLine->mActionType);
168+
169+
170+
// See ExpandExpression() for detailed comments about the following section.
171+
if (func.mParamCount > 0)
172+
{
173+
// Copy the appropriate values into each of the function's formal parameters.
174+
func.mParam[0].var->Assign((char *)awParam); // Assign parameter #1: wParam
175+
if (func.mParamCount > 1) // Assign parameter #2: lParam
176+
{
177+
// v1.0.38.01: LPARAM is now written out as a DWORD because the majority of system messages
178+
// use LPARAM as a pointer or other unsigned value. This shouldn't affect most scripts because
179+
// of the way ATOI64() and ATOU() wrap a negative number back into the unsigned domain for
180+
// commands such as PostMessage/SendMessage.
181+
func.mParam[1].var->Assign((char *)alParam);
182+
}
183+
}
184+
185+
// v1.0.38.04: Below was added to maximize responsiveness to incoming messages. The reasoning
186+
// is similar to why the same thing is done in MsgSleep() prior to its launch of a thread, so see
187+
// MsgSleep for more comments:
188+
g_script.mLastScriptRest = g_script.mLastPeekTime = GetTickCount();
189+
190+
191+
char *return_value;
192+
func.Call(return_value); // Call the UDF.
193+
194+
195+
// Fix for v1.0.47: Must handle return_value BEFORE calling FreeAndRestoreFunctionVars() because return_value
196+
// might be the contents of one of the function's local variables (which are about to be free'd).
197+
/* bool block_further_processing = *return_value; // No need to check the following because they're implied for *return_value!=0: result != EARLY_EXIT && result != FAIL;
198+
if (block_further_processing)
199+
aMsgReply = (LPARAM)ATOI64(return_value); // Use 64-bit in case it's an unsigned number greater than 0x7FFFFFFF, in which case this allows it to wrap around to a negative.
200+
//else leave aMsgReply uninitialized because we'll be returning false later below, which tells our caller
201+
// to ignore aMsgReply.
202+
*/
203+
Var::FreeAndRestoreFunctionVars(func, var_backup, var_backup_count);
204+
ResumeUnderlyingThread(&global_saved, ErrorLevel_saved, true);
205+
206+
return 0 ; // block_further_processing; // If false, the caller will ignore aMsgReply and process this message normally. If true, aMsgReply contains the reply the caller should immediately send for this message.
207+
}
208+
209+
210+
211+
212+

exports.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Naveen v1. #define EXPORT __declspec(dllexport)
1+
// Naveen v1. #define EXPORT __declspec(dllexport)
22
#define EXPORT extern "C" __declspec(dllexport)
33
#define BIF(fun) void fun(ExprTokenType &aResultToken, ExprTokenType *aParam[], int aParamCount)
44

@@ -11,3 +11,11 @@ BIF(BIF_Static) ;
1111
BIF(BIF_Alias) ;
1212
BIF(BIF_CacheEnable) ;
1313
BIF(BIF_GetTokenValue) ;
14+
15+
16+
EXPORT int ahkLabel(char *aLabelName);
17+
EXPORT int ahkFunction(char *func, char *param1, char *param2, char *param3, char *param4);
18+
bool callFunc(WPARAM awParam, LPARAM alParam);
19+
// do not export callFunc, it must be called within script thread
20+
void BIF_Import(ExprTokenType &aResultToken, ExprTokenType *aParam[], int aParamCount);
21+
void BIF_FindFunc(ExprTokenType &aResultToken, ExprTokenType *aParam[], int aParamCount);

globaldata.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -761,3 +761,4 @@ HWND g_HistoryHwndPrev = NULL;
761761

762762
// Also hook related:
763763
DWORD g_TimeLastInputPhysical = GetTickCount();
764+
HKL g_HKL = GetKeyboardLayout(0); // ahkx

globaldata.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ GNU General Public License for more details.
2121
#include "clipboard.h" // For the global clipboard object
2222
#include "script.h" // For the global script object and g_ErrorLevel
2323
#include "os_version.h" // For the global OS_Version object
24-
24+
extern HKL g_HKL; // ahkx
2525
extern HINSTANCE g_hInstance;
2626
extern DWORD g_MainThreadID;
2727
extern DWORD g_HookThreadID;

hook.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ enum UserMessages {AHK_HOOK_HOTKEY = WM_USER, AHK_HOTSTRING, AHK_USER_MENU, AHK_
3737
// with msgs sent by HTML control (AHK_CLIPBOARD_CHANGE) and possibly others (I think WM_USER+100 may be the
3838
// start of a range used by other common controls too). So trying a higher number that's (hopefully) very
3939
// unlikely to be used by OS features.
40-
, AHK_CLIPBOARD_CHANGE, AHK_HOOK_TEST_MSG, AHK_CHANGE_HOOK_STATE, AHK_GETWINDOWTEXT};
40+
, AHK_CLIPBOARD_CHANGE, AHK_HOOK_TEST_MSG, AHK_CHANGE_HOOK_STATE, AHK_GETWINDOWTEXT
41+
, AHK_EXECUTE // ahkx
42+
, AHK_EXECUTE_FUNCTION
43+
, AHK_EXECUTE_LABEL
44+
};
4145
// NOTE: TRY NEVER TO CHANGE the specific numbers of the above messages, since some users might be
4246
// using the Post/SendMessage commands to automate AutoHotkey itself. Here is the original order
4347
// that should be maintained:

keyboard_mouse.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ GNU General Public License for more details.
136136
// This saves 60K of memory in one place, and possibly there are other large savings too.
137137
// The following older comment dates back to 2003/inception and I don't remember its exact intent,
138138
// but there is no current storage of mouse message constants in scan code variables:
139-
// OLD: Although only need 9 bits for compressed and 16 for uncompressed scan code, use a full 32 bits
139+
// OLD: Although only need 9 bits for compressed and 16 for uncompressed scan code, use a full 32 bits
140140
// so that mouse messages (WPARAM) can be stored as scan codes. Formerly USHORT (which is always 16-bit).
141141
typedef USHORT sc_type; // Scan code.
142142
typedef UCHAR vk_type; // Virtual key.
@@ -313,12 +313,12 @@ bool ActiveWindowLayoutHasAltGr();
313313
ResultType LayoutHasAltGr(HKL aLayout, ResultType aHasAltGr = LAYOUT_UNDETERMINED);
314314

315315
//---------------------------------------------------------------------
316-
316+
extern HKL g_HKL; // ahkx
317317
char *SCtoKeyName(sc_type aSC, char *aBuf, int aBufSize);
318318
char *VKtoKeyName(vk_type aVK, sc_type aSC, char *aBuf, int aBufSize);
319319
sc_type TextToSC(char *aText);
320320
vk_type TextToVK(char *aText, modLR_type *pModifiersLR = NULL, bool aExcludeThoseHandledByScanCode = false
321-
, bool aAllowExplicitVK = true, HKL aKeybdLayout = GetKeyboardLayout(0));
321+
, bool aAllowExplicitVK = true, HKL aKeybdLayout = g_HKL);
322322
vk_type CharToVKAndModifiers(char aChar, modLR_type *pModifiersLR, HKL aKeybdLayout);
323323
vk_type TextToSpecial(char *aText, UINT aTextLength, KeyEventTypes &aEventTypem, modLR_type &aModifiersLR
324324
, bool aUpdatePersistent);

script.h

+4
Original file line numberDiff line numberDiff line change
@@ -2328,6 +2328,10 @@ class Script
23282328
UINT mLineCount; // The number of lines.
23292329
Label *mFirstLabel, *mLastLabel; // The first and last labels in the linked list.
23302330
Func *mFirstFunc, *mLastFunc; // The first and last functions in the linked list.
2331+
Line *mTempLine; // for use with dll Execute # Naveen N9
2332+
Label *mTempLabel; // for use with dll Execute # Naveen N9
2333+
Func *mTempFunc; // for use with dll Execute # Naveen N9
2334+
23312335
Var **mVar, **mLazyVar; // Array of pointers-to-variable, allocated upon first use and later expanded as needed.
23322336
int mVarCount, mVarCountMax, mLazyVarCount; // Count of items in the above array as well as the maximum capacity.
23332337
WinGroup *mFirstGroup, *mLastGroup; // The first and last variables in the linked list.

script2.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,7 @@ ResultType Line::PerformShowWindow(ActionTypeType aActionType, char *aTitle, cha
16861686
// it probably does since there's absolutely no mention to the contrary
16871687
// anywhere on MS's site or on the web. But clearly, if it does work, it
16881688
// does so only because Async() doesn't really post the message to the thread's
1689-
// queue, instead opting for more aggresive measures. Thus, it seems best
1689+
// queue, instead opting for more aggresive s. Thus, it seems best
16901690
// to do it this way to have maximum confidence in it:
16911691
//if (nCmdShow == SW_FORCEMINIMIZE) // Safer not to use ShowWindowAsync() in this case.
16921692
ShowWindow(target_window, nCmdShow);
@@ -5238,6 +5238,18 @@ LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lPar
52385238
// such as when/if it yields our timeslice upon returning FALSE (uncertain/unlikely, but in any case
52395239
// it might do more harm than good).
52405240
return 0;
5241+
5242+
case AHK_EXECUTE: // sent from dll host # Naveen N9
5243+
g_script.mTempLine = (Line *)wParam ;
5244+
g_script.mTempLine->ExecUntil(UNTIL_RETURN);
5245+
return 0;
5246+
case AHK_EXECUTE_LABEL:
5247+
g_script.mTempLabel = (Label *)wParam ;
5248+
g_script.mTempLabel->Execute();
5249+
return 0;
5250+
case AHK_EXECUTE_FUNCTION:
5251+
callFunc(wParam, lParam);
5252+
return 0;
52415253

52425254
case AHK_RETURN_PID:
52435255
// This is obsolete in light of WinGet's support for fetching the PID of any window.

util.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,17 @@ inline char *UTOA(unsigned long value, char *buf)
512512
// seems best to use the same approach to avoid calling ToAsciiEx() more than once in cases where a
513513
// script has hotstrings and also uses the Input command. Calling ToAsciiEx() twice in such a case would
514514
// be likely to aggravate its side effects with dead keys as described at length in the hook/Input code).
515-
#define Get_active_window_keybd_layout \
516-
HWND active_window;\
517-
HKL active_window_keybd_layout = GetKeyboardLayout((active_window = GetForegroundWindow())\
518-
? GetWindowThreadProcessId(active_window, NULL) : 0); // When no foreground window, the script's own layout seems like the safest default.
519-
515+
/*
516+
#define Get_active_window_keybd_layout \
517+
HWND active_window;\
518+
HKL active_window_keybd_layout = GetKeyboardLayout((active_window = GetForegroundWindow())\
519+
? GetWindowThreadProcessId(active_window, NULL) : 0); // When no foreground window, the script's own layout seems like the safest default.
520+
521+
*/
522+
#define Get_active_window_keybd_layout \
523+
HWND active_window = GetForegroundWindow();\
524+
HKL active_window_keybd_layout = g_HKL;
525+
// above added in ahkx
520526
#define DATE_FORMAT_LENGTH 14 // "YYYYMMDDHHMISS"
521527
#define IS_LEAP_YEAR(year) ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
522528

0 commit comments

Comments
 (0)