OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [mw/] [src/] [demos/] [nanox/] [setselection.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
#include <stdio.h>
2
#include <stdlib.h>
3
#include <unistd.h>
4
 
5
#include "nano-X.h"
6
 
7
int main(int argc, char *argv[])
8
{
9
        GR_WINDOW_ID wid;
10
        GR_EVENT event;
11
        FILE *fp;
12
        char *buf = NULL;
13
        int buf_size = 0;
14
        int data_len = 0;
15
        int ret = 0;
16
        GR_EVENT_CLIENT_DATA_REQ *req;
17
        GR_EVENT_SELECTION_CHANGED *sc;
18
 
19
        if(argc != 2) {
20
                fprintf(stderr, "Usage: setselection <text file>\n");
21
                return 1;
22
        }
23
 
24
        if(!(fp = fopen(argv[1], "r"))) {
25
                fprintf(stderr, "Couldn't open text file\n");
26
                return 2;
27
        }
28
 
29
        do {
30
                data_len += ret;
31
                buf_size = data_len + 65536;
32
                if(!(buf = realloc(buf, buf_size))) {
33
                        fprintf(stderr, "Out of memory\n");
34
                        return 3;
35
                }
36
        } while((ret = fread(buf + data_len, 1, 65536, fp)) > 0);
37
        if(ret < 0) {
38
                fprintf(stderr, "Failed to read text file sucessfully\n");
39
                return 2;
40
        }
41
 
42
        if(GrOpen() < 0) {
43
                fprintf(stderr, "Couldn't connect to Nano-X server\n");
44
                return 4;
45
        }
46
 
47
        wid = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, 1, 1, 0, 0, 0);
48
        if(!wid) {
49
                fprintf(stderr, "Couldn't get a window\n");
50
                return 5;
51
        }
52
 
53
        GrSelectEvents(wid, GR_EVENT_MASK_CLIENT_DATA_REQ |
54
                                GR_EVENT_MASK_SELECTION_CHANGED);
55
 
56
        GrSetSelectionOwner(wid, "nota/realtype text/plain non/existant "
57
                                "something/else");
58
 
59
        while(1) {
60
                GrGetNextEvent(&event);
61
                switch(event.type) {
62
                        case GR_EVENT_TYPE_CLIENT_DATA_REQ:
63
                                req = &event.clientdatareq;
64
                                fprintf(stderr, "Got request with serial "
65
                                        "number %ld from window %d for mime "
66
                                        "type %d\n", req->serial, req->rid,
67
                                                        req->mimetype);
68
                                GrSendClientData(wid, req->rid, req->serial,
69
                                                data_len, data_len, buf);
70
                                break;
71
                        case GR_EVENT_TYPE_SELECTION_CHANGED:
72
                                sc = &event.selectionchanged;
73
                                if(sc->new_owner != wid) {
74
                                        fprintf(stderr, "Lost selection to "
75
                                                "window %d\n", sc->new_owner);
76
                                        return 0;
77
                                }
78
                        default:
79
                                break;
80
                }
81
        }
82
 
83
        return 0;
84
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.