alphabet.c 36.4 KB
Newer Older
David Bruce's avatar
David Bruce committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
   alphabet.c:

   Code for word lists, keyboard fingering, and related issues.

   Copyright 2003, 2008, 2009, 2010.
   Authors: Jesse Andrews, Sreyas Kurumanghat, David Bruce.
   
   Project email: <tux4kids-tuxtype-dev@lists.alioth.debian.org>
   Project website: http://tux4kids.alioth.debian.org

   alphabet.c is part of Tux Typing, a.k.a "tuxtype".

Tux Typing 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 3 of the License, or
(at your option) any later version.

Tux Typing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
David Bruce's avatar
David Bruce committed
27 28 29



30
#include "convert_utf.h"
David Bruce's avatar
David Bruce committed
31 32
#include "globals.h"
#include "funcs.h"
33
#include "SDL_extras.h"
David Bruce's avatar
David Bruce committed
34 35 36 37 38 39 40 41 42 43 44 45



/* An individual item in the list of cached unicode characters that are rendered at   */
/* the start of each game.                                                            */
typedef struct uni_glyph {
  wchar_t unicode_value;
  SDL_Surface* white_glyph;
  SDL_Surface* red_glyph;
} uni_glyph;

/* These are the arrays for the red and white letters: */
46
static uni_glyph char_glyphs[MAX_UNICODES] = {{0, NULL, NULL}};
David Bruce's avatar
David Bruce committed
47 48 49

/* An individual item in the list of unicode characters in the keyboard setup.   */
/* Basically, just the Unicode value for the key and the finger used to type it. */
50 51 52 53
/*typedef struct keymap {
	char keyname[5];
	int shift;
} keymap;*/
David Bruce's avatar
David Bruce committed
54 55 56
typedef struct kbd_char {
  wchar_t unicode_value;
  char finger;
57 58 59 60
  //keymap key;
  char keyname[5];
  int shift;
  char latin_char;
David Bruce's avatar
David Bruce committed
61 62 63 64
} kbd_char;

/* List with one entry for each typable character in keyboard setup - has the */
/* Unicode value of the key and the associated fingering.                     */
65
static kbd_char keyboard_list[MAX_UNICODES] = {{0, -1, {0}, 0, -1}};
David Bruce's avatar
David Bruce committed
66 67 68 69 70 71 72 73 74 75

/* Used for word list functions (see below): */
static int num_words;
static wchar_t word_list[MAX_NUM_WORDS][MAX_WORD_SIZE + 1];
static wchar_t char_list[MAX_UNICODES];  // List of distinct letters in word list
static int num_chars_used = 0;       // Number of different letters in word list

/* Local function prototypes: */
static void gen_char_list(void);
static int add_char(wchar_t uc);
76 77
//static void set_letters(signed char* t);
//static void show_letters(void);
David Bruce's avatar
David Bruce committed
78 79 80
static void clear_keyboard(void);
static int unicode_in_key_list(wchar_t uni_char);
int check_needed_unicodes_str(const wchar_t* s);
81
int map_keys(wchar_t wide_char, kbd_char* keyboard_entry);
David Bruce's avatar
David Bruce committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96



/*****************************************************/
/*                                                   */
/*          "Public" Functions                       */
/*                                                   */
/*****************************************************/



/* FIXME would be better to get keymap from system somehow (SDL? X11?) - */
/* all this does now is fiddle with the ALPHABET and FINGER arrays */
int LoadKeyboard(void)
{
97
  char fn[FNLEN];
David Bruce's avatar
David Bruce committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  int found = 0;

  clear_keyboard();

  /* First look for keyboard.lst in theme path, if desired: */
  if (!settings.use_english)
  {
    sprintf(fn , "%s/keyboard.lst", settings.theme_data_path);
    if (CheckFile(fn))
    {
      found = 1;
    }
  }

  /* Now look in default path if desired or needed: */
  if (!found)
  {
    sprintf(fn , "%s/keyboard.lst", settings.default_data_path);
    if (CheckFile(fn))
    {
      found = 1;
    }
  }

  if (!found)
  {
    fprintf(stderr, "LoadKeyboard(): Error finding file for keyboard setup!\n");
    return 0;
  }
  
  /* fn should now contain valid path to keyboard.lst: */
  DEBUGCODE{fprintf(stderr, "fn = %s\n", fn);}

  {
132
    char str[255];
David Bruce's avatar
David Bruce committed
133 134
    wchar_t wide_str[255];

135 136
    FILE* f = NULL;
    int k = 0;
David Bruce's avatar
David Bruce committed
137 138 139 140 141 142 143 144 145 146 147

    f = fopen( fn, "r" );

    if (f == NULL)
    {
      LOG("LoadKeyboard() - could not open keyboard.lst\n");
      return 0;
    }

    do
    {
148 149 150 151
      int fscanf_result = fscanf( f, "%[^\n]\n", str);
      
      if (fscanf_result == EOF)
        break;
David Bruce's avatar
David Bruce committed
152
      /* Convert to wcs from UTF-8, if needed; */
153
      ConvertFromUTF8(wide_str, str, 255);
David Bruce's avatar
David Bruce committed
154 155 156 157

      /* Line must have 3 chars (if more, rest are ignored) */
      /* Format is: FINGER|Char  e.g   "3|d"                */
      /* wide_str[0] == finger used to type char            */
158
      /* wide_str[1] =='|'                                  */
David Bruce's avatar
David Bruce committed
159 160
      /* wide_str[2] == Unicode value of character          */

161 162
      /* NOTE the following code is really confusing!       */

David Bruce's avatar
David Bruce committed
163 164 165 166 167 168 169 170 171 172
      /* Some sanity checks:  */
      if ((wcslen(wide_str) >=3)
       && (wcstol(&wide_str[0], NULL, 0) >=0)   /* These lines just make sure the */
       && (wcstol(&wide_str[0], NULL, 0) < 10)  /* finger is between 0 and 10     */
       && (wide_str[1] == '|')
       && (k < MAX_UNICODES)
       && !unicode_in_key_list(wide_str[2])) /* Make sure char not already added */
      {
        DEBUGCODE
        {
173
          fprintf(stderr, "Adding key: Unicode char = '%C'\tUnicode value = %d\tfinger = %ld\n",
David Bruce's avatar
David Bruce committed
174
                  wide_str[2], wide_str[2], wcstol(&wide_str[0], NULL, 0)); 
175
        }
David Bruce's avatar
David Bruce committed
176 177 178 179

        /* Just plug values into array: */
        keyboard_list[k].unicode_value = wide_str[2];
        keyboard_list[k].finger = wcstol(&wide_str[0], NULL, 0);
180

181
        if (wcslen(wide_str) < 5)
182 183 184
        {
          if(!settings.use_english)
          {
185
            map_keys(-1, &keyboard_list[k]);
186 187 188 189 190 191 192 193 194 195 196 197 198 199
            keyboard_list[k].latin_char=-1;
            keyboard_list[k].finger = wcstol(&wide_str[0], NULL, 0);
          }
          else
          {
            map_keys(wide_str[2],&keyboard_list[k]);
            keyboard_list[k].latin_char=wide_str[2];
          }
        }
        else
        {
          map_keys(wide_str[4],&keyboard_list[k]);
          keyboard_list[k].latin_char=wide_str[4];
        }
David Bruce's avatar
David Bruce committed
200 201
        k++;
      }
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
      else
      {
        if(wcslen(wide_str) == 1)
        {
          if(!settings.use_english)
          {
            keyboard_list[k].unicode_value = wide_str[0];
            map_keys(-1,&keyboard_list[k]);
            keyboard_list[k].latin_char=-1;
          }
          else
          {
            keyboard_list[k].unicode_value = wide_str[0];
            map_keys(-1, &keyboard_list[k]);
            keyboard_list[k].latin_char = wide_str[0];
          }
          k++;
        }
      }
David Bruce's avatar
David Bruce committed
221 222 223 224 225 226 227 228 229 230 231 232 233
    } while (!feof(f));

    fclose(f);
    LOG("Leaving LoadKeyboard()\n");
    return 1;
  }
}

