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 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <stdio.h>
#include <string.h>
#include <o3tl/safeint.hxx>
#include <osl/thread.h>
#include <sal/log.hxx>
#include <X11/Xlib.h>
#include <vcl/commandevent.hxx>
#include <unx/i18n_cb.hxx>
#include <unx/i18n_ic.hxx>
#include <unx/i18n_im.hxx>
#include <salframe.hxx>
// i. preedit start callback
int
PreeditStartCallback ( XIC, XPointer client_data, XPointer )
{
preedit_data_t* pPreeditData = reinterpret_cast<preedit_data_t*>(client_data);
if ( pPreeditData->eState == PreeditStatus::ActivationRequired )
{
pPreeditData->eState = PreeditStatus::Active;
pPreeditData->aText.nLength = 0;
}
return -1;
}
// ii. preedit done callback
void
PreeditDoneCallback ( XIC, XPointer client_data, XPointer )
{
preedit_data_t* pPreeditData = reinterpret_cast<preedit_data_t*>(client_data);
if (pPreeditData->eState == PreeditStatus::Active )
{
if( pPreeditData->pFrame )
pPreeditData->pFrame->CallCallback( SalEvent::EndExtTextInput, nullptr );
}
pPreeditData->eState = PreeditStatus::StartPending;
}
// iii. preedit draw callback
// Handle deletion of text in a preedit_draw_callback
// from and howmuch are guaranteed to be nonnegative
static void
Preedit_DeleteText(preedit_text_t *ptext, int from, int howmuch)
{
// If we've been asked to delete no text then just set
// nLength correctly and return
if (ptext->nLength == 0)
{
ptext->nLength = from;
return;
}
int to = from + howmuch;
if (to == static_cast<int>(ptext->nLength))
{
// delete from the end of the text
ptext->nLength = from;
}
else if (to < static_cast<int>(ptext->nLength))
{
// cut out of the middle of the text
memmove( static_cast<void*>(ptext->pUnicodeBuffer + from),
static_cast<void*>(ptext->pUnicodeBuffer + to),
(ptext->nLength - to) * sizeof(sal_Unicode));
memmove( static_cast<void*>(ptext->pCharStyle + from),
static_cast<void*>(ptext->pCharStyle + to),
(ptext->nLength - to) * sizeof(XIMFeedback));
ptext->nLength -= howmuch;
}
else
{
// XXX this indicates an error, are we out of sync ?
SAL_INFO("vcl.app", "Preedit_DeleteText( from=" << from
<< " to=" << to
<< " length=" << ptext->nLength
<< " ).");
fprintf (stderr, "\t XXX internal error, out of sync XXX\n");
ptext->nLength = from;
}
// NULL-terminate the string
ptext->pUnicodeBuffer[ptext->nLength] = u'\0';
}
// reallocate the textbuffer with sufficiently large size 2^x
// nnewlimit is presupposed to be larger than ptext->size
static void
enlarge_buffer ( preedit_text_t *ptext, int nnewlimit )
{
size_t nnewsize = ptext->nSize;
while ( nnewsize <= o3tl::make_unsigned(nnewlimit) )
nnewsize *= 2;
ptext->nSize = nnewsize;
ptext->pUnicodeBuffer = static_cast<sal_Unicode*>(realloc(static_cast<void*>(ptext->pUnicodeBuffer),
nnewsize * sizeof(sal_Unicode)));
ptext->pCharStyle = static_cast<XIMFeedback*>(realloc(static_cast<void*>(ptext->pCharStyle),
nnewsize * sizeof(XIMFeedback)));
}
// Handle insertion of text in a preedit_draw_callback
// string field of XIMText struct is guaranteed to be != NULL
static void
Preedit_InsertText(preedit_text_t *pText, XIMText *pInsertText, int where)
{
sal_Unicode *pInsertTextString;
int nInsertTextLength = 0;
XIMFeedback *pInsertTextCharStyle = pInsertText->feedback;
nInsertTextLength = pInsertText->length;
// can't handle wchar_t strings, so convert to multibyte chars first
char *pMBString;
size_t nMBLength;
if (pInsertText->encoding_is_wchar)
{
wchar_t *pWCString = pInsertText->string.wide_char;
size_t nBytes = wcstombs ( nullptr, pWCString, 1024 /* don't care */);
pMBString = static_cast<char*>(alloca( nBytes + 1 ));<--- Obsolete function 'alloca' called. [+]The obsolete function 'alloca' is called. In C++11 and later it is recommended to use std::array<> or a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons (http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).
nMBLength = wcstombs ( pMBString, pWCString, nBytes + 1);
}
else
{
pMBString = pInsertText->string.multi_byte;
nMBLength = strlen(pMBString); // xxx
}
// convert multibyte chars to unicode
rtl_TextEncoding nEncoding = osl_getThreadTextEncoding();
if (nEncoding != RTL_TEXTENCODING_UNICODE)
{
rtl_TextToUnicodeConverter aConverter =
rtl_createTextToUnicodeConverter( nEncoding );
rtl_TextToUnicodeContext aContext =
rtl_createTextToUnicodeContext(aConverter);
sal_Size nBufferSize = nInsertTextLength * 2;
pInsertTextString = static_cast<sal_Unicode*>(alloca(nBufferSize));<--- Obsolete function 'alloca' called. [+]The obsolete function 'alloca' is called. In C++11 and later it is recommended to use std::array<> or a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons (http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).
sal_uInt32 nConversionInfo;
sal_Size nConvertedChars;
rtl_convertTextToUnicode( aConverter, aContext,
pMBString, nMBLength,
pInsertTextString, nBufferSize,
RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_IGNORE
| RTL_TEXTTOUNICODE_FLAGS_INVALID_IGNORE,
&nConversionInfo, &nConvertedChars );
rtl_destroyTextToUnicodeContext(aConverter, aContext);
rtl_destroyTextToUnicodeConverter(aConverter);
}
else
{
pInsertTextString = reinterpret_cast<sal_Unicode*>(pMBString);
}
// enlarge target text-buffer if necessary
if (pText->nSize <= (pText->nLength + nInsertTextLength))
enlarge_buffer(pText, pText->nLength + nInsertTextLength);
// insert text: displace old mem and put new bytes in
int from = where;
int to = where + nInsertTextLength;
int howmany = pText->nLength - where;
memmove(static_cast<void*>(pText->pUnicodeBuffer + to),
static_cast<void*>(pText->pUnicodeBuffer + from),
howmany * sizeof(sal_Unicode));
memmove(static_cast<void*>(pText->pCharStyle + to),
static_cast<void*>(pText->pCharStyle + from),
howmany * sizeof(XIMFeedback));
to = from;
howmany = nInsertTextLength;
memcpy(static_cast<void*>(pText->pUnicodeBuffer + to), static_cast<void*>(pInsertTextString),
howmany * sizeof(sal_Unicode));
memcpy(static_cast<void*>(pText->pCharStyle + to), static_cast<void*>(pInsertTextCharStyle),
howmany * sizeof(XIMFeedback));
pText->nLength += howmany;
// NULL-terminate the string
pText->pUnicodeBuffer[pText->nLength] = u'\0';
}
// Handle the change of attributes in a preedit_draw_callback
static void
Preedit_UpdateAttributes ( preedit_text_t* ptext, XIMFeedback const * feedback,
int from, int amount )
{
if ( (from + amount) > static_cast<int>(ptext->nLength) )
{
// XXX this indicates an error, are we out of sync ?
SAL_INFO("vcl.app", "Preedit_UpdateAttributes( "
<< from << " + " << amount << " > " << ptext->nLength
<< " ).");
fprintf (stderr, "\t XXX internal error, out of sync XXX\n");
return;
}
memcpy ( ptext->pCharStyle + from,
feedback, amount * sizeof(XIMFeedback) );
}
// Convert the XIM feedback values into appropriate VCL
// EXTTEXTINPUT_ATTR values
// returns an allocate list of attributes, which must be freed by caller
static ExtTextInputAttr*
Preedit_FeedbackToSAL ( const XIMFeedback* pfeedback, int nlength, std::vector<ExtTextInputAttr>& rSalAttr )
{
ExtTextInputAttr *psalattr;
ExtTextInputAttr nval;
ExtTextInputAttr noldval = ExtTextInputAttr::NONE;
XIMFeedback nfeedback;
// only work with reasonable length
if (nlength > 0 && nlength > sal::static_int_cast<int>(rSalAttr.size()) )
{
rSalAttr.reserve( nlength );
psalattr = rSalAttr.data();
}
else
return nullptr;
for (int npos = 0; npos < nlength; npos++)
{
nval = ExtTextInputAttr::NONE;
nfeedback = pfeedback[npos];
// means to use the feedback of the previous char
if (nfeedback == 0)
{
nval = noldval;
}
// convert feedback to attributes
else
{
if (nfeedback & XIMReverse)
nval |= ExtTextInputAttr::Highlight;
if (nfeedback & XIMUnderline)
nval |= ExtTextInputAttr::Underline;
if (nfeedback & XIMHighlight)
nval |= ExtTextInputAttr::Highlight;
if (nfeedback & XIMPrimary)
nval |= ExtTextInputAttr::DottedUnderline;
if (nfeedback & XIMSecondary)
nval |= ExtTextInputAttr::DashDotUnderline;
if (nfeedback & XIMTertiary) // same as 2ery
nval |= ExtTextInputAttr::DashDotUnderline;
}
// copy in list
psalattr[npos] = nval;
noldval = nval;
}
// return list of sal attributes
return psalattr;
}
void
PreeditDrawCallback(XIC ic, XPointer client_data,
XIMPreeditDrawCallbackStruct *call_data)
{
preedit_data_t* pPreeditData = reinterpret_cast<preedit_data_t*>(client_data);
// if there's nothing to change then change nothing
if ( ( (call_data->text == nullptr) && (call_data->chg_length == 0) )
|| pPreeditData->pFrame == nullptr )
return;
// Solaris 7 deletes the preedit buffer after commit
// since the next call to preeditstart will have the same effect just skip this.
// if (pPreeditData->eState == ePreeditStatusStartPending && call_data->text == NULL)
// return;
if ( pPreeditData->eState == PreeditStatus::StartPending )
pPreeditData->eState = PreeditStatus::ActivationRequired;
PreeditStartCallback( ic, client_data, nullptr );
// Edit the internal textbuffer as indicated by the call_data,
// chg_first and chg_length are guaranteed to be nonnegative
// handle text deletion
if (call_data->text == nullptr)
{
Preedit_DeleteText(&(pPreeditData->aText),
call_data->chg_first, call_data->chg_length );
}
else
{
// handle text insertion
if ( (call_data->chg_length == 0)
&& (call_data->text->string.wide_char != nullptr))
{
Preedit_InsertText(&(pPreeditData->aText), call_data->text,
call_data->chg_first);
}
else if ( (call_data->chg_length != 0)
&& (call_data->text->string.wide_char != nullptr))
{
// handle text replacement by deletion and insertion of text,
// not smart, just good enough
Preedit_DeleteText(&(pPreeditData->aText),
call_data->chg_first, call_data->chg_length);
Preedit_InsertText(&(pPreeditData->aText), call_data->text,
call_data->chg_first);
}
else if ( (call_data->chg_length != 0)
&& (call_data->text->string.wide_char == nullptr))
{
// not really a text update, only attributes are concerned
Preedit_UpdateAttributes(&(pPreeditData->aText),
call_data->text->feedback,
call_data->chg_first, call_data->chg_length);
}
}
// build the SalExtTextInputEvent and send it up
pPreeditData->aInputEv.mpTextAttr = Preedit_FeedbackToSAL(
pPreeditData->aText.pCharStyle, pPreeditData->aText.nLength, pPreeditData->aInputFlags);
pPreeditData->aInputEv.mnCursorPos = call_data->caret;
pPreeditData->aInputEv.maText = OUString(pPreeditData->aText.pUnicodeBuffer,
pPreeditData->aText.nLength);
pPreeditData->aInputEv.mnCursorFlags = 0; // default: make cursor visible
if ( pPreeditData->eState == PreeditStatus::Active && pPreeditData->pFrame )
pPreeditData->pFrame->CallCallback(SalEvent::ExtTextInput, static_cast<void*>(&pPreeditData->aInputEv));
if (pPreeditData->aText.nLength == 0 && pPreeditData->pFrame )
pPreeditData->pFrame->CallCallback( SalEvent::EndExtTextInput, nullptr );
if (pPreeditData->aText.nLength == 0)
pPreeditData->eState = PreeditStatus::StartPending;
GetPreeditSpotLocation(ic, reinterpret_cast<XPointer>(pPreeditData));
}
void
GetPreeditSpotLocation(XIC ic, XPointer client_data)
{
// Send SalEventExtTextInputPos event to get spotlocation
SalExtTextInputPosEvent aPosEvent;
preedit_data_t* pPreeditData = reinterpret_cast<preedit_data_t*>(client_data);
if( pPreeditData->pFrame )
pPreeditData->pFrame->CallCallback(SalEvent::ExtTextInputPos, static_cast<void*>(&aPosEvent));
XPoint point;
point.x = aPosEvent.mnX + aPosEvent.mnWidth;
point.y = aPosEvent.mnY + aPosEvent.mnHeight;
XVaNestedList preedit_attr;
preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &point, nullptr);
XSetICValues(ic, XNPreeditAttributes, preedit_attr, nullptr);
XFree(preedit_attr);
}
// iv. preedit caret callback
#if OSL_DEBUG_LEVEL > 1
void
PreeditCaretCallback ( XIC ic, XPointer client_data,
XIMPreeditCaretCallbackStruct *call_data )
{
// XXX PreeditCaretCallback is pure debug code for now
const char *direction = "?";
const char *style = "?";
switch ( call_data->style )
{
case XIMIsInvisible: style = "Invisible"; break;
case XIMIsPrimary: style = "Primary"; break;
case XIMIsSecondary: style = "Secondary"; break;
}
switch ( call_data->direction )
{
case XIMForwardChar: direction = "Forward char"; break;
case XIMBackwardChar: direction = "Backward char"; break;
case XIMForwardWord: direction = "Forward word"; break;
case XIMBackwardWord: direction = "Backward word"; break;
case XIMCaretUp: direction = "Caret up"; break;
case XIMCaretDown: direction = "Caret down"; break;
case XIMNextLine: direction = "Next line"; break;
case XIMPreviousLine: direction = "Previous line"; break;
case XIMLineStart: direction = "Line start"; break;
case XIMLineEnd: direction = "Line end"; break;
case XIMAbsolutePosition: direction = "Absolute"; break;
case XIMDontChange: direction = "Don't change"; break;
}
SAL_INFO("vcl.app", "PreeditCaretCallback( ic=" << ic
<< ", client=" << client_data
<< ",");
SAL_INFO("vcl.app", "\t position=" << call_data->position
<< ", direction=\"" << direction
<< "\", style=\"" << style
<< "\" ).");
}
#else
void
PreeditCaretCallback ( XIC, XPointer, XIMPreeditCaretCallbackStruct* )
{
}
#endif
// v. commit string callback: convert an extended text input (iiimp ... )
// into an ordinary key-event
Bool
IsControlCode(sal_Unicode nChar)
{
if ( nChar <= 0x1F /* C0 controls */ )
return True;
else
return False;
}
// vi. status callbacks: for now these are empty, they are just needed for turbo linux
void
StatusStartCallback (XIC, XPointer, XPointer)
{
}
void
StatusDoneCallback (XIC, XPointer, XPointer)
{
}
void
StatusDrawCallback (XIC, XPointer, XIMStatusDrawCallbackStruct *)
{
}
// vii. destroy callbacks: internally disable all IC/IM calls
void
IC_IMDestroyCallback (XIM, XPointer client_data, XPointer)
{
SalI18N_InputContext *pContext = reinterpret_cast<SalI18N_InputContext*>(client_data);
if (pContext != nullptr)
pContext->HandleDestroyIM();
}
void
IM_IMDestroyCallback (XIM, XPointer client_data, XPointer)
{
SalI18N_InputMethod *pMethod = reinterpret_cast<SalI18N_InputMethod*>(client_data);
if (pMethod != nullptr)
pMethod->HandleDestroyIM();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|