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/>.
20 #include <readline/readline.h>
21 #include <readline/history.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
27 #include "gacela_SDL.h"
28 #include "gacela_GL.h"
29 #include "gacela_FTGL.h"
33 find_matching_paren (int k)
37 int end_parens_found = 0;
39 // Choose the corresponding opening bracket
40 if (k == ')') c = '(';
41 else if (k == ']') c = '[';
42 else if (k == '}') c = '{';
44 for (i = rl_point-2; i >= 0; i--)
46 // Is the current character part of a character literal?
48 && rl_line_buffer[i - 1] == '\\'
49 && rl_line_buffer[i - 2] == '#')
51 else if (rl_line_buffer[i] == k)
53 else if (rl_line_buffer[i] == '"')
55 // Skip over a string literal
56 for (i--; i >= 0; i--)
57 if (rl_line_buffer[i] == '"'
59 && rl_line_buffer[i - 1] == '\\'))
62 else if (rl_line_buffer[i] == c)
64 if (end_parens_found==0) return i;
65 else --end_parens_found;
72 match_paren (int x, int k)
77 struct timeval timeout;
81 // Did we just insert a quoted paren? If so, then don't bounce
83 && rl_line_buffer[rl_point - 2] == '\\')
87 timeout.tv_sec = tmp / 1000000;
88 timeout.tv_usec = tmp % 1000000;
90 fno = fileno (rl_instream);
91 FD_SET (fno, &readset);
96 rl_point = find_matching_paren (k);
100 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
108 ctrl_c_handler (int signum)
110 printf ("ERROR: User interrupt\nABORT: (signal)\n");
114 init_gacela_client ()
116 struct sigaction new_action;
118 // init bouncing parens
119 rl_bind_key (')', match_paren);
120 rl_bind_key (']', match_paren);
121 rl_bind_key ('}', match_paren);
124 new_action.sa_handler = ctrl_c_handler;
125 sigemptyset (&new_action.sa_mask);
126 new_action.sa_flags = 0;
128 sigaction (SIGINT, &new_action, NULL);
132 gacela_client (char *hostname, int port)
135 struct hostent *server;
136 struct sockaddr_in serv_addr;
141 // Connect to the server
142 sockfd = socket (AF_INET, SOCK_STREAM, 0);
143 server = gethostbyname (hostname);
144 bzero ((char *) &serv_addr, sizeof (serv_addr));
145 serv_addr.sin_family = AF_INET;
146 bcopy ((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
147 serv_addr.sin_port = htons (port);
148 connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr));
151 asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
153 init_gacela_client ();
154 read_history (history_path);
157 line = readline ("gacela> ");
161 printf ("%s\n", line);
167 write_history (history_path);
172 init_gacela (void *data, int argc, char **argv)
174 // Guile configuration
175 scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
176 scm_c_eval_string ("(use-modules (ice-9 readline))");
177 scm_c_eval_string ("(activate-readline)");
178 scm_c_eval_string ("(use-modules (ice-9 optargs))");
179 scm_c_eval_string ("(use-modules (ice-9 receive))");
180 // scm_c_eval_string ("(read-enable 'case-insensitive)");
182 // Bindings for C functions and structs
183 SDL_register_functions (NULL);
184 GL_register_functions (NULL);
185 FTGL_register_functions (NULL);
191 load_scheme_files (char *path)
193 char load_path[strlen (path) + 1024];
195 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
196 scm_c_eval_string (load_path);
197 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
201 start_single (char *working_path)
203 char *argv = "guile";
205 scm_with_guile (&init_gacela, NULL);
206 load_scheme_files (working_path);
207 scm_shell (1, &argv);
211 start_server (char *working_path, int port)
213 char start_server[100];
215 scm_with_guile (&init_gacela, NULL);
216 load_scheme_files (working_path);
217 sprintf (start_server, "(start-server %d)", port);
218 scm_c_eval_string (start_server);
219 scm_c_eval_string ("(game-loop)");
223 start_client (char *hostname, int port)
226 gacela_client (hostname, port);
230 main (int argc, char *argv[])
236 for (i = 1; i < argc; i++)
237 if (strcmp (argv[i], "--shell-mode") == 0)
241 start_single (dirname (argv[0]));
246 start_client ("localhost", 1234);