void MotionDetect(SDLSurface &surface) { static Buffer <byte> img0; static Buffer <byte> img1; static bool which; static int prevLen; byte *prev, *actual; const int delta = 3; int len = int(surface.GetWidth()*surface.GetHeight()/delta); if (len != prevLen) { img0.Alloc(len); img1.Alloc(len); } if (which) { prev = img0; actual = img1; which = false; } else { prev = img1; actual = img0; which = true; } byte *preva, *actuala; actuala = actual; for (int r = 0; r < surface.GetHeight(); r+=delta) { for (int c = 0; c < surface.GetWidth(); c+=delta) *(actuala++) = Grayscale(surface.GetPixel(c, r)); } if (len != prevLen) { prevLen = len; return; } actuala = actual; preva = prev; for (int r = 0; r < surface.GetHeight(); r+=delta) { for (int c = 0; c < surface.GetWidth(); c+=delta) { if (abs(*(actuala++) - *(preva++)) > 10) surface.FillRect(c, r, delta, delta, Blue()); } } }
void Puzzle::Generate() { int n = size.cx * size.cy; box.Alloc(n); n--; for(int i = 0; i < n; i++) box[i] = i + 1; box[n] = 0; hole = size - 1; n = 20 * size.cx * size.cy; while(n > 0) n -= Move(hole.x + (rand() % 3) - 1, hole.y + (rand() % 3) - 1); moves = 0; Status(); SetRect(GetWorkArea().CenterRect(AddFrameSize(size * 32))); }
void Painter::DrawTextOp(int x, int y, int angle, const wchar *text, Font font, Color ink, int n, const int *dx) { Begin(); EvenOdd(true); if(angle) Rotate(angle * M_2PI / 36000); if(n < 0) n = wstrlen(text); double *ddx = NULL; Buffer<double> h; if(dx) { h.Alloc(n); ddx = h; for(int i = 0; i < n; i++) ddx[i] = dx[i]; } Text(x, y, text, font, n, ddx); Fill(ink); End(); }
void *SystemDraw::CursorX11(const Image& img) { GuiLock __; int q = (int64)img.GetAuxData() - 1; if(q >= 0) return (void *)XCreateFontCursor(Xdisplay, q); int len = img.GetLength(); Size sz = img.GetSize(); Pixmap pixmap = XCreatePixmap(Xdisplay, Xroot, sz.cx, sz.cy, 32); XPicture picture = XRenderCreatePicture( Xdisplay, pixmap, XRenderFindStandardFormat(Xdisplay, PictStandardARGB32), 0, 0 ); XImage ximg; sInitXImage(ximg, sz); ximg.bitmap_pad = 32; ximg.bytes_per_line = 4 * sz.cx; ximg.bits_per_pixel = 32; ximg.blue_mask = 0x00ff0000; ximg.green_mask = 0x0000ff00; ximg.red_mask = 0x000000ff; Buffer<RGBA> pma; pma.Alloc(len); memcpy(pma, ~img, len * sizeof(RGBA)); ximg.bitmap_unit = 32; ximg.depth = 32; ximg.data = (char *)~pma; XInitImage(&ximg); GC gc = XCreateGC(Xdisplay, pixmap, 0, 0); XPutImage(Xdisplay, pixmap, gc, &ximg, 0, 0, 0, 0, sz.cx, sz.cy); XFreeGC(Xdisplay, gc); XFreePixmap(Xdisplay, pixmap); Point p = img.GetHotSpot(); Cursor c = XRenderCreateCursor(Xdisplay, picture, p.x, p.y); XRenderFreePicture(Xdisplay, picture); return (void *)c; }
void LocalSlaveProcess::Open(const char *command, const char *envptr) { SVRLOG("LocalSlaveProcess::Open(" << command << ")"); Kill(); while(*command && (byte)*command <= ' ') command++; #ifdef PLATFORM_WIN32 HANDLE hOutputReadTmp, hInputRead; HANDLE hInputWriteTmp, hOutputWrite; HANDLE hErrorWrite; SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; HANDLE hp = GetCurrentProcess(); CreatePipe(&hOutputReadTmp, &hOutputWrite, &sa, 0); DuplicateHandle(hp, hOutputWrite, hp, &hErrorWrite, 0, TRUE, DUPLICATE_SAME_ACCESS); CreatePipe(&hInputRead, &hInputWriteTmp, &sa, 0); DuplicateHandle(hp, hOutputReadTmp, hp, &hOutputRead, 0, FALSE, DUPLICATE_SAME_ACCESS); DuplicateHandle(hp, hInputWriteTmp, hp, &hInputWrite, 0, FALSE, DUPLICATE_SAME_ACCESS); CloseHandle(hOutputReadTmp); CloseHandle(hInputWriteTmp); PROCESS_INFORMATION pi; STARTUPINFO si; ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; si.hStdInput = hInputRead; si.hStdOutput = hOutputWrite; si.hStdError = hErrorWrite; int n = (int)strlen(command) + 1; Buffer<char> cmd(n); memcpy(cmd, command, n); bool h = CreateProcess(NULL, cmd, &sa, &sa, TRUE, NORMAL_PRIORITY_CLASS, (void *)envptr, NULL, &si, &pi); SVRLOG("CreateProcess " << (h ? "succeeded" : "failed")); CloseHandle(hErrorWrite); CloseHandle(hInputRead); CloseHandle(hOutputWrite); if(h) { hProcess = pi.hProcess; CloseHandle(pi.hThread); } else { Free(); throw Exc(NFormat("Error running process: %s\nCommand: %s", GetErrorMessage(GetLastError()), command)); } #endif #ifdef PLATFORM_POSIX // parse command line for execve cmd_buf.Alloc(strlen(command) + 1); char *cmd_out = cmd_buf; const char *p = command; const char *b = p; while(*p && (byte)*p > ' ') if(*p++ == '\"') while(*p && *p++ != '\"') ; const char *app = cmd_out; args.Add(cmd_out); memcpy(cmd_out, b, p - b); cmd_out += p - b; *cmd_out++ = '\0'; while(*p) if((byte)*p <= ' ') p++; else { args.Add(cmd_out); while(*p && (byte)*p > ' ') if(*p == '\\') { if(*++p) *cmd_out++ = *p++; } else if(*p == '\"') { p++; while(*p && *p != '\"') if(*p == '\\') { if(*++p) *cmd_out++ = *p++; } else *cmd_out++ = *p++; if(*p == '\"') p++; } else *cmd_out++ = *p++; *cmd_out++ = '\0'; } args.Add(NULL); String app_full = GetFileOnPath(app, getenv("PATH"), true); if(IsNull(app_full)) throw Exc(Format("Cannot find executable '%s'\n", app)); if(pipe(rpipe) || pipe(wpipe)) throw Exc(NFormat(t_("pipe() error; error code = %d"), errno)); SVRLOG("\nLocalSlaveProcess::Open"); SVRLOG("rpipe[" << rpipe[0] << ", " << rpipe[1] << "]"); SVRLOG("wpipe[" << wpipe[0] << ", " << wpipe[1] << "]"); pid = fork(); SVRLOG("\tfork, pid = " << (int)pid << ", getpid = " << (int)getpid()); if(pid < 0) throw Exc(NFormat(t_("fork() error; error code = %d"), errno)); if(pid) { // parent process; clear child pipe endpoints SVRLOG("parent process - continue"); // rpipe[0] = wpipe[1] = -1; return; } SVRLOG("child process - execute application"); // rpipe[1] = wpipe[0] = -1; if(dup2(rpipe[0], 0) < 0) { // stdin SVRLOG("dup2(stdin) error: " << errno << ", " << strerror(errno)); } if(dup2(wpipe[1], 1) < 0) { // stdout SVRLOG("dup2(stdout) error: " << errno << ", " << strerror(errno)); } if(dup2(wpipe[1], 2) < 0) { // stderr SVRLOG("dup2(stderr) error: " << errno << ", " << strerror(errno)); } #if DO_SVRLOG SVRLOG(args.GetCount() << "arguments:"); for(int a = 0; a < args.GetCount(); a++) SVRLOG("[" << a << "]: <" << (args[a] ? args[a] : "NULL") << ">"); #endif//DO_SVRLOG SVRLOG("running execve, app = " << app << ", #args = " << args.GetCount()); const char *from = envptr; Vector<const char *> env; while(*from) { env.Add(from); from += strlen(from) + 1; } env.Add(NULL); execve(app_full, args.Begin(), (char *const *)env.Begin()); SVRLOG("execve failed, errno = " << errno); printf("Error running '%s', error code %d\n", command, errno); exit(-errno); #endif }