summaryrefslogtreecommitdiff
path: root/src/input.c
blob: ffba34644d85404815de72e23a5cc0981e6a1427 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* Read kdb input from the user.
 * Copyright (C) 2000, 2001 Shawn Betts
 *
 * This file is part of ratpoison.
 *
 * ratpoison 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, or (at your option)
 * any later version.
 * 
 * ratpoison 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 software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>

#include "ratpoison.h"

/* Return the name of the keysym. caller must free returned pointer */
char *
keysym_to_string (KeySym keysym, unsigned int modifier)
{
  const unsigned int mod_table[] = {ControlMask, 
				    Mod1Mask, 
				    Mod2Mask, 
				    Mod3Mask, 
				    Mod4Mask, 
				    Mod5Mask};
  const unsigned char str_table[] = "CM2345";

  unsigned char *name;
  int pos, i;

  name = malloc (15);
  if (name == NULL)
    {
      PRINT_ERROR ("Out of memory!\n");
      exit (EXIT_FAILURE);
    }

  for (pos = 0, i = 0; i < 6; i++)
    {
      if (modifier & mod_table[i])
	{
	  name[pos] = str_table[i];
	  name[pos+1] = '-';
	  pos += 2;
	}
    }

  name[pos] = keysym;
  name[pos+1] = '\0';

  return name;
}

/* Cooks a keycode + modifier into a keysym + modifier. This should be
   used anytime meaningful key information is to be extracted from a
   KeyPress or KeyRelease event. */
void
cook_keycode (KeyCode keycode, KeySym *keysym, unsigned int *mod)
{
  KeySym normal, shifted;

  normal = XKeycodeToKeysym(dpy, keycode, 0);
  shifted = XKeycodeToKeysym(dpy, keycode, 1);

  /* FIXME: eew, this looks gross. */
  if (*mod & (ShiftMask | LockMask))
    {
      /* if the shifted code is not defined, then we use the normal
	 keysym and keep the shift mask */
      if (shifted == NoSymbol)
	{
	  *keysym = normal;
	}
      /* But if the shifted code is defined, we use it and remove the
	 shift mask */
      else if (*mod & ShiftMask)
	{
	  *keysym = shifted;
	  *mod &= ~(ShiftMask | LockMask);
	}
      /* If caps lock is on, use shifted for alpha keys */
      else if (normal >= XK_a 
	       && normal <= XK_z
	       && *mod & LockMask)
	{
	  *keysym = shifted;
	}
      else
	{
	  *keysym = normal;
	}
    }
  else
    {
      *keysym = normal;
    }

  PRINT_DEBUG ("cooked keysym: %ld '%c' mask: %d\n", 
	       *keysym, (char)*keysym, *mod);
}

void
read_key (KeySym *keysym, unsigned int *modifiers)
{
  XEvent ev;

  do
    {
      XMaskEvent (dpy, KeyPressMask, &ev);
      *modifiers = ev.xkey.state;
      cook_keycode (ev.xkey.keycode, keysym, modifiers);
    } while (IsModifierKey (*keysym));
}

static void
update_input_window (screen_info *s, char *prompt, char *input, int input_len)
{
  int 	prompt_width = XTextWidth (s->font, prompt, strlen (prompt));
  int 	input_width  = XTextWidth (s->font, input, input_len);
  int 	width;

  width = BAR_X_PADDING * 2 + prompt_width + input_width;

  if (width < INPUT_WINDOW_SIZE + prompt_width)
    {
      width = INPUT_WINDOW_SIZE + prompt_width;
    }

  XMoveResizeWindow (dpy, s->input_window, 
		     bar_x (s, width), bar_y (s), width,
		     (FONT_HEIGHT (s->font) + BAR_Y_PADDING * 2));

  XClearWindow (dpy, s->input_window);

  XDrawString (dpy, s->input_window, s->normal_gc, 
	       BAR_X_PADDING, 
	       BAR_Y_PADDING + s->font->max_bounds.ascent, prompt, 
	       strlen (prompt));

  XDrawString (dpy, s->input_window, s->normal_gc, 
	       BAR_X_PADDING + prompt_width,
	       BAR_Y_PADDING + s->font->max_bounds.ascent, input, 
	       input_len);
}

char *
get_input (char *prompt)
{
  screen_info *s = current_screen ();
  int cur_len = 0;		/* Current length of the string. */
  int allocated_len=100; /* The amount of memory we allocated for str */
  KeySym ch;
  unsigned int modifier;
  int revert;
  Window fwin;
  char *str;

  /* Allocate some memory to start with */
  str = (char *) malloc ( allocated_len );
  if ( str == NULL )
    {
      PRINT_ERROR ("Out of memory\n");
      exit (EXIT_FAILURE);
    }

  /* We don't want to draw overtop of the program bar. */
  hide_bar (s);

  XMapWindow (dpy, s->input_window);
  XClearWindow (dpy, s->input_window);
  XRaiseWindow (dpy, s->input_window);

  update_input_window (s, prompt, str, cur_len);

  XGetInputFocus (dpy, &fwin, &revert);
  XSetInputFocus (dpy, s->input_window, RevertToPointerRoot, CurrentTime);
  /* XSync (dpy, False); */


  read_key (&ch, &modifier);
  while (ch != XK_Return) 
    {
      PRINT_DEBUG ("key %ld\n", ch);
      if (ch == XK_BackSpace)
	{
	  if (cur_len > 0) cur_len--;
	  update_input_window(s, prompt, str, cur_len);
	}
      else
	{
	  if (cur_len > allocated_len - 1)
	    {
	      allocated_len += 100;
	      str = realloc ( str, allocated_len );
	      if (str == NULL)
		{
		  PRINT_ERROR ("Out of memory\n");
		  exit (EXIT_FAILURE);
		}
	    }
	  str[cur_len] = ch;
	  cur_len++;

	  update_input_window(s, prompt, str, cur_len);
	}

      read_key (&ch, &modifier);
    }

  str[cur_len] = 0;
  XSetInputFocus (dpy, fwin, RevertToPointerRoot, CurrentTime);
  XUnmapWindow (dpy, s->input_window);
  return str;
}