gdmlogin.c 100 KB
Newer Older
1 2 3
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * GDM - The GNOME Display Manager
4
 * Copyright (C) 1998, 1999, 2000 Martin K. Petersen <mkp@mkp.net>
Martin Peterson's avatar
Martin Peterson committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

21
#include "config.h"
22

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
23
#include <math.h>
Martin Peterson's avatar
Martin Peterson committed
24 25 26
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
27
#include <fcntl.h>
Martin Peterson's avatar
Martin Peterson committed
28 29 30 31
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <dirent.h>
32
#include <locale.h>
33 34 35 36 37
#include <pwd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>

38 39
#include <X11/X.h>
#include <X11/Xlib.h>
40
#include <X11/XKBlib.h>
41 42 43 44

#include <glib/gi18n.h>
#include <gdk/gdkx.h>
#include <gdk/gdkkeysyms.h>
45 46

#if HAVE_PAM
47
#include <security/pam_appl.h>
48 49 50 51
#define PW_ENTRY_SIZE PAM_MAX_RESP_SIZE
#else
#define PW_ENTRY_SIZE GDM_MAX_PASS
#endif
52

Martin Peterson's avatar
Martin Peterson committed
53
#include "gdm.h"
54
#include "gdmuser.h"
55
#include "gdmcomm.h"
56
#include "gdmcommon.h"
57
#include "gdmsession.h"
58
#include "gdmlanguages.h"
59
#include "gdmwm.h"
60
#include "gdmconfig.h"
61
#include "misc.h"
Martin Peterson's avatar
Martin Peterson committed
62

63
#include "gdm-common.h"
64
#include "gdm-socket-protocol.h"
65
#include "gdm-daemon-config-keys.h"
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/*
 * Set the DOING_GDM_DEVELOPMENT env variable if you aren't running
 * within the protocol
 */
gboolean DOING_GDM_DEVELOPMENT              = FALSE;
gboolean GdmConfiguratorFound               = FALSE;
gboolean *GdmCustomCmdsFound                = NULL;
gboolean GdmSuspendFound                    = FALSE;
gboolean GdmHaltFound                       = FALSE;
gboolean GdmRebootFound                     = FALSE;
gboolean GdmAnyCustomCmdsFound              = FALSE;
static gboolean browser_ok                  = TRUE;
static gboolean disable_system_menu_buttons = FALSE;
static gboolean GdmLockPosition             = FALSE;
static gboolean GdmSetPosition              = FALSE;
82 83
static gint GdmPositionX;
static gint GdmPositionY;
84

85
#define GTK_KEY "gtk-2.0"
George Lebl's avatar
George Lebl committed
86

87 88 89 90 91 92
enum {
	GREETER_ULIST_ICON_COLUMN = 0,
	GREETER_ULIST_LABEL_COLUMN,
	GREETER_ULIST_LOGIN_COLUMN
};

93 94
enum {
	GDM_BACKGROUND_NONE = 0,
95 96 97
	GDM_BACKGROUND_IMAGE_AND_COLOR = 1,
	GDM_BACKGROUND_COLOR = 2,
	GDM_BACKGROUND_IMAGE = 3,
98
};
99

100
static GtkWidget *login;
101 102 103
static GtkWidget *logo_frame = NULL;
static GtkWidget *logo_image = NULL;
static GtkWidget *table = NULL;
104
static GtkWidget *welcome;
105
static GtkWidget *label;
106 107
static GtkWidget *icon_button = NULL;
static GtkWidget *title_box = NULL;
108
static GtkWidget *clock_label = NULL;
109
static GtkWidget *entry;
110
static GtkWidget *ok_button;
111
static GtkWidget *start_again_button;
112
static GtkWidget *msg;
113
static GtkWidget *auto_timed_msg;
114 115
static GtkWidget *err_box;
static guint err_box_clear_handler = 0;
116
static gboolean require_quarter = FALSE;
117
static GtkWidget *icon_win = NULL;
118 119 120
static GtkWidget *sessmenu;
static GtkWidget *langmenu;

121 122
static gboolean login_is_local = FALSE;

123 124
static GtkWidget *browser;
static GtkTreeModel *browser_model;
125
static GdkPixbuf *defface;
Martin Peterson's avatar
Martin Peterson committed
126 127

/* Eew. Loads of global vars. It's hard to be event controlled while maintaining state */
128
static GList *users = NULL;
129
static GList *users_string = NULL;
130
static gint size_of_users = 0;
131 132 133

static gchar *curuser = NULL;
static gchar *session = NULL;
134

135
static gint dont_savelang = GTK_RESPONSE_YES;
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/* back_prog_timeout_event_id: event of the timer.
 * back_prog_watcher_event_id: event of the background program watcher.
 * back_prog_pid: 	       process ID of the background program.
 * back_prog_has_run:	       true if the background program has run
 *  			       at least once.
 * back_prog_watching_events:  true if we are watching for user events.
 * back_prog_delayed: 	       true if the execution of the program has
 *                             been delayed.
 */
static gint back_prog_timeout_event_id = -1;
static gint back_prog_watcher_event_id = -1;
static gint back_prog_pid = -1;
static gboolean back_prog_has_run = FALSE;
static gboolean back_prog_watching_events = FALSE;
static gboolean back_prog_delayed = FALSE;
152

153
static guint timed_handler_id = 0;
154

155
#if FIXME
156
static char *selected_browser_user = NULL;
157
#endif
158 159
static gboolean  selecting_user = TRUE;
static gchar    *selected_user = NULL;
160

161
extern GList *sessions;
162 163 164 165
extern GHashTable *sessnames;
extern gchar *default_session;
extern const gchar *current_session;
extern gboolean session_dir_whacked_out;
166
extern gint gdm_timed_delay;
167

168 169
static gboolean first_prompt = TRUE;

170
static void login_window_resize (gboolean force);
171

172 173 174 175 176 177 178 179 180 181 182 183 184
/* Background program logic */
static void back_prog_on_exit (GPid pid, gint status, gpointer data);
static gboolean back_prog_on_timeout (gpointer data);
static gboolean back_prog_delay_timeout (GSignalInvocationHint *ihint, 
					 guint n_param_values, 
					 const GValue *param_values, 
					 gpointer data);
static void back_prog_watch_events (void);
static gchar * back_prog_get_path (void);
static void back_prog_launch_after_timeout (void);
static void back_prog_run (void);
static void back_prog_stop (void);

