]> Pileus Git - ~andy/gtk/blob - gdk/gxid_lib.c
Removed DEFS "+=" mess that breaks recent automakes. INCLUDES is the
[~andy/gtk] / gdk / gxid_lib.c
1 /* 
2  * gxid version 0.3
3  *
4  * Copyright 1997 Owen Taylor <owt1@cornell.edu>
5 */
6
7 #include "config.h"
8 #include "gxid_lib.h"
9
10 #ifdef XINPUT_GXI
11
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <netdb.h>
19
20 /* handles mechanics of communicating with a client */
21 static int 
22 gxid_send_message(char *host, int port, GxidMessage *msg)
23 {
24   int socket_fd;
25   struct sockaddr_in sin;
26   int count;
27   GxidI32 retval;
28   struct hostent *he;
29
30   if (!port) port = 6951;
31
32   if (!host || strcmp(host,"localhost") )
33     {
34       /* looking it up as localhost can be _SLOW_ on ppp systems */
35       /* FIXME: Could localhost be anything other than loopback? */
36       host = "127.0.0.1";
37     }
38
39   he = gethostbyname(host);
40   if (!he)
41     {
42       fprintf(stderr,"gxid_lib: error looking up %s\n",host);
43       return GXID_RETURN_ERROR;
44     }
45
46   sin.sin_family = he->h_addrtype;
47   sin.sin_port = htons(port);
48   memcpy(&sin.sin_addr,he->h_addr_list[0],he->h_length);
49
50   socket_fd = socket(AF_INET,SOCK_STREAM,0);
51   if (socket_fd < 0)
52     {
53       fprintf(stderr,"gxid_lib: can't get socket");
54       return GXID_RETURN_ERROR;
55     }
56
57   if (connect(socket_fd, (struct sockaddr *)&sin, 
58               sizeof sin) < 0)
59     {
60       fprintf(stderr,"gxid_lib: can't connect to %s:%d\n",host,port);
61       close(socket_fd);
62       return GXID_RETURN_ERROR;
63     }
64
65   count = write(socket_fd,(char *)msg,ntohl(msg->any.length));
66   if (count != ntohl(msg->any.length))
67     {
68       fprintf(stderr,"gxid_lib: error writing");
69       close(socket_fd);
70       return GXID_RETURN_ERROR;
71     }
72
73   /* now read the return code */
74   count = read(socket_fd,(char *)&retval,sizeof(GxidI32));
75   if (count != sizeof(GxidI32))
76     {
77       fprintf(stderr,"gxid_lib: error reading return code");
78       close(socket_fd);
79       return GXID_RETURN_ERROR;
80     }
81
82   close (socket_fd);
83   return ntohl(retval);
84 }
85
86 /* claim a device. If exclusive, device is claimed exclusively */
87 int 
88 gxid_claim_device(char *host, int port, GxidU32 device, GxidU32 window,
89                   int exclusive)
90 {
91   GxidClaimDevice msg;
92   msg.type = htonl(GXID_CLAIM_DEVICE);
93   msg.length = htonl(sizeof(GxidClaimDevice));
94   msg.device = htonl(device);
95   msg.window = htonl(window);
96   msg.exclusive = htonl(exclusive);
97
98   return gxid_send_message(host,port,(GxidMessage *)&msg);
99 }
100
101 /* release a device/window pair */
102 int 
103 gxid_release_device(char *host, int port, GxidU32 device, GxidU32 window)
104 {
105   GxidReleaseDevice msg;
106   msg.type = htonl(GXID_RELEASE_DEVICE);
107   msg.length = htonl(sizeof(GxidReleaseDevice));
108   msg.device = htonl(device);
109   msg.window = htonl(window);
110
111   return gxid_send_message(host,port,(GxidMessage *)&msg);
112 }
113
114 #else /* !XINPUT_GXI */
115
116 /* Some compilers don't like empty source files */
117 int 
118 gxid_claim_device(char *host, int port, GxidU32 device, GxidU32 window,
119                   int exclusive)
120 {
121   return 0;
122 }
123
124 #endif /* XINPUT_GXI */
125