diff options
Diffstat (limited to 'src/x11.c')
-rw-r--r-- | src/x11.c | 148 |
1 files changed, 59 insertions, 89 deletions
@@ -11,7 +11,7 @@ #include "platform.h" #include "types.h" -#include "pugl/pugl.h" +#include <pugl/pugl.h> #include <X11/X.h> #include <X11/Xatom.h> @@ -70,7 +70,8 @@ #ifdef __cplusplus # define PUGL_INIT_STRUCT \ - {} + { \ + } #else # define PUGL_INIT_STRUCT {0} #endif @@ -404,14 +405,18 @@ updateSizeHints(const PuglView* const view) XSizeHints sizeHints = PUGL_INIT_STRUCT; if (!view->hints[PUGL_RESIZABLE]) { - const PuglRect frame = puglGetFrame(view); + PuglArea size = puglGetSizeHint(view, PUGL_CURRENT_SIZE); + if (!puglIsValidSize(size.width, size.height)) { + size = puglGetSizeHint(view, PUGL_DEFAULT_SIZE); + } + sizeHints.flags = PBaseSize | PMinSize | PMaxSize; - sizeHints.base_width = (int)frame.width; - sizeHints.base_height = (int)frame.height; - sizeHints.min_width = (int)frame.width; - sizeHints.min_height = (int)frame.height; - sizeHints.max_width = (int)frame.width; - sizeHints.max_height = (int)frame.height; + sizeHints.base_width = (int)size.width; + sizeHints.base_height = (int)size.height; + sizeHints.min_width = (int)size.width; + sizeHints.min_height = (int)size.height; + sizeHints.max_width = (int)size.width; + sizeHints.max_height = (int)size.height; } else { // Avoid setting PBaseSize for top level views to avoid window manager bugs const PuglArea defaultSize = view->sizeHints[PUGL_DEFAULT_SIZE]; @@ -522,8 +527,8 @@ puglGetAncestorCenter(const PuglView* const view) &ancestorAttrs); const PuglPoint center = { - (PuglCoord)(ancestorAttrs.x + ancestorAttrs.width / 2), - (PuglCoord)(ancestorAttrs.y + ancestorAttrs.height / 2)}; + (PuglCoord)(ancestorAttrs.x + (ancestorAttrs.width / 2)), + (PuglCoord)(ancestorAttrs.y + (ancestorAttrs.height / 2))}; return center; } @@ -1000,7 +1005,7 @@ translateClientMessage(PuglView* const view, XClientMessageEvent message) { Display* const display = view->world->impl->display; const PuglX11Atoms* const atoms = &view->world->impl->atoms; - PuglEvent event = {{PUGL_NOTHING, 0}}; + PuglEvent event = {{PUGL_NOTHING, 0U}}; if (message.message_type == atoms->WM_PROTOCOLS) { const Atom protocol = (Atom)message.data.l[0]; @@ -1083,7 +1088,7 @@ getCurrentConfiguration(PuglView* const view) } // Build a configure event based on the current window configuration - PuglEvent configureEvent = {{PUGL_CONFIGURE, 0}}; + PuglEvent configureEvent = {{PUGL_CONFIGURE, 0U}}; configureEvent.configure.x = (PuglCoord)x; configureEvent.configure.y = (PuglCoord)y; configureEvent.configure.width = (PuglSpan)attrs.width; @@ -1114,7 +1119,7 @@ translatePropertyNotify(PuglView* const view, XPropertyEvent message) { const PuglInternals* const impl = view->impl; const PuglX11Atoms* const atoms = &view->world->impl->atoms; - PuglEvent event = {{PUGL_NOTHING, 0}}; + PuglEvent event = {{PUGL_NOTHING, 0U}}; if (message.atom == atoms->NET_WM_STATE) { // Get all the current states set in the window hints @@ -1164,7 +1169,7 @@ translatePropertyNotify(PuglView* const view, XPropertyEvent message) static PuglEvent translateEvent(PuglView* const view, XEvent xevent) { - PuglEvent event = {{PUGL_NOTHING, 0}}; + PuglEvent event = {{PUGL_NOTHING, 0U}}; event.any.flags = xevent.xany.send_event ? PUGL_IS_SEND_EVENT : 0; switch (xevent.type) { @@ -1185,6 +1190,9 @@ translateEvent(PuglView* const view, XEvent xevent) view->impl->mapped = false; event = makeConfigureEvent(view); break; + case DestroyNotify: + view->impl->win = None; + break; case ConfigureNotify: event = makeConfigureEvent(view); event.configure.width = (PuglSpan)xevent.xconfigure.width; @@ -1508,16 +1516,6 @@ puglSendEvent(PuglView* const view, const PuglEvent* const event) return PUGL_UNSUPPORTED; } -#ifndef PUGL_DISABLE_DEPRECATED -PuglStatus -puglWaitForEvent(PuglView* const view) -{ - XEvent xevent; - XPeekEvent(view->world->impl->display, &xevent); - return PUGL_SUCCESS; -} -#endif - static void mergeExposeEvents(PuglExposeEvent* const dst, const PuglExposeEvent* const src) { @@ -1586,7 +1584,7 @@ handleSelectionNotify(const PuglWorld* const world, Display* const display = view->world->impl->display; const Atom selection = event->selection; PuglX11Clipboard* const board = getX11SelectionClipboard(view, selection); - PuglEvent puglEvent = {{PUGL_NOTHING, 0}}; + PuglEvent puglEvent = {{PUGL_NOTHING, 0U}}; if (event->target == atoms->TARGETS) { // Notification of available datatypes @@ -1596,7 +1594,7 @@ handleSelectionNotify(const PuglWorld* const world, view, event->requestor, event->property, &numFormats, &formats) && !setClipboardFormats(view, board, numFormats, formats)) { const PuglDataOfferEvent offer = { - PUGL_DATA_OFFER, 0, (double)event->time / 1e3}; + PUGL_DATA_OFFER, 0U, (double)event->time / 1e3}; puglEvent.offer = offer; board->acceptedFormatIndex = UINT32_MAX; @@ -1673,8 +1671,7 @@ handleSelectionRequest(const PuglWorld* const world, } /// Flush pending configure and expose events for all views -PUGL_WARN_UNUSED_RESULT -static PuglStatus +PUGL_WARN_UNUSED_RESULT static PuglStatus flushExposures(PuglWorld* const world) { PuglStatus st0 = PUGL_SUCCESS; @@ -1727,7 +1724,7 @@ handleTimerEvent(PuglWorld* const world, const XEvent xevent) for (size_t i = 0; i < world->impl->numTimers; ++i) { if (world->impl->timers[i].alarm == notify->alarm) { - PuglEvent event = {{PUGL_TIMER, 0}}; + PuglEvent event = {{PUGL_TIMER, 0U}}; event.timer.id = world->impl->timers[i].id; puglDispatchEvent(world->impl->timers[i].view, &event); } @@ -1825,14 +1822,6 @@ dispatchX11Events(PuglWorld* const world) return st; } -#ifndef PUGL_DISABLE_DEPRECATED -PuglStatus -puglProcessEvents(PuglView* const view) -{ - return puglUpdate(view->world, 0.0); -} -#endif - PuglStatus puglUpdate(PuglWorld* const world, const double timeout) { @@ -1871,7 +1860,10 @@ double puglGetTime(const PuglWorld* const world) { struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); + if (clock_gettime(CLOCK_MONOTONIC, &ts)) { + return 0.0; + } + return ((double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0) - world->startTime; } @@ -1899,14 +1891,14 @@ puglObscureRegion(PuglView* const view, const PuglSpan cw = MIN(view->lastConfigure.width, (PuglSpan)width); const PuglSpan ch = MIN(view->lastConfigure.height, (PuglSpan)height); - const PuglExposeEvent event = {PUGL_EXPOSE, 0, cx, cy, cw, ch}; + const PuglExposeEvent event = {PUGL_EXPOSE, 0U, cx, cy, cw, ch}; if (view->world->impl->dispatchingEvents) { // Currently dispatching events, add/expand expose for the loop end mergeExposeEvents(&view->impl->pendingExpose.expose, &event); } else if (view->impl->win) { // Not dispatching events, send an X expose so we wake up next time - PuglEvent exposeEvent = {{PUGL_EXPOSE, 0}}; + PuglEvent exposeEvent = {{PUGL_EXPOSE, 0U}}; exposeEvent.expose = event; return puglSendEvent(view, &exposeEvent); } @@ -1960,65 +1952,41 @@ puglGetScaleFactor(const PuglView* const view) return view->world->impl->scaleFactor; } -PuglStatus -puglSetFrame(PuglView* const view, const PuglRect frame) -{ - if (!view->impl->win) { - // Set defaults to be used when realized - view->defaultX = frame.x; - view->defaultY = frame.y; - view->sizeHints[PUGL_DEFAULT_SIZE].width = frame.width; - view->sizeHints[PUGL_DEFAULT_SIZE].height = frame.height; - return PUGL_SUCCESS; - } - - return puglX11Status(XMoveResizeWindow(view->world->impl->display, - view->impl->win, - frame.x, - frame.y, - frame.width, - frame.height)); -} - -PuglStatus -puglSetPosition(PuglView* const view, const int x, const int y) +static PuglStatus +puglSetWindowPosition(PuglView* const view, const int x, const int y) { - Display* const display = view->world->impl->display; - - if (!puglIsValidPosition(x, y)) { - return PUGL_BAD_PARAMETER; - } - - if (!view->impl->win) { - // Set defaults to be used when realized - view->defaultX = x; - view->defaultY = y; - return PUGL_SUCCESS; - } - - return puglX11Status(XMoveWindow(display, + return puglX11Status(XMoveWindow(view->world->impl->display, view->impl->win, (int)(x - view->impl->frameExtentLeft), (int)(y - view->impl->frameExtentTop))); } -PuglStatus -puglSetSize(PuglView* const view, const unsigned width, const unsigned height) +static PuglStatus +puglSetWindowSize(PuglView* const view, + const unsigned width, + const unsigned height) { - Display* const display = view->world->impl->display; + return !view->impl->win + ? PUGL_SUCCESS + : puglX11Status(XResizeWindow( + view->world->impl->display, view->impl->win, width, height)); +} - if (!puglIsValidSize(width, height)) { +PuglStatus +puglSetPositionHint(PuglView* const view, + const PuglPositionHint hint, + const int x, + const int y) +{ + if (x <= INT16_MIN || x > INT16_MAX || y <= INT16_MIN || y > INT16_MAX) { return PUGL_BAD_PARAMETER; } - if (!view->impl->win) { - // Set defaults to be used when realized - view->sizeHints[PUGL_DEFAULT_SIZE].width = (PuglSpan)width; - view->sizeHints[PUGL_DEFAULT_SIZE].height = (PuglSpan)height; - return PUGL_SUCCESS; - } + view->positionHints[hint].x = (PuglCoord)x; + view->positionHints[hint].y = (PuglCoord)y; - return puglX11Status(XResizeWindow(display, view->impl->win, width, height)); + return (hint == PUGL_CURRENT_POSITION) ? puglSetWindowPosition(view, x, y) + : PUGL_SUCCESS; } PuglStatus @@ -2029,7 +1997,9 @@ puglSetSizeHint(PuglView* const view, { const PuglStatus st = puglStoreSizeHint(view, hint, width, height); - return st ? st : updateSizeHints(view); + return st ? st + : (hint == PUGL_CURRENT_SIZE) ? puglSetWindowSize(view, width, height) + : updateSizeHints(view); } PuglStatus |