summaryrefslogtreecommitdiffstats
path: root/kxkb/x11helper.cpp
blob: c4c051c91aa571db5950d4f8897307c2627529b5 (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
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
#include <tqdom.h>
#include <tqdir.h>
#include <tqstring.h>
#include <tqwindowdefs.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqdict.h>
#include <tqregexp.h>

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

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#define explicit int_explicit        // avoid compiler name clash in XKBlib.h
#include <X11/XKBlib.h>
#undef explicit
#include <X11/extensions/XKBrules.h>

#include "x11helper.h"
#include "config.h"


// Compiler will size array automatically.
static const char* X11DirList[] =
    {
#ifdef X11_XKB_RULES_DIR
        X11_XKB_RULES_DIR,
#endif
        XLIBDIR,
        "/usr/share/X11/",
        "/usr/lib/X11/",
        "/usr/lib64/X11/",
        "/usr/X11/share/X11/",
        "/usr/X11/lib/X11/",
        "/usr/X11/lib64/X11/",
        "/usr/X11R7/share/X11/",
        "/usr/X11R7/lib/X11/",
        "/usr/X11R7/lib64/X11/",
        "/usr/X11R6/share/X11/",
        "/usr/X11R6/lib/X11/",
        "/usr/X11R6/lib64/X11/",
        "/usr/local/X11/share/X11/",
        "/usr/local/X11/lib/X11/",
        "/usr/local/X11/lib64/X11/",
        "/usr/local/X11R7/share/X11/",
        "/usr/local/X11R7/lib/X11/",
        "/usr/local/X11R7/lib64/X11/",
        "/usr/local/X11R6/share/X11/",
        "/usr/local/X11R6/lib/X11/",
        "/usr/local/X11R6/lib64/X11/",
        "/usr/local/share/X11/",
        "/usr/local/lib/X11/",
        "/usr/local/lib64/X11/",
        "/usr/pkg/share/X11/",
        "/usr/pkg/xorg/lib/X11/",
        "/etc/X11/"
    };

// Compiler will size array automatically.
static const char* rulesFileList[] =
    {
	"xkb/rules/xorg",
	"xkb/rules/xfree86"
    };

// Macro will return number of elements in any static array as long as the
// array has at least one element.
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))

static const int X11_DIR_COUNT = ARRAY_SIZE(X11DirList);
static const int X11_RULES_COUNT = ARRAY_SIZE(rulesFileList);

const TQString X11Helper::X11_WIN_CLASS_ROOT = "<root>";
const TQString X11Helper::X11_WIN_CLASS_UNKNOWN = "<unknown>";

static const TQRegExp NON_CLEAN_LAYOUT_REGEXP("[^a-z]");

bool X11Helper::m_layoutsClean = true;

const TQString
X11Helper::findX11Dir()
{
	for(int ii=0; ii<X11_DIR_COUNT; ii++) {
		const char* xDir = X11DirList[ii];
		if( xDir != NULL && TQDir(TQString(xDir) + "xkb").exists() ) {
//			for(int jj=0; jj<X11_RULES_COUNT; jj++) {
//				
//			}
			return TQString(xDir);
	    }
	
//	  if( X11_DIR.isEmpty() ) {
//	    return;
//	  }
	}
	return NULL;	
}

const TQString
X11Helper::findXkbRulesFile(TQString x11Dir, Display *dpy)
{
	TQString rulesFile;
	XkbRF_VarDefsRec vd;
	char *tmp = NULL;

	if (XkbRF_GetNamesProp(dpy, &tmp, &vd) && tmp != NULL ) {
// 			kdDebug() << "namesprop " << tmp  << endl;
	  	rulesFile = x11Dir + TQString("xkb/rules/%1").arg(tmp);
// 			kdDebug() << "rulesF " << rulesFile  << endl;
	}
	else {
    // old way
    	for(int ii=0; ii<X11_RULES_COUNT; ii++) {
    		const char* ruleFile = rulesFileList[ii];
     		TQString xruleFilePath = x11Dir + ruleFile;
//  			kdDebug() << "trying " << xruleFilePath << endl;
		    if( TQFile(xruleFilePath).exists() ) {
				rulesFile = xruleFilePath;
			break;
		    }
    	}
    }

	return rulesFile;
}