185 186
static void process_operation (guchar op_code, const gchar *args);

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
/* 
 * This function is called when the background program exits.
 * It will add a timer to restart the program after the
 * restart delay has elapsed, if this is enabled.
 */
static void 
back_prog_on_exit (GPid pid, gint status, gpointer data)
{
	g_assert (back_prog_timeout_event_id == -1);
	
	back_prog_watcher_event_id = -1;
	back_prog_pid = -1;
	
	back_prog_launch_after_timeout ();
}

/* 
 * This function starts the background program (if any) when
 * the background program timer triggers, unless the execution
 * has been delayed.
 */
static gboolean 
back_prog_on_timeout (gpointer data)
{
	g_assert (back_prog_watcher_event_id == -1);
	g_assert (back_prog_pid == -1);
	
	back_prog_timeout_event_id = -1;
	
	if (back_prog_delayed) {
	 	back_prog_launch_after_timeout ();
	} else {
		back_prog_run ();
	}
	
	return FALSE;
}

/*
 * This function is called to delay the execution of the background
 * program when the user is doing something (when we detect an event).
 */
static gboolean
back_prog_delay_timeout (GSignalInvocationHint *ihint,
	       		 guint n_param_values,
	       		 const GValue *param_values,
	       		 gpointer data)
{
	back_prog_delayed = TRUE;
	return TRUE;
}

/*
 * This function creates signal listeners to catch user events.
 * That allows us to avoid spawning the background program
 * when the user is doing something.
 */
static void
back_prog_watch_events (void)
	{
	guint sid;
	
	if (back_prog_watching_events)
		return;
	
	back_prog_watching_events = TRUE;
	
	sid = g_signal_lookup ("activate", GTK_TYPE_MENU_ITEM);
	g_signal_add_emission_hook (sid, 0, back_prog_delay_timeout, 
				    NULL, NULL);

258
	sid = g_signal_lookup ("key_release_event", GTK_TYPE_WIDGET);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	g_signal_add_emission_hook (sid, 0, back_prog_delay_timeout, 
				    NULL, NULL);

	sid = g_signal_lookup ("button_press_event", GTK_TYPE_WIDGET);
	g_signal_add_emission_hook (sid, 0, back_prog_delay_timeout, 
				    NULL, NULL);
	}

/*
 * This function returns the path of the background program
 * if there is one. Otherwise, NULL is returned.
 */
static gchar *
back_prog_get_path (void)
{
274 275 276 277 278 279
	gchar *backprog = gdm_config_get_string (GDM_KEY_BACKGROUND_PROGRAM);

	if ((gdm_config_get_int (GDM_KEY_BACKGROUND_TYPE) == GDM_BACKGROUND_NONE ||
	     gdm_config_get_bool (GDM_KEY_RUN_BACKGROUND_PROGRAM_ALWAYS)) &&
	    ! ve_string_empty (backprog)) {
		return backprog;
280 281 282 283 284 285 286 287 288 289 290
	} else 
		return NULL;
}

/* 
 * This function creates a timer to start the background 
 * program after the requested delay (in seconds) has elapsed.
 */ 
static void 
back_prog_launch_after_timeout ()
{
291 292
	int timeout;
	
293 294 295 296 297 298 299 300 301 302
	g_assert (back_prog_timeout_event_id == -1);
	g_assert (back_prog_watcher_event_id == -1);
	g_assert (back_prog_pid == -1);
	
	/* No program to run. */
	if (! back_prog_get_path ())
		return;
	
	/* First time. */
	if (! back_prog_has_run) {
303
		timeout = gdm_config_get_int (GDM_KEY_BACKGROUND_PROGRAM_INITIAL_DELAY);
304 305
		
	/* Already run, but we are allowed to restart it. */
306 307
	} else if (gdm_config_get_bool (GDM_KEY_RESTART_BACKGROUND_PROGRAM)) {
		timeout = gdm_config_get_int (GDM_KEY_BACKGROUND_PROGRAM_RESTART_DELAY);
308 309 310 311 312 313 314 315 316 317 318 319 320
	
	/* Already run, but we are not allowed to restart it. */
	} else {
		return;
	}
	
	back_prog_delayed = FALSE;
	back_prog_watch_events ();
	back_prog_timeout_event_id = g_timeout_add (timeout * 1000,
						    back_prog_on_timeout,
						    NULL);
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
static GtkWidget *
hig_dialog_new (GtkWindow      *parent,
		GtkDialogFlags flags,
		GtkMessageType type,
		GtkButtonsType buttons,
		const gchar    *primary_message,
		const gchar    *secondary_message)
{
	GtkWidget *dialog;

	dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
		                         GTK_DIALOG_DESTROY_WITH_PARENT,
		                         type,
		                         buttons,
		                         "%s", primary_message);

	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
		                                  "%s", secondary_message);

	gtk_window_set_title (GTK_WINDOW (dialog), "");
	gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
	gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);

  	return dialog;
}

/*
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
 * This function starts the background program (if any).
 */
static void
back_prog_run (void)
{
	GPid pid = -1;
	GError *error = NULL;
	gchar *command = NULL;
	gchar **back_prog_argv = NULL;
	
	g_assert (back_prog_timeout_event_id == -1);
	g_assert (back_prog_watcher_event_id == -1);
	g_assert (back_prog_pid == -1);
	
	command = back_prog_get_path ();
	if (! command)
		return;
365 366

        gdm_common_debug ("Running background program <%s>", command);
367 368 369
	
	/* Focus new windows. We want to give focus to the background program. */
	gdm_wm_focus_new_windows (TRUE);
370 371 372 373

	back_prog_argv = NULL;
	g_shell_parse_argv (command, NULL, &back_prog_argv, NULL);

374 375 376 377 378 379 380 381 382 383 384
	/* Don't reap child automatically: we want to catch the event. */
	if (! g_spawn_async (".", 
			     back_prog_argv, 
			     NULL, 
			     (GSpawnFlags) (G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD), 
			     NULL, 
			     NULL, 
			     &pid, 
			     &error)) {
			    
		GtkWidget *dialog;
385 386
		gchar *msg;
		
387
                gdm_common_debug ("Cannot run background program %s : %s", command, error->message);
388 389 390 391
		msg = g_strdup_printf (_("Cannot run command '%s': %s."),
		                       command,
		                       error->message);
					    
392 393 394 395 396 397
		dialog = hig_dialog_new (NULL,
					 GTK_DIALOG_MODAL,
					 GTK_MESSAGE_ERROR,
					 GTK_BUTTONS_OK,
					 _("Cannot start background application"),
					 msg);
398 399
		g_free (msg);
		
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
		gtk_widget_show_all (dialog);
		gdm_wm_center_window (GTK_WINDOW (dialog));

		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
		g_error_free (error);
		g_strfreev (back_prog_argv);
		
		return;
	}
	
	g_strfreev (back_prog_argv);
	back_prog_watcher_event_id = g_child_watch_add (pid, 
							back_prog_on_exit,
							NULL);
	back_prog_pid = pid;
	back_prog_has_run = TRUE;
}

