📄 main.c
字号:
} } else { fprintf(stderr,"Ignoring invalid adjustment %s\n", token); } } else if(!strncmp(token,ADJ_PLL,sizeof(ADJ_PLL)-1)) { num = strtol( (nptr=token+sizeof(ADJ_PLL)-1), &eptr, 10); if(!*eptr) { if(*nptr) { adj.flags |= V2U_FLAG_VALID_PLLSHIFT; adj.pllshift = ((unsigned)num) & 0xFF; } } else { fprintf(stderr,"Ignoring invalid adjustment %s\n", token); } } else if(!strncmp(token,ADJ_FLAGS,sizeof(ADJ_FLAGS)-1)) { num = strtol( (nptr=token+sizeof(ADJ_FLAGS)-1), &eptr, 10); if(!*eptr) { if(*nptr) { adj.flags |= V2U_FLAG_VALID_GRABFLAGS; adj.grab_flags_mask = -1; adj.grab_flags = num; } } else { fprintf(stderr,"Ignoring invalid adjustment %s\n", token); } } else { fprintf(stderr,"Ignoring invalid adjustment %s\n", token); } token = strtok_r(NULL, ADJ_SEPARATOR, &tmp); } if( (result = usbfs_ioctl(fd, IOCTL_VGA2USB_SETPARAMS, &adj)) < 0 ) { goto Exit; } Exit: return result;}/** * Lists VGA modes */static int vga_list(const int fd){ int idx = 0; printf("VGA Mode List\nidx\tWxH-VF\t\tFlags\n"); while( idx < 256 ) { V2U_Property p; p.key = V2UKey_VGAMode; p.value.vgamode.idx = idx; if( usbfs_ioctl(fd, IOCTL_VGA2USB_GET_PROPERTY, &p) < 0 ) { break; } printf("%d\t%dx%d-%d\t\t0x%02X (%s)\n", idx, p.value. vgamode.vesa_mode.HorAddrTime, p.value.vgamode.vesa_mode.VerAddrTime, p.value.vgamode.vesa_mode.VerFrequency, p.value.vgamode.vesa_mode.Type, ((p.value.vgamode.vesa_mode.Type & VIDEOMODE_TYPE_VALID) && (p.value.vgamode.vesa_mode.Type & VIDEOMODE_TYPE_ENABLED) ? "ENABLED" : "DISABLED" )); idx++; } return 0;}/** * Prints VGA mode description */static int vga_get(const int fd, const char * args){ V2U_Property p; if( 1 != sscanf( args, "%u", &p.value.vgamode.idx ) ) { fprintf(stderr,"failed to parse vga mode index: '%s'", args); return -EINVAL; } p.key = V2UKey_VGAMode; if( usbfs_ioctl(fd, IOCTL_VGA2USB_GET_PROPERTY, &p) < 0 ) { fprintf(stderr,"failed to retrieve vga mode %d", p.value.vgamode.idx); return -EINVAL; } printf("idx: %d\n", p.value.vgamode.idx); printf("VerFreq: %u\n", p.value.vgamode.vesa_mode.VerFrequency); printf("HorAddrTime: %hu\n", p.value.vgamode.vesa_mode.HorAddrTime); printf("HorFrontPorch: %hu\n", p.value.vgamode.vesa_mode.HorFrontPorch); printf("HorSyncTime: %hu\n", p.value.vgamode.vesa_mode.HorSyncTime); printf("HorBackPorch: %hu\n", p.value.vgamode.vesa_mode.HorBackPorch); printf("VerAddrTime: %hu\n", p.value.vgamode.vesa_mode.VerAddrTime); printf("VerFrontPorch: %hu\n", p.value.vgamode.vesa_mode.VerFrontPorch); printf("VerSyncTime: %hu\n", p.value.vgamode.vesa_mode.VerSyncTime); printf("VerBackPorch: %hu\n", p.value.vgamode.vesa_mode.VerBackPorch); printf("Flags: %d %s%s%s%s\n", p.value.vgamode.vesa_mode.Type, (p.value.vgamode.vesa_mode.Type & VIDEOMODE_TYPE_ENABLED ? "ENABLED " : ""), (p.value.vgamode.vesa_mode.Type & VIDEOMODE_TYPE_INTERLACED ? "INTERLACED " : ""), (p.value.vgamode.vesa_mode.Type & VIDEOMODE_TYPE_HSYNCPOSITIVE ? "HSYNC_POS " : "HSYNC_NEG "), (p.value.vgamode.vesa_mode.Type & VIDEOMODE_TYPE_VSYNCPOSITIVE ? "VSYNC_POS " : "VSYNC_NEG ") ); return 0;}/** * Sets VGA mode description */static int vga_set(const int fd, const char * args){ int result; V2U_Property p; p.key = V2UKey_VGAMode; result = sscanf( args, "%u:%u:%hu:%hu:%hu:%hu:%hu:%hu:%hu:%hu:%i", &p.value.vgamode.idx, &p.value.vgamode.vesa_mode.VerFrequency, &p.value.vgamode.vesa_mode.HorAddrTime, &p.value.vgamode.vesa_mode.HorFrontPorch, &p.value.vgamode.vesa_mode.HorSyncTime, &p.value.vgamode.vesa_mode.HorBackPorch, &p.value.vgamode.vesa_mode.VerAddrTime, &p.value.vgamode.vesa_mode.VerFrontPorch, &p.value.vgamode.vesa_mode.VerSyncTime, &p.value.vgamode.vesa_mode.VerBackPorch, &p.value.vgamode.vesa_mode.Type ); if( result != 11 ) { fprintf(stderr,"failed to parse vga mode spec: %s", args); return -EINVAL; } if( usbfs_ioctl(fd, IOCTL_VGA2USB_SET_PROPERTY, &p) < 0 ) { fprintf(stderr,"failed to set vga mode"); return -errno; } return 0;}/** * Prints VGA mode description */static int vga_onoff(const int fd, int isOn, const char * args){ V2U_Property p; if( 1 != sscanf( args, "%u", &p.value.vgamode.idx ) ) { fprintf(stderr,"failed to parse vga mode index: '%s'", args); return -EINVAL; } p.key = V2UKey_VGAMode; if( usbfs_ioctl(fd, IOCTL_VGA2USB_GET_PROPERTY, &p) < 0 ) { fprintf(stderr,"failed to retrieve vga mode %d", p.value.vgamode.idx); return -errno; } if( isOn ) { p.value.vgamode.vesa_mode.Type |= VIDEOMODE_TYPE_ENABLED; } else { p.value.vgamode.vesa_mode.Type &= ~VIDEOMODE_TYPE_ENABLED; } if( usbfs_ioctl(fd, IOCTL_VGA2USB_SET_PROPERTY, &p) < 0 ) { fprintf(stderr,"failed to set vga mode"); return -errno; } return 0;}/** * */void usage() { printf("Usage: vga2usb s|c <image file> - save/saveRGB 24bit image\n"); printf("Usage: vga2usb g - grab (and discard) image\n"); printf(" vga2usb v - detect video mode\n"); printf(" vga2usb a hs=XX:vs=XX:phase=XX:gain=XX:offset=XX:pll=XX\n"); printf(" - set adjustments for vertical shift,\n"); printf(" horizontal shift, phase,\n"); printf(" offset, gain and PLL\n"); printf(" vga2usb b - get currently configured adjustments\n"); printf(" vga2usb v - detect video mode\n"); printf(" vga2usb vga-list - lists vga modes\n"); printf(" vga2usb vga-get <idx> - prints vga mode details\n"); printf(" vga2usb vga-set VGASPEC - sets vga mode details\n"); printf(" vga2usb vga-off <idx> - disables vga mode\n"); printf(" vga2usb vga-on <idx> - enables vga mode\n"); printf("\ VGASPEC string has the following syntax:\n\ idx:vfreq:hres:hfporch:hsync:hbporch:vres:vfporch:vsync:vbporch:flags\n\ idx index in VGA mode table (first few entries are reserved\n\ for custom modes, the rest is predefined VGA modes)\n\ vfreq vertical frequency in KHz\n\ hres horizontal resolution\n\ hfporch horizontal front porch\n\ hsync horizontal sync time\n\ hbporch horizontal back porch\n\ vres vertical resolution\n\ vfporch vertical front porch\n\ vsync vertical sync time\n\ vbporch vertical back porch\n\ flags combination of the follofing bit\n\ 0x%02X VGA mode entry is valid\n\ 0x%02X VGA mode is enabled\n\ 0x%02X Described video mode is interlaced mode\n\ 0x%02X Mode have positive horizontal sync\n\ 0x%02X Mode have positive vertical sync\n\", VIDEOMODE_TYPE_VALID, VIDEOMODE_TYPE_ENABLED, VIDEOMODE_TYPE_INTERLACED, VIDEOMODE_TYPE_HSYNCPOSITIVE, VIDEOMODE_TYPE_VSYNCPOSITIVE);}/** * */int main(int argc, char **argv){ int result = -EOK; int usbfd; char fname[256]; struct usb_device * usbdev = NULL; if( argc<2 ) { usage(); exit(1); } // Find the usb device and handle rest of the commands if( !(usbdev = find_vga2usb()) ) { printf("vga2usb device not found\n"); exit(1); } snprintf(fname,sizeof(fname),"/proc/bus/usb/%s/%s",usbdev->bus->dirname,usbdev->filename); usbfd = open(fname, O_RDWR); if( usbfd == -1) { perror("Open usb device: "); exit(errno); } if( 0 == strcmp(argv[1], "g" ) ) { if( (result=grab_frame(usbfd, argc==3 ? argv[2] : "test.png", FALSE, FALSE, 24)) < 0 ) { perror("Grab frame: "); goto Exit; } } else if( 0 == strcmp(argv[1], "c" ) ) { if( (result=grab_frame(usbfd, argc==3 ? argv[2] : "test.png", TRUE, TRUE, 24)) < 0 ) { perror("Grab frame: "); goto Exit; } } else if( 0 == strcmp(argv[1], "s" ) ) { if( (result=grab_frame(usbfd, argc==3 ? argv[2] : "test.png", TRUE, FALSE, 24)) < 0 ) { perror("Grab frame: "); goto Exit; } } else if( 0 == strcmp(argv[1], "a" ) ) { if(argc!=3) { printf("Wrong arguments"); goto Exit; } if( (result=set_adjustment(usbfd, argv[2])) < 0 ) { perror("Set adjustments: "); goto Exit; } } else if( 0 == strcmp(argv[1], "b" ) ) { if( (result=get_adjustment(usbfd)) < 0 ) { perror("Get adjustments: "); goto Exit; } } else if( 0 == strcmp(argv[1], "v" ) ) { if( (result=get_videomode(usbfd)) < 0 ) { perror("Get videomode: "); goto Exit; } } else if( 0 == strcmp(argv[1], "t" ) ) { if( (result=get_timings(usbfd)) < 0 ) { perror("Get timings: "); goto Exit; } } else if( 0 == strcmp(argv[1], "vga-list" ) ) { if( (result=vga_list(usbfd)) < 0 ) { perror("Get vga list: "); goto Exit; } } else if( 0 == strcmp(argv[1], "vga-get" ) ) { if(argc!=3) { printf("Wrong arguments"); goto Exit; } if( (result=vga_get(usbfd, argv[2])) < 0 ) { perror("Get vga: "); goto Exit; } } else if( 0 == strcmp(argv[1], "vga-set" ) ) { if(argc!=3) { printf("Wrong arguments"); goto Exit; } if( (result=vga_set(usbfd, argv[2])) < 0 ) { perror("Set vga: "); goto Exit; } } else if( 0 == strcmp(argv[1], "vga-off" ) ) { if(argc!=3) { printf("Wrong arguments"); goto Exit; } if( (result=vga_onoff(usbfd, FALSE, argv[2])) < 0 ) { perror("Vga off: "); goto Exit; } } else if( 0 == strcmp(argv[1], "vga-on" ) ) { if(argc!=3) { printf("Wrong arguments"); goto Exit; } if( (result=vga_onoff(usbfd, TRUE, argv[2])) < 0 ) { perror("Vga off: "); goto Exit; } } else { printf("Unknown command\n"); } Exit: close(usbfd); return result < 0 ? -result : 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -