3333#include <X11/Xlib.h>
3434#include <gdk/gdkx.h>
3535#include <gtk/gtk.h>
36+ #include <gdk/gdkkeysyms.h>
3637
3738#ifdef __GNUC__
3839#define ATTR_UNUSED __attribute__((unused))
@@ -116,6 +117,7 @@ static const char *ocean_stripes[] = {
116117
117118GtkWidget * window , * label , * entry ;
118119gint grab_server , grab_pointer ;
120+ int exit_code ;
119121
120122static void
121123report_failed_grab (const char * what )
@@ -205,19 +207,26 @@ create_tile_pixbuf (GdkPixbuf *dest_pixbuf,
205207}
206208
207209void
208- enter_callback (GtkWidget * widget ATTR_UNUSED ,
209- GtkWidget * entryw )
210+ ungrab ()
210211{
211- const gchar * passphrase ;
212- passphrase = gtk_entry_get_text (GTK_ENTRY (entryw ));
213-
214212 if (grab_server )
215213 XUngrabServer (GDK_DISPLAY ());
216214 if (grab_pointer )
217215 gdk_pointer_ungrab (GDK_CURRENT_TIME );
218216
219217 gdk_keyboard_ungrab (GDK_CURRENT_TIME );
220218 gdk_flush ();
219+ }
220+
221+
222+ void
223+ enter_callback (GtkWidget * widget ATTR_UNUSED ,
224+ GtkWidget * entryw )
225+ {
226+ const gchar * passphrase ;
227+ passphrase = gtk_entry_get_text (GTK_ENTRY (entryw ));
228+
229+ ungrab ();
221230
222231 puts (passphrase );
223232
@@ -226,6 +235,31 @@ enter_callback(GtkWidget *widget ATTR_UNUSED,
226235 gtk_main_quit ();
227236}
228237
238+ void
239+ cancel ()
240+ {
241+ ungrab ();
242+ exit_code = 1 ;
243+ gtk_main_quit ();
244+ }
245+
246+ gboolean
247+ key_press_callback (GtkWidget * widget ATTR_UNUSED ,
248+ GdkEventKey * event ,
249+ GtkWidget * entryw ATTR_UNUSED )
250+ {
251+ gboolean rv = FALSE;
252+ switch (event -> keyval )
253+ {
254+ case GDK_KEY_Escape :
255+ cancel ();
256+ rv = TRUE;
257+ break ;
258+ }
259+
260+ return rv ;
261+ }
262+
229263void
230264passphrase_dialog (char * message )
231265{
@@ -349,6 +383,9 @@ passphrase_dialog(char *message)
349383 g_signal_connect (G_OBJECT (entry ), "activate" ,
350384 G_CALLBACK (enter_callback ),
351385 (gpointer ) entry );
386+ g_signal_connect (window , "key_press_event" ,
387+ G_CALLBACK (key_press_callback ),
388+ (gpointer ) entry );
352389
353390 gtk_window_stick (GTK_WINDOW (window ));
354391 gtk_window_set_keep_above (GTK_WINDOW (window ), TRUE);
@@ -405,6 +442,8 @@ passphrase_dialog(char *message)
405442int
406443main (int argc , char * * argv )
407444{
445+ exit_code = 0 ;
446+
408447 gchar * message ;
409448
410449 gtk_init (& argc , & argv );
@@ -417,5 +456,6 @@ main(int argc, char **argv)
417456 setvbuf (stdout , 0 , _IONBF , 0 );
418457 passphrase_dialog (message );
419458 gtk_main ();
420- return 0 ;
459+
460+ return exit_code ;
421461}
0 commit comments