int main(void) { int err = dtree_open("device-tree"); halt_on_error(err, "Can not open testing device-tree"); test_list_all(); dtree_reset(); test_find_existent(); dtree_reset(); test_find_non_existent(); dtree_reset(); test_find_null(); dtree_reset(); test_find_empty(); dtree_reset(); test_find_with_discriminator(); dtree_reset(); test_find_debug(); dtree_reset(); dtree_close(); }
int main(void) { const int expect = 8; // magic number, see device-tree/ in current dir, number of name@addr dirs int err = dtree_open("device-tree"); halt_on_error(err, "Can not open testing device-tree"); test_all_dev(expect); dtree_close(); }
void init() { int old_clflag; struct fb_fix_screeninfo fixscreeninfo; struct fb_fix_screeninfo varscreeninfo; struct sigaction sa; struct vt_mode s; /* open terminal */ tty_fd = open("/dev/tty", O_RDWR); halt_on_error("/dev/tty"); /* set raw mode (and save terminal settings) */ tcgetattr(tty_fd, &term); halt_on_error("tcgetattr"); old_clflag = term.c_lflag; term.c_lflag &= ~(ICANON | ECHO); tcsetattr(tty_fd, TCSAFLUSH, &term); halt_on_error("tcsetattr"); term.c_lflag = old_clflag; /* set signal handlers */ signal(SIGINT, sig_break); ordie("SIGINT"); signal(SIGTERM, sig_break); ordie("SIGTERM"); signal(SIGABRT, sig_break); ordie("SIGABRT"); /* open framebuffer */ fb_fd = open("/dev/fb0", O_RDWR | O_NONBLOCK); ordie("/dev/fb0"); /* get some info about framebuffer */ ioctl(fb_fd, FBIOGET_VSCREENINFO, &varscreeninfo); ordie("varscreen"); if (varscreeninfo.type != FB_TYPE_VGA_PLANES) error("This program supports just vga16fb framebuffer"); ioctl(fb_fd, FBIOGET_FSCREENINFO, &fixscreeninfo); ordie("fixscreen"); /* map video memory to our memory segment */ screen_size = fixscreeninfo.smem_len; screen = mmap((void*)fixscreeninfo.smem_start, screen_size, PROT_READ|PROT_WRITE, MAP_SHARED, fb_fd, 0); halt_on_error("mmap"); /* take over virtual terminal switching (if we are running in VT) */ if (ioctl(tty_fd, VT_GETMODE, &s) == 0) { sa.sa_handler = vt_activate; sigaction(SIGUSR1, &sa, NULL); ordie("sigaction(SIGUSR1)"); sa.sa_handler = vt_release; sigaction(SIGUSR2, &sa, NULL); ordie("sigaction(SIGUSR2)"); s.mode = VT_PROCESS; s.acqsig = SIGUSR1; /* SIGUSER1 is sent on switch to our con */ s.relsig = SIGUSR2; /* SIGUSER2 is sent on switch to another con */ ioctl(tty_fd, VT_SETMODE, &s); ordie("VT_SETMODE"); } else errno = 0; /* reset errno (set by ioctl) */ /* ESC 7 -- terminal: save current state */ printf("\0337"); /* ESC [ 2 J -- terminal: erase whole screen */ printf("\033[2J"); }
int main(int argc, char* argv[]) { bool quit = false; bool refresh = true; FILE *f; char *filename; int pdx, pdy; if (argc < 2) { puts("Usage: fbi16 [file]"); exit(0); } else filename = argv[1]; init(); image = NULL; f = fopen(filename, "rb"); halt_on_error(filename); read_pgm(f); fclose(f); /* center horizontal */ if (blocks < 640/8) sdx = (640/8 - blocks)/2; else sdx = 0; /* center verical */ if (height < 480) sdy = (480 - height)/2; else sdy = 0; pdx = pdy = dx = dy = 0; while (!quit) { if (refresh || pdx != dx || pdy != dy) { show_image(dx, dy); pdx = dx; pdy = dy; refresh = false; } switch (getchar()) { case 'q': case 'Q': quit = true; break; /* scroll left */ case 's': if (blocks > 640/8) { dx += 1; if (dx > (blocks - 640/8)) dx = blocks - 640/8; } break; case 'S': if (blocks > 640/8) { dx += 2; if (dx > (blocks - 640/8)) dx = blocks - 640/8; } break; /* scroll right */ case 'a': if (blocks > 640/8) { dx -= 1; if (dx < 0) dx = 0; } break; case 'A': if (blocks > 640/8) { dx -= 2; if (dx < 0) dx = 0; } break; /* scroll down */ case 'w': if (height > 480) { dy += 10; if (dy > (height - 480)) dy = height - 480; } break; case 'W': if (height > 480) { dy += 20; if (dy > (height - 480)) dy = height - 480; } break; /* scroll up */ case 'z': if (height > 480) { dy -= 10; if (dy < 0) dy = 0; } break; case 'Z': if (height > 480) { dy -= 20; if (dy < 0) dy = 0; } break; /* refresh image */ case '\n': case 'r': case 'R': refresh = true; break; /* invert image */ case 'i': case 'I': invert_image(); refresh = true; break; } } clean(); return EXIT_SUCCESS; }
void read_pgm(FILE *f) { int maxval; uint8_t* line; uint8_t* imgpix; int y, x; int c; c = fscanf(f, "P5\n%d %d\n%d", &width, &height, &maxval); if (c < 3) error("Not a PGM file"); /*if (maxval > 255) error("16-bit PGM not supported");*/ blocks = (width+7)/8; if (maxval > 255) { line = (uint8_t*)malloc(8*blocks*2); if (line == NULL) error("malloc failed (1)"); else memset(line, 0, 8*blocks*2); } else { line = (uint8_t*)malloc(8*blocks); if (line == NULL) error("malloc failed (2)"); else memset(line, 0, 8*blocks); } image = (uint8_t*)malloc(blocks * height); if (image == NULL) error("malloc failed"); imgpix = &image[0]; /* read file line by line */ if (maxval < 256) { for (y=0; y < height; y++) { if (fread((void*)line, width, 1, f) < 1) error("Truncated PGM file"); halt_on_error("fread"); for (x=0; x < blocks; x++) *imgpix++ = ( (line[x*8 + 0] > 127 ? 0x80 : 0x00) | (line[x*8 + 1] > 127 ? 0x40 : 0x00) | (line[x*8 + 2] > 127 ? 0x20 : 0x00) | (line[x*8 + 3] > 127 ? 0x10 : 0x00) | (line[x*8 + 4] > 127 ? 0x08 : 0x00) | (line[x*8 + 5] > 127 ? 0x04 : 0x00) | (line[x*8 + 6] > 127 ? 0x02 : 0x00) | (line[x*8 + 7] > 127 ? 0x01 : 0x00) ); } } else { /* maxval >= 256 -> 2 bytes per pixel */ for (y=0; y < height; y++) { if (fread((void*)line, 2*width, 1, f) < 1) error("Truncated PGM file"); halt_on_error("fread"); for (x=0; x < blocks; x++) *imgpix++ = ( (line[x*16 + 1] > 127 ? 0x80 : 0x00) | (line[x*16 + 3] > 127 ? 0x40 : 0x00) | (line[x*16 + 5] > 127 ? 0x20 : 0x00) | (line[x*16 + 7] > 127 ? 0x10 : 0x00) | (line[x*16 + 9] > 127 ? 0x08 : 0x00) | (line[x*16 + 11] > 127 ? 0x04 : 0x00) | (line[x*16 + 13] > 127 ? 0x02 : 0x00) | (line[x*16 + 15] > 127 ? 0x01 : 0x00) ); } } free(line); }