/*
 * This function stops the background program if it is running,
 * and removes any associated timer or watcher.
 */
static void 
back_prog_stop (void)
{
	if (back_prog_timeout_event_id != -1) {
		GSource *source = g_main_context_find_source_by_id
					(NULL, back_prog_timeout_event_id);
		if (source != NULL)
			g_source_destroy (source);
			
		back_prog_timeout_event_id = -1;
	}
	
	if (back_prog_watcher_event_id != -1) {
		GSource *source = g_main_context_find_source_by_id
					(NULL, back_prog_watcher_event_id);
		if (source != NULL)
			g_source_destroy (source);
			
		back_prog_watcher_event_id = -1;
	}
	
	if (back_prog_pid != -1) {		
		if (kill (back_prog_pid, SIGTERM) == 0) {
			waitpid (back_prog_pid, NULL, 0);
		}

		back_prog_pid = -1;
	}
}

453 454 455 456 457 458
/*
 * Timed Login: Timer
 */
static gboolean
gdm_timer (gpointer data)
{
459
	if (gdm_timed_delay <= 0) {
460
		/* timed interruption */
461 462
		printf ("%c%c%c\n", STX, BEL, GDM_INTERRUPT_TIMED_LOGIN);
		fflush (stdout);
463 464 465
	} else {
		gchar *autologin_msg;

466 467
		/*
		 * Note that this message is not handled the same way as in
468 469 470 471
		 * the greeter, we don't parse it through the enriched text.
		 */
		autologin_msg = gdm_common_expand_text (
			_("User %u will login in %t"));
472 473
		gtk_label_set_text (GTK_LABEL (auto_timed_msg), autologin_msg);
		gtk_widget_show (GTK_WIDGET (auto_timed_msg));
474
		g_free (autologin_msg);
475
		login_window_resize (FALSE /* force */);
476
	}
477 478

	gdm_timed_delay--;
479 480 481 482
	return TRUE;
}

/*
483 484
 * Timed Login: On GTK events, increase delay to at least 30
 * seconds, or the GDM_KEY_TIMED_LOGIN_DELAY, whichever is higher
485 486
 */
static gboolean
487 488 489 490
gdm_timer_up_delay (GSignalInvocationHint *ihint,
		    guint	           n_param_values,
		    const GValue	  *param_values,
		    gpointer		   data)
491
{
492 493 494 495
	if (gdm_timed_delay < 30)
		gdm_timed_delay = 30;
	if (gdm_timed_delay < gdm_config_get_int (GDM_KEY_TIMED_LOGIN_DELAY))
		gdm_timed_delay = gdm_config_get_int (GDM_KEY_TIMED_LOGIN_DELAY);
496
	return TRUE;
497 498 499 500 501 502 503 504 505 506 507 508 509
}

/* The reaping stuff */
static time_t last_reap_delay = 0;

static gboolean
delay_reaping (GSignalInvocationHint *ihint,
	       guint	           n_param_values,
	       const GValue	  *param_values,
	       gpointer		   data)
{
	last_reap_delay = time (NULL);
	return TRUE;
510 511
}      

512 513 514 515 516 517
static void
gdm_kill_thingies (void)
{
	back_prog_stop ();
}

518 519 520
static gboolean
reap_flexiserver (gpointer data)
{
521 522 523 524
	int reapminutes = gdm_config_get_int (GDM_KEY_FLEXI_REAP_DELAY_MINUTES);

	if (reapminutes > 0 &&
	    ((time (NULL) - last_reap_delay) / 60) > reapminutes) {
525 526 527 528 529 530 531
		gdm_kill_thingies ();
		_exit (DISPLAY_REMANAGE);
	}
	return TRUE;
}


532
static gboolean
533 534 535 536
gdm_event (GSignalInvocationHint *ihint,
	   guint		n_param_values,
	   const GValue	       *param_values,
	   gpointer		data)
537
{
538 539
	GdkEvent *event;

540 541 542 543
	/* HAAAAAAAAAAAAAAAAACK */
	/* Since the user has not logged in yet and may have left/right
	 * mouse buttons switched, we just translate every right mouse click
	 * to a left mouse click */
544 545 546 547 548
	if (n_param_values != 2 ||
	    !G_VALUE_HOLDS (&param_values[1], GDK_TYPE_EVENT))
	  return FALSE;
	
	event = g_value_get_boxed (&param_values[1]);
549 550 551 552 553 554 555
	if ((event->type == GDK_BUTTON_PRESS ||
	     event->type == GDK_2BUTTON_PRESS ||
	     event->type == GDK_3BUTTON_PRESS ||
	     event->type == GDK_BUTTON_RELEASE)
	    && event->button.button == 3)
		event->button.button = 1;

556 557 558 559 560 561 562 563 564
	/* Support Ctrl-U for blanking the username/password entry */
	if (event->type == GDK_KEY_PRESS &&
	    (event->key.state & GDK_CONTROL_MASK) &&
	    (event->key.keyval == GDK_u ||
	     event->key.keyval == GDK_U)) {

		gtk_entry_set_text (GTK_ENTRY (entry), "");
	}

565 566
	return TRUE;
}      
567

Martin Peterson's avatar
Martin Peterson committed
568
static void
569
gdm_login_done (int sig)
Martin Peterson's avatar
Martin Peterson committed
570
{
571 572
	gdm_kill_thingies ();
	_exit (EXIT_SUCCESS);
Martin Peterson's avatar
Martin Peterson committed
573 574
}

