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
|
/*
* Copyright (C) 2005 Ross Burton <ross@burtonini.com>
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include "e-sexp.h"
#include "devilspie.h"
#include "parser.h"
#include "logical.h"
#include "matchers.h"
#include "actions.h"
/**
* Table mapping function names to their implementations.
*/
static const struct {
char *name;
void *func;
/* TRUE if a function can perform shortcut evaluation or doesn't execute everything, FALSE otherwise */
gboolean shortcut;
} symbols[] = {
/* Logical operations */
{ "is", func_is, FALSE },
{ "contains", func_contains, FALSE },
{ "matches", func_matches, FALSE },
/* Matchers */
{ "window_name", func_window_name, FALSE },
{ "window_role", func_window_role, FALSE },
{ "window_class", func_window_class, FALSE },
{ "window_xid", func_window_xid, FALSE },
{ "application_name", func_application_name, FALSE },
{ "window_property", func_window_property, FALSE },
{ "window_workspace", func_window_workspace, FALSE},
/* Actions */
{ "debug", func_debug, FALSE },
{ "print", func_print, FALSE },
{ "println", func_println, FALSE },
{ "str", func_str, FALSE },
{ "hex", func_hex, FALSE },
{ "geometry", func_geometry, FALSE },
{ "fullscreen", func_fullscreen, FALSE },
{ "unfullscreen", func_unfullscreen, FALSE },
{ "focus", func_focus, FALSE },
{ "center", func_center, FALSE },
{ "maximize", func_maximize, FALSE },
{ "maximize_vertically", func_maximize_vertically, FALSE },
{ "maximize_horizontally", func_maximize_horizontally, FALSE },
{ "unmaximize", func_unmaximize, FALSE },
{ "minimize", func_minimize, FALSE },
{ "unminimize", func_unminimize, FALSE },
{ "shade", func_shade, FALSE },
{ "unshade", func_unshade, FALSE },
{ "close", func_close, FALSE },
{ "pin", func_pin, FALSE },
{ "unpin", func_unpin, FALSE },
{ "stick", func_stick, FALSE },
{ "unstick", func_unstick, FALSE },
{ "set_workspace", func_set_workspace, FALSE },
{ "change_workspace", func_change_workspace, FALSE },
{ "set_viewport", func_set_viewport, FALSE },
{ "skip_pager", func_skip_pager, FALSE },
{ "skip_tasklist", func_skip_tasklist, FALSE },
{ "above", func_above, FALSE },
{ "below", func_below, FALSE },
{ "decorate", func_decorate, FALSE },
{ "undecorate", func_undecorate, FALSE },
{ "wintype", func_wintype, FALSE },
{ "opacity", func_opacity, FALSE },
{ "spawn_sync", func_spawn_sync, FALSE },
{ "spawn_async", func_spawn_async, FALSE },
{ "quit", func_quit, FALSE},
};
/**
* Load a single configuration file.
*/
GList *load_configuration_file (const char *path)
{
/* TODO: GError argument */
GList *exp_list = NULL;
GScanner *gs = NULL;
ESExp *sexp = NULL;
size_t i;
int more_left = 0;
FILE *f;
g_return_val_if_fail (path != NULL, NULL);
if (debug) g_printerr(_("Loading %s\n"), path);
if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
if (debug) g_printerr(_("%s is not a normal file, skipping\n"), path);
return NULL;
}
f = fopen(path, "r");
if (!f) {
g_printerr(_("Cannot open %s\n"), path);
return NULL;
}
do {
sexp = e_sexp_new ();
/* HACK: Since e-sexp doesn't seem to have a good way to share one
scanner reference between multiple sexps, we fake it by freeing the
new one it's given at instantiation and replace it with the one we
grab the first time around. */
if (gs) {
g_scanner_destroy(sexp->scanner);
sexp->scanner = gs;
}
/* HACK: If this is the first sexp in the file, hold on to the scanner
it gets at instantiation. */
if (NULL == gs)
gs = sexp->scanner;
for(i=0; i < sizeof(symbols)/sizeof(symbols[0]); i++) {
if (symbols[i].shortcut) {
e_sexp_add_ifunction(sexp, 0, symbols[i].name, symbols[i].func, &context);
} else {
e_sexp_add_function(sexp, 0, symbols[i].name, symbols[i].func, &context);
}
}
e_sexp_input_file(sexp, fileno(f));
more_left = e_sexp_parse(sexp);
if (more_left == -1) {
g_printerr(_("Cannot parse %s: %s\n"), path, e_sexp_error (sexp));
g_object_unref (sexp);
fclose(f);
return NULL;
}
exp_list = g_list_append(exp_list, sexp);
} while (more_left);
fclose(f);
return exp_list;
}
/**
* Load all *.ds files in a particular directory.
*/
static void load_dir (const char *path)
{
/* TODO: GError argument*/
GError *error = NULL;
GDir *dir;
const char *name;
g_return_if_fail (path != NULL);
if (debug) g_printerr(_("Loading %s\n"), path);
if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
if (debug) g_printerr(_("%s doesn't exist\n"), path);
return;
}
if (!g_file_test (path, G_FILE_TEST_IS_DIR)) {
g_printerr(_("%s isn't a directory\n"), path);
return;
}
dir = g_dir_open (path, 0, &error);
if (!dir) {
g_printerr (_("Cannot open %s: %s\n"), path, error->message);
g_error_free (error);
return;
}
while ((name = g_dir_read_name (dir)) != NULL) {
char *filepath;
GList *s;
if (!g_str_has_suffix (name, ".ds"))
continue;
filepath = g_build_filename (path, name, NULL);
s = load_configuration_file (filepath);
if (s) sexps = g_list_concat (sexps, s);
g_free (filepath);
}
g_dir_close (dir);
}
/**
* Load the configuration files. This will load all .ds files in
* ${sysconfdir}/devilspie and ~/.devilspie/.
*/
void load_configuration(void) {
char *p;
p = g_build_filename (SYSCONFDIR, "devilspie", NULL);
load_dir (p);
g_free (p);
p = g_build_filename (g_get_home_dir (), ".devilspie", NULL);
load_dir (p);
g_free (p);
}
|