Debian Codegraph @

src/config.c

raw symbols SCIP
1 /**
2 * This file is part of devilspie2
3 * Copyright (C) 2013-2017 Andreas Rönnquist
4 *
5 * devilspie2 is free software: you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * devilspie2 is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with devilspie2.
17 * If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include <glib.h>
20
21 #define WNCK_I_KNOW_THIS_IS_UNSTABLE
22 #include <libwnck/libwnck.h>
23
24 #include <glib/gi18n.h>
25
26 #include <locale.h>
27
28
29 #include <lua.h>
30 #include <lualib.h>
31 #include <lauxlib.h>
32
33 #include "script.h"
34 #include "script_functions.h"
35
36 #include "config.h"
37
38 /**
39 *
40 */
41 GSList *event_lists[W_NUM_EVENTS] = { NULL, NULL, NULL, NULL };
42 const char *event_names[W_NUM_EVENTS] = {
43 "window_open",
44 "window_close",
45 "window_focus",
46 "window_blur",
47 };
48
49
50 /**
51 * filename_list_sortfunc
52 * function to sort the inserted filenames, to be able to determine
53 * which order files are loaded.
54 */
55 gint filename_list_sortfunc(gconstpointer a,gconstpointer b)
56 {
57 gchar *file1 = (gchar*)a;
58 gchar *file2 = (gchar*)b;
59
60 return g_ascii_strcasecmp(file1, file2);
61 }
62
63
64 /**
65 *
66 */
67 GSList *add_lua_file_to_list(GSList *list, gchar *filename)
68 {
69 gchar *temp_filename = g_strdup(filename);
70
71 list=g_slist_insert_sorted(list,
72 temp_filename,
73 filename_list_sortfunc);
74
75 return list;
76 }
77
78
79
80 /**
81 *
82 */
83 GSList *get_table_of_strings(lua_State *luastate,
84 gchar *script_folder,
85 gchar *table_name)
86 {
87 GSList *list = NULL;
88
89 if (luastate) {
90
91 lua_getglobal(luastate, table_name);
92
93 // Do we have a value?
94 if (lua_isnil(luastate, -1)) {
95 goto EXITPOINT;
96 }
97
98 // Is it a table?
99 if (!lua_istable(luastate, -1)) {
100 goto EXITPOINT;
101 }
102
103 lua_pushnil(luastate);
104
105 while(lua_next(luastate, -2)) {
106 if (lua_isstring(luastate, -1)) {
107 char *temp = (char *)lua_tostring(luastate, -1);
108
109 gchar *added_filename = g_build_path(G_DIR_SEPARATOR_S,
110 script_folder,
111 temp,
112 NULL);
113
114 list = add_lua_file_to_list(list, added_filename);
115 }
116 lua_pop(luastate, 1);
117 }
118 lua_pop(luastate, 1);
119 }
120
121 EXITPOINT:
122
123 return list;
124 }
125
126
127 /**
128 * is_in_list
129 * Go through _one_ list, and check if the filename is in this list
130 */
131 gboolean is_in_list(GSList *list, gchar *filename)
132 {
133 gboolean result = FALSE;
134
135 if (list) {
136 GSList *temp_list = list;
137
138 while (temp_list) {
139 gchar *list_filename = (gchar*)temp_list->data;
140 if (list_filename) {
141
142 if (g_ascii_strcasecmp(list_filename, filename) == 0) {
143 result = TRUE;
144 }
145 }
146 temp_list = temp_list->next;
147 }
148 }
149
150 return result;
151 }
152
153
154 /**
155 * is_in_any_list
156 * Go through our lists, and check if the file is already in any of them
157 */
158 gboolean is_in_any_list(gchar *filename)
159 {
160 win_event_type i;
161
162 for (i=0; i < W_NUM_EVENTS; i++) {
163 if (is_in_list(event_lists[i], filename))
164 return TRUE;
165 }
166
167 return FALSE;
168 }
169
170
171
172 /**
173 * load_config
174 * Load configuration from a file - From this we set up the lists of files
175 * which decides what script to load on what wnck event.
176 */
177 int load_config(gchar *filename)
178 {
179 lua_State *config_lua_state = NULL;
180 int result = 0;
181 const gchar *current_file = NULL;
182 GSList *temp_window_open_file_list = NULL;
183
184 // First get list of Lua files in folder - Then read variables from
185 // devilspie2.lua and put the files in the required lists.
186
187 gchar *script_folder = g_path_get_dirname(filename);
188
189 GDir *dir = g_dir_open(script_folder, 0, NULL);
190 if (!g_file_test(script_folder, G_FILE_TEST_IS_DIR)) {
191
192 printf("%s", _("script_folder isn't a folder."));
193 printf("\n");
194 return -1;
195 }
196
197 int total_number_of_files = 0;
198
199 config_lua_state = init_script();
200
201 if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
202
203 if (load_script(config_lua_state, filename) != 0) {
204 printf(_("Error: %s"), filename);
205 printf("\n");
206 result = -1;
207 goto EXITPOINT;
208 }
209
210 run_script(config_lua_state);
211
212 event_lists[W_CLOSE] = get_table_of_strings(config_lua_state,
213 script_folder,
214 "scripts_window_close");
215 event_lists[W_FOCUS] = get_table_of_strings(config_lua_state,
216 script_folder,
217 "scripts_window_focus");
218 event_lists[W_BLUR] = get_table_of_strings(config_lua_state,
219 script_folder,
220 "scripts_window_blur");
221 }
222
223 // add the files in the folder to our linked list
224 while ((current_file = g_dir_read_name(dir))) {
225
226 gchar *temp_filename = g_build_path(G_DIR_SEPARATOR_S,
227 script_folder,
228 current_file,
229 NULL);
230
231 // we only bother with *.lua in the folder
232 if (g_str_has_suffix(current_file, ".lua")) {
233
234 if (!is_in_any_list(temp_filename)) {
235 temp_window_open_file_list =
236 add_lua_file_to_list(temp_window_open_file_list, temp_filename);
237 }
238 total_number_of_files++;
239 }
240
241 g_free(temp_filename);
242 }
243
244 event_lists[W_OPEN] = temp_window_open_file_list;
245 EXITPOINT:
246 if (config_lua_state)
247 done_script(config_lua_state);
248
249 return result;
250 }
251
252
253
254 /**
255 *
256 */
257 void unallocate_file_list(GSList *file_list)
258 {
259 if (file_list) {
260
261 while(file_list) {
262
263 gchar *file_name = (gchar*)file_list->data;
264
265 if (file_name) g_free(file_name);
266
267 file_list = file_list->next;
268 }
269 }
270 }
271
272
273 /**
274 *
275 */
276 void clear_file_lists()
277 {
278 win_event_type i = 0;
279
280 for (i = 0; i < W_NUM_EVENTS; i++) {
281 if (event_lists[i]) {
282 unallocate_file_list(event_lists[i]);
283 }
284 }
285 }