/* Returns the finger hint(0-9) associated with a given Unicode value */
/* in the keyboard_list:                                              */
/* Returns -1 if somehow no finger associated with a Unicode value    */
/* in the list (shouldn't happen).                                    */
/* Returns -2 if Unicode value not in list.                           */
234
/* Returns -3 if argument outside of array.                           */
235 236
int GetFinger(int i)
{
237 238 239 240 241 242
  if (i < -1 || i >= MAX_UNICODES)
  {
    fprintf(stderr, "GetFinger() - requested index i = %d outside of array\n", i);
    return -3;
  }
  
243 244
  if (i == -1)
  {
245
    fprintf(stderr, "GetFinger() - Unicode char '%C' not found in list.\n", i);
246 247 248 249 250 251
    return -2;
  }

  if ((keyboard_list[i].finger < 0)
   || (keyboard_list[i].finger > 9))
  {
252
    fprintf(stderr, "GetFinger() - Unicode char '%C' has no valid finger.\n", i);
253 254 255 256 257 258 259 260
    return -1;
  }  

  return (int)keyboard_list[i].finger; /* Keep compiler from complaining */
}

int GetShift(int i)
{
261 262 263 264 265 266
  if (i < 0 || i >= MAX_UNICODES)
  {
    fprintf(stderr, "GetShift() - requested index i = %d outside of array\n", i);
    return -1;
  }
  return keyboard_list[i].shift;
267 268 269
}

int GetIndex(wchar_t uni_char)
David Bruce's avatar
David Bruce committed
270 271 272 273 274 275 276 277 278 279 280
{
  int i = 0;

  while ((i < MAX_UNICODES)
     &&  (keyboard_list[i].unicode_value != uni_char))
  {
    i++;
  }

  if (i == MAX_UNICODES)
  {
281
    fprintf(stderr, "GetIndex - Unicode char '%C' not found in list.\n", uni_char);
David Bruce's avatar
David Bruce committed
282
    return -1;
283
  }
David Bruce's avatar
David Bruce committed
284

285
  return i;
David Bruce's avatar
David Bruce committed
286 287
}

288
void GetKeyPos(int index, char* buf)
289
{
290 291 292 293 294 295 296
  if (index < 0 || index > MAX_UNICODES)
  {
    fprintf(stderr, "GetKeyPos() - invalid argument 'index' = %d\n", index); 
    buf = "";
    return;
  }
  sprintf(buf,"keyboard/keyboard_%s.png", keyboard_list[index].keyname);	
297
}
David Bruce's avatar
David Bruce committed
298

299

300 301
void GetWrongKeyPos(int index, char *buf)
{
302 303 304 305 306 307 308
  if (index < 0 || index > MAX_UNICODES)
  {
    fprintf(stderr, "GetWrongKeyPos() - invalid argument 'index' = %d\n", index); 
    buf = "";
    return;
  }
  sprintf(buf,"keyboard/keyboardN_%s.png", keyboard_list[index].keyname);
309
}
David Bruce's avatar
David Bruce committed
310

311

312
void GetKeyShift(int index, char* buf)
313
{
314 315 316 317 318 319 320
   if (index < 0 || index > MAX_UNICODES)
  {
    fprintf(stderr, "GetKeyShift() - invalid argument 'index' = %d\n", index); 
    buf = "";
    return;
  }

321 322 323 324 325
  if(keyboard_list[index].shift == 0)
    sprintf(buf,"keyboard/keyboard_None.png");
  else if(keyboard_list[index].shift == 1)
    sprintf(buf,"keyboard/keyboard_D00.png");
  else
326
    sprintf(buf,"keyboard/keyboard_D12.png");
327
}
328 329


330 331
wchar_t GetLastKey()
{
332 333 334 335
  //Avoid segfault if num_chars_used has gotten a bad value:
  if(!num_chars_used
    || num_chars_used < 0
    || num_chars_used >= MAX_UNICODES)
336 337 338
    return -1;
  else
    return keyboard_list[num_chars_used - 1].unicode_value;
339
} 
340 341


David Bruce's avatar
David Bruce committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355
int unicode_in_key_list(wchar_t uni_char)
{
  int i = 0;
  while ((i < MAX_UNICODES)
     &&  (keyboard_list[i].unicode_value != uni_char))
  {
    i++;
  }
  if (i == MAX_UNICODES)
    return 0;
  else
    return 1;
}

356

David Bruce's avatar
David Bruce committed
357 358