575 576 577
static void
set_screen_pos (GtkWidget *widget, int x, int y)
{
578 579
	int width, height;

580 581 582
	g_return_if_fail (widget != NULL);
	g_return_if_fail (GTK_IS_WIDGET (widget));

583 584
	gtk_window_get_size (GTK_WINDOW (widget), &width, &height);

Pablo Saratxaga's avatar
Pablo Saratxaga committed
585 586
	/* allow negative values, to be like standard X geometry ones */
	if (x < 0)
587
		x = gdm_wm_screen.width + x - width;
Pablo Saratxaga's avatar
Pablo Saratxaga committed
588
	if (y < 0)
589
		y = gdm_wm_screen.height + y - height;
Pablo Saratxaga's avatar
Pablo Saratxaga committed
590

591 592 593 594
	if (x < gdm_wm_screen.x)
		x = gdm_wm_screen.x;
	if (y < gdm_wm_screen.y)
		y = gdm_wm_screen.y;
595 596 597 598
	if (x > gdm_wm_screen.x + gdm_wm_screen.width - width)
		x = gdm_wm_screen.x + gdm_wm_screen.width - width;
	if (y > gdm_wm_screen.y + gdm_wm_screen.height - height)
		y = gdm_wm_screen.y + gdm_wm_screen.height - height;
599

600
	gtk_window_move (GTK_WINDOW (widget), x, y);
601
}
Martin Peterson's avatar
Martin Peterson committed
602

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
static guint set_pos_id = 0;

static gboolean
set_pos_idle (gpointer data)
{
	if (GdmSetPosition) {
		set_screen_pos (login, GdmPositionX, GdmPositionY);
	} else {
		gdm_wm_center_window (GTK_WINDOW (login));
	}
	set_pos_id = 0;
	return FALSE;
}

static void
login_window_resize (gboolean force)
{
	/* allow opt out if we don't really need
	 * a resize */
	if ( ! force) {
		GtkRequisition req;
		int width, height;

		gtk_window_get_size (GTK_WINDOW (login), &width, &height);
		gtk_widget_size_request (login, &req);

		if (req.width <= width && req.height <= height)
			return;
	}

	GTK_WINDOW (login)->need_default_size = TRUE;
	gtk_container_check_resize (GTK_CONTAINER (login));

	if (set_pos_id == 0)
		set_pos_id = g_idle_add (set_pos_idle, NULL);
}


641 642 643 644
typedef struct _CursorOffset {
	int x;
	int y;
} CursorOffset;
Martin Peterson's avatar
Martin Peterson committed
645

646 647 648 649 650 651 652 653 654 655 656 657 658 659
static gboolean
within_rect (GdkRectangle *rect, int x, int y)
{
	return
		x >= rect->x &&
		x <= rect->x + rect->width &&
		y >= rect->y &&
		y <= rect->y + rect->height;
}

/* switch to the xinerama screen where x,y are */
static void
set_screen_to_pos (int x, int y)
{
660
	if ( ! within_rect (&gdm_wm_screen, x, y)) {
661
		int i;
662
		/* If not within gdm_wm_screen boundaries,
663 664
		 * maybe we want to switch xinerama
		 * screen */
665 666 667
		for (i = 0; i < gdm_wm_screens; i++) {
			if (within_rect (&gdm_wm_allscreens[i], x, y)) {
				gdm_wm_set_screen (i);
668 669 670 671 672 673
				break;
			}
		}
	}
}

674 675 676
static void
gdm_run_gdmconfig (GtkWidget *w, gpointer data)
{
677 678 679 680 681 682 683
	gtk_widget_set_sensitive (browser, FALSE);

	/* Make sure to unselect the user */
	if (selected_user != NULL)
		g_free (selected_user);
	selected_user = NULL;

684
	/* we should be now fine for focusing new windows */
685
	gdm_wm_focus_new_windows (TRUE);
686

687
	/* configure interruption */
688 689
	printf ("%c%c%c\n", STX, BEL, GDM_INTERRUPT_CONFIGURE);
	fflush (stdout);
690
}
Martin Peterson's avatar
Martin Peterson committed
691

692
static void
693
gdm_login_restart_handler (void)
Martin Peterson's avatar
Martin Peterson committed
694
{
695 696 697 698
	if (gdm_wm_warn_dialog (
	    _("Are you sure you want to restart the computer?"), "",
	    _("_Restart"), NULL, TRUE) == GTK_RESPONSE_YES) {

699
		gdm_kill_thingies ();
700 701
		_exit (DISPLAY_REBOOT);
	}
Martin Peterson's avatar
Martin Peterson committed
702 703
}

704
static void
705
gdm_custom_cmd_handler (GtkWidget *widget, gpointer data)
706
{	
707 708
	if (data) {
		int *cmd_id = (int*)data;
709
		gchar * key_string = g_strdup_printf ("%s%d=", GDM_KEY_CUSTOM_CMD_TEXT_TEMPLATE, *cmd_id);
710 711 712 713 714 715 716 717
		if (gdm_wm_warn_dialog (
			    gdm_config_get_string (key_string) , "", GTK_STOCK_OK, NULL, TRUE) == GTK_RESPONSE_YES) {
			
			printf ("%c%c%c%d\n", STX, BEL, GDM_INTERRUPT_CUSTOM_CMD, *cmd_id);
			fflush (stdout);
		}
		
		g_free (key_string);		
718 719
	}
}
Martin Peterson's avatar
Martin Peterson committed
720

721
static void
722
gdm_login_halt_handler (void)
Martin Peterson's avatar
Martin Peterson committed
723
{
724 725 726 727
	if (gdm_wm_warn_dialog (
	    _("Are you sure you want to Shut Down the computer?"), "",
	    _("Shut _Down"), NULL, TRUE) == GTK_RESPONSE_YES) {

728
		gdm_kill_thingies ();
729 730
		_exit (DISPLAY_HALT);
	}
Martin Peterson's avatar
Martin Peterson committed
731 732
}

733 734 735
static void
gdm_login_use_chooser_handler (void)
{
736
	gdm_kill_thingies ();
737 738 739
	_exit (DISPLAY_RUN_CHOOSER);
}

740 741 742
static void
gdm_login_suspend_handler (void)
{
743 744 745 746
	if (gdm_wm_warn_dialog (
	    _("Are you sure you want to suspend the computer?"), "",
	    _("_Suspend"), NULL, TRUE) == GTK_RESPONSE_YES) {

747 748 749
		/* suspend interruption */
		printf ("%c%c%c\n", STX, BEL, GDM_INTERRUPT_SUSPEND);
		fflush (stdout);
750 751
	}
}
Martin Peterson's avatar
Martin Peterson committed
752

753 754 755
static void
gdm_theme_handler (GtkWidget *widget, gpointer data)
{
756
    const char *theme_name = (const char *)data;
757 758 759 760 761

    printf ("%c%c%c%s\n", STX, BEL, GDM_INTERRUPT_THEME, theme_name);
  
    fflush (stdout);

762 763 764 765
    gdm_set_theme (theme_name);

    login_window_resize (FALSE);
    gdm_wm_center_window (GTK_WINDOW (login));
766 767
}

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
768 769 770 771 772 773 774 775 776 777 778 779
static int dance_handler = 0;

