]> icculus.org git repositories - dana/openbox.git/blob - src/bindings.hh
new code for bindings/callbacks. much sexier. now passes python classes back to the...
[dana/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   bool translate(const std::string &str, Binding &b) const;
67   BindingTree *buildtree(const StringVect &keylist, PyObject *callback) const;
68   void assimilate(BindingTree *node);
69
70   static void reset(OBBindings *self); // the timer's timeout function
71
72 public:
73   //! Initializes an OBBinding object
74   OBBindings();
75   //! Destroys the OBBinding object
76   virtual ~OBBindings();
77
78   //! Adds a new key binding
79   /*!
80     A binding will fail to be added if the binding already exists (as part of
81     a chain or not), or if any of the strings in the keylist are invalid.    
82     @return true if the binding could be added; false if it could not.
83   */
84   bool add(const StringVect &keylist, PyObject *callback);
85
86   //! Removes a key binding
87   /*!
88     @return The callbackid of the binding, or '< 0' if there was no binding to
89             be removed.
90   */
91   bool remove(const StringVect &keylist);
92
93   //! Removes all key bindings
94   void removeAll();
95
96   void fire(unsigned int modifiers,unsigned int key, Time time);
97
98   void setResetKey(const std::string &key);
99
100   void grabKeys(bool grab);
101 };
102
103 }
104
105 #endif // __binding_hh