]> icculus.org git repositories - mikachu/openbox.git/blob - src/bindings.hh
typo
[mikachu/openbox.git] / src / bindings.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 #ifndef __binding_hh
3 #define __binding_hh
4
5 /*! @file binding.hh
6   @brief I dunno.. some binding stuff?
7 */
8
9 #include "actions.hh"
10 #include "otk/timer.hh"
11
12 extern "C" {
13 #include <Python.h>
14 }
15
16 #include <string>
17 #include <list>
18 #include <vector>
19
20 namespace ob {
21
22 class OBClient;
23
24 typedef struct Binding {
25   unsigned int modifiers;
26   unsigned int key;
27
28   bool operator==(struct Binding &b2) { return key == b2.key &&
29                                           modifiers == b2.modifiers; }
30   bool operator!=(struct Binding &b2) { return key != b2.key ||
31                                           modifiers != b2.modifiers; }
32   Binding(unsigned int mod, unsigned int k) { modifiers = mod; key = k; }
33 } Binding;
34
35 typedef struct BindingTree {
36   Binding binding;
37   PyObject *callback; // the callback given for the binding in add()
38   bool chain;     // true if this is a chain to another key (not an action)
39
40   struct BindingTree *next_sibling; // the next binding in the tree at the same
41                                     // level
42   struct BindingTree *first_child;  // the first child of this binding (next
43                                     // binding in a chained sequence).
44   BindingTree(PyObject *callback) : binding(0, 0) {
45     this->callback = callback; chain = true; next_sibling = first_child = 0;
46   }
47   BindingTree() : binding(0, 0) {
48     this->callback = 0; chain = true; next_sibling = first_child = 0;
49   }
50 } BindingTree;
51
52 class OBBindings {
53 public:
54   //! A list of strings
55   typedef std::vector<std::string> StringVect;
56
57 private:
58   BindingTree _tree; // root node of the tree (this doesn't have siblings!)
59   BindingTree *_curpos; // position in the keytree
60
61   Binding _resetkey; // the key which resets the key chain status
62
63   otk::OBTimer _timer;
64   
65   PyObject *find(BindingTree *search, bool *conflict) const;
66   BindingTree *buildtree(const StringVect &keylist, PyObject *callback) const;
67   void assimilate(BindingTree *node);
68
69   static void reset(OBBindings *self); // the timer's timeout function
70
71 public:
72   //! Initializes an OBBindings object
73   OBBindings();
74   //! Destroys the OBBindings object
75   virtual ~OBBindings();
76
77   //! Translates a binding string into the actual Binding
78   bool translate(const std::string &str, Binding &b, bool askey = true) const;
79   
80   //! Adds a new key binding
81   /*!
82     A binding will fail to be added if the binding already exists (as part of
83     a chain or not), or if any of the strings in the keylist are invalid.    
84     @return true if the binding could be added; false if it could not.
85   */
86   bool add(const StringVect &keylist, PyObject *callback);
87
88   //! Removes a key binding
89   /*!
90     @return The callbackid of the binding, or '< 0' if there was no binding to
91             be removed.
92   */
93   bool remove(const StringVect &keylist);
94
95   //! Removes all key bindings
96   void removeAll();
97
98   void fire(unsigned int modifiers,unsigned int key, Time time);
99
100   void setResetKey(const std::string &key);
101
102   void grabKeys(bool grab);
103 };
104
105 }
106
107 #endif // __binding_hh