OSDN Git Service

replace realm_subinfo's char[n] to cptr
[hengband/hengband.git] / src / chuukei.c
index 8802a57..fdbd00d 100644 (file)
@@ -2,12 +2,14 @@
 
 #include "angband.h"
 
-#ifdef CHUUKEI
-
 #include <stdio.h>
 #include <stdarg.h>
 #include <ctype.h>
+#ifdef WINDOWS
+#include <windows.h>
+#endif
 
+#ifdef CHUUKEI
 #if defined(WINDOWS)
 #include <winsock.h>
 #elif defined(MACINTOSH)
 #endif
 
 #define MAX_HOSTNAME 256
+#endif
+
 #define RINGBUF_SIZE 1024*1024
 #define FRESH_QUEUE_SIZE 4096
+#ifdef WINDOWS
+#define WAIT 100
+#else
 #define WAIT 100*1000 /* ¥Ö¥é¥¦¥ºÂ¦¤Î¥¦¥¨¥¤¥È(usñ°Ì) */
+#endif
 #define DEFAULT_DELAY 50
 #define RECVBUF_SIZE 1024
 
@@ -36,10 +44,15 @@ static int sd; /* 
 static long epoch_time;  /* ¥Ð¥Ã¥Õ¥¡³«»Ï»þ¹ï */
 static long time_diff;   /* ¥×¥ì¥¤Â¦¤È¤Î»þ´Ö¤Î¤º¤ì(¤³¤ì¤ò¸«¤Ê¤¬¤é¥Ç¥£¥ì¥¤¤òÄ´À°¤·¤Æ¤¤¤¯) */
 static int browse_delay; /* É½¼¨¤¹¤ë¤Þ¤Ç¤Î»þ´Ö(100msñ°Ì)(¤³¤Î´Ö¤Ë¥é¥°¤òµÛ¼ý¤¹¤ë) */
+#ifdef CHUUKEI
 static int server_port;
 static char server_name[MAX_HOSTNAME];
+#endif
 
+static int movie_fd;
+static int movie_mode;
 
+#ifdef CHUUKEI
 #ifdef WINDOWS
 #define close closesocket
 #endif
@@ -48,7 +61,7 @@ static char server_name[MAX_HOSTNAME];
 static InetSvcRef inet_services = nil;
 static EndpointRef ep                  = kOTInvalidEndpointRef;
 #endif
