/* $Id: national.c,v 3.1 1994/01/07 22:51:34 ppessi Exp $
 * 
 * national.c --- handle national fonts and keyboard layouts
 *
 * Author: ppessi <Pekka.Pessi@hut.fi>
 *
 * Copyright (c) 1993 Pekka Pessi
 *
 * Created      : Mon Mar 29 06:32:31 1993 ppessi
 * Last modified: Wed Jan  5 07:51:36 1994 ppessi
 *
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "nifty.h"
#include "napsaprefs.h"
#include "amiga.h"
#include "wimp.h"

#define MAX_NATIONS 31

struct nation {
  const char *n_kmapname;	/* name of the kmap resource */
  void *n_kmap;			/* pointer to the kmap resource */
  const char *n_patches;	/* mapping between Latin1 and national code */
};

unsigned char national_show[CHARSETSIZE];
unsigned char national_send[CHARSETSIZE];

/*
 * Note that US* has synonymes
 */
char nation_names[] =
  "US ASCII" NUL "Danish" NUL "Finnish" NUL "French" NUL
  "German" NUL "Norwegian" NUL "Swedish" NUL "UK ASCII" NUL;

static struct nation nations[MAX_NATIONS] = 
{ 
  /* US-ASCII */
  { "usa1", NULL, NULL },
  /* Danish */
  { "dk", NULL, 
      /* Danish I [Æ]Å\Ø{æ}å|ø */  
      "[\306]\305\\\330{\346}\345|\370" }, 
  /* Finnish */
  { "s", NULL, 
      /* [Ä]Å\Ö{ä}å|ö */  
      "[\304]\305\\\326{\344}\345|\366" }, 
  /* French */
  { "f", NULL, 
      /* @à[°\ç]§{é|ù}è~¨ */  
      "@\340[\227\\\337]\305{\351|\371}\350~\250" }, 
  /* German */
  { "d", NULL,
      /* @§[Ä]Ü\Ö{ä}ü|ö~ß */
      "@\247[\304]\334\\\326{\344}\374|\366~\337" },
  /* Norwegian */
  { "n", NULL, 
      /* $¤@É[Æ]Å\Ø^Ü`é{æ}å|ø~ü */
      "$\244@\311[\306]\305\\\330^\334`\351{\346}\345|\370~\374" },
  /* Swedish */
  { "s", NULL, 
      /* $¤@É[Ä]Å\Ö^Ü`é{ä}å|ö~ü */
      "$\244@\311[\304]\305\\\326^\334`\351{\344}\345|\366~\374" },
  /* UK */
  { "gb", NULL,
      /* british #£*/ 
      "#\243" },
}; /* ASCII */

static struct nation *used_nation = NULL;
static short used_nation_number;
static short no_nation = 7;

static unsigned char kmap_template[] =
"\000\001\002\003\004\005\006\007"
"\010\011\012\013\014\015\016\017"
"\020\021\022\023\024\025\026\027"
"\030\031\032\033\034\035\036\037"
" !c#$Y|#@ca\"--r-o+23'uP.,1o\"   ?"
"AAAAAAACEEEEIIIIDNOOOOO*OUUUUYTs"
"aaaaaaaceeeeiiiidnooooo/ouuuuyty";

/*
 * initialize current mapping
 */
void setnation(unsigned int n)
{
  int i;
  const unsigned char *patch;

  if (n >= no_nation)
    return;

  if (used_nation == nations + n)
    return;

  used_nation = nations + n;
  used_nation_number = n;

  for (i = 0; i < CHARSETSIZE; i++) {
    national_send[i] = national_show[i] = i;
  }

  /* Set the keymap */
  used_nation->n_kmap = 
    setmap(used_nation->n_kmapname, used_nation->n_kmap);

  /* patch send & receive tables */
  memcpy(national_send + 128, kmap_template, 128);
  for (patch = used_nation->n_patches; patch && *patch; patch += 2) {
    national_send[patch[1]] = patch[0];
    national_show[patch[0]] = patch[1];
  }
}

/*
 * Parse keymap resource
 */
int parsekeymap(char *s, void * dummy)
{
  char *mapname = PathPart(s);

  /* Are we only given the mapname? */ 
  if (mapname == s) {
    np.keymap = s;
    return 1;
  } else if (*mapname == '/') {
    int i;
    const char *t = nation_names;

    *mapname++ = '\0';	/* NUL terminate the map name */

    if (!Stricmp("ASCII", s)) {
      nations[0].n_kmapname = mapname;
      return 1;
    }

    for (i = 0; *t; i++) {
      if (!Strnicmp(t, s, 2)) {
	/* Found nation */
	nations[i].n_kmapname = mapname;
	return 1;
      }
      while (*t++)
	;
    }
    /* Illegal nation, don't set keymap */
    perrorparse("Illegal nation %s", s);
  }
  free(s);
  return 0;
}


void initnations(void)
{
  setnation(np.nation);
}

void deinitnations(void)
{
}
