AndroidUtil.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #include "AndroidUtil.h"
  23. #include "io/stream.h"
  24. #include "io/fileStream.h"
  25. #include "io/memstream.h"
  26. #include "graphics/gPalette.h"
  27. #include "graphics/gBitmap.h"
  28. #include "memory/frameAllocator.h"
  29. #include "console/console.h"
  30. #include "platformAndroid/platformAndroid.h"
  31. //For networking bsd and IP
  32. #include <netinet/in.h>
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <arpa/inet.h>
  36. //Luma: Orientation support
  37. //TODO: android
  38. //int gAndroidGameCurrentOrientation = UIDeviceOrientationLandscapeRight;
  39. TCPObject* gpTCPObject = NULL;
  40. char gszTCPAddress[256];
  41. //-----------------------------------------------------------------------------
  42. void Platform::outputDebugString( const char *string )
  43. {
  44. fprintf(stderr, "%s", string);
  45. fprintf(stderr, "\n" );
  46. fflush(stderr);
  47. }
  48. //--------------------------------------
  49. //TODO: android
  50. /*
  51. bool GBitmap::readPNGiPhone(Stream& io_rStream)
  52. {
  53. int filesize = io_rStream.getStreamSize();
  54. U8 *buff = new U8[filesize+1024];
  55. CGDataProviderRef data_provider = CGDataProviderCreateWithData(nil, buff, filesize, nil);
  56. CGImageRef apple_image = CGImageCreateWithPNGDataProvider(data_provider, nil, false, kCGRenderingIntentDefault);
  57. // Choose alpha strategy based on whether the source image has alpha or not.
  58. int width = CGImageGetWidth(apple_image);
  59. int height = CGImageGetHeight(apple_image);
  60. U32 rowBytes = width * 4;
  61. // Set up the row pointers...
  62. AssertISV(width <= 1024, "Error, cannot load images wider than 1024 pixels!");
  63. AssertISV(height <= 1024, "Error, cannot load images taller than 1024 pixels!");
  64. BitmapFormat format = RGBA;
  65. // actually allocate the bitmap space...
  66. allocateBitmap(width, height,
  67. false, // don't extrude miplevels...
  68. format); // use determined format...
  69. U8 *pBase = (U8*)getBits();
  70. CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
  71. CGContextRef texture_context = CGBitmapContextCreate(pBase, width, height, 8, rowBytes, color_space, kCGImageAlphaPremultipliedLast);
  72. CGContextDrawImage(texture_context, CGRectMake(0.0, 0.0, width, height), apple_image);
  73. CGImageRelease(apple_image);
  74. CGDataProviderRelease(data_provider);
  75. delete [] buff;
  76. return true;
  77. }*/
  78. //Luma: Orientation support
  79. int _AndroidGameGetOrientation()
  80. {
  81. //TODO: android
  82. //return gAndroidGameCurrentOrientation;
  83. return 0;
  84. }
  85. void _AndroidGameSetCurrentOrientation(int iOrientation)
  86. {
  87. //TODO: android
  88. //gAndroidGameCurrentOrientation = iOrientation;
  89. }
  90. S32 _AndroidGetPortraitTouchoffset()
  91. {
  92. S32 offset = 0;
  93. S32 deviceType = Con::getIntVariable("$pref::Android::DeviceType");
  94. bool retinaEnabled = Con::getBoolVariable("$pref::Android::RetinaEnabled");
  95. if (deviceType == 2)
  96. offset = 500;
  97. else if (deviceType == 1)
  98. offset = retinaEnabled ? 500 : 250;
  99. else
  100. offset = retinaEnabled ? 320 : 160;
  101. return offset;
  102. }
  103. //Luma: Ability to get the Local IP (Internal IP) for an Android as opposed to it's External one
  104. void _AndroidGetLocalIP(unsigned char *pcIPString)
  105. {
  106. //TODO: android
  107. /*
  108. int a,b,c,d ;
  109. struct ifaddrs* interface;
  110. char* addr;
  111. if (getifaddrs(&interface) == 0)
  112. {
  113. struct ifaddrs* allInterfaces = interface;
  114. while (interface != NULL)
  115. {
  116. const struct sockaddr_in* address = (const struct sockaddr_in*) interface->ifa_addr;
  117. addr = inet_ntoa(address->sin_addr);
  118. if ((address->sin_family == AF_INET) && (strcmp(addr, "127.0.0.1" )))
  119. {
  120. break;
  121. }
  122. interface = interface->ifa_next;
  123. }
  124. freeifaddrs(allInterfaces);
  125. }
  126. if(interface)
  127. {
  128. sscanf( addr, "%i.%i.%i.%i", &a, &b, &c, &d);
  129. }
  130. else
  131. {
  132. a = 0;
  133. b = 0;
  134. c = 0;
  135. d = 0;
  136. }
  137. pcIPString[0] = (unsigned char)a;
  138. pcIPString[1] = (unsigned char)b;
  139. pcIPString[2] = (unsigned char)c;
  140. pcIPString[3] = (unsigned char)d;
  141. */
  142. }
  143. //Luma: Make sure that the Android Radio is on before connection via TCP... NOTE: sometimes the Radio wont be ready for immediate use after this is processed... need to see why
  144. //TODO: android
  145. /*
  146. static void TCPObjectConnectCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *dataIn, void *info)
  147. {
  148. if(type == kCFSocketConnectCallBack)
  149. {
  150. if(dataIn)
  151. {
  152. SInt32 error = *((SInt32*)dataIn);
  153. Con::printf("Error connecting with CFSocker: Error code %d\n",error);
  154. }
  155. }
  156. //regardless, we want to connect to the TCPObject if we opened the socket or not so that it can continue its process properly
  157. if(gpTCPObject)
  158. {
  159. gpTCPObject->connect(gszTCPAddress);
  160. gpTCPObject = NULL;
  161. }
  162. }
  163. //Luma: Make sure that the Android Radio is on before connection via TCP... NOTE: sometimes the Radio wont be ready for immediate use after this is processed... need to see why
  164. CFSocketRef CreateCFSocketToURLAndPort(const char *ipAddress, U16 port)
  165. {
  166. CFSocketContext context;
  167. context.version = 0;
  168. context.info = NULL;
  169. context.retain = NULL;
  170. context.release = NULL;
  171. context.copyDescription = NULL;
  172. CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault,
  173. PF_INET,
  174. SOCK_STREAM,
  175. IPPROTO_TCP,
  176. kCFSocketConnectCallBack,
  177. TCPObjectConnectCallback,
  178. &context);
  179. struct sockaddr_in addr4;
  180. memset(&addr4, 0, sizeof(addr4));
  181. addr4.sin_family = AF_INET;
  182. addr4.sin_len = sizeof(addr4);
  183. addr4.sin_port = htons(port);
  184. inet_aton(ipAddress, &addr4.sin_addr);
  185. return socket;
  186. }
  187. //Luma: Make sure that the Android Radio is on before connection via TCP... NOTE: sometimes the Radio wont be ready for immediate use after this is processed... need to see why
  188. void OpenAndroidNetworkingAndConnectToTCPObject(TCPObject *psTCPObject, const char *pcAddress)
  189. {
  190. char remoteAddr[256];
  191. //store TCPObject and Port in globals
  192. gpTCPObject = psTCPObject;
  193. if(psTCPObject)
  194. {
  195. dStrcpy(gszTCPAddress, pcAddress);
  196. }
  197. //break up url / port to pass in
  198. dStrcpy(remoteAddr, pcAddress);
  199. U16 port = 80;
  200. char *portString = dStrchr(remoteAddr, ':');
  201. if(portString)
  202. {
  203. *portString++ = 0;
  204. port = dAtoi(portString);
  205. }
  206. //call socket create function
  207. CreateCFSocketToURLAndPort(remoteAddr, port);
  208. }
  209. */