// Creates a native cursor object from the specified image and hotspot // Cursor _glfwCreateCursor(const GLFWimage* image, int xhot, int yhot) { int i; Cursor cursor; XcursorImage* native = XcursorImageCreate(image->width, image->height); if (native == NULL) return None; native->xhot = xhot; native->yhot = yhot; unsigned char* source = (unsigned char*) image->pixels; XcursorPixel* target = native->pixels; for (i = 0; i < image->width * image->height; i++, target++, source += 4) { *target = (source[3] << 24) | (source[0] << 16) | (source[1] << 8) | source[2]; } cursor = XcursorImageLoadCursor(_glfw.x11.display, native); XcursorImageDestroy(native); return cursor; }
// Creates a native cursor object from the specified image and hotspot // Cursor _glfwCreateCursorX11(const GLFWimage* image, int xhot, int yhot) { int i; Cursor cursor; if (!_glfw.x11.xcursor.handle) return None; XcursorImage* native = XcursorImageCreate(image->width, image->height); if (native == NULL) return None; native->xhot = xhot; native->yhot = yhot; unsigned char* source = (unsigned char*) image->pixels; XcursorPixel* target = native->pixels; for (i = 0; i < image->width * image->height; i++, target++, source += 4) { unsigned int alpha = source[3]; *target = (alpha << 24) | ((unsigned char) ((source[0] * alpha) / 255) << 16) | ((unsigned char) ((source[1] * alpha) / 255) << 8) | ((unsigned char) ((source[2] * alpha) / 255) << 0); } cursor = XcursorImageLoadCursor(_glfw.x11.display, native); XcursorImageDestroy(native); return cursor; }
int _glfwPlatformCreateCursor(_GLFWcursor* cursor, const GLFWimage* image, int xhot, int yhot) { int i; XcursorImage* native = XcursorImageCreate(image->width, image->height); if (native == NULL) return GL_FALSE; native->xhot = xhot; native->yhot = yhot; unsigned char* source = (unsigned char*) image->pixels; XcursorPixel* target = native->pixels; for (i = 0; i < image->width * image->height; i++, target++, source += 4) { *target = (source[3] << 24) | (source[0] << 16) | (source[1] << 8) | source[2]; } cursor->x11.handle = XcursorImageLoadCursor(_glfw.x11.display, native); XcursorImageDestroy(native); if (cursor->x11.handle == None) return GL_FALSE; return GL_TRUE; }
JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nCreateCursor (JNIEnv *env, jclass clazz, jlong display, jint width, jint height, jint x_hotspot, jint y_hotspot, jint num_images, jobject image_buffer, jint images_offset, jobject delay_buffer, jint delays_offset) { Display *disp = (Display *)(intptr_t)display; const int *delays = NULL; if (delay_buffer != NULL) delays = (const int *)(*env)->GetDirectBufferAddress(env, delay_buffer) + delays_offset; XcursorPixel *pixels = (XcursorPixel *)(*env)->GetDirectBufferAddress(env, image_buffer) + images_offset; int stride = width*height; XcursorImages *cursor_images = XcursorImagesCreate(num_images); if (cursor_images == NULL) { throwException(env, "Could not allocate cursor."); return None; } cursor_images->nimage = num_images; int i; for (i = 0; i < num_images; i++) { XcursorImage *cursor_image = XcursorImageCreate(width, height); cursor_image->xhot = x_hotspot; cursor_image->yhot = y_hotspot; cursor_image->pixels = &(pixels[stride*i]); if (num_images > 1) cursor_image->delay = delays[i]; cursor_images->images[i] = cursor_image; } Cursor cursor = XcursorImagesLoadCursor(disp, cursor_images); XcursorImagesDestroy(cursor_images); return cursor; }
static Cursor X11_CreateXCursorCursor(SDL_Surface * surface, int hot_x, int hot_y) { Display *display = GetDisplay(); Cursor cursor = None; XcursorImage *image; image = XcursorImageCreate(surface->w, surface->h); if (!image) { SDL_OutOfMemory(); return None; } image->xhot = hot_x; image->yhot = hot_y; image->delay = 0; SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888); SDL_assert(surface->pitch == surface->w * 4); SDL_memcpy(image->pixels, surface->pixels, surface->h * surface->pitch); cursor = XcursorImageLoadCursor(display, image); XcursorImageDestroy(image); return cursor; }
int loadCursor(char *path, int hotx, int hoty, float tail_dist, float angle) { if (buildCache(path) != 0) return 1; // create a cursor image and set it wormy.xci = XcursorImageCreate (2*wormy.radius, 2*wormy.radius); wormy.xci->xhot = (2.0*wormy.radius-wormy.width)/2 + hotx; wormy.xci->yhot = (2.0*wormy.radius-wormy.height)/2 + hoty; wormy.xci->pixels = wormy.cache[0]; wormy.original = XCreateFontCursor(wormy.disp, XC_left_ptr); XDefineCursor(wormy.disp, DefaultRootWindow(wormy.disp), wormy.original); wormy.cursor = XcursorImageLoadCursor(wormy.disp, wormy.xci); XFixesChangeCursor(wormy.disp, wormy.cursor, wormy.original); // using cairo coordinates (inverted y axis) wormy.hot_a = atan2f(wormy.xci->yhot - wormy.radius, wormy.xci->xhot - wormy.radius); wormy.hot_d = sqrt((wormy.xci->xhot - wormy.radius) * (wormy.xci->xhot - wormy.radius) + (wormy.xci->yhot - wormy.radius) * (wormy.xci->yhot - wormy.radius)); wormy.tail_d = tail_dist; wormy.angle = angle * M_PI / 180.0; return 0; }
ALLEGRO_MOUSE_CURSOR *_al_xwin_create_mouse_cursor(ALLEGRO_BITMAP *bmp, int x_focus, int y_focus) { ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver(); Display *xdisplay = system->x11display; int bmp_w; int bmp_h; ALLEGRO_MOUSE_CURSOR_XGLX *xcursor; XcursorImage *image; int c, ix, iy; bool was_locked; bmp_w = al_get_bitmap_width(bmp); bmp_h = al_get_bitmap_height(bmp); xcursor = al_malloc(sizeof *xcursor); if (!xcursor) { return NULL; } image = XcursorImageCreate(bmp->w, bmp->h); if (image == None) { al_free(xcursor); return NULL; } was_locked = al_is_bitmap_locked(bmp); if (!was_locked) { al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY); } c = 0; for (iy = 0; iy < bmp_h; iy++) { for (ix = 0; ix < bmp_w; ix++) { ALLEGRO_COLOR col; unsigned char r, g, b, a; col = al_get_pixel(bmp, ix, iy); al_unmap_rgba(col, &r, &g, &b, &a); image->pixels[c++] = (a<<24) | (r<<16) | (g<<8) | (b); } } if (!was_locked) { al_unlock_bitmap(bmp); } image->xhot = x_focus; image->yhot = y_focus; _al_mutex_lock(&system->lock); xcursor->cursor = XcursorImageLoadCursor(xdisplay, image); _al_mutex_unlock(&system->lock); XcursorImageDestroy(image); return (ALLEGRO_MOUSE_CURSOR *)xcursor; }
void PreviewCursor::cropCursorImage(XcursorImage *&image) const { // Calculate the auto-crop rectangle QRect r(QPoint(image->width, image->height), QPoint()); XcursorPixel *pixels = image->pixels; for(int y = 0; y < int(image->height); y++) { for(int x = 0; x < int(image->width); x++) { if(*(pixels++) >> 24) { if(x < r.left()) r.setLeft(x); if(x > r.right()) r.setRight(x); if(y < r.top()) r.setTop(y); if(y > r.bottom()) r.setBottom(y); } } } // Normalize the rectangle r = r.normalize(); // Don't crop the image if the size isn't going to change if(r.width() == int(image->width) && r.height() == int(image->height)) return; // Create the new image XcursorImage *cropped = XcursorImageCreate(r.width(), r.height()); XcursorPixel *src = image->pixels + r.top() * image->width + r.left(); XcursorPixel *dst = cropped->pixels; for(int y = 0; y < r.height(); y++, src += (image->width - r.width())) { for(int x = 0; x < r.width(); x++) { *(dst++) = *(src++); } } // Destroy the original XcursorImageDestroy(image); image = cropped; }
static void setcursor(uint8_t * rgba, int16_t hotx, int16_t hoty) { XcursorImage * ci = XcursorImageCreate(32, 32); unsigned i; uint32_t * dst = (uint32_t*)ci->pixels; defaultcursor(); ci->xhot = hotx; ci->yhot = hoty; for (i = 0; i < 32*32; i++) dst[i] = 0 + (rgba[i*4+3] << 24) + (rgba[i*4+0] << 16) + (rgba[i*4+1] << 8) + (rgba[i*4+2] << 0) ; g_cursor = XcursorImageLoadCursor(g_dpy, ci); XDefineCursor(g_dpy, g_win, g_cursor); XcursorImageDestroy(ci); }
void CHwX11Cursor::PushImage(int xsize, int ysize, void* mem) { xmaxsize=std::max(xmaxsize,xsize); ymaxsize=std::max(ymaxsize,ysize); XcursorImage* image = XcursorImageCreate(xsize,ysize); image->delay = (XcursorUInt)(defFrameLength*1000.0f); char* dst=(char*)image->pixels; char* src=(char*)mem; char* end=src+xsize*ysize*4; do{ dst[0]=src[2]; // B dst[1]=src[1]; // G dst[2]=src[0]; // R dst[3]=src[3]; // A dst+=4; src+=4; }while(src<end); cimages.push_back(image); }
void CHwX11Cursor::resizeImage(XcursorImage*& image, const int new_x, const int new_y) { if (int(image->width) == new_x && int(image->height) == new_y) return; const int old_x = image->width; const int old_y = image->height; XcursorImage* new_image = XcursorImageCreate(new_x, new_y); new_image->delay = image->delay; const unsigned char* src = (unsigned char*)image->pixels; unsigned char* dst = (unsigned char*)new_image->pixels; memset(dst, 0, new_x*new_y*4); for (int y = 0; y < old_y; ++y) for (int x = 0; x < old_x; ++x) for (int v = 0; v < 4; ++v) dst[(y*new_x+x)*4+v] = src[(y*old_x+x)*4+v]; XcursorImageDestroy(image); image = new_image; }
static Cursor create_cursor_from_image_data_resource(Display *dpy, Window wnd, PP_Resource image_data, int hotspot_x, int hotspot_y) { struct pp_image_data_s *id = pp_resource_acquire(image_data, PP_RESOURCE_IMAGE_DATA); if (!id) { trace_warning("%s, bad resource\n", __func__); return None; } XcursorImage *cursor_image = XcursorImageCreate(id->width, id->height); cursor_image->xhot = hotspot_x; cursor_image->yhot = hotspot_y; memcpy(cursor_image->pixels, id->data, id->stride * id->height); Cursor cursor = XcursorImageLoadCursor(dpy, cursor_image); XcursorImageDestroy(cursor_image); pp_resource_release(image_data); return cursor; }
static Bool create_cursor(Handle self, Handle icon, Point hot_spot) { #ifdef HAVE_X11_XCURSOR_XCURSOR_H DEFXX; XcursorImage* i; PIcon c = PIcon(icon); Bool kill; int x, y; XcursorPixel * dst; Byte * src_data, * src_mask; if ( hot_spot. x < 0) hot_spot. x = 0; if ( hot_spot. y < 0) hot_spot. y = 0; if ( hot_spot. x >= c-> w) hot_spot. x = c-> w - 1; if ( hot_spot. y >= c-> h) hot_spot. y = c-> h - 1; XX-> pointer_hot_spot = hot_spot; if (( i = XcursorImageCreate( c-> w, c-> h )) == NULL) { warn( "XcursorImageCreate(%d,%d) error", c->w, c->h); return false; } i-> xhot = hot_spot. x; i-> yhot = c-> h - hot_spot. y - 1; if ( c-> type != imRGB || c-> maskType != imbpp8 ) { icon = CIcon(icon)->dup(icon); kill = true; CIcon(icon)-> set_type( icon, imRGB ); CIcon(icon)-> set_maskType( icon, imbpp8 ); } else kill = false; c = PIcon(icon); src_data = c->data + c->lineSize * ( c-> h - 1 ); src_mask = c->mask + c->maskLine * ( c-> h - 1 ); dst = i->pixels; for ( y = 0; y < c-> h; y++) { Byte * s_data = src_data, * s_mask = src_mask; for ( x = 0; x < c-> w; x++) { *(dst++) = s_data[0]| (s_data[1] << 8)| (s_data[2] << 16)| (*(s_mask++) << 24) ; s_data += 3; } src_mask -= c->maskLine; src_data -= c->lineSize; } if ( kill ) Object_destroy(icon); XX-> user_pointer = XcursorImageLoadCursor(DISP, i); if ( XX-> user_pointer == None) { XcursorImageDestroy(i); warn( "error creating cursor"); return false; } XX-> user_xcursor = i; return true; #else DEFXX; Handle cursor; Bool noSZ = PIcon(icon)-> w != guts.cursor_width || PIcon(icon)-> h != guts.cursor_height; Bool noBPP = (PIcon(icon)-> type & imBPP) != 1; XColor xcb, xcw; PIcon c; if ( noSZ || noBPP) { cursor = CIcon(icon)->dup(icon); c = PIcon(cursor); if ( cursor == nilHandle) { warn( "Error duping user cursor"); return false; } if ( noSZ) { CIcon(cursor)-> stretch( cursor, guts.cursor_width, guts.cursor_height); if ( c-> w != guts.cursor_width || c-> h != guts.cursor_height) { warn( "Error stretching user cursor"); Object_destroy( cursor); return false; } } if ( noBPP) { CIcon(cursor)-> set_type( cursor, imMono); if ((c-> type & imBPP) != 1) { warn( "Error black-n-whiting user cursor"); Object_destroy( cursor); return false; } } } else cursor = icon; if ( !prima_create_icon_pixmaps( cursor, &XX-> user_p_source, &XX-> user_p_mask)) { warn( "Error creating user cursor pixmaps"); if ( noSZ || noBPP) Object_destroy( cursor); return false; } if ( noSZ || noBPP) Object_destroy( cursor); if ( hot_spot. x < 0) hot_spot. x = 0; if ( hot_spot. y < 0) hot_spot. y = 0; if ( hot_spot. x >= guts. cursor_width) hot_spot. x = guts. cursor_width - 1; if ( hot_spot. y >= guts. cursor_height) hot_spot. y = guts. cursor_height - 1; XX-> pointer_hot_spot = hot_spot; xcb. red = xcb. green = xcb. blue = 0; xcw. red = xcw. green = xcw. blue = 0xFFFF; xcb. pixel = guts. monochromeMap[0]; xcw. pixel = guts. monochromeMap[1]; xcb. flags = xcw. flags = DoRed | DoGreen | DoBlue; XX-> user_pointer = XCreatePixmapCursor( DISP, XX-> user_p_source, XX-> user_p_mask, &xcw, &xcb, hot_spot. x, guts.cursor_height - hot_spot. y - 1); if ( XX-> user_pointer == None) { warn( "error creating cursor from pixmaps"); return false; } return true; #endif }