359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
// /* FIXME dead code but could be useful*/
// static void show_letters(void)
// {
// 	int i, l = 0;
// 	SDL_Surface* abit;
// 	SDL_Rect dst;
// 	int stop = 0;
// 	unsigned char t[255];
// 
// 	for (i=0; i<256; i++)
// 		if (ALPHABET[i])
// 			t[l++]=i;
// 
// 	t[l] = 0;
// 
// 	abit = BlackOutline(t, font, &white);
// 
// 	dst.x = 320 - (abit->w / 2);
// 	dst.y = 275;
// 	dst.w = abit->w;
// 	dst.h = abit->h;
// 
// 	SDL_BlitSurface(abit, NULL, screen, &dst);
// 
// 	SDL_FreeSurface(abit);
// 
// 	abit = BlackOutline("Alphabet Set To:", font, &white);
// 	dst.x = 320 - (abit->w / 2);
// 	dst.y = 200;
// 	dst.w = abit->w;
// 	dst.h = abit->h;
// 
// 	SDL_BlitSurface(abit, NULL, screen, &dst);
// 
// 	SDL_UpdateRect(screen, 0, 0, 0 ,0);
// 
// 	while (!stop) 
// 		while (SDL_PollEvent(&event)) 
// 			switch (event.type) {
// 				case SDL_QUIT:
// 					exit(0);
// 				case SDL_KEYDOWN:
// 				case SDL_MOUSEBUTTONDOWN:
// 					stop = 1;
// 			}
// 
// 	SDL_FreeSurface(abit);
// }
David Bruce's avatar
David Bruce committed
407 408


409

David Bruce's avatar
David Bruce committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/* Returns a random Unicode char from the char_glyphs list: */
/* --- get a letter --- */
wchar_t GetRandLetter(void)
{
  static wchar_t last = -1; // we don't want to return same letter twice in a row
  wchar_t letter;
  int i = 0;

  if (!num_chars_used)
  {
    fprintf(stderr, "GetRandLetter() - no letters in list!\n");
    last = -1;
    return -1;
  }

  do
  {
    i = rand() % num_chars_used;
    letter = char_glyphs[i].unicode_value;
  } while (letter == last);

  last = letter;

  return letter;
}

/******************************************************************************
*                           WORD FILE & DATA STRUCTURE                        *
******************************************************************************/



/* ClearWordList: clears the number of words
 */
void ClearWordList(void)
{
  int i;
447 448 449
//  for (i = 0; i < num_words; i++)
  //more robust if we just zero entire list - takes neglible time
  for (i = 0; i < MAX_NUM_WORDS; i++)
David Bruce's avatar
David Bruce committed
450 451 452 453 454 455
  {
    word_list[i][0] = '\0';
  }
  num_words = 0;
}

456

David Bruce's avatar
David Bruce committed
457 458 459 460 461 462

/* GetWord: returns a random word that wasn't returned
 * the previous time (unless there is only 1 word!!!)
 */
wchar_t* GetWord(void)
{
463 464
  static int last_choice = -1;
  int choice;
David Bruce's avatar
David Bruce committed
465

466
  LOG("Entering GetWord()\n");
David Bruce's avatar
David Bruce committed
467

468 469 470 471 472 473 474
  /* Safety/sanity checks: */
  /* Count list to make sure num_words is correct: */
  num_words = 0;
  while (word_list[num_words][0] != '\0')
  {
    num_words++;
  }
David Bruce's avatar
David Bruce committed
475

476 477 478 479 480
  if (0 == num_words)
  {
    LOG("No words in list\n");
    return NULL;
  }
David Bruce's avatar
David Bruce committed
481

482 483 484 485 486
  /* Now pick one: */
  do
  {
    choice = (rand() % num_words);
  } while ((choice == last_choice) || (num_words < 2));
David Bruce's avatar
David Bruce committed
487

488
  last_choice = choice;
David Bruce's avatar
David Bruce committed
489

490 491
  /* NOTE need %S rather than %s because of wide characters */
  DEBUGCODE { fprintf(stderr, "Selected word is: %S\n", word_list[choice]); }
David Bruce's avatar
David Bruce committed
492

493
  return word_list[choice];
David Bruce's avatar
David Bruce committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507
}



/* GenerateWordList(): adds the words from a given wordlist
 * it ignores any words too long or that has bad
 * character (such as #)
 */

