1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26
27
28 #include <stdio.h>
29
30 #include "unx/saldisp.hxx"
31 #include "unx/saldata.hxx"
32 #include "unx/i18n_xkb.hxx"
33
SalI18N_KeyboardExtension(Display * pDisplay)34 SalI18N_KeyboardExtension::SalI18N_KeyboardExtension( Display* pDisplay)
35 : mbUseExtension( true )
36 , mnDefaultGroup( 0 )
37 {
38 mpDisplay = pDisplay;
39
40 // allow user to set the default keyboard group idx or to disable the usage
41 // of x keyboard extension at all:
42 // setenv SAL_XKEYBOARDGROUP disables keyboard extension
43 // setenv SAL_XKEYBOARDGROUP 2 sets the keyboard group index to 2
44 // keyboard group index must be in [1,4], may be specified in hex or decimal
45 static char *pUseKeyboardExtension = getenv( "SAL_XKEYBOARDGROUP" );
46 if ( pUseKeyboardExtension != NULL )
47 {
48 mbUseExtension = pUseKeyboardExtension[0] != '\0' ;
49 if ( mbUseExtension )
50 mnDefaultGroup = strtol( pUseKeyboardExtension, NULL, 0 );
51 if ( mnDefaultGroup > XkbMaxKbdGroup )
52 mnDefaultGroup = 0;
53 }
54
55 // query XServer support for XKB Extension,
56 // do not call XQueryExtension() / XInitExtension() due to possible version
57 // clashes !
58 if ( mbUseExtension )
59 {
60 int nMajorExtOpcode;
61 int nExtMajorVersion = XkbMajorVersion;
62 int nExtMinorVersion = XkbMinorVersion;
63
64 mbUseExtension = (sal_Bool)XkbQueryExtension( mpDisplay,
65 &nMajorExtOpcode, (int*)&mnEventBase, (int*)&mnErrorBase,
66 &nExtMajorVersion, &nExtMinorVersion );
67 }
68
69 // query notification for changes of the keyboard group
70 if ( mbUseExtension )
71 {
72 #define XkbGroupMask ( XkbGroupStateMask | XkbGroupBaseMask \
73 | XkbGroupLatchMask | XkbGroupLockMask )
74
75 mbUseExtension = XkbSelectEventDetails( mpDisplay,
76 XkbUseCoreKbd, XkbStateNotify, XkbGroupMask, XkbGroupMask );
77 }
78
79 // query initial keyboard group
80 if ( mbUseExtension )
81 {
82 XkbStateRec aStateRecord;
83 XkbGetState( mpDisplay, XkbUseCoreKbd, &aStateRecord );
84 mnGroup = aStateRecord.group;
85 }
86 }
87
88 void
Dispatch(XEvent * pEvent)89 SalI18N_KeyboardExtension::Dispatch( XEvent* pEvent)
90 {
91 // must the event be handled?
92 if ( !mbUseExtension
93 || (pEvent->type != mnEventBase) )
94 return;
95
96 // only handle state notify events for now, and only interested
97 // in group details
98 sal_uInt32 nXKBType = ((XkbAnyEvent*)pEvent)->xkb_type;
99 switch ( nXKBType )
100 {
101 case XkbStateNotify:
102
103 mnGroup = ((XkbStateNotifyEvent*)pEvent)->group;
104 break;
105
106 default:
107
108 #if OSL_DEBUG_LEVEL > 1
109 fprintf(stderr, "Got unrequested XkbAnyEvent %#x/%i\n",
110 static_cast<unsigned int>(nXKBType), static_cast<int>(nXKBType) );
111 #endif
112 break;
113 }
114 }
115
LookupKeysymInGroup(sal_uInt32 nKeyCode,sal_uInt32 nShiftState,sal_uInt32 nGroup) const116 sal_uInt32 SalI18N_KeyboardExtension::LookupKeysymInGroup( sal_uInt32 nKeyCode,
117 sal_uInt32 nShiftState, sal_uInt32 nGroup ) const
118 {
119 nShiftState &= ShiftMask;
120
121 KeySym nKeySymbol = XkbKeycodeToKeysym( mpDisplay, nKeyCode, nGroup, nShiftState );
122 return nKeySymbol;
123 }
124
125
126