CIrrDeviceLinux.cpp 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // Copyright (C) 2014-2015 Dawid Gan
  3. // This file is part of the "Irrlicht Engine".
  4. // For conditions of distribution and use, see copyright notice in irrlicht.h
  5. extern bool GLContextDebugBit;
  6. #include "CIrrDeviceLinux.h"
  7. #ifdef _IRR_COMPILE_WITH_X11_DEVICE_
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <sys/utsname.h>
  12. #include <time.h>
  13. #include <locale.h>
  14. #include "IEventReceiver.h"
  15. #include "ISceneManager.h"
  16. #include "IGUIEnvironment.h"
  17. #include "os.h"
  18. #include "CTimer.h"
  19. #include "irrString.h"
  20. #include "Keycodes.h"
  21. #include "COSOperator.h"
  22. #include "CColorConverter.h"
  23. #include "SIrrCreationParameters.h"
  24. #include "IGUISpriteBank.h"
  25. #include <X11/XKBlib.h>
  26. #include <X11/Xatom.h>
  27. #ifdef _IRR_LINUX_XCURSOR_
  28. #include <X11/Xcursor/Xcursor.h>
  29. #endif
  30. #if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. #ifdef __FreeBSD__
  34. #include <sys/joystick.h>
  35. #elif defined(__linux__)
  36. // linux/joystick.h includes linux/input.h, which #defines values for various KEY_FOO keys.
  37. // These override the irr::KEY_FOO equivalents, which stops key handling from working.
  38. // As a workaround, defining _INPUT_H stops linux/input.h from being included; it
  39. // doesn't actually seem to be necessary except to pull in sys/ioctl.h.
  40. #define _INPUT_H
  41. #include <sys/ioctl.h> // Would normally be included in linux/input.h
  42. #include <linux/joystick.h>
  43. #undef _INPUT_H
  44. #endif
  45. #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
  46. #define XRANDR_ROTATION_LEFT (1 << 1)
  47. #define XRANDR_ROTATION_RIGHT (1 << 3)
  48. namespace irr
  49. {
  50. namespace video
  51. {
  52. extern bool useCoreContext;
  53. IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
  54. io::IFileSystem* io, CIrrDeviceLinux* device);
  55. IVideoDriver* createOGLES2Driver(const SIrrlichtCreationParameters& params,
  56. video::SExposedVideoData& data, io::IFileSystem* io);
  57. }
  58. } // end namespace irr
  59. #if defined(_IRR_COMPILE_WITH_X11_)
  60. namespace
  61. {
  62. Atom X_ATOM_CLIPBOARD;
  63. Atom X_ATOM_TARGETS;
  64. Atom X_ATOM_UTF8_STRING;
  65. };
  66. #endif
  67. namespace irr
  68. {
  69. const char* wmDeleteWindow = "WM_DELETE_WINDOW";
  70. //! constructor
  71. CIrrDeviceLinux::CIrrDeviceLinux(const SIrrlichtCreationParameters& param)
  72. : CIrrDeviceStub(param),
  73. #ifdef _IRR_COMPILE_WITH_X11_
  74. display(0), visual(0), screennr(0), window(0), StdHints(0), SoftwareImage(0),
  75. XInputMethod(0), XInputContext(0), m_font_set(0), numlock_mask(0),
  76. m_ime_enabled(false),
  77. #ifdef _IRR_COMPILE_WITH_OPENGL_
  78. glxWin(0),
  79. Context(0),
  80. #endif
  81. #endif
  82. Width(param.WindowSize.Width), Height(param.WindowSize.Height),
  83. WindowHasFocus(false), WindowMinimized(false),
  84. UseXVidMode(false), UseXRandR(false), UseGLXWindow(false),
  85. ExternalWindow(false), AutorepeatSupport(0)
  86. {
  87. #ifdef _DEBUG
  88. setDebugName("CIrrDeviceLinux");
  89. #endif
  90. // print version, distribution etc.
  91. // thx to LynxLuna for pointing me to the uname function
  92. core::stringc linuxversion;
  93. struct utsname LinuxInfo;
  94. uname(&LinuxInfo);
  95. linuxversion += LinuxInfo.sysname;
  96. linuxversion += " ";
  97. linuxversion += LinuxInfo.release;
  98. linuxversion += " ";
  99. linuxversion += LinuxInfo.version;
  100. linuxversion += " ";
  101. linuxversion += LinuxInfo.machine;
  102. Operator = new COSOperator(linuxversion, this);
  103. os::Printer::log(linuxversion.c_str(), ELL_INFORMATION);
  104. // create keymap
  105. createKeyMap();
  106. // create window
  107. if (CreationParams.DriverType != video::EDT_NULL)
  108. {
  109. // create the window, only if we do not use the null device
  110. if (!createWindow())
  111. return;
  112. }
  113. // create cursor control
  114. CursorControl = new CCursorControl(this, CreationParams.DriverType == video::EDT_NULL);
  115. // create driver
  116. createDriver();
  117. if (!VideoDriver)
  118. return;
  119. #ifdef _IRR_COMPILE_WITH_X11_
  120. m_ime_position.x = 0;
  121. m_ime_position.y = 0;
  122. createInputContext();
  123. numlock_mask = getNumlockMask(display);
  124. // Get maximum 16 characters from input method
  125. m_ime_char_holder.reallocate(16);
  126. #endif
  127. createGUIAndScene();
  128. }
  129. //! destructor
  130. CIrrDeviceLinux::~CIrrDeviceLinux()
  131. {
  132. #ifdef _IRR_COMPILE_WITH_X11_
  133. if (StdHints)
  134. XFree(StdHints);
  135. // Disable cursor (it is drop'ed in stub)
  136. if (CursorControl)
  137. {
  138. CursorControl->setVisible(false);
  139. static_cast<CCursorControl*>(CursorControl)->clearCursors();
  140. }
  141. // Must free OpenGL textures etc before destroying context, so can't wait for stub destructor
  142. if ( GUIEnvironment )
  143. {
  144. GUIEnvironment->drop();
  145. GUIEnvironment = NULL;
  146. }
  147. if ( SceneManager )
  148. {
  149. SceneManager->drop();
  150. SceneManager = NULL;
  151. }
  152. if ( VideoDriver )
  153. {
  154. VideoDriver->drop();
  155. VideoDriver = NULL;
  156. }
  157. destroyInputContext();
  158. if (display)
  159. {
  160. #ifdef _IRR_COMPILE_WITH_OPENGL_
  161. if (Context)
  162. {
  163. if (glxWin)
  164. {
  165. if (!glXMakeContextCurrent(display, None, None, NULL))
  166. os::Printer::log("Could not release glx context.", ELL_WARNING);
  167. }
  168. else
  169. {
  170. if (!glXMakeCurrent(display, None, NULL))
  171. os::Printer::log("Could not release glx context.", ELL_WARNING);
  172. }
  173. glXDestroyContext(display, Context);
  174. if (glxWin)
  175. glXDestroyWindow(display, glxWin);
  176. }
  177. #endif // #ifdef _IRR_COMPILE_WITH_OPENGL_
  178. if (SoftwareImage)
  179. XDestroyImage(SoftwareImage);
  180. if (!ExternalWindow)
  181. {
  182. XDestroyWindow(display,window);
  183. }
  184. // Reset fullscreen resolution change
  185. restoreResolution();
  186. if (!ExternalWindow)
  187. {
  188. XCloseDisplay(display);
  189. }
  190. }
  191. if (visual)
  192. XFree(visual);
  193. #endif // #ifdef _IRR_COMPILE_WITH_X11_
  194. #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
  195. for (u32 joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
  196. {
  197. if (ActiveJoysticks[joystick].fd >= 0)
  198. {
  199. close(ActiveJoysticks[joystick].fd);
  200. }
  201. }
  202. #endif
  203. }
  204. #if defined(_IRR_COMPILE_WITH_X11_)
  205. static bool XErrorSignaled = false;
  206. int IrrPrintXError(Display *display, XErrorEvent *event)
  207. {
  208. char msg[256];
  209. char msg2[256];
  210. XErrorSignaled = true;
  211. snprintf(msg, 256, "%d", event->request_code);
  212. XGetErrorDatabaseText(display, "XRequest", msg, "unknown", msg2, 256);
  213. XGetErrorText(display, event->error_code, msg, 256);
  214. os::Printer::log("X Error", msg, ELL_WARNING);
  215. os::Printer::log("From call ", msg2, ELL_WARNING);
  216. return 0;
  217. }
  218. #endif
  219. bool CIrrDeviceLinux::restoreResolution()
  220. {
  221. if (!CreationParams.Fullscreen)
  222. return true;
  223. #ifdef _IRR_LINUX_X11_VIDMODE_
  224. if (UseXVidMode && CreationParams.Fullscreen)
  225. {
  226. XF86VidModeSwitchToMode(display, screennr, &oldVideoMode);
  227. XF86VidModeSetViewPort(display, screennr, 0, 0);
  228. }
  229. #endif
  230. #ifdef _IRR_LINUX_X11_RANDR_
  231. if (UseXRandR && CreationParams.Fullscreen && old_mode != BadRRMode)
  232. {
  233. XRRScreenResources* res = XRRGetScreenResources(display, DefaultRootWindow(display));
  234. if (!res)
  235. return false;
  236. XRROutputInfo* output = XRRGetOutputInfo(display, res, output_id);
  237. if (!output || !output->crtc || output->connection == RR_Disconnected)
  238. {
  239. XRRFreeOutputInfo(output);
  240. return false;
  241. }
  242. XRRCrtcInfo* crtc = XRRGetCrtcInfo(display, res, output->crtc);
  243. if (!crtc)
  244. {
  245. XRRFreeOutputInfo(output);
  246. return false;
  247. }
  248. Status s = XRRSetCrtcConfig(display, res, output->crtc, CurrentTime,
  249. crtc->x, crtc->y, old_mode,
  250. crtc->rotation, &output_id, 1);
  251. XRRFreeOutputInfo(output);
  252. XRRFreeCrtcInfo(crtc);
  253. XRRFreeScreenResources(res);
  254. if (s != Success)
  255. return false;
  256. }
  257. #endif
  258. return true;
  259. }
  260. bool CIrrDeviceLinux::changeResolution()
  261. {
  262. if (!CreationParams.Fullscreen)
  263. return true;
  264. getVideoModeList();
  265. #if defined(_IRR_LINUX_X11_VIDMODE_) || defined(_IRR_LINUX_X11_RANDR_)
  266. s32 eventbase, errorbase;
  267. s32 bestMode = -1;
  268. #endif
  269. #ifdef _IRR_LINUX_X11_VIDMODE_
  270. if (XF86VidModeQueryExtension(display, &eventbase, &errorbase))
  271. {
  272. // enumerate video modes
  273. s32 modeCount;
  274. XF86VidModeModeInfo** modes;
  275. float refresh_rate, refresh_rate_old;
  276. XF86VidModeGetAllModeLines(display, screennr, &modeCount, &modes);
  277. // find fitting mode
  278. for (s32 i = 0; i<modeCount; ++i)
  279. {
  280. if (bestMode==-1 && modes[i]->hdisplay >= Width && modes[i]->vdisplay >= Height)
  281. {
  282. bestMode = i;
  283. }
  284. else if (bestMode!=-1 &&
  285. modes[i]->hdisplay == modes[bestMode]->hdisplay &&
  286. modes[i]->vdisplay == modes[bestMode]->vdisplay)
  287. {
  288. refresh_rate_old = (modes[bestMode]->dotclock * 1000.0) / (modes[bestMode]->htotal * modes[bestMode]->vtotal);
  289. refresh_rate = (modes[i]->dotclock * 1000.0) / (modes[i]->htotal * modes[i]->vtotal);
  290. if (refresh_rate > refresh_rate_old)
  291. {
  292. bestMode = i;
  293. }
  294. }
  295. else if (bestMode!=-1 &&
  296. modes[i]->hdisplay >= Width &&
  297. modes[i]->vdisplay >= Height &&
  298. modes[i]->hdisplay <= modes[bestMode]->hdisplay &&
  299. modes[i]->vdisplay <= modes[bestMode]->vdisplay)
  300. {
  301. bestMode = i;
  302. }
  303. }
  304. if (bestMode != -1)
  305. {
  306. os::Printer::log("Starting vidmode fullscreen mode...", ELL_INFORMATION);
  307. os::Printer::log("hdisplay: ", core::stringc(modes[bestMode]->hdisplay).c_str(), ELL_INFORMATION);
  308. os::Printer::log("vdisplay: ", core::stringc(modes[bestMode]->vdisplay).c_str(), ELL_INFORMATION);
  309. XF86VidModeSwitchToMode(display, screennr, modes[bestMode]);
  310. XF86VidModeSetViewPort(display, screennr, 0, 0);
  311. UseXVidMode=true;
  312. }
  313. else
  314. {
  315. os::Printer::log("Could not find specified video mode, running windowed.", ELL_WARNING);
  316. CreationParams.Fullscreen = false;
  317. }
  318. XFree(modes);
  319. }
  320. #endif
  321. #ifdef _IRR_LINUX_X11_RANDR_
  322. while (XRRQueryExtension(display, &eventbase, &errorbase))
  323. {
  324. if (output_id == BadRROutput)
  325. break;
  326. XRRScreenResources* res = XRRGetScreenResources(display, DefaultRootWindow(display));
  327. if (!res)
  328. break;
  329. XRROutputInfo* output = XRRGetOutputInfo(display, res, output_id);
  330. if (!output || !output->crtc || output->connection == RR_Disconnected)
  331. {
  332. XRRFreeOutputInfo(output);
  333. XRRFreeScreenResources(res);
  334. break;
  335. }
  336. XRRCrtcInfo* crtc = XRRGetCrtcInfo(display, res, output->crtc);
  337. if (!crtc)
  338. {
  339. XRRFreeOutputInfo(output);
  340. XRRFreeScreenResources(res);
  341. break;
  342. }
  343. float refresh_rate, refresh_rate_new;
  344. core::dimension2d<u32> mode0_size = core::dimension2d<u32>(0, 0);
  345. for (int i = 0; i < res->nmode; i++)
  346. {
  347. const XRRModeInfo* mode = &res->modes[i];
  348. core::dimension2d<u32> size;
  349. if (crtc->rotation & (XRANDR_ROTATION_LEFT|XRANDR_ROTATION_RIGHT))
  350. {
  351. size = core::dimension2d<u32>(mode->height, mode->width);
  352. }
  353. else
  354. {
  355. size = core::dimension2d<u32>(mode->width, mode->height);
  356. }
  357. if (bestMode == -1 && mode->id == output->modes[0])
  358. {
  359. mode0_size = size;
  360. }
  361. if (bestMode == -1 && size.Width == Width && size.Height == Height)
  362. {
  363. for (int j = 0; j < output->nmode; j++)
  364. {
  365. if (mode->id == output->modes[j])
  366. {
  367. bestMode = j;
  368. refresh_rate = (mode->dotClock * 1000.0) / (mode->hTotal * mode->vTotal);
  369. break;
  370. }
  371. }
  372. }
  373. else if (bestMode != -1 && size.Width == Width && size.Height == Height)
  374. {
  375. refresh_rate_new = (mode->dotClock * 1000.0) / (mode->hTotal * mode->vTotal);
  376. if (refresh_rate_new <= refresh_rate)
  377. break;
  378. for (int j = 0; j < output->nmode; j++)
  379. {
  380. if (mode->id == output->modes[j])
  381. {
  382. bestMode = j;
  383. refresh_rate = refresh_rate_new;
  384. break;
  385. }
  386. }
  387. }
  388. }
  389. // If video mode not found, try to use first available
  390. if (bestMode == -1)
  391. {
  392. bestMode = 0;
  393. Width = mode0_size.Width;
  394. Height = mode0_size.Height;
  395. }
  396. Status s = XRRSetCrtcConfig(display, res, output->crtc, CurrentTime,
  397. crtc->x, crtc->y, output->modes[bestMode],
  398. crtc->rotation, &output_id, 1);
  399. if (s == Success)
  400. UseXRandR = true;
  401. XRRFreeCrtcInfo(crtc);
  402. XRRFreeOutputInfo(output);
  403. XRRFreeScreenResources(res);
  404. break;
  405. }
  406. if (UseXRandR == false)
  407. {
  408. os::Printer::log("Could not get video output. Try to run in windowed mode.", ELL_WARNING);
  409. CreationParams.Fullscreen = false;
  410. }
  411. #endif
  412. return CreationParams.Fullscreen;
  413. }
  414. #if defined(_IRR_COMPILE_WITH_X11_)
  415. void IrrPrintXGrabError(int grabResult, const c8 * grabCommand )
  416. {
  417. if ( grabResult == GrabSuccess )
  418. {
  419. // os::Printer::log(grabCommand, ": GrabSuccess", ELL_INFORMATION);
  420. return;
  421. }
  422. switch ( grabResult )
  423. {
  424. case AlreadyGrabbed:
  425. os::Printer::log(grabCommand, ": AlreadyGrabbed", ELL_WARNING);
  426. break;
  427. case GrabNotViewable:
  428. os::Printer::log(grabCommand, ": GrabNotViewable", ELL_WARNING);
  429. break;
  430. case GrabFrozen:
  431. os::Printer::log(grabCommand, ": GrabFrozen", ELL_WARNING);
  432. break;
  433. case GrabInvalidTime:
  434. os::Printer::log(grabCommand, ": GrabInvalidTime", ELL_WARNING);
  435. break;
  436. default:
  437. os::Printer::log(grabCommand, ": grab failed with unknown problem", ELL_WARNING);
  438. break;
  439. }
  440. }
  441. #endif
  442. #ifdef _IRR_COMPILE_WITH_OPENGL_
  443. static GLXContext getMeAGLContext(Display *display, GLXFBConfig glxFBConfig, bool force_legacy_context)
  444. {
  445. GLXContext Context;
  446. irr::video::useCoreContext = true;
  447. int core43ctxdebug[] =
  448. {
  449. GLX_CONTEXT_MAJOR_VERSION_ARB, 4,
  450. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  451. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  452. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
  453. None
  454. };
  455. int core43ctx[] =
  456. {
  457. GLX_CONTEXT_MAJOR_VERSION_ARB, 4,
  458. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  459. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  460. None
  461. };
  462. int core33ctxdebug[] =
  463. {
  464. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  465. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  466. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  467. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
  468. None
  469. };
  470. int core33ctx[] =
  471. {
  472. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  473. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  474. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  475. None
  476. };
  477. int core31ctxdebug[] =
  478. {
  479. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  480. GLX_CONTEXT_MINOR_VERSION_ARB, 1,
  481. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  482. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
  483. None
  484. };
  485. int core31ctx[] =
  486. {
  487. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  488. GLX_CONTEXT_MINOR_VERSION_ARB, 1,
  489. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  490. None
  491. };
  492. PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = 0;
  493. glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)
  494. glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" );
  495. if(!force_legacy_context)
  496. {
  497. // create core 4.3 context
  498. os::Printer::log("Creating OpenGL 4.3 context...", ELL_INFORMATION);
  499. Context = glXCreateContextAttribsARB(display, glxFBConfig, 0, True, GLContextDebugBit ? core43ctxdebug : core43ctx);
  500. if (!XErrorSignaled)
  501. return Context;
  502. XErrorSignaled = false;
  503. // create core 3.3 context
  504. os::Printer::log("Creating OpenGL 3.3 context...", ELL_INFORMATION);
  505. Context = glXCreateContextAttribsARB(display, glxFBConfig, 0, True, GLContextDebugBit ? core33ctxdebug : core33ctx);
  506. if (!XErrorSignaled)
  507. return Context;
  508. XErrorSignaled = false;
  509. // create core 3.1 context (for older mesa)
  510. os::Printer::log("Creating OpenGL 3.1 context...", ELL_INFORMATION);
  511. Context = glXCreateContextAttribsARB(display, glxFBConfig, 0, True, GLContextDebugBit ? core31ctxdebug : core31ctx);
  512. if (!XErrorSignaled)
  513. return Context;
  514. } // if(force_legacy_context)
  515. XErrorSignaled = false;
  516. irr::video::useCoreContext = false;
  517. // fall back to legacy context
  518. os::Printer::log("Creating legacy OpenGL 2.1 context...", ELL_INFORMATION);
  519. Context = glXCreateNewContext(display, glxFBConfig, GLX_RGBA_TYPE, NULL, True);
  520. return Context;
  521. }
  522. #endif
  523. bool CIrrDeviceLinux::createWindow()
  524. {
  525. #ifdef _IRR_COMPILE_WITH_X11_
  526. os::Printer::log("Creating X window...", ELL_INFORMATION);
  527. XSetErrorHandler(IrrPrintXError);
  528. display = XOpenDisplay(0);
  529. if (!display)
  530. {
  531. os::Printer::log("Error: Need running XServer to start Irrlicht Engine.", ELL_ERROR);
  532. if (XDisplayName(0)[0])
  533. os::Printer::log("Could not open display", XDisplayName(0), ELL_ERROR);
  534. else
  535. os::Printer::log("Could not open display, set DISPLAY variable", ELL_ERROR);
  536. return false;
  537. }
  538. screennr = DefaultScreen(display);
  539. changeResolution();
  540. #ifdef _IRR_COMPILE_WITH_OPENGL_
  541. GLXFBConfig glxFBConfig = NULL;
  542. int major, minor;
  543. bool isAvailableGLX=false;
  544. if (CreationParams.DriverType==video::EDT_OPENGL)
  545. {
  546. isAvailableGLX=glXQueryExtension(display,&major,&minor);
  547. if (isAvailableGLX && glXQueryVersion(display, &major, &minor))
  548. {
  549. #ifdef GLX_VERSION_1_3
  550. typedef GLXFBConfig * ( * PFNGLXCHOOSEFBCONFIGPROC) (Display *dpy, int screen, const int *attrib_list, int *nelements);
  551. #ifdef _IRR_OPENGL_USE_EXTPOINTER_
  552. PFNGLXCHOOSEFBCONFIGPROC glxChooseFBConfig = (PFNGLXCHOOSEFBCONFIGPROC)glXGetProcAddress(reinterpret_cast<const GLubyte*>("glXChooseFBConfig"));
  553. #else
  554. PFNGLXCHOOSEFBCONFIGPROC glxChooseFBConfig=glXChooseFBConfig;
  555. #endif
  556. if (major==1 && minor>2 && glxChooseFBConfig)
  557. {
  558. // attribute array for the draw buffer
  559. int visualAttrBuffer[] =
  560. {
  561. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  562. GLX_RED_SIZE, 4,
  563. GLX_GREEN_SIZE, 4,
  564. GLX_BLUE_SIZE, 4,
  565. GLX_ALPHA_SIZE, CreationParams.WithAlphaChannel?1:0,
  566. GLX_DEPTH_SIZE, CreationParams.ZBufferBits, //10,11
  567. GLX_DOUBLEBUFFER, CreationParams.Doublebuffer?True:False,
  568. GLX_STENCIL_SIZE, CreationParams.Stencilbuffer?1:0,
  569. #if defined(GLX_VERSION_1_4) && defined(GLX_SAMPLE_BUFFERS) // we need to check the extension string!
  570. GLX_SAMPLE_BUFFERS, 1,
  571. GLX_SAMPLES, CreationParams.AntiAlias, // 18,19
  572. #elif defined(GLX_ARB_multisample)
  573. GLX_SAMPLE_BUFFERS_ARB, 1,
  574. GLX_SAMPLES_ARB, CreationParams.AntiAlias, // 18,19
  575. #elif defined(GLX_SGIS_multisample)
  576. GLX_SAMPLE_BUFFERS_SGIS, 1,
  577. GLX_SAMPLES_SGIS, CreationParams.AntiAlias, // 18,19
  578. #endif
  579. #ifdef GLX_ARB_framebuffer_sRGB
  580. GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, CreationParams.HandleSRGB,
  581. #elif defined(GLX_EXT_framebuffer_sRGB)
  582. GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, CreationParams.HandleSRGB,
  583. #endif
  584. GLX_STEREO, CreationParams.Stereobuffer?True:False,
  585. None
  586. };
  587. GLXFBConfig *configList=0;
  588. int nitems=0;
  589. if (CreationParams.AntiAlias<2)
  590. {
  591. visualAttrBuffer[17] = 0;
  592. visualAttrBuffer[19] = 0;
  593. }
  594. // first round with unchanged values
  595. {
  596. configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
  597. if (!configList && CreationParams.AntiAlias)
  598. {
  599. while (!configList && (visualAttrBuffer[19]>1))
  600. {
  601. visualAttrBuffer[19] -= 1;
  602. configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
  603. }
  604. if (!configList)
  605. {
  606. visualAttrBuffer[17] = 0;
  607. visualAttrBuffer[19] = 0;
  608. configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
  609. if (configList)
  610. {
  611. os::Printer::log("No FSAA available.", ELL_WARNING);
  612. CreationParams.AntiAlias=0;
  613. }
  614. else
  615. {
  616. //reenable multisampling
  617. visualAttrBuffer[17] = 1;
  618. visualAttrBuffer[19] = CreationParams.AntiAlias;
  619. }
  620. }
  621. }
  622. }
  623. // Next try with flipped stencil buffer value
  624. // If the first round was with stencil flag it's now without
  625. // Other way round also makes sense because some configs
  626. // only have depth buffer combined with stencil buffer
  627. if (!configList)
  628. {
  629. if (CreationParams.Stencilbuffer)
  630. os::Printer::log("No stencilbuffer available, disabling stencil shadows.", ELL_WARNING);
  631. CreationParams.Stencilbuffer = !CreationParams.Stencilbuffer;
  632. visualAttrBuffer[15]=CreationParams.Stencilbuffer?1:0;
  633. configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
  634. if (!configList && CreationParams.AntiAlias)
  635. {
  636. while (!configList && (visualAttrBuffer[19]>1))
  637. {
  638. visualAttrBuffer[19] -= 1;
  639. configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
  640. }
  641. if (!configList)
  642. {
  643. visualAttrBuffer[17] = 0;
  644. visualAttrBuffer[19] = 0;
  645. configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
  646. if (configList)
  647. {
  648. os::Printer::log("No FSAA available.", ELL_WARNING);
  649. CreationParams.AntiAlias=0;
  650. }
  651. else
  652. {
  653. //reenable multisampling
  654. visualAttrBuffer[17] = 1;
  655. visualAttrBuffer[19] = CreationParams.AntiAlias;
  656. }
  657. }
  658. }
  659. }
  660. // Next try without double buffer
  661. if (!configList && CreationParams.Doublebuffer)
  662. {
  663. os::Printer::log("No doublebuffering available.", ELL_WARNING);
  664. CreationParams.Doublebuffer=false;
  665. visualAttrBuffer[13] = GLX_DONT_CARE;
  666. CreationParams.Stencilbuffer = false;
  667. visualAttrBuffer[15]=0;
  668. configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
  669. if (!configList && CreationParams.AntiAlias)
  670. {
  671. while (!configList && (visualAttrBuffer[19]>1))
  672. {
  673. visualAttrBuffer[19] -= 1;
  674. configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
  675. }
  676. if (!configList)
  677. {
  678. visualAttrBuffer[17] = 0;
  679. visualAttrBuffer[19] = 0;
  680. configList=glxChooseFBConfig(display, screennr, visualAttrBuffer,&nitems);
  681. if (configList)
  682. {
  683. os::Printer::log("No FSAA available.", ELL_WARNING);
  684. CreationParams.AntiAlias=0;
  685. }
  686. else
  687. {
  688. //reenable multisampling
  689. visualAttrBuffer[17] = 1;
  690. visualAttrBuffer[19] = CreationParams.AntiAlias;
  691. }
  692. }
  693. }
  694. }
  695. if (configList)
  696. {
  697. glxFBConfig=configList[0];
  698. XFree(configList);
  699. UseGLXWindow=true;
  700. #ifdef _IRR_OPENGL_USE_EXTPOINTER_
  701. typedef XVisualInfo * ( * PFNGLXGETVISUALFROMFBCONFIGPROC) (Display *dpy, GLXFBConfig config);
  702. PFNGLXGETVISUALFROMFBCONFIGPROC glxGetVisualFromFBConfig= (PFNGLXGETVISUALFROMFBCONFIGPROC)glXGetProcAddress(reinterpret_cast<const GLubyte*>("glXGetVisualFromFBConfig"));
  703. if (glxGetVisualFromFBConfig)
  704. visual = glxGetVisualFromFBConfig(display,glxFBConfig);
  705. #else
  706. visual = glXGetVisualFromFBConfig(display,glxFBConfig);
  707. #endif
  708. }
  709. }
  710. else
  711. #endif
  712. {
  713. // attribute array for the draw buffer
  714. int visualAttrBuffer[] =
  715. {
  716. GLX_RGBA, GLX_USE_GL,
  717. GLX_RED_SIZE, 4,
  718. GLX_GREEN_SIZE, 4,
  719. GLX_BLUE_SIZE, 4,
  720. GLX_ALPHA_SIZE, CreationParams.WithAlphaChannel?1:0,
  721. GLX_DEPTH_SIZE, CreationParams.ZBufferBits,
  722. GLX_STENCIL_SIZE, CreationParams.Stencilbuffer?1:0, // 12,13
  723. // The following attributes have no flags, but are
  724. // either present or not. As a no-op we use
  725. // GLX_USE_GL, which is silently ignored by glXChooseVisual
  726. CreationParams.Doublebuffer?GLX_DOUBLEBUFFER:GLX_USE_GL, // 14
  727. CreationParams.Stereobuffer?GLX_STEREO:GLX_USE_GL, // 15
  728. #ifdef GLX_ARB_framebuffer_sRGB
  729. CreationParams.HandleSRGB?GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB:GLX_USE_GL,
  730. #elif defined(GLX_EXT_framebuffer_sRGB)
  731. CreationParams.HandleSRGB?GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT:GLX_USE_GL,
  732. #endif
  733. None
  734. };
  735. visual=glXChooseVisual(display, screennr, visualAttrBuffer);
  736. if (!visual)
  737. {
  738. if (CreationParams.Stencilbuffer)
  739. os::Printer::log("No stencilbuffer available, disabling.", ELL_WARNING);
  740. CreationParams.Stencilbuffer = !CreationParams.Stencilbuffer;
  741. visualAttrBuffer[13]=CreationParams.Stencilbuffer?1:0;
  742. visual=glXChooseVisual(display, screennr, visualAttrBuffer);
  743. if (!visual && CreationParams.Doublebuffer)
  744. {
  745. os::Printer::log("No doublebuffering available.", ELL_WARNING);
  746. CreationParams.Doublebuffer=false;
  747. visualAttrBuffer[14] = GLX_USE_GL;
  748. visual=glXChooseVisual(display, screennr, visualAttrBuffer);
  749. }
  750. }
  751. }
  752. }
  753. else
  754. os::Printer::log("No GLX support available. OpenGL driver will not work.", ELL_WARNING);
  755. }
  756. // don't use the XVisual with OpenGL, because it ignores all requested
  757. // properties of the CreationParams
  758. else if (!visual)
  759. #endif // _IRR_COMPILE_WITH_OPENGL_
  760. // create visual with standard X methods
  761. {
  762. os::Printer::log("Using plain X visual");
  763. XVisualInfo visTempl; //Template to hold requested values
  764. int visNumber; // Return value of available visuals
  765. visTempl.screen = screennr;
  766. // ARGB visuals should be avoided for usual applications
  767. visTempl.depth = CreationParams.WithAlphaChannel?32:24;
  768. while ((!visual) && (visTempl.depth>=16))
  769. {
  770. visual = XGetVisualInfo(display, VisualScreenMask|VisualDepthMask,
  771. &visTempl, &visNumber);
  772. visTempl.depth -= 8;
  773. }
  774. }
  775. if (!visual)
  776. {
  777. os::Printer::log("Fatal error, could not get visual.", ELL_ERROR);
  778. XCloseDisplay(display);
  779. display=0;
  780. return false;
  781. }
  782. #ifdef _DEBUG
  783. else
  784. os::Printer::log("Visual chosen: ", core::stringc(static_cast<u32>(visual->visualid)).c_str(), ELL_DEBUG);
  785. #endif
  786. // create color map
  787. Colormap colormap;
  788. colormap = XCreateColormap(display,
  789. RootWindow(display, visual->screen),
  790. visual->visual, AllocNone);
  791. attributes.colormap = colormap;
  792. attributes.border_pixel = 0;
  793. attributes.event_mask = StructureNotifyMask | FocusChangeMask | ExposureMask;
  794. if (!CreationParams.IgnoreInput)
  795. attributes.event_mask |= PointerMotionMask |
  796. ButtonPressMask | KeyPressMask |
  797. ButtonReleaseMask | KeyReleaseMask;
  798. if (!CreationParams.WindowId)
  799. {
  800. Atom *list;
  801. Atom type;
  802. int form;
  803. unsigned long remain, len;
  804. Atom WMCheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", false);
  805. Status s = XGetWindowProperty(display, DefaultRootWindow(display),
  806. WMCheck, 0L, 1L, False, XA_WINDOW,
  807. &type, &form, &len, &remain,
  808. (unsigned char **)&list);
  809. XFree(list);
  810. bool netWM = (s == Success) && len;
  811. attributes.override_redirect = !netWM && CreationParams.Fullscreen;
  812. // create new Window
  813. window = XCreateWindow(display,
  814. RootWindow(display, visual->screen),
  815. 0, 0, Width, Height, 0, visual->depth,
  816. InputOutput, visual->visual,
  817. CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect,
  818. &attributes);
  819. XMapRaised(display, window);
  820. CreationParams.WindowId = (void*)window;
  821. Atom wmDelete;
  822. wmDelete = XInternAtom(display, wmDeleteWindow, True);
  823. XSetWMProtocols(display, window, &wmDelete, 1);
  824. if (CreationParams.Fullscreen)
  825. {
  826. if (netWM)
  827. {
  828. // Some window managers don't respect values from XCreateWindow and
  829. // place window in random position. This may cause that fullscreen
  830. // window is showed in wrong screen. It doesn't matter for vidmode
  831. // which displays cloned image in all devices.
  832. #ifdef _IRR_LINUX_X11_RANDR_
  833. XMoveResizeWindow(display, window, crtc_x, crtc_y, Width, Height);
  834. XRaiseWindow(display, window);
  835. XFlush(display);
  836. #endif
  837. // Set the fullscreen mode via the window manager. This allows alt-tabing, volume hot keys & others.
  838. // Get the needed atom from there freedesktop names
  839. Atom WMStateAtom = XInternAtom(display, "_NET_WM_STATE", true);
  840. Atom WMFullscreenAtom = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", true);
  841. XEvent xev = {0}; // The event should be filled with zeros before setting its attributes
  842. xev.type = ClientMessage;
  843. xev.xclient.window = window;
  844. xev.xclient.message_type = WMStateAtom;
  845. xev.xclient.format = 32;
  846. xev.xclient.data.l[0] = 1;
  847. xev.xclient.data.l[1] = WMFullscreenAtom;
  848. XSendEvent(display, RootWindow(display, visual->screen), false,
  849. SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  850. XFlush(display);
  851. // Wait until window state is already changed to fullscreen
  852. bool fullscreen = false;
  853. for (int i = 0; i < 500; i++)
  854. {
  855. Atom type;
  856. int format;
  857. unsigned long numItems, bytesAfter;
  858. unsigned char* data = NULL;
  859. int s = XGetWindowProperty(display, window, WMStateAtom,
  860. 0l, 1024, False, XA_ATOM, &type,
  861. &format, &numItems, &bytesAfter,
  862. &data);
  863. if (s == Success)
  864. {
  865. Atom* atoms = (Atom*)data;
  866. for (unsigned int i = 0; i < numItems; ++i)
  867. {
  868. if (atoms[i] == WMFullscreenAtom)
  869. {
  870. fullscreen = true;
  871. break;
  872. }
  873. }
  874. }
  875. XFree(data);
  876. if (fullscreen == true)
  877. break;
  878. usleep(1000);
  879. }
  880. if (!fullscreen)
  881. {
  882. os::Printer::log("Warning! Got timeout while checking fullscreen sate", ELL_WARNING);
  883. }
  884. }
  885. else
  886. {
  887. XSetInputFocus(display, window, RevertToParent, CurrentTime);
  888. int grabKb = XGrabKeyboard(display, window, True, GrabModeAsync,
  889. GrabModeAsync, CurrentTime);
  890. IrrPrintXGrabError(grabKb, "XGrabKeyboard");
  891. int grabPointer = XGrabPointer(display, window, True, ButtonPressMask,
  892. GrabModeAsync, GrabModeAsync, window, None, CurrentTime);
  893. IrrPrintXGrabError(grabPointer, "XGrabPointer");
  894. XWarpPointer(display, None, window, 0, 0, 0, 0, 0, 0);
  895. }
  896. }
  897. }
  898. else
  899. {
  900. // attach external window
  901. window = (Window)CreationParams.WindowId;
  902. if (!CreationParams.IgnoreInput)
  903. {
  904. XCreateWindow(display,
  905. window,
  906. 0, 0, Width, Height, 0, visual->depth,
  907. InputOutput, visual->visual,
  908. CWBorderPixel | CWColormap | CWEventMask,
  909. &attributes);
  910. }
  911. XWindowAttributes wa;
  912. XGetWindowAttributes(display, window, &wa);
  913. CreationParams.WindowSize.Width = wa.width;
  914. CreationParams.WindowSize.Height = wa.height;
  915. CreationParams.Fullscreen = false;
  916. ExternalWindow = true;
  917. }
  918. WindowMinimized=false;
  919. // Currently broken in X, see Bug ID 2795321
  920. // XkbSetDetectableAutoRepeat(display, True, &AutorepeatSupport);
  921. #ifdef _IRR_COMPILE_WITH_OPENGL_
  922. // connect glx context to window
  923. Context=0;
  924. if (isAvailableGLX && CreationParams.DriverType==video::EDT_OPENGL)
  925. {
  926. if (UseGLXWindow)
  927. {
  928. glxWin=glXCreateWindow(display,glxFBConfig,window,NULL);
  929. if (glxWin)
  930. {
  931. getLogger()->setLogLevel(ELL_NONE);
  932. Context = getMeAGLContext(display, glxFBConfig, CreationParams.ForceLegacyDevice);
  933. getLogger()->setLogLevel(CreationParams.LoggingLevel);
  934. if (Context)
  935. {
  936. if (!glXMakeContextCurrent(display, glxWin, glxWin, Context))
  937. {
  938. os::Printer::log("Could not make context current.", ELL_WARNING);
  939. glXDestroyContext(display, Context);
  940. }
  941. }
  942. else
  943. {
  944. os::Printer::log("Could not create GLX rendering context.", ELL_WARNING);
  945. }
  946. }
  947. else
  948. {
  949. os::Printer::log("Could not create GLX window.", ELL_WARNING);
  950. }
  951. }
  952. else
  953. {
  954. Context = glXCreateContext(display, visual, NULL, True);
  955. if (Context)
  956. {
  957. if (!glXMakeCurrent(display, window, Context))
  958. {
  959. os::Printer::log("Could not make context current.", ELL_WARNING);
  960. glXDestroyContext(display, Context);
  961. }
  962. }
  963. else
  964. {
  965. os::Printer::log("Could not create GLX rendering context.", ELL_WARNING);
  966. }
  967. }
  968. }
  969. #endif // _IRR_COMPILE_WITH_OPENGL_
  970. Window tmp;
  971. u32 borderWidth;
  972. int x,y;
  973. unsigned int bits;
  974. XGetGeometry(display, window, &tmp, &x, &y, &Width, &Height, &borderWidth, &bits);
  975. CreationParams.Bits = bits;
  976. CreationParams.WindowSize.Width = Width;
  977. CreationParams.WindowSize.Height = Height;
  978. StdHints = XAllocSizeHints();
  979. long num;
  980. XGetWMNormalHints(display, window, StdHints, &num);
  981. // create an XImage for the software renderer
  982. //(thx to Nadav for some clues on how to do that!)
  983. if (CreationParams.DriverType == video::EDT_SOFTWARE || CreationParams.DriverType == video::EDT_BURNINGSVIDEO)
  984. {
  985. SoftwareImage = XCreateImage(display,
  986. visual->visual, visual->depth,
  987. ZPixmap, 0, 0, Width, Height,
  988. BitmapPad(display), 0);
  989. // use malloc because X will free it later on
  990. if (SoftwareImage)
  991. SoftwareImage->data = (char*) malloc(SoftwareImage->bytes_per_line * SoftwareImage->height * sizeof(char));
  992. }
  993. initXAtoms();
  994. #endif // #ifdef _IRR_COMPILE_WITH_X11_
  995. return true;
  996. }
  997. //! create the driver
  998. void CIrrDeviceLinux::createDriver()
  999. {
  1000. switch(CreationParams.DriverType)
  1001. {
  1002. #ifdef _IRR_COMPILE_WITH_X11_
  1003. case video::EDT_SOFTWARE:
  1004. #ifdef _IRR_COMPILE_WITH_SOFTWARE_
  1005. VideoDriver = video::createSoftwareDriver(CreationParams.WindowSize, CreationParams.Fullscreen, FileSystem, this);
  1006. #else
  1007. os::Printer::log("No Software driver support compiled in.", ELL_ERROR);
  1008. #endif
  1009. break;
  1010. case video::EDT_BURNINGSVIDEO:
  1011. #ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
  1012. VideoDriver = video::createBurningVideoDriver(CreationParams, FileSystem, this);
  1013. #else
  1014. os::Printer::log("Burning's video driver was not compiled in.", ELL_ERROR);
  1015. #endif
  1016. break;
  1017. case video::EDT_OPENGL:
  1018. #ifdef _IRR_COMPILE_WITH_OPENGL_
  1019. if (Context)
  1020. VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, this);
  1021. #else
  1022. os::Printer::log("No OpenGL support compiled in.", ELL_ERROR);
  1023. #endif
  1024. break;
  1025. case video::EDT_OGLES2:
  1026. {
  1027. #ifdef _IRR_COMPILE_WITH_OGLES2_
  1028. video::SExposedVideoData data;
  1029. data.OpenGLLinux.X11Window = window;
  1030. data.OpenGLLinux.X11Display = display;
  1031. VideoDriver = video::createOGLES2Driver(CreationParams, data, FileSystem);
  1032. #else
  1033. os::Printer::log("No OpenGL ES 2.0 support compiled in.", ELL_ERROR);
  1034. #endif
  1035. break;
  1036. }
  1037. case video::EDT_DIRECT3D8:
  1038. case video::EDT_DIRECT3D9:
  1039. os::Printer::log("This driver is not available in Linux. Try OpenGL or Software renderer.",
  1040. ELL_ERROR);
  1041. break;
  1042. case video::EDT_NULL:
  1043. VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
  1044. break;
  1045. default:
  1046. os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
  1047. break;
  1048. #else
  1049. case video::EDT_NULL:
  1050. VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
  1051. break;
  1052. default:
  1053. os::Printer::log("No X11 support compiled in. Only Null driver available.", ELL_ERROR);
  1054. break;
  1055. #endif
  1056. }
  1057. }
  1058. #ifdef _IRR_COMPILE_WITH_X11_
  1059. bool CIrrDeviceLinux::createInputContext()
  1060. {
  1061. if (!display)
  1062. return false;
  1063. // One one side it would be nicer to let users do that - on the other hand
  1064. // not setting the environment locale will not work when using i18n X11 functions.
  1065. // So users would have to call it always or their input is broken badly.
  1066. // We can restore immediately - so won't mess with anything in users apps.
  1067. core::stringc oldLocale(setlocale(LC_CTYPE, NULL));
  1068. setlocale(LC_CTYPE, ""); // use environment locale
  1069. if ( !XSupportsLocale() )
  1070. {
  1071. os::Printer::log("Locale not supported. Falling back to non-i18n input.", ELL_WARNING);
  1072. setlocale(LC_CTYPE, oldLocale.c_str());
  1073. return false;
  1074. }
  1075. char* p = XSetLocaleModifiers("");
  1076. if (p == NULL)
  1077. {
  1078. os::Printer::log("Could not set locale modifiers. Falling back to non-i18n input.", ELL_WARNING);
  1079. setlocale(LC_CTYPE, oldLocale.c_str());
  1080. return false;
  1081. }
  1082. XInputMethod = XOpenIM(display, NULL, NULL, NULL);
  1083. if ( !XInputMethod )
  1084. {
  1085. os::Printer::log("XOpenIM failed to create an input method. Falling back to non-i18n input.", ELL_WARNING);
  1086. setlocale(LC_CTYPE, oldLocale.c_str());
  1087. return false;
  1088. }
  1089. XIMStyles *im_supported_styles = NULL;
  1090. XGetIMValues(XInputMethod, XNQueryInputStyle, &im_supported_styles, (void*)NULL);
  1091. if (!im_supported_styles)
  1092. {
  1093. os::Printer::log("XGetIMValues failed to get supported styles. Falling back to non-i18n input.", ELL_WARNING);
  1094. setlocale(LC_CTYPE, oldLocale.c_str());
  1095. return false;
  1096. }
  1097. // Only OverTheSpot and Root pre-edit type are implemented for now
  1098. core::array<unsigned long> supported_style;
  1099. //supported_style.push_back(XIMPreeditPosition | XIMStatusNothing);
  1100. supported_style.push_back(XIMPreeditNothing | XIMStatusNothing);
  1101. XIMStyle best_style = 0;
  1102. bool found = false;
  1103. int supported_style_start = -1;
  1104. while (!found && supported_style_start < (int)supported_style.size())
  1105. {
  1106. supported_style_start++;
  1107. for (int i = 0; i < im_supported_styles->count_styles; i++)
  1108. {
  1109. XIMStyle cur_style = im_supported_styles->supported_styles[i];
  1110. if (cur_style == supported_style[supported_style_start])
  1111. {
  1112. best_style = cur_style;
  1113. found = true;
  1114. break;
  1115. }
  1116. }
  1117. }
  1118. XFree(im_supported_styles);
  1119. if (!found)
  1120. {
  1121. XDestroyIC(XInputContext);
  1122. XInputContext = 0;
  1123. os::Printer::log("XInputMethod has no input style we can use. Falling back to non-i18n input.", ELL_WARNING);
  1124. setlocale(LC_CTYPE, oldLocale.c_str());
  1125. return false;
  1126. }
  1127. // Only use root preedit type for now
  1128. /*if (best_style != (XIMPreeditNothing | XIMStatusNothing))
  1129. {
  1130. char **list = NULL;
  1131. int count = 0;
  1132. m_font_set = XCreateFontSet(display, "fixed", &list, &count, NULL);
  1133. if (!m_font_set)
  1134. {
  1135. os::Printer::log("XInputContext failed to create font set. Falling back to non-i18n input.", ELL_WARNING);
  1136. setlocale(LC_CTYPE, oldLocale.c_str());
  1137. return false;
  1138. }
  1139. if (count > 0)
  1140. {
  1141. XFreeStringList(list);
  1142. }
  1143. XPoint spot = {0, 0};
  1144. XVaNestedList p_list = XVaCreateNestedList(0, XNSpotLocation, &spot,
  1145. XNFontSet, m_font_set, (void*)NULL);
  1146. XInputContext = XCreateIC(XInputMethod,
  1147. XNInputStyle, best_style,
  1148. XNClientWindow, window,
  1149. XNFocusWindow, window,
  1150. XNPreeditAttributes, p_list,
  1151. (void*)NULL);
  1152. XFree(p_list);
  1153. }
  1154. else*/
  1155. {
  1156. XInputContext = XCreateIC(XInputMethod,
  1157. XNInputStyle, best_style,
  1158. XNClientWindow, window,
  1159. XNFocusWindow, window,
  1160. (void*)NULL);
  1161. }
  1162. if (!XInputContext )
  1163. {
  1164. os::Printer::log("XInputContext failed to create an input context. Falling back to non-i18n input.", ELL_WARNING);
  1165. setlocale(LC_CTYPE, oldLocale.c_str());
  1166. return false;
  1167. }
  1168. XSetICFocus(XInputContext);
  1169. setlocale(LC_CTYPE, oldLocale.c_str());
  1170. return true;
  1171. }
  1172. void CIrrDeviceLinux::destroyInputContext()
  1173. {
  1174. if ( XInputContext )
  1175. {
  1176. XUnsetICFocus(XInputContext);
  1177. XDestroyIC(XInputContext);
  1178. XInputContext = 0;
  1179. }
  1180. if ( XInputMethod )
  1181. {
  1182. XCloseIM(XInputMethod);
  1183. XInputMethod = 0;
  1184. }
  1185. if (display && m_font_set)
  1186. {
  1187. XFreeFontSet(display, m_font_set);
  1188. m_font_set = 0;
  1189. }
  1190. }
  1191. void CIrrDeviceLinux::setIMELocation(const irr::core::position2di& pos)
  1192. {
  1193. if (!XInputContext || !m_ime_enabled) return;
  1194. m_ime_position.x = pos.X;
  1195. m_ime_position.y = pos.Y;
  1196. updateIMELocation();
  1197. }
  1198. void CIrrDeviceLinux::updateIMELocation()
  1199. {
  1200. if (!XInputContext || !m_ime_enabled) return;
  1201. XVaNestedList list;
  1202. list = XVaCreateNestedList(0, XNSpotLocation, &m_ime_position, (void*)NULL);
  1203. XSetICValues(XInputContext, XNPreeditAttributes, list, (void*)NULL);
  1204. XFree(list);
  1205. }
  1206. void CIrrDeviceLinux::setIMEEnable(bool enable)
  1207. {
  1208. if (!XInputContext) return;
  1209. m_ime_enabled = enable;
  1210. }
  1211. int CIrrDeviceLinux::getNumlockMask(Display* display)
  1212. {
  1213. int mask_table[8] = {ShiftMask, LockMask, ControlMask, Mod1Mask,
  1214. Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask};
  1215. if (!display)
  1216. return 0;
  1217. KeyCode numlock_keycode = XKeysymToKeycode(display, XK_Num_Lock);
  1218. if (numlock_keycode == NoSymbol)
  1219. return 0;
  1220. XModifierKeymap* map = XGetModifierMapping(display);
  1221. if (!map)
  1222. return 0;
  1223. int mask = 0;
  1224. for (int i = 0; i < 8 * map->max_keypermod; i++)
  1225. {
  1226. if (map->modifiermap[i] != numlock_keycode)
  1227. continue;
  1228. mask = mask_table[i/map->max_keypermod];
  1229. break;
  1230. }
  1231. XFreeModifiermap(map);
  1232. return mask;
  1233. }
  1234. EKEY_CODE CIrrDeviceLinux::getKeyCode(XEvent &event)
  1235. {
  1236. EKEY_CODE keyCode = (EKEY_CODE)0;
  1237. SKeyMap mp;
  1238. // First check for numpad keys
  1239. bool is_numpad_key = false;
  1240. if (event.xkey.state & numlock_mask)
  1241. {
  1242. mp.X11Key = XkbKeycodeToKeysym(display, event.xkey.keycode, 0, 1);
  1243. if (mp.X11Key >=XK_KP_0 && mp.X11Key <= XK_KP_9)
  1244. is_numpad_key = true;
  1245. }
  1246. // If it's not numpad key, then get keycode in typical way
  1247. if (!is_numpad_key)
  1248. {
  1249. mp.X11Key = XkbKeycodeToKeysym(display, event.xkey.keycode, 0, 0);
  1250. }
  1251. const s32 idx = KeyMap.binary_search(mp);
  1252. if (idx != -1)
  1253. {
  1254. keyCode = (EKEY_CODE)KeyMap[idx].Win32Key;
  1255. }
  1256. if (keyCode == 0)
  1257. {
  1258. // Any value is better than none, that allows at least using the keys.
  1259. // Worst case is that some keys will be identical, still better than _all_
  1260. // unknown keys being identical.
  1261. if ( !mp.X11Key )
  1262. {
  1263. keyCode = (EKEY_CODE)event.xkey.keycode;
  1264. os::Printer::log("No such X11Key, using event keycode", core::stringc(event.xkey.keycode).c_str(), ELL_INFORMATION);
  1265. }
  1266. else if (idx == -1)
  1267. {
  1268. keyCode = (EKEY_CODE)mp.X11Key;
  1269. os::Printer::log("EKEY_CODE not found, using orig. X11 keycode", core::stringc(mp.X11Key).c_str(), ELL_INFORMATION);
  1270. }
  1271. else
  1272. {
  1273. keyCode = (EKEY_CODE)mp.X11Key;
  1274. os::Printer::log("EKEY_CODE is 0, using orig. X11 keycode", core::stringc(mp.X11Key).c_str(), ELL_INFORMATION);
  1275. }
  1276. }
  1277. return keyCode;
  1278. }
  1279. #endif
  1280. //! runs the device. Returns false if device wants to be deleted
  1281. bool CIrrDeviceLinux::run()
  1282. {
  1283. os::Timer::tick();
  1284. #ifdef _IRR_COMPILE_WITH_X11_
  1285. if ( CursorControl )
  1286. static_cast<CCursorControl*>(CursorControl)->update();
  1287. if ((CreationParams.DriverType != video::EDT_NULL) && display)
  1288. {
  1289. SEvent irrevent;
  1290. irrevent.MouseInput.ButtonStates = 0xffffffff;
  1291. while (XPending(display) > 0 && !Close)
  1292. {
  1293. if (!m_ime_char_holder.empty())
  1294. {
  1295. irrevent.KeyInput.Char = m_ime_char_holder[0];
  1296. irrevent.EventType = irr::EET_KEY_INPUT_EVENT;
  1297. irrevent.KeyInput.PressedDown = true;
  1298. irrevent.KeyInput.Control = false;
  1299. irrevent.KeyInput.Shift = false;
  1300. irrevent.KeyInput.Key = (irr::EKEY_CODE)0;
  1301. postEventFromUser(irrevent);
  1302. m_ime_char_holder.erase(0);
  1303. continue;
  1304. }
  1305. XEvent event;
  1306. XNextEvent(display, &event);
  1307. if (m_ime_enabled && XFilterEvent(&event, None))
  1308. {
  1309. updateIMELocation();
  1310. continue;
  1311. }
  1312. switch (event.type)
  1313. {
  1314. case ConfigureNotify:
  1315. updateIMELocation();
  1316. // check for changed window size
  1317. if ((event.xconfigure.width != (int) Width) ||
  1318. (event.xconfigure.height != (int) Height))
  1319. {
  1320. Width = event.xconfigure.width;
  1321. Height = event.xconfigure.height;
  1322. // resize image data
  1323. if (SoftwareImage)
  1324. {
  1325. XDestroyImage(SoftwareImage);
  1326. SoftwareImage = XCreateImage(display,
  1327. visual->visual, visual->depth,
  1328. ZPixmap, 0, 0, Width, Height,
  1329. BitmapPad(display), 0);
  1330. // use malloc because X will free it later on
  1331. if (SoftwareImage)
  1332. SoftwareImage->data = (char*) malloc(SoftwareImage->bytes_per_line * SoftwareImage->height * sizeof(char));
  1333. }
  1334. if (VideoDriver)
  1335. VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));
  1336. }
  1337. break;
  1338. case MapNotify:
  1339. WindowMinimized=false;
  1340. break;
  1341. case UnmapNotify:
  1342. WindowMinimized=true;
  1343. break;
  1344. case FocusIn:
  1345. WindowHasFocus=true;
  1346. break;
  1347. case FocusOut:
  1348. WindowHasFocus=false;
  1349. break;
  1350. case MotionNotify:
  1351. irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
  1352. irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
  1353. irrevent.MouseInput.X = event.xbutton.x;
  1354. irrevent.MouseInput.Y = event.xbutton.y;
  1355. irrevent.MouseInput.Control = (event.xkey.state & ControlMask) != 0;
  1356. irrevent.MouseInput.Shift = (event.xkey.state & ShiftMask) != 0;
  1357. // mouse button states
  1358. irrevent.MouseInput.ButtonStates = (event.xbutton.state & Button1Mask) ? irr::EMBSM_LEFT : 0;
  1359. irrevent.MouseInput.ButtonStates |= (event.xbutton.state & Button3Mask) ? irr::EMBSM_RIGHT : 0;
  1360. irrevent.MouseInput.ButtonStates |= (event.xbutton.state & Button2Mask) ? irr::EMBSM_MIDDLE : 0;
  1361. postEventFromUser(irrevent);
  1362. break;
  1363. case ButtonPress:
  1364. case ButtonRelease:
  1365. irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
  1366. irrevent.MouseInput.X = event.xbutton.x;
  1367. irrevent.MouseInput.Y = event.xbutton.y;
  1368. irrevent.MouseInput.Control = (event.xkey.state & ControlMask) != 0;
  1369. irrevent.MouseInput.Shift = (event.xkey.state & ShiftMask) != 0;
  1370. // mouse button states
  1371. // This sets the state which the buttons had _prior_ to the event.
  1372. // So unlike on Windows the button which just got changed has still the old state here.
  1373. // We handle that below by flipping the corresponding bit later.
  1374. irrevent.MouseInput.ButtonStates = (event.xbutton.state & Button1Mask) ? irr::EMBSM_LEFT : 0;
  1375. irrevent.MouseInput.ButtonStates |= (event.xbutton.state & Button3Mask) ? irr::EMBSM_RIGHT : 0;
  1376. irrevent.MouseInput.ButtonStates |= (event.xbutton.state & Button2Mask) ? irr::EMBSM_MIDDLE : 0;
  1377. irrevent.MouseInput.Event = irr::EMIE_COUNT;
  1378. switch(event.xbutton.button)
  1379. {
  1380. case Button1:
  1381. irrevent.MouseInput.Event =
  1382. (event.type == ButtonPress) ? irr::EMIE_LMOUSE_PRESSED_DOWN : irr::EMIE_LMOUSE_LEFT_UP;
  1383. irrevent.MouseInput.ButtonStates ^= irr::EMBSM_LEFT;
  1384. break;
  1385. case Button3:
  1386. irrevent.MouseInput.Event =
  1387. (event.type == ButtonPress) ? irr::EMIE_RMOUSE_PRESSED_DOWN : irr::EMIE_RMOUSE_LEFT_UP;
  1388. irrevent.MouseInput.ButtonStates ^= irr::EMBSM_RIGHT;
  1389. break;
  1390. case Button2:
  1391. irrevent.MouseInput.Event =
  1392. (event.type == ButtonPress) ? irr::EMIE_MMOUSE_PRESSED_DOWN : irr::EMIE_MMOUSE_LEFT_UP;
  1393. irrevent.MouseInput.ButtonStates ^= irr::EMBSM_MIDDLE;
  1394. break;
  1395. case Button4:
  1396. if (event.type == ButtonPress)
  1397. {
  1398. irrevent.MouseInput.Event = EMIE_MOUSE_WHEEL;
  1399. irrevent.MouseInput.Wheel = 1.0f;
  1400. }
  1401. break;
  1402. case Button5:
  1403. if (event.type == ButtonPress)
  1404. {
  1405. irrevent.MouseInput.Event = EMIE_MOUSE_WHEEL;
  1406. irrevent.MouseInput.Wheel = -1.0f;
  1407. }
  1408. break;
  1409. }
  1410. if (irrevent.MouseInput.Event != irr::EMIE_COUNT)
  1411. {
  1412. postEventFromUser(irrevent);
  1413. if ( irrevent.MouseInput.Event >= EMIE_LMOUSE_PRESSED_DOWN && irrevent.MouseInput.Event <= EMIE_MMOUSE_PRESSED_DOWN )
  1414. {
  1415. u32 clicks = checkSuccessiveClicks(irrevent.MouseInput.X, irrevent.MouseInput.Y, irrevent.MouseInput.Event);
  1416. if ( clicks == 2 )
  1417. {
  1418. irrevent.MouseInput.Event = (EMOUSE_INPUT_EVENT)(EMIE_LMOUSE_DOUBLE_CLICK + irrevent.MouseInput.Event-EMIE_LMOUSE_PRESSED_DOWN);
  1419. postEventFromUser(irrevent);
  1420. }
  1421. else if ( clicks == 3 )
  1422. {
  1423. irrevent.MouseInput.Event = (EMOUSE_INPUT_EVENT)(EMIE_LMOUSE_TRIPLE_CLICK + irrevent.MouseInput.Event-EMIE_LMOUSE_PRESSED_DOWN);
  1424. postEventFromUser(irrevent);
  1425. }
  1426. }
  1427. }
  1428. break;
  1429. case MappingNotify:
  1430. XRefreshKeyboardMapping (&event.xmapping) ;
  1431. break;
  1432. case KeyRelease:
  1433. if (0 == AutorepeatSupport && (XPending( display ) > 0) )
  1434. {
  1435. // check for Autorepeat manually
  1436. // We'll do the same as Windows does: Only send KeyPressed
  1437. // So every KeyRelease is a real release
  1438. XEvent next_event;
  1439. XPeekEvent (event.xkey.display, &next_event);
  1440. if ((next_event.type == KeyPress) &&
  1441. (next_event.xkey.keycode == event.xkey.keycode) &&
  1442. (next_event.xkey.time - event.xkey.time) < 2) // usually same time, but on some systems a difference of 1 is possible
  1443. {
  1444. // Ignore the key release event
  1445. break;
  1446. }
  1447. }
  1448. irrevent.EventType = irr::EET_KEY_INPUT_EVENT;
  1449. irrevent.KeyInput.PressedDown = false;
  1450. irrevent.KeyInput.Char = 0; // on release that's undefined
  1451. irrevent.KeyInput.Control = (event.xkey.state & ControlMask) != 0;
  1452. irrevent.KeyInput.Shift = (event.xkey.state & ShiftMask) != 0;
  1453. irrevent.KeyInput.Key = getKeyCode(event);
  1454. postEventFromUser(irrevent);
  1455. break;
  1456. case KeyPress:
  1457. {
  1458. SKeyMap mp;
  1459. if ( XInputContext )
  1460. {
  1461. Status status;
  1462. int strLen = XwcLookupString(XInputContext, &event.xkey, m_ime_char_holder.pointer(), 16 * sizeof(wchar_t), &mp.X11Key, &status);
  1463. if ( status == XBufferOverflow )
  1464. {
  1465. os::Printer::log("XwcLookupString needs a larger buffer", ELL_INFORMATION);
  1466. }
  1467. if ( strLen > 0 && (status == XLookupChars || status == XLookupBoth) )
  1468. {
  1469. if ( strLen > 1 )
  1470. {
  1471. m_ime_char_holder.set_used(strLen > 16 ? 16 : strLen);
  1472. continue;
  1473. }
  1474. else
  1475. {
  1476. irrevent.KeyInput.Char = m_ime_char_holder[0];
  1477. }
  1478. }
  1479. else
  1480. {
  1481. irrevent.KeyInput.Char = 0;
  1482. }
  1483. }
  1484. else // Old version without InputContext. Does not support i18n, but good to have as fallback.
  1485. {
  1486. union
  1487. {
  1488. char buf[8];
  1489. wchar_t wbuf[2];
  1490. } tmp = {{0}};
  1491. XLookupString(&event.xkey, tmp.buf, sizeof(tmp.buf), &mp.X11Key, NULL);
  1492. irrevent.KeyInput.Char = tmp.wbuf[0];
  1493. }
  1494. irrevent.EventType = irr::EET_KEY_INPUT_EVENT;
  1495. irrevent.KeyInput.PressedDown = true;
  1496. irrevent.KeyInput.Control = (event.xkey.state & ControlMask) != 0;
  1497. irrevent.KeyInput.Shift = (event.xkey.state & ShiftMask) != 0;
  1498. irrevent.KeyInput.Key = getKeyCode(event);
  1499. postEventFromUser(irrevent);
  1500. }
  1501. break;
  1502. case ClientMessage:
  1503. {
  1504. char *atom = XGetAtomName(display, event.xclient.message_type);
  1505. if (*atom == *wmDeleteWindow)
  1506. {
  1507. os::Printer::log("Quit message received.", ELL_INFORMATION);
  1508. Close = true;
  1509. }
  1510. else
  1511. {
  1512. // we assume it's a user message
  1513. irrevent.EventType = irr::EET_USER_EVENT;
  1514. irrevent.UserEvent.UserData1 = (s32)event.xclient.data.l[0];
  1515. irrevent.UserEvent.UserData2 = (s32)event.xclient.data.l[1];
  1516. postEventFromUser(irrevent);
  1517. }
  1518. XFree(atom);
  1519. }
  1520. break;
  1521. case SelectionRequest:
  1522. {
  1523. XEvent respond;
  1524. XSelectionRequestEvent *req = &(event.xselectionrequest);
  1525. if ( req->target == X_ATOM_UTF8_STRING)
  1526. {
  1527. XChangeProperty (display,
  1528. req->requestor,
  1529. req->property, req->target,
  1530. 8, // format
  1531. PropModeReplace,
  1532. (unsigned char*) Clipboard.c_str(),
  1533. Clipboard.size());
  1534. respond.xselection.property = req->property;
  1535. }
  1536. else if ( req->target == X_ATOM_TARGETS )
  1537. {
  1538. long data[1];
  1539. data[0] = X_ATOM_UTF8_STRING;
  1540. XChangeProperty (display, req->requestor,
  1541. req->property, XA_ATOM,
  1542. 32, PropModeReplace,
  1543. (unsigned char *) &data,
  1544. sizeof (data));
  1545. respond.xselection.property = req->property;
  1546. }
  1547. else
  1548. {
  1549. respond.xselection.property= None;
  1550. }
  1551. respond.xselection.type= SelectionNotify;
  1552. respond.xselection.display= req->display;
  1553. respond.xselection.requestor= req->requestor;
  1554. respond.xselection.selection=req->selection;
  1555. respond.xselection.target= req->target;
  1556. respond.xselection.time = req->time;
  1557. XSendEvent (display, req->requestor,0,0,&respond);
  1558. XFlush (display);
  1559. }
  1560. break;
  1561. default:
  1562. break;
  1563. } // end switch
  1564. } // end while
  1565. }
  1566. #endif //_IRR_COMPILE_WITH_X11_
  1567. if (!Close)
  1568. pollJoysticks();
  1569. return !Close;
  1570. }
  1571. //! Pause the current process for the minimum time allowed only to allow other processes to execute
  1572. void CIrrDeviceLinux::yield()
  1573. {
  1574. struct timespec ts = {0,1};
  1575. nanosleep(&ts, NULL);
  1576. }
  1577. //! Pause execution and let other processes to run for a specified amount of time.
  1578. void CIrrDeviceLinux::sleep(u32 timeMs, bool pauseTimer=false)
  1579. {
  1580. const bool wasStopped = Timer ? Timer->isStopped() : true;
  1581. struct timespec ts;
  1582. ts.tv_sec = (time_t) (timeMs / 1000);
  1583. ts.tv_nsec = (long) (timeMs % 1000) * 1000000;
  1584. if (pauseTimer && !wasStopped)
  1585. Timer->stop();
  1586. nanosleep(&ts, NULL);
  1587. if (pauseTimer && !wasStopped)
  1588. Timer->start();
  1589. }
  1590. //! sets the caption of the window
  1591. void CIrrDeviceLinux::setWindowCaption(const wchar_t* text)
  1592. {
  1593. #ifdef _IRR_COMPILE_WITH_X11_
  1594. if (CreationParams.DriverType == video::EDT_NULL)
  1595. return;
  1596. XTextProperty txt;
  1597. if (Success==XwcTextListToTextProperty(display, const_cast<wchar_t**>(&text),
  1598. 1, XStdICCTextStyle, &txt))
  1599. {
  1600. XSetWMName(display, window, &txt);
  1601. XSetWMIconName(display, window, &txt);
  1602. XFree(txt.value);
  1603. }
  1604. #endif
  1605. }
  1606. //! presents a surface in the client area
  1607. bool CIrrDeviceLinux::present(video::IImage* image, void* windowId, core::rect<s32>* srcRect)
  1608. {
  1609. #ifdef _IRR_COMPILE_WITH_X11_
  1610. // this is only necessary for software drivers.
  1611. if (!SoftwareImage)
  1612. return true;
  1613. // thx to Nadav, who send me some clues of how to display the image
  1614. // to the X Server.
  1615. const u32 destwidth = SoftwareImage->width;
  1616. const u32 minWidth = core::min_(image->getDimension().Width, destwidth);
  1617. const u32 destPitch = SoftwareImage->bytes_per_line;
  1618. video::ECOLOR_FORMAT destColor;
  1619. switch (SoftwareImage->bits_per_pixel)
  1620. {
  1621. case 16:
  1622. if (SoftwareImage->depth==16)
  1623. destColor = video::ECF_R5G6B5;
  1624. else
  1625. destColor = video::ECF_A1R5G5B5;
  1626. break;
  1627. case 24: destColor = video::ECF_R8G8B8; break;
  1628. case 32: destColor = video::ECF_A8R8G8B8; break;
  1629. default:
  1630. os::Printer::log("Unsupported screen depth.");
  1631. return false;
  1632. }
  1633. u8* srcdata = reinterpret_cast<u8*>(image->lock());
  1634. u8* destData = reinterpret_cast<u8*>(SoftwareImage->data);
  1635. const u32 destheight = SoftwareImage->height;
  1636. const u32 srcheight = core::min_(image->getDimension().Height, destheight);
  1637. const u32 srcPitch = image->getPitch();
  1638. for (u32 y=0; y!=srcheight; ++y)
  1639. {
  1640. video::CColorConverter::convert_viaFormat(srcdata,image->getColorFormat(), minWidth, destData, destColor);
  1641. srcdata+=srcPitch;
  1642. destData+=destPitch;
  1643. }
  1644. image->unlock();
  1645. GC gc = DefaultGC(display, DefaultScreen(display));
  1646. Window myWindow=window;
  1647. if (windowId)
  1648. myWindow = reinterpret_cast<Window>(windowId);
  1649. XPutImage(display, myWindow, gc, SoftwareImage, 0, 0, 0, 0, destwidth, destheight);
  1650. #endif
  1651. return true;
  1652. }
  1653. //! notifies the device that it should close itself
  1654. void CIrrDeviceLinux::closeDevice()
  1655. {
  1656. Close = true;
  1657. }
  1658. //! returns if window is active. if not, nothing need to be drawn
  1659. bool CIrrDeviceLinux::isWindowActive() const
  1660. {
  1661. return (WindowHasFocus && !WindowMinimized);
  1662. }
  1663. //! returns if window has focus.
  1664. bool CIrrDeviceLinux::isWindowFocused() const
  1665. {
  1666. return WindowHasFocus;
  1667. }
  1668. //! returns if window is minimized.
  1669. bool CIrrDeviceLinux::isWindowMinimized() const
  1670. {
  1671. return WindowMinimized;
  1672. }
  1673. //! returns color format of the window.
  1674. video::ECOLOR_FORMAT CIrrDeviceLinux::getColorFormat() const
  1675. {
  1676. #ifdef _IRR_COMPILE_WITH_X11_
  1677. if (visual && (visual->depth != 16))
  1678. return video::ECF_R8G8B8;
  1679. else
  1680. #endif
  1681. return video::ECF_R5G6B5;
  1682. }
  1683. //! Sets if the window should be resizable in windowed mode.
  1684. void CIrrDeviceLinux::setResizable(bool resize)
  1685. {
  1686. #ifdef _IRR_COMPILE_WITH_X11_
  1687. if (CreationParams.DriverType == video::EDT_NULL || CreationParams.Fullscreen )
  1688. return;
  1689. XUnmapWindow(display, window);
  1690. if ( !resize )
  1691. {
  1692. // Must be heap memory because data size depends on X Server
  1693. XSizeHints *hints = XAllocSizeHints();
  1694. hints->flags=PSize|PMinSize|PMaxSize;
  1695. hints->min_width=hints->max_width=hints->base_width=Width;
  1696. hints->min_height=hints->max_height=hints->base_height=Height;
  1697. XSetWMNormalHints(display, window, hints);
  1698. XFree(hints);
  1699. }
  1700. else
  1701. {
  1702. XSetWMNormalHints(display, window, StdHints);
  1703. }
  1704. XMapWindow(display, window);
  1705. XFlush(display);
  1706. #endif // #ifdef _IRR_COMPILE_WITH_X11_
  1707. }
  1708. //! Return pointer to a list with all video modes supported by the gfx adapter.
  1709. video::IVideoModeList* CIrrDeviceLinux::getVideoModeList()
  1710. {
  1711. #ifdef _IRR_COMPILE_WITH_X11_
  1712. if (!VideoModeList.getVideoModeCount())
  1713. {
  1714. bool temporaryDisplay = false;
  1715. if (!display)
  1716. {
  1717. display = XOpenDisplay(0);
  1718. temporaryDisplay=true;
  1719. }
  1720. if (display)
  1721. {
  1722. #if defined(_IRR_LINUX_X11_VIDMODE_) || defined(_IRR_LINUX_X11_RANDR_)
  1723. s32 eventbase, errorbase;
  1724. s32 defaultDepth=DefaultDepth(display,screennr);
  1725. #endif
  1726. #ifdef _IRR_LINUX_X11_VIDMODE_
  1727. if (XF86VidModeQueryExtension(display, &eventbase, &errorbase))
  1728. {
  1729. // enumerate video modes
  1730. int modeCount;
  1731. XF86VidModeModeInfo** modes;
  1732. XF86VidModeGetAllModeLines(display, screennr, &modeCount, &modes);
  1733. // save current video mode
  1734. oldVideoMode = *modes[0];
  1735. // find fitting mode
  1736. VideoModeList.setDesktop(defaultDepth, core::dimension2d<u32>(
  1737. modes[0]->hdisplay, modes[0]->vdisplay));
  1738. for (int i = 0; i<modeCount; ++i)
  1739. {
  1740. VideoModeList.addMode(core::dimension2d<u32>(
  1741. modes[i]->hdisplay, modes[i]->vdisplay), defaultDepth);
  1742. }
  1743. XFree(modes);
  1744. }
  1745. #endif
  1746. #ifdef _IRR_LINUX_X11_RANDR_
  1747. output_id = BadRROutput;
  1748. old_mode = BadRRMode;
  1749. while (XRRQueryExtension(display, &eventbase, &errorbase))
  1750. {
  1751. XRROutputInfo* output = NULL;
  1752. XRRCrtcInfo* crtc = NULL;
  1753. crtc_x = crtc_y = -1;
  1754. XRRScreenResources* res = XRRGetScreenResources(display, DefaultRootWindow(display));
  1755. if (!res)
  1756. break;
  1757. RROutput primary_id = XRRGetOutputPrimary(display, DefaultRootWindow(display));
  1758. for (int i = 0; i < res->noutput; i++)
  1759. {
  1760. XRROutputInfo* output_tmp = XRRGetOutputInfo(display, res, res->outputs[i]);
  1761. if (!output_tmp || !output_tmp->crtc || output_tmp->connection == RR_Disconnected)
  1762. {
  1763. XRRFreeOutputInfo(output_tmp);
  1764. continue;
  1765. }
  1766. XRRCrtcInfo* crtc_tmp = XRRGetCrtcInfo(display, res, output_tmp->crtc);
  1767. if (!crtc_tmp)
  1768. {
  1769. XRRFreeOutputInfo(output_tmp);
  1770. continue;
  1771. }
  1772. if (res->outputs[i] == primary_id ||
  1773. output_id == BadRROutput || crtc_tmp->x < crtc->x ||
  1774. (crtc_tmp->x == crtc->x && crtc_tmp->y < crtc->y))
  1775. {
  1776. XRRFreeCrtcInfo(crtc);
  1777. XRRFreeOutputInfo(output);
  1778. output = output_tmp;
  1779. crtc = crtc_tmp;
  1780. output_id = res->outputs[i];
  1781. }
  1782. else
  1783. {
  1784. XRRFreeCrtcInfo(crtc_tmp);
  1785. XRRFreeOutputInfo(output_tmp);
  1786. }
  1787. if (res->outputs[i] == primary_id)
  1788. break;
  1789. }
  1790. if (output_id == BadRROutput)
  1791. {
  1792. os::Printer::log("Could not get video output.", ELL_WARNING);
  1793. break;
  1794. }
  1795. crtc_x = crtc->x;
  1796. crtc_y = crtc->y;
  1797. for (int i = 0; i < res->nmode; i++)
  1798. {
  1799. const XRRModeInfo* mode = &res->modes[i];
  1800. core::dimension2d<u32> size;
  1801. if (crtc->rotation & (XRANDR_ROTATION_LEFT|XRANDR_ROTATION_RIGHT))
  1802. {
  1803. size = core::dimension2d<u32>(mode->height, mode->width);
  1804. }
  1805. else
  1806. {
  1807. size = core::dimension2d<u32>(mode->width, mode->height);
  1808. }
  1809. for (int j = 0; j < output->nmode; j++)
  1810. {
  1811. if (mode->id == output->modes[j])
  1812. {
  1813. VideoModeList.addMode(size, defaultDepth);
  1814. break;
  1815. }
  1816. }
  1817. if (mode->id == crtc->mode)
  1818. {
  1819. old_mode = crtc->mode;
  1820. VideoModeList.setDesktop(defaultDepth, size);
  1821. }
  1822. }
  1823. XRRFreeCrtcInfo(crtc);
  1824. XRRFreeOutputInfo(output);
  1825. XRRFreeScreenResources(res);
  1826. break;
  1827. }
  1828. #endif
  1829. }
  1830. if (display && temporaryDisplay)
  1831. {
  1832. XCloseDisplay(display);
  1833. display=0;
  1834. }
  1835. }
  1836. #endif
  1837. return &VideoModeList;
  1838. }
  1839. //! Minimize window
  1840. void CIrrDeviceLinux::minimizeWindow()
  1841. {
  1842. #ifdef _IRR_COMPILE_WITH_X11_
  1843. XIconifyWindow(display, window, screennr);
  1844. #endif
  1845. }
  1846. //! Maximize window
  1847. void CIrrDeviceLinux::maximizeWindow()
  1848. {
  1849. #ifdef _IRR_COMPILE_WITH_X11_
  1850. XMapWindow(display, window);
  1851. #endif
  1852. }
  1853. //! Restore original window size
  1854. void CIrrDeviceLinux::restoreWindow()
  1855. {
  1856. #ifdef _IRR_COMPILE_WITH_X11_
  1857. XMapWindow(display, window);
  1858. #endif
  1859. }
  1860. void CIrrDeviceLinux::createKeyMap()
  1861. {
  1862. // I don't know if this is the best method to create
  1863. // the lookuptable, but I'll leave it like that until
  1864. // I find a better version.
  1865. #ifdef _IRR_COMPILE_WITH_X11_
  1866. KeyMap.reallocate(190);
  1867. KeyMap.push_back(SKeyMap(XK_BackSpace, KEY_BACK));
  1868. KeyMap.push_back(SKeyMap(XK_Tab, KEY_TAB));
  1869. KeyMap.push_back(SKeyMap(XK_ISO_Left_Tab, KEY_TAB));
  1870. KeyMap.push_back(SKeyMap(XK_Linefeed, 0)); // ???
  1871. KeyMap.push_back(SKeyMap(XK_Clear, KEY_CLEAR));
  1872. KeyMap.push_back(SKeyMap(XK_Return, KEY_RETURN));
  1873. KeyMap.push_back(SKeyMap(XK_Pause, KEY_PAUSE));
  1874. KeyMap.push_back(SKeyMap(XK_Scroll_Lock, KEY_SCROLL));
  1875. KeyMap.push_back(SKeyMap(XK_Sys_Req, 0)); // ???
  1876. KeyMap.push_back(SKeyMap(XK_Escape, KEY_ESCAPE));
  1877. KeyMap.push_back(SKeyMap(XK_Insert, KEY_INSERT));
  1878. KeyMap.push_back(SKeyMap(XK_Delete, KEY_DELETE));
  1879. KeyMap.push_back(SKeyMap(XK_Home, KEY_HOME));
  1880. KeyMap.push_back(SKeyMap(XK_Left, KEY_LEFT));
  1881. KeyMap.push_back(SKeyMap(XK_Up, KEY_UP));
  1882. KeyMap.push_back(SKeyMap(XK_Right, KEY_RIGHT));
  1883. KeyMap.push_back(SKeyMap(XK_Down, KEY_DOWN));
  1884. KeyMap.push_back(SKeyMap(XK_Prior, KEY_PRIOR));
  1885. KeyMap.push_back(SKeyMap(XK_Page_Up, KEY_PRIOR));
  1886. KeyMap.push_back(SKeyMap(XK_Next, KEY_NEXT));
  1887. KeyMap.push_back(SKeyMap(XK_Page_Down, KEY_NEXT));
  1888. KeyMap.push_back(SKeyMap(XK_End, KEY_END));
  1889. KeyMap.push_back(SKeyMap(XK_Begin, KEY_HOME));
  1890. KeyMap.push_back(SKeyMap(XK_Num_Lock, KEY_NUMLOCK));
  1891. KeyMap.push_back(SKeyMap(XK_KP_Space, KEY_SPACE));
  1892. KeyMap.push_back(SKeyMap(XK_KP_Tab, KEY_TAB));
  1893. KeyMap.push_back(SKeyMap(XK_KP_Enter, KEY_RETURN));
  1894. KeyMap.push_back(SKeyMap(XK_KP_F1, KEY_F1));
  1895. KeyMap.push_back(SKeyMap(XK_KP_F2, KEY_F2));
  1896. KeyMap.push_back(SKeyMap(XK_KP_F3, KEY_F3));
  1897. KeyMap.push_back(SKeyMap(XK_KP_F4, KEY_F4));
  1898. KeyMap.push_back(SKeyMap(XK_KP_Home, KEY_HOME));
  1899. KeyMap.push_back(SKeyMap(XK_KP_Left, KEY_LEFT));
  1900. KeyMap.push_back(SKeyMap(XK_KP_Up, KEY_UP));
  1901. KeyMap.push_back(SKeyMap(XK_KP_Right, KEY_RIGHT));
  1902. KeyMap.push_back(SKeyMap(XK_KP_Down, KEY_DOWN));
  1903. KeyMap.push_back(SKeyMap(XK_Print, KEY_PRINT));
  1904. KeyMap.push_back(SKeyMap(XK_KP_Prior, KEY_PRIOR));
  1905. KeyMap.push_back(SKeyMap(XK_KP_Page_Up, KEY_PRIOR));
  1906. KeyMap.push_back(SKeyMap(XK_KP_Next, KEY_NEXT));
  1907. KeyMap.push_back(SKeyMap(XK_KP_Page_Down, KEY_NEXT));
  1908. KeyMap.push_back(SKeyMap(XK_KP_End, KEY_END));
  1909. KeyMap.push_back(SKeyMap(XK_KP_Begin, KEY_HOME));
  1910. KeyMap.push_back(SKeyMap(XK_KP_Insert, KEY_INSERT));
  1911. KeyMap.push_back(SKeyMap(XK_KP_Delete, KEY_DELETE));
  1912. KeyMap.push_back(SKeyMap(XK_KP_Equal, 0)); // ???
  1913. KeyMap.push_back(SKeyMap(XK_KP_Multiply, KEY_MULTIPLY));
  1914. KeyMap.push_back(SKeyMap(XK_KP_Add, KEY_ADD));
  1915. KeyMap.push_back(SKeyMap(XK_KP_Separator, KEY_SEPARATOR));
  1916. KeyMap.push_back(SKeyMap(XK_KP_Subtract, KEY_SUBTRACT));
  1917. KeyMap.push_back(SKeyMap(XK_KP_Decimal, KEY_DECIMAL));
  1918. KeyMap.push_back(SKeyMap(XK_KP_Divide, KEY_DIVIDE));
  1919. KeyMap.push_back(SKeyMap(XK_KP_0, KEY_NUMPAD0));
  1920. KeyMap.push_back(SKeyMap(XK_KP_1, KEY_NUMPAD1));
  1921. KeyMap.push_back(SKeyMap(XK_KP_2, KEY_NUMPAD2));
  1922. KeyMap.push_back(SKeyMap(XK_KP_3, KEY_NUMPAD3));
  1923. KeyMap.push_back(SKeyMap(XK_KP_4, KEY_NUMPAD4));
  1924. KeyMap.push_back(SKeyMap(XK_KP_5, KEY_NUMPAD5));
  1925. KeyMap.push_back(SKeyMap(XK_KP_6, KEY_NUMPAD6));
  1926. KeyMap.push_back(SKeyMap(XK_KP_7, KEY_NUMPAD7));
  1927. KeyMap.push_back(SKeyMap(XK_KP_8, KEY_NUMPAD8));
  1928. KeyMap.push_back(SKeyMap(XK_KP_9, KEY_NUMPAD9));
  1929. KeyMap.push_back(SKeyMap(XK_F1, KEY_F1));
  1930. KeyMap.push_back(SKeyMap(XK_F2, KEY_F2));
  1931. KeyMap.push_back(SKeyMap(XK_F3, KEY_F3));
  1932. KeyMap.push_back(SKeyMap(XK_F4, KEY_F4));
  1933. KeyMap.push_back(SKeyMap(XK_F5, KEY_F5));
  1934. KeyMap.push_back(SKeyMap(XK_F6, KEY_F6));
  1935. KeyMap.push_back(SKeyMap(XK_F7, KEY_F7));
  1936. KeyMap.push_back(SKeyMap(XK_F8, KEY_F8));
  1937. KeyMap.push_back(SKeyMap(XK_F9, KEY_F9));
  1938. KeyMap.push_back(SKeyMap(XK_F10, KEY_F10));
  1939. KeyMap.push_back(SKeyMap(XK_F11, KEY_F11));
  1940. KeyMap.push_back(SKeyMap(XK_F12, KEY_F12));
  1941. KeyMap.push_back(SKeyMap(XK_Shift_L, KEY_LSHIFT));
  1942. KeyMap.push_back(SKeyMap(XK_Shift_R, KEY_RSHIFT));
  1943. KeyMap.push_back(SKeyMap(XK_Control_L, KEY_LCONTROL));
  1944. KeyMap.push_back(SKeyMap(XK_Control_R, KEY_RCONTROL));
  1945. KeyMap.push_back(SKeyMap(XK_Caps_Lock, KEY_CAPITAL));
  1946. KeyMap.push_back(SKeyMap(XK_Shift_Lock, KEY_CAPITAL));
  1947. KeyMap.push_back(SKeyMap(XK_Meta_L, KEY_LWIN));
  1948. KeyMap.push_back(SKeyMap(XK_Meta_R, KEY_RWIN));
  1949. KeyMap.push_back(SKeyMap(XK_Alt_L, KEY_LMENU));
  1950. KeyMap.push_back(SKeyMap(XK_Alt_R, KEY_RMENU));
  1951. KeyMap.push_back(SKeyMap(XK_ISO_Level3_Shift, KEY_RMENU));
  1952. KeyMap.push_back(SKeyMap(XK_Menu, KEY_MENU));
  1953. KeyMap.push_back(SKeyMap(XK_space, KEY_SPACE));
  1954. KeyMap.push_back(SKeyMap(XK_exclam, 0)); //?
  1955. KeyMap.push_back(SKeyMap(XK_quotedbl, 0)); //?
  1956. KeyMap.push_back(SKeyMap(XK_section, 0)); //?
  1957. KeyMap.push_back(SKeyMap(XK_numbersign, KEY_OEM_2));
  1958. KeyMap.push_back(SKeyMap(XK_dollar, 0)); //?
  1959. KeyMap.push_back(SKeyMap(XK_percent, 0)); //?
  1960. KeyMap.push_back(SKeyMap(XK_ampersand, 0)); //?
  1961. KeyMap.push_back(SKeyMap(XK_apostrophe, KEY_OEM_7));
  1962. KeyMap.push_back(SKeyMap(XK_parenleft, 0)); //?
  1963. KeyMap.push_back(SKeyMap(XK_parenright, 0)); //?
  1964. KeyMap.push_back(SKeyMap(XK_asterisk, 0)); //?
  1965. KeyMap.push_back(SKeyMap(XK_plus, KEY_PLUS)); //?
  1966. KeyMap.push_back(SKeyMap(XK_comma, KEY_COMMA)); //?
  1967. KeyMap.push_back(SKeyMap(XK_minus, KEY_MINUS)); //?
  1968. KeyMap.push_back(SKeyMap(XK_period, KEY_PERIOD)); //?
  1969. KeyMap.push_back(SKeyMap(XK_slash, KEY_OEM_2)); //?
  1970. KeyMap.push_back(SKeyMap(XK_0, KEY_KEY_0));
  1971. KeyMap.push_back(SKeyMap(XK_1, KEY_KEY_1));
  1972. KeyMap.push_back(SKeyMap(XK_2, KEY_KEY_2));
  1973. KeyMap.push_back(SKeyMap(XK_3, KEY_KEY_3));
  1974. KeyMap.push_back(SKeyMap(XK_4, KEY_KEY_4));
  1975. KeyMap.push_back(SKeyMap(XK_5, KEY_KEY_5));
  1976. KeyMap.push_back(SKeyMap(XK_6, KEY_KEY_6));
  1977. KeyMap.push_back(SKeyMap(XK_7, KEY_KEY_7));
  1978. KeyMap.push_back(SKeyMap(XK_8, KEY_KEY_8));
  1979. KeyMap.push_back(SKeyMap(XK_9, KEY_KEY_9));
  1980. KeyMap.push_back(SKeyMap(XK_colon, 0)); //?
  1981. KeyMap.push_back(SKeyMap(XK_semicolon, KEY_OEM_1));
  1982. KeyMap.push_back(SKeyMap(XK_less, KEY_OEM_102));
  1983. KeyMap.push_back(SKeyMap(XK_equal, KEY_PLUS));
  1984. KeyMap.push_back(SKeyMap(XK_greater, 0)); //?
  1985. KeyMap.push_back(SKeyMap(XK_question, 0)); //?
  1986. KeyMap.push_back(SKeyMap(XK_at, KEY_KEY_2)); //?
  1987. KeyMap.push_back(SKeyMap(XK_mu, 0)); //?
  1988. KeyMap.push_back(SKeyMap(XK_EuroSign, 0)); //?
  1989. KeyMap.push_back(SKeyMap(XK_A, KEY_KEY_A));
  1990. KeyMap.push_back(SKeyMap(XK_B, KEY_KEY_B));
  1991. KeyMap.push_back(SKeyMap(XK_C, KEY_KEY_C));
  1992. KeyMap.push_back(SKeyMap(XK_D, KEY_KEY_D));
  1993. KeyMap.push_back(SKeyMap(XK_E, KEY_KEY_E));
  1994. KeyMap.push_back(SKeyMap(XK_F, KEY_KEY_F));
  1995. KeyMap.push_back(SKeyMap(XK_G, KEY_KEY_G));
  1996. KeyMap.push_back(SKeyMap(XK_H, KEY_KEY_H));
  1997. KeyMap.push_back(SKeyMap(XK_I, KEY_KEY_I));
  1998. KeyMap.push_back(SKeyMap(XK_J, KEY_KEY_J));
  1999. KeyMap.push_back(SKeyMap(XK_K, KEY_KEY_K));
  2000. KeyMap.push_back(SKeyMap(XK_L, KEY_KEY_L));
  2001. KeyMap.push_back(SKeyMap(XK_M, KEY_KEY_M));
  2002. KeyMap.push_back(SKeyMap(XK_N, KEY_KEY_N));
  2003. KeyMap.push_back(SKeyMap(XK_O, KEY_KEY_O));
  2004. KeyMap.push_back(SKeyMap(XK_P, KEY_KEY_P));
  2005. KeyMap.push_back(SKeyMap(XK_Q, KEY_KEY_Q));
  2006. KeyMap.push_back(SKeyMap(XK_R, KEY_KEY_R));
  2007. KeyMap.push_back(SKeyMap(XK_S, KEY_KEY_S));
  2008. KeyMap.push_back(SKeyMap(XK_T, KEY_KEY_T));
  2009. KeyMap.push_back(SKeyMap(XK_U, KEY_KEY_U));
  2010. KeyMap.push_back(SKeyMap(XK_V, KEY_KEY_V));
  2011. KeyMap.push_back(SKeyMap(XK_W, KEY_KEY_W));
  2012. KeyMap.push_back(SKeyMap(XK_X, KEY_KEY_X));
  2013. KeyMap.push_back(SKeyMap(XK_Y, KEY_KEY_Y));
  2014. KeyMap.push_back(SKeyMap(XK_Z, KEY_KEY_Z));
  2015. KeyMap.push_back(SKeyMap(XK_bracketleft, KEY_OEM_4));
  2016. KeyMap.push_back(SKeyMap(XK_backslash, KEY_OEM_5));
  2017. KeyMap.push_back(SKeyMap(XK_bracketright, KEY_OEM_6));
  2018. KeyMap.push_back(SKeyMap(XK_asciicircum, KEY_OEM_5));
  2019. KeyMap.push_back(SKeyMap(XK_degree, 0)); //?
  2020. KeyMap.push_back(SKeyMap(XK_underscore, KEY_MINUS)); //?
  2021. KeyMap.push_back(SKeyMap(XK_grave, KEY_OEM_3));
  2022. KeyMap.push_back(SKeyMap(XK_acute, KEY_OEM_6));
  2023. KeyMap.push_back(SKeyMap(XK_a, KEY_KEY_A));
  2024. KeyMap.push_back(SKeyMap(XK_b, KEY_KEY_B));
  2025. KeyMap.push_back(SKeyMap(XK_c, KEY_KEY_C));
  2026. KeyMap.push_back(SKeyMap(XK_d, KEY_KEY_D));
  2027. KeyMap.push_back(SKeyMap(XK_e, KEY_KEY_E));
  2028. KeyMap.push_back(SKeyMap(XK_f, KEY_KEY_F));
  2029. KeyMap.push_back(SKeyMap(XK_g, KEY_KEY_G));
  2030. KeyMap.push_back(SKeyMap(XK_h, KEY_KEY_H));
  2031. KeyMap.push_back(SKeyMap(XK_i, KEY_KEY_I));
  2032. KeyMap.push_back(SKeyMap(XK_j, KEY_KEY_J));
  2033. KeyMap.push_back(SKeyMap(XK_k, KEY_KEY_K));
  2034. KeyMap.push_back(SKeyMap(XK_l, KEY_KEY_L));
  2035. KeyMap.push_back(SKeyMap(XK_m, KEY_KEY_M));
  2036. KeyMap.push_back(SKeyMap(XK_n, KEY_KEY_N));
  2037. KeyMap.push_back(SKeyMap(XK_o, KEY_KEY_O));
  2038. KeyMap.push_back(SKeyMap(XK_p, KEY_KEY_P));
  2039. KeyMap.push_back(SKeyMap(XK_q, KEY_KEY_Q));
  2040. KeyMap.push_back(SKeyMap(XK_r, KEY_KEY_R));
  2041. KeyMap.push_back(SKeyMap(XK_s, KEY_KEY_S));
  2042. KeyMap.push_back(SKeyMap(XK_t, KEY_KEY_T));
  2043. KeyMap.push_back(SKeyMap(XK_u, KEY_KEY_U));
  2044. KeyMap.push_back(SKeyMap(XK_v, KEY_KEY_V));
  2045. KeyMap.push_back(SKeyMap(XK_w, KEY_KEY_W));
  2046. KeyMap.push_back(SKeyMap(XK_x, KEY_KEY_X));
  2047. KeyMap.push_back(SKeyMap(XK_y, KEY_KEY_Y));
  2048. KeyMap.push_back(SKeyMap(XK_z, KEY_KEY_Z));
  2049. KeyMap.push_back(SKeyMap(XK_ssharp, KEY_OEM_4));
  2050. KeyMap.push_back(SKeyMap(XK_adiaeresis, KEY_OEM_7));
  2051. KeyMap.push_back(SKeyMap(XK_odiaeresis, KEY_OEM_3));
  2052. KeyMap.push_back(SKeyMap(XK_udiaeresis, KEY_OEM_1));
  2053. KeyMap.push_back(SKeyMap(XK_Super_L, KEY_LWIN));
  2054. KeyMap.push_back(SKeyMap(XK_Super_R, KEY_RWIN));
  2055. KeyMap.sort();
  2056. #endif
  2057. }
  2058. bool CIrrDeviceLinux::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
  2059. {
  2060. #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
  2061. joystickInfo.clear();
  2062. u32 joystick;
  2063. for (joystick = 0; joystick < 32; ++joystick)
  2064. {
  2065. // The joystick device could be here...
  2066. core::stringc devName = "/dev/js";
  2067. devName += joystick;
  2068. SJoystickInfo returnInfo;
  2069. JoystickInfo info;
  2070. info.fd = open(devName.c_str(), O_RDONLY);
  2071. if (-1 == info.fd)
  2072. {
  2073. // ...but Ubuntu and possibly other distros
  2074. // create the devices in /dev/input
  2075. devName = "/dev/input/js";
  2076. devName += joystick;
  2077. info.fd = open(devName.c_str(), O_RDONLY);
  2078. if (-1 == info.fd)
  2079. {
  2080. // and BSD here
  2081. devName = "/dev/joy";
  2082. devName += joystick;
  2083. info.fd = open(devName.c_str(), O_RDONLY);
  2084. }
  2085. }
  2086. if (-1 == info.fd)
  2087. continue;
  2088. #ifdef __FreeBSD__
  2089. info.axes=2;
  2090. info.buttons=2;
  2091. #elif defined(__linux__)
  2092. ioctl( info.fd, JSIOCGAXES, &(info.axes) );
  2093. ioctl( info.fd, JSIOCGBUTTONS, &(info.buttons) );
  2094. fcntl( info.fd, F_SETFL, O_NONBLOCK );
  2095. #endif
  2096. (void)memset(&info.persistentData, 0, sizeof(info.persistentData));
  2097. info.persistentData.EventType = irr::EET_JOYSTICK_INPUT_EVENT;
  2098. info.persistentData.JoystickEvent.Joystick = ActiveJoysticks.size();
  2099. // There's no obvious way to determine which (if any) axes represent a POV
  2100. // hat, so we'll just set it to "not used" and forget about it.
  2101. info.persistentData.JoystickEvent.POV = 65535;
  2102. ActiveJoysticks.push_back(info);
  2103. returnInfo.HasGenericName = false;
  2104. returnInfo.Joystick = joystick;
  2105. returnInfo.PovHat = SJoystickInfo::POV_HAT_UNKNOWN;
  2106. returnInfo.Axes = info.axes;
  2107. returnInfo.Buttons = info.buttons;
  2108. #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
  2109. char name[80];
  2110. ioctl( info.fd, JSIOCGNAME(80), name);
  2111. returnInfo.Name = name;
  2112. #endif
  2113. joystickInfo.push_back(returnInfo);
  2114. }
  2115. for (joystick = 0; joystick < joystickInfo.size(); ++joystick)
  2116. {
  2117. char logString[256];
  2118. (void)sprintf(logString, "Found joystick %u, %u axes, %u buttons '%s'",
  2119. joystick, joystickInfo[joystick].Axes,
  2120. joystickInfo[joystick].Buttons, joystickInfo[joystick].Name.c_str());
  2121. os::Printer::log(logString, ELL_INFORMATION);
  2122. }
  2123. return true;
  2124. #else
  2125. return false;
  2126. #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
  2127. }
  2128. void CIrrDeviceLinux::pollJoysticks()
  2129. {
  2130. #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
  2131. if (0 == ActiveJoysticks.size())
  2132. return;
  2133. for (u32 j= 0; j< ActiveJoysticks.size(); ++j)
  2134. {
  2135. JoystickInfo & info = ActiveJoysticks[j];
  2136. bool event_received = false;
  2137. #ifdef __FreeBSD__
  2138. struct joystick js;
  2139. if (read(info.fd, &js, sizeof(js)) == sizeof(js))
  2140. {
  2141. event_received = true;
  2142. info.persistentData.JoystickEvent.ButtonStates = js.b1 | (js.b2 << 1); /* should be a two-bit field */
  2143. info.persistentData.JoystickEvent.Axis[0] = js.x; /* X axis */
  2144. info.persistentData.JoystickEvent.Axis[1] = js.y; /* Y axis */
  2145. }
  2146. #elif defined(__linux__)
  2147. struct js_event event;
  2148. while (sizeof(event) == read(info.fd, &event, sizeof(event)))
  2149. {
  2150. switch(event.type & ~JS_EVENT_INIT)
  2151. {
  2152. case JS_EVENT_BUTTON:
  2153. if (event.value)
  2154. {
  2155. event_received = true;
  2156. info.persistentData.JoystickEvent.ButtonStates |= (1 << event.number);
  2157. }
  2158. else
  2159. {
  2160. event_received = true;
  2161. info.persistentData.JoystickEvent.ButtonStates &= ~(1 << event.number);
  2162. }
  2163. break;
  2164. case JS_EVENT_AXIS:
  2165. if (event.number < SEvent::SJoystickEvent::NUMBER_OF_AXES)
  2166. {
  2167. event_received = true;
  2168. info.persistentData.JoystickEvent.Axis[event.number] = event.value;
  2169. }
  2170. break;
  2171. default:
  2172. break;
  2173. }
  2174. }
  2175. #endif
  2176. // Send an irrlicht joystick event once per ::run() even if no new data were received.
  2177. (void)postEventFromUser(info.persistentData);
  2178. #ifdef _IRR_COMPILE_WITH_X11_
  2179. if (event_received)
  2180. {
  2181. XResetScreenSaver(display);
  2182. }
  2183. #endif
  2184. }
  2185. #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
  2186. }
  2187. //! Set the current Gamma Value for the Display
  2188. bool CIrrDeviceLinux::setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast )
  2189. {
  2190. #if defined(_IRR_LINUX_X11_VIDMODE_) || defined(_IRR_LINUX_X11_RANDR_)
  2191. s32 eventbase, errorbase;
  2192. #ifdef _IRR_LINUX_X11_VIDMODE_
  2193. if (XF86VidModeQueryExtension(display, &eventbase, &errorbase))
  2194. {
  2195. XF86VidModeGamma gamma;
  2196. gamma.red=red;
  2197. gamma.green=green;
  2198. gamma.blue=blue;
  2199. XF86VidModeSetGamma(display, screennr, &gamma);
  2200. return true;
  2201. }
  2202. #endif
  2203. #if defined(_IRR_LINUX_X11_VIDMODE_) && defined(_IRR_LINUX_X11_RANDR_)
  2204. else
  2205. #endif
  2206. #ifdef _IRR_LINUX_X11_RANDR_
  2207. if (XRRQueryExtension(display, &eventbase, &errorbase))
  2208. {
  2209. XRRQueryVersion(display, &eventbase, &errorbase); // major, minor
  2210. if (eventbase>=1 && errorbase>1)
  2211. {
  2212. #if (RANDR_MAJOR>1 || RANDR_MINOR>1)
  2213. XRRCrtcGamma *gamma = XRRGetCrtcGamma(display, screennr);
  2214. if (gamma)
  2215. {
  2216. *gamma->red=(u16)red;
  2217. *gamma->green=(u16)green;
  2218. *gamma->blue=(u16)blue;
  2219. XRRSetCrtcGamma(display, screennr, gamma);
  2220. XRRFreeGamma(gamma);
  2221. return true;
  2222. }
  2223. #endif
  2224. }
  2225. }
  2226. #endif
  2227. #endif
  2228. return false;
  2229. }
  2230. //! Get the current Gamma Value for the Display
  2231. bool CIrrDeviceLinux::getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast )
  2232. {
  2233. brightness = 0.f;
  2234. contrast = 0.f;
  2235. #if defined(_IRR_LINUX_X11_VIDMODE_) || defined(_IRR_LINUX_X11_RANDR_)
  2236. s32 eventbase, errorbase;
  2237. #ifdef _IRR_LINUX_X11_VIDMODE_
  2238. if (XF86VidModeQueryExtension(display, &eventbase, &errorbase))
  2239. {
  2240. XF86VidModeGamma gamma;
  2241. XF86VidModeGetGamma(display, screennr, &gamma);
  2242. red = gamma.red;
  2243. green = gamma.green;
  2244. blue = gamma.blue;
  2245. return true;
  2246. }
  2247. #endif
  2248. #if defined(_IRR_LINUX_X11_VIDMODE_) && defined(_IRR_LINUX_X11_RANDR_)
  2249. else
  2250. #endif
  2251. #ifdef _IRR_LINUX_X11_RANDR_
  2252. if (XRRQueryExtension(display, &eventbase, &errorbase))
  2253. {
  2254. XRRQueryVersion(display, &eventbase, &errorbase); // major, minor
  2255. if (eventbase>=1 && errorbase>1)
  2256. {
  2257. #if (RANDR_MAJOR>1 || RANDR_MINOR>1)
  2258. XRRCrtcGamma *gamma = XRRGetCrtcGamma(display, screennr);
  2259. if (gamma)
  2260. {
  2261. red = *gamma->red;
  2262. green = *gamma->green;
  2263. blue= *gamma->blue;
  2264. XRRFreeGamma(gamma);
  2265. return true;
  2266. }
  2267. #endif
  2268. }
  2269. }
  2270. #endif
  2271. #endif
  2272. return false;
  2273. }
  2274. //! gets text from the clipboard
  2275. //! \return Returns 0 if no string is in there.
  2276. const c8* CIrrDeviceLinux::getTextFromClipboard() const
  2277. {
  2278. #if defined(_IRR_COMPILE_WITH_X11_)
  2279. if (X_ATOM_CLIPBOARD == None)
  2280. {
  2281. os::Printer::log("Couldn't access X clipboard", ELL_WARNING);
  2282. return 0;
  2283. }
  2284. Window ownerWindow = XGetSelectionOwner(display, X_ATOM_CLIPBOARD);
  2285. if (ownerWindow == window)
  2286. {
  2287. return Clipboard.c_str();
  2288. }
  2289. Clipboard = "";
  2290. if (ownerWindow == None)
  2291. return 0;
  2292. Atom selection = XInternAtom(display, "IRR_SELECTION", False);
  2293. XConvertSelection(display, X_ATOM_CLIPBOARD, X_ATOM_UTF8_STRING, selection, window, CurrentTime);
  2294. const int SELECTION_RETRIES = 500;
  2295. int i = 0;
  2296. for (i = 0; i < SELECTION_RETRIES; i++)
  2297. {
  2298. XEvent xevent;
  2299. bool res = XCheckTypedWindowEvent(display, window, SelectionNotify, &xevent);
  2300. if (res && xevent.xselection.selection == X_ATOM_CLIPBOARD)
  2301. break;
  2302. usleep(1000);
  2303. }
  2304. if (i == SELECTION_RETRIES)
  2305. {
  2306. os::Printer::log("Timed out waiting for SelectionNotify event", ELL_WARNING);
  2307. return 0;
  2308. }
  2309. Atom type;
  2310. int format;
  2311. unsigned long numItems, dummy;
  2312. unsigned char *data;
  2313. int result = XGetWindowProperty(display, window, selection, 0, INT_MAX/4,
  2314. False, AnyPropertyType, &type, &format,
  2315. &numItems, &dummy, &data);
  2316. if (result == Success)
  2317. Clipboard = (irr::c8*)data;
  2318. XFree (data);
  2319. return Clipboard.c_str();
  2320. #else
  2321. return 0;
  2322. #endif
  2323. }
  2324. //! copies text to the clipboard
  2325. void CIrrDeviceLinux::copyToClipboard(const c8* text) const
  2326. {
  2327. #if defined(_IRR_COMPILE_WITH_X11_)
  2328. // Actually there is no clipboard on X but applications just say they own the clipboard and return text when asked.
  2329. // Which btw. also means that on X you lose clipboard content when closing applications.
  2330. Clipboard = text;
  2331. XSetSelectionOwner (display, X_ATOM_CLIPBOARD, window, CurrentTime);
  2332. XFlush (display);
  2333. #endif
  2334. }
  2335. #ifdef _IRR_COMPILE_WITH_X11_
  2336. // return true if the passed event has the type passed in parameter arg
  2337. Bool PredicateIsEventType(Display *display, XEvent *event, XPointer arg)
  2338. {
  2339. if ( event && event->type == *(int*)arg )
  2340. {
  2341. // os::Printer::log("remove event:", core::stringc((int)arg).c_str(), ELL_INFORMATION);
  2342. return True;
  2343. }
  2344. return False;
  2345. }
  2346. #endif //_IRR_COMPILE_WITH_X11_
  2347. //! Remove all messages pending in the system message loop
  2348. void CIrrDeviceLinux::clearSystemMessages()
  2349. {
  2350. #ifdef _IRR_COMPILE_WITH_X11_
  2351. if (CreationParams.DriverType != video::EDT_NULL)
  2352. {
  2353. XEvent event;
  2354. int usrArg = ButtonPress;
  2355. while ( XCheckIfEvent(display, &event, PredicateIsEventType, XPointer(&usrArg)) == True ) {}
  2356. usrArg = ButtonRelease;
  2357. while ( XCheckIfEvent(display, &event, PredicateIsEventType, XPointer(&usrArg)) == True ) {}
  2358. usrArg = MotionNotify;
  2359. while ( XCheckIfEvent(display, &event, PredicateIsEventType, XPointer(&usrArg)) == True ) {}
  2360. usrArg = KeyRelease;
  2361. while ( XCheckIfEvent(display, &event, PredicateIsEventType, XPointer(&usrArg)) == True ) {}
  2362. usrArg = KeyPress;
  2363. while ( XCheckIfEvent(display, &event, PredicateIsEventType, XPointer(&usrArg)) == True ) {}
  2364. }
  2365. #endif //_IRR_COMPILE_WITH_X11_
  2366. }
  2367. void CIrrDeviceLinux::initXAtoms()
  2368. {
  2369. #ifdef _IRR_COMPILE_WITH_X11_
  2370. X_ATOM_CLIPBOARD = XInternAtom(display, "CLIPBOARD", False);
  2371. X_ATOM_TARGETS = XInternAtom(display, "TARGETS", False);
  2372. X_ATOM_UTF8_STRING = XInternAtom (display, "UTF8_STRING", False);
  2373. #endif
  2374. }
  2375. #ifdef _IRR_COMPILE_WITH_X11_
  2376. Cursor CIrrDeviceLinux::TextureToMonochromeCursor(irr::video::ITexture * tex, const core::rect<s32>& sourceRect, const core::position2d<s32> &hotspot)
  2377. {
  2378. XImage * sourceImage = XCreateImage(display, visual->visual,
  2379. 1, // depth,
  2380. ZPixmap, // XYBitmap (depth=1), ZPixmap(depth=x)
  2381. 0, 0, sourceRect.getWidth(), sourceRect.getHeight(),
  2382. 32, // bitmap_pad,
  2383. 0// bytes_per_line (0 means continuos in memory)
  2384. );
  2385. sourceImage->data = new char[sourceImage->height * sourceImage->bytes_per_line];
  2386. XImage * maskImage = XCreateImage(display, visual->visual,
  2387. 1, // depth,
  2388. ZPixmap,
  2389. 0, 0, sourceRect.getWidth(), sourceRect.getHeight(),
  2390. 32, // bitmap_pad,
  2391. 0 // bytes_per_line
  2392. );
  2393. maskImage->data = new char[maskImage->height * maskImage->bytes_per_line];
  2394. // write texture into XImage
  2395. video::ECOLOR_FORMAT format = tex->getColorFormat();
  2396. u32 bytesPerPixel = video::IImage::getBitsPerPixelFromFormat(format) / 8;
  2397. u32 bytesLeftGap = sourceRect.UpperLeftCorner.X * bytesPerPixel;
  2398. u32 bytesRightGap = tex->getPitch() - sourceRect.LowerRightCorner.X * bytesPerPixel;
  2399. const u8* data = (const u8*)tex->lock(video::ETLM_READ_ONLY, 0);
  2400. data += sourceRect.UpperLeftCorner.Y*tex->getPitch();
  2401. for ( s32 y = 0; y < sourceRect.getHeight(); ++y )
  2402. {
  2403. data += bytesLeftGap;
  2404. for ( s32 x = 0; x < sourceRect.getWidth(); ++x )
  2405. {
  2406. video::SColor pixelCol;
  2407. pixelCol.setData((const void*)data, format);
  2408. data += bytesPerPixel;
  2409. if ( pixelCol.getAlpha() == 0 ) // transparent
  2410. {
  2411. XPutPixel(maskImage, x, y, 0);
  2412. XPutPixel(sourceImage, x, y, 0);
  2413. }
  2414. else // color
  2415. {
  2416. if ( pixelCol.getAverage() >= 127 )
  2417. XPutPixel(sourceImage, x, y, 1);
  2418. else
  2419. XPutPixel(sourceImage, x, y, 0);
  2420. XPutPixel(maskImage, x, y, 1);
  2421. }
  2422. }
  2423. data += bytesRightGap;
  2424. }
  2425. tex->unlock();
  2426. Pixmap sourcePixmap = XCreatePixmap(display, window, sourceImage->width, sourceImage->height, sourceImage->depth);
  2427. Pixmap maskPixmap = XCreatePixmap(display, window, maskImage->width, maskImage->height, maskImage->depth);
  2428. XGCValues values;
  2429. values.foreground = 1;
  2430. values.background = 1;
  2431. GC gc = XCreateGC( display, sourcePixmap, GCForeground | GCBackground, &values );
  2432. XPutImage(display, sourcePixmap, gc, sourceImage, 0, 0, 0, 0, sourceImage->width, sourceImage->height);
  2433. XPutImage(display, maskPixmap, gc, maskImage, 0, 0, 0, 0, maskImage->width, maskImage->height);
  2434. XFreeGC(display, gc);
  2435. XDestroyImage(sourceImage);
  2436. XDestroyImage(maskImage);
  2437. Cursor cursorResult = 0;
  2438. XColor foreground, background;
  2439. foreground.red = 65535;
  2440. foreground.green = 65535;
  2441. foreground.blue = 65535;
  2442. foreground.flags = DoRed | DoGreen | DoBlue;
  2443. background.red = 0;
  2444. background.green = 0;
  2445. background.blue = 0;
  2446. background.flags = DoRed | DoGreen | DoBlue;
  2447. cursorResult = XCreatePixmapCursor(display, sourcePixmap, maskPixmap, &foreground, &background, hotspot.X, hotspot.Y);
  2448. XFreePixmap(display, sourcePixmap);
  2449. XFreePixmap(display, maskPixmap);
  2450. return cursorResult;
  2451. }
  2452. #ifdef _IRR_LINUX_XCURSOR_
  2453. Cursor CIrrDeviceLinux::TextureToARGBCursor(irr::video::ITexture * tex, const core::rect<s32>& sourceRect, const core::position2d<s32> &hotspot)
  2454. {
  2455. XcursorImage * image = XcursorImageCreate (sourceRect.getWidth(), sourceRect.getHeight());
  2456. image->xhot = hotspot.X;
  2457. image->yhot = hotspot.Y;
  2458. // write texture into XcursorImage
  2459. video::ECOLOR_FORMAT format = tex->getColorFormat();
  2460. u32 bytesPerPixel = video::IImage::getBitsPerPixelFromFormat(format) / 8;
  2461. u32 bytesLeftGap = sourceRect.UpperLeftCorner.X * bytesPerPixel;
  2462. u32 bytesRightGap = tex->getPitch() - sourceRect.LowerRightCorner.X * bytesPerPixel;
  2463. XcursorPixel* target = image->pixels;
  2464. const u8* data = (const u8*)tex->lock(ETLM_READ_ONLY, 0);
  2465. data += sourceRect.UpperLeftCorner.Y*tex->getPitch();
  2466. for ( s32 y = 0; y < sourceRect.getHeight(); ++y )
  2467. {
  2468. data += bytesLeftGap;
  2469. for ( s32 x = 0; x < sourceRect.getWidth(); ++x )
  2470. {
  2471. video::SColor pixelCol;
  2472. pixelCol.setData((const void*)data, format);
  2473. data += bytesPerPixel;
  2474. *target = (XcursorPixel)pixelCol.color;
  2475. ++target;
  2476. }
  2477. data += bytesRightGap;
  2478. }
  2479. tex->unlock();
  2480. Cursor cursorResult=XcursorImageLoadCursor(display, image);
  2481. XcursorImageDestroy(image);
  2482. return cursorResult;
  2483. }
  2484. #endif // #ifdef _IRR_LINUX_XCURSOR_
  2485. Cursor CIrrDeviceLinux::TextureToCursor(irr::video::ITexture * tex, const core::rect<s32>& sourceRect, const core::position2d<s32> &hotspot)
  2486. {
  2487. #ifdef _IRR_LINUX_XCURSOR_
  2488. return TextureToARGBCursor( tex, sourceRect, hotspot );
  2489. #else
  2490. return TextureToMonochromeCursor( tex, sourceRect, hotspot );
  2491. #endif
  2492. }
  2493. #endif // _IRR_COMPILE_WITH_X11_
  2494. CIrrDeviceLinux::CCursorControl::CCursorControl(CIrrDeviceLinux* dev, bool null)
  2495. : Device(dev)
  2496. #ifdef _IRR_COMPILE_WITH_X11_
  2497. , PlatformBehavior(gui::ECPB_NONE), lastQuery(0)
  2498. #endif
  2499. , IsVisible(true), Null(null), UseReferenceRect(false)
  2500. , ActiveIcon(gui::ECI_NORMAL), ActiveIconStartTime(0)
  2501. {
  2502. #ifdef _IRR_COMPILE_WITH_X11_
  2503. if (!Null)
  2504. {
  2505. XGCValues values;
  2506. unsigned long valuemask = 0;
  2507. XColor fg, bg;
  2508. // this code, for making the cursor invisible was sent in by
  2509. // Sirshane, thank your very much!
  2510. Pixmap invisBitmap = XCreatePixmap(Device->display, Device->window, 32, 32, 1);
  2511. Pixmap maskBitmap = XCreatePixmap(Device->display, Device->window, 32, 32, 1);
  2512. Colormap screen_colormap = DefaultColormap( Device->display, DefaultScreen( Device->display ) );
  2513. XAllocNamedColor( Device->display, screen_colormap, "black", &fg, &fg );
  2514. XAllocNamedColor( Device->display, screen_colormap, "white", &bg, &bg );
  2515. GC gc = XCreateGC( Device->display, invisBitmap, valuemask, &values );
  2516. XSetForeground( Device->display, gc, BlackPixel( Device->display, DefaultScreen( Device->display ) ) );
  2517. XFillRectangle( Device->display, invisBitmap, gc, 0, 0, 32, 32 );
  2518. XFillRectangle( Device->display, maskBitmap, gc, 0, 0, 32, 32 );
  2519. invisCursor = XCreatePixmapCursor( Device->display, invisBitmap, maskBitmap, &fg, &bg, 1, 1 );
  2520. XFreeGC(Device->display, gc);
  2521. XFreePixmap(Device->display, invisBitmap);
  2522. XFreePixmap(Device->display, maskBitmap);
  2523. initCursors();
  2524. }
  2525. #endif
  2526. }
  2527. CIrrDeviceLinux::CCursorControl::~CCursorControl()
  2528. {
  2529. // Do not clearCursors here as the display is already closed
  2530. // TODO (cutealien): droping cursorcontrol earlier might work, not sure about reason why that's done in stub currently.
  2531. }
  2532. #ifdef _IRR_COMPILE_WITH_X11_
  2533. void CIrrDeviceLinux::CCursorControl::clearCursors()
  2534. {
  2535. if (!Null)
  2536. XFreeCursor(Device->display, invisCursor);
  2537. for ( u32 i=0; i < Cursors.size(); ++i )
  2538. {
  2539. for ( u32 f=0; f < Cursors[i].Frames.size(); ++f )
  2540. {
  2541. XFreeCursor(Device->display, Cursors[i].Frames[f].IconHW);
  2542. }
  2543. }
  2544. }
  2545. void CIrrDeviceLinux::CCursorControl::initCursors()
  2546. {
  2547. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_top_left_arrow)) ); // (or XC_arrow?)
  2548. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_crosshair)) );
  2549. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_hand2)) ); // (or XC_hand1? )
  2550. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_question_arrow)) );
  2551. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_xterm)) );
  2552. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_X_cursor)) ); // (or XC_pirate?)
  2553. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_watch)) ); // (or XC_clock?)
  2554. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_fleur)) );
  2555. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_top_right_corner)) ); // NESW not available in X11
  2556. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_top_left_corner)) ); // NWSE not available in X11
  2557. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_sb_v_double_arrow)) );
  2558. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_sb_h_double_arrow)) );
  2559. Cursors.push_back( CursorX11(XCreateFontCursor(Device->display, XC_sb_up_arrow)) ); // (or XC_center_ptr?)
  2560. }
  2561. void CIrrDeviceLinux::CCursorControl::update()
  2562. {
  2563. if ( (u32)ActiveIcon < Cursors.size() && !Cursors[ActiveIcon].Frames.empty() && Cursors[ActiveIcon].FrameTime )
  2564. {
  2565. // update animated cursors. This could also be done by X11 in case someone wants to figure that out (this way was just easier to implement)
  2566. u32 now = Device->getTimer()->getRealTime();
  2567. u32 frame = ((now - ActiveIconStartTime) / Cursors[ActiveIcon].FrameTime) % Cursors[ActiveIcon].Frames.size();
  2568. XDefineCursor(Device->display, Device->window, Cursors[ActiveIcon].Frames[frame].IconHW);
  2569. }
  2570. }
  2571. #endif
  2572. //! Sets the active cursor icon
  2573. void CIrrDeviceLinux::CCursorControl::setActiveIcon(gui::ECURSOR_ICON iconId)
  2574. {
  2575. #ifdef _IRR_COMPILE_WITH_X11_
  2576. if ( iconId >= (s32)Cursors.size() )
  2577. return;
  2578. if ( Cursors[iconId].Frames.size() )
  2579. XDefineCursor(Device->display, Device->window, Cursors[iconId].Frames[0].IconHW);
  2580. ActiveIconStartTime = Device->getTimer()->getRealTime();
  2581. ActiveIcon = iconId;
  2582. #endif
  2583. }
  2584. //! Add a custom sprite as cursor icon.
  2585. gui::ECURSOR_ICON CIrrDeviceLinux::CCursorControl::addIcon(const gui::SCursorSprite& icon)
  2586. {
  2587. #ifdef _IRR_COMPILE_WITH_X11_
  2588. if ( icon.SpriteId >= 0 )
  2589. {
  2590. CursorX11 cX11;
  2591. cX11.FrameTime = icon.SpriteBank->getSprites()[icon.SpriteId].frameTime;
  2592. for ( u32 i=0; i < icon.SpriteBank->getSprites()[icon.SpriteId].Frames.size(); ++i )
  2593. {
  2594. irr::u32 texId = icon.SpriteBank->getSprites()[icon.SpriteId].Frames[i].textureNumber;
  2595. irr::u32 rectId = icon.SpriteBank->getSprites()[icon.SpriteId].Frames[i].rectNumber;
  2596. irr::core::rect<s32> rectIcon = icon.SpriteBank->getPositions()[rectId];
  2597. Cursor cursor = Device->TextureToCursor(icon.SpriteBank->getTexture(texId), rectIcon, icon.HotSpot);
  2598. cX11.Frames.push_back( CursorFrameX11(cursor) );
  2599. }
  2600. Cursors.push_back( cX11 );
  2601. return (gui::ECURSOR_ICON)(Cursors.size() - 1);
  2602. }
  2603. #endif
  2604. return gui::ECI_NORMAL;
  2605. }
  2606. //! replace the given cursor icon.
  2607. void CIrrDeviceLinux::CCursorControl::changeIcon(gui::ECURSOR_ICON iconId, const gui::SCursorSprite& icon)
  2608. {
  2609. #ifdef _IRR_COMPILE_WITH_X11_
  2610. if ( iconId >= (s32)Cursors.size() )
  2611. return;
  2612. for ( u32 i=0; i < Cursors[iconId].Frames.size(); ++i )
  2613. XFreeCursor(Device->display, Cursors[iconId].Frames[i].IconHW);
  2614. if ( icon.SpriteId >= 0 )
  2615. {
  2616. CursorX11 cX11;
  2617. cX11.FrameTime = icon.SpriteBank->getSprites()[icon.SpriteId].frameTime;
  2618. for ( u32 i=0; i < icon.SpriteBank->getSprites()[icon.SpriteId].Frames.size(); ++i )
  2619. {
  2620. irr::u32 texId = icon.SpriteBank->getSprites()[icon.SpriteId].Frames[i].textureNumber;
  2621. irr::u32 rectId = icon.SpriteBank->getSprites()[icon.SpriteId].Frames[i].rectNumber;
  2622. irr::core::rect<s32> rectIcon = icon.SpriteBank->getPositions()[rectId];
  2623. Cursor cursor = Device->TextureToCursor(icon.SpriteBank->getTexture(texId), rectIcon, icon.HotSpot);
  2624. cX11.Frames.push_back( CursorFrameX11(cursor) );
  2625. }
  2626. Cursors[iconId] = cX11;
  2627. }
  2628. #endif
  2629. }
  2630. irr::core::dimension2di CIrrDeviceLinux::CCursorControl::getSupportedIconSize() const
  2631. {
  2632. // this returns the closest match that is smaller or same size, so we just pass a value which should be large enough for cursors
  2633. unsigned int width=0, height=0;
  2634. #ifdef _IRR_COMPILE_WITH_X11_
  2635. XQueryBestCursor(Device->display, Device->window, 64, 64, &width, &height);
  2636. #endif
  2637. return core::dimension2di(width, height);
  2638. }
  2639. } // end namespace
  2640. #endif // _IRR_COMPILE_WITH_X11_DEVICE_