static gboolean
dance (gpointer data)
{
	static double t1 = 0.0, t2 = 0.0;
	double xm, ym;
	int x, y;
	static int width = -1;
	static int height = -1;

	if (width == -1)
780
		width = gdm_wm_screen.width;
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
781
	if (height == -1)
782
		height = gdm_wm_screen.height;
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
783 784 785 786 787 788 789

	if (login == NULL ||
	    login->window == NULL) {
		dance_handler = 0;
		return FALSE;
	}

790 791
	xm = cos (2.31 * t1);
	ym = sin (1.03 * t2);
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
792 793 794 795

	t1 += 0.03 + (rand () % 10) / 500.0;
	t2 += 0.03 + (rand () % 10) / 500.0;

796 797
	x = gdm_wm_screen.x + (width / 2) + (width / 5) * xm;
	y = gdm_wm_screen.y + (height / 2) + (height / 5) * ym;
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
798 799 800 801 802 803 804 805

	set_screen_pos (login,
			x - login->allocation.width / 2,
			y - login->allocation.height / 2);

	return TRUE;
}

806
static gboolean
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
807 808
evil (const char *user)
{
809
	static gboolean old_lock;
810

Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
811
	if (dance_handler == 0 &&
812
	    /* do not translate */
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
813
	    strcmp (user, "Start Dancing") == 0) {
814
		gdm_common_setup_cursor (GDK_UMBRELLA);
815
		dance_handler = g_timeout_add (50, dance, NULL);
816
		old_lock = GdmLockPosition;
817
		GdmLockPosition = TRUE;
818 819 820
		gtk_entry_set_text (GTK_ENTRY (entry), "");
		return TRUE;
	} else if (dance_handler != 0 &&
821
		   /* do not translate */
822
		   strcmp (user, "Stop Dancing") == 0) {
823
		gdm_common_setup_cursor (GDK_LEFT_PTR);
824
		g_source_remove (dance_handler);
825 826
		dance_handler = 0;
		GdmLockPosition = old_lock;
827
		gdm_wm_center_window (GTK_WINDOW (login));
828 829
		gtk_entry_set_text (GTK_ENTRY (entry), "");
		return TRUE;
830
				 /* do not translate */
831
	} else if (strcmp (user, "Gimme Random Cursor") == 0) {
832
		gdm_common_setup_cursor (((rand () >> 3) % (GDK_LAST_CURSOR/2)) * 2);
833
		gtk_entry_set_text (GTK_ENTRY (entry), "");
834 835
		return TRUE;
				 /* do not translate */
836 837 838 839 840 841
	} else if (strcmp (user, "Require Quater") == 0 ||
		   strcmp (user, "Require Quarter") == 0) {
		/* btw, note that I misspelled quarter before and
		 * thus this checks for Quater as well as Quarter to
		 * keep compatibility which is obviously important for
		 * something like this */
842
		require_quarter = TRUE;
843
		gtk_entry_set_text (GTK_ENTRY (entry), "");
844
		return TRUE;
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
845
	}
846

847
	return FALSE;
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
848
}
Martin Peterson's avatar
Martin Peterson committed
849

850 851
static void
gdm_login_enter (GtkWidget *entry)
Martin Peterson's avatar
Martin Peterson committed
852
{
853 854
	const char *login_string;
	const char *str;
855
	char *tmp;
856

857 858
	if (entry == NULL)
		return;
Martin Peterson's avatar
Martin Peterson committed
859

860
	gtk_widget_set_sensitive (entry, FALSE);
861
	gtk_widget_set_sensitive (ok_button, FALSE);
862
	gtk_widget_set_sensitive (start_again_button, FALSE);
863

864
	login_string = gtk_entry_get_text (GTK_ENTRY (entry));
865

866 867 868 869 870 871
	str = gtk_label_get_text (GTK_LABEL (label));
	if (str != NULL &&
	    (strcmp (str, _("Username:")) == 0 ||
	     strcmp (str, _("_Username:")) == 0) &&
	    /* If in timed login mode, and if this is the login
	     * entry.  Then an enter by itself is sort of like I want to
872
	     * log in as the timed user, really.  */
873 874
	    ve_string_empty (login_string) &&
	    timed_handler_id != 0) {
875
		/* timed interruption */
876 877
		printf ("%c%c%c\n", STX, BEL, GDM_INTERRUPT_TIMED_LOGIN);
		fflush (stdout);
878
		return;
879 880
	}

881
	if (str != NULL &&
882 883
	    (strcmp (str, _("Username:")) == 0 ||
	     strcmp (str, _("_Username:")) == 0) &&
884 885 886 887 888 889 890 891
	    /* evilness */
	    evil (login_string)) {
		/* obviously being 100% reliable is not an issue for
		   this test */
		gtk_widget_set_sensitive (entry, TRUE);
		gtk_widget_grab_focus (entry);	
		gtk_window_set_focus (GTK_WINDOW (login), entry);	
		return;
Jiri (George) Lebl's avatar
Jiri (George) Lebl committed
892 893
	}

894 895
	/* clear the err_box */
	if (err_box_clear_handler > 0)
896
		g_source_remove (err_box_clear_handler);
897
	err_box_clear_handler = 0;
898
	gtk_label_set_text (GTK_LABEL (err_box), "");
899

900
	tmp = ve_locale_from_utf8 (gtk_entry_get_text (GTK_ENTRY (entry)));
901
	printf ("%c%s\n", STX, tmp);
902
	fflush (stdout);
903
	g_free (tmp);
904 905 906 907 908 909 910 911
}

static void
gdm_login_ok_button_press (GtkButton *button, GtkWidget *entry)
{
	gdm_login_enter (entry);
}

912
static void
913
gdm_login_start_again_button_press (GtkButton *button, GtkWidget *entry)
914 915 916 917 918 919 920 921
{
	GtkTreeSelection *selection;

	if (browser != NULL) {
		selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (browser));
		gtk_tree_selection_unselect_all (selection);
	}

922 923 924 925
	if (selected_user != NULL)
		g_free (selected_user);
	selected_user = NULL;

926 927 928 929 930
	printf ("%c%c%c\n", STX, BEL,
		GDM_INTERRUPT_CANCEL);
	fflush (stdout);
}

