2 * GTK implementation of a GUI password/passphrase prompt.
12 #include <gdk/gdkkeysyms.h>
14 #include "gtkcompat.h"
18 #define N_DRAWING_AREAS 3
20 struct drawing_area_ctx {
23 int width, height, current;
27 GtkWidget *dialog, *promptlabel;
28 struct drawing_area_ctx drawingareas[N_DRAWING_AREAS];
30 #if GTK_CHECK_VERSION(2,0,0)
36 int passlen, passsize;
39 static void visually_acknowledge_keypress(struct askpass_ctx *ctx)
42 new_active = rand() % (N_DRAWING_AREAS - 1);
43 if (new_active >= ctx->active_area)
45 ctx->drawingareas[ctx->active_area].current = 0;
46 gtk_widget_queue_draw(ctx->drawingareas[ctx->active_area].area);
47 ctx->drawingareas[new_active].current = 1;
48 gtk_widget_queue_draw(ctx->drawingareas[new_active].area);
49 ctx->active_area = new_active;
52 static int last_char_len(struct askpass_ctx *ctx)
55 * GTK always encodes in UTF-8, so we can do this in a fixed way.
58 assert(ctx->passlen > 0);
60 while ((unsigned)((unsigned char)ctx->passphrase[i] - 0x80) < 0x40) {
65 return ctx->passlen - i;
68 static void add_text_to_passphrase(struct askpass_ctx *ctx, gchar *str)
70 int len = strlen(str);
71 if (ctx->passlen + len >= ctx->passsize) {
72 /* Take some care with buffer expansion, because there are
73 * pieces of passphrase in the old buffer so we should ensure
74 * realloc doesn't leave a copy lying around in the address
76 int oldsize = ctx->passsize;
79 ctx->passsize = (ctx->passlen + len) * 5 / 4 + 1024;
80 newbuf = snewn(ctx->passsize, char);
81 memcpy(newbuf, ctx->passphrase, oldsize);
82 smemclr(ctx->passphrase, oldsize);
83 sfree(ctx->passphrase);
84 ctx->passphrase = newbuf;
86 strcpy(ctx->passphrase + ctx->passlen, str);
88 visually_acknowledge_keypress(ctx);
91 static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
93 struct askpass_ctx *ctx = (struct askpass_ctx *)data;
95 if (event->keyval == GDK_Return && event->type == GDK_KEY_PRESS) {
97 } else if (event->keyval == GDK_Escape && event->type == GDK_KEY_PRESS) {
98 smemclr(ctx->passphrase, ctx->passsize);
99 ctx->passphrase = NULL;
102 #if GTK_CHECK_VERSION(2,0,0)
103 if (gtk_im_context_filter_keypress(ctx->imc, event))
107 if (event->type == GDK_KEY_PRESS) {
108 if (!strcmp(event->string, "\x15")) {
109 /* Ctrl-U. Wipe out the whole line */
111 visually_acknowledge_keypress(ctx);
112 } else if (!strcmp(event->string, "\x17")) {
113 /* Ctrl-W. Delete back to the last space->nonspace
114 * boundary. We interpret 'space' in a really simple
115 * way (mimicking terminal drivers), and don't attempt
116 * to second-guess exciting Unicode space
118 while (ctx->passlen > 0) {
120 ctx->passlen -= last_char_len(ctx);
121 deleted = ctx->passphrase[ctx->passlen];
122 prior = (ctx->passlen == 0 ? ' ' :
123 ctx->passphrase[ctx->passlen-1]);
124 if (!g_ascii_isspace(deleted) && g_ascii_isspace(prior))
127 visually_acknowledge_keypress(ctx);
128 } else if (event->keyval == GDK_BackSpace) {
129 /* Backspace. Delete one character. */
130 if (ctx->passlen > 0)
131 ctx->passlen -= last_char_len(ctx);
132 visually_acknowledge_keypress(ctx);
133 #if !GTK_CHECK_VERSION(2,0,0)
134 } else if (event->string[0]) {
135 add_text_to_passphrase(ctx, event->string);
143 #if GTK_CHECK_VERSION(2,0,0)
144 static void input_method_commit_event(GtkIMContext *imc, gchar *str,
147 struct askpass_ctx *ctx = (struct askpass_ctx *)data;
148 add_text_to_passphrase(ctx, str);
152 static gint configure_area(GtkWidget *widget, GdkEventConfigure *event,
155 struct drawing_area_ctx *ctx = (struct drawing_area_ctx *)data;
156 ctx->width = event->width;
157 ctx->height = event->height;
158 gtk_widget_queue_draw(widget);
162 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
165 struct drawing_area_ctx *ctx = (struct drawing_area_ctx *)data;
167 GdkGC *gc = gdk_gc_new(ctx->area->window);
168 gdk_gc_set_foreground(gc, &ctx->cols[ctx->current]);
169 gdk_draw_rectangle(widget->window, gc, TRUE,
170 0, 0, ctx->width, ctx->height);
175 static int try_grab_keyboard(struct askpass_ctx *ctx)
177 int ret = gdk_keyboard_grab(ctx->dialog->window, FALSE, GDK_CURRENT_TIME);
178 return ret == GDK_GRAB_SUCCESS;
181 typedef int (try_grab_fn_t)(struct askpass_ctx *ctx);
183 static int repeatedly_try_grab(struct askpass_ctx *ctx, try_grab_fn_t fn)
186 * Repeatedly try to grab some aspect of the X server. We have to
187 * do this rather than just trying once, because there is at least
188 * one important situation in which the grab may fail the first
189 * time: any user who is launching an add-key operation off some
190 * kind of window manager hotkey will almost by definition be
191 * running this script with a keyboard grab already active, namely
192 * the one-key grab that the WM (or whatever) uses to detect
193 * presses of the hotkey. So at the very least we have to give the
194 * user time to release that key.
196 const useconds_t ms_limit = 5*1000000; /* try for 5 seconds */
197 const useconds_t ms_step = 1000000/8; /* at 1/8 second intervals */
200 for (ms = 0; ms < ms_limit; ms += ms_step) {
208 static const char *gtk_askpass_setup(struct askpass_ctx *ctx,
209 const char *window_title,
210 const char *prompt_text)
216 ctx->passsize = 2048;
217 ctx->passphrase = snewn(ctx->passsize, char);
222 ctx->dialog = gtk_dialog_new();
223 gtk_window_set_title(GTK_WINDOW(ctx->dialog), window_title);
224 ctx->promptlabel = gtk_label_new(prompt_text);
225 gtk_label_set_line_wrap(GTK_LABEL(ctx->promptlabel), TRUE);
226 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area
227 (GTK_DIALOG(ctx->dialog))),
229 #if GTK_CHECK_VERSION(2,0,0)
230 ctx->imc = gtk_im_multicontext_new();
232 ctx->colmap = gdk_colormap_get_system();
233 ctx->cols[0].red = ctx->cols[0].green = ctx->cols[0].blue = 0xFFFF;
234 ctx->cols[1].red = ctx->cols[1].green = ctx->cols[1].blue = 0;
235 gdk_colormap_alloc_colors(ctx->colmap, ctx->cols, 2,
236 FALSE, TRUE, success);
237 if (!success[0] | !success[1])
238 return "unable to allocate colours";
239 for (i = 0; i < N_DRAWING_AREAS; i++) {
240 ctx->drawingareas[i].area = gtk_drawing_area_new();
241 ctx->drawingareas[i].cols = ctx->cols;
242 ctx->drawingareas[i].current = 0;
243 ctx->drawingareas[i].width = ctx->drawingareas[i].height = 0;
244 /* It would be nice to choose this size in some more
245 * context-sensitive way, like measuring the size of some
246 * piece of template text. */
247 gtk_widget_set_size_request(ctx->drawingareas[i].area, 32, 32);
248 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_action_area
249 (GTK_DIALOG(ctx->dialog))),
250 ctx->drawingareas[i].area);
251 gtk_signal_connect(GTK_OBJECT(ctx->drawingareas[i].area),
253 GTK_SIGNAL_FUNC(configure_area),
254 &ctx->drawingareas[i]);
255 gtk_signal_connect(GTK_OBJECT(ctx->drawingareas[i].area),
257 GTK_SIGNAL_FUNC(expose_area),
258 &ctx->drawingareas[i]);
259 gtk_widget_show(ctx->drawingareas[i].area);
261 ctx->active_area = rand() % N_DRAWING_AREAS;
262 ctx->drawingareas[ctx->active_area].current = 1;
265 * Arrange to receive key events. We don't really need to worry
266 * from a UI perspective about which widget gets the events, as
267 * long as we know which it is so we can catch them. So we'll pick
268 * the prompt label at random, and we'll use gtk_grab_add to
269 * ensure key events go to it.
271 gtk_widget_set_sensitive(ctx->promptlabel, TRUE);
273 #if GTK_CHECK_VERSION(2,0,0)
274 gtk_window_set_keep_above(GTK_WINDOW(ctx->dialog), TRUE);
278 * Actually show the window, and wait for it to be shown.
280 gtk_widget_show_now(ctx->dialog);
283 * Now that the window is displayed, make it grab the input focus.
285 gtk_grab_add(ctx->promptlabel);
286 if (!repeatedly_try_grab(ctx, try_grab_keyboard))
287 return "unable to grab keyboard";
290 * And now that we've got the keyboard grab, connect up our
291 * keyboard handlers, and display the prompt.
293 #if GTK_CHECK_VERSION(2,0,0)
294 g_signal_connect(G_OBJECT(ctx->imc), "commit",
295 G_CALLBACK(input_method_commit_event), ctx);
297 gtk_signal_connect(GTK_OBJECT(ctx->promptlabel), "key_press_event",
298 GTK_SIGNAL_FUNC(key_event), ctx);
299 gtk_signal_connect(GTK_OBJECT(ctx->promptlabel), "key_release_event",
300 GTK_SIGNAL_FUNC(key_event), ctx);
301 #if GTK_CHECK_VERSION(2,0,0)
302 gtk_im_context_set_client_window(ctx->imc, ctx->dialog->window);
304 gtk_widget_show(ctx->promptlabel);
309 static void gtk_askpass_cleanup(struct askpass_ctx *ctx)
311 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
312 gtk_grab_remove(ctx->promptlabel);
314 if (ctx->passphrase) {
315 assert(ctx->passlen < ctx->passsize);
316 ctx->passphrase[ctx->passlen] = '\0';
319 gtk_widget_destroy(ctx->dialog);
322 static int setup_gtk(const char *display)
324 static int gtk_initialised = FALSE;
327 char **argv = real_argv;
334 argv[argc++] = dupstr("dummy");
335 argv[argc++] = dupprintf("--display=%s", display);
337 ret = gtk_init_check(&argc, &argv);
341 gtk_initialised = ret;
345 char *gtk_askpass_main(const char *display, const char *wintitle,
346 const char *prompt, int *success)
348 struct askpass_ctx actx, *ctx = &actx;
351 /* In case gtk_init hasn't been called yet by the program */
352 if (!setup_gtk(display)) {
354 return dupstr("unable to initialise GTK");
357 if ((err = gtk_askpass_setup(ctx, wintitle, prompt)) != NULL) {
359 return dupprintf("%s", err);
362 gtk_askpass_cleanup(ctx);
364 if (ctx->passphrase) {
366 return ctx->passphrase;
369 return dupstr("passphrase input cancelled");
374 void modalfatalbox(const char *p, ...)
377 fprintf(stderr, "FATAL ERROR: ");
379 vfprintf(stderr, p, ap);
385 int main(int argc, char **argv)
387 int success, exitcode;
390 gtk_init(&argc, &argv);
394 ret = dupprintf("usage: %s <prompt text>", argv[0]);
397 ret = gtk_askpass_main(argv[1], &success);
410 smemclr(ret, strlen(ret));