📄 img_demo.c
字号:
#include <stdio.h> #include <stdlib.h> #include <unistd.h>#include <string.h> #include <network.h>#include <tftp_support.h>#include <dirent.h>#include <errno.h>#include <cyg/kernel/kapi.h> /* All the kernel specific stuff */#include <cyg/infra/diag.h> #define MWINCLUDECOLORS#include "nano-X.h"static int show_time = 5;//// Component interfaces//static voidfdprintf(int fd, char *fmt, ...){ char msg[256]; int ret; va_list ap; // Ignore if no 'fd' va_start(ap, fmt); ret = vsprintf(msg, fmt, ap); va_end(ap); diag_printf(msg); if (fd <= 0) return; write(fd, msg, strlen(msg));}voidshow_jpeg(int client, char *file, int show_time){ GR_EVENT event; /* current event */ GR_IMAGE_ID id = 0; GR_SIZE w = -1; GR_SIZE h = -1; GR_IMAGE_INFO info; GR_WINDOW_ID w1; /* id for large window */ GR_GC_ID gc1; /* graphics context for text */ GR_SCREEN_INFO si; int time_left; bool ever_exposed = false;#if !defined(HAVE_JPEG_SUPPORT) && !defined(HAVE_BMP_SUPPORT) && !defined(HAVE_GIF_SUPPORT) printf("Sorry, no image support compiled in\n"); exit(1); #endif GrGetScreenInfo(&si); printf("Loading image: %s\n", file); if (access(file, F_OK) < 0) { fdprintf(client, "Can't access \"%s\": %s\n", file, strerror(errno)); return; } id = GrLoadImageFromFile(file, 0); if (id) { GrGetImageInfo(id, &info); } else { // File exists, so why the error? int fd, len; char buf[64]; fdprintf(client, "Can't load %s\n", file); if ((fd = open(file, O_RDONLY)) >= 0) { len = read(fd, buf, 64); if (len != 64) { diag_printf("Short read? len = %d\n", len); } else { diag_dump_buf(buf, len); } close(fd); } else { diag_printf("Can't oopen \"%s\": %s\n", file, strerror(errno)); } return; } w = info.width; h = info.height; if ((si.rows < info.height) || (si.cols < info.width)) { // Preserve aspect ratio if (si.cols < info.width) { w = si.cols; h = (si.cols * info.height) / info.width; } if (si.rows < h) { w = (si.rows * w) / h; h = si.rows; } } printf("Create window - orig %dx%d => %dx%d\n", info.width, info.height, w, h); fdprintf(client, "<INFO> Display \"%s\" - orig %dx%d => %dx%d\n", file, info.width, info.height, w, h); w1 = GrNewWindow(GR_ROOT_WINDOW_ID, 10, 10, w, h, 4, BLACK, WHITE); GrSelectEvents(w1, GR_EVENT_MASK_CLOSE_REQ|GR_EVENT_MASK_EXPOSURE); GrMapWindow(w1); gc1 = GrNewGC(); GrSetGCForeground(gc1, WHITE);#define TO_MS 50 time_left = show_time * 1000; while (time_left > 0) { GrGetNextEventTimeout(&event, TO_MS); // milliseconds switch(event.type) { case GR_EVENT_TYPE_CLOSE_REQ: GrDestroyWindow(w1); GrFreeImage(id); return; /* no return*/ case GR_EVENT_TYPE_EXPOSURE: /*GrDrawImageFromFile(w1, gc1, 0, 0, w, h, argv[1],0);*/ GrDrawImageToFit(w1, gc1, 0, 0, w, h, id); ever_exposed = true; break; default: case GR_EVENT_TYPE_NONE: case GR_EVENT_TYPE_TIMEOUT: time_left -= TO_MS; if ((time_left < 0) && !ever_exposed) { // Things get real cranky if we delete the window too fast! time_left = TO_MS; } break; } } GrUnmapWindow(w1); GrDestroyWindow(w1); GrDestroyGC(gc1); GrFreeImage(id);}static voiddo_ls(int client, char *buf){ int err; DIR *dirp; int num=0; char name[256]; buf += 2; // Skip over command while (*buf && (*buf == ' ')) buf++; if (*buf) { // Name provided strcpy(name, buf); } else { strcpy(name, "."); } fdprintf(client, "<INFO> reading directory %s\n",name); dirp = opendir( name ); if( dirp == NULL ) { fdprintf(client, "Can't open directory \"%s\"\n", name); return; } for(;;) { struct dirent *entry = readdir( dirp ); struct stat sbuf; char fullname[PATH_MAX]; if( entry == NULL ) break; num++; diag_printf("<INFO> entry %s",entry->d_name); fdprintf(client, "<INFO> entry %14s",entry->d_name); if( name[0] ) { strcpy(fullname, name ); if( !(name[0] == '/' && name[1] == 0 ) ) strcat(fullname, "/" ); } else fullname[0] = 0; strcat(fullname, entry->d_name ); err = stat( fullname, &sbuf ); if( err < 0 ) { if( errno == ENOSYS ) { fdprintf(client, " <no status available>"); } else { diag_printf(" - error %s\n", strerror(errno)); fdprintf(client, " - error %s\n", strerror(errno)); } } else { diag_printf(" [mode %08x ino %08x nlink %d size %d]\n", sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size); fdprintf(client, " [mode %08x ino %08x nlink %d size %d]\n", sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size); } } err = closedir( dirp );}static voiddo_show(int client, char *buf){ char name[256]; buf += 4; // Skip over command while (*buf && (*buf == ' ')) buf++; if (*buf) { // Name provided strcpy(name, buf); } else { fdprintf(client, "usage: show <file>\n"); return; } show_jpeg(client, name, show_time);}static voiddo_show_all(int client, char *buf){ int err; DIR *dirp; char *c, name[256]; buf += 8; // Skip over command while (*buf && (*buf == ' ')) buf++; if (*buf) { // Name provided strcpy(name, buf); } else { strcpy(name, "."); } fdprintf(client, "<INFO> show .jpg files in directory %s\n",name); dirp = opendir( name ); if( dirp == NULL ) { fdprintf(client, "Can't open directory \"%s\"\n", name); return; } for(;;) { struct dirent *entry = readdir( dirp ); struct stat sbuf; char fullname[PATH_MAX]; if( entry == NULL ) break; if( name[0] ) { strcpy(fullname, name ); if( !(name[0] == '/' && name[1] == 0 ) ) strcat(fullname, "/" ); } else fullname[0] = 0; strcat(fullname, entry->d_name ); err = stat( fullname, &sbuf ); if( err < 0 ) { fdprintf(client, "<ERROR> Can't access \"%s\":", fullname); if( errno == ENOSYS ) { fdprintf(client, " <no status available>"); } else { fdprintf(client, "%s\n", strerror(errno)); } } else {#if 0 if (/* hack: !S_ISREG(sbuf.st_mode)*/ (sbuf.st_mode & 0x8000) == 0) { continue; }#endif if ((c = rindex(fullname, '.')) != (char *)NULL) { if (strcmp(c, ".jpg") == 0) { show_jpeg(client, fullname, show_time); } } } } err = closedir( dirp );}static voiddo_time(int client, char *buf){ char *cp; int val; buf += 4; // Skip over command while (*buf && (*buf == ' ')) buf++; if (*buf) { // value provided val = strtol(buf, &cp, 0); if (val > 0) { show_time = val; fdprintf(client, "<INFO> time set to %d seconds\n", val); return; } } fdprintf(client, "usage: time <value>\n");}static voiddo_get(int client, char *buf){ char *fn, *sn, *data;#ifdef CYGPKG_FS_RAM char _fn[PATH_MAX];#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -