File: dgt.c

package info (click to toggle)
crafty 19.15-1
  • links: PTS
  • area: non-free
  • in suites: sarge
  • size: 2,488 kB
  • ctags: 3,374
  • sloc: ansic: 28,852; cpp: 5,834; makefile: 431; sh: 108; perl: 30
file content (157 lines) | stat: -rw-r--r-- 4,631 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include "chess.h"
#include "data.h"

/* last modified 11/19/98 */
/*
 *******************************************************************************
 *                                                                             *
 *   DGTInit() fork()'s a process to handle the DGT chessboard, and then sets  *
 *   up a pipe to communicate with it.                                         *
 *                                                                             *
 *******************************************************************************
 */
#if defined(DGT)

void DGTDelayTime(int ms)
{
  int oldt, newt;

  oldt = ReadClock(elapsed);
  do {
    newt = ReadClock(elapsed);
  } while (newt - ms / 10 < oldt);
}
void DGTInit(int nargs, char *args[])
{
/*
 ************************************************************
 *                                                          *
 *  fork() to spawn a new process, then execve() to execute *
 *  dgt instead of the cloned copy of crafty created by the *
 *  fork() call.                                            *
 *                                                          *
 ************************************************************
 */
  char dgt[] = { "dgt" };
  char *argv[5] = { 0, 0, 0, 0, 0 }, *env[2];
  int tp[2], fp[2];
  char device[] = { "/dev/ttyS0" }, setup[] = {
  "lw"};

  printf("Executing DGT driver.\n");
  argv[0] = dgt;
  if (nargs > 1)
    argv[1] = args[1];
  else
    argv[1] = device;
  if (nargs > 2)
    argv[2] = args[2];
  else
    argv[2] = setup;
  if (nargs > 3)
    argv[3] = args[3];
  env[0] = dgt;
  env[1] = 0;
  signal(SIGPIPE, SIG_IGN);
  pipe(tp);
  pipe(fp);
  to_dgt = tp[1];
  from_dgt = fp[0];
/*
 **************************************************
 *                                                *
 *  child process has to force stdin, stdout and  *
 *  stderr to use the two pipes we just created.  *
 *                                                *
 **************************************************
 */
  if (fork() == 0) {
    dup2(tp[0], 0);
    dup2(fp[1], 1);
    close(tp[0]);
    close(tp[1]);
    close(fp[0]);
    close(fp[1]);
    dup2(1, fileno(stderr));
    execve("dgt", argv, env);
    printf("ERROR: unable to execute dgt driver!\n");
    exit(1);
  }
  close(tp[0]);
  close(fp[1]);
  write(to_dgt, "reset\n", 6);
  DGTDelayTime(200);
  write(to_dgt, "set\n", 4);
  DGTDelayTime(300);
  write(to_dgt, "update\n", 7);
  DGT_active = 1;
}

/* last modified 11/19/98 */
/*
 *******************************************************************************
 *                                                                             *
 *   DGTRead() reads data from the tty port connected to the DGT electronic    *
 *   chess board and handles all data from that device.                        *
 *                                                                             *
 *******************************************************************************
 */
void DGTRead()
{
  static char buffer[512];
  char temp[512];
  static int bytes = 0;
  char *last;

  bytes = read(from_dgt, buffer + bytes, 256);
  buffer[bytes] = 0;
  last = strchr(buffer, '\n');
  while (last) {
    if (last)
      *last = 0;
    bytes -= strlen(buffer) + 1;
    if (!strncmp(buffer, "move", 4)) {
      strcpy(cmd_buffer + strlen(cmd_buffer), buffer + 5);
      strcpy(cmd_buffer + strlen(cmd_buffer), "\n");
    }
    if (!strncmp(buffer, "command", 7)) {
      strcpy(cmd_buffer + strlen(cmd_buffer), buffer + 8);
      strcpy(cmd_buffer + strlen(cmd_buffer), "\n");
    } else if (!strncmp(buffer, "info", 4))
      Print(128, "DGT: %s\n", buffer + 4);
    strcpy(temp, last + 1);
    strcpy(buffer, temp);
    last = strchr(buffer, '\n');
  }
}

/* last modified 11/20/98 */
/*
 *******************************************************************************
 *                                                                             *
 *   DGTCheckInput() checks to determine if input from the DGT board arrived.  *
 *                                                                             *
 *******************************************************************************
 */
int DGTCheckInput(void)
{
  fd_set readfds;
  struct timeval tv;
  int data;

  FD_ZERO(&readfds);
  FD_SET(from_dgt, &readfds);
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  (void) select(32, &readfds, 0, 0, &tv);
  data = FD_ISSET(from_dgt, &readfds);
  return (data);
}
#endif