tGraphical server added - vaccinewars - be a doctor and try to vaccinate the world
git clone git://src.adamsgaard.dk/vaccinewars
Log
Files
Refs
README
LICENSE
---
commit 118b9d08742be23909ab43e0016cdbec8517152c
parent 77dac8e5d09edd00cafc3553dc81f837d13f6887
Author: Ben Webb 
Date:   Sat, 28 Apr 2001 17:48:05 +0000

Graphical server added


Diffstat:
  M src/dopewars.h                      |       1 +
  M src/serverside.c                    |     178 ++++++++++++++++++++++++++++++-
  M src/serverside.h                    |       5 ++++-
  M src/winmain.c                       |       5 +++++

4 files changed, 186 insertions(+), 3 deletions(-)
---
diff --git a/src/dopewars.h b/src/dopewars.h
t@@ -295,6 +295,7 @@ struct PLAYER_T {
    Player *OnBehalfOf;
    ConnBuf ReadBuf,WriteBuf;
    Abilities Abil;
+   gint InputTag;
    GPtrArray *FightArray; /* If non-NULL, a list of players in a fight */
    Player *Attacking;     /* The player that this player is attacking */
    gint CopIndex;  /* if >0,  then this player is a cop, described
diff --git a/src/serverside.c b/src/serverside.c
t@@ -44,6 +44,10 @@
 #include 
 #endif
 
+#ifdef GUI_SERVER
+#include "gtkport.h"
+#endif
+
 #ifndef SD_SEND
 #define SD_SEND 1
 #endif
t@@ -488,11 +492,13 @@ void ServerHelp() {
       } else {
          g_string_assign(VarName,Globals[i].Name);
       }
-      g_print("%-26s %s\n",VarName->str,_(Globals[i].Help));
+      g_print("%-26s\t%s\n",VarName->str,_(Globals[i].Help));
       Lines++;
+#ifndef GUI_SERVER
       if (Lines%24==0) {
          g_print(_("--More--")); bgetch(); g_print("\n");
       }
+#endif
    }
    g_string_free(VarName,TRUE);
 #else
t@@ -566,7 +572,9 @@ gboolean ReadServerKey(GString *LineBuf,gboolean *EndOfLine) {
 
 void StartServer() {
    struct sockaddr_in ServerAddr;
+#ifndef CYGWIN
    struct sigaction sact;
+#endif
 
    Scanner=g_scanner_new(&ScannerConfig);
    Scanner->input_name="(stdin)";
t@@ -686,7 +694,7 @@ gboolean HandleServerCommand(char *string) {
    return FALSE;
 }
 
-void HandleNewConnection() {
+Player *HandleNewConnection() {
    int cadsize;
    int ClientSock;
    struct sockaddr_in ClientAddr;
t@@ -703,6 +711,7 @@ void HandleNewConnection() {
    if (ConnectTimeout) {
       tmp->ConnectTimeout=time(NULL)+(time_t)ConnectTimeout;
    }
+   return tmp;
 }
 
 void StopServer() {
t@@ -825,6 +834,171 @@ void ServerLoop() {
    StopServer();
    g_string_free(LineBuf,TRUE);
 }
+
+#ifdef GUI_SERVER
+static GtkWidget *TextOutput;
+static gint ListenTag=0;
+static void SetSocketWriteTest(Player *Play,gboolean WriteTest);
+static void GuiSetTimeouts();
+static time_t NextTimeout=0;
+static guint TimeoutTag=-1;
+
+static gint GuiDoTimeouts(gpointer data) {
+/* Forget the TimeoutTag so that GuiSetTimeouts doesn't delete it - it'll be
+   deleted automatically anyway when we return FALSE */
+   TimeoutTag=-1;
+   NextTimeout=0;
+
+   FirstServer=HandleTimeouts(FirstServer);
+   GuiSetTimeouts();
+   return FALSE;
+}
+
+void GuiSetTimeouts() {
+   int MinTimeout;
+   time_t TimeNow;
+   TimeNow=time(NULL);
+   MinTimeout=GetMinimumTimeout(FirstServer);
+   if (TimeNow+MinTimeout < NextTimeout || NextTimeout0) {
+         TimeoutTag=gtk_timeout_add(MinTimeout*1000,GuiDoTimeouts,NULL);
+         NextTimeout=TimeNow+MinTimeout;
+      }
+   }
+}
+
+static void GuiServerPrintFunc(const gchar *string) {
+   gint EditPos;
+   
+   gtk_text_freeze(GTK_TEXT(TextOutput));
+   EditPos=gtk_text_get_length(GTK_TEXT(TextOutput));
+   gtk_editable_insert_text(GTK_EDITABLE(TextOutput),string,strlen(string),
+                            &EditPos);
+   gtk_text_thaw(GTK_TEXT(TextOutput));
+   gtk_editable_set_position(GTK_EDITABLE(TextOutput),EditPos);
+}
+
+static void GuiServerLogMessage(const gchar *log_domain,
+                                GLogLevelFlags log_level,const gchar *message,
+                                gpointer user_data) {
+   gchar *text;
+   text = g_strdup_printf("Message: %s\n",message);
+   GuiServerPrintFunc(text);
+   g_free(text);
+}
+
+static void GuiQuitServer() {
+   gtk_main_quit();
+   StopServer();
+}
+
+static void GuiDoCommand(GtkWidget *widget,gpointer data) {
+   gchar *text;
+   gboolean retval;
+   text=gtk_editable_get_chars(GTK_EDITABLE(widget),0,-1);
+   gtk_editable_delete_text(GTK_EDITABLE(widget),0,-1);
+   retval=HandleServerCommand(text);
+   g_free(text);
+   if (retval) GuiQuitServer();
+}
+
+static void GuiHandleSocket(gpointer data,gint socket,
+                            GdkInputCondition condition) {
+   Player *Play;
+   Play = (Player *)data;
+
+   /* Sanity check - is the player still around? */
+   if (!g_slist_find(FirstServer,(gpointer)Play)) return;
+
+   if (condition&GDK_INPUT_WRITE) {
+      if (!WriteConnectionBufferToWire(Play)) {
+         if (RemovePlayerFromServer(Play,WantQuit)) GuiQuitServer();
+      } else if (Play->WriteBuf.DataPresent==0) {
+         SetSocketWriteTest(Play,FALSE);
+      }
+   }
+   if (condition&GDK_INPUT_READ) {
+      if (!ReadConnectionBufferFromWire(Play)) {
+         if (RemovePlayerFromServer(Play,WantQuit)) GuiQuitServer();
+      } else {
+         HandleServerPlayer(Play);
+         GuiSetTimeouts();  /* We may have set some new timeouts */
+      }
+   }
+}
+
+void SetSocketWriteTest(Player *Play,gboolean WriteTest) {
+   if (Play->InputTag) gdk_input_remove(Play->InputTag);
+   Play->InputTag=gdk_input_add(Play->fd,
+                      GDK_INPUT_READ|(WriteTest ? GDK_INPUT_WRITE : 0),
+                      GuiHandleSocket,(gpointer)Play);
+}
+
+static void GuiNewConnect(gpointer data,gint socket,
+                          GdkInputCondition condition) {
+   Player *Play;
+   if (condition&GDK_INPUT_READ) {
+      Play=HandleNewConnection();
+      Play->InputTag=0;
+      SetSocketWriteTest(Play,TRUE);
+   }
+}
+
+static gboolean TriedPoliteShutdown=FALSE;
+
+static gint GuiRequestDelete(GtkWidget *widget,GdkEvent *event,gpointer data) {
+   if (TriedPoliteShutdown) {
+      GuiQuitServer();
+   } else {
+      TriedPoliteShutdown=TRUE;
+      if (HandleServerCommand("quit")) GuiQuitServer();
+   }
+   return TRUE; /* Never allow automatic deletion - we handle it manually */
+}
+
+void GuiServerLoop() {
+   GtkWidget *window,*text,*hbox,*vbox,*entry,*label;
+   GtkAdjustment *adj;
+
+   window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
+   gtk_signal_connect(GTK_OBJECT(window),"delete_event",
+                      GTK_SIGNAL_FUNC(GuiRequestDelete),NULL);
+   gtk_window_set_default_size(GTK_WINDOW(window),500,250);
+   gtk_window_set_title(GTK_WINDOW(window),_("dopewars server"));
+   gtk_container_set_border_width(GTK_CONTAINER(window),7);
+
+   vbox=gtk_vbox_new(FALSE,7);
+   adj=(GtkAdjustment *)gtk_adjustment_new(0,0,100,1,10,10);
+   TextOutput=text=gtk_scrolled_text_new(NULL,adj,&hbox);
+   gtk_text_set_editable(GTK_TEXT(text),FALSE);
+   gtk_text_set_word_wrap(GTK_TEXT(text),TRUE);
+   gtk_box_pack_start(GTK_BOX(vbox),hbox,TRUE,TRUE,0);
+
+   hbox=gtk_hbox_new(FALSE,4);
+   label=gtk_label_new(_("Command:"));
+   gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,0);
+   entry=gtk_entry_new();
+   gtk_signal_connect(GTK_OBJECT(entry),"activate",
+                      GTK_SIGNAL_FUNC(GuiDoCommand),NULL);
+   gtk_box_pack_start(GTK_BOX(hbox),entry,TRUE,TRUE,0);
+   gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,0);
+
+   gtk_container_add(GTK_CONTAINER(window),vbox);
+   gtk_widget_show_all(window);
+
+   g_set_print_handler(GuiServerPrintFunc);
+   g_log_set_handler(NULL,G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_WARNING,
+                     GuiServerLogMessage,NULL);
+   StartServer();
+
+   SocketWriteTestPt = SetSocketWriteTest;
+   ListenTag=gdk_input_add(ListenSock,GDK_INPUT_READ,GuiNewConnect,NULL);
+   gtk_main();
+}
+#endif /* GUI_SERVER */
+
 #endif /* NETWORKING */
 
 void FinishGame(Player *Play,char *Message) {
diff --git a/src/serverside.h b/src/serverside.h
t@@ -44,7 +44,7 @@ void ClientLeftServer(Player *Play);
 void StartServer();
 void StopServer();
 gboolean HandleServerCommand(char *string);
-void HandleNewConnection();
+Player *HandleNewConnection();
 void ServerLoop();
 void HandleServerPlayer(Player *Play);
 void HandleServerMessage(gchar *buf,Player *ReallyFrom);
t@@ -77,5 +77,8 @@ void RunFromCombat(Player *Play,int ToLocation);
 gboolean CanPlayerFire(Player *Play);
 gboolean CanRunHere(Player *Play);
 Player *GetNextShooter(Player *Play);
+#ifdef GUI_SERVER
+void GuiServerLoop();
+#endif
 
 #endif
diff --git a/src/winmain.c b/src/winmain.c
t@@ -119,6 +119,10 @@ int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
       } else {
          StartNetworking();
          if (Server) {
+#ifdef GUI_SERVER
+            win32_init(hInstance,hPrevInstance);
+            GuiServerLoop();
+#else
             AllocConsole();
             SetConsoleTitle(_("dopewars server"));
             g_log_set_handler(NULL,G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_WARNING,
t@@ -126,6 +130,7 @@ int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
             g_set_print_handler(ServerPrintFunc);
             newterm(NULL,NULL,NULL);
             ServerLoop();
+#endif
          } else if (AIPlayer) {
             AllocConsole();
             SetConsoleTitle(_("dopewars AI"));