not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / khotkeys / libkhotkeysprivate / shortcuts_handler.cpp
blob2333ec2f71efc97d0ef871ae7cee44b1d6798cf2
1 /*
2 Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include <config-X11.h>
22 #include "input.h"
23 #include "shortcuts_handler.h"
24 #include "khotkeysglobal.h"
26 #include <X11/X.h>
27 // #include <X11/Xutil.h>
28 #include <QX11Info>
29 #include <fixx11h.h>
31 #include <KDE/KAction>
32 #include <KDE/KDebug>
34 namespace KHotKeys {
37 ShortcutsHandler::ShortcutsHandler( HandlerType type, QObject *parent )
38 : QObject(parent)
39 ,_type(type)
40 ,_actions(new KActionCollection(this))
42 _actions->setComponentData(KComponentData("khotkeys"));
45 ShortcutsHandler::~ShortcutsHandler()
47 _actions->clear();
48 delete _actions;
52 KAction *ShortcutsHandler::addAction(
53 const QString &id,
54 const QString &text,
55 const KShortcut &shortcut )
57 // Create the action
58 KAction *newAction = _actions->addAction(id);
59 if (!newAction)
61 return 0;
63 // If our HandlerType is configuration we have to tell kdedglobalaccel
64 // that this action is only for configuration purposes.
65 // see KAction::~KAction
66 if (_type==Configuration)
68 newAction->setProperty("isConfigurationAction", QVariant(true));
70 newAction->setText(text);
71 newAction->setGlobalShortcut( shortcut, KAction::ActiveShortcut );
72 // Enable global shortcut. If that fails there is no sense in proceeding
73 if (!newAction->isGlobalShortcutEnabled())
75 kWarning() << "Failed to enable global shortcut for '"
76 << text << "' " << id;
77 _actions->removeAction(newAction);
78 return 0;
80 Q_ASSERT(newAction->isEnabled());
81 return newAction;
85 QAction *ShortcutsHandler::getAction( const QString &id )
87 return _actions->action(id);
91 bool ShortcutsHandler::removeAction( const QString &id )
93 QAction *action = getAction( id );
94 if (!action)
96 return false;
98 else
100 _actions->removeAction(action);
101 return true;
106 #ifdef HAVE_XTEST
108 } // namespace KHotKeys
109 #include <X11/extensions/XTest.h>
110 namespace KHotKeys
113 static bool xtest_available = false;
114 static bool xtest_inited = false;
115 static bool xtest()
117 if( xtest_inited )
118 return xtest_available;
119 xtest_inited = true;
120 int dummy1, dummy2, dummy3, dummy4;
121 xtest_available =
122 ( XTestQueryExtension( QX11Info::display(), &dummy1, &dummy2, &dummy3, &dummy4 ) == True );
123 return xtest_available;
125 #endif
127 bool ShortcutsHandler::send_macro_key( const QString& key, Window window_P )
129 kError() << "ShortcutsHandler::send_macro_key not implemented!!!";
130 Q_UNUSED( key );
131 Q_UNUSED( window_P );
132 return false;
134 #if 0
135 int keysym;
136 uint x_mod;
137 #if 0
138 // TODO fix this, make sure it works even with stuff like "dead_acute"
139 QKeySequence ks( key );
140 if( key == "Enter" && ks.isEmpty() )
141 key = "Return"; // CHECKE hack
142 keyboard_handler->send_macro_key( ks.isEmpty() ? 0 : ks[0], w );
144 bool ok = KKeyServer::keyQtToSymX(keycode, keysym) && KKeyServer::keyQtToModX(keycode, x_mod);
145 #endif
146 KeyCode x_keycode = XKeysymToKeycode( QX11Info::display(), keysym );
147 if( x_keycode == NoSymbol )
148 return false;
149 #ifdef HAVE_XTEST
150 if( xtest() && window_P == None )
152 // CHECKME tohle jeste potrebuje modifikatory
153 bool ret = XTestFakeKeyEvent( QX11Info::display(), x_keycode, True, CurrentTime );
154 ret = ret && XTestFakeKeyEvent( QX11Info::display(), x_keycode, False, CurrentTime );
155 return ret;
157 #endif
158 if( window_P == None || window_P == InputFocus )
159 window_P = windows_handler->active_window();
160 if( window_P == None ) // CHECKME tohle cele je ponekud ...
161 window_P = InputFocus;
162 XKeyEvent ev;
163 ev.type = KeyPress;
164 ev.display = QX11Info::display();
165 ev.window = window_P;
166 ev.root = QX11Info::appRootWindow(); // I don't know whether these have to be set
167 ev.subwindow = None; // to these values, but it seems to work, hmm
168 ev.time = CurrentTime;
169 ev.x = 0;
170 ev.y = 0;
171 ev.x_root = 0;
172 ev.y_root = 0;
173 ev.keycode = x_keycode;
174 ev.state = x_mod;
175 ev.same_screen = True;
176 bool ret = XSendEvent( QX11Info::display(), window_P, True, KeyPressMask, ( XEvent* )&ev );
177 #if 1
178 ev.type = KeyRelease; // is this actually really needed ??
179 ev.display = QX11Info::display();
180 ev.window = window_P;
181 ev.root = QX11Info::appRootWindow();
182 ev.subwindow = None;
183 ev.time = CurrentTime;
184 ev.x = 0;
185 ev.y = 0;
186 ev.x_root = 0;
187 ev.y_root = 0;
188 ev.state = x_mod;
189 ev.keycode = x_keycode;
190 ev.same_screen = True;
191 ret = ret && XSendEvent( QX11Info::display(), window_P, True, KeyReleaseMask, ( XEvent* )&ev );
192 #endif
193 // Qt's autorepeat compression is broken and can create "aab" from "aba"
194 // XSync() should create delay longer than Qt's max autorepeat interval
195 XSync( QX11Info::display(), False );
196 return ret;
197 #endif
200 bool Mouse::send_mouse_button( int button_P, bool release_P )
202 #ifdef HAVE_XTEST
203 if( xtest())
205 // CHECKME tohle jeste potrebuje modifikatory
206 // a asi i spravnou timestamp misto CurrentTime
207 bool ret = XTestFakeButtonEvent( QX11Info::display(), button_P, True, CurrentTime );
208 if( release_P )
209 ret = ret && XTestFakeButtonEvent( QX11Info::display(), button_P, False, CurrentTime );
210 return ret;
212 #endif
213 return false;
216 } // namespace KHotKeys
218 #include "moc_shortcuts_handler.cpp"