
#import "BackgroundCLI.h"
#import "Workspace.h"
#import "Background.h"

void usage(char *fn);

@implementation BackgroundCLI

+ new
{
    [super new];
    {
	const char *fn;
	NXRect r={{0, 0}};
	id window;
	id view;
	BOOL perm=NO;
	
 	if (NXArgc<2 || NXArgc>3) usage(NXArgv[0]);
	fn = NXArgv[1];
	if (strcmp(NXArgv[1],"-p")==0)
	    {
		perm = YES;
		if (NXArgc<3) 
		    usage(NXArgv[0]); 
		else 
		    fn = NXArgv[2];
	    }
	[NXApp getScreenSize:&(r.size)];
	view = [[Background alloc] initFrame:&r];
	[view setImageNamed:fn];
	[view setPermanent:perm];
	window = [[Workspace alloc] initContent:&r style:NX_TOKENSTYLE 
	    backing:NX_NONRETAINED  buttonMask:0 defer:YES];
	[window setContentView:view];
	[window useOptimizedDrawing:NO];
	[window removeFromEventMask:(NX_LMOUSEDOWNMASK | NX_LMOUSEUPMASK
  			   | NX_MOUSEMOVEDMASK | NX_LMOUSEDRAGGEDMASK
			   | NX_MOUSEENTEREDMASK | NX_MOUSEEXITEDMASK
			   | NX_KEYDOWNMASK | NX_KEYUPMASK
			   | NX_CURSORUPDATEMASK)];
	PSsetwindowlevel(-1, [window windowNum]);
	[window placeWindowAndDisplay:&r];
	[window display];
	[window orderWindow:NX_ABOVE relativeTo:0];
	NXPing();
	if (!perm) 
	    while(YES) 
		sleep(1000); 
	else 
	    return self;
    }
}

- run
{
    return self;
}

@end

void usage(char *fn) {
    fprintf(stderr,"Usage: %s [-p] <image-file>\n",fn);
    exit(1);
}
