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