summaryrefslogtreecommitdiff
path: root/src/actions.c
blob: 9b99c5b83feeaeb2c5ea3a2603780885847af79c (plain)
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
/*
 * Copyright (C) 1997-2000, Michael Jennings
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

static const char cvs_ident[] = "$Id$";

#include "config.h"
#include "feature.h"

#include <unistd.h>
#include <limits.h>

#include "startup.h"
#include "actions.h"
#include "command.h"
#include "e.h"
#include "events.h"
#include "menus.h"
#include "options.h"
#include "pixmap.h"
#include "screen.h"
#include "script.h"
#include "scrollbar.h"
#include "term.h"
#include "windows.h"

action_t *action_list = NULL;

unsigned char
action_handle_string(event_t *ev, action_t *action) {

  REQUIRE_RVAL(action->param.string != NULL, 0);
  cmd_write((unsigned char *) action->param.string, strlen(action->param.string));
  return 1;
  ev = NULL;
}

unsigned char
action_handle_echo(event_t *ev, action_t *action) {

  REQUIRE_RVAL(action->param.string != NULL, 0);
  tt_write((unsigned char *) action->param.string, strlen(action->param.string));
  return 1;
  ev = NULL;
}

unsigned char
action_handle_script(event_t *ev, action_t *action) {
  REQUIRE_RVAL(action->param.script != NULL, 0);
  script_parse(action->param.script);
  return 1;
  ev = NULL;
}

unsigned char
action_handle_menu(event_t *ev, action_t *action) {
  REQUIRE_RVAL(action->param.menu != NULL, 0);
  menu_invoke(ev->xbutton.x, ev->xbutton.y, TermWin.parent, action->param.menu, ev->xbutton.time);
  return 1;
}

action_t *
action_find_match(unsigned short mod, unsigned char button, KeySym keysym) {

  action_t *action;

  D_ACTIONS(("mod == 0x%08x, button == %d, keysym == 0x%08x\n", mod, button, keysym));
  for (action = action_list; action; action = action->next) {
    D_ACTIONS(("Checking action.  mod == 0x%08x, button == %d, keysym == 0x%08x\n", action->mod, action->button, action->keysym));
    if ((action->mod == mod) && (action->button == button) && (action->keysym == keysym)) {
      D_ACTIONS(("Match found at %8p\n", action));
      return action;
    }
  }
  return NULL;
}

unsigned char
action_dispatch(event_t *ev, KeySym keysym) {

  action_t *action;
  unsigned int m = (AltMask | MetaMask | NumLockMask);

  ASSERT(ev != NULL);
  D_ACTIONS(("Event %8p:  Button %d, Keysym 0x%08x, Key State 0x%08x\n", ev, ev->xbutton.button, keysym, ev->xkey.state));
  for (action = action_list; action; action = action->next) {
    D_ACTIONS(("Checking action.  mod == 0x%08x, button == %d, keysym == 0x%08x\n", action->mod, action->button, action->keysym));
    if (ev->xany.type == ButtonPress) {
      if ((action->button == BUTTON_NONE) || ((action->button != BUTTON_ANY) && (action->button != ev->xbutton.button))) {
        continue;
      }
    } else if (action->button != BUTTON_NONE) {
      continue;
    }
    D_ACTIONS(("Button passed.\n"));
    if (action->mod != MOD_ANY) {
      if (LOGICAL_XOR((action->mod & MOD_SHIFT), (ev->xkey.state & ShiftMask))) {
        continue;
      }
      if (LOGICAL_XOR((action->mod & MOD_CTRL), (ev->xkey.state & ControlMask))) {
        continue;
      }
      if (LOGICAL_XOR((action->mod & MOD_LOCK), (ev->xkey.state & LockMask))) {
        continue;
      }
      if (LOGICAL_XOR((action->mod & MOD_META), (ev->xkey.state & MetaMask))) {
        continue;
      }
      if (LOGICAL_XOR((action->mod & MOD_ALT), (ev->xkey.state & AltMask))) {
        continue;
      }
      if (((action->mod & MOD_MOD1) && !(ev->xkey.state & Mod1Mask)) || (!(action->mod & MOD_MOD1) && (ev->xkey.state & Mod1Mask) && !(Mod1Mask & m))) {
        continue;                                                                                                                                 
      }                                                                                                                                           
      if (((action->mod & MOD_MOD2) && !(ev->xkey.state & Mod2Mask)) || (!(action->mod & MOD_MOD2) && (ev->xkey.state & Mod2Mask) && !(Mod2Mask & m))) {
        continue;                                                                                                                                 
      }                                                                                                                                           
      if (((action->mod & MOD_MOD3) && !(ev->xkey.state & Mod3Mask)) || (!(action->mod & MOD_MOD3) && (ev->xkey.state & Mod3Mask) && !(Mod3Mask & m))) {
        continue;                                                                                                                                 
      }                                                                                                                                           
      if (((action->mod & MOD_MOD4) && !(ev->xkey.state & Mod4Mask)) || (!(action->mod & MOD_MOD4) && (ev->xkey.state & Mod4Mask) && !(Mod4Mask & m))) {
        continue;                                                                                                                                 
      }                                                                                                                                           
      if (((action->mod & MOD_MOD5) && !(ev->xkey.state & Mod5Mask)) || (!(action->mod & MOD_MOD5) && (ev->xkey.state & Mod5Mask) && !(Mod5Mask & m))) {
        continue;
      }
    }      
    D_ACTIONS(("Modifiers passed.  keysym == 0x%08x, action->keysym == 0x%08x\n", keysym, action->keysym));
    if ((ev->xany.type == KeyPress) && (action->keysym) && (keysym != action->keysym)) {
      continue;
    }
    D_ACTIONS(("Match found.\n"));
    /* If we've passed all the above tests, it's a match.  Dispatch the handler. */
    return ((action->handler)(ev, action));
  }
  return (0);

}

void
action_add(unsigned short mod, unsigned char button, KeySym keysym, action_type_t type, void *param) {

  action_t *action;

  if (!action_list || (action = action_find_match(mod, button, keysym)) == NULL) {
    action = (action_t *) MALLOC(sizeof(action_t));
    action->next = action_list;
    action_list = action;
  } else {
    if (action->type == ACTION_STRING || action->type == ACTION_ECHO || action->type == ACTION_SCRIPT) {
      if (action->param.string) {
	FREE(action->param.string);
      }
    }
  }
  action->mod = mod;
  action->button = button;
  action->type = type;
  action->keysym = keysym;
  switch(type) {
    case ACTION_STRING:
      action->handler = (action_handler_t) action_handle_string;
      action->param.string = (char *) MALLOC(strlen((char *) param) + 2);
      strcpy(action->param.string, (char *) param);
      parse_escaped_string(action->param.string);
      break;
    case ACTION_ECHO:
      action->handler = (action_handler_t) action_handle_echo;
      action->param.string = (char *) MALLOC(strlen((char *) param) + 2);
      strcpy(action->param.string, (char *) param);
      parse_escaped_string(action->param.string);
      break;
    case ACTION_SCRIPT:
      action->handler = (action_handler_t) action_handle_script;
      action->param.script = (char *) MALLOC(strlen((char *) param) + 2);
      strcpy(action->param.script, (char *) param);
      break;
    case ACTION_MENU:
      action->handler = (action_handler_t) action_handle_menu;
      action->param.menu = (menu_t *) param;
      break;
    default:
      break;
  }
  D_ACTIONS(("Added action.  mod == 0x%08x, button == %d, keysym == 0x%08x\n", action->mod, action->button, action->keysym));
}