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 "gacela_SDL.h"
25 #include "gacela_GL.h"
26 #include "gacela_FTGL.h"
30 find_matching_paren (int k)
34 int end_parens_found = 0;
36 // Choose the corresponding opening bracket
37 if (k == ')') c = '(';
38 else if (k == ']') c = '[';
39 else if (k == '}') c = '{';
41 for (i = rl_point-2; i >= 0; i--)
43 // Is the current character part of a character literal?
45 && rl_line_buffer[i - 1] == '\\'
46 && rl_line_buffer[i - 2] == '#')
48 else if (rl_line_buffer[i] == k)
50 else if (rl_line_buffer[i] == '"')
52 // Skip over a string literal
53 for (i--; i >= 0; i--)
54 if (rl_line_buffer[i] == '"'
56 && rl_line_buffer[i - 1] == '\\'))
59 else if (rl_line_buffer[i] == c)
61 if (end_parens_found==0) return i;
62 else --end_parens_found;
69 match_paren (int x, int k)
74 struct timeval timeout;
78 // Did we just insert a quoted paren? If so, then don't bounce
80 && rl_line_buffer[rl_point - 2] == '\\')
84 timeout.tv_sec = tmp / 1000000;
85 timeout.tv_usec = tmp % 1000000;
87 fno = fileno (rl_instream);
88 FD_SET (fno, &readset);
93 rl_point = find_matching_paren (k);
97 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
105 ctrl_c_handler (int signum)
107 printf ("ERROR: User interrupt\nABORT: (signal)\n");
111 init_gacela_client ()
113 struct sigaction new_action;
115 // init bouncing parens
116 rl_bind_key (')', match_paren);
117 rl_bind_key (']', match_paren);
118 rl_bind_key ('}', match_paren);
121 new_action.sa_handler = ctrl_c_handler;
122 sigemptyset (&new_action.sa_mask);
123 new_action.sa_flags = 0;
125 sigaction (SIGINT, &new_action, NULL);
129 gacela_client (char *hostname, int port)
132 struct hostent *server;
133 struct sockaddr_in serv_addr;
138 // Connect to the server
139 sockfd = socket (AF_INET, SOCK_STREAM, 0);
140 server = gethostbyname (hostname);
141 bzero ((char *) &serv_addr, sizeof (serv_addr));
142 serv_addr.sin_family = AF_INET;
143 bcopy ((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
144 serv_addr.sin_port = htons (port);
145 connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr));
148 asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
150 init_gacela_client ();
151 read_history (history_path);
154 line = readline ("gacela> ");
158 printf ("%s\n", line);
164 write_history (history_path);
169 init_gacela (void *data, int argc, char **argv)
171 // Guile configuration
172 scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
173 scm_c_eval_string ("(use-modules (ice-9 readline))");
174 scm_c_eval_string ("(activate-readline)");
175 scm_c_eval_string ("(use-modules (ice-9 optargs))");
176 scm_c_eval_string ("(use-modules (ice-9 receive))");
177 // scm_c_eval_string ("(read-enable 'case-insensitive)");
179 // Bindings for C functions and structs
180 SDL_register_functions (NULL);
181 GL_register_functions (NULL);
182 FTGL_register_functions (NULL);
188 load_scheme_files (char *path)
190 char load_path[strlen (path) + 1024];
192 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
193 scm_c_eval_string (load_path);
194 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
198 start_single (int argc, char *argv[])
200 scm_with_guile (&init_gacela, NULL);
201 load_scheme_files (dirname (argv[0]));
202 scm_shell (argc, argv);
206 start_server (int argc, char *argv[])
208 scm_with_guile (&init_gacela, NULL);
209 load_scheme_files (dirname (argv[0]));
210 scm_c_eval_string ("(start-server 1234)");
211 scm_c_eval_string ("(game-loop)");
215 start_client (char *hostname, int port)
218 gacela_client (hostname, port);
222 main (int argc, char *argv[])
224 start_single (argc, argv);
229 start_client ("localhost", 1234);