summaryrefslogtreecommitdiff
path: root/gdk/gxid_lib.c
blob: 0c2a73bbf5dd33b5d775b537d1f07359fa36f012 (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
/* 
 * gxid version 0.3
 *
 * Copyright 1997 Owen Taylor <owt1@cornell.edu>
*/

#include "config.h"
#include "gxid_lib.h"

#ifdef XINPUT_GXI

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

/* handles mechanics of communicating with a client */
static int 
gxid_send_message(char *host, int port, GxidMessage *msg)
{
  int socket_fd;
  struct sockaddr_in sin;
  int count;
  GxidI32 retval;
  struct hostent *he;

  if (!port) port = 6951;

  if (!host || strcmp(host,"localhost") )
    {
      /* looking it up as localhost can be _SLOW_ on ppp systems */
      /* FIXME: Could localhost be anything other than loopback? */
      host = "127.0.0.1";
    }

  he = gethostbyname(host);
  if (!he)
    {
      fprintf(stderr,"gxid_lib: error looking up %s\n",host);
      return GXID_RETURN_ERROR;
    }

  sin.sin_family = he->h_addrtype;
  sin.sin_port = htons(port);
  memcpy(&sin.sin_addr,he->h_addr_list[0],he->h_length);

  socket_fd = socket(AF_INET,SOCK_STREAM,0);
  if (socket_fd < 0)
    {
      fprintf(stderr,"gxid_lib: can't get socket");
      return GXID_RETURN_ERROR;
    }

  if (connect(socket_fd, (struct sockaddr *)&sin, 
	      sizeof sin) < 0)
    {
      fprintf(stderr,"gxid_lib: can't connect to %s:%d\n",host,port);
      close(socket_fd);
      return GXID_RETURN_ERROR;
    }

  count = write(socket_fd,(char *)msg,ntohl(msg->any.length));
  if (count != ntohl(msg->any.length))
    {
      fprintf(stderr,"gxid_lib: error writing");
      close(socket_fd);
      return GXID_RETURN_ERROR;
    }

  /* now read the return code */
  count = read(socket_fd,(char *)&retval,sizeof(GxidI32));
  if (count != sizeof(GxidI32))
    {
      fprintf(stderr,"gxid_lib: error reading return code");
      close(socket_fd);
      return GXID_RETURN_ERROR;
    }

  close (socket_fd);
  return ntohl(retval);
}

/* claim a device. If exclusive, device is claimed exclusively */
int 
gxid_claim_device(char *host, int port, GxidU32 device, GxidU32 window,
		  int exclusive)
{
  GxidClaimDevice msg;
  msg.type = htonl(GXID_CLAIM_DEVICE);
  msg.length = htonl(sizeof(GxidClaimDevice));
  msg.device = htonl(device);
  msg.window = htonl(window);
  msg.exclusive = htonl(exclusive);

  return gxid_send_message(host,port,(GxidMessage *)&msg);
}

/* release a device/window pair */
int 
gxid_release_device(char *host, int port, GxidU32 device, GxidU32 window)
{
  GxidReleaseDevice msg;
  msg.type = htonl(GXID_RELEASE_DEVICE);
  msg.length = htonl(sizeof(GxidReleaseDevice));
  msg.device = htonl(device);
  msg.window = htonl(window);

  return gxid_send_message(host,port,(GxidMessage *)&msg);
}

#else /* !XINPUT_GXI */

/* Some compilers don't like empty source files */
int 
gxid_claim_device(char *host, int port, GxidU32 device, GxidU32 window,
		  int exclusive)
{
  return 0;
}

#endif /* XINPUT_GXI */