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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
|
/* -*-c-*- */
/* 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
* (at your option) 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, see: <http://www.gnu.org/licenses/>
*/
#include "config.h"
#include <stdio.h>
#include <X11/keysym.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include "libs/fvwmlib.h"
#include "libs/Strings.h"
#include "libs/wild.h"
#include "libs/Grab.h"
#include "libs/Bindings.h"
#include "libs/charmap.h"
#include "libs/wcontext.h"
#include "libs/modifiers.h"
static Bool is_grabbing_everything = False;
static int key_min = 0;
static int key_max = 0;
/* Free the memory use by a binding. */
void FreeBindingStruct(Binding *b)
{
if (b->key_name)
{
free(b->key_name);
}
if (b->Action)
{
free(b->Action);
}
if (b->Action2)
{
free(b->Action2);
}
if (b->windowName)
{
free(b->windowName);
}
free(b);
return;
}
void FreeBindingList(Binding *b)
{
Binding *t;
for (; b != NULL; b = t)
{
t = b->NextBinding;
FreeBindingStruct(b);
}
return;
}
/* Unlink a binding b from a binding list pblist. The previous binding in the
* list (prev) must be given also. Pass NULL at the beginning of the list.
* The *pblist pointer may be modified by this function. */
static void UnlinkBinding(Binding **pblist, Binding *b, Binding *prev)
{
Binding *t;
if (!prev && b != *pblist)
{
for (t = *pblist; t && t != b; prev = t, t = t->NextBinding)
{
/* Find the previous binding in the list. */
}
if (t == NULL)
{
/* Binding not found */
return;
}
}
if (prev)
{
/* middle of list */
prev->NextBinding = b->NextBinding;
}
else
{
/* must have been first one, set new start */
*pblist = b->NextBinding;
}
return;
}
/* To remove a binding from the global list (probably needs more processing
* for mouse binding lines though, like when context is a title bar button).
* Specify either button or keysym, depending on type. */
void RemoveBinding(Binding **pblist, Binding *b, Binding *prev)
{
UnlinkBinding(pblist, b, NULL);
FreeBindingStruct(b);
return;
}
/*
*
* Actually adds a new binding to a list (pblist) of existing bindings.
* Specify either button or keysym/key_name, depending on type.
* The parameters action and action2 are assumed to reside in malloced memory
* that will be freed in RemoveBinding. The key_name is copied into private
* memory and has to be freed by the caller.
*
*/
int AddBinding(
Display *dpy, Binding **pblist, binding_t type,
int button, KeySym keysym, char *key_name, int modifiers, int contexts,
void *action, void *action2, char *windowName)
{
int i;
int min;
int max;
int maxmods;
int m;
int mask;
int count = 0;
KeySym tkeysym;
Binding *temp;
/*
** Unfortunately a keycode can be bound to multiple keysyms and a
** keysym can bound to multiple keycodes. Thus we have to check every
** keycode with any single modifier.
*/
if (BIND_IS_KEY_BINDING(type))
{
if (key_max == 0)
{
XDisplayKeycodes(dpy, &key_min, &key_max);
}
min=key_min;
max=key_max;
maxmods = 8;
}
else
{
min = button;
max = button;
maxmods = 0;
}
for (i = min; i <= max; i++)
{
unsigned int bound_mask = 0;
/* If this is a mouse binding we'll fall through the for loop
* (maxmods is zero) and the if condition is always true (type
* is zero). Since min == max == button there is no loop at all
* is case of a mouse binding. */
for (m = 0, tkeysym = XK_Left;
m <= maxmods && tkeysym != NoSymbol; m++)
{
if (BIND_IS_MOUSE_BINDING(type) ||
(tkeysym = fvwm_KeycodeToKeysym(dpy, i, m, 0)) == keysym)
{
unsigned int add_modifiers = 0;
unsigned int bind_mask = 1;
unsigned int check_bound_mask = 0;
switch (m)
{
case 0:
/* key generates the key sym with no
* modifiers depressed - bind it */
break;
case 1:
/* key generates the key sym with shift
* depressed */
if (modifiers != AnyModifier &&
!(modifiers & ShiftMask))
{
add_modifiers = ShiftMask;
bind_mask = (1 << m);
/* but don't bind it again if
* already bound without
* modifiers */
check_bound_mask = 1;
}
break;
default:
/* key generates the key sym with
* undefined modifiers depressed -
* let's make an educated guess at what
* modifiers the user expected
* based on the XFree86 default
* configuration. */
mask = modifier_mapindex_to_mask[m - 1];
if (modifiers != AnyModifier &&
!((modifiers & mask) != mask))
{
add_modifiers = mask;
bind_mask = (1 << m);
/* but don't bind it again if
* already bound without
* modifiers */
check_bound_mask = 1;
}
break;
}
if ((bind_mask & bound_mask) ||
(check_bound_mask & bound_mask))
{
/* already bound, break out */
break;
}
temp = *pblist;
(*pblist) = fxmalloc(sizeof(Binding));
(*pblist)->type = type;
(*pblist)->Button_Key = i;
if (BIND_IS_KEY_BINDING(type) &&
key_name != NULL)
{
(*pblist)->key_name =
stripcpy(key_name);
}
else
{
(*pblist)->key_name = NULL;
}
(*pblist)->Context = contexts;
(*pblist)->Modifier = modifiers | add_modifiers;
(*pblist)->Action =
(action) ? stripcpy(action) : NULL;
(*pblist)->Action2 =
(action2) ? stripcpy(action2) : NULL;
(*pblist)->windowName =
windowName ? stripcpy(windowName) :
NULL;
(*pblist)->NextBinding = temp;
bound_mask |= bind_mask;
count++;
}
}
}
return count;
}
/* returns 1 if binding b1 is identical to binding b2 (except action)
* returns 2 if b1 and b2 are identical except that b1 has a different window
* name than b2 and both are not NULL
* returns 0 otherwise
*/
static int compare_bindings(Binding *b1, Binding *b2)
{
if (b1->type != b2->type)
{
return 0;
}
if (b1->Context != b2->Context)
{
return 0;
}
if (b1->Modifier != b2->Modifier)
{
return 0;
}
if (b1->Button_Key != b2->Button_Key)
{
return 0;
}
/* definition: "global binding" => b->windowName == NULL
* definition: "window-specific binding" => b->windowName != NULL
*/
if (b1->windowName && b2->windowName)
{
/* Both bindings are window-specific. The existing binding, b2,
* is only replaced (by b1) if it applies to the same window */
if (strcmp(b1->windowName, b2->windowName) != 0)
{
return 2;
}
}
else if (b1->windowName || b2->windowName)
{
/* 1 binding is window-specific, the other is global - no need
* to replace this binding. */
return 0;
}
if (BIND_IS_KEY_BINDING(b1->type) || BIND_IS_MOUSE_BINDING(b1->type))
{
return 1;
}
return 0;
}
/*
* Does exactly the opposite of AddBinding: It removes the bindings that
* AddBinding would have added to the *pblist_src and collects them in the
* *pblist_dest. This can be used to remove a binding completely from the
* list. The bindings still have to be freed.
*/
void CollectBindingList(
Display *dpy, Binding **pblist_src, Binding **pblist_dest,
Bool *ret_are_similar_bindings_left, binding_t type,
int button, KeySym keysym,
int modifiers, int contexts, char *windowName)
{
Binding *tmplist = NULL;
Binding *bold;
Binding *oldprev;
*ret_are_similar_bindings_left = False;
/* generate a private list of bindings to be removed */
AddBinding(
dpy, &tmplist, type,
button, keysym, NULL, modifiers, contexts, NULL, NULL,
windowName);
/* now find equivalent bindings in the given binding list and move
* them to the new clist */
for (bold = *pblist_src, oldprev = NULL; bold != NULL; )
{
Binding *btmp;
Binding *bfound;
bfound = NULL;
for (btmp = tmplist;
btmp != NULL && (
bfound == NULL ||
*ret_are_similar_bindings_left == False);
btmp = btmp->NextBinding)
{
int rc;
rc = compare_bindings(btmp, bold);
if (rc == 1)
{
bfound = btmp;
}
else if (rc == 2)
{
*ret_are_similar_bindings_left = True;
if (bfound != NULL)
{
break;
}
}
}
if (bfound != NULL)
{
Binding *next;
/* move matched binding from src list to dest list */
UnlinkBinding(pblist_src, bold, oldprev);
next = bold->NextBinding;
bold->NextBinding = *pblist_dest;
*pblist_dest = bold;
/* oldprev is unchanged */
bold = next;
}
else
{
oldprev = bold;
bold = bold->NextBinding;
}
}
/* throw away the temporary list */
FreeBindingList(tmplist);
return;
}
/*
* bindingAppliesToWindow()
*
* The Key/Mouse/PointerKey syntax (optionally) allows a window name
* (or class or resource) to be specified with the binding, denoting
* which windows the binding can be invoked in. This function determines
* if the binding actually applies to a window based on its
* name/class/resource.
*/
static Bool does_binding_apply_to_window(
Binding *binding, const XClassHint *win_class, const char *win_name)
{
/* If no window name is specified with the binding then that means
* the binding applies to ALL windows. */
if (binding->windowName == NULL)
{
return True;
}
else if (win_class == NULL || win_name == NULL)
{
return False;
}
if (matchWildcards(binding->windowName, win_name) == True ||
matchWildcards(binding->windowName, win_class->res_name) == True ||
matchWildcards(binding->windowName, win_class->res_class) == True)
{
return True;
}
return False;
}
static Bool __compare_binding(
Binding *b,
int button_keycode, unsigned int modifier, unsigned int used_modifiers,
int Context, binding_t type, const XClassHint *win_class,
const char *win_name)
{
if (b->type != type || !(b->Context & Context))
{
return False;
}
if ((b->Modifier & used_modifiers) != modifier &&
b->Modifier != AnyModifier)
{
return False;
}
if (BIND_IS_MOUSE_BINDING(type) &&
(b->Button_Key != button_keycode && b->Button_Key != 0))
{
return False;
}
else if (BIND_IS_KEY_BINDING(type) && b->Button_Key != button_keycode)
{
return False;
}
if (!does_binding_apply_to_window(b, win_class, win_name))
{
return False;
}
return True;
}
/* is_pass_through_action() - returns true if the action indicates that the
* binding should be ignored by fvwm & passed through to the underlying
* window.
* Note: it is only meaningful to check for pass-thru actions on
* window-specific bindings. */
Bool is_pass_through_action(const char *action)
{
/* action should never be NULL. */
return (strncmp(action, "--", 2) == 0);
}
/* Check if something is bound to a key or button press and return the action
* to be executed or NULL if not. */
void *CheckBinding(
Binding *blist,
int button_keycode, unsigned int modifier,unsigned int dead_modifiers,
int Context, binding_t type, const XClassHint *win_class,
const char *win_name)
{
Binding *b;
unsigned int used_modifiers = ~dead_modifiers;
void *action = NULL;
modifier &= (used_modifiers & ALL_MODIFIERS);
for (b = blist; b != NULL; b = b->NextBinding)
{
if (__compare_binding(
b, button_keycode, modifier,
used_modifiers, Context, type, win_class,
win_name) == True)
{
/* If this is a global binding, keep searching <blist>
* in the hope of finding a window-specific binding.
* If we don't find a win-specific binding, we use the
* _first_ matching global binding we hit. */
if (action == NULL || b->windowName)
{
action = b->Action;
if (b->windowName)
{
if (is_pass_through_action(action))
{
action = NULL;
}
break;
}
}
}
}
return action;
}
void *CheckTwoBindings(
Bool *ret_is_second_binding, Binding *blist,
int button_keycode, unsigned int modifier,unsigned int dead_modifiers,
int Context, binding_t type, const XClassHint *win_class,
const char *win_name, int Context2, binding_t type2,
const XClassHint *win_class2, const char *win_name2)
{
Binding *b;
unsigned int used_modifiers = ~dead_modifiers;
void *action = NULL;
modifier &= (used_modifiers & ALL_MODIFIERS);
for (b = blist; b != NULL; b = b->NextBinding)
{
if (__compare_binding(
b, button_keycode, modifier,
used_modifiers, Context, type, win_class, win_name)
== True)
{
if (action == NULL || b->windowName)
{
*ret_is_second_binding = False;
action = b->Action;
if (b->windowName)
{
if (is_pass_through_action(action))
action = NULL;
break;
}
}
}
if (__compare_binding(
b, button_keycode, modifier,
used_modifiers, Context2, type2, win_class2,
win_name2) == True)
{
if (action == NULL || b->windowName)
{
*ret_is_second_binding = True;
action = b->Action;
if (b->windowName)
{
if (is_pass_through_action(action))
{
action = NULL;
}
break;
}
}
}
}
return action;
}
/*
* GrabWindowKey - grab needed keys for the window for one binding
* GrabAllWindowKeys - grab needed keys for the window for all bindings
* in blist
* GrabWindowButton - same for mouse buttons
* GrabAllWindowButtons - same for mouse buttons
* GrabAllWindowKeysAndButtons - both of the above
*
* Inputs:
* w - the window to use (the frame window)
* grab - 1 to grab, 0 to ungrab
* binding - pointer to the bindinge to grab/ungrab
* contexts - all context bits that shall receive bindings
* dead_modifiers - modifiers to ignore for 'AnyModifier'
* cursor - the mouse cursor to use when the pointer is on the
* grabbed area (mouse bindings only)
*
*/
void GrabWindowKey(Display *dpy, Window w, Binding *binding,
unsigned int contexts, unsigned int dead_modifiers,
Bool fGrab)
{
/* remove unnecessary bits from dead_modifiers */
dead_modifiers &= ~(binding->Modifier & dead_modifiers);
dead_modifiers &= ALL_MODIFIERS;
if((binding->Context & contexts) && BIND_IS_KEY_BINDING(binding->type))
{
if (fGrab)
{
XGrabKey(
dpy, binding->Button_Key, binding->Modifier, w,
True, GrabModeAsync, GrabModeAsync);
}
else
{
XUngrabKey(
dpy, binding->Button_Key, binding->Modifier, w);
}
if(binding->Modifier != AnyModifier && dead_modifiers != 0)
{
register unsigned int mods;
register unsigned int max = dead_modifiers;
register unsigned int living_modifiers =
~dead_modifiers;
/* handle all bindings for the dead modifiers */
for (mods = 1; mods <= max; mods++)
{
/* Since mods starts with 1 we don't need to
* test if mods contains a dead modifier.
* Otherwise both, dead and living modifiers
* would be zero ==> mods == 0 */
if (mods & living_modifiers)
{
continue;
}
if (fGrab)
{
XGrabKey(
dpy, binding->Button_Key,
mods|binding->Modifier, w,
True, GrabModeAsync,
GrabModeAsync);
}
else
{
XUngrabKey(
dpy, binding->Button_Key,
mods|binding->Modifier, w);
}
}
}
if (!is_grabbing_everything)
{
XSync(dpy, 0);
}
}
return;
}
void GrabAllWindowKeys(
Display *dpy, Window w, Binding *blist, unsigned int contexts,
unsigned int dead_modifiers, Bool fGrab)
{
MyXGrabServer(dpy);
is_grabbing_everything = True;
for ( ; blist != NULL; blist = blist->NextBinding)
{
GrabWindowKey(dpy, w, blist, contexts, dead_modifiers, fGrab);
}
is_grabbing_everything = False;
MyXUngrabServer(dpy);
return;
}
void GrabWindowButton(
Display *dpy, Window w, Binding *binding, unsigned int contexts,
unsigned int dead_modifiers, Cursor cursor, Bool fGrab)
{
if (binding->Action == NULL)
{
return;
}
dead_modifiers &= ~(binding->Modifier & dead_modifiers); /* dje */
dead_modifiers &= ALL_MODIFIERS;
if ((binding->Context & contexts) &&
((BIND_IS_MOUSE_BINDING(binding->type))))
{
int bmin = 1;
int bmax = NUMBER_OF_EXTENDED_MOUSE_BUTTONS;
int button;
if(binding->Button_Key >0)
{
bmin = bmax = binding->Button_Key;
}
for (button = bmin; button <= bmax; button++)
{
if (fGrab)
{
XGrabButton(
dpy, button, binding->Modifier, w,
True, ButtonPressMask |
ButtonReleaseMask, GrabModeSync,
GrabModeAsync, None, cursor);
}
else
{
XUngrabButton(
dpy, button, binding->Modifier, w);
}
if (binding->Modifier != AnyModifier &&
dead_modifiers != 0)
{
register unsigned int mods;
register unsigned int max = dead_modifiers;
register unsigned int living_modifiers =
~dead_modifiers;
/* handle all bindings for the dead modifiers */
for (mods = 1; mods <= max; mods++)
{
/* Since mods starts with 1 we don't
* need to test if mods contains a
* dead modifier. Otherwise both, dead
* and living modifiers would be zero
* ==> mods == 0 */
if (mods & living_modifiers)
{
continue;
}
if (fGrab)
{
XGrabButton(
dpy, button,
mods|binding->Modifier,
w, True,
ButtonPressMask |
ButtonReleaseMask,
GrabModeSync,
GrabModeAsync, None,
cursor);
}
else
{
XUngrabButton(
dpy, button,
mods|binding->Modifier,
w);
}
}
}
if (!is_grabbing_everything)
{
XSync(dpy, 0);
}
}
}
return;
}
void GrabAllWindowButtons(
Display *dpy, Window w, Binding *blist, unsigned int contexts,
unsigned int dead_modifiers, Cursor cursor, Bool fGrab)
{
MyXGrabServer(dpy);
is_grabbing_everything = True;
for ( ; blist != NULL; blist = blist->NextBinding)
{
GrabWindowButton(dpy, w, blist, contexts, dead_modifiers,
cursor, fGrab);
}
is_grabbing_everything = False;
MyXUngrabServer(dpy);
return;
}
void GrabAllWindowKeysAndButtons(
Display *dpy, Window w, Binding *blist, unsigned int contexts,
unsigned int dead_modifiers, Cursor cursor, Bool fGrab)
{
MyXGrabServer(dpy);
is_grabbing_everything = True;
for ( ; blist != NULL; blist = blist->NextBinding)
{
if (blist->Context & contexts)
{
if (BIND_IS_MOUSE_BINDING(blist->type))
{
GrabWindowButton(
dpy, w, blist, contexts,
dead_modifiers, cursor, fGrab);
}
else if (BIND_IS_KEY_BINDING(blist->type))
{
GrabWindowKey(
dpy, w, blist, contexts,
dead_modifiers, fGrab);
}
}
}
is_grabbing_everything = False;
MyXUngrabServer(dpy);
return;
}
void GrabWindowKeyOrButton(
Display *dpy, Window w, Binding *binding, unsigned int contexts,
unsigned int dead_modifiers, Cursor cursor, Bool fGrab)
{
if (BIND_IS_MOUSE_BINDING(binding->type))
{
GrabWindowButton(
dpy, w, binding, contexts, dead_modifiers, cursor,
fGrab);
}
else if (BIND_IS_KEY_BINDING(binding->type))
{
GrabWindowKey(
dpy, w, binding, contexts, dead_modifiers, fGrab);
}
return;
}
/*
*
* Like XStringToKeysym, but allows some typos and does some additional
* error checking.
*
*/
KeySym FvwmStringToKeysym(Display *dpy, char *key)
{
KeySym keysym;
char *s;
if (!isalpha(*key))
{
keysym = XStringToKeysym(key);
}
else
{
s = fxstrdup(key);
/* always prefer the lower case spelling if it exists */
*s = tolower(*s);
keysym = XStringToKeysym(s);
if (keysym == NoSymbol)
{
*s = toupper(*s);
keysym = XStringToKeysym(s);
}
free(s);
}
if (keysym == NoSymbol || XKeysymToKeycode(dpy, keysym) == 0)
{
return 0;
}
return keysym;
}
|