summaryrefslogtreecommitdiffstats
path: root/kkbswitch/xkeyboard.cpp
blob: de78bc48f07253ee6091fbba3531ffbc0ea86105 (plain)
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
/***************************************************************************
                          xkeyboard.cpp  -  description
                             -------------------
    begin                : Sun Jul 8 2001
    copyright            : (C) 2001 by Leonid Zeitlin
    email                : lz@europe.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "xkeyboard.h"

#include <ntqwindowdefs.h>
#include <ntqstringlist.h>

#include <kdebug.h>
#include <tdelocale.h>

XKeyboard *XKeyboard::m_self = 0;

XKeyboard::XKeyboard()
{
  Display *display = tqt_xdisplay();
#ifdef HAVE_LIBXKLAVIER
//  XklSetDebugLevel(0);
  XklSetLogAppender(XklLogAppender);
  XklInit(display);
  XklRegisterStateCallback(XklStateCallback, this);
  XklRegisterConfigCallback(XklConfigCallback, this);
#else
  int opcode, errorBase, major = XkbMajorVersion, minor = XkbMinorVersion;

  // check the library version
  if (!XkbLibraryVersion(&major, &minor)) {
    kdWarning() << i18n("This program was built against XKB extension library\n"
      "version %1.%2, but is run with the library version %3.%4.\n"
      "This may cause various problems and even result in a complete\n"
      "failure to function\n").arg(XkbMajorVersion).arg(XkbMinorVersion).arg(major).arg(minor);
  }

  // initialize the extension
  m_xkb_available = XkbQueryExtension(display, &opcode, &m_event_code, &errorBase, &major, &minor);
  if (!m_xkb_available) {
    kdError() << i18n("The X Server does not support a compatible XKB extension.\n"
      "Either the server is not XKB-capable or the extension was disabled.\n"
      "This program would not work with this server, so it will exit now\n");
  }
  else {
    // register for XKB events
    //// group state change, i.e. the current group changed:
    XkbSelectEventDetails(display, XkbUseCoreKbd, XkbStateNotify,
      XkbAllStateComponentsMask, XkbGroupStateMask);
    //// keyboard mapping change:
    XkbSelectEventDetails(display, XkbUseCoreKbd, XkbMapNotify,
      XkbAllMapComponentsMask, XkbKeySymsMask);
    //// group names change:
    XkbSelectEventDetails(display, XkbUseCoreKbd, XkbNamesNotify,
      XkbAllNamesMask, XkbGroupNamesMask);
    //// new keyboard:
    XkbSelectEventDetails(display, XkbUseCoreKbd, XkbNewKeyboardNotify,
      XkbAllNewKeyboardEventsMask, XkbAllNewKeyboardEventsMask);
    // retrieve the number of keyboard groups
    retrieveNumKbdGroups();
  }
#endif
  m_self = this;
}

XKeyboard::~XKeyboard(){
#ifdef HAVE_LIBXKLAVIER
  XklStopListen();
  XklTerm();
#endif  
}

/** Determine if the given XEvent e is an XKB event with type XkbStateNotify.
  * If so, newgroupno will return the new keyboard group #.
  * In other words, return value of true means that this XEvent tells us
  * that the user just switched the keyboard group to the new value
  * newgroupno */
/*bool XKeyboard::isXkbStateNotifyEvent(XEvent *e, int *newgroupno){
  bool ret = false;
  if (e->type == m_event_code) {
    XkbEvent *kb_ev = (XkbEvent *) e;
    if (kb_ev->any.xkb_type == XkbStateNotify) {
      ret = true;
      *newgroupno = kb_ev->state.group;
    }
  }
  return ret;
}*/

/** Set the current keyboard group to the given groupno */
void XKeyboard::setGroupNo(int groupno){
#ifdef HAVE_LIBXKLAVIER
  XklLockGroup(groupno);
#else
  XkbLockGroup(tqt_xdisplay(), XkbUseCoreKbd, groupno);
#endif  
}

#ifndef HAVE_LIBXKLAVIER
extern "C" {
  static int IgnoreXError(Display *, XErrorEvent *) {
    return 0;
  }
}
#endif