/* Now returns the number of words in the list, so if no words */
/* returns "false"                                             */
int GenerateWordList(const char* wordFn)
{
  int j;
508 509
  int ret;
  char temp_word[FNLEN];
David Bruce's avatar
David Bruce committed
510 511 512
  wchar_t temp_wide_word[FNLEN];
  size_t length;

513
  FILE* wordFile = NULL;
David Bruce's avatar
David Bruce committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527

  DEBUGCODE { fprintf(stderr, "Entering GenerateWordList() for file: %s\n", wordFn); }

  num_words = 0;

  /* --- open the file --- */
  wordFile = fopen( wordFn, "r" );
  if ( wordFile == NULL )
  {
    fprintf(stderr, "ERROR: could not load wordlist: %s\n", wordFn );
    return 0;
  }

  /* --- load words from file named as argument: */
528
  DEBUGCODE { fprintf(stderr, "Loading words from file: %s\n", wordFn); }
David Bruce's avatar
David Bruce committed
529 530

  /* ignore the title (i.e. first line) */
531 532
  /* (compiler complains unless we inspect return value) */
  ret = fscanf( wordFile, "%[^\n]\n", temp_word);
David Bruce's avatar
David Bruce committed
533 534 535

  while (!feof(wordFile) && (num_words < MAX_NUM_WORDS))
  {
536
    ret = fscanf( wordFile, "%[^\n]\n", temp_word);
David Bruce's avatar
David Bruce committed
537 538
    DEBUGCODE {fprintf(stderr, "temp_word = %s\n", temp_word);}

539 540 541 542
    /* Ignore comment lines - starting with '#' */
    if (temp_word[0] == '#')
      continue;

David Bruce's avatar
David Bruce committed
543 544 545 546 547 548 549
    for (j = 0; j < strlen(temp_word); j++)
    {
      if (temp_word[j] == '\n' || temp_word[j] == '\r')
        temp_word[j] = '\0';
    }

    /* Convert from UTF-8 to wcs and make sure word is usable: */
550
    length = ConvertFromUTF8(temp_wide_word, temp_word, FNLEN);
551

David Bruce's avatar
David Bruce committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565
    DOUT(length);

    if (length == -1)  /* Means invalid UTF-8 sequence or conversion failed */
    {
      fprintf(stderr, "Word '%s' not added - invalid UTF-8 sequence!\n", temp_word);
      continue;
    }

    if (length == 0)
    {
      fprintf(stderr, "Word '%ls' not added - length is zero\n", temp_wide_word);
      continue;
    }

566 567 568 569 570 571 572 573 574
    //NOTE now the cascade and laser games have their own checks for how long of words
    //they can actually handle, so we let long words in now - DSB

//     if (length > MAX_WORD_SIZE)
//     {
//       fprintf(stderr, "Word '%s' not added - exceeds %d characters\n",
//               temp_word, MAX_WORD_SIZE);
//       continue;
//     }
David Bruce's avatar
David Bruce committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

    if (num_words >= MAX_NUM_WORDS)
    {
      fprintf(stderr, "Word '%s' not added - list has reached max of %d characters\n",
              temp_word, MAX_NUM_WORDS);
      continue;
    }

    if (!check_needed_unicodes_str(temp_wide_word))
    {
      fprintf(stderr, "Word '%S' not added - contains Unicode chars not in keyboard list\n",
              temp_wide_word);
      continue;
    }

    /* If we make it to here, OK to add word: */
    /* NOTE we have to add one to the length argument */
    /* to include the terminating null.  */
    DEBUGCODE
    {
      fprintf(stderr, "Adding word: %ls\n", temp_wide_word);
    }

    wcsncpy(word_list[num_words], temp_wide_word, strlen(temp_word) + 1);
    num_words++;
  }
        
  /* Make sure list is terminated with null character */
  word_list[num_words][0] = '\0';

  DOUT(num_words);

607 608 609 610 611
  if (wordFile)
  {
    fclose(wordFile);
    wordFile = NULL;
  }
David Bruce's avatar
David Bruce committed
612 613 614 615 616 617 618 619 620 621 622
  /* Make list of all unicode characters used in word list: */
  /* (we use this to check to make sure all are "typable"); */
  gen_char_list();

  LOG("Leaving GenerateWordList()\n");

  return (num_words);
}


/* This version creates the letters using TTF_RenderUNICODE_Blended */
623
int RenderLetters(int font_size)
David Bruce's avatar
David Bruce committed
624
{
625
  wchar_t t[2];
David Bruce's avatar
David Bruce committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
  int i, j;  /* i is chars attempted, j is chars actually rendered. */

  i = j = num_chars_used = 0;

  t[1] = '\0';

  while (i < MAX_UNICODES)
  {
    t[0] = keyboard_list[i].unicode_value;

    if (t[0] != 0)
    {
      DEBUGCODE
      {
        fprintf(stderr, "Creating SDL_Surface for list element %d, char = '%lc', Unicode value = %d\n", i, *t, *t);
      }

      char_glyphs[j].unicode_value = t[0];
644 645
      char_glyphs[j].white_glyph = BlackOutline_w(t, font_size, &white, 1);
      char_glyphs[j].red_glyph = BlackOutline_w(t, font_size, &red, 1);
David Bruce's avatar
David Bruce committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

      j++;
      num_chars_used++;
    }
    i++;
  }

  return num_chars_used;
}



void FreeLetters(void)
{
  int i;

662
  for (i = 0; i < MAX_UNICODES; i++)
David Bruce's avatar
David Bruce committed
663
  {
664 665 666 667 668 669 670 671 672 673
    if (char_glyphs[i].white_glyph)
    {
      SDL_FreeSurface(char_glyphs[i].white_glyph);
      char_glyphs[i].white_glyph = NULL;
    }
    if (char_glyphs[i].red_glyph)
    {
      SDL_FreeSurface(char_glyphs[i].red_glyph);
      char_glyphs[i].red_glyph = NULL;
    }
David Bruce's avatar
David Bruce committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
    char_glyphs[i].unicode_value = 0;
    char_glyphs[i].white_glyph = NULL;
    char_glyphs[i].red_glyph = NULL;
  } 
  /* List now empty: */
  num_chars_used = 0;
}


SDL_Surface* GetWhiteGlyph(wchar_t t)
{
  int i;

  for (i = 0;
       (char_glyphs[i].unicode_value != t) && (i <= num_chars_used);
       i++)
  {}

  /* Now return appropriate pointer: */
  if (i > num_chars_used)
  {
    /* Didn't find character: */
    fprintf(stderr, "Could not find glyph for Unicode char '%C', value = %d\n", t, t);
    return NULL;
  }
  
  /* Return corresponding surface for blitting: */
  return char_glyphs[i].white_glyph;
}



SDL_Surface* GetRedGlyph(wchar_t t)
{
  int i;

  for (i = 0;
       char_glyphs[i].unicode_value != t && i <= num_chars_used;
       i++)
  {}

  /* Now return appropriate pointer: */
  if (i > num_chars_used)
  {
    /* Didn't find character: */
    fprintf(stderr, "Could not find glyph for unicode character %lc\n", t);
    return NULL;
  }
  
  /* Return corresponding surface for blitting: */
  return char_glyphs[i].red_glyph;
}


/* Checks to see if all of the glyphs needed by the word list have been     */
/* successfully rendered based on the Unicode values given in keyboard.lst. */
/* If not, then the list contains characters that will not display and (if  */
/* keyboard.lst is correct) cannot be typed. Most likely, this means that   */
732
/* keyboard.lst is not correct.                                             */
David Bruce's avatar
David Bruce committed
733 734 735 736 737 738 739 740 741 742 743 744
/* Returns 1 if all needed chars found, 0 otherwise.                        */
int CheckNeededGlyphs(void)
{
  int i = 0;

  while ((i < MAX_UNICODES)
      && (char_list[i] != '\0'))
  {
    if (!GetWhiteGlyph(char_list[i]))
    {
      fprintf(stderr, "\nCheckNeededGlyphs() - needed char '%C' (Unicode value = %d) not found.\n",
              char_list[i], char_list[i]);
745 746
      fprintf(stderr, "This probably means that the theme's 'keyboard.lst' file is incorrect or incomplete.\n"
               "It could also mean that the available font lacks needed characters\n");
David Bruce's avatar
David Bruce committed
747 748 749 750 751 752 753 754 755 756 757 758
      return 0;
    }
    i++;
  }
  LOG("CheckNeededGlyphs() - all chars found.\n");
  return 1;
}

