forked from thjaeger/easystroke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actiondb.h
419 lines (381 loc) · 12.7 KB
/
actiondb.h
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
/*
* Copyright (c) 2008-2009, Thomas Jaeger <ThJaeger@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __STROKEDB_H__
#define __STROKEDB_H__
#include <string>
#include <map>
#include <set>
#include <boost/serialization/access.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/split_member.hpp>
#include <iostream>
#include "gesture.h"
#include "prefdb.h"
class Action;
class Command;
class SendKey;
class SendText;
class Scroll;
class Ignore;
class Button;
class Misc;
class Ranking;
typedef boost::shared_ptr<Action> RAction;
typedef boost::shared_ptr<Command> RCommand;
typedef boost::shared_ptr<SendKey> RSendKey;
typedef boost::shared_ptr<SendText> RSendText;
typedef boost::shared_ptr<Scroll> RScroll;
typedef boost::shared_ptr<Ignore> RIgnore;
typedef boost::shared_ptr<Button> RButton;
typedef boost::shared_ptr<Misc> RMisc;
typedef boost::shared_ptr<Ranking> RRanking;
class Unique;
class Modifiers;
typedef boost::shared_ptr<Modifiers> RModifiers;
bool mods_equal(RModifiers m1, RModifiers m2);
class Action {
friend class boost::serialization::access;
friend std::ostream& operator<<(std::ostream& output, const Action& c);
template<class Archive> void serialize(Archive & ar, const unsigned int version);
public:
virtual void run() {}
virtual RModifiers prepare() { return RModifiers(); }
virtual const Glib::ustring get_label() const = 0;
};
class Command : public Action {
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version);
Command(const std::string &c) : cmd(c) {}
public:
std::string cmd;
Command() {}
static RCommand create(const std::string &c) { return RCommand(new Command(c)); }
virtual void run();
virtual const Glib::ustring get_label() const { return cmd; }
};
class ModAction : public Action {
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version);
protected:
ModAction() {}
Gdk::ModifierType mods;
ModAction(Gdk::ModifierType mods_) : mods(mods_) {}
virtual RModifiers prepare();
public:
virtual const Glib::ustring get_label() const;
};
class SendKey : public ModAction {
friend class boost::serialization::access;
guint key;
BOOST_SERIALIZATION_SPLIT_MEMBER()
template<class Archive> void load(Archive & ar, const unsigned int version);
template<class Archive> void save(Archive & ar, const unsigned int version) const;
SendKey(guint key_, Gdk::ModifierType mods) :
ModAction(mods), key(key_) {}
public:
SendKey() {}
static RSendKey create(guint key, Gdk::ModifierType mods) {
return RSendKey(new SendKey(key, mods));
}
virtual void run();
virtual RModifiers prepare();
virtual const Glib::ustring get_label() const;
};
BOOST_CLASS_VERSION(SendKey, 1)
#define IS_KEY(act) (act && dynamic_cast<SendKey *>(act.get()))
class SendText : public Action {
friend class boost::serialization::access;
Glib::ustring text;
BOOST_SERIALIZATION_SPLIT_MEMBER()
template<class Archive> void load(Archive & ar, const unsigned int version);
template<class Archive> void save(Archive & ar, const unsigned int version) const;
SendText(Glib::ustring text_) : text(text_) {}
public:
SendText() {}
static RSendText create(Glib::ustring text) { return RSendText(new SendText(text)); }
virtual void run();
virtual const Glib::ustring get_label() const { return text; }
};
class Scroll : public ModAction {
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version);
Scroll(Gdk::ModifierType mods) : ModAction(mods) {}
public:
Scroll() {}
static RScroll create(Gdk::ModifierType mods) { return RScroll(new Scroll(mods)); }
virtual const Glib::ustring get_label() const;
};
#define IS_SCROLL(act) (act && dynamic_cast<Scroll *>(act.get()))
class Ignore : public ModAction {
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version);
Ignore(Gdk::ModifierType mods) : ModAction(mods) {}
public:
Ignore() {}
static RIgnore create(Gdk::ModifierType mods) { return RIgnore(new Ignore(mods)); }
virtual const Glib::ustring get_label() const;
};
#define IS_IGNORE(act) (act && dynamic_cast<Ignore *>(act.get()))
class Button : public ModAction {
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version);
Button(Gdk::ModifierType mods, guint button_) : ModAction(mods), button(button_) {}
guint button;
public:
Button() {}
ButtonInfo get_button_info() const;
static unsigned int get_button(RAction act) {
if (!act)
return 0;
Button *b = dynamic_cast<Button *>(act.get());
if (!b)
return 0;
return b->get_button_info().button;
}
static RButton create(Gdk::ModifierType mods, guint button_) { return RButton(new Button(mods, button_)); }
virtual const Glib::ustring get_label() const;
virtual void run();
};
#define IF_BUTTON(act, b) if (unsigned int b = Button::get_button(act))
class Misc : public Action {
friend class boost::serialization::access;
public:
enum Type { NONE, UNMINIMIZE, SHOWHIDE, DISABLE };
Type type;
private:
template<class Archive> void serialize(Archive & ar, const unsigned int version);
Misc(Type t) : type(t) {}
public:
static const char *types[5];
Misc() {}
virtual const Glib::ustring get_label() const;
static RMisc create(Type t) { return RMisc(new Misc(t)); }
virtual void run();
};
class StrokeSet : public std::set<RStroke> {
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version);
};
// Internal use only
class Click : public Action {
virtual const Glib::ustring get_label() const { return "Click"; }
};
#define IS_CLICK(act) (act && dynamic_cast<Click *>(act.get()))
class StrokeInfo {
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version);
public:
StrokeInfo(RStroke s, RAction a) : action(a) { strokes.insert(s); }
StrokeInfo() {}
StrokeSet strokes;
RAction action;
std::string name;
};
typedef boost::shared_ptr<StrokeInfo> RStrokeInfo;
BOOST_CLASS_VERSION(StrokeInfo, 1)
class Ranking {
static bool show(RRanking r);
int x, y;
public:
RStroke stroke, best_stroke;
RAction action;
double score;
std::string name;
std::multimap<double, std::pair<std::string, RStroke> > r;
static void queue_show(RRanking r, RTriple e);
};
class Unique {
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version);
public:
int level;
int i;
};
class ActionListDiff {
friend class boost::serialization::access;
friend class ActionDB;
template<class Archive> void serialize(Archive & ar, const unsigned int version);
ActionListDiff *parent;
std::set<Unique *> deleted;
std::map<Unique *, StrokeInfo> added;
std::list<Unique *> order;
std::list<ActionListDiff> children;
void update_order() {
int j = 0;
for (std::list<Unique *>::iterator i = order.begin(); i != order.end(); i++, j++) {
(*i)->level = level;
(*i)->i = j;
}
}
void fix_tree(bool rebuild_order) {
if (rebuild_order)
for (std::map<Unique *, StrokeInfo>::iterator i = added.begin(); i != added.end(); i++)
if (!(parent && parent->contains(i->first)))
order.push_back(i->first);
update_order();
for (std::list<ActionListDiff>::iterator i = children.begin(); i != children.end(); i++) {
i->parent = this;
i->level = level + 1;
i->fix_tree(rebuild_order);
}
}
public:
int level;
bool app;
std::string name;
ActionListDiff() : parent(0), level(0), app(false) {}
typedef std::list<ActionListDiff>::iterator iterator;
iterator begin() { return children.begin(); }
iterator end() { return children.end(); }
RStrokeInfo get_info(Unique *id, bool *deleted = 0, bool *stroke = 0, bool *name = 0, bool *action = 0) const;
int order_size() const { return order.size(); }
int size_rec() const {
int size = added.size();
for (std::list<ActionListDiff>::const_iterator i = children.begin(); i != children.end(); i++)
size += i->size_rec();
return size;
}
bool resettable(Unique *id) const {
return parent && (added.count(id) || deleted.count(id)) && parent->contains(id);
}
Unique *add(StrokeInfo &si, Unique *before = 0) {
Unique *id = new Unique;
added.insert(std::pair<Unique *, StrokeInfo>(id, si));
id->level = level;
id->i = order.size();
if (before)
order.insert(std::find(order.begin(), order.end(), before), id);
else
order.push_back(id);
update_order();
return id;
}
void set_action(Unique *id, RAction action) { added[id].action = action; }
void set_strokes(Unique *id, StrokeSet strokes) { added[id].strokes = strokes; }
void set_name(Unique *id, std::string name) { added[id].name = name; }
bool contains(Unique *id) const {
if (deleted.count(id))
return false;
if (added.count(id))
return true;
return parent && parent->contains(id);
}
bool remove(Unique *id) {
bool really = !(parent && parent->contains(id));
if (really) {
added.erase(id);
order.remove(id);
update_order();
} else
deleted.insert(id);
for (std::list<ActionListDiff>::iterator i = children.begin(); i != children.end(); i++)
i->remove(id);
return really;
}
void reset(Unique *id) {
if (!parent)
return;
added.erase(id);
deleted.erase(id);
}
void add_apps(std::map<std::string, ActionListDiff *> &apps) {
if (app)
apps[name] = this;
for (std::list<ActionListDiff>::iterator i = children.begin(); i != children.end(); i++)
i->add_apps(apps);
}
ActionListDiff *add_child(std::string name, bool app) {
children.push_back(ActionListDiff());
ActionListDiff *child = &(children.back());
child->name = name;
child->app = app;
child->parent = this;
child->level = level + 1;
return child;
}
bool remove() {
if (!parent)
return false;
for (std::list<ActionListDiff>::iterator i = parent->children.begin(); i != parent->children.end(); i++) {
if (&*i == this) {
parent->children.erase(i);
return true;
}
}
return false;
}
bool move(Unique *src, Unique *dest) {
if (!src)
return false;
if (src == dest)
return false;
if (parent && parent->contains(src))
return false;
if (dest && parent && parent->contains(dest))
return false;
if (!added.count(src))
return false;
if (dest && !added.count(dest))
return false;
order.remove(src);
order.insert(dest ? std::find(order.begin(), order.end(), dest) : order.end(), src);
update_order();
return true;
}
boost::shared_ptr<std::map<Unique *, StrokeSet> > get_strokes() const;
boost::shared_ptr<std::set<Unique *> > get_ids(bool include_deleted) const;
int count_actions() const {
return (parent ? parent->count_actions() : 0) + order.size() - deleted.size();
}
void all_strokes(std::list<RStroke> &strokes) const;
RAction handle(RStroke s, RRanking &r) const;
// b1 is always reported as b2
void handle_advanced(RStroke s, std::map<guint, RAction> &a, std::map<guint, RRanking> &r, int b1, int b2) const;
~ActionListDiff();
};
BOOST_CLASS_VERSION(ActionListDiff, 1)
class ActionDB {
friend class boost::serialization::access;
friend class ActionDBWatcher;
template<class Archive> void load(Archive & ar, const unsigned int version);
template<class Archive> void save(Archive & ar, const unsigned int version) const;
BOOST_SERIALIZATION_SPLIT_MEMBER()
public:
std::map<std::string, ActionListDiff *> apps;
private:
ActionListDiff root;
public:
typedef std::map<Unique *, StrokeInfo>::const_iterator const_iterator;
const const_iterator begin() const { return root.added.begin(); }
const const_iterator end() const { return root.added.end(); }
ActionListDiff *get_root() { return &root; }
const ActionListDiff *get_action_list(std::string wm_class) const {
std::map<std::string, ActionListDiff *>::const_iterator i = apps.find(wm_class);
return i == apps.end() ? &root : i->second;
}
ActionDB();
};
BOOST_CLASS_VERSION(ActionDB, 3)
class ActionDBWatcher : public TimeoutWatcher {
bool good_state;
public:
ActionDBWatcher() : TimeoutWatcher(5000), good_state(true) {}
void init();
virtual void timeout();
};
extern ActionDB actions;
void update_actions();
#endif