platform.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //platform-specific wrappers
  2. #if defined(_WIN32)
  3. //Windows 32-bit and 64-bit
  4. #define WIN32_LEAN_AND_MEAN
  5. #include <windows.h>
  6. #include <nall/utf8.hpp>
  7. char* realpath(const char *filename, char *resolvedname) {
  8. wchar_t fn[_MAX_PATH] = L"";
  9. _wfullpath(fn, nall::utf16_t(filename), _MAX_PATH);
  10. strcpy(resolvedname, nall::utf8_t(fn));
  11. return resolvedname;
  12. }
  13. char* userpath(char *path) {
  14. wchar_t fp[_MAX_PATH] = L"";
  15. SHGetFolderPathW(0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, 0, 0, fp);
  16. strcpy(path, nall::utf8_t(fp));
  17. return path;
  18. }
  19. char* getcwd(char *path) {
  20. wchar_t fp[_MAX_PATH] = L"";
  21. _wgetcwd(fp, _MAX_PATH);
  22. strcpy(path, nall::utf8_t(fp));
  23. return path;
  24. }
  25. int mkdir(const char *path) {
  26. return _wmkdir(nall::utf16_t(path));
  27. }
  28. void initargs(int &argc, char **&argv) {
  29. wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
  30. argv = new char*[argc];
  31. for(unsigned i = 0; i < argc; i++) {
  32. argv[i] = new char[_MAX_PATH];
  33. strcpy(argv[i], nall::utf8_t(wargv[i]));
  34. }
  35. }
  36. bool Application::App::winEventFilter(MSG *msg, long *result) {
  37. if(msg->message == WM_SYSCOMMAND) {
  38. if(msg->wParam == SC_SCREENSAVE || msg->wParam == SC_MONITORPOWER) {
  39. *result = 0;
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. void supressScreenSaver() {
  46. //handled by event filter above
  47. }
  48. #else
  49. #define None XNone
  50. #define Window XWindow
  51. #include <X11/extensions/XTest.h>
  52. #undef None
  53. #undef Window
  54. //POSIX-compatible (Linux, BSD, etc.)
  55. char* userpath(char *path) {
  56. *path = 0;
  57. struct passwd *userinfo = getpwuid(getuid());
  58. if(userinfo) strcpy(path, userinfo->pw_dir);
  59. return path;
  60. }
  61. char *getcwd(char *path) {
  62. return getcwd(path, PATH_MAX);
  63. }
  64. #define mkdir(path) (mkdir)(path, 0755)
  65. void initargs(int &argc, char **&argv) {
  66. }
  67. void supressScreenSaver() {
  68. static clock_t delta_x = 0, delta_y = 0;
  69. delta_y = clock();
  70. if(delta_y - delta_x < CLOCKS_PER_SEC * 20) return;
  71. delta_x = delta_y;
  72. //XSetScreenSaver(timeout = 0) does not work
  73. //XResetScreenSaver() does not work
  74. //XScreenSaverSuspend() does not work
  75. //DPMSDisable() does not work
  76. //XSendEvent(KeyPressMask) does not work
  77. //use XTest extension to send fake keypress every ~20 seconds.
  78. //keycode of 255 does not map to any actual key,
  79. //but it will block screensaver and power management.
  80. Display *display = XOpenDisplay(0);
  81. //XTestFakeKeyEvent(display, 255, True, 0);
  82. //XTestFakeKeyEvent(display, 255, False, 0);
  83. }
  84. #endif