summaryrefslogtreecommitdiffstats
path: root/xkb.c
blob: f9395e6fd2f9ab8525d2b2223cc0e486f21220ab (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
#include <stdio.h>
#include <fcntl.h>
#include <Python.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBrules.h>
#include <gdk/gdkx.h>
#include <assert.h>

#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))

#define MAX_COMPONENTS 400
#define XKB_XFREE86_RULES "/usr/X11R6/lib/X11/xkb/rules/xfree86"
#define XKB_SUN_RULES "/usr/X11R6/lib/X11/xkb/rules/sun"

static XkbRF_RulesPtr rules;

PyObject *list_rules ();
PyObject *set_rule (PyObject *, PyObject *);
PyObject * py_get_rulesbase ();
PyObject *py_get_mousekeys ();
PyObject *py_set_mousekeys (PyObject * s, PyObject * args);

char * get_rulesbase ();

static PyMethodDef _xkbMethods[] = {
    { "list_rules", list_rules, 1 },
    { "set_rule", set_rule, 1 },
    { "get_rulesbase", py_get_rulesbase, 1 },
    { "get_mousekeys", py_get_mousekeys, 1 },
    { "set_mousekeys", py_set_mousekeys, 1 },
    { NULL, NULL }
};

char *
get_rulesbase ()
{
  char *rulesbase = XKB_XFREE86_RULES;
#ifdef __sparc__
  int fd;
  
  fd = open("/dev/kbd", O_RDONLY);
  if (fd >= 0) {
    rulesbase = XKB_SUN_RULES;
  }
#endif

  return rulesbase;
}

PyObject *
py_get_rulesbase ()
{
  return Py_BuildValue ("s", get_rulesbase ());
}


PyObject *
py_get_mousekeys ()
{
    XkbDescPtr desc;
    
    desc = XkbGetKeyboard (GDK_DISPLAY(),
			   XkbAllComponentsMask,
			   XkbUseCoreKbd);
    if (desc == NULL) {
	return NULL;
    }
    
    if (XkbGetControls (GDK_DISPLAY(), ~0, desc) != Success) {
	return NULL;
    }

    if (desc->ctrls->enabled_ctrls & XkbMouseKeysMask)
	return Py_BuildValue ("i", 1);
    else
	return Py_BuildValue ("i", 0);
}

PyObject *
py_set_mousekeys (PyObject * s, PyObject * args)
{
    int setting;
    
    if (!PyArg_ParseTuple(args, "i", &setting))
	return NULL;

    if (XkbChangeEnabledControls (GDK_DISPLAY(), XkbUseCoreKbd,
				  XkbMouseKeysMask,
				  setting ? XkbMouseKeysMask : 0) != True) {
	return NULL;
    }
    
    Py_INCREF(Py_None);
    return Py_None;
}

void 
init_xkb ()
{
  char *lang;
  PyObject *m;
  m = Py_InitModule ("_xkb", _xkbMethods);

  lang = getenv ("LC_ALL");
  rules = XkbRF_Load (get_rulesbase (), (lang) ? lang : "C", True, True);
  if (!rules)
    Py_FatalError ("unable to load XKB rules database");

  if (PyErr_Occurred ())
    Py_FatalError ("can't initialize module _xkb");
}

PyObject *
set_rule (PyObject *self, PyObject *args)
{
  XkbRF_VarDefsRec defs;
  XkbComponentNamesRec rnames;

  if (!PyArg_ParseTuple (args, "ssss", &defs.model, &defs.layout, &defs.variant, &defs.options))
    return NULL;

  if (!strcmp (defs.model, ""))
    defs.model = NULL;
  if (!strcmp (defs.layout, ""))
    defs.layout = NULL;
  if (!strcmp (defs.variant, ""))
    defs.variant = NULL;
  if (!strcmp (defs.options, ""))
    defs.options = NULL;

  XkbRF_GetComponents (rules, &defs, &rnames);

  XkbGetKeyboardByName (GDK_DISPLAY (), XkbUseCoreKbd, &rnames, 
			XkbGBN_AllComponentsMask, 
                        XkbGBN_AllComponentsMask, True);

  XkbRF_SetNamesProp (GDK_DISPLAY (), get_rulesbase (), &defs);

  return Py_BuildValue ("i", 1);
}

PyObject * 
list_rules ()
{
  PyObject *models, *layouts, *variants, *options, *py_rules;
  int num_comp;
  int i;

  models = PyDict_New ();
  num_comp = min (rules->models.num_desc, MAX_COMPONENTS);
  for (i = 0; i < num_comp; i++) 
    {
      PyObject *desc, *name;

      name = PyString_FromString (rules->models.desc[i].name);
      desc = PyString_FromString (rules->models.desc[i].desc);
      PyDict_SetItem (models, name, desc);
    }

  layouts = PyDict_New ();
  num_comp = min (rules->layouts.num_desc, MAX_COMPONENTS);
  for (i = 0; i < num_comp; i++) 
    {
      PyObject *desc, *name;

      name = PyString_FromString (rules->layouts.desc[i].name);
      desc = PyString_FromString (rules->layouts.desc[i].desc);
      PyDict_SetItem (layouts, name, desc);
    }
  
  variants = PyDict_New ();
  num_comp = min (rules->variants.num_desc, MAX_COMPONENTS);
  for (i = 0; i < num_comp; i++) 
    {
      PyObject *desc, *name;

      name = PyString_FromString (rules->variants.desc[i].name);
      desc = PyString_FromString (rules->variants.desc[i].desc);
      PyDict_SetItem (variants, name, desc);
    }

  options = PyDict_New ();
  num_comp = min (rules->options.num_desc, MAX_COMPONENTS);
  for (i = 0; i < num_comp; i++) 
    {
      PyObject *desc, *name;

      name = PyString_FromString (rules->options.desc[i].name);
      desc = PyString_FromString (rules->options.desc[i].desc);
      PyDict_SetItem (options, name, desc);
    }

  py_rules = PyTuple_New (4);

  PyTuple_SET_ITEM (py_rules, 0, models);
  PyTuple_SET_ITEM (py_rules, 1, layouts);
  PyTuple_SET_ITEM (py_rules, 2, variants);
  PyTuple_SET_ITEM (py_rules, 3, options);

  return py_rules;
}

int main (int argc, char **argv)
{
  int major, minor, event, error, reason;
  int max;
  int i;
  Display *dpy;
  XkbComponentNamesRec ptrns = { NULL, NULL, NULL, NULL, NULL, "*" };
  XkbComponentListPtr comps;
  XkbDescPtr xkb;


  major = XkbMajorVersion;
  minor = XkbMinorVersion;
/*    if (!XkbQueryExtension(dpy, &op, &event, &error, &major, &minor))  */
/*      { */
/*        fprintf (stderr, "no xkb\n"); */
/*      } */

  dpy = XkbOpenDisplay (NULL, &event, &error, &major, &minor, &reason);
  assert (dpy);

  max = MAX_COMPONENTS;
  comps = XkbListComponents (dpy, XkbUseCoreKbd, &ptrns, &max);
  assert (comps);
/*    for (i = 0; i < comps->num_geometry; i++) */
/*    { */
/*      printf ("%s\n", comps->geometry[i].name); */
/*    } */
 

  list_rules ();

/*    xkb = XkbGetKeyboard (dpy, XkbAllComponentsMask, XkbUseCoreKbd); */
/*    xkb->names = NULL; */
/*    XkbGetNames (dpy, XkbGeometryNameMask, xkb); */
/*    printf ("%s\n", XkbAtomText (dpy, xkb->names->geometry, 3)); */

  return 0;
}