931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
static gboolean
gdm_login_focus_in (GtkWidget *widget, GdkEventFocus *event)
{
	if (title_box != NULL)
		gtk_widget_set_state (title_box, GTK_STATE_SELECTED);

	if (icon_button != NULL)
		gtk_widget_set_state (icon_button, GTK_STATE_NORMAL);

	return FALSE;
}

static gboolean
gdm_login_focus_out (GtkWidget *widget, GdkEventFocus *event)
{
	if (title_box != NULL)
		gtk_widget_set_state (title_box, GTK_STATE_NORMAL);

	return FALSE;
}
Martin Peterson's avatar
Martin Peterson committed
951 952 953 954 955 956

static void 
gdm_login_session_handler (GtkWidget *widget) 
{
    gchar *s;

957
    current_session = g_object_get_data (G_OBJECT (widget), SESSION_NAME);
George Lebl's avatar
George Lebl committed
958

959
    s = g_strdup_printf (_("%s session selected"), gdm_session_name (current_session));
George Lebl's avatar
George Lebl committed
960

961
    gtk_label_set_text (GTK_LABEL (msg), s);
962
    g_free (s);
963 964

    login_window_resize (FALSE /* force */);
Martin Peterson's avatar
Martin Peterson committed
965 966 967
}

static void 
968
gdm_login_session_init (GtkWidget *menu)
Martin Peterson's avatar
Martin Peterson committed
969
{
970
    GSList *sessgrp = NULL;
971
    GList *tmp;
Martin Peterson's avatar
Martin Peterson committed
972
    GtkWidget *item;
973
    int num = 1;
974
    char *label;
Martin Peterson's avatar
Martin Peterson committed
975

976
    current_session = NULL;
977
    
978
    if (gdm_config_get_bool (GDM_KEY_SHOW_LAST_SESSION)) {
979
            current_session = LAST_SESSION;
980
            item = gtk_radio_menu_item_new_with_mnemonic (NULL, _("_Last"));
981 982 983
            g_object_set_data (G_OBJECT (item),
			       SESSION_NAME,
			       LAST_SESSION);
984
            sessgrp = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
985 986 987 988
            gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
            g_signal_connect (G_OBJECT (item), "activate",
			      G_CALLBACK (gdm_login_session_handler),
			      NULL);
989
            gtk_widget_show (GTK_WIDGET (item));
990
            item = gtk_menu_item_new ();
991
            gtk_widget_set_sensitive (item, FALSE);
992
            gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
993 994
            gtk_widget_show (GTK_WIDGET (item));
    }
Martin Peterson's avatar
Martin Peterson committed
995

996
    gdm_session_list_init ();
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

    for (tmp = sessions; tmp != NULL; tmp = tmp->next) {
	    GdmSession *session;
	    char *file;

	    file = (char *) tmp->data;
	    session = g_hash_table_lookup (sessnames, file);

	    if (num < 10 && 
	       (strcmp (file, GDM_SESSION_FAILSAFE_GNOME) != 0) &&
	       (strcmp (file, GDM_SESSION_FAILSAFE_XTERM) != 0))
		    label = g_strdup_printf ("_%d. %s", num, session->name);
	    else
		    label = g_strdup (session->name);
1011
	    num++;
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

	    item = gtk_radio_menu_item_new_with_mnemonic (sessgrp, label);
	    g_free (label);
	    g_object_set_data_full (G_OBJECT (item), SESSION_NAME,
		 g_strdup (file), (GDestroyNotify) g_free);

	    sessgrp = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
	    gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
	    g_signal_connect (G_OBJECT (item), "activate",
		G_CALLBACK (gdm_login_session_handler), NULL);
	    gtk_widget_show (GTK_WIDGET (item));
1023
    }
1024

1025 1026 1027 1028 1029 1030 1031 1032 1033
    /* Select the proper session */
    {
            GSList *tmp;
            
            tmp = sessgrp;
            while (tmp != NULL) {
                    GtkWidget *w = tmp->data;
                    const char *n;

1034
                    n = g_object_get_data (G_OBJECT (w), SESSION_NAME);
1035
                    
1036
                    if (n && strcmp (n, current_session) == 0) {
1037 1038 1039 1040 1041 1042 1043 1044
                            gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
                                                            TRUE);
                            break;
                    }
                    
                    tmp = tmp->next;
            }
    }
Martin Peterson's avatar
Martin Peterson committed
1045 1046 1047 1048
}


static void 
1049
gdm_login_language_handler (GtkMenuItem *menu_item, gpointer user_data) 
Martin Peterson's avatar
Martin Peterson committed
1050
{
1051
    gdm_lang_handler (user_data);
Martin Peterson's avatar
Martin Peterson committed
1052 1053 1054
}


1055 1056
static GtkWidget *
gdm_login_language_menu_new (void)
Martin Peterson's avatar
Martin Peterson committed
1057
{
1058
    GtkWidget *menu;
1059
    GtkWidget *item;
Martin Peterson's avatar
Martin Peterson committed
1060

1061
    gdm_lang_initialize_model (gdm_config_get_string (GDM_KEY_LOCALE_FILE));
Martin Peterson's avatar
Martin Peterson committed
1062

1063
    menu = gtk_menu_new ();
Martin Peterson's avatar
Martin Peterson committed
1064

1065
    item = gtk_menu_item_new_with_mnemonic (_("Select _Language..."));
1066 1067 1068 1069 1070
    gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
    g_signal_connect (G_OBJECT (item), "activate", 
		      G_CALLBACK (gdm_login_language_handler), 
		      NULL);
    gtk_widget_show (GTK_WIDGET (item));
1071 1072

    return menu;
Martin Peterson's avatar
Martin Peterson committed
1073 1074
}

1075 1076 1077
static gboolean
theme_allowed (const char *theme)
{
1078
	gchar * themestoallow = gdm_config_get_string (GDM_KEY_GTK_THEMES_TO_ALLOW);
1079 1080 1081
	char **vec;
	int i;

1082 1083
	if (ve_string_empty (themestoallow) ||
	    g_ascii_strcasecmp (themestoallow, "all") == 0)
1084 1085
		return TRUE;

1086
	vec = g_strsplit (themestoallow, ",", 0);
1087 1088 1089 1090
	if (vec == NULL || vec[0] == NULL)
		return TRUE;

	for (i = 0; vec[i] != NULL; i++) {
1091 1092
		if (strcmp (vec[i], theme) == 0) {
			g_strfreev (vec);
1093
			return TRUE;
1094
		}
1095 1096 1097 1098 1099 1100 1101
	}

	g_strfreev (vec);
	return FALSE;
}

