]> icculus.org git repositories - dana/openbox.git/blob - src/actions.cc
so close to keybindings. wont link for now.
[dana/openbox.git] / src / actions.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "actions.hh"
8 #include "widget.hh"
9 #include "openbox.hh"
10 #include "client.hh"
11 #include "python.hh"
12 #include "bindings.hh"
13 #include "otk/display.hh"
14
15 #include <stdio.h>
16
17 namespace ob {
18
19 const unsigned int OBActions::DOUBLECLICKDELAY = 300;
20
21 OBActions::OBActions()
22   : _button(0)
23 {
24   // XXX: load a configuration out of somewhere
25
26 }
27
28
29 OBActions::~OBActions()
30 {
31 }
32
33
34 void OBActions::buttonPressHandler(const XButtonEvent &e)
35 {
36   OtkEventHandler::buttonPressHandler(e);
37   
38   // run the PRESS guile hook
39   OBWidget *w = dynamic_cast<OBWidget*>
40     (Openbox::instance->findHandler(e.window));
41
42   python_callback(Action_ButtonPress, e.window,
43                   (OBWidget::WidgetType)(w ? w->type():-1),
44                   e.state, e.button, e.x_root, e.y_root, e.time);
45   if (w && w->type() == OBWidget::Type_Frame) // a binding
46     Openbox::instance->bindings()->fire(Action_ButtonPress, e.window,
47                                         e.state, e.button, e.time);
48     
49   if (_button) return; // won't count toward CLICK events
50
51   _button = e.button;
52 }
53   
54
55 void OBActions::buttonReleaseHandler(const XButtonEvent &e)
56 {
57   OtkEventHandler::buttonReleaseHandler(e);
58   
59   OBWidget *w = dynamic_cast<OBWidget*>
60     (Openbox::instance->findHandler(e.window));
61
62   // run the RELEASE guile hook
63   python_callback(Action_ButtonRelease, e.window,
64                   (OBWidget::WidgetType)(w ? w->type():-1),
65                   e.state, e.button, e.x_root, e.y_root, e.time);
66   if (w && w->type() == OBWidget::Type_Frame) // a binding
67     Openbox::instance->bindings()->fire(Action_ButtonRelease, e.window,
68                                         e.state, e.button, e.time);
69
70   // not for the button we're watching?
71   if (_button != e.button) return;
72
73   _button = 0;
74
75   // find the area of the window
76   XWindowAttributes attr;
77   if (!XGetWindowAttributes(otk::OBDisplay::display, e.window, &attr)) return;
78
79   // if not on the window any more, it isnt a CLICK
80   if (!(e.same_screen && e.x >= 0 && e.y >= 0 &&
81         e.x < attr.width && e.y < attr.height))
82     return;
83
84   // run the CLICK guile hook
85   python_callback(Action_Click, e.window,
86                   (OBWidget::WidgetType)(w ? w->type():-1),
87                   e.state, e.button, e.time);
88   if (w && w->type() == OBWidget::Type_Frame) // a binding
89     Openbox::instance->bindings()->fire(Action_Click, e.window,
90                                         e.state, e.button, e.time);
91
92   if (e.time - _release.time < DOUBLECLICKDELAY &&
93       _release.win == e.window && _release.button == e.button) {
94
95     // run the DOUBLECLICK guile hook
96     python_callback(Action_DoubleClick, e.window,
97                   (OBWidget::WidgetType)(w ? w->type():-1),
98                   e.state, e.button, e.time);
99     if (w && w->type() == OBWidget::Type_Frame) // a binding
100       Openbox::instance->bindings()->fire(Action_DoubleClick, e.window,
101                                           e.state, e.button, e.time);
102     
103     // reset so you cant triple click for 2 doubleclicks
104     _release.win = 0;
105     _release.button = 0;
106     _release.time = 0;
107   } else {
108     // save the button release, might be part of a double click
109     _release.win = e.window;
110     _release.button = e.button;
111     _release.time = e.time;
112   }
113 }
114
115
116 void OBActions::enterHandler(const XCrossingEvent &e)
117 {
118   OtkEventHandler::enterHandler(e);
119   
120   OBWidget *w = dynamic_cast<OBWidget*>
121     (Openbox::instance->findHandler(e.window));
122
123   // run the ENTER guile hook
124   python_callback(Action_EnterWindow, e.window,
125                   (OBWidget::WidgetType)(w ? w->type():-1), e.state);
126 }
127
128
129 void OBActions::leaveHandler(const XCrossingEvent &e)
130 {
131   OtkEventHandler::leaveHandler(e);
132
133   OBWidget *w = dynamic_cast<OBWidget*>
134     (Openbox::instance->findHandler(e.window));
135
136   // run the LEAVE guile hook
137   python_callback(Action_LeaveWindow, e.window,
138                   (OBWidget::WidgetType)(w ? w->type():-1), e.state);
139 }
140
141
142 void OBActions::keyPressHandler(const XKeyEvent &e)
143 {
144 //  OBWidget *w = dynamic_cast<OBWidget*>
145 //    (Openbox::instance->findHandler(e.window));
146
147   Openbox::instance->bindings()->fire(Action_KeyPress, e.window,
148                                       e.state, e.keycode, e.time);
149 }
150
151
152 void OBActions::motionHandler(const XMotionEvent &e)
153 {
154   if (!e.same_screen) return; // this just gets stupid
155
156   int x_root = e.x_root, y_root = e.y_root;
157   
158   // compress changes to a window into a single change
159   XEvent ce;
160   while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
161     if (ce.xmotion.window != e.window) {
162       XPutBackEvent(otk::OBDisplay::display, &ce);
163       break;
164     } else {
165       x_root = e.x_root;
166       y_root = e.y_root;
167     }
168   }
169
170
171   OBWidget *w = dynamic_cast<OBWidget*>
172     (Openbox::instance->findHandler(e.window));
173
174   // XXX: i can envision all sorts of crazy shit with this.. gestures, etc
175   //      maybe that should all be done via python tho..
176   // run the simple MOTION guile hook for now...
177   python_callback(Action_MouseMotion, e.window,
178                   (OBWidget::WidgetType)(w ? w->type():-1),
179                   e.state, e.x_root, e.y_root, e.time);
180 }
181
182
183 }