1 /* Gacela, a GNU Guile extension for fast games development
2 Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <readline/readline.h>
23 #include <readline/history.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
29 #include "gacela_SDL.h"
30 #include "gacela_GL.h"
31 #include "gacela_FTGL.h"
35 find_matching_paren (int k)
39 int end_parens_found = 0;
41 // Choose the corresponding opening bracket
42 if (k == ')') c = '(';
43 else if (k == ']') c = '[';
44 else if (k == '}') c = '{';
46 for (i = rl_point-2; i >= 0; i--)
48 // Is the current character part of a character literal?
50 && rl_line_buffer[i - 1] == '\\'
51 && rl_line_buffer[i - 2] == '#')
53 else if (rl_line_buffer[i] == k)
55 else if (rl_line_buffer[i] == '"')
57 // Skip over a string literal
58 for (i--; i >= 0; i--)
59 if (rl_line_buffer[i] == '"'
61 && rl_line_buffer[i - 1] == '\\'))
64 else if (rl_line_buffer[i] == c)
66 if (end_parens_found==0) return i;
67 else --end_parens_found;
74 match_paren (int x, int k)
79 struct timeval timeout;
83 // Did we just insert a quoted paren? If so, then don't bounce
85 && rl_line_buffer[rl_point - 2] == '\\')
89 timeout.tv_sec = tmp / 1000000;
90 timeout.tv_usec = tmp % 1000000;
92 fno = fileno (rl_instream);
93 FD_SET (fno, &readset);
98 rl_point = find_matching_paren (k);
102 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
110 ctrl_c_handler (int signum)
112 printf ("ERROR: User interrupt\nABORT: (signal)\n");
116 init_gacela_client ()
118 struct sigaction new_action;
120 // init bouncing parens
121 rl_bind_key (')', match_paren);
122 rl_bind_key (']', match_paren);
123 rl_bind_key ('}', match_paren);
126 new_action.sa_handler = ctrl_c_handler;
127 sigemptyset (&new_action.sa_mask);
128 new_action.sa_flags = 0;
130 sigaction (SIGINT, &new_action, NULL);
134 gacela_client (char *hostname, int port)
137 struct hostent *server;
138 struct sockaddr_in serv_addr;
143 // Connect to the server
144 sockfd = socket (AF_INET, SOCK_STREAM, 0);
145 server = gethostbyname (hostname);
146 bzero ((char *) &serv_addr, sizeof (serv_addr));
147 serv_addr.sin_family = AF_INET;
148 bcopy ((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
149 serv_addr.sin_port = htons (port);
150 connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr));
153 asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
155 init_gacela_client ();
156 read_history (history_path);
159 line = readline ("gacela> ");
163 printf ("%s\n", line);
169 write_history (history_path);
174 init_gacela (void *data, int argc, char **argv)
176 // Guile configuration
177 scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
178 scm_c_eval_string ("(use-modules (ice-9 readline))");
179 scm_c_eval_string ("(activate-readline)");
180 scm_c_eval_string ("(use-modules (ice-9 optargs))");
181 scm_c_eval_string ("(use-modules (ice-9 receive))");
182 // scm_c_eval_string ("(read-enable 'case-insensitive)");
184 // Bindings for C functions and structs
185 SDL_register_functions (NULL);
186 GL_register_functions (NULL);
187 FTGL_register_functions (NULL);
193 load_scheme_files (char *path)
195 char load_path[strlen (path) + 1024];
197 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
198 scm_c_eval_string (load_path);
199 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
203 start_single (char *working_path)
205 char *argv = "guile";
207 scm_with_guile (&init_gacela, NULL);
208 load_scheme_files (working_path);
209 scm_shell (1, &argv);
213 start_server (char *working_path, int port)
215 char start_server[100];
217 scm_with_guile (&init_gacela, NULL);
218 load_scheme_files (working_path);
219 sprintf (start_server, "(start-server %d)", port);
220 scm_c_eval_string (start_server);
221 scm_c_eval_string ("(game-loop)");
225 start_client (char *hostname, int port)
228 gacela_client (hostname, port);
232 main (int argc, char *argv[])
235 int mode = 0; // shell: 1, server: 2, client: 3
240 // Checking arguments
241 for (i = 1; i < argc; i++) {
242 if (strcmp (argv[i], "--shell-mode") == 0)
244 else if (strncmp (argv[i], "--server", 8) == 0) {
246 arg = strtok (argv[i], "=");
247 arg = strtok (NULL, "=");
251 else if (strncmp (argv[i], "--client", 8) == 0) {
253 arg = strtok (argv[i], "=");
254 arg = strtok (NULL, "=");
256 host = strtok (arg, ":");
257 arg = strtok (NULL, ":");
265 start_single (dirname (argv[0]));
266 else if (mode == 2 && port != 0)
267 start_server (dirname (argv[0]), port);
268 else if (mode == 3 && port != 0)
269 start_client (host, port);
274 start_client ("localhost", 1234);