int check_needed_unicodes_str(const wchar_t* s)
{
  int i = 0;

759 760 761 762 763 764 765
  if (!s)
  {
    fprintf(stderr, "check_needed_unicodes_str() - null ptr passed as arg");
    return 0;
  }

  while (i < MAX_WORD_SIZE && i < wcslen(s))
David Bruce's avatar
David Bruce committed
766 767 768 769 770 771 772 773 774 775 776 777
  {
    if (!unicode_in_key_list(s[i]))
    {
      fprintf(stderr, "\ncheck_needed_unicodes_str() - needed char '%C' (Unicode value = %d) not found.\n",
              s[i], s[i]);
      return 0;
    }
    i++;
  }
  return 1;
}

778

779 780


781
/******************************************************************/
782
int map_keys(wchar_t wide_char, kbd_char* keyboard_entry)
783
{
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
  if (!keyboard_entry)
  {
    fprintf(stderr, "Error: map_keys() - keyboard_entry arg is NULL\n");
    return -1;
  }

  switch(wide_char)
  {
	case '`':strcpy(keyboard_entry->keyname,"A00");
		keyboard_entry->shift=0;
		keyboard_entry->finger=0;
		break;
	case '~':strcpy(keyboard_entry->keyname,"A00");
		keyboard_entry->shift=2;
		keyboard_entry->finger=0;
		break;
	case '1':strcpy(keyboard_entry->keyname,"A01");
		keyboard_entry->shift=0;
		keyboard_entry->finger=0;
		break;
	case '!':strcpy(keyboard_entry->keyname,"A01");
		keyboard_entry->shift=2;
		keyboard_entry->finger=0;
		break;
	case '2':strcpy(keyboard_entry->keyname,"A02");
		keyboard_entry->shift=0;
		keyboard_entry->finger=1;
		break;
	case '@':strcpy(keyboard_entry->keyname,"A02");
		keyboard_entry->shift=2;
		keyboard_entry->finger=1;
		break;
	case '3':strcpy(keyboard_entry->keyname,"A03");
		keyboard_entry->shift=0;
		keyboard_entry->finger=2;
		break;
	case '#':strcpy(keyboard_entry->keyname,"A03");
		keyboard_entry->shift=2;
		keyboard_entry->finger=2;
		break;
	case '4':strcpy(keyboard_entry->keyname,"A04");
		keyboard_entry->shift=0;
		keyboard_entry->finger=3;
		break;
	case '$':strcpy(keyboard_entry->keyname,"A04");
		keyboard_entry->shift=2;
		keyboard_entry->finger=3;
		break;
	case '5':strcpy(keyboard_entry->keyname,"A05");
		keyboard_entry->shift=0;
		keyboard_entry->finger=3;
		break;
	case '%':strcpy(keyboard_entry->keyname,"A05");
		keyboard_entry->shift=2;
		keyboard_entry->finger=3;
		break;
	case '6':strcpy(keyboard_entry->keyname,"A06");
		keyboard_entry->shift=0;
		keyboard_entry->finger=6;
		break;
	case '^':strcpy(keyboard_entry->keyname,"A06");
		keyboard_entry->shift=1;
		keyboard_entry->finger=6;
		break;
	case '7':strcpy(keyboard_entry->keyname,"A07");
		keyboard_entry->shift=0;
		keyboard_entry->finger=6;
		break;
	case '&':strcpy(keyboard_entry->keyname,"A07");
		keyboard_entry->shift=1;
		keyboard_entry->finger=6;
		break;
	case '8':strcpy(keyboard_entry->keyname,"A08");
		keyboard_entry->shift=0;
		keyboard_entry->finger=7;
		break;
	case '*':strcpy(keyboard_entry->keyname,"A08");
		keyboard_entry->shift=1;
		keyboard_entry->finger=7;
		break;
	case '9':strcpy(keyboard_entry->keyname,"A09");
		keyboard_entry->shift=0;
		keyboard_entry->finger=8;
		break;
	case '(':strcpy(keyboard_entry->keyname,"A09");
		keyboard_entry->shift=1;
		keyboard_entry->finger=8;
		break;
	case '0':strcpy(keyboard_entry->keyname,"A10");
		keyboard_entry->shift=0;
		keyboard_entry->finger=9;
		break;
	case ')':strcpy(keyboard_entry->keyname,"A10");
		keyboard_entry->shift=1;
		keyboard_entry->finger=9;
		break;
	case '-':strcpy(keyboard_entry->keyname,"A11");
		keyboard_entry->shift=0;
		keyboard_entry->finger=9;
		break;
	case '_':strcpy(keyboard_entry->keyname,"A11");
		keyboard_entry->shift=1;
		keyboard_entry->finger=9;
		break;
	case '=':strcpy(keyboard_entry->keyname,"A12");
		keyboard_entry->shift=0;
		keyboard_entry->finger=9;
		break;
	case '+':strcpy(keyboard_entry->keyname,"A12");
		keyboard_entry->shift=1;
		keyboard_entry->finger=9;
		break;
	case '\\':strcpy(keyboard_entry->keyname,"A13");
		keyboard_entry->shift=0;
		keyboard_entry->finger=9;
		break;
	case '|':strcpy(keyboard_entry->keyname,"A13");
		keyboard_entry->shift=1;
		keyboard_entry->finger=9;
		break;
	case 'q':strcpy(keyboard_entry->keyname,"B01");
		keyboard_entry->shift=0;
		keyboard_entry->finger=0;
		break;
	case 'Q':strcpy(keyboard_entry->keyname,"B01");
		keyboard_entry->shift=2;
		keyboard_entry->finger=0;
		break;
	case 'w':strcpy(keyboard_entry->keyname,"B02");
		keyboard_entry->shift=0;
		keyboard_entry->finger=1;
		break;
	case 'W':strcpy(keyboard_entry->keyname,"B02");
		keyboard_entry->shift=2;
		keyboard_entry->finger=1;
		break;
	case 'e':strcpy(keyboard_entry->keyname,"B03");
		keyboard_entry->shift=0;
		keyboard_entry->finger=2;
		break;
	case 'E':strcpy(keyboard_entry->keyname,"B03");
		keyboard_entry->shift=2;
		keyboard_entry->finger=3;
		break;
	case 'r':strcpy(keyboard_entry->keyname,"B04");
		keyboard_entry->shift=0;
		keyboard_entry->finger=3;
		break;
	case 'R':strcpy(keyboard_entry->keyname,"B04");
		keyboard_entry->shift=2;
		keyboard_entry->finger=3;
		break;
	case 't':strcpy(keyboard_entry->keyname,"B05");
		keyboard_entry->shift=0;
		keyboard_entry->finger=3;
		break;
	case 'T':strcpy(keyboard_entry->keyname,"B05");
		keyboard_entry->shift=2;
		keyboard_entry->finger=3;
		break;
	case 'y':strcpy(keyboard_entry->keyname,"B06");
		keyboard_entry->shift=0;
		keyboard_entry->finger=6;
		break;
	case 'Y':strcpy(keyboard_entry->keyname,"B06");
		keyboard_entry->shift=1;
		keyboard_entry->finger=6;
		break;
	case 'u':strcpy(keyboard_entry->keyname,"B07");
		keyboard_entry->shift=0;
		keyboard_entry->finger=6;
		break;
	case 'U':strcpy(keyboard_entry->keyname,"B07");
		keyboard_entry->shift=1;
		keyboard_entry->finger=6;
		break;
	case 'i':strcpy(keyboard_entry->keyname,"B08");
		keyboard_entry->shift=0;
		keyboard_entry->finger=7;
		break;
	case 'I':strcpy(keyboard_entry->keyname,"B08");
		keyboard_entry->shift=1;
		keyboard_entry->finger=7;
		break;
	case 'o':strcpy(keyboard_entry->keyname,"B09");
		keyboard_entry->shift=0;
		keyboard_entry->finger=8;
		break;
	case 'O':strcpy(keyboard_entry->keyname,"B09");
		keyboard_entry->shift=1;
		keyboard_entry->finger=8;
		break;
	case 'p':strcpy(keyboard_entry->keyname,"B10");
		keyboard_entry->shift=0;
		keyboard_entry->finger=9;
		break;
	case 'P':strcpy(keyboard_entry->keyname,"B10");
		keyboard_entry->shift=1;
		keyboard_entry->finger=9;
		break;
	case '[':strcpy(keyboard_entry->keyname,"B11");
		keyboard_entry->shift=0;
		keyboard_entry->finger=9;
		break;
	case '{':strcpy(keyboard_entry->keyname,"B11");
		keyboard_entry->shift=1;
		keyboard_entry->finger=9;
		break;
	case ']':strcpy(keyboard_entry->keyname,"B12");
		keyboard_entry->shift=0;
		keyboard_entry->finger=9;
		break;
	case '}':strcpy(keyboard_entry->keyname,"B12");
		keyboard_entry->shift=1;
		keyboard_entry->finger=9;
		break;
	case 'a':strcpy(keyboard_entry->keyname,"C01");
		keyboard_entry->shift=0;
		keyboard_entry->finger=0;
		break;
	case 'A':strcpy(keyboard_entry->keyname,"C01");
		keyboard_entry->shift=2;
		keyboard_entry->finger=0;
		break;
	case 's':strcpy(keyboard_entry->keyname,"C02");
		keyboard_entry->shift=0;
		keyboard_entry->finger=1;
		break;
	case 'S':strcpy(keyboard_entry->keyname,"C02");
		keyboard_entry->shift=2;
		keyboard_entry->finger=1;
		break;
	case 'd':strcpy(keyboard_entry->keyname,"C03");
		keyboard_entry->shift=0;
		keyboard_entry->finger=2;
		break;
	case 'D':strcpy(keyboard_entry->keyname,"C03");
		keyboard_entry->shift=2;
		keyboard_entry->finger=2;
		break;
	case 'f':strcpy(keyboard_entry->keyname,"C04");
		keyboard_entry->shift=0;
		keyboard_entry->finger=3;
		break;
	case 'F':strcpy(keyboard_entry->keyname,"C04");
		keyboard_entry->shift=2;
		keyboard_entry->finger=3;
		break;
	case 'g':strcpy(keyboard_entry->keyname,"C05");
		keyboard_entry->shift=0;
		keyboard_entry->finger=3;
		break;
	case 'G':strcpy(keyboard_entry->keyname,"C05");
		keyboard_entry->shift=1;
		keyboard_entry->finger=3;
		break;
	case 'h':strcpy(keyboard_entry->keyname,"C06");
		keyboard_entry->shift=0;
		keyboard_entry->finger=6;
		break;
	case 'H':strcpy(keyboard_entry->keyname,"C06");
		keyboard_entry->shift=1;
		keyboard_entry->finger=6;
		break;
	case 'j':strcpy(keyboard_entry->keyname,"C07");
		keyboard_entry->shift=0;
		keyboard_entry->finger=6;
		break;
	case 'J':strcpy(keyboard_entry->keyname,"C07");
		keyboard_entry->shift=1;
		keyboard_entry->finger=6;
		break;
	case 'k':strcpy(keyboard_entry->keyname,"C08");
		keyboard_entry->shift=0;
		keyboard_entry->finger=7;
		break;
	case 'K':strcpy(keyboard_entry->keyname,"C08");
		keyboard_entry->shift=1;
		keyboard_entry->finger=7;
		break;
	case 'l':strcpy(keyboard_entry->keyname,"C09");
		keyboard_entry->shift=0;
		keyboard_entry->finger=8;
		break;
	case 'L':strcpy(keyboard_entry->keyname,"C09");
		keyboard_entry->shift=1;
		keyboard_entry->finger=8;
		break;
	case ';':strcpy(keyboard_entry->keyname,"C10");
		keyboard_entry->shift=0;
		keyboard_entry->finger=9;
		break;
	case ':':strcpy(keyboard_entry->keyname,"C10");
		keyboard_entry->shift=1;
		keyboard_entry->finger=9;
		break;			
	case '\'':strcpy(keyboard_entry->keyname,"C11");
		keyboard_entry->shift=0;
		keyboard_entry->finger=9;
		break;
	case '"':strcpy(keyboard_entry->keyname,"C11");
		keyboard_entry->shift=1;
		keyboard_entry->finger=9;
		break;
1088
	case 'z':strcpy(keyboard_entry->keyname,"D02");
1089 1090 1091
		keyboard_entry->shift=0;
		keyboard_entry->finger=0;
		break;
1092
	case 'Z':strcpy(keyboard_entry->keyname,"D02");
1093 1094 1095
		keyboard_entry->shift=2;
		keyboard_entry->finger=0;
		break;
1096
	case 'x':strcpy(keyboard_entry->keyname,"D03");
1097 1098 1099
		keyboard_entry->shift=0;
		keyboard_entry->finger=1;
		break;
1100
	case 'X':strcpy(keyboard_entry->keyname,"D03");
1101 1102 1103
		keyboard_entry->shift=2;
		keyboard_entry->finger=1;
		break;
1104
	case 'c':strcpy(keyboard_entry->keyname,"D04");
1105 1106 1107
		keyboard_entry->shift=0;
		keyboard_entry->finger=2;
		break;
1108
	case 'C':strcpy(keyboard_entry->keyname,"D04");
1109 1110 1111
		keyboard_entry->shift=2;
		keyboard_entry->finger=2;
		break;
1112
	case 'v':strcpy(keyboard_entry->keyname,"D05");
1113 1114 1115
		keyboard_entry->shift=0;
		keyboard_entry->finger=3;
		break;
1116
	case 'V':strcpy(keyboard_entry->keyname,"D05");
1117 1118 1119
		keyboard_entry->shift=2;
		keyboard_entry->finger=3;
		break;
1120
	case 'b':strcpy(keyboard_entry->keyname,"D06");
1121 1122 1123
		keyboard_entry->shift=0;
		keyboard_entry->finger=3;
		break;
1124
	case 'B':strcpy(keyboard_entry->keyname,"D06");
1125 1126 1127
		keyboard_entry->shift=2;
		keyboard_entry->finger=3;
		break;
1128
	case 'n':strcpy(keyboard_entry->keyname,"D07");
1129 1130 1131
		keyboard_entry->shift=0;
		keyboard_entry->finger=6;
		break;
1132
	case 'N':strcpy(keyboard_entry->keyname,"D07");
1133 1134 1135
		keyboard_entry->shift=1;
		keyboard_entry->finger=6;
		break;
1136
	case 'm':strcpy(keyboard_entry->keyname,"D08");
1137 1138 1139
		keyboard_entry->shift=0;
		keyboard_entry->finger=6;
		break;
1140
	case 'M':strcpy(keyboard_entry->keyname,"D08");
1141 1142 1143
		keyboard_entry->shift=1;
		keyboard_entry->finger=6;
		break;
1144
	case ',':strcpy(keyboard_entry->keyname,"D09");
1145 1146 1147
		keyboard_entry->shift=0;
		keyboard_entry->finger=7;
		break;
1148
	case '<':strcpy(keyboard_entry->keyname,"D09");
1149 1150 1151
		keyboard_entry->shift=1;
		keyboard_entry->finger=7;
		break;
1152
	case '.':strcpy(keyboard_entry->keyname,"D010");
1153 1154 1155
		keyboard_entry->shift=0;
		keyboard_entry->finger=8;
		break;
1156
	case '>':strcpy(keyboard_entry->keyname,"D010");
1157 1158 1159
		keyboard_entry->shift=1;
		keyboard_entry->finger=8;
		break;
1160
	case '/':strcpy(keyboard_entry->keyname,"D11");
1161 1162 1163
		keyboard_entry->shift=0;
		keyboard_entry->finger=9;
		break;
1164
	case '?':strcpy(keyboard_entry->keyname,"D11");
1165 1166 1167 1168 1169 1170 1171
		keyboard_entry->shift=1;
		keyboard_entry->finger=9;
		break;
	case ' ':strcpy(keyboard_entry->keyname,"E03");
		keyboard_entry->shift=0;
		keyboard_entry->finger=5;
		break;
1172
	// I think this is for when we call "keymap(-1, ...)"
1173
	default:strcpy(keyboard_entry->keyname,"None");
1174 1175
		keyboard_entry->shift = 0;
		keyboard_entry->finger = -1;
1176 1177
		break;
  }	
1178
  return 0;
1179 1180 1181 1182
}

