summaryrefslogtreecommitdiff
path: root/src/core/schema-bindings.c
blob: d7cf7b6b4c783ebe45366a071a6663f3ac799281 (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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

/*
 * Copyright (C) 2008 Thomas Thurman
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/** \file  Schema bindings generator.
 *
 * This program simply takes the items given in the binding list in
 * all-keybindings.h and turns them into a portion of
 * the MateConf .schemas file.
 *
 * FIXME: also need to make 50-marco-desktop-key.xml
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <glib.h>
#include "config.h"

#define _(x) x

static void single_stanza (gboolean is_window, const char *name,
               const char *default_value,
               gboolean can_reverse,
               const char *description);

char *about_keybindings, *about_reversible_keybindings;

char *source_filename, *target_filename;
FILE *source_file, *target_file;

static void
single_stanza (gboolean is_window, const char *name,
               const char *default_value,
               gboolean can_reverse,
               const char *description)
{
  char *keybinding_type = is_window? "window": "global";
  char *escaped_default_value, *escaped_description;

  if (description==NULL)
    return; /* it must be undocumented, so it can't go in this table */

  escaped_description = g_markup_escape_text (description, -1);
  escaped_default_value = default_value==NULL? "disabled":
        g_markup_escape_text (default_value, -1);
  
  fprintf (target_file, "    <schema>\n");
  fprintf (target_file, "      <key>/schemas/apps/marco/%s_keybindings/%s</key>\n",
            keybinding_type, name);
  fprintf (target_file, "      <applyto>/apps/marco/%s_keybindings/%s</applyto>\n",
            keybinding_type, name);
  fprintf (target_file, "      <owner>marco</owner>\n");
  fprintf (target_file, "      <type>string</type>\n");
  fprintf (target_file, "      <default>%s</default>\n", escaped_default_value);
  
  fprintf (target_file, "      <locale name=\"C\">\n");
  fprintf (target_file, "        <short>%s</short>\n", description);
  fprintf (target_file, "        <long>%s</long>\n",
                   can_reverse? about_reversible_keybindings:
                   about_keybindings);
  fprintf (target_file, "      </locale>\n");
  fprintf (target_file, "    </schema>\n\n");

  g_free (escaped_description);

  if (default_value!=NULL)
    g_free (escaped_default_value);
}

static void produce_bindings ();

static void
produce_bindings ()
{
  /* 10240 is ridiculous overkill; we're writing the input file and
   * the lines are always 80 chars or less.
   */
  char buffer[10240];

  source_file = fopen(source_filename, "r");

  if (!source_file)
    {
      g_error ("Cannot compile without %s: %s\n",
        source_filename, strerror (errno));
    }

  target_file = fopen(target_filename, "w");

  if (!target_file)
    {
      g_error ("Cannot create %s: %s\n",
        target_filename, strerror (errno));
    }

  while (fgets (buffer, sizeof (buffer), source_file))
    {
      if (strstr (buffer, "<!-- GENERATED -->"))
         break;
         
      fprintf (target_file, "%s", buffer);
    }

  if (!feof (source_file))
    {
#define keybind(name, handler, param, flags, stroke, description) \
  single_stanza ( \
               flags & BINDING_PER_WINDOW, \
               #name, \
               stroke, \
               flags & BINDING_REVERSES, \
               description);
#include "all-keybindings.h"
#undef keybind
    }

  while (fgets (buffer, sizeof (buffer), source_file))
      fprintf (target_file, "%s", buffer);

  if (fclose (source_file)!=0)
    {
      g_error ("Cannot close %s: %s\n",
        source_filename, strerror (errno));
    }

  if (fclose (target_file)!=0)
    {
      g_error ("Cannot close %s: %s\n",
        target_filename, strerror (errno));
    }
}

int
main (int argc, char **argv)
{
  if (argc!=3)
    {
      g_error ("Syntax: %s <source.in.in> <target.in>\n", argv[0]);
    }
  
  source_filename = argv[1];
  target_filename = argv[2];

  /* Translators: Please don't translate "Control", "Shift", etc, since these
   * are hardcoded (in gtk/gtkaccelgroup.c; it's not marco's fault).
   * "disabled" must also stay as it is.
   */
  about_keybindings = g_markup_escape_text(_( \
        "The format looks like \"<Control>a\" or \"<Shift><Alt>F1\".\n\n"\
        "The parser is fairly liberal and allows "\
  	"lower or upper case, and also abbreviations such as \"<Ctl>\" and " \
	"\"<Ctrl>\". If you set the option to the special string " \
	"\"disabled\", then there will be no keybinding for this action."),
        -1);

  about_reversible_keybindings = g_markup_escape_text(_( \
        "The format looks like \"<Control>a\" or \"<Shift><Alt>F1\".\n\n"\
        "The parser is fairly liberal and allows "\
  	"lower or upper case, and also abbreviations such as \"<Ctl>\" and " \
	"\"<Ctrl>\". If you set the option to the special string " \
	"\"disabled\", then there will be no keybinding for this action.\n\n"\
	"This keybinding may be reversed by holding down the \"shift\" key; "
	"therefore, \"shift\" cannot be one of the keys it uses."),
	-1);

  produce_bindings ();

  g_free (about_keybindings);
  g_free (about_reversible_keybindings);
  
  return 0;
}

/* eof schema-bindings.c */