2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
9 /*********************************************************************************************/
11 #define SHOWFLAGS 1 /* Set to 1 to show Flags in the keylist */
13 #include <libraries/kms.h>
15 #include <proto/alib.h>
16 #include <proto/dos.h>
17 #include <proto/intuition.h>
18 #include <proto/iffparse.h>
19 #include <proto/utility.h>
22 #include <aros/debug.h>
27 /*********************************************************************************************/
29 #define PREFS_PATH_ENVARC "ENVARC:SYS/input.prefs"
30 #define PREFS_PATH_ENV "ENV:SYS/input.prefs"
32 /*********************************************************************************************/
35 struct InputPrefs inputprefs
;
36 struct KMSPrefs kmsprefs
;
37 struct List keymap_list
;
39 /*********************************************************************************************/
41 static const struct nameexp layout_expansion_table
[] =
43 {"al" , "Albanian" , "Countries/Albania" },
44 {"usa" , "American" , "Countries/United_States" },
45 {"usa0" , "American" , "Countries/United_States" },
46 {"us" , "American" , "Countries/United_States" },
47 {"usx" , "American" , "Countries/United_States" },
48 {"col" , "Colemak (US)" , "Countries/United_States" },
49 {"d" , "Deutsch" , "Countries/Germany" },
50 {"b" , "Belge" , "Countries/Belgium" },
51 {"by" , "Belarussian" , "Countries/Belarus" },
52 {"br" , "Brasileiro" , "Countries/Brazil" },
53 {"gb" , "British" , "Countries/United_Kingdom"},
54 {"gbx" , "British Extended" , "Countries/United_Kingdom"},
55 {"bg" , "Bulgarian" , "Countries/Bulgaria" },
56 {"cdn" , "Canadien Français" , "Countries/Quebec" },
57 {"cz" , "Czech" , "Countries/Czech_Republic"},
58 {"dk" , "Dansk" , "Countries/Denmark" },
59 {"nl" , "Dutch" , "Countries/Netherlands" },
60 {"dvx" , "Dvorak" , "Misc/Unknown" },
61 {"usa2" , "Dvorak" , "Misc/Unknown" },
62 {"dvl" , "Dvorak Left-handed" , "Misc/Unknown" },
63 {"dvr" , "Dvorak Right-handed" , "Misc/Unknown" },
64 {"irl" , "English Ireland" , "Countries/Ireland" },
65 {"e" , "Español" , "Countries/Spain" },
66 {"sp" , "Español no deadkeys" , "Countries/Spain" },
67 {"est" , "Estonian" , "Countries/Estonia" },
68 {"fin" , "Finnish" , "Countries/Finland" },
69 {"f" , "Français" , "Countries/France" },
70 {"gr" , "Hellenic" , "Countries/Greece" },
71 {"hr" , "Hrvatski" , "Countries/Croatia" },
72 {"is" , "Íslenska" , "Countries/Iceland" },
73 {"i" , "Italiana" , "Countries/Italy" },
74 {"la" , "Latin American" , "Misc/Unknown" },
75 {"lv" , "Latvian" , "Countries/Latvia" },
76 {"lt" , "Lithuanian" , "Countries/Lithuania" },
77 {"h" , "Magyar" , "Countries/Hungary" },
78 {"n" , "Norsk" , "Countries/Norway" },
79 {"pl" , "Polski" , "Countries/Poland" },
80 {"p" , "Português" , "Countries/Portugal" },
81 {"rus" , "Russian" , "Countries/Russia" },
82 {"ch2" , "Suisse" , "Countries/Switzerland" },
83 {"sk" , "Slovak" , "Countries/Slovakia" },
84 {"ch1" , "Schweizer" , "Countries/Switzerland" },
85 {"s" , "Svenskt" , "Countries/Sweden" },
86 {"tr" , "Türkçe" , "Countries/Turkey" },
87 {"ua" , "Ukranian" , "Countries/Ukraine" },
91 static const struct typeexp type_expansion_table
[] = {
99 /*********************************************************************************************/
101 static BOOL
Prefs_Load(STRPTR from
)
105 BPTR fh
= Open(from
, MODE_OLDFILE
);
108 retval
= Prefs_ImportFH(fh
);
115 /*********************************************************************************************/
117 static void ExpandName(struct ListviewEntry
*entry
)
121 const struct nameexp
*exp
;
123 strcpy(entry
->layoutname
, entry
->realname
);
125 sp
= strchr(entry
->realname
, '_');
129 * We have several variants of US keyboard, so
130 * we append type name.
132 if ((sp
[1] == 'u' && sp
[2] == 's'))
134 const struct typeexp
*te
;
137 for (te
= type_expansion_table
; te
->shortname
; te
++)
139 if (!strcmp(entry
->realname
, te
->shortname
))
150 sp
= entry
->realname
;
152 for (exp
= layout_expansion_table
; exp
->shortname
; exp
++)
154 if (stricmp(exp
->shortname
, sp
) == 0)
157 snprintf(entry
->layoutname
, sizeof(entry
->layoutname
), "%s (%s)", exp
->longname
, type
);
159 strcpy(entry
->layoutname
, exp
->longname
);
161 if (exp
->flag
!= NULL
)
164 sprintf(entry
->displayflag
, "\033I[5:Locale:Flags/%s]", exp
->flag
);
166 entry
->displayflag
[0] = '\0';
176 /*********************************************************************************************/
179 void SortInNode(struct List
*list
, struct Node
*node
)
181 struct Node
*sort
, *prev
= NULL
;
183 ForeachNode(list
, sort
)
185 if (Stricmp(node
->ln_Name
, sort
->ln_Name
) < 0) break;
189 Insert(list
, node
, prev
);
192 void Prefs_ScanDirectory(STRPTR pattern
, struct List
*list
, LONG entrysize
)
194 struct AnchorPath ap
;
195 struct ListviewEntry
*entry
, *entry2
;
196 struct List templist
;
200 memset(&ap
, 0, sizeof(ap
));
204 * Add default keymap.
205 * CHECKME: possibly too hacky. May be we should verify its presence in keymap.resource?
206 * How is it done in classic AmigaOS(tm) ?
208 entry
= AllocPooled(mempool
, entrysize
);
211 strcpy(entry
->layoutname
, "American (Default)");
212 strcpy(entry
->realname
, DEFAULT_KEYMAP
);
214 strcpy(entry
->displayflag
, "\033I[5:Locale:Flags/Countries/United_States]");
216 entry
->displayflag
[0] = '\0';
219 entry
->node
.ln_Name
= entry
->layoutname
;
220 AddTail(&templist
, &entry
->node
);
223 error
= MatchFirst(pattern
, &ap
);
226 if (ap
.ap_Info
.fib_DirEntryType
< 0)
228 entry
= (struct ListviewEntry
*)AllocPooled((APTR
)mempool
, entrysize
);
231 entry
->node
.ln_Name
= entry
->layoutname
;
232 strncpy(entry
->realname
, ap
.ap_Info
.fib_FileName
, sizeof(entry
->realname
));
234 sp
= strchr(entry
->realname
, '_');
238 sp
= entry
->realname
;
239 strcpy(entry
->layoutname
, sp
);
242 AddTail(&templist
, &entry
->node
);
245 error
= MatchNext(&ap
);
249 /* Sort by Layout Name */
251 ForeachNodeSafe(&templist
, entry
, entry2
)
253 Remove(&entry
->node
);
254 SortInNode(list
, &entry
->node
);
258 /*********************************************************************************************/
260 static LONG stopchunks
[] =
266 BOOL
Prefs_ImportFH(BPTR fh
)
268 struct FileInputPrefs loadprefs
;
269 struct FileKMSPrefs loadkmsprefs
;
270 struct IFFHandle
*iff
;
274 memset(&loadprefs
, 0, sizeof(loadprefs
));
276 if ((iff
= AllocIFF()))
278 iff
->iff_Stream
= (IPTR
)fh
;
281 D(Printf("LoadPrefs: stream opened.\n"));
285 if (!OpenIFF(iff
, IFFF_READ
))
287 D(Printf("LoadPrefs: OpenIFF okay.\n"));
289 if (!StopChunks(iff
, stopchunks
, 2))
291 D(Printf("LoadPrefs: StopChunk okay.\n"));
294 while (!ParseIFF(iff
, IFFPARSE_SCAN
))
296 struct ContextNode
*cn
;
298 D(Printf("LoadPrefs: ParseIFF okay.\n"));
300 cn
= CurrentChunk(iff
);
306 D(Printf("LoadPrefs: INPT chunk\n"));
308 if (size
> sizeof(struct FileInputPrefs
))
309 size
= sizeof(struct FileInputPrefs
);
311 if (ReadChunkBytes(iff
, &loadprefs
, size
) == size
)
313 D(Printf("LoadPrefs: Reading chunk successful.\n"));
315 CopyMem(loadprefs
.ip_Keymap
, inputprefs
.ip_Keymap
, sizeof(loadprefs
.ip_Keymap
));
316 inputprefs
.ip_PointerTicks
= ARRAY_TO_WORD(loadprefs
.ip_PointerTicks
);
317 inputprefs
.ip_DoubleClick
.tv_secs
= ARRAY_TO_LONG(loadprefs
.ip_DoubleClick_secs
);
318 inputprefs
.ip_DoubleClick
.tv_micro
= ARRAY_TO_LONG(loadprefs
.ip_DoubleClick_micro
);
319 inputprefs
.ip_KeyRptDelay
.tv_secs
= ARRAY_TO_LONG(loadprefs
.ip_KeyRptDelay_secs
);
320 inputprefs
.ip_KeyRptDelay
.tv_micro
= ARRAY_TO_LONG(loadprefs
.ip_KeyRptDelay_micro
);
321 inputprefs
.ip_KeyRptSpeed
.tv_secs
= ARRAY_TO_LONG(loadprefs
.ip_KeyRptSpeed_secs
);
322 inputprefs
.ip_KeyRptSpeed
.tv_micro
= ARRAY_TO_LONG(loadprefs
.ip_KeyRptSpeed_micro
);
323 inputprefs
.ip_MouseAccel
= ARRAY_TO_WORD(loadprefs
.ip_MouseAccel
);
324 inputprefs
.ip_ClassicKeyboard
= ARRAY_TO_LONG(loadprefs
.ip_ClassicKeyboard
);
325 CopyMem(loadprefs
.ip_KeymapName
, inputprefs
.ip_KeymapName
, sizeof(loadprefs
.ip_KeymapName
));
326 inputprefs
.ip_SwitchMouseButtons
= loadprefs
.ip_SwitchMouseButtons
[3];
328 D(Printf("LoadPrefs: SwitchMouseButtons: %ld\n", inputprefs
.ip_SwitchMouseButtons
));
335 D(Printf("LoadPrefs: KMSW chunk\n"));
337 if (size
> sizeof(struct FileKMSPrefs
))
338 size
= sizeof(struct FileKMSPrefs
);
340 if (ReadChunkBytes(iff
, &loadkmsprefs
, size
) == size
)
342 D(Printf("LoadPrefs: Reading chunk successful.\n"));
344 kmsprefs
.kms_Enabled
= loadkmsprefs
.kms_Enabled
;
345 kmsprefs
.kms_Reserved
= loadkmsprefs
.kms_Reserved
;
346 kmsprefs
.kms_SwitchQual
= ARRAY_TO_WORD(loadkmsprefs
.kms_SwitchQual
);
347 kmsprefs
.kms_SwitchCode
= ARRAY_TO_WORD(loadkmsprefs
.kms_SwitchCode
);
348 CopyMem(loadkmsprefs
.kms_AltKeymap
, kmsprefs
.kms_AltKeymap
, sizeof(kmsprefs
.kms_AltKeymap
));
354 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
355 } /* if (!StopChunk(iff, ID_PREF, ID_INPT)) */
357 } /* if (!OpenIFF(iff, IFFF_READ)) */
358 } /* if (fh != NULL) */
360 } /* if ((iff = AllocIFF())) */
365 /*********************************************************************************************/
367 BOOL
Prefs_ExportFH(BPTR fh
)
369 struct FileInputPrefs saveprefs
;
370 struct FileKMSPrefs savekmsprefs
;
371 struct IFFHandle
*iff
;
374 BOOL delete_if_error
= FALSE
;
377 D(Printf("SavePrefs: SwitchMouseButtons: %ld\n", inputprefs
.ip_SwitchMouseButtons
));
379 CopyMem(inputprefs
.ip_Keymap
, saveprefs
.ip_Keymap
, sizeof(saveprefs
.ip_Keymap
));
380 WORD_TO_ARRAY(inputprefs
.ip_PointerTicks
, saveprefs
.ip_PointerTicks
);
381 LONG_TO_ARRAY(inputprefs
.ip_DoubleClick
.tv_secs
, saveprefs
.ip_DoubleClick_secs
);
382 LONG_TO_ARRAY(inputprefs
.ip_DoubleClick
.tv_micro
, saveprefs
.ip_DoubleClick_micro
);
383 LONG_TO_ARRAY(inputprefs
.ip_KeyRptDelay
.tv_secs
, saveprefs
.ip_KeyRptDelay_secs
);
384 LONG_TO_ARRAY(inputprefs
.ip_KeyRptDelay
.tv_micro
, saveprefs
.ip_KeyRptDelay_micro
);
385 LONG_TO_ARRAY(inputprefs
.ip_KeyRptSpeed
.tv_secs
, saveprefs
.ip_KeyRptSpeed_secs
);
386 LONG_TO_ARRAY(inputprefs
.ip_KeyRptSpeed
.tv_micro
, saveprefs
.ip_KeyRptSpeed_micro
);
387 WORD_TO_ARRAY(inputprefs
.ip_MouseAccel
, saveprefs
.ip_MouseAccel
);
388 LONG_TO_ARRAY(inputprefs
.ip_ClassicKeyboard
, saveprefs
.ip_ClassicKeyboard
);
389 CopyMem(inputprefs
.ip_KeymapName
, saveprefs
.ip_KeymapName
, sizeof(saveprefs
.ip_KeymapName
));
390 saveprefs
.ip_SwitchMouseButtons
[0] = 0;
391 saveprefs
.ip_SwitchMouseButtons
[1] = 0;
392 saveprefs
.ip_SwitchMouseButtons
[2] = 0;
393 saveprefs
.ip_SwitchMouseButtons
[3] = inputprefs
.ip_SwitchMouseButtons
;
395 savekmsprefs
.kms_Enabled
= kmsprefs
.kms_Enabled
;
396 savekmsprefs
.kms_Reserved
= kmsprefs
.kms_Reserved
;
397 WORD_TO_ARRAY(kmsprefs
.kms_SwitchQual
, savekmsprefs
.kms_SwitchQual
);
398 WORD_TO_ARRAY(kmsprefs
.kms_SwitchCode
, savekmsprefs
.kms_SwitchCode
);
399 CopyMem(kmsprefs
.kms_AltKeymap
, savekmsprefs
.kms_AltKeymap
, sizeof(savekmsprefs
.kms_AltKeymap
));
401 if ((iff
= AllocIFF()))
403 iff
->iff_Stream
= (IPTR
)fh
;
406 D(Printf("SavePrefs: stream opened.\n"));
409 delete_if_error
= TRUE
;
414 if (!OpenIFF(iff
, IFFF_WRITE
))
416 D(Printf("SavePrefs: OpenIFF okay.\n"));
418 if (!PushChunk(iff
, ID_PREF
, ID_FORM
, IFFSIZE_UNKNOWN
))
420 D(Printf("SavePrefs: PushChunk(FORM) okay.\n"));
422 if (!PushChunk(iff
, ID_PREF
, ID_PRHD
, sizeof(struct FilePrefHeader
)))
424 struct FilePrefHeader head
;
426 D(Printf("SavePrefs: PushChunk(PRHD) okay.\n"));
428 head
.ph_Version
= 0; // FIXME: should be PHV_CURRENT, but see <prefs/prefhdr.h>
433 head
.ph_Flags
[3] = 0;
435 if (WriteChunkBytes(iff
, &head
, sizeof(head
)) == sizeof(head
))
437 D(Printf("SavePrefs: WriteChunkBytes(PRHD) okay.\n"));
441 if (!PushChunk(iff
, ID_PREF
, ID_INPT
, sizeof(saveprefs
)))
443 D(Printf("SavePrefs: PushChunk(INPT) okay.\n"));
445 if (WriteChunkBytes(iff
, &saveprefs
, sizeof(saveprefs
)) == sizeof(saveprefs
))
447 D(Printf("SavePrefs: WriteChunkBytes(INPT) okay.\n"));
454 } /* if (!PushChunk(iff, ID_PREF, ID_INPT, sizeof(saveprefs))) */
456 if (!PushChunk(iff
, ID_PREF
, ID_KMSW
, sizeof(savekmsprefs
)))
458 if (WriteChunkBytes(iff
, &savekmsprefs
, sizeof(savekmsprefs
)) == sizeof(savekmsprefs
))
459 retval
= retval
&& TRUE
;
463 } /* if (!PushChunk(iff, ID_PREF, ID_INPT, sizeof(saveprefs))) */
465 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
471 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
475 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
479 } /* if (!OpenIFF(iff, IFFFWRITE)) */
482 } /* if (iff->iff_Stream)) */
486 } /* if ((iff = AllocIFF())) */
489 if (!retval
&& delete_if_error
)
491 DeleteFile(filename
);
498 /*********************************************************************************************/
500 BOOL
Prefs_HandleArgs(STRPTR from
, BOOL use
, BOOL save
)
506 if (!Prefs_Load(from
))
508 ShowMessage("Can't read from input file");
514 if (!Prefs_Load(PREFS_PATH_ENV
))
516 if (!Prefs_Load(PREFS_PATH_ENVARC
))
520 "Can't read from file " PREFS_PATH_ENVARC
521 ".\nUsing default values."
530 fh
= Open(PREFS_PATH_ENV
, MODE_NEWFILE
);
538 ShowMessage("Can't open " PREFS_PATH_ENV
" for writing.");
543 fh
= Open(PREFS_PATH_ENVARC
, MODE_NEWFILE
);
551 ShowMessage("Can't open " PREFS_PATH_ENVARC
" for writing.");
557 /*********************************************************************************************/
559 BOOL
Prefs_Default(void)
561 strcpy(inputprefs
.ip_Keymap
, DEFAULT_KEYMAP
);
562 inputprefs
.ip_PointerTicks
= 1;
563 inputprefs
.ip_DoubleClick
.tv_secs
= 0;
564 inputprefs
.ip_DoubleClick
.tv_micro
= 500000;
565 inputprefs
.ip_KeyRptDelay
.tv_secs
= 0;
566 inputprefs
.ip_KeyRptDelay
.tv_micro
= 500000;
567 inputprefs
.ip_KeyRptSpeed
.tv_secs
= 0;
568 inputprefs
.ip_KeyRptSpeed
.tv_micro
= 40000;
569 inputprefs
.ip_MouseAccel
= 1;
570 inputprefs
.ip_ClassicKeyboard
= 0;
571 strcpy(inputprefs
.ip_KeymapName
, DEFAULT_KEYMAP
);
572 inputprefs
.ip_SwitchMouseButtons
= FALSE
;
574 kmsprefs
.kms_Enabled
= FALSE
;
575 kmsprefs
.kms_SwitchQual
= KMS_QUAL_DISABLE
;
576 kmsprefs
.kms_SwitchCode
= KMS_CODE_NOKEY
;
577 kmsprefs
.kms_AltKeymap
[0] = 0;