Add command binding support with traversable history
[luakit.git] / luakit.c
blobfb6986ae97a849f841265947b0311fab180db232
1 /*
2 * luakit.c - luakit main functions
4 * Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * MIT/X Consortium License (applies to some functions from surf.c)
24 * (C) 2009 Enno Boland <gottox@s01.de>
26 * See COPYING.MIT for the full license.
30 #include <gtk/gtk.h>
31 #include <signal.h>
32 #include <stdlib.h>
33 #include <sys/wait.h>
34 #include "globalconf.h"
35 #include "common/util.h"
36 #include "luah.h"
37 #include "luakit.h"
39 static void sigchld(int sigint);
41 void
42 sigchld(int signum) {
43 (void) signum;
44 if (signal(SIGCHLD, sigchld) == SIG_ERR)
45 fatal("Can't install SIGCHLD handler");
46 while(0 < waitpid(-1, NULL, WNOHANG));
49 /* load command line options into luakit and return uris to load */
50 gchar**
51 parseopts(int argc, char *argv[]) {
52 GOptionContext *context;
53 gboolean *only_version = NULL;
54 gchar **uris = NULL;
56 /* define command line options */
57 const GOptionEntry entries[] = {
58 { "uri", 'u', 0, G_OPTION_ARG_STRING_ARRAY, &uris,
59 "uri(s) to load at startup", "URI" },
60 { "config", 'c', 0, G_OPTION_ARG_STRING, &globalconf.confpath,
61 "configuration file to use", "FILE" },
62 { "version", 'V', 0, G_OPTION_ARG_NONE, &only_version,
63 "show version", NULL },
64 { NULL, 0, 0, 0, NULL, NULL, NULL }};
66 /* parse command line options */
67 context = g_option_context_new("[URI...]");
68 g_option_context_add_main_entries(context, entries, NULL);
69 g_option_context_add_group(context, gtk_get_option_group(TRUE));
70 // TODO Passing gtk options (like --sync) to luakit causes a segfault right
71 // here. I'm clueless.
72 g_option_context_parse(context, &argc, &argv, NULL);
73 g_option_context_free(context);
75 /* print version and exit */
76 if(only_version) {
77 g_printf("Version: %s\n", VERSION);
78 exit(EXIT_SUCCESS);
81 if (uris && argv[1])
82 fatal("invalid mix of -u and default uri arguments");
84 if (uris)
85 return uris;
86 else
87 return argv+1;
90 void
91 init_lua(gchar **uris)
93 gchar *uri;
94 lua_State *L;
95 xdgHandle xdg;
97 /* init globalconf structs */
98 globalconf.signals = signal_tree_new();
99 globalconf.windows = g_ptr_array_new();
101 /* get XDG basedir data */
102 xdgInitHandle(&xdg);
104 /* init lua */
105 luaH_init(&xdg);
106 L = globalconf.L;
108 /* push a table of the statup uris */
109 lua_newtable(L);
110 for (gint i = 0; (uri = uris[i]); i++) {
111 lua_pushstring(L, uri);
112 lua_rawseti(L, -2, i + 1);
114 lua_setglobal(L, "uris");
116 /* parse and run configuration file */
117 if(!luaH_parserc(&xdg, globalconf.confpath, TRUE))
118 fatal("couldn't find rc file");
120 if (!globalconf.windows->len)
121 fatal("no windows spawned by rc file load, exiting");
123 /* we are finished with this */
124 xdgWipeHandle(&xdg);
127 void
128 init_directories()
130 /* create luakit directory */
131 globalconf.base_directory = g_build_filename(g_get_home_dir(), LUAKIT_CONFIG_DIR, NULL);
132 g_mkdir_with_parents(globalconf.base_directory, 0771);
136 main(int argc, char *argv[]) {
137 gchar **uris = NULL;
139 /* clean up any zombies */
140 sigchld(0);
142 gtk_init(&argc, &argv);
143 if (!g_thread_supported())
144 g_thread_init(NULL);
146 /* parse command line opts and get uris to load */
147 uris = parseopts(argc, argv);
149 init_directories();
150 init_lua(uris);
151 gtk_main();
152 return EXIT_SUCCESS;
155 // vim: ft=c:et:sw=4:ts=8:sts=4:enc=utf-8:tw=80