summaryrefslogtreecommitdiff
path: root/hw/dmx/config/dmxconfig.c
blob: 2cc9ab396a4a4de30f47e64dbe485d3dc879e475 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
 * Copyright 2002-2003 Red Hat Inc., Durham, North Carolina.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation on the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * Authors:
 *   Rickard E. (Rik) Faith <faith@redhat.com>
 *
 */

/** \file
 * Provides interface for reading DMX configuration files and for
 * combining that information with command-line configuration parameters. */

#ifdef HAVE_DMX_CONFIG_H
#include <dmx-config.h>
#endif

#include "dmx.h"
#include "dmxinput.h"
#include "dmxconfig.h"
#include "dmxparse.h"
#include "dmxlog.h"
#include "dmxcb.h"
#include "dmxstat.h"
#include "parser.h"

extern int yyparse(void);
extern FILE *yyin;

static char *dmxXkbRules;
static char *dmxXkbModel;
static char *dmxXkbLayout;
static char *dmxXkbVariant;
static char *dmxXkbOptions;

/** Stores lists of configuration information. */
typedef struct DMXConfigListStruct {
    const char *name;
    struct DMXConfigListStruct *next;
} DMXConfigList, *DMXConfigListPtr;

/** This stucture stores the parsed configuration information. */
typedef struct DMXConfigCmdStruct {
    const char *filename;
    const char *config;
    DMXConfigList *displays;
    DMXConfigList *inputs;
    DMXConfigList *xinputs;
} DMXConfigCmd, *DMXConfigCmdPtr;

DMXConfigEntryPtr dmxConfigEntry;
static DMXConfigCmd dmxConfigCmd;

static int dmxDisplaysFromCommandLine;

/** Make a note that \a display is the name of an X11 display that
 * should be initialized as a backend (output) display.  Called from
 * #ddxProcessArgument. */
void
dmxConfigStoreDisplay(const char *display)
{
    DMXConfigListPtr entry = malloc(sizeof(*entry));

    entry->name = strdup(display);
    entry->next = NULL;
    if (!dmxConfigCmd.displays)
        dmxConfigCmd.displays = entry;
    else {
        DMXConfigList *pt;

        for (pt = dmxConfigCmd.displays; pt->next; pt = pt->next);
        if (!pt)
            dmxLog(dmxFatal, "dmxConfigStoreDisplay: end of list non-NULL\n");
        pt->next = entry;
    }
    ++dmxDisplaysFromCommandLine;
}

/** Make a note that \a input is the name of an X11 display that should
 * be used for input (either a backend or a console input device). */
void
dmxConfigStoreInput(const char *input)
{
    DMXConfigListPtr entry = malloc(sizeof(*entry));

    entry->name = strdup(input);
    entry->next = NULL;
    if (!dmxConfigCmd.inputs)
        dmxConfigCmd.inputs = entry;
    else {
        DMXConfigList *pt;

        for (pt = dmxConfigCmd.inputs; pt->next; pt = pt->next);
        if (!pt)
            dmxLog(dmxFatal, "dmxConfigStoreInput: end of list non-NULL\n");
        pt->next = entry;
    }
}

/** Make a note that \a input is the name of an X11 display that should
 * be used for input from XInput extension devices. */
void
dmxConfigStoreXInput(const char *input)
{
    DMXConfigListPtr entry = malloc(sizeof(*entry));

    entry->name = strdup(input);
    entry->next = NULL;
    if (!dmxConfigCmd.xinputs)
        dmxConfigCmd.xinputs = entry;
    else {
        DMXConfigList *pt;

        for (pt = dmxConfigCmd.xinputs; pt->next; pt = pt->next);
        if (!pt)
            dmxLog(dmxFatal, "dmxConfigStoreXInput: end of list non-NULL\n");
        pt->next = entry;
    }
}

/** Make a note that \a file is the configuration file. */
void
dmxConfigStoreFile(const char *file)
{
    if (dmxConfigCmd.filename)
        dmxLog(dmxFatal, "Only one -configfile allowed\n");
    dmxConfigCmd.filename = strdup(file);
}

/** Make a note that \a config should be used as the configuration for
 * current instantiation of the DMX server. */
void
dmxConfigStoreConfig(const char *config)
{
    if (dmxConfigCmd.config)
        dmxLog(dmxFatal, "Only one -config allowed\n");
    dmxConfigCmd.config = strdup(config);
}

static int
dmxConfigReadFile(const char *filename, int debug)
{
    FILE *str;

    if (!(str = fopen(filename, "r")))
        return -1;
    dmxLog(dmxInfo, "Reading configuration file \"%s\"\n", filename);
    yyin = str;
    yydebug = debug;
    yyparse();
    fclose(str);
    return 0;
}

