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> ");
164 write (sockfd, "(", 1);
165 n = write (sockfd, line, strlen (line));
166 write (sockfd, ")", 1);
168 error("ERROR writing to socket");
173 n = read (sockfd, buffer, 255);
175 error("ERROR reading from socket");
176 printf ("%s\n", buffer);
182 write_history (history_path);
187 init_gacela (void *data, int argc, char **argv)
189 // Guile configuration
190 scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
191 scm_c_eval_string ("(use-modules (ice-9 readline))");
192 scm_c_eval_string ("(activate-readline)");
193 scm_c_eval_string ("(use-modules (ice-9 optargs))");
194 scm_c_eval_string ("(use-modules (ice-9 receive))");
195 // scm_c_eval_string ("(read-enable 'case-insensitive)");
197 // Bindings for C functions and structs
198 SDL_register_functions (NULL);
199 GL_register_functions (NULL);
200 FTGL_register_functions (NULL);
206 load_scheme_files (char *path)
208 char load_path[strlen (path) + 1024];
210 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
211 scm_c_eval_string (load_path);
212 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
216 start_single (char *working_path)
218 char *argv = "guile";
220 scm_with_guile (&init_gacela, NULL);
221 load_scheme_files (working_path);
222 scm_shell (1, &argv);
226 start_server (char *working_path, int port)
228 char start_server[100];
230 scm_with_guile (&init_gacela, NULL);
231 load_scheme_files (working_path);
232 sprintf (start_server, "(start-server %d)", port);
233 scm_c_eval_string (start_server);
234 scm_c_eval_string ("(game-loop)");
238 start_client (char *hostname, int port)
241 gacela_client (hostname, port);
245 main (int argc, char *argv[])
248 int mode = 0; // shell: 1, server: 2, client: 3
253 // Checking arguments
254 for (i = 1; i < argc; i++) {
255 if (strcmp (argv[i], "--shell-mode") == 0)
257 else if (strncmp (argv[i], "--server", 8) == 0) {
259 arg = strtok (argv[i], "=");
260 arg = strtok (NULL, "=");
264 else if (strncmp (argv[i], "--client", 8) == 0) {
266 arg = strtok (argv[i], "=");
267 arg = strtok (NULL, "=");
269 host = strtok (arg, ":");
270 arg = strtok (NULL, ":");
278 start_single (dirname (argv[0]));
279 else if (mode == 2 && port != 0)
280 start_server (dirname (argv[0]), port);
281 else if (mode == 3 && port != 0)
282 start_client (host, port);
287 start_client ("localhost", 1234);