void GenerateKeyboard(SDL_Surface* keyboard)
{
1183 1184 1185 1186 1187 1188
  SDL_Surface* tmp = NULL;
  SDL_Rect new;
  int i;
  int col;
  char row;
  int render = 1;
1189 1190 1191
  wchar_t t[2];
  char buf[8];
//  TTF_Font* smallfont = NULL;
1192 1193 1194 1195 1196 1197 1198 1199 1200

  DEBUGCODE { printf("Entering GenerateKeyboard\n"); }

  if(!keyboard)
  {
    DEBUGCODE { printf("Error - keyboard SDL_Surface ptr null\n"); }
    return;
  }

1201 1202 1203 1204 1205 1206
//   smallfont = LoadFont(settings.theme_font_name, 15);
//   if(!smallfont)
//   {
//     DEBUGCODE { printf("Error loading font\n"); }
//     return;
//   }
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

  /* to give us a null-terminated string: */
  t[1] = '\0';
  /* Draw the unshifted and shifted chars overlying the corresponding key */
  /* for every entry in keyboard_list[]:                                  */
  for(i = 0; i < num_chars_used; i++)
  {
    render = 1;
    new.x = 0;
    new.y = 0;
    new.w = 5;
    new.h = 5;

    t[0] = keyboard_list[i].unicode_value;
    sscanf(keyboard_list[i].keyname,"%c%d",&row,&col);

1223 1224
    /* NOTE these values have been tweaked to better center the */
    /* characters on the key images - DSB.                      */
1225 1226
    switch(row)
    {
1227 1228 1229
      case 'A': new.y += 6;   new.x += 12; break;
      case 'B': new.y += 39;  new.x += 23; break;
      case 'C': new.y += 69;  new.x += 33; break;
1230 1231
      case 'D': new.y += 100;  new.x += 21; break;
//      case 'D': new.y += 99;  new.x += 54; break;
1232
      case 'E': new.y += 129; break;
1233 1234 1235 1236 1237 1238
      default: render = 0; break;
    }

    if(!render)
      continue;

1239
    new.x += (int)(30.5 * col);
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250

    if(keyboard_list[i].shift > 0)
    {
      new.x -= 9;
      if(new.y < 9)
        new.y -= 5;
      else
        new.y -= 9;
    }

    DEBUGCODE { printf("Making %d : %C\n",i,keyboard_list[i].unicode_value); }
1251 1252 1253
    ConvertToUTF8(t, buf, 8);
    tmp = SimpleText(buf, 15, &black);
//    tmp = TTF_RenderUNICODE_Blended((TTF_Font*)smallfont, t, black);
1254 1255 1256 1257 1258 1259 1260 1261
    if(tmp == NULL)
    {
      fprintf(stderr, "Error Making %d : %C\n", i, keyboard_list[i].unicode_value);
    }
    else
      SDL_BlitSurface(tmp, NULL, keyboard, &new);
  }
  //Know this is safe - if NULL would have returned above:
1262
//  TTF_CloseFont(smallfont);
1263 1264

  DEBUGCODE { printf("Leaving GenerateKeyboard\n"); }
1265
}
1266 1267