RulesInfo*
X11Helper::loadRules(const TQString& file, bool layoutsOnly) {
	XkbRF_RulesPtr xkbRules = XkbRF_Load(TQFile::encodeName(file).data(), "", true, true);

	if (xkbRules == NULL) {
// throw Exception
		return NULL;
	}

	RulesInfo* rulesInfo = new RulesInfo();

	for (int i = 0; i < xkbRules->layouts.num_desc; ++i) {
		TQString layoutName(xkbRules->layouts.desc[i].name);
		rulesInfo->layouts.replace( layoutName, tqstrdup( xkbRules->layouts.desc[i].desc ) );

		if( m_layoutsClean == true
				  && layoutName.find( NON_CLEAN_LAYOUT_REGEXP ) != -1 
				  && layoutName.endsWith("/jp") == false ) {
			kdDebug() << "Layouts are not clean (Xorg < 6.9.0 or XFree86)" << endl;
			m_layoutsClean = false;
		}
	}

	if( layoutsOnly == true ) {
		XkbRF_Free(xkbRules, true);
		return rulesInfo;
	}

  for (int i = 0; i < xkbRules->models.num_desc; ++i)
      rulesInfo->models.replace(xkbRules->models.desc[i].name, tqstrdup( xkbRules->models.desc[i].desc ) );

  // Prefer XML file for Xkb options
  if (TQFile(file + ".xml").exists()) {
      XkbRF_Free(xkbRules, true);

      TQDomDocument xmlrules("xkbrules");
      TQFile xmlfile(file + ".xml");
      if (!xmlfile.open(IO_ReadOnly)) {
          return NULL;
      }
      if (!xmlrules.setContent(&xmlfile)) {
          xmlfile.close();
          return NULL;
      }
      xmlfile.close();

      TQDomElement options = xmlrules.documentElement().namedItem("optionList").toElement();
      TQDomNode optGroupNode = options.firstChild();
      while (!optGroupNode.isNull()) {
          TQDomElement optGroupElem = optGroupNode.toElement();
          if (optGroupElem.tagName() == "group") {
              TQDomNode optNode = optGroupElem.firstChild();
              while (!optNode.isNull()) {
                TQDomElement optElem = optNode.toElement();
                if (!optElem.isNull()) {
                    // This might be either a configItem (group) or an option tag
                    // If it is an option tag, it contains a configItem that describes
                    // the option
                    if (optElem.tagName() == "option") {
                        optElem = optElem.namedItem("configItem").toElement();
                    }

                    TQString optName = optElem.namedItem("name").toElement().text();
                    TQString optDesc = optElem.namedItem("description").toElement().text();
                    if (optDesc.isEmpty()) {
                        optDesc = optName;
                    }
                    // Items from these 'meta' groups fall into other groups
                    // Admittedly not the best way to handle this
                    if (optName == "currencysign" || optName == "compat") continue;

                    // HACK this should be called "compose" or else the code breaks
                    if (optName == "Compose key") optName = "compose";

                    rulesInfo->options.replace(optName.ascii(), tqstrdup(optDesc.ascii()));
                }
                optNode = optNode.nextSibling();
              }
          }
          optGroupNode = optGroupNode.nextSibling();
      }
  }
  else {
      for (int i = 0; i < xkbRules->options.num_desc; ++i)
          rulesInfo->options.replace(xkbRules->options.desc[i].name, tqstrdup( xkbRules->options.desc[i].desc ) );

      XkbRF_Free(xkbRules, true);

      // workaround for empty 'compose' options group description
      if( rulesInfo->options.find("compose:menu") && !rulesInfo->options.find("compose") ) {
          rulesInfo->options.replace("compose", I18N_NOOP("Compose Key Position"));
      }
  }

//   // workaround for empty misc options group description in XFree86 4.4.0
//   if( rulesInfo->options.find("numpad:microsoft") && !rulesInfo->options.find("misc") ) {
//     rulesInfo->options.replace("misc", "Miscellaneous compatibility options" );
//   }

  return rulesInfo;
}

