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"
22 #define N_DRAWING_AREAS 3
24 struct drawing_area_ctx {
26 #ifndef DRAW_DEFAULT_CAIRO
29 int width, height, current;
33 GtkWidget *dialog, *promptlabel;
34 struct drawing_area_ctx drawingareas[N_DRAWING_AREAS];
36 #if GTK_CHECK_VERSION(2,0,0)
39 #ifndef DRAW_DEFAULT_CAIRO
44 int passlen, passsize;
45 #if GTK_CHECK_VERSION(3,0,0)
46 GdkDevice *keyboard; /* for gdk_device_grab */
50 static void visually_acknowledge_keypress(struct askpass_ctx *ctx)
53 new_active = rand() % (N_DRAWING_AREAS - 1);
54 if (new_active >= ctx->active_area)
56 ctx->drawingareas[ctx->active_area].current = 0;
57 gtk_widget_queue_draw(ctx->drawingareas[ctx->active_area].area);
58 ctx->drawingareas[new_active].current = 1;
59 gtk_widget_queue_draw(ctx->drawingareas[new_active].area);
60 ctx->active_area = new_active;
63 static int last_char_len(struct askpass_ctx *ctx)
66 * GTK always encodes in UTF-8, so we can do this in a fixed way.
69 assert(ctx->passlen > 0);
71 while ((unsigned)((unsigned char)ctx->passphrase[i] - 0x80) < 0x40) {
76 return ctx->passlen - i;
79 static void add_text_to_passphrase(struct askpass_ctx *ctx, gchar *str)
81 int len = strlen(str);
82 if (ctx->passlen + len >= ctx->passsize) {
83 /* Take some care with buffer expansion, because there are
84 * pieces of passphrase in the old buffer so we should ensure
85 * realloc doesn't leave a copy lying around in the address
87 int oldsize = ctx->passsize;
90 ctx->passsize = (ctx->passlen + len) * 5 / 4 + 1024;
91 newbuf = snewn(ctx->passsize, char);
92 memcpy(newbuf, ctx->passphrase, oldsize);
93 smemclr(ctx->passphrase, oldsize);
94 sfree(ctx->passphrase);
95 ctx->passphrase = newbuf;
97 strcpy(ctx->passphrase + ctx->passlen, str);
99 visually_acknowledge_keypress(ctx);
102 static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
104 struct askpass_ctx *ctx = (struct askpass_ctx *)data;
106 if (event->keyval == GDK_KEY_Return &&
107 event->type == GDK_KEY_PRESS) {
109 } else if (event->keyval == GDK_KEY_Escape &&
110 event->type == GDK_KEY_PRESS) {
111 smemclr(ctx->passphrase, ctx->passsize);
112 ctx->passphrase = NULL;
115 #if GTK_CHECK_VERSION(2,0,0)
116 if (gtk_im_context_filter_keypress(ctx->imc, event))
120 if (event->type == GDK_KEY_PRESS) {
121 if (!strcmp(event->string, "\x15")) {
122 /* Ctrl-U. Wipe out the whole line */
124 visually_acknowledge_keypress(ctx);
125 } else if (!strcmp(event->string, "\x17")) {
126 /* Ctrl-W. Delete back to the last space->nonspace
127 * boundary. We interpret 'space' in a really simple
128 * way (mimicking terminal drivers), and don't attempt
129 * to second-guess exciting Unicode space
131 while (ctx->passlen > 0) {
133 ctx->passlen -= last_char_len(ctx);
134 deleted = ctx->passphrase[ctx->passlen];
135 prior = (ctx->passlen == 0 ? ' ' :
136 ctx->passphrase[ctx->passlen-1]);
137 if (!g_ascii_isspace(deleted) && g_ascii_isspace(prior))
140 visually_acknowledge_keypress(ctx);
141 } else if (event->keyval == GDK_KEY_BackSpace) {
142 /* Backspace. Delete one character. */
143 if (ctx->passlen > 0)
144 ctx->passlen -= last_char_len(ctx);
145 visually_acknowledge_keypress(ctx);
146 #if !GTK_CHECK_VERSION(2,0,0)
147 } else if (event->string[0]) {
148 add_text_to_passphrase(ctx, event->string);
156 #if GTK_CHECK_VERSION(2,0,0)
157 static void input_method_commit_event(GtkIMContext *imc, gchar *str,
160 struct askpass_ctx *ctx = (struct askpass_ctx *)data;
161 add_text_to_passphrase(ctx, str);
165 static gint configure_area(GtkWidget *widget, GdkEventConfigure *event,
168 struct drawing_area_ctx *ctx = (struct drawing_area_ctx *)data;
169 ctx->width = event->width;
170 ctx->height = event->height;
171 gtk_widget_queue_draw(widget);
175 #ifdef DRAW_DEFAULT_CAIRO
176 static void askpass_redraw_cairo(cairo_t *cr, struct drawing_area_ctx *ctx)
178 cairo_set_source_rgb(cr, 1-ctx->current, 1-ctx->current, 1-ctx->current);
182 static void askpass_redraw_gdk(GdkWindow *win, struct drawing_area_ctx *ctx)
184 GdkGC *gc = gdk_gc_new(win);
185 gdk_gc_set_foreground(gc, &ctx->cols[ctx->current]);
186 gdk_draw_rectangle(win, gc, TRUE, 0, 0, ctx->width, ctx->height);
191 #if GTK_CHECK_VERSION(3,0,0)
192 static gint draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
194 struct drawing_area_ctx *ctx = (struct drawing_area_ctx *)data;
195 askpass_redraw_cairo(cr, ctx);
199 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
202 struct drawing_area_ctx *ctx = (struct drawing_area_ctx *)data;
204 #ifdef DRAW_DEFAULT_CAIRO
205 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(ctx->area));
206 askpass_redraw_cairo(cr, ctx);
209 askpass_redraw_gdk(gtk_widget_get_window(ctx->area), ctx);
216 static int try_grab_keyboard(struct askpass_ctx *ctx)
220 #if GTK_CHECK_VERSION(3,0,0)
222 * Grabbing the keyboard is quite complicated in GTK 3.
224 GdkDeviceManager *dm;
225 GdkDevice *pointer, *keyboard;
227 dm = gdk_display_get_device_manager
228 (gtk_widget_get_display(ctx->dialog));
232 pointer = gdk_device_manager_get_client_pointer(dm);
235 keyboard = gdk_device_get_associated_device(pointer);
238 if (gdk_device_get_source(keyboard) != GDK_SOURCE_KEYBOARD)
241 ctx->keyboard = keyboard;
242 ret = gdk_device_grab(ctx->keyboard,
243 gtk_widget_get_window(ctx->dialog),
246 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
251 * It's much simpler in GTK 1 and 2!
253 ret = gdk_keyboard_grab(gtk_widget_get_window(ctx->dialog),
254 FALSE, GDK_CURRENT_TIME);
257 return ret == GDK_GRAB_SUCCESS;
260 typedef int (try_grab_fn_t)(struct askpass_ctx *ctx);
262 static int repeatedly_try_grab(struct askpass_ctx *ctx, try_grab_fn_t fn)
265 * Repeatedly try to grab some aspect of the X server. We have to
266 * do this rather than just trying once, because there is at least
267 * one important situation in which the grab may fail the first
268 * time: any user who is launching an add-key operation off some
269 * kind of window manager hotkey will almost by definition be
270 * running this script with a keyboard grab already active, namely
271 * the one-key grab that the WM (or whatever) uses to detect
272 * presses of the hotkey. So at the very least we have to give the
273 * user time to release that key.
275 const useconds_t ms_limit = 5*1000000; /* try for 5 seconds */
276 const useconds_t ms_step = 1000000/8; /* at 1/8 second intervals */
279 for (ms = 0; ms < ms_limit; ms += ms_step) {
287 static const char *gtk_askpass_setup(struct askpass_ctx *ctx,
288 const char *window_title,
289 const char *prompt_text)
294 ctx->passsize = 2048;
295 ctx->passphrase = snewn(ctx->passsize, char);
300 ctx->dialog = gtk_dialog_new();
301 gtk_window_set_title(GTK_WINDOW(ctx->dialog), window_title);
302 ctx->promptlabel = gtk_label_new(prompt_text);
303 gtk_label_set_line_wrap(GTK_LABEL(ctx->promptlabel), TRUE);
304 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area
305 (GTK_DIALOG(ctx->dialog))),
307 #if GTK_CHECK_VERSION(2,0,0)
308 ctx->imc = gtk_im_multicontext_new();
310 #ifndef DRAW_DEFAULT_CAIRO
313 ctx->colmap = gdk_colormap_get_system();
314 ctx->cols[0].red = ctx->cols[0].green = ctx->cols[0].blue = 0xFFFF;
315 ctx->cols[1].red = ctx->cols[1].green = ctx->cols[1].blue = 0;
316 gdk_colormap_alloc_colors(ctx->colmap, ctx->cols, 2,
317 FALSE, TRUE, success);
318 if (!success[0] | !success[1])
319 return "unable to allocate colours";
322 for (i = 0; i < N_DRAWING_AREAS; i++) {
323 ctx->drawingareas[i].area = gtk_drawing_area_new();
324 #ifndef DRAW_DEFAULT_CAIRO
325 ctx->drawingareas[i].cols = ctx->cols;
327 ctx->drawingareas[i].current = 0;
328 ctx->drawingareas[i].width = ctx->drawingareas[i].height = 0;
329 /* It would be nice to choose this size in some more
330 * context-sensitive way, like measuring the size of some
331 * piece of template text. */
332 gtk_widget_set_size_request(ctx->drawingareas[i].area, 32, 32);
333 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_action_area
334 (GTK_DIALOG(ctx->dialog))),
335 ctx->drawingareas[i].area);
336 g_signal_connect(G_OBJECT(ctx->drawingareas[i].area),
338 G_CALLBACK(configure_area),
339 &ctx->drawingareas[i]);
340 #if GTK_CHECK_VERSION(3,0,0)
341 g_signal_connect(G_OBJECT(ctx->drawingareas[i].area),
343 G_CALLBACK(draw_area),
344 &ctx->drawingareas[i]);
346 g_signal_connect(G_OBJECT(ctx->drawingareas[i].area),
348 G_CALLBACK(expose_area),
349 &ctx->drawingareas[i]);
351 gtk_widget_show(ctx->drawingareas[i].area);
353 ctx->active_area = rand() % N_DRAWING_AREAS;
354 ctx->drawingareas[ctx->active_area].current = 1;
357 * Arrange to receive key events. We don't really need to worry
358 * from a UI perspective about which widget gets the events, as
359 * long as we know which it is so we can catch them. So we'll pick
360 * the prompt label at random, and we'll use gtk_grab_add to
361 * ensure key events go to it.
363 gtk_widget_set_sensitive(ctx->promptlabel, TRUE);
365 #if GTK_CHECK_VERSION(2,0,0)
366 gtk_window_set_keep_above(GTK_WINDOW(ctx->dialog), TRUE);
370 * Actually show the window, and wait for it to be shown.
372 gtk_widget_show_now(ctx->dialog);
375 * Now that the window is displayed, make it grab the input focus.
377 gtk_grab_add(ctx->promptlabel);
378 if (!repeatedly_try_grab(ctx, try_grab_keyboard))
379 return "unable to grab keyboard";
382 * And now that we've got the keyboard grab, connect up our
383 * keyboard handlers, and display the prompt.
385 #if GTK_CHECK_VERSION(2,0,0)
386 g_signal_connect(G_OBJECT(ctx->imc), "commit",
387 G_CALLBACK(input_method_commit_event), ctx);
389 g_signal_connect(G_OBJECT(ctx->promptlabel), "key_press_event",
390 G_CALLBACK(key_event), ctx);
391 g_signal_connect(G_OBJECT(ctx->promptlabel), "key_release_event",
392 G_CALLBACK(key_event), ctx);
393 #if GTK_CHECK_VERSION(2,0,0)
394 gtk_im_context_set_client_window(ctx->imc,
395 gtk_widget_get_window(ctx->dialog));
397 gtk_widget_show(ctx->promptlabel);
402 static void gtk_askpass_cleanup(struct askpass_ctx *ctx)
404 #if GTK_CHECK_VERSION(3,0,0)
405 gdk_device_ungrab(ctx->keyboard, GDK_CURRENT_TIME);
407 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
409 gtk_grab_remove(ctx->promptlabel);
411 if (ctx->passphrase) {
412 assert(ctx->passlen < ctx->passsize);
413 ctx->passphrase[ctx->passlen] = '\0';
416 gtk_widget_destroy(ctx->dialog);
419 static int setup_gtk(const char *display)
421 static int gtk_initialised = FALSE;
424 char **argv = real_argv;
431 argv[argc++] = dupstr("dummy");
432 argv[argc++] = dupprintf("--display=%s", display);
434 ret = gtk_init_check(&argc, &argv);
438 gtk_initialised = ret;
442 char *gtk_askpass_main(const char *display, const char *wintitle,
443 const char *prompt, int *success)
445 struct askpass_ctx actx, *ctx = &actx;
448 /* In case gtk_init hasn't been called yet by the program */
449 if (!setup_gtk(display)) {
451 return dupstr("unable to initialise GTK");
454 if ((err = gtk_askpass_setup(ctx, wintitle, prompt)) != NULL) {
456 return dupprintf("%s", err);
459 gtk_askpass_cleanup(ctx);
461 if (ctx->passphrase) {
463 return ctx->passphrase;
466 return dupstr("passphrase input cancelled");
471 void modalfatalbox(const char *p, ...)
474 fprintf(stderr, "FATAL ERROR: ");
476 vfprintf(stderr, p, ap);
482 int main(int argc, char **argv)
484 int success, exitcode;
487 gtk_init(&argc, &argv);
491 ret = dupprintf("usage: %s <prompt text>", argv[0]);
494 ret = gtk_askpass_main(argv[1], &success);
507 smemclr(ret, strlen(ret));