| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: dos_keyboard_layout.cpp,v 1.22 2009-09-06 19:25:33 c2woody Exp $ */ |
| 20 | |
| 21 | #include "dosbox.h" |
| 22 | #include "bios.h" |
| 23 | #include "bios_disk.h" |
| 24 | #include "setup.h" |
| 25 | #include "support.h" |
| 26 | #include "../ints/int10.h" |
| 27 | #include "regs.h" |
| 28 | #include "callback.h" |
| 29 | #include "mapper.h" |
| 30 | #include "drives.h" |
| 31 | #include "dos_inc.h" |
| 32 | |
| 33 | #include "dos_codepages.h" |
| 34 | #include "dos_keyboard_layout_data.h" |
| 35 | |
| 36 | #if defined (WIN32) |
| 37 | #include <windows.h> |
| 38 | #endif |
| 39 | |
| 40 | |
| 41 | static FILE* OpenDosboxFile(const char* name) { |
| 42 | Bit8u drive; |
| 43 | char fullname[DOS_PATHLENGTH]; |
| 44 | |
| 45 | localDrive* ldp=0; |
| 46 | // try to build dos name |
| 47 | if (DOS_MakeName(name,fullname,&drive)) { |
| 48 | try { |
| 49 | // try to open file on mounted drive first |
| 50 | ldp=dynamic_cast<localDrive*>(Drives[drive]); |
| 51 | if (ldp) { |
| 52 | FILE *tmpfile=ldp->GetSystemFilePtr(fullname, "rb"); |
| 53 | if (tmpfile != NULL) return tmpfile; |
| 54 | } |
| 55 | } |
| 56 | catch(...) {} |
| 57 | } |
| 58 | FILE *tmpfile=fopen(name, "rb"); |
| 59 | return tmpfile; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | class keyboard_layout { |
| 64 | public: |
| 65 | keyboard_layout() { |
| 66 | this->reset(); |
| 67 | language_codes=NULL; |
| 68 | use_foreign_layout=false; |
| 69 | sprintf(current_keyboard_file_name, "none"); |
| 70 | }; |
| 71 | |
| 72 | ~keyboard_layout(); |
| 73 | |
| 74 | // read in a codepage from a .cpi-file |
| 75 | Bitu read_codepage_file(const char* codepage_file_name, Bit32s codepage_id); |
| 76 | Bit16u extract_codepage(const char* keyboard_file_name); |
| 77 | // read in a keyboard layout from a .kl-file |
| 78 | Bitu read_keyboard_file(const char* keyboard_file_name, Bit32s req_cp); |
| 79 | |
| 80 | // call layout_key to apply the current language layout |
| 81 | bool layout_key(Bitu key, Bit8u flags1, Bit8u flags2, Bit8u flags3); |
| 82 | |
| 83 | Bitu switch_keyboard_layout(const char* new_layout, keyboard_layout* &created_layout, Bit32s& tried_cp); |
| 84 | void switch_foreign_layout(); |
| 85 | const char* get_layout_name(); |
| 86 | const char* main_language_code(); |
| 87 | |
| 88 | |
| 89 | private: |
| 90 | static const Bit8u layout_pages=12; |
| 91 | Bit16u current_layout[(MAX_SCAN_CODE+1)*layout_pages]; |
| 92 | struct { |
| 93 | Bit16u required_flags,forbidden_flags; |
| 94 | Bit16u required_userflags,forbidden_userflags; |
| 95 | } current_layout_planes[layout_pages-4]; |
| 96 | Bit8u additional_planes,used_lock_modifiers; |
| 97 | |
| 98 | // diacritics table |
| 99 | Bit8u diacritics[2048]; |
| 100 | Bit16u diacritics_entries; |
| 101 | Bit16u diacritics_character; |
| 102 | Bit16u user_keys; |
| 103 | |
| 104 | char current_keyboard_file_name[256]; |
| 105 | bool use_foreign_layout; |
| 106 | |
| 107 | // language code storage used when switching layouts |
| 108 | char** language_codes; |
| 109 | Bitu language_code_count; |
| 110 | |
| 111 | void reset(); |
| 112 | void read_keyboard_file(Bit32s specific_layout); |
| 113 | Bitu read_keyboard_file(const char* keyboard_file_name, Bit32s specific_layout, Bit32s requested_codepage); |
| 114 | bool map_key(Bitu key, Bit16u layouted_key, bool is_command, bool is_keypair); |
| 115 | }; |
| 116 | |
| 117 | |
| 118 | keyboard_layout::~keyboard_layout() { |
| 119 | if (language_codes) { |
| 120 | for (Bitu i=0; i<language_code_count; i++) |
| 121 | delete[] language_codes[i]; |
| 122 | delete[] language_codes; |
| 123 | language_codes=NULL; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void keyboard_layout::reset() { |
| 128 | for (Bit32u i=0; i<(MAX_SCAN_CODE+1)*layout_pages; i++) current_layout[i]=0; |
| 129 | for (Bit32u i=0; i<layout_pages-4; i++) { |
| 130 | current_layout_planes[i].required_flags=0; |
| 131 | current_layout_planes[i].forbidden_flags=0xffff; |
| 132 | current_layout_planes[i].required_userflags=0; |
| 133 | current_layout_planes[i].forbidden_userflags=0xffff; |
| 134 | } |
| 135 | used_lock_modifiers=0x0f; |
| 136 | diacritics_entries=0; // no diacritics loaded |
| 137 | diacritics_character=0; |
| 138 | user_keys=0; // all userkeys off |
| 139 | language_code_count=0; |
| 140 | } |
| 141 | |
| 142 | Bitu keyboard_layout::read_keyboard_file(const char* keyboard_file_name, Bit32s req_cp) { |
| 143 | return this->read_keyboard_file(keyboard_file_name, -1, req_cp); |
| 144 | } |
| 145 | |
| 146 | // switch to a different layout |
| 147 | void keyboard_layout::read_keyboard_file(Bit32s specific_layout) { |
| 148 | if (strcmp(current_keyboard_file_name,"none")) |
| 149 | this->read_keyboard_file(current_keyboard_file_name, specific_layout, dos.loaded_codepage); |
| 150 | } |
| 151 | |
| 152 | static Bit32u read_kcl_file(const char* kcl_file_name, const char* layout_id, bool first_id_only) { |
| 153 | FILE* tempfile = OpenDosboxFile(kcl_file_name); |
| 154 | if (tempfile==0) return 0; |
| 155 | |
| 156 | static Bit8u rbuf[8192]; |
| 157 | |
| 158 | // check ID-bytes of file |
| 159 | Bit32u dr=(Bit32u)fread(rbuf, sizeof(Bit8u), 7, tempfile); |
| 160 | if ((dr<7) || (rbuf[0]!=0x4b) || (rbuf[1]!=0x43) || (rbuf[2]!=0x46)) { |
| 161 | fclose(tempfile); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | fseek(tempfile, 7+rbuf[6], SEEK_SET); |
| 166 | |
| 167 | for (;;) { |
| 168 | Bit32u cur_pos=(Bit32u)(ftell(tempfile)); |
| 169 | dr=(Bit32u)fread(rbuf, sizeof(Bit8u), 5, tempfile); |
| 170 | if (dr<5) break; |
| 171 | Bit16u len=host_readw(&rbuf[0]); |
| 172 | |
| 173 | Bit8u data_len=rbuf[2]; |
| 174 | |
| 175 | char lng_codes[258]; |
| 176 | fseek(tempfile, -2, SEEK_CUR); |
| 177 | // get all language codes for this layout |
| 178 | for (Bitu i=0; i<data_len;) { |
| 179 | fread(rbuf, sizeof(Bit8u), 2, tempfile); |
| 180 | Bit16u lcnum=host_readw(&rbuf[0]); |
| 181 | i+=2; |
| 182 | Bitu lcpos=0; |
| 183 | for (;i<data_len;) { |
| 184 | fread(rbuf, sizeof(Bit8u), 1, tempfile); |
| 185 | i++; |
| 186 | if (((char)rbuf[0])==',') break; |
| 187 | lng_codes[lcpos++]=(char)rbuf[0]; |
| 188 | } |
| 189 | lng_codes[lcpos]=0; |
| 190 | if (strcasecmp(lng_codes, layout_id)==0) { |
| 191 | // language ID found in file, return file position |
| 192 | fclose(tempfile); |
| 193 | return cur_pos; |
| 194 | } |
| 195 | if (first_id_only) break; |
| 196 | if (lcnum) { |
| 197 | sprintf(&lng_codes[lcpos],"%d",lcnum); |
| 198 | if (strcasecmp(lng_codes, layout_id)==0) { |
| 199 | // language ID found in file, return file position |
| 200 | return cur_pos; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | fseek(tempfile, cur_pos+3+len, SEEK_SET); |
| 205 | } |
| 206 | |
| 207 | fclose(tempfile); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static Bit32u read_kcl_data(Bit8u * kcl_data, Bit32u kcl_data_size, const char* layout_id, bool first_id_only) { |
| 212 | // check ID-bytes |
| 213 | if ((kcl_data[0]!=0x4b) || (kcl_data[1]!=0x43) || (kcl_data[2]!=0x46)) { |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | Bit32u dpos=7+kcl_data[6]; |
| 218 | |
| 219 | for (;;) { |
| 220 | if (dpos+5>kcl_data_size) break; |
| 221 | Bit32u cur_pos=dpos; |
| 222 | Bit16u len=host_readw(&kcl_data[dpos]); |
| 223 | Bit8u data_len=kcl_data[dpos+2]; |
| 224 | dpos+=5; |
| 225 | |
| 226 | char lng_codes[258]; |
| 227 | // get all language codes for this layout |
| 228 | for (Bitu i=0; i<data_len;) { |
| 229 | Bit16u lcnum=host_readw(&kcl_data[dpos-2]); |
| 230 | i+=2; |
| 231 | Bitu lcpos=0; |
| 232 | for (;i<data_len;) { |
| 233 | if (dpos+1>kcl_data_size) break; |
| 234 | char lc=(char)kcl_data[dpos]; |
| 235 | dpos++; |
| 236 | i++; |
| 237 | if (lc==',') break; |
| 238 | lng_codes[lcpos++]=lc; |
| 239 | } |
| 240 | lng_codes[lcpos]=0; |
| 241 | if (strcasecmp(lng_codes, layout_id)==0) { |
| 242 | // language ID found, return position |
| 243 | return cur_pos; |
| 244 | } |
| 245 | if (first_id_only) break; |
| 246 | if (lcnum) { |
| 247 | sprintf(&lng_codes[lcpos],"%d",lcnum); |
| 248 | if (strcasecmp(lng_codes, layout_id)==0) { |
| 249 | // language ID found, return position |
| 250 | return cur_pos; |
| 251 | } |
| 252 | } |
| 253 | dpos+=2; |
| 254 | } |
| 255 | dpos=cur_pos+3+len; |
| 256 | } |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | Bitu keyboard_layout::read_keyboard_file(const char* keyboard_file_name, Bit32s specific_layout, Bit32s requested_codepage) { |
| 261 | this->reset(); |
| 262 | |
| 263 | if (specific_layout==-1) strcpy(current_keyboard_file_name, keyboard_file_name); |
| 264 | if (!strcmp(keyboard_file_name,"none")) return KEYB_NOERROR; |
| 265 | |
| 266 | static Bit8u read_buf[65535]; |
| 267 | Bit32u read_buf_size, read_buf_pos, bytes_read; |
| 268 | Bit32u start_pos=5; |
| 269 | |
| 270 | char nbuf[512]; |
| 271 | read_buf_size = 0; |
| 272 | sprintf(nbuf, "%s.kl", keyboard_file_name); |
| 273 | FILE* tempfile = OpenDosboxFile(nbuf); |
| 274 | if (tempfile==NULL) { |
| 275 | // try keyboard layout libraries next |
| 276 | if ((start_pos=read_kcl_file("keyboard.sys",keyboard_file_name,true))) { |
| 277 | tempfile = OpenDosboxFile("keyboard.sys"); |
| 278 | } else if ((start_pos=read_kcl_file("keybrd2.sys",keyboard_file_name,true))) { |
| 279 | tempfile = OpenDosboxFile("keybrd2.sys"); |
| 280 | } else if ((start_pos=read_kcl_file("keybrd3.sys",keyboard_file_name,true))) { |
| 281 | tempfile = OpenDosboxFile("keybrd3.sys"); |
| 282 | } else if ((start_pos=read_kcl_file("keyboard.sys",keyboard_file_name,false))) { |
| 283 | tempfile = OpenDosboxFile("keyboard.sys"); |
| 284 | } else if ((start_pos=read_kcl_file("keybrd2.sys",keyboard_file_name,false))) { |
| 285 | tempfile = OpenDosboxFile("keybrd2.sys"); |
| 286 | } else if ((start_pos=read_kcl_file("keybrd3.sys",keyboard_file_name,false))) { |
| 287 | tempfile = OpenDosboxFile("keybrd3.sys"); |
| 288 | } else if ((start_pos=read_kcl_data(layout_keyboardsys,33196,keyboard_file_name,true))) { |
| 289 | read_buf_size=0; |
| 290 | for (Bitu ct=start_pos+2; ct<33196; ct++) read_buf[read_buf_size++]=layout_keyboardsys[ct]; |
| 291 | } else if ((start_pos=read_kcl_data(layout_keybrd2sys,25431,keyboard_file_name,true))) { |
| 292 | read_buf_size=0; |
| 293 | for (Bitu ct=start_pos+2; ct<25431; ct++) read_buf[read_buf_size++]=layout_keybrd2sys[ct]; |
| 294 | } else if ((start_pos=read_kcl_data(layout_keybrd3sys,27122,keyboard_file_name,true))) { |
| 295 | read_buf_size=0; |
| 296 | for (Bitu ct=start_pos+2; ct<27122; ct++) read_buf[read_buf_size++]=layout_keybrd3sys[ct]; |
| 297 | } else if ((start_pos=read_kcl_data(layout_keyboardsys,33196,keyboard_file_name,false))) { |
| 298 | read_buf_size=0; |
| 299 | for (Bitu ct=start_pos+2; ct<33196; ct++) read_buf[read_buf_size++]=layout_keyboardsys[ct]; |
| 300 | } else if ((start_pos=read_kcl_data(layout_keybrd2sys,25431,keyboard_file_name,false))) { |
| 301 | read_buf_size=0; |
| 302 | for (Bitu ct=start_pos+2; ct<25431; ct++) read_buf[read_buf_size++]=layout_keybrd2sys[ct]; |
| 303 | } else if ((start_pos=read_kcl_data(layout_keybrd3sys,27122,keyboard_file_name,false))) { |
| 304 | read_buf_size=0; |
| 305 | for (Bitu ct=start_pos+2; ct<27122; ct++) read_buf[read_buf_size++]=layout_keybrd3sys[ct]; |
| 306 | } else { |
| 307 | LOG(LOG_BIOS,LOG_ERROR)("Keyboard layout file %s not found",keyboard_file_name); |
| 308 | return KEYB_FILENOTFOUND; |
| 309 | } |
| 310 | if (tempfile) { |
| 311 | fseek(tempfile, start_pos+2, SEEK_SET); |
| 312 | read_buf_size=(Bit32u)fread(read_buf, sizeof(Bit8u), 65535, tempfile); |
| 313 | fclose(tempfile); |
| 314 | } |
| 315 | start_pos=0; |
| 316 | } else { |
| 317 | // check ID-bytes of file |
| 318 | Bit32u dr=(Bit32u)fread(read_buf, sizeof(Bit8u), 4, tempfile); |
| 319 | if ((dr<4) || (read_buf[0]!=0x4b) || (read_buf[1]!=0x4c) || (read_buf[2]!=0x46)) { |
| 320 | LOG(LOG_BIOS,LOG_ERROR)("Invalid keyboard layout file %s",keyboard_file_name); |
| 321 | return KEYB_INVALIDFILE; |
| 322 | } |
| 323 | |
| 324 | fseek(tempfile, 0, SEEK_SET); |
| 325 | read_buf_size=(Bit32u)fread(read_buf, sizeof(Bit8u), 65535, tempfile); |
| 326 | fclose(tempfile); |
| 327 | } |
| 328 | |
| 329 | Bit8u data_len,submappings; |
| 330 | data_len=read_buf[start_pos++]; |
| 331 | |
| 332 | language_codes=new char*[data_len]; |
| 333 | language_code_count=0; |
| 334 | // get all language codes for this layout |
| 335 | for (Bitu i=0; i<data_len;) { |
| 336 | language_codes[language_code_count]=new char[256]; |
| 337 | i+=2; |
| 338 | Bitu lcpos=0; |
| 339 | for (;i<data_len;) { |
| 340 | char lcode=char(read_buf[start_pos+i]); |
| 341 | i++; |
| 342 | if (lcode==',') break; |
| 343 | language_codes[language_code_count][lcpos++]=lcode; |
| 344 | } |
| 345 | language_codes[language_code_count][lcpos]=0; |
| 346 | language_code_count++; |
| 347 | } |
| 348 | |
| 349 | start_pos+=data_len; // start_pos==absolute position of KeybCB block |
| 350 | |
| 351 | submappings=read_buf[start_pos]; |
| 352 | additional_planes=read_buf[start_pos+1]; |
| 353 | |
| 354 | // four pages always occupied by normal,shift,flags,commandbits |
| 355 | if (additional_planes>(layout_pages-4)) additional_planes=(layout_pages-4); |
| 356 | |
| 357 | // seek to plane descriptor |
| 358 | read_buf_pos=start_pos+0x14+submappings*8; |
| 359 | for (Bit16u cplane=0; cplane<additional_planes; cplane++) { |
| 360 | Bit16u plane_flags; |
| 361 | |
| 362 | // get required-flags (shift/alt/ctrl-states etc.) |
| 363 | plane_flags=host_readw(&read_buf[read_buf_pos]); |
| 364 | read_buf_pos+=2; |
| 365 | current_layout_planes[cplane].required_flags=plane_flags; |
| 366 | used_lock_modifiers |= (plane_flags&0x70); |
| 367 | // get forbidden-flags |
| 368 | plane_flags=host_readw(&read_buf[read_buf_pos]); |
| 369 | read_buf_pos+=2; |
| 370 | current_layout_planes[cplane].forbidden_flags=plane_flags; |
| 371 | |
| 372 | // get required-userflags |
| 373 | plane_flags=host_readw(&read_buf[read_buf_pos]); |
| 374 | read_buf_pos+=2; |
| 375 | current_layout_planes[cplane].required_userflags=plane_flags; |
| 376 | // get forbidden-userflags |
| 377 | plane_flags=host_readw(&read_buf[read_buf_pos]); |
| 378 | read_buf_pos+=2; |
| 379 | current_layout_planes[cplane].forbidden_userflags=plane_flags; |
| 380 | } |
| 381 | |
| 382 | bool found_matching_layout=false; |
| 383 | |
| 384 | // check all submappings and use them if general submapping or same codepage submapping |
| 385 | for (Bit16u sub_map=0; (sub_map<submappings) && (!found_matching_layout); sub_map++) { |
| 386 | Bit16u submap_cp, table_offset; |
| 387 | |
| 388 | if ((sub_map!=0) && (specific_layout!=-1)) sub_map=(Bit16u)(specific_layout&0xffff); |
| 389 | |
| 390 | // read codepage of submapping |
| 391 | submap_cp=host_readw(&read_buf[start_pos+0x14+sub_map*8]); |
| 392 | if ((submap_cp!=0) && (submap_cp!=requested_codepage) && (specific_layout==-1)) |
| 393 | continue; // skip nonfitting submappings |
| 394 | |
| 395 | if (submap_cp==requested_codepage) found_matching_layout=true; |
| 396 | |
| 397 | // get table offset |
| 398 | table_offset=host_readw(&read_buf[start_pos+0x18+sub_map*8]); |
| 399 | diacritics_entries=0; |
| 400 | if (table_offset!=0) { |
| 401 | // process table |
| 402 | Bit16u i,j; |
| 403 | for (i=0; i<2048;) { |
| 404 | if (read_buf[start_pos+table_offset+i]==0) break; // end of table |
| 405 | diacritics_entries++; |
| 406 | i+=read_buf[start_pos+table_offset+i+1]*2+2; |
| 407 | } |
| 408 | // copy diacritics table |
| 409 | for (j=0; j<=i; j++) diacritics[j]=read_buf[start_pos+table_offset+j]; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | // get table offset |
| 414 | table_offset=host_readw(&read_buf[start_pos+0x16+sub_map*8]); |
| 415 | if (table_offset==0) continue; // non-present table |
| 416 | |
| 417 | read_buf_pos=start_pos+table_offset; |
| 418 | |
| 419 | bytes_read=read_buf_size-read_buf_pos; |
| 420 | |
| 421 | // process submapping table |
| 422 | for (Bit32u i=0; i<bytes_read;) { |
| 423 | Bit8u scan=read_buf[read_buf_pos++]; |
| 424 | if (scan==0) break; |
| 425 | Bit8u scan_length=(read_buf[read_buf_pos]&7)+1; // length of data struct |
| 426 | read_buf_pos+=2; |
| 427 | i+=3; |
| 428 | if (((scan&0x7f)<=MAX_SCAN_CODE) && (scan_length>0)) { |
| 429 | // add all available mappings |
| 430 | for (Bit16u addmap=0; addmap<scan_length; addmap++) { |
| 431 | if (addmap>additional_planes+2) break; |
| 432 | Bitu charptr=read_buf_pos+addmap*((read_buf[read_buf_pos-2]&0x80)?2:1); |
| 433 | Bit16u kchar=read_buf[charptr]; |
| 434 | |
| 435 | if (kchar!=0) { // key remapped |
| 436 | if (read_buf[read_buf_pos-2]&0x80) kchar|=read_buf[charptr+1]<<8; // scancode/char pair |
| 437 | // overwrite mapping |
| 438 | current_layout[scan*layout_pages+addmap]=kchar; |
| 439 | // clear command bit |
| 440 | current_layout[scan*layout_pages+layout_pages-2]&=~(1<<addmap); |
| 441 | // add command bit |
| 442 | current_layout[scan*layout_pages+layout_pages-2]|=(read_buf[read_buf_pos-1] & (1<<addmap)); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | current_layout[scan*layout_pages+layout_pages-1]=read_buf[read_buf_pos-2]; // flags |
| 447 | if (read_buf[read_buf_pos-2]&0x80) scan_length*=2; // granularity flag (S) |
| 448 | } |
| 449 | i+=scan_length; // advance pointer |
| 450 | read_buf_pos+=scan_length; |
| 451 | } |
| 452 | if (specific_layout==sub_map) break; |
| 453 | } |
| 454 | |
| 455 | if (found_matching_layout) { |
| 456 | if (specific_layout==-1) LOG(LOG_BIOS,LOG_NORMAL)("Keyboard layout %s successfully loaded",keyboard_file_name); |
| 457 | else LOG(LOG_BIOS,LOG_NORMAL)("Keyboard layout %s (%i) successfully loaded",keyboard_file_name,specific_layout); |
| 458 | this->use_foreign_layout=true; |
| 459 | return KEYB_NOERROR; |
| 460 | } |
| 461 | |
| 462 | LOG(LOG_BIOS,LOG_ERROR)("No matching keyboard layout found in %s",keyboard_file_name); |
| 463 | |
| 464 | // reset layout data (might have been changed by general layout) |
| 465 | this->reset(); |
| 466 | |
| 467 | return KEYB_LAYOUTNOTFOUND; |
| 468 | } |
| 469 | |
| 470 | bool keyboard_layout::layout_key(Bitu key, Bit8u flags1, Bit8u flags2, Bit8u flags3) { |
| 471 | if (key>MAX_SCAN_CODE) return false; |
| 472 | if (!this->use_foreign_layout) return false; |
| 473 | |
| 474 | bool is_special_pair=(current_layout[key*layout_pages+layout_pages-1] & 0x80)==0x80; |
| 475 | |
| 476 | if ((((flags1&used_lock_modifiers)&0x7c)==0) && ((flags3&2)==0)) { |
| 477 | // check if shift/caps is active: |
| 478 | // (left_shift OR right_shift) XOR (key_affected_by_caps AND caps_locked) |
| 479 | if ((((flags1&2)>>1) | (flags1&1)) ^ (((current_layout[key*layout_pages+layout_pages-1] & 0x40) & (flags1 & 0x40))>>6)) { |
| 480 | // shift plane |
| 481 | if (current_layout[key*layout_pages+1]!=0) { |
| 482 | // check if command-bit is set for shift plane |
| 483 | bool is_command=(current_layout[key*layout_pages+layout_pages-2]&2)!=0; |
| 484 | if (this->map_key(key, current_layout[key*layout_pages+1], |
| 485 | is_command, is_special_pair)) return true; |
| 486 | } |
| 487 | } else { |
| 488 | // normal plane |
| 489 | if (current_layout[key*layout_pages]!=0) { |
| 490 | // check if command-bit is set for normal plane |
| 491 | bool is_command=(current_layout[key*layout_pages+layout_pages-2]&1)!=0; |
| 492 | if (this->map_key(key, current_layout[key*layout_pages], |
| 493 | is_command, is_special_pair)) return true; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // calculate current flags |
| 499 | Bit16u current_flags=(flags1&0x7f) | (((flags2&3) | (flags3&0xc))<<8); |
| 500 | if (flags1&3) current_flags|=0x4000; // either shift key active |
| 501 | if (flags3&2) current_flags|=0x1000; // e0 prefixed |
| 502 | |
| 503 | // check all planes if flags fit |
| 504 | for (Bit16u cplane=0; cplane<additional_planes; cplane++) { |
| 505 | Bit16u req_flags=current_layout_planes[cplane].required_flags; |
| 506 | Bit16u req_userflags=current_layout_planes[cplane].required_userflags; |
| 507 | // test flags |
| 508 | if (((current_flags & req_flags)==req_flags) && |
| 509 | ((user_keys & req_userflags)==req_userflags) && |
| 510 | ((current_flags & current_layout_planes[cplane].forbidden_flags)==0) && |
| 511 | ((user_keys & current_layout_planes[cplane].forbidden_userflags)==0)) { |
| 512 | // remap key |
| 513 | if (current_layout[key*layout_pages+2+cplane]!=0) { |
| 514 | // check if command-bit is set for this plane |
| 515 | bool is_command=((current_layout[key*layout_pages+layout_pages-2]>>(cplane+2))&1)!=0; |
| 516 | if (this->map_key(key, current_layout[key*layout_pages+2+cplane], |
| 517 | is_command, is_special_pair)) return true; |
| 518 | } else break; // abort plane checking |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | if (diacritics_character>0) { |
| 523 | // ignore state-changing keys |
| 524 | switch(key) { |
| 525 | case 0x1d: /* Ctrl Pressed */ |
| 526 | case 0x2a: /* Left Shift Pressed */ |
| 527 | case 0x36: /* Right Shift Pressed */ |
| 528 | case 0x38: /* Alt Pressed */ |
| 529 | case 0x3a: /* Caps Lock */ |
| 530 | case 0x45: /* Num Lock */ |
| 531 | case 0x46: /* Scroll Lock */ |
| 532 | break; |
| 533 | default: |
| 534 | if (diacritics_character-200>=diacritics_entries) { |
| 535 | diacritics_character=0; |
| 536 | return true; |
| 537 | } |
| 538 | Bit16u diacritics_start=0; |
| 539 | // search start of subtable |
| 540 | for (Bit16u i=0; i<diacritics_character-200; i++) |
| 541 | diacritics_start+=diacritics[diacritics_start+1]*2+2; |
| 542 | |
| 543 | BIOS_AddKeyToBuffer((Bit16u)(key<<8) | diacritics[diacritics_start]); |
| 544 | diacritics_character=0; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | bool keyboard_layout::map_key(Bitu key, Bit16u layouted_key, bool is_command, bool is_keypair) { |
| 552 | if (is_command) { |
| 553 | Bit8u key_command=(Bit8u)(layouted_key&0xff); |
| 554 | // check if diacritics-command |
| 555 | if ((key_command>=200) && (key_command<235)) { |
| 556 | // diacritics command |
| 557 | diacritics_character=key_command; |
| 558 | if (diacritics_character-200>=diacritics_entries) diacritics_character=0; |
| 559 | return true; |
| 560 | } else if ((key_command>=120) && (key_command<140)) { |
| 561 | // switch layout command |
| 562 | this->read_keyboard_file(key_command-119); |
| 563 | return true; |
| 564 | } else if ((key_command>=180) && (key_command<188)) { |
| 565 | // switch user key off |
| 566 | user_keys&=~(1<<(key_command-180)); |
| 567 | return true; |
| 568 | } else if ((key_command>=188) && (key_command<196)) { |
| 569 | // switch user key on |
| 570 | user_keys|=(1<<(key_command-188)); |
| 571 | return true; |
| 572 | } else if (key_command==160) return true; // nop command |
| 573 | } else { |
| 574 | // non-command |
| 575 | if (diacritics_character>0) { |
| 576 | if (diacritics_character-200>=diacritics_entries) diacritics_character = 0; |
| 577 | else { |
| 578 | Bit16u diacritics_start=0; |
| 579 | // search start of subtable |
| 580 | for (Bit16u i=0; i<diacritics_character-200; i++) |
| 581 | diacritics_start+=diacritics[diacritics_start+1]*2+2; |
| 582 | |
| 583 | Bit8u diacritics_length=diacritics[diacritics_start+1]; |
| 584 | diacritics_start+=2; |
| 585 | diacritics_character=0; // reset |
| 586 | |
| 587 | // search scancode |
| 588 | for (Bit16u i=0; i<diacritics_length; i++) { |
| 589 | if (diacritics[diacritics_start+i*2]==(layouted_key&0xff)) { |
| 590 | // add diacritics to keybuf |
| 591 | BIOS_AddKeyToBuffer((Bit16u)(key<<8) | diacritics[diacritics_start+i*2+1]); |
| 592 | return true; |
| 593 | } |
| 594 | } |
| 595 | // add standard-diacritics to keybuf |
| 596 | BIOS_AddKeyToBuffer((Bit16u)(key<<8) | diacritics[diacritics_start-2]); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | // add remapped key to keybuf |
| 601 | if (is_keypair) BIOS_AddKeyToBuffer(layouted_key); |
| 602 | else BIOS_AddKeyToBuffer((Bit16u)(key<<8) | (layouted_key&0xff)); |
| 603 | |
| 604 | return true; |
| 605 | } |
| 606 | return false; |
| 607 | } |
| 608 | |
| 609 | Bit16u keyboard_layout::extract_codepage(const char* keyboard_file_name) { |
| 610 | if (!strcmp(keyboard_file_name,"none")) return 437; |
| 611 | |
| 612 | Bit32u read_buf_size; |
| 613 | static Bit8u read_buf[65535]; |
| 614 | Bit32u start_pos=5; |
| 615 | |
| 616 | char nbuf[512]; |
| 617 | sprintf(nbuf, "%s.kl", keyboard_file_name); |
| 618 | FILE* tempfile = OpenDosboxFile(nbuf); |
| 619 | if (tempfile==NULL) { |
| 620 | // try keyboard layout libraries next |
| 621 | if ((start_pos=read_kcl_file("keyboard.sys",keyboard_file_name,true))) { |
| 622 | tempfile = OpenDosboxFile("keyboard.sys"); |
| 623 | } else if ((start_pos=read_kcl_file("keybrd2.sys",keyboard_file_name,true))) { |
| 624 | tempfile = OpenDosboxFile("keybrd2.sys"); |
| 625 | } else if ((start_pos=read_kcl_file("keybrd3.sys",keyboard_file_name,true))) { |
| 626 | tempfile = OpenDosboxFile("keybrd3.sys"); |
| 627 | } else if ((start_pos=read_kcl_file("keyboard.sys",keyboard_file_name,false))) { |
| 628 | tempfile = OpenDosboxFile("keyboard.sys"); |
| 629 | } else if ((start_pos=read_kcl_file("keybrd2.sys",keyboard_file_name,false))) { |
| 630 | tempfile = OpenDosboxFile("keybrd2.sys"); |
| 631 | } else if ((start_pos=read_kcl_file("keybrd3.sys",keyboard_file_name,false))) { |
| 632 | tempfile = OpenDosboxFile("keybrd3.sys"); |
| 633 | } else if ((start_pos=read_kcl_data(layout_keyboardsys,33196,keyboard_file_name,true))) { |
| 634 | read_buf_size=0; |
| 635 | for (Bitu ct=start_pos+2; ct<33196; ct++) read_buf[read_buf_size++]=layout_keyboardsys[ct]; |
| 636 | } else if ((start_pos=read_kcl_data(layout_keybrd2sys,25431,keyboard_file_name,true))) { |
| 637 | read_buf_size=0; |
| 638 | for (Bitu ct=start_pos+2; ct<25431; ct++) read_buf[read_buf_size++]=layout_keybrd2sys[ct]; |
| 639 | } else if ((start_pos=read_kcl_data(layout_keybrd3sys,27122,keyboard_file_name,true))) { |
| 640 | read_buf_size=0; |
| 641 | for (Bitu ct=start_pos+2; ct<27122; ct++) read_buf[read_buf_size++]=layout_keybrd3sys[ct]; |
| 642 | } else if ((start_pos=read_kcl_data(layout_keyboardsys,33196,keyboard_file_name,false))) { |
| 643 | read_buf_size=0; |
| 644 | for (Bitu ct=start_pos+2; ct<33196; ct++) read_buf[read_buf_size++]=layout_keyboardsys[ct]; |
| 645 | } else if ((start_pos=read_kcl_data(layout_keybrd2sys,25431,keyboard_file_name,false))) { |
| 646 | read_buf_size=0; |
| 647 | for (Bitu ct=start_pos+2; ct<25431; ct++) read_buf[read_buf_size++]=layout_keybrd2sys[ct]; |
| 648 | } else if ((start_pos=read_kcl_data(layout_keybrd3sys,27122,keyboard_file_name,false))) { |
| 649 | read_buf_size=0; |
| 650 | for (Bitu ct=start_pos+2; ct<27122; ct++) read_buf[read_buf_size++]=layout_keybrd3sys[ct]; |
| 651 | } else { |
| 652 | start_pos=0; |
| 653 | LOG(LOG_BIOS,LOG_ERROR)("Keyboard layout file %s not found",keyboard_file_name); |
| 654 | return 437; |
| 655 | } |
| 656 | if (tempfile) { |
| 657 | fseek(tempfile, start_pos+2, SEEK_SET); |
| 658 | read_buf_size=(Bit32u)fread(read_buf, sizeof(Bit8u), 65535, tempfile); |
| 659 | fclose(tempfile); |
| 660 | } |
| 661 | start_pos=0; |
| 662 | } else { |
| 663 | // check ID-bytes of file |
| 664 | Bit32u dr=(Bit32u)fread(read_buf, sizeof(Bit8u), 4, tempfile); |
| 665 | if ((dr<4) || (read_buf[0]!=0x4b) || (read_buf[1]!=0x4c) || (read_buf[2]!=0x46)) { |
| 666 | LOG(LOG_BIOS,LOG_ERROR)("Invalid keyboard layout file %s",keyboard_file_name); |
| 667 | return 437; |
| 668 | } |
| 669 | |
| 670 | fseek(tempfile, 0, SEEK_SET); |
| 671 | read_buf_size=(Bit32u)fread(read_buf, sizeof(Bit8u), 65535, tempfile); |
| 672 | fclose(tempfile); |
| 673 | } |
| 674 | |
| 675 | Bit8u data_len,submappings; |
| 676 | data_len=read_buf[start_pos++]; |
| 677 | |
| 678 | start_pos+=data_len; // start_pos==absolute position of KeybCB block |
| 679 | |
| 680 | submappings=read_buf[start_pos]; |
| 681 | |
| 682 | // check all submappings and use them if general submapping or same codepage submapping |
| 683 | for (Bit16u sub_map=0; (sub_map<submappings); sub_map++) { |
| 684 | Bit16u submap_cp; |
| 685 | |
| 686 | // read codepage of submapping |
| 687 | submap_cp=host_readw(&read_buf[start_pos+0x14+sub_map*8]); |
| 688 | |
| 689 | if (submap_cp!=0) return submap_cp; |
| 690 | } |
| 691 | return 437; |
| 692 | } |
| 693 | |
| 694 | Bitu keyboard_layout::read_codepage_file(const char* codepage_file_name, Bit32s codepage_id) { |
| 695 | char cp_filename[512]; |
| 696 | strcpy(cp_filename, codepage_file_name); |
| 697 | if (!strcmp(cp_filename,"none")) return KEYB_NOERROR; |
| 698 | |
| 699 | if (codepage_id==dos.loaded_codepage) return KEYB_NOERROR; |
| 700 | |
| 701 | if (!strcmp(cp_filename,"auto")) { |
| 702 | // select matching .cpi-file for specified codepage |
| 703 | switch (codepage_id) { |
| 704 | case 437: case 850: case 852: case 853: case 857: case 858: |
| 705 | sprintf(cp_filename, "EGA.CPI"); break; |
| 706 | case 775: case 859: case 1116: case 1117: |
| 707 | sprintf(cp_filename, "EGA2.CPI"); break; |
| 708 | case 771: case 772: case 808: case 855: case 866: case 872: |
| 709 | sprintf(cp_filename, "EGA3.CPI"); break; |
| 710 | case 848: case 849: case 1125: case 1131: case 61282: |
| 711 | sprintf(cp_filename, "EGA4.CPI"); break; |
| 712 | case 737: case 851: case 869: |
| 713 | sprintf(cp_filename, "EGA5.CPI"); break; |
| 714 | case 113: case 899: case 59829: case 60853: |
| 715 | sprintf(cp_filename, "EGA6.CPI"); break; |
| 716 | case 58152: case 58210: case 59234: case 60258: case 62306: |
| 717 | sprintf(cp_filename, "EGA7.CPI"); break; |
| 718 | case 770: case 773: case 774: case 777: case 778: |
| 719 | sprintf(cp_filename, "EGA8.CPI"); break; |
| 720 | case 860: case 861: case 863: case 865: |
| 721 | sprintf(cp_filename, "EGA9.CPI"); break; |
| 722 | case 667: case 668: case 790: case 867: case 991: case 57781: |
| 723 | sprintf(cp_filename, "EGA10.CPI"); break; |
| 724 | default: |
| 725 | LOG_MSG("No matching cpi file for codepage %i",codepage_id); |
| 726 | return KEYB_INVALIDCPFILE; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | Bit32u start_pos; |
| 731 | Bit16u number_of_codepages; |
| 732 | |
| 733 | char nbuf[512]; |
| 734 | sprintf(nbuf, "%s", cp_filename); |
| 735 | FILE* tempfile=OpenDosboxFile(nbuf); |
| 736 | if (tempfile==NULL) { |
| 737 | size_t strsz=strlen(nbuf); |
| 738 | if (strsz) { |
| 739 | char plc=(char)toupper(*reinterpret_cast<unsigned char*>(&nbuf[strsz-1])); |
| 740 | if (plc=='I') { |
| 741 | // try CPX-extension as well |
| 742 | nbuf[strsz-1]='X'; |
| 743 | tempfile=OpenDosboxFile(nbuf); |
| 744 | } else if (plc=='X') { |
| 745 | // try CPI-extension as well |
| 746 | nbuf[strsz-1]='I'; |
| 747 | tempfile=OpenDosboxFile(nbuf); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | static Bit8u cpi_buf[65536]; |
| 753 | Bit32u cpi_buf_size=0,size_of_cpxdata=0;; |
| 754 | bool upxfound=false; |
| 755 | Bit16u found_at_pos=5; |
| 756 | if (tempfile==NULL) { |
| 757 | // check if build-in codepage is available |
| 758 | switch (codepage_id) { |
| 759 | case 437: case 850: case 852: case 853: case 857: case 858: |
| 760 | for (Bitu bct=0; bct<6322; bct++) cpi_buf[bct]=font_ega_cpx[bct]; |
| 761 | cpi_buf_size=6322; |
| 762 | break; |
| 763 | case 771: case 772: case 808: case 855: case 866: case 872: |
| 764 | for (Bitu bct=0; bct<5455; bct++) cpi_buf[bct]=font_ega3_cpx[bct]; |
| 765 | cpi_buf_size=5455; |
| 766 | break; |
| 767 | case 737: case 851: case 869: |
| 768 | for (Bitu bct=0; bct<5720; bct++) cpi_buf[bct]=font_ega5_cpx[bct]; |
| 769 | cpi_buf_size=5720; |
| 770 | break; |
| 771 | default: |
| 772 | return KEYB_INVALIDCPFILE; |
| 773 | break; |
| 774 | } |
| 775 | upxfound=true; |
| 776 | found_at_pos=0x29; |
| 777 | size_of_cpxdata=cpi_buf_size; |
| 778 | } else { |
| 779 | Bit32u dr=(Bit32u)fread(cpi_buf, sizeof(Bit8u), 5, tempfile); |
| 780 | // check if file is valid |
| 781 | if (dr<5) { |
| 782 | LOG(LOG_BIOS,LOG_ERROR)("Codepage file %s invalid",cp_filename); |
| 783 | return KEYB_INVALIDCPFILE; |
| 784 | } |
| 785 | // check if non-compressed cpi file |
| 786 | if ((cpi_buf[0]!=0xff) || (cpi_buf[1]!=0x46) || (cpi_buf[2]!=0x4f) || |
| 787 | (cpi_buf[3]!=0x4e) || (cpi_buf[4]!=0x54)) { |
| 788 | // check if dr-dos custom cpi file |
| 789 | if ((cpi_buf[0]==0x7f) && (cpi_buf[1]!=0x44) && (cpi_buf[2]!=0x52) && |
| 790 | (cpi_buf[3]!=0x46) && (cpi_buf[4]!=0x5f)) { |
| 791 | LOG(LOG_BIOS,LOG_ERROR)("Codepage file %s has unsupported DR-DOS format",cp_filename); |
| 792 | return KEYB_INVALIDCPFILE; |
| 793 | } |
| 794 | // check if compressed cpi file |
| 795 | Bit8u next_byte=0; |
| 796 | for (Bitu i=0; i<100; i++) { |
| 797 | fread(&next_byte, sizeof(Bit8u), 1, tempfile); found_at_pos++; |
| 798 | while (next_byte==0x55) { |
| 799 | fread(&next_byte, sizeof(Bit8u), 1, tempfile); found_at_pos++; |
| 800 | if (next_byte==0x50) { |
| 801 | fread(&next_byte, sizeof(Bit8u), 1, tempfile); found_at_pos++; |
| 802 | if (next_byte==0x58) { |
| 803 | fread(&next_byte, sizeof(Bit8u), 1, tempfile); found_at_pos++; |
| 804 | if (next_byte==0x21) { |
| 805 | // read version ID |
| 806 | fread(&next_byte, sizeof(Bit8u), 1, tempfile); |
| 807 | found_at_pos++; |
| 808 | upxfound=true; |
| 809 | break; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | if (upxfound) break; |
| 815 | } |
| 816 | if (!upxfound) { |
| 817 | LOG(LOG_BIOS,LOG_ERROR)("Codepage file %s invalid: %x",cp_filename,cpi_buf[0]); |
| 818 | return KEYB_INVALIDCPFILE; |
| 819 | } else { |
| 820 | if (next_byte<10) E_Exit("UPX-compressed cpi file, but upx-version too old"); |
| 821 | |
| 822 | // read in compressed CPX-file |
| 823 | fseek(tempfile, 0, SEEK_SET); |
| 824 | size_of_cpxdata=(Bitu)fread(cpi_buf, sizeof(Bit8u), 65536, tempfile); |
| 825 | } |
| 826 | } else { |
| 827 | // standard uncompressed cpi-file |
| 828 | fseek(tempfile, 0, SEEK_SET); |
| 829 | cpi_buf_size=(Bit32u)fread(cpi_buf, sizeof(Bit8u), 65536, tempfile); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | if (upxfound) { |
| 834 | if (size_of_cpxdata>0xfe00) E_Exit("Size of cpx-compressed data too big"); |
| 835 | |
| 836 | found_at_pos+=19; |
| 837 | // prepare for direct decompression |
| 838 | cpi_buf[found_at_pos]=0xcb; |
| 839 | |
| 840 | Bit16u seg=0; |
| 841 | Bit16u size=0x1500; |
| 842 | if (!DOS_AllocateMemory(&seg,&size)) E_Exit("Not enough free low memory to unpack data"); |
| 843 | MEM_BlockWrite((seg<<4)+0x100,cpi_buf,size_of_cpxdata); |
| 844 | |
| 845 | // setup segments |
| 846 | Bit16u save_ds=SegValue(ds); |
| 847 | Bit16u save_es=SegValue(es); |
| 848 | Bit16u save_ss=SegValue(ss); |
| 849 | Bit32u save_esp=reg_esp; |
| 850 | SegSet16(ds,seg); |
| 851 | SegSet16(es,seg); |
| 852 | SegSet16(ss,seg+0x1000); |
| 853 | reg_esp=0xfffe; |
| 854 | |
| 855 | // let UPX unpack the file |
| 856 | CALLBACK_RunRealFar(seg,0x100); |
| 857 | |
| 858 | SegSet16(ds,save_ds); |
| 859 | SegSet16(es,save_es); |
| 860 | SegSet16(ss,save_ss); |
| 861 | reg_esp=save_esp; |
| 862 | |
| 863 | // get unpacked content |
| 864 | MEM_BlockRead((seg<<4)+0x100,cpi_buf,65536); |
| 865 | cpi_buf_size=65536; |
| 866 | |
| 867 | DOS_FreeMemory(seg); |
| 868 | } |
| 869 | |
| 870 | |
| 871 | start_pos=host_readd(&cpi_buf[0x13]); |
| 872 | number_of_codepages=host_readw(&cpi_buf[start_pos]); |
| 873 | start_pos+=4; |
| 874 | |
| 875 | // search if codepage is provided by file |
| 876 | for (Bit16u test_codepage=0; test_codepage<number_of_codepages; test_codepage++) { |
| 877 | Bit16u device_type, font_codepage, font_type; |
| 878 | |
| 879 | // device type can be display/printer (only the first is supported) |
| 880 | device_type=host_readw(&cpi_buf[start_pos+0x04]); |
| 881 | font_codepage=host_readw(&cpi_buf[start_pos+0x0e]); |
| 882 | |
| 883 | Bit32u font_data_header_pt; |
| 884 | font_data_header_pt=host_readd(&cpi_buf[start_pos+0x16]); |
| 885 | |
| 886 | font_type=host_readw(&cpi_buf[font_data_header_pt]); |
| 887 | |
| 888 | if ((device_type==0x0001) && (font_type==0x0001) && (font_codepage==codepage_id)) { |
| 889 | // valid/matching codepage found |
| 890 | |
| 891 | Bit16u number_of_fonts,font_data_length; |
| 892 | number_of_fonts=host_readw(&cpi_buf[font_data_header_pt+0x02]); |
| 893 | font_data_length=host_readw(&cpi_buf[font_data_header_pt+0x04]); |
| 894 | |
| 895 | bool font_changed=false; |
| 896 | Bit32u font_data_start=font_data_header_pt+0x06; |
| 897 | |
| 898 | // load all fonts if possible |
| 899 | for (Bit16u current_font=0; current_font<number_of_fonts; current_font++) { |
| 900 | Bit8u font_height=cpi_buf[font_data_start]; |
| 901 | font_data_start+=6; |
| 902 | if (font_height==0x10) { |
| 903 | // 16x8 font |
| 904 | PhysPt font16pt=Real2Phys(int10.rom.font_16); |
| 905 | for (Bitu i=0;i<256*16;i++) { |
| 906 | phys_writeb(font16pt+i,cpi_buf[font_data_start+i]); |
| 907 | } |
| 908 | font_changed=true; |
| 909 | } else if (font_height==0x0e) { |
| 910 | // 14x8 font |
| 911 | PhysPt font14pt=Real2Phys(int10.rom.font_14); |
| 912 | for (Bitu i=0;i<256*14;i++) { |
| 913 | phys_writeb(font14pt+i,cpi_buf[font_data_start+i]); |
| 914 | } |
| 915 | font_changed=true; |
| 916 | } else if (font_height==0x08) { |
| 917 | // 8x8 fonts |
| 918 | PhysPt font8pt=Real2Phys(int10.rom.font_8_first); |
| 919 | for (Bitu i=0;i<128*8;i++) { |
| 920 | phys_writeb(font8pt+i,cpi_buf[font_data_start+i]); |
| 921 | } |
| 922 | font8pt=Real2Phys(int10.rom.font_8_second); |
| 923 | for (Bitu i=0;i<128*8;i++) { |
| 924 | phys_writeb(font8pt+i,cpi_buf[font_data_start+i+128*8]); |
| 925 | } |
| 926 | font_changed=true; |
| 927 | } |
| 928 | font_data_start+=font_height*256; |
| 929 | } |
| 930 | |
| 931 | LOG(LOG_BIOS,LOG_NORMAL)("Codepage %i successfully loaded",codepage_id); |
| 932 | |
| 933 | // set codepage entries |
| 934 | dos.loaded_codepage=(Bit16u)(codepage_id&0xffff); |
| 935 | |
| 936 | // update font if necessary |
| 937 | if (font_changed && (CurMode->type==M_TEXT) && (IS_EGAVGA_ARCH)) { |
| 938 | INT10_ReloadFont(); |
| 939 | } |
| 940 | INT10_SetupRomMemoryChecksum(); |
| 941 | |
| 942 | return KEYB_NOERROR; |
| 943 | } |
| 944 | |
| 945 | start_pos=host_readd(&cpi_buf[start_pos]); |
| 946 | start_pos+=2; |
| 947 | } |
| 948 | |
| 949 | LOG(LOG_BIOS,LOG_ERROR)("Codepage %i not found",codepage_id); |
| 950 | |
| 951 | return KEYB_INVALIDCPFILE; |
| 952 | } |
| 953 | |
| 954 | Bitu keyboard_layout::switch_keyboard_layout(const char* new_layout, keyboard_layout*& created_layout, Bit32s& tried_cp) { |
| 955 | if (strncasecmp(new_layout,"US",2)) { |
| 956 | // switch to a foreign layout |
| 957 | char tbuf[256]; |
| 958 | strcpy(tbuf, new_layout); |
| 959 | size_t newlen=strlen(tbuf); |
| 960 | |
| 961 | bool language_code_found=false; |
| 962 | // check if language code is present in loaded foreign layout |
| 963 | for (Bitu i=0; i<language_code_count; i++) { |
| 964 | if (!strncasecmp(tbuf,language_codes[i],newlen)) { |
| 965 | language_code_found=true; |
| 966 | break; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | if (language_code_found) { |
| 971 | if (!this->use_foreign_layout) { |
| 972 | // switch to foreign layout |
| 973 | this->use_foreign_layout=true; |
| 974 | diacritics_character=0; |
| 975 | LOG(LOG_BIOS,LOG_NORMAL)("Switched to layout %s",tbuf); |
| 976 | } |
| 977 | } else { |
| 978 | keyboard_layout * temp_layout=new keyboard_layout(); |
| 979 | Bitu req_codepage=temp_layout->extract_codepage(new_layout); |
| 980 | tried_cp = req_codepage; |
| 981 | Bitu kerrcode=temp_layout->read_keyboard_file(new_layout, req_codepage); |
| 982 | if (kerrcode) { |
| 983 | delete temp_layout; |
| 984 | return kerrcode; |
| 985 | } |
| 986 | // ...else keyboard layout loaded successfully, change codepage accordingly |
| 987 | kerrcode=temp_layout->read_codepage_file("auto", req_codepage); |
| 988 | if (kerrcode) { |
| 989 | delete temp_layout; |
| 990 | return kerrcode; |
| 991 | } |
| 992 | // Everything went fine, switch to new layout |
| 993 | created_layout=temp_layout; |
| 994 | } |
| 995 | } else if (this->use_foreign_layout) { |
| 996 | // switch to the US layout |
| 997 | this->use_foreign_layout=false; |
| 998 | diacritics_character=0; |
| 999 | LOG(LOG_BIOS,LOG_NORMAL)("Switched to US layout"); |
| 1000 | } |
| 1001 | return KEYB_NOERROR; |
| 1002 | } |
| 1003 | |
| 1004 | void keyboard_layout::switch_foreign_layout() { |
| 1005 | this->use_foreign_layout=!this->use_foreign_layout; |
| 1006 | diacritics_character=0; |
| 1007 | if (this->use_foreign_layout) LOG(LOG_BIOS,LOG_NORMAL)("Switched to foreign layout"); |
| 1008 | else LOG(LOG_BIOS,LOG_NORMAL)("Switched to US layout"); |
| 1009 | } |
| 1010 | |
| 1011 | const char* keyboard_layout::get_layout_name() { |
| 1012 | // get layout name (language ID or NULL if default layout) |
| 1013 | if (use_foreign_layout) { |
| 1014 | if (strcmp(current_keyboard_file_name,"none") != 0) { |
| 1015 | return (const char*)¤t_keyboard_file_name; |
| 1016 | } |
| 1017 | } |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | |
| 1021 | const char* keyboard_layout::main_language_code() { |
| 1022 | if (language_codes) { |
| 1023 | return language_codes[0]; |
| 1024 | } |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
| 1028 | |
| 1029 | static keyboard_layout* loaded_layout=NULL; |
| 1030 | |
| 1031 | // CTRL-ALT-F2 switches between foreign and US-layout using this function |
| 1032 | /* static void switch_keyboard_layout(bool pressed) { |
| 1033 | if (!pressed) |
| 1034 | return; |
| 1035 | if (loaded_layout) loaded_layout->switch_foreign_layout(); |
| 1036 | } */ |
| 1037 | |
| 1038 | // called by int9-handler |
| 1039 | bool DOS_LayoutKey(Bitu key, Bit8u flags1, Bit8u flags2, Bit8u flags3) { |
| 1040 | if (loaded_layout) return loaded_layout->layout_key(key, flags1, flags2, flags3); |
| 1041 | else return false; |
| 1042 | } |
| 1043 | |
| 1044 | Bitu DOS_LoadKeyboardLayout(const char * layoutname, Bit32s codepage, const char * codepagefile) { |
| 1045 | keyboard_layout * temp_layout=new keyboard_layout(); |
| 1046 | // try to read the layout for the specified codepage |
| 1047 | Bitu kerrcode=temp_layout->read_keyboard_file(layoutname, codepage); |
| 1048 | if (kerrcode) { |
| 1049 | delete temp_layout; |
| 1050 | return kerrcode; |
| 1051 | } |
| 1052 | // ...else keyboard layout loaded successfully, change codepage accordingly |
| 1053 | kerrcode=temp_layout->read_codepage_file(codepagefile, codepage); |
| 1054 | if (kerrcode) { |
| 1055 | delete temp_layout; |
| 1056 | return kerrcode; |
| 1057 | } |
| 1058 | // Everything went fine, switch to new layout |
| 1059 | loaded_layout=temp_layout; |
| 1060 | return KEYB_NOERROR; |
| 1061 | } |
| 1062 | |
| 1063 | Bitu DOS_SwitchKeyboardLayout(const char* new_layout, Bit32s& tried_cp) { |
| 1064 | if (loaded_layout) { |
| 1065 | keyboard_layout* changed_layout=NULL; |
| 1066 | Bitu ret_code=loaded_layout->switch_keyboard_layout(new_layout, changed_layout, tried_cp); |
| 1067 | if (changed_layout) { |
| 1068 | // Remove old layout, activate new layout |
| 1069 | delete loaded_layout; |
| 1070 | loaded_layout=changed_layout; |
| 1071 | } |
| 1072 | return ret_code; |
| 1073 | } else return 0xff; |
| 1074 | } |
| 1075 | |
| 1076 | // get currently loaded layout name (NULL if no layout is loaded) |
| 1077 | const char* DOS_GetLoadedLayout(void) { |
| 1078 | if (loaded_layout) { |
| 1079 | return loaded_layout->get_layout_name(); |
| 1080 | } |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | class DOS_KeyboardLayout: public Module_base { |
| 1086 | public: |
| 1087 | DOS_KeyboardLayout(Section* configuration):Module_base(configuration){ |
| 1088 | Section_prop * section=static_cast<Section_prop *>(configuration); |
| 1089 | dos.loaded_codepage=437; // US codepage already initialized |
| 1090 | loaded_layout=new keyboard_layout(); |
| 1091 | |
| 1092 | const char * layoutname=section->Get_string("keyboardlayout"); |
| 1093 | |
| 1094 | Bits wants_dos_codepage = -1; |
| 1095 | if (!strncmp(layoutname,"auto",4)) { |
| 1096 | #if defined (WIN32) |
| 1097 | WORD cur_kb_layout = LOWORD(GetKeyboardLayout(0)); |
| 1098 | WORD cur_kb_subID = 0; |
| 1099 | char layoutID_string[KL_NAMELENGTH]; |
| 1100 | if (GetKeyboardLayoutName(layoutID_string)) { |
| 1101 | if (strlen(layoutID_string) == 8) { |
| 1102 | int cur_kb_layout_by_name = ConvHexWord((char*)&layoutID_string[4]); |
| 1103 | layoutID_string[4] = 0; |
| 1104 | int subID = ConvHexWord((char*)&layoutID_string[0]); |
| 1105 | if ((cur_kb_layout_by_name>0) && (cur_kb_layout_by_name<65536)) { |
| 1106 | // use layout ID extracted from the layout string |
| 1107 | cur_kb_layout = (WORD)cur_kb_layout_by_name; |
| 1108 | } |
| 1109 | if ((subID>=0) && (subID<100)) { |
| 1110 | // use sublanguage ID extracted from the layout string |
| 1111 | cur_kb_subID = (WORD)subID; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | // try to match emulated keyboard layout with host-keyboardlayout |
| 1116 | // codepage 437 (standard) is preferred |
| 1117 | switch (cur_kb_layout) { |
| 1118 | /* case 1026: |
| 1119 | layoutname = "bg241"; |
| 1120 | break; */ |
| 1121 | case 1029: |
| 1122 | layoutname = "cz243"; |
| 1123 | break; |
| 1124 | case 1030: |
| 1125 | layoutname = "dk"; |
| 1126 | break; |
| 1127 | case 1031: |
| 1128 | layoutname = "gr"; |
| 1129 | wants_dos_codepage = 437; |
| 1130 | break; |
| 1131 | case 1033: |
| 1132 | // US |
| 1133 | return; |
| 1134 | case 1032: |
| 1135 | layoutname = "gk"; |
| 1136 | break; |
| 1137 | case 1034: |
| 1138 | layoutname = "sp"; |
| 1139 | wants_dos_codepage = 437; |
| 1140 | break; |
| 1141 | case 1035: |
| 1142 | layoutname = "su"; |
| 1143 | wants_dos_codepage = 437; |
| 1144 | break; |
| 1145 | case 1036: |
| 1146 | layoutname = "fr"; |
| 1147 | wants_dos_codepage = 437; |
| 1148 | break; |
| 1149 | case 1038: |
| 1150 | if (cur_kb_subID==1) layoutname = "hu"; |
| 1151 | else layoutname = "hu208"; |
| 1152 | break; |
| 1153 | case 1039: |
| 1154 | layoutname = "is161"; |
| 1155 | break; |
| 1156 | case 1040: |
| 1157 | layoutname = "it"; |
| 1158 | wants_dos_codepage = 437; |
| 1159 | break; |
| 1160 | case 1043: |
| 1161 | layoutname = "nl"; |
| 1162 | wants_dos_codepage = 437; |
| 1163 | break; |
| 1164 | case 1044: |
| 1165 | layoutname = "no"; |
| 1166 | break; |
| 1167 | case 1045: |
| 1168 | layoutname = "pl"; |
| 1169 | break; |
| 1170 | case 1046: |
| 1171 | layoutname = "br"; |
| 1172 | wants_dos_codepage = 437; |
| 1173 | break; |
| 1174 | /* case 1048: |
| 1175 | layoutname = "ro446"; |
| 1176 | break; */ |
| 1177 | case 1049: |
| 1178 | layoutname = "ru"; |
| 1179 | wants_dos_codepage = 437; |
| 1180 | break; |
| 1181 | case 1050: |
| 1182 | layoutname = "hr"; |
| 1183 | break; |
| 1184 | case 1051: |
| 1185 | layoutname = "sk"; |
| 1186 | break; |
| 1187 | /* case 1052: |
| 1188 | layoutname = "sq448"; |
| 1189 | break; */ |
| 1190 | case 1053: |
| 1191 | layoutname = "sv"; |
| 1192 | wants_dos_codepage = 437; |
| 1193 | break; |
| 1194 | case 1055: |
| 1195 | layoutname = "tr"; |
| 1196 | break; |
| 1197 | case 1058: |
| 1198 | layoutname = "ur"; |
| 1199 | wants_dos_codepage = 437; |
| 1200 | break; |
| 1201 | case 1059: |
| 1202 | layoutname = "bl"; |
| 1203 | break; |
| 1204 | case 1060: |
| 1205 | layoutname = "si"; |
| 1206 | break; |
| 1207 | case 1061: |
| 1208 | layoutname = "et"; |
| 1209 | break; |
| 1210 | /* case 1062: |
| 1211 | layoutname = "lv"; |
| 1212 | break; */ |
| 1213 | /* case 1063: |
| 1214 | layoutname = "lt221"; |
| 1215 | break; */ |
| 1216 | /* case 1064: |
| 1217 | layoutname = "tj"; |
| 1218 | break; |
| 1219 | case 1066: |
| 1220 | layoutname = "vi"; |
| 1221 | break; |
| 1222 | case 1067: |
| 1223 | layoutname = "hy"; |
| 1224 | break; */ |
| 1225 | case 2055: |
| 1226 | layoutname = "sg"; |
| 1227 | wants_dos_codepage = 437; |
| 1228 | break; |
| 1229 | case 2070: |
| 1230 | layoutname = "po"; |
| 1231 | break; |
| 1232 | case 4108: |
| 1233 | layoutname = "sf"; |
| 1234 | wants_dos_codepage = 437; |
| 1235 | break; |
| 1236 | default: |
| 1237 | break; |
| 1238 | } |
| 1239 | #endif |
| 1240 | } |
| 1241 | |
| 1242 | bool extract_codepage = true; |
| 1243 | if (wants_dos_codepage>0) { |
| 1244 | if ((loaded_layout->read_codepage_file("auto", (Bitu)wants_dos_codepage)) == KEYB_NOERROR) { |
| 1245 | // preselected codepage was successfully loaded |
| 1246 | extract_codepage = false; |
| 1247 | } |
| 1248 | } |
| 1249 | if (extract_codepage) { |
| 1250 | // try to find a good codepage for the requested layout |
| 1251 | Bitu req_codepage = loaded_layout->extract_codepage(layoutname); |
| 1252 | loaded_layout->read_codepage_file("auto", req_codepage); |
| 1253 | } |
| 1254 | |
| 1255 | /* if (strncmp(layoutname,"auto",4) && strncmp(layoutname,"none",4)) { |
| 1256 | LOG_MSG("Loading DOS keyboard layout %s ...",layoutname); |
| 1257 | } */ |
| 1258 | if (loaded_layout->read_keyboard_file(layoutname, dos.loaded_codepage)) { |
| 1259 | if (strncmp(layoutname,"auto",4)) { |
| 1260 | LOG_MSG("Error loading keyboard layout %s",layoutname); |
| 1261 | } |
| 1262 | } else { |
| 1263 | const char* lcode = loaded_layout->main_language_code(); |
| 1264 | if (lcode) { |
| 1265 | LOG_MSG("DOS keyboard layout loaded with main language code %s for layout %s",lcode,layoutname); |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | ~DOS_KeyboardLayout(){ |
| 1271 | if ((dos.loaded_codepage!=437) && (CurMode->type==M_TEXT)) { |
| 1272 | INT10_ReloadRomFonts(); |
| 1273 | dos.loaded_codepage=437; // US codepage |
| 1274 | } |
| 1275 | if (loaded_layout) { |
| 1276 | delete loaded_layout; |
| 1277 | loaded_layout=NULL; |
| 1278 | } |
| 1279 | } |
| 1280 | }; |
| 1281 | |
| 1282 | static DOS_KeyboardLayout* test; |
| 1283 | |
| 1284 | void DOS_KeyboardLayout_ShutDown(Section* /*sec*/) { |
| 1285 | delete test; |
| 1286 | } |
| 1287 | |
| 1288 | void DOS_KeyboardLayout_Init(Section* sec) { |
| 1289 | test = new DOS_KeyboardLayout(sec); |
| 1290 | sec->AddDestroyFunction(&DOS_KeyboardLayout_ShutDown,true); |
| 1291 | // MAPPER_AddHandler(switch_keyboard_layout,MK_f2,MMOD1|MMOD2,"sw_layout","Switch Layout"); |
| 1292 | } |