Set destructor = NULL in destroy func to prevent it being called twice
[luakit.git] / luakit.c
blobbd970b365f69f6c2775ee8c7b0455c2d0453471d
1 /*
2 * luakit.c - luakit main functions
4 * Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
5 * Copyright (C) 2009 Enno Boland <gottox@s01.de>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <gtk/gtk.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <sys/wait.h>
26 #include "globalconf.h"
27 #include "common/util.h"
28 #include "luah.h"
29 #include "luakit.h"
31 static void sigchld(int sigint);
33 void
34 sigchld(int signum) {
35 (void) signum;
36 if (signal(SIGCHLD, sigchld) == SIG_ERR)
37 fatal("Can't install SIGCHLD handler");
38 while(0 < waitpid(-1, NULL, WNOHANG));
41 /* load command line options into luakit and return uris to load */
42 gchar**
43 parseopts(int argc, char *argv[]) {
44 GOptionContext *context;
45 gboolean *only_version = NULL;
46 gchar **uris = NULL;
48 /* define command line options */
49 const GOptionEntry entries[] = {
50 { "uri", 'u', 0, G_OPTION_ARG_STRING_ARRAY, &uris, "uri(s) to load at startup", "URI" },
51 { "config", 'c', 0, G_OPTION_ARG_STRING, &globalconf.confpath, "configuration file to use", "FILE" },
52 { "version", 'V', 0, G_OPTION_ARG_NONE, &only_version, "print version and exit", NULL },
53 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &globalconf.verbose, "print debugging output", NULL },
54 { NULL, 0, 0, 0, NULL, NULL, NULL },
57 /* parse command line options */
58 context = g_option_context_new("[URI...]");
59 g_option_context_add_main_entries(context, entries, NULL);
60 g_option_context_add_group(context, gtk_get_option_group(TRUE));
61 // TODO Passing gtk options (like --sync) to luakit causes a segfault right
62 // here. I'm clueless.
63 g_option_context_parse(context, &argc, &argv, NULL);
64 g_option_context_free(context);
66 /* print version and exit */
67 if(only_version) {
68 g_printf("Version: %s\n", VERSION);
69 exit(EXIT_SUCCESS);
72 if (uris && argv[1])
73 fatal("invalid mix of -u and default uri arguments");
75 if (uris)
76 return uris;
77 else
78 return argv+1;
81 void
82 init_lua(gchar **uris)
84 gchar *uri;
85 lua_State *L;
87 /* init globalconf structs */
88 globalconf.signals = signal_new();
89 globalconf.windows = g_ptr_array_new();
91 /* init lua */
92 luaH_init();
93 L = globalconf.L;
95 /* push a table of the statup uris */
96 lua_newtable(L);
97 for (gint i = 0; (uri = uris[i]); i++) {
98 lua_pushstring(L, uri);
99 lua_rawseti(L, -2, i + 1);
101 lua_setglobal(L, "uris");
103 /* parse and run configuration file */
104 if(!luaH_parserc(globalconf.confpath, TRUE))
105 fatal("couldn't find rc file");
107 if (!globalconf.windows->len)
108 fatal("no windows spawned by rc file, exiting");
111 void
112 init_directories(void)
114 /* create luakit directory */
115 globalconf.cache_dir = g_build_filename(g_get_user_cache_dir(), "luakit", NULL);
116 globalconf.config_dir = g_build_filename(g_get_user_config_dir(), "luakit", NULL);
117 globalconf.data_dir = g_build_filename(g_get_user_data_dir(), "luakit", NULL);
118 g_mkdir_with_parents(globalconf.cache_dir, 0771);
119 g_mkdir_with_parents(globalconf.config_dir, 0771);
120 g_mkdir_with_parents(globalconf.data_dir, 0771);
124 main(int argc, char *argv[]) {
125 gchar **uris = NULL;
127 /* clean up any zombies */
128 sigchld(0);
130 gtk_init(&argc, &argv);
131 if (!g_thread_supported())
132 g_thread_init(NULL);
134 /* parse command line opts and get uris to load */
135 uris = parseopts(argc, argv);
137 init_directories();
138 init_lua(uris);
140 gtk_main();
141 return EXIT_SUCCESS;
144 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80