static const char *
dmxConfigMatch(const char *target, DMXConfigEntryPtr entry)
{
    DMXConfigVirtualPtr v = entry->virtual;
    const char *name = NULL;

    if (v && v->name)
        name = v->name;

    if (v && !dmxConfigCmd.config)
        return v->name ? v->name : "<noname>";
    if (!name)
        return NULL;
    if (!strcmp(name, target))
        return name;
    return NULL;
}

static DMXScreenInfo *
dmxConfigAddDisplay(const char *name,
                    int scrnWidth, int scrnHeight,
                    int scrnX, int scrnY,
                    int scrnXSign, int scrnYSign,
                    int rootWidth, int rootHeight,
                    int rootX, int rootY, int rootXSign, int rootYSign)
{
    DMXScreenInfo *dmxScreen;

    if (!(dmxScreens = realloc(dmxScreens,
                               (dmxNumScreens + 1) * sizeof(*dmxScreens))))
        dmxLog(dmxFatal,
               "dmxConfigAddDisplay: realloc failed for screen %d (%s)\n",
               dmxNumScreens, name);

    dmxScreen = &dmxScreens[dmxNumScreens];
    memset(dmxScreen, 0, sizeof(*dmxScreen));
    dmxScreen->name = name;
    dmxScreen->index = dmxNumScreens;
    dmxScreen->scrnWidth = scrnWidth;
    dmxScreen->scrnHeight = scrnHeight;
    dmxScreen->scrnX = scrnX;
    dmxScreen->scrnY = scrnY;
    dmxScreen->scrnXSign = scrnXSign;
    dmxScreen->scrnYSign = scrnYSign;
    dmxScreen->rootWidth = rootWidth;
    dmxScreen->rootHeight = rootHeight;
    dmxScreen->rootX = rootX;
    dmxScreen->rootY = rootY;
    dmxScreen->stat = dmxStatAlloc();
    ++dmxNumScreens;
    return dmxScreen;
}

DMXInputInfo *
dmxConfigAddInput(const char *name, int core)
{
    DMXInputInfo *dmxInput;

    if (!(dmxInputs = realloc(dmxInputs,
                              (dmxNumInputs + 1) * sizeof(*dmxInputs))))
        dmxLog(dmxFatal,
               "dmxConfigAddInput: realloc failed for input %d (%s)\n",
               dmxNumInputs, name);

    dmxInput = &dmxInputs[dmxNumInputs];

    memset(dmxInput, 0, sizeof(*dmxInput));
    dmxInput->name = name;
    dmxInput->inputIdx = dmxNumInputs;
    dmxInput->scrnIdx = -1;
    dmxInput->core = core;
    ++dmxNumInputs;
    return dmxInput;
}

static void
dmxConfigCopyFromDisplay(DMXConfigDisplayPtr d)
{
    DMXScreenInfo *dmxScreen;

    dmxScreen = dmxConfigAddDisplay(d->name,
                                    d->scrnWidth, d->scrnHeight,
                                    d->scrnX, d->scrnY,
                                    d->scrnXSign, d->scrnYSign,
                                    d->rootWidth, d->rootHeight,
                                    d->rootX, d->rootY,
                                    d->rootXSign, d->rootXSign);
    dmxScreen->where = PosAbsolute;
    dmxScreen->whereX = d->rootXOrigin;
    dmxScreen->whereY = d->rootYOrigin;
}