/** Get the names of the currently configured keyboard groups */
void XKeyboard::getGroupNames(TQStringList &list){
#ifdef HAVE_LIBXKLAVIER
  const char** groupnames = XklGetGroupNames();
  int numgroups = XklGetNumGroups();
  for (int i = 0; i < numgroups; i++) 
    list.append(groupnames[i]);
#else
  XkbDescRec xkb;
  Display *display = tqt_xdisplay();
  char *names[XkbNumKbdGroups];

  memset(&xkb, 0, sizeof(xkb));
  xkb.device_spec = XkbUseCoreKbd;
  XkbGetNames(display, XkbGroupNamesMask, &xkb);
  memset(names, 0, sizeof(char *) * XkbNumKbdGroups);
  // XGetAtomNames below may generate BadAtom error, which is not a problem.
  // (it may happen if the name for a group was not defined)
  // Thus we temporarily ignore X errors
  XErrorHandler old_handler = XSetErrorHandler(IgnoreXError);
  XGetAtomNames(display, xkb.names->groups, m_numgroups, names);
  // resume normal X error processing
  XSetErrorHandler(old_handler);
  for (int i = 0; i < m_numgroups; i++) {
    if (names[i]) {
      list.append(names[i]);
      XFree(names[i]);
    }
    else list.append(TQString::null);
  }
  XkbFreeNames(&xkb, XkbGroupNamesMask, 1);
#endif  
}

XKeyboard * XKeyboard::self()
{
  return m_self;
}

/** return the current keyboard group index */
int XKeyboard::getGroupNo(){
#ifdef HAVE_LIBXKLAVIER
  return XklGetCurrentState()->group;
#else
  XkbStateRec rec;
  XkbGetState(tqt_xdisplay(), XkbUseCoreKbd, &rec);
  return (int) rec.group;
#endif  
}

/** Returns if the given event notifies us of a keyboard layout change that requires a
  * reconfiguration
  * (e.g. new group added, group names changed, etc.) */
/*bool XKeyboard::isLayoutChangeEvent(XEvent *e){
  if (e->type == m_event_code) {
    XkbEvent *xkb_ev = (XkbEvent *) e;
    if ((xkb_ev->any.xkb_type == XkbMapNotify) && (xkb_ev->map.changed & XkbKeySymsMask)
      || (xkb_ev->any.xkb_type == XkbNamesNotify) && (xkb_ev->names.changed & XkbGroupNamesMask)
      || (xkb_ev->any.xkb_type == XkbNewKeyboardNotify)) {
      retrieveNumKbdGroups();
      return true;
    }
  }
  return false;
}*/

#ifndef HAVE_LIBXKLAVIER
/** No descriptions */
void XKeyboard::retrieveNumKbdGroups(){
  XkbDescRec xkb;

  memset(&xkb, 0, sizeof(xkb));
  /* Interestingly, in RedHat 6.0 (XFree86 3.3.3.1) the XkbGetControls call
  below works even if xkb.device_spec is not set. But in RedHat 7.1 (XFree86 4.0.3)
  it returns BadImplementation status code, and you have to specify
  xkb.device_spec = XkbUseCoreKbd. */
  xkb.device_spec = XkbUseCoreKbd;
  XkbGetControls(tqt_xdisplay(), XkbGroupsWrapMask, &xkb);
  m_numgroups = xkb.ctrls->num_groups;
  XkbFreeControls(&xkb, XkbGroupsWrapMask, 1);
}
#endif

/** Examines an X Event passed to it and takes actions if the event is of
  * interest to XKeyboard */
void XKeyboard::processEvent(XEvent *ev) {
#ifdef HAVE_LIBXKLAVIER
  XklFilterEvents(ev);
#else
  if (ev->type == m_event_code) {
    // This an XKB event
    XkbEvent *xkb_ev = (XkbEvent *) ev;
    if (xkb_ev->any.xkb_type == XkbStateNotify) {
      // state notify event, the current group has changed
      emit groupChanged(xkb_ev->state.group);
    }
    else if ((xkb_ev->any.xkb_type == XkbMapNotify) && (xkb_ev->map.changed & XkbKeySymsMask)
      || (xkb_ev->any.xkb_type == XkbNamesNotify) && (xkb_ev->names.changed & XkbGroupNamesMask)
      || (xkb_ev->any.xkb_type == XkbNewKeyboardNotify)) {
      // keyboard layout has changed
      retrieveNumKbdGroups();
      emit layoutChanged();
    }
  }
#endif  
}

#ifdef HAVE_LIBXKLAVIER
void XKeyboard::XklStateCallback(XklStateChange changeType, int group,
  Bool /*restore*/, void */*userData*/)
{
  if (changeType == GROUP_CHANGED)
    emit XKeyboard::self()->groupChanged(group);
}

void XKeyboard::XklConfigCallback(void */*userData*/)
{
  emit XKeyboard::self()->layoutChanged();
}

void XKeyboard::XklLogAppender(const char file[], const char function[],
    int level, const char format[], va_list args)
{
  int size = vsnprintf(NULL, 0, format, args);
  char *str = new char[size + 1];
  vsnprintf(str, size, format, args);
  kdDebug() << file << "/" << function << ": " << str << endl;
  delete[] str;
}    
#endif