-
+#endif
 /* ÉÁ²è¤¹¤ë»þ¹ï¤ò³Ð¤¨¤Æ¤ª¤¯¥­¥å¡¼¹½Â¤ÂΠ*/
 static struct
 {
@@ -87,15 +100,12 @@ static errr (*old_bigcurs_hook)(int x, int y);
 static errr (*old_wipe_hook)(int x, int y, int n);
 static errr (*old_text_hook)(int x, int y, int n, byte a, cptr s);
 
-
 static void disable_chuukei_server(void)
 {
        term *t = angband_term[0];
-
-       if (!chuukei_server) return;
-
+#ifdef CHUUKEI
        chuukei_server = FALSE;
-
+#endif /* CHUUKEI */
        t->xtra_hook = old_xtra_hook;
        t->curs_hook = old_curs_hook;
        t->bigcurs_hook = old_bigcurs_hook;
@@ -103,9 +113,8 @@ static void disable_chuukei_server(void)
        t->text_hook = old_text_hook;
 }
 
-
 /* ANSI C¤Ë¤è¤ì¤ÐstaticÊÑ¿ô¤Ï0¤Ç½é´ü²½¤µ¤ì¤ë¤¬°ì±þ½é´ü²½¤¹¤ë */
-static errr init_chuukei(void)
+static errr init_buffer(void)
 {
        fresh_queue.next = fresh_queue.tail = 0;
        ring.wptr = ring.rptr = ring.inlen = 0;
@@ -138,9 +147,20 @@ static errr insert_ringbuf(char *buf)
        int len;
        len = strlen(buf) + 1; /* +1¤Ï½ªÃ¼Ê¸»úʬ */
 
+       if (movie_mode)
+       {
+               fd_write(movie_fd, buf, len);
+#ifdef CHUUKEI
+               if (!chuukei_server) return 0;
+#else
+               return 0;
+#endif
+       }
+
        /* ¥Ð¥Ã¥Õ¥¡¤ò¥ª¡¼¥Ð¡¼ */
        if (ring.inlen + len >= RINGBUF_SIZE)
        {
+#ifdef CHUUKEI
                if (chuukei_server) disable_chuukei_server();
                else chuukei_client = FALSE;
 
@@ -148,7 +168,7 @@ static errr insert_ringbuf(char *buf)
                inkey();
 
                close(sd);
-
+#endif
                return (-1);
        }
 
@@ -175,6 +195,7 @@ static errr insert_ringbuf(char *buf)
        return (0);
 }
 
+#ifdef CHUUKEI
 void flush_ringbuf(void)
 {
 #ifndef MACINTOSH
@@ -209,7 +230,7 @@ void flush_ringbuf(void)
                if (result <= 0)
                {
                        /* ¥µ¡¼¥Ð¤È¤ÎÀܳÃÇ¡© */
-                       disable_chuukei_server();
+                       if (chuukei_server) disable_chuukei_server();
 
                        prt("¥µ¡¼¥Ð¤È¤ÎÀܳ¤¬ÀÚÃǤµ¤ì¤Þ¤·¤¿¡£", 0, 0);
                        inkey();
@@ -239,7 +260,7 @@ void flush_ringbuf(void)
                if (result <= 0)
                {
                        /* ¥µ¡¼¥Ð¤È¤ÎÀܳÃÇ¡© */
-                       disable_chuukei_server();
+                       if (chuukei_server) disable_chuukei_server();
 
                        prt("¥µ¡¼¥Ð¤È¤ÎÀܳ¤¬ÀÚÃǤµ¤ì¤Þ¤·¤¿¡£", 0, 0);
                        inkey();
@@ -257,6 +278,7 @@ void flush_ringbuf(void)
 #endif
 }
 
+
 static int read_chuukei_prf(cptr prf_name)
 {
        char buf[1024];
@@ -320,7 +342,7 @@ int connect_chuukei_server(char *prf_name)
                return (-1);
        }
 
-       if (init_chuukei() < 0)
+       if (init_buffer() < 0)
        {
                printf("Malloc error\n");
                return (-1);
@@ -385,7 +407,7 @@ int connect_chuukei_server(char *prf_name)
                return (-1);
        }
        
-       init_chuukei();
+       init_buffer();
        
        printf("server = %s\nport = %d\n", server_name, server_port);
 
@@ -469,7 +491,7 @@ int connect_chuukei_server(char *prf_name)
 
 #endif
 }
-
+#endif /* CHUUKEI */
 
 /* str¤¬Æ±¤¸Ê¸»ú¤Î·«¤êÊÖ¤·¤«¤É¤¦¤«Ä´¤Ù¤ë */
 static bool string_is_repeat(char *str, int len)
@@ -508,7 +530,11 @@ static errr send_text_to_chuukei_server(int x, int y, int len, byte col, cptr st
        }
        else if(string_is_repeat(buf2, len))
        {
-               sprintf(buf, "n%c%c%c%c%c", x+1, y+1, len, col, buf2[0]);
+               int i;
+               for (i = len; i > 0; i -= 127)
+               {
+                       sprintf(buf, "n%c%c%c%c%c", x+1, y+1, MIN(i, 127), col, buf2[0]);
+               }
        }
        else
        {
@@ -603,6 +629,91 @@ void prepare_chuukei_hooks(void)
 }
 
 
+/*
+ * Prepare z-term hooks to call send_*_to_chuukei_server()'s
+ */
+void prepare_movie_hooks(void)
+{
+       char buf[1024];
+       char tmp[80];
+
+       if (movie_mode)
+       {
+               movie_mode = 0;
+#ifdef CHUUKEI
+               if (!chuukei_server) disable_chuukei_server();
+#else
+               disable_chuukei_server();
+#endif
+               fd_close(movie_fd);
+#ifdef JP
+               msg_print("Ï¿²è¤ò½ªÎ»¤·¤Þ¤·¤¿¡£");
+#else
+               msg_print("Stopped recording.");
+#endif
+       }
+       else
+       {
+               sprintf(tmp, "%s.amv", player_base);
+#ifdef JP
+               if (get_string("¥à¡¼¥Ó¡¼µ­Ï¿¥Õ¥¡¥¤¥ë: ", tmp, 80))
+#else
+               if (get_string("Movie file name: ", tmp, 80))
+#endif
+               {
+                       int fd;
+
+                       path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp);
+
+                       fd = fd_open(buf, O_RDONLY);
+
+                       /* Existing file */
+                       if (fd >= 0)
+                       {
+                               char out_val[160];
+
+                               /* Close the file */
+                               (void)fd_close(fd);
+
+                               /* Build query */
+#ifdef JP
+                               (void)sprintf(out_val, "¸½Â¸¤¹¤ë¥Õ¥¡¥¤¥ë¤Ë¾å½ñ¤­¤·¤Þ¤¹¤«? (%s)", buf);
+#else
+                               (void)sprintf(out_val, "Replace existing file %s? ", buf);
+#endif
+
+                               /* Ask */
+                               if (!get_check(out_val)) return;
+
+                               movie_fd = fd_open(buf, O_WRONLY | O_TRUNC);
+                       }
+                       else
+                       {
+                               movie_fd = fd_make(buf, 0644);
+                       }
+
+                       if (!movie_fd)
+                       {
+#ifdef JP
+                               msg_print("¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡ª");
+#else
+                               msg_print("Can not open file.");
+#endif
+                               return;
+                       }
+
+                       movie_mode = 1;
+#ifdef CHUUKEI
+                       if (!chuukei_server) prepare_chuukei_hooks();
+#else
+                       prepare_chuukei_hooks();
+#endif
+                       do_cmd_redraw();
+               }
+       }
+}
+
+#ifdef CHUUKEI
 static int handle_timestamp_data(int timestamp)
 {
        long current_time = get_current_time();
@@ -645,7 +756,35 @@ static int handle_timestamp_data(int timestamp)
        /* Success */
        return 0;
 }
+#endif /* CHUUKEI */
+
+static int handle_movie_timestamp_data(int timestamp)
+{
+       static int initialized = FALSE;
+
+       /* ÉÁ²è¥­¥å¡¼¤Ï¶õ¤«¤É¤¦¤«¡© */
+       if (!initialized)
+       {
+               /* ¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤·»Ï¤á¤Î»þ´Ö¤òÊݸ¤·¤Æ¤ª¤¯ */
+               epoch_time = get_current_time();
+               epoch_time += browse_delay;
+               epoch_time -= timestamp;
+               //time_diff = current_time - timestamp;
+               initialized = TRUE;
+       }
+
+       /* ÉÁ²è¥­¥å¡¼¤ËÊݸ¤·¡¢Êݸ°ÌÃÖ¤ò¿Ê¤á¤ë */
+       fresh_queue.time[fresh_queue.tail] = timestamp;
+       fresh_queue.tail ++;
+
+       /* ¥­¥å¡¼¤ÎºÇ¸åÈø¤ËÅþ㤷¤¿¤éÀèƬ¤ËÌ᤹ */
+       fresh_queue.tail %= FRESH_QUEUE_SIZE;
+
+       /* Success */
+       return 0;
+}
 
+#ifdef CHUUKEI
 static int read_sock(void)
 {
        static char recv_buf[RECVBUF_SIZE];
@@ -655,6 +794,7 @@ static int read_sock(void)
 
        /* Á°²ó»Ä¤Ã¤¿¥Ç¡¼¥¿¤Î¸å¤Ë¤Ä¤Å¤±¤ÆÇÛ¿®¥µ¡¼¥Ð¤«¤é¥Ç¡¼¥¿¼õ¿® */
        recv_bytes = recv(sd, recv_buf + remain_bytes, RECVBUF_SIZE - remain_bytes, 0);
+
        if (recv_bytes <= 0)
                return -1;
 
@@ -686,6 +826,48 @@ static int read_sock(void)
 
        return 0;
 }
+#endif
+
+static int read_movie_file(void)
+{
+       static char recv_buf[RECVBUF_SIZE];
+       static int remain_bytes = 0;
+       int recv_bytes;
+       int i;
+
+       recv_bytes = read(movie_fd, recv_buf + remain_bytes, RECVBUF_SIZE - remain_bytes);
+
+       if (recv_bytes <= 0)
+               return -1;
+
+       /* Á°²ó»Ä¤Ã¤¿¥Ç¡¼¥¿Î̤˺£²óÆɤó¤À¥Ç¡¼¥¿Î̤òÄɲà*/
+       remain_bytes += recv_bytes;
+
+       for (i = 0; i < remain_bytes; i ++)
+       {
+               /* ¥Ç¡¼¥¿¤Î¤¯¤®¤ê('\0')¤òõ¤¹ */
+               if (recv_buf[i] == '\0')
+               {
+                       /* 'd'¤Ç»Ï¤Þ¤ë¥Ç¡¼¥¿(¥¿¥¤¥à¥¹¥¿¥ó¥×)¤Î¾ì¹ç¤Ï
+                          ÉÁ²è¥­¥å¡¼¤ËÊݸ¤¹¤ë½èÍý¤ò¸Æ¤Ö */
+                       if ((recv_buf[0] == 'd') &&
+                           (handle_movie_timestamp_data(atoi(recv_buf + 1)) < 0))
+                               return -1;
+
+                       /* ¼õ¿®¥Ç¡¼¥¿¤òÊݸ */
+                       if (insert_ringbuf(recv_buf) < 0) 
+                               return -1;
+
+                       /* ¼¡¤Î¥Ç¡¼¥¿°Ü¹Ô¤òrecv_buf¤ÎÀèƬ¤Ë°ÜÆ° */
+                       memmove(recv_buf, recv_buf + i + 1, remain_bytes - i - 1);
+
+                       remain_bytes -= (i+1);
+                       i = 0;
+               }
+       }
+
+       return 0;
+}
 
 
 #ifndef WINDOWS
@@ -729,6 +911,23 @@ static bool get_nextbuf(char *buf)
        return (TRUE);
 }
 
