mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-03-22 14:39:24 -05:00
Make gtkask.c compile under GTK 1.
This is less than ideal - passphrase input now happens in ISO 8859-1, and the passphrase prompt window is neither centred nor always-on-top. But it basically works, and restores bare-minimum GTK 1 support to the codebase as a whole.
This commit is contained in:
parent
0550943b51
commit
824ad9430c
@ -5,9 +5,14 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
#include <gdk/gdkkeysyms.h>
|
#include <gdk/gdkkeysyms.h>
|
||||||
|
|
||||||
|
#include "gtkcompat.h"
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
#define N_DRAWING_AREAS 3
|
#define N_DRAWING_AREAS 3
|
||||||
@ -22,7 +27,9 @@ struct askpass_ctx {
|
|||||||
GtkWidget *dialog, *promptlabel;
|
GtkWidget *dialog, *promptlabel;
|
||||||
struct drawing_area_ctx drawingareas[N_DRAWING_AREAS];
|
struct drawing_area_ctx drawingareas[N_DRAWING_AREAS];
|
||||||
int active_area;
|
int active_area;
|
||||||
|
#if GTK_CHECK_VERSION(2,0,0)
|
||||||
GtkIMContext *imc;
|
GtkIMContext *imc;
|
||||||
|
#endif
|
||||||
GdkColormap *colmap;
|
GdkColormap *colmap;
|
||||||
GdkColor cols[2];
|
GdkColor cols[2];
|
||||||
char *passphrase;
|
char *passphrase;
|
||||||
@ -58,9 +65,33 @@ static int last_char_len(struct askpass_ctx *ctx)
|
|||||||
return ctx->passlen - i;
|
return ctx->passlen - i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void add_text_to_passphrase(struct askpass_ctx *ctx, gchar *str)
|
||||||
|
{
|
||||||
|
int len = strlen(str);
|
||||||
|
if (ctx->passlen + len >= ctx->passsize) {
|
||||||
|
/* Take some care with buffer expansion, because there are
|
||||||
|
* pieces of passphrase in the old buffer so we should ensure
|
||||||
|
* realloc doesn't leave a copy lying around in the address
|
||||||
|
* space. */
|
||||||
|
int oldsize = ctx->passsize;
|
||||||
|
char *newbuf;
|
||||||
|
|
||||||
|
ctx->passsize = (ctx->passlen + len) * 5 / 4 + 1024;
|
||||||
|
newbuf = snewn(ctx->passsize, char);
|
||||||
|
memcpy(newbuf, ctx->passphrase, oldsize);
|
||||||
|
smemclr(ctx->passphrase, oldsize);
|
||||||
|
sfree(ctx->passphrase);
|
||||||
|
ctx->passphrase = newbuf;
|
||||||
|
}
|
||||||
|
strcpy(ctx->passphrase + ctx->passlen, str);
|
||||||
|
ctx->passlen += len;
|
||||||
|
visually_acknowledge_keypress(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
|
static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
|
||||||
{
|
{
|
||||||
struct askpass_ctx *ctx = (struct askpass_ctx *)data;
|
struct askpass_ctx *ctx = (struct askpass_ctx *)data;
|
||||||
|
|
||||||
if (event->keyval == GDK_Return && event->type == GDK_KEY_PRESS) {
|
if (event->keyval == GDK_Return && event->type == GDK_KEY_PRESS) {
|
||||||
gtk_main_quit();
|
gtk_main_quit();
|
||||||
} else if (event->keyval == GDK_Escape && event->type == GDK_KEY_PRESS) {
|
} else if (event->keyval == GDK_Escape && event->type == GDK_KEY_PRESS) {
|
||||||
@ -68,8 +99,10 @@ static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
|
|||||||
ctx->passphrase = NULL;
|
ctx->passphrase = NULL;
|
||||||
gtk_main_quit();
|
gtk_main_quit();
|
||||||
} else {
|
} else {
|
||||||
|
#if GTK_CHECK_VERSION(2,0,0)
|
||||||
if (gtk_im_context_filter_keypress(ctx->imc, event))
|
if (gtk_im_context_filter_keypress(ctx->imc, event))
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (event->type == GDK_KEY_PRESS) {
|
if (event->type == GDK_KEY_PRESS) {
|
||||||
if (!strcmp(event->string, "\x15")) {
|
if (!strcmp(event->string, "\x15")) {
|
||||||
@ -97,36 +130,24 @@ static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
|
|||||||
if (ctx->passlen > 0)
|
if (ctx->passlen > 0)
|
||||||
ctx->passlen -= last_char_len(ctx);
|
ctx->passlen -= last_char_len(ctx);
|
||||||
visually_acknowledge_keypress(ctx);
|
visually_acknowledge_keypress(ctx);
|
||||||
|
#if !GTK_CHECK_VERSION(2,0,0)
|
||||||
|
} else if (event->string[0]) {
|
||||||
|
add_text_to_passphrase(ctx, event->string);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if GTK_CHECK_VERSION(2,0,0)
|
||||||
static void input_method_commit_event(GtkIMContext *imc, gchar *str,
|
static void input_method_commit_event(GtkIMContext *imc, gchar *str,
|
||||||
gpointer data)
|
gpointer data)
|
||||||
{
|
{
|
||||||
struct askpass_ctx *ctx = (struct askpass_ctx *)data;
|
struct askpass_ctx *ctx = (struct askpass_ctx *)data;
|
||||||
int len = strlen(str);
|
add_text_to_passphrase(ctx, str);
|
||||||
if (ctx->passlen + len >= ctx->passsize) {
|
|
||||||
/* Take some care with buffer expansion, because there are
|
|
||||||
* pieces of passphrase in the old buffer so we should ensure
|
|
||||||
* realloc doesn't leave a copy lying around in the address
|
|
||||||
* space. */
|
|
||||||
int oldsize = ctx->passsize;
|
|
||||||
char *newbuf;
|
|
||||||
|
|
||||||
ctx->passsize = (ctx->passlen + len) * 5 / 4 + 1024;
|
|
||||||
newbuf = snewn(ctx->passsize, char);
|
|
||||||
memcpy(newbuf, ctx->passphrase, oldsize);
|
|
||||||
smemclr(ctx->passphrase, oldsize);
|
|
||||||
sfree(ctx->passphrase);
|
|
||||||
ctx->passphrase = newbuf;
|
|
||||||
}
|
|
||||||
strcpy(ctx->passphrase + ctx->passlen, str);
|
|
||||||
ctx->passlen += len;
|
|
||||||
visually_acknowledge_keypress(ctx);
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static gint configure_area(GtkWidget *widget, GdkEventConfigure *event,
|
static gint configure_area(GtkWidget *widget, GdkEventConfigure *event,
|
||||||
gpointer data)
|
gpointer data)
|
||||||
@ -205,7 +226,9 @@ static const char *gtk_askpass_setup(struct askpass_ctx *ctx,
|
|||||||
gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area
|
gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area
|
||||||
(GTK_DIALOG(ctx->dialog))),
|
(GTK_DIALOG(ctx->dialog))),
|
||||||
ctx->promptlabel);
|
ctx->promptlabel);
|
||||||
|
#if GTK_CHECK_VERSION(2,0,0)
|
||||||
ctx->imc = gtk_im_multicontext_new();
|
ctx->imc = gtk_im_multicontext_new();
|
||||||
|
#endif
|
||||||
ctx->colmap = gdk_colormap_get_system();
|
ctx->colmap = gdk_colormap_get_system();
|
||||||
ctx->cols[0].red = ctx->cols[0].green = ctx->cols[0].blue = 0xFFFF;
|
ctx->cols[0].red = ctx->cols[0].green = ctx->cols[0].blue = 0xFFFF;
|
||||||
ctx->cols[1].red = ctx->cols[1].green = ctx->cols[1].blue = 0;
|
ctx->cols[1].red = ctx->cols[1].green = ctx->cols[1].blue = 0;
|
||||||
@ -246,7 +269,10 @@ static const char *gtk_askpass_setup(struct askpass_ctx *ctx,
|
|||||||
* ensure key events go to it.
|
* ensure key events go to it.
|
||||||
*/
|
*/
|
||||||
gtk_widget_set_sensitive(ctx->promptlabel, TRUE);
|
gtk_widget_set_sensitive(ctx->promptlabel, TRUE);
|
||||||
|
|
||||||
|
#if GTK_CHECK_VERSION(2,0,0)
|
||||||
gtk_window_set_keep_above(GTK_WINDOW(ctx->dialog), TRUE);
|
gtk_window_set_keep_above(GTK_WINDOW(ctx->dialog), TRUE);
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Actually show the window, and wait for it to be shown.
|
* Actually show the window, and wait for it to be shown.
|
||||||
@ -264,13 +290,17 @@ static const char *gtk_askpass_setup(struct askpass_ctx *ctx,
|
|||||||
* And now that we've got the keyboard grab, connect up our
|
* And now that we've got the keyboard grab, connect up our
|
||||||
* keyboard handlers, and display the prompt.
|
* keyboard handlers, and display the prompt.
|
||||||
*/
|
*/
|
||||||
|
#if GTK_CHECK_VERSION(2,0,0)
|
||||||
g_signal_connect(G_OBJECT(ctx->imc), "commit",
|
g_signal_connect(G_OBJECT(ctx->imc), "commit",
|
||||||
G_CALLBACK(input_method_commit_event), ctx);
|
G_CALLBACK(input_method_commit_event), ctx);
|
||||||
|
#endif
|
||||||
gtk_signal_connect(GTK_OBJECT(ctx->promptlabel), "key_press_event",
|
gtk_signal_connect(GTK_OBJECT(ctx->promptlabel), "key_press_event",
|
||||||
GTK_SIGNAL_FUNC(key_event), ctx);
|
GTK_SIGNAL_FUNC(key_event), ctx);
|
||||||
gtk_signal_connect(GTK_OBJECT(ctx->promptlabel), "key_release_event",
|
gtk_signal_connect(GTK_OBJECT(ctx->promptlabel), "key_release_event",
|
||||||
GTK_SIGNAL_FUNC(key_event), ctx);
|
GTK_SIGNAL_FUNC(key_event), ctx);
|
||||||
|
#if GTK_CHECK_VERSION(2,0,0)
|
||||||
gtk_im_context_set_client_window(ctx->imc, ctx->dialog->window);
|
gtk_im_context_set_client_window(ctx->imc, ctx->dialog->window);
|
||||||
|
#endif
|
||||||
gtk_widget_show(ctx->promptlabel);
|
gtk_widget_show(ctx->promptlabel);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
22
unix/gtkcompat.h
Normal file
22
unix/gtkcompat.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Header file to make compatibility with older GTK versions less
|
||||||
|
* painful, by #defining various things that are pure spelling changes
|
||||||
|
* between GTK1 and GTK2.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if !GTK_CHECK_VERSION(2,0,0)
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <X11/X.h>
|
||||||
|
|
||||||
|
#define G_CALLBACK(x) GTK_SIGNAL_FUNC(x)
|
||||||
|
#define G_OBJECT(x) GTK_OBJECT(x)
|
||||||
|
#define g_ascii_isspace(x) (isspace((unsigned char)(x)))
|
||||||
|
#define g_signal_connect gtk_signal_connect
|
||||||
|
#define GDK_GRAB_SUCCESS GrabSuccess
|
||||||
|
#define gtk_dialog_get_content_area(dlg) ((dlg)->vbox)
|
||||||
|
#define gtk_dialog_get_action_area(dlg) ((dlg)->action_area)
|
||||||
|
#define gtk_widget_set_size_request gtk_widget_set_usize
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user