// check $oldlayouts and $nonlatin groups for XFree 4.3 and later
OldLayouts*
X11Helper::loadOldLayouts(const TQString& rulesFile)
{
  static const char* oldLayoutsTag = "! $oldlayouts";
  static const char* nonLatinLayoutsTag = "! $nonlatin";
  TQStringList m_oldLayouts;
  TQStringList m_nonLatinLayouts;
  
  TQFile f(rulesFile);
  
  if (f.open(IO_ReadOnly))
    {
      TQTextStream ts(&f);
      TQString line;

      while (!ts.eof()) {
	  line = ts.readLine().simplifyWhiteSpace();

	  if( line.find(oldLayoutsTag) == 0 ) {

	    line = line.mid(strlen(oldLayoutsTag));
	    line = line.mid(line.find('=')+1).simplifyWhiteSpace();
	    while( !ts.eof() && line.endsWith("\\") )
		line = line.left(line.length()-1) + ts.readLine();
	    line = line.simplifyWhiteSpace();

	    m_oldLayouts = TQStringList::split(TQRegExp("\\s"), line);
//	    kdDebug() << "oldlayouts " << m_oldLayouts.join("|") << endl;
	    if( !m_nonLatinLayouts.empty() )
	      break;
	    
	  }
	  else
	  if( line.find(nonLatinLayoutsTag) == 0 ) {

	    line = line.mid(strlen(nonLatinLayoutsTag)+1).simplifyWhiteSpace();
	    line = line.mid(line.find('=')+1).simplifyWhiteSpace();
	    while( !ts.eof() && line.endsWith("\\") )
		line = line.left(line.length()-1) + ts.readLine();
	    line = line.simplifyWhiteSpace();

	    m_nonLatinLayouts = TQStringList::split(TQRegExp("\\s"), line);
//	    kdDebug() << "nonlatin " << m_nonLatinLayouts.join("|") << endl;
	    if( !m_oldLayouts.empty() )
	      break;
	    
	  }
      }

      f.close();
    }
	
	OldLayouts* oldLayoutsStruct = new OldLayouts();
	oldLayoutsStruct->oldLayouts = m_oldLayouts;
	oldLayoutsStruct->nonLatinLayouts = m_nonLatinLayouts;
	
	return oldLayoutsStruct;
}


/* pretty simple algorithm - reads the layout file and
    tries to find "xkb_symbols"
    also checks whether previous line contains "hidden" to skip it
*/
TQStringList*
X11Helper::getVariants(const TQString& layout, const TQString& x11Dir, bool oldLayouts)
{
  TQStringList* result = new TQStringList();

  TQString file = x11Dir + "xkb/symbols/";
  // workaround for XFree 4.3 new directory for one-group layouts
  if( TQDir(file+"pc").exists() && !oldLayouts )
    file += "pc/";
    
  file += layout;

//  kdDebug() << "reading variants from " << file << endl;
  
  TQFile f(file);
  if (f.open(IO_ReadOnly))
    {
      TQTextStream ts(&f);

      TQString line;
      TQString prev_line;

      while (!ts.eof()) {
    	  prev_line = line;
	  line = ts.readLine().simplifyWhiteSpace();

	    if (line[0] == '#' || line.left(2) == "//" || line.isEmpty())
		continue;

	    int pos = line.find("xkb_symbols");
	    if (pos < 0)
		continue;

	    if( prev_line.find("hidden") >=0 )
		continue;

	    pos = line.find('"', pos) + 1;
	    int pos2 = line.find('"', pos);
	    if( pos < 0 || pos2 < 0 )
		continue;

	    result->append(line.mid(pos, pos2-pos));
//  kdDebug() << "adding variant " << line.mid(pos, pos2-pos) << endl;
      }

      f.close();
    }

	return result;
}

TQString 
X11Helper::getWindowClass(WId winId, Display* dpy)
{
  unsigned long nitems_ret, bytes_after_ret;
  unsigned char* prop_ret;
  Atom     type_ret;
  int      format_ret;
  Window w = (Window)winId;	// suppose WId == Window
  TQString  property;

  if( winId == X11Helper::UNKNOWN_WINDOW_ID ) {
	  kdDebug() << "Got window class for " << winId << ": '" << X11_WIN_CLASS_ROOT << "'" << endl;
	  return X11_WIN_CLASS_ROOT;
  }
  
//  kdDebug() << "Getting window class for " << winId << endl;
  if((XGetWindowProperty(dpy, w, XA_WM_CLASS, 0L, 256L, 0, XA_STRING,
			&type_ret, &format_ret, &nitems_ret,
			&bytes_after_ret, &prop_ret) == Success) && (type_ret != None)) {
    property = TQString::fromLocal8Bit(reinterpret_cast<char*>(prop_ret));
    XFree(prop_ret);
  }
  else {
	  property = X11_WIN_CLASS_UNKNOWN;
  }
  kdDebug() << "Got window class for " << winId << ": '" << property << "'" << endl;
  
  return property;
}

bool X11Helper::areSingleGroupsSupported()
{
	return true; //TODO:
}

void X11Helper::initializeTranslations() {
    // TDE is usually installed into some non-standard prefix and by default system-wide locale
    // dirs are not considered when searching for gettext message catalogues, so we have to add
    // it explicitly.
#ifdef WITH_XKB_TRANSLATIONS
    TDEGlobal::dirs()->addResourceDir("locale", XKB_CONFIG_LOCALE_DIR);
    TDEGlobal::locale()->insertCatalogue("xkeyboard-config");
#endif
}