+/* ¥×¥ì¥¤¥Û¥¹¥È¤Î¥Þ¥Ã¥×¤¬Â礭¤¤¤È¤­¥¯¥é¥¤¥¢¥ó¥È¤Î¥Þ¥Ã¥×¤â¥ê¥µ¥¤¥º¤¹¤ë */
+static void update_term_size(int x, int y, int len)
+{
+       int ox, oy;
+       int nx, ny;
+       Term_get_size(&ox, &oy);
+       nx = ox;
+       ny = oy;
+
+       /* ²£Êý¸þ¤Î¥Á¥§¥Ã¥¯ */
+       if (x + len > ox) nx = x + len;
+       /* ½ÄÊý¸þ¤Î¥Á¥§¥Ã¥¯ */
+       if (y + 1 > oy) ny = y + 1;
+
+       if (nx != ox || ny != oy) Term_resize(nx, ny);
+}
+
 static bool flush_ringbuf_client(void)
 {
        char buf[1024];
@@ -745,7 +944,7 @@ static bool flush_ringbuf_client(void)
                char id;
                int x, y, len, col;
                int i;
-               char tmp1, tmp2, tmp3, tmp4;
+               unsigned char tmp1, tmp2, tmp3, tmp4;
                char *mesg;
 
                sscanf(buf, "%c%c%c%c%c", &id, &tmp1, &tmp2, &tmp3, &tmp4);
@@ -766,6 +965,7 @@ static bool flush_ringbuf_client(void)
 #ifdef SJIS
                        euc2sjis(mesg);
 #endif
+                       update_term_size(x, y, len);
                        (void)((*angband_term[0]->text_hook)(x, y, len, (byte)col, mesg));
                        strncpy(&Term->scr->c[y][x], mesg, len);
                        for (i = x; i < x+len; i++)
@@ -780,6 +980,7 @@ static bool flush_ringbuf_client(void)
                                mesg[i] = mesg[0];
                        }
                        mesg[i] = '\0';
