/*
 * $Source: /afs/athena.mit.edu/user/j/i/jik/sipbsrc/src/xscreensaver/RCS/logoutButton.c,v $
 * $Author: jik $
 *
 * This file is part of xscreensaver.  It contains the code for logout
 * button that gets popped after the user has been locked for a
 * (configurable) amount of time.
 * 
 * Author: Jonathan Kamens, MIT Project Athena and
 *                          MIT Student Information Processing Board
 *
 * Coyright (c) 1989 by Jonathan Kamens.  This code may be distributed
 * freely as long as this notice is kept intact in its entirety and
 * every effort is made to send all corrections and improvements to
 * the code back to the author.  Also, don't try to make any money off
 * of it or pretend that you wrote it.
 */

#include <X11/Intrinsic.h>
#include <X11/Xaw/Command.h>
#include <stdio.h>
#include "xsaver.h"
#include "globals.h"
#include "float.h"

extern char *getenv();
extern void XtMoveWidget();

static Widget logoutButton = 0;
static Position x, y;
static Dimension width, height;
static Dimension borderWidth;
static int xdirection = 1, ydirection = 0;
static Boolean activated = 0;

/*ARGSUSED*/
static void logoutCallback(Widget w, XtPointer cloient_data,
			   XtPointer call_data)
{
#ifdef LOGHOST
     char udp_msg[80];

     sprintf(udp_msg, "Button pressed after %d seconds.\n",
	     xtimes.current - xtimes.start);
     log_string(udp_msg);
#endif

     do_timeout();
     XtVaSetValues(logoutButton,
		   XtNlabel, "If the screen is still locked in about\na minute, reboot the workstation.",
		   0);
     XtVaGetValues(logoutButton,
		   XtNx, &x,
		   XtNy, &y,
		   XtNwidth, &width,
		   XtNheight, &height,
		   0);
     width += 2 * borderWidth;
     height += 2 * borderWidth;
     if (x > display_width - width) {
	  x = display_width - width;
     }
     if (y > display_height - height) {
	  y = display_height - height;
     }
     XtMoveWidget(logoutButton, x, y);
     return;
}

/* ARGSUSED */
void activateLogoutButton(XtPointer client_data, XtIntervalId *id)
{
#ifdef LOGHOST
     char udp_msg[250];
#endif
     
     *((XtIntervalId *)client_data) = 0;

     if (! logoutButton) {
	  char buttonMessage[250];
     
	  char *username = getenv("USER");
     
	  sprintf(buttonMessage,
		  "If you need this workstation,\nclick here to log %s out.",
		  username ? username : "this user");

	  x = y = 0;

	  logoutButton = XtVaCreateWidget("logoutButton",
					  commandWidgetClass,
					  root_widget,
					  XtNlabel, buttonMessage,
					  XtNx, 0,
					  XtNy, 0,
					  0);
	  if (! logoutButton) {
	       fprintf(stderr, "%s: Error creating logout button.\n", whoami);
	       return;
	  }

	  XtAddCallback(logoutButton, XtNcallback, logoutCallback, 0);
	  XtRealizeWidget(logoutButton);
	  XtVaGetValues(logoutButton,
			XtNwidth, &width,
			XtNheight, &height,
			XtNborderWidth, &borderWidth,
			0);
     }

     width += 2 * borderWidth;
     height += 2 * borderWidth;

     XtMapWidget(logoutButton);
     activated = 1;

#ifdef LOGHOST
     sprintf(udp_msg, "Button activated after %d seconds.\n",
	     xtimes.current - xtimes.start);
     log_string(udp_msg);
#endif
     
     return;
}

void deactivateLogoutButton()
{
     if (activated) {
	  XtUnmapWidget(logoutButton);
	  activated = 0;
     }
}

void moveLogoutButton()
{
     if (! activated)
	  return;

     x += xdirection;
     y += ydirection;

     if (x < 0) {
	  /* moving left on bottom of screen */
	  xdirection = 0;
	  ydirection = -1;
	  x++;
	  y--;
     }
     else if (x > display_width - width) {
	  /* moving right on top of screen */
	  xdirection = 0;
	  ydirection = 1;
	  x--;
	  y++;
     }
     else if (y < 0) {
	  /* moving up on left of screen */
	  xdirection = 1;
	  ydirection = 0;
	  x++;
	  y++;
     }
     else if (y > display_height - height) {
	  /* moving down on right of screen */
	  xdirection = -1;
	  ydirection = 0;
	  x--;
	  y--;
     }

     XtMoveWidget(logoutButton, x, y);
     return;
}
