summaryrefslogtreecommitdiff
path: root/windows/keybuddy2/src/hotstrings.cpp
blob: 2c5a3975e376439e8599fdca8bd960be1b0b7d89 (plain)
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
#include "includes.h"

hotString* hotString::pHotStrings=NULL;
int hotString::numHotStrings=0;
WString hotString::hsBuffer;
int hotString::bufferLen=0;
long hotString::lastFocusPtr=0;

// load all hotstrings from file
void hotString::loadHotStrings(){
	
	// in a first pass, just rush through the file and count the number of hotstrings
	
	FILE* pFile;
	int i;
	char buffer;
	wchar unibuffer;
	
	#define FGETUC(pbuf,pfile) fread(pbuf,sizeof(wchar),1,pfile)

	numHotStrings=0;
	bool valueEnd;
	
	pFile = fopen(SRCPATH "hotstrings.txt", "rb");
	if (pFile==NULL) {PromptOK("Can't read hotstrings.txt"); exit(1);}	
	
	fgetc(pFile); // skip BOM
	fgetc(pFile);
	
	while(!feof(pFile)){
		FGETUC(&unibuffer,pFile); // read identifier
		if(	unibuffer>=97 && unibuffer<=122 ){
			
			FGETUC(&unibuffer,pFile); // read =
			FGETUC(&unibuffer,pFile); // read "
			
			valueEnd=false;
			while(!valueEnd){
				FGETUC(&unibuffer,pFile);
				if(unibuffer==34){valueEnd=true;} // read " -> end of value
				if(unibuffer==92){FGETUC(&unibuffer,pFile);} // read \ -> skip next character
			}
			FGETUC(&unibuffer,pFile); // read separator
			if(unibuffer==13){ // line break -> entry complete
				numHotStrings++;
				FGETUC(&unibuffer,pFile); // read chr10
			}
		}
	}

	// now allocate as much memory as needed
	pHotStrings = new hotString[numHotStrings];
	
	// now go through in a second pass and store hotstrings
	int iEntry=0;
	
	rewind(pFile);	
	
	fgetc(pFile); // skip BOM
	fgetc(pFile);
	
	WString* pTarget=NULL;
	fpos_t valueStart;
	int valueLength;
	bool isH;	
	
	while(!feof(pFile)){
		FGETUC(&unibuffer,pFile); // read identifier
		if(feof(pFile)){break;} // sometimes the first feof didnt indicate the end, so check again after read operatio

		isH=false;

		switch(unibuffer){
			case 'h':
			case 'H':
				pTarget=&(hotString::pHotStrings[iEntry].hs);
				isH=true;
				break;
			case 'c':
			case 'C':
				pTarget=&(hotString::pHotStrings[iEntry].winClass);
				break;
			case 't':
			case 'T':
				pTarget=&(hotString::pHotStrings[iEntry].winTitle);
				break;
			case 's':
			case 'S':
				pTarget=&(hotString::pHotStrings[iEntry].value);
				hotString::pHotStrings[iEntry].launch=false;
				break;											
			case 'l':
			case 'L':
				pTarget=&(hotString::pHotStrings[iEntry].value);
				hotString::pHotStrings[iEntry].launch=true;
				break;
			default:
				PromptOK("Error parsing hotstrings.txt: unknown element"); exit(1);
		}		
		
		FGETUC(&unibuffer,pFile); // read =
		FGETUC(&unibuffer,pFile); // read "
		
		fgetpos(pFile, &valueStart);
		
		// read value first to determine its length, then again and store

		valueEnd=false;
		valueLength=0;
		while(!valueEnd){
			FGETUC(&unibuffer,pFile);
			if(unibuffer==34){ // read " -> end of value
				valueEnd=true;
			}
			else{
				if(unibuffer==92){ // read \ -> dont check next character
					FGETUC(&unibuffer,pFile);
					valueLength++;
				}
				else{ // increase number of chars
					valueLength++;
				}
			}
		}
		
		if(isH && valueLength>bufferLen){
			bufferLen=valueLength;
		}
		
		*pTarget = WString(0,valueLength);
		fsetpos(pFile, &valueStart);
		for(i=0;i<valueLength;i++){
			FGETUC(&unibuffer,pFile);
			if(unibuffer==92){FGETUC(&unibuffer,pFile);} // read \ -> skip to next character
			pTarget->Set(i,unibuffer);
		}
		
		FGETUC(&unibuffer,pFile); // read "
		FGETUC(&unibuffer,pFile); // read separator
		if(unibuffer==13){ // line break -> entry complete
			LOGG("Read hotstring: hs=");LOGG(pHotStrings[iEntry].hs);
			LOGG(" winTitle=");LOGG(pHotStrings[iEntry].winTitle);
			LOGG(" winClass=");LOGG(pHotStrings[iEntry].winClass);
			LOGG(" value=");LOGG(pHotStrings[iEntry].value);LOGGNL;			
			iEntry++;
			FGETUC(&unibuffer,pFile); // read chr10
		}
	}
	fclose(pFile);
	
	// create log buffer as long as the longest hotstring
	hsBuffer = WString(0,bufferLen);

	// sort list
	qsort(pHotStrings,numHotStrings,sizeof(hotString),compareHotStrings);
	
}

