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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [gfx/] [mw/] [v2_0/] [src/] [ecos/] [img_demo.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
#include <stdio.h>  
2
#include <stdlib.h> 
3
#include <unistd.h>
4
#include <string.h> 
5
#include <network.h>
6
#include <tftp_support.h>
7
#include <dirent.h>
8
#include <errno.h>
9
#include <cyg/kernel/kapi.h>            /* All the kernel specific stuff */
10
#include <cyg/infra/diag.h>        
11
 
12
#define MWINCLUDECOLORS
13
#include "nano-X.h"
14
 
15
static int show_time = 5;
16
 
17
//
18
// Component interfaces
19
//
20
 
21
static void
22
fdprintf(int fd, char *fmt, ...)
23
{
24
    char msg[256];
25
    int ret;
26
    va_list ap;
27
 
28
    // Ignore if no 'fd'
29
    va_start(ap, fmt);
30
    ret = vsprintf(msg, fmt, ap);
31
    va_end(ap);
32
    diag_printf(msg);
33
    if (fd <= 0) return;
34
    write(fd, msg, strlen(msg));
35
}
36
 
37
void
38
show_jpeg(int client, char *file, int show_time)
39
{
40
    GR_EVENT        event;          /* current event */
41
    GR_IMAGE_ID     id = 0;
42
    GR_SIZE         w = -1;
43
    GR_SIZE         h = -1;
44
    GR_IMAGE_INFO   info;
45
    GR_WINDOW_ID    w1;             /* id for large window */
46
    GR_GC_ID        gc1;            /* graphics context for text */
47
    GR_SCREEN_INFO  si;
48
 
49
    int time_left;
50
    bool ever_exposed = false;
51
 
52
#if !defined(HAVE_JPEG_SUPPORT) && !defined(HAVE_BMP_SUPPORT) && !defined(HAVE_GIF_SUPPORT)
53
    printf("Sorry, no image support compiled in\n");
54
    exit(1);
55
#endif
56
 
57
    GrGetScreenInfo(&si);
58
    printf("Loading image: %s\n", file);
59
    if (access(file, F_OK) < 0) {
60
        fdprintf(client, "Can't access \"%s\": %s\n", file, strerror(errno));
61
        return;
62
    }
63
    id = GrLoadImageFromFile(file, 0);
64
    if (id) {
65
        GrGetImageInfo(id, &info);
66
    } else {
67
        // File exists, so why the error?
68
        int fd, len;
69
        char buf[64];
70
        fdprintf(client, "Can't load %s\n", file);
71
        if ((fd = open(file, O_RDONLY)) >= 0) {
72
            len = read(fd, buf, 64);
73
            if (len != 64) {
74
                diag_printf("Short read? len = %d\n", len);
75
            } else {
76
                diag_dump_buf(buf, len);
77
            }
78
            close(fd);
79
        } else {
80
            diag_printf("Can't oopen \"%s\": %s\n",  file, strerror(errno));
81
        }
82
        return;
83
    }
84
 
85
    w = info.width;
86
    h = info.height;
87
    if ((si.rows < info.height) || (si.cols < info.width)) {
88
        // Preserve aspect ratio
89
        if (si.cols < info.width) {
90
            w = si.cols;
91
            h = (si.cols * info.height) / info.width;
92
        }
93
        if (si.rows < h) {
94
            w = (si.rows * w) / h;
95
            h = si.rows;
96
        }
97
    }
98
    printf("Create window - orig %dx%d => %dx%d\n", info.width, info.height, w, h);
99
    fdprintf(client, "<INFO> Display \"%s\" - orig %dx%d => %dx%d\n", file, info.width, info.height, w, h);
100
    w1 = GrNewWindow(GR_ROOT_WINDOW_ID, 10, 10, w, h, 4, BLACK, WHITE);
101
    GrSelectEvents(w1, GR_EVENT_MASK_CLOSE_REQ|GR_EVENT_MASK_EXPOSURE);
102
    GrMapWindow(w1);
103
    gc1 = GrNewGC();
104
    GrSetGCForeground(gc1, WHITE);
105
 
106
#define TO_MS 50
107
    time_left = show_time * 1000;
108
    while (time_left > 0) {
109
        GrGetNextEventTimeout(&event, TO_MS);  // milliseconds
110
        switch(event.type) {
111
        case GR_EVENT_TYPE_CLOSE_REQ:
112
            GrDestroyWindow(w1);
113
            GrFreeImage(id);
114
            return;
115
            /* no return*/
116
        case GR_EVENT_TYPE_EXPOSURE:
117
            /*GrDrawImageFromFile(w1, gc1, 0, 0, w, h, argv[1],0);*/
118
            GrDrawImageToFit(w1, gc1, 0, 0, w, h, id);
119
            ever_exposed = true;
120
            break;
121
        default:
122
        case GR_EVENT_TYPE_NONE:
123
        case GR_EVENT_TYPE_TIMEOUT:
124
            time_left -= TO_MS;
125
            if ((time_left < 0) && !ever_exposed) {
126
                // Things get real cranky if we delete the window too fast!
127
                time_left = TO_MS;
128
            }
129
            break;
130
        }
131
    }
132
    GrUnmapWindow(w1);
133
    GrDestroyWindow(w1);
134
    GrDestroyGC(gc1);
135
    GrFreeImage(id);
136
}
137
 
138
static void
139
do_ls(int client, char *buf)
140
{
141
    int err;
142
    DIR *dirp;
143
    int num=0;
144
    char name[256];
145
 
146
    buf += 2;  // Skip over command
147
    while (*buf && (*buf == ' ')) buf++;
148
    if (*buf) {
149
        // Name provided
150
        strcpy(name, buf);
151
    } else {
152
        strcpy(name, ".");
153
    }
154
 
155
    fdprintf(client, "<INFO> reading directory %s\n",name);
156
    dirp = opendir( name );
157
    if( dirp == NULL ) {
158
        fdprintf(client, "Can't open directory \"%s\"\n", name);
159
        return;
160
    }
161
    for(;;) {
162
        struct dirent *entry = readdir( dirp );
163
        struct stat sbuf;
164
        char fullname[PATH_MAX];
165
 
166
        if( entry == NULL )
167
            break;
168
        num++;
169
        diag_printf("<INFO> entry %s",entry->d_name);
170
        fdprintf(client, "<INFO> entry %14s",entry->d_name);
171
        if( name[0] ) {
172
            strcpy(fullname, name );
173
            if( !(name[0] == '/' && name[1] == 0 ) )
174
                strcat(fullname, "/" );
175
        }
176
        else fullname[0] = 0;
177
        strcat(fullname, entry->d_name );
178
        err = stat( fullname, &sbuf );
179
        if( err < 0 ) {
180
            if( errno == ENOSYS ) {
181
                fdprintf(client, " <no status available>");
182
            } else {
183
                diag_printf(" - error %s\n", strerror(errno));
184
                fdprintf(client, " - error %s\n", strerror(errno));
185
            }
186
        } else {
187
            diag_printf(" [mode %08x ino %08x nlink %d size %d]\n",
188
                        sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size);
189
            fdprintf(client, " [mode %08x ino %08x nlink %d size %d]\n",
190
                     sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size);
191
        }
192
    }
193
    err = closedir( dirp );
194
}
195
 
196
static void
197
do_show(int client, char *buf)
198
{
199
    char name[256];
200
 
201
    buf += 4;  // Skip over command
202
    while (*buf && (*buf == ' ')) buf++;
203
    if (*buf) {
204
        // Name provided
205
        strcpy(name, buf);
206
    } else {
207
        fdprintf(client, "usage: show <file>\n");
208
        return;
209
    }
210
    show_jpeg(client, name, show_time);
211
}
212
 
213
static void
214
do_show_all(int client, char *buf)
215
{
216
    int err;
217
    DIR *dirp;
218
    char *c, name[256];
219
 
220
    buf += 8;  // Skip over command
221
    while (*buf && (*buf == ' ')) buf++;
222
    if (*buf) {
223
        // Name provided
224
        strcpy(name, buf);
225
    } else {
226
        strcpy(name, ".");
227
    }
228
 
229
    fdprintf(client, "<INFO> show .jpg files in directory %s\n",name);
230
    dirp = opendir( name );
231
    if( dirp == NULL ) {
232
        fdprintf(client, "Can't open directory \"%s\"\n", name);
233
        return;
234
    }
235
    for(;;) {
236
        struct dirent *entry = readdir( dirp );
237
        struct stat sbuf;
238
        char fullname[PATH_MAX];
239
 
240
        if( entry == NULL )
241
            break;
242
        if( name[0] ) {
243
            strcpy(fullname, name );
244
            if( !(name[0] == '/' && name[1] == 0 ) )
245
                strcat(fullname, "/" );
246
        }
247
        else fullname[0] = 0;
248
        strcat(fullname, entry->d_name );
249
        err = stat( fullname, &sbuf );
250
        if( err < 0 ) {
251
            fdprintf(client, "<ERROR> Can't access \"%s\":", fullname);
252
            if( errno == ENOSYS ) {
253
                fdprintf(client, " <no status available>");
254
            } else {
255
                fdprintf(client, "%s\n", strerror(errno));
256
            }
257
        } else {
258
#if 0
259
            if (/* hack: !S_ISREG(sbuf.st_mode)*/ (sbuf.st_mode & 0x8000) == 0) {
260
                continue;
261
            }
262
#endif
263
            if ((c = rindex(fullname, '.')) != (char *)NULL) {
264
                if (strcmp(c, ".jpg") == 0) {
265
                    show_jpeg(client, fullname, show_time);
266
                }
267
            }
268
        }
269
    }
270
    err = closedir( dirp );
271
}
272
 
273
static void
274
do_time(int client, char *buf)
275
{
276
    char *cp;
277
    int val;
278
 
279
    buf += 4;  // Skip over command
280
    while (*buf && (*buf == ' ')) buf++;
281
    if (*buf) {
282
        // value provided
283
        val = strtol(buf, &cp, 0);
284
        if (val > 0) {
285
            show_time = val;
286
            fdprintf(client, "<INFO> time set to %d seconds\n", val);
287
            return;
288
        }
289
    }
290
    fdprintf(client, "usage: time <value>\n");
291
}
292
 
293
static void
294
do_get(int client, char *buf)
295
{
296
    char *fn, *sn, *data;
297
#ifdef CYGPKG_FS_RAM
298
    char _fn[PATH_MAX];
299
#endif
300
    int fd, len, err;
301
    struct sockaddr_in srvr_addr;
302
 
303
    buf += 3;  // Skip over command
304
    fn = strtok(buf, " ,");
305
    sn = strtok(NULL, " ,");
306
    if ((fn == (char *)NULL) || (sn == (char *)NULL)) {
307
        fdprintf(client, "usage: get <file> <server>\n");
308
        return;
309
    }
310
    // For now, only numeric IP addresses
311
    if (!inet_aton(sn, &srvr_addr.sin_addr)) {
312
        fdprintf(client, "Can't get host info: %s\n", sn);
313
        return;
314
    }
315
    srvr_addr.sin_port = 0;
316
    if ((data = (char *)malloc(0x100000)) == (char *)NULL) {
317
        fdprintf(client, "Can't allocate temp buffer\n");
318
        return;
319
    }
320
    if ((len = tftp_get(fn, &srvr_addr, data, 0x100000, TFTP_OCTET, &err)) > 0) {
321
        fdprintf(client, "Read %d bytes\n", len);
322
        fd = open(fn, O_RDWR|O_CREAT);
323
        if (fd > 0) {
324
            err = write(fd, data, len);
325
            if (err != len) {
326
                fdprintf(client, "Error writing data\n");
327
            }
328
            close(fd);
329
        } else {
330
            fdprintf(client, "Can't create \"%s\"\n", fn);
331
        }
332
#ifdef CYGPKG_FS_RAM
333
        sprintf(_fn, "/%s", fn);
334
        fd = open(_fn, O_RDWR|O_CREAT);
335
        if (fd > 0) {
336
            err = write(fd, data, len);
337
            if (err != len) {
338
                fdprintf(client, "Error writing data\n");
339
            }
340
            close(fd);
341
        } else {
342
            fdprintf(client, "Can't create \"%s\"\n", _fn);
343
        }
344
#endif
345
    } else {
346
        fdprintf(client, "Error reading data\n");
347
    }
348
    free(data);
349
}
350
 
351
static void
352
do_rm(int client, char *buf)
353
{
354
    char *fn;
355
 
356
    buf += 2;  // Skip over command
357
    fn = strtok(buf, " ,");
358
    if (fn == (char *)NULL) {
359
        fdprintf(client, "usage: rm <file>\n");
360
        return;
361
    }
362
    if (unlink(fn) < 0) {
363
        fdprintf(client, "Can't remove \"%s\": %s\n", fn, strerror(errno));
364
    }
365
}
366
 
367
static void
368
do_cmd(int client, char *buf)
369
{
370
    char *cp = buf+strlen(buf)-1;
371
    while ((*cp == '\n') || (*cp == '\r')) {
372
        *cp-- = '\0';  // Remove trailing terminators
373
    }
374
    printf("Command: %s\n", buf);
375
    if (strncmp(buf, "ls", 2) == 0) {
376
        do_ls(client, buf);
377
    } else
378
    if (strncmp(buf, "show_all", 8) == 0) {
379
        do_show_all(client, buf);
380
    } else
381
    if (strncmp(buf, "show", 4) == 0) {
382
        do_show(client, buf);
383
    } else
384
    if (strncmp(buf, "time", 4) == 0) {
385
        do_time(client, buf);
386
    } else
387
    if (strncmp(buf, "get", 3) == 0) {
388
        do_get(client, buf);
389
    } else
390
    if (strncmp(buf, "rm", 2) == 0) {
391
        do_rm(client, buf);
392
    } else
393
    {
394
        fdprintf(client, "Unknown command: %s\n", buf);
395
    }
396
}
397
 
398
static void
399
do_copy_file(char *src, char *dst)
400
{
401
    int err, srcfd, dstfd, len;
402
    char *buf;
403
 
404
#define COPY_SIZE 0x1000
405
    if ((srcfd = open(src, O_RDONLY)) >= 0) {
406
        if ((dstfd = open(dst, O_RDWR|O_CREAT)) >= 0) {
407
            if ((buf = (char *)malloc(COPY_SIZE)) != (char *)NULL) {
408
                while ((len = read(srcfd, buf, COPY_SIZE)) > 0) {
409
                    write(dstfd, buf, len);
410
                }
411
                free(buf);
412
            } else {
413
                diag_printf("Can't allocate working buffer\n");
414
            }
415
            close(srcfd);
416
            close(dstfd);
417
        } else {
418
            diag_printf("Can't create \"%s\": %s\n", dst, strerror(errno));
419
            close(srcfd);
420
        }
421
    } else {
422
        diag_printf("Can't open \"%s\": %s\n", src, strerror(errno));
423
    }
424
}
425
 
426
static void
427
do_copy_all(char *src, char *dst)
428
{
429
    int err;
430
    DIR *dirp;
431
    char *c, *buf;
432
 
433
    diag_printf("Copy all files from %s to %s\n", src, dst);
434
    dirp = opendir(src);
435
    if(dirp == NULL) {
436
        diag_printf("Can't open directory \"%s\"\n", src);
437
        return;
438
    }
439
    for(;;) {
440
        struct dirent *entry = readdir( dirp );
441
        char srcname[PATH_MAX];
442
        char dstname[PATH_MAX];
443
 
444
        if( entry == NULL )
445
            break;
446
        strcpy(srcname, src);
447
        strcpy(dstname, dst);
448
        if( !(src[0] == '/' && src[1] == 0 ) )
449
            strcat(srcname, "/" );
450
        if( !(dst[0] == '/' && dst[1] == 0 ) )
451
            strcat(dstname, "/" );
452
        strcat(srcname, entry->d_name );
453
        strcat(dstname, entry->d_name );
454
        if ((c = rindex(srcname, '.')) != (char *)NULL) {
455
            if (strcmp(c, ".jpg") == 0) {
456
                diag_printf("Copy %s => %s\n", srcname, dstname);
457
                do_copy_file(srcname, dstname);
458
            }
459
        }
460
    }
461
    closedir(dirp);
462
}
463
 
464
static void
465
pexit(char *why)
466
{
467
    printf("Image demo thread exiting - %s\n", why);
468
    GrClose();
469
    cyg_thread_exit();
470
}
471
 
472
int
473
img_demo_thread(CYG_ADDRWORD data)
474
{
475
    int err, s, client, client_len;
476
    struct sockaddr client_addr;
477
    struct sockaddr_in my_addr;
478
    struct addrinfo *ai, *addrs, hints;
479
    char buf[256], addr_buf[256];
480
    int one = 1;
481
    fd_set in_fds, src_fds;
482
    int num, len;
483
    struct timeval tv;
484
 
485
    INIT_PER_THREAD_DATA();
486
 
487
    printf("Image demo here\n");
488
#ifdef CYGPKG_FS_RAM
489
    // Stage files from JFFS2 image into RAM
490
    err = mount("", "/ramfs", "ramfs");
491
    if (err >= 0) {
492
        do_copy_all("/", "/ramfs");
493
    } else {
494
        pexit("Can't mount RAMfs\n");
495
    }
496
    chdir("/ramfs");
497
#endif
498
    if(GrOpen() < 0) {
499
        fprintf(stderr, "Couldn't connect to Nano-X server\n");
500
        exit(1);
501
    }
502
    // Set up as a generic server, listening on TCP/7734
503
#if 0
504
    bzero(&hints, sizeof(hints));
505
    hints.ai_family = PF_UNSPEC;b
506
    hints.ai_socktype = SOCK_STREAM;
507
    hints.ai_flags = AI_PASSIVE;
508
    if ((err = getaddrinfo(NULL, "7734", &hints, &addrs)) != EAI_NONE) {
509
        diag_printf("can't getaddrinfo(): %s\n", gai_strerror(err));
510
        pexit("getaddrinfo");
511
    }
512
    s = socket(ai->ai_family, ai->ai_socktype, 0);
513
    if (s < 0) {
514
        pexit("stream socket");
515
    }
516
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
517
        pexit("setsockopt SO_REUSEADDR");
518
    }
519
    if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
520
        pexit("setsockopt SO_REUSEPORT");
521
    }