static void
dmxConfigCopyFromWall(DMXConfigWallPtr w)
{
    DMXConfigStringPtr pt;
    DMXScreenInfo *dmxScreen;
    int edge = dmxNumScreens;
    int last = dmxNumScreens;

    if (!w->xwall && !w->ywall) {       /* Try to make it square */
        int count;

        for (pt = w->nameList, count = 0; pt; pt = pt->next)
            ++count;
        w->xwall = sqrt(count) + .5;
    }

    for (pt = w->nameList; pt; pt = pt->next) {
        dmxScreen = dmxConfigAddDisplay(pt->string, w->width, w->height,
                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        if (pt == w->nameList) {        /* Upper left */
            dmxScreen->where = PosAbsolute;
            dmxScreen->whereX = 0;
            dmxScreen->whereY = 0;
        }
        else if (w->xwall) {    /* Tile left to right, then top to bottom */
            if (!((dmxNumScreens - 1) % w->xwall)) {
                dmxScreen->where = PosBelow;
                dmxScreen->whereRefScreen = edge;
                edge = dmxNumScreens - 1;
            }
            else {
                dmxScreen->where = PosRightOf;
                dmxScreen->whereRefScreen = last;
            }
        }
        else {                  /* Tile top to bottom, then left to right */
            if (!((dmxNumScreens - 1) % w->ywall)) {
                dmxScreen->where = PosRightOf;
                dmxScreen->whereRefScreen = edge;
                edge = dmxNumScreens - 1;
            }
            else {
                dmxScreen->where = PosBelow;
                dmxScreen->whereRefScreen = last;
            }

        }
        last = dmxNumScreens - 1;
        if (dmxScreen->where == PosAbsolute)
            dmxLog(dmxInfo, "Added %s at %d %d\n",
                   pt->string, dmxScreen->whereX, dmxScreen->whereY);
        else
            dmxLog(dmxInfo, "Added %s %s %s\n",
                   pt->string,
                   dmxScreen->where == PosBelow ? "below" : "right of",
                   dmxScreens[dmxScreen->whereRefScreen].name);
    }
}

static void
dmxConfigCopyFromOption(DMXConfigOptionPtr o)
{
    DMXConfigStringPtr pt;
    int argc = 0;
    char **argv = NULL;

    if (serverGeneration != 1)
        return;                 /* FIXME: only do once, for now */
    if (!o || !o->string)
        return;
    for (pt = o->option; pt; pt = pt->next) {
        if (pt->string) {
            ++argc;
            argv = realloc(argv, (argc + 1) * sizeof(*argv));
            argv[argc] = (char *) pt->string;
        }
    }
    argv[0] = NULL;
    ProcessCommandLine(argc + 1, argv);
    free(argv);
}

static void
dmxConfigCopyFromParam(DMXConfigParamPtr p)
{
    const char **argv;
    int argc;

    if ((argv = dmxConfigLookupParam(p, "xkbrules", &argc)) && argc == 2) {
        dmxConfigSetXkbRules(argv[1]);
    }
    else if ((argv = dmxConfigLookupParam(p, "xkbmodel", &argc))
             && argc == 2) {
        dmxConfigSetXkbModel(argv[1]);
    }
    else if ((argv = dmxConfigLookupParam(p, "xkblayout", &argc))
             && argc == 2) {
        dmxConfigSetXkbLayout(argv[1]);
    }
    else if ((argv = dmxConfigLookupParam(p, "xkbvariant", &argc))
             && argc == 2) {
        dmxConfigSetXkbVariant(argv[1]);
    }
    else if ((argv = dmxConfigLookupParam(p, "xkboptions", &argc))
             && argc == 2) {
        dmxConfigSetXkbOptions(argv[1]);
    }
}

static void
dmxConfigCopyData(DMXConfigVirtualPtr v)
{
    DMXConfigSubPtr sub;

    if (v->dim)
        dmxSetWidthHeight(v->dim->x, v->dim->y);
    else
        dmxSetWidthHeight(0, 0);
    for (sub = v->subentry; sub; sub = sub->next) {
        switch (sub->type) {
        case dmxConfigDisplay:
            dmxConfigCopyFromDisplay(sub->display);
            break;
        case dmxConfigWall:
            dmxConfigCopyFromWall(sub->wall);
            break;
        case dmxConfigOption:
            dmxConfigCopyFromOption(sub->option);
            break;
        case dmxConfigParam:
            dmxConfigCopyFromParam(sub->param);
            break;
        default:
            dmxLog(dmxFatal,
                   "dmxConfigCopyData: not a display, wall, or value\n");
        }
    }
}

static void
dmxConfigFromCommandLine(void)
{
    DMXConfigListPtr pt;

    dmxLog(dmxInfo, "Using configuration from command line\n");
    for (pt = dmxConfigCmd.displays; pt; pt = pt->next) {
        DMXScreenInfo *dmxScreen = dmxConfigAddDisplay(pt->name,
                                                       0, 0, 0, 0, 0, 0,
                                                       0, 0, 0, 0, 0, 0);

        if (dmxNumScreens == 1) {
            dmxScreen->where = PosAbsolute;
            dmxScreen->whereX = 0;
            dmxScreen->whereY = 0;
            dmxLog(dmxInfo, "Added %s at %d %d\n",
                   dmxScreen->name, dmxScreen->whereX, dmxScreen->whereY);
        }
        else {
            dmxScreen->where = PosRightOf;
            dmxScreen->whereRefScreen = dmxNumScreens - 2;
            if (dmxScreen->whereRefScreen < 0)
                dmxScreen->whereRefScreen = 0;
            dmxLog(dmxInfo, "Added %s %s %s\n",
                   dmxScreen->name,
                   dmxScreen->where == PosBelow ? "below" : "right of",
                   dmxScreens[dmxScreen->whereRefScreen].name);
        }
    }
}

static void
dmxConfigFromConfigFile(void)
{
    DMXConfigEntryPtr pt;
    const char *name;

    for (pt = dmxConfigEntry; pt; pt = pt->next) {
        /* FIXME -- if an input is specified, use it */
        if (pt->type != dmxConfigVirtual)
            continue;
        if ((name = dmxConfigMatch(dmxConfigCmd.config, pt))) {
            dmxLog(dmxInfo, "Using configuration \"%s\"\n", name);
            dmxConfigCopyData(pt->virtual);
            return;
        }
    }
    dmxLog(dmxFatal, "Could not find configuration \"%s\" in \"%s\"\n",
           dmxConfigCmd.config, dmxConfigCmd.filename);
}

static void
dmxConfigConfigInputs(void)
{
    DMXConfigListPtr pt;

    if (dmxNumInputs)
        return;

    if (dmxConfigCmd.inputs) {  /* Use command line */
        for (pt = dmxConfigCmd.inputs; pt; pt = pt->next)
            dmxConfigAddInput(pt->name, TRUE);
    }
    else if (dmxNumScreens) {   /* Use first display */
        dmxConfigAddInput(dmxScreens[0].name, TRUE);
    }
    else {                      /* Use dummy */
        dmxConfigAddInput("dummy", TRUE);
    }

    if (dmxConfigCmd.xinputs) { /* Non-core devices from command line */
        for (pt = dmxConfigCmd.xinputs; pt; pt = pt->next)
            dmxConfigAddInput(pt->name, FALSE);
    }
}

/** Set up the appropriate global variables so that the DMX server will
 * be initialized using the configuration specified in the config file
 * and on the command line. */
void
dmxConfigConfigure(void)
{
    if (dmxConfigEntry) {
        dmxConfigFreeEntry(dmxConfigEntry);
        dmxConfigEntry = NULL;
    }
    if (dmxConfigCmd.filename) {
        if (dmxConfigCmd.displays)
            dmxLog(dmxWarning,
                   "Using configuration file \"%s\" instead of command line\n",
                   dmxConfigCmd.filename);
        dmxConfigReadFile(dmxConfigCmd.filename, 0);
        dmxConfigFromConfigFile();
    }
    else {
        if (dmxConfigCmd.config)
            dmxLog(dmxWarning,
                   "Configuration name (%s) without configuration file\n",
                   dmxConfigCmd.config);
        dmxConfigFromCommandLine();
    }
    dmxConfigConfigInputs();
}

/** This function determines the number of displays we WILL have and
 * sets MAXSCREENS to that value.  This is difficult since the number
 * depends on the command line (which is easy to count) or on the config
 * file, which has to be parsed. */
void
dmxConfigSetMaxScreens(void)
{
    static int processing = 0;

    if (processing)
        return;                 /* Prevent reentry via ProcessCommandLine */
    processing = 1;
    if (dmxConfigCmd.filename) {
        if (!dmxNumScreens)
            dmxConfigConfigure();
#ifndef MAXSCREENS
        SetMaxScreens(dmxNumScreens);
#endif
    }
    else
#ifndef MAXSCREENS
        SetMaxScreens(dmxDisplaysFromCommandLine);
#endif
    processing = 0;
}

/** This macro is used to generate the following access methods:
 * - dmxConfig{Set,Get}rules
 * - dmxConfig{Set,Get}model
 * - dmxConfig{Set,Get}layout
 * - dmxConfig{Set,Get}variant
 * - dmxConfig{Set,Get}options
 * These methods are used to read and write information about the keyboard. */

#define GEN(param,glob,def)                                                   \
 void dmxConfigSet##glob(const char *param) {                                 \
     if (dmx##glob) free((void *)dmx##glob);                                  \
     dmx##glob = strdup(param);                                               \
 }                                                                            \
 char *dmxConfigGet##glob(void) {                                             \
     return (char *)(dmx##glob ? dmx##glob : def);                            \
 }

GEN(rules, XkbRules, XKB_DFLT_RULES)
    GEN(model, XkbModel, XKB_DFLT_MODEL)
    GEN(layout, XkbLayout, XKB_DFLT_LAYOUT)
    GEN(variant, XkbVariant, XKB_DFLT_VARIANT)
    GEN(options, XkbOptions, XKB_DFLT_OPTIONS)