|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2017 - ROLI Ltd.
-
- JUCE is an open source library subject to commercial or open-source
- licensing.
-
- By using JUCE, you agree to the terms of both the JUCE 5 End-User License
- Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
- 27th April 2017).
-
- End User License Agreement: www.juce.com/juce-5-licence
- Privacy Policy: www.juce.com/juce-5-privacy-policy
-
- Or: You may also use this code under the terms of the GPL v3 (see
- www.gnu.org/licenses).
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- typedef void (*WindowMessageReceiveCallback) (XEvent&);
- WindowMessageReceiveCallback dispatchWindowMessage = nullptr;
-
- typedef void (*SelectionRequestCallback) (XSelectionRequestEvent&);
- SelectionRequestCallback handleSelectionRequest = nullptr;
-
- ::Window juce_messageWindowHandle;
-
- XContext windowHandleXContext;
-
- //==============================================================================
- namespace X11ErrorHandling
- {
- static XErrorHandler oldErrorHandler = (XErrorHandler) 0;
- static XIOErrorHandler oldIOErrorHandler = (XIOErrorHandler) 0;
-
- //==============================================================================
- // Usually happens when client-server connection is broken
- int ioErrorHandler (::Display*)
- {
- DBG ("ERROR: connection to X server broken.. terminating.");
-
- if (JUCEApplicationBase::isStandaloneApp())
- MessageManager::getInstance()->stopDispatchLoop();
-
- // set this somewhere
- // errorOccurred = true;
- return 0;
- }
-
-
- int errorHandler (::Display* display, XErrorEvent* event)
- {
- ignoreUnused (display, event);
-
- #if JUCE_DEBUG_XERRORS
- char errorStr[64] = { 0 };
- char requestStr[64] = { 0 };
-
- XGetErrorText (display, event->error_code, errorStr, 64);
- XGetErrorDatabaseText (display, "XRequest", String (event->request_code).toUTF8(), "Unknown", requestStr, 64);
- DBG ("ERROR: X returned " << errorStr << " for operation " << requestStr);
- #endif
-
- return 0;
- }
-
- void installXErrorHandlers()
- {
- oldIOErrorHandler = XSetIOErrorHandler (ioErrorHandler);
- oldErrorHandler = XSetErrorHandler (errorHandler);
- }
-
- void removeXErrorHandlers()
- {
- XSetIOErrorHandler (oldIOErrorHandler);
- oldIOErrorHandler = 0;
-
- XSetErrorHandler (oldErrorHandler);
- oldErrorHandler = 0;
- }
- }
-
- //==============================================================================
- XWindowSystem::XWindowSystem() noexcept
- : display (nullptr)
- {
- if (JUCEApplicationBase::isStandaloneApp())
- {
- // Initialise xlib for multiple thread support
- static bool initThreadCalled = false;
-
- if (! initThreadCalled)
- {
- if (! XInitThreads())
- {
- // This is fatal! Print error and closedown
- Logger::outputDebugString ("Failed to initialise xlib thread support.");
- Process::terminate();
- return;
- }
-
- initThreadCalled = true;
- }
-
- X11ErrorHandling::installXErrorHandlers();
- }
- }
-
- XWindowSystem::~XWindowSystem() noexcept
- {
- if (JUCEApplicationBase::isStandaloneApp())
- X11ErrorHandling::removeXErrorHandlers();
-
- clearSingletonInstance();
- }
-
-
- ::Display* XWindowSystem::displayRef() noexcept
- {
- if (++displayCount - 1 == 0)
- {
- String displayName (getenv ("DISPLAY"));
- if (displayName.isEmpty())
- displayName = ":0.0";
-
- display = XOpenDisplay (displayName.toUTF8());
-
- initialiseXDisplay();
- }
-
- return this->display;
- }
-
- ::Display* XWindowSystem::displayUnref() noexcept
- {
- jassert (display != nullptr);
- jassert (displayCount.get() > 0);
-
- if (--displayCount == 0)
- {
- destroyXDisplay();
- XCloseDisplay (display);
- display = nullptr;
- }
-
- return display;
- }
-
- void XWindowSystem::initialiseXDisplay() noexcept
- {
- // This is fatal! Print error and closedown
- if (display == nullptr)
- {
- Logger::outputDebugString ("Failed to connect to the X Server.");
- Process::terminate();
- }
-
- // Create a context to store user data associated with Windows we create
- windowHandleXContext = XUniqueContext();
-
- // We're only interested in client messages for this window, which are always sent
- XSetWindowAttributes swa;
- swa.event_mask = NoEventMask;
-
- // Create our message window (this will never be mapped)
- const int screen = DefaultScreen (display);
- juce_messageWindowHandle = XCreateWindow (display, RootWindow (display, screen),
- 0, 0, 1, 1, 0, 0, InputOnly,
- DefaultVisual (display, screen),
- CWEventMask, &swa);
-
- XSync (display, False);
-
- // Setup input event handler
- int fd = XConnectionNumber (display);
- LinuxEventLoop::setWindowSystemFd
- (fd,
- [this](int /*fd*/) {
- do
- {
- XEvent evt;
-
- {
- ScopedXLock xlock (display);
-
- if (! XPending (display))
- return false;
-
- XNextEvent (display, &evt);
- }
-
- if (evt.type == SelectionRequest && evt.xany.window == juce_messageWindowHandle
- && handleSelectionRequest != nullptr)
- handleSelectionRequest (evt.xselectionrequest);
- else if (evt.xany.window != juce_messageWindowHandle
- && dispatchWindowMessage != nullptr)
- dispatchWindowMessage (evt);
-
- } while (display != nullptr);
-
- return false;
- });
- }
-
- void XWindowSystem::destroyXDisplay() noexcept
- {
- ScopedXLock xlock (display);
-
- XDestroyWindow (display, juce_messageWindowHandle);
-
- juce_messageWindowHandle = 0;
-
- XSync (display, True);
-
- LinuxEventLoop::removeWindowSystemFd();
- }
-
- juce_ImplementSingleton (XWindowSystem)
-
- //==============================================================================
- ScopedXDisplay::ScopedXDisplay() : display (XWindowSystem::getInstance()->displayRef())
- {
- }
-
- ScopedXDisplay::~ScopedXDisplay()
- {
- XWindowSystem::getInstance()->displayUnref();
- }
-
- //==============================================================================
- ScopedXLock::ScopedXLock(::Display* d) : display (d)
- {
- if (display != nullptr) XLockDisplay (display);
- }
-
- ScopedXLock::~ScopedXLock()
- {
- if (display != nullptr) XUnlockDisplay (display);
- }
-
- //==============================================================================
- Atoms::Atoms(::Display* display)
- {
- protocols = getIfExists (display, "WM_PROTOCOLS");
- protocolList [TAKE_FOCUS] = getIfExists (display, "WM_TAKE_FOCUS");
- protocolList [DELETE_WINDOW] = getIfExists (display, "WM_DELETE_WINDOW");
- protocolList [PING] = getIfExists (display, "_NET_WM_PING");
- changeState = getIfExists (display, "WM_CHANGE_STATE");
- state = getIfExists (display, "WM_STATE");
- userTime = getCreating (display, "_NET_WM_USER_TIME");
- activeWin = getCreating (display, "_NET_ACTIVE_WINDOW");
- pid = getCreating (display, "_NET_WM_PID");
- windowType = getIfExists (display, "_NET_WM_WINDOW_TYPE");
- windowState = getIfExists (display, "_NET_WM_STATE");
-
- XdndAware = getCreating (display, "XdndAware");
- XdndEnter = getCreating (display, "XdndEnter");
- XdndLeave = getCreating (display, "XdndLeave");
- XdndPosition = getCreating (display, "XdndPosition");
- XdndStatus = getCreating (display, "XdndStatus");
- XdndDrop = getCreating (display, "XdndDrop");
- XdndFinished = getCreating (display, "XdndFinished");
- XdndSelection = getCreating (display, "XdndSelection");
-
- XdndTypeList = getCreating (display, "XdndTypeList");
- XdndActionList = getCreating (display, "XdndActionList");
- XdndActionCopy = getCreating (display, "XdndActionCopy");
- XdndActionPrivate = getCreating (display, "XdndActionPrivate");
- XdndActionDescription = getCreating (display, "XdndActionDescription");
-
- XembedMsgType = getCreating (display, "_XEMBED");
- XembedInfo = getCreating (display, "_XEMBED_INFO");
-
- allowedMimeTypes[0] = getCreating (display, "UTF8_STRING");
- allowedMimeTypes[1] = getCreating (display, "text/plain;charset=utf-8");
- allowedMimeTypes[2] = getCreating (display, "text/plain");
- allowedMimeTypes[3] = getCreating (display, "text/uri-list");
-
- allowedActions[0] = getCreating (display, "XdndActionMove");
- allowedActions[1] = XdndActionCopy;
- allowedActions[2] = getCreating (display, "XdndActionLink");
- allowedActions[3] = getCreating (display, "XdndActionAsk");
- allowedActions[4] = XdndActionPrivate;
- }
-
- Atom Atoms::getIfExists (::Display* display, const char* name)
- {
- return XInternAtom (display, name, True);
- }
-
- Atom Atoms::getCreating (::Display* display, const char* name)
- {
- return XInternAtom (display, name, False);
- }
-
- String Atoms::getName (::Display* display, const Atom atom)
- {
- if (atom == None)
- return "None";
-
- return String (XGetAtomName (display, atom));
- }
-
- bool Atoms::isMimeTypeFile (::Display* display, const Atom atom)
- {
- return getName (display, atom).equalsIgnoreCase ("text/uri-list");
- }
-
-
- const unsigned long Atoms::DndVersion = 3;
-
- //==============================================================================
- GetXProperty::GetXProperty (::Display* display, 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::~GetXProperty()
- {
- if (data != nullptr)
- XFree (data);
- }
|