1268
void updatekeylist(int key, char ch)
1269
{
1270 1271 1272
  keyboard_list[key].latin_char = ch;
  wchar_t wtmp = ch;
  map_keys(wtmp, &keyboard_list[key]);
1273
}
1274

1275 1276 1277 1278 1279

/* FIXME this is _always_ going to fail - everything in DATA_PREFIX   */
/* is supposed to be read-only as far as a regular user is concerned. */
/* Not sure why would want the program to be able to modify the       */
/* keyboard map, anyway - DSB.                                        */
1280 1281
void savekeyboard(void)
{
1282 1283 1284 1285
  char fn[FNLEN];
  FILE* fp = NULL;
  int i = 0;
  wchar_t uni;
1286

1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
  if(!settings.use_english)
    sprintf(fn , "%s/keyboard.lst", settings.theme_data_path);
  else
    sprintf(fn , "%s/keyboard.lst", settings.default_data_path);

  /* We aren't supposed to be able to modify DATA! */
  fp = fopen(fn,"w");
  if (fp == NULL)
  {
    LOG("savekeyboard() - could not open keyboard.lst for saving\n");
    return;
  }

  for(i = 0; i < num_chars_used; i++)
  {
    uni = (wchar_t)keyboard_list[i].unicode_value;

    if(keyboard_list[i].finger == -1)
    {
      fprintf(fp,"%C\n", uni);
    }
    else if(keyboard_list[i].latin_char == -1)
    {
      fprintf(fp,"%d|%C\n",keyboard_list[i].finger, uni);
    }
    else
    {
      fprintf(fp,"%d|%C|%c\n", keyboard_list[i].finger, uni, keyboard_list[i].latin_char);
    }
  }
  fclose(fp);
1318
}
1319 1320


