Debian Codegraph @

src/ApvlvCmds.cc

raw
1 /*
2 * This file is part of the apvlv package
3 *
4 * Copyright (C) 2008 Alf.
5 *
6 * Contact: Alf <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23 /* @CPPFILE ApvlvCmds.cc
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #include <Qt>
29 #include <cctype>
30 #include <cstring>
31
32 #include "ApvlvCmds.h"
33 #include "ApvlvView.h"
34
35 namespace apvlv
36 {
37 using namespace std;
38 using namespace Qt;
39
40 StringKeyMap Command::mKeyMap = {
41 { "<BS>", Key_Backspace }, { "<Tab>", Key_Tab },
42 { "<CR>", Key_Return }, { "<Esc>", Key_Escape },
43 { "<Space>", Key_Space }, { "<lt>", Key_Less },
44 { "<Bslash>", Key_Backslash }, { "<Bar>", Key_Bar },
45 { "<Del>", Key_Delete }, { "<Up>", Key_Up },
46 { "<Down>", Key_Down }, { "<Left>", Key_Left },
47 { "<Right>", Key_Right }, { "<Help>", Key_Help },
48 { "<Insert>", Key_Insert }, { "<Home>", Key_Home },
49 { "<End>", Key_End }, { "<PageUp>", Key_PageUp },
50 { "<PageDown>", Key_PageDown }, { "<KP_Up>", Key_Up },
51 { "<KP_Down>", Key_Down }, { "<KP_Left>", Key_Left },
52 { "<KP_Right>", Key_Right }, { "<KP_Prior>", Key_MediaPrevious },
53 { "<KP_Next>", Key_MediaNext }, { "<KP_Home>", Key_Home },
54 { "<KP_End>", Key_End },
55 };
56
57 CommandMap ApvlvCmds::mMaps;
58
59 static int
60 keyToControlChar (QKeyEvent *key)
61 {
62 int char_key = key->key ();
63 if (key->modifiers () & Qt::ShiftModifier)
64 {
65 char_key = toupper (char_key);
66 }
67 else
68 {
69 char_key = tolower (char_key);
70 }
71 if (key->modifiers () & ControlModifier)
72 char_key = ctrlValue (char_key);
73
74 return char_key;
75 }
76
77 constexpr static bool
78 isModifierKey (uint k)
79 {
80 switch (k)
81 {
82 case Key_Shift:
83 case Key_CapsLock:
84 case Key_Meta:
85 case Key_Alt:
86 case Key_Super_L:
87 case Key_Super_R:
88 case Key_Hyper_L:
89 case Key_Hyper_R:
90 case Key_Control:
91 return true;
92 default:
93 return false;
94 }
95 }
96
97 Command::Command ()
98 : mType (CmdType::CT_CMD), mHasPreCount (false), mPreCount (1),
99 mOrigin (nullptr)
100 {
101 }
102
103 void
104 Command::type (CmdType type)
105 {
106 mType = type;
107 }
108
109 CmdType
110 Command::type ()
111 {
112 return mType;
113 }
114
115 void
116 Command::push (string_view sv, CmdType type)
117 {
118 mType = type;
119
120 mHasPreCount = false;
121 mPreCount = 1;
122
123 auto s = sv.data ();
124 if (isdigit (*s))
125 {
126 mHasPreCount = true;
127 mPreCount = (signed int)strtol (s, nullptr, 10);
128 do
129 {
130 s++;
131 }
132 while (isdigit (*s));
133 }
134
135 if (*s == ':' || *s == '/' || *s == '?')
136 {
137 mStrCommand = s;
138 mType = CmdType::CT_STRING;
139
140 size_t off = mStrCommand.find ("<CR>");
141 if (off != string::npos)
142 {
143 mStrCommand.erase (off, mStrCommand.length () - off);
144 mType = CmdType::CT_STRING_RETURN;
145 mNext = make_unique<Command> ();
146 mNext->push (s + off + 4);
147 }
148 qDebug () << "set string type command: [" << mStrCommand << "]";
149 return;
150 }
151
152 while (*s != '\0')
153 {
154 s = append (s);
155 }
156 }
157
158 void
159 Command::process (ApvlvView *view)
160 {
161 if (type () == CmdType::CT_STRING)
162 {
163 view->promptCommand (mStrCommand.c_str ());
164 }
165 else if (type () == CmdType::CT_STRING_RETURN)
166 {
167 view->run (mStrCommand.c_str ());
168 }
169 else
170 {
171 for (uint k = 0; k < keyVals ()->size (); ++k)
172 {
173 int key = keyval (k);
174 if (key > 0)
175 view->process (mHasPreCount, preCount (), keyval (k));
176 }
177 }
178
179 if (next () != nullptr)
180 {
181 next ()->process (view);
182 }
183 }
184
185 bool
186 Command::append (QKeyEvent *key)
187 {
188 if (isModifierKey (key->key ()))
189 return false;
190
191 auto char_key = keyToControlChar (key);
192 mKeyVals.push_back (char_key);
193 return true;
194 }
195
196 const char *
197 Command::append (const char *s)
198 {
199 size_t len;
200 const char *e = strchr (s, '>');
201
202 len = strlen (s);
203
204 if (len >= 4 && *s == '<' && (*e != '\0' && *(s + 2) != '-'))
205 {
206 e++;
207 for (const auto &it : mKeyMap)
208 {
209 if (it.first.compare (0, e - s, s) == 0)
210 {
211 mKeyVals.push_back (it.second);
212 return e;
213 }
214 }
215 }
216
217 if (len >= 5 && s[0] == '<' && s[2] == '-' && s[4] == '>')
218 {
219 if (s[1] == 'C')
220 {
221 mKeyVals.push_back (ctrlValue (s[3]));
222 }
223 else
224 {
225 qWarning () << "Can't recognize the symbol: " << s;
226 }
227 return s + 5;
228 }
229 else
230 {
231 mKeyVals.push_back (s[0]);
232 return s + 1;
233 }
234 }
235
236 void
237 Command::setPreCount (int precount)
238 {
239 mPreCount = precount;
240 mHasPreCount = true;
241 }
242
243 int
244 Command::preCount () const
245 {
246 return mPreCount;
247 }
248
249 void
250 Command::origin (Command *ori)
251 {
252 mOrigin = ori;
253 }
254
255 Command *
256 Command::origin ()
257 {
258 return mOrigin;
259 }
260
261 CommandKeyList *
262 Command::keyVals ()
263 {
264 return &mKeyVals;
265 }
266
267 CommandKeyList
268 Command::keyvalv ()
269 {
270 return mKeyVals;
271 }
272
273 Command *
274 Command::next ()
275 {
276 return mNext.get ();
277 }
278
279 int
280 Command::keyval (uint id)
281 {
282 return id >= mKeyVals.size () ? -1 : mKeyVals[id];
283 }
284
285 void
286 ApvlvCmds::buildCommandMap (string_view os, string_view ms)
287 {
288 Command fir;
289 fir.push (os);
290
291 auto *secp = new Command ();
292 secp->push (ms);
293
294 for (auto &mMap : mMaps)
295 {
296 if (mMap.first == fir.keyvalv ())
297 {
298 delete mMap.second;
299 mMap.second = secp;
300 return;
301 }
302 }
303
304 mMaps[fir.keyvalv ()] = secp;
305 }
306
307 ApvlvCmds::ApvlvCmds (ApvlvView *view)
308 {
309 mView = view;
310
311 mState = CmdState::CMD_OK;
312
313 mTimeoutTimer = make_unique<QTimer> (this);
314 QObject::connect (mTimeoutTimer.get (), SIGNAL (timeout ()), this,
315 SLOT (timeoutCallback ()));
316 }
317
318 ApvlvCmds::~ApvlvCmds ()
319 {
320 if (mTimeoutTimer->isActive ())
321 {
322 mTimeoutTimer->stop ();
323 }
324 }
325
326 void
327 ApvlvCmds::append (QKeyEvent *gev)
328 {
329 if (mTimeoutTimer->isActive ())
330 {
331 mTimeoutTimer->stop ();
332 }
333
334 if (mState == CmdState::GETTING_CMD)
335 {
336 CommandKeyList v = mCmdHead->keyvalv ();
337 v.push_back (keyToControlChar (gev));
338 CmdReturn r = isMapCommand (&v);
339 if (r == CmdReturn::NO_MATCH)
340 {
341 process (mCmdHead.get ());
342 mCmdHead.release ();
343 mState = CmdState::CMD_OK;
344 }
345 }
346
347 if (mCmdHead == nullptr)
348 mCmdHead = make_unique<Command> ();
349
350 if (mState == CmdState::CMD_OK)
351 {
352 if (isdigit (int (gev->key ())) && gev->key () != '0')
353 {
354 auto c = char (gev->key ());
355 mCountString += c;
356 mState = CmdState::GETTING_COUNT;
357 mTimeoutTimer->start (3000);
358 return;
359 }
360 }
361
362 else if (mState == CmdState::GETTING_COUNT)
363 {
364 if (isdigit (int (gev->key ())))
365 {
366 auto c = char (gev->key ());
367 mCountString += c;
368 mTimeoutTimer->start (3000);
369 return;
370 }
371 else
372 {
373 if (!mCountString.empty ())
374 {
375 mCmdHead->setPreCount (
376 int (strtol (mCountString.c_str (), nullptr, 10)));
377 mCountString = "";
378 }
379 }
380 }
381
382 bool valid = mCmdHead->append (gev);
383 if (!valid)
384 {
385 mTimeoutTimer->start (3000);
386 return;
387 }
388
389 mState = CmdState::GETTING_CMD;
390 CmdReturn ret = isMapCommand (mCmdHead->keyVals ());
391 if (ret == CmdReturn::NEED_MORE)
392 {
393 mTimeoutTimer->start (3000);
394 return;
395 }
396
397 Command *pcmd;
398 if (ret == CmdReturn::MATCH)
399 {
400 pcmd = getMapCommand (mCmdHead.get ());
401 pcmd->origin (mCmdHead.get ());
402 process (pcmd);
403 pcmd->origin (nullptr);
404 pcmd = nullptr;
405 }
406 else
407 {
408 pcmd = process (mCmdHead.get ());
409 }
410
411 mCmdHead.reset (pcmd);
412 mState = CmdState::CMD_OK;
413 }
414
415 Command *
416 ApvlvCmds::process (Command *cmd)
417 {
418 uint times = 1;
419 Command *orig = cmd->origin ();
420 if (orig != nullptr)
421 {
422 times = orig->preCount ();
423 }
424
425 for (uint i = 0; i < times; ++i)
426 {
427 cmd->process (mView);
428 }
429 return orig;
430 }
431
432 CmdReturn
433 ApvlvCmds::isMapCommand (CommandKeyList *ack)
434 {
435 for (auto &mMap : mMaps)
436 {
437 if (*ack == mMap.first)
438 {
439 return CmdReturn::MATCH;
440 }
441 else
442 {
443 uint i;
444 for (i = 0; i < ack->size (); ++i)
445 {
446 if ((*ack)[i] != mMap.first[i])
447 break;
448 }
449
450 if (i == ack->size ())
451 {
452 return CmdReturn::NEED_MORE;
453 }
454 }
455 }
456
457 return CmdReturn::NO_MATCH;
458 }
459
460 Command *
461 ApvlvCmds::getMapCommand (Command *cmd)
462 {
463 auto it = mMaps.find (*cmd->keyVals ());
464 return it != mMaps.end () ? it->second : nullptr;
465 }
466
467 void
468 ApvlvCmds::timeoutCallback ()
469 {
470 if (mCmdHead != nullptr)
471 {
472 process (mCmdHead.get ());
473 mCmdHead.release ();
474 }
475 mState = CmdState::CMD_OK;
476 }
477 }
478
479 // Local Variables:
480 // mode: c++
481 // End: