macPlatform.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #import <Cocoa/Cocoa.h>
  23. #import <unistd.h>
  24. #import "platform/platform.h"
  25. #import "console/console.h"
  26. #import "core/stringTable.h"
  27. #import "core/util/str.h"
  28. #import "platform/platformInput.h"
  29. #import "platform/threads/thread.h"
  30. #import "core/util/journal/process.h"
  31. //-----------------------------------------------------------------------------
  32. // Completely closes and restarts the simulation
  33. void Platform::restartInstance()
  34. {
  35. // Returns the NSBundle that corresponds to the directory where the current app executable is located.
  36. NSBundle* mainAppBundle = [NSBundle mainBundle];
  37. // Returns the file URL of the receiver's executable file.
  38. // Not currently used, but left here for reference
  39. //NSURL* execURL = [mainAppBundle executableURL];
  40. // Returns the full pathname of the receiver's executable file.
  41. NSString* execString = [mainAppBundle executablePath];
  42. // Create a mutable string we can build into an executable command
  43. NSMutableString* mut = [[[NSMutableString alloc] init] autorelease];
  44. // Base string is the executable path
  45. [mut appendString:execString];
  46. // append ampersand so that we can launch without blocking.
  47. // encase in quotes so that spaces in the path are accepted.
  48. [mut insertString:@"\"" atIndex:0];
  49. [mut appendString:@"\" & "];
  50. [mut appendString:@"\\0"];
  51. // Convert to a C string
  52. const char* execCString = [mut UTF8String];
  53. // Echo the command before we run it
  54. Con::printf("---- %s -----", execCString);
  55. // Run the restart command and hope for the best
  56. system(execCString);
  57. }
  58. void Platform::postQuitMessage(const S32 in_quitVal)
  59. {
  60. Process::requestShutdown();
  61. }
  62. void Platform::forceShutdown(S32 returnValue)
  63. {
  64. //exit(returnValue);
  65. [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0];
  66. }
  67. //-----------------------------------------------------------------------------
  68. void Platform::debugBreak()
  69. {
  70. raise(SIGTRAP);
  71. }
  72. #pragma mark ---- Various Directories ----
  73. //-----------------------------------------------------------------------------
  74. const char* Platform::getUserDataDirectory()
  75. {
  76. // application support directory is most in line with the current usages of this function.
  77. // this may change with later usage
  78. // perhaps the user data directory should be pref-controlled?
  79. NSString *nsDataDir = [@"~/Library/Application Support/" stringByStandardizingPath];
  80. return StringTable->insert([nsDataDir UTF8String]);
  81. }
  82. //-----------------------------------------------------------------------------
  83. const char* Platform::getUserHomeDirectory()
  84. {
  85. return StringTable->insert([[@"~/Documents" stringByStandardizingPath] UTF8String]);
  86. }
  87. //-----------------------------------------------------------------------------
  88. StringTableEntry osGetTemporaryDirectory()
  89. {
  90. NSString *tdir = NSTemporaryDirectory();
  91. const char *path = [tdir UTF8String];
  92. return StringTable->insert(path);
  93. }
  94. #pragma mark ---- Platform utility funcs ----
  95. //-----------------------------------------------------------------------------
  96. void Platform::outputDebugString( const char *string, ... )
  97. {
  98. #ifdef TORQUE_DEBUG
  99. char buffer[ 2048 ];
  100. va_list args;
  101. va_start( args, string );
  102. dVsprintf( buffer, sizeof( buffer ), string, args );
  103. va_end( args );
  104. U32 length = strlen( buffer );
  105. if( length == ( sizeof( buffer ) - 1 ) )
  106. length --;
  107. buffer[ length ] = '\n';
  108. buffer[ length + 1 ] = '\0';
  109. fputs( buffer, stderr );
  110. fflush(stderr);
  111. #endif
  112. }
  113. #ifndef TORQUE_SDL
  114. //-----------------------------------------------------------------------------
  115. bool Platform::openWebBrowser( const char* webAddress )
  116. {
  117. OSStatus err;
  118. CFURLRef url = CFURLCreateWithBytes(NULL,(UInt8*)webAddress,dStrlen(webAddress),kCFStringEncodingASCII,NULL);
  119. err = LSOpenCFURLRef(url,NULL);
  120. CFRelease(url);
  121. return(err==noErr);
  122. }
  123. #endif
  124. #pragma mark ---- Administrator ----
  125. //-----------------------------------------------------------------------------
  126. bool Platform::getUserIsAdministrator()
  127. {
  128. // if we can write to /Library, we're probably an admin
  129. // HACK: this is not really very good, because people can chmod Library.
  130. return (access("/Library", W_OK) == 0);
  131. }