void interrupt_callback (void *target, IOReturn result, void *refcon, void *sender, uint32_t bufferSize) { static int interrupt_counter = 0; HIDDataRef hidDataRef = (HIDDataRef) refcon; char hw_x = SCHAR_MAX, hw_y = SCHAR_MAX; /* hardware coordinates, received from mouse */ int sw_x = INT_MAX, sw_y = INT_MAX; /* software coordinates, grabbed from system mouse location */ if (!hidDataRef) { return; } if (bufferSize < 4) { return; } CGEventRef event = CGEventCreate (NULL); CGPoint point = CGEventGetLocation(event); sw_x = point.x - point0.x; sw_y = point.y - point0.y; point0.x = point.x; point0.y = point.y; if (interrupt_counter > 0) { printf(" sw: %3i x %3i\n", sw_x, sw_y); } hw_x = (char) hidDataRef->buffer[1]; hw_y = (char) hidDataRef->buffer[2]; printf("hw: %3i x %3i", hw_x, hw_y); interrupt_counter++; }
static inline CGPoint GetMouseLoc() { CGEventRef ourEvent = CGEventCreate(NULL); CGPoint point = CGEventGetLocation(ourEvent); CFRelease(ourEvent); return point; }
int MouseTool::GetMouseState(int *x, int *y){ int _x, _y; int state=0; #ifdef __APPLE__ CGEventRef event = CGEventCreate(NULL); CGPoint cursor = CGEventGetLocation(event); CFRelease(event); if(CGEventSourceButtonState(0, kCGMouseButtonLeft)) state |= MT_LBUTTON; if(CGEventSourceButtonState(0, kCGMouseButtonRight)) state |= MT_RBUTTON; if(CGEventSourceButtonState(0, kCGMouseButtonCenter)) state |= MT_MBUTTON; _x = cursor.x; _y = cursor.y; #elif __WIN32__ POINT point; GetCursorPos(&point); _x = point.x; _y = point.y; //printf("%d %d %d\n",!(GetAsyncKeyState(VK_RBUTTON) & 1<<16),GetAsyncKeyState(VK_MBUTTON),GetKeyState(VK_LBUTTON)); if(GetAsyncKeyState(VK_RBUTTON) & 1 << 16) state |= MT_RBUTTON; if(GetAsyncKeyState(VK_MBUTTON) & 1 << 16) state |= MT_MBUTTON; if(GetAsyncKeyState(VK_LBUTTON) & 1 << 16) state |= MT_LBUTTON; #else XEvent evt; XQueryPointer(dpy, root_window, &evt.xbutton.root, &evt.xbutton.root, &_x, &_y, &evt.xbutton.x, &evt.xbutton.y, &evt.xbutton.state); if(evt.xbutton.state & Button1Mask) state |= MT_RBUTTON; if(evt.xbutton.state & Button2Mask) state |= MT_MBUTTON; if(evt.xbutton.state & Button3Mask) state |= MT_LBUTTON; #endif if(x != NULL) *x = _x; if(y != NULL) *y = _y; return state; }
INLINE MMPoint getMousePos() { #if defined(IS_MACOSX) CGEventRef event = CGEventCreate(NULL); CGPoint point = CGEventGetLocation(event); CFRelease(event); return MMPointFromCGPoint(point); #elif defined(USE_X11) int x, y; /* This is all we care about. Seriously. */ Window garb1, garb2; /* Why you can't specify NULL as a parameter */ int garb_x, garb_y; /* is beyond me. */ unsigned int more_garbage; Display *display = XGetMainDisplay(); XQueryPointer(display, XDefaultRootWindow(display), &garb1, &garb2, &x, &y, &garb_x, &garb_y, &more_garbage); return MMPointMake(x, y); #elif defined(IS_WINDOWS) POINT point; GetCursorPos(&point); return MMPointFromPOINT(point); #endif }
CGPoint mouse_current_position() { CGEventRef event = CGEventCreate(nil); CGPoint point = CGEventGetLocation(event); RELEASE(event); return point; }
internal CGPoint GetCursorPos() { CGEventRef Event = CGEventCreate(NULL); CGPoint Cursor = CGEventGetLocation(Event); CFRelease(Event); return Cursor; }
int main(int argc, char *argv[]){ float maxX = atoi(argv[1]); float maxY = atoi(argv[2]); Mahalo *m = new Mahalo(); m->setup(); float tones[16] = { C4, Csharp4, D4, Dsharp4, E4, F5, Fsharp5, G5, Gsharp5, A5, Asharp5, B5, C6, Csharp6, D6, Dsharp6 }; Wave *waves[16]; SampleMixer *mix = new SampleMixer(); for(int i = 0; i < 16; i++){ waves[i] = new Wave(tones[i],m->getRate(),0.0); mix->Add(waves[i]); } m->setSampleSource(mix); m->sstart(); int fd; unsigned char n[3]; int which = 0; unsigned delay = 0; float level = 0.0; unsigned freq; fd = open(argv[1],O_RDONLY); while( read(fd,n,3) ){ CGEventRef event = CGEventCreate(NULL); CGPoint cursor = CGEventGetLocation(event); CFRelease(event); fprintf(stderr,"%f %f\n",cursor.x, cursor.y); delay = (int)((cursor.y / maxY) * 1000); freq = (int)((cursor.x / maxX) * 16); waves[freq]->setAmpVal(1.0 - (cursor.y / maxY)); usleep(delay); waves[freq]->setAmpVal(0.0); usleep(3000); } usleep(5000000); m->sstop(); }
void os_keypress(usbdevice* kb, int scancode, int down){ // Check for modifier keys and update flags int flags = 0; if(scancode == KEY_CAPSLOCK){ if(down){ if(!(kb->eflags & kCGEventFlagMaskAlphaShift)) kb->eflags |= kCGEventFlagMaskAlphaShift; else kb->eflags &= ~kCGEventFlagMaskAlphaShift; } flags = 1; } else if(scancode == KEY_LEFTSHIFT || scancode == KEY_RIGHTSHIFT){ if(down) kb->eflags |= kCGEventFlagMaskShift; else kb->eflags &= ~kCGEventFlagMaskShift; flags = 1; } else if(scancode == KEY_LEFTCTRL || scancode == KEY_RIGHTCTRL){ if(down) kb->eflags |= kCGEventFlagMaskControl; else kb->eflags &= ~kCGEventFlagMaskControl; flags = 1; } else if(scancode == KEY_LEFTMETA || scancode == KEY_RIGHTMETA){ if(down) kb->eflags |= kCGEventFlagMaskCommand; else kb->eflags &= ~kCGEventFlagMaskCommand; flags = 1; } else if(scancode == KEY_LEFTALT || scancode == KEY_RIGHTALT){ if(down) kb->eflags |= kCGEventFlagMaskAlternate; else kb->eflags &= ~kCGEventFlagMaskAlternate; flags = 1; } CGEventRef kp; if(flags){ kp = CGEventCreate(kb->event); CGEventSetType(kp, kCGEventFlagsChanged); CGEventSetIntegerValueField(kp, kCGKeyboardEventKeycode, scancode); kb->lastkeypress = -1; } else { kp = CGEventCreateKeyboardEvent(kb->event, scancode, down); kb->lastkeypress = (down ? scancode : -1); } kb->keypresstime = 0; CGEventSetFlags(kp, kb->eflags); CGEventPost(kCGHIDEventTap, kp); CFRelease(kp); }
int main(int argc, char **argv) { CGEventRef ourEvent = CGEventCreate(NULL); CGPoint ourLoc = CGEventGetLocation(ourEvent); CFRelease(ourEvent); printf("%5.0f,%5.0f\n", (float)ourLoc.x, (float)ourLoc.y); exit(0); }
void detect_window_below_cursor() { window_lst.clear(); CFArrayRef osx_window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID); if(osx_window_list) { CFIndex osx_window_count = CFArrayGetCount(osx_window_list); for(CFIndex i = 0; i < osx_window_count; ++i) { CFDictionaryRef elem = (CFDictionaryRef)CFArrayGetValueAtIndex(osx_window_list, i); window_lst.push_back(app_info()); CFDictionaryApplyFunction(elem, print_keys, NULL); } CGEventRef event = CGEventCreate(NULL); CGPoint cursor = CGEventGetLocation(event); CFRelease(event); std::cout << "Mouse Pos: " << cursor.x << ", " << cursor.y << std::endl; for(int i = 0; i < window_lst.size(); ++i) { if(window_lst[i].layer == 0) { if(cursor.x >= window_lst[i].x && cursor.x <= window_lst[i].x + window_lst[i].width && cursor.y >= window_lst[i].y && cursor.y <= window_lst[i].y + window_lst[i].height) { window_lst_focus_index = i; pid = window_lst[i].pid; GetProcessForPID(pid, &psn); SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly); break; } std::cout << "Owner: " << window_lst[i].owner << std::endl; std::cout << "Name: " << window_lst[i].name << std::endl; std::cout << "PID: " << window_lst[i].pid << std::endl; std::cout << "Layer: " << window_lst[i].layer << std::endl; std::cout << "X: " << window_lst[i].x << std::endl; std::cout << "Y: " << window_lst[i].y << std::endl; std::cout << "Width: " << window_lst[i].width << std::endl; std::cout << "Height: " << window_lst[i].height << std::endl; } } std::cout << "Keyboard focus: " << pid << std::endl; } }
int main() { int keyCode = 49; //space bar srand ( time(NULL) ); printf("User input coming in 30 seconds\n"); sleep(27); printf("3...\n"); sleep(1); printf("2...\n"); sleep(1); printf("1...\n"); sleep(1); printf("NOW!"); while(1){ CGEventRef ourEvent = CGEventCreate(NULL); CGPoint point = CGEventGetLocation(ourEvent); CGEventRef click1_down = CGEventCreateMouseEvent( NULL, kCGEventLeftMouseDown, point, kCGMouseButtonLeft ); CGEventRef click1_up = CGEventCreateMouseEvent( NULL, kCGEventLeftMouseUp, point, kCGMouseButtonLeft ); CGEventPost(kCGHIDEventTap, click1_down); sleep(0.11); CGEventPost(kCGHIDEventTap, click1_up); CFRelease(click1_up); CFRelease(click1_down); CFRelease(ourEvent); sleep(0.89); if(rand() % 100 <= 2){ //2% chance CGEventRef e = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)keyCode, true); CGEventPost(kCGSessionEventTap, e); CFRelease(e); e = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)keyCode, false); CGEventPost(kCGSessionEventTap, e); CFRelease(e); } sleep(9); } }
screen_info *GetDisplayOfMousePointer() { CGEventRef Event = CGEventCreate(NULL); CGPoint Cursor = CGEventGetLocation(Event); CFRelease(Event); std::map<unsigned int, screen_info>::iterator It; for(It = DisplayMap.begin(); It != DisplayMap.end(); ++It) { screen_info *Screen = &It->second; if(Cursor.x >= Screen->X && Cursor.x <= Screen->X + Screen->Width && Cursor.y >= Screen->Y && Cursor.y <= Screen->Y + Screen->Height) return Screen; } return NULL; }
void infoDisplays(void) { CGDisplayErr dErr; CGDisplayCount displayCount, i; CGDirectDisplayID mainDisplay; CGDisplayCount maxDisplays = MAX_DISPLAYS; CGDirectDisplayID onlineDisplays[MAX_DISPLAYS]; CGEventRef ourEvent = CGEventCreate(NULL); CGPoint ourLoc = CGEventGetLocation(ourEvent); CFRelease(ourEvent); mainDisplay = CGMainDisplayID(); dErr = CGGetOnlineDisplayList(maxDisplays, onlineDisplays, &displayCount); if (dErr != kCGErrorSuccess) { fprintf(stderr, "CGGetOnlineDisplayList: error %d.\n", dErr); exit(1); } printf("# Display_ID Resolution ____Display_Bounds____ Rotation\n"); for (i = 0; i < displayCount; i++) { CGDirectDisplayID dID = onlineDisplays[i]; printf("%-2d %10p %4lux%-4lu %5.0f %5.0f %5.0f %5.0f %3.0f %s%s%s", CGDisplayUnitNumber (dID), dID, CGDisplayPixelsWide(dID), CGDisplayPixelsHigh(dID), CGRectGetMinX (CGDisplayBounds (dID)), CGRectGetMinY (CGDisplayBounds (dID)), CGRectGetMaxX (CGDisplayBounds (dID)), CGRectGetMaxY (CGDisplayBounds (dID)), CGDisplayRotation (dID), (CGDisplayIsActive (dID)) ? "" : "[inactive]", (dID == mainDisplay) ? "[main]" : "", (CGDisplayIsBuiltin (dID)) ? "[internal]\n" : "\n"); } printf("Mouse Cursor Position: ( %5.0f , %5.0f )\n", (float)ourLoc.x, (float)ourLoc.y); exit(0); }
/** * Calculate the delta for a mouse move and add them to the event. * @param event The mouse move event (by ref). * @param point The new mouse x and y. */ void calculateDeltas(CGEventRef *event, MMPoint point) { /** * The next few lines are a workaround for games not detecting mouse moves. * See this issue for more information: * https://github.com/octalmage/robotjs/issues/159 */ CGEventRef get = CGEventCreate(NULL); CGPoint mouse = CGEventGetLocation(get); // Calculate the deltas. int64_t deltaX = point.x - mouse.x; int64_t deltaY = point.y - mouse.y; CGEventSetIntegerValueField(*event, kCGMouseEventDeltaX, deltaX); CGEventSetIntegerValueField(*event, kCGMouseEventDeltaY, deltaY); CFRelease(get); }
Purity::Vector2i Purity::Mouse::getPosition() { Vector2i ret(0,0); #ifdef __gnu_linux__ // Open a connection with the X server Display* display = XOpenDisplay(NULL); // we don't care about these but they are required ::Window root, child; int x, y; unsigned int buttons; int gx = 0; int gy = 0; XQueryPointer(display, DefaultRootWindow(display), &root, &child, &gx, &gy, &x, &y, &buttons); // Close the connection with the X server XCloseDisplay(display); ret.x = gx; ret.y = gy; #elif defined _WIN32 POINT point; GetCursorPos(&point); ret.x = point.x; ret.y = point.y; #elif defined __APPLE__ && !(TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR) auto event = CGEventCreate(nullptr); auto cursor = CGEventGetLocation(event); ret.x = cursor.x; ret.y = cursor.y; CFRelease(event); #endif return ret; }
// Event helpers static void postevent(io_connect_t event, UInt32 type, NXEventData* ev, IOOptionBits flags, IOOptionBits options, int silence_errors){ // Hack #1: IOHIDPostEvent will fail with kIOReturnNotPrivileged if the event doesn't originate from the UID that owns /dev/console // You'd think being root would be good enough. You'd be wrong. ckb-daemon needs to run as root for other reasons though // (namely, being able to seize the physical IOHIDDevices) so what we do instead is change our EUID to the appropriate owner, // post the event, and then change it right back. // Yeah... uid_t uid = 0; gid_t gid = 0; struct stat file; if(!stat("/dev/console", &file)){ uid = file.st_uid; gid = file.st_gid; } euid_guard_start; if(uid != 0) seteuid(uid); if(gid != 0) setegid(gid); IOGPoint location = {0, 0}; if((options & kIOHIDSetRelativeCursorPosition) && type != NX_MOUSEMOVED){ // Hack #2: IOHIDPostEvent will not accept relative mouse coordinates for any event other than NX_MOUSEMOVED // So we need to get the current absolute coordinates from CoreGraphics and then modify those... CGEventRef cge = CGEventCreate(nil); CGPoint loc = CGEventGetLocation(cge); CFRelease(cge); location.x = floor(loc.x + ev->mouseMove.dx); location.y = floor(loc.y + ev->mouseMove.dy); options = (options & ~kIOHIDSetRelativeCursorPosition) | kIOHIDSetCursorPosition; } kern_return_t res = IOHIDPostEvent(event, type, location, ev, kNXEventDataVersion, flags | NX_NONCOALSESCEDMASK, options); if(res != kIOReturnSuccess && !silence_errors) ckb_warn("Post event failed: %x\n", res); if(uid != 0) seteuid(0); if(gid != 0) setegid(0); euid_guard_stop; }
FREObject setCursorPos(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) { int tx = 0; int ty = 0; FREGetObjectAsInt32(argv[0], &tx); FREGetObjectAsInt32(argv[1], &ty); //as3Print("Set cursor pos \n");//,tx,ty); CGPoint point; point.x = tx; point.y = ty; CGAssociateMouseAndMouseCursorPosition(true); //CGWarpMouseCursorPosition(point); // printf("Error : %i\n",error); CGDisplayMoveCursorToPoint(CGMainDisplayID(),point); CGAssociateMouseAndMouseCursorPosition(true); CGEventRef mouse = CGEventCreateMouseEvent (NULL, kCGEventMouseMoved, CGPointMake(tx, ty),0); CGEventPost(kCGHIDEventTap, mouse); CFRelease(mouse); CGEventRef event = CGEventCreate(NULL); CGPoint cursorGet = CGEventGetLocation(event); CFRelease(event); char msg[256]; sprintf(msg,"After set, check pos %f %f\n",cursorGet.x, cursorGet.y); //as3Print(msg); FREObject result; FRENewObjectFromBool(1, &result); //FRENewObjectFromBool(true,&result); return result; }
CGPoint mousePos() { CGEventRef event = CGEventCreate(nil); CGPoint loc = CGEventGetLocation(event); CFRelease(event); return loc; }
CGPoint MouseMac::getCursorPositionCGPoint() { CGEventRef event = CGEventCreate(NULL); return CGEventGetLocation(event); }
void QGeneralSettingWidget::TPDP_OnGST(T3K_DEVICE_INFO /*devInfo*/, ResponsePart /*Part*/, unsigned short /*ticktime*/, const char */*partid*/, unsigned char /*cActionGroup*/, unsigned char cAction, unsigned short /*wFeasibleness*/, unsigned short /*x*/, unsigned short /*y*/, unsigned short /*w*/, unsigned short /*h*/, float fZoom, const char */*msg*/) { if( cAction == t3kgstNoAction || (fZoom == 0.0f || fZoom == 1.0f) /*|| cAction != t3kgstFingersMove*/ ) { if( g_fZoom != 0.0f && g_fZoom != 1.0f ) { // end gesture CGEventRef e = CGEventCreate(NULL); CGEventSetType(e, 0x1D); CGEventSetFlags(e, 0x100); CGEventSetTimestamp(e, 0); CGEventSetIntegerValueField(e, 0x6E, 0x3E); CGEventSetIntegerValueField(e, 0x75, 0x00); CGEventPost(kCGHIDEventTap, e); CFRelease(e); g_fZoom = 0.0f; qDebug() << "end gesture"; } } else if( fZoom != 0.0f && fZoom != 1.0f ) { CGEventRef e = CGEventCreate(NULL); CGEventSetType(e, 0x1D); CGEventSetFlags(e, 0x100); if( g_fZoom == 0.0f || g_fZoom == 1.0f ) { g_fZoom = fZoom; qDebug() << "begin gesture : " << g_fZoom; CGEventSetTimestamp(e, 0); CGEventSetIntegerValueField(e, 0x6E, 0x3D); CGEventSetIntegerValueField(e, 0x75, 0x08); CGEventPost(kCGHIDEventTap, e); } else { float dZoom = g_fZoom - fZoom; qDebug() << "=== " << g_fZoom << " " << fZoom << " changed Zoom : " << dZoom; if( qAbs(dZoom) >= 0.02 && qAbs(dZoom) <= 0.05 ) { CGEventSetTimestamp(e, 0); CGEventSetIntegerValueField(e, 0x6E, 0x08); CGEventSetDoubleValueField(e, 0x71, -dZoom); CGEventPost(kCGHIDEventTap, e); } g_fZoom = fZoom; } CFRelease(e); } qDebug() << "Zoom : " << fZoom << " " << cAction; // qDebug() << QString("Gesture : AG(%1), A(%2), F(%3), X(%4), Y(%5), W(%6), H(%7), ZOOM(%8), MSG(%9)").arg(cActionGroup).arg(cAction).arg(wFeasibleness) // .arg(x).arg(y).arg(w).arg(h).arg(fZoom).arg(msg); }
block_t *screen_Capture(demux_t *p_demux) { demux_sys_t *p_sys = p_demux->p_sys; screen_data_t *p_data = (screen_data_t *)p_sys->p_data; block_t *p_block; CGRect capture_rect; CGImageRef image; /* forward cursor location */ CGPoint cursor_pos; CGEventRef event = CGEventCreate(NULL); cursor_pos = CGEventGetLocation(event); CFRelease(event); cursor_pos.x -= p_data->screen_left; cursor_pos.y -= p_data->screen_top; if (p_sys->b_follow_mouse) FollowMouse(p_sys, cursor_pos.x, cursor_pos.y); capture_rect.origin.x = p_sys->i_left; capture_rect.origin.y = p_sys->i_top; capture_rect.size.width = p_data->width; capture_rect.size.height = p_data->height; /* fetch image data */ image = CGDisplayCreateImageForRect(p_data->display_id, capture_rect); if (!image) { msg_Warn(p_demux, "no image!"); return NULL; } /* create offscreen context */ if (!p_data->offscreen_context) { CGColorSpaceRef colorspace; colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); p_data->offscreen_bitmap_size = p_sys->fmt.video.i_width * p_sys->fmt.video.i_height * 4; p_data->offscreen_bitmap = calloc(1, p_data->offscreen_bitmap_size); if (p_data->offscreen_bitmap == NULL) { msg_Warn(p_demux, "can't allocate offscreen bitmap"); CFRelease(image); return NULL; } p_data->offscreen_context = CGBitmapContextCreate(p_data->offscreen_bitmap, p_sys->fmt.video.i_width, p_sys->fmt.video.i_height, 8, p_sys->fmt.video.i_width * 4, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); if (!p_data->offscreen_context) { msg_Warn(p_demux, "can't create offscreen bitmap context"); CFRelease(image); return NULL; } CGColorSpaceRelease(colorspace); p_data->offscreen_rect = CGRectMake(0, 0, p_sys->fmt.video.i_width, p_sys->fmt.video.i_height); } /* fetch cursor image */ CGImageRef cursor_image; int cid = CGSMainConnectionID(); CGPoint outHotSpot; cursor_image = CGSCreateRegisteredCursorImage(cid, (char *)"com.apple.coregraphics.GlobalCurrent", &outHotSpot); /* draw screen image and cursor image */ CGRect cursor_rect; cursor_rect.size.width = CGImageGetWidth(cursor_image); cursor_rect.size.height = CGImageGetHeight(cursor_image); cursor_rect.origin.x = cursor_pos.x - p_sys->i_left - outHotSpot.x; cursor_rect.origin.y = p_data->offscreen_rect.size.height - (cursor_pos.y + cursor_rect.size.height - p_sys->i_top - outHotSpot.y); CGContextDrawImage(p_data->offscreen_context, p_data->offscreen_rect, image); CGContextDrawImage(p_data->offscreen_context, cursor_rect, cursor_image); /* build block */ p_block = block_Alloc(p_data->offscreen_bitmap_size); if (!p_block) { msg_Warn(p_demux, "can't get block"); CFRelease(image); return NULL; } memmove(p_block->p_buffer, p_data->offscreen_bitmap, p_data->offscreen_bitmap_size); CFRelease(image); return p_block; }
CGPoint MousePos () { CGEventRef ourEvent = CGEventCreate(NULL); return CGEventGetLocation(ourEvent); }