static GSList *
1102 1103 1104 1105 1106
build_theme_list (void)
{
    DIR *dir;
    struct dirent *de;
    gchar *theme_dir;
1107
    GSList *theme_list = NULL;
1108 1109 1110 1111 1112

    theme_dir = gtk_rc_get_theme_dir ();
    dir = opendir (theme_dir);

    while ((de = readdir (dir))) {
1113
	char *name;
1114 1115
	if (de->d_name[0] == '.')
		continue;
1116 1117 1118 1119 1120 1121
	if ( ! theme_allowed (de->d_name))
		continue;
	name = g_build_filename (theme_dir, de->d_name, GTK_KEY, NULL);
	if (g_file_test (name, G_FILE_TEST_IS_DIR))
		theme_list = g_slist_append (theme_list, g_strdup (de->d_name));
	g_free (name);
1122 1123 1124
    }
    g_free (theme_dir);
    closedir (dir);
1125

1126 1127 1128 1129 1130 1131
    return theme_list;
}

static GtkWidget *
gdm_login_theme_menu_new (void)
{
1132
    GSList *theme_list;
1133 1134
    GtkWidget *item;
    GtkWidget *menu;
1135 1136
    int num = 1;

1137
    if ( ! gdm_config_get_bool (GDM_KEY_ALLOW_GTK_THEME_CHANGE))
1138
	    return NULL;
1139 1140

    menu = gtk_menu_new ();
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    
    for (theme_list = build_theme_list ();
	 theme_list != NULL;
	 theme_list = theme_list->next) {
        char *menu_item_name;
        char *theme_name = theme_list->data;
	theme_list->data = NULL;

	if (num < 10)
		menu_item_name = g_strdup_printf ("_%d. %s", num, _(theme_name));
	else if ((num -10) + (int)'a' <= (int)'z')
		menu_item_name = g_strdup_printf ("_%c. %s",
						  (char)(num-10)+'a',
						  _(theme_name));
	else
		menu_item_name = g_strdup (theme_name);
1157
	num++;
1158

1159
	item = gtk_menu_item_new_with_mnemonic (menu_item_name);
1160 1161 1162 1163
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
	gtk_widget_show (GTK_WIDGET (item));
	g_signal_connect (G_OBJECT (item), "activate",
			  G_CALLBACK (gdm_theme_handler), theme_name);
1164
	g_free (menu_item_name);
1165
    }
1166
    g_slist_free (theme_list);
1167 1168 1169
    return menu;
}

1170 1171 1172
static gboolean
err_box_clear (gpointer data)
{
1173 1174
	if (err_box != NULL)
		gtk_label_set_text (GTK_LABEL (err_box), "");
1175 1176 1177 1178 1179

	err_box_clear_handler = 0;
	return FALSE;
}

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
static void
browser_set_user (const char *user)
{
  gboolean old_selecting_user = selecting_user;
  GtkTreeSelection *selection;
  GtkTreeIter iter = {0};
  GtkTreeModel *tm = NULL;

  if (browser == NULL)
    return;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (browser));
  gtk_tree_selection_unselect_all (selection);

  if (ve_string_empty (user))
    return;

  selecting_user = FALSE;

  tm = gtk_tree_view_get_model (GTK_TREE_VIEW (browser));

  if (gtk_tree_model_get_iter_first (tm, &iter))
    {
      do
        {
          char *login;
	  gtk_tree_model_get (tm, &iter, GREETER_ULIST_LOGIN_COLUMN,
			      &login, -1);
	  if (login != NULL && strcmp (user, login) == 0)
	    {
	      GtkTreePath *path = gtk_tree_model_get_path (tm, &iter);
	      gtk_tree_selection_select_iter (selection, &iter);
	      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (browser),
					    path, NULL,
					    FALSE, 0.0, 0.0);
	      gtk_tree_path_free (path);
	      break;
	    }
	  
        }
      while (gtk_tree_model_iter_next (tm, &iter));
    }
  selecting_user = old_selecting_user;
}

1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
static Display *
get_parent_display (void)
{
  static gboolean tested = FALSE;
  static Display *dsp = NULL;

  if (tested)
    return dsp;

  tested = TRUE;

  if (g_getenv ("GDM_PARENT_DISPLAY") != NULL)
    {
      char *old_xauth = g_strdup (g_getenv ("XAUTHORITY"));
      if (g_getenv ("GDM_PARENT_XAUTHORITY") != NULL)
        {
1241
	  g_setenv ("XAUTHORITY",
1242
		     g_getenv ("GDM_PARENT_XAUTHORITY"), TRUE);
1243 1244 1245
	}
      dsp = XOpenDisplay (g_getenv ("GDM_PARENT_DISPLAY"));
      if (old_xauth != NULL)
1246
        g_setenv ("XAUTHORITY", old_xauth, TRUE);
1247
      else
1248
        g_unsetenv ("XAUTHORITY");
1249 1250 1251 1252 1253 1254 1255 1256 1257
      g_free (old_xauth);
    }

  return dsp;
}

static gboolean
greeter_is_capslock_on (void)
{
1258
  XkbStateRec states;
1259 1260
  Display *dsp;

1261
  /* HACK! incredible hack, if GDM_PARENT_DISPLAY is set we get
1262 1263 1264 1265 1266 1267
   * indicator state from the parent display, since we must be inside an
   * Xnest */
  dsp = get_parent_display ();
  if (dsp == NULL)
    dsp = GDK_DISPLAY ();

1268
  if (XkbGetState (dsp, XkbUseCoreKbd, &states) != Success)
1269 1270
      return FALSE;

1271
  return (states.locked_mods & LockMask) != 0;
1272 1273
}

1274 1275 1276 1277 1278 1279 1280 1281 1282
static void
face_browser_select_user (gchar *login)
{
        printf ("%c%c%c%s\n", STX, BEL,
                GDM_INTERRUPT_SELECT_USER, login);

        fflush (stdout);
}

