Newer
Older
/*
==============================================================================
This file is part of the JUCE library.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
extern XContext windowHandleXContext;
typedef void (*WindowMessageReceiveCallback) (XEvent&);
extern WindowMessageReceiveCallback dispatchWindowMessage;
//==============================================================================
struct Atoms
{
Atoms()
{
protocols = getIfExists ("WM_PROTOCOLS");
protocolList [TAKE_FOCUS] = getIfExists ("WM_TAKE_FOCUS");
protocolList [DELETE_WINDOW] = getIfExists ("WM_DELETE_WINDOW");
protocolList [PING] = getIfExists ("_NET_WM_PING");
changeState = getIfExists ("WM_CHANGE_STATE");
state = getIfExists ("WM_STATE");
userTime = getCreating ("_NET_WM_USER_TIME");
activeWin = getCreating ("_NET_ACTIVE_WINDOW");
pid = getCreating ("_NET_WM_PID");
windowType = getIfExists ("_NET_WM_WINDOW_TYPE");
windowState = getIfExists ("_NET_WM_STATE");
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
XdndAware = getCreating ("XdndAware");
XdndEnter = getCreating ("XdndEnter");
XdndLeave = getCreating ("XdndLeave");
XdndPosition = getCreating ("XdndPosition");
XdndStatus = getCreating ("XdndStatus");
XdndDrop = getCreating ("XdndDrop");
XdndFinished = getCreating ("XdndFinished");
XdndSelection = getCreating ("XdndSelection");
XdndTypeList = getCreating ("XdndTypeList");
XdndActionList = getCreating ("XdndActionList");
XdndActionCopy = getCreating ("XdndActionCopy");
XdndActionPrivate = getCreating ("XdndActionPrivate");
XdndActionDescription = getCreating ("XdndActionDescription");
allowedMimeTypes[0] = getCreating ("UTF8_STRING");
allowedMimeTypes[1] = getCreating ("text/plain;charset=utf-8");
allowedMimeTypes[2] = getCreating ("text/plain");
allowedMimeTypes[3] = getCreating ("text/uri-list");
allowedActions[0] = getCreating ("XdndActionMove");
allowedActions[1] = XdndActionCopy;
allowedActions[2] = getCreating ("XdndActionLink");
allowedActions[3] = getCreating ("XdndActionAsk");
allowedActions[4] = XdndActionPrivate;
}
enum ProtocolItems
{
TAKE_FOCUS = 0,
DELETE_WINDOW = 1,
PING = 2
};
Atom protocols, protocolList[3], changeState, state, userTime,
activeWin, pid, windowType, windowState,
XdndAware, XdndEnter, XdndLeave, XdndPosition, XdndStatus,
XdndDrop, XdndFinished, XdndSelection, XdndTypeList, XdndActionList,
XdndActionDescription, XdndActionCopy, XdndActionPrivate,
allowedActions[5],
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
static const unsigned long DndVersion;
static Atom getIfExists (const char* name) { return XInternAtom (display, name, True); }
static Atom getCreating (const char* name) { return XInternAtom (display, name, False); }
static String getName (const Atom atom)
{
if (atom == None)
return "None";
return String (XGetAtomName (display, atom));
}
static bool isMimeTypeFile (const Atom atom) { return getName (atom).equalsIgnoreCase ("text/uri-list"); }
};
const unsigned long Atoms::DndVersion = 3;
//==============================================================================
struct GetXProperty
{
GetXProperty (Window window, Atom atom, long offset, long length, bool shouldDelete, Atom requestedType)
: data (nullptr)
{
success = (XGetWindowProperty (display, window, atom, offset, length,
(Bool) shouldDelete, requestedType, &actualType,
&actualFormat, &numItems, &bytesLeft, &data) == Success)
&& data != nullptr;
}
~GetXProperty()
{
if (data != nullptr)
XFree (data);
}
bool success;
unsigned char* data;
unsigned long numItems, bytesLeft;
Atom actualType;
int actualFormat;
};
//==============================================================================
namespace Keys
{
enum MouseButtons
{
NoButton = 0,
LeftButton = 1,
MiddleButton = 2,
RightButton = 3,
WheelUp = 4,
WheelDown = 5
};
static int AltMask = 0;
static int NumLockMask = 0;
static bool numLock = false;
static bool capsLock = false;
static char keyStates [32];
static const int extendedKeyModifier = 0x10000000;
}
bool KeyPress::isKeyCurrentlyDown (const int keyCode)
{
int keysym;
if (keyCode & Keys::extendedKeyModifier)
{
keysym = 0xff00 | (keyCode & 0xff);
}
else
{
keysym = keyCode;
if (keysym == (XK_Tab & 0xff)
|| keysym == (XK_Return & 0xff)
|| keysym == (XK_Escape & 0xff)
|| keysym == (XK_BackSpace & 0xff))
{
keysym |= 0xff00;
}
}
ScopedXLock xlock;
const int keycode = XKeysymToKeycode (display, (KeySym) keysym);
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const int keybyte = keycode >> 3;
const int keybit = (1 << (keycode & 7));
return (Keys::keyStates [keybyte] & keybit) != 0;
}
//==============================================================================
#if JUCE_USE_XSHM
namespace XSHMHelpers
{
static int trappedErrorCode = 0;
extern "C" int errorTrapHandler (Display*, XErrorEvent* err)
{
trappedErrorCode = err->error_code;
return 0;
}
static bool isShmAvailable() noexcept
{
static bool isChecked = false;
static bool isAvailable = false;
if (! isChecked)
{
isChecked = true;
if (XShmQueryVersion (display, &major, &minor, &pixmaps))
trappedErrorCode = 0;
XErrorHandler oldHandler = XSetErrorHandler (errorTrapHandler);
XShmSegmentInfo segmentInfo;
zerostruct (segmentInfo);
if (XImage* xImage = XShmCreateImage (display, DefaultVisual (display, DefaultScreen (display)),
24, ZPixmap, 0, &segmentInfo, 50, 50))
{
if ((segmentInfo.shmid = shmget (IPC_PRIVATE,
(size_t) (xImage->bytes_per_line * xImage->height),
IPC_CREAT | 0777)) >= 0)
segmentInfo.shmaddr = (char*) shmat (segmentInfo.shmid, 0, 0);
if (segmentInfo.shmaddr != (void*) -1)
{
segmentInfo.readOnly = False;
xImage->data = segmentInfo.shmaddr;
XSync (display, False);
if (XShmAttach (display, &segmentInfo) != 0)
{
XSync (display, False);
XShmDetach (display, &segmentInfo);
XFlush (display);
XDestroyImage (xImage);
shmdt (segmentInfo.shmaddr);
}
shmctl (segmentInfo.shmid, IPC_RMID, 0);
XSetErrorHandler (oldHandler);
if (trappedErrorCode != 0)
isAvailable = false;
}
}
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
}
}
return isAvailable;
}
}
#endif
//==============================================================================
#if JUCE_USE_XRENDER
namespace XRender
{
typedef Status (*tXRenderQueryVersion) (Display*, int*, int*);
typedef XRenderPictFormat* (*tXRenderFindStandardFormat) (Display*, int);
typedef XRenderPictFormat* (*tXRenderFindFormat) (Display*, unsigned long, XRenderPictFormat*, int);
typedef XRenderPictFormat* (*tXRenderFindVisualFormat) (Display*, Visual*);
static tXRenderQueryVersion xRenderQueryVersion = nullptr;
static tXRenderFindStandardFormat xRenderFindStandardFormat = nullptr;
static tXRenderFindFormat xRenderFindFormat = nullptr;
static tXRenderFindVisualFormat xRenderFindVisualFormat = nullptr;
static bool isAvailable()
{
static bool hasLoaded = false;
if (! hasLoaded)
{
ScopedXLock xlock;
if (void* h = dlopen ("libXrender.so", RTLD_GLOBAL | RTLD_NOW))
{
xRenderQueryVersion = (tXRenderQueryVersion) dlsym (h, "XRenderQueryVersion");
xRenderFindStandardFormat = (tXRenderFindStandardFormat) dlsym (h, "XRenderFindStandardFormat");
xRenderFindFormat = (tXRenderFindFormat) dlsym (h, "XRenderFindFormat");
xRenderFindVisualFormat = (tXRenderFindVisualFormat) dlsym (h, "XRenderFindVisualFormat");
}
if (xRenderQueryVersion != nullptr
&& xRenderFindStandardFormat != nullptr
&& xRenderFindFormat != nullptr
&& xRenderFindVisualFormat != nullptr)
{
int major, minor;
if (xRenderQueryVersion (display, &major, &minor))
return true;
}
}
xRenderQueryVersion = nullptr;
}
return xRenderQueryVersion != nullptr;
}
static bool hasCompositingWindowManager() noexcept
{
return display != nullptr
&& XGetSelectionOwner (display, Atoms::getCreating ("_NET_WM_CM_S0")) != 0;
}
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
static XRenderPictFormat* findPictureFormat()
{
ScopedXLock xlock;
XRenderPictFormat* pictFormat = nullptr;
if (isAvailable())
{
pictFormat = xRenderFindStandardFormat (display, PictStandardARGB32);
if (pictFormat == nullptr)
{
XRenderPictFormat desiredFormat;
desiredFormat.type = PictTypeDirect;
desiredFormat.depth = 32;
desiredFormat.direct.alphaMask = 0xff;
desiredFormat.direct.redMask = 0xff;
desiredFormat.direct.greenMask = 0xff;
desiredFormat.direct.blueMask = 0xff;
desiredFormat.direct.alpha = 24;
desiredFormat.direct.red = 16;
desiredFormat.direct.green = 8;
desiredFormat.direct.blue = 0;
pictFormat = xRenderFindFormat (display,
PictFormatType | PictFormatDepth
| PictFormatRedMask | PictFormatRed
| PictFormatGreenMask | PictFormatGreen
| PictFormatBlueMask | PictFormatBlue
| PictFormatAlphaMask | PictFormatAlpha,
&desiredFormat,
0);
}
}
return pictFormat;
}
}
#endif
//==============================================================================
namespace Visuals
{
static Visual* findVisualWithDepth (const int desiredDepth) noexcept
{
ScopedXLock xlock;
Visual* visual = nullptr;
int numVisuals = 0;
long desiredMask = VisualNoMask;
XVisualInfo desiredVisual;
desiredVisual.screen = DefaultScreen (display);
desiredVisual.depth = desiredDepth;
desiredMask = VisualScreenMask | VisualDepthMask;
if (desiredDepth == 32)
{
desiredVisual.c_class = TrueColor;
desiredVisual.red_mask = 0x00FF0000;
desiredVisual.green_mask = 0x0000FF00;
desiredVisual.blue_mask = 0x000000FF;
desiredVisual.bits_per_rgb = 8;
desiredMask |= VisualClassMask;
desiredMask |= VisualRedMaskMask;
desiredMask |= VisualGreenMaskMask;
desiredMask |= VisualBlueMaskMask;
desiredMask |= VisualBitsPerRGBMask;
}
if (XVisualInfo* xvinfos = XGetVisualInfo (display,
desiredMask,
&desiredVisual,
&numVisuals))
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
{
for (int i = 0; i < numVisuals; i++)
{
if (xvinfos[i].depth == desiredDepth)
{
visual = xvinfos[i].visual;
break;
}
}
XFree (xvinfos);
}
return visual;
}
static Visual* findVisualFormat (const int desiredDepth, int& matchedDepth) noexcept
{
Visual* visual = nullptr;
if (desiredDepth == 32)
{
#if JUCE_USE_XSHM
if (XSHMHelpers::isShmAvailable())
{
#if JUCE_USE_XRENDER
if (XRender::isAvailable())
{
if (XRenderPictFormat* pictFormat = XRender::findPictureFormat())
{
int numVisuals = 0;
XVisualInfo desiredVisual;
desiredVisual.screen = DefaultScreen (display);
desiredVisual.depth = 32;
desiredVisual.bits_per_rgb = 8;
if (XVisualInfo* xvinfos = XGetVisualInfo (display,
VisualScreenMask | VisualDepthMask | VisualBitsPerRGBMask,
&desiredVisual, &numVisuals))
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
{
for (int i = 0; i < numVisuals; ++i)
{
XRenderPictFormat* pictVisualFormat = XRender::xRenderFindVisualFormat (display, xvinfos[i].visual);
if (pictVisualFormat != nullptr
&& pictVisualFormat->type == PictTypeDirect
&& pictVisualFormat->direct.alphaMask)
{
visual = xvinfos[i].visual;
matchedDepth = 32;
break;
}
}
XFree (xvinfos);
}
}
}
#endif
if (visual == nullptr)
{
visual = findVisualWithDepth (32);
if (visual != nullptr)
matchedDepth = 32;
}
}
#endif
}
if (visual == nullptr && desiredDepth >= 24)
{
visual = findVisualWithDepth (24);
if (visual != nullptr)
matchedDepth = 24;
}
if (visual == nullptr && desiredDepth >= 16)
{
visual = findVisualWithDepth (16);
if (visual != nullptr)
matchedDepth = 16;
}
return visual;
}
}
//==============================================================================
class XBitmapImage : public ImagePixelData
{
public:
XBitmapImage (const Image::PixelFormat format, const int w, const int h,
const bool clearImage, const unsigned int imageDepth_, Visual* visual)
: ImagePixelData (format, w, h),
imageDepth (imageDepth_),
gc (None)
{
jassert (format == Image::RGB || format == Image::ARGB);
pixelStride = (format == Image::RGB) ? 3 : 4;
lineStride = ((w * pixelStride + 3) & ~3);
ScopedXLock xlock;
#if JUCE_USE_XSHM
usingXShm = false;
if ((imageDepth > 16) && XSHMHelpers::isShmAvailable())
{
zerostruct (segmentInfo);
segmentInfo.shmid = -1;
segmentInfo.shmaddr = (char *) -1;
segmentInfo.readOnly = False;
xImage = XShmCreateImage (display, visual, imageDepth, ZPixmap, 0,
&segmentInfo, (unsigned int) w, (unsigned int) h);
if (xImage != nullptr)
{
if ((segmentInfo.shmid = shmget (IPC_PRIVATE,
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
IPC_CREAT | 0777)) >= 0)
{
if (segmentInfo.shmid != -1)
{
segmentInfo.shmaddr = (char*) shmat (segmentInfo.shmid, 0, 0);
if (segmentInfo.shmaddr != (void*) -1)
{
segmentInfo.readOnly = False;
xImage->data = segmentInfo.shmaddr;
imageData = (uint8*) segmentInfo.shmaddr;
if (XShmAttach (display, &segmentInfo) != 0)
usingXShm = true;
else
jassertfalse;
}
else
{
shmctl (segmentInfo.shmid, IPC_RMID, 0);
}
}
}
}
}
imageDataAllocated.allocate ((size_t) (lineStride * h), format == Image::ARGB && clearImage);
imageData = imageDataAllocated;
xImage = (XImage*) ::calloc (1, sizeof (XImage));
xImage->width = w;
xImage->height = h;
xImage->xoffset = 0;
xImage->format = ZPixmap;
xImage->data = (char*) imageData;
xImage->byte_order = ImageByteOrder (display);
xImage->bitmap_unit = BitmapUnit (display);
xImage->bitmap_bit_order = BitmapBitOrder (display);
xImage->bitmap_pad = 32;
xImage->depth = pixelStride * 8;
xImage->bytes_per_line = lineStride;
xImage->bits_per_pixel = pixelStride * 8;
xImage->red_mask = 0x00FF0000;
xImage->green_mask = 0x0000FF00;
xImage->blue_mask = 0x000000FF;
if (imageDepth == 16)
{
const int pixStride = 2;
const int stride = ((w * pixStride + 3) & ~3);
xImage->data = imageData16Bit;
xImage->bitmap_pad = 16;
xImage->depth = pixStride * 8;
xImage->bytes_per_line = stride;
xImage->bits_per_pixel = pixStride * 8;
xImage->red_mask = visual->red_mask;
xImage->green_mask = visual->green_mask;
xImage->blue_mask = visual->blue_mask;
}
if (! XInitImage (xImage))
jassertfalse;
}
}
~XBitmapImage()
{
ScopedXLock xlock;
if (gc != None)
XFreeGC (display, gc);
#if JUCE_USE_XSHM
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
{
XShmDetach (display, &segmentInfo);
XFlush (display);
XDestroyImage (xImage);
shmdt (segmentInfo.shmaddr);
shmctl (segmentInfo.shmid, IPC_RMID, 0);
}
else
#endif
{
xImage->data = nullptr;
XDestroyImage (xImage);
}
}
LowLevelGraphicsContext* createLowLevelContext() override
{
sendDataChangeMessage();
return new LowLevelGraphicsSoftwareRenderer (Image (this));
}
void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode mode) override
{
bitmap.data = imageData + x * pixelStride + y * lineStride;
bitmap.pixelFormat = pixelFormat;
bitmap.lineStride = lineStride;
bitmap.pixelStride = pixelStride;
if (mode != Image::BitmapData::readOnly)
sendDataChangeMessage();
}
ImagePixelData* clone() override
{
jassertfalse;
return nullptr;
}
ImageType* createType() const override { return new NativeImageType(); }
void blitToWindow (Window window, int dx, int dy, unsigned int dw, unsigned int dh, int sx, int sy)
{
ScopedXLock xlock;
if (gc == None)
{
XGCValues gcvalues;
gcvalues.foreground = None;
gcvalues.background = None;
gcvalues.function = GXcopy;
gcvalues.plane_mask = AllPlanes;
gcvalues.clip_mask = None;
gcvalues.graphics_exposures = False;
gc = XCreateGC (display, window,
GCBackground | GCForeground | GCFunction | GCPlaneMask | GCClipMask | GCGraphicsExposures,
&gcvalues);
}
if (imageDepth == 16)
{
const uint32 rMask = (uint32) xImage->red_mask;
const uint32 gMask = (uint32) xImage->green_mask;
const uint32 bMask = (uint32) xImage->blue_mask;
const uint32 rShiftL = (uint32) jmax (0, getShiftNeeded (rMask));
const uint32 rShiftR = (uint32) jmax (0, -getShiftNeeded (rMask));
const uint32 gShiftL = (uint32) jmax (0, getShiftNeeded (gMask));
const uint32 gShiftR = (uint32) jmax (0, -getShiftNeeded (gMask));
const uint32 bShiftL = (uint32) jmax (0, getShiftNeeded (bMask));
const uint32 bShiftR = (uint32) jmax (0, -getShiftNeeded (bMask));
const Image::BitmapData srcData (Image (this), Image::BitmapData::readOnly);
{
const uint8* p = srcData.getPixelPointer (sx, y);
{
const PixelRGB* const pixel = (const PixelRGB*) p;
p += srcData.pixelStride;
XPutPixel (xImage, x, y,
(((((uint32) pixel->getRed()) << rShiftL) >> rShiftR) & rMask)
| (((((uint32) pixel->getGreen()) << gShiftL) >> gShiftR) & gMask)
| (((((uint32) pixel->getBlue()) << bShiftL) >> bShiftR) & bMask));
}
}
}
// blit results to screen.
#if JUCE_USE_XSHM
XShmPutImage (display, (::Drawable) window, gc, xImage, sx, sy, dx, dy, dw, dh, True);
else
#endif
XPutImage (display, (::Drawable) window, gc, xImage, sx, sy, dx, dy, dw, dh);
}
#if JUCE_USE_XSHM
bool isUsingXShm() const noexcept { return usingXShm; }
#endif
private:
//==============================================================================
XImage* xImage;
const unsigned int imageDepth;
HeapBlock<uint8> imageDataAllocated;
HeapBlock<char> imageData16Bit;
int pixelStride, lineStride;
uint8* imageData;
GC gc;
#if JUCE_USE_XSHM
XShmSegmentInfo segmentInfo;
bool usingXShm;
#endif
static int getShiftNeeded (const uint32 mask) noexcept
{
for (int i = 32; --i >= 0;)
if (((mask >> i) & 1) != 0)
return i - 7;
jassertfalse;
return 0;
}
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XBitmapImage)
};
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//==============================================================================
#if JUCE_USE_XRANDR
template <>
struct ContainerDeletePolicy<XRRScreenResources>
{
static void destroy (XRRScreenResources* object);
};
template <>
struct ContainerDeletePolicy<XRROutputInfo>
{
static void destroy (XRROutputInfo* object);
};
template <>
struct ContainerDeletePolicy<XRRCrtcInfo>
{
static void destroy (XRRCrtcInfo* object);
};
#endif
//==============================================================================
class DisplayGeometry
{
private:
//==============================================================================
DisplayGeometry (::Display* dpy, double masterScale)
{
jassert (instance == nullptr);
instance = this;
queryDisplayInfos (dpy, masterScale);
updatePositions();
}
public:
//==============================================================================
struct ExtendedInfo
{
// Unlike Desktop::Displays::Display, the following is in
// physical pixels, i.e. the area is not scaled
Rectangle<int> totalBounds;
// Usable bounds is the usable area in local coordinates
// with respect to the above totalBounds
Rectangle<int> usableBounds;
// top-left point of display in scaled coordinates. This
// is different from totalBounds.getTopLeft() / scale,
// because the neighbouring display may have a different
// scale factor
Point<int> topLeftScaled;
double dpi, scale;
bool isMain;
};
Array<ExtendedInfo> infos;
//==============================================================================
ExtendedInfo& findDisplayForRect (const Rectangle<int>& bounds, bool isScaledBounds)
{
int maxArea = -1;
ExtendedInfo* retval = nullptr;
for (int i = 0; i < infos.size(); ++i)
{
ExtendedInfo& dpy = infos.getReference (i);
Rectangle<int> displayBounds = dpy.totalBounds;
if (isScaledBounds)
displayBounds = (displayBounds.withZeroOrigin() / dpy.scale) + dpy.topLeftScaled;
displayBounds = displayBounds.getIntersection (bounds);
int area = displayBounds.getWidth() * displayBounds.getHeight();
if (area >= maxArea)
{
maxArea = area;
retval = &dpy;
}
}
return *retval;
}
ExtendedInfo& findDisplayForPoint (Point<int> pt, bool isScaledPoint)
{
int minDistance = (int) ((((unsigned int)(-1)) >> 1) - 1);
ExtendedInfo* retval = nullptr;
for (int i = 0; i < infos.size(); ++i)
{
ExtendedInfo& dpy = infos.getReference (i);
Rectangle<int> displayBounds = dpy.totalBounds;
if (isScaledPoint)
displayBounds = (displayBounds.withZeroOrigin() / dpy.scale) + dpy.topLeftScaled;
if (displayBounds.contains (pt))
return dpy;
int distance = displayBounds.getCentre().getDistanceFrom (pt);
if (distance <= minDistance)
{
minDistance = distance;
retval = &dpy;
}
}
return *retval;
}
//==============================================================================
static Rectangle<int> physicalToScaled (const Rectangle<int>& physicalBounds)
{
// first find with which display physicalBounds has the most overlap
ExtendedInfo& dpy = getInstance().findDisplayForRect (physicalBounds, false);
// convert to local screen bounds
Rectangle<int> retval = physicalBounds - dpy.totalBounds.getTopLeft();
// now we can safely scale the coordinates and convert to global again
return (retval / dpy.scale) + dpy.topLeftScaled;
}
static Rectangle<int> scaledToPhysical (const Rectangle<int>& scaledBounds)
{
// first find with which display physicalBounds has the most overlap
ExtendedInfo& dpy = getInstance().findDisplayForRect (scaledBounds, true);
// convert to local screen bounds
Rectangle<int> retval = scaledBounds - dpy.topLeftScaled;
// now we can safely scale the coordinates and convert to global again
return (retval * dpy.scale) + dpy.totalBounds.getTopLeft();
}
//==============================================================================
template <typename ValueType>
static Point<ValueType> physicalToScaled (const Point<ValueType>& physicalPoint)
{
ExtendedInfo& dpy = getInstance().findDisplayForPoint (physicalPoint.roundToInt(), false);
Point<ValueType> scaledTopLeft =
Point<ValueType> (dpy.topLeftScaled.getX(), dpy.topLeftScaled.getY());
Point<ValueType> physicalTopLeft =
Point<ValueType> (dpy.totalBounds.getX(), dpy.totalBounds.getY());
return ((physicalPoint - physicalTopLeft) / dpy.scale) + scaledTopLeft;
}
template <typename ValueType>
static Point<ValueType> scaledToPhysical (const Point<ValueType>& scaledPoint)
{
ExtendedInfo& dpy = getInstance().findDisplayForPoint (scaledPoint.roundToInt(), true);
Point<ValueType> scaledTopLeft =
Point<ValueType> (dpy.topLeftScaled.getX(), dpy.topLeftScaled.getY());
Point<ValueType> physicalTopLeft =
Point<ValueType> (dpy.totalBounds.getX(), dpy.totalBounds.getY());
return ((scaledPoint - scaledTopLeft) * dpy.scale) + physicalTopLeft;
}
//==============================================================================
static DisplayGeometry& getInstance()
{
jassert (instance != nullptr);
return *instance;
}
static DisplayGeometry& getOrCreateInstance (::Display* dpy, double masterScale)
{
if (instance == nullptr)
new DisplayGeometry (dpy, masterScale);
return getInstance();
}
private:
//==============================================================================
static DisplayGeometry* instance;
//==============================================================================
#if JUCE_USE_XINERAMA
static Array<XineramaScreenInfo> XineramaQueryDisplays (::Display* dpy)
{
typedef Bool (*tXineramaIsActive) (::Display*);
typedef XineramaScreenInfo* (*tXineramaQueryScreens) (::Display*, int*);
int major_opcode, first_event, first_error;
if (XQueryExtension (dpy, "XINERAMA", &major_opcode, &first_event, &first_error))
{
static void* libXinerama = nullptr;
static tXineramaIsActive isActiveFuncPtr = nullptr;
static tXineramaQueryScreens xineramaQueryScreens = nullptr;
if (libXinerama == nullptr)
{
libXinerama = dlopen ("libXinerama.so", RTLD_GLOBAL | RTLD_NOW);
if (libXinerama == nullptr)
libXinerama = dlopen ("libXinerama.so.1", RTLD_GLOBAL | RTLD_NOW);
if (libXinerama != nullptr)
{
isActiveFuncPtr = (tXineramaIsActive) dlsym (libXinerama, "XineramaIsActive");
xineramaQueryScreens = (tXineramaQueryScreens) dlsym (libXinerama, "XineramaQueryScreens");
}
}
if (isActiveFuncPtr != nullptr && xineramaQueryScreens != nullptr && isActiveFuncPtr (dpy) != 0)
{
int numScreens;
if (XineramaScreenInfo* xinfo = xineramaQueryScreens (dpy, &numScreens))
{
Array<XineramaScreenInfo> infos (xinfo, numScreens);
XFree (xinfo);
return infos;
}
}
}
return Array<XineramaScreenInfo>();
}
#endif
//==============================================================================
#if JUCE_USE_XRANDR
friend struct ContainerDeletePolicy<XRRScreenResources>;
friend struct ContainerDeletePolicy<XRROutputInfo>;
friend struct ContainerDeletePolicy<XRRCrtcInfo>;
class XRandrWrapper
{
private:
XRandrWrapper()
: libXrandr (nullptr),
getScreenResourcesPtr (nullptr),
freeScreenResourcesPtr (nullptr),
getOutputInfoPtr (nullptr),
freeOutputInfoPtr (nullptr),
getCrtcInfoPtr (nullptr),
freeCrtcInfoPtr (nullptr),
getOutputPrimaryPtr (nullptr)
{
if (libXrandr == nullptr)
{
libXrandr = dlopen ("libXrandr.so", RTLD_GLOBAL | RTLD_NOW);
if (libXrandr == nullptr)
libXrandr = dlopen ("libXinerama.so.2", RTLD_GLOBAL | RTLD_NOW);
if (libXrandr != nullptr)
{
getScreenResourcesPtr = (tXRRGetScreenResources) dlsym (libXrandr, "XRRGetScreenResources");
freeScreenResourcesPtr = (tXRRFreeScreenResources) dlsym (libXrandr, "XRRFreeScreenResources");
getOutputInfoPtr = (tXRRGetOutputInfo) dlsym (libXrandr, "XRRGetOutputInfo");
freeOutputInfoPtr = (tXRRFreeOutputInfo) dlsym (libXrandr, "XRRFreeOutputInfo");
getCrtcInfoPtr = (tXRRGetCrtcInfo) dlsym (libXrandr, "XRRGetCrtcInfo");
freeCrtcInfoPtr = (tXRRFreeCrtcInfo) dlsym (libXrandr, "XRRFreeCrtcInfo");
getOutputPrimaryPtr = (tXRRGetOutputPrimary) dlsym (libXrandr, "XRRGetOutputPrimary");
}
}