New way to spawn crack-attack.
[crack-attack.git] / src / gtk-gui / callbacks.cxx
blob30f6cff684aa5367ab2958d2903267302dca6bbd
1 /*
2 * callbacks.cxx
3 * Andrew Sayman - 10/11/04
5 * Copyright (C) 2000 Daniel Nelson
6 * Copyright (C) 2004 Andrew Sayman
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Daniel Nelson - aluminumangel.org
23 * 174 W. 18th Ave.
24 * Columbus, OH 43210
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
31 #include <gtk/gtk.h>
33 #include "../Mode.h"
34 #include "../Attack.h"
35 #include "callbacks.h"
36 #include "interface.h"
37 #include "support.h"
38 #include "prefs.h"
39 #include "persist.h"
40 #include "modeparser.h"
42 static GtkWidget *fraClient, *fraSingle, *fraServer;
43 static GtkWindow *window = NULL;
44 static int mode = CM_SOLO;
45 static gboolean MS_RUNNING = FALSE;
46 static pid_t running_process = 0;
47 static int height = 400, width = 400;
49 extern int glut_argc;
50 extern char **glut_argv;
52 void turn_game_prefs_off () {
53 GAME_SINGLE = GAME_SERVER = GAME_CLIENT = GAME_EXTREME = FALSE;
56 void lookup_widgets(GtkToggleButton * gtb) {
57 fraClient = lookup_widget(GTK_WIDGET(gtb), "fraClient");
58 fraSingle = lookup_widget(GTK_WIDGET(gtb), "fraSingle");
59 fraServer = lookup_widget(GTK_WIDGET(gtb), "fraServer");
62 void turn_sensitive_off() {
63 gtk_widget_set_sensitive (fraClient, FALSE);
64 gtk_widget_set_sensitive (fraSingle, FALSE);
65 gtk_widget_set_sensitive (fraServer, FALSE);
68 void toggle_sensitive (GtkToggleButton* gtb, gint selector) {
69 gtk_widget_set_sensitive (fraClient, FALSE);
70 gtk_widget_set_sensitive (fraSingle, FALSE);
71 gtk_widget_set_sensitive (fraServer, FALSE);
72 switch (selector) {
73 case 1: gtk_widget_set_sensitive (fraClient, TRUE);
74 break;
75 case 2: gtk_widget_set_sensitive (fraServer, TRUE);
76 break;
80 void prepare_for_actions (GtkToggleButton *gtb) {
81 lookup_widgets(gtb);
82 turn_game_prefs_off();
83 turn_sensitive_off();
86 void
87 on_rbtnSingle_toggled (GtkToggleButton *togglebutton,
88 gpointer user_data)
90 GtkWidget *x = lookup_widget(GTK_WIDGET(togglebutton), "cbtnXtreme2");
91 gtk_toggle_button_set_active((GtkToggleButton *) x, FALSE);
92 if (gtk_toggle_button_get_active(togglebutton)) {
93 prepare_for_actions(togglebutton);
94 gtk_widget_set_sensitive (fraSingle, TRUE);
95 GAME_SINGLE = TRUE;
96 mode |= CM_SOLO;
97 } else {
98 mode &= ~CM_SOLO;
103 void
104 on_rbtnServer_toggled (GtkToggleButton *togglebutton,
105 gpointer user_data)
107 GtkWidget *x = lookup_widget(GTK_WIDGET(togglebutton), "cbtnXtreme");
108 gtk_toggle_button_set_active((GtkToggleButton *) x, FALSE);
109 if (gtk_toggle_button_get_active(togglebutton)) {
110 prepare_for_actions(togglebutton);
111 gtk_widget_set_sensitive (fraServer, TRUE);
112 GAME_SERVER = TRUE;
113 mode |= CM_SERVER;
114 } else {
115 mode &= ~CM_SERVER;
119 void
120 on_rbtnClient_toggled (GtkToggleButton *togglebutton,
121 gpointer user_data)
123 if (gtk_toggle_button_get_active(togglebutton)) {
124 prepare_for_actions(togglebutton);
125 gtk_widget_set_sensitive (fraClient, TRUE);
126 GAME_CLIENT = TRUE;
127 mode |= CM_CLIENT;
128 } else {
129 mode &= ~CM_CLIENT;
134 void
135 on_Xtreme_toggled (GtkToggleButton *togglebutton,
136 gpointer user_data)
138 if (gtk_toggle_button_get_active(togglebutton)) {
139 GAME_EXTREME = TRUE;
140 mode |= CM_X;
141 } else {
142 GAME_EXTREME = FALSE;
143 mode &= ~CM_X;
147 void
148 ca_error_dialog (const char *message)
150 GtkWidget *dialog = NULL;
151 dialog = gtk_message_dialog_new (window,
152 GTK_DIALOG_DESTROY_WITH_PARENT,
153 GTK_MESSAGE_ERROR,
154 GTK_BUTTONS_CLOSE,
155 message);
156 gtk_dialog_run (GTK_DIALOG (dialog));
157 gtk_widget_destroy (dialog);
160 void
161 on_btnStart_clicked (GtkButton *button,
162 gpointer user_data)
164 GtkTextView *view;
165 GtkTextBuffer *buffer;
166 GtkWidget *dialog;
167 GtkEntry *entPlayerName, *entPort, *entServerAddress;
168 GError **error = NULL;
169 gchar **output = NULL, **errorput = NULL;
170 gint *exit = NULL;
171 gchar *command = NULL;
172 gchar *tmp = NULL;
173 pid_t fork_ret;
174 int status;
175 command = g_strdup("crack-attack ");
176 char player_name[GC_PLAYER_NAME_LENGTH];
177 char host_name[256];
178 int port;
179 window = GTK_WINDOW(lookup_widget(GTK_WIDGET(button),"winCrackAttackSplash"));
180 if (MS_RUNNING) {
181 //Game is running, display an error message to the user
182 ca_error_dialog("Error: crack-attack is already running");
183 return;
185 if (GAME_SINGLE == TRUE) {
186 host_name[0] = '\0';
187 } else if (GAME_SERVER == TRUE) {
188 host_name[0] = '\0';
189 entPort = GTK_ENTRY(lookup_widget(GTK_WIDGET(button), "entPort"));
190 if (entPort) {
191 tmp = (gchar *)gtk_entry_get_text(entPort);
192 if (tmp) {
193 port = atoi(tmp);
194 } else {
195 port = 0;
198 } else if (GAME_CLIENT == TRUE) {
199 entPort = GTK_ENTRY(lookup_widget(GTK_WIDGET(button), "entPort2"));
200 if (entPort) {
201 tmp = (gchar *)gtk_entry_get_text(entPort);
202 if (tmp) {
203 port = atoi(tmp);
204 } else {
205 port = 0;
208 entServerAddress = GTK_ENTRY(lookup_widget(GTK_WIDGET(button), "entServerAddress"));
209 if (entServerAddress) {
210 tmp = (gchar *)gtk_entry_get_text(entServerAddress);
211 g_strlcpy (host_name, tmp, 256);
213 #ifdef DEVELOPMENT
214 cout << "Host name: " << host_name << endl;
215 #endif
218 /* Make the game xtreme!! */
219 if (GAME_EXTREME == TRUE) {
220 mode |= CM_X;
223 /* Set the name */
224 entPlayerName = GTK_ENTRY(lookup_widget(GTK_WIDGET(button), "entPlayerName"));
225 if (entPlayerName) {
226 tmp = (gchar *)gtk_entry_get_text(entPlayerName);
227 if (tmp) {
228 if (strlen(tmp) == 0) {
229 ca_error_dialog("Player's name isn't set.");
230 gtk_entry_set_text(entPlayerName, g_get_user_name());
231 return;
233 g_strlcpy(player_name, tmp, 256);
234 #ifdef DEVELOPMENT
235 g_print ("Player name: %s tmp: %s",player_name,tmp);
236 #endif
239 #ifdef DEVELOPMENT
240 g_print(command);
241 #endif
243 // Save the gui data when the game starts.
244 gui_data_save(button);
246 // Set the height and width to the selected value.
247 height = width = gui_get_dimensions(GTK_WIDGET(button));
249 gtk_widget_hide(GTK_WIDGET(window));
250 int exit_status;
251 GError *err;
252 gchar *c = generate_arguments(mode, br_strcat(BINDIR, GC_DD GC_BINARY), GTK_WIDGET(button));
253 #ifdef DEVELOPMENT
254 g_print("Command line: %s\n", c);
255 #endif
256 gboolean ret = g_spawn_command_line_sync(c, NULL, NULL, &exit_status, &err);
257 g_free(c);
258 if (!ret) {
259 if (err) ca_error_dialog(err->message);
261 gtk_widget_show(GTK_WIDGET(window));
262 g_free(command);
267 void
268 on_btnHelp_clicked (GtkButton *button,
269 gpointer user_data)
275 void
276 on_cbtnLowGraphics_toggled (GtkToggleButton *togglebutton,
277 gpointer user_data)
279 GtkWidget *lowButton = lookup_widget(GTK_WIDGET(togglebutton), "cbtnReallyLowGraphics");
280 if (gtk_toggle_button_get_active(togglebutton)) {
281 GRAPHICS_LOW = TRUE;
282 mode |= CM_LOW_GRAPHICS;
283 } else {
284 GRAPHICS_LOW = FALSE;
285 mode &= ~CM_LOW_GRAPHICS;
286 mode &= ~CM_REALLY_LOW_GRAPHICS;
287 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(lowButton), FALSE);
291 void
292 on_cbtnReallyLowGraphics_toggled (GtkToggleButton *togglebutton,
293 gpointer user_data)
295 GtkWidget *lowButton = lookup_widget(GTK_WIDGET(togglebutton), "cbtnLowGraphics");
296 if (gtk_toggle_button_get_active(togglebutton)) {
297 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(lowButton), TRUE);
298 GRAPHICS_LOW = TRUE;
299 mode |= CM_LOW_GRAPHICS;
300 mode |= CM_REALLY_LOW_GRAPHICS;
301 } else {
302 GRAPHICS_LOW = FALSE;
303 mode &= ~CM_REALLY_LOW_GRAPHICS;
307 gboolean
308 on_winCrackAttackSplash_delete_event (GtkWindow *window,
309 gpointer user_data)
311 return FALSE;
314 void
315 on_640by480_activate (GtkMenuItem *menuitem,
316 gpointer user_data)
318 height = width = GC_RESOLUTION_0;
321 void
322 on_800by600_activate (GtkMenuItem *menuitem,
323 gpointer user_data)
325 height = width = GC_RESOLUTION_1;
328 void
329 on_1024by768_activate (GtkMenuItem *menuitem,
330 gpointer user_data)
332 height = width = GC_RESOLUTION_2;
335 void
336 on_1280by1024_activate (GtkMenuItem *menuitem,
337 gpointer user_data)
339 height = width = GC_RESOLUTION_3;
342 void
343 on_1600by1200_activate (GtkMenuItem *menuitem,
344 gpointer user_data)
346 height = width = GC_RESOLUTION_4;
349 static void undo_ai_settings()
351 mode &= ~CM_AI;
352 mode &= ~CM_AI_EASY;
353 mode &= ~CM_AI_MEDIUM;
354 mode &= ~CM_AI_HARD;
357 void
358 on_ai_none_activate (GtkMenuItem *menuitem,
359 gpointer user_data)
361 GtkWidget *x = lookup_widget(GTK_WIDGET(menuitem), "cbtnXtreme");
362 gtk_widget_set_sensitive(x, TRUE);
363 undo_ai_settings();
366 void
367 on_ai_easy_activate (GtkMenuItem *menuitem,
368 gpointer user_data)
370 GtkWidget *x = lookup_widget(GTK_WIDGET(menuitem), "cbtnXtreme");
371 gtk_toggle_button_set_active((GtkToggleButton *) x, FALSE);
372 gtk_widget_set_sensitive(x, FALSE);
373 undo_ai_settings();
374 mode |= CM_AI;
375 mode |= CM_AI_EASY;
378 void
379 on_ai_medium_activate (GtkMenuItem *menuitem,
380 gpointer user_data)
382 GtkWidget *x = lookup_widget(GTK_WIDGET(menuitem), "cbtnXtreme");
383 gtk_toggle_button_set_active((GtkToggleButton *) x, FALSE);
384 gtk_widget_set_sensitive(x, FALSE);
385 undo_ai_settings();
386 mode |= CM_AI;
387 mode |= CM_AI_MEDIUM;
390 void
391 on_ai_hard_activate (GtkMenuItem *menuitem,
392 gpointer user_data)
394 GtkWidget *x = lookup_widget(GTK_WIDGET(menuitem), "cbtnXtreme");
395 gtk_toggle_button_set_active((GtkToggleButton *) x, FALSE);
396 gtk_widget_set_sensitive(x, FALSE);
397 undo_ai_settings();
398 mode |= CM_AI;
399 mode |= CM_AI_HARD;