Commit | Line | Data |
---|---|---|
ef79bbde P |
1 | // |
2 | // DF Netplay Plugin | |
3 | // | |
4 | // Based on netSock 0.2 by linuzappz. | |
5 | // The Plugin is free source code. | |
6 | // | |
7 | ||
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <string.h> | |
11 | ||
12 | #include <gdk/gdk.h> | |
13 | #include <gtk/gtk.h> | |
14 | #include <glade/glade.h> | |
15 | #include <signal.h> | |
16 | ||
17 | #include "cfg.c" | |
18 | ||
19 | void cfgSysMessage(const char *fmt, ...) { | |
20 | GtkWidget *MsgDlg; | |
21 | va_list list; | |
22 | char msg[512]; | |
23 | ||
24 | va_start(list, fmt); | |
25 | vsprintf(msg, fmt, list); | |
26 | va_end(list); | |
27 | ||
28 | if (msg[strlen(msg) - 1] == '\n') msg[strlen(msg) - 1] = 0; | |
29 | ||
30 | MsgDlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, | |
31 | GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, _("NetPlay")); | |
32 | gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(MsgDlg), "%s", msg); | |
33 | ||
34 | gtk_dialog_run(GTK_DIALOG(MsgDlg)); | |
35 | gtk_widget_destroy(MsgDlg); | |
36 | } | |
37 | ||
38 | void CFGconfigure() { | |
39 | cfgSysMessage(_("Nothing to configure")); | |
40 | } | |
41 | ||
42 | #ifdef __linux__ | |
43 | ||
44 | #include <sys/ioctl.h> | |
45 | #include <linux/if.h> | |
46 | ||
47 | #define MAXINTERFACES 16 | |
48 | ||
49 | void sockGetIP(char *IPAddress) { | |
50 | int fd, intrface; | |
51 | struct ifreq buf[MAXINTERFACES]; | |
52 | struct ifconf ifc; | |
53 | struct sockaddr_in addr; | |
54 | ||
55 | strcpy(IPAddress, "127.0.0.1"); | |
56 | ||
57 | if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) { | |
58 | ifc.ifc_len = sizeof(buf); | |
59 | ifc.ifc_buf = (caddr_t)buf; | |
60 | if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc)) { | |
61 | intrface = ifc.ifc_len / sizeof(struct ifreq); | |
62 | while (intrface-- > 0) { | |
63 | if (!(ioctl(fd, SIOCGIFADDR, (char *)&buf[intrface]))) { | |
64 | memcpy(&addr, &(buf[intrface].ifr_addr), sizeof(addr)); | |
65 | strcpy(IPAddress, inet_ntoa(addr.sin_addr)); | |
66 | break; | |
67 | } | |
68 | } | |
69 | } | |
70 | close(fd); | |
71 | } | |
72 | } | |
73 | ||
74 | #else | |
75 | ||
76 | void sockGetIP(char *IPAddress) { | |
77 | struct hostent *host; | |
78 | char str[256]; | |
79 | ||
80 | gethostname(str, 256); | |
81 | host = gethostbyname(str); | |
82 | ||
83 | if (host != NULL) | |
84 | strcpy(IPAddress, inet_ntoa(*((struct in_addr *)host->h_addr_list[0]))); | |
85 | else strcpy(IPAddress, "127.0.0.1"); | |
86 | } | |
87 | ||
88 | #endif | |
89 | ||
90 | void OnCopyIP(GtkWidget *widget, gpointer user_data) { | |
91 | char str[256]; | |
92 | ||
93 | sockGetIP(str); | |
94 | gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), str, strlen(str)); | |
95 | cfgSysMessage(_("IP %s"), str); | |
96 | } | |
97 | ||
98 | long CFGopen() { | |
99 | GladeXML *xml; | |
100 | GtkWidget *widget, *MainWindow; | |
101 | char buf[256]; | |
102 | ||
103 | LoadConf(); | |
104 | ||
105 | xml = glade_xml_new(DATADIR "dfnet.glade2", "dlgStart", NULL); | |
106 | if (xml == NULL) { | |
107 | g_warning("We could not load the interface!"); | |
108 | return 0; | |
109 | } | |
110 | ||
111 | MainWindow = glade_xml_get_widget(xml, "dlgStart"); | |
112 | gtk_window_set_title(GTK_WINDOW(MainWindow), _("NetPlay")); | |
113 | ||
114 | widget = glade_xml_get_widget(xml, "btnCopyIP"); | |
115 | g_signal_connect_data(GTK_OBJECT(widget), "clicked", | |
116 | GTK_SIGNAL_FUNC(OnCopyIP), NULL, NULL, G_CONNECT_AFTER); | |
117 | ||
118 | widget = glade_xml_get_widget(xml, "tbServerIP"); | |
119 | gtk_entry_set_text(GTK_ENTRY(widget), conf.ipAddress); | |
120 | ||
121 | widget = glade_xml_get_widget(xml, "tbPort"); | |
122 | sprintf(buf, "%d", conf.PortNum); | |
123 | gtk_entry_set_text(GTK_ENTRY(widget), buf); | |
124 | ||
125 | if (conf.PlayerNum == 1) { | |
126 | widget = glade_xml_get_widget(xml, "rbServer"); | |
127 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE); | |
128 | } else { | |
129 | widget = glade_xml_get_widget(xml, "rbClient"); | |
130 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE); | |
131 | } | |
132 | ||
133 | if (gtk_dialog_run(GTK_DIALOG(MainWindow)) == GTK_RESPONSE_OK) { | |
134 | widget = glade_xml_get_widget(xml, "tbServerIP"); | |
135 | strcpy(conf.ipAddress, gtk_entry_get_text(GTK_ENTRY(widget))); | |
136 | ||
137 | widget = glade_xml_get_widget(xml, "tbPort"); | |
138 | conf.PortNum = atoi(gtk_entry_get_text(GTK_ENTRY(widget))); | |
139 | ||
140 | widget = glade_xml_get_widget(xml, "rbServer"); | |
141 | if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) { | |
142 | conf.PlayerNum = 1; | |
143 | } else { | |
144 | conf.PlayerNum = 2; | |
145 | } | |
146 | ||
147 | SaveConf(); | |
148 | gtk_widget_destroy(MainWindow); | |
149 | return 1; | |
150 | } | |
151 | ||
152 | gtk_widget_destroy(MainWindow); | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
157 | void OnWaitDialog_Abort() { | |
158 | kill(getppid(), SIGUSR2); | |
159 | } | |
160 | ||
161 | void CFGwait() { | |
162 | GtkWidget *WaitDlg; | |
163 | ||
164 | WaitDlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, | |
165 | GTK_BUTTONS_CANCEL, _("Waiting for connection...")); | |
166 | ||
167 | gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(WaitDlg), | |
168 | _("The Client should now Start a Connection, waiting...")); | |
169 | ||
170 | gtk_dialog_run(GTK_DIALOG(WaitDlg)); | |
171 | gtk_widget_destroy(WaitDlg); | |
172 | ||
173 | OnWaitDialog_Abort(); | |
174 | } | |
175 | ||
176 | long CFGpause() { | |
177 | return 0; | |
178 | } | |
179 | ||
180 | void CFGabout() { | |
181 | const char *authors[]= {"linuzappz <linuzappz@hotmail.com>", "Wei Mingzhi <whistler_wmz@users.sf.net>", NULL}; | |
182 | GtkWidget *widget; | |
183 | ||
184 | widget = gtk_about_dialog_new(); | |
185 | gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(widget), "Socket NetPlay Driver"); | |
186 | gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(widget), "0.21"); | |
187 | gtk_about_dialog_set_authors(GTK_ABOUT_DIALOG(widget), authors); | |
188 | gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(widget), "http://www.codeplex.com/pcsxr/"); | |
189 | ||
190 | gtk_dialog_run(GTK_DIALOG(widget)); | |
191 | gtk_widget_destroy(widget); | |
192 | } | |
193 | ||
194 | long CFGmessage(char *args[], int num) { | |
195 | char msg[512]; | |
196 | ||
197 | memset(msg, 0, sizeof(msg)); | |
198 | while (num) { | |
199 | strcat(msg, *args); strcat(msg, " "); | |
200 | num--; args++; | |
201 | } | |
202 | cfgSysMessage(msg); | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
207 | int main(int argc, char *argv[]) { | |
208 | #ifdef ENABLE_NLS | |
209 | setlocale(LC_ALL, ""); | |
210 | bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR); | |
211 | bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); | |
212 | textdomain(GETTEXT_PACKAGE); | |
213 | #endif | |
214 | ||
215 | gtk_set_locale(); | |
216 | gtk_init(&argc, &argv); | |
217 | ||
218 | if (!strcmp(argv[1], "configure")) { | |
219 | CFGconfigure(); | |
220 | } else if (!strcmp(argv[1], "open")) { | |
221 | return CFGopen(); | |
222 | } else if (!strcmp(argv[1], "wait")) { | |
223 | CFGwait(); | |
224 | } else if (!strcmp(argv[1], "pause")) { | |
225 | return CFGpause(); | |
226 | } else if (!strcmp(argv[1], "about")) { | |
227 | CFGabout(); | |
228 | } else if (!strcmp(argv[1], "message")) { | |
229 | CFGmessage(&argv[2], argc - 2); | |
230 | } | |
231 | ||
232 | return 0; | |
233 | } |