2 * GTK implementation of a GUI password/passphrase prompt.
9 #include <gdk/gdkkeysyms.h>
13 #define N_DRAWING_AREAS 3
15 struct drawing_area_ctx {
18 int width, height, current;
22 GtkWidget *dialog, *promptlabel;
23 struct drawing_area_ctx drawingareas[N_DRAWING_AREAS];
29 int passlen, passsize;
32 static void visually_acknowledge_keypress(struct askpass_ctx *ctx)
35 new_active = rand() % (N_DRAWING_AREAS - 1);
36 if (new_active >= ctx->active_area)
38 ctx->drawingareas[ctx->active_area].current = 0;
39 gtk_widget_queue_draw(ctx->drawingareas[ctx->active_area].area);
40 ctx->drawingareas[new_active].current = 1;
41 gtk_widget_queue_draw(ctx->drawingareas[new_active].area);
42 ctx->active_area = new_active;
45 static int last_char_len(struct askpass_ctx *ctx)
48 * GTK always encodes in UTF-8, so we can do this in a fixed way.
51 assert(ctx->passlen > 0);
53 while ((unsigned)((unsigned char)ctx->passphrase[i] - 0x80) < 0x40) {
58 return ctx->passlen - i;
61 static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
63 struct askpass_ctx *ctx = (struct askpass_ctx *)data;
64 if (event->keyval == GDK_Return) {
66 } else if (event->keyval == GDK_Escape) {
67 smemclr(ctx->passphrase, ctx->passsize);
68 ctx->passphrase = NULL;
71 if (gtk_im_context_filter_keypress(ctx->imc, event))
74 if (event->type == GDK_KEY_PRESS) {
75 if (!strcmp(event->string, "\x15")) {
76 /* Ctrl-U. Wipe out the whole line */
78 visually_acknowledge_keypress(ctx);
79 } else if (!strcmp(event->string, "\x17")) {
80 /* Ctrl-W. Delete back to the last space->nonspace
81 * boundary. We interpret 'space' in a really simple
82 * way (mimicking terminal drivers), and don't attempt
83 * to second-guess exciting Unicode space
85 while (ctx->passlen > 0) {
87 ctx->passlen -= last_char_len(ctx);
88 deleted = ctx->passphrase[ctx->passlen];
89 prior = (ctx->passlen == 0 ? ' ' :
90 ctx->passphrase[ctx->passlen-1]);
91 if (!g_ascii_isspace(deleted) && g_ascii_isspace(prior))
94 visually_acknowledge_keypress(ctx);
95 } else if (event->keyval == GDK_BackSpace) {
96 /* Backspace. Delete one character. */
98 ctx->passlen -= last_char_len(ctx);
99 visually_acknowledge_keypress(ctx);
106 static void input_method_commit_event(GtkIMContext *imc, gchar *str,
109 struct askpass_ctx *ctx = (struct askpass_ctx *)data;
110 int len = strlen(str);
111 if (ctx->passlen + len >= ctx->passsize) {
112 /* Take some care with buffer expansion, because there are
113 * pieces of passphrase in the old buffer so we should ensure
114 * realloc doesn't leave a copy lying around in the address
116 int oldsize = ctx->passsize;
119 ctx->passsize = (ctx->passlen + len) * 5 / 4 + 1024;
120 newbuf = snewn(ctx->passsize, char);
121 memcpy(newbuf, ctx->passphrase, oldsize);
122 smemclr(ctx->passphrase, oldsize);
123 sfree(ctx->passphrase);
124 ctx->passphrase = newbuf;
126 strcpy(ctx->passphrase + ctx->passlen, str);
128 visually_acknowledge_keypress(ctx);
131 static gint configure_area(GtkWidget *widget, GdkEventConfigure *event,
134 struct drawing_area_ctx *ctx = (struct drawing_area_ctx *)data;
135 ctx->width = event->width;
136 ctx->height = event->height;
137 gtk_widget_queue_draw(widget);
141 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
144 struct drawing_area_ctx *ctx = (struct drawing_area_ctx *)data;
146 GdkGC *gc = gdk_gc_new(ctx->area->window);
147 gdk_gc_set_foreground(gc, &ctx->cols[ctx->current]);
148 gdk_draw_rectangle(widget->window, gc, TRUE,
149 0, 0, ctx->width, ctx->height);
154 static int try_grab_keyboard(struct askpass_ctx *ctx)
156 int ret = gdk_keyboard_grab(ctx->dialog->window, FALSE, GDK_CURRENT_TIME);
157 return ret == GDK_GRAB_SUCCESS;
160 typedef int (try_grab_fn_t)(struct askpass_ctx *ctx);
162 static int repeatedly_try_grab(struct askpass_ctx *ctx, try_grab_fn_t fn)
165 * Repeatedly try to grab some aspect of the X server. We have to
166 * do this rather than just trying once, because there is at least
167 * one important situation in which the grab may fail the first
168 * time: any user who is launching an add-key operation off some
169 * kind of window manager hotkey will almost by definition be
170 * running this script with a keyboard grab already active, namely
171 * the one-key grab that the WM (or whatever) uses to detect
172 * presses of the hotkey. So at the very least we have to give the
173 * user time to release that key.
175 const useconds_t ms_limit = 5*1000000; /* try for 5 seconds */
176 const useconds_t ms_step = 1000000/8; /* at 1/8 second intervals */
179 for (ms = 0; ms < ms_limit; ms++) {
188 static const char *gtk_askpass_setup(struct askpass_ctx *ctx,
189 const char *window_title,
190 const char *prompt_text)
196 ctx->passsize = 2048;
197 ctx->passphrase = snewn(ctx->passsize, char);
202 ctx->dialog = gtk_dialog_new();
203 gtk_window_set_title(GTK_WINDOW(ctx->dialog), window_title);
204 ctx->promptlabel = gtk_label_new(prompt_text);
205 gtk_label_set_line_wrap(GTK_LABEL(ctx->promptlabel), TRUE);
206 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area
207 (GTK_DIALOG(ctx->dialog))),
209 ctx->imc = gtk_im_multicontext_new();
210 ctx->colmap = gdk_colormap_get_system();
211 ctx->cols[0].red = ctx->cols[0].green = ctx->cols[0].blue = 0xFFFF;
212 ctx->cols[1].red = ctx->cols[1].green = ctx->cols[1].blue = 0;
213 gdk_colormap_alloc_colors(ctx->colmap, ctx->cols, 2,
214 FALSE, TRUE, success);
215 if (!success[0] | !success[1])
216 return "unable to allocate colours";
217 for (i = 0; i < N_DRAWING_AREAS; i++) {
218 ctx->drawingareas[i].area = gtk_drawing_area_new();
219 ctx->drawingareas[i].cols = ctx->cols;
220 ctx->drawingareas[i].current = 0;
221 ctx->drawingareas[i].width = ctx->drawingareas[i].height = 0;
222 /* It would be nice to choose this size in some more
223 * context-sensitive way, like measuring the size of some
224 * piece of template text. */
225 gtk_widget_set_size_request(ctx->drawingareas[i].area, 32, 32);
226 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_action_area
227 (GTK_DIALOG(ctx->dialog))),
228 ctx->drawingareas[i].area);
229 gtk_signal_connect(GTK_OBJECT(ctx->drawingareas[i].area),
231 GTK_SIGNAL_FUNC(configure_area),
232 &ctx->drawingareas[i]);
233 gtk_signal_connect(GTK_OBJECT(ctx->drawingareas[i].area),
235 GTK_SIGNAL_FUNC(expose_area),
236 &ctx->drawingareas[i]);
237 gtk_widget_show(ctx->drawingareas[i].area);
239 ctx->active_area = rand() % N_DRAWING_AREAS;
240 ctx->drawingareas[ctx->active_area].current = 1;
243 * Arrange to receive key events. We don't really need to worry
244 * from a UI perspective about which widget gets the events, as
245 * long as we know which it is so we can catch them. So we'll pick
246 * the prompt label at random, and we'll use gtk_grab_add to
247 * ensure key events go to it.
249 gtk_widget_set_sensitive(ctx->promptlabel, TRUE);
250 gtk_window_set_keep_above(GTK_WINDOW(ctx->dialog), TRUE);
253 * Actually show the window, and wait for it to be shown.
255 gtk_widget_show_now(ctx->dialog);
258 * Now that the window is displayed, make it grab the input focus.
260 gtk_grab_add(ctx->promptlabel);
261 if (!repeatedly_try_grab(ctx, try_grab_keyboard))
262 return "unable to grab keyboard";
265 * And now that we've got the keyboard grab, connect up our
266 * keyboard handlers, and display the prompt.
268 g_signal_connect(G_OBJECT(ctx->imc), "commit",
269 G_CALLBACK(input_method_commit_event), ctx);
270 gtk_signal_connect(GTK_OBJECT(ctx->promptlabel), "key_press_event",
271 GTK_SIGNAL_FUNC(key_event), ctx);
272 gtk_signal_connect(GTK_OBJECT(ctx->promptlabel), "key_release_event",
273 GTK_SIGNAL_FUNC(key_event), ctx);
274 gtk_im_context_set_client_window(ctx->imc, ctx->dialog->window);
275 gtk_widget_show(ctx->promptlabel);
280 static void gtk_askpass_cleanup(struct askpass_ctx *ctx)
282 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
283 gtk_grab_remove(ctx->promptlabel);
285 if (ctx->passphrase) {
286 assert(ctx->passlen < ctx->passsize);
287 ctx->passphrase[ctx->passlen] = '\0';
290 gtk_widget_destroy(ctx->dialog);
293 static int setup_gtk(const char *display)
295 static int gtk_initialised = FALSE;
298 char **argv = real_argv;
305 argv[argc++] = dupstr("dummy");
306 argv[argc++] = dupprintf("--display=%s", display);
308 ret = gtk_init_check(&argc, &argv);
312 gtk_initialised = ret;
316 char *gtk_askpass_main(const char *display, const char *wintitle,
317 const char *prompt, int *success)
319 struct askpass_ctx actx, *ctx = &actx;
322 /* In case gtk_init hasn't been called yet by the program */
323 if (!setup_gtk(display)) {
325 return dupstr("unable to initialise GTK");
328 if ((err = gtk_askpass_setup(ctx, wintitle, prompt)) != NULL) {
330 return dupprintf("%s", err);
333 gtk_askpass_cleanup(ctx);
335 if (ctx->passphrase) {
337 return ctx->passphrase;
340 return dupstr("passphrase input cancelled");
345 void modalfatalbox(char *p, ...)
348 fprintf(stderr, "FATAL ERROR: ");
350 vfprintf(stderr, p, ap);
356 int main(int argc, char **argv)
358 int success, exitcode;
361 gtk_init(&argc, &argv);
365 ret = dupprintf("usage: %s <prompt text>", argv[0]);
368 ret = gtk_askpass_main(argv[1], &success);
381 smemclr(ret, strlen(ret));