// compare the hotstringbuffer to all hotstrings and fire if there is a match
// to be more efficient, this could be done using a trie
// however, c++ takes very few time to compare thousands of strings, so this was
// not considered necessary
void hotString::checkHotStrings(){
	int i,k;
	bool hit;
	
	for(i=0;i<numHotStrings;i++){
		
		if(pHotStrings[i].hs.IsEqual(hsBuffer.Right(pHotStrings[i].hs.GetLength()))){ // current hotstring matches typed letters
			
			hit=false;
			
			if(pHotStrings[i].winClass.IsEmpty() && pHotStrings[i].winTitle.IsEmpty()){ // no further conditions, send
				hit=true;
			}
			else{ // check if focus object class and foreground window title match
				WString wt,oc;
				getFocusInfo(oc,wt);

				if( (pHotStrings[i].winClass.IsEmpty() || pHotStrings[i].winClass.IsEqual(oc)) 
					&& (pHotStrings[i].winTitle.IsEmpty() || wt.Find(pHotStrings[i].winTitle)>-1) ){ // ok, everything matches, fire!
					
					hit=true;
				}
			}
			
			if(hit){
							
				for(k=0;k<pHotStrings[i].hs.GetLength();k++){ // backspaces to delete the typement
					KeyBuddy2::SendUNIKey(0xF008,false,HOTSTRING);
					KeyBuddy2::SendUNIKey(0xF008,true,HOTSTRING);
				}
				
				if(!pHotStrings[i].launch){ // send string
					for(k=0;k<pHotStrings[i].value.GetLength();k++){
						KeyBuddy2::SendUNIKey(pHotStrings[i].value[k],false,HOTSTRING);
						KeyBuddy2::SendUNIKey(pHotStrings[i].value[k],true,HOTSTRING);
					}
					break;
					clearBuffer();
				}
				else{ // launch program
					WString cmd=pHotStrings[i].value;

					WString path,exe,params;
					int delim1=-2,delim2=-2;
					
					delim1=cmd.Find("|");
					if(delim1==-1){
						path="";
						exe=cmd;
						params="";
					}
					else{
						delim2=cmd.Find("|",delim1+1);

						if(delim2==-1){
							path=cmd.Left(delim1);
							exe=cmd.Mid(delim1+1);;
							params="";
						}
						else{
							path=cmd.Left(delim1);
							exe=cmd.Mid(delim1+1,delim2-delim1-1);
							params=cmd.Mid(delim2+1);
						}
					}

					ShellExecuteW( NULL, NULL, exe, params, path, SW_SHOWNORMAL );

					LOGG("working directory: ");LOGG(path);LOGGNL;
					LOGG("exe: ");LOGG(exe);LOGGNL;
					LOGG("params: ");LOGG(params);LOGGNL;
				}
			}
		}
	}
}

long hotString::getFocusWindowPtr(){

	DWORD unused;

	long myThread=GetWindowThreadProcessId(KeyBuddy2::pKB2->GetHWND(),&unused);
	long otherThread=GetWindowThreadProcessId(GetForegroundWindow(),&unused);
	
	if(myThread!=otherThread){
		AttachThreadInput(otherThread,myThread,true);
	}
	
	long result=(long)GetFocus();
	
	if(myThread!=otherThread){
		AttachThreadInput(otherThread,myThread,false);
	}

	return result;
}

void hotString::getFocusInfo(WString& objectClass, WString& parentTitle){

	char buffer[256];
	GetClassName((HWND)getFocusWindowPtr(),buffer,255);
	objectClass=WString(buffer);

	GetWindowText(GetForegroundWindow(),buffer,255);
	parentTitle=WString(buffer);
}