File: g_connect.c

package info (click to toggle)
gbatnav 1.0.4cvs20051004-8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 4,780 kB
  • sloc: ansic: 14,122; sh: 11,667; makefile: 643; yacc: 288; xml: 42; sed: 16
file content (239 lines) | stat: -rw-r--r-- 6,881 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*	$Id: g_connect.c,v 1.3 2002/04/19 02:28:52 riq Exp $	*/
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2000 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <rquesada@core-sdi.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/**
 * @file conectar.c
 * Tiene los dialogos de coneccion y de seleccion de color y game type
 */
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <assert.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>

#include "common.h"
#include "support.h"
#include "protocol.h"
#include "cliente.h"
#include "proceso.h"
#include "g_interface.h"
#include "pantalla.h"
#include "gbnclient.h"
#include "sendmsg.h"
#include "configure.h"
#include "bnwrite.h"
#include "version.h"
#include "g_connect.h"

static GtkWidget *conectar_window = NULL;
static GtkWidget *con_entry_name=NULL;
static GtkWidget *con_spinner_port=NULL;
static GtkWidget *con_entry_server=NULL;

static GtkWidget *gametype_dialog=NULL;
static GtkWidget *gametype_button_secret=NULL;
static GtkWidget *gametype_button_conqworld=NULL;

static GtkWidget *button_launch=NULL;


static void close_descriptors( void )
{
	int i, open_max;

	/* force manually the close of the socket */
	if( usuario.sock >0 )
		close( usuario.sock );

	open_max = sysconf (_SC_OPEN_MAX);
	for( i = 3; i < open_max; i++ )
		fcntl (i, F_SETFD, FD_CLOEXEC);
}

static BATNAV_STATUS launch_server( int port )
{
	pid_t pid;
	char *args[4];

	if ( (pid = fork()) < 0) {
		perror("gbnclient:");
		return BATNAV_STATUS_ERROR;
	} else if (pid == 0) {

		char buffer[100];
		close_descriptors();
		args[0] = BINDIR"/gbnserver";
		args[1] = "--port";
		snprintf(buffer, sizeof(buffer)-1, "%d", port); 
		buffer[ sizeof(buffer)-1 ] = 0;
		args[2] = buffer;
		args[3] = NULL;

		if( execvp(args[0], args) < 0) {
			fprintf(stderr,"Launching server failed. Does the file '%s' exists ?\n",args[0]);
			perror("exe:");

			/* This saves a crash */
			args[0] = "/bin/true";
			args[1] = NULL;
			execv(args[0],args);
			exit(1);
		}

		return BATNAV_STATUS_ERROR;
	} else {
		/* wait util server is launched */
		sleep(2);
	}
	return BATNAV_STATUS_SUCCESS;
}

static BATNAV_STATUS conectar_real()
{
	if( send_connect() == 0 ) {
		return BATNAV_STATUS_SUCCESS;
	}
	return BATNAV_STATUS_ERROR;
}

static gint conectar_button_con_cb(void)
{
	strncpy(usuario.nombre,gtk_entry_get_text(GTK_ENTRY(con_entry_name)),sizeof(usuario.nombre)-1);
	usuario.nombre[sizeof(usuario.nombre)-1]=0;
	strncpy(usuario.server,gtk_entry_get_text(GTK_ENTRY(con_entry_server)),sizeof(usuario.server)-1);
	usuario.server[sizeof(usuario.server)-1]=0;
	usuario.port = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(con_spinner_port));


	/* if you checked "start local server" we'll copy "localhost" to g_juego.sername */
	if( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button_launch)) ) {
		launch_server( usuario.port );
	}

	g_settings_delay(settings);
	g_settings_set_uint(settings, "port", usuario.port);
	g_settings_set_string(settings, "servername", usuario.server);
	g_settings_set_string(settings, "playername", usuario.nombre);
	g_settings_apply(settings);

	conectar_real();

	return FALSE;
}

/*
 * Called when something has changed on the properybox
 */
static void
prop_changed (GtkWidget *w, void *n)
{
	if( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button_launch)) )
		gtk_entry_set_text( GTK_ENTRY( con_entry_server ), "localhost");
}

/**
 * @fn void conectar_view()
 * Muestra la ventana de coneccion
 */
void conectar_view()
{
	GtkWidget *label;
	GtkWidget *table;
	GtkWidget *frame;
        GtkAdjustment *adj;
        gint response;

	if( usuario.with_ggz ) {
		conectar_real();
		return;
	}

	conectar_window = teg_dialog_new(_("Connect to server"),_("Connect to server")); 
	gtk_dialog_add_buttons(GTK_DIALOG(conectar_window),
                               _("_OK"), GTK_RESPONSE_ACCEPT,
                               _("_Cancel"), GTK_RESPONSE_REJECT,
		NULL );
	gtk_dialog_set_default_response(GTK_DIALOG(conectar_window),
	                                GTK_RESPONSE_ACCEPT);

	/* server options */
	table = gtk_grid_new ();
	gtk_container_set_border_width (GTK_CONTAINER (table), 4);
	gtk_grid_set_row_spacing (GTK_GRID (table), 4);
	gtk_grid_set_column_spacing (GTK_GRID (table), 4);

	frame = gtk_frame_new (_("Server Options"));
	gtk_container_set_border_width (GTK_CONTAINER (frame), 1);
	gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area
	                                (GTK_DIALOG(conectar_window))), frame);

	/* server port */
	label = gtk_label_new(_("Server port:"));
	gtk_grid_attach( GTK_GRID(table), label, 0, 0, 1, 1 );
	adj = (GtkAdjustment *) gtk_adjustment_new( usuario.port, 1.0, 65536.0, 1.0, 5.0, 1.0 );
	con_spinner_port = gtk_spin_button_new( adj, 0.0, 0);
	gtk_grid_attach( GTK_GRID(table), con_spinner_port, 1, 0, 1, 1);

	/* server name */
	label = gtk_label_new(_("Server name:"));
	gtk_grid_attach( GTK_GRID(table), label, 0, 1, 1, 1);
	con_entry_server = gtk_entry_new( );
	gtk_entry_set_text( GTK_ENTRY( con_entry_server ), usuario.server);
	gtk_grid_attach( GTK_GRID(table), con_entry_server, 1, 1, 1, 1);
	

	/* player name */
	label = gtk_label_new(_("Name:"));
	gtk_grid_attach( GTK_GRID(table), label, 0, 2, 1, 1);
	con_entry_name = gtk_entry_new( );
	gtk_entry_set_text( GTK_ENTRY( con_entry_name ), usuario.nombre);
	gtk_grid_attach( GTK_GRID(table), con_entry_name, 1, 2, 1, 1);


	gtk_container_add(GTK_CONTAINER(frame), table);

	/* launch localhost server */
	button_launch = gtk_check_button_new_with_label(_("Start server locally"));
	gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area
	                                (GTK_DIALOG(conectar_window))),
	                  button_launch);
	g_signal_connect (G_OBJECT (button_launch), "toggled",
	    	G_CALLBACK (prop_changed), NULL);


	/* fin */

	gtk_widget_show (GTK_WIDGET(table));
	gtk_widget_show_all(conectar_window);
	response = gtk_dialog_run(GTK_DIALOG(conectar_window));
	if(response == GTK_RESPONSE_ACCEPT)
	        conectar_button_con_cb();

	gtk_widget_destroy(conectar_window);
}