2 * GTK implementation of a GUI password/passphrase prompt.
13 #if !GTK_CHECK_VERSION(3,0,0)
14 #include <gdk/gdkkeysyms.h>
18 #include "gtkcompat.h"
23 #define N_DRAWING_AREAS 3
25 struct drawing_area_ctx {
27 #ifndef DRAW_DEFAULT_CAIRO
30 int width, height, current;
34 GtkWidget *dialog, *promptlabel;
35 struct drawing_area_ctx drawingareas[N_DRAWING_AREAS];
37 #if GTK_CHECK_VERSION(2,0,0)
40 #ifndef DRAW_DEFAULT_CAIRO
45 int passlen, passsize;
46 #if GTK_CHECK_VERSION(3,0,0)
47 GdkDevice *keyboard; /* for gdk_device_grab */
51 static void visually_acknowledge_keypress(struct askpass_ctx *ctx)
54 new_active = rand() % (N_DRAWING_AREAS - 1);
55 if (new_active >= ctx->active_area)
57 ctx->drawingareas[ctx->active_area].current = 0;
58 gtk_widget_queue_draw(ctx->drawingareas[ctx->active_area].area);
59 ctx->drawingareas[new_active].current = 1;
60 gtk_widget_queue_draw(ctx->drawingareas[new_active].area);
61 ctx->active_area = new_active;
64 static int last_char_len(struct askpass_ctx *ctx)
67 * GTK always encodes in UTF-8, so we can do this in a fixed way.
70 assert(ctx->passlen > 0);
72 while ((unsigned)((unsigned char)ctx->passphrase[i] - 0x80) < 0x40) {
77 return ctx->passlen - i;
80 static void add_text_to_passphrase(struct askpass_ctx *ctx, gchar *str)
82 int len = strlen(str);
83 if (ctx->passlen + len >= ctx->passsize) {
84 /* Take some care with buffer expansion, because there are
85 * pieces of passphrase in the old buffer so we should ensure
86 * realloc doesn't leave a copy lying around in the address
88 int oldsize = ctx->passsize;
91 ctx->passsize = (ctx->passlen + len) * 5 / 4 + 1024;
92 newbuf = snewn(ctx->passsize, char);
93 memcpy(newbuf, ctx->passphrase, oldsize);
94 smemclr(ctx->passphrase, oldsize);
95 sfree(ctx->passphrase);
96 ctx->passphrase = newbuf;
98 strcpy(ctx->passphrase + ctx->passlen, str);
100 visually_acknowledge_keypress(ctx);
103 static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
105 struct askpass_ctx *ctx = (struct askpass_ctx *)data;
107 if (event->keyval == GDK_KEY_Return &&
108 event->type == GDK_KEY_PRESS) {
110 } else if (event->keyval == GDK_KEY_Escape &&
111 event->type == GDK_KEY_PRESS) {
112 smemclr(ctx->passphrase, ctx->passsize);
113 ctx->passphrase = NULL;
116 #if GTK_CHECK_VERSION(2,0,0)
117 if (gtk_im_context_filter_keypress(ctx->imc, event))
121 if (event->type == GDK_KEY_PRESS) {
122 if (!strcmp(event->string, "\x15")) {
123 /* Ctrl-U. Wipe out the whole line */
125 visually_acknowledge_keypress(ctx);
126 } else if (!strcmp(event->string, "\x17")) {
127 /* Ctrl-W. Delete back to the last space->nonspace
128 * boundary. We interpret 'space' in a really simple
129 * way (mimicking terminal drivers), and don't attempt
130 * to second-guess exciting Unicode space
132 while (ctx->passlen > 0) {
134 ctx->passlen -= last_char_len(ctx);
135 deleted = ctx->passphrase[ctx->passlen];
136 prior = (ctx->passlen == 0 ? ' ' :
137 ctx->passphrase[ctx->passlen-1]);
138 if (!g_ascii_isspace(deleted) && g_ascii_isspace(prior))
141 visually_acknowledge_keypress(ctx);
142 } else if (event->keyval == GDK_KEY_BackSpace) {
143 /* Backspace. Delete one character. */
144 if (ctx->passlen > 0)
145 ctx->passlen -= last_char_len(ctx);
146 visually_acknowledge_keypress(ctx);
147 #if !GTK_CHECK_VERSION(2,0,0)
148 } else if (event->string[0]) {
149 add_text_to_passphrase(ctx, event->string);
157 #if GTK_CHECK_VERSION(2,0,0)
158 static void input_method_commit_event(GtkIMContext *imc, gchar *str,
161 struct askpass_ctx *ctx = (struct askpass_ctx *)data;
162 add_text_to_passphrase(ctx, str);
166 static gint configure_area(GtkWidget *widget, GdkEventConfigure *event,
169 struct drawing_area_ctx *ctx = (struct drawing_area_ctx *)data;
170 ctx->width = event->width;
171 ctx->height = event->height;
172 gtk_widget_queue_draw(widget);
176 #ifdef DRAW_DEFAULT_CAIRO
177 static void askpass_redraw_cairo(cairo_t *cr, struct drawing_area_ctx *ctx)
179 cairo_set_source_rgb(cr, 1-ctx->current, 1-ctx->current, 1-ctx->current);
183 static void askpass_redraw_gdk(GdkWindow *win, struct drawing_area_ctx *ctx)
185 GdkGC *gc = gdk_gc_new(win);
186 gdk_gc_set_foreground(gc, &ctx->cols[ctx->current]);
187 gdk_draw_rectangle(win, gc, TRUE, 0, 0, ctx->width, ctx->height);
192 #if GTK_CHECK_VERSION(3,0,0)
193 static gint draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
195 struct drawing_area_ctx *ctx = (struct drawing_area_ctx *)data;
196 askpass_redraw_cairo(cr, ctx);
200 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
203 struct drawing_area_ctx *ctx = (struct drawing_area_ctx *)data;
205 #ifdef DRAW_DEFAULT_CAIRO
206 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(ctx->area));
207 askpass_redraw_cairo(cr, ctx);
210 askpass_redraw_gdk(gtk_widget_get_window(ctx->area), ctx);
217 static int try_grab_keyboard(struct askpass_ctx *ctx)
221 #if GTK_CHECK_VERSION(3,0,0)
223 * Grabbing the keyboard is quite complicated in GTK 3.
225 GdkDeviceManager *dm;
226 GdkDevice *pointer, *keyboard;
228 dm = gdk_display_get_device_manager
229 (gtk_widget_get_display(ctx->dialog));
233 pointer = gdk_device_manager_get_client_pointer(dm);
236 keyboard = gdk_device_get_associated_device(pointer);
239 if (gdk_device_get_source(keyboard) != GDK_SOURCE_KEYBOARD)
242 ctx->keyboard = keyboard;
243 ret = gdk_device_grab(ctx->keyboard,
244 gtk_widget_get_window(ctx->dialog),
247 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
252 * It's much simpler in GTK 1 and 2!
254 ret = gdk_keyboard_grab(gtk_widget_get_window(ctx->dialog),
255 FALSE, GDK_CURRENT_TIME);
258 return ret == GDK_GRAB_SUCCESS;
261 typedef int (try_grab_fn_t)(struct askpass_ctx *ctx);
263 static int repeatedly_try_grab(struct askpass_ctx *ctx, try_grab_fn_t fn)
266 * Repeatedly try to grab some aspect of the X server. We have to
267 * do this rather than just trying once, because there is at least
268 * one important situation in which the grab may fail the first
269 * time: any user who is launching an add-key operation off some
270 * kind of window manager hotkey will almost by definition be
271 * running this script with a keyboard grab already active, namely
272 * the one-key grab that the WM (or whatever) uses to detect
273 * presses of the hotkey. So at the very least we have to give the
274 * user time to release that key.
276 const useconds_t ms_limit = 5*1000000; /* try for 5 seconds */
277 const useconds_t ms_step = 1000000/8; /* at 1/8 second intervals */
280 for (ms = 0; ms < ms_limit; ms += ms_step) {
288 static const char *gtk_askpass_setup(struct askpass_ctx *ctx,
289 const char *window_title,
290 const char *prompt_text)
296 ctx->passsize = 2048;
297 ctx->passphrase = snewn(ctx->passsize, char);
302 ctx->dialog = our_dialog_new();
303 gtk_window_set_title(GTK_WINDOW(ctx->dialog), window_title);
304 gtk_window_set_position(GTK_WINDOW(ctx->dialog), GTK_WIN_POS_CENTER);
305 ctx->promptlabel = gtk_label_new(prompt_text);
306 align_label_left(GTK_LABEL(ctx->promptlabel));
307 gtk_label_set_line_wrap(GTK_LABEL(ctx->promptlabel), TRUE);
308 #if GTK_CHECK_VERSION(3,0,0)
309 gtk_label_set_width_chars(GTK_LABEL(ctx->promptlabel), 48);
311 our_dialog_add_to_content_area(GTK_WINDOW(ctx->dialog),
312 ctx->promptlabel, TRUE, TRUE, 0);
313 #if GTK_CHECK_VERSION(2,0,0)
314 ctx->imc = gtk_im_multicontext_new();
316 #ifndef DRAW_DEFAULT_CAIRO
319 ctx->colmap = gdk_colormap_get_system();
320 ctx->cols[0].red = ctx->cols[0].green = ctx->cols[0].blue = 0xFFFF;
321 ctx->cols[1].red = ctx->cols[1].green = ctx->cols[1].blue = 0;
322 gdk_colormap_alloc_colors(ctx->colmap, ctx->cols, 2,
323 FALSE, TRUE, success);
324 if (!success[0] | !success[1])
325 return "unable to allocate colours";
329 action_area = our_dialog_make_action_hbox(GTK_WINDOW(ctx->dialog));
331 for (i = 0; i < N_DRAWING_AREAS; i++) {
332 ctx->drawingareas[i].area = gtk_drawing_area_new();
333 #ifndef DRAW_DEFAULT_CAIRO
334 ctx->drawingareas[i].cols = ctx->cols;
336 ctx->drawingareas[i].current = 0;
337 ctx->drawingareas[i].width = ctx->drawingareas[i].height = 0;
338 /* It would be nice to choose this size in some more
339 * context-sensitive way, like measuring the size of some
340 * piece of template text. */
341 gtk_widget_set_size_request(ctx->drawingareas[i].area, 32, 32);
342 gtk_box_pack_end(action_area, ctx->drawingareas[i].area,
344 g_signal_connect(G_OBJECT(ctx->drawingareas[i].area),
346 G_CALLBACK(configure_area),
347 &ctx->drawingareas[i]);
348 #if GTK_CHECK_VERSION(3,0,0)
349 g_signal_connect(G_OBJECT(ctx->drawingareas[i].area),
351 G_CALLBACK(draw_area),
352 &ctx->drawingareas[i]);
354 g_signal_connect(G_OBJECT(ctx->drawingareas[i].area),
356 G_CALLBACK(expose_area),
357 &ctx->drawingareas[i]);
359 gtk_widget_show(ctx->drawingareas[i].area);
361 ctx->active_area = rand() % N_DRAWING_AREAS;
362 ctx->drawingareas[ctx->active_area].current = 1;
365 * Arrange to receive key events. We don't really need to worry
366 * from a UI perspective about which widget gets the events, as
367 * long as we know which it is so we can catch them. So we'll pick
368 * the prompt label at random, and we'll use gtk_grab_add to
369 * ensure key events go to it.
371 gtk_widget_set_sensitive(ctx->promptlabel, TRUE);
373 #if GTK_CHECK_VERSION(2,0,0)
374 gtk_window_set_keep_above(GTK_WINDOW(ctx->dialog), TRUE);
378 * Actually show the window, and wait for it to be shown.
380 gtk_widget_show_now(ctx->dialog);
383 * Now that the window is displayed, make it grab the input focus.
385 gtk_grab_add(ctx->promptlabel);
386 if (!repeatedly_try_grab(ctx, try_grab_keyboard))
387 return "unable to grab keyboard";
390 * And now that we've got the keyboard grab, connect up our
391 * keyboard handlers, and display the prompt.
393 #if GTK_CHECK_VERSION(2,0,0)
394 g_signal_connect(G_OBJECT(ctx->imc), "commit",
395 G_CALLBACK(input_method_commit_event), ctx);
397 g_signal_connect(G_OBJECT(ctx->promptlabel), "key_press_event",
398 G_CALLBACK(key_event), ctx);
399 g_signal_connect(G_OBJECT(ctx->promptlabel), "key_release_event",
400 G_CALLBACK(key_event), ctx);
401 #if GTK_CHECK_VERSION(2,0,0)
402 gtk_im_context_set_client_window(ctx->imc,
403 gtk_widget_get_window(ctx->dialog));
405 gtk_widget_show(ctx->promptlabel);
410 static void gtk_askpass_cleanup(struct askpass_ctx *ctx)
412 #if GTK_CHECK_VERSION(3,0,0)
413 gdk_device_ungrab(ctx->keyboard, GDK_CURRENT_TIME);
415 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
417 gtk_grab_remove(ctx->promptlabel);
419 if (ctx->passphrase) {
420 assert(ctx->passlen < ctx->passsize);
421 ctx->passphrase[ctx->passlen] = '\0';
424 gtk_widget_destroy(ctx->dialog);
427 static int setup_gtk(const char *display)
429 static int gtk_initialised = FALSE;
432 char **argv = real_argv;
439 argv[argc++] = dupstr("dummy");
440 argv[argc++] = dupprintf("--display=%s", display);
442 ret = gtk_init_check(&argc, &argv);
446 gtk_initialised = ret;
450 char *gtk_askpass_main(const char *display, const char *wintitle,
451 const char *prompt, int *success)
453 struct askpass_ctx actx, *ctx = &actx;
456 /* In case gtk_init hasn't been called yet by the program */
457 if (!setup_gtk(display)) {
459 return dupstr("unable to initialise GTK");
462 if ((err = gtk_askpass_setup(ctx, wintitle, prompt)) != NULL) {
464 return dupprintf("%s", err);
467 gtk_askpass_cleanup(ctx);
469 if (ctx->passphrase) {
471 return ctx->passphrase;
474 return dupstr("passphrase input cancelled");
479 void modalfatalbox(const char *p, ...)
482 fprintf(stderr, "FATAL ERROR: ");
484 vfprintf(stderr, p, ap);
490 int main(int argc, char **argv)
492 int success, exitcode;
495 gtk_init(&argc, &argv);
499 ret = dupprintf("usage: %s <prompt text>", argv[0]);
502 ret = gtk_askpass_main(argv[1], &success);
515 smemclr(ret, strlen(ret));