/***************************************************************************
                          activate.c  -  description
                             -------------------
    begin                : Tue Nov 21 2000
    copyright            : (C) 2001 by cosmo
    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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "activate.h"
//#include <window.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/*extern char *HomeDir;*/

int exists(const char *filename)
{
	FILE *temp;

 	if((temp=fopen(filename, "r"))==NULL)
  	{
  	fprintf(stdout, "The file %s does not exist!\n", filename);
  	return 0;
		}
	else
  	{
  	fclose(temp);
		return 1;
  	}
}

int loadConfig(Display *mainDisplay, Window mainWindow)
{
	FILE *CONFIG;
	char homedir[MAXBASEDIRLEN];
	char curline[200];
	char *curpoint;
	char basedir[MAXBASEDIRLEN];
  char icondir[MAXICONDIRLEN], curicon[MAXICONDIRLEN+20];	

	strcpy(basedir,"p/opt/gnome/share/xmodmap/");		// Hardcoded for now!
	strcpy(icondir,"p/home/cosmo/ToBeBurned/develop/keyb/");

	Anchor=calloc(1,sizeof(KeyboardMap));
	Anchor->Next=NULL;
	Anchor->CountryXpm=XCreatePixmap(mainDisplay, mainWindow, 60, 60, DefaultDepth(mainDisplay, DefaultScreen(mainDisplay)) );
	Anchor->CountryShp=XCreatePixmap(mainDisplay, mainWindow, 60, 60, DefaultDepth(mainDisplay, DefaultScreen(mainDisplay)) );
	CurrentMap=Anchor;

	if(strlen(getenv("HOME"))>=MAXBASEDIRLEN)	exit(1);	// This would REALLY be very weird!
	strcpy(homedir, getenv("HOME"));

	if(!exists(strcat(homedir,"/.askeymaprc")))
			{ fprintf(stderr,"Config file %s/.askeymaprc missing. Attempting to copy a sample one over...\n",homedir);
				if(exists("/usr/local/share/askeymap/askeymaprc"))
					system("cp /usr/local/share/askeymap/askeymaprc ~/.askeymaprc");
				else
				{ fprintf(stderr,"Not found. Please locate the file askeymaprc and copy it into your home directory as .askeymaprc before continuing.\n");
				exit(1);
				}
			}
// Open the configuration file	
	CONFIG=fopen(homedir,"r");

// Parse it

	while(!feof(CONFIG))
	{
		fgets(curline, 200, CONFIG);
	
		curpoint=strtok(curline, " =:\x08\n");		// Get the first identifier in the line
    if(curpoint == NULL) continue;
		//puts(curpoint);
		//puts("--");		
		
		if(*curpoint=='#') continue;

		// Change the default directories if needed
		if(!strcmp(curpoint,"XMODMAPDIR")) strncpy(basedir,strtok(NULL, " =:\x08\n"), MAXBASEDIRLEN-20);
		if(!strcmp(curpoint,"ICONDIR")) strncpy(icondir,strtok(NULL, " =:\x08\n"), MAXICONDIRLEN-20);
	
		// Or add a new keyboard map
		if(!strcmp(curpoint,"KEYBOARD"))
		{ if(CurrentMap->Next!=NULL) CurrentMap=CurrentMap->Next;

	  	strncpy(CurrentMap->CountryCode, strtok(NULL, " =:\x08\n"), 2);
			CurrentMap->CountryCode[2]='\0';
			CurrentMap->CountryCode[0]=tolower(CurrentMap->CountryCode[0]);
			CurrentMap->CountryCode[1]=tolower(CurrentMap->CountryCode[1]);

		  CurrentMap->MapFile[0]='\0';
			strcat(CurrentMap->MapFile, basedir);
			strcat(CurrentMap->MapFile, "/xmodmap.");
			strcat(CurrentMap->MapFile, CurrentMap->CountryCode);
		  //puts(CurrentMap->MapFile);
			strcpy(curicon, icondir);
			strcat(curicon, "/icon");
			strcat(curicon, CurrentMap->CountryCode);
			strcat(curicon, ".xpm");
			
			if(!exists(curicon) || !exists(CurrentMap->MapFile))	// Error, this entry is not valid
				{
				fprintf(stderr, "Error processing country entry: %s\n", CurrentMap->CountryCode);
				CurrentMap->MapFile[0]='\0';
				//CurrentMap->CountryXpm=NULL;
				//CurrentMap->CountryShp=NULL;
        CurrentMap->Next=NULL;	// Ensure that the current structure gets overwritten next time
				continue;
				}
			XpmReadFileToPixmap(mainDisplay, mainWindow, curicon, &(CurrentMap->CountryXpm), &(CurrentMap->CountryShp), &(CurrentMap->pixatt));
	
			CurrentMap->Next=calloc(1,sizeof(KeyboardMap));
			CurrentMap->Next->CountryXpm=XCreatePixmap(mainDisplay, mainWindow, 60, 60, DefaultDepth(mainDisplay, DefaultScreen(mainDisplay)) );
			CurrentMap->Next->CountryShp=XCreatePixmap(mainDisplay, mainWindow, 60, 60, DefaultDepth(mainDisplay, DefaultScreen(mainDisplay)) );
      CurrentMap->Next->Next=NULL;
			}


	}

	// The only problem is that the last entry in the linked list is unusable. Delete it.
	if(Anchor->Next==NULL)
		{
		fprintf(stderr, "Error: Only one valid keyboard selected! No point in continuing.\n");
		exit(1);
		}
	for(CurrentMap=Anchor;CurrentMap->Next->Next!=NULL;CurrentMap=CurrentMap->Next) puts(CurrentMap->MapFile);
	puts(CurrentMap->MapFile);
/*  if(CurrentMap->Next->CountryXpm != NULL)*/ XFreePixmap(mainDisplay, CurrentMap->Next->CountryXpm);
/*	if(CurrentMap->Next->CountryShp != NULL)*/ XFreePixmap(mainDisplay, CurrentMap->Next->CountryShp);
	free(CurrentMap->Next);
	
	CurrentMap->Next=NULL;
		
	CurrentMap=Anchor;		// Start at the beginning of the list
	fclose(CONFIG);
	return 0;
}

int activate(const char *MapPath)
{
	char Command[150];	

//printf("%s ", MapPath);

	/* Check whether the file as we've constructed it really exists... */
	if(!exists(MapPath)) return -1;

sprintf(Command, "xmodmap %s", MapPath);
system(Command);

//printf("Activated! \a\n");

return 0;
}

void activateNext(Pixmap *Pix, Pixmap *Shp, XpmAttributes *attr)
{
	if(CurrentMap->Next==NULL) CurrentMap=Anchor;
		else CurrentMap=CurrentMap->Next;

	activate(CurrentMap->MapFile);
	
	memcpy(Pix,&(CurrentMap->CountryXpm), sizeof(Pixmap));
	memcpy(Shp,&(CurrentMap->CountryShp), sizeof(Pixmap));
	memcpy(attr,&(CurrentMap->pixatt), sizeof(XpmAttributes));
}













































































