Modified to prevent IME's start automatically in on-demand mode
[platform/core/uifw/isf.git] / ism / src / scim_utility.h
1 /** @file scim_utility.h
2  *  @brief various utility functions.
3  */
4
5 /* ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable. */
6
7 /*
8  * Smart Common Input Method
9  *
10  * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn>
11  * Copyright (c) 2012-2015 Samsung Electronics Co., Ltd.
12  *
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this program; if not, write to the
26  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
27  * Boston, MA  02111-1307  USA
28  *
29  * Modifications by Samsung Electronics Co., Ltd.
30  * 1. Add time and logs functions for performance profile
31  * 2. Define RECT_INFO, ISF_CANDIDATE_MODE_T and ISF_CANDIDATE_PORTRAIT_LINE_T
32  *
33  * $Id: scim_utility.h,v 1.36 2005/04/09 15:38:39 suzhe Exp $
34  */
35
36 #ifndef __SCIM_UTILITY_H
37 #define __SCIM_UTILITY_H
38
39 //#include <syslog.h>
40
41 namespace scim {
42 /**
43  * @addtogroup Accessories
44  * @ingroup InputServiceFramework
45  * @{
46  */
47
48 #ifndef SCIM_KEYBOARD_ICON_FILE
49 #define SCIM_KEYBOARD_ICON_FILE            (SCIM_ICONDIR "/keyboard.png")
50 #endif
51
52 #define SCIM_PATH_DELIM_STRING "/"
53 #define SCIM_PATH_DELIM        '/'
54
55 // UTF-8 <-> ucs4_t convert
56
57 /* Return code if invalid. (xxx_mbtowc, xxx_wctomb) */
58 #define RET_ILSEQ      0
59 /* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
60 #define RET_TOOFEW(n)  (-1-(n))
61 /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
62 #define RET_TOOSMALL   -1
63 /* Replacement character for invalid multibyte sequence or wide character. */
64 #define BAD_WCHAR ((ucs4_t) 0xfffd)
65 #define BAD_CHAR '?'
66
67 /**
68  * @brief Convert an utf8 char sequence to ucs4.
69  *
70  * @param pwc destination buffer to store the ucs4 code.
71  * @param src source buffer contains the utf8 char sequence.
72  * @param src_len the size of source buffer.
73  *
74  * @return number of chars in s actually converted.
75  */
76 EXAPI int utf8_mbtowc (ucs4_t *pwc, const unsigned char *src, int src_len);
77
78 /**
79  * @brief Convert an ucs4 code to utf8 char sequence.
80  *
81  * @param dest destination buffer to store utf8 char sequence.
82  * @param wc the ucs4 code to be converted.
83  * @param dest_size the size of destination buffer.
84  *
85  * @return the number of bytes actually written into dest.
86  */
87 EXAPI int utf8_wctomb (unsigned char *dest, ucs4_t wc, int dest_size);
88
89 /**
90  * @brief Convert an utf8 string to an ucs4 string.
91  *
92  * @param str source utf8 string.
93  * @return the destination widestring.
94  */
95 EXAPI WideString utf8_mbstowcs (const String & str);
96
97 /**
98  * @brief Convert an utf8 string to an ucs4 string.
99  *
100  * @param str source utf8 string.
101  * @param len length of the source string.
102  * @return the destination widestring.
103  */
104 EXAPI WideString utf8_mbstowcs (const char *str, int len = -1);
105
106 /**
107  * @brief Convert an ucs4 string to an utf8 string.
108  *
109  * @param wstr source ucs4 string.
110  *
111  * @return the destination utf8 string.
112  */
113 EXAPI String utf8_wcstombs (const WideString & wstr);
114
115 /**
116  * @brief Convert an ucs4 string to an utf8 string.
117  *
118  * @param wstr source ucs4 string.
119  * @param len length of the source string.
120  *
121  * @return the destination utf8 string.
122  */
123 EXAPI String utf8_wcstombs (const ucs4_t *wstr, int len = -1);
124
125 /**
126  * @brief Read a wide char from istream.
127  *
128  * The content in the istream are actually in utf-8 encoding.
129  *
130  * @param is the stream to be read.
131  *
132  * @return if equal to 0 then got the end of the stream or error occurred.
133  */
134 EXAPI ucs4_t utf8_read_wchar (std::istream &is);
135
136 /**
137  * @brief Write a wide char to ostream.
138  *
139  * The content written into the ostream will be converted into utf-8 encoding.
140  *
141  * @param os the stream to be written.
142  * @param wc the wide char to be written to the stream.
143  * @return the same stream object reference.
144  */
145 EXAPI std::ostream & utf8_write_wchar (std::ostream &os, ucs4_t wc);
146
147 /**
148  * @brief Read a wide string from istream.
149  *
150  * The content in the istream are actually in utf-8 encoding.
151  *
152  * @param is the stream to be read.
153  * @param delim the delimiter of the string.
154  * @param rm_delim if the delim should be removed from the destination string.
155  * @return the wide string read from the given stream.
156  */
157 EXAPI WideString utf8_read_wstring (std::istream &is, ucs4_t delim = (ucs4_t) '\n', bool rm_delim = true);
158
159 /**
160  * @brief Write a wide string to ostream.
161  *
162  * The content written into the ostream will be converted into utf-8 encoding.
163  *
164  * @param os the stream to be written.
165  * @param wstr the wide string to be written into the stream.
166  * @return the same stream object reference.
167  */
168 EXAPI std::ostream & utf8_write_wstring (std::ostream &os, const WideString & wstr);
169
170 /**
171  * @brief Convert an uint32 variable into a sequence of bytes.
172  *
173  * @param bytes the buffer to store the result.
174  * @param n the variable to be converted.
175  */
176 EXAPI inline
177 void scim_uint32tobytes (unsigned char *bytes, uint32 n)
178 {
179     bytes [0] = (unsigned char) ((n & 0xFF));
180     bytes [1] = (unsigned char) ((n >> 8) & 0xFF);
181     bytes [2] = (unsigned char) ((n >> 16) & 0xFF);
182     bytes [3] = (unsigned char) ((n >> 24) & 0xFF);
183 }
184
185 /**
186  * @brief Convert a sequence of bytes into an uint32 value.
187  *
188  * @param bytes the buffer contains the bytes to be converted.
189  * @return the result uint32 value.
190  */
191 EXAPI inline
192 uint32 scim_bytestouint32 (const unsigned char *bytes)
193 {
194     return  ((uint32) bytes [0])
195             | (((uint32) bytes [1]) << 8)
196             | (((uint32) bytes [2]) << 16)
197             | (((uint32) bytes [3]) << 24);
198 }
199
200 /**
201  * @brief Convert an uint16 variable into a sequence of bytes.
202  *
203  * @param bytes the buffer to store the result.
204  * @param n the variable to be converted.
205  */
206 EXAPI inline
207 void scim_uint16tobytes (unsigned char *bytes, uint16 n)
208 {
209     bytes [0] = (unsigned char) ((n & 0xFF));
210     bytes [1] = (unsigned char) ((n >> 8) & 0xFF);
211 }
212
213 /**
214  * @brief Convert a sequence of bytes into an uint16 value.
215  *
216  * @param bytes the buffer contains the bytes to be converted.
217  * @return the result uint16 value.
218  */
219 EXAPI inline
220 uint16 scim_bytestouint16 (const unsigned char *bytes)
221 {
222     return  ((uint16) bytes [0]) | (((uint16) bytes [1]) << 8);
223 }
224
225 /**
226  * @brief Test if the locale is valid, and return the good locale name.
227  *
228  * @param locale the locale to be tested.
229  * @return If the locale is valid, it's the good locale name, otherwise empty.
230  */
231 EXAPI String scim_validate_locale (const String& locale);
232
233 /**
234  * @brief Get the encoding for a locale.
235  *
236  * @param locale the name of the locale.
237  * @return The encoding used by the given locale.
238  */
239 EXAPI String scim_get_locale_encoding (const String& locale);
240
241 /**
242  * @brief Get current system locale.
243  * @return The current system locale.
244  */
245 EXAPI String scim_get_current_locale ();
246
247 /**
248  * @brief Get current system language.
249  * @return The current system language.
250  */
251 EXAPI String scim_get_current_language ();
252
253 /**
254  * @brief Get the max length of the multibyte char of a locale.
255  *
256  * @param locale the name of the locale.
257  * @return the maxlen of this locale.
258  */
259 EXAPI int scim_get_locale_maxlen (const String& locale);
260
261 /**
262  * @brief Split string list into a string vector according to the delim char.
263  *
264  * @param vec the string vector to store the result.
265  * @param str the string to be splitted.
266  * @param delim the delimiter to split the strings.
267  * @return the number of the strings in the result list.
268  */
269 EXAPI int scim_split_string_list (std::vector<String>& vec, const String& str, char delim = ',');
270
271 /**
272  * @brief Combine a string vector into one string list, separated by char delim.
273  *
274  * @param vec the string vector which contains the strings to be combined.
275  * @param delim the delimiter which should be put between two strings.
276  * @return the result string.
277  */
278 EXAPI String scim_combine_string_list (const std::vector<String>& vec, char delim = ',');
279
280 /**
281  * @brief Get machine endian type
282  * @return 1 little endian, 0 big endian
283  */
284 EXAPI bool scim_is_little_endian ();
285
286 /**
287  * @brief Test if wchar_t is using UCS4 encoding.
288  */
289 EXAPI bool scim_if_wchar_ucs4_equal ();
290
291 /**
292  * @brief Convert a half width unicode char to its full width counterpart.
293  */
294 EXAPI ucs4_t scim_wchar_to_full_width (ucs4_t code);
295
296 /**
297  * @brief Convert a full width unicode char to its half width counterpart.
298  */
299 EXAPI ucs4_t scim_wchar_to_half_width (ucs4_t code);
300
301 /**
302  * @brief Get the home dir of current user.
303  */
304 EXAPI String scim_get_home_dir ();
305
306 /**
307  * @brief Get the name of current user.
308  */
309 EXAPI String scim_get_user_name ();
310
311 /**
312  * @brief Get SCIM data dir of current user.
313  */
314 EXAPI String scim_get_user_data_dir ();
315
316 /**
317  * @brief Load a file into memory.
318  *
319  * @param filename the name of the file to be loaded.
320  * @param bufptr the place to store the newly allocated buffer pointer,
321  *        if bufptr == NULL then the file is not actually loaded,
322  *        just return the file size.
323  *        The pointer *bufptr must be deleted afterwards.
324  * @return the size of the data actually loaded (mostly, it's the file size),
325  *         zero means load failed.
326  */
327 EXAPI size_t scim_load_file (const String &filename, char **bufptr);
328
329 /**
330  * @brief Make a directory.
331  *
332  * @param dir the dir path to be created.
333  *
334  * @return true if sucess.
335  */
336 EXAPI bool scim_make_dir (const String &dir);
337
338 /**
339  * @brief Get the localized name of a language id.
340  * @param lang the language id.
341  * @return the localized name of this language, in utf8 encoding.
342  */
343 EXAPI String scim_get_language_name (const String &lang);
344
345 /**
346  * @brief Get the English name of a language id.
347  * @param lang the language id.
348  * @return the English name of this language, in utf8 encoding.
349  */
350 EXAPI String scim_get_language_name_english (const String &lang);
351
352 /**
353  * @brief Get the untranslated name of a language id.
354  * @param lang the language id.
355  * @return the untranslated name of this language, in utf8 encoding.
356  */
357 EXAPI String scim_get_language_name_untranslated (const String &lang);
358
359 /**
360  * @brief Get the supported locales for a language.
361  *
362  * For example language zh_CN may support zh_CN.UTF-8, zh_CN.GB18030, zh_CN.GBK, zh_CN.GB2312 locales.
363  *
364  * @param lang the language id.
365  * @return the supported locales separated by comma.
366  */
367 EXAPI String scim_get_language_locales (const String &lang);
368
369 /**
370  * @brief Get the language id for a locale.
371  * @param locale the locale name
372  * @return the language id for this locale.
373  */
374 EXAPI String scim_get_locale_language (const String &locale);
375
376 /**
377  * @brief Test if the language is valid, and return the good language code.
378  * @param lang the language to be tested.
379  * @return If the language is valid, return the good language id, otherwise return "~other".
380  */
381 EXAPI String scim_validate_language (const String &lang);
382
383 /**
384  * @brief Get the normalized language id of a language.
385  *
386  * Some short language id will be normalized to it's full id, for example:
387  * "ja" -> "ja_JP"
388  * "ko" -> "ko_KR"
389  * "zh" -> "zh_CN"
390  *
391  * furthermore, zh_HK will be normalized to zh_TW, zh_SG will be normalized to zh_CN.
392  *
393  * @param lang the original language
394  * @return the normalized language code.
395  */
396 EXAPI String scim_get_normalized_language (const String &lang);
397
398 /**
399  * @brief Launch a SCIM process with specific options.
400  *
401  * @param daemon        If true then launch scim in a daemon process,
402  *                      otherwise the current process will be stopped until
403  *                      the newly created process exit.
404  * @param config        The Config module to be used.
405  * @param imengines     The IMEngines to be loaded, separated by comma.
406  * @param frontend      The FrontEnd module to be used.
407  * @param argv          Additional arguments passed to the new process's FrontEnd. Must
408  *                      terminated by a NULL pointer.
409  *
410  * @return Return 0 means the process started/exited without any problem, otherwise
411  *         means an error occurred.
412  */
413 EXAPI int  scim_launch (bool          daemon,
414                   const String &config,
415                   const String &imengines,
416                   const String &frontend,
417                   char  * const argv [] = 0);
418
419 /**
420  * @brief Launch a SCIM Panel process with specific options.
421  *
422  * @param daemon        If true then launch scim in a daemon process,
423  *                      otherwise the current process will be stopped until
424  *                      the newly created process exit.
425  * @param config        The Config module to be used.
426  * @param display       The display name on which the panel runs.
427  *                      eg. for X11 : localhost:0.0
428  * @param argv          Additional arguments passed to the new process's FrontEnd. Must
429  *                      terminated by a NULL pointer.
430  *
431  * @return Return 0 means the process started/exited without any problem, otherwise
432  *         means an error occurred.
433  */
434 EXAPI int scim_launch_panel (bool          daemon,
435                        const String &config,
436                        const String &display,
437                        char * const  argv [] = 0);
438
439 /**
440  * @brief Sleep some microseconds.
441  *
442  * @param usec The amount of microseconds to be sleeped.
443  */
444 EXAPI void scim_usleep (unsigned int usec);
445
446 /**
447  * @brief Switch process into daemon mode.
448  */
449 EXAPI void scim_daemon ();
450
451 /**
452  * @brief Save string to ISF log file.
453  *
454  * @param str The string to be saved.
455  */
456 EXAPI void isf_save_log (const char *fmt, ...);
457
458 #define ISF_SAVE_LOG(fmt, arg...) \
459     do{ \
460         isf_save_log ("time:%ld  pid:%d  %s  %s  " fmt, time (0), getpid (), __FILE__, __func__, ##arg); \
461     }while(0);
462
463 #ifdef ISF_PROF
464
465 void ISF_PROF_DEBUG_TIME(char const* func, int line, char const* str);
466 void ISF_PROF_DEBUG_TIME_BEGIN ();
467 void ISF_PROF_DEBUG_TIME_END (char const* format, char const* func, int line);
468
469 #define ISF_PROF_DEBUG(str) ISF_PROF_DEBUG_TIME(__FUNCTION__, __LINE__, str );
470 #define ISF_PROF_BEGIN() ISF_PROF_DEBUG_TIME_BEGIN ();
471 #define ISF_PROF_END(fmt, args...) \
472     do{ \
473         ISF_PROF_DEBUG_TIME_END("[%s:%d]", __FUNCTION__, __LINE__); \
474         printf (fmt, ##args); \
475     }while(0);
476
477 #else
478
479 #define ISF_PROF_DEBUG(str)
480 #define ISF_PROF_BEGIN()
481 #define ISF_PROF_END(fmt, ...)
482
483 #endif    /* ISF_PROF */
484
485 //#define ENABLE_ISF_LOG 1
486 #ifdef ENABLE_ISF_LOG
487     #define ISF_LOG(fmt, args...) \
488     do{ \
489         /*syslog (LOG_DEBUG, "[%s %d in %s ()]", __FILE__, __LINE__, __FUNCTION__); \
490         syslog (LOG_DEBUG, fmt, ##args); \ */
491     }while(0);
492 #else
493     #define ISF_LOG(fmt, ...)
494 #endif
495
496 EXAPI void gettime (clock_t clock_start, const char* str);
497
498 typedef struct rectinfo
499 {
500     uint32 pos_x;
501     uint32 pos_y;
502     uint32 width;
503     uint32 height;
504 } RECT_INFO;
505
506 typedef enum
507 {
508     FIXED_CANDIDATE_WINDOW = 0,
509     FLOATING_CANDIDATE_WINDOW,
510     SOFT_CANDIDATE_WINDOW
511 } ISF_CANDIDATE_MODE_T;
512
513 typedef enum
514 {
515     ONE_LINE_CANDIDATE = 1,
516     TWO_LINE_CANDIDATE,
517     THREE_LINE_CANDIDATE,
518     FOUR_LINE_CANDIDATE
519 } ISF_CANDIDATE_PORTRAIT_LINE_T;
520
521
522 #define PROP_X_EXT_KEYBOARD_INPUT_DETECTED  "HW Keyboard Input Started"
523 #define PROP_X_EXT_KEYBOARD_EXIST           "X External Keyboard Exist"
524
525 #define VCONFKEY_AUTOCAPITAL_ALLOW_BOOL "file/private/isf/autocapital_allow"
526 #define VCONFKEY_AUTOPERIOD_ALLOW_BOOL  "file/private/isf/autoperiod_allow"
527
528 #define mzsc(m_code)                    "\x1b[" # m_code
529 #define mzc_normal                      mzsc(0m)
530 #define mzc_red                         mzsc(1;31m)
531
532
533 /** @} */
534 } /* namespace scim */
535
536 #endif /* __SCIM_UTILITY_H */
537 /*
538 vi:ts=4:nowrap:ai:expandtab
539 */