comparison packages/services/gfx/mw/current/src/demos/nanox/getselection.c @ 208:e0c0827131d1 ecos

Merge from eCos master repository on 2002-05-20-20:11:54-BST
author jlarmour
date Mon, 20 May 2002 22:19:26 +0000
parents
children
comparison
equal deleted inserted replaced
207:74c807ddde34 208:e0c0827131d1
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 #include "nano-X.h"
7
8 static int bytes_received = 0;
9 static char *data = NULL;
10
11 int got_client_data(GR_EVENT *event)
12 {
13 GR_EVENT_CLIENT_DATA *ev = &event->clientdata;
14
15 fprintf(stderr, "Got client data packet with serial number %ld for "
16 "window %d from window %d\n", ev->serial, ev->wid,
17 ev->rid);
18 if(!(data = realloc(data, bytes_received + ev->datalen))) {
19 fprintf(stderr, "Out of memory\n");
20 exit(7);
21 }
22 memcpy(data + bytes_received, ev->data, ev->datalen);
23 free(ev->data);
24
25 fprintf(stderr, "Got client data packet with serial number %ld for "
26 "window %d from window %d\n", ev->serial, ev->wid,
27 ev->rid);
28 fprintf(stderr, "Already received %d bytes, this packet is %ld bytes "
29 "long, and the total data length is %ld bytes so ",
30 bytes_received, ev->datalen, ev->len);
31
32 bytes_received += ev->datalen;
33 if(bytes_received == ev->len) {
34 fprintf(stderr, "we have received all of the data now.\n");
35 fprintf(stderr, "The data in the packet is:\n%s\n", data);
36 return 1;
37 }
38 else if(bytes_received < ev->len) {
39 fprintf(stderr, "this is not the last data packet.\n");
40 return 0;
41 } else fprintf(stderr, "we have received too much data (shouldn't "
42 "happen)\n");
43
44 return 1;
45 }
46
47 int main(int argc, char *argv[])
48 {
49 GR_CHAR *typelist, *p;
50 GR_WINDOW_ID sid, wid;
51 GR_EVENT event;
52 int n = 0, mimetype = -1;
53
54 if(GrOpen() < 0) {
55 fprintf(stderr, "Couldn't connect to Nano-X server\n");
56 return 1;
57 }
58
59 sid = GrGetSelectionOwner(&typelist);
60 if(!sid) {
61 fprintf(stderr, "Clipboard is empty\n");
62 return 2;
63 }
64
65 if(!typelist) {
66 fprintf(stderr, "GrGetSelectionOwner() returned an empty "
67 "type list for window %d\n", sid);
68 return 3;
69 }
70
71 fprintf(stderr, "Window %d owns the selection\n", sid);
72 fprintf(stderr, "It claims to be able to supply data in the following "
73 "types:\n%s\n", typelist);
74
75 p = strtok(typelist, " ");
76 do {
77 if(!strncmp("text/plain", p, 10)) {
78 mimetype = n;
79 break;
80 }
81 n++;
82 } while((p = strtok(NULL, " ")));
83
84 if(mimetype == -1) {
85 fprintf(stderr, "Type text/plain is not available\n");
86 return 4;
87 }
88
89 free(typelist);
90
91 fprintf(stderr, "Type text/plain is available- requesting data\n");
92
93 wid = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, 1, 1, 0, 0, 0);
94 if(!wid) {
95 fprintf(stderr, "Couldn't get a window\n");
96 return 5;
97 }
98
99 GrSelectEvents(wid, GR_EVENT_MASK_CLIENT_DATA);
100
101 GrRequestClientData(wid, sid, 0, mimetype);
102
103 while(1) {
104 GrGetNextEventTimeout(&event, 4000);
105 switch(event.type) {
106 case GR_EVENT_TYPE_CLIENT_DATA:
107 if(got_client_data(&event))
108 return 0;
109 break;
110 case GR_EVENT_TYPE_TIMEOUT:
111 fprintf(stderr, "Timed out waiting for data\n");
112 return 6;
113 default:
114 break;
115 }
116 }
117
118 return 0;
119 }