Debian Codegraph @

src/devilspie2.c

raw symbols SCIP
1 /**
2 * This file is part of devilspie2
3 * Copyright (C) 2005 Ross Burton, 2011-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
20 #include <string.h>
21
22 #include <stdlib.h>
23 #include <glib.h>
24 #include <glib/gstdio.h>
25 #include <gdk/gdk.h>
26 #include <gdk/gdkx.h>
27 #include <glib/gi18n.h>
28
29 #define WNCK_I_KNOW_THIS_IS_UNSTABLE
30 #include <libwnck/libwnck.h>
31
32 #include <lua.h>
33 #include <lualib.h>
34 #include <lauxlib.h>
35
36 #include <locale.h>
37
38 #include "script.h"
39 #include "script_functions.h"
40
41 #include "error_strings.h"
42
43 #include "config.h"
44
45
46 /**
47 *
48 */
49 GMainLoop *loop = NULL;
50
51 static gboolean debug = FALSE;
52 static gboolean emulate = FALSE;
53 static gboolean show_version = FALSE;
54
55 #ifdef HAVE_GTK3
56 // libwnck Version Information is only availible if you have
57 // libwnck 3.0 or later
58 static gboolean show_wnck_version = FALSE;
59 #endif
60
61 static gchar *script_folder = NULL;
62 static gchar *temp_folder = NULL;
63
64 GFileMonitor *mon = NULL;
65
66 gchar *config_filename = NULL;
67
68 /**
69 *
70 */
71 static void load_list_of_scripts(WnckScreen *screen, WnckWindow *window,
72 GSList *file_list)
73 {
74 GSList *temp_file_list = file_list;
75 // set the window to work on
76 set_current_window(window);
77
78 // for every file in the folder - load the script
79 if (event_lists[W_OPEN] != NULL) {
80
81 while(temp_file_list) {
82 gchar *filename = (gchar*)temp_file_list->data;
83
84 // is it a Lua file?
85 if (g_str_has_suffix((gchar*)filename, ".lua")) {
86
87 // init the script, run it
88 if (!load_script(global_lua_state, filename)) {
89 }
90
91 run_script(global_lua_state);
92
93 }
94 temp_file_list=temp_file_list->next;
95 }
96 }
97 return;
98
99 }
100
101
102 /**
103 *
104 */
105 static void window_opened_cb(WnckScreen *screen, WnckWindow *window)
106 {
107 load_list_of_scripts(screen, window, event_lists[W_OPEN]);
108 }
109
110
111 /**
112 *
113 */
114 static void window_closed_cb(WnckScreen *screen, WnckWindow *window)
115 {
116 load_list_of_scripts(screen, window, event_lists[W_CLOSE]);
117 }
118
119
120 /**
121 *
122 */
123 static void window_changed_cb(WnckScreen *screen, WnckWindow *window)
124 {
125 WnckWindow *cur;
126
127 load_list_of_scripts(screen, window, event_lists[W_BLUR]);
128 cur = wnck_screen_get_active_window(screen);
129 load_list_of_scripts(screen, cur, event_lists[W_FOCUS]);
130 }
131
132
133 /**
134 *
135 */
136 void init_screens()
137 {
138 int i;
139 int num_screens;
140
141 #ifndef GDK_VERSION_3_10
142 num_screens = gdk_display_get_n_screens(gdk_display_get_default());
143 #else
144 num_screens = 1;
145 #endif
146
147 for (i=0; i<num_screens; i++) {
148 WnckScreen *screen = wnck_screen_get(i);
149
150 g_signal_connect(screen, "window-opened",
151 (GCallback)window_opened_cb, NULL);
152 g_signal_connect(screen, "window-closed",
153 (GCallback)window_closed_cb, NULL);
154 g_signal_connect(screen, "active-window-changed",
155 (GCallback)window_changed_cb, NULL);
156 }
157 }
158
159
160 /**
161 * atexit handler - kill the script
162 */
163 void devilspie_exit()
164 {
165 clear_file_lists();
166
167 if (temp_folder != NULL) g_free(temp_folder);
168
169 if (mon) g_object_unref(mon);
170
171 if (config_filename) g_free(config_filename);
172 }
173
174
175 /**
176 * handle signals that are sent to the application
177 */
178 static void signal_handler(int sig)
179 {
180 printf("\n");
181 printf("%s", _("Received signal:"));
182 printf(" %d (%s)\n", sig, strsignal(sig));
183
184 done_script_error_messages();
185
186 if (sig == SIGINT) {
187 exit(EXIT_FAILURE);
188 }
189
190 }
191
192
193 /**
194 *
195 */
196 void print_list(GSList *list)
197 {
198 GSList *temp_list;
199 if (list != NULL) {
200 temp_list = list;
201
202 while(temp_list) {
203 gchar *file_name = temp_list->data;
204
205 if (file_name) {
206 if (g_str_has_suffix((gchar*)file_name, ".lua")) {
207 printf("%s\n", (gchar*)file_name);
208 }
209 }
210 temp_list = temp_list->next;
211 }
212 }
213 }
214
215
216 /**
217 *
218 */
219 void print_script_lists()
220 {
221 gboolean have_any_files = FALSE;
222 win_event_type i;
223
224 if (debug)
225 printf("------------\n");
226
227 for (i = 0; i < W_NUM_EVENTS; i++) {
228 if (event_lists[i])
229 have_any_files = TRUE;
230 // If we are running debug mode - print the list of files:
231 if (debug) {
232 printf(_("List of Lua files handling \"%s\" events in folder:"),
233 event_names[i]);
234 printf("\n");
235 if (event_lists[i]) {
236 print_list(event_lists[i]);
237 }
238 }
239 }
240
241 if (!have_any_files) {
242 printf("%s", _("No script files found in the script folder - exiting."));
243 printf("\n\n");
244 exit(EXIT_SUCCESS);
245 }
246 }
247
248
249 /**
250 *
251 */
252 void folder_changed_callback(GFileMonitor *mon,
253 GFile *first_file,
254 GFile *second_file,
255 GFileMonitorEvent event,
256 gpointer user_data)
257 {
258 gchar *our_filename = (gchar*)(user_data);
259
260 // If a file is created or deleted, we need to check the file lists again
261 if ((event == G_FILE_MONITOR_EVENT_CREATED) ||
262 (event == G_FILE_MONITOR_EVENT_DELETED)) {
263
264 clear_file_lists();
265
266 set_current_window(NULL);
267 load_config(our_filename);
268
269 if (debug)
270 printf("Files in folder updated!\n - new lists:\n\n");
271
272 print_script_lists();
273
274 if (debug)
275 printf("-----------\n");
276 }
277
278 // Also monitor if our devilspie2.lua file is changed - since it handles
279 // which files are window close or window open scripts.
280 if (event == G_FILE_MONITOR_EVENT_CHANGED) {
281 if (first_file) {
282 gchar *short_filename = g_file_get_basename(first_file);
283
284 if (g_strcmp0(short_filename, "devilspie2.lua")==0) {
285
286 clear_file_lists();
287
288 set_current_window(NULL);
289 load_config(our_filename);
290
291 print_script_lists();
292
293 if (debug)
294 printf("----------");
295 }
296 }
297 }
298 }
299
300
301 /**
302 * Program main entry
303 */
304 int main(int argc, char *argv[])
305 {
306 static const GOptionEntry options[]= {
307 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
308 N_("Print debug info to stdout")
309 },
310 { "emulate", 'e', 0, G_OPTION_ARG_NONE, &emulate,
311 N_("Don't apply any rules, only emulate execution")
312 },
313 { "folder", 'f', 0, G_OPTION_ARG_STRING, &script_folder,
314 N_("Search for scripts in this folder"),N_("FOLDER")
315 },
316 { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
317 N_("Show Devilspie2 version and quit")
318 },
319 #ifdef HAVE_GTK3
320 // libwnck Version Information is only availible if you have
321 // libwnck 3.0 or later
322 { "wnck-version", 'w', 0, G_OPTION_ARG_NONE, &show_wnck_version,
323 N_("Show libwnck version and quit")
324 },
325 #endif
326 { NULL }
327 };
328
329 GError *error = NULL;
330 GOptionContext *context;
331
332 // Init gettext stuff
333 setlocale(LC_ALL, "");
334
335 bindtextdomain(PACKAGE, LOCALEDIR);
336 bind_textdomain_codeset(PACKAGE, "");
337 textdomain(PACKAGE);
338
339 gchar *devilspie2_description =
340 g_strdup_printf(_("apply rules on windows"));
341
342 gchar *full_desc_string = g_strdup_printf("- %s", devilspie2_description);
343
344 context = g_option_context_new(full_desc_string);
345 g_option_context_add_main_entries(context, options, NULL);
346 if (!g_option_context_parse(context, &argc, &argv, &error)) {
347 g_print(_("option parsing failed: %s"), error->message);
348 printf("\n");
349 exit(EXIT_FAILURE);
350 }
351
352 gdk_init(&argc, &argv);
353
354 g_free(full_desc_string);
355 g_free(devilspie2_description);
356
357 // if the folder is NULL, default to ~/.config/devilspie2/
358 if (script_folder == NULL) {
359
360 temp_folder = g_build_path(G_DIR_SEPARATOR_S,
361 g_get_user_config_dir(),
362 "devilspie2",
363 NULL);
364
365 // check if the folder does exist
366 if (!g_file_test(temp_folder, G_FILE_TEST_IS_DIR)) {
367
368 // - and if it doesn't, create it.
369 if (g_mkdir(temp_folder, 0700) != 0) {
370 printf("%s", _("Couldn't create the default folder for devilspie2 scripts."));
371 printf("\n");
372 exit(EXIT_FAILURE);
373 }
374 }
375
376 script_folder = temp_folder;
377 }
378
379 if (show_version) {
380 printf("Devilspie2 v%s\n\n", DEVILSPIE2_VERSION);
381 exit(EXIT_SUCCESS);
382 }
383 #ifdef HAVE_GTK3
384 // libwnck Version Information is only availible if you have
385 // libwnck 3.0 or later
386 if (show_wnck_version) {
387 printf("libwnck v%d.%d.%d\n\n",
388 WNCK_MAJOR_VERSION,
389 WNCK_MINOR_VERSION,
390 WNCK_MICRO_VERSION);
391 exit(EXIT_SUCCESS);
392 }
393 #endif
394
395 #if (GTK_MAJOR_VERSION >= 3)
396 if (!GDK_IS_X11_DISPLAY(gdk_display_get_default())) {
397 printf("%s", _("An X11 display is required for devilspie2."));
398 printf("\n\n");
399 return EXIT_FAILURE;
400 }
401
402 #endif
403
404 if (init_script_error_messages()!=0) {
405 printf("%s", _("Couldn't init script error messages!"));
406 printf("\n");
407 exit(EXIT_FAILURE);
408 }
409
410 // set the current window to NULL, we don't need to be able to modify
411 // the windows when reading the config
412 set_current_window(NULL);
413
414 config_filename =
415 g_build_filename(script_folder, "devilspie2.lua", NULL);
416
417 if (load_config(config_filename) != 0) {
418
419 devilspie_exit();
420 return EXIT_FAILURE;
421 }
422
423 if (debug) {
424
425 if (emulate) {
426 printf("%s", _("Running devilspie2 in debug and emulate mode."));
427 } else {
428 printf("%s", _("Running devilspie2 in debug mode."));
429 }
430
431 printf("\n\n");
432
433 printf(_("Using scripts from folder: %s"), script_folder);
434
435 printf("\n");
436
437 devilspie2_debug = TRUE;
438 }
439
440 // Should we only run an emulation (don't modify any windows)
441 if (emulate) devilspie2_emulate = emulate;
442
443 GFile *directory_file;
444 directory_file = g_file_new_for_path(script_folder);
445 // mon = g_file_monitor_directory(directory_file, G_FILE_MONITOR_WATCH_MOUNTS,
446 mon = g_file_monitor_directory(directory_file, G_FILE_MONITOR_NONE,
447 NULL, NULL);
448 if (!mon) {
449 printf("%s", _("Couldn't create directory monitor!"));
450 printf("\n");
451 return EXIT_FAILURE;
452 }
453
454 g_signal_connect(mon, "changed", G_CALLBACK(folder_changed_callback),
455 (gpointer)(config_filename));
456
457 global_lua_state = init_script();
458 print_script_lists();
459
460 if (debug) printf("------------\n");
461
462 // remove stuff cleanly
463 atexit(devilspie_exit);
464
465 struct sigaction signal_action;
466
467 sigemptyset(&signal_action.sa_mask);
468 signal_action.sa_flags = 0;
469 signal_action.sa_handler = signal_handler;
470
471 if (sigaction(SIGINT, &signal_action, NULL) == -1) {
472 exit(EXIT_FAILURE);
473 }
474
475 init_screens();
476
477 loop=g_main_loop_new(NULL, TRUE);
478 g_main_loop_run(loop);
479
480 return EXIT_SUCCESS;
481 }