1
0
forked from 0ad/0ad

Removes obsolete sys_cursor. Refs D2557.

sys_cursor was introduced in 7e1bcd5159 and partially in 5299dcad86.
Usage of sys_cursor was completely removed in 9a2d0f803e. At the moment
we use SDL and GL cursors.

Patch By: linkmauve
Comments By: Stan
This was SVN commit r23364.
This commit is contained in:
Vladislav Belov 2020-01-10 23:45:40 +00:00
parent 604ec667f3
commit bbcd9b34cc
7 changed files with 5 additions and 513 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2020 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -34,18 +34,8 @@
#include "lib/external_libraries/libsdl.h"
#include "lib/ogl.h"
#include "lib/res/h_mgr.h"
#include "lib/sysdep/cursor.h"
#include "ogl_tex.h"
// On Windows, allow runtime choice between system cursors and OpenGL
// cursors (Windows = more responsive, OpenGL = more consistent with what
// the game sees)
#if OS_WIN || OS_UNIX
# define ALLOW_SYS_CURSOR 1
#else
# define ALLOW_SYS_CURSOR 0
#endif
class SDLCursor
{
SDL_Surface* surface;
@ -303,7 +293,7 @@ static Status Cursor_to_string(const Cursor* c, wchar_t* buf)
// note: these standard resource interface functions are not exposed to the
// caller. all we need here is storage for the sys_cursor / GLCursor and
// caller. all we need here is storage for the SDL_Cursor / GLCursor and
// a name -> data lookup mechanism, both provided by h_mgr.
// in other words, we continually create/free the cursor resource in
// cursor_draw and trust h_mgr's caching to absorb it.

View File

@ -1,74 +0,0 @@
/* Copyright (C) 2010 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* mouse cursor
*/
#ifndef INCLUDED_SYSDEP_CURSOR
#define INCLUDED_SYSDEP_CURSOR
typedef void* sys_cursor;
/**
* Create a cursor from the given color image.
*
* @param w,h Image dimensions [pixels]. the maximum value is
* implementation-defined; 32x32 is typical and safe.
* @param bgra_img cursor image (BGRA format, bottom-up).
* It is copied and can be freed after this call returns.
* @param hx,hy 'hotspot', i.e. offset from the upper-left corner to the
* position where mouse clicks are registered.
* @param cursor Is 0 if the return code indicates failure, otherwise
* a valid cursor that must be sys_cursor_free-ed when no longer needed.
**/
extern Status sys_cursor_create(int w, int h, void* bgra_img, int hx, int hy, sys_cursor* cursor);
/**
* Create a transparent cursor (used to hide the system cursor).
*
* @param cursor is 0 if the return code indicates failure, otherwise
* a valid cursor that must be sys_cursor_free-ed when no longer needed.
**/
extern Status sys_cursor_create_empty(sys_cursor* cursor);
/**
* override the current system cursor.
*
* @param cursor can be 0 to restore the default.
**/
extern Status sys_cursor_set(sys_cursor cursor);
/**
* destroy the indicated cursor and frees its resources.
*
* @param cursor if currently in use, the default cursor is restored first.
**/
extern Status sys_cursor_free(sys_cursor cursor);
/**
* reset any cached cursor data.
* on some systems, this is needed when resetting the SDL video subsystem.
**/
extern Status sys_cursor_reset();
#endif // #ifndef INCLUDED_SYSDEP_CURSOR

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2012 Wildfire Games.
/* Copyright (C) 2020 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -23,7 +23,6 @@
#include "precompiled.h"
#include "lib/sysdep/sysdep.h"
#include "lib/sysdep/cursor.h"
#include "lib/external_libraries/libsdl.h"
@ -64,57 +63,3 @@ Status GetVideoMode(int* xres, int* yres, int* bpp, int* freq)
}
}
// stub implementation of sys_cursor* functions
// note: do not return ERR_NOT_IMPLEMENTED or similar because that
// would result in WARN_ERRs.
Status sys_cursor_create(int w, int h, void* bgra_img, int hx, int hy, sys_cursor* cursor)
{
UNUSED2(w);
UNUSED2(h);
UNUSED2(hx);
UNUSED2(hy);
UNUSED2(bgra_img);
*cursor = 0;
return INFO::OK;
}
// returns a dummy value representing an empty cursor
Status sys_cursor_create_empty(sys_cursor* cursor)
{
*cursor = (void*)1; // any non-zero value, since the cursor NULL has special meaning
return INFO::OK;
}
// replaces the current system cursor with the one indicated. need only be
// called once per cursor; pass 0 to restore the default.
Status sys_cursor_set(sys_cursor cursor)
{
if (cursor) // dummy empty cursor
SDL_ShowCursor(SDL_DISABLE);
else // restore default cursor
SDL_ShowCursor(SDL_ENABLE);
return INFO::OK;
}
// destroys the indicated cursor and frees its resources. if it is
// currently the system cursor, the default cursor is restored first.
Status sys_cursor_free(sys_cursor cursor)
{
// bail now to prevent potential confusion below; there's nothing to do.
if(!cursor)
return INFO::OK;
SDL_ShowCursor(SDL_ENABLE);
return INFO::OK;
}
Status sys_cursor_reset()
{
return INFO::OK;
}

View File

@ -1,104 +0,0 @@
/* Copyright (C) 2012 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "precompiled.h"
#import "lib/sysdep/cursor.h"
#import <AppKit/NSCursor.h>
#import <AppKit/NSImage.h>
#import <ApplicationServices/ApplicationServices.h>
//TODO: make sure these are threadsafe
Status sys_cursor_create(int w, int h, void* bgra_img, int hx, int hy, sys_cursor* cursor)
{
NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:0 pixelsWide:w pixelsHigh:h
bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:w*4 bitsPerPixel:0];
if (!bitmap)
{
debug_printf("sys_cursor_create: Error creating NSBitmapImageRep!\n");
return ERR::FAIL;
}
u8* planes[5];
[bitmap getBitmapDataPlanes:planes];
const u8* bgra = static_cast<const u8*>(bgra_img);
u8* dst = planes[0];
for (int i = 0; i < w*h*4; i += 4)
{
dst[i] = bgra[i+2];
dst[i+1] = bgra[i+1];
dst[i+2] = bgra[i];
dst[i+3] = bgra[i+3];
}
NSImage* image = [[NSImage alloc] init];
if (!image)
{
[bitmap release];
debug_printf("sys_cursor_create: Error creating NSImage!\n");
return ERR::FAIL;
}
[image addRepresentation:bitmap];
[bitmap release];
NSCursor* impl = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(hx, hy)];
[image release];
if (!impl)
{
debug_printf("sys_cursor_create: Error creating NSCursor!\n");
return ERR::FAIL;
}
*cursor = static_cast<sys_cursor>(impl);
return INFO::OK;
}
Status sys_cursor_free(sys_cursor cursor)
{
NSCursor* impl = static_cast<NSCursor*>(cursor);
[impl release];
return INFO::OK;
}
Status sys_cursor_create_empty(sys_cursor* cursor)
{
static u8 empty_bgra[] = {0, 0, 0, 0};
sys_cursor_create(1, 1, reinterpret_cast<void*>(empty_bgra), 0, 0, cursor);
return INFO::OK;
}
Status sys_cursor_set(sys_cursor cursor)
{
NSCursor* impl = static_cast<NSCursor*>(cursor);
[impl set];
return INFO::OK;
}
Status sys_cursor_reset()
{
return INFO::OK;
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2020 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -35,22 +35,17 @@
#include "lib/debug.h"
#include "lib/utf8.h"
#include "lib/sysdep/gfx.h"
#include "lib/sysdep/cursor.h"
#include "ps/VideoMode.h"
#define Cursor X__Cursor
#include <X11/Xlib.h>
#include <stdlib.h>
#include <X11/Xatom.h>
#include <X11/Xcursor/Xcursor.h>
#include "SDL.h"
#include "SDL_syswm.h"
#include <algorithm>
#undef Cursor
#undef Status
static Display *g_SDL_Display;
@ -372,120 +367,4 @@ Status sys_clipboard_set(const wchar_t *str)
return INFO::OK;
}
struct sys_cursor_impl
{
XcursorImage* image;
X__Cursor cursor;
};
static XcursorPixel cursor_pixel_to_x11_format(const XcursorPixel& bgra_pixel)
{
BOOST_STATIC_ASSERT(sizeof(XcursorPixel) == 4 * sizeof(u8));
XcursorPixel ret;
u8* dst = reinterpret_cast<u8*>(&ret);
const u8* b = reinterpret_cast<const u8*>(&bgra_pixel);
const u8 a = b[3];
for(size_t i = 0; i < 3; ++i)
*dst++ = (b[i]) * a / 255;
*dst = a;
return ret;
}
Status sys_cursor_create(int w, int h, void* bgra_img, int hx, int hy, sys_cursor* cursor)
{
debug_printf("sys_cursor_create: using Xcursor to create %d x %d cursor\n", w, h);
XcursorImage* image = XcursorImageCreate(w, h);
if(!image)
WARN_RETURN(ERR::FAIL);
const XcursorPixel* bgra_img_begin = reinterpret_cast<XcursorPixel*>(bgra_img);
std::transform(bgra_img_begin, bgra_img_begin + (w*h), image->pixels,
cursor_pixel_to_x11_format);
image->xhot = hx;
image->yhot = hy;
SDL_SysWMinfo wminfo;
if(!get_wminfo(wminfo))
WARN_RETURN(ERR::FAIL);
sys_cursor_impl* impl = new sys_cursor_impl;
impl->image = image;
impl->cursor = XcursorImageLoadCursor(wminfo.info.x11.display, image);
if(impl->cursor == None)
WARN_RETURN(ERR::FAIL);
*cursor = static_cast<sys_cursor>(impl);
return INFO::OK;
}
// returns a dummy value representing an empty cursor
Status sys_cursor_create_empty(sys_cursor* cursor)
{
static u8 transparent_bgra[] = { 0x0, 0x0, 0x0, 0x0 };
return sys_cursor_create(1, 1, static_cast<void*>(transparent_bgra), 0, 0, cursor);
}
// replaces the current system cursor with the one indicated. need only be
// called once per cursor; pass 0 to restore the default.
Status sys_cursor_set(sys_cursor cursor)
{
if(!cursor) // restore default cursor
SDL_ShowCursor(SDL_DISABLE);
else
{
SDL_SysWMinfo wminfo;
if(!get_wminfo(wminfo))
WARN_RETURN(ERR::FAIL);
if(wminfo.subsystem != SDL_SYSWM_X11)
WARN_RETURN(ERR::FAIL);
SDL_ShowCursor(SDL_ENABLE);
Window window;
if(wminfo.info.x11.window)
window = wminfo.info.x11.window;
else
WARN_RETURN(ERR::FAIL);
XDefineCursor(wminfo.info.x11.display, window,
static_cast<sys_cursor_impl*>(cursor)->cursor);
// SDL2 doesn't have a lockable event thread, so it just uses
// XSync directly instead of lock_func/unlock_func
XSync(wminfo.info.x11.display, False);
}
return INFO::OK;}
// destroys the indicated cursor and frees its resources. if it is
// currently the system cursor, the default cursor is restored first.
Status sys_cursor_free(sys_cursor cursor)
{
// bail now to prevent potential confusion below; there's nothing to do.
if(!cursor)
return INFO::OK;
sys_cursor_set(0); // restore default cursor
sys_cursor_impl* impl = static_cast<sys_cursor_impl*>(cursor);
XcursorImageDestroy(impl->image);
SDL_SysWMinfo wminfo;
if(!get_wminfo(wminfo))
return ERR::FAIL;
XFreeCursor(wminfo.info.x11.display, impl->cursor);
delete impl;
return INFO::OK;
}
Status sys_cursor_reset()
{
return INFO::OK;
}
#endif // #if HAVE_X

View File

@ -1,142 +0,0 @@
/* Copyright (C) 2018 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "precompiled.h"
#include "lib/sysdep/cursor.h"
#include "lib/sysdep/gfx.h"
#include "lib/sysdep/os/win/win.h"
#include "lib/sysdep/os/win/wutil.h"
static sys_cursor cursor_from_HICON(HICON hIcon)
{
return (sys_cursor)(uintptr_t)hIcon;
}
static sys_cursor cursor_from_HCURSOR(HCURSOR hCursor)
{
return (sys_cursor)(uintptr_t)hCursor;
}
static HICON HICON_from_cursor(sys_cursor cursor)
{
return (HICON)(uintptr_t)cursor;
}
static HCURSOR HCURSOR_from_cursor(sys_cursor cursor)
{
return (HCURSOR)(uintptr_t)cursor;
}
static Status sys_cursor_create_common(int w, int h, void* bgra_img, void* mask_img, int hx, int hy, sys_cursor* cursor)
{
*cursor = 0;
// MSDN says selecting this HBITMAP into a DC is slower since we use
// CreateBitmap; bpp/format must be checked against those of the DC.
// this is the simplest way and we don't care about slight performance
// differences because this is typically only called once.
HBITMAP hbmColor = CreateBitmap(w, h, 1, 32, bgra_img);
// CreateIconIndirect doesn't access this; we just need to pass
// an empty bitmap.
HBITMAP hbmMask = CreateBitmap(w, h, 1, 1, mask_img);
// create the cursor (really an icon; they differ only in
// fIcon and the hotspot definitions).
ICONINFO ii;
ii.fIcon = FALSE; // cursor
ii.xHotspot = (DWORD)hx;
ii.yHotspot = (DWORD)hy;
ii.hbmMask = hbmMask;
ii.hbmColor = hbmColor;
HICON hIcon = CreateIconIndirect(&ii);
// CreateIconIndirect makes copies, so we no longer need these.
DeleteObject(hbmMask);
DeleteObject(hbmColor);
if(!wutil_IsValidHandle(hIcon))
WARN_RETURN(ERR::FAIL);
*cursor = cursor_from_HICON(hIcon);
return INFO::OK;
}
Status sys_cursor_create(int w, int h, void* bgra_img, int hx, int hy, sys_cursor* cursor)
{
// alpha-blended cursors do not work on a 16-bit display
// (they get drawn as a black square), so refuse to load the
// cursor in that case
int bpp = 0;
RETURN_STATUS_IF_ERR(gfx::GetVideoMode(NULL, NULL, &bpp, NULL));
if (bpp <= 16)
return ERR::FAIL;
return sys_cursor_create_common(w, h, bgra_img, NULL, hx, hy, cursor);
}
Status sys_cursor_create_empty(sys_cursor* cursor)
{
// the mask gets ignored on 32-bit displays, but is used on 16-bit displays;
// setting it to 0xFF makes the cursor invisible (though I'm not quite
// sure why it's that way round)
u8 bgra_img[] = {0, 0, 0, 0};
u8 mask_img[] = {0xFF};
return sys_cursor_create_common(1, 1, bgra_img, mask_img, 0, 0, cursor);
}
Status sys_cursor_set(sys_cursor cursor)
{
// restore default cursor.
if(!cursor)
cursor = cursor_from_HCURSOR(LoadCursor(0, IDC_ARROW));
(void)SetCursor(HCURSOR_from_cursor(cursor));
// return value (previous cursor) is useless.
return INFO::OK;
}
Status sys_cursor_free(sys_cursor cursor)
{
// bail now to prevent potential confusion below; there's nothing to do.
if(!cursor)
return INFO::OK;
// if the cursor being freed is active, restore the default arrow
// (just for safety).
if(cursor_from_HCURSOR(GetCursor()) == cursor)
WARN_IF_ERR(sys_cursor_set(0));
if(!DestroyIcon(HICON_from_cursor(cursor)))
WARN_RETURN(StatusFromWin());
return INFO::OK;
}
Status sys_cursor_reset()
{
return INFO::OK;
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2020 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -26,7 +26,6 @@
#include "lib/file/common/file_stats.h"
#include "lib/res/h_mgr.h"
#include "lib/res/graphics/cursor.h"
#include "lib/sysdep/cursor.h"
#include "graphics/CinemaManager.h"
#include "graphics/FontMetrics.h"
@ -695,7 +694,6 @@ static void InitSDL()
static void ShutdownSDL()
{
SDL_Quit();
sys_cursor_reset();
}