AndroidUtil.cpp 8.4 KB

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