+                       update_term_size(x, y, len);
                        (void)((*angband_term[0]->text_hook)(x, y, len, (byte)col, mesg));
                        strncpy(&Term->scr->c[y][x], mesg, len);
                        for (i = x; i < x+len; i++)
@@ -789,12 +990,14 @@ static bool flush_ringbuf_client(void)
                        break;
 
                case 's': /* °ìʸ»ú */
+                       update_term_size(x, y, 1);
                        (void)((*angband_term[0]->text_hook)(x, y, 1, (byte)col, mesg));
                        strncpy(&Term->scr->c[y][x], mesg, 1);
                        Term->scr->a[y][x] = col;
                        break;
 
                case 'w':
+                       update_term_size(x, y, len);
                        (void)((*angband_term[0]->wipe_hook)(x, y, len));
                        break;
 
@@ -804,9 +1007,11 @@ static bool flush_ringbuf_client(void)
                        break;
 
                case 'c':
+                       update_term_size(x, y, 1);
                        (void)((*angband_term[0]->curs_hook)(x, y));
                        break;
                case 'C':
+                       update_term_size(x, y, 1);
                        (void)((*angband_term[0]->bigcurs_hook)(x, y));
                        break;
                }
@@ -817,6 +1022,7 @@ static bool flush_ringbuf_client(void)
        return (TRUE);
 }
 
+#ifdef CHUUKEI
 void browse_chuukei()
 {
 #ifndef MACINTOSH
@@ -888,5 +1094,46 @@ void browse_chuukei()
        }
 #endif /*MACINTOSH*/
 }
-
 #endif /* CHUUKEI */
+
+void prepare_browse_movie_aux(cptr filename)
+{
+       movie_fd = fd_open(filename, O_RDONLY);
+       
+       browsing_movie = TRUE;
+
+       init_buffer();
+}
+
+void prepare_browse_movie(cptr filename)
+{
+       char buf[1024];
+       path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
+
+       prepare_browse_movie_aux(buf);
+}
+
+void browse_movie(void)
+{
+       Term_clear();
+       Term_fresh();
+       Term_xtra(TERM_XTRA_REACT, 0);
+
+       while (read_movie_file() == 0)
+       {
+               while (fresh_queue.next != fresh_queue.tail)
+               {
+                       if (!flush_ringbuf_client())
+                       {
+                               Term_xtra(TERM_XTRA_FLUSH, 0);
+
+                               /* ¥½¥±¥Ã¥È¤Ë¥Ç¡¼¥¿¤¬Íè¤Æ¤¤¤ë¤«¤É¤¦¤«Ä´¤Ù¤ë */
+#ifdef WINDOWS
+                               Sleep(WAIT);
+#else
+                               usleep(WAIT);
+#endif
+                       }
+               }
+       }
+}