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
|
/*---------------------------------------------------------------------------------
Name : handle_key.c
Author : Marvin Raaijmakers
Description : Handles key press events
Date of last change: 03-Aug-2006
Copyright (C) 2005-2006 Marvin Raaijmakers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
You can contact me at: marvinr(at)users(dot)sf(dot)net
(replace (at) by @ and (dot) by .)
-----------------------------------------------------------------------------------*/
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <X11/Xlib.h>
#include <keytouchd.h>
static char *get_focused_window_name (Display *display);
static KTPluginKey *get_plugin_key ( KTPluginKeyFunction *key_function,
Display *display );
static void send_key_event ( Display *display,
Window window,
char *keysym,
unsigned int state,
int type );
static void emulate_press_modifiers ( unsigned int state,
Window window,
Display *display );
static void emulate_release_modifiers ( unsigned int state,
Window window,
Display *display );
static void emulate_key ( KTPluginKey *key,
Display *display );
char
*get_focused_window_name (Display *display)
/*
Input:
display - Specifies the connection to the X server
Output:
-
Returns:
The name of the currently focussed window. If no window is focussed NULL is
returned. If the focussed window has no name it returns the first ancestor
of it that has a name. If there is no such ancestor NULL is returned.
Description:
This function returns the name of the currently focussed window.
*/
{
unsigned int num_children;
int revert_to;
Window window,
parent,
root,
*children;
char *window_name;
XGetInputFocus (display, &window, &revert_to);
/* If there is no focused window */
if (window == 0)
{
return (NULL);
}
parent = window;
/* Find the first ancestor with a window name */
while (!XFetchName(display, window, &window_name) && parent != DefaultRootWindow(display))
{
window = parent;
XQueryTree (display, window, &root, &parent, &children, &num_children);
XFree (children);
}
return (window_name);
}
KTPluginKey
*get_plugin_key ( KTPluginKeyFunction *key_function,
Display *display )
/*
Input:
key_function - The key function to get the key information from
display - Specifies the connection to the X server
Output:
-
Returns:
A pointer to the KTPluginKey in key_function that belongs to the currently
focussed window. If no KTPlugins key belongs to the currently focussed
window NULL is returned.
Description:
This function returns the KTPluginKey in key_function that belongs to the
currently focussed window.
*/
{
char *window_name, /* The name of the window that has input focus */
*substr;
int count;
window_name = get_focused_window_name(display);
if (window_name == NULL)
{
/* If there is a KTPluginKey for every application */
if (key_function->num_programs &&
key_function->key[key_function->num_programs-1].window_name.name == NULL)
{
return (&key_function->key[key_function->num_programs-1]);
}
}
else
{
for (count = 0; count < key_function->num_programs; count++)
{
/* This should only happen when count is equal
* to key_function->num_programs-1 */
if (key_function->key[count].window_name.name == NULL)
{
XFree (window_name);
return (&key_function->key[count]);
}
else
{
substr = strstr(window_name, key_function->key[count].window_name.name);
if (substr != NULL)
{
/* If the substring must appear at the end of the window's name
* and it does.
* Or:
* If the substring must appear at the beginning of the window's
* name and it does. */
if ((*(substr+strlen(key_function->key[count].window_name.name)) == '\0' &&
key_function->key[count].window_name.is_end) ||
(substr == window_name &&
!key_function->key[count].window_name.is_end))
{
XFree (window_name);
return (&key_function->key[count]);
}
}
}
}
XFree (window_name);
}
return (NULL);
}
void
send_key_event ( Display *display,
Window window,
char *keysym,
unsigned int state,
int type )
/*
Input:
display - Specifies the connection to the X server
window - Specifies the window the window the event is to be sent to
keysym - The string of the keysym of the key to send
state - The state of the key event
type - KeyPress or KeyRelease
Output:
-
Returns:
-
Description:
This function sends a key press or release event to window.
*/
{
long event_mask;
XKeyEvent key_event;
if (type == KeyPress)
{
event_mask = KeyPressMask;
}
else
{
event_mask = KeyReleaseMask;
}
key_event.type = type;
key_event.display = display;
key_event.window = window;
key_event.root = DefaultRootWindow(display);
key_event.state = state;
key_event.keycode = XKeysymToKeycode(display, XStringToKeysym(keysym));
XSendEvent (display, window, False, event_mask, (XEvent *) &key_event);
usleep (80000); /* This is a value where mozilla doesn't ignore the event */
}
void
emulate_release_modifiers ( unsigned int state,
Window window,
Display *display )
/*
Input:
state - The bitmask indication which modifiers are pressed
window - The window to send the key release event(s) to
display - Specifies the connection to the X server
Output:
-
Returns:
-
Description:
This function sends for every modifier whos bit is set in state a
KeyRelease event. It only does this for the following modifiers:
- Shift
- Control
- Mod1
*/
{
unsigned int tmp_state;
tmp_state = state;
if (state & ControlMask)
{
send_key_event (display, window, "Control_L", tmp_state, KeyRelease);
tmp_state ^= ControlMask;
}
if (state & ShiftMask)
{
send_key_event (display, window, "Shift_L", tmp_state, KeyRelease);
tmp_state ^= ShiftMask;
}
if (state & Mod1Mask)
{
send_key_event (display, window, "Alt_L", tmp_state, KeyRelease);
tmp_state ^= Mod1Mask;
}
}
void
emulate_press_modifiers ( unsigned int state,
Window window,
Display *display )
/*
Input:
state - The bitmask indication which modifiers are pressed
window - The window to send the key press event(s) to
display - Specifies the connection to the X server
Output:
-
Returns:
-
Description:
This function sends for every modifier whos bit is set in state a
KeyPress event. It only does this for the following modifiers:
- Shift
- Control
- Mod1
*/
{
unsigned int tmp_state;
tmp_state = 0;
if (state & ControlMask)
{
send_key_event (display, window, "Control_L", tmp_state, KeyPress);
tmp_state |= ControlMask;
}
if (state & ShiftMask)
{
send_key_event (display, window, "Shift_L", tmp_state, KeyPress);
tmp_state |= ShiftMask;
}
if (state & Mod1Mask)
{
send_key_event (display, window, "Alt_L", tmp_state, KeyPress);
tmp_state |= Mod1Mask;
}
}
void
emulate_key ( KTPluginKey *key,
Display *display )
/*
Input:
key - Information about the key events to emulate
display - Specifies the connection to the X server
Output:
-
Returns:
-
Description:
This function sends key events to the window that has input focus on
display. First this function sends for every modifier whos bit is set in
key->state a KeyPress event. Then it sends a KeyPress followed by a
KeyRelease event for the key that has the keysym key->keysym. Then it sends
for every modifier whos bit is set in key->state a KeyRelease event.
*/
{
Window focus_window;
int revert_to;
XGetInputFocus (display, &focus_window, &revert_to);
emulate_press_modifiers (key->state, focus_window, display);
send_key_event (display, focus_window, key->keysym, key->state, KeyPress);
send_key_event (display, focus_window, key->keysym, key->state, KeyRelease);
emulate_release_modifiers (key->state, focus_window, display);
XSync (display, False);
}
void
handle_key ( KTKeySettings *key,
KTPreferences *preferences,
Display *display )
/*
Input:
key - Contians information about what to do
preferences - The users preferences
display - The display the key event occured on
Output:
-
Returns:
-
Description:
This function handles a key event occured on display. The key variable
points to a structure that tells this function what to do.
*/
{
KTPluginFunction *plugin_function;
KTPluginKey *plugin_key;
if (key->action.type == KTActionTypeProgram)
{
if (fork() == 0)
{
execlp ("sh", "sh", "-c", key->action.program.command, NULL);
}
}
else if (key->action.type == KTActionTypePlugin && key->action.plugin.function != NULL)
{
plugin_function = key->action.plugin.function;
/* If it is a function we shoul call */
if (plugin_function->type == KTPluginFunctionType_Function)
{
(*plugin_function->function.function) (preferences);
}
/* If it is a key press emulation */
else if (plugin_function->type == KTPluginFunctionType_Key)
{
/* Get info about the key press we should emulate */
plugin_key = get_plugin_key (&plugin_function->function.key_function, display);
if (plugin_key != NULL)
{
emulate_key (plugin_key, display);
}
}
}
}
|