Fixes for datatype size on amd64.
[crack-attack.git] / src / gtk-gui / callbacks.cxx
blob6e0b8ed8ccc76b03466b082261ec49ac8275cbed
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>
32 #include <glib.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 #ifndef _WIN32
43 #include <sys/types.h>
44 #include <signal.h>
45 #else
46 #include <windows.h>
47 #endif
49 static GtkWidget *fraClient, *fraSingle, *fraServer;
50 static GtkWindow *window = NULL, *networking = NULL;
51 static int mode = CM_SOLO;
52 static GPid game_pid = 0;
53 static GIOChannel *channel = NULL;
54 static guint source_id = 0;
56 extern int glut_argc;
57 extern char **glut_argv;
59 void turn_game_prefs_off () {
60 GAME_SINGLE = GAME_SERVER = GAME_CLIENT = GAME_EXTREME = FALSE;
63 void lookup_widgets(GtkToggleButton * gtb) {
64 fraClient = lookup_widget(GTK_WIDGET(gtb), "fraClient");
65 fraSingle = lookup_widget(GTK_WIDGET(gtb), "fraSingle");
66 fraServer = lookup_widget(GTK_WIDGET(gtb), "fraServer");
69 void turn_sensitive_off() {
70 gtk_widget_set_sensitive (fraClient, FALSE);
71 gtk_widget_set_sensitive (fraSingle, FALSE);
72 gtk_widget_set_sensitive (fraServer, FALSE);
75 void toggle_sensitive (GtkToggleButton* gtb, gint selector) {
76 gtk_widget_set_sensitive (fraClient, FALSE);
77 gtk_widget_set_sensitive (fraSingle, FALSE);
78 gtk_widget_set_sensitive (fraServer, FALSE);
79 switch (selector) {
80 case 1: gtk_widget_set_sensitive (fraClient, TRUE);
81 break;
82 case 2: gtk_widget_set_sensitive (fraServer, TRUE);
83 break;
87 void prepare_for_actions (GtkToggleButton *gtb) {
88 lookup_widgets(gtb);
89 turn_game_prefs_off();
90 turn_sensitive_off();
93 static void
94 game_end (GPid pid, gint status, gpointer data) {
95 #ifdef DEVELOPMENT
96 g_print("game_end called!\n");
97 #endif
98 game_pid = 0;
99 if (networking) {
100 gtk_widget_destroy(GTK_WIDGET(networking));
102 if (window) {
103 gtk_widget_show(GTK_WIDGET(window));
105 g_spawn_close_pid (pid);
108 void
109 ca_error_dialog (const char *message)
111 GtkWidget *dialog = NULL;
112 dialog = gtk_message_dialog_new (window,
113 GTK_DIALOG_DESTROY_WITH_PARENT,
114 GTK_MESSAGE_ERROR,
115 GTK_BUTTONS_CLOSE,
116 message);
117 gtk_dialog_run (GTK_DIALOG (dialog));
118 gtk_widget_destroy (dialog);
121 #ifndef _WIN32
122 void
123 on_btnCancel_clicked (GtkButton *button,
124 gpointer user_data)
126 GtkWidget *win = GTK_WIDGET(user_data);
127 gtk_widget_destroy(win);
130 void
131 on_winNetworking_destroy (GtkObject *object,
132 gpointer user_data)
134 if (game_pid) {
135 kill(game_pid, SIGKILL);
137 networking = NULL;
138 if(NULL!=channel) {
139 g_io_channel_unref(channel);
140 channel = NULL;
144 gboolean networking_output (
145 GIOChannel *source, GIOCondition condition, gpointer data)
147 if (condition & G_IO_IN) {
148 GtkWidget *win = GTK_WIDGET(data);
149 GtkTextView *txtOutput = NULL;
150 GtkTextIter iter;
151 GtkTextBuffer *buffer = NULL;
152 gchar *out = NULL;
153 gsize length = 0;
155 txtOutput = GTK_TEXT_VIEW(
156 lookup_widget(GTK_WIDGET(win), "txtOutput"));
157 buffer = gtk_text_view_get_buffer(txtOutput);
158 gtk_text_buffer_get_end_iter(buffer, &iter);
159 g_io_channel_read_line(source, &out, &length, NULL, NULL);
160 if (length > 0 && out[0] != '\33') {
161 g_print(out);
162 gtk_text_buffer_insert(buffer, &iter, out, length);
164 g_free(out);
165 return true;
168 if (condition & G_IO_HUP) {
169 g_source_remove(source_id);
170 return false;
173 return true;
176 void
177 spawn_networking_dialog (gint standard_output)
179 networking = GTK_WINDOW(create_winNetworking());
180 gtk_widget_show(GTK_WIDGET(networking));
182 if(channel != NULL) {
183 g_io_channel_unref(channel);
184 channel = NULL;
186 channel = g_io_channel_unix_new(standard_output);
187 source_id = g_io_add_watch(channel, (GIOCondition) (G_IO_IN | G_IO_HUP),
188 networking_output, networking);
190 #endif // _WIN32
192 void
193 on_btnStart_clicked (GtkButton *button,
194 gpointer user_data)
196 window =
197 GTK_WINDOW(lookup_widget(GTK_WIDGET(button),"winCrackAttackSplash"));
199 // Save the gui data when the game starts.
200 gui_data_save(GTK_WIDGET(button));
202 // Set the mode based on all the widget values...
203 mode = generate_mode(GTK_WIDGET(button));
205 #ifdef DEVELOPMENT
206 g_print("Looking for location: %s\n", GC_BINARY_LOCATION);
207 #endif
208 GError *err = NULL;
209 GPid pid;
210 gtk_widget_hide(GTK_WIDGET(window));
211 GSpawnFlags flags = (GSpawnFlags) (G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
212 G_SPAWN_DO_NOT_REAP_CHILD);
214 gchar **args =
215 generate_array(mode, GC_BINARY_LOCATION, GTK_WIDGET(button));
217 gint sin, sout, serr;
218 gboolean ret =
219 g_spawn_async_with_pipes(
220 NULL, args, NULL,
221 flags, NULL, NULL,
222 &pid,
223 &sin, &sout, &serr,
224 &err);
226 if (ret) {
227 g_child_watch_add(pid, (GChildWatchFunc) game_end, NULL);
228 game_pid = pid;
230 #ifndef _WIN32
231 if (mode & CM_SERVER) {
232 spawn_networking_dialog(sout);
234 #endif
236 } else {
237 if (err) ca_error_dialog(err->message);
240 g_free(args);
243 gboolean
244 on_winCrackAttackSplash_delete_event (GtkWindow *window,
245 gpointer user_data)
247 return FALSE;
250 void
251 on_cmbQuality_changed (GtkComboBox *cmb,
252 gpointer user_data)
254 gint tmp = gtk_combo_box_get_active(cmb);
255 GtkImage * imgLogo = GTK_IMAGE(lookup_widget(GTK_WIDGET(cmb), "imgLogo"));
256 gchar *filename = NULL;
257 switch (tmp) {
258 case 0: {
259 filename = find_pixmap_file("preview_normal.png");
260 break;
262 case 1: {
263 filename = find_pixmap_file("preview_reduced.png");
264 break;
266 case 2: {
267 filename = find_pixmap_file("preview_extremely_reduced.png");
268 break;
270 default: {
271 filename = find_pixmap_file("preview_normal.png");
274 gtk_image_set_from_file(imgLogo, filename);