aboutsummaryrefslogtreecommitdiff
path: root/plugins/dfnet/gui.c
blob: 60e165a79489378a8d5fa9af24f910023fdfc7d4 (plain)
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
//
// DF Netplay Plugin
//
// Based on netSock 0.2 by linuzappz.
// The Plugin is free source code.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <signal.h>

#include "cfg.c"

void cfgSysMessage(const char *fmt, ...) {
	GtkWidget *MsgDlg;
	va_list list;
	char msg[512];

	va_start(list, fmt);
	vsprintf(msg, fmt, list);
	va_end(list);

	if (msg[strlen(msg) - 1] == '\n') msg[strlen(msg) - 1] = 0;

	MsgDlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
		GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, _("NetPlay"));
	gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(MsgDlg), "%s", msg);

	gtk_dialog_run(GTK_DIALOG(MsgDlg));
	gtk_widget_destroy(MsgDlg);
}

void CFGconfigure() {
	cfgSysMessage(_("Nothing to configure"));
}

#ifdef __linux__

#include <sys/ioctl.h>
#include <linux/if.h>

#define MAXINTERFACES 16

void sockGetIP(char *IPAddress) {
	int fd, intrface;
	struct ifreq buf[MAXINTERFACES];
	struct ifconf ifc;
	struct sockaddr_in addr;

	strcpy(IPAddress, "127.0.0.1");

	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
		ifc.ifc_len = sizeof(buf);
		ifc.ifc_buf = (caddr_t)buf;
		if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc)) {
			intrface = ifc.ifc_len / sizeof(struct ifreq);
			while (intrface-- > 0) {
				if (!(ioctl(fd, SIOCGIFADDR, (char *)&buf[intrface]))) {
					memcpy(&addr, &(buf[intrface].ifr_addr), sizeof(addr));
					strcpy(IPAddress, inet_ntoa(addr.sin_addr));
					break;
				}
			}
		}
		close(fd);
	}
}

#else

void sockGetIP(char *IPAddress) {
	struct hostent *host;
	char str[256];

	gethostname(str, 256);
	host = gethostbyname(str);

	if (host != NULL)
		strcpy(IPAddress, inet_ntoa(*((struct in_addr *)host->h_addr_list[0])));
	else strcpy(IPAddress, "127.0.0.1");
}

#endif

void OnCopyIP(GtkWidget *widget, gpointer user_data) {
	char str[256];

	sockGetIP(str);
	gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), str, strlen(str));
	cfgSysMessage(_("IP %s"), str);
}

long CFGopen() {
	GladeXML *xml;
	GtkWidget *widget, *MainWindow;
	char buf[256];

	LoadConf();

	xml = glade_xml_new(DATADIR "dfnet.glade2", "dlgStart", NULL);
	if (xml == NULL) {
		g_warning("We could not load the interface!");
		return 0;
	}

	MainWindow = glade_xml_get_widget(xml, "dlgStart");
	gtk_window_set_title(GTK_WINDOW(MainWindow), _("NetPlay"));

	widget = glade_xml_get_widget(xml, "btnCopyIP");
	g_signal_connect_data(GTK_OBJECT(widget), "clicked",
		GTK_SIGNAL_FUNC(OnCopyIP), NULL, NULL, G_CONNECT_AFTER);

	widget = glade_xml_get_widget(xml, "tbServerIP");
	gtk_entry_set_text(GTK_ENTRY(widget), conf.ipAddress);

	widget = glade_xml_get_widget(xml, "tbPort");
	sprintf(buf, "%d", conf.PortNum);
	gtk_entry_set_text(GTK_ENTRY(widget), buf);

	if (conf.PlayerNum == 1) {
		widget = glade_xml_get_widget(xml, "rbServer");
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
	} else {
		widget = glade_xml_get_widget(xml, "rbClient");
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
	}

	if (gtk_dialog_run(GTK_DIALOG(MainWindow)) == GTK_RESPONSE_OK) {
		widget = glade_xml_get_widget(xml, "tbServerIP");
		strcpy(conf.ipAddress, gtk_entry_get_text(GTK_ENTRY(widget)));

		widget = glade_xml_get_widget(xml, "tbPort");
		conf.PortNum = atoi(gtk_entry_get_text(GTK_ENTRY(widget)));

		widget = glade_xml_get_widget(xml, "rbServer");
		if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
			conf.PlayerNum = 1;
		} else {
			conf.PlayerNum = 2;
		}

		SaveConf();
		gtk_widget_destroy(MainWindow);
		return 1;
	}

	gtk_widget_destroy(MainWindow);

	return 0;
}

void OnWaitDialog_Abort() {
	kill(getppid(), SIGUSR2);
}

void CFGwait() {
	GtkWidget *WaitDlg;

	WaitDlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO,
		GTK_BUTTONS_CANCEL, _("Waiting for connection..."));

	gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(WaitDlg),
		_("The Client should now Start a Connection, waiting..."));

	gtk_dialog_run(GTK_DIALOG(WaitDlg));
	gtk_widget_destroy(WaitDlg);

	OnWaitDialog_Abort();
}

long CFGpause() {
	return 0;
}

void CFGabout() {
	const char *authors[]= {"linuzappz <linuzappz@hotmail.com>", "Wei Mingzhi <whistler_wmz@users.sf.net>", NULL};
	GtkWidget *widget;

	widget = gtk_about_dialog_new();
	gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(widget), "Socket NetPlay Driver");
	gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(widget), "0.21");
	gtk_about_dialog_set_authors(GTK_ABOUT_DIALOG(widget), authors);
	gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(widget), "http://www.codeplex.com/pcsxr/");

	gtk_dialog_run(GTK_DIALOG(widget));
	gtk_widget_destroy(widget);
}

long CFGmessage(char *args[], int num) {
	char msg[512];

	memset(msg, 0, sizeof(msg));
	while (num) {
		strcat(msg, *args); strcat(msg, " ");
		num--; args++;
	}
	cfgSysMessage(msg);

	return 0;
}

int main(int argc, char *argv[]) {
#ifdef ENABLE_NLS
	setlocale(LC_ALL, "");
	bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
	textdomain(GETTEXT_PACKAGE);
#endif

	gtk_set_locale();
	gtk_init(&argc, &argv);

	if (!strcmp(argv[1], "configure")) {
		CFGconfigure();
	} else if (!strcmp(argv[1], "open")) {
		return CFGopen();
	} else if (!strcmp(argv[1], "wait")) {
		CFGwait();
	} else if (!strcmp(argv[1], "pause")) {
		return CFGpause();
	} else if (!strcmp(argv[1], "about")) {
		CFGabout();
	} else if (!strcmp(argv[1], "message")) {
		CFGmessage(&argv[2], argc - 2);
	}

	return 0;
}