/***************************************************************************
                          main.c  -  description
                             -------------------
    begin                : Tue Nov 21 20:35:44 CET 2000
    copyright            : (C) 2001 by cosmo (Kasim Terzic)
    email                : cosmo@operamail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
/**************************************************************************************
 *  Largely based on the excellent afterstep applet tutorial by Albert aka Tigr, URL: *
 *    http://www.tigr.net/afterstep/doc/Devel-HOWTO.html, as well as                  *
 *  the great Xlib tutorials over at                                                  *
 *    http://www-genome.wi.mit.edu/~hnguyen/xlib-programming/xlib-programming.html    *
 *    http://tronche.com/gui/x/xlib-tutorial/                                         *
 **************************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "window.h"
#include "activate.h"
#include <unistd.h> // So we got the profile for 10 seconds
#include <stdio.h>

#define WIDTH	62
#define HEIGHT 62

char * app_name = "askeymap";
unsigned char Update = 0, Exit = 0;
unsigned char withdrawn = 0;

int main(int argc, char** argv)
{
	KeyboardMap *LastMap;
	char HomeDir[100];	/* Path to the xmodmap files */

	// check for the withdrawn option
	if (argc > 1)
		if(!strncmp(argv[1],"-w",2)||!strncmp(argv[1],"-W",2))
			{
			withdrawn=1;
			printf("Withdrawn mode. withdrawn=%d\n",withdrawn);
			}

	mainDisplay = XOpenDisplay(NULL);
	if (!mainDisplay)
			{
			fprintf(stderr,"askeymap: can't open display %s.\n",	XDisplayName(NULL));
			exit(1);
			}	

	// Get some colours
	black_pixel = BlackPixel(mainDisplay, DefaultScreen(mainDisplay));
	white_pixel = WhitePixel(mainDisplay, DefaultScreen(mainDisplay));

	// Create the windows
	mainWindow = XCreateSimpleWindow(mainDisplay, DefaultRootWindow(mainDisplay), 0, 0,
				WIDTH, HEIGHT, 0, black_pixel, black_pixel);

	iconWindow = XCreateSimpleWindow(mainDisplay, DefaultRootWindow(mainDisplay), 0, 0,
				WIDTH, HEIGHT, 0, black_pixel, black_pixel);

	WmHints.flags = IconWindowHint;   // Set the iconWindow as the icon for the main one
        WmHints.icon_window = iconWindow;
        XSetWMHints(mainDisplay,
                    mainWindow,
                    &WmHints);

		// Let the windowmanager know about our name and stuff

	status = XStringListToTextProperty(&app_name, 1, &title);
        XSetWMName(mainDisplay, mainWindow, &title);
	XSetWMName(mainDisplay, iconWindow, &title);
        classHint.res_name = "askeymap" ;
        classHint.res_class = "ASKEYMAP";
        XSetClassHint(mainDisplay, mainWindow, &classHint);
        XStoreName(mainDisplay, mainWindow, "askeymap");
        XSetIconName(mainDisplay, mainWindow, "askeymap");

	// Give the size so we can't resize it...
	win_size_hints = XAllocSizeHints();
	if (!win_size_hints) {
  	  fprintf(stderr, "XAllocSizeHints - out of memory\n");
    	exit(1);
		}

	// initialize the structure appropriately.
	win_size_hints->flags = PSize | PMinSize | PMaxSize;
	win_size_hints->min_width = WIDTH;
	win_size_hints->min_height = HEIGHT;
	win_size_hints->base_width = WIDTH;
	win_size_hints->base_height = HEIGHT;
	win_size_hints->max_width = WIDTH;
	win_size_hints->max_height = HEIGHT;

	// pass the size hints to the window manager.
	XSetWMNormalHints(mainDisplay, mainWindow, win_size_hints);
	XFree(win_size_hints);

	// Add support for -w withdrawn mode for WindowMaker
        WmHints.flags = StateHint | IconWindowHint;
        WmHints.initial_state =
            withdrawn ? WithdrawnState : NormalState;
        /* which window serves as an icon */
        WmHints.icon_window = iconWindow;
        if (withdrawn) {
                WmHints.window_group = mainWindow;
                WmHints.flags |= WindowGroupHint;
        }
        /* the position refers to the icon then */
        if (withdrawn) {
                WmHints.icon_x = 0;
                WmHints.icon_y = 0;
                WmHints.flags |= IconPositionHint;
         }
        XSetWMHints(mainDisplay,
                    mainWindow,
                    &WmHints);
        status = XSetCommand(mainDisplay, mainWindow, argv, argc);


	// Ignore the close window events
	//	wm_delete_window = XInternAtom(mainDisplay, "WM_DELETE_WINDOW", False);
	//	wm_protocols = XInternAtom(mainDisplay, "WM_PROTOCOLS", False);
	//	status = XSetWMProtocols(mainDisplay, mainWindow, &wm_delete_window, 1);

	// We want to get MapNotify events
	XSelectInput(mainDisplay, withdrawn?iconWindow:mainWindow, StructureNotifyMask);


	// "Map" the window (that is, make it appear on the screen)
	XMapWindow(mainDisplay, mainWindow);

	loadConfig(mainDisplay, DefaultRootWindow(mainDisplay));
	
