no message
This commit is contained in:
102
cocos/platform/linux/FileUtils-linux.cpp
Normal file
102
cocos/platform/linux/FileUtils-linux.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "FileUtils-linux.h"
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include "base/Log.h"
|
||||
#include "base/memory/Memory.h"
|
||||
|
||||
#define DECLARE_GUARD std::lock_guard<std::recursive_mutex> mutexGuard(_mutex)
|
||||
#ifndef CC_RESOURCE_FOLDER_LINUX
|
||||
#define CC_RESOURCE_FOLDER_LINUX ("/Resources/")
|
||||
#endif
|
||||
|
||||
namespace cc {
|
||||
|
||||
FileUtils *createFileUtils() {
|
||||
return ccnew FileUtilsLinux();
|
||||
}
|
||||
|
||||
FileUtilsLinux::FileUtilsLinux() {
|
||||
init();
|
||||
}
|
||||
|
||||
bool FileUtilsLinux::init() {
|
||||
// get application path
|
||||
char fullpath[256] = {0};
|
||||
ssize_t length = readlink("/proc/self/exe", fullpath, sizeof(fullpath) - 1);
|
||||
|
||||
if (length <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
fullpath[length] = '\0';
|
||||
ccstd::string appPath = fullpath;
|
||||
_defaultResRootPath = appPath.substr(0, appPath.find_last_of('/'));
|
||||
_defaultResRootPath.append(CC_RESOURCE_FOLDER_LINUX);
|
||||
|
||||
// Set writable path to $XDG_CONFIG_HOME or ~/.config/<app name>/ if $XDG_CONFIG_HOME not exists.
|
||||
const char *xdg_config_path = getenv("XDG_CONFIG_HOME");
|
||||
ccstd::string xdgConfigPath;
|
||||
if (xdg_config_path == nullptr) {
|
||||
xdgConfigPath = getenv("HOME");
|
||||
xdgConfigPath.append("/.config");
|
||||
} else {
|
||||
xdgConfigPath = xdg_config_path;
|
||||
}
|
||||
_writablePath = xdgConfigPath;
|
||||
_writablePath.append(appPath.substr(appPath.find_last_of('/')));
|
||||
_writablePath.append("/");
|
||||
|
||||
return FileUtils::init();
|
||||
}
|
||||
|
||||
bool FileUtilsLinux::isFileExistInternal(const ccstd::string &filename) const {
|
||||
if (filename.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ccstd::string strPath = filename;
|
||||
if (!isAbsolutePath(strPath)) { // Not absolute path, add the default root path at the beginning.
|
||||
strPath.insert(0, _defaultResRootPath);
|
||||
}
|
||||
|
||||
struct stat sts;
|
||||
return (stat(strPath.c_str(), &sts) == 0) && S_ISREG(sts.st_mode);
|
||||
}
|
||||
|
||||
ccstd::string FileUtilsLinux::getWritablePath() const {
|
||||
struct stat st;
|
||||
stat(_writablePath.c_str(), &st);
|
||||
if (!S_ISDIR(st.st_mode)) {
|
||||
mkdir(_writablePath.c_str(), 0744);
|
||||
}
|
||||
|
||||
return _writablePath;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
41
cocos/platform/linux/FileUtils-linux.h
Normal file
41
cocos/platform/linux/FileUtils-linux.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/FileUtils.h"
|
||||
namespace cc {
|
||||
class CC_DLL FileUtilsLinux : public FileUtils {
|
||||
public:
|
||||
FileUtilsLinux();
|
||||
~FileUtilsLinux() override = default;
|
||||
|
||||
bool isFileExistInternal(const ccstd::string &filename) const override;
|
||||
ccstd::string getWritablePath() const override;
|
||||
bool init() override;
|
||||
|
||||
private:
|
||||
ccstd::string _writablePath;
|
||||
};
|
||||
} // namespace cc
|
||||
113
cocos/platform/linux/LinuxPlatform.cpp
Normal file
113
cocos/platform/linux/LinuxPlatform.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "platform/linux/LinuxPlatform.h"
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "platform/SDLHelper.h"
|
||||
|
||||
#include "modules/Accelerometer.h"
|
||||
#include "modules/Battery.h"
|
||||
#include "modules/Network.h"
|
||||
#include "modules/System.h"
|
||||
#include "modules/Vibrator.h"
|
||||
|
||||
#if defined(CC_SERVER_MODE)
|
||||
#include "platform/empty/modules/Screen.h"
|
||||
#include "platform/empty/modules/SystemWindow.h"
|
||||
#include "platform/empty/modules/SystemWindowManager.h"
|
||||
#else
|
||||
#include "modules/Screen.h"
|
||||
#include "modules/SystemWindow.h"
|
||||
#include "modules/SystemWindowManager.h"
|
||||
#endif
|
||||
|
||||
#include "base/memory/Memory.h"
|
||||
|
||||
namespace {
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace cc {
|
||||
LinuxPlatform::LinuxPlatform() {
|
||||
}
|
||||
|
||||
LinuxPlatform::~LinuxPlatform() {
|
||||
}
|
||||
|
||||
int32_t LinuxPlatform::init() {
|
||||
registerInterface(std::make_shared<Accelerometer>());
|
||||
registerInterface(std::make_shared<Battery>());
|
||||
registerInterface(std::make_shared<Network>());
|
||||
registerInterface(std::make_shared<Screen>());
|
||||
registerInterface(std::make_shared<System>());
|
||||
_windowManager = std::make_shared<SystemWindowManager>();
|
||||
registerInterface(_windowManager);
|
||||
registerInterface(std::make_shared<Vibrator>());
|
||||
return _windowManager->init();
|
||||
}
|
||||
|
||||
ISystemWindow *LinuxPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) {
|
||||
return ccnew SystemWindow(windowId, externalHandle);
|
||||
}
|
||||
|
||||
static long getCurrentMillSecond() {
|
||||
long lLastTime;
|
||||
struct timeval stCurrentTime;
|
||||
|
||||
gettimeofday(&stCurrentTime, NULL);
|
||||
lLastTime = stCurrentTime.tv_sec * 1000 + stCurrentTime.tv_usec * 0.001; // milliseconds
|
||||
return lLastTime;
|
||||
}
|
||||
|
||||
void LinuxPlatform::exit() {
|
||||
_quit = true;
|
||||
}
|
||||
|
||||
int32_t LinuxPlatform::loop() {
|
||||
long lastTime = 0L;
|
||||
long curTime = 0L;
|
||||
long desiredInterval = 0L;
|
||||
long actualInterval = 0L;
|
||||
onResume();
|
||||
while (!_quit) {
|
||||
curTime = getCurrentMillSecond();
|
||||
desiredInterval = static_cast<long>(1000.0 / getFps());
|
||||
_windowManager->processEvent();
|
||||
actualInterval = curTime - lastTime;
|
||||
if (actualInterval >= desiredInterval) {
|
||||
lastTime = getCurrentMillSecond();
|
||||
runTask();
|
||||
} else {
|
||||
usleep((desiredInterval - curTime + lastTime) * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
onDestroy();
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
56
cocos/platform/linux/LinuxPlatform.h
Normal file
56
cocos/platform/linux/LinuxPlatform.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/UniversalPlatform.h"
|
||||
|
||||
namespace cc {
|
||||
class SystemWindow;
|
||||
class SystemWindowManager;
|
||||
|
||||
class CC_DLL LinuxPlatform : public UniversalPlatform {
|
||||
public:
|
||||
LinuxPlatform();
|
||||
/**
|
||||
* Destructor of WindowPlatform.
|
||||
*/
|
||||
~LinuxPlatform() override;
|
||||
/**
|
||||
* Implementation of Windows platform initialization.
|
||||
*/
|
||||
int32_t init() override;
|
||||
|
||||
void exit() override;
|
||||
int32_t loop() override;
|
||||
|
||||
ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override;
|
||||
|
||||
private:
|
||||
bool _quit{false};
|
||||
std::shared_ptr<SystemWindowManager> _windowManager{nullptr};
|
||||
std::shared_ptr<SystemWindow> _window{nullptr};
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
39
cocos/platform/linux/modules/Accelerometer.cpp
Normal file
39
cocos/platform/linux/modules/Accelerometer.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "platform/linux/modules/Accelerometer.h"
|
||||
|
||||
namespace cc {
|
||||
void Accelerometer::setAccelerometerEnabled(bool isEnabled) {
|
||||
}
|
||||
|
||||
void Accelerometer::setAccelerometerInterval(float interval) {
|
||||
}
|
||||
|
||||
const Accelerometer::MotionValue &Accelerometer::getDeviceMotionValue() {
|
||||
static MotionValue motionValue;
|
||||
return motionValue;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
49
cocos/platform/linux/modules/Accelerometer.h
Normal file
49
cocos/platform/linux/modules/Accelerometer.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IAccelerometer.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL Accelerometer : public IAccelerometer {
|
||||
public:
|
||||
/**
|
||||
* To enable or disable accelerometer.
|
||||
*/
|
||||
void setAccelerometerEnabled(bool isEnabled) override;
|
||||
|
||||
/**
|
||||
* Sets the interval of accelerometer.
|
||||
*/
|
||||
void setAccelerometerInterval(float interval) override;
|
||||
|
||||
/**
|
||||
* Gets the motion value of current device.
|
||||
*/
|
||||
const MotionValue &getDeviceMotionValue() override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
33
cocos/platform/linux/modules/Battery.cpp
Normal file
33
cocos/platform/linux/modules/Battery.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "platform/linux/modules/Battery.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
float Battery::getBatteryLevel() const {
|
||||
return 1.0F;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
36
cocos/platform/linux/modules/Battery.h
Normal file
36
cocos/platform/linux/modules/Battery.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IBattery.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL Battery : public IBattery {
|
||||
public:
|
||||
float getBatteryLevel() const override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
@@ -0,0 +1,332 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "platform/linux/modules/CanvasRenderingContext2DDelegate.h"
|
||||
#include "platform/linux/LinuxPlatform.h"
|
||||
#include "platform/linux/modules/SystemWindow.h"
|
||||
|
||||
namespace {
|
||||
#define RGB(r, g, b) (int)((int)r | (((int)g) << 8) | (((int)b) << 16))
|
||||
#define RGBA(r, g, b, a) (int)((int)r | (((int)g) << 8) | (((int)b) << 16) | (((int)a) << 24))
|
||||
} // namespace
|
||||
|
||||
namespace cc {
|
||||
//static const char gdefaultFontName[] = "-*-helvetica-medium-o-*-*-24-*-*-*-*-*-iso8859-*";
|
||||
//static const char gdefaultFontName[] = "lucidasanstypewriter-bold-24";
|
||||
static const char gdefaultFontName[] = "lucidasans-24";
|
||||
|
||||
CanvasRenderingContext2DDelegate::CanvasRenderingContext2DDelegate() {
|
||||
SystemWindow *window = BasePlatform::getPlatform()->getInterface<SystemWindow>();
|
||||
CC_ASSERT_NOT_NULL(window);
|
||||
_dis = reinterpret_cast<Display *>(window->getDisplay());
|
||||
_win = reinterpret_cast<Drawable>(window->getWindowHandle());
|
||||
}
|
||||
|
||||
CanvasRenderingContext2DDelegate::~CanvasRenderingContext2DDelegate() {
|
||||
XFreePixmap(_dis, _pixmap);
|
||||
XFreeGC(_dis, _gc);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::recreateBuffer(float w, float h) {
|
||||
_bufferWidth = w;
|
||||
_bufferHeight = h;
|
||||
if (_bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
return;
|
||||
}
|
||||
auto textureSize = static_cast<int>(_bufferWidth * _bufferHeight * 4);
|
||||
auto *data = static_cast<int8_t *>(malloc(sizeof(int8_t) * textureSize));
|
||||
memset(data, 0x00, textureSize);
|
||||
_imageData.fastSet((uint8_t *)data, textureSize);
|
||||
|
||||
if (_pixmap) {
|
||||
XFreePixmap(_dis, _pixmap);
|
||||
_pixmap = 0;
|
||||
}
|
||||
if (!_win) {
|
||||
return;
|
||||
}
|
||||
//Screen *scr = DefaultScreenOfDisplay(_dis);
|
||||
_pixmap = XCreatePixmap(_dis, _win, w, h, 32);
|
||||
_gc = XCreateGC(_dis, _pixmap, 0, 0);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::beginPath() {
|
||||
// called: set_lineWidth() -> beginPath() -> moveTo() -> lineTo() -> stroke(), when draw line
|
||||
XSetLineAttributes(_dis, _gc, static_cast<int>(_lineWidth), LineSolid, _lineCap, _lineJoin);
|
||||
XSetForeground(_dis, _gc, RGB(255, 255, 255));
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::closePath() {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::moveTo(float x, float y) {
|
||||
//MoveToEx(_DC, static_cast<int>(x), static_cast<int>(-(y - _bufferHeight - _fontSize)), nullptr);
|
||||
_x = x;
|
||||
_y = y;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::lineTo(float x, float y) {
|
||||
//LineTo(_DC, static_cast<int>(x), static_cast<int>(-(y - _bufferHeight - _fontSize)));
|
||||
XDrawLine(_dis, _pixmap, _gc, _x, _y, x, y);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::stroke() {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::saveContext() {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::restoreContext() {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::clearRect(float x, float y, float w, float h) {
|
||||
if (_bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_imageData.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
recreateBuffer(w, h);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::fillRect(float x, float y, float w, float h) {
|
||||
if (_bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
return;
|
||||
}
|
||||
|
||||
XSetForeground(_dis, _gc, _fillStyle);
|
||||
XFillRectangle(_dis, _pixmap, _gc, x, y, w, h);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::fillText(const ccstd::string &text, float x, float y, float /*maxWidth*/) {
|
||||
if (text.empty() || _bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
return;
|
||||
}
|
||||
|
||||
Point offsetPoint = convertDrawPoint(Point{x, y}, text);
|
||||
XSetForeground(_dis, _gc, 0xff000000 | _fillStyle);
|
||||
XSetFont(_dis, _gc, _font->fid);
|
||||
XDrawString(_dis, _pixmap, _gc, offsetPoint[0], offsetPoint[1], text.c_str(), (int)(text.length()));
|
||||
XImage *image = XGetImage(_dis, _pixmap, 0, 0, _bufferWidth, _bufferHeight, AllPlanes, ZPixmap);
|
||||
int width = image->width;
|
||||
int height = image->height;
|
||||
unsigned char *data = _imageData.getBytes();
|
||||
for (int y = 0; y < height; ++y) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
*(((int *)data + (y * width) + x)) = static_cast<int>(XGetPixel(image, x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::strokeText(const ccstd::string &text, float /*x*/, float /*y*/, float /*maxWidth*/) const {
|
||||
if (text.empty() || _bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
CanvasRenderingContext2DDelegate::Size CanvasRenderingContext2DDelegate::measureText(const ccstd::string &text) {
|
||||
if (text.empty())
|
||||
return ccstd::array<float, 2>{0.0f, 0.0f};
|
||||
int font_ascent = 0;
|
||||
int font_descent = 0;
|
||||
int direction = 0;
|
||||
XCharStruct overall;
|
||||
XQueryTextExtents(_dis, _font->fid, text.c_str(), text.length(), &direction, &font_ascent, &font_descent, &overall);
|
||||
return ccstd::array<float, 2>{static_cast<float>(overall.width),
|
||||
static_cast<float>(overall.ascent + overall.descent)};
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::updateFont(const ccstd::string &fontName,
|
||||
float fontSize,
|
||||
bool bold,
|
||||
bool italic,
|
||||
bool oblique,
|
||||
bool /* smallCaps */) {
|
||||
do {
|
||||
_fontName = fontName;
|
||||
_fontSize = static_cast<int>(fontSize);
|
||||
/// TODO(bug):Remove default settings
|
||||
ccstd::string fontName = "helvetica"; // default
|
||||
char serv[1024] = {0};
|
||||
ccstd::string slant = "";
|
||||
if (italic) {
|
||||
slant = "*I";
|
||||
} else if (oblique) {
|
||||
slant = "*o";
|
||||
}
|
||||
// *name-bold*Italic(Oblique)*size
|
||||
snprintf(serv, sizeof(serv) - 1, "*%s%s%s*--%d*", fontName.c_str(),
|
||||
bold ? "*Bold" : "",
|
||||
slant.c_str(),
|
||||
_fontSize);
|
||||
if (_font) {
|
||||
XFreeFont(_dis, _font);
|
||||
_font = 0;
|
||||
}
|
||||
|
||||
_font = XLoadQueryFont(_dis, serv);
|
||||
if (!_font) {
|
||||
static int fontSizes[] = {8, 10, 12, 14, 18, 24};
|
||||
int i = 0;
|
||||
int size = sizeof(fontSizes) / sizeof(fontSizes[0]);
|
||||
for (i = 0; i < size; ++i) {
|
||||
if (_fontSize < fontSizes[i]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == 0) {
|
||||
_fontSize = fontSizes[0];
|
||||
} else if (i > 1 && i < size) {
|
||||
_fontSize = fontSizes[i - 1];
|
||||
} else {
|
||||
_fontSize = fontSizes[size - 1];
|
||||
}
|
||||
snprintf(serv, sizeof(serv) - 1, "*%s*%d*", "lucidasans", _fontSize);
|
||||
_font = XLoadQueryFont(_dis, serv);
|
||||
if (!_font) {
|
||||
_font = XLoadQueryFont(_dis, serv);
|
||||
}
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setTextAlign(TextAlign align) {
|
||||
_textAlign = align;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setTextBaseline(TextBaseline baseline) {
|
||||
_textBaseLine = baseline;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setFillStyle(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||
_fillStyle = RGBA(r, g, b, a);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setStrokeStyle(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||
_strokeStyle = RGBA(r, g, b, a);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setLineWidth(float lineWidth) {
|
||||
_lineWidth = lineWidth;
|
||||
}
|
||||
|
||||
const cc::Data &CanvasRenderingContext2DDelegate::getDataRef() const {
|
||||
return _imageData;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::removeCustomFont() {
|
||||
XFreeFont(_dis, None);
|
||||
}
|
||||
|
||||
// x, y offset value
|
||||
int CanvasRenderingContext2DDelegate::drawText(const ccstd::string &text, int x, int y) {
|
||||
XTextItem item{const_cast<char *>(text.c_str()), static_cast<int>(text.length()), 0, None};
|
||||
return XDrawText(_dis, _pixmap, _gc, x, y, &item, 1);
|
||||
}
|
||||
|
||||
CanvasRenderingContext2DDelegate::Size CanvasRenderingContext2DDelegate::sizeWithText(const wchar_t *pszText, int nLen) {
|
||||
// if (text.empty())
|
||||
// return ccstd::array<float, 2>{0.0f, 0.0f};
|
||||
// XFontStruct *fs = XLoadQueryFont(dpy, "cursor");
|
||||
// CC_ASSERT(fs);
|
||||
// int font_ascent = 0;
|
||||
// int font_descent = 0;
|
||||
// XCharStruct overall;
|
||||
// XQueryTextExtents(_dis, fs -> fid, text.c_str(), text.length(), nullptr, &font_ascent, &font_descent, &overall);
|
||||
// return ccstd::array<float, 2>{static_cast<float>(overall.lbearing),
|
||||
// static_cast<float>(overall.rbearing)};
|
||||
return ccstd::array<float, 2>{0.0F, 0.0F};
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::prepareBitmap(int nWidth, int nHeight) {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::deleteBitmap() {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::fillTextureData() {
|
||||
}
|
||||
|
||||
ccstd::array<float, 2> CanvasRenderingContext2DDelegate::convertDrawPoint(Point point, const ccstd::string &text) {
|
||||
int font_ascent = 0;
|
||||
int font_descent = 0;
|
||||
int direction = 0;
|
||||
XCharStruct overall;
|
||||
XQueryTextExtents(_dis, _font->fid, text.c_str(), text.length(), &direction, &font_ascent, &font_descent, &overall);
|
||||
int width = overall.width;
|
||||
if (_textAlign == TextAlign::CENTER) {
|
||||
point[0] -= width / 2.0f;
|
||||
} else if (_textAlign == TextAlign::RIGHT) {
|
||||
point[0] -= width;
|
||||
}
|
||||
|
||||
if (_textBaseLine == TextBaseline::TOP) {
|
||||
point[1] += overall.ascent;
|
||||
} else if (_textBaseLine == TextBaseline::MIDDLE) {
|
||||
point[1] += (overall.descent - overall.ascent) / 2 - overall.descent;
|
||||
} else if (_textBaseLine == TextBaseline::BOTTOM) {
|
||||
point[1] += -overall.descent;
|
||||
} else if (_textBaseLine == TextBaseline::ALPHABETIC) {
|
||||
//point[1] -= overall.ascent;
|
||||
// X11 The default way of drawing text
|
||||
}
|
||||
|
||||
return point;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::fill() {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setLineCap(const ccstd::string &lineCap) {
|
||||
_lineCap = LineSolid;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setLineJoin(const ccstd::string &lineJoin) {
|
||||
_lineJoin = JoinRound;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::fillImageData(const Data & /* imageData */,
|
||||
float /* imageWidth */,
|
||||
float /* imageHeight */,
|
||||
float /* offsetX */,
|
||||
float /* offsetY */) {
|
||||
//XCreateImage(display, visual, DefaultDepth(display,DefaultScreen(display)), ZPixmap, 0, image32, width, height, 32, 0);
|
||||
//XPutImage(dpy, w, gc, image, 0, 0, 50, 60, 40, 30);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::strokeText(const ccstd::string & /* text */,
|
||||
float /* x */,
|
||||
float /* y */,
|
||||
float /* maxWidth */) {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::rect(float /* x */,
|
||||
float /* y */,
|
||||
float /* w */,
|
||||
float /* h */) {
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
127
cocos/platform/linux/modules/CanvasRenderingContext2DDelegate.h
Normal file
127
cocos/platform/linux/modules/CanvasRenderingContext2DDelegate.h
Normal file
@@ -0,0 +1,127 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/canvas/ICanvasRenderingContext2D.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <regex>
|
||||
#include "base/csscolorparser.h"
|
||||
#include "base/std/container/array.h"
|
||||
#include "cocos/bindings/manual/jsb_platform.h"
|
||||
#include "math/Math.h"
|
||||
#include "platform/FileUtils.h"
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xos.h>
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL CanvasRenderingContext2DDelegate : public ICanvasRenderingContext2D::Delegate {
|
||||
public:
|
||||
using Point = ccstd::array<float, 2>;
|
||||
using Vec2 = ccstd::array<float, 2>;
|
||||
using Size = ccstd::array<float, 2>;
|
||||
using Color4F = ccstd::array<float, 4>;
|
||||
using TextAlign = ICanvasRenderingContext2D::TextAlign;
|
||||
using TextBaseline = ICanvasRenderingContext2D::TextBaseline;
|
||||
CanvasRenderingContext2DDelegate();
|
||||
~CanvasRenderingContext2DDelegate() override;
|
||||
|
||||
void recreateBuffer(float w, float h) override;
|
||||
void beginPath() override;
|
||||
void closePath() override;
|
||||
void moveTo(float x, float y) override;
|
||||
void lineTo(float x, float y) override;
|
||||
void stroke() override;
|
||||
void saveContext() override;
|
||||
void restoreContext() override;
|
||||
void clearRect(float /*x*/, float /*y*/, float w, float h) override;
|
||||
void fillRect(float x, float y, float w, float h) override;
|
||||
void fillText(const ccstd::string &text, float x, float y, float /*maxWidth*/) override;
|
||||
void strokeText(const ccstd::string &text, float /*x*/, float /*y*/, float /*maxWidth*/) const;
|
||||
Size measureText(const ccstd::string &text) override;
|
||||
void updateFont(const ccstd::string &fontName, float fontSize, bool bold, bool italic, bool oblique, bool smallCaps) override;
|
||||
void setTextAlign(TextAlign align) override;
|
||||
void setTextBaseline(TextBaseline baseline) override;
|
||||
void setFillStyle(uint8_t r, uint8_t g, uint8_t b, uint8_t a) override;
|
||||
void setStrokeStyle(uint8_t r, uint8_t g, uint8_t b, uint8_t a) override;
|
||||
void setLineWidth(float lineWidth) override;
|
||||
const cc::Data &getDataRef() const override;
|
||||
void fill() override;
|
||||
void setLineCap(const ccstd::string &lineCap) override;
|
||||
void setLineJoin(const ccstd::string &lineCap) override;
|
||||
void fillImageData(const Data &imageData, float imageWidth, float imageHeight, float offsetX, float offsetY) override;
|
||||
void strokeText(const ccstd::string &text, float /*x*/, float /*y*/, float /*maxWidth*/) override;
|
||||
void rect(float x, float y, float w, float h) override;
|
||||
void updateData() override {}
|
||||
void setShadowBlur(float blur) override {}
|
||||
void setShadowColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) override {}
|
||||
void setShadowOffsetX(float offsetX) override {}
|
||||
void setShadowOffsetY(float offsetY) override {}
|
||||
|
||||
private:
|
||||
static wchar_t *utf8ToUtf16(const ccstd::string &str, int *pRetLen = nullptr);
|
||||
void removeCustomFont();
|
||||
int drawText(const ccstd::string &text, int x, int y);
|
||||
Size sizeWithText(const wchar_t *pszText, int nLen);
|
||||
void prepareBitmap(int nWidth, int nHeight);
|
||||
void deleteBitmap();
|
||||
void fillTextureData();
|
||||
ccstd::array<float, 2> convertDrawPoint(Point point, const ccstd::string &text);
|
||||
|
||||
public:
|
||||
Display *_dis{nullptr};
|
||||
int _screen{0};
|
||||
Drawable _win{0};
|
||||
Drawable _pixmap{0};
|
||||
XFontStruct *_font{0};
|
||||
GC _gc;
|
||||
|
||||
private:
|
||||
int32_t _x{0};
|
||||
int32_t _y{0};
|
||||
int32_t _lineCap{0};
|
||||
int32_t _lineJoin{0};
|
||||
|
||||
private:
|
||||
cc::Data _imageData;
|
||||
ccstd::string _curFontPath;
|
||||
int _savedDC{0};
|
||||
float _lineWidth{0.0F};
|
||||
float _bufferWidth{0.0F};
|
||||
float _bufferHeight{0.0F};
|
||||
|
||||
ccstd::string _fontName;
|
||||
int _fontSize{0};
|
||||
Size _textSize;
|
||||
TextAlign _textAlign{TextAlign::CENTER};
|
||||
TextBaseline _textBaseLine{TextBaseline::TOP};
|
||||
unsigned long _fillStyle{0};
|
||||
unsigned long _strokeStyle{0};
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
33
cocos/platform/linux/modules/Network.cpp
Normal file
33
cocos/platform/linux/modules/Network.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "platform/linux/modules/Network.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
INetwork::NetworkType Network::getNetworkType() const {
|
||||
return INetwork::NetworkType::LAN;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
36
cocos/platform/linux/modules/Network.h
Normal file
36
cocos/platform/linux/modules/Network.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/INetwork.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL Network : public INetwork {
|
||||
public:
|
||||
NetworkType getNetworkType() const override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
89
cocos/platform/linux/modules/Screen.cpp
Normal file
89
cocos/platform/linux/modules/Screen.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "platform/linux/modules/Screen.h"
|
||||
#include "base/Macros.h"
|
||||
#include "cocos/bindings/jswrapper/SeApi.h"
|
||||
// clang-format off
|
||||
// Some macros in xlib.h conflict with v8 headers.
|
||||
#include <X11/Xlib.h>
|
||||
// clang-format on
|
||||
|
||||
namespace cc {
|
||||
|
||||
int Screen::getDPI() const {
|
||||
static int dpi = -1;
|
||||
if (dpi == -1) {
|
||||
Display *dpy;
|
||||
char *displayname = NULL;
|
||||
int scr = 0; /* Screen number */
|
||||
dpy = XOpenDisplay(displayname);
|
||||
/*
|
||||
* there are 2.54 centimeters to an inch; so there are 25.4 millimeters.
|
||||
*
|
||||
* dpi = N pixels / (M millimeters / (25.4 millimeters / 1 inch))
|
||||
* = N pixels / (M inch / 25.4)
|
||||
* = N * 25.4 pixels / M inch
|
||||
*/
|
||||
double xres = ((((double)DisplayWidth(dpy, scr)) * 25.4) /
|
||||
((double)DisplayWidthMM(dpy, scr)));
|
||||
dpi = (int)(xres + 0.5);
|
||||
//printf("dpi = %d\n", dpi);
|
||||
XCloseDisplay(dpy);
|
||||
}
|
||||
return dpi;
|
||||
}
|
||||
|
||||
float Screen::getDevicePixelRatio() const {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Screen::setKeepScreenOn(bool value) {
|
||||
CC_UNUSED_PARAM(value);
|
||||
}
|
||||
|
||||
Screen::Orientation Screen::getDeviceOrientation() const {
|
||||
return Orientation::PORTRAIT;
|
||||
}
|
||||
|
||||
Vec4 Screen::getSafeAreaEdge() const {
|
||||
return cc::Vec4();
|
||||
}
|
||||
|
||||
bool Screen::isDisplayStats() {
|
||||
se::AutoHandleScope hs;
|
||||
se::Value ret;
|
||||
char commandBuf[100] = "cc.profiler.isShowingStats();";
|
||||
se::ScriptEngine::getInstance()->evalString(commandBuf, 100, &ret);
|
||||
return ret.toBoolean();
|
||||
}
|
||||
|
||||
void Screen::setDisplayStats(bool isShow) {
|
||||
se::AutoHandleScope hs;
|
||||
char commandBuf[100] = {0};
|
||||
sprintf(commandBuf, isShow ? "cc.profiler.showStats();" : "cc.profiler.hideStats();");
|
||||
se::ScriptEngine::getInstance()->evalString(commandBuf);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
50
cocos/platform/linux/modules/Screen.h
Normal file
50
cocos/platform/linux/modules/Screen.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IScreen.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL Screen : public IScreen {
|
||||
public:
|
||||
int getDPI() const override;
|
||||
float getDevicePixelRatio() const override;
|
||||
void setKeepScreenOn(bool value) override;
|
||||
Orientation getDeviceOrientation() const override;
|
||||
Vec4 getSafeAreaEdge() const override;
|
||||
/**
|
||||
@brief Get current display stats.
|
||||
@return bool, is displaying stats or not.
|
||||
*/
|
||||
bool isDisplayStats() override;
|
||||
|
||||
/**
|
||||
@brief set display stats information.
|
||||
*/
|
||||
void setDisplayStats(bool isShow) override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
132
cocos/platform/linux/modules/System.cpp
Normal file
132
cocos/platform/linux/modules/System.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "platform/linux/modules/System.h"
|
||||
#include <string.h>
|
||||
#include <sys/utsname.h>
|
||||
#include "SDL2/SDL_clipboard.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
using OSType = System::OSType;
|
||||
|
||||
OSType System::getOSType() const {
|
||||
return OSType::LINUX;
|
||||
}
|
||||
|
||||
ccstd::string System::getDeviceModel() const {
|
||||
return "Linux";
|
||||
}
|
||||
|
||||
System::LanguageType System::getCurrentLanguage() const {
|
||||
char *pLanguageName = getenv("LANG");
|
||||
if (!pLanguageName) {
|
||||
return LanguageType::ENGLISH;
|
||||
}
|
||||
strtok(pLanguageName, "_");
|
||||
if (!pLanguageName) {
|
||||
return LanguageType::ENGLISH;
|
||||
}
|
||||
|
||||
return getLanguageTypeByISO2(pLanguageName);
|
||||
}
|
||||
|
||||
ccstd::string System::getCurrentLanguageCode() const {
|
||||
static char code[3] = {0};
|
||||
char *pLanguageName = getenv("LANG");
|
||||
if (!pLanguageName) {
|
||||
return "en";
|
||||
}
|
||||
strtok(pLanguageName, "_");
|
||||
if (!pLanguageName) {
|
||||
return "en";
|
||||
}
|
||||
strncpy(code, pLanguageName, 2);
|
||||
code[2] = '\0';
|
||||
return code;
|
||||
}
|
||||
|
||||
ccstd::string System::getSystemVersion() const {
|
||||
struct utsname u;
|
||||
uname(&u);
|
||||
return u.version;
|
||||
}
|
||||
|
||||
bool System::openURL(const ccstd::string &url) {
|
||||
ccstd::string op = ccstd::string("xdg-open '").append(url).append("'");
|
||||
return system(op.c_str()) == 0;
|
||||
}
|
||||
|
||||
System::LanguageType System::getLanguageTypeByISO2(const char *code) const {
|
||||
// this function is used by all platforms to get system language
|
||||
// except windows: cocos/platform/win32/CCApplication-win32.cpp
|
||||
LanguageType ret = LanguageType::ENGLISH;
|
||||
|
||||
if (strncmp(code, "zh", 2) == 0) {
|
||||
ret = LanguageType::CHINESE;
|
||||
} else if (strncmp(code, "ja", 2) == 0) {
|
||||
ret = LanguageType::JAPANESE;
|
||||
} else if (strncmp(code, "fr", 2) == 0) {
|
||||
ret = LanguageType::FRENCH;
|
||||
} else if (strncmp(code, "it", 2) == 0) {
|
||||
ret = LanguageType::ITALIAN;
|
||||
} else if (strncmp(code, "de", 2) == 0) {
|
||||
ret = LanguageType::GERMAN;
|
||||
} else if (strncmp(code, "es", 2) == 0) {
|
||||
ret = LanguageType::SPANISH;
|
||||
} else if (strncmp(code, "nl", 2) == 0) {
|
||||
ret = LanguageType::DUTCH;
|
||||
} else if (strncmp(code, "ru", 2) == 0) {
|
||||
ret = LanguageType::RUSSIAN;
|
||||
} else if (strncmp(code, "hu", 2) == 0) {
|
||||
ret = LanguageType::HUNGARIAN;
|
||||
} else if (strncmp(code, "pt", 2) == 0) {
|
||||
ret = LanguageType::PORTUGUESE;
|
||||
} else if (strncmp(code, "ko", 2) == 0) {
|
||||
ret = LanguageType::KOREAN;
|
||||
} else if (strncmp(code, "ar", 2) == 0) {
|
||||
ret = LanguageType::ARABIC;
|
||||
} else if (strncmp(code, "nb", 2) == 0) {
|
||||
ret = LanguageType::NORWEGIAN;
|
||||
} else if (strncmp(code, "pl", 2) == 0) {
|
||||
ret = LanguageType::POLISH;
|
||||
} else if (strncmp(code, "tr", 2) == 0) {
|
||||
ret = LanguageType::TURKISH;
|
||||
} else if (strncmp(code, "uk", 2) == 0) {
|
||||
ret = LanguageType::UKRAINIAN;
|
||||
} else if (strncmp(code, "ro", 2) == 0) {
|
||||
ret = LanguageType::ROMANIAN;
|
||||
} else if (strncmp(code, "bg", 2) == 0) {
|
||||
ret = LanguageType::BULGARIAN;
|
||||
} else if (strncmp(code, "hi", 2) == 0) {
|
||||
ret = LanguageType::HINDI;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void System::copyTextToClipboard(const ccstd::string &text) {
|
||||
SDL_SetClipboardText(text.c_str());
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
69
cocos/platform/linux/modules/System.h
Normal file
69
cocos/platform/linux/modules/System.h
Normal file
@@ -0,0 +1,69 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/ISystem.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL System : public ISystem {
|
||||
public:
|
||||
/**
|
||||
@brief Get target system type.
|
||||
*/
|
||||
OSType getOSType() const override;
|
||||
/**
|
||||
@brief Get target device model.
|
||||
*/
|
||||
ccstd::string getDeviceModel() const override;
|
||||
/**
|
||||
@brief Get current language config.
|
||||
@return Current language config.
|
||||
*/
|
||||
LanguageType getCurrentLanguage() const override;
|
||||
/**
|
||||
@brief Get current language iso 639-1 code.
|
||||
@return Current language iso 639-1 code.
|
||||
*/
|
||||
ccstd::string getCurrentLanguageCode() const override;
|
||||
/**
|
||||
@brief Get system version.
|
||||
@return system version.
|
||||
*/
|
||||
ccstd::string getSystemVersion() const override;
|
||||
/**
|
||||
@brief Open url in default browser.
|
||||
@param String with url to open.
|
||||
@return True if the resource located by the URL was successfully opened; otherwise false.
|
||||
*/
|
||||
bool openURL(const ccstd::string &url) override;
|
||||
|
||||
void copyTextToClipboard(const std::string &text) override;
|
||||
|
||||
private:
|
||||
LanguageType getLanguageTypeByISO2(const char *code) const;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
103
cocos/platform/linux/modules/SystemWindow.cpp
Normal file
103
cocos/platform/linux/modules/SystemWindow.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "platform/linux/modules/SystemWindow.h"
|
||||
|
||||
#include "base/Log.h"
|
||||
#include "base/Macros.h"
|
||||
|
||||
// SDL headers
|
||||
#include <functional>
|
||||
#include "SDL2/SDL.h"
|
||||
#include "SDL2/SDL_main.h"
|
||||
#include "SDL2/SDL_syswm.h"
|
||||
#include "engine/EngineEvents.h"
|
||||
#include "platform/SDLHelper.h"
|
||||
|
||||
namespace cc {
|
||||
SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle)
|
||||
: _windowId(windowId) {
|
||||
if (externalHandle) {
|
||||
_windowHandle = reinterpret_cast<uintptr_t>(externalHandle);
|
||||
}
|
||||
}
|
||||
|
||||
SystemWindow::~SystemWindow() {
|
||||
_windowHandle = 0;
|
||||
_windowId = 0;
|
||||
}
|
||||
|
||||
bool SystemWindow::createWindow(const char *title,
|
||||
int w, int h, int flags) {
|
||||
_window = SDLHelper::createWindow(title, w, h, flags);
|
||||
if (!_window) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_width = w;
|
||||
_height = h;
|
||||
_windowHandle = SDLHelper::getWindowHandle(_window);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SystemWindow::createWindow(const char *title,
|
||||
int x, int y, int w,
|
||||
int h, int flags) {
|
||||
_window = SDLHelper::createWindow(title, x, y, w, h, flags);
|
||||
if (!_window) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_width = w;
|
||||
_height = h;
|
||||
_windowHandle = SDLHelper::getWindowHandle(_window);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SystemWindow::closeWindow() {
|
||||
#ifndef CC_SERVER_MODE
|
||||
SDL_Event et;
|
||||
et.type = SDL_QUIT;
|
||||
SDL_PushEvent(&et);
|
||||
#endif
|
||||
}
|
||||
|
||||
uintptr_t SystemWindow::getWindowHandle() const {
|
||||
return _windowHandle;
|
||||
}
|
||||
|
||||
uintptr_t SystemWindow::getDisplay() const {
|
||||
return SDLHelper::getDisplay(_window);
|
||||
}
|
||||
|
||||
void SystemWindow::setCursorEnabled(bool value) {
|
||||
SDLHelper::setCursorEnabled(value);
|
||||
}
|
||||
|
||||
SystemWindow::Size SystemWindow::getViewSize() const {
|
||||
return Size{static_cast<float>(_width), static_cast<float>(_height)};
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
73
cocos/platform/linux/modules/SystemWindow.h
Normal file
73
cocos/platform/linux/modules/SystemWindow.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "platform/interfaces/modules/ISystemWindow.h"
|
||||
|
||||
struct SDL_Window;
|
||||
namespace cc {
|
||||
class SDLHelper;
|
||||
class CC_DLL SystemWindow : public ISystemWindow {
|
||||
friend class SystemWindowManager;
|
||||
|
||||
public:
|
||||
explicit SystemWindow(uint32_t windowId, void* externalHandle);
|
||||
~SystemWindow() override;
|
||||
|
||||
bool createWindow(const char* title,
|
||||
int w, int h, int flags) override;
|
||||
bool createWindow(const char* title,
|
||||
int x, int y, int w,
|
||||
int h, int flags) override;
|
||||
void closeWindow() override;
|
||||
|
||||
virtual uint32_t getWindowId() const override { return _windowId; }
|
||||
uintptr_t getWindowHandle() const override;
|
||||
|
||||
uintptr_t getDisplay() const;
|
||||
Size getViewSize() const override;
|
||||
void setViewSize(uint32_t w, uint32_t h) override {
|
||||
_width = w;
|
||||
_height = h;
|
||||
}
|
||||
/*
|
||||
@brief enable/disable(lock) the cursor, default is enabled
|
||||
*/
|
||||
void setCursorEnabled(bool value) override;
|
||||
|
||||
private:
|
||||
SDL_Window* getSDLWindow() const { return _window; }
|
||||
|
||||
uint32_t _width{0};
|
||||
uint32_t _height{0};
|
||||
|
||||
uint32_t _windowId{0};
|
||||
uintptr_t _windowHandle{0};
|
||||
SDL_Window* _window{nullptr};
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
87
cocos/platform/linux/modules/SystemWindowManager.cpp
Normal file
87
cocos/platform/linux/modules/SystemWindowManager.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "SystemWindowManager.h"
|
||||
#include "SDL2/SDL_events.h"
|
||||
#include "platform/BasePlatform.h"
|
||||
#include "platform/SDLHelper.h"
|
||||
#include "platform/interfaces/modules/ISystemWindowManager.h"
|
||||
#include "platform/linux/modules/SystemWindow.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
int SystemWindowManager::init() {
|
||||
return SDLHelper::init();
|
||||
}
|
||||
|
||||
void SystemWindowManager::processEvent() {
|
||||
SDL_Event sdlEvent;
|
||||
while (SDL_PollEvent(&sdlEvent) != 0) {
|
||||
SDL_Window *sdlWindow = SDL_GetWindowFromID(sdlEvent.window.windowID);
|
||||
// SDL_Event like SDL_QUIT does not associate a window
|
||||
if (!sdlWindow) {
|
||||
SDLHelper::dispatchSDLEvent(0, sdlEvent);
|
||||
} else {
|
||||
ISystemWindow *window = getWindowFromSDLWindow(sdlWindow);
|
||||
CC_ASSERT(window);
|
||||
uint32_t windowId = window->getWindowId();
|
||||
SDLHelper::dispatchSDLEvent(windowId, sdlEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ISystemWindow *SystemWindowManager::createWindow(const ISystemWindowInfo &info) {
|
||||
ISystemWindow *window = BasePlatform::getPlatform()->createNativeWindow(_nextWindowId, info.externalHandle);
|
||||
if (window) {
|
||||
if (!info.externalHandle) {
|
||||
window->createWindow(info.title.c_str(), info.x, info.y, info.width, info.height, info.flags);
|
||||
}
|
||||
_windows[_nextWindowId] = std::shared_ptr<ISystemWindow>(window);
|
||||
_nextWindowId++;
|
||||
}
|
||||
return window;
|
||||
}
|
||||
|
||||
ISystemWindow *SystemWindowManager::getWindow(uint32_t windowId) const {
|
||||
if (windowId == 0)
|
||||
return nullptr;
|
||||
|
||||
auto iter = _windows.find(windowId);
|
||||
if (iter != _windows.end())
|
||||
return iter->second.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
cc::ISystemWindow *SystemWindowManager::getWindowFromSDLWindow(SDL_Window *window) const {
|
||||
for (const auto &iter : _windows) {
|
||||
SystemWindow *sysWindow = static_cast<SystemWindow *>(iter.second.get());
|
||||
SDL_Window *sdlWindow = sysWindow->getSDLWindow();
|
||||
if (sdlWindow == window) {
|
||||
return sysWindow;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
54
cocos/platform/linux/modules/SystemWindowManager.h
Normal file
54
cocos/platform/linux/modules/SystemWindowManager.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base/std/container/unordered_map.h"
|
||||
#include "platform/interfaces/modules/ISystemWindowManager.h"
|
||||
|
||||
struct SDL_Window;
|
||||
|
||||
namespace cc {
|
||||
|
||||
class ISystemWindow;
|
||||
|
||||
class SystemWindowManager : public ISystemWindowManager {
|
||||
public:
|
||||
SystemWindowManager() = default;
|
||||
|
||||
int init() override;
|
||||
void processEvent() override;
|
||||
|
||||
ISystemWindow *createWindow(const ISystemWindowInfo &info) override;
|
||||
ISystemWindow *getWindow(uint32_t windowId) const override;
|
||||
const SystemWindowMap &getWindows() const override { return _windows; }
|
||||
|
||||
ISystemWindow *getWindowFromSDLWindow(SDL_Window *window) const;
|
||||
|
||||
private:
|
||||
uint32_t _nextWindowId{1}; // start from 1, 0 means an invalid ID
|
||||
|
||||
SystemWindowMap _windows;
|
||||
};
|
||||
} // namespace cc
|
||||
35
cocos/platform/linux/modules/Vibrator.cpp
Normal file
35
cocos/platform/linux/modules/Vibrator.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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 "platform/linux/modules/Vibrator.h"
|
||||
|
||||
#include "base/Macros.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
void Vibrator::vibrate(float duration) {
|
||||
CC_UNUSED_PARAM(duration);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
43
cocos/platform/linux/modules/Vibrator.h
Normal file
43
cocos/platform/linux/modules/Vibrator.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IVibrator.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL Vibrator : public IVibrator {
|
||||
public:
|
||||
/**
|
||||
* Vibrator for the specified amount of time.
|
||||
* If Vibrator is not supported, then invoking this method has no effect.
|
||||
* Some platforms limit to a maximum duration of 5 seconds.
|
||||
* Duration is ignored on iOS due to API limitations.
|
||||
* @param duration The duration in seconds.
|
||||
*/
|
||||
void vibrate(float duration) override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
Reference in New Issue
Block a user