summaryrefslogtreecommitdiff
path: root/cache/cache.c
blob: 42aea5a7d393e85a09f0d14ba24dbcf957774b45 (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
#include <xkbcommon/xkbcommon.h>
#include <stdio.h>
#include <string.h>

#define DFLT_RULES "evdev"
#define DFLT_MODEL "pc105"
#define DFLT_LAYOUT "us"

#define STRLEN(s) (s ? strlen(s) : 0)
#define STR(s) (s ? s : "")

void parseArgs(int argc, char **argv, struct xkb_rule_names *names)
{
    int i;
    char *tmp, *rule_path;
    FILE *file;
    int len_rule_path;
    char buf[1024] = {0, };

    if (argc < 2)
    {
        rule_path = getenv("RULE_FILE_PATH");

	if (!rule_path)
        {
            printf("Failed to get RULE_FILE_PATH !\n");
            return;
        }

        printf("Cache file rule from %s file\n", rule_path);

        file = fopen(rule_path, "r");
        if (!file) return;

        while (!feof(file))
        {
            fscanf(file, "%s", buf);
            if (strstr(buf, "rules") > 0)
            {
                tmp = strtok(buf, "=");
                tmp = strtok(NULL, "=");
                if (tmp) names->rules= strdup(tmp);
            }
            else if (strstr(buf, "model") > 0)
            {
                tmp = strtok(buf, "=");
                tmp = strtok(NULL, "=");
                if (tmp) names->model= strdup(tmp);
            }
            else if (strstr(buf, "layout") > 0)
            {
                tmp = strtok(buf, "=");
                tmp = strtok(NULL, "=");
                if (tmp) names->layout= strdup(tmp);
            }
            else if (strstr(buf, "variant") > 0)
            {
                tmp = strtok(buf, "=");
                tmp = strtok(NULL, "=");
                if (tmp) names->variant= strdup(tmp);
            }
            else if (strstr(buf, "options") > 0)
            {
                tmp = strtok(buf, "=");
                tmp = strtok(NULL, "=");
                if (tmp) names->options= strdup(tmp);
            }
        }

        fclose(file);
    }
    else
    {
        for (i = 1; i < argc; i++)
        {
            printf("Cache file rule from argument\n");

            if (strstr(argv[i], "-rules") > 0)
            {
                tmp = strtok(argv[i], "=");
                tmp = strtok(NULL, "=");
                names->rules= strdup(tmp);
            }
            else if (strstr(argv[i], "-model") > 0)
            {
                tmp = strtok(argv[i], "=");
                tmp = strtok(NULL, "=");
                names->model = strdup(tmp);
            }
            else if (strstr(argv[i], "-layout") > 0)
            {
                tmp = strtok(argv[i], "=");
                tmp = strtok(NULL, "=");
                names->layout = strdup(tmp);
            }
            else if (strstr(argv[i], "-variant") > 0)
            {
                tmp = strtok(argv[i], "=");
                tmp = strtok(NULL, "=");
                names->variant = strdup(tmp);
            }
            else if (strstr(argv[i], "-options") > 0)
            {
                tmp = strtok(argv[i], "=");
                tmp = strtok(NULL, "=");
                names->options = strdup(tmp);
            }
        }
    }
}

void checkRules(struct xkb_rule_names *names)
{
   if (!names->rules)
   {
      printf("Set default rules: %s\n", DFLT_RULES);
      names->rules = strdup(DFLT_RULES);
   }
   else printf("Current rules: %s\n", names->rules);

   if (!names->model)
   {
      printf("Set default model: %s\n", DFLT_MODEL);
      names->model = strdup(DFLT_MODEL);
   }
   else printf("Current model: %s\n", names->model);

   if (!names->layout)
   {
      printf("Set default layout: %s\n", DFLT_LAYOUT);
      names->layout = strdup(DFLT_LAYOUT);
   }
   else printf("Current layout: %s\n", names->layout);

   if (!names->variant) printf("There is no variant\n");
   else printf("Current variant: %s\n", names->variant);

   if (!names->options) printf("There is no options\n");
   else printf("Current options: %s\n", names->options);
}

int main(int argc, char **argv)
{
    struct xkb_context *ctx;
    struct xkb_keymap *map;
    struct xkb_rule_names names;
    char *keymap_path = NULL;
    char *keymap_string = NULL;
    char *cache_path = NULL;
    FILE *file = NULL;
    int len_cache_path;
    
    memset(&names, 0, sizeof(names));

    parseArgs(argc, argv, &names);

    checkRules(&names);

    ctx = xkb_context_new(0);
    if (!ctx) {
       printf("Failed to generate a xkb context file\n");
       return 0;
    }

    keymap_path = getenv("LOCAL_KEYMAP_PATH");

    if (!keymap_path)
    {
        printf("Failed to get LOCAL_KEYMAP_PATH !\n");
        return 0;
    }

    xkb_context_include_path_append(ctx, keymap_path);

    map = xkb_map_new_from_names(ctx, &names, 0);

    keymap_string = xkb_map_get_as_string(map);

    if (!keymap_string) {
        printf("Failed convert keymap to string\n");
        return 0;
    }

    len_cache_path = STRLEN(names.rules) + STRLEN(names.model) + STRLEN(names.layout) + STRLEN(names.variant) + STRLEN(names.options) + sizeof("xkb") + 5;
    cache_path = (char *)calloc(1, len_cache_path);
    snprintf(cache_path, len_cache_path, "%s-%s-%s-%s-%s.xkb", STR(names.rules), STR(names.model), STR(names.layout), STR(names.variant), STR(names.options));

    file = fopen(cache_path, "w");
    if (fputs(keymap_string, file) < 0)
    {
        printf("Failed  to write keymap file: %s\n", cache_path);
        fclose(file);
        unlink(cache_path);
    }
  else
    {
        printf("Success to make keymap file: %s\n", cache_path);
        fclose(file);
    }

    if (names.rules) free(names.rules);
    if (names.model) free(names.model);
    if (names.layout) free(names.layout);
    if (names.variant) free(names.variant);
    if (names.options) free(names.options);
    if (cache_path) free(cache_path);
    xkb_keymap_unref(map);
    xkb_context_unref(ctx);

    return 0;
}