comparison os2/dw.c @ 1751:96c6133ce3b2

Added initial code to attempt to detect the keyboard layout for use with UniCreateKeyboard(). Detection only takes place during initialization currently.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 15 Jun 2012 21:35:58 +0000
parents 76b24619f6fa
children 2d3cd1f616a9
comparison
equal deleted inserted replaced
1750:1de7daad85a3 1751:96c6133ce3b2
126 126
127 #ifdef UNICODE 127 #ifdef UNICODE
128 /* Atom for "text/unicode" clipboard format */ 128 /* Atom for "text/unicode" clipboard format */
129 ATOM Unicode; 129 ATOM Unicode;
130 KHAND Keyboard; 130 KHAND Keyboard;
131 UconvObject Uconv; /* conversion object */
131 #endif 132 #endif
132 133
133 unsigned long _colors[] = { 134 unsigned long _colors[] = {
134 CLR_BLACK, 135 CLR_BLACK,
135 CLR_DARKRED, 136 CLR_DARKRED,
2788 #define MAX_CP_NAME 12 /* maximum length of a codepage name */ 2789 #define MAX_CP_NAME 12 /* maximum length of a codepage name */
2789 #define MAX_CP_SPEC 64 /* maximum length of a UconvObject codepage specifier */ 2790 #define MAX_CP_SPEC 64 /* maximum length of a UconvObject codepage specifier */
2790 2791
2791 char *_WideToUTF8(UniChar *unistr) 2792 char *_WideToUTF8(UniChar *unistr)
2792 { 2793 {
2793 UconvObject uconv; /* conversion object */ 2794 /* Convert text to UTF-8 codepage */
2794 UniChar suCodepage[MAX_CP_SPEC]; /* conversion specifier */
2795 /* Convert text to the active codepage */
2796 ULONG ulRC;
2797 char *retval = NULL; 2795 char *retval = NULL;
2798 2796 /* Now do the conversion */
2799 /* Create the conversion object */ 2797 ULONG ulBufLen = (UniStrlen(unistr) * 4) + 1;
2800 UniMapCpToUcsCp(1208, suCodepage, MAX_CP_NAME); 2798 char *s, *pszLocalText = (char *)malloc(ulBufLen);
2801 UniStrcat(suCodepage, (UniChar *) L"@map=cdra,path=no"); 2799
2802 2800 if(UniStrFromUcs(Uconv, pszLocalText,
2803 if((ulRC = UniCreateUconvObject(suCodepage, &uconv)) == ULS_SUCCESS) 2801 unistr, ulBufLen) == ULS_SUCCESS)
2804 { 2802 {
2805 /* Now do the conversion */ 2803 /* (some codepages use 0x1A for substitutions; replace with ?) */
2806 ULONG ulBufLen = (UniStrlen(unistr) * 4) + 1; 2804 while((s = strchr(pszLocalText, 0x1A)) != NULL) *s = '?';
2807 char *s, *pszLocalText = (char *)malloc(ulBufLen); 2805 /* Output the converted text */
2808 2806 retval = pszLocalText;
2809 if((ulRC = UniStrFromUcs(uconv, pszLocalText,
2810 unistr, ulBufLen )) == ULS_SUCCESS)
2811 {
2812 /* (some codepages use 0x1A for substitutions; replace with ?) */
2813 while((s = strchr(pszLocalText, 0x1A)) != NULL) *s = '?';
2814 /* Output the converted text */
2815 retval = pszLocalText;
2816 }
2817 #ifdef DEBUG
2818 else
2819 dw_debug("Error pasting Unicode text:\nUniStrFromUcs() = %08X", ulRC);
2820 #endif
2821 UniFreeUconvObject(uconv);
2822 } 2807 }
2823 #ifdef DEBUG 2808 else if(pszLocalText)
2824 else 2809 free(pszLocalText);
2825 dw_debug("Error pasting Unicode text:\nUniCreateUconvObject() = %08X", ulRC); 2810 return retval;
2826 #endif 2811 }
2812
2813 UniChar *_UTF8toWide(char *utf8str)
2814 {
2815 /* Convert text to Unicode */
2816 UniChar *retval = NULL;
2817 /* Now do the conversion */
2818 UniChar *buf = calloc(strlen(utf8str) + 1, sizeof(UniChar));
2819
2820 if(UniStrToUcs(Uconv, buf,
2821 utf8str, strlen(utf8str) * sizeof(UniChar)) == ULS_SUCCESS)
2822 {
2823 /* Output the converted text */
2824 retval = buf;
2825 }
2826 else if(buf)
2827 free(buf);
2827 return retval; 2828 return retval;
2828 } 2829 }
2829 #endif 2830 #endif
2830 2831
2831 MRESULT EXPENTRY _run_event(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2) 2832 MRESULT EXPENTRY _run_event(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
4201 return oldproc(hwnd, msg, mp1, mp2); 4202 return oldproc(hwnd, msg, mp1, mp2);
4202 4203
4203 return WinDefWindowProc(hwnd, msg, mp1, mp2); 4204 return WinDefWindowProc(hwnd, msg, mp1, mp2);
4204 } 4205 }
4205 4206
4207 #ifdef UNICODE
4208 /* Internal function to detect the active keyboard layout */
4209 UniChar *_detect_keyb(void)
4210 {
4211 HFILE handle;
4212 struct
4213 {
4214 USHORT length;
4215 USHORT codepage;
4216 UCHAR strings[8];
4217 } kd;
4218 ULONG action;
4219 UniChar *buf = NULL;
4220
4221 if(DosOpen("KBD$", &handle, &action, 0, 0,
4222 OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
4223 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
4224 NULL) == 0)
4225 {
4226 ULONG plen = 0, dlen = sizeof(kd);
4227
4228 kd.length = dlen;
4229
4230 if(DosDevIOCtl(handle, 4, 0x7b, NULL, plen, &plen,
4231 &kd, dlen, &dlen) == 0 && strlen(kd.strings) > 0)
4232 {
4233
4234 /* Convert to Unicode */
4235 buf = _UTF8toWide(kd.strings);
4236 }
4237 DosClose (handle);
4238 }
4239 return buf;
4240 }
4241 #endif
4242
4206 /* 4243 /*
4207 * Initializes the Dynamic Windows engine. 4244 * Initializes the Dynamic Windows engine.
4208 * Parameters: 4245 * Parameters:
4209 * newthread: True if this is the only thread. 4246 * newthread: True if this is the only thread.
4210 * False if there is already a message loop running. 4247 * False if there is already a message loop running.
4232 if(!_dw_exec_dir[0]) 4269 if(!_dw_exec_dir[0])
4233 _getcwd(_dw_exec_dir, MAX_PATH); 4270 _getcwd(_dw_exec_dir, MAX_PATH);
4234 4271
4235 if(newthread) 4272 if(newthread)
4236 { 4273 {
4274 #ifdef UNICODE
4275 UniChar *kbd;
4276 UniChar suCodepage[MAX_CP_SPEC]; /* conversion specifier */
4277 #endif
4237 dwhab = WinInitialize(0); 4278 dwhab = WinInitialize(0);
4238 dwhmq = WinCreateMsgQueue(dwhab, 0); 4279 dwhmq = WinCreateMsgQueue(dwhab, 0);
4239 #ifdef UNICODE 4280 #ifdef UNICODE
4281 /* Create the conversion object */
4282 UniMapCpToUcsCp(1208, suCodepage, MAX_CP_NAME);
4283 UniStrcat(suCodepage, (UniChar *) L"@map=cdra,path=no");
4284 UniCreateUconvObject(suCodepage, &Uconv);
4240 /* Create the Unicode atom for copy and paste */ 4285 /* Create the Unicode atom for copy and paste */
4241 Unicode = WinAddAtom(WinQuerySystemAtomTable(), (PSZ)"text/unicode"); 4286 Unicode = WinAddAtom(WinQuerySystemAtomTable(), (PSZ)"text/unicode");
4242 /* TODO: Need to figure out how to determine the correct keyboard here */ 4287 /* Figure out how to determine the correct keyboard here */
4243 UniCreateKeyboard(&Keyboard, (UniChar *) L"de", 0); 4288 kbd = _detect_keyb();
4289 /* Default to US if could not detect */
4290 UniCreateKeyboard(&Keyboard, (UniChar *)kbd ? kbd : L"us", 0);
4291 /* Free temporary memory */
4292 if(kbd)
4293 free(kbd);
4244 /* Set the codepage to 1208 (UTF-8) */ 4294 /* Set the codepage to 1208 (UTF-8) */
4245 WinSetCp(dwhmq, 1208); 4295 WinSetCp(dwhmq, 1208);
4246 #endif 4296 #endif
4247 } 4297 }
4248 4298
11416 /* Deinit the GBM */ 11466 /* Deinit the GBM */
11417 if(_gbm_deinit) 11467 if(_gbm_deinit)
11418 _gbm_deinit(); 11468 _gbm_deinit();
11419 11469
11420 #ifdef UNICODE 11470 #ifdef UNICODE
11421 /* Deregister the Unicode clipboard format */ 11471 /* Free the conversion object */
11422 WinDeleteAtom(WinQuerySystemAtomTable(), Unicode); 11472 UniFreeUconvObject(Uconv);
11473 /* Deregister the Unicode clipboard format */
11474 WinDeleteAtom(WinQuerySystemAtomTable(), Unicode);
11423 #endif 11475 #endif
11424 11476
11425 /* Destroy the main message queue and anchor block */ 11477 /* Destroy the main message queue and anchor block */
11426 WinDestroyMsgQueue(dwhmq); 11478 WinDestroyMsgQueue(dwhmq);
11427 WinTerminate(dwhab); 11479 WinTerminate(dwhab);