// Create a "Graphics Context"
	gc = XCreateGC(mainDisplay, mainWindow, 0, NULL);

	// Tell the GC we draw using the white color
	XSetForeground(mainDisplay, gc, white_pixel);

	// Wait for the MapNotify event
	for(;;) {
		XEvent e;
		XNextEvent(mainDisplay, &e);
		if (e.type == MapNotify) break;
		}

	currentPixmap=XCreatePixmap(mainDisplay, DefaultRootWindow(mainDisplay), 60, 60, DefaultDepth(mainDisplay, DefaultScreen(mainDisplay)) );
	currentShape=XCreatePixmap(mainDisplay, DefaultRootWindow(mainDisplay), 60, 60, DefaultDepth(mainDisplay, DefaultScreen(mainDisplay)) );

	memcpy(&currentPixmap, &(Anchor->CountryXpm), sizeof( Pixmap ));
	XCopyArea(mainDisplay, currentPixmap, mainWindow, gc, 0, 0, 60, 60, 0, 0);
	XCopyArea(mainDisplay, currentPixmap, iconWindow, gc, 0, 0, 60, 60, 0, 0);

	// Send the flush request to the server
	XFlush(mainDisplay);

	XSelectInput(mainDisplay, withdrawn?iconWindow:mainWindow, ExposureMask | ButtonPressMask);

	for(;;) {
		while (XPending(mainDisplay)) {
			XEvent e;
			XNextEvent(mainDisplay, &e);

 			switch(e.type) {
				case ClientMessage:
							if ((Event.xclient.message_type == wm_protocols)
								&& (Event.xclient.data.l[0] == wm_delete_window)) { Exit=1; }
							break;
				// Deal with the button presses
				case ButtonPress:
							if(e.xbutton.button == Button1) {
							activateNext(&currentPixmap, &currentShape, &pixatt);
									Update=1;
									}
							else if(e.xbutton.button == Button3) Exit=1;
							break;
				// If exposed, redraw the pixmap
				case Expose:
							if(e.xexpose.count==0) {	Update=1;	}
							break;
				default:
				}
      			
		}
		if(Update) {
     		XCopyArea(mainDisplay, currentPixmap, mainWindow, gc, 0, 0, 60, 60, 0, 0);
		XCopyArea(mainDisplay, currentPixmap, iconWindow, gc, 0, 0, 60, 60, 0, 0);
				XFlush(mainDisplay);
				}	

		if(Exit) {
				break;
				}		
	
		usleep(20000L);
	}

	XFree(gc);

	// Free our linked list.

	CurrentMap=Anchor;
	do {
		XFreePixmap(mainDisplay, CurrentMap->CountryXpm);
		XFreePixmap(mainDisplay, CurrentMap->CountryShp);
		CurrentMap=CurrentMap->Next;
	} while(CurrentMap != NULL); //CurrentMap=CurrentMap->Next;		

	CurrentMap=Anchor;
	
	while(CurrentMap->Next!=NULL)
	{	LastMap=CurrentMap;
		CurrentMap=CurrentMap->Next;
		free(LastMap);

	}

	free(CurrentMap);
	XFree(mainDisplay);
	return 0;
}



























































