diff options
Diffstat (limited to 'intern/ghost/intern/GHOST_SystemX11.cpp')
-rw-r--r-- | intern/ghost/intern/GHOST_SystemX11.cpp | 410 |
1 files changed, 230 insertions, 180 deletions
diff --git a/intern/ghost/intern/GHOST_SystemX11.cpp b/intern/ghost/intern/GHOST_SystemX11.cpp index 54d38e1c0f3..00cc5f3af8f 100644 --- a/intern/ghost/intern/GHOST_SystemX11.cpp +++ b/intern/ghost/intern/GHOST_SystemX11.cpp @@ -25,6 +25,7 @@ #ifdef WITH_INPUT_NDOF # include "GHOST_NDOFManagerUnix.h" #endif +#include "GHOST_utildefines.h" #ifdef WITH_XDND # include "GHOST_DropTargetX11.h" @@ -58,9 +59,9 @@ #include <sys/time.h> #include <unistd.h> +#include <cstdio> /* for fprintf only */ #include <cstdlib> /* for exit */ #include <iostream> -#include <stdio.h> /* for fprintf only */ #include <vector> /* For debugging, so we can break-point X11 errors. */ @@ -89,8 +90,8 @@ static GHOST_TKey ghost_key_from_keysym_or_keycode(const KeySym key, const KeyCode keycode); /* these are for copy and select copy */ -static char *txt_cut_buffer = NULL; -static char *txt_select_buffer = NULL; +static char *txt_cut_buffer = nullptr; +static char *txt_select_buffer = nullptr; #ifdef WITH_XWAYLAND_HACK static bool use_xwayland_hack = false; @@ -98,10 +99,10 @@ static bool use_xwayland_hack = false; using namespace std; -GHOST_SystemX11::GHOST_SystemX11() : GHOST_System(), m_xkb_descr(NULL), m_start_time(0) +GHOST_SystemX11::GHOST_SystemX11() : GHOST_System(), m_xkb_descr(nullptr), m_start_time(0) { XInitThreads(); - m_display = XOpenDisplay(NULL); + m_display = XOpenDisplay(nullptr); if (!m_display) { std::cerr << "Unable to open a display" << std::endl; @@ -117,7 +118,7 @@ GHOST_SystemX11::GHOST_SystemX11() : GHOST_System(), m_xkb_descr(NULL), m_start_ /* NOTE: Don't open connection to XIM server here, because the locale has to be * set before opening the connection but `setlocale()` has not been called yet. * the connection will be opened after entering the event loop. */ - m_xim = NULL; + m_xim = nullptr; #endif #define GHOST_INTERN_ATOM_IF_EXISTS(atom) \ @@ -165,7 +166,7 @@ GHOST_SystemX11::GHOST_SystemX11() : GHOST_System(), m_xkb_descr(NULL), m_start_ /* compute the initial time */ timeval tv; - if (gettimeofday(&tv, NULL) == -1) { + if (gettimeofday(&tv, nullptr) == -1) { GHOST_ASSERT(false, "Could not instantiate timer!"); } @@ -180,7 +181,7 @@ GHOST_SystemX11::GHOST_SystemX11() : GHOST_System(), m_xkb_descr(NULL), m_start_ use_xkb = XkbQueryExtension( m_display, &xkb_opcode, &xkb_event, &xkb_error, &xkb_major, &xkb_minor); if (use_xkb) { - XkbSetDetectableAutoRepeat(m_display, true, NULL); + XkbSetDetectableAutoRepeat(m_display, true, nullptr); m_xkb_descr = XkbGetMap(m_display, 0, XkbUseCoreKbd); if (m_xkb_descr) { @@ -190,7 +191,7 @@ GHOST_SystemX11::GHOST_SystemX11() : GHOST_System(), m_xkb_descr(NULL), m_start_ } #ifdef WITH_XWAYLAND_HACK - use_xwayland_hack = getenv("WAYLAND_DISPLAY") != NULL; + use_xwayland_hack = getenv("WAYLAND_DISPLAY") != nullptr; #endif #ifdef WITH_X11_XINPUT @@ -266,7 +267,7 @@ GHOST_TSuccess GHOST_SystemX11::init() uint64_t GHOST_SystemX11::getMilliSeconds() const { timeval tv; - if (gettimeofday(&tv, NULL) == -1) { + if (gettimeofday(&tv, nullptr) == -1) { GHOST_ASSERT(false, "Could not compute time!"); } @@ -334,10 +335,11 @@ GHOST_IWindow *GHOST_SystemX11::createWindow(const char *title, const bool is_dialog, const GHOST_IWindow *parentWindow) { - GHOST_WindowX11 *window = NULL; + GHOST_WindowX11 *window = nullptr; - if (!m_display) - return 0; + if (!m_display) { + return nullptr; + } window = new GHOST_WindowX11(this, m_display, @@ -367,7 +369,7 @@ GHOST_IWindow *GHOST_SystemX11::createWindow(const char *title, } else { delete window; - window = NULL; + window = nullptr; } } return window; @@ -395,7 +397,7 @@ GHOST_IContext *GHOST_SystemX11::createOffscreenContext(GHOST_GLSettings glSetti #if defined(WITH_GL_PROFILE_CORE) { const char *version_major = (char *)glewGetString(GLEW_VERSION_MAJOR); - if (version_major != NULL && version_major[0] == '1') { + if (version_major != nullptr && version_major[0] == '1') { fprintf(stderr, "Error: GLEW version 2.0 and above is required.\n"); abort(); } @@ -438,9 +440,9 @@ GHOST_IContext *GHOST_SystemX11::createOffscreenContext(GHOST_GLSettings glSetti EGL_OPENGL_API); #else context = new GHOST_ContextGLX(false, - (Window)NULL, + (Window) nullptr, m_display, - (GLXFBConfig)NULL, + (GLXFBConfig) nullptr, profile_mask, 4, minor, @@ -449,10 +451,10 @@ GHOST_IContext *GHOST_SystemX11::createOffscreenContext(GHOST_GLSettings glSetti GHOST_OPENGL_GLX_RESET_NOTIFICATION_STRATEGY); #endif - if (context->initializeDrawingContext()) + if (context->initializeDrawingContext()) { return context; - else - delete context; + } + delete context; } #if defined(WITH_GL_EGL) @@ -469,9 +471,9 @@ GHOST_IContext *GHOST_SystemX11::createOffscreenContext(GHOST_GLSettings glSetti EGL_OPENGL_API); #else context = new GHOST_ContextGLX(false, - (Window)NULL, + (Window) nullptr, m_display, - (GLXFBConfig)NULL, + (GLXFBConfig) nullptr, profile_mask, 3, 3, @@ -480,12 +482,12 @@ GHOST_IContext *GHOST_SystemX11::createOffscreenContext(GHOST_GLSettings glSetti GHOST_OPENGL_GLX_RESET_NOTIFICATION_STRATEGY); #endif - if (context->initializeDrawingContext()) + if (context->initializeDrawingContext()) { return context; - else - delete context; + } + delete context; - return NULL; + return nullptr; } /** @@ -505,8 +507,9 @@ static void destroyIMCallback(XIM /*xim*/, XPointer ptr, XPointer /*data*/) { GHOST_PRINT("XIM server died\n"); - if (ptr) - *(XIM *)ptr = NULL; + if (ptr) { + *(XIM *)ptr = nullptr; + } } bool GHOST_SystemX11::openX11_IM() @@ -517,14 +520,15 @@ bool GHOST_SystemX11::openX11_IM() /* set locale modifiers such as `@im=ibus` specified by XMODIFIERS. */ XSetLocaleModifiers(""); - m_xim = XOpenIM(m_display, NULL, (char *)GHOST_X11_RES_NAME, (char *)GHOST_X11_RES_CLASS); - if (!m_xim) + m_xim = XOpenIM(m_display, nullptr, (char *)GHOST_X11_RES_NAME, (char *)GHOST_X11_RES_CLASS); + if (!m_xim) { return false; + } XIMCallback destroy; destroy.callback = (XIMProc)destroyIMCallback; destroy.client_data = (XPointer)&m_xim; - XSetIMValues(m_xim, XNDestroyCallback, &destroy, NULL); + XSetIMValues(m_xim, XNDestroyCallback, &destroy, nullptr); return true; } #endif @@ -532,8 +536,9 @@ bool GHOST_SystemX11::openX11_IM() GHOST_WindowX11 *GHOST_SystemX11::findGhostWindow(Window xwind) const { - if (xwind == 0) - return NULL; + if (xwind == 0) { + return nullptr; + } /* It is not entirely safe to do this as the backptr may point * to a window that has recently been removed. @@ -551,7 +556,7 @@ GHOST_WindowX11 *GHOST_SystemX11::findGhostWindow(Window xwind) const return window; } } - return NULL; + return nullptr; } static void SleepTillEvent(Display *display, int64_t maxSleep) @@ -563,7 +568,7 @@ static void SleepTillEvent(Display *display, int64_t maxSleep) FD_SET(fd, &fds); if (maxSleep == -1) { - select(fd + 1, &fds, NULL, NULL, NULL); + select(fd + 1, &fds, nullptr, nullptr, nullptr); } else { timeval tv; @@ -571,7 +576,7 @@ static void SleepTillEvent(Display *display, int64_t maxSleep) tv.tv_sec = maxSleep / 1000; tv.tv_usec = (maxSleep - tv.tv_sec * 1000) * 1000; - select(fd + 1, &fds, NULL, NULL, &tv); + select(fd + 1, &fds, nullptr, nullptr, &tv); } } @@ -639,10 +644,11 @@ bool GHOST_SystemX11::processEvents(bool waitForEvent) SleepTillEvent(m_display, -1); } else { - int64_t maxSleep = next - getMilliSeconds(); + const int64_t maxSleep = next - getMilliSeconds(); - if (maxSleep >= 0) + if (maxSleep >= 0) { SleepTillEvent(m_display, next - getMilliSeconds()); + } } } @@ -658,7 +664,7 @@ bool GHOST_SystemX11::processEvents(bool waitForEvent) /* open connection to XIM server and create input context (XIC) * when receiving the first FocusIn or KeyPress event after startup, * or recover XIM and XIC when the XIM server has been restarted */ - if (xevent.type == FocusIn || xevent.type == KeyPress) { + if (ELEM(xevent.type, FocusIn, KeyPress)) { if (!m_xim && openX11_IM()) { GHOST_PRINT("Connected to XIM server\n"); } @@ -677,7 +683,7 @@ bool GHOST_SystemX11::processEvents(bool waitForEvent) } /* dispatch event to XIM server */ - if ((XFilterEvent(&xevent, (Window)NULL) == True)) { + if ((XFilterEvent(&xevent, (Window) nullptr) == True)) { /* do nothing now, the event is consumed by XIM. */ continue; } @@ -690,8 +696,9 @@ bool GHOST_SystemX11::processEvents(bool waitForEvent) } else if (xevent.type == KeyPress) { if ((xevent.xkey.keycode == m_last_release_keycode) && - ((xevent.xkey.time <= m_last_release_time))) + ((xevent.xkey.time <= m_last_release_time))) { continue; + } } processEvent(&xevent); @@ -718,9 +725,9 @@ bool GHOST_SystemX11::processEvents(bool waitForEvent) * in order to confirm the window is active. */ XPeekEvent(m_display, &xev_next); - if (xev_next.type == KeyPress || xev_next.type == KeyRelease) { - /* XK_Hyper_L/R currently unused */ - const static KeySym modifiers[8] = { + if (ELEM(xev_next.type, KeyPress, KeyRelease)) { + /* XK_Hyper_L/R currently unused. */ + const static KeySym modifiers[] = { XK_Shift_L, XK_Shift_R, XK_Control_L, @@ -731,16 +738,15 @@ bool GHOST_SystemX11::processEvents(bool waitForEvent) XK_Super_R, }; - for (int i = 0; i < (sizeof(modifiers) / sizeof(*modifiers)); i++) { + for (int i = 0; i < (int)ARRAY_SIZE(modifiers); i++) { KeyCode kc = XKeysymToKeycode(m_display, modifiers[i]); if (kc != 0 && ((xevent.xkeymap.key_vector[kc >> 3] >> (kc & 7)) & 1) != 0) { pushEvent(new GHOST_EventKey(getMilliSeconds(), GHOST_kEventKeyDown, window, ghost_key_from_keysym(modifiers[i]), - '\0', - NULL, - false)); + false, + nullptr)); } } } @@ -773,7 +779,7 @@ static bool checkTabletProximity(Display *display, XDevice *device) /* see: state.c from xinput, to get more data out of the device */ XDeviceState *state; - if (device == NULL) { + if (device == nullptr) { return false; } @@ -812,17 +818,17 @@ static bool checkTabletProximity(Display *display, XDevice *device) void GHOST_SystemX11::processEvent(XEvent *xe) { GHOST_WindowX11 *window = findGhostWindow(xe->xany.window); - GHOST_Event *g_event = NULL; + GHOST_Event *g_event = nullptr; /* Detect auto-repeat. */ bool is_repeat = false; - if (xe->type == KeyPress || xe->type == KeyRelease) { + if (ELEM(xe->type, KeyPress, KeyRelease)) { XKeyEvent *xke = &(xe->xkey); /* Set to true if this key will repeat. */ bool is_repeat_keycode = false; - if (m_xkb_descr != NULL) { + if (m_xkb_descr != nullptr) { /* Use XKB support. */ is_repeat_keycode = ( /* Should always be true, check just in case. */ @@ -883,9 +889,11 @@ void GHOST_SystemX11::processEvent(XEvent *xe) if (xe->type == xi_presence) { XDevicePresenceNotifyEvent *notify_event = (XDevicePresenceNotifyEvent *)xe; - if ((notify_event->devchange == DeviceEnabled) || - (notify_event->devchange == DeviceDisabled) || - (notify_event->devchange == DeviceAdded) || (notify_event->devchange == DeviceRemoved)) { + if (ELEM(notify_event->devchange, + DeviceEnabled, + DeviceDisabled, + DeviceAdded, + DeviceRemoved)) { refreshXInputDevices(); /* update all window events */ @@ -954,8 +962,9 @@ void GHOST_SystemX11::processEvent(XEvent *xe) GHOST_Rect bounds; /* fallback to window bounds */ - if (window->getCursorGrabBounds(bounds) == GHOST_kFailure) + if (window->getCursorGrabBounds(bounds) == GHOST_kFailure) { window->getClientBounds(bounds); + } /* Could also clamp to screen bounds wrap with a window outside the view will * fail at the moment. Use offset of 8 in case the window is at screen bounds. */ @@ -1007,7 +1016,9 @@ void GHOST_SystemX11::processEvent(XEvent *xe) case KeyRelease: { XKeyEvent *xke = &(xe->xkey); KeySym key_sym; + char *utf8_buf = nullptr; char ascii; + #if defined(WITH_X11_XINPUT) && defined(X_HAVE_UTF8_STRING) /* utf8_array[] is initial buffer used for Xutf8LookupString(). * if the length of the utf8 string exceeds this array, allocate @@ -1016,12 +1027,10 @@ void GHOST_SystemX11::processEvent(XEvent *xe) * at the end of this buffer when the constructor of GHOST_EventKey * reads 6 bytes regardless of the effective data length. */ char utf8_array[16 * 6 + 5]; /* 16 utf8 characters */ - char *utf8_buf = utf8_array; - int len = 1; /* at least one null character will be stored */ + int len = 1; /* at least one null character will be stored */ #else - char *utf8_buf = NULL; + char utf8_array[sizeof(GHOST_TEventKeyData::utf8_buf)] = {'\0'}; #endif - GHOST_TEventType type = (xke->type == KeyPress) ? GHOST_kEventKeyDown : GHOST_kEventKeyUp; GHOST_TKey gkey; @@ -1065,7 +1074,7 @@ void GHOST_SystemX11::processEvent(XEvent *xe) key_sym = XLookupKeysym(xke, 0); } - if (!XLookupString(xke, &ascii, 1, &key_sym_str, NULL)) { + if (!XLookupString(xke, &ascii, 1, &key_sym_str, nullptr)) { ascii = '\0'; } @@ -1135,87 +1144,100 @@ void GHOST_SystemX11::processEvent(XEvent *xe) gkey = ghost_key_from_keysym_or_keycode(key_sym, m_xkb_descr, xke->keycode); - if (!XLookupString(xke, &ascii, 1, NULL, NULL)) { + if (!XLookupString(xke, &ascii, 1, nullptr, nullptr)) { ascii = '\0'; } #endif #if defined(WITH_X11_XINPUT) && defined(X_HAVE_UTF8_STRING) - /* Setting unicode on key-up events gives #XLookupNone status. */ - XIC xic = window->getX11_XIC(); - if (xic && xke->type == KeyPress) { - Status status; + /* Only used for key-press. */ + XIC xic = nullptr; +#endif - /* Use utf8 because its not locale repentant, from XORG docs. */ - if (!(len = Xutf8LookupString( - xic, xke, utf8_buf, sizeof(utf8_array) - 5, &key_sym, &status))) { - utf8_buf[0] = '\0'; - } + if (xke->type == KeyPress) { + utf8_buf = utf8_array; +#if defined(WITH_X11_XINPUT) && defined(X_HAVE_UTF8_STRING) + /* Setting unicode on key-up events gives #XLookupNone status. */ + xic = window->getX11_XIC(); + if (xic) { + Status status; + + /* Use utf8 because its not locale repentant, from XORG docs. */ + if (!(len = Xutf8LookupString( + xic, xke, utf8_buf, sizeof(utf8_array) - 5, &key_sym, &status))) { + utf8_buf[0] = '\0'; + } - if (status == XBufferOverflow) { - utf8_buf = (char *)malloc(len + 5); - len = Xutf8LookupString(xic, xke, utf8_buf, len, &key_sym, &status); - } + if (status == XBufferOverflow) { + utf8_buf = (char *)malloc(len + 5); + len = Xutf8LookupString(xic, xke, utf8_buf, len, &key_sym, &status); + } - if ((status == XLookupChars || status == XLookupBoth)) { - if ((unsigned char)utf8_buf[0] >= 32) { /* not an ascii control character */ - /* do nothing for now, this is valid utf8 */ + if (ELEM(status, XLookupChars, XLookupBoth)) { + if ((unsigned char)utf8_buf[0] >= 32) { /* not an ascii control character */ + /* do nothing for now, this is valid utf8 */ + } + else { + utf8_buf[0] = '\0'; + } + } + else if (status == XLookupKeySym) { + /* this key doesn't have a text representation, it is a command + * key of some sort */ } else { - utf8_buf[0] = '\0'; + printf("Bad keycode lookup. Keysym 0x%x Status: %s\n", + (unsigned int)key_sym, + (status == XLookupNone ? "XLookupNone" : + status == XLookupKeySym ? "XLookupKeySym" : + "Unknown status")); + + printf("'%.*s' %p %p\n", len, utf8_buf, xic, m_xim); } } - else if (status == XLookupKeySym) { - /* this key doesn't have a text representation, it is a command - * key of some sort */ - } else { - printf("Bad keycode lookup. Keysym 0x%x Status: %s\n", - (unsigned int)key_sym, - (status == XLookupNone ? "XLookupNone" : - status == XLookupKeySym ? "XLookupKeySym" : - "Unknown status")); - - printf("'%.*s' %p %p\n", len, utf8_buf, xic, m_xim); + utf8_buf[0] = '\0'; } - } - else { - utf8_buf[0] = '\0'; - } #endif + if (!utf8_buf[0] && ascii) { + utf8_buf[0] = ascii; + utf8_buf[1] = '\0'; + } + } - g_event = new GHOST_EventKey( - getMilliSeconds(), type, window, gkey, ascii, utf8_buf, is_repeat); + g_event = new GHOST_EventKey(getMilliSeconds(), type, window, gkey, is_repeat, utf8_buf); #if defined(WITH_X11_XINPUT) && defined(X_HAVE_UTF8_STRING) /* when using IM for some languages such as Japanese, * one event inserts multiple utf8 characters */ - if (xic && xke->type == KeyPress) { + if (xke->type == KeyPress && xic) { unsigned char c; int i = 0; - while (1) { - /* search character boundary */ - if ((unsigned char)utf8_buf[i++] > 0x7f) { + while (true) { + /* Search character boundary. */ + if ((uchar)utf8_buf[i++] > 0x7f) { for (; i < len; ++i) { c = utf8_buf[i]; - if (c < 0x80 || c > 0xbf) + if (c < 0x80 || c > 0xbf) { break; + } } } - if (i >= len) + if (i >= len) { break; - - /* enqueue previous character */ + } + /* Enqueue previous character. */ pushEvent(g_event); g_event = new GHOST_EventKey( - getMilliSeconds(), type, window, gkey, '\0', &utf8_buf[i], is_repeat); + getMilliSeconds(), type, window, gkey, is_repeat, &utf8_buf[i]); } } - if (utf8_buf != utf8_array) + if (utf8_buf != utf8_array) { free(utf8_buf); + } #endif break; @@ -1224,42 +1246,52 @@ void GHOST_SystemX11::processEvent(XEvent *xe) case ButtonPress: case ButtonRelease: { XButtonEvent &xbe = xe->xbutton; - GHOST_TButtonMask gbmask = GHOST_kButtonMaskLeft; + GHOST_TButton gbmask = GHOST_kButtonMaskLeft; GHOST_TEventType type = (xbe.type == ButtonPress) ? GHOST_kEventButtonDown : GHOST_kEventButtonUp; /* process wheel mouse events and break, only pass on press events */ if (xbe.button == Button4) { - if (xbe.type == ButtonPress) + if (xbe.type == ButtonPress) { g_event = new GHOST_EventWheel(getMilliSeconds(), window, 1); + } break; } - else if (xbe.button == Button5) { - if (xbe.type == ButtonPress) + if (xbe.button == Button5) { + if (xbe.type == ButtonPress) { g_event = new GHOST_EventWheel(getMilliSeconds(), window, -1); + } break; } /* process rest of normal mouse buttons */ - if (xbe.button == Button1) + if (xbe.button == Button1) { gbmask = GHOST_kButtonMaskLeft; - else if (xbe.button == Button2) + } + else if (xbe.button == Button2) { gbmask = GHOST_kButtonMaskMiddle; - else if (xbe.button == Button3) + } + else if (xbe.button == Button3) { gbmask = GHOST_kButtonMaskRight; - /* It seems events 6 and 7 are for horizontal scrolling. - * you can re-order button mapping like this... (swaps 6,7 with 8,9) - * `xmodmap -e "pointer = 1 2 3 4 5 8 9 6 7"` */ - else if (xbe.button == 6) + /* It seems events 6 and 7 are for horizontal scrolling. + * you can re-order button mapping like this... (swaps 6,7 with 8,9) + * `xmodmap -e "pointer = 1 2 3 4 5 8 9 6 7"` */ + } + else if (xbe.button == 6) { gbmask = GHOST_kButtonMaskButton6; - else if (xbe.button == 7) + } + else if (xbe.button == 7) { gbmask = GHOST_kButtonMaskButton7; - else if (xbe.button == 8) + } + else if (xbe.button == 8) { gbmask = GHOST_kButtonMaskButton4; - else if (xbe.button == 9) + } + else if (xbe.button == 9) { gbmask = GHOST_kButtonMaskButton5; - else + } + else { break; + } g_event = new GHOST_EventButton( getMilliSeconds(), type, window, gbmask, window->GetTabletData()); @@ -1323,8 +1355,9 @@ void GHOST_SystemX11::processEvent(XEvent *xe) if (XGetWindowAttributes(m_display, xcme.window, &attr) == True) { if (XGetInputFocus(m_display, &fwin, &revert_to) == True) { if (attr.map_state == IsViewable) { - if (fwin != xcme.window) + if (fwin != xcme.window) { XSetInputFocus(m_display, xcme.window, RevertToParent, xcme.data.l[1]); + } } } } @@ -1373,10 +1406,12 @@ void GHOST_SystemX11::processEvent(XEvent *xe) // printf("X: %s window %d\n", // xce.type == EnterNotify ? "entering" : "leaving", (int) xce.window); - if (xce.type == EnterNotify) + if (xce.type == EnterNotify) { m_windowManager->setActiveWindow(window); - else + } + else { m_windowManager->setWindowInactive(window); + } break; } @@ -1429,8 +1464,7 @@ void GHOST_SystemX11::processEvent(XEvent *xe) nxe.xselection.time = xse->time; /* Check to see if the requester is asking for String */ - if (xse->target == utf8_string || xse->target == string || xse->target == compound_text || - xse->target == c_string) { + if (ELEM(xse->target, utf8_string, string, compound_text, c_string)) { if (xse->selection == XInternAtom(m_display, "PRIMARY", False)) { XChangeProperty(m_display, xse->requestor, @@ -1483,7 +1517,7 @@ void GHOST_SystemX11::processEvent(XEvent *xe) default: { #ifdef WITH_X11_XINPUT for (GHOST_TabletX11 &xtablet : m_xtablets) { - if (xe->type == xtablet.MotionEvent || xe->type == xtablet.PressEvent) { + if (ELEM(xe->type, xtablet.MotionEvent, xtablet.PressEvent)) { XDeviceMotionEvent *data = (XDeviceMotionEvent *)xe; if (data->deviceid != xtablet.ID) { continue; @@ -1642,10 +1676,10 @@ static GHOST_TSuccess getCursorPosition_impl(Display *display, &mask_return) == False) { return GHOST_kFailure; } - else { - x = rx; - y = ry; - } + + x = rx; + y = ry; + return GHOST_kSuccess; } @@ -1715,7 +1749,7 @@ GHOST_TSuccess GHOST_SystemX11::setCursorPosition(int32_t x, int32_t y) void GHOST_SystemX11::addDirtyWindow(GHOST_WindowX11 *bad_wind) { - GHOST_ASSERT((bad_wind != NULL), "addDirtyWindow() NULL ptr trapped (window)"); + GHOST_ASSERT((bad_wind != nullptr), "addDirtyWindow() nullptr ptr trapped (window)"); m_dirty_windows.push_back(bad_wind); } @@ -1952,18 +1986,19 @@ void GHOST_SystemX11::getClipboard_xcout(const XEvent *evt, return; case XCLIB_XCOUT_SENTCONVSEL: - if (evt->type != SelectionNotify) + if (evt->type != SelectionNotify) { return; + } if (target == m_atom.UTF8_STRING && evt->xselection.property == None) { *context = XCLIB_XCOUT_FALLBACK_UTF8; return; } - else if (target == m_atom.COMPOUND_TEXT && evt->xselection.property == None) { + if (target == m_atom.COMPOUND_TEXT && evt->xselection.property == None) { *context = XCLIB_XCOUT_FALLBACK_COMP; return; } - else if (target == m_atom.TEXT && evt->xselection.property == None) { + if (target == m_atom.TEXT && evt->xselection.property == None) { *context = XCLIB_XCOUT_FALLBACK_TEXT; return; } @@ -2039,12 +2074,14 @@ void GHOST_SystemX11::getClipboard_xcout(const XEvent *evt, * then read it, delete it, etc. */ /* make sure that the event is relevant */ - if (evt->type != PropertyNotify) + if (evt->type != PropertyNotify) { return; + } /* skip unless the property has a new value */ - if (evt->xproperty.state != PropertyNewValue) + if (evt->xproperty.state != PropertyNewValue) { return; + } /* check size and format of the property */ XGetWindowProperty(m_display, @@ -2118,7 +2155,6 @@ void GHOST_SystemX11::getClipboard_xcout(const XEvent *evt, XFlush(m_display); return; } - return; } char *GHOST_SystemX11::getClipboard(bool selection) const @@ -2133,10 +2169,12 @@ char *GHOST_SystemX11::getClipboard(bool selection) const XEvent evt; unsigned int context = XCLIB_XCOUT_NONE; - if (selection == True) + if (selection == True) { sseln = m_atom.PRIMARY; - else + } + else { sseln = m_atom.CLIPBOARD; + } const vector<GHOST_IWindow *> &win_vec = m_windowManager->getWindows(); vector<GHOST_IWindow *>::const_iterator win_it = win_vec.begin(); @@ -2151,19 +2189,18 @@ char *GHOST_SystemX11::getClipboard(bool selection) const strcpy(sel_buf, txt_cut_buffer); return sel_buf; } - else { - sel_buf = (char *)malloc(strlen(txt_select_buffer) + 1); - strcpy(sel_buf, txt_select_buffer); - return sel_buf; - } + sel_buf = (char *)malloc(strlen(txt_select_buffer) + 1); + strcpy(sel_buf, txt_select_buffer); + return sel_buf; + } + if (owner == None) { + return nullptr; } - else if (owner == None) - return NULL; /* Restore events so copy doesn't swallow other event types (keyboard/mouse). */ vector<XEvent> restore_events; - while (1) { + while (true) { /* only get an event if xcout() is doing something */ bool restore_this_event = false; if (context != XCLIB_XCOUT_NONE) { @@ -2184,26 +2221,27 @@ char *GHOST_SystemX11::getClipboard(bool selection) const target = m_atom.STRING; continue; } - else if (context == XCLIB_XCOUT_FALLBACK_UTF8) { + if (context == XCLIB_XCOUT_FALLBACK_UTF8) { /* utf8 fail, move to compound text. */ context = XCLIB_XCOUT_NONE; target = m_atom.COMPOUND_TEXT; continue; } - else if (context == XCLIB_XCOUT_FALLBACK_COMP) { + if (context == XCLIB_XCOUT_FALLBACK_COMP) { /* Compound text fail, move to text. */ context = XCLIB_XCOUT_NONE; target = m_atom.TEXT; continue; } - else if (context == XCLIB_XCOUT_FALLBACK_TEXT) { + if (context == XCLIB_XCOUT_FALLBACK_TEXT) { /* Text fail, nothing else to try, break. */ context = XCLIB_XCOUT_NONE; } /* Only continue if #xcout() is doing something. */ - if (context == XCLIB_XCOUT_NONE) + if (context == XCLIB_XCOUT_NONE) { break; + } } while (!restore_events.empty()) { @@ -2217,14 +2255,16 @@ char *GHOST_SystemX11::getClipboard(bool selection) const memcpy(tmp_data, (char *)sel_buf, sel_len); tmp_data[sel_len] = '\0'; - if (sseln == m_atom.STRING) + if (sseln == m_atom.STRING) { XFree(sel_buf); - else + } + else { free(sel_buf); + } return tmp_data; } - return NULL; + return nullptr; } void GHOST_SystemX11::putClipboard(const char *buffer, bool selection) const @@ -2240,8 +2280,9 @@ void GHOST_SystemX11::putClipboard(const char *buffer, bool selection) const if (selection == False) { XSetSelectionOwner(m_display, m_atom.CLIPBOARD, m_window, CurrentTime); owner = XGetSelectionOwner(m_display, m_atom.CLIPBOARD); - if (txt_cut_buffer) + if (txt_cut_buffer) { free((void *)txt_cut_buffer); + } txt_cut_buffer = (char *)malloc(strlen(buffer) + 1); strcpy(txt_cut_buffer, buffer); @@ -2249,15 +2290,17 @@ void GHOST_SystemX11::putClipboard(const char *buffer, bool selection) const else { XSetSelectionOwner(m_display, m_atom.PRIMARY, m_window, CurrentTime); owner = XGetSelectionOwner(m_display, m_atom.PRIMARY); - if (txt_select_buffer) + if (txt_select_buffer) { free((void *)txt_select_buffer); + } txt_select_buffer = (char *)malloc(strlen(buffer) + 1); strcpy(txt_select_buffer, buffer); } - if (owner != m_window) + if (owner != m_window) { fprintf(stderr, "failed to own primary\n"); + } } } @@ -2352,14 +2395,16 @@ static void split(const char *text, const char *seps, char ***str, int *count) *count = 0; data = strdup(text); - for (tok = strtok(data, seps); tok != NULL; tok = strtok(NULL, seps)) + for (tok = strtok(data, seps); tok != nullptr; tok = strtok(nullptr, seps)) { (*count)++; + } free(data); data = strdup(text); *str = (char **)malloc((size_t)(*count) * sizeof(char *)); - for (i = 0, tok = strtok(data, seps); tok != NULL; tok = strtok(NULL, seps), i++) + for (i = 0, tok = strtok(data, seps); tok != nullptr; tok = strtok(nullptr, seps), i++) { (*str)[i] = strdup(tok); + } free(data); } @@ -2368,9 +2413,9 @@ GHOST_TSuccess GHOST_SystemX11::showMessageBox(const char *title, const char *help_label, const char *continue_label, const char *link, - GHOST_DialogOptions) const + GHOST_DialogOptions /*dialog_options*/) const { - char **text_splitted = NULL; + char **text_splitted = nullptr; int textLines = 0; split(message, "\n", &text_splitted, &textLines); @@ -2433,7 +2478,7 @@ GHOST_TSuccess GHOST_SystemX11::showMessageBox(const char *title, XSelectInput(m_display, window, ExposureMask | ButtonPressMask | ButtonReleaseMask); XMapWindow(m_display, window); - while (1) { + while (true) { XNextEvent(m_display, &e); if (e.type == Expose) { for (int i = 0; i < textLines; i++) { @@ -2454,7 +2499,7 @@ GHOST_TSuccess GHOST_SystemX11::showMessageBox(const char *title, if (dialog_data.isInsideButton(e, 1)) { break; } - else if (dialog_data.isInsideButton(e, 2)) { + if (dialog_data.isInsideButton(e, 2)) { if (strlen(link)) { string cmd = "xdg-open \"" + string(link) + "\""; if (system(cmd.c_str()) != 0) { @@ -2543,7 +2588,7 @@ int GHOST_X11_ApplicationIOErrorHandler(Display * /*display*/) static bool is_filler_char(char c) { - return isspace(c) || c == '_' || c == '-' || c == ';' || c == ':'; + return isspace(c) || ELEM(c, '_', '-', ';', ':'); } /* These C functions are copied from Wine 3.12's `wintab.c` */ @@ -2551,18 +2596,23 @@ static bool match_token(const char *haystack, const char *needle) { const char *h, *n; for (h = haystack; *h;) { - while (*h && is_filler_char(*h)) + while (*h && is_filler_char(*h)) { h++; - if (!*h) + } + if (!*h) { break; + } - for (n = needle; *n && *h && tolower(*h) == tolower(*n); n++) + for (n = needle; *n && *h && tolower(*h) == tolower(*n); n++) { h++; - if (!*n && (is_filler_char(*h) || !*h)) + } + if (!*n && (is_filler_char(*h) || !*h)) { return true; + } - while (*h && !is_filler_char(*h)) + while (*h && !is_filler_char(*h)) { h++; + } } return false; } @@ -2580,19 +2630,19 @@ static bool match_token(const char *haystack, const char *needle) static GHOST_TTabletMode tablet_mode_from_name(const char *name, const char *type) { int i; - static const char *tablet_stylus_whitelist[] = {"stylus", "wizardpen", "acecad", "pen", NULL}; + static const char *tablet_stylus_whitelist[] = {"stylus", "wizardpen", "acecad", "pen", nullptr}; - static const char *type_blacklist[] = {"pad", "cursor", "touch", NULL}; + static const char *type_blacklist[] = {"pad", "cursor", "touch", nullptr}; /* Skip some known unsupported types. */ - for (i = 0; type_blacklist[i] != NULL; i++) { + for (i = 0; type_blacklist[i] != nullptr; i++) { if (type && (strcasecmp(type, type_blacklist[i]) == 0)) { return GHOST_kTabletModeNone; } } /* First check device type to avoid cases where name is "Pen and Eraser" and type is "ERASER" */ - for (i = 0; tablet_stylus_whitelist[i] != NULL; i++) { + for (i = 0; tablet_stylus_whitelist[i] != nullptr; i++) { if (type && match_token(type, tablet_stylus_whitelist[i])) { return GHOST_kTabletModeStylus; } @@ -2600,7 +2650,7 @@ static GHOST_TTabletMode tablet_mode_from_name(const char *name, const char *typ if (type && match_token(type, "eraser")) { return GHOST_kTabletModeEraser; } - for (i = 0; tablet_stylus_whitelist[i] != NULL; i++) { + for (i = 0; tablet_stylus_whitelist[i] != nullptr; i++) { if (name && match_token(name, tablet_stylus_whitelist[i])) { return GHOST_kTabletModeStylus; } @@ -2629,7 +2679,7 @@ void GHOST_SystemX11::refreshXInputDevices() for (int i = 0; i < device_count; ++i) { char *device_type = device_info[i].type ? XGetAtomName(m_display, device_info[i].type) : - NULL; + nullptr; GHOST_TTabletMode tablet_mode = tablet_mode_from_name(device_info[i].name, device_type); // printf("Tablet type:'%s', name:'%s', index:%d\n", device_type, device_info[i].name, i); @@ -2638,7 +2688,7 @@ void GHOST_SystemX11::refreshXInputDevices() XFree((void *)device_type); } - if (!(tablet_mode == GHOST_kTabletModeStylus || tablet_mode == GHOST_kTabletModeEraser)) { + if (!ELEM(tablet_mode, GHOST_kTabletModeStylus, GHOST_kTabletModeEraser)) { continue; } @@ -2646,15 +2696,15 @@ void GHOST_SystemX11::refreshXInputDevices() xtablet.ID = device_info[i].id; xtablet.Device = XOpenDevice(m_display, xtablet.ID); - if (xtablet.Device != NULL) { + if (xtablet.Device != nullptr) { /* Find how many pressure levels tablet has */ XAnyClassPtr ici = device_info[i].inputclassinfo; - if (ici != NULL) { + if (ici != nullptr) { for (int j = 0; j < device_info[i].num_classes; ++j) { if (ici->c_class == ValuatorClass) { XValuatorInfo *xvi = (XValuatorInfo *)ici; - if (xvi->axes != NULL) { + if (xvi->axes != nullptr) { xtablet.PressureLevels = xvi->axes[2].max_value; if (xvi->num_axes > 3) { |