522
    if(bind(s, ai->ai_addr, ai->ai_addr->sa_len) < 0) {
523
        pexit("bind error");
524
    }
525
    listen(s, SOMAXCONN);
526
#else
527
    s = socket(AF_INET, SOCK_STREAM, 0);
528
    if (s < 0) {
529
        pexit("stream socket");
530
    }
531
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
532
        pexit("setsockopt SO_REUSEADDR");
533
    }
534
    if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
535
        pexit("setsockopt SO_REUSEPORT");
536
    }
537
    memset(&my_addr, 0, sizeof(my_addr));
538
    my_addr.sin_family = AF_INET;
539
    my_addr.sin_len = sizeof(my_addr);
540
    my_addr.sin_port = htons(7734);
541
    my_addr.sin_addr.s_addr = INADDR_ANY;
542
    if(bind(s, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) {
543
        pexit("bind error");
544
    }
545
    listen(s, SOMAXCONN);
546
#endif
547
    while (true) {
548
        // Wait for a connection
549
        FD_ZERO(&src_fds);
550
        FD_SET(s, &src_fds);
551
        tv.tv_sec = 15;
552
        tv.tv_usec = 0;
553
        num = select(s+1, &src_fds, 0, 0, &tv);
554
        if (num > 0) {
555
            client_len = sizeof(client_addr);
556
            if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
557
                pexit("accept");
558
            }
559
            client_len = sizeof(client_addr);
560
            getpeername(client, &client_addr, &client_len);
561
            _inet_ntop(&client_addr, addr_buf, sizeof(addr_buf));
562
            diag_printf("connection from %s(%d)\n", addr_buf, ntohs(_inet_port(&client_addr)));
563
            fdprintf(client, "Hello %s(%d)\n", addr_buf, ntohs(_inet_port(&client_addr)));
564
            while (true) {
565
                fdprintf(client, "// Ready\n");
566
                tv.tv_sec = 5;
567
                tv.tv_usec = 0;
568
                FD_ZERO(&in_fds);
569
                FD_SET(client, &in_fds);
570
                num = select(client+1, &in_fds, 0, 0, &tv);
571
                if (num > 0) {
572
                    len = read(client, buf, sizeof(buf)-1);
573
                    if (len <= 0) {
574
                        diag_printf("Client read error: %s\n", strerror(errno));
575
                        break;
576
                    }
577
                    buf[len-1] = '\0';
578
                    do_cmd(client, buf);
579
                } else if (num == 0) {
580
                    fdprintf(client, "<IDLE> show_all\n");
581
                    do_show_all(client, "show_all .");
582
                } else {
583
                    perror("select");
584
                }
585
            }
586
            close(client);
587
            diag_printf("Connection with %s closed\n", addr_buf);
588
        } else if (num == 0) {
589
            // No connection within 15 seconds
590
            do_show_all(0, "show_all .");
591
        } else {
592
            diag_printf("select returned: %d\n", num);
593
            pexit("bad select");
594
        }
595
    }
596
}

powered by: WebSVN 2.1.0

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