indent onscreen_keyboard.c onscreen_keyboard.h

This commit is contained in:
Bill Kendrick 2017-10-15 10:59:50 -07:00
parent e283cdb473
commit e9447f8c3b
2 changed files with 1095 additions and 1094 deletions

View file

@ -5,30 +5,31 @@
//#define DEBUG_OSK_COMPOSEMAP //#define DEBUG_OSK_COMPOSEMAP
static SDL_Color def_bgcolor = {255, 255, 255, 255}; static SDL_Color def_bgcolor = { 255, 255, 255, 255 };
static SDL_Color def_fgcolor = {0, 0, 0, 0}; static SDL_Color def_fgcolor = { 0, 0, 0, 0 };
static void load_hlayout(osk_layout *layout, char * layout_name); static void load_hlayout(osk_layout * layout, char *layout_name);
static void load_keymap(osk_layout *layout, char * keymap_name); static void load_keymap(osk_layout * layout, char *keymap_name);
static void load_composemap(osk_layout *layout, char * composemap_name); static void load_composemap(osk_layout * layout, char *composemap_name);
static int is_blank_or_comment(char *line); static int is_blank_or_comment(char *line);
/* static int isw_blank_or_comment(wchar_t *line); */ /* static int isw_blank_or_comment(wchar_t *line); */
static void keybd_prepare(on_screen_keyboard *keyboard); static void keybd_prepare(on_screen_keyboard * keyboard);
static void draw_key(osk_key key, on_screen_keyboard * keyboard, int hot); static void draw_key(osk_key key, on_screen_keyboard * keyboard, int hot);
static void label_key(osk_key key, on_screen_keyboard *keyboard); static void label_key(osk_key key, on_screen_keyboard * keyboard);
static void draw_keyboard(on_screen_keyboard *keyboard); static void draw_keyboard(on_screen_keyboard * keyboard);
static osk_key * find_key(on_screen_keyboard * keyboard, int x, int y); static osk_key *find_key(on_screen_keyboard * keyboard, int x, int y);
static void set_key(osk_key *orig, osk_key *dest, int firsttime); static void set_key(osk_key * orig, osk_key * dest, int firsttime);
static void load_keysymdefs(osk_layout * layout, char * keysymdefs_name); static void load_keysymdefs(osk_layout * layout, char *keysymdefs_name);
static struct osk_layout *load_layout(on_screen_keyboard *keyboard, char *layout_name); static struct osk_layout *load_layout(on_screen_keyboard * keyboard, char *layout_name);
#ifdef DEBUG_OSK_COMPOSEMAP #ifdef DEBUG_OSK_COMPOSEMAP
static void print_composemap(osk_composenode *composemap, char * sp); static void print_composemap(osk_composenode * composemap, char *sp);
#endif #endif
#ifdef WIN32 #ifdef WIN32
@ -36,9 +37,9 @@ static void print_composemap(osk_composenode *composemap, char * sp);
#define wcstok(line, delim, pointer) wcstok(line, delim) #define wcstok(line, delim, pointer) wcstok(line, delim)
#define strtok_r(line, delim, pointer) strtok(line, delim) #define strtok_r(line, delim, pointer) strtok(line, delim)
static void mtw(wchar_t * wtok, char * tok); static void mtw(wchar_t * wtok, char *tok);
static void mtw(wchar_t * wtok, char * tok) static void mtw(wchar_t * wtok, char *tok)
{ {
/* workaround using iconv to get a functionallity somewhat approximate as mbstowcs() */ /* workaround using iconv to get a functionallity somewhat approximate as mbstowcs() */
Uint16 *ui16; Uint16 *ui16;
@ -50,23 +51,28 @@ static void mtw(wchar_t * wtok, char * tok)
in = 250; in = 250;
out = 250; out = 250;
ui16 = malloc(sizeof(Uint16) * 255); ui16 = malloc(sizeof(Uint16) * 255);
wrptr = (char *) ui16; wrptr = (char *)ui16;
trans = iconv_open("WCHAR_T", "UTF-8"); trans = iconv_open("WCHAR_T", "UTF-8");
iconv(trans, (const char **) &tok, &in, &wrptr, &out); iconv(trans, (const char **)&tok, &in, &wrptr, &out);
*((wchar_t *) wrptr) = L'\0'; *((wchar_t *) wrptr) = L'\0';
swprintf(wtok, L"%ls", ui16); swprintf(wtok, L"%ls", ui16);
free(ui16); free(ui16);
iconv_close(trans); iconv_close(trans);
} }
#define mbstowcs(wtok, tok, size) mtw(wtok, tok) #define mbstowcs(wtok, tok, size) mtw(wtok, tok)
#endif #endif
struct osk_keyboard *osk_create(char *layout_name, SDL_Surface *canvas, SDL_Surface *button_up, SDL_Surface *button_down, SDL_Surface *button_off, SDL_Surface *button_nav, SDL_Surface *button_hold, SDL_Surface *oskdel, SDL_Surface *osktab, SDL_Surface *oskenter, SDL_Surface *oskcapslock, SDL_Surface *oskshift, int disable_change) struct osk_keyboard *osk_create(char *layout_name, SDL_Surface * canvas, SDL_Surface * button_up,
SDL_Surface * button_down, SDL_Surface * button_off, SDL_Surface * button_nav,
SDL_Surface * button_hold, SDL_Surface * oskdel, SDL_Surface * osktab,
SDL_Surface * oskenter, SDL_Surface * oskcapslock, SDL_Surface * oskshift,
int disable_change)
{ {
SDL_Surface *surface; SDL_Surface *surface;
osk_layout *layout; osk_layout *layout;
on_screen_keyboard * keyboard; on_screen_keyboard *keyboard;
keyboard = malloc(sizeof(on_screen_keyboard)); keyboard = malloc(sizeof(on_screen_keyboard));
@ -94,9 +100,7 @@ struct osk_keyboard *osk_create(char *layout_name, SDL_Surface *canvas, SDL_Surf
layout->width * button_up->w, layout->width * button_up->w,
layout->height * button_up->h, layout->height * button_up->h,
canvas->format->BitsPerPixel, canvas->format->BitsPerPixel,
canvas->format->Rmask, canvas->format->Rmask, canvas->format->Gmask, canvas->format->Bmask, 0);
canvas->format->Gmask,
canvas->format->Bmask, 0);
if (!surface) if (!surface)
{ {
printf("Error creating the onscreen keyboard surface\n"); printf("Error creating the onscreen keyboard surface\n");
@ -136,7 +140,9 @@ struct osk_keyboard *osk_create(char *layout_name, SDL_Surface *canvas, SDL_Surf
keyboard->kmdf.dead3 = NULL; keyboard->kmdf.dead3 = NULL;
keyboard->kmdf.dead4 = NULL; keyboard->kmdf.dead4 = NULL;
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, keyboard->layout->bgcolor.r, keyboard->layout->bgcolor.g, keyboard->layout->bgcolor.b)); SDL_FillRect(surface, NULL,
SDL_MapRGB(surface->format, keyboard->layout->bgcolor.r, keyboard->layout->bgcolor.g,
keyboard->layout->bgcolor.b));
keybd_prepare(keyboard); keybd_prepare(keyboard);
@ -144,12 +150,12 @@ struct osk_keyboard *osk_create(char *layout_name, SDL_Surface *canvas, SDL_Surf
return keyboard; return keyboard;
} }
static struct osk_layout *load_layout(on_screen_keyboard *keyboard, char *layout_name) static struct osk_layout *load_layout(on_screen_keyboard * keyboard, char *layout_name)
{ {
FILE *fi; FILE *fi;
int hlayout_loaded; int hlayout_loaded;
char * line; char *line;
char * filename; char *filename;
char *key, *value; char *key, *value;
osk_layout *layout; osk_layout *layout;
@ -260,7 +266,7 @@ static struct osk_layout *load_layout(on_screen_keyboard *keyboard, char *layout
/* A hlayout contains the definitions of the keyboard as seen in the screen. /* A hlayout contains the definitions of the keyboard as seen in the screen.
Things like the number of rows of the keyboard, the font used to render the keys, Things like the number of rows of the keyboard, the font used to render the keys,
the width of the keys, and a code that matches each key like in real hardware keyboards */ the width of the keys, and a code that matches each key like in real hardware keyboards */
void load_hlayout(osk_layout *layout, char * hlayout_name) void load_hlayout(osk_layout * layout, char *hlayout_name)
{ {
int width, height; int width, height;
int key_number, line_number; int key_number, line_number;
@ -273,7 +279,7 @@ void load_hlayout(osk_layout *layout, char * hlayout_name)
char *line; char *line;
char *key, *fontpath; char *key, *fontpath;
char *plain_label, *top_label, *altgr_label, *shift_altgr_label; char *plain_label, *top_label, *altgr_label, *shift_altgr_label;
FILE * fi; FILE *fi;
key_number = line_number = 0; key_number = line_number = 0;
width = height = 0; width = height = 0;
@ -317,24 +323,24 @@ void load_hlayout(osk_layout *layout, char * hlayout_name)
while (!feof(fi)) while (!feof(fi))
{ {
if(width && height && !allocated) if (width && height && !allocated)
{ {
layout->keys = malloc(height * sizeof(osk_key *)); layout->keys = malloc(height * sizeof(osk_key *));
layout->keys[0] = malloc(width * sizeof(osk_key )); layout->keys[0] = malloc(width * sizeof(osk_key));
for (i = 0; i< width; i++) for (i = 0; i < width; i++)
{ {
layout->keys[0][i].width = 0; layout->keys[0][i].width = 0;
layout->keys[0][i].plain_label = NULL; layout->keys[0][i].plain_label = NULL;
layout->keys[ line_number][i].top_label=NULL; layout->keys[line_number][i].top_label = NULL;
layout->keys[ line_number][i].altgr_label=NULL; layout->keys[line_number][i].altgr_label = NULL;
layout->keys[ line_number][i].shift_altgr_label=NULL; layout->keys[line_number][i].shift_altgr_label = NULL;
} }
layout->width = width; layout->width = width;
layout->height = height; layout->height = height;
#ifdef DEBUG #ifdef DEBUG
printf("w %i, h %i\n" , layout->width, layout->height); printf("w %i, h %i\n", layout->width, layout->height);
#endif #endif
allocated = 1; allocated = 1;
} }
@ -365,7 +371,7 @@ void load_hlayout(osk_layout *layout, char * hlayout_name)
printf("linefont %s\n", line); printf("linefont %s\n", line);
#endif #endif
sscanf(line, "%s %i %i %i", key, &r, &g, &b); sscanf(line, "%s %i %i %i", key, &r, &g, &b);
if (r > 0 && r< 256 && g > 0 && g< 256 && b > 0 && b< 256) if (r > 0 && r < 256 && g > 0 && g < 256 && b > 0 && b < 256)
{ {
layout->fgcolor.r = r; layout->fgcolor.r = r;
layout->fgcolor.g = g; layout->fgcolor.g = g;
@ -379,7 +385,7 @@ void load_hlayout(osk_layout *layout, char * hlayout_name)
printf("linefont %s\n", line); printf("linefont %s\n", line);
#endif #endif
sscanf(line, "%s %i %i %i", key, &r, &g, &b); sscanf(line, "%s %i %i %i", key, &r, &g, &b);
if (r > 0 && r< 256 && g > 0 && g< 256 && b > 0 && b< 256) if (r > 0 && r < 256 && g > 0 && g < 256 && b > 0 && b < 256)
{ {
layout->bgcolor.r = r; layout->bgcolor.r = r;
layout->bgcolor.g = g; layout->bgcolor.g = g;
@ -389,16 +395,16 @@ void load_hlayout(osk_layout *layout, char * hlayout_name)
} }
else if (strncmp(line, "NEWLINE", 7) == 0) else if (strncmp(line, "NEWLINE", 7) == 0)
{ {
line_number ++; line_number++;
key_number = 0; key_number = 0;
layout->keys[line_number] = malloc(width * sizeof(osk_key)); layout->keys[line_number] = malloc(width * sizeof(osk_key));
for (i = 0; i< width; i++) for (i = 0; i < width; i++)
{ {
layout->keys[line_number][i].width = 0; layout->keys[line_number][i].width = 0;
layout->keys[ line_number][i].plain_label=NULL; layout->keys[line_number][i].plain_label = NULL;
layout->keys[ line_number][i].top_label=NULL; layout->keys[line_number][i].top_label = NULL;
layout->keys[ line_number][i].altgr_label=NULL; layout->keys[line_number][i].altgr_label = NULL;
layout->keys[ line_number][i].shift_altgr_label=NULL; layout->keys[line_number][i].shift_altgr_label = NULL;
} }
} }
else if (width && height && allocated && strncmp(line, "KEY ", 4) == 0 && key_number < width) else if (width && height && allocated && strncmp(line, "KEY ", 4) == 0 && key_number < width)
@ -412,22 +418,17 @@ void load_hlayout(osk_layout *layout, char * hlayout_name)
"%s %i %i.%i %s %s %s %s %i", "%s %i %i.%i %s %s %s %s %i",
key, key,
&keycode, &keycode,
&key_width, &key_width, &key_width_decimal, plain_label, top_label, altgr_label, shift_altgr_label, &shiftcaps);
&key_width_decimal,
plain_label,
top_label,
altgr_label,
shift_altgr_label,
&shiftcaps);
layout->keys[line_number][key_number].keycode = keycode; layout->keys[line_number][key_number].keycode = keycode;
layout->keys[line_number][key_number].width = (float)0.1 * key_width_decimal + key_width; layout->keys[line_number][key_number].width = (float)0.1 *key_width_decimal + key_width;
layout->keys[line_number][key_number].plain_label = plain_label; layout->keys[line_number][key_number].plain_label = plain_label;
layout->keys[line_number][key_number].top_label = top_label; layout->keys[line_number][key_number].top_label = top_label;
layout->keys[line_number][key_number].altgr_label = altgr_label; layout->keys[line_number][key_number].altgr_label = altgr_label;
layout->keys[line_number][key_number].shift_altgr_label = shift_altgr_label; layout->keys[line_number][key_number].shift_altgr_label = shift_altgr_label;
layout->keys[line_number][key_number].shiftcaps = shiftcaps; layout->keys[line_number][key_number].shiftcaps = shiftcaps;
layout->keys[line_number][key_number].stick = 0; layout->keys[line_number][key_number].stick = 0;
key_number ++; key_number++;
} }
} }
@ -461,13 +462,13 @@ void load_hlayout(osk_layout *layout, char * hlayout_name)
/* A keymap contains the keysyms (X keysym mnemonics) associated to each keycode in the hlayout.*/ /* A keymap contains the keysyms (X keysym mnemonics) associated to each keycode in the hlayout.*/
void load_keymap(osk_layout *layout, char * keymap_name) void load_keymap(osk_layout * layout, char *keymap_name)
{ {
int i, keycode, readed; int i, keycode, readed;
char *filename; char *filename;
char *ksname1, *ksname2, *ksname3, *ksname4; char *ksname1, *ksname2, *ksname3, *ksname4;
char *line; char *line;
FILE * fi; FILE *fi;
filename = malloc(sizeof(char) * 255); filename = malloc(sizeof(char) * 255);
@ -493,7 +494,7 @@ void load_keymap(osk_layout *layout, char * keymap_name)
line = malloc(sizeof(char) * 1024); line = malloc(sizeof(char) * 1024);
layout->keymap = malloc(256 * sizeof(osk_keymap)); layout->keymap = malloc(256 * sizeof(osk_keymap));
for (i = 0;i < 256; i++) for (i = 0; i < 256; i++)
{ {
layout->keymap[i].plain = NULL; layout->keymap[i].plain = NULL;
layout->keymap[i].caps = NULL; layout->keymap[i].caps = NULL;
@ -520,8 +521,7 @@ void load_keymap(osk_layout *layout, char * keymap_name)
/* FIXME: Why is the us-intl keymap duplicating the two first entries of every keycode? */ /* FIXME: Why is the us-intl keymap duplicating the two first entries of every keycode? */
/* And why is the arabic keymap using the 5th and 6th entries as plain/shifted keys? */ /* And why is the arabic keymap using the 5th and 6th entries as plain/shifted keys? */
readed = sscanf(line, "keycode %i = %s %s %s %s", &keycode, readed = sscanf(line, "keycode %i = %s %s %s %s", &keycode, ksname1, ksname2, ksname3, ksname4);
ksname1, ksname2, ksname3, ksname4);
if (readed == 5 && keycode > 8 && keycode < 256) if (readed == 5 && keycode > 8 && keycode < 256)
{ {
@ -561,7 +561,7 @@ void load_keymap(osk_layout *layout, char * keymap_name)
} }
/* Scans a line of keysyms and result and classifies them. */ /* Scans a line of keysyms and result and classifies them. */
static void gettokens(char * line, char * delim, char ** pointer, osk_composenode *composenode, osk_layout *layout) static void gettokens(char *line, char *delim, char **pointer, osk_composenode * composenode, osk_layout * layout)
{ {
int i; int i;
char *tok; char *tok;
@ -572,7 +572,7 @@ static void gettokens(char * line, char * delim, char ** pointer, osk_composenod
tok = strdup(strtok_r(line, delim, pointer)); tok = strdup(strtok_r(line, delim, pointer));
if(!tok) if (!tok)
return; return;
if (tok[0] == ':') /* End of precompose keysyms, next will be the result in UTF-8. */ if (tok[0] == ':') /* End of precompose keysyms, next will be the result in UTF-8. */
@ -614,7 +614,7 @@ static void gettokens(char * line, char * delim, char ** pointer, osk_composenod
for (i = 0; i < composenode->size; i++) for (i = 0; i < composenode->size; i++)
{ {
mbstowcs(wtok, tok, 255); mbstowcs(wtok, tok, 255);
if(wcscmp(composenode->childs[i]->keysym, wtok) == 0) if (wcscmp(composenode->childs[i]->keysym, wtok) == 0)
{ {
/* printf("Size %d, keysym %ls =>", composenode->size, composenode->childs[i]->keysym); */ /* printf("Size %d, keysym %ls =>", composenode->size, composenode->childs[i]->keysym); */
@ -628,11 +628,11 @@ static void gettokens(char * line, char * delim, char ** pointer, osk_composenod
} }
composenode->size = composenode->size + 1; composenode->size = composenode->size + 1;
composenode->childs = realloc(composenode->childs,composenode->size * sizeof(osk_composenode *)); composenode->childs = realloc(composenode->childs, composenode->size * sizeof(osk_composenode *));
mbstowcs(wtok, tok, 255); mbstowcs(wtok, tok, 255);
auxnode = malloc(sizeof(osk_composenode)); auxnode = malloc(sizeof(osk_composenode));
composenode->childs[composenode->size - 1] = auxnode;//malloc(sizeof(osk_composenode)); composenode->childs[composenode->size - 1] = auxnode; //malloc(sizeof(osk_composenode));
composenode->childs[composenode->size - 1]->keysym = wtok; composenode->childs[composenode->size - 1]->keysym = wtok;
composenode->childs[composenode->size - 1]->result = NULL; composenode->childs[composenode->size - 1]->result = NULL;
composenode->childs[composenode->size - 1]->size = 0; composenode->childs[composenode->size - 1]->size = 0;
@ -649,12 +649,12 @@ static void gettokens(char * line, char * delim, char ** pointer, osk_composenod
/* A compose map contains the sequences of keysyms (X keysym mnemonics) needed to generate another keysym. /* A compose map contains the sequences of keysyms (X keysym mnemonics) needed to generate another keysym.
The last in the sequence is the result, the others will be searched in the order they appear. The last in the sequence is the result, the others will be searched in the order they appear.
They will be classified in a multiway tree.*/ They will be classified in a multiway tree.*/
static void load_composemap(osk_layout *layout, char * composemap_name) static void load_composemap(osk_layout * layout, char *composemap_name)
{ {
char *filename; char *filename;
char **pointer; char **pointer;
char *line; char *line;
FILE * fi; FILE *fi;
pointer = malloc(sizeof(wchar_t *)); pointer = malloc(sizeof(wchar_t *));
filename = malloc(sizeof(char) * 255); filename = malloc(sizeof(char) * 255);
@ -692,7 +692,7 @@ static void load_composemap(osk_layout *layout, char * composemap_name)
if (is_blank_or_comment(line)) if (is_blank_or_comment(line))
continue; continue;
gettokens(line, (char *) ">< \t", pointer, layout->composemap, layout); gettokens(line, (char *)">< \t", pointer, layout->composemap, layout);
} }
fclose(fi); fclose(fi);
@ -704,10 +704,10 @@ static void load_composemap(osk_layout *layout, char * composemap_name)
} }
#ifdef DEBUG_OSK_COMPOSEMAP #ifdef DEBUG_OSK_COMPOSEMAP
static void print_composemap(osk_composenode *composemap, char * sp) static void print_composemap(osk_composenode * composemap, char *sp)
{ {
int i; int i;
char * space; char *space;
space = malloc(sizeof(char) * 255); space = malloc(sizeof(char) * 255);
@ -757,12 +757,12 @@ static void print_composemap(osk_composenode *composemap, char * sp)
/* This parses the contents of keysymdef.h from the source of xorg. /* This parses the contents of keysymdef.h from the source of xorg.
Therefore, if somebody wants to provide custom keysymdefs, he has to follow its syntax. */ Therefore, if somebody wants to provide custom keysymdefs, he has to follow its syntax. */
static void load_keysymdefs(osk_layout *layout, char * keysymdefs_name) static void load_keysymdefs(osk_layout * layout, char *keysymdefs_name)
{ {
int i; int i;
char *filename; char *filename;
char *line; char *line;
FILE * fi; FILE *fi;
filename = malloc(sizeof(char) * 255); filename = malloc(sizeof(char) * 255);
@ -788,7 +788,7 @@ static void load_keysymdefs(osk_layout *layout, char * keysymdefs_name)
layout->keysymdefs = malloc(sizeof(keysymdefs)); layout->keysymdefs = malloc(sizeof(keysymdefs));
layout->keysymdefs[0].unicode = 0; layout->keysymdefs[0].unicode = 0;
i = 0; i = 0;
line = malloc(1024*sizeof(wchar_t)); line = malloc(1024 * sizeof(wchar_t));
while (!feof(fi)) while (!feof(fi))
{ {
@ -803,9 +803,7 @@ static void load_keysymdefs(osk_layout *layout, char * keysymdefs_name)
layout->keysymdefs[i].unicode = 0; layout->keysymdefs[i].unicode = 0;
layout->keysymdefs[i].mnemo = malloc(sizeof(char) * 128); layout->keysymdefs[i].mnemo = malloc(sizeof(char) * 128);
sscanf(line, "#define XK_%s %x /* U+%x", sscanf(line, "#define XK_%s %x /* U+%x",
layout->keysymdefs[i].mnemo, layout->keysymdefs[i].mnemo, &layout->keysymdefs[i].keysym, &layout->keysymdefs[i].unicode);
&layout->keysymdefs[i].keysym,
&layout->keysymdefs[i].unicode);
i++; i++;
} }
@ -826,26 +824,26 @@ static void load_keysymdefs(osk_layout *layout, char * keysymdefs_name)
/* } */ /* } */
/* Returns the x keysym corresponding to a mnemonic string */ /* Returns the x keysym corresponding to a mnemonic string */
static int mnemo2keysym(char * mnemo, on_screen_keyboard * keyboard) static int mnemo2keysym(char *mnemo, on_screen_keyboard * keyboard)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < keyboard->layout->sizeofkeysymdefs; i++) for (i = 0; i < keyboard->layout->sizeofkeysymdefs; i++)
{ {
if (strcmp(mnemo , keyboard->layout->keysymdefs[i].mnemo) == 0) if (strcmp(mnemo, keyboard->layout->keysymdefs[i].mnemo) == 0)
return(keyboard->layout->keysymdefs[i].keysym); return (keyboard->layout->keysymdefs[i].keysym);
} }
i = 0; i = 0;
/* Perhaps the mnemo is in UXXXX format? */ /* Perhaps the mnemo is in UXXXX format? */
if(sscanf(mnemo, "U%x", &i)) if (sscanf(mnemo, "U%x", &i))
return(i | 0x01000000); return (i | 0x01000000);
/* Or maybe mnemo is already a keysym? */ /* Or maybe mnemo is already a keysym? */
if (sscanf(mnemo, "0x%x", &i)) if (sscanf(mnemo, "0x%x", &i))
return(i); return (i);
return(0); return (0);
} }
/* Returns the unicode value of a x keysym if any, otherwise returns 0 */ /* Returns the unicode value of a x keysym if any, otherwise returns 0 */
@ -862,8 +860,7 @@ static int keysym2unicode(int keysym, on_screen_keyboard * keyboard)
* This software is in the public domain. Share and enjoy! * This software is in the public domain. Share and enjoy!
*/ */
/* first check for Latin-1 characters (1:1 mapping) */ /* first check for Latin-1 characters (1:1 mapping) */
if ((keysym >= 0x0020 && keysym <= 0x007e) || if ((keysym >= 0x0020 && keysym <= 0x007e) || (keysym >= 0x00a0 && keysym <= 0x00ff))
(keysym >= 0x00a0 && keysym <= 0x00ff))
return keysym; return keysym;
/* also check for directly encoded 24-bit UCS characters */ /* also check for directly encoded 24-bit UCS characters */
@ -874,9 +871,9 @@ static int keysym2unicode(int keysym, on_screen_keyboard * keyboard)
for (i = 0; i < keyboard->layout->sizeofkeysymdefs; i++) for (i = 0; i < keyboard->layout->sizeofkeysymdefs; i++)
if (keysym == keyboard->layout->keysymdefs[i].keysym) if (keysym == keyboard->layout->keysymdefs[i].keysym)
return(keyboard->layout->keysymdefs[i].unicode); return (keyboard->layout->keysymdefs[i].unicode);
return(keysym); return (keysym);
} }
@ -886,7 +883,7 @@ static void get_composed_keysym(on_screen_keyboard * keyboard, osk_composenode *
int i; int i;
/* If there is not a compose table return the keysym */ /* If there is not a compose table return the keysym */
if (! composenode) if (!composenode)
{ {
if (keyboard->composed) if (keyboard->composed)
free(keyboard->composed); free(keyboard->composed);
@ -903,7 +900,7 @@ static void get_composed_keysym(on_screen_keyboard * keyboard, osk_composenode *
{ {
if (composenode->childs[i]->result) if (composenode->childs[i]->result)
{ {
if(keyboard->composed) if (keyboard->composed)
free(keyboard->composed); free(keyboard->composed);
keyboard->composed = wcsdup(composenode->childs[i]->result); keyboard->composed = wcsdup(composenode->childs[i]->result);
keyboard->composing = keyboard->layout->composemap; keyboard->composing = keyboard->layout->composemap;
@ -913,7 +910,7 @@ static void get_composed_keysym(on_screen_keyboard * keyboard, osk_composenode *
} }
else else
{ {
if(keyboard->composed) if (keyboard->composed)
free(keyboard->composed); free(keyboard->composed);
keyboard->composed = NULL; keyboard->composed = NULL;
keyboard->composing = composenode->childs[i]; keyboard->composing = composenode->childs[i];
@ -927,7 +924,7 @@ static void get_composed_keysym(on_screen_keyboard * keyboard, osk_composenode *
if (keyboard->layout->composemap == composenode) if (keyboard->layout->composemap == composenode)
{ {
if(keyboard->composed) if (keyboard->composed)
free(keyboard->composed); free(keyboard->composed);
keyboard->composed = wcsdup(keysym); keyboard->composed = wcsdup(keysym);
keyboard->composed_type = 0; keyboard->composed_type = 0;
@ -935,7 +932,7 @@ static void get_composed_keysym(on_screen_keyboard * keyboard, osk_composenode *
else /* reset */ else /* reset */
{ {
keyboard->composing = keyboard->layout->composemap; keyboard->composing = keyboard->layout->composemap;
if(keyboard->composed) if (keyboard->composed)
free(keyboard->composed); free(keyboard->composed);
keyboard->composed = NULL; keyboard->composed = NULL;
keyboard->composed_type = 0; keyboard->composed_type = 0;
@ -987,32 +984,33 @@ static int is_blank_or_comment(char *line)
/* Fixme: Is it safe to supose that if a font is loaded at one size, it will be loaded at any size? */ /* Fixme: Is it safe to supose that if a font is loaded at one size, it will be loaded at any size? */
/* Fixme: sizes should be dynamically adapted to the button size */ /* Fixme: sizes should be dynamically adapted to the button size */
/* Fixme: starting a layout with one font causes all other layouts be in that font */ /* Fixme: starting a layout with one font causes all other layouts be in that font */
static void keybd_prepare(on_screen_keyboard *keyboard) static void keybd_prepare(on_screen_keyboard * keyboard)
{ {
char * fontname; char *fontname;
fontname = malloc(sizeof(char) * 255); fontname = malloc(sizeof(char) * 255);
if (keyboard->osk_fonty == NULL) { if (keyboard->osk_fonty == NULL)
{
if (keyboard->layout->fontpath) if (keyboard->layout->fontpath)
{ {
/* First try if it is an absolute path */ /* First try if it is an absolute path */
keyboard->osk_fonty = TTF_OpenFont( keyboard->layout->fontpath, 12 ); keyboard->osk_fonty = TTF_OpenFont(keyboard->layout->fontpath, 12);
if (keyboard->osk_fonty == NULL) if (keyboard->osk_fonty == NULL)
{ {
/* Now trying if it is relative to DATA_PREFIX/fonts/ */ /* Now trying if it is relative to DATA_PREFIX/fonts/ */
snprintf(fontname, 255, "%s/fonts/%s", DATA_PREFIX, keyboard->layout->fontpath); snprintf(fontname, 255, "%s/fonts/%s", DATA_PREFIX, keyboard->layout->fontpath);
keyboard->osk_fonty = TTF_OpenFont( fontname, 12 ); keyboard->osk_fonty = TTF_OpenFont(fontname, 12);
if (keyboard->osk_fonty == NULL) if (keyboard->osk_fonty == NULL)
{ {
/* Perhaps it is relative to DATA_PREFIX only? */ /* Perhaps it is relative to DATA_PREFIX only? */
snprintf(fontname, 255, "%s/%s", DATA_PREFIX, keyboard->layout->fontpath); snprintf(fontname, 255, "%s/%s", DATA_PREFIX, keyboard->layout->fontpath);
keyboard->osk_fonty = TTF_OpenFont( fontname, 12 ); keyboard->osk_fonty = TTF_OpenFont(fontname, 12);
if (keyboard->osk_fonty == NULL) if (keyboard->osk_fonty == NULL)
{ {
/* Or to DATA_PREFIX/fonts/locale/ ? */ /* Or to DATA_PREFIX/fonts/locale/ ? */
snprintf(fontname, 255, "%s/fonts/locale/%s", DATA_PREFIX, keyboard->layout->fontpath); snprintf(fontname, 255, "%s/fonts/locale/%s", DATA_PREFIX, keyboard->layout->fontpath);
keyboard->osk_fonty = TTF_OpenFont( fontname, 12 ); keyboard->osk_fonty = TTF_OpenFont(fontname, 12);
} }
} }
} }
@ -1022,14 +1020,13 @@ static void keybd_prepare(on_screen_keyboard *keyboard)
{ {
/* Going with the default */ /* Going with the default */
sprintf(fontname, "%s/fonts/FreeSansBold.ttf", DATA_PREFIX); sprintf(fontname, "%s/fonts/FreeSansBold.ttf", DATA_PREFIX);
keyboard->osk_fonty = TTF_OpenFont( fontname, 12 ); keyboard->osk_fonty = TTF_OpenFont(fontname, 12);
} }
if (keyboard->osk_fonty == NULL) if (keyboard->osk_fonty == NULL)
{ {
fprintf(stderr, "\nError: Can't open the font!\n" fprintf(stderr, "\nError: Can't open the font!\n"
"The Simple DirectMedia Layer error that occurred was:\n" "The Simple DirectMedia Layer error that occurred was:\n" "%s\n\n", SDL_GetError());
"%s\n\n", SDL_GetError());
free(fontname); free(fontname);
exit(1); exit(1);
} }
@ -1039,14 +1036,14 @@ static void keybd_prepare(on_screen_keyboard *keyboard)
} }
static void apply_surface (int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip) static void apply_surface(int x, int y, SDL_Surface * source, SDL_Surface * destination, SDL_Rect * clip)
{ {
SDL_Rect offset; SDL_Rect offset;
offset.x = x; offset.x = x;
offset.y = y; offset.y = y;
SDL_BlitSurface( source, clip, destination, &offset ); SDL_BlitSurface(source, clip, destination, &offset);
} }
@ -1075,10 +1072,10 @@ static void apply_surface (int x, int y, SDL_Surface *source, SDL_Surface *desti
/* } */ /* } */
/* Stretches a button from the middle, keeping the extrems intact */ /* Stretches a button from the middle, keeping the extrems intact */
static SDL_Surface * stretch_surface(SDL_Surface * orig, int width) static SDL_Surface *stretch_surface(SDL_Surface * orig, int width)
{ {
int i; int i;
SDL_Surface * dest; SDL_Surface *dest;
SDL_Rect rect; SDL_Rect rect;
SDL_Rect orig_rect; SDL_Rect orig_rect;
@ -1091,9 +1088,7 @@ static SDL_Surface * stretch_surface(SDL_Surface * orig, int width)
width, width,
orig->h, orig->h,
orig->format->BitsPerPixel, orig->format->BitsPerPixel,
orig->format->Rmask, orig->format->Rmask, orig->format->Gmask, orig->format->Bmask, 0);
orig->format->Gmask,
orig->format->Bmask, 0);
SDL_BlitSurface(orig, NULL, dest, NULL); SDL_BlitSurface(orig, NULL, dest, NULL);
rect.y = 0; rect.y = 0;
@ -1134,11 +1129,12 @@ static SDL_Surface * stretch_surface(SDL_Surface * orig, int width)
} }
/* Draws the keyboard surface */ /* Draws the keyboard surface */
static void draw_keyboard(on_screen_keyboard *keyboard) static void draw_keyboard(on_screen_keyboard * keyboard)
{ {
int i, j; int i, j;
int key_height, accumulated_width, accumulated_height; int key_height, accumulated_width, accumulated_height;
float key_width; float key_width;
key_width = keyboard->button_up->w; key_width = keyboard->button_up->w;
key_height = keyboard->button_up->h; key_height = keyboard->button_up->h;
@ -1156,7 +1152,8 @@ static void draw_keyboard(on_screen_keyboard *keyboard)
keyboard->layout->keys[j][i].x = accumulated_width; keyboard->layout->keys[j][i].x = accumulated_width;
keyboard->layout->keys[j][i].y = accumulated_height; keyboard->layout->keys[j][i].y = accumulated_height;
draw_key(keyboard->layout->keys[j][i], keyboard, 0); } draw_key(keyboard->layout->keys[j][i], keyboard, 0);
}
accumulated_width += (keyboard->layout->keys[j][i].width * key_width); accumulated_width += (keyboard->layout->keys[j][i].width * key_width);
} }
accumulated_height += key_height; accumulated_height += key_height;
@ -1178,9 +1175,9 @@ static void draw_key(osk_key key, on_screen_keyboard * keyboard, int hot)
text = malloc(sizeof(char) * 255); text = malloc(sizeof(char) * 255);
snprintf(text, 6,"%s", key.plain_label); snprintf(text, 6, "%s", key.plain_label);
if( strncmp("NULL", text, 4) != 0 && key.keycode != 0) if (strncmp("NULL", text, 4) != 0 && key.keycode != 0)
{ {
if (hot) if (hot)
skey = stretch_surface(keyboard->button_down, key.width * keyboard->button_down->w); skey = stretch_surface(keyboard->button_down, key.width * keyboard->button_down->w);
@ -1213,7 +1210,7 @@ static void draw_key(osk_key key, on_screen_keyboard * keyboard, int hot)
/* FIXME: TODO draw top and bottom_right (altgr) labels */ /* FIXME: TODO draw top and bottom_right (altgr) labels */
static void label_key(osk_key key, on_screen_keyboard *keyboard) static void label_key(osk_key key, on_screen_keyboard * keyboard)
{ {
SDL_Surface *messager; SDL_Surface *messager;
int modstate; int modstate;
@ -1227,7 +1224,7 @@ static void label_key(osk_key key, on_screen_keyboard *keyboard)
/* FIXME There MUST be a simpler way to do this. Pere 2011/8/3 */ /* FIXME There MUST be a simpler way to do this. Pere 2011/8/3 */
/* First the plain ones */ /* First the plain ones */
if (modstate == KMOD_NONE || (modstate == (KMOD_NONE | KMOD_LALT))) if (modstate == KMOD_NONE || (modstate == (KMOD_NONE | KMOD_LALT)))
text=strdup(key.plain_label); text = strdup(key.plain_label);
else if (modstate == KMOD_SHIFT) else if (modstate == KMOD_SHIFT)
{ {
@ -1280,52 +1277,51 @@ static void label_key(osk_key key, on_screen_keyboard *keyboard)
text = strdup(key.top_label); text = strdup(key.top_label);
} }
if( strncmp("DELETE", text, 6) == 0) if (strncmp("DELETE", text, 6) == 0)
{ {
apply_surface(key.x, key.y, keyboard->oskdel, keyboard->surface, NULL); apply_surface(key.x, key.y, keyboard->oskdel, keyboard->surface, NULL);
} }
else if( strncmp("TAB", text, 3) == 0) else if (strncmp("TAB", text, 3) == 0)
{ {
apply_surface(key.x, key.y, keyboard->osktab, keyboard->surface, NULL); apply_surface(key.x, key.y, keyboard->osktab, keyboard->surface, NULL);
} }
else if( strncmp("ENTER", text, 5) == 0) else if (strncmp("ENTER", text, 5) == 0)
{ {
apply_surface(key.x, key.y, keyboard->oskenter, keyboard->surface, NULL); apply_surface(key.x, key.y, keyboard->oskenter, keyboard->surface, NULL);
} }
else if( strncmp("CAPSLOCK", text, 8) == 0) else if (strncmp("CAPSLOCK", text, 8) == 0)
{ {
apply_surface(key.x, key.y, keyboard->oskcapslock, keyboard->surface, NULL); apply_surface(key.x, key.y, keyboard->oskcapslock, keyboard->surface, NULL);
} }
else if( strncmp("SHIFT", text, 5) == 0) else if (strncmp("SHIFT", text, 5) == 0)
{ {
apply_surface(key.x, key.y, keyboard->oskshift, keyboard->surface, NULL); apply_surface(key.x, key.y, keyboard->oskshift, keyboard->surface, NULL);
} }
else if( strncmp("SPACE", text, 5) != 0 && strncmp("NULL", text, 4) != 0) else if (strncmp("SPACE", text, 5) != 0 && strncmp("NULL", text, 4) != 0)
{ {
messager = TTF_RenderUTF8_Blended(keyboard->osk_fonty, text, keyboard->layout->fgcolor); messager = TTF_RenderUTF8_Blended(keyboard->osk_fonty, text, keyboard->layout->fgcolor);
apply_surface( key.x + 5, key.y, messager, keyboard->surface, NULL); apply_surface(key.x + 5, key.y, messager, keyboard->surface, NULL);
SDL_FreeSurface(messager); SDL_FreeSurface(messager);
} }
free(text); free(text);
} }
/* Searches the key corresponding to coordinates */ /* Searches the key corresponding to coordinates */
static osk_key * find_key(on_screen_keyboard * keyboard, int x, int y) static osk_key *find_key(on_screen_keyboard * keyboard, int x, int y)
{ {
int i, j; int i, j;
osk_key *key; osk_key *key;
key = NULL; key = NULL;
for (j = 0; j <keyboard->layout->height; j++) for (j = 0; j < keyboard->layout->height; j++)
{ {
if (keyboard->layout->keys[j][0].y < y && if (keyboard->layout->keys[j][0].y < y && keyboard->layout->keys[j][0].y + keyboard->button_up->h > y)
keyboard->layout->keys[j][0].y + keyboard->button_up->h > y)
for (i = 0; i < keyboard->layout->width; i++) for (i = 0; i < keyboard->layout->width; i++)
if (keyboard->layout->keys[j][i].x < x && if (keyboard->layout->keys[j][i].x < x &&
keyboard->layout->keys[j][i].x + keyboard->layout->keys[j][i].width * keyboard->button_up->w > x) keyboard->layout->keys[j][i].x + keyboard->layout->keys[j][i].width * keyboard->button_up->w > x)
@ -1340,7 +1336,7 @@ static osk_key * find_key(on_screen_keyboard * keyboard, int x, int y)
/* Copies orig to dest or sets dest to defaults if orig is NULL. /* Copies orig to dest or sets dest to defaults if orig is NULL.
if firstime is setted, don't frees the strings as there aren't. */ if firstime is setted, don't frees the strings as there aren't. */
static void set_key(osk_key *orig, osk_key *dest, int firsttime) static void set_key(osk_key * orig, osk_key * dest, int firsttime)
{ {
if (orig == NULL) if (orig == NULL)
{ {
@ -1384,7 +1380,7 @@ static void set_key(osk_key *orig, osk_key *dest, int firsttime)
} }
} }
static char * find_keysym(osk_key key, on_screen_keyboard *keyboard) static char *find_keysym(osk_key key, on_screen_keyboard * keyboard)
{ {
int keycode; int keycode;
char *keysym; char *keysym;
@ -1452,11 +1448,11 @@ static char * find_keysym(osk_key key, on_screen_keyboard *keyboard)
keysym = keysyms.caps; keysym = keysyms.caps;
} }
return(keysym); return (keysym);
} }
/* We lose the SDL ModState by leaving and entering the tuxpaint window, so using a custom state */ /* We lose the SDL ModState by leaving and entering the tuxpaint window, so using a custom state */
static int handle_keymods(char * keysym, osk_key * key, on_screen_keyboard *keyboard) static int handle_keymods(char *keysym, osk_key * key, on_screen_keyboard * keyboard)
{ {
SDLMod mod; SDLMod mod;
SDL_Event ev; SDL_Event ev;
@ -1492,9 +1488,8 @@ static int handle_keymods(char * keysym, osk_key * key, on_screen_keyboard *keyb
} }
/* Seems ISO_Level3_Shift and ISO_Next_Group are used too for right Alt */ /* Seems ISO_Level3_Shift and ISO_Next_Group are used too for right Alt */
else if (strncmp("ISO_Level3_Shift", keysym, 16) == 0|| else if (strncmp("ISO_Level3_Shift", keysym, 16) == 0 ||
strncmp("ISO_Next_Group", keysym, 14) == 0|| strncmp("ISO_Next_Group", keysym, 14) == 0 || strncmp("ALT_R", keysym, 5) == 0)
strncmp("ALT_R", keysym, 5) == 0)
{ {
if (mod & KMOD_RALT) if (mod & KMOD_RALT)
{ {
@ -1536,9 +1531,9 @@ static int handle_keymods(char * keysym, osk_key * key, on_screen_keyboard *keyb
else else
keyboard->modifiers = KMOD_NONE; keyboard->modifiers = KMOD_NONE;
if(keyboard->kmdf.shift) if (keyboard->kmdf.shift)
keyboard->kmdf.shift->stick = 0; keyboard->kmdf.shift->stick = 0;
if(keyboard->kmdf.altgr) if (keyboard->kmdf.altgr)
keyboard->kmdf.altgr->stick = 0; keyboard->kmdf.altgr->stick = 0;
return 0; return 0;
@ -1546,44 +1541,44 @@ static int handle_keymods(char * keysym, osk_key * key, on_screen_keyboard *keyb
/* set_dead_sticks and clear_dead_sticks deals with the persistence of /* set_dead_sticks and clear_dead_sticks deals with the persistence of
the keys that are still affecting other key presses. */ the keys that are still affecting other key presses. */
static void set_dead_sticks(osk_key *key, on_screen_keyboard *keyboard) static void set_dead_sticks(osk_key * key, on_screen_keyboard * keyboard)
{ {
key->stick= 1; key->stick = 1;
if(!keyboard->kmdf.dead) if (!keyboard->kmdf.dead)
keyboard->kmdf.dead = key; keyboard->kmdf.dead = key;
else if(!keyboard->kmdf.dead2) else if (!keyboard->kmdf.dead2)
keyboard->kmdf.dead2 = key; keyboard->kmdf.dead2 = key;
else if(!keyboard->kmdf.dead3) else if (!keyboard->kmdf.dead3)
keyboard->kmdf.dead3 = key; keyboard->kmdf.dead3 = key;
else if(!keyboard->kmdf.dead4) else if (!keyboard->kmdf.dead4)
keyboard->kmdf.dead4 = key; keyboard->kmdf.dead4 = key;
} }
static void clear_dead_sticks(on_screen_keyboard *keyboard) static void clear_dead_sticks(on_screen_keyboard * keyboard)
{ {
if(keyboard->kmdf.dead) if (keyboard->kmdf.dead)
{ {
keyboard->kmdf.dead->stick = 0; keyboard->kmdf.dead->stick = 0;
keyboard->kmdf.dead = NULL; keyboard->kmdf.dead = NULL;
} }
if(keyboard->kmdf.dead2) if (keyboard->kmdf.dead2)
{ {
keyboard->kmdf.dead2->stick = 0; keyboard->kmdf.dead2->stick = 0;
keyboard->kmdf.dead2 = NULL; keyboard->kmdf.dead2 = NULL;
} }
if(keyboard->kmdf.dead3) if (keyboard->kmdf.dead3)
{ {
keyboard->kmdf.dead3->stick = 0; keyboard->kmdf.dead3->stick = 0;
keyboard->kmdf.dead3 = NULL; keyboard->kmdf.dead3 = NULL;
} }
if(keyboard->kmdf.dead4) if (keyboard->kmdf.dead4)
{ {
keyboard->kmdf.dead4->stick = 0; keyboard->kmdf.dead4->stick = 0;
keyboard->kmdf.dead4 = NULL; keyboard->kmdf.dead4 = NULL;
} }
} }
struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y) struct osk_keyboard *osk_clicked(on_screen_keyboard * keyboard, int x, int y)
{ {
int i; int i;
osk_key *key; osk_key *key;
@ -1592,7 +1587,7 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y)
char *name, *aux_name, *aux_list, *aux_list_ptr; char *name, *aux_name, *aux_list, *aux_list_ptr;
wchar_t *wkeysym; wchar_t *wkeysym;
wchar_t *ks; wchar_t *ks;
on_screen_keyboard * new_keyboard; on_screen_keyboard *new_keyboard;
#ifdef DEBUG #ifdef DEBUG
printf("list: %s\n", keyboard->keyboard_list); printf("list: %s\n", keyboard->keyboard_list);
@ -1613,7 +1608,7 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y)
if (keyboard->disable_change) if (keyboard->disable_change)
{ {
// free(key); // free(key);
return(keyboard); return (keyboard);
} }
aux_list = strdup(keyboard->keyboard_list); aux_list = strdup(keyboard->keyboard_list);
@ -1626,14 +1621,14 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y)
if (key->keycode == 1) if (key->keycode == 1)
{ {
for (i = 0;;i++, aux_list = NULL) for (i = 0;; i++, aux_list = NULL)
{ {
name = strtok(aux_list, " \n\r\t"); name = strtok(aux_list, " \n\r\t");
if (i == 0) if (i == 0)
aux_name = name; aux_name = name;
if(strcmp(name, keyboard->name) == 0) if (strcmp(name, keyboard->name) == 0)
{ {
name = strtok(NULL, " \n\r\t"); name = strtok(NULL, " \n\r\t");
if (name == NULL) if (name == NULL)
@ -1645,17 +1640,17 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y)
else else
{ {
aux_name = NULL; aux_name = NULL;
for (i = 0;;i++, aux_list = NULL) for (i = 0;; i++, aux_list = NULL)
{ {
name = strtok(aux_list, " \n\r\t"); name = strtok(aux_list, " \n\r\t");
if(name == NULL) if (name == NULL)
{ {
name = aux_name; name = aux_name;
break; break;
} }
if(strstr(name, keyboard->name)) if (strstr(name, keyboard->name))
{ {
name = aux_name; name = aux_name;
if (name != NULL) if (name != NULL)
@ -1667,14 +1662,17 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y)
} }
new_keyboard = osk_create(name, keyboard->surface, keyboard->button_up, keyboard->button_down, keyboard->button_off, keyboard->button_nav, keyboard->button_hold, keyboard->oskdel, keyboard->osktab, keyboard->oskenter, keyboard->oskcapslock, keyboard->oskshift, keyboard->disable_change); new_keyboard =
osk_create(name, keyboard->surface, keyboard->button_up, keyboard->button_down, keyboard->button_off,
keyboard->button_nav, keyboard->button_hold, keyboard->oskdel, keyboard->osktab,
keyboard->oskenter, keyboard->oskcapslock, keyboard->oskshift, keyboard->disable_change);
free(aux_list_ptr); free(aux_list_ptr);
if (new_keyboard == NULL) if (new_keyboard == NULL)
{ {
// free(key); // free(key);
return(keyboard); /* Don't break here, at least the old keyboard should work */ return (keyboard); /* Don't break here, at least the old keyboard should work */
} }
else else
{ {
@ -1682,7 +1680,7 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y)
new_keyboard->keyboard_list = strdup(keyboard->keyboard_list); new_keyboard->keyboard_list = strdup(keyboard->keyboard_list);
// free(key); // free(key);
osk_free(keyboard); osk_free(keyboard);
return(new_keyboard); return (new_keyboard);
} }
} }
@ -1690,21 +1688,19 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y)
keysym = find_keysym(*key, keyboard); keysym = find_keysym(*key, keyboard);
if (!keysym) if (!keysym)
{ {
return(keyboard); return (keyboard);
} }
draw_key(*key, keyboard, 1); draw_key(*key, keyboard, 1);
if (handle_keymods(keysym, key, keyboard)) if (handle_keymods(keysym, key, keyboard))
{ {
return(keyboard); /* no more processing is needed */ return (keyboard); /* no more processing is needed */
} }
wkeysym = malloc(sizeof(wchar_t) * (strlen(keysym) + 1)); wkeysym = malloc(sizeof(wchar_t) * (strlen(keysym) + 1));
mbsrtowcs(wkeysym, (const char **) &keysym, mbsrtowcs(wkeysym, (const char **)&keysym, strlen(keysym) + 1, NULL);
strlen(keysym)+1,
NULL);
#ifdef DEBUG #ifdef DEBUG
printf("wkeysym %ls %i\n\n", wkeysym, (int)wcslen(wkeysym)); printf("wkeysym %ls %i\n\n", wkeysym, (int)wcslen(wkeysym));
@ -1731,8 +1727,7 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y)
event.key.keysym.sym = SDLK_RETURN; event.key.keysym.sym = SDLK_RETURN;
event.key.keysym.unicode = '\r'; event.key.keysym.unicode = '\r';
} }
else if (wcsncmp(L"Tab", ks, 3) == 0 || else if (wcsncmp(L"Tab", ks, 3) == 0 || wcsncmp(L"ISO_Left_Tab", ks, 12) == 0)
wcsncmp(L"ISO_Left_Tab", ks, 12) == 0)
{ {
event.key.keysym.sym = SDLK_TAB; event.key.keysym.sym = SDLK_TAB;
event.key.keysym.unicode = '\t'; event.key.keysym.unicode = '\t';
@ -1743,10 +1738,9 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y)
event.key.keysym.unicode = '\b'; event.key.keysym.unicode = '\b';
} }
else if (wcsncmp(L"NoSymbol", ks, 8) == 0) else if (wcsncmp(L"NoSymbol", ks, 8) == 0)
return(keyboard); return (keyboard);
else else if (keyboard->composed_type == 1)
if (keyboard->composed_type == 1)
event.key.keysym.unicode = *keyboard->composed; event.key.keysym.unicode = *keyboard->composed;
else else
event.key.keysym.unicode = keysym2unicode(mnemo2keysym(mnemo, keyboard), keyboard); event.key.keysym.unicode = keysym2unicode(mnemo2keysym(mnemo, keyboard), keyboard);
@ -1780,12 +1774,13 @@ struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y)
free(wkeysym); free(wkeysym);
} }
return(keyboard); return (keyboard);
} }
void osk_released(on_screen_keyboard *keyboard) void osk_released(on_screen_keyboard * keyboard)
{ {
osk_key *key; osk_key *key;
key = keyboard->last_key_pressed; key = keyboard->last_key_pressed;
if (key) if (key)
{ {
@ -1797,10 +1792,11 @@ void osk_released(on_screen_keyboard *keyboard)
} }
static void free_keymap(osk_keymap *keymap) static void free_keymap(osk_keymap * keymap)
{ {
int i; int i;
for (i=0; i<256; i++)
for (i = 0; i < 256; i++)
{ {
if (keymap[i].plain) if (keymap[i].plain)
free(keymap[i].plain); free(keymap[i].plain);
@ -1817,7 +1813,8 @@ static void free_keymap(osk_keymap *keymap)
static void free_composemap(osk_composenode * composenode) static void free_composemap(osk_composenode * composenode)
{ {
int i; int i;
for(i = 0; i < composenode->size; i++)
for (i = 0; i < composenode->size; i++)
{ {
free_composemap(composenode->childs[i]); free_composemap(composenode->childs[i]);
free(composenode->childs[i]); free(composenode->childs[i]);
@ -1831,7 +1828,7 @@ static void free_composemap(osk_composenode * composenode)
free(composenode->keysym); free(composenode->keysym);
} }
static void free_keysymdefs(keysymdefs *ksd, int size) static void free_keysymdefs(keysymdefs * ksd, int size)
{ {
int i; int i;
@ -1839,7 +1836,7 @@ static void free_keysymdefs(keysymdefs *ksd, int size)
free(ksd[i].mnemo); free(ksd[i].mnemo);
} }
static void free_keys(osk_layout *layout) static void free_keys(osk_layout * layout)
{ {
int i, j; int i, j;
@ -1862,7 +1859,7 @@ static void free_keys(osk_layout *layout)
free(layout->keys); free(layout->keys);
} }
static void free_layout(osk_layout *layout) static void free_layout(osk_layout * layout)
{ {
if (layout->name != NULL) if (layout->name != NULL)
free(layout->name); free(layout->name);
@ -1878,15 +1875,15 @@ static void free_layout(osk_layout *layout)
free(layout); free(layout);
} }
void osk_free(on_screen_keyboard *keyboard) void osk_free(on_screen_keyboard * keyboard)
{ {
free(keyboard->name); free(keyboard->name);
free_layout(keyboard->layout); free_layout(keyboard->layout);
if(keyboard->composed) if (keyboard->composed)
free(keyboard->composed); free(keyboard->composed);
if(keyboard->last_key_pressed) if (keyboard->last_key_pressed)
free(keyboard->last_key_pressed); free(keyboard->last_key_pressed);
if(keyboard->keyboard_list) if (keyboard->keyboard_list)
free(keyboard->keyboard_list); free(keyboard->keyboard_list);
SDL_FreeSurface(keyboard->surface); SDL_FreeSurface(keyboard->surface);
set_key(NULL, &keyboard->keymodifiers.shift, 0); set_key(NULL, &keyboard->keymodifiers.shift, 0);
@ -2062,4 +2059,3 @@ void osk_free(on_screen_keyboard *keyboard)
/* } */ /* } */
/* } */ /* } */
/* } */ /* } */

View file

@ -28,10 +28,10 @@ typedef struct osk_keymap
{ {
int keycode; int keycode;
int disable_caps; /* If caps lock should affect this key */ int disable_caps; /* If caps lock should affect this key */
char * plain; /* The default Xkeysym for the keycode */ char *plain; /* The default Xkeysym for the keycode */
char * caps; /* If CapsLock or Shift + key */ char *caps; /* If CapsLock or Shift + key */
char * altgr; /* If AltGr + key */ char *altgr; /* If AltGr + key */
char * shiftaltgr; /* If AltGr + Shift + key */ char *shiftaltgr; /* If AltGr + Shift + key */
} osk_keymap; } osk_keymap;
typedef struct osk_key typedef struct osk_key
@ -51,16 +51,16 @@ typedef struct osk_key
typedef struct osk_composenode typedef struct osk_composenode
{ {
wchar_t * keysym; wchar_t *keysym;
wchar_t * result; wchar_t *result;
int size; /* How many childs are there. */ int size; /* How many childs are there. */
struct osk_composenode ** childs; struct osk_composenode **childs;
// struct osk_composenode **parent; // struct osk_composenode **parent;
} osk_composenode; } osk_composenode;
typedef struct keysymdefs typedef struct keysymdefs
{ {
char * mnemo; char *mnemo;
int keysym; int keysym;
int unicode; int unicode;
} keysymdefs; } keysymdefs;
@ -71,11 +71,11 @@ typedef struct osk_layout
int *rows; int *rows;
int width; int width;
int height; int height;
char * fontpath; char *fontpath;
osk_key **keys; osk_key **keys;
osk_keymap *keymap; osk_keymap *keymap;
osk_composenode * composemap; osk_composenode *composemap;
keysymdefs * keysymdefs; keysymdefs *keysymdefs;
unsigned int sizeofkeysymdefs; unsigned int sizeofkeysymdefs;
SDL_Color bgcolor; SDL_Color bgcolor;
SDL_Color fgcolor; SDL_Color fgcolor;
@ -91,19 +91,19 @@ typedef struct osk_keymodifiers
typedef struct osk_kmdf typedef struct osk_kmdf
{ {
osk_key * shift; osk_key *shift;
osk_key * altgr; osk_key *altgr;
osk_key * compose; osk_key *compose;
osk_key * dead; osk_key *dead;
osk_key * dead2; osk_key *dead2;
osk_key * dead3; osk_key *dead3;
osk_key * dead4; osk_key *dead4;
} osk_kmdf; } osk_kmdf;
typedef struct osk_keyboard typedef struct osk_keyboard
{ {
char * name; /* The name of the keyboard */ char *name; /* The name of the keyboard */
char * keyboard_list; /* The names of the keyboards allowed from this one */ char *keyboard_list; /* The names of the keyboards allowed from this one */
SDL_Surface *surface; /* The surface containing the keyboard */ SDL_Surface *surface; /* The surface containing the keyboard */
SDL_Surface *button_up; /* The surfaces containing the buttons */ SDL_Surface *button_up; /* The surfaces containing the buttons */
SDL_Surface *button_down; SDL_Surface *button_down;
@ -123,24 +123,29 @@ typedef struct osk_keyboard
osk_kmdf kmdf; osk_kmdf kmdf;
osk_layout *layout; /* The layout struct */ osk_layout *layout; /* The layout struct */
char *layout_name[256]; /* The layout name */ char *layout_name[256]; /* The layout name */
TTF_Font * osk_fonty; /* Font */ TTF_Font *osk_fonty; /* Font */
int disable_change; /* If true, stay with the first layout found */ int disable_change; /* If true, stay with the first layout found */
wchar_t * key[256]; /* The text of the key */ wchar_t *key[256]; /* The text of the key */
int keycode; /* The unicode code corresponding to the key */ int keycode; /* The unicode code corresponding to the key */
wchar_t * composed; /* The unicode char found after a sequence of key presses */ wchar_t *composed; /* The unicode char found after a sequence of key presses */
int composed_type; /* 1 if the value stored in composed is yet the unicode value */ int composed_type; /* 1 if the value stored in composed is yet the unicode value */
osk_composenode * composing; /* The node in the middle of a compose sequence */ osk_composenode *composing; /* The node in the middle of a compose sequence */
osk_key * last_key_pressed; /* The last key pressed */ osk_key *last_key_pressed; /* The last key pressed */
} on_screen_keyboard; } on_screen_keyboard;
struct osk_keyboard *osk_create(char *layout_name, SDL_Surface *canvas, SDL_Surface *button_up, SDL_Surface *button_down, SDL_Surface *button_off, SDL_Surface *button_nav, SDL_Surface *button_hold, SDL_Surface *oskdel, SDL_Surface *osktab, SDL_Surface *oskenter, SDL_Surface *oskcapslock, SDL_Surface *oskshift, int disable_change); struct osk_keyboard *osk_create(char *layout_name, SDL_Surface * canvas, SDL_Surface * button_up,
SDL_Surface * button_down, SDL_Surface * button_off, SDL_Surface * button_nav,
SDL_Surface * button_hold, SDL_Surface * oskdel, SDL_Surface * osktab,
SDL_Surface * oskenter, SDL_Surface * oskcapslock, SDL_Surface * oskshift,
int disable_change);
struct osk_layout *osk_load_layout(char *layout_name); struct osk_layout *osk_load_layout(char *layout_name);
void osk_get_layout_data(char *layout_name, int *layout_w, int *layout_h, char * layout_buttons, char *layout_labels, char *layout_keycodes); void osk_get_layout_data(char *layout_name, int *layout_w, int *layout_h, char *layout_buttons, char *layout_labels,
void osk_reset(on_screen_keyboard *osk); char *layout_keycodes);
struct osk_keyboard * osk_clicked(on_screen_keyboard *keyboard, int x, int y); void osk_reset(on_screen_keyboard * osk);
void osk_released(on_screen_keyboard *osk); struct osk_keyboard *osk_clicked(on_screen_keyboard * keyboard, int x, int y);
void osk_hover(on_screen_keyboard *keyboard, int x, int y); void osk_released(on_screen_keyboard * osk);
void osk_free(on_screen_keyboard *osk); void osk_hover(on_screen_keyboard * keyboard, int x, int y);
void osk_change_layout(on_screen_keyboard *osk); void osk_free(on_screen_keyboard * osk);
void osk_change_layout(on_screen_keyboard * osk);