#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <Ph.h>
#include <errno.h>

/****************************************************************** STATICS ***/
static PhDim_t  rdim = { 600, 400 };

/************************************************************ OPEN_REGION() ***/
PhRid_t
open_region( void )
{
	PhRegion_t      region;
	PhRect_t        rect;
	PhRid_t         rid;

	memset( &region, 0, sizeof( region ) );
	/* Wish to have pointer motion
	 * events enqueued to us.
	 */
	region.events_sense = Ph_EV_PTR_MOTION | Ph_EV_KEY | Ph_EV_BUT_PRESS;

	/* Wish to be opaque to all pointer-type
	 * events and be visually opaque.
	 */
	region.events_opaque = Ph_EV_PTR_ALL | Ph_EV_DRAW | Ph_EV_KEY | Ph_EV_EXPOSE | Ph_EV_BLIT;

	/* Origin at (100,100) relative to root */
	region.origin.x = region.origin.y = 0;
	region.parent = Ph_ROOT_RID;

	region.flags |= Ph_FORCE_BOUNDARY;

	rect.ul.x = rect.ul.y = 0;
	rect.lr.x = rdim.w;
	rect.lr.y = rdim.h;

	rid = PhRegionOpen(	
		Ph_REGION_PARENT |
		Ph_REGION_FLAGS |
		Ph_REGION_EV_SENSE |
		Ph_REGION_EV_OPAQUE | 
		Ph_REGION_ORIGIN | 
		Ph_REGION_RECT, &region, &rect, NULL );

	if( rid != -1 ) {
		PhRegion_t	info = { 0 };
		
		PgSetRegion( rid );
        PgSetFillColor( Pg_BLACK );
        PgDrawRect( &rect, Pg_DRAW_FILL );
        PgFlush();
		
		printf( "rid=%ld\n", rid );
		info.rid = rid;
		info.cursor_color = 0xFF0000;
		info.cursor_type = Ph_CURSOR_BIG_POINTER;
		
		printf( "PhRegionChange()=%d, errno = %d\n",
			PhRegionChange( Ph_REGION_CURSOR, 0, &info, NULL, NULL ), errno );
	}
	
	return rid;
}

/******************************************************************* MAIN() ***/
int
main( int argc, char *argv[] )
{
	if( NULL == PhAttach( NULL, NULL ) ) {
		fprintf( stderr, "Couldn't attach a " "Photon channel.\n" );
		exit( EXIT_FAILURE );
	}

	if( -1 == open_region() ) {
		fprintf( stderr, "Couldn't open region.\n" );
		exit( EXIT_FAILURE );
	}
	
	while( 1 ) {
		sleep(100);
	}
	
	return 0;
}