1321 1322
/****************************************************************/

1323 1324


David Bruce's avatar
David Bruce committed
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
/****************************************************/
/*                                                  */
/*       Local ("private") functions:               */
/*                                                  */
/****************************************************/


/* Creates a list of distinct Unicode characters in */
/* word_list[][] (so the program knows what         */
/* needs to be rendered for the games)              */
static void gen_char_list(void)
{
  int i, j;
  i = j = 0;
  char_list[0] = '\0';

  while (word_list[i][0] != '\0' && i < MAX_NUM_WORDS) 
  {
    j = 0;

    while (word_list[i][j]!= '\0' && j < MAX_WORD_SIZE)
    {
      add_char(word_list[i][j]);
      j++;
    }

    i++;
  }

  DEBUGCODE
  {
    fprintf(stderr, "char_list = %S\n", char_list);
  }
}



void ResetCharList(void)
{
  char_list[0] = '\0';
}



/* Creates a list of distinct Unicode characters in       */
/* the argument string for subsequent rendering.          */
/* Like gen_char_list() but takes a string argument       */
/* instead of going through the currently selected        */
/* word list. Argument should be UTF-8                    */
/* Can be called multiple times on different strings      */
/* to accumulate entire repertoire - call ResetCharList() */
/* to start over                                          */
1377
void GenCharListFromString(const char* UTF8_str)
David Bruce's avatar
David Bruce committed
1378 1379 1380 1381
{
  int i = 0;
  wchar_t wchar_buf[MAX_UNICODES];

1382
  ConvertFromUTF8(wchar_buf, UTF8_str, MAX_UNICODES);
David Bruce's avatar
David Bruce committed
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434

  /* FNLEN is max length of phrase (I think) */
  while (wchar_buf[i] != '\0' && i < FNLEN) 
  {
    add_char(wchar_buf[i]);
    i++;
  }

  DEBUGCODE
  {
    fprintf(stderr, "char_list = %S\n", char_list);
  }
}



/* Checks to see if the argument is already in the list and adds    */
/* it if necessary.  Returns 1 if char added, 0 if already in list, */
/* -1 if list already up to maximum size:                           */
/* FIXME performance would be better with hashtable                 */
static int add_char(wchar_t uc)
{
  int i = 0;
  while ((char_list[i] != uc)
      && (char_list[i] != '\0')
      && (i < MAX_UNICODES - 1))          //Because 1 need for null terminator
  {
    i++;
  }

  /* unicode already in list: */
  if (char_list[i] == uc)
  {
    DEBUGCODE{ fprintf(stderr,
                       "Unicode value: %d\tcharacter %lc already in list\n",
                        uc, uc);}
    return 0;
  }

  if (char_list[i] == '\0')
  {
    DEBUGCODE{ fprintf(stderr, "Adding unicode value: %d\tcharacter %lc\n", uc, uc);}
    char_list[i] = uc;
    char_list[i + 1] = '\0';
    return 1;
  }

  if (i == MAX_UNICODES - 1)            //Because 1 need for null terminator
  {
    LOG ("Unable to add unicode - list at max capacity");
    return -1;
  }
1435 1436
  // We never want to get here...
  return -1;
David Bruce's avatar
David Bruce committed
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
}



static void clear_keyboard(void)
{
  int i = 0;
  for (i = 0; i < MAX_UNICODES; i++)
  {
    keyboard_list[i].unicode_value = 0;
    keyboard_list[i].finger = -1;
  }
}