| 1 |
|
| 2 |
|
| 3 |
t |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 94 |
if (lua_isnil(luastate, -1)) { |
| 95 |
goto EXITPOINT; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 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 |
|
| 129 |
|
| 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 |
|
| 156 |
|
| 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 |
|
| 174 |
|
| 175 |
|
| 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 |
|
| 185 |
|
| 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 |
|
| 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 |
|
| 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 |
} |