bool video::init_window(int sizex, int sizey) { assert(win_hInstance != 0); g_sizex = sizex; g_sizey = sizey; if( !WinInit(win_hInstance, win_iCmdShow, gWndClass, title, false) ) return DisplayError("Unable to initialize the program's window."); running = true; if( !DDInit() ) { DestroyDDraw(); goto fail; } if( !DDOverlayInit() || !DrawOverlay() ) { DestroyOverlay(); DestroyDDraw(); goto fail; } DDPIXELFORMAT PixelFormat; memset(&PixelFormat, 0, sizeof(PixelFormat)); PixelFormat.dwSize = sizeof(PixelFormat); g_pDDSOverlay->GetPixelFormat(&PixelFormat); mask2bits(PixelFormat.dwRBitMask, red_mask, red_shift); mask2bits(PixelFormat.dwGBitMask, green_mask, green_shift); mask2bits(PixelFormat.dwBBitMask, blue_mask, blue_shift); if(PixelFormat.dwFlags == DDPF_RGB) depth = depth_t(PixelFormat.dwRGBBitCount); else depth = -depth_t(PixelFormat.dwFourCC); for(int i = 0, e = sizex * sizey * PixelFormat.dwRGBBitCount / 32, c = get_color(0, 0, 0); i < e; i++) g_pImg[i] = c; // clear surface ShowWindow(g_hAppWnd, SW_SHOW); g_hVSync = CreateThread ( NULL, // LPSECURITY_ATTRIBUTES security_attrs 0, // SIZE_T stacksize (LPTHREAD_START_ROUTINE) thread_vsync, this, // argument 0, 0); SetPriorityClass(g_hVSync, IDLE_PRIORITY_CLASS); // questionable return true; fail: g_pImg = new unsigned int[g_sizex * g_sizey]; return false; }
static int GGIopen(ggi_visual * vis, struct ggi_dlhandle *dlh, const char *args, void *argptr, uint32_t * dlret) { int err = GGI_OK; directx_priv *priv; GGIGII ggigii; gg_option options[NUM_OPTS]; DPRINT("DirectX-target starting\n"); memcpy(options, optlist, sizeof(options)); priv = malloc(sizeof(directx_priv)); if (priv == NULL) { err = GGI_ENOMEM; goto err0; } if ((LIBGGI_GC(vis) = malloc(sizeof(ggi_gc))) == NULL) { err = GGI_ENOMEM; goto err1; } memset(priv, 0, sizeof(directx_priv)); LIBGGI_PRIVATE(vis) = priv; priv->cs = GGI_directx_LockCreate(); if (priv->cs == NULL) { err = GGI_ENOMEM; goto err2; } priv->spincs = GGI_directx_LockCreate(); if (priv->spincs == NULL) { err = GGI_ENOMEM; goto err3; } priv->redraw = 1; priv->setpalette = 1; priv->sizingcs = GGI_directx_LockCreate(); if (priv->sizingcs == NULL) { err = GGI_ENOMEM; goto err4; } priv->xmin = 0; priv->ymin = 0; priv->xmax = 0; priv->ymax = 0; priv->xstep = -1; priv->ystep = -1; if (args) { args = ggParseOptions(args, options, NUM_OPTS); if (args == NULL) { fprintf(stderr, "display-directx: error in " "arguments.\n"); } } if (_ggi_physz_parse_option(options[OPT_PHYSZ].result, &(priv->physzflags), &(priv->physz))) { err = GGI_EARGINVAL; goto err5; } if (options[OPT_KEEPCURSOR].result[0] == 'n') { priv->cursortype = (options[OPT_NOCURSOR].result[0] == 'n') ? 1 : 0; } else { priv->cursortype = 2; } if (options[OPT_INWIN].result[0] != 'n') { if (strcmp(options[OPT_INWIN].result, "root")) { priv->hParent = (HANDLE) strtoul(options[OPT_INWIN].result, NULL, 0); if (!IsWindow(priv->hParent)) { fprintf(stderr, "0x%08x " "is not a valid window handle.\n", (unsigned) priv->hParent); priv->hParent = NULL; } } else priv->hParent = GetDesktopWindow(); } if (options[OPT_FULLSCREEN].result[0] == 'n') { priv->fullscreen = 0; } else { priv->fullscreen = 1; } if (!DDInit(vis)) { err = GGI_ENODEVICE; goto err3; } ggigii.hWnd = priv->hWnd; ggigii.hInstance = priv->hInstance; if (tolower((uint8_t) options[OPT_NOINPUT].result[0]) == 'n' && /* FIXME: dxinput doesn't work with -inwin yet; the following condition disables the default input target if -inwin has been specified */ (!priv->hParent || getenv("GGI_INPUT") || getenv("GGI_INPUT_directx"))) { gii_input *inp; inp = giiOpen("directx", &ggigii, NULL); if (inp == NULL) { DPRINT_MISC("Unable to open directx inputlib\n"); GGIclose(vis, dlh); err = GGI_ENODEVICE; goto err3; } priv->inp = inp; /* Now join the new event source in. */ vis->input = giiJoinInputs(vis->input, inp); } else { priv->inp = NULL; } vis->opdisplay->setmode = GGI_directx_setmode; vis->opdisplay->getmode = GGI_directx_getmode; vis->opdisplay->setflags = GGI_directx_setflags; vis->opdisplay->checkmode = GGI_directx_checkmode; vis->opdisplay->flush = GGI_directx_flush; vis->opdisplay->getapi = GGI_directx_getapi; *dlret = GGI_DL_OPDISPLAY | GGI_DL_OPDRAW; return GGI_OK; err5: GGI_directx_LockDestroy(priv->cs); err4: GGI_directx_LockDestroy(priv->spincs); err3: GGI_directx_LockDestroy(priv->sizingcs); err2: free(LIBGGI_GC(vis)); err1: free(priv); err0: return err; }
int main(int argc, char **argv) { int fd; int count; int index; struct termios oldtio,newtio; Uint8 buffer[257]; // temporary buffer for reads, largest response is a DATA response from an N64 DexDrive Uint32 length; DDType type; // detected type DDResponse response; char *tmp; Uint32 framesdump; // frames to dump (size of card) Uint32 framesize; // size of each frame Uint16 i, j; FILE *outfile; struct timespec wait; Uint32 barsize; // current length of the progress bar Uint32 barstep; // pages per each progress bar char char *animation; Uint32 animlength; char *serialport = "/dev/ttyUSB0"; char *memcardfile ="memcard.mcr"; int blinklight = FALSE; //animation = ".oOo. "; animation = "-\\|/"; animlength = strlen(animation); while((count = getopt (argc, argv, "bdf:hs:")) != -1) switch(count) { case 'b': blinklight = TRUE; break; case 'd': debug = TRUE; break; case 'f': memcardfile = optarg; break; case 'h': show_help(); exit(-1); break; case 's': serialport = optarg; break; case '?': if ((optopt == 'f')||(optopt == 'm')) fprintf(stderr, "Option '-%c' requires an argument.\n", optopt); else if (isprint (optopt)) fprintf(stderr, "Unknown option '-%c'.\n", optopt); else fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt); exit(-1); default: abort(); } if (debug) { fprintf(stdout, "DEBUG: serialport = %s, memcardfile = %s, blinklight = %d, debug = %d\n", serialport, memcardfile, blinklight, debug); } for (index = optind; index < argc; index++) { fprintf(stderr, "Non-option argument %s\n", argv[index]); exit(-1); } if (access(memcardfile, F_OK) == 0) { fprintf(stderr, "File %s already exist, aborting.\n", memcardfile); exit(-1); } fd = open(serialport, O_RDWR | O_NOCTTY ); if (fd < 0) { perror(serialport); cleanup(fd, oldtio, -1); } tcgetattr(fd,&oldtio); /* save current port settings */ bzero(&newtio, sizeof(newtio)); newtio.c_cflag = B38400 | CS8 | CLOCAL | CREAD ; // 38400 baud 8 bits no parity 1 stop bit newtio.c_iflag = IGNPAR | IXON | IXOFF; newtio.c_oflag = 0; /* set input mode (non-canonical, no echo,...) */ newtio.c_lflag = 0; newtio.c_cc[VTIME] = 100; //Some operations are slow so give the device //a lot of time to complete (10 seconds) newtio.c_cc[VMIN] = 0; //Get whatever is available by the timeout tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); response = DDInit(fd, buffer, &length); if(response != ID) { tmp = DDStrResponse(response); fprintf(stderr, "Couldn't initialize device: %s.\n", tmp); free(tmp); cleanup(fd, oldtio, -1); } if(length != 5) { fprintf(stderr, "Device returned incomplete init response.\n"); cleanup(fd, oldtio, -1); } else { tmp = HexOut(buffer, 5); fprintf(stdout, "Device initialized, returned %s\n", tmp); free(tmp); if(memcmp(&buffer[1], "PSX", 3) == 0) { fprintf(stdout, "Playstation DexDrive detected.\n"); type = PSX; framesdump = 0x400; framesize = 128; } else if(memcmp(&buffer[1], "N64", 3) == 0) { fprintf(stdout, "Nintendo 64 DexDrive detected.\n"); type = N64; framesdump = 0x80; framesize = 256; } else { fprintf(stderr, "Unknown DexDrive detected.\n"); cleanup(fd, oldtio, -1); } barstep = framesdump / BARMAXSIZE; } response = DDHandshake(fd); if(response == ERROR) { if(type == PSX) { fprintf(stdout, "Handshake returned expected ERROR response for a Playstation dexdrive.\n"); } else { fprintf(stderr, "N64 DexDrive returned ERROR in response to handshake.\n"); cleanup(fd, oldtio, -1); } } response = DDStatus(fd, buffer, &length); fprintf(stdout, "%s.\n", DDStrResponse(response)); switch(response) { case NOCARD: cleanup(fd, oldtio, -1); break; case CARD_NEW: break; case CARD: if(type == PSX && length == 1) { switch(buffer[0]) { case 0x00: fprintf(stdout, "Playstation DexDrive reports a successful write since last reset.\n"); break; case 0x10: fprintf(stdout, "Playstation DexDrive reports no successful writes since last reset.\n"); break; default: fprintf(stderr, "Playstation DexDrive returned an unknown status: %X.", buffer[0]); break; } } break; default: cleanup(fd, oldtio, -1); break; } outfile = fopen(memcardfile, "wb"); if(outfile == NULL) { fprintf(stderr, "Couldn't open file for writing.\n"); cleanup(fd, oldtio, -1); } wait.tv_sec = 0; wait.tv_nsec = 10000000; for(i = 0; i < framesdump; i++) { //print progress bar fputc('\r', stderr); fputc('|', stderr); barsize = i / barstep; for(j = 0; j < barsize; j++) { fputc('=', stderr); } for(j = 0; j < BARMAXSIZE - barsize; j++) { fputc('-', stderr); } fprintf(stderr, "| %c %i/%i", animation[i % animlength], i + 1, framesdump); if (debug) { fprintf(stdout, "\n"); } if (blinklight) { response = DDLight(fd, 1); } response = DDRead(fd, i, buffer, &length, framesize + 1); if(length < framesize + 1) { fprintf(stderr, "Incomplete DATA response, expected %d bytes, got %d.\n", framesize + 1, length); if (blinklight) { response = DDLight(fd, 0); } cleanup(fd, oldtio, -1); } switch(response) { case NOCARD: fprintf(stderr, "No card is inserted or card removed.\n"); if (blinklight) { response = DDLight(fd, 0); } cleanup(fd, oldtio, -1); break; case DATA: fwrite(buffer, sizeof(Uint8), framesize, outfile); break; default: tmp = DDStrResponse(response); fprintf(stderr, "Got an unexpected response: %s.\n", tmp); free(tmp); if (blinklight) { response = DDLight(fd, 0); } cleanup(fd, oldtio, -1); break; } if (blinklight) { response = DDLight(fd, 0); } nanosleep(&wait, NULL); } fclose(outfile); fprintf(stdout, "\nDump complete and saved as %s\n", memcardfile); cleanup(fd, oldtio, 0); }
int main(int argc, char **argv) { int fd; struct termios oldtio,newtio; Uint8 buffer[257]; // temporary buffer for reads, largest response is a DATA response from an N64 DexDrive Uint32 length; DDType type; // detected type DDResponse response; char *tmp; Uint32 framesdump; // frames to dump (size of card) Uint32 framesize; // size of each frame Uint16 i, j; FILE *outfile; struct timespec wait; Uint32 barsize; // current length of the progress bar Uint32 barstep; // pages per each progress bar char char *animation; Uint32 animlength; animation = ".oOo. "; animlength = strlen(animation); if(argc < 2) { fprintf(stderr, "USAGE: %s <serial device> [output file]\n", argv[0]); exit(-1); } else { fd = open(argv[1], O_RDWR | O_NOCTTY ); } if (fd < 0) { perror(argv[1]); cleanup(fd, oldtio, -1); } tcgetattr(fd,&oldtio); /* save current port settings */ bzero(&newtio, sizeof(newtio)); newtio.c_cflag = B38400 | CS8 | CLOCAL | CREAD ; // 38400 baud 8 bits no parity 1 stop bit newtio.c_iflag = IGNPAR | IXON | IXOFF; newtio.c_oflag = 0; /* set input mode (non-canonical, no echo,...) */ newtio.c_lflag = 0; newtio.c_cc[VTIME] = 100; //Some operations are slow so give the device //a lot of time to complete (10 seconds) newtio.c_cc[VMIN] = 0; //Get whatever is available by the timeout tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); response = DDInit(fd, buffer, &length); if(response != ID) { tmp = DDStrResponse(response); fprintf(stderr, "Couldn't initialize device: %s.\n", tmp); free(tmp); cleanup(fd, oldtio, -1); } if(length != 5) { fprintf(stderr, "Device returned incomplete init response.\n"); cleanup(fd, oldtio, -1); } else { tmp = HexOut(buffer, 5); fprintf(stderr, "Device initialized, returned %s\n", tmp); free(tmp); if(memcmp(&buffer[1], "PSX", 3) == 0) { fprintf(stderr, "Playstation DexDrive detected.\n"); type = PSX; framesdump = 0x400; framesize = 128; } else if(memcmp(&buffer[1], "N64", 3) == 0) { fprintf(stderr, "Nintendo 64 DexDrive detected.\n"); type = N64; framesdump = 0x80; framesize = 256; } else { fprintf(stderr, "Unknown DexDrive detected.\n"); cleanup(fd, oldtio, -1); } barstep = framesdump / BARMAXSIZE; } response = DDHandshake(fd); if(response == ERROR) { if(type == PSX) { fprintf(stderr, "Handshake returned expected ERROR response for a Playstation dexdrive.\n"); } else { fprintf(stderr, "N64 DexDrive returned ERROR in response to handshake.\n"); cleanup(fd, oldtio, -1); } } response = DDStatus(fd, buffer, &length); fprintf(stderr, "%s.\n", DDStrResponse(response)); switch(response) { case NOCARD: cleanup(fd, oldtio, -1); break; case CARD_NEW: break; case CARD: if(type == PSX && length == 1) { switch(buffer[0]) { case 0x00: fprintf(stderr, "Playstation DexDrive reports a successful write since last reset.\n"); break; case 0x10: fprintf(stderr, "Playstation DexDrive reports no successful writes since last reset.\n"); break; default: fprintf(stderr, "Playstation DexDrive returned an unknown status: %X.", buffer[0]); break; } } break; default: cleanup(fd, oldtio, -1); break; } if(argc > 2) { outfile = fopen(argv[1], "wb"); } else { outfile = fopen("memcard.bin", "wb"); } if(outfile == NULL) { fprintf(stderr, "Couldn't open file for writing.\n"); cleanup(fd, oldtio, -1); } wait.tv_sec = 0; wait.tv_nsec = 10000000; for(i = 0; i < framesdump; i++) { #ifndef DEBUG fputc('\r', stderr); fputc('|', stderr); barsize = i / barstep; for(j = 0; j < barsize; j++) { fputc('=', stderr); } for(j = 0; j < BARMAXSIZE - barsize; j++) { fputc('-', stderr); } fprintf(stderr, "| %c %i/%i", animation[i % animlength], i + 1, framesdump); #endif #ifdef BLINKLIGHT response = DDLight(fd, 1); #endif response = DDRead(fd, i, buffer, &length, framesize + 1); if(length < framesize + 1) { fprintf(stderr, "Incomplete DATA response, expected %d bytes, got %d.\n", framesize + 1, length); #ifdef BLINKLIGHT response = DDLight(fd, 0); #endif cleanup(fd, oldtio, -1); } switch(response) { case NOCARD: fprintf(stderr, "No card is inserted or card removed.\n"); #ifdef BLINKLIGHT response = DDLight(fd, 0); #endif cleanup(fd, oldtio, -1); break; case DATA: fwrite(buffer, sizeof(Uint8), framesize, outfile); break; default: tmp = DDStrResponse(response); fprintf(stderr, "Got an unexpected response: %s.\n", tmp); free(tmp); #ifdef BLINKLIGHT response = DDLight(fd, 0); #endif cleanup(fd, oldtio, -1); break; } #ifdef BLINKLIGHT response = DDLight(fd, 0); #endif nanosleep(&wait, NULL); } fclose(outfile); cleanup(fd, oldtio, 0); }