PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llwindow/llwindowsdl.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 2389 lines | 1738 code | 343 blank | 308 comment | 282 complexity | e27d5b09f5e8acc10cb18969d3425392 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llwindowsdl.cpp
  3. * @brief SDL implementation of LLWindow class
  4. * @author This module has many fathers, and it shows.
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #if LL_SDL
  28. #include "linden_common.h"
  29. #include "llwindowsdl.h"
  30. #include "llwindowcallbacks.h"
  31. #include "llkeyboardsdl.h"
  32. #include "llerror.h"
  33. #include "llgl.h"
  34. #include "llstring.h"
  35. #include "lldir.h"
  36. #include "llfindlocale.h"
  37. #if LL_GTK
  38. extern "C" {
  39. # include "gtk/gtk.h"
  40. }
  41. #include <locale.h>
  42. #endif // LL_GTK
  43. extern "C" {
  44. # include "fontconfig/fontconfig.h"
  45. }
  46. #if LL_LINUX || LL_SOLARIS
  47. // not necessarily available on random SDL platforms, so #if LL_LINUX
  48. // for execv(), waitpid(), fork()
  49. # include <unistd.h>
  50. # include <sys/types.h>
  51. # include <sys/wait.h>
  52. #endif // LL_LINUX || LL_SOLARIS
  53. extern BOOL gDebugWindowProc;
  54. const S32 MAX_NUM_RESOLUTIONS = 200;
  55. // static variable for ATI mouse cursor crash work-around:
  56. static bool ATIbug = false;
  57. //
  58. // LLWindowSDL
  59. //
  60. #if LL_X11
  61. # include <X11/Xutil.h>
  62. #endif //LL_X11
  63. // TOFU HACK -- (*exactly* the same hack as LLWindowMacOSX for a similar
  64. // set of reasons): Stash a pointer to the LLWindowSDL object here and
  65. // maintain in the constructor and destructor. This assumes that there will
  66. // be only one object of this class at any time. Currently this is true.
  67. static LLWindowSDL *gWindowImplementation = NULL;
  68. void maybe_lock_display(void)
  69. {
  70. if (gWindowImplementation && gWindowImplementation->Lock_Display) {
  71. gWindowImplementation->Lock_Display();
  72. }
  73. }
  74. void maybe_unlock_display(void)
  75. {
  76. if (gWindowImplementation && gWindowImplementation->Unlock_Display) {
  77. gWindowImplementation->Unlock_Display();
  78. }
  79. }
  80. #if LL_GTK
  81. // Lazily initialize and check the runtime GTK version for goodness.
  82. // static
  83. bool LLWindowSDL::ll_try_gtk_init(void)
  84. {
  85. static BOOL done_gtk_diag = FALSE;
  86. static BOOL gtk_is_good = FALSE;
  87. static BOOL done_setlocale = FALSE;
  88. static BOOL tried_gtk_init = FALSE;
  89. if (!done_setlocale)
  90. {
  91. llinfos << "Starting GTK Initialization." << llendl;
  92. maybe_lock_display();
  93. gtk_disable_setlocale();
  94. maybe_unlock_display();
  95. done_setlocale = TRUE;
  96. }
  97. if (!tried_gtk_init)
  98. {
  99. tried_gtk_init = TRUE;
  100. if (!g_thread_supported ()) g_thread_init (NULL);
  101. maybe_lock_display();
  102. gtk_is_good = gtk_init_check(NULL, NULL);
  103. maybe_unlock_display();
  104. if (!gtk_is_good)
  105. llwarns << "GTK Initialization failed." << llendl;
  106. }
  107. if (gtk_is_good && !done_gtk_diag)
  108. {
  109. llinfos << "GTK Initialized." << llendl;
  110. llinfos << "- Compiled against GTK version "
  111. << GTK_MAJOR_VERSION << "."
  112. << GTK_MINOR_VERSION << "."
  113. << GTK_MICRO_VERSION << llendl;
  114. llinfos << "- Running against GTK version "
  115. << gtk_major_version << "."
  116. << gtk_minor_version << "."
  117. << gtk_micro_version << llendl;
  118. maybe_lock_display();
  119. const gchar* gtk_warning = gtk_check_version(
  120. GTK_MAJOR_VERSION,
  121. GTK_MINOR_VERSION,
  122. GTK_MICRO_VERSION);
  123. maybe_unlock_display();
  124. if (gtk_warning)
  125. {
  126. llwarns << "- GTK COMPATIBILITY WARNING: " <<
  127. gtk_warning << llendl;
  128. gtk_is_good = FALSE;
  129. } else {
  130. llinfos << "- GTK version is good." << llendl;
  131. }
  132. done_gtk_diag = TRUE;
  133. }
  134. return gtk_is_good;
  135. }
  136. #endif // LL_GTK
  137. #if LL_X11
  138. // static
  139. Window LLWindowSDL::get_SDL_XWindowID(void)
  140. {
  141. if (gWindowImplementation) {
  142. return gWindowImplementation->mSDL_XWindowID;
  143. }
  144. return None;
  145. }
  146. //static
  147. Display* LLWindowSDL::get_SDL_Display(void)
  148. {
  149. if (gWindowImplementation) {
  150. return gWindowImplementation->mSDL_Display;
  151. }
  152. return NULL;
  153. }
  154. #endif // LL_X11
  155. LLWindowSDL::LLWindowSDL(LLWindowCallbacks* callbacks,
  156. const std::string& title, S32 x, S32 y, S32 width,
  157. S32 height, U32 flags,
  158. BOOL fullscreen, BOOL clearBg,
  159. BOOL disable_vsync, BOOL use_gl,
  160. BOOL ignore_pixel_depth, U32 fsaa_samples)
  161. : LLWindow(callbacks, fullscreen, flags),
  162. Lock_Display(NULL),
  163. Unlock_Display(NULL), mGamma(1.0f)
  164. {
  165. // Initialize the keyboard
  166. gKeyboard = new LLKeyboardSDL();
  167. gKeyboard->setCallbacks(callbacks);
  168. // Note that we can't set up key-repeat until after SDL has init'd video
  169. // Ignore use_gl for now, only used for drones on PC
  170. mWindow = NULL;
  171. mNeedsResize = FALSE;
  172. mOverrideAspectRatio = 0.f;
  173. mGrabbyKeyFlags = 0;
  174. mReallyCapturedCount = 0;
  175. mHaveInputFocus = -1;
  176. mIsMinimized = -1;
  177. mFSAASamples = fsaa_samples;
  178. #if LL_X11
  179. mSDL_XWindowID = None;
  180. mSDL_Display = NULL;
  181. #endif // LL_X11
  182. #if LL_GTK
  183. // We MUST be the first to initialize GTK so that GTK doesn't get badly
  184. // initialized with a non-C locale and cause lots of serious random
  185. // weirdness.
  186. ll_try_gtk_init();
  187. #endif // LL_GTK
  188. // Assume 4:3 aspect ratio until we know better
  189. mOriginalAspectRatio = 1024.0 / 768.0;
  190. if (title.empty())
  191. mWindowTitle = "SDL Window"; // *FIX: (???)
  192. else
  193. mWindowTitle = title;
  194. // Create the GL context and set it up for windowed or fullscreen, as appropriate.
  195. if(createContext(x, y, width, height, 32, fullscreen, disable_vsync))
  196. {
  197. gGLManager.initGL();
  198. //start with arrow cursor
  199. initCursors();
  200. setCursor( UI_CURSOR_ARROW );
  201. }
  202. stop_glerror();
  203. // Stash an object pointer for OSMessageBox()
  204. gWindowImplementation = this;
  205. #if LL_X11
  206. mFlashing = FALSE;
  207. #endif // LL_X11
  208. mKeyScanCode = 0;
  209. mKeyVirtualKey = 0;
  210. mKeyModifiers = KMOD_NONE;
  211. }
  212. static SDL_Surface *Load_BMP_Resource(const char *basename)
  213. {
  214. const int PATH_BUFFER_SIZE=1000;
  215. char path_buffer[PATH_BUFFER_SIZE]; /* Flawfinder: ignore */
  216. // Figure out where our BMP is living on the disk
  217. snprintf(path_buffer, PATH_BUFFER_SIZE-1, "%s%sres-sdl%s%s",
  218. gDirUtilp->getAppRODataDir().c_str(),
  219. gDirUtilp->getDirDelimiter().c_str(),
  220. gDirUtilp->getDirDelimiter().c_str(),
  221. basename);
  222. path_buffer[PATH_BUFFER_SIZE-1] = '\0';
  223. return SDL_LoadBMP(path_buffer);
  224. }
  225. #if LL_X11
  226. // This is an XFree86/XOrg-specific hack for detecting the amount of Video RAM
  227. // on this machine. It works by searching /var/log/var/log/Xorg.?.log or
  228. // /var/log/XFree86.?.log for a ': (VideoRAM ?|Memory): (%d+) kB' regex, where
  229. // '?' is the X11 display number derived from $DISPLAY
  230. static int x11_detect_VRAM_kb_fp(FILE *fp, const char *prefix_str)
  231. {
  232. const int line_buf_size = 1000;
  233. char line_buf[line_buf_size];
  234. while (fgets(line_buf, line_buf_size, fp))
  235. {
  236. //lldebugs << "XLOG: " << line_buf << llendl;
  237. // Why the ad-hoc parser instead of using a regex? Our
  238. // favourite regex implementation - libboost_regex - is
  239. // quite a heavy and troublesome dependency for the client, so
  240. // it seems a shame to introduce it for such a simple task.
  241. // *FIXME: libboost_regex is a dependency now anyway, so we may
  242. // as well use it instead of this hand-rolled nonsense.
  243. const char *part1_template = prefix_str;
  244. const char part2_template[] = " kB";
  245. char *part1 = strstr(line_buf, part1_template);
  246. if (part1) // found start of matching line
  247. {
  248. part1 = &part1[strlen(part1_template)]; // -> after
  249. char *part2 = strstr(part1, part2_template);
  250. if (part2) // found end of matching line
  251. {
  252. // now everything between part1 and part2 is
  253. // supposed to be numeric, describing the
  254. // number of kB of Video RAM supported
  255. int rtn = 0;
  256. for (; part1 < part2; ++part1)
  257. {
  258. if (*part1 < '0' || *part1 > '9')
  259. {
  260. // unexpected char, abort parse
  261. rtn = 0;
  262. break;
  263. }
  264. rtn *= 10;
  265. rtn += (*part1) - '0';
  266. }
  267. if (rtn > 0)
  268. {
  269. // got the kB number. return it now.
  270. return rtn;
  271. }
  272. }
  273. }
  274. }
  275. return 0; // 'could not detect'
  276. }
  277. static int x11_detect_VRAM_kb()
  278. {
  279. #if LL_SOLARIS && defined(__sparc)
  280. // NOTE: there's no Xorg server on SPARC so just return 0
  281. // and allow SDL to attempt to get the amount of VRAM
  282. return(0);
  283. #else
  284. std::string x_log_location("/var/log/");
  285. std::string fname;
  286. int rtn = 0; // 'could not detect'
  287. int display_num = 0;
  288. FILE *fp;
  289. char *display_env = getenv("DISPLAY"); // e.g. :0 or :0.0 or :1.0 etc
  290. // parse DISPLAY number so we can go grab the right log file
  291. if (display_env[0] == ':' &&
  292. display_env[1] >= '0' && display_env[1] <= '9')
  293. {
  294. display_num = display_env[1] - '0';
  295. }
  296. // *TODO: we could be smarter and see which of Xorg/XFree86 has the
  297. // freshest time-stamp.
  298. // Try Xorg log first
  299. fname = x_log_location;
  300. fname += "Xorg.";
  301. fname += ('0' + display_num);
  302. fname += ".log";
  303. fp = fopen(fname.c_str(), "r");
  304. if (fp)
  305. {
  306. llinfos << "Looking in " << fname
  307. << " for VRAM info..." << llendl;
  308. rtn = x11_detect_VRAM_kb_fp(fp, ": VideoRAM: ");
  309. fclose(fp);
  310. if (0 == rtn)
  311. {
  312. fp = fopen(fname.c_str(), "r");
  313. if (fp)
  314. {
  315. rtn = x11_detect_VRAM_kb_fp(fp, ": Video RAM: ");
  316. fclose(fp);
  317. if (0 == rtn)
  318. {
  319. fp = fopen(fname.c_str(), "r");
  320. if (fp)
  321. {
  322. rtn = x11_detect_VRAM_kb_fp(fp, ": Memory: ");
  323. fclose(fp);
  324. }
  325. }
  326. }
  327. }
  328. }
  329. else
  330. {
  331. llinfos << "Could not open " << fname
  332. << " - skipped." << llendl;
  333. // Try old XFree86 log otherwise
  334. fname = x_log_location;
  335. fname += "XFree86.";
  336. fname += ('0' + display_num);
  337. fname += ".log";
  338. fp = fopen(fname.c_str(), "r");
  339. if (fp)
  340. {
  341. llinfos << "Looking in " << fname
  342. << " for VRAM info..." << llendl;
  343. rtn = x11_detect_VRAM_kb_fp(fp, ": VideoRAM: ");
  344. fclose(fp);
  345. if (0 == rtn)
  346. {
  347. fp = fopen(fname.c_str(), "r");
  348. if (fp)
  349. {
  350. rtn = x11_detect_VRAM_kb_fp(fp, ": Memory: ");
  351. fclose(fp);
  352. }
  353. }
  354. }
  355. else
  356. {
  357. llinfos << "Could not open " << fname
  358. << " - skipped." << llendl;
  359. }
  360. }
  361. return rtn;
  362. #endif // LL_SOLARIS
  363. }
  364. #endif // LL_X11
  365. BOOL LLWindowSDL::createContext(int x, int y, int width, int height, int bits, BOOL fullscreen, BOOL disable_vsync)
  366. {
  367. //bool glneedsinit = false;
  368. llinfos << "createContext, fullscreen=" << fullscreen <<
  369. " size=" << width << "x" << height << llendl;
  370. // captures don't survive contexts
  371. mGrabbyKeyFlags = 0;
  372. mReallyCapturedCount = 0;
  373. if (SDL_Init(SDL_INIT_VIDEO) < 0)
  374. {
  375. llinfos << "sdl_init() failed! " << SDL_GetError() << llendl;
  376. setupFailure("sdl_init() failure, window creation error", "error", OSMB_OK);
  377. return false;
  378. }
  379. SDL_version c_sdl_version;
  380. SDL_VERSION(&c_sdl_version);
  381. llinfos << "Compiled against SDL "
  382. << int(c_sdl_version.major) << "."
  383. << int(c_sdl_version.minor) << "."
  384. << int(c_sdl_version.patch) << llendl;
  385. const SDL_version *r_sdl_version;
  386. r_sdl_version = SDL_Linked_Version();
  387. llinfos << " Running against SDL "
  388. << int(r_sdl_version->major) << "."
  389. << int(r_sdl_version->minor) << "."
  390. << int(r_sdl_version->patch) << llendl;
  391. const SDL_VideoInfo *video_info = SDL_GetVideoInfo( );
  392. if (!video_info)
  393. {
  394. llinfos << "SDL_GetVideoInfo() failed! " << SDL_GetError() << llendl;
  395. setupFailure("SDL_GetVideoInfo() failed, Window creation error", "Error", OSMB_OK);
  396. return FALSE;
  397. }
  398. if (video_info->current_h > 0)
  399. {
  400. mOriginalAspectRatio = (float)video_info->current_w / (float)video_info->current_h;
  401. llinfos << "Original aspect ratio was " << video_info->current_w << ":" << video_info->current_h << "=" << mOriginalAspectRatio << llendl;
  402. }
  403. SDL_EnableUNICODE(1);
  404. SDL_WM_SetCaption(mWindowTitle.c_str(), mWindowTitle.c_str());
  405. // Set the application icon.
  406. SDL_Surface *bmpsurface;
  407. bmpsurface = Load_BMP_Resource("ll_icon.BMP");
  408. if (bmpsurface)
  409. {
  410. // This attempts to give a black-keyed mask to the icon.
  411. SDL_SetColorKey(bmpsurface,
  412. SDL_SRCCOLORKEY,
  413. SDL_MapRGB(bmpsurface->format, 0,0,0) );
  414. SDL_WM_SetIcon(bmpsurface, NULL);
  415. // The SDL examples cheerfully avoid freeing the icon
  416. // surface, but I'm betting that's leaky.
  417. SDL_FreeSurface(bmpsurface);
  418. bmpsurface = NULL;
  419. }
  420. // note: these SetAttributes make Tom's 9600-on-AMD64 fail to
  421. // get a visual, but it's broken anyway when it does, and without
  422. // these SetAttributes we might easily get an avoidable substandard
  423. // visual to work with on most other machines.
  424. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  425. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
  426. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  427. #if !LL_SOLARIS
  428. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, (bits <= 16) ? 16 : 24);
  429. // We need stencil support for a few (minor) things.
  430. if (!getenv("LL_GL_NO_STENCIL"))
  431. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  432. #else
  433. // NOTE- use smaller Z-buffer to enable more graphics cards
  434. // - This should not affect better GPUs and has been proven
  435. // to provide 24-bit z-buffers when available.
  436. //
  437. // As the API states:
  438. //
  439. // GLX_DEPTH_SIZE Must be followed by a nonnegative
  440. // minimum size specification. If this
  441. // value is zero, visuals with no depth
  442. // buffer are preferred. Otherwise, the
  443. // largest available depth buffer of at
  444. // least the minimum size is preferred.
  445. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  446. #endif
  447. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, (bits <= 16) ? 1 : 8);
  448. // *FIX: try to toggle vsync here?
  449. mFullscreen = fullscreen;
  450. int sdlflags = SDL_OPENGL | SDL_RESIZABLE | SDL_ANYFORMAT;
  451. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  452. if (mFSAASamples > 0)
  453. {
  454. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
  455. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, mFSAASamples);
  456. }
  457. mSDLFlags = sdlflags;
  458. if (mFullscreen)
  459. {
  460. llinfos << "createContext: setting up fullscreen " << width << "x" << height << llendl;
  461. // If the requested width or height is 0, find the best default for the monitor.
  462. if((width == 0) || (height == 0))
  463. {
  464. // Scan through the list of modes, looking for one which has:
  465. // height between 700 and 800
  466. // aspect ratio closest to the user's original mode
  467. S32 resolutionCount = 0;
  468. LLWindowResolution *resolutionList = getSupportedResolutions(resolutionCount);
  469. if(resolutionList != NULL)
  470. {
  471. F32 closestAspect = 0;
  472. U32 closestHeight = 0;
  473. U32 closestWidth = 0;
  474. int i;
  475. llinfos << "createContext: searching for a display mode, original aspect is " << mOriginalAspectRatio << llendl;
  476. for(i=0; i < resolutionCount; i++)
  477. {
  478. F32 aspect = (F32)resolutionList[i].mWidth / (F32)resolutionList[i].mHeight;
  479. llinfos << "createContext: width " << resolutionList[i].mWidth << " height " << resolutionList[i].mHeight << " aspect " << aspect << llendl;
  480. if( (resolutionList[i].mHeight >= 700) && (resolutionList[i].mHeight <= 800) &&
  481. (fabs(aspect - mOriginalAspectRatio) < fabs(closestAspect - mOriginalAspectRatio)))
  482. {
  483. llinfos << " (new closest mode) " << llendl;
  484. // This is the closest mode we've seen yet.
  485. closestWidth = resolutionList[i].mWidth;
  486. closestHeight = resolutionList[i].mHeight;
  487. closestAspect = aspect;
  488. }
  489. }
  490. width = closestWidth;
  491. height = closestHeight;
  492. }
  493. }
  494. if((width == 0) || (height == 0))
  495. {
  496. // Mode search failed for some reason. Use the old-school default.
  497. width = 1024;
  498. height = 768;
  499. }
  500. mWindow = SDL_SetVideoMode(width, height, bits, sdlflags | SDL_FULLSCREEN);
  501. if (!mWindow && bits > 16)
  502. {
  503. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  504. mWindow = SDL_SetVideoMode(width, height, bits, sdlflags | SDL_FULLSCREEN);
  505. }
  506. if (mWindow)
  507. {
  508. mFullscreen = TRUE;
  509. mFullscreenWidth = mWindow->w;
  510. mFullscreenHeight = mWindow->h;
  511. mFullscreenBits = mWindow->format->BitsPerPixel;
  512. mFullscreenRefresh = -1;
  513. llinfos << "Running at " << mFullscreenWidth
  514. << "x" << mFullscreenHeight
  515. << "x" << mFullscreenBits
  516. << " @ " << mFullscreenRefresh
  517. << llendl;
  518. }
  519. else
  520. {
  521. llwarns << "createContext: fullscreen creation failure. SDL: " << SDL_GetError() << llendl;
  522. // No fullscreen support
  523. mFullscreen = FALSE;
  524. mFullscreenWidth = -1;
  525. mFullscreenHeight = -1;
  526. mFullscreenBits = -1;
  527. mFullscreenRefresh = -1;
  528. std::string error = llformat("Unable to run fullscreen at %d x %d.\nRunning in window.", width, height);
  529. OSMessageBox(error, "Error", OSMB_OK);
  530. }
  531. }
  532. if(!mFullscreen && (mWindow == NULL))
  533. {
  534. if (width == 0)
  535. width = 1024;
  536. if (height == 0)
  537. width = 768;
  538. llinfos << "createContext: creating window " << width << "x" << height << "x" << bits << llendl;
  539. mWindow = SDL_SetVideoMode(width, height, bits, sdlflags);
  540. if (!mWindow && bits > 16)
  541. {
  542. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  543. mWindow = SDL_SetVideoMode(width, height, bits, sdlflags);
  544. }
  545. if (!mWindow)
  546. {
  547. llwarns << "createContext: window creation failure. SDL: " << SDL_GetError() << llendl;
  548. setupFailure("Window creation error", "Error", OSMB_OK);
  549. return FALSE;
  550. }
  551. } else if (!mFullscreen && (mWindow != NULL))
  552. {
  553. llinfos << "createContext: SKIPPING - !fullscreen, but +mWindow " << width << "x" << height << "x" << bits << llendl;
  554. }
  555. // Detect video memory size.
  556. # if LL_X11
  557. gGLManager.mVRAM = x11_detect_VRAM_kb() / 1024;
  558. if (gGLManager.mVRAM != 0)
  559. {
  560. llinfos << "X11 log-parser detected " << gGLManager.mVRAM << "MB VRAM." << llendl;
  561. } else
  562. # endif // LL_X11
  563. {
  564. // fallback to letting SDL detect VRAM.
  565. // note: I've not seen SDL's detection ever actually find
  566. // VRAM != 0, but if SDL *does* detect it then that's a bonus.
  567. gGLManager.mVRAM = video_info->video_mem / 1024;
  568. if (gGLManager.mVRAM != 0)
  569. {
  570. llinfos << "SDL detected " << gGLManager.mVRAM << "MB VRAM." << llendl;
  571. }
  572. }
  573. // If VRAM is not detected, that is handled later
  574. // *TODO: Now would be an appropriate time to check for some
  575. // explicitly unsupported cards.
  576. //const char* RENDERER = (const char*) glGetString(GL_RENDERER);
  577. GLint depthBits, stencilBits, redBits, greenBits, blueBits, alphaBits;
  578. glGetIntegerv(GL_RED_BITS, &redBits);
  579. glGetIntegerv(GL_GREEN_BITS, &greenBits);
  580. glGetIntegerv(GL_BLUE_BITS, &blueBits);
  581. glGetIntegerv(GL_ALPHA_BITS, &alphaBits);
  582. glGetIntegerv(GL_DEPTH_BITS, &depthBits);
  583. glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
  584. llinfos << "GL buffer:" << llendl;
  585. llinfos << " Red Bits " << S32(redBits) << llendl;
  586. llinfos << " Green Bits " << S32(greenBits) << llendl;
  587. llinfos << " Blue Bits " << S32(blueBits) << llendl;
  588. llinfos << " Alpha Bits " << S32(alphaBits) << llendl;
  589. llinfos << " Depth Bits " << S32(depthBits) << llendl;
  590. llinfos << " Stencil Bits " << S32(stencilBits) << llendl;
  591. GLint colorBits = redBits + greenBits + blueBits + alphaBits;
  592. // fixme: actually, it's REALLY important for picking that we get at
  593. // least 8 bits each of red,green,blue. Alpha we can be a bit more
  594. // relaxed about if we have to.
  595. #if LL_SOLARIS && defined(__sparc)
  596. // again the __sparc required because Xsun support, 32bit are very pricey on SPARC
  597. if(colorBits < 24) //HACK: on SPARC allow 24-bit color
  598. #else
  599. if (colorBits < 32)
  600. #endif
  601. {
  602. close();
  603. setupFailure(
  604. #if LL_SOLARIS && defined(__sparc)
  605. "Second Life requires at least 24-bit color on SPARC to run in a window.\n"
  606. "Please use fbconfig to set your default color depth to 24 bits.\n"
  607. "You may also need to adjust the X11 setting in SMF. To do so use\n"
  608. " 'svccfg -s svc:/application/x11/x11-server setprop options/default_depth=24'\n"
  609. #else
  610. "Second Life requires True Color (32-bit) to run in a window.\n"
  611. "Please go to Control Panels -> Display -> Settings and\n"
  612. "set the screen to 32-bit color.\n"
  613. #endif
  614. "Alternately, if you choose to run fullscreen, Second Life\n"
  615. "will automatically adjust the screen each time it runs.",
  616. "Error",
  617. OSMB_OK);
  618. return FALSE;
  619. }
  620. #if 0 // *FIX: we're going to brave it for now...
  621. if (alphaBits < 8)
  622. {
  623. close();
  624. setupFailure(
  625. "Second Life is unable to run because it can't get an 8 bit alpha\n"
  626. "channel. Usually this is due to video card driver issues.\n"
  627. "Please make sure you have the latest video card drivers installed.\n"
  628. "Also be sure your monitor is set to True Color (32-bit) in\n"
  629. "Control Panels -> Display -> Settings.\n"
  630. "If you continue to receive this message, contact customer service.",
  631. "Error",
  632. OSMB_OK);
  633. return FALSE;
  634. }
  635. #endif
  636. #if LL_X11
  637. /* Grab the window manager specific information */
  638. SDL_SysWMinfo info;
  639. SDL_VERSION(&info.version);
  640. if ( SDL_GetWMInfo(&info) )
  641. {
  642. /* Save the information for later use */
  643. if ( info.subsystem == SDL_SYSWM_X11 )
  644. {
  645. mSDL_Display = info.info.x11.display;
  646. mSDL_XWindowID = info.info.x11.wmwindow;
  647. Lock_Display = info.info.x11.lock_func;
  648. Unlock_Display = info.info.x11.unlock_func;
  649. }
  650. else
  651. {
  652. llwarns << "We're not running under X11? Wild."
  653. << llendl;
  654. }
  655. }
  656. else
  657. {
  658. llwarns << "We're not running under any known WM. Wild."
  659. << llendl;
  660. }
  661. #endif // LL_X11
  662. //make sure multisampling is disabled by default
  663. glDisable(GL_MULTISAMPLE_ARB);
  664. // We need to do this here, once video is init'd
  665. if (-1 == SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
  666. SDL_DEFAULT_REPEAT_INTERVAL))
  667. llwarns << "Couldn't enable key-repeat: " << SDL_GetError() <<llendl;
  668. // Don't need to get the current gamma, since there's a call that restores it to the system defaults.
  669. return TRUE;
  670. }
  671. // changing fullscreen resolution, or switching between windowed and fullscreen mode.
  672. BOOL LLWindowSDL::switchContext(BOOL fullscreen, const LLCoordScreen &size, BOOL disable_vsync, const LLCoordScreen * const posp)
  673. {
  674. const BOOL needsRebuild = TRUE; // Just nuke the context and start over.
  675. BOOL result = true;
  676. llinfos << "switchContext, fullscreen=" << fullscreen << llendl;
  677. stop_glerror();
  678. if(needsRebuild)
  679. {
  680. destroyContext();
  681. result = createContext(0, 0, size.mX, size.mY, 0, fullscreen, disable_vsync);
  682. if (result)
  683. {
  684. gGLManager.initGL();
  685. //start with arrow cursor
  686. initCursors();
  687. setCursor( UI_CURSOR_ARROW );
  688. }
  689. }
  690. stop_glerror();
  691. return result;
  692. }
  693. void LLWindowSDL::destroyContext()
  694. {
  695. llinfos << "destroyContext begins" << llendl;
  696. #if LL_X11
  697. mSDL_Display = NULL;
  698. mSDL_XWindowID = None;
  699. Lock_Display = NULL;
  700. Unlock_Display = NULL;
  701. #endif // LL_X11
  702. // Clean up remaining GL state before blowing away window
  703. llinfos << "shutdownGL begins" << llendl;
  704. gGLManager.shutdownGL();
  705. llinfos << "SDL_QuitSS/VID begins" << llendl;
  706. SDL_QuitSubSystem(SDL_INIT_VIDEO); // *FIX: this might be risky...
  707. mWindow = NULL;
  708. }
  709. LLWindowSDL::~LLWindowSDL()
  710. {
  711. quitCursors();
  712. destroyContext();
  713. if(mSupportedResolutions != NULL)
  714. {
  715. delete []mSupportedResolutions;
  716. }
  717. gWindowImplementation = NULL;
  718. }
  719. void LLWindowSDL::show()
  720. {
  721. // *FIX: What to do with SDL?
  722. }
  723. void LLWindowSDL::hide()
  724. {
  725. // *FIX: What to do with SDL?
  726. }
  727. //virtual
  728. void LLWindowSDL::minimize()
  729. {
  730. // *FIX: What to do with SDL?
  731. }
  732. //virtual
  733. void LLWindowSDL::restore()
  734. {
  735. // *FIX: What to do with SDL?
  736. }
  737. // close() destroys all OS-specific code associated with a window.
  738. // Usually called from LLWindowManager::destroyWindow()
  739. void LLWindowSDL::close()
  740. {
  741. // Is window is already closed?
  742. // if (!mWindow)
  743. // {
  744. // return;
  745. // }
  746. // Make sure cursor is visible and we haven't mangled the clipping state.
  747. setMouseClipping(FALSE);
  748. showCursor();
  749. destroyContext();
  750. }
  751. BOOL LLWindowSDL::isValid()
  752. {
  753. return (mWindow != NULL);
  754. }
  755. BOOL LLWindowSDL::getVisible()
  756. {
  757. BOOL result = FALSE;
  758. // *FIX: This isn't really right...
  759. // Then what is?
  760. if (mWindow)
  761. {
  762. result = TRUE;
  763. }
  764. return(result);
  765. }
  766. BOOL LLWindowSDL::getMinimized()
  767. {
  768. BOOL result = FALSE;
  769. if (mWindow && (1 == mIsMinimized))
  770. {
  771. result = TRUE;
  772. }
  773. return(result);
  774. }
  775. BOOL LLWindowSDL::getMaximized()
  776. {
  777. BOOL result = FALSE;
  778. if (mWindow)
  779. {
  780. // TODO
  781. }
  782. return(result);
  783. }
  784. BOOL LLWindowSDL::maximize()
  785. {
  786. // TODO
  787. return FALSE;
  788. }
  789. BOOL LLWindowSDL::getFullscreen()
  790. {
  791. return mFullscreen;
  792. }
  793. BOOL LLWindowSDL::getPosition(LLCoordScreen *position)
  794. {
  795. // *FIX: can anything be done with this?
  796. position->mX = 0;
  797. position->mY = 0;
  798. return TRUE;
  799. }
  800. BOOL LLWindowSDL::getSize(LLCoordScreen *size)
  801. {
  802. if (mWindow)
  803. {
  804. size->mX = mWindow->w;
  805. size->mY = mWindow->h;
  806. return (TRUE);
  807. }
  808. return (FALSE);
  809. }
  810. BOOL LLWindowSDL::getSize(LLCoordWindow *size)
  811. {
  812. if (mWindow)
  813. {
  814. size->mX = mWindow->w;
  815. size->mY = mWindow->h;
  816. return (TRUE);
  817. }
  818. return (FALSE);
  819. }
  820. BOOL LLWindowSDL::setPosition(const LLCoordScreen position)
  821. {
  822. if(mWindow)
  823. {
  824. // *FIX: (???)
  825. //MacMoveWindow(mWindow, position.mX, position.mY, false);
  826. }
  827. return TRUE;
  828. }
  829. BOOL LLWindowSDL::setSizeImpl(const LLCoordScreen size)
  830. {
  831. if(mWindow)
  832. {
  833. // Push a resize event onto SDL's queue - we'll handle it
  834. // when it comes out again.
  835. SDL_Event event;
  836. event.type = SDL_VIDEORESIZE;
  837. event.resize.w = size.mX;
  838. event.resize.h = size.mY;
  839. SDL_PushEvent(&event); // copied into queue
  840. return TRUE;
  841. }
  842. return FALSE;
  843. }
  844. void LLWindowSDL::swapBuffers()
  845. {
  846. if (mWindow)
  847. {
  848. SDL_GL_SwapBuffers();
  849. }
  850. }
  851. U32 LLWindowSDL::getFSAASamples()
  852. {
  853. return mFSAASamples;
  854. }
  855. void LLWindowSDL::setFSAASamples(const U32 samples)
  856. {
  857. mFSAASamples = samples;
  858. }
  859. F32 LLWindowSDL::getGamma()
  860. {
  861. return 1/mGamma;
  862. }
  863. BOOL LLWindowSDL::restoreGamma()
  864. {
  865. //CGDisplayRestoreColorSyncSettings();
  866. SDL_SetGamma(1.0f, 1.0f, 1.0f);
  867. return true;
  868. }
  869. BOOL LLWindowSDL::setGamma(const F32 gamma)
  870. {
  871. mGamma = gamma;
  872. if (mGamma == 0) mGamma = 0.1f;
  873. mGamma = 1/mGamma;
  874. SDL_SetGamma(mGamma, mGamma, mGamma);
  875. return true;
  876. }
  877. BOOL LLWindowSDL::isCursorHidden()
  878. {
  879. return mCursorHidden;
  880. }
  881. // Constrains the mouse to the window.
  882. void LLWindowSDL::setMouseClipping( BOOL b )
  883. {
  884. //SDL_WM_GrabInput(b ? SDL_GRAB_ON : SDL_GRAB_OFF);
  885. }
  886. // virtual
  887. void LLWindowSDL::setMinSize(U32 min_width, U32 min_height, bool enforce_immediately)
  888. {
  889. LLWindow::setMinSize(min_width, min_height, enforce_immediately);
  890. #if LL_X11
  891. // Set the minimum size limits for X11 window
  892. // so the window manager doesn't allow resizing below those limits.
  893. XSizeHints* hints = XAllocSizeHints();
  894. hints->flags |= PMinSize;
  895. hints->min_width = mMinWindowWidth;
  896. hints->min_height = mMinWindowHeight;
  897. XSetWMNormalHints(mSDL_Display, mSDL_XWindowID, hints);
  898. XFree(hints);
  899. #endif
  900. }
  901. BOOL LLWindowSDL::setCursorPosition(const LLCoordWindow position)
  902. {
  903. BOOL result = TRUE;
  904. LLCoordScreen screen_pos;
  905. if (!convertCoords(position, &screen_pos))
  906. {
  907. return FALSE;
  908. }
  909. //llinfos << "setCursorPosition(" << screen_pos.mX << ", " << screen_pos.mY << ")" << llendl;
  910. // do the actual forced cursor move.
  911. SDL_WarpMouse(screen_pos.mX, screen_pos.mY);
  912. //llinfos << llformat("llcw %d,%d -> scr %d,%d", position.mX, position.mY, screen_pos.mX, screen_pos.mY) << llendl;
  913. return result;
  914. }
  915. BOOL LLWindowSDL::getCursorPosition(LLCoordWindow *position)
  916. {
  917. //Point cursor_point;
  918. LLCoordScreen screen_pos;
  919. //GetMouse(&cursor_point);
  920. int x, y;
  921. SDL_GetMouseState(&x, &y);
  922. screen_pos.mX = x;
  923. screen_pos.mY = y;
  924. return convertCoords(screen_pos, position);
  925. }
  926. F32 LLWindowSDL::getNativeAspectRatio()
  927. {
  928. #if 0
  929. // RN: this hack presumes that the largest supported resolution is monitor-limited
  930. // and that pixels in that mode are square, therefore defining the native aspect ratio
  931. // of the monitor...this seems to work to a close approximation for most CRTs/LCDs
  932. S32 num_resolutions;
  933. LLWindowResolution* resolutions = getSupportedResolutions(num_resolutions);
  934. return ((F32)resolutions[num_resolutions - 1].mWidth / (F32)resolutions[num_resolutions - 1].mHeight);
  935. //rn: AC
  936. #endif
  937. // MBW -- there are a couple of bad assumptions here. One is that the display list won't include
  938. // ridiculous resolutions nobody would ever use. The other is that the list is in order.
  939. // New assumptions:
  940. // - pixels are square (the only reasonable choice, really)
  941. // - The user runs their display at a native resolution, so the resolution of the display
  942. // when the app is launched has an aspect ratio that matches the monitor.
  943. //RN: actually, the assumption that there are no ridiculous resolutions (above the display's native capabilities) has
  944. // been born out in my experience.
  945. // Pixels are often not square (just ask the people who run their LCDs at 1024x768 or 800x600 when running fullscreen, like me)
  946. // The ordering of display list is a blind assumption though, so we should check for max values
  947. // Things might be different on the Mac though, so I'll defer to MBW
  948. // The constructor for this class grabs the aspect ratio of the monitor before doing any resolution
  949. // switching, and stashes it in mOriginalAspectRatio. Here, we just return it.
  950. if (mOverrideAspectRatio > 0.f)
  951. {
  952. return mOverrideAspectRatio;
  953. }
  954. return mOriginalAspectRatio;
  955. }
  956. F32 LLWindowSDL::getPixelAspectRatio()
  957. {
  958. F32 pixel_aspect = 1.f;
  959. if (getFullscreen())
  960. {
  961. LLCoordScreen screen_size;
  962. if (getSize(&screen_size))
  963. {
  964. pixel_aspect = getNativeAspectRatio() * (F32)screen_size.mY / (F32)screen_size.mX;
  965. }
  966. }
  967. return pixel_aspect;
  968. }
  969. // This is to support 'temporarily windowed' mode so that
  970. // dialogs are still usable in fullscreen.
  971. void LLWindowSDL::beforeDialog()
  972. {
  973. bool running_x11 = false;
  974. #if LL_X11
  975. running_x11 = (mSDL_XWindowID != None);
  976. #endif //LL_X11
  977. llinfos << "LLWindowSDL::beforeDialog()" << llendl;
  978. if (SDLReallyCaptureInput(FALSE)) // must ungrab input so popup works!
  979. {
  980. if (mFullscreen)
  981. {
  982. // need to temporarily go non-fullscreen; bless SDL
  983. // for providing a SDL_WM_ToggleFullScreen() - though
  984. // it only works in X11
  985. if (running_x11 && mWindow)
  986. {
  987. SDL_WM_ToggleFullScreen(mWindow);
  988. }
  989. }
  990. }
  991. #if LL_X11
  992. if (mSDL_Display)
  993. {
  994. // Everything that we/SDL asked for should happen before we
  995. // potentially hand control over to GTK.
  996. maybe_lock_display();
  997. XSync(mSDL_Display, False);
  998. maybe_unlock_display();
  999. }
  1000. #endif // LL_X11
  1001. #if LL_GTK
  1002. // this is a good time to grab some GTK version information for
  1003. // diagnostics, if not already done.
  1004. ll_try_gtk_init();
  1005. #endif // LL_GTK
  1006. maybe_lock_display();
  1007. }
  1008. void LLWindowSDL::afterDialog()
  1009. {
  1010. bool running_x11 = false;
  1011. #if LL_X11
  1012. running_x11 = (mSDL_XWindowID != None);
  1013. #endif //LL_X11
  1014. llinfos << "LLWindowSDL::afterDialog()" << llendl;
  1015. maybe_unlock_display();
  1016. if (mFullscreen)
  1017. {
  1018. // need to restore fullscreen mode after dialog - only works
  1019. // in X11
  1020. if (running_x11 && mWindow)
  1021. {
  1022. SDL_WM_ToggleFullScreen(mWindow);
  1023. }
  1024. }
  1025. }
  1026. #if LL_X11
  1027. // set/reset the XWMHints flag for 'urgency' that usually makes the icon flash
  1028. void LLWindowSDL::x11_set_urgent(BOOL urgent)
  1029. {
  1030. if (mSDL_Display && !mFullscreen)
  1031. {
  1032. XWMHints *wm_hints;
  1033. llinfos << "X11 hint for urgency, " << urgent << llendl;
  1034. maybe_lock_display();
  1035. wm_hints = XGetWMHints(mSDL_Display, mSDL_XWindowID);
  1036. if (!wm_hints)
  1037. wm_hints = XAllocWMHints();
  1038. if (urgent)
  1039. wm_hints->flags |= XUrgencyHint;
  1040. else
  1041. wm_hints->flags &= ~XUrgencyHint;
  1042. XSetWMHints(mSDL_Display, mSDL_XWindowID, wm_hints);
  1043. XFree(wm_hints);
  1044. XSync(mSDL_Display, False);
  1045. maybe_unlock_display();
  1046. }
  1047. }
  1048. #endif // LL_X11
  1049. void LLWindowSDL::flashIcon(F32 seconds)
  1050. {
  1051. #if !LL_X11
  1052. llinfos << "Stub LLWindowSDL::flashIcon(" << seconds << ")" << llendl;
  1053. #else
  1054. llinfos << "X11 LLWindowSDL::flashIcon(" << seconds << ")" << llendl;
  1055. F32 remaining_time = mFlashTimer.getRemainingTimeF32();
  1056. if (remaining_time < seconds)
  1057. remaining_time = seconds;
  1058. mFlashTimer.reset();
  1059. mFlashTimer.setTimerExpirySec(remaining_time);
  1060. x11_set_urgent(TRUE);
  1061. mFlashing = TRUE;
  1062. #endif // LL_X11
  1063. }
  1064. #if LL_GTK
  1065. BOOL LLWindowSDL::isClipboardTextAvailable()
  1066. {
  1067. if (ll_try_gtk_init())
  1068. {
  1069. GtkClipboard * const clipboard =
  1070. gtk_clipboard_get(GDK_NONE);
  1071. return gtk_clipboard_wait_is_text_available(clipboard) ?
  1072. TRUE : FALSE;
  1073. }
  1074. return FALSE; // failure
  1075. }
  1076. BOOL LLWindowSDL::pasteTextFromClipboard(LLWString &text)
  1077. {
  1078. if (ll_try_gtk_init())
  1079. {
  1080. GtkClipboard * const clipboard =
  1081. gtk_clipboard_get(GDK_NONE);
  1082. gchar * const data = gtk_clipboard_wait_for_text(clipboard);
  1083. if (data)
  1084. {
  1085. text = LLWString(utf8str_to_wstring(data));
  1086. g_free(data);
  1087. return TRUE;
  1088. }
  1089. }
  1090. return FALSE; // failure
  1091. }
  1092. BOOL LLWindowSDL::copyTextToClipboard(const LLWString &text)
  1093. {
  1094. if (ll_try_gtk_init())
  1095. {
  1096. const std::string utf8 = wstring_to_utf8str(text);
  1097. GtkClipboard * const clipboard =
  1098. gtk_clipboard_get(GDK_NONE);
  1099. gtk_clipboard_set_text(clipboard, utf8.c_str(), utf8.length());
  1100. return TRUE;
  1101. }
  1102. return FALSE; // failure
  1103. }
  1104. BOOL LLWindowSDL::isPrimaryTextAvailable()
  1105. {
  1106. if (ll_try_gtk_init())
  1107. {
  1108. GtkClipboard * const clipboard =
  1109. gtk_clipboard_get(GDK_SELECTION_PRIMARY);
  1110. return gtk_clipboard_wait_is_text_available(clipboard) ?
  1111. TRUE : FALSE;
  1112. }
  1113. return FALSE; // failure
  1114. }
  1115. BOOL LLWindowSDL::pasteTextFromPrimary(LLWString &text)
  1116. {
  1117. if (ll_try_gtk_init())
  1118. {
  1119. GtkClipboard * const clipboard =
  1120. gtk_clipboard_get(GDK_SELECTION_PRIMARY);
  1121. gchar * const data = gtk_clipboard_wait_for_text(clipboard);
  1122. if (data)
  1123. {
  1124. text = LLWString(utf8str_to_wstring(data));
  1125. g_free(data);
  1126. return TRUE;
  1127. }
  1128. }
  1129. return FALSE; // failure
  1130. }
  1131. BOOL LLWindowSDL::copyTextToPrimary(const LLWString &text)
  1132. {
  1133. if (ll_try_gtk_init())
  1134. {
  1135. const std::string utf8 = wstring_to_utf8str(text);
  1136. GtkClipboard * const clipboard =
  1137. gtk_clipboard_get(GDK_SELECTION_PRIMARY);
  1138. gtk_clipboard_set_text(clipboard, utf8.c_str(), utf8.length());
  1139. return TRUE;
  1140. }
  1141. return FALSE; // failure
  1142. }
  1143. #else
  1144. BOOL LLWindowSDL::isClipboardTextAvailable()
  1145. {
  1146. return FALSE; // unsupported
  1147. }
  1148. BOOL LLWindowSDL::pasteTextFromClipboard(LLWString &dst)
  1149. {
  1150. return FALSE; // unsupported
  1151. }
  1152. BOOL LLWindowSDL::copyTextToClipboard(const LLWString &s)
  1153. {
  1154. return FALSE; // unsupported
  1155. }
  1156. BOOL LLWindowSDL::isPrimaryTextAvailable()
  1157. {
  1158. return FALSE; // unsupported
  1159. }
  1160. BOOL LLWindowSDL::pasteTextFromPrimary(LLWString &dst)
  1161. {
  1162. return FALSE; // unsupported
  1163. }
  1164. BOOL LLWindowSDL::copyTextToPrimary(const LLWString &s)
  1165. {
  1166. return FALSE; // unsupported
  1167. }
  1168. #endif // LL_GTK
  1169. LLWindow::LLWindowResolution* LLWindowSDL::getSupportedResolutions(S32 &num_resolutions)
  1170. {
  1171. if (!mSupportedResolutions)
  1172. {
  1173. mSupportedResolutions = new LLWindowResolution[MAX_NUM_RESOLUTIONS];
  1174. mNumSupportedResolutions = 0;
  1175. SDL_Rect **modes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
  1176. if ( (modes != NULL) && (modes != ((SDL_Rect **) -1)) )
  1177. {
  1178. int count = 0;
  1179. while (*modes && count<MAX_NUM_RESOLUTIONS) // they're sorted biggest to smallest, so find end...
  1180. {
  1181. modes++;
  1182. count++;
  1183. }
  1184. while (count--)
  1185. {
  1186. modes--;
  1187. SDL_Rect *r = *modes;
  1188. int w = r->w;
  1189. int h = r->h;
  1190. if ((w >= 800) && (h >= 600))
  1191. {
  1192. // make sure we don't add the same resolution multiple times!
  1193. if ( (mNumSupportedResolutions == 0) ||
  1194. ((mSupportedResolutions[mNumSupportedResolutions-1].mWidth != w) &&
  1195. (mSupportedResolutions[mNumSupportedResolutions-1].mHeight != h)) )
  1196. {
  1197. mSupportedResolutions[mNumSupportedResolutions].mWidth = w;
  1198. mSupportedResolutions[mNumSupportedResolutions].mHeight = h;
  1199. mNumSupportedResolutions++;
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. num_resolutions = mNumSupportedResolutions;
  1206. return mSupportedResolutions;
  1207. }
  1208. BOOL LLWindowSDL::convertCoords(LLCoordGL from, LLCoordWindow *to)
  1209. {
  1210. if (!to)
  1211. return FALSE;
  1212. to->mX = from.mX;
  1213. to->mY = mWindow->h - from.mY - 1;
  1214. return TRUE;
  1215. }
  1216. BOOL LLWindowSDL::convertCoords(LLCoordWindow from, LLCoordGL* to)
  1217. {
  1218. if (!to)
  1219. return FALSE;
  1220. to->mX = from.mX;
  1221. to->mY = mWindow->h - from.mY - 1;
  1222. return TRUE;
  1223. }
  1224. BOOL LLWindowSDL::convertCoords(LLCoordScreen from, LLCoordWindow* to)
  1225. {
  1226. if (!to)
  1227. return FALSE;
  1228. // In the fullscreen case, window and screen coordinates are the same.
  1229. to->mX = from.mX;
  1230. to->mY = from.mY;
  1231. return (TRUE);
  1232. }
  1233. BOOL LLWindowSDL::convertCoords(LLCoordWindow from, LLCoordScreen *to)
  1234. {
  1235. if (!to)
  1236. return FALSE;
  1237. // In the fullscreen case, window and screen coordinates are the same.
  1238. to->mX = from.mX;
  1239. to->mY = from.mY;
  1240. return (TRUE);
  1241. }
  1242. BOOL LLWindowSDL::convertCoords(LLCoordScreen from, LLCoordGL *to)
  1243. {
  1244. LLCoordWindow window_coord;
  1245. return(convertCoords(from, &window_coord) && convertCoords(window_coord, to));
  1246. }
  1247. BOOL LLWindowSDL::convertCoords(LLCoordGL from, LLCoordScreen *to)
  1248. {
  1249. LLCoordWindow window_coord;
  1250. return(convertCoords(from, &window_coord) && convertCoords(window_coord, to));
  1251. }
  1252. void LLWindowSDL::setupFailure(const std::string& text, const std::string& caption, U32 type)
  1253. {
  1254. destroyContext();
  1255. OSMessageBox(text, caption, type);
  1256. }
  1257. BOOL LLWindowSDL::SDLReallyCaptureInput(BOOL capture)
  1258. {
  1259. // note: this used to be safe to call nestedly, but in the
  1260. // end that's not really a wise usage pattern, so don't.
  1261. if (capture)
  1262. mReallyCapturedCount = 1;
  1263. else
  1264. mReallyCapturedCount = 0;
  1265. SDL_GrabMode wantmode, newmode;
  1266. if (mReallyCapturedCount <= 0) // uncapture
  1267. {
  1268. wantmode = SDL_GRAB_OFF;
  1269. } else // capture
  1270. {
  1271. wantmode = SDL_GRAB_ON;
  1272. }
  1273. if (mReallyCapturedCount < 0) // yuck, imbalance.
  1274. {
  1275. mReallyCapturedCount = 0;
  1276. llwarns << "ReallyCapture count was < 0" << llendl;
  1277. }
  1278. if (!mFullscreen) /* only bother if we're windowed anyway */
  1279. {
  1280. #if LL_X11
  1281. if (mSDL_Display)
  1282. {
  1283. /* we dirtily mix raw X11 with SDL so that our pointer
  1284. isn't (as often) constrained to the limits of the
  1285. window while grabbed, which feels nicer and
  1286. hopefully eliminates some reported 'sticky pointer'
  1287. problems. We use raw X11 instead of
  1288. SDL_WM_GrabInput() because the latter constrains
  1289. the pointer to the window and also steals all
  1290. *keyboard* input from the window manager, which was
  1291. frustrating users. */
  1292. int result;
  1293. if (wantmode == SDL_GRAB_ON)
  1294. {
  1295. //llinfos << "X11 POINTER GRABBY" << llendl;
  1296. //newmode = SDL_WM_GrabInput(wantmode);
  1297. maybe_lock_display();
  1298. result = XGrabPointer(mSDL_Display, mSDL_XWindowID,
  1299. True, 0, GrabModeAsync,
  1300. GrabModeAsync,
  1301. None, None, CurrentTime);
  1302. maybe_unlock_display();
  1303. if (GrabSuccess == result)
  1304. newmode = SDL_GRAB_ON;
  1305. else
  1306. newmode = SDL_GRAB_OFF;
  1307. } else if (wantmode == SDL_GRAB_OFF)
  1308. {
  1309. //llinfos << "X11 POINTER UNGRABBY" << llendl;
  1310. newmode = SDL_GRAB_OFF;
  1311. //newmode = SDL_WM_GrabInput(SDL_GRAB_OFF);
  1312. maybe_lock_display();
  1313. XUngrabPointer(mSDL_Display, CurrentTime);
  1314. // Make sure the ungrab happens RIGHT NOW.
  1315. XSync(mSDL_Display, False);
  1316. maybe_unlock_display();
  1317. } else
  1318. {
  1319. newmode = SDL_GRAB_QUERY; // neutral
  1320. }
  1321. } else // not actually running on X11, for some reason
  1322. newmode = wantmode;
  1323. #endif // LL_X11
  1324. } else {
  1325. // pretend we got what we wanted, when really we don't care.
  1326. newmode = wantmode;
  1327. }
  1328. // return boolean success for whether we ended up in the desired state
  1329. return (capture && SDL_GRAB_ON==newmode) ||
  1330. (!capture && SDL_GRAB_OFF==newmode);
  1331. }
  1332. U32 LLWindowSDL::SDLCheckGrabbyKeys(SDLKey keysym, BOOL gain)
  1333. {
  1334. /* part of the fix for SL-13243: Some popular window managers like
  1335. to totally eat alt-drag for the purposes of moving windows. We
  1336. spoil their day by acquiring the exclusive X11 mouse lock for as
  1337. long as ALT is held down, so the window manager can't easily
  1338. see what's happening. Tested successfully with Metacity.
  1339. And... do the same with CTRL, for other darn WMs. We don't
  1340. care about other metakeys as SL doesn't use them with dragging
  1341. (for now). */
  1342. /* We maintain a bitmap of critical keys which are up and down
  1343. instead of simply key-counting, because SDL sometimes reports
  1344. misbalanced keyup/keydown event pairs to us for whatever reason. */
  1345. U32 mask = 0;
  1346. switch (keysym)
  1347. {
  1348. case SDLK_LALT:
  1349. mask = 1U << 0; break;
  1350. case SDLK_RALT:
  1351. mask = 1U << 1; break;
  1352. case SDLK_LCTRL:
  1353. mask = 1U << 2; break;
  1354. case SDLK_RCTRL:
  1355. mask = 1U << 3; break;
  1356. default:
  1357. break;
  1358. }
  1359. if (gain)
  1360. mGrabbyKeyFlags |= mask;
  1361. else
  1362. mGrabbyKeyFlags &= ~mask;
  1363. //llinfos << "mGrabbyKeyFlags=" << mGrabbyKeyFlags << llendl;
  1364. /* 0 means we don't need to mousegrab, otherwise grab. */
  1365. return mGrabbyKeyFlags;
  1366. }
  1367. void check_vm_bloat()
  1368. {
  1369. #if LL_LINUX
  1370. // watch our own VM and RSS sizes, warn if we bloated rapidly
  1371. FILE *fp = fopen("/proc/self/stat", "r");
  1372. if (fp)
  1373. {
  1374. static long long last_vm_size = 0;
  1375. static long long last_rss_size = 0;
  1376. const long long significant_vm_difference = 250 * 1024*1024;
  1377. const long long significant_rss_difference = 50 * 1024*1024;
  1378. ssize_t res;
  1379. size_t dummy;
  1380. char *ptr;
  1381. for (int i=0; i<22; ++i) // parse past the values we don't want
  1382. {
  1383. ptr = NULL;
  1384. res = getdelim(&ptr, &dummy, ' ', fp);
  1385. free(ptr);
  1386. }
  1387. // 23rd space-delimited entry is vsize
  1388. ptr = NULL;
  1389. res = getdelim(&ptr, &dummy, ' ', fp);
  1390. llassert(ptr);
  1391. long long this_vm_size = atoll(ptr);
  1392. free(ptr);
  1393. // 24th space-delimited entry is RSS
  1394. ptr = NULL;
  1395. res = getdelim(&ptr, &dummy, ' ', fp);
  1396. llassert(ptr);
  1397. long long this_rss_size = getpagesize() * atoll(ptr);
  1398. free(ptr);
  1399. llinfos << "VM SIZE IS NOW " << (this_vm_size/(1024*1024)) << " MB, RSS SIZE IS NOW " << (this_rss_size/(1024*1024)) << " MB" << llendl;
  1400. if (llabs(last_vm_size - this_vm_size) >
  1401. significant_vm_difference)
  1402. {
  1403. if (this_vm_size > last_vm_size)
  1404. {
  1405. llwarns << "VM size grew by " << (this_vm_size - last_vm_size)/(1024*1024) << " MB in last frame" << llendl;
  1406. }
  1407. else
  1408. {
  1409. llinfos << "VM size shrank by " << (last_vm_size - this_vm_size)/(1024*1024) << " MB in last frame" << llendl;
  1410. }
  1411. }
  1412. if (llabs(last_rss_size - this_rss_size) >
  1413. significant_rss_difference)
  1414. {
  1415. if (this_rss_size > last_rss_size)
  1416. {
  1417. llwarns << "RSS size grew by " << (this_rss_size - last_rss_size)/(1024*1024) << " MB in last frame" << llendl;
  1418. }
  1419. else
  1420. {
  1421. llinfos << "RSS size shrank by " << (last_rss_size - this_rss_size)/(1024*1024) << " MB in last frame" << llendl;
  1422. }
  1423. }
  1424. last_rss_size = this_rss_size;
  1425. last_vm_size = this_vm_size;
  1426. fclose(fp);
  1427. }
  1428. #endif // LL_LINUX
  1429. }
  1430. // virtual
  1431. void LLWindowSDL::processMiscNativeEvents()
  1432. {
  1433. #if LL_GTK
  1434. // Pump GTK events to avoid starvation for:
  1435. // * DBUS servicing
  1436. // * Anything else which quietly hooks into the default glib/GTK loop
  1437. if (ll_try_gtk_init())
  1438. {
  1439. // Yuck, Mozilla's GTK callbacks play with the locale - push/pop
  1440. // the locale to protect it, as exotic/non-C locales
  1441. // causes our code lots of general critical weirdness
  1442. // and crashness. (SL-35450)
  1443. static std::string saved_locale;
  1444. saved_locale = ll_safe_string(setlocale(LC_ALL, NULL));
  1445. // Pump until we've nothing left to do or passed 1/15th of a
  1446. // second pumping for this frame.
  1447. static LLTimer pump_timer;
  1448. pump_timer.reset();
  1449. pump_timer.setTimerExpirySec(1.0f / 15.0f);
  1450. do {
  1451. // Always do at least one non-blocking pump
  1452. gtk_main_iteration_do(FALSE);
  1453. } while (gtk_events_pending() &&
  1454. !pump_timer.hasExpired());
  1455. setlocale(LC_ALL, saved_locale.c_str() );
  1456. }
  1457. #endif // LL_GTK
  1458. // hack - doesn't belong here - but this is just for debugging
  1459. if (getenv("LL_DEBUG_BLOAT"))
  1460. {
  1461. check_vm_bloat();
  1462. }
  1463. }
  1464. void LLWindowSDL::gatherInput()
  1465. {
  1466. const Uint32 CLICK_THRESHOLD = 300; // milliseconds
  1467. static int leftClick = 0;
  1468. static int rightClick = 0;
  1469. static Uint32 lastLeftDown = 0;
  1470. static Uint32 lastRightDown = 0;
  1471. SDL_Event event;
  1472. // Handle all outstanding SDL events
  1473. while (SDL_PollEvent(&event))
  1474. {
  1475. switch (event.type)
  1476. {
  1477. case SDL_MOUSEMOTION:
  1478. {
  1479. LLCoordWindow winCoord(event.button.x, event.button.y);
  1480. LLCoordGL openGlCoord;
  1481. convertCoords(winCoord, &openGlCoord);
  1482. MASK mask = gKeyboard->currentMask(TRUE);
  1483. mCallbacks->handleMouseMove(this, openGlCoord, mask);
  1484. break;
  1485. }
  1486. case SDL_KEYDOWN:
  1487. mKeyScanCode = event.key.keysym.scancode;
  1488. mKeyVirtualKey = event.key.keysym.unicode;
  1489. mKeyModifiers = event.key.keysym.mod;
  1490. gKeyboard->handleKeyDown(event.key.keysym.sym, event.key.keysym.mod);
  1491. // part of the fix for SL-13243
  1492. if (SDLCheckGrabbyKeys(event.key.keysym.sym, TRUE) != 0)
  1493. SDLReallyCaptureInput(TRUE);
  1494. if (event.key.keysym.unicode)
  1495. {
  1496. handleUnicodeUTF16(event.key.keysym.unicode,
  1497. gKeyboard->currentMask(FALSE));
  1498. }
  1499. break;
  1500. case SDL_KEYUP:
  1501. mKeyScanCode = event.key.keysym.scancode;
  1502. mKeyVirtualKey = event.key.keysym.unicode;
  1503. mKeyModifiers = event.key.keysym.mod;
  1504. if (SDLCheckGrabbyKeys(event.key.keysym.sym, FALSE) == 0)
  1505. SDLReallyCaptureInput(FALSE); // part of the fix for SL-13243
  1506. gKeyboard->handleKeyUp(event.key.keysym.sym, event.key.keysym.mod);
  1507. break;
  1508. case SDL_MOUSEBUTTONDOWN:
  1509. {
  1510. bool isDoubleClick = false;
  1511. LLCoordWindow winCoord(event.button.x, event.button.y);
  1512. LLCoordGL openGlCoord;
  1513. convertCoords(winCoord, &openGlCoord);
  1514. MASK mask = gKeyboard->currentMask(TRUE);
  1515. if (event.button.button == SDL_BUTTON_LEFT) // SDL doesn't manage double clicking...
  1516. {
  1517. Uint32 now = SDL_GetTicks();
  1518. if ((now - lastLeftDown) > CLICK_THRESHOLD)
  1519. leftClick = 1;
  1520. else
  1521. {
  1522. if (++leftClick >= 2)
  1523. {
  1524. leftClick = 0;
  1525. isDoubleClick = true;
  1526. }
  1527. }
  1528. lastLeftDown = now;
  1529. }
  1530. else if (event.button.button == SDL_BUTTON_RIGHT)
  1531. {
  1532. Uint32 now = SDL_GetTicks();
  1533. if ((now - lastRightDown) > CLICK_THRESHOLD)
  1534. rightClick = 1;
  1535. else
  1536. {
  1537. if (++rightClick >= 2)
  1538. {
  1539. rightClick = 0;
  1540. isDoubleClick = true;
  1541. }
  1542. }
  1543. lastRightDown = now;
  1544. }
  1545. if (event.button.button == SDL_BUTTON_LEFT) // left
  1546. {
  1547. if (isDoubleClick)
  1548. mCallbacks->handleDoubleClick(this, openGlCoord, mask);
  1549. else
  1550. mCallbacks->handleMouseDown(this, openGlCoord, mask);
  1551. }
  1552. else if (event.button.button == SDL_BUTTON_RIGHT) // right
  1553. {
  1554. mCallbacks->handleRightMouseDown(this, openGlCoord, mask);
  1555. }
  1556. else if (event.button.button == SDL_BUTTON_MIDDLE) // middle
  1557. {
  1558. mCallbacks->handleMiddleMouseDown(this, openGlCoord, mask);
  1559. }
  1560. else if (event.button.button == 4) // mousewheel up...thanks to X11 for making SDL consider these "buttons".
  1561. mCallbacks->handleScrollWheel(this, -1);
  1562. else if (event.button.button == 5) // mousewheel down...thanks to X11 for making SDL consider these "buttons".
  1563. mCallbacks->handleScrollWheel(this, 1);
  1564. break;
  1565. }
  1566. case SDL_MOUSEBUTTONUP:
  1567. {
  1568. LLCoordWindow winCoord(event.button.x, event.button.y);
  1569. LLCoordGL openGlCoord;
  1570. convertCoords(winCoord, &openGlCoord);
  1571. MASK mask = gKeyboard->currentMask(TRUE);
  1572. if (event.button.button == SDL_BUTTON_LEFT) // left
  1573. mCallbacks->handleMouseUp(this, openGlCoord, mask);
  1574. else if (event.button.button == SDL_BUTTON_RIGHT) // right
  1575. mCallbacks->handleRightMouseUp(this, openGlCoord, mask);
  1576. else if (event.button.button == SDL_BUTTON_MIDDLE) // middle
  1577. mCallbacks->handleMiddleMouseUp(this, openGlCoord, mask);
  1578. // don't handle mousewheel here...
  1579. break;
  1580. }
  1581. case SDL_VIDEOEXPOSE: // VIDEOEXPOSE doesn't specify the damage, but hey, it's OpenGL...repaint the whole thing!
  1582. mCallbacks->handlePaint(this, 0, 0, mWindow->w, mWindow->h);
  1583. break;
  1584. case SDL_VIDEORESIZE: // *FIX: handle this?
  1585. {
  1586. llinfos << "Handling a resize event: " << event.resize.w <<
  1587. "x" << event.resize.h << llendl;
  1588. S32 width = llmax(event.resize.w, (S32)mMinWindowWidth);
  1589. S32 height = llmax(event.resize.h, (S32)mMinWindowHeight);
  1590. // *FIX: I'm not sure this is necessary!
  1591. mWindow = SDL_SetVideoMode(width, height, 32, mSDLFlags);
  1592. if (!mWindow)
  1593. {
  1594. // *FIX: More informative dialog?
  1595. llinfos << "Could not recreate context after resize! Quitting..." << llendl;
  1596. if(mCallbacks->handleCloseRequest(this))
  1597. {
  1598. // Get the app to initiate cleanup.
  1599. mCallbacks->handleQuit(this);
  1600. // The app is responsible for calling destroyWindow when done with GL
  1601. }
  1602. break;
  1603. }
  1604. mCallbacks->handleResize(this, width, height);
  1605. break;
  1606. }
  1607. case SDL_ACTIVEEVENT:
  1608. if (event.active.state & SDL_APPINPUTFOCUS)
  1609. {
  1610. // Note that for SDL (particularly on X11), keyboard
  1611. // and mouse focus are independent things. Here we are
  1612. // tracking keyboard focus state changes.
  1613. // We have to do our own state massaging because SDL
  1614. // can send us two unfocus events in a row for example,
  1615. // which confuses the focus code [SL-24071].
  1616. if (event.active.gain != mHaveInputFocus)
  1617. {
  1618. mHaveInputFocus = !!event.active.gain;
  1619. if (mHaveInputFocus)
  1620. mCallbacks->handleFocus(this);
  1621. else
  1622. mCallbacks->handleFocusLost(this);
  1623. }
  1624. }
  1625. if (event.active.state & SDL_APPACTIVE)
  1626. {
  1627. // Change in iconification/minimization state.
  1628. if ((!event.active.gain) != mIsMinimized)
  1629. {
  1630. mIsMinimized = (!event.active.gain);
  1631. mCallbacks->handleActivate(this, !mIsMinimized);
  1632. llinfos << "SDL deiconification state switched to " << BOOL(event.active.gain) << llendl;
  1633. }
  1634. else
  1635. {
  1636. llinfos << "Ignored bogus redundant SDL deiconification state switch to " << BOOL(event.active.gain) << llendl;
  1637. }
  1638. }
  1639. break;
  1640. case SDL_QUIT:
  1641. if(mCallbacks->handleCloseRequest(this))
  1642. {
  1643. // Get the app to initiate cleanup.
  1644. mCallbacks->handleQuit(this);
  1645. // The app is responsible for calling destroyWindow when done with GL
  1646. }
  1647. break;
  1648. default:
  1649. //llinfos << "Unhandled SDL event type " << event.type << llendl;
  1650. break;
  1651. }
  1652. }
  1653. updateCursor();
  1654. #if LL_X11
  1655. // This is a good time to stop flashing the icon if our mFlashTimer has
  1656. // expired.
  1657. if (mFlashing && mFlashTimer.hasExpired())
  1658. {
  1659. x11_set_urgent(FALSE);
  1660. mFlashing = FALSE;
  1661. }
  1662. #endif // LL_X11
  1663. }
  1664. static SDL_Cursor *makeSDLCursorFromBMP(const char *filename, int hotx, int hoty)
  1665. {
  1666. SDL_Cursor *sdlcursor = NULL;
  1667. SDL_Surface *bmpsurface;
  1668. // Load cursor pixel data from BMP file
  1669. bmpsurface = Load_BMP_Resource(filename);
  1670. if (bmpsurface && bmpsurface->w%8==0)
  1671. {
  1672. SDL_Surface *cursurface;
  1673. lldebugs << "Loaded cursor file " << filename << " "
  1674. << bmpsurface->w << "x" << bmpsurface->h << llendl;
  1675. cursurface = SDL_CreateRGBSurface (SDL_SWSURFACE,
  1676. bmpsurface->w,
  1677. bmpsurface->h,
  1678. 32,
  1679. SDL_SwapLE32(0xFFU),
  1680. SDL_SwapLE32(0xFF00U),
  1681. SDL_SwapLE32(0xFF0000U),
  1682. SDL_SwapLE32(0xFF000000U));
  1683. SDL_FillRect(cursurface, NULL, SDL_SwapLE32(0x00000000U));
  1684. // Blit the cursor pixel data onto a 32-bit RGBA surface so we
  1685. // only have to cope with processing one type of pixel format.
  1686. if (0 == SDL_BlitSurface(bmpsurface, NULL,
  1687. cursurface, NULL))
  1688. {
  1689. // n.b. we already checked that width is a multiple of 8.
  1690. const int bitmap_bytes = (cursurface->w * cursurface->h) / 8;
  1691. unsigned char *cursor_data = new unsigned char[bitmap_bytes];
  1692. unsigned char *cursor_mask = new unsigned char[bitmap_bytes];
  1693. memset(cursor_data, 0, bitmap_bytes);
  1694. memset(cursor_mask, 0, bitmap_bytes);
  1695. int i,j;
  1696. // Walk the RGBA cursor pixel data, extracting both data and
  1697. // mask to build SDL-friendly cursor bitmaps from. The mask
  1698. // is inferred by color-keying against 200,200,200
  1699. for (i=0; i<cursurface->h; ++i) {
  1700. for (j=0; j<cursurface->w; ++j) {
  1701. U8 *pixelp =
  1702. ((U8*)cursurface->pixels)
  1703. + cursurface->pitch * i
  1704. + j*cursurface->format->BytesPerPixel;
  1705. U8 srcred = pixelp[0];
  1706. U8 srcgreen = pixelp[1];
  1707. U8 srcblue = pixelp[2];
  1708. BOOL mask_bit = (srcred != 200)
  1709. || (srcgreen != 200)
  1710. || (srcblue != 200);
  1711. BOOL data_bit = mask_bit && (srcgreen <= 80);//not 0x80
  1712. unsigned char bit_offset = (cursurface->w/8) * i
  1713. + j/8;
  1714. cursor_data[bit_offset] |= (data_bit) << (7 - (j&7));
  1715. cursor_mask[bit_offset] |= (mask_bit) << (7 - (j&7));
  1716. }
  1717. }
  1718. sdlcursor = SDL_CreateCursor((Uint8*)cursor_data,
  1719. (Uint8*)cursor_mask,
  1720. cursurface->w, cursurface->h,
  1721. hotx, hoty);
  1722. delete[] cursor_data;
  1723. delete[] cursor_mask;
  1724. } else {
  1725. llwarns << "CURSOR BLIT FAILURE, cursurface: " << cursurface << llendl;
  1726. }
  1727. SDL_FreeSurface(cursurface);
  1728. SDL_FreeSurface(bmpsurface);
  1729. } else {
  1730. llwarns << "CURSOR LOAD FAILURE " << filename << llendl;
  1731. }
  1732. return sdlcursor;
  1733. }
  1734. void LLWindowSDL::updateCursor()
  1735. {
  1736. if (ATIbug) {
  1737. // cursor-updating is very flaky when this bug is
  1738. // present; do nothing.
  1739. return;
  1740. }
  1741. if (mCurrentCursor != mNextCursor)
  1742. {
  1743. if (mNextCursor < UI_CURSOR_COUNT)
  1744. {
  1745. SDL_Cursor *sdlcursor = mSDLCursors[mNextCursor];
  1746. // Try to default to the arrow for any cursors that
  1747. // did not load correctly.
  1748. if (!sdlcursor && mSDLCursors[UI_CURSOR_ARROW])
  1749. sdlcursor = mSDLCursors[UI_CURSOR_ARROW];
  1750. if (sdlcursor)
  1751. SDL_SetCursor(sdlcursor);
  1752. } else {
  1753. llwarns << "Tried to set invalid cursor number " << mNextCursor << llendl;
  1754. }
  1755. mCurrentCursor = mNextCursor;
  1756. }
  1757. }
  1758. void LLWindowSDL::initCursors()
  1759. {
  1760. int i;
  1761. // Blank the cursor pointer array for those we may miss.
  1762. for (i=0; i<UI_CURSOR_COUNT; ++i)
  1763. {
  1764. mSDLCursors[i] = NULL;
  1765. }
  1766. // Pre-make an SDL cursor for each of the known cursor types.
  1767. // We hardcode the hotspots - to avoid that we'd have to write
  1768. // a .cur file loader.
  1769. // NOTE: SDL doesn't load RLE-compressed BMP files.
  1770. mSDLCursors[UI_CURSOR_ARROW] = makeSDLCursorFromBMP("llarrow.BMP",0,0);
  1771. mSDLCursors[UI_CURSOR_WAIT] = makeSDLCursorFromBMP("wait.BMP",12,15);
  1772. mSDLCursors[UI_CURSOR_HAND] = makeSDLCursorFromBMP("hand.BMP",7,10);
  1773. mSDLCursors[UI_CURSOR_IBEAM] = makeSDLCursorFromBMP("ibeam.BMP",15,16);
  1774. mSDLCursors[UI_CURSOR_CROSS] = makeSDLCursorFromBMP("cross.BMP",16,14);
  1775. mSDLCursors[UI_CURSOR_SIZENWSE] = makeSDLCursorFromBMP("sizenwse.BMP",14,17);
  1776. mSDLCursors[UI_CURSOR_SIZENESW] = makeSDLCursorFromBMP("sizenesw.BMP",17,17);
  1777. mSDLCursors[UI_CURSOR_SIZEWE] = makeSDLCursorFromBMP("sizewe.BMP",16,14);
  1778. mSDLCursors[UI_CURSOR_SIZENS] = makeSDLCursorFromBMP("sizens.BMP",17,16);
  1779. mSDLCursors[UI_CURSOR_NO] = makeSDLCursorFromBMP("llno.BMP",8,8);
  1780. mSDLCursors[UI_CURSOR_WORKING] = makeSDLCursorFromBMP("working.BMP",12,15);
  1781. mSDLCursors[UI_CURSOR_TOOLGRAB] = makeSDLCursorFromBMP("lltoolgrab.BMP",2,13);
  1782. mSDLCursors[UI_CURSOR_TOOLLAND] = makeSDLCursorFromBMP("lltoolland.BMP",1,6);
  1783. mSDLCursors[UI_CURSOR_TOOLFOCUS] = makeSDLCursorFromBMP("lltoolfocus.BMP",8,5);
  1784. mSDLCursors[UI_CURSOR_TOOLCREATE] = makeSDLCursorFromBMP("lltoolcreate.BMP",7,7);
  1785. mSDLCursors[UI_CURSOR_ARROWDRAG] = makeSDLCursorFromBMP("arrowdrag.BMP",0,0);
  1786. mSDLCursors[UI_CURSOR_ARROWCOPY] = makeSDLCursorFromBMP("arrowcop.BMP",0,0);
  1787. mSDLCursors[UI_CURSOR_ARROWDRAGMULTI] = makeSDLCursorFromBMP("llarrowdragmulti.BMP",0,0);
  1788. mSDLCursors[UI_CURSOR_ARROWCOPYMULTI] = makeSDLCursorFromBMP("arrowcopmulti.BMP",0,0);
  1789. mSDLCursors[UI_CURSOR_NOLOCKED] = makeSDLCursorFromBMP("llnolocked.BMP",8,8);
  1790. mSDLCursors[UI_CURSOR_ARROWLOCKED] = makeSDLCursorFromBMP("llarrowlocked.BMP",0,0);
  1791. mSDLCursors[UI_CURSOR_GRABLOCKED] = makeSDLCursorFromBMP("llgrablocked.BMP",2,13);
  1792. mSDLCursors[UI_CURSOR_TOOLTRANSLATE] = makeSDLCursorFromBMP("lltooltranslate.BMP",0,0);
  1793. mSDLCursors[UI_CURSOR_TOOLROTATE] = makeSDLCursorFromBMP("lltoolrotate.BMP",0,0);
  1794. mSDLCursors[UI_CURSOR_TOOLSCALE] = makeSDLCursorFromBMP("lltoolscale.BMP",0,0);
  1795. mSDLCursors[UI_CURSOR_TOOLCAMERA] = makeSDLCursorFromBMP("lltoolcamera.BMP",7,5);
  1796. mSDLCursors[UI_CURSOR_TOOLPAN] = makeSDLCursorFromBMP("lltoolpan.BMP",7,5);
  1797. mSDLCursors[UI_CURSOR_TOOLZOOMIN] = makeSDLCursorFromBMP("lltoolzoomin.BMP",7,5);
  1798. mSDLCursors[UI_CURSOR_TOOLPICKOBJECT3] = makeSDLCursorFromBMP("toolpickobject3.BMP",0,0);
  1799. mSDLCursors[UI_CURSOR_TOOLPLAY] = makeSDLCursorFromBMP("toolplay.BMP",0,0);
  1800. mSDLCursors[UI_CURSOR_TOOLPAUSE] = makeSDLCursorFromBMP("toolpause.BMP",0,0);
  1801. mSDLCursors[UI_CURSOR_TOOLMEDIAOPEN] = makeSDLCursorFromBMP("toolmediaopen.BMP",0,0);
  1802. mSDLCursors[UI_CURSOR_PIPETTE] = makeSDLCursorFromBMP("lltoolpipette.BMP",2,28);
  1803. mSDLCursors[UI_CURSOR_TOOLSIT] = makeSDLCursorFromBMP("toolsit.BMP",20,15);
  1804. mSDLCursors[UI_CURSOR_TOOLBUY] = makeSDLCursorFromBMP("toolbuy.BMP",20,15);
  1805. mSDLCursors[UI_CURSOR_TOOLOPEN] = makeSDLCursorFromBMP("toolopen.BMP",20,15);
  1806. if (getenv("LL_ATI_MOUSE_CURSOR_BUG") != NULL) {
  1807. llinfos << "Disabling cursor updating due to LL_ATI_MOUSE_CURSOR_BUG" << llendl;
  1808. ATIbug = true;
  1809. }
  1810. }
  1811. void LLWindowSDL::quitCursors()
  1812. {
  1813. int i;
  1814. if (mWindow)
  1815. {
  1816. for (i=0; i<UI_CURSOR_COUNT; ++i)
  1817. {
  1818. if (mSDLCursors[i])
  1819. {
  1820. SDL_FreeCursor(mSDLCursors[i]);
  1821. mSDLCursors[i] = NULL;
  1822. }
  1823. }
  1824. } else {
  1825. // SDL doesn't refcount cursors, so if the window has
  1826. // already been destroyed then the cursors have gone with it.
  1827. llinfos << "Skipping quitCursors: mWindow already gone." << llendl;
  1828. for (i=0; i<UI_CURSOR_COUNT; ++i)
  1829. mSDLCursors[i] = NULL;
  1830. }
  1831. }
  1832. void LLWindowSDL::captureMouse()
  1833. {
  1834. // SDL already enforces the semantics that captureMouse is
  1835. // used for, i.e. that we continue to get mouse events as long
  1836. // as a button is down regardless of whether we left the
  1837. // window, and in a less obnoxious way than SDL_WM_GrabInput
  1838. // which would confine the cursor to the window too.
  1839. lldebugs << "LLWindowSDL::captureMouse" << llendl;
  1840. }
  1841. void LLWindowSDL::releaseMouse()
  1842. {
  1843. // see LWindowSDL::captureMouse()
  1844. lldebugs << "LLWindowSDL::releaseMouse" << llendl;
  1845. }
  1846. void LLWindowSDL::hideCursor()
  1847. {
  1848. if(!mCursorHidden)
  1849. {
  1850. // llinfos << "hideCursor: hiding" << llendl;
  1851. mCursorHidden = TRUE;
  1852. mHideCursorPermanent = TRUE;
  1853. SDL_ShowCursor(0);
  1854. }
  1855. else
  1856. {
  1857. // llinfos << "hideCursor: already hidden" << llendl;
  1858. }
  1859. }
  1860. void LLWindowSDL::showCursor()
  1861. {
  1862. if(mCursorHidden)
  1863. {
  1864. // llinfos << "showCursor: showing" << llendl;
  1865. mCursorHidden = FALSE;
  1866. mHideCursorPermanent = FALSE;
  1867. SDL_ShowCursor(1);
  1868. }
  1869. else
  1870. {
  1871. // llinfos << "showCursor: already visible" << llendl;
  1872. }
  1873. }
  1874. void LLWindowSDL::showCursorFromMouseMove()
  1875. {
  1876. if (!mHideCursorPermanent)
  1877. {
  1878. showCursor();
  1879. }
  1880. }
  1881. void LLWindowSDL::hideCursorUntilMouseMove()
  1882. {
  1883. if (!mHideCursorPermanent)
  1884. {
  1885. hideCursor();
  1886. mHideCursorPermanent = FALSE;
  1887. }
  1888. }
  1889. //
  1890. // LLSplashScreenSDL - I don't think we'll bother to implement this; it's
  1891. // fairly obsolete at this point.
  1892. //
  1893. LLSplashScreenSDL::LLSplashScreenSDL()
  1894. {
  1895. }
  1896. LLSplashScreenSDL::~LLSplashScreenSDL()
  1897. {
  1898. }
  1899. void LLSplashScreenSDL::showImpl()
  1900. {
  1901. }
  1902. void LLSplashScreenSDL::updateImpl(const std::string& mesg)
  1903. {
  1904. }
  1905. void LLSplashScreenSDL::hideImpl()
  1906. {
  1907. }
  1908. #if LL_GTK
  1909. static void response_callback (GtkDialog *dialog,
  1910. gint arg1,
  1911. gpointer user_data)
  1912. {
  1913. gint *response = (gint*)user_data;
  1914. *response = arg1;
  1915. gtk_widget_destroy(GTK_WIDGET(dialog));
  1916. gtk_main_quit();
  1917. }
  1918. S32 OSMessageBoxSDL(const std::string& text, const std::string& caption, U32 type)
  1919. {
  1920. S32 rtn = OSBTN_CANCEL;
  1921. if(gWindowImplementation != NULL)
  1922. gWindowImplementation->beforeDialog();
  1923. if (LLWindowSDL::ll_try_gtk_init())
  1924. {
  1925. GtkWidget *win = NULL;
  1926. llinfos << "Creating a dialog because we're in windowed mode and GTK is happy." << llendl;
  1927. GtkDialogFlags flags = GTK_DIALOG_MODAL;
  1928. GtkMessageType messagetype;
  1929. GtkButtonsType buttons;
  1930. switch (type)
  1931. {
  1932. default:
  1933. case OSMB_OK:
  1934. messagetype = GTK_MESSAGE_WARNING;
  1935. buttons = GTK_BUTTONS_OK;
  1936. break;
  1937. case OSMB_OKCANCEL:
  1938. messagetype = GTK_MESSAGE_QUESTION;
  1939. buttons = GTK_BUTTONS_OK_CANCEL;
  1940. break;
  1941. case OSMB_YESNO:
  1942. messagetype = GTK_MESSAGE_QUESTION;
  1943. buttons = GTK_BUTTONS_YES_NO;
  1944. break;
  1945. }
  1946. win = gtk_message_dialog_new(NULL, flags, messagetype, buttons, "%s",
  1947. text.c_str());
  1948. # if LL_X11
  1949. // Make GTK tell the window manager to associate this
  1950. // dialog with our non-GTK SDL window, which should try
  1951. // to keep it on top etc.
  1952. if (gWindowImplementation &&
  1953. gWindowImplementation->mSDL_XWindowID != None)
  1954. {
  1955. gtk_widget_realize(GTK_WIDGET(win)); // so we can get its gdkwin
  1956. GdkWindow *gdkwin = gdk_window_foreign_new(gWindowImplementation->mSDL_XWindowID);
  1957. gdk_window_set_transient_for(GTK_WIDGET(win)->window,
  1958. gdkwin);
  1959. }
  1960. # endif //LL_X11
  1961. gtk_window_set_position(GTK_WINDOW(win),
  1962. GTK_WIN_POS_CENTER_ON_PARENT);
  1963. gtk_window_set_type_hint(GTK_WINDOW(win),
  1964. GDK_WINDOW_TYPE_HINT_DIALOG);
  1965. if (!caption.empty())
  1966. gtk_window_set_title(GTK_WINDOW(win), caption.c_str());
  1967. gint response = GTK_RESPONSE_NONE;
  1968. g_signal_connect (win,
  1969. "response",
  1970. G_CALLBACK (response_callback),
  1971. &response);
  1972. // we should be able to use a gtk_dialog_run(), but it's
  1973. // apparently not written to exist in a world without a higher
  1974. // gtk_main(), so we manage its signal/destruction outselves.
  1975. gtk_widget_show_all (win);
  1976. gtk_main();
  1977. //llinfos << "response: " << response << llendl;
  1978. switch (response)
  1979. {
  1980. case GTK_RESPONSE_OK: rtn = OSBTN_OK; break;
  1981. case GTK_RESPONSE_YES: rtn = OSBTN_YES; break;
  1982. case GTK_RESPONSE_NO: rtn = OSBTN_NO; break;
  1983. case GTK_RESPONSE_APPLY: rtn = OSBTN_OK; break;
  1984. case GTK_RESPONSE_NONE:
  1985. case GTK_RESPONSE_CANCEL:
  1986. case GTK_RESPONSE_CLOSE:
  1987. case GTK_RESPONSE_DELETE_EVENT:
  1988. default: rtn = OSBTN_CANCEL;
  1989. }
  1990. }
  1991. else
  1992. {
  1993. llinfos << "MSGBOX: " << caption << ": " << text << llendl;
  1994. llinfos << "Skipping dialog because we're in fullscreen mode or GTK is not happy." << llendl;
  1995. rtn = OSBTN_OK;
  1996. }
  1997. if(gWindowImplementation != NULL)
  1998. gWindowImplementation->afterDialog();
  1999. return rtn;
  2000. }
  2001. static void color_changed_callback(GtkWidget *widget,
  2002. gpointer user_data)
  2003. {
  2004. GtkColorSelection *colorsel = GTK_COLOR_SELECTION(widget);
  2005. GdkColor *colorp = (GdkColor*)user_data;
  2006. gtk_color_selection_get_current_color(colorsel, colorp);
  2007. }
  2008. /*
  2009. Make the raw keyboard data available - used to poke through to LLQtWebKit so
  2010. that Qt/Webkit has access to the virtual keycodes etc. that it needs
  2011. */
  2012. LLSD LLWindowSDL::getNativeKeyData()
  2013. {
  2014. LLSD result = LLSD::emptyMap();
  2015. U32 modifiers = 0; // pretend-native modifiers... oh what a tangled web we weave!
  2016. // we go through so many levels of device abstraction that I can't really guess
  2017. // what a plugin under GDK under Qt under SL under SDL under X11 considers
  2018. // a 'native' modifier mask. this has been sort of reverse-engineered... they *appear*
  2019. // to match GDK consts, but that may be co-incidence.
  2020. modifiers |= (mKeyModifiers & KMOD_LSHIFT) ? 0x0001 : 0;
  2021. modifiers |= (mKeyModifiers & KMOD_RSHIFT) ? 0x0001 : 0;// munge these into the same shift
  2022. modifiers |= (mKeyModifiers & KMOD_CAPS) ? 0x0002 : 0;
  2023. modifiers |= (mKeyModifiers & KMOD_LCTRL) ? 0x0004 : 0;
  2024. modifiers |= (mKeyModifiers & KMOD_RCTRL) ? 0x0004 : 0;// munge these into the same ctrl
  2025. modifiers |= (mKeyModifiers & KMOD_LALT) ? 0x0008 : 0;// untested
  2026. modifiers |= (mKeyModifiers & KMOD_RALT) ? 0x0008 : 0;// untested
  2027. // *todo: test ALTs - I don't have a case for testing these. Do you?
  2028. // *todo: NUM? - I don't care enough right now (and it's not a GDK modifier).
  2029. result["scan_code"] = (S32)mKeyScanCode;
  2030. result["virtual_key"] = (S32)mKeyVirtualKey;
  2031. result["modifiers"] = (S32)modifiers;
  2032. return result;
  2033. }
  2034. BOOL LLWindowSDL::dialogColorPicker( F32 *r, F32 *g, F32 *b)
  2035. {
  2036. BOOL rtn = FALSE;
  2037. beforeDialog();
  2038. if (ll_try_gtk_init())
  2039. {
  2040. GtkWidget *win = NULL;
  2041. win = gtk_color_selection_dialog_new(NULL);
  2042. # if LL_X11
  2043. // Get GTK to tell the window manager to associate this
  2044. // dialog with our non-GTK SDL window, which should try
  2045. // to keep it on top etc.
  2046. if (m