summaryrefslogtreecommitdiffstats
path: root/kxkb/extension.cpp
blob: df61e2fa23a168f652280e96da75bc69166e726b (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
#include <string.h>
#include <errno.h>

#include <tqstring.h>
#include <tqmap.h>
#include <tqfile.h>
#include <tqdir.h>

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

#include <X11/Xatom.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBfile.h>
#include <X11/extensions/XKBrules.h>
#include <X11/extensions/XKBgeom.h>
#include <X11/extensions/XKM.h>

#include "extension.h"


static TQString getLayoutKey(const TQString& layout, const TQString& variant)
{
	return layout + "." + variant;
}

XKBExtension::XKBExtension(Display *d)
{
	if ( d == NULL )
		d = tqt_xdisplay();
	m_dpy = d;
	
//	TQStringList dirs = TDEGlobal::dirs()->findDirs ( "tmp", "" );
//	m_tempDir = dirs.count() == 0 ? "/tmp/" : dirs[0];
	m_tempDir = locateLocal("tmp", "");
}

bool XKBExtension::init()
{
    // Verify the Xlib has matching XKB extension.

    int major = XkbMajorVersion;
    int minor = XkbMinorVersion;
	
    if (!XkbLibraryVersion(&major, &minor))
    {
        kdError() << "[kxkb-extension] Xlib XKB extension " << major << '.' << minor <<
            " != " << XkbMajorVersion << '.' << XkbMinorVersion << endl;
        return false;
    }

    // Verify the X server has matching XKB extension.

    int opcode_rtrn;
    int error_rtrn;
    int xkb_opcode;
    if (!XkbQueryExtension(m_dpy, &opcode_rtrn, &xkb_opcode, &error_rtrn,
                         &major, &minor))
    {
        kdError() << "[kxkb-extension] X server XKB extension " << major << '.' << minor <<
            " != " << XkbMajorVersion << '.' << XkbMinorVersion << endl;
        return false;
    }

    // Do it, or face horrible memory corrupting bugs
    ::XkbInitAtoms(NULL);

    // watch group change events
    XkbSelectEventDetails(m_dpy, XkbUseCoreKbd, XkbStateNotify,
                          XkbAllStateComponentsMask, XkbGroupStateMask);

    return true;
}

XKBExtension::~XKBExtension()
{
/*	if( m_compiledLayoutFileNames.isEmpty() == false )
		deletePrecompiledLayouts();*/
}

bool XKBExtension::setXkbOptions(const XkbOptions options)
{
	TQString exe = TDEGlobal::dirs()->findExe("setxkbmap");
	if (exe.isEmpty())
		return false;

	TDEProcess p;
	p << exe;

	if (!options.layouts.isEmpty())
	{
		p << "-layout";
		p << options.layouts;
	}

	if (!options.variants.isEmpty())
	{
		p << "-variant";
		p << options.variants;
	}
	
	if (!options.model.isEmpty()) {
		p << "-model";
		p << options.model;
	}

	if (options.resetOld) {
		p << "-option";
	}

	if (!options.options.isEmpty()) {
		p << "-option";

		if (options.resetOld)
		{
			p << options.options;
		}
		else
		{
			// Avoid duplication of options in Append mode
			TQStringList srvOptions = TQStringList::split(",", XKBExtension::getServerOptions());
			TQStringList kxkbOptions = TQStringList::split(",", options.options);
			TQStringList newOptions;
			for (TQStringList::Iterator it = kxkbOptions.begin(); it != kxkbOptions.end(); ++it)
			{
				TQString option(*it);
				if (!srvOptions.contains(option))
				{
					newOptions << option;
				}
			}
			p << newOptions.join(",");
		}
	}

	kdDebug() << "[setXkbOptions] Command: " << p.args() << endl;

	p.start(TDEProcess::Block);

	return p.normalExit() && (p.exitStatus() == 0);
}

TQString XKBExtension::getServerOptions()
{
    XkbRF_VarDefsRec vd;
    if (XkbRF_GetNamesProp(tqt_xdisplay(), nullptr, &vd) && vd.options)
    {
        kdDebug() << "[kxkb-extension] Got server options " << vd.options << endl;
        return TQString(vd.options);
    }
    return TQString::null;
}

bool XKBExtension::setGroup(unsigned int group)
{
	kdDebug() << "[kxkb-extension] Setting group " << group << endl;
	return XkbLockGroup( m_dpy, XkbUseCoreKbd, group );
}

unsigned int XKBExtension::getGroup() const
{
	XkbStateRec xkbState;
	XkbGetState( m_dpy, XkbUseCoreKbd, &xkbState );
	return xkbState.group;
}

/** Examines an X Event passed to it and takes actions if the event is of
  * interest to KXkb */
void XKBExtension::processXEvent(XEvent *event) {
	XkbEvent* xkb_event = (XkbEvent*)event;
    if (xkb_event->any.xkb_type == XkbStateNotify) {
		emit groupChanged(xkb_event->state.group);
    }
}

#include "extension.moc"