source: trunk/tools/neercs/old/configuration.c @ 1656

Last change on this file since 1656 was 1656, checked in by sam, 7 years ago

neercs: start porting the old code to Windows; this breaks the Visual Studio
build until further fixes are made.

  • Property svn:keywords set to Id
File size: 14.1 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
4 *                2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  This program is free software. It comes without any warranty, to
8 *  the extent permitted by applicable law. You can redistribute it
9 *  and/or modify it under the terms of the Do What The Fuck You Want
10 *  To Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#if defined HAVE_CONFIG_H
15#   include "config.h"
16#endif
17
18#if !defined _WIN32
19
20#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <unistd.h>
26#include <errno.h>
27#include <string.h>
28
29#include "neercs.h"
30
31
32struct config_line *get_config(const char *name);
33int set_window_manager(const char *argv, struct screen_list *screen_list);
34int set_cube_duration(const char *argv, struct screen_list *screen_list);
35int set_thumbnails(const char *argv, struct screen_list *screen_list);
36int set_status_bar(const char *argv, struct screen_list *screen_list);
37int set_screensaver_timeout(const char *argv, struct screen_list *screen_list);
38int set_autolock_timeout(const char *argv, struct screen_list *screen_list);
39int set_lock_on_detach(const char *argv, struct screen_list *screen_list);
40int set_socket_dir(const char *argv, struct screen_list *screen_list);
41int set_delay(const char *argv, struct screen_list *screen_list);
42int set_eyecandy(const char *argv, struct screen_list *screen_list);
43int set_border(const char *argv, struct screen_list *screen_list);
44char *get_window_manager(struct screen_list *screen_list);
45char *get_cube_duration(struct screen_list *screen_list);
46char *get_thumbnails(struct screen_list *screen_list);
47char *get_status_bar(struct screen_list *screen_list);
48char *get_screensaver_timeout(struct screen_list *screen_list);
49char *get_autolock_timeout(struct screen_list *screen_list);
50char *get_lock_on_detach(struct screen_list *screen_list);
51char *get_socket_dir(struct screen_list *screen_list);
52char *get_delay(struct screen_list *screen_list);
53char *get_eyecandy(struct screen_list *screen_list);
54char *get_border(struct screen_list *screen_list);
55
56/* Options definition and associated function pointer */
57struct config_line config_option[] = {
58    {.name = "window_manager",.set = set_window_manager,.get =
59     get_window_manager},
60    {.name = "eyecandy",.set = set_eyecandy,.get = get_eyecandy},
61    {.name = "borders",.set = set_border,.get = get_border},
62    {.name = "cube_duration",.set = set_cube_duration,.get =
63     get_cube_duration},
64    {.name = "thumbnails",.set = set_thumbnails,.get = get_thumbnails},
65    {.name = "status_bar",.set = set_status_bar,.get = get_status_bar},
66    {.name = "screensaver_timeout",.set = set_screensaver_timeout,.get =
67     get_screensaver_timeout},
68    {.name = "autolock_timeout",.set = set_autolock_timeout,.get =
69     get_autolock_timeout},
70    {.name = "lock_on_detach",.set = set_lock_on_detach,.get =
71     get_lock_on_detach},
72    {.name = "socket_dir",.set = set_socket_dir,.get = get_socket_dir},
73    {.name = "delay",.set = set_delay,.get = get_delay},
74
75    {.name = "last",.set = NULL},
76};
77
78
79
80int read_configuration_file(char *filename, struct screen_list *screen_list)
81{
82    FILE *fp;
83    struct stat st;
84    int size = 0, i = 0, total = 0, offset = 0, l = 1;
85    char *buffer = NULL;
86
87    screen_list->config = NULL;
88
89    /* Check if file exist */
90    if (stat(filename, &st) < 0)
91    {
92        return -1;
93    }
94    /* Get its size */
95    size = st.st_size;
96    if (!size)
97    {
98        fprintf(stderr, "File too short\n");
99        return -1;
100    }
101
102    /* Open it */
103    fp = fopen(filename, "r");
104    if (!fp)
105    {
106        return -1;
107    }
108
109    buffer = malloc(size + 1);
110    if (!buffer)
111    {
112        fclose(fp);
113        fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__,
114                __LINE__);
115        return -1;
116    }
117    /* Read it */
118    while ((i = fread(buffer + total, 1, size, fp)) > 0)
119    {
120        total += i;
121    }
122    buffer[total] = '\n';
123
124    fclose(fp);
125
126    /* Parse it */
127    while ((i =
128            parse_conf_line(buffer + offset, total - offset, screen_list)) > 0)
129    {
130        offset += i;
131        l++;
132    }
133
134    free(buffer);
135
136    /* Fill neercs configuration with it */
137    fill_config(screen_list);
138
139    return 1;
140}
141
142struct config_line *get_config_option(void)
143{
144    return config_option;
145}
146
147int parse_conf_line(char *buf, int size, struct screen_list *screen_list)
148{
149    int i, s = 0, c = 0;
150    char *line = NULL;
151    int l = 0;
152    int in_quote = 0, end_spaces = 0;
153    static struct option *prev = NULL;
154
155    if (size <= 0)
156        return -1;
157
158    /* Find EOL */
159    for (i = 0; i < size; i++)
160    {
161        if (buf[i] == '\n')
162        {
163            s = i + 1;
164            break;
165        }
166    }
167
168    /* Strip comments and trailing spaces */
169    for (i = 0; i < s; i++)
170    {
171        if (buf[i] == ';' && !in_quote)
172        {
173            break;
174        }
175        else if (buf[i] == '\n')
176        {
177            break;
178        }
179        else if (buf[i] == ' ' && !c)
180        {
181        }
182        else
183        {
184            if (line == NULL)
185            {
186                line = malloc(2);
187                if (!line)
188                {
189                    fprintf(stderr, "Can't allocate memory at %s:%d\n",
190                            __FUNCTION__, __LINE__);
191                    return -1;
192                }
193            }
194            else
195            {
196                line = realloc(line, l + 2);
197                if (!line)
198                {
199                    fprintf(stderr, "Can't allocate memory at %s:%d\n",
200                            __FUNCTION__, __LINE__);
201                    return -1;
202                }
203            }
204            if (buf[i] == '"')
205                in_quote = !in_quote;
206            if (buf[i] == ' ')
207                end_spaces++;
208            else
209                end_spaces = 0;
210
211            line[l] = buf[i];
212            line[l + 1] = 0;
213            l++;
214            c = 1;
215        }
216    }
217
218    if (c == 0)
219    {
220        /* This line is empty, do nothing */
221    }
222    else
223    {
224        struct option *option = malloc(sizeof(struct option));
225        if (!option)
226        {
227            fprintf(stderr, "Can't allocate memory at %s:%d\n",
228                    __FUNCTION__, __LINE__);
229            return -1;
230        }
231        option->next = NULL;
232        l -= end_spaces;
233        line = realloc(line, l + 1);
234        line[l] = 0;
235
236        get_key_value(line, option);
237
238        if (!screen_list->config)
239            screen_list->config = option;
240
241        if (prev)
242            prev->next = option;
243
244        prev = option;
245    }
246    free(line);
247    return s;
248}
249
250int get_key_value(char *line, struct option *option)
251{
252    unsigned int i, o = 0, b = 0, end_spaces = 0;
253    char *cur = NULL;
254    option->value = NULL;
255    option->key = NULL;
256
257    /* Line is a section delimiter */
258    if (line[0] == '[')
259    {
260        option->value = malloc(strlen(line) - 1);
261        if (!option->value)
262        {
263            fprintf(stderr, "Can't allocate memory at %s:%d\n",
264                    __FUNCTION__, __LINE__);
265            return -1;
266        }
267        memcpy(option->value, line + 1, strlen(line) - 1);
268        option->value[strlen(line) - 2] = 0;
269        return 0;
270    }
271
272    cur = malloc(1);
273    if (!cur)
274    {
275        fprintf(stderr, "Can't allocate memory at %s:%d\n",
276                __FUNCTION__, __LINE__);
277        return -1;
278    }
279    cur[0] = 0;
280
281    for (i = 0; i < strlen(line); i++)
282    {
283        if (line[i] == ' ' && !b)
284            continue;
285
286
287        if (line[i] == '=')
288        {
289            b = 0;
290            cur[o - end_spaces] = 0;
291            cur = realloc(cur, (o - end_spaces) + 1);
292            if (!cur)
293            {
294                fprintf(stderr, "Can't allocate memory at %s:%d\n",
295                        __FUNCTION__, __LINE__);
296                return -1;
297            }
298            o = 0;
299            option->key = cur;
300            cur = malloc(1);
301        }
302        else
303        {
304            if (line[i] == ' ')
305                end_spaces++;
306            else
307                end_spaces = 0;
308
309            cur = realloc(cur, o + 2);
310            if (!cur)
311            {
312                fprintf(stderr, "Can't allocate memory at %s:%d\n",
313                        __FUNCTION__, __LINE__);
314                return -1;
315            }
316            cur[o] = line[i];
317            o++;
318            b = 1;
319
320        }
321    }
322    cur[o] = 0;
323    option->value = cur;
324    return 0;
325}
326
327
328
329struct config_line *get_config(const char *name)
330{
331    int i = 0;
332
333    debug("Looking for '%s'\n", name);
334
335    while (strncmp(config_option[i].name, "last", strlen("last")))
336    {
337        debug("%d Testing against '%s'\n", i, config_option[i].name);
338        if (!strncmp(name, config_option[i].name, strlen(name)))
339        {
340            debug("Found\n");
341            return &config_option[i];
342        }
343        i++;
344    }
345    return NULL;
346}
347
348
349
350int fill_config(struct screen_list *screen_list)
351{
352    int i = 0;
353    struct option *option = screen_list->config;
354
355    while (option)
356    {
357        if (option->key == NULL)
358        {
359            option = option->next;
360            continue;
361        }
362
363        struct config_line *c = get_config(option->key);
364        if (c)
365        {
366            c->set((const char *)option->value, screen_list);
367        }
368        option = option->next;
369    }
370
371    return i;
372}
373
374
375
376/*
377 * Options setters
378 */
379
380#define IS_OPTION(t) (!strncmp(argv, t, strlen(argv)))
381#define IS_OPTION_TRUE (IS_OPTION("true") || IS_OPTION("True") || IS_OPTION("1"))
382
383int set_window_manager(const char *argv, struct screen_list *screen_list)
384{
385    if (IS_OPTION("full"))
386        screen_list->wm_type = WM_FULL;
387    else if (IS_OPTION("hsplit"))
388        screen_list->wm_type = WM_HSPLIT;
389    else if (IS_OPTION("vsplit"))
390        screen_list->wm_type = WM_VSPLIT;
391    else if (IS_OPTION("card"))
392        screen_list->wm_type = WM_CARD;
393    else
394    {
395        fprintf(stderr, "Unknown window manager '%s'\n", argv);
396        return -1;
397    }
398    return 0;
399}
400
401int set_cube_duration(const char *argv, struct screen_list *screen_list)
402{
403    screen_list->cube.duration = atoi(argv) * 1000000;
404    return 0;
405}
406
407int set_thumbnails(const char *argv, struct screen_list *screen_list)
408{
409    if (IS_OPTION_TRUE)
410        screen_list->modals.mini = 1;
411    else
412        screen_list->modals.mini = 0;
413    return 0;
414
415}
416
417int set_status_bar(const char *argv, struct screen_list *screen_list)
418{
419    if (IS_OPTION_TRUE)
420        screen_list->modals.status = 1;
421    else
422        screen_list->modals.status = 0;
423    return 0;
424}
425
426int set_screensaver_timeout(const char *argv, struct screen_list *screen_list)
427{
428    screen_list->screensaver.timeout = atoi(argv) * 1000000;
429    /* if timeout is 0, set it to 0xFFFFFFFFFFFFFFFF */
430    if (!screen_list->screensaver.timeout)
431        screen_list->screensaver.timeout -= 1;
432    return 0;
433}
434
435int set_autolock_timeout(const char *argv, struct screen_list *screen_list)
436{
437    if (screen_list->lock.autolock_timeout == 0 ||
438        screen_list->lock.autolock_timeout == ((long long unsigned int)0) - 1)
439    {
440        screen_list->lock.autolock_timeout = atoi(argv) * 1000000;
441        /* if timeout is 0, set it to 0xFFFFFFFFFFFFFFFF */
442        if (!screen_list->lock.autolock_timeout)
443            screen_list->lock.autolock_timeout -= 1;
444    }
445    return 0;
446}
447
448int set_lock_on_detach(const char *argv, struct screen_list *screen_list)
449{
450    if (IS_OPTION_TRUE)
451        screen_list->lock.lock_on_detach = 1;
452    else
453        screen_list->lock.lock_on_detach = 0;
454    return 0;
455}
456
457int set_eyecandy(const char *argv, struct screen_list *screen_list)
458{
459    if (IS_OPTION_TRUE)
460        screen_list->eyecandy = 1;
461    else
462        screen_list->eyecandy = 0;
463    return 0;
464}
465
466int set_border(const char *argv, struct screen_list *screen_list)
467{
468    if (IS_OPTION_TRUE)
469        screen_list->border_size = 1;
470    else
471        screen_list->border_size = 0;
472    return 0;
473}
474
475int set_socket_dir(const char *argv, struct screen_list *screen_list)
476{
477    screen_list->comm.socket_dir = strdup(argv);
478    return 0;
479}
480
481int set_delay(const char *argv, struct screen_list *screen_list)
482{
483    screen_list->requested_delay = atoi(argv);
484    screen_list->delay = atoi(argv);
485    return 0;
486}
487
488char *get_window_manager(struct screen_list *screen_list)
489{
490    debug("Window manager is %d", screen_list->wm_type);
491    switch (screen_list->wm_type)
492    {
493    case WM_FULL:
494        return "full";
495    case WM_CARD:
496        return "card";
497    case WM_VSPLIT:
498        return "vsplit";
499    case WM_HSPLIT:
500        return "hsplit";
501    default:
502        return "invalid window manager";
503    }
504    return NULL;                /* Not reached */
505}
506
507char *get_cube_duration(struct screen_list *screen_list)
508{
509    char *r = malloc(100);
510    sprintf(r, "%f", (float)screen_list->cube.duration / 1000000.0f);
511    return r;
512}
513
514char *get_thumbnails(struct screen_list *screen_list)
515{
516    if (screen_list->modals.mini)
517        return "true";
518    return "false";
519}
520
521char *get_status_bar(struct screen_list *screen_list)
522{
523    if (screen_list->modals.status)
524        return "true";
525    return "false";
526}
527
528char *get_eyecandy(struct screen_list *screen_list)
529{
530    if (screen_list->eyecandy)
531        return "true";
532    return "false";
533}
534
535char *get_border(struct screen_list *screen_list)
536{
537    if (screen_list->border_size)
538        return "true";
539    return "false";
540}
541
542char *get_screensaver_timeout(struct screen_list *screen_list)
543{
544    char *r = malloc(100);
545    sprintf(r, "%f", (float)screen_list->screensaver.timeout / 1000000.0f);
546    return r;
547}
548
549char *get_autolock_timeout(struct screen_list *screen_list)
550{
551    char *r = malloc(100);
552    sprintf(r, "%f", (float)screen_list->lock.autolock_timeout / 1000000.0f);
553    return r;
554}
555
556char *get_lock_on_detach(struct screen_list *screen_list)
557{
558    if (screen_list->lock.lock_on_detach)
559        return "true";
560    else
561        return "false";
562}
563
564char *get_socket_dir(struct screen_list *screen_list)
565{
566    return screen_list->comm.socket_dir;
567}
568
569char *get_delay(struct screen_list *screen_list)
570{
571    char *r = malloc(100);
572    sprintf(r, "%d", screen_list->requested_delay);
573    return r;
574}
575
576#endif
577
Note: See TracBrowser for help on using the repository browser.