Martin Peterson's avatar
Martin Peterson committed
1283
static gboolean
1284
gdm_login_ctrl_handler (GIOChannel *source, GIOCondition cond, gint fd)
Martin Peterson's avatar
Martin Peterson committed
1285 1286
{
    gchar buf[PIPE_SIZE];
1287
    gchar *p;
1288
    gsize len;
Martin Peterson's avatar
Martin Peterson committed
1289

1290
    /* If this is not incoming i/o then return */
1291 1292
    if (cond != G_IO_IN) 
	return (TRUE);
Martin Peterson's avatar
Martin Peterson committed
1293

1294 1295
    /* Read random garbage from i/o channel until STX is found */
    do {
1296
	g_io_channel_read_chars (source, buf, 1, &len, NULL);
1297 1298 1299

	if (len != 1)
	    return (TRUE);
1300
    } while (buf[0] && buf[0] != STX);
1301

1302 1303 1304 1305
    memset (buf, '\0', sizeof (buf));
    if (g_io_channel_read_chars (source, buf, sizeof (buf) - 1, &len, NULL) !=
	G_IO_STATUS_NORMAL)
      return TRUE;
1306

1307 1308 1309
    p = memchr (buf, STX, len);
    if (p != NULL) {
      len = p - buf;
1310 1311
      g_io_channel_seek_position (source, -((sizeof (buf) - 1) - len), G_SEEK_CUR, NULL);
      memset (buf + len, '\0', (sizeof (buf) - 1) - len);
1312 1313 1314 1315
    }
    buf[len - 1] = '\0';  
 
    process_operation ((guchar) buf[0], buf + 1);
Martin Peterson's avatar
Martin Peterson committed
1316

1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
    return TRUE;
}

static void
process_operation (guchar       op_code,
		   const gchar *args)
{
    char *tmp;
    gint i, x, y;
    GtkWidget *dlg;
    static gboolean replace_msg = TRUE;
    static gboolean messages_to_give = FALSE;
1329 1330 1331
    gint lookup_status = SESSION_LOOKUP_SUCCESS;
    gchar *firstmsg = NULL;
    gchar *secondmsg = NULL;
1332
    gint dont_save_session = GTK_RESPONSE_YES;
1333
    
1334
    /* Parse opcode */
1335
    switch (op_code) {
1336 1337 1338 1339
    case GDM_SETLOGIN:
	/* somebody is trying to fool us this is the user that
	 * wants to log in, and well, we are the gullible kind */
	g_free (curuser);
1340
	curuser = g_strdup (args);
1341
	if (browser_ok && gdm_config_get_bool (GDM_KEY_BROWSER))
1342
		browser_set_user (curuser);
1343 1344
	printf ("%c\n", STX);
	fflush (stdout);
1345 1346 1347
	break;

    case GDM_PROMPT:
1348
	tmp = ve_locale_to_utf8 (args);
1349
	if (tmp != NULL && strcmp (tmp, _("Username:")) == 0) {
1350 1351 1352
		gdm_common_login_sound (gdm_config_get_string (GDM_KEY_SOUND_PROGRAM),
					gdm_config_get_string (GDM_KEY_SOUND_ON_LOGIN_FILE),
					gdm_config_get_bool   (GDM_KEY_SOUND_ON_LOGIN));
1353 1354
		gtk_label_set_text_with_mnemonic (GTK_LABEL (label), _("_Username:"));
	} else {
1355 1356
		if (tmp != NULL)
			gtk_label_set_text (GTK_LABEL (label), tmp);
1357
	}
1358 1359
	g_free (tmp);

1360 1361 1362
	gtk_widget_set_sensitive (GTK_WIDGET (start_again_button), !first_prompt);
	first_prompt = FALSE;

1363 1364
	gtk_widget_show (GTK_WIDGET (label));
	gtk_entry_set_text (GTK_ENTRY (entry), "");
1365
	gtk_entry_set_max_length (GTK_ENTRY (entry), PW_ENTRY_SIZE);
1366 1367
	gtk_entry_set_visibility (GTK_ENTRY (entry), TRUE);
	gtk_widget_set_sensitive (entry, TRUE);
1368
	gtk_widget_set_sensitive (ok_button, FALSE);
1369 1370 1371 1372
	gtk_widget_grab_focus (entry);	
	gtk_window_set_focus (GTK_WINDOW (login), entry);	
	gtk_widget_show (entry);

1373
	/* replace rather then append next message string */
1374 1375
	replace_msg = TRUE;

1376 1377 1378
	/* the user has seen messages */
	messages_to_give = FALSE;

1379
	login_window_resize (FALSE /* force */);
Martin Peterson's avatar
Martin Peterson committed
1380 1381 1382
	break;

    case GDM_NOECHO:
1383
	tmp = ve_locale_to_utf8 (args);
1384
	if (tmp != NULL && strcmp (tmp, _("Password:")) == 0) {
1385 1386
		gtk_label_set_text_with_mnemonic (GTK_LABEL (label), _("_Password:"));
	} else {
1387 1388
		if (tmp != NULL)
			gtk_label_set_text (GTK_LABEL (label), tmp);
1389
	}
1390 1391
	g_free (tmp);

1392 1393 1394
	gtk_widget_set_sensitive (GTK_WIDGET (start_again_button), !first_prompt);
	first_prompt = FALSE;

1395 1396
	gtk_widget_show (GTK_WIDGET (label));
	gtk_entry_set_text (GTK_ENTRY (entry), "");
1397
	gtk_entry_set_max_length (GTK_ENTRY (entry), PW_ENTRY_SIZE);
1398
	gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
1399
	gtk_widget_set_sensitive (entry, TRUE);
1400
	gtk_widget_set_sensitive (ok_button, FALSE);
1401
	gtk_widget_grab_focus (entry);	
1402
	gtk_window_set_focus (GTK_WINDOW (login), entry);	
1403
	gtk_widget_show (entry);
1404

1405
	/* replace rather then append next message string */
1406
	replace_msg = TRUE;
1407

1408 1409 1410
	/* the user has seen messages */
	messages_to_give = FALSE;

1411
	login_window_resize (FALSE /* force */);
Martin Peterson's avatar
Martin Peterson committed
1412 1413
	break;

1414 1415 1416 1417
    case GDM_MSG:
	/* the user has not yet seen messages */
	messages_to_give = TRUE;

1418
	/* HAAAAAAACK.  Sometimes pam sends many many messages, SO
1419 1420
	 * we try to collect them until the next prompt or reset or
	 * whatnot */
1421 1422
	if ( ! replace_msg &&
	   /* empty message is for clearing */
1423
	   ! ve_string_empty (args)) {
1424 1425
		const char *oldtext;
		oldtext = gtk_label_get_text (GTK_LABEL (msg));
1426
		if ( ! ve_string_empty (oldtext)) {
1427
			char *newtext;
1428
			tmp = ve_locale_to_utf8 (args);
1429 1430
			newtext = g_strdup_printf ("%s\n%s", oldtext, tmp);
			g_free (tmp);
1431
			gtk_label_set_text (GTK_LABEL (msg), newtext);