-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeak.goc
2336 lines (1992 loc) · 64 KB
/
speak.goc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@include <stdapp.goh>
@include <foam.goh>
#include <gstring.h>
@include <internal/respondr.goh>
@include <internal/resp/eci.goh>
@include <internal/resp/indicato.goh>
#include <initfile.h>
/* header file object/gSystemC.goh is broken */
extern ClassStruct GenSystemClass;
#include <ansi/stdio.h>
#include <ansi/stdlib.h>
#include <ansi/string.h>
@include "common.goh"
#include "macroeng.h"
#define SPEAK_TRANSLATION_FILE "speaklst"
#define TALX_AUTO_EXEC NEC("extrapps\\TALX 9110") EC("extrapps\\EC TALX 9110")
#define ECHO_OFF 0x00
#define ECHO_WORDS 0x01
#define ECHO_LETTERS 0x02
#define SERIAL_LENGTH 11
#define POLLING_INTERVAL 20 // In ticks
#define POLLING_COUNT 45 // Polling duration (in intervals)
#define POLLING_SLOW_LIMIT 10 // After 10 intervals polling slows down
#define POLLING_SLOW_DIVIDER 3 // to 1/3 of its normal rate
#define PROGRESS_BEEP_INTERVALS 3 // Intervals between two "progress" beeps
#define VERSION "1.20"
@class SpeakProcessClass, GenProcessClass;
@message void MSG_SPEAK_PROC_TEST_SERIAL();
@message void MSG_SPEAK_PROC_ENABLE_AUTOSTART();
@message void MSG_SPEAK_PROC_DISABLE_AUTOSTART();
@message void MSG_SPEAK_PROC_EXIT();
@endc
/*
***************************************************************************
* UI Objects und tables
***************************************************************************
*/
struct keyTable_struct {
word key;
ByteFlags flags;
#define SPKALWAYS 0x01
#define NAVKEY 0x02
char text[10];
};
struct resTable_struct {
optr obj;
char text[11];
byte type;
#define RES_VM 1 // obj refers to a Gen object with a VisMoniker
#define RES_CM 2 // obj refers to a Gen object wit a ComplexMoniker
#define RES_CH 3 // obj refers to a @chunk char []
};
@include "speak_ui.goh"
/*
***************************************************************************
* Global variables
***************************************************************************
*/
/* Loaded search/replace table */
MemHandle repmh;
void LoadTranslation(void);
void UnloadTranslation(void);
void TranslateResources(void);
/* Event polling timer */
TimerHandle timerHandle;
word timerID;
word count, G_progressCount;
int timerEventType;
word lastKey;
Boolean speakContinue;
dword lastSpeakTick;
/* String for word-by-word speaking */
byte keystr[128];
word speakKeystr;
byte backspChar;
Boolean keyboardTraining;
Boolean G_longSpell, G_capsSpell, G_punctSpell;
/* Serial number handling */
TCHAR IMEI[17];
Boolean notDemo;
Boolean IMEIvalidated;
/* Current call type (one of INCT_INDICATOR_CALL_TYPE_...) */
word G_indicatorCallType;
ClassStruct *olbuttonclass;
/* Braille-related */
SynthType G_output, G_alternativeOutput;
Boolean G_brailleMode;
word G_brailleLine;
#define BRL_TITLE 1
#define BRL_FOCUS 2
#define BRL_TEXT 3
#define BRL_TREE 4
#define BRL_PSEUDOCUR 5
#define BRL_KEYS 6
#define BRL_APPKEYS 7
#define BRL_MIN BRL_TITLE
#define BRL_MAX BRL_APPKEYS
word G_brailleOffset, G_brTextStart;
Boolean G_brailleMore;
Boolean G_pseudoCursorMode;
optr G_brRoutingMarkObj;
/*
***************************************************************************
* Code for SpeakProcessClass
***************************************************************************
*/
@classdecl SpeakProcessClass;
void ReplaceCMOptr(optr obj, optr text)
{
ReplaceComplexMoniker rcm = {
0,0,0,
NULL,
NULL,
CMST_OPTR,
CMST_KEEP,
0, TRUE,
};
ReplaceComplexMonikerChunkHandles rcmch;
/* Update title of object by optr */
rcm.RCM_topTextSource = (dword)text;
@call obj::MSG_COMPLEX_MONIKER_REPLACE_MONIKER(&rcmch, &rcm);
}
void ReplaceCMText(optr obj, char *text)
{
ReplaceComplexMoniker rcm = {
0,0,0,
NULL,
NULL,
CMST_FPTR,
CMST_KEEP,
0, TRUE,
};
ReplaceComplexMonikerChunkHandles rcmch;
/* Update title of object by text */
rcm.RCM_topTextSource = (dword)text;
@call obj::MSG_COMPLEX_MONIKER_REPLACE_MONIKER(&rcmch, &rcm);
}
/* Send a simulated keystroke to the input manager */
void SynthesizeKey(byte cs, byte key, byte shift)
{
const char scanTab[] = "..1234567890..."
".qwertyuiop..."
".asdfghjkl...."
".yxcvbnm";
optr im = ConstructOptr(MacroGetInputProcess(), 0);
word codeArg = (((word)cs)<<8) | key;
word shArg = ((word)shift)<<8;
word scanArg = 0;
char *p;
/* Translate letters according to scan code table (for Ctrl codes) */
if(cs==CS_BSW)
{
p = strchr(scanTab, tolower(key));
if(p)
scanArg = (p-scanTab)<<8;
}
else if(cs==CS_CONTROL)
{
if(key==VC_LEFT || key==VC_HOME) scanArg = 0x4b00;
if(key==VC_RIGHT || key==VC_END) scanArg = 0x4d00;
if(key==VC_UP) scanArg = 0x4800;
if(key==VC_DOWN) scanArg = 0x5000;
}
/* Send key to input manager to simulate typing */
@send im::MSG_META_KBD_CHAR(codeArg, shArg | CF_FIRST_PRESS, scanArg);
@send im::MSG_META_KBD_CHAR(codeArg, shArg | CF_RELEASE, scanArg);
}
void TestAutostart(void)
{
MemHandle mh = NullHandle;
word size;
char *p;
Boolean autostart = FALSE;
// Find out if our name is already in the autostart sequence
if(!InitFileReadStringBlock("ui", "execOnStartup", &mh, IFRF_FIRST_ONLY, &size))
{
p = MemLock(mh);
if(strstr(p, TALX_AUTO_EXEC))
autostart = TRUE;
MemFree(mh);
}
/* Set triggers to allow changing of current autostart status */
if(autostart)
{
@send EnableAutostartTrigger::MSG_GEN_SET_NOT_USABLE(VUM_NOW);
@send DisableAutostartTrigger::MSG_GEN_SET_USABLE(VUM_NOW);
}
else
{
@send EnableAutostartTrigger::MSG_GEN_SET_USABLE(VUM_NOW);
@send DisableAutostartTrigger::MSG_GEN_SET_NOT_USABLE(VUM_NOW);
}
}
Boolean testSerial(char *serial)
{
int len,i;
struct {
unsigned long secret1;
char imei[17];
unsigned long secret2;
} compSerial;
union {
unsigned short words[2];
unsigned long longint;
} serialNumber;
char num[6];
# include <serial.inc>
/* Get previously determined IMEI */
strcpy(compSerial.imei, IMEI);
compSerial.secret1 = secret[0];
/* Pad out IMEI buffer with zeroes */
len = strlen(compSerial.imei);
while(len<sizeof(compSerial.imei))
compSerial.imei[len++] = 0;
/* Parse components of serial number */
for(i=0; i<2 && *serial; i++)
{
/* get up to 5 digits of one part */
for(len=0; isdigit(serial[len]) && len<5; len++)
num[len] = serial[len];
/* convert number */
num[len] = 0;
serialNumber.words[i] = atoi(num);
/* skip over non-digits */
for(; serial[len] && !isdigit(serial[len]); len++)
;
serial += len; // advance to next number
}
compSerial.secret2 = secret[1];
return (i==2 &&
calc_crc(&compSerial, sizeof(compSerial))==serialNumber.longint);
}
void ActivateDemo(void)
{
Boolean autostart = TRUE;
notDemo = TRUE;
ReplaceCMOptr(@MainBox, @TALXFull);
@send SerialDialog::MSG_GEN_INTERACTION_ACTIVATE_COMMAND(IC_DISMISS);
/* Enable autostart unless manually overridden */
InitFileReadBoolean("TALX", "autostart", &autostart);
if(autostart)
@send process::MSG_SPEAK_PROC_ENABLE_AUTOSTART();
}
void DeactivateDemo(void)
{
char title[80];
ReplaceComplexMoniker rcm = {
0,0,0,
NULL,
NULL,
CMST_FPTR,
CMST_KEEP,
0, TRUE,
};
ReplaceComplexMonikerChunkHandles rcmch;
notDemo = FALSE;
/* Create localizable string with IMEI */
MemLock(OptrToHandle(@SerialTitle));
sprintf(title, LMemDeref(@SerialTitle), IMEI);
MemUnlock(OptrToHandle(@SerialTitle));
/* Update title of dialog box */
rcm.RCM_topTextSource = (dword)title;
@call SerialBox::MSG_COMPLEX_MONIKER_REPLACE_MONIKER(&rcmch, &rcm);
/* Update title of window to read "demo" until proven wrong */
ReplaceCMOptr(@MainBox, @TALXDemo);
@send SerialDialog::MSG_GEN_INTERACTION_INITIATE();
}
void ValidateIMEI(void)
{
word read;
char serialNumber[SERIAL_LENGTH+1];
/* Do not try to get IMEI for 90 seconds after rebooting the phone,
to avoid getting into conflicts during startup */
if(TimerGetCount() < 90*60 || IMEIvalidated)
return;
/* Get IMEI from phone - if no IMEI is available, the user
is lucky, and no check is performed (for now) */
EciGetImei((byte *)IMEI);
if(!IMEI[0])
return; /* Wait if still unavailable */
*serialNumber = 0;
InitFileReadStringBuffer("TALX", "serial", serialNumber,
sizeof(serialNumber), &read);
/* If serial number is accepted, make sure the UI reflects this */
if(testSerial(serialNumber))
ActivateDemo();
else
DeactivateDemo();
/* Only ask once */
IMEIvalidated = TRUE;
}
@method SpeakProcessClass, MSG_META_NOTIFY
{
if(notificationType == GWNT_RESPONDER_NOTIFICATION &&
manufID == MANUFACTURER_ID_GEOWORKS)
{
switch(data)
{
case RNT_LID_OPEN:
@send SpeakApp::MSG_MAPP_READ_WINDOW(0);
break;
case RNT_LID_CLOSED:
/* Prevent further speaking in "automatic" mode */
if(speakContinue)
{
G_pseudoCursorPos = G_oldPseudoCursorPos;
speakContinue = FALSE;
}
SynthStop(TRUE);
/* See if someone fiddled with the IMEI */
ValidateIMEI();
break;
}
}
else if(notificationType == GWNT_INDICATOR_SET_ACTIVE_CALL &&
manufID == MANUFACTURER_ID_GEOWORKS)
{
/* Remember current call type */
G_indicatorCallType = (word)data;
}
@callsuper();
}
#pragma codeseg INIT_TEXT
@method SpeakProcessClass, MSG_GEN_PROCESS_OPEN_APPLICATION
{
optr myThread = ConstructOptr(GeodeGetProcessHandle(), NullChunk);
GeodeHandle rudy;
timerHandle = 0;
*keystr = 0;
backspChar = 0;
speakKeystr = 0;
notDemo = TRUE;
keyboardTraining = FALSE;
IMEIvalidated = FALSE;
speakContinue = FALSE;
*IMEI = 0;
lastSpeakTick = 0;
timerEventType = TET_NAV_KEY;
G_indicatorCallType = INCT_INDICATOR_CALL_TYPE_NONE;
G_recordingActive = FALSE;
G_recordingIndex = 0;
count = G_progressCount = 0;
/* Braille */
G_output = SPEECH_INTERNAL;
G_alternativeOutput = SPEECH_OFF;
G_brailleMode = FALSE;
G_brailleLine = BRL_MIN;
G_brailleWidth = 0;
G_brailleOffset = 0;
G_brailleMore = FALSE;
G_pseudoCursorMode = FALSE;
G_brRoutingMarkObj = NullOptr;
/* Load string translations and update text in resources accordingly */
LoadTranslation();
TranslateResources();
@callsuper();
/* Read alternative output from INI file */
InitFileReadInteger("TALX", "alternativeOutput", &G_alternativeOutput);
G_capsSpell = @call SpellCaps::MSG_GEN_ITEM_GROUP_GET_SELECTION();
G_longSpell = @call SpellLong::MSG_GEN_ITEM_GROUP_GET_SELECTION();
G_punctSpell = @call SpellPunct::MSG_GEN_ITEM_GROUP_GET_SELECTION();
if(@call Language::MSG_GEN_ITEM_GROUP_GET_SELECTION()!=LANG_NONE)
{
@call TextLanguageGroup::MSG_GEN_SET_ENABLED(VUM_DELAYED_VIA_APP_QUEUE);
}
gen_crc_table();
ValidateIMEI();
TestAutostart(); // ensure autostart UI is consistent
GCNListAdd(myThread, MANUFACTURER_ID_GEOWORKS, GCNSLT_RESPONDER_NOTIFICATIONS);
GCNListAdd(myThread, MANUFACTURER_ID_GEOWORKS, GCNSLT_NOTIFY_INDICATOR_EVENT);
MacroInit();
MacroSetHotkeys(hotkeys, @SpeakApp, MSG_MAPP_OTHER_KEY);
/* Unfortunately, the "rudy" SpecUI library doesn't export OLButtonClass,
which is private to the specific UI. Anyway, as we have to be able to
detect objects of this class reliably, we reconstruct the class pointer
based on another export in the same segment and the assumption that
the class structure starts at offset 0 in that segment. */
rudy = GeodeFind("rudy ",8,0,0);
olbuttonclass = (ClassStruct *)ConstructOptr(
SegmentOf(ProcGetLibraryEntry(rudy,30)), 0x0000);
}
@method SpeakProcessClass, MSG_SPEAK_PROC_ENABLE_AUTOSTART
{
/* Put us into autostart sequence */
UserAddAutoExec(TALX_AUTO_EXEC);
/* Remember this decision */
InitFileWriteBoolean("TALX", "autostart", TRUE);
InitFileCommit();
TestAutostart(); // update UI consistently
}
@method SpeakProcessClass, MSG_SPEAK_PROC_DISABLE_AUTOSTART
{
if(FoamDisplayQuestion(@ConfirmDisableAuto)==IC_YES)
{ // ask user for confirmation
/* Remove us from autostart sequence */
UserRemoveAutoExec(TALX_AUTO_EXEC);
/* Remember this decision */
InitFileWriteBoolean("TALX", "autostart", FALSE);
InitFileCommit();
TestAutostart(); // update UI consistently
}
}
@method SpeakProcessClass, MSG_SPEAK_PROC_EXIT
{
if(FoamDisplayQuestion(@ConfirmExit)==IC_YES)
{ // ask user for confirmation
@send SpeakApp::MSG_META_QUIT();
}
}
@method SpeakProcessClass, MSG_SPEAK_PROC_TEST_SERIAL
{
char serialNumber[SERIAL_LENGTH+1];
@call SerialTextField::MSG_VIS_TEXT_GET_ALL_PTR(serialNumber);
if(testSerial(serialNumber))
{
InitFileWriteString("TALX", "serial", serialNumber);
ActivateDemo();
InitFileCommit();
}
else
FoamDisplayNote(@InvalidSerial);
}
@method SpeakProcessClass, MSG_GEN_PROCESS_CLOSE_APPLICATION
{
optr myThread = ConstructOptr(GeodeGetProcessHandle(), NullChunk);
if(timerHandle)
{
TimerStop(timerHandle, timerID);
timerHandle = 0;
}
UnloadTranslation();
GCNListRemove(myThread, MANUFACTURER_ID_GEOWORKS, GCNSLT_RESPONDER_NOTIFICATIONS);
MacroDeinit();
return @callsuper();
}
/*
***************************************************************************
* Checksums
***************************************************************************
*/
unsigned long crc_table[256];
void gen_crc_table(void)
{
unsigned long crc, poly;
int i, j;
/* build the crc table */
poly = 0xEDB88320L;
for (i = 0; i < 256; i++)
{
crc = i;
for (j = 8; j > 0; j--)
{
if (crc & 1)
crc = (crc >> 1) ^ poly;
else
crc >>= 1;
}
crc_table[i] = crc;
}
}
#pragma codeseg
unsigned long calc_crc(byte *buf, int n)
{
int i;
unsigned long crc = 0xFFFFFFFF;
for(i=0; i<n; i++)
crc = ((crc>>8) & 0x00FFFFFF) ^ crc_table[ (crc^buf[i]) & 0xFF ];
return crc^0xFFFFFFFF;
}
/*
***************************************************************************
* Search/replace pre-processing of sentences
***************************************************************************
*/
#pragma codeseg INIT_TEXT
#define xdigit2int(c) \
((toupper((byte)(c))-'0') - (isalpha((byte)(c))?('A'-'0'-10):0))
void LoadTranslation(void)
{
FILE *f;
char line[256],*p,*s1,*s2;
char *repbuf;
word len,buflen;
FoamSetDocumentDir(FDD_TONES);
f = fopen(SPEAK_TRANSLATION_FILE, "r");
if(f)
{
len = 0;
buflen = 256;
repmh = MemAlloc(buflen, HF_DYNAMIC, 0);
repbuf = MemLock(repmh);
while(!feof(f) && fgets(line, sizeof(line), f))
{
/* Assume that input file uses ANSI resp. ISO Latin-1 charset */
LocalCodePageToGeos(line, strlen(line), CODE_PAGE_LATIN_1, '?');
p = line;
while(*p==' ' || *p=='\t' || *p=='\r' || *p=='\n')
p++; // skip over leading spaces
if(*p)
{
/* Extract first string */
s1 = p;
while(*p && *p!=' ' && *p!='\t' && *p!='\r' && *p!='\n')
p++; // get first string
if(*p) // delimit first string
*(p++) = 0;
while(*p==' ' || *p=='\t' || *p=='\r' || *p=='\n')
p++; // skip over delimiting whitespace
s2 = p;
/* Remove trailing whitespaces from second string */
p = s2+strlen(s2);
while(p>s2 && (*(p-1)=='\r' || *(p-1)=='\n' || *(p-1)==' ' || *(p-1)=='\t'))
p--;
*p = 0; // delimit second string
/* Fix {...} placeholders to internal representation to allow
for safer "inband signalling". */
if(s1[0]=='{' && s1[strlen(s1)-1]=='}')
{
s1[0] = '\x01';
s1[strlen(s1)-1] = '\x01';
}
/* Fix Geos encoding escape (only for single char patterns) */
if(s1[0]=='\\' && s1[1]=='x' &&
isxdigit((byte)s1[2]) && isxdigit((byte)s1[3]))
{
s1[0] = (xdigit2int(s1[2])<<4) + xdigit2int(s1[3]);
s1[1] = 0;
}
while(len+strlen(s1)+strlen(s2)+3>buflen)
{
buflen += 128;
MemReAlloc(repmh, buflen, 0);
repbuf = MemDeref(repmh);
}
strcpy(repbuf+len, s1);
len += strlen(s1)+1;
strcpy(repbuf+len, s2);
len += strlen(s2)+1;
}
}
repbuf[len] = 0; // Terminate with an empty string
MemUnlock(repmh);
fclose(f);
}
else
repmh = NullHandle;
}
void UnloadTranslation(void)
{
if(repmh) // Release translation table
{
MemFree(repmh);
repmh = NullHandle;
}
}
#pragma codeseg SPEAK_TRANS_TEXT
void TranslateString(char *str, Boolean singleChar)
{
char buf[MAX_SPEAK_BUF],*repbuf,*s,*d,*t;
int i;
Boolean hit;
word minlevel;
if(!repmh) return; // Bail out if no translation loaded
d = buf;
repbuf = MemLock(repmh);
for(s = str; *s; s++)
{
t = repbuf;
while(*t) // Check all translations
{
hit = FALSE;
for(i=0; t[i] && t[i]==s[i]; i++)
; // See if translation applies
if(t[i]==0 && (singleChar || i>1))
{ // yes:
s += (i-1); // Remove from source
t += i+1; // Go to replacement
/*
* Check \1,\2,\3 or \#1, \#2, \#3 prefixes to indicate
* minimum punctuation level required for doing a
* conditional replacement. The '#' chracter indicates
* that the replacement is to be done in any case if none of
* the adjacent characters is a letter or if the previous or
* next character is a duplication of this one.
*/
if(*t=='\\')
{
t++; // Skip over '\'
if(*t==C_NUMBER_SIGN)
{
t++; // Skip over '#'
if((!isalpha((byte)s[1]) && (s==str || !isalpha((byte)s[-1])))
|| s[0]==s[1]
|| (s>str && s[0]==s[-1]))
minlevel = 0; // Yes: always do replacement
else
minlevel = (*t-'0'); // No: replacement depends on level;
}
else
minlevel = (*t-'0');
t++; // Skip over level digit
if(*t==' ') t++; // Skip blank before replacement
}
else
minlevel = 0; // Uncoditional: always do replacement
if(str[1]==0) // Replace always in 1-character string
minlevel = 0; // (key echo)
if(G_punctSpell >= minlevel) // Only if replacement is to be made
{
if(d>buf && isword((byte)d[-1]))
*(d++) = ' '; // Leading space if needed
while(*t)
*(d++) = *(t++);
if(d>buf && isword((byte)d[-1]))
*(d++) = ' '; // Trailing space if needed
hit = TRUE;
}
break;
}
while(*t) t++; // Skip over rest of translation
t++;
while(*t) t++; // Skip over replacement
t++;
}
if(!hit)
{
if(*s=='\x01') // Remove unknown placeholders
{
if(s[1]) s++;
while(s[1] && *s!='\x01')
s++;
}
else
*(d++) = *s;
}
}
MemUnlock(repmh);
*(d++) = 0;
strcpy(str, buf); // Copy back result
}
void TranslateWord(byte *buf)
{
int i,l;
char *ins = NULL;
if(!*buf) return; // Ignore empty buffers
if(G_capsSpell && talx_isupper(buf[0]))
{
ins = PL("SpUC");
if(buf[1])
{
for(i=1; talx_isupper(buf[i]); i++)
;
if(!isword(buf[i]))
ins = PL("SpCA");
}
}
if(ins)
{
l = strlen(ins);
for(i=strlen((char *)buf); i>=0; i--)
buf[i+l] = buf[i];
for(i=0; i<l; i++)
buf[i] = ins[i];
}
}
void TranslateLetter(byte *buf, Boolean doUpper)
{
byte ch = *buf; // Work on first letter of buffer
byte *p;
p = buf;
if(ch==' ') // Special handling for space
strcpy((char *)p, PL("SpSpace")); // (avoid replacement with blank)
else if(isalpha(ch))
{
if(doUpper && G_capsSpell && talx_isupper(buf[0]))
{
strcpy((char *)buf, PL("SpUC"));
p = buf+strlen((char *)buf);
}
if(G_longSpell) // Spell based on table
sprintf((char *)p, PL("Sp%c"), toupper(ch));
else
*p = 0; // Fall through to direct replacement
}
else if(ch)
{
sprintf((char *)p, PL("Sp%c"), ch); // Symbol: try spelling replacement
}
if(*p)
TranslateString((char *)p, FALSE);// Check translation table
if(!*p) // No "spelling" replacement found
{
p[0] = ch; // Pass back character regularly
p[1] = 0; // Leave handling to speech layer
TranslateString((char *)p, FALSE);
}
}
void SpellWord(byte *str)
{
byte buf[MAX_SPEAK_BUF], *p, lc = 0, nc;
int j;
j = 0;
p = str;
while(*p && j<MAX_SPEAK_BUF-SPEAK_BUF_GUARD)
{
if(j)
buf[j++] = ' ';
nc = *p; // get this character
if(nc==' ' && (str[1]!=0)) // pause for spaces
{
if(lc!=' ') // first space in a row causes a pause
strcpy((char *)buf+j, PL("."));
else
buf[j] = 0;
p++;
}
else if(nc=='\x01') // placeholder?
{
buf[j++] = *(p++); // copy leading delimiter
while(*p && *p!='\x01') // copy placeholder (don't spell it...)
buf[j++] = *(p++);
if(*p) // copy trailing delimiter
buf[j++] = *(p++);
buf[j] = 0;
}
else
{
buf[j] = *(p++);
TranslateLetter(buf+j, (str[1]==0));
}
j += strlen((char *)buf+j);
lc = nc; // remember character for next round
}
buf[j] = 0;
strcpy((char *)str, (char *)buf);
}
#pragma codeseg INIT_TEXT
void TranslateResources(void)
{
struct resTable_struct *resTable;
char buf[512];
int i,j;
optr obj;
MemLock(OptrToHandle(@resTableChunk));
resTable = LMemDeref(@resTableChunk);
for(i=0; resTable[i].obj; i++)
{
strcpy(buf, resTable[i].text); // Get resource template
TranslateString(buf, FALSE); // Translate according to table
for(j=0; buf[j]; j++) // Insert newlines where requested
if(buf[j]=='\\')
buf[j] = '\r';
obj = resTable[i].obj;
switch(resTable[i].type)
{
case RES_VM: // VisMoniker
@call obj::MSG_GEN_REPLACE_VIS_MONIKER_TEXT(buf, VUM_DELAYED_VIA_APP_QUEUE);
break;
case RES_CM: // ComplexMoniker
ReplaceCMText(obj, buf);
break;
case RES_CH: // ChunkHandle
MemLock(OptrToHandle(obj));
if(!LMemReAlloc(obj, strlen(buf)+1))
strcpy(LMemDeref(obj), buf);
MemUnlock(OptrToHandle(obj));
break;
}
}
MemUnlock(OptrToHandle(@resTableChunk));
/* Translate template for "About" text - handled specially because it
requires a more extended set of hard-coded strings */
strcpy(buf, AboutTextTemplate);
TranslateString(buf, FALSE);
/* Enter new "About" text */
MemLock(OptrToHandle(@AboutTextChunk));
if(!LMemReAlloc(@AboutTextChunk, strlen(buf)+1))
strcpy(LMemDeref(@AboutTextChunk), buf);
MemUnlock(OptrToHandle(@AboutTextChunk));
}
/*
***************************************************************************
* Logic speech support
***************************************************************************
*/
#pragma codeseg
/* Compute length of string, taking into account replacements */
int LengthString(const char *message)
{
char buf[MAX_SPEAK_BUF];
strcpy(buf, message);
TranslateString(buf, !G_brailleMode);
return strlen(buf);
}
void SpeakSentence(byte *buf, optr app)
{
SynthStop(TRUE);
if(app!=@SpeakApp)
{
if(app && !notDemo) return; // Don't output data if in "Demo" mode
}
SynthSwitch(G_output);
SynthSpeak(buf);
}
void addObjectName(char *buf, char *pre, char *labelText, char *sep)
{
if(*labelText)
{
strcat(buf, pre);
strcat(buf, labelText);
strcat(buf, sep);
}
}
void printLabelsBr(ObjectRefs *oref, ObjectRefTexts *oreftexts, char *buf,
WordFlags sayWhat, word *cursor, word *cursor2)
{
int i,j;
word width = G_brailleWidth? G_brailleWidth : 40;
word len;
G_brTextStart = CURSOR_POS_NONE; // start of text object on line
/* No cursor position identified yet */
*cursor = *cursor2 = CURSOR_POS_NONE;
/* Set focus based on what we would normally say */
if(sayWhat & SAY_WHAT_TITLE)
G_brailleLine = BRL_TITLE;
else if(sayWhat & SAY_WHAT_FOCUS)
{
/* Reposition on text line if the focus label belongs there */
if(G_lastObj.focusParent && !G_lastObj.focusLabel && G_lastObj.textObj)
G_brailleLine = BRL_TEXT;
else
G_brailleLine = BRL_FOCUS;
}
else if(sayWhat & (SAY_WHAT_TEXT | SAY_WHAT_TEXTPART))
G_brailleLine = (G_brailleLine==BRL_PSEUDOCUR)? G_brailleLine : BRL_TEXT;
else if(sayWhat & (SAY_WHAT_TREE))
G_brailleLine = BRL_TREE;
/* Create current line */