summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfluxgen <fluxgen>2002-01-07 23:44:09 (GMT)
committerfluxgen <fluxgen>2002-01-07 23:44:09 (GMT)
commit2765d34a196cf204db2c5d57d63e254cc2b90d8e (patch)
treeee4bee87327ac7335ad7660f1bbb40afa86f8c82 /src
parent472602d2bc2dc6e63ce260549068b765ef615d8f (diff)
downloadfluxbox_lack-2765d34a196cf204db2c5d57d63e254cc2b90d8e.zip
fluxbox_lack-2765d34a196cf204db2c5d57d63e254cc2b90d8e.tar.bz2
Removed Fluxbox and ScreenInfo dep, moved from strtok to StringUtil::stringtok
Diffstat (limited to 'src')
-rw-r--r--src/Keys.cc140
1 files changed, 74 insertions, 66 deletions
diff --git a/src/Keys.cc b/src/Keys.cc
index 59a8789..8b15f69 100644
--- a/src/Keys.cc
+++ b/src/Keys.cc
@@ -19,6 +19,7 @@
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE. 20// DEALINGS IN THE SOFTWARE.
21 21
22//$Id: Keys.cc,v 1.4 2002/01/07 23:44:09 fluxgen Exp $
22 23
23#ifdef HAVE_CONFIG_H 24#ifdef HAVE_CONFIG_H
24# include "config.h" 25# include "config.h"
@@ -26,9 +27,7 @@
26 27
27#include "Keys.hh" 28#include "Keys.hh"
28 29
29#ifndef _FLUXBOX_HH_ 30#include "StringUtil.hh"
30# include "fluxbox.hh"
31#endif
32 31
33#ifdef HAVE_STDIO_H 32#ifdef HAVE_STDIO_H
34# include <stdio.h> 33# include <stdio.h>
@@ -70,6 +69,8 @@
70 69
71#include <iostream> 70#include <iostream>
72#include <fstream> 71#include <fstream>
72#include <vector>
73#include <cassert>
73 74
74using namespace std; 75using namespace std;
75 76
@@ -122,13 +123,18 @@ Keys::t_actionstr Keys::m_actionlist[] = {
122 {0, LASTKEYGRAB} 123 {0, LASTKEYGRAB}
123 }; 124 };
124 125
125Keys::Keys(char *filename) { 126Keys::Keys(Display *display, char *filename):
126 m_abortkey=0; 127m_abortkey(0),
127 load(filename); 128m_display(display)
129{
130 assert(display);
131 if (filename)
132 load(filename);
128} 133}
129 134
130Keys::~Keys() { 135Keys::~Keys() {
131 deleteTree(); 136 deleteTree();
137 ungrabKeys();
132} 138}
133 139
134//--------- deleteTree ----------- 140//--------- deleteTree -----------
@@ -146,27 +152,29 @@ void Keys::deleteTree() {
146 } 152 }
147} 153}
148 154
155//-------- ungrabKeys ---------
156// Ungrabs the keys
157//-----------------------------
158void Keys::ungrabKeys() {
159 for (int screen=0; screen<ScreenCount(m_display); screen++) {
160 XUngrabKey(m_display, AnyKey, AnyModifier,
161 RootWindow(m_display, screen));
162 }
163}
164
149//-------------- load ---------------- 165//-------------- load ----------------
150// Load and grab keys 166// Load and grab keys
151// Returns true on success else false 167// Returns true on success else false
152// TODO: error checking and (nls on them? ) 168// TODO: error checking
153// possible replacement of strtok
154//------------------------------------ 169//------------------------------------
155bool Keys::load(char *filename) { 170bool Keys::load(char *filename) {
156 if (!filename) 171 if (!filename)
157 return false; 172 return false;
158 173
159 Fluxbox *fluxbox = Fluxbox::instance();
160 Display *display = fluxbox->getXDisplay();
161 ScreenInfo *screeninfo=0;
162 //ungrab all keys 174 //ungrab all keys
163 int screen=0; 175 ungrabKeys();
164 while ((screeninfo = fluxbox->getScreenInfo(screen++)) ) { 176
165 XUngrabKey(display, AnyKey, AnyModifier, 177 XSync(m_display, False);
166 screeninfo->getRootWindow());
167 }
168
169 XSync(display, False);
170 178
171 //open the file 179 //open the file
172 ifstream infile(filename); 180 ifstream infile(filename);
@@ -174,35 +182,41 @@ bool Keys::load(char *filename) {
174 return false; 182 return false;
175 183
176 184
177 char *linebuffer = new char[1024]; 185 auto_ptr<char> linebuffer(new char[1024]);
178 int line=0; 186
179 int linepos=0; //position in the line 187 int line=0;//current line, so we can tell the user where the fault is
180 188
181 while (!infile.eof()) { 189 while (!infile.eof()) {
182 infile.getline(linebuffer, 1024); 190
191 infile.getline(linebuffer.get(), 1024);
183 192
184 line++; 193 line++;
185 char *val = strtok(linebuffer, " "); 194 vector<string> val;
186 linepos = (val==0 ? 0 : strlen(val) + 1); 195 //Parse arguments
187 196 StringUtil::stringtok(val, linebuffer.get());
188 int numarg = 1; 197 //must have at least 1 argument
198 if (val.size()<=0)
199 continue;
200
201
189 unsigned int key=0, mod=0; 202 unsigned int key=0, mod=0;
190 char keyarg=0; 203 char keyarg=0;
191 t_key *current_key=0, *last_key=0; 204 t_key *current_key=0, *last_key=0;
192 205
193 while (val) { 206 for (unsigned int argc=0; argc<val.size(); argc++) {
194 207
195 if (val[0]!=':') { 208 if (val[argc][0]!=':') {
196 keyarg++; 209 keyarg++;
197 if (keyarg==1) //first arg is modifier 210 if (keyarg==1) //first arg is modifier
198 mod = getModifier(val); 211 mod = getModifier(val[argc].c_str());
199 else if (keyarg>1) { 212 else if (keyarg>1) {
200 213
201 //keyarg=0; 214 //keyarg=0;
202 int tmpmod=getModifier(val); 215 int tmpmod=getModifier(val[argc].c_str());
203 if(tmpmod) mod|=tmpmod; //If it's a modifier 216 if(tmpmod)
217 mod|=tmpmod; //If it's a modifier
204 else{ 218 else{
205 key = getKey(val); // else get the key 219 key = getKey(val[argc].c_str()); // else get the key
206 if (!current_key) { 220 if (!current_key) {
207 current_key = new t_key(key, mod); 221 current_key = new t_key(key, mod);
208 last_key = current_key; 222 last_key = current_key;
@@ -216,19 +230,18 @@ bool Keys::load(char *filename) {
216 230
217 } else { 231 } else {
218 232
219 val++; //ignore the ':'
220
221 unsigned int i=0; 233 unsigned int i=0;
222 234
223 for (i=0; i< LASTKEYGRAB; i++) { 235 for (i=0; i< LASTKEYGRAB; i++) {
224 if (strcasecmp(m_actionlist[i].string, val) == 0) 236 // +1 on the val[argc] because we dont want to compare the ':'
237 if (strcasecmp(m_actionlist[i].string, val[argc].c_str()+1) == 0)
225 break; 238 break;
226 } 239 }
227 240
228 if (i < LASTKEYGRAB ) { 241 if (i < LASTKEYGRAB ) {
229 if (!current_key) { 242 if (!current_key) {
230 cerr<<"Error on line: "<<line<<endl; 243 cerr<<"Error on line: "<<line<<endl;
231 cerr<<linebuffer<<endl; 244 cerr<<linebuffer.get()<<endl;
232 delete current_key; 245 delete current_key;
233 current_key = 0; 246 current_key = 0;
234 last_key = 0; 247 last_key = 0;
@@ -252,7 +265,9 @@ bool Keys::load(char *filename) {
252 265
253 last_key->action = m_actionlist[i].action; 266 last_key->action = m_actionlist[i].action;
254 if (last_key->action == Keys::EXECUTE) 267 if (last_key->action == Keys::EXECUTE)
255 last_key->execcommand = &linebuffer[linepos]; 268 last_key->execcommand =
269 static_cast<char *>(strcasestr(linebuffer.get(), getActionStr(Keys::EXECUTE))+
270 strlen(getActionStr(Keys::EXECUTE)));
256 271
257 //add the keychain to list 272 //add the keychain to list
258 if (!mergeTree(current_key)) 273 if (!mergeTree(current_key))
@@ -261,8 +276,9 @@ bool Keys::load(char *filename) {
261 #ifdef DEBUG 276 #ifdef DEBUG
262 if (m_actionlist[i].action == Keys::EXECUTE) { 277 if (m_actionlist[i].action == Keys::EXECUTE) {
263 278
264 cerr<<"linepos:"<<linepos<<endl; 279 cerr<<"line:"<<line<<endl;
265 cerr<<"buffer:"<<&linebuffer[linepos]<<endl; 280 cerr<<"buffer:"<<static_cast<char *>(strcasestr(linebuffer.get(), getActionStr(Keys::EXECUTE))+
281 strlen(getActionStr(Keys::EXECUTE)))<<endl;
266 cerr<<"command:"<<last_key->execcommand<<endl; 282 cerr<<"command:"<<last_key->execcommand<<endl;
267 283
268 } 284 }
@@ -275,7 +291,7 @@ bool Keys::load(char *filename) {
275 291
276 } else { //destroy list if no action is found 292 } else { //destroy list if no action is found
277 #ifdef DEBUG 293 #ifdef DEBUG
278 cerr<<"Didnt find action="<<val<<endl; 294 cerr<<"Didnt find action="<<val[argc]<<endl;
279 #endif 295 #endif
280 //destroy current_key ... this will also destroy the last_key 296 //destroy current_key ... this will also destroy the last_key
281 delete current_key; 297 delete current_key;
@@ -284,14 +300,10 @@ bool Keys::load(char *filename) {
284 } 300 }
285 301
286 break; //dont process this linebuffer more 302 break; //dont process this linebuffer more
287 } 303 }
288 numarg++;
289 val = strtok(0, " ");
290 linepos += (val == 0 ? 0 : strlen(val) + 1);
291 } 304 }
292 } 305 }
293 306
294 delete linebuffer;
295 #ifdef DEBUG 307 #ifdef DEBUG
296 showTree(); 308 showTree();
297 #endif 309 #endif
@@ -304,53 +316,50 @@ bool Keys::load(char *filename) {
304//---------------------------------------- 316//----------------------------------------
305void Keys::grabKey(unsigned int key, unsigned int mod) { 317void Keys::grabKey(unsigned int key, unsigned int mod) {
306 318
307 Fluxbox *fluxbox = Fluxbox::instance();
308 Display *display = fluxbox->getXDisplay();
309
310 #ifdef DEBUG 319 #ifdef DEBUG
311 cerr<<__FILE__<<"("<<__LINE__<<"): keycode "<<key<<" mod "<<hex<<mod<<dec<<endl; 320 cerr<<__FILE__<<"("<<__LINE__<<"): keycode "<<key<<" mod "<<hex<<mod<<dec<<endl;
312 #endif 321 #endif
313 int i=0;
314 ScreenInfo *screeninfo=0;
315 322
316 while ((screeninfo = fluxbox->getScreenInfo(i++)) ) { 323 for (int screen=0; screen<ScreenCount(m_display); screen++) {
317 Window root = screeninfo->getRootWindow(); 324
318 XGrabKey(display, key, mod, 325 Window root = RootWindow(m_display, screen);
326
327 XGrabKey(m_display, key, mod,
319 root, True, 328 root, True,
320 GrabModeAsync, GrabModeAsync); 329 GrabModeAsync, GrabModeAsync);
321 330
322 // Grab with numlock, capslock and scrlock 331 // Grab with numlock, capslock and scrlock
323 332
324 //numlock 333 //numlock
325 XGrabKey(display, key, mod|Mod2Mask, 334 XGrabKey(m_display, key, mod|Mod2Mask,
326 root, True, 335 root, True,
327 GrabModeAsync, GrabModeAsync); 336 GrabModeAsync, GrabModeAsync);
328 //scrolllock 337 //scrolllock
329 XGrabKey(display, key, mod|Mod5Mask, 338 XGrabKey(m_display, key, mod|Mod5Mask,
330 root, True, 339 root, True,
331 GrabModeAsync, GrabModeAsync); 340 GrabModeAsync, GrabModeAsync);
332 //capslock 341 //capslock
333 XGrabKey(display, key, mod|LockMask, 342 XGrabKey(m_display, key, mod|LockMask,
334 root, True, 343 root, True,
335 GrabModeAsync, GrabModeAsync); 344 GrabModeAsync, GrabModeAsync);
336 345
337 //capslock+numlock 346 //capslock+numlock
338 XGrabKey(display, key, mod|LockMask|Mod2Mask, 347 XGrabKey(m_display, key, mod|LockMask|Mod2Mask,
339 root, True, 348 root, True,
340 GrabModeAsync, GrabModeAsync); 349 GrabModeAsync, GrabModeAsync);
341 350
342 //capslock+scrolllock 351 //capslock+scrolllock
343 XGrabKey(display, key, mod|LockMask|Mod5Mask, 352 XGrabKey(m_display, key, mod|LockMask|Mod5Mask,
344 root, True, 353 root, True,
345 GrabModeAsync, GrabModeAsync); 354 GrabModeAsync, GrabModeAsync);
346 355
347 //capslock+numlock+scrolllock 356 //capslock+numlock+scrolllock
348 XGrabKey(display, key, mod|Mod2Mask|Mod5Mask|LockMask, 357 XGrabKey(m_display, key, mod|Mod2Mask|Mod5Mask|LockMask,
349 root, True, 358 root, True,
350 GrabModeAsync, GrabModeAsync); 359 GrabModeAsync, GrabModeAsync);
351 360
352 //numlock+scrollLock 361 //numlock+scrollLock
353 XGrabKey(display, key, mod|Mod2Mask|Mod5Mask, 362 XGrabKey(m_display, key, mod|Mod2Mask|Mod5Mask,
354 root, True, 363 root, True,
355 GrabModeAsync, GrabModeAsync); 364 GrabModeAsync, GrabModeAsync);
356 365
@@ -363,13 +372,13 @@ void Keys::grabKey(unsigned int key, unsigned int mod) {
363// else zero on failure. 372// else zero on failure.
364// TODO fix more masks 373// TODO fix more masks
365//---------------------------------------- 374//----------------------------------------
366unsigned int Keys::getModifier(char *modstr) { 375unsigned int Keys::getModifier(const char *modstr) {
367 if (!modstr) 376 if (!modstr)
368 return 0; 377 return 0;
369 struct t_modlist{ 378 struct t_modlist{
370 char *string; 379 char *string;
371 unsigned int mask; 380 unsigned int mask;
372 bool operator == (char *modstr) { 381 bool operator == (const char *modstr) {
373 return (strcasecmp(string, modstr) == 0 && mask !=0); 382 return (strcasecmp(string, modstr) == 0 && mask !=0);
374 } 383 }
375 } modlist[] = { 384 } modlist[] = {
@@ -395,12 +404,11 @@ unsigned int Keys::getModifier(char *modstr) {
395// Returns keycode of keystr on success 404// Returns keycode of keystr on success
396// else it returns zero 405// else it returns zero
397//----------------------------------- 406//-----------------------------------
398unsigned int Keys::getKey(char *keystr) { 407unsigned int Keys::getKey(const char *keystr) {
399 if (!keystr) 408 if (!keystr)
400 return 0; 409 return 0;
401 return XKeysymToKeycode(Fluxbox::instance()->getXDisplay(), 410 return XKeysymToKeycode(m_display,
402 XStringToKeysym 411 XStringToKeysym(keystr));
403 (keystr));
404} 412}
405 413
406//--------- getAction ----------------- 414//--------- getAction -----------------