/*
 * Puppeteer 1.1
 *
 * Copyright (c) 1994 Primitive Software Ltd.  All rights reserved.
 *
 * Author: Dave Griffiths <dave@prim.demon.co.uk>
 */

#import "Puppeteer.h"
#import "WindowInfo.h"

/*
 * preferencesPuppet. This program demonstrates the use of the Puppet object to
 * toggle the Public Window Server switch from the command line. It assumes that
 * you have no additional preferences modules loaded - if you have, you may need to
 * adjust the x coordinate of one of the mouse clicks.
 */
 
int
waitForWindowCountToChange(id puppet, int currentWindowCount)
/*
 * Wait for the application's window count to change. Timeout after 5 seconds.
 * Return the new window count.
 */
{
	int i, newWindowCount;
	
	for (i=0; i<5; i++) {
		if ((newWindowCount = [puppet windowCount]) != currentWindowCount)
			break;
		sleep(1);
	}

	return newWindowCount;
}

void
togglePWS(id puppet)
{
	int oldWindowCount, newWindowCount;
	id speaker = [puppet appSpeaker];

	/*
	 * Unhide Preview. Things don't work properly if the target application is
	 * hidden.
	 */
	[speaker selectorRPC:"unhide" paramTypes:""];

	/*
	 * Make Preferences the active application.
	 */
	[puppet postActivate:YES];
	
	/*
	 * Click in the far right of the slider for the top scroll view. The unix
	 * settings icon should then be second from the right.
	 */ 		
	[puppet postSingleClick:NX_KEYWINDOW flags:0 x:377.0 y:212.0];
	
	/*
	 * Click on the unix settings icon. If you have other preference modules
	 * loaded, the x coordinate may need adjusting here.
	 */ 		
	[puppet postSingleClick:NX_KEYWINDOW flags:0 x:278.0 y:256.0];
	
	/*
	 * Now click the "Public Window Server" switch. If this is currently off,
	 * an alert panel will pop up. To cope with this, we wait a few seconds to
	 * see if the window count changes. If it does, we can assume that there is
	 * an alert panel waiting and click it's OK button.
	 */ 		
	oldWindowCount = [puppet windowCount];
	[puppet postSingleClick:NX_KEYWINDOW flags:0 x:240.0 y:66.0];
	newWindowCount = waitForWindowCountToChange(puppet, oldWindowCount);
	if (newWindowCount != oldWindowCount) {
		/*
		 * There must be an alert panel. Click OK. Tell the user what happened.
		 */
		[puppet postSingleClick:NX_KEYWINDOW flags:0 x:328.0 y:20.0];
		printf("Public Window Server enabled\n");
	} else
		printf("Public Window Server disabled\n");
}

void
main(argc, argv)
int argc;
char **argv;
{
	int c, errflg = 0;
	id puppet;

	while ((c = getopt(argc, argv, "P")) != EOF)
	switch (c) {
	case '?':
	default:
		errflg++;
		break;
	}
	if (errflg) {
		fprintf(stderr, "Usage: preferencesPuppet [-P]\n");
		exit(2);
	}
	
	/*
	 * Create the Preferences puppet, launching Preferences if necessary.
	 */
	puppet = [Puppeteer connectToApp:"Preferences" launch:YES];
	if (!puppet) {
		fprintf(stderr, "Could not connect to Preferences\n");
		exit(1);
	}

	/*
	 * Attach the strings. Preferences will then be ready to accept events.
	 */	
	[puppet attachStrings];
	
	togglePWS(puppet);

	/*
	 * Release strings. This is necessary for Preferences to continue to respond to
	 * real user events.
	 */
	[puppet releaseStrings];
}