SwAS git · main
SwAS - app switcher for sway (SVG/PNG icons, themeable)
C 97.9% Markdown 1.4%git clone https://git.christianimmanuel.de/sway/SwAS.gitwget https://git.christianimmanuel.de/sway/SwAS/archive/SwAS.tar.gzFix bug swov opens in background AND speedup rendering
README.md | 33 ++- commit_message.txt | 53 ++--- sw_draw.h | 619 +++++++++++++++++++++++++++++++++++++++++------------ swas.c | 86 +++++--- 4 files changed, 581 insertions(+), 210 deletions(-) diff --git a/README.md b/README.md index 43a3f1e..d935a0f 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,6 @@ labwc and the other wlroots compositors do. ``` bindsym $mod+space exec swas -for_window [app_id="swas"] floating enable, border none ``` | key | action | @@ -114,20 +113,19 @@ underneath, showing every workspace with the windows already in it. ``` bindsym $mod+space exec swas --replace overview=1 - -no_focus [app_id="swov-backdrop"] -for_window [app_id="swov-backdrop"] floating enable, border none -for_window [app_id="swas"] floating enable, border none ``` -The app id changed with the name, so any rule of yours saying `app_id="appwheel"` -needs updating. The config and history move too — `~/.config/swas/config` and -`~/.cache/swas/history` — but the old `appwheel` ones are still read when the -new ones are absent, so nothing is lost by not moving them. +That is the whole config. Both programs are layer-shell surfaces, not +windows, so the old `for_window` and `no_focus` rules for `swas` and +`swov-backdrop` do nothing and can be deleted. + +The overview is not started when the wheel opens. It comes up the moment you +start dragging an app, which is the only time it is of any use — so pressing +the binding costs one process, not two. -`no_focus` is a command in its own right and takes the criteria as its -argument — it is *not* something you put after `for_window`. Written the wrong -way round it fails silently at load and the backdrop takes your keyboard. +The config and history live in `~/.config/swas/config` and +`~/.cache/swas/history`; the old `appwheel` ones are still read when those +are absent, so nothing is lost by not moving them. Started by swov (its `d` key), swas talks to that overview instead of bringing up its own: the wheel appears in front of it, and closes again after @@ -137,16 +135,11 @@ one drop, leaving the overview where it was. That is what `SWAS_OV_IN` and `--replace` closes a wheel that is already open before starting, so pressing the binding twice gives you one wheel rather than two stacked on each other. -The `no_focus` line matters. Without it sway hands the keyboard to the backdrop -the moment it appears and the first letters you type are lost. - If something looks wrong, run it from a terminal with `overview_debug=1`: every line between the two, and swov's own complaints, go to stderr. -The backdrop maps on top of the wheel — on Wayland a window cannot lift itself -back — so swas asks swov to restack it as soon as that window is up. If -you ever end up with a wheel that ignores the mouse, that is what went wrong; -`esc` still closes it, and `overview=0` falls back to the plain tiles. +The backdrop sits on the layer below the wheel, so the wheel stays in front +and keeps the keyboard. `overview=0` falls back to the plain tiles. Drop it against the **edge of a workspace** and it opens beside everything already there — left of a column of two windows, not left of the top one. @@ -255,3 +248,5 @@ that. - Apps whose `.desktop` file says `Terminal=true` are run inside `terminal=`. - SVG icons are rasterised at the size they are drawn, so they stay sharp at any `icon_px`. +- A frame is only drawn when something changes or something is still moving. + An open wheel sitting still uses no CPU. diff --git a/commit_message.txt b/commit_message.txt index fcf6eea..4e5b3b7 100644 --- a/commit_message.txt +++ b/commit_message.txt @@ -1,30 +1,35 @@ -shared: bold faces, offscreen canvases and a PNG writer +shared: a rasteriser fast enough for a full-screen overlay -What swov needed from the layer that swas and swbr already use, added -without changing anything the two of them do. +sw_draw.h was written for swbr, which draws a bar. Asked to draw a whole +screen it cost 77 ms a frame at 1600x900 and most of a second at 4K, because +every shape walked its bounding box and blended each pixel in floating +point. What changed: -sw_draw.h - - sw_fonts_bold() / sw_font_bold(): a second face for headings. Without - one the regular face is emboldened by drawing each glyph twice, a - third of a pixel apart, so bold still reads as bold on a desktop that - ships only one weight. The cache is keyed on size and weight together. - - sw_canvas_new/free/clear and sw_blit_canvas(): an offscreen canvas - that can be scaled, tinted and faded onto another. Shrinking box- - filters; a 1:1 aligned blit is a straight premultiplied-over copy. - This is what swov's backdrop fade and its full -> half -> small blur - chain are made of. - - sw_write_png(): one canvas to a PNG, stored (uncompressed) deflate, - for swov --shot. No zlib, a big file, which is fine for one frame. - - sw_text_width_n() and sw_font_descent(), for measuring part of a - string and centring a line in a box. + - Blending is 8-bit integer throughout. A run of one colour composites two + channels at a time out of a packed word, so the inner loop is one + multiply per half and an add. + - Every shape fills its solid middle with plain spans and keeps the + distance field for the edge: a rounded rectangle evaluates it only in + the four corners, a circle only at the ends of each span, an annular + wedge only inside its own bounding box rather than the whole disc. + - Triangles are a distance field too, stepped along the row, instead of + four samples a pixel. + - sw_canvas_bg() replaces the clip rectangle instead of blending into it, + for the one fill that is always the first thing drawn. -sw_wl.h - - SwEvent.code carries the raw evdev keycode, so a program can read a - key by its place on the board rather than by what it types. swov's - number row needs that on a layout where the digits take shift. - - sw_win_create() accepts a NULL SwApp: a program that drives its own - frames with sw_win_begin/end has no callbacks to give, and passing - NULL used to dereference it. +Measured, same frames: a swov-like frame 77 -> 1.2 ms at 1600x900 and +473 -> 6.6 ms at 4K; a swas-like frame 278 -> 7.4 ms and 1741 -> 40 ms; a +single wedge 24 -> 0.24 ms. Output is unchanged to the eye — the distance +fields antialias at least as well as the sampling did. + +Earlier in this release: bold faces (sw_fonts_bold / sw_font_bold, with a +synthesised bold when the desktop ships one weight), offscreen canvases +(sw_canvas_new / sw_blit_canvas) for the backdrop's fade and blur, +sw_write_png for swov --shot, sw_text_width_n and sw_font_descent. + +sw_wl.h: SwEvent.code carries the raw evdev keycode, so a program can read a +key by its place on the board; and sw_win_create accepts a NULL SwApp, which +a program driving its own frames has no use for and which used to crash. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SuAfijAAYjLpcVwhCq3AF4 diff --git a/sw_draw.h b/sw_draw.h index f9e133e..cca1c2c 100644 --- a/sw_draw.h +++ b/sw_draw.h @@ -1,12 +1,20 @@ /* sw_draw.h — a software canvas. ARGB8888, premultiplied, which is what - * wl_shm wants. Rounded rectangles come out of a signed distance field, text - * out of stb_truetype with a per-size glyph cache, images out of a box filter. + * wl_shm wants. Rounded rectangles, circles, wedges and triangles come out of + * signed distance fields, text out of stb_truetype with a per-size glyph + * cache, images out of a box filter. * * No GPU and no windowing system: a Canvas is a pixel buffer and a clip * rectangle, so a whole frame can be drawn and then inspected in a test with * no compositor anywhere near it. That is the point of doing it this way, as * much as the short link line. * + * It is written to be fast enough for a full-screen overlay. Blending is in + * 8-bit integers, a run of one colour composites two channels at a time, and + * every shape fills its solid middle with plain spans and keeps the distance + * field for the edge — a rounded rectangle only evaluates it in the four + * corners, a wedge only inside its own bounding box. Doing this per pixel in + * floating point, as the first version did, cost a 4K frame most of a second. + * * Lifted out of swbr, which has drawn itself like this all along. * * Define SW_DRAW_IMPL in exactly one translation unit before including this. @@ -93,6 +101,11 @@ float sw_text_width(Font *f, const char *s); void sw_canvas_clip(Canvas *cv, int x0, int y0, int x1, int y1); void sw_canvas_clip_all(Canvas *cv); void sw_canvas_fill(Canvas *cv, Col c); /* the whole clip rectangle */ +/* The frame's background: the clip rectangle is *replaced* by c rather than + blended with it. On a buffer that was just cleared the result is the same, + and it is a plain store instead of a read-modify-write — worth having on a + full-screen overlay, where this is the single biggest thing drawn. */ +void sw_canvas_bg(Canvas *cv, Col c); void sw_fill_rect(Canvas *cv, float x, float y, float w, float h, Col c); void sw_fill_round(Canvas *cv, float x, float y, float w, float h, @@ -473,101 +486,251 @@ void sw_canvas_clip(Canvas *cv, int x0, int y0, int x1, int y1) void sw_canvas_clip_all(Canvas *cv) { sw_canvas_clip(cv, 0, 0, cv->w, cv->h); } -static inline void sw_blend(Canvas *cv, int x, int y, Col c, float cov) +/* ------------------------------------------------------------- blending + * The canvas is premultiplied ARGB8888. Everything below works in 8-bit + * integers: a float blend per pixel is what made a full-screen frame cost + * tens of milliseconds. SW_MUL is the usual x*y/255 without a divide. + */ + +#define SW_MUL(x, y) ((unsigned)((((unsigned)(x) * (unsigned)(y) + 128u) * 257u) >> 16)) + +/* Two 8-bit channels packed in 0x00ff00ff, each already multiplied by a + 0..255 factor: divide both by 255, rounded, without a divide. */ +static inline uint32_t sw_div255x2(uint32_t x) +{ + uint32_t t = x + 0x00800080u; + return ((t + ((t >> 8) & 0x00ff00ffu)) >> 8) & 0x00ff00ffu; +} + +/* One pixel, no bounds check: callers clip their spans first. `a` is the + coverage and the colour's own alpha already multiplied together. */ +static inline void sw_put(uint32_t *p, Col c, unsigned a) +{ + unsigned ia, d, da, dr, dg, db; + if (!a) return; + if (a == 255) { + *p = 0xff000000u | ((uint32_t)c.r << 16) | ((uint32_t)c.g << 8) | c.b; + return; + } + ia = 255u - a; + d = *p; + da = (d >> 24) & 0xffu; dr = (d >> 16) & 0xffu; + dg = (d >> 8) & 0xffu; db = d & 0xffu; + *p = ((SW_MUL(da, ia) + a) << 24) + | ((SW_MUL(c.r, a) + SW_MUL(dr, ia)) << 16) + | ((SW_MUL(c.g, a) + SW_MUL(dg, ia)) << 8) + | (SW_MUL(c.b, a) + SW_MUL(db, ia)); +} + +static inline void sw_blend8(Canvas *cv, int x, int y, Col c, unsigned a) { if (x < cv->cx0 || x >= cv->cx1 || y < cv->cy0 || y >= cv->cy1) return; - float a = (float)c.a / 255.0f * cov; - if (a <= 0.0f) return; - uint32_t d = cv->px[y * cv->w + x]; - float da = (float)((d >> 24) & 0xff) / 255.0f; - float dr = (float)((d >> 16) & 0xff) / 255.0f; - float dg = (float)((d >> 8) & 0xff) / 255.0f; - float db = (float)(d & 0xff) / 255.0f; - float sr = (float)c.r / 255.0f * a; - float sg = (float)c.g / 255.0f * a; - float sb = (float)c.b / 255.0f * a; - float ia = 1.0f - a; - float orr = sr + dr * ia, og = sg + dg * ia, ob = sb + db * ia, oa = a + da * ia; - cv->px[y * cv->w + x] = - ((uint32_t)(orr * 255.0f + 0.5f) << 16) | - ((uint32_t)(og * 255.0f + 0.5f) << 8) | - ((uint32_t)(ob * 255.0f + 0.5f)) | - ((uint32_t)(oa * 255.0f + 0.5f) << 24); + sw_put(&cv->px[(size_t)y * (size_t)cv->w + x], c, a); +} + +/* Kept for callers that think in coverage. */ +static inline void sw_blend(Canvas *cv, int x, int y, Col c, float cov) +{ + int a; + if (cov <= 0.0f) return; + if (cov > 1.0f) cov = 1.0f; + a = (int)((float)c.a * cov + 0.5f); + if (a <= 0) return; + sw_blend8(cv, x, y, c, (unsigned)a); +} + +/* A run of pixels on one row at a constant alpha. This is where the time + goes, so the clip is applied once and the inner loop is bare. */ +static void sw_span(Canvas *cv, int x0, int x1, int y, Col c, unsigned a) +{ + uint32_t *p, *e; + if (!a || y < cv->cy0 || y >= cv->cy1) return; + if (x0 < cv->cx0) x0 = cv->cx0; + if (x1 > cv->cx1) x1 = cv->cx1; + if (x0 >= x1) return; + p = &cv->px[(size_t)y * (size_t)cv->w + x0]; + e = p + (x1 - x0); + if (a == 255) { + uint32_t v = 0xff000000u | ((uint32_t)c.r << 16) | ((uint32_t)c.g << 8) | c.b; + while (p < e) *p++ = v; + return; + } + { /* two channels at a time: the colour is constant along the run, so + the only work per pixel is one multiply per half and an add */ + unsigned ia = 255u - a; + uint32_t srb = (SW_MUL(c.r, a) << 16) | SW_MUL(c.b, a); + uint32_t sag = ((unsigned)a << 16) | SW_MUL(c.g, a); + while (p < e) { + uint32_t d = *p; + uint32_t rb = sw_div255x2((d & 0x00ff00ffu) * ia); + uint32_t ag = sw_div255x2(((d >> 8) & 0x00ff00ffu) * ia); + *p++ = ((srb + rb) & 0x00ff00ffu) | (((sag + ag) & 0x00ff00ffu) << 8); + } + } +} + +/* how much of [a0,a1) the unit cell [i,i+1) covers */ +static inline float sw_cov1(float a0, float a1, int i) +{ + float lo = (float)i, hi = lo + 1.0f; + float l = a0 > lo ? a0 : lo, r = a1 < hi ? a1 : hi; + return r > l ? r - l : 0.0f; } void sw_fill_rect(Canvas *cv, float x, float y, float w, float h, Col c) { + int ix0, ix1, iy0, iy1, yy, fx0, fx1; + float x1f = x + w, y1f = y + h; if (w <= 0 || h <= 0 || !c.a) return; - int x0 = (int)floorf(x), y0 = (int)floorf(y); - int x1 = (int)ceilf(x + w), y1 = (int)ceilf(y + h); - for (int yy = y0; yy < y1; ++yy) - for (int xx = x0; xx < x1; ++xx) { - float cxs = sw_clampf(x + w, (float)xx, (float)xx + 1) - sw_clampf(x, (float)xx, (float)xx + 1); - float cys = sw_clampf(y + h, (float)yy, (float)yy + 1) - sw_clampf(y, (float)yy, (float)yy + 1); - sw_blend(cv, xx, yy, c, cxs * cys); + + ix0 = (int)floorf(x); ix1 = (int)ceilf(x1f); + iy0 = (int)floorf(y); iy1 = (int)ceilf(y1f); + if (ix0 < cv->cx0) ix0 = cv->cx0; + if (ix1 > cv->cx1) ix1 = cv->cx1; + if (iy0 < cv->cy0) iy0 = cv->cy0; + if (iy1 > cv->cy1) iy1 = cv->cy1; + if (ix0 >= ix1 || iy0 >= iy1) return; + + fx0 = (int)ceilf(x); if (fx0 < ix0) fx0 = ix0; /* first full column */ + fx1 = (int)floorf(x1f); if (fx1 > ix1) fx1 = ix1; /* past the last one */ + + for (yy = iy0; yy < iy1; ++yy) { + float yc = sw_cov1(y, y1f, yy); + unsigned af; + if (yc <= 0.0f) continue; + af = (unsigned)((float)c.a * yc + 0.5f); + if (fx0 > ix0) sw_blend8(cv, ix0, yy, + c, (unsigned)((float)c.a * yc * sw_cov1(x, x1f, ix0) + 0.5f)); + if (fx1 > fx0) sw_span(cv, fx0, fx1, yy, c, af); + if (fx1 < ix1) sw_blend8(cv, ix1 - 1, yy, + c, (unsigned)((float)c.a * yc * sw_cov1(x, x1f, ix1 - 1) + 0.5f)); + } +} + +/* One rounded corner: inside its own box the distance to the shape is just + the distance to the circle's centre, so this is a quarter disc. */ +static void sw_corner(Canvas *cv, float cx, float cy, float r, + int sx, int sy, Col c) +{ + int x0, y0, x1, y1, xx, yy; + if (r <= 0.0f) return; + x0 = (int)floorf(sx < 0 ? cx - r - 1.0f : cx); + x1 = (int)ceilf (sx < 0 ? cx : cx + r + 1.0f); + y0 = (int)floorf(sy < 0 ? cy - r - 1.0f : cy); + y1 = (int)ceilf (sy < 0 ? cy : cy + r + 1.0f); + if (x0 < cv->cx0) x0 = cv->cx0; + if (x1 > cv->cx1) x1 = cv->cx1; + if (y0 < cv->cy0) y0 = cv->cy0; + if (y1 > cv->cy1) y1 = cv->cy1; + for (yy = y0; yy < y1; ++yy) { + float dy = (float)yy + 0.5f - cy; + uint32_t *row = &cv->px[(size_t)yy * (size_t)cv->w]; + for (xx = x0; xx < x1; ++xx) { + float dx = (float)xx + 0.5f - cx; + float d = sqrtf(dx * dx + dy * dy) - r; + float cov = 0.5f - d; + if (cov <= 0.0f) continue; + if (cov > 1.0f) cov = 1.0f; + sw_put(&row[xx], c, (unsigned)((float)c.a * cov + 0.5f)); } + } } -/* Rounded box with a radius per corner (tl, tr, br, bl), anti-aliased through - * the usual signed distance trick. A corner radius of 0 is a square corner — - * that is how the bar stays flush with the screen edge. */ +/* Rounded box with a radius per corner (tl, tr, br, bl). The straight parts + * are plain rectangles and only the four corners go through a distance + * field, which is what keeps a full-screen fill cheap. A corner radius of 0 + * is a square corner — that is how the bar stays flush with the screen edge. */ void sw_fill_round(Canvas *cv, float x, float y, float w, float h, float rtl, float rtr, float rbr, float rbl, Col c) { + float cap, rt, rb, mid; if (w <= 0 || h <= 0 || !c.a) return; - float hw = w * 0.5f, hh = h * 0.5f; - float ccx = x + hw, ccy = y + hh; - float cap = fminf(hw, hh); + cap = fminf(w * 0.5f, h * 0.5f); rtl = sw_clampf(rtl, 0, cap); rtr = sw_clampf(rtr, 0, cap); rbr = sw_clampf(rbr, 0, cap); rbl = sw_clampf(rbl, 0, cap); + if (rtl <= 0.0f && rtr <= 0.0f && rbr <= 0.0f && rbl <= 0.0f) { + sw_fill_rect(cv, x, y, w, h, c); + return; + } + + rt = fmaxf(rtl, rtr); + rb = fmaxf(rbr, rbl); + mid = h - rt - rb; + + if (mid > 0.0f) sw_fill_rect(cv, x, y + rt, w, mid, c); + + /* top band */ + if (rt > 0.0f) { + sw_fill_rect(cv, x + rtl, y, w - rtl - rtr, rt, c); + if (rtl < rt) sw_fill_rect(cv, x, y + rtl, rtl, rt - rtl, c); + if (rtr < rt) sw_fill_rect(cv, x + w - rtr, y + rtr, rtr, rt - rtr, c); + sw_corner(cv, x + rtl, y + rtl, rtl, -1, -1, c); + sw_corner(cv, x + w - rtr, y + rtr, rtr, +1, -1, c); + } + /* bottom band */ + if (rb > 0.0f) { + sw_fill_rect(cv, x + rbl, y + h - rb, w - rbl - rbr, rb, c); + if (rbl < rb) sw_fill_rect(cv, x, y + h - rb, rbl, rb - rbl, c); + if (rbr < rb) sw_fill_rect(cv, x + w - rbr, y + h - rb, rbr, rb - rbr, c); + sw_corner(cv, x + rbl, y + h - rbl, rbl, -1, +1, c); + sw_corner(cv, x + w - rbr, y + h - rbr, rbr, +1, +1, c); + } +} - int x0 = (int)floorf(x) - 1, y0 = (int)floorf(y) - 1; - int x1 = (int)ceilf(x + w) + 1, y1 = (int)ceilf(y + h) + 1; - for (int yy = y0; yy < y1; ++yy) { - if (yy < cv->cy0 || yy >= cv->cy1) continue; - for (int xx = x0; xx < x1; ++xx) { - if (xx < cv->cx0 || xx >= cv->cx1) continue; - float px = (float)xx + 0.5f - ccx, py = (float)yy + 0.5f - ccy; - float r = px < 0 ? (py < 0 ? rtl : rbl) : (py < 0 ? rtr : rbr); - float qx = fabsf(px) - (hw - r), qy = fabsf(py) - (hh - r); - float mx = fmaxf(qx, 0.0f), my = fmaxf(qy, 0.0f); - float d = sqrtf(mx * mx + my * my) + fminf(fmaxf(qx, qy), 0.0f) - r; - float cov = sw_clampf(0.5f - d, 0.0f, 1.0f); - if (cov > 0) sw_blend(cv, xx, yy, c, cov); +/* One corner of an outline: the same quarter disc kept to a ring. */ +static void sw_corner_ring(Canvas *cv, float cx, float cy, float r, float t, + int sx, int sy, Col c) +{ + int x0, y0, x1, y1, xx, yy; + if (r <= 0.0f || t <= 0.0f) return; + x0 = (int)floorf(sx < 0 ? cx - r - 1.0f : cx); + x1 = (int)ceilf (sx < 0 ? cx : cx + r + 1.0f); + y0 = (int)floorf(sy < 0 ? cy - r - 1.0f : cy); + y1 = (int)ceilf (sy < 0 ? cy : cy + r + 1.0f); + if (x0 < cv->cx0) x0 = cv->cx0; + if (x1 > cv->cx1) x1 = cv->cx1; + if (y0 < cv->cy0) y0 = cv->cy0; + if (y1 > cv->cy1) y1 = cv->cy1; + for (yy = y0; yy < y1; ++yy) { + float dy = (float)yy + 0.5f - cy; + uint32_t *row = &cv->px[(size_t)yy * (size_t)cv->w]; + for (xx = x0; xx < x1; ++xx) { + float dx = (float)xx + 0.5f - cx; + float d = sqrtf(dx * dx + dy * dy) - r; + float cov = 0.5f - fabsf(d + t * 0.5f) + t * 0.5f; + if (cov <= 0.0f) continue; + if (cov > 1.0f) cov = 1.0f; + sw_put(&row[xx], c, (unsigned)((float)c.a * cov + 0.5f)); } } } -/* The same distance field kept to a ring: an outline that follows the corner - * radius and stays inside the box, so a button with no fill still has an edge. */ +/* An outline that follows the corner radius and stays inside the box, so a + * button with no fill still has an edge. Four straight bars and four arcs. */ void sw_stroke_round(Canvas *cv, float x, float y, float w, float h, float rtl, float rtr, float rbr, float rbl, float t, Col c) { + float cap; if (w <= 0 || h <= 0 || t <= 0 || !c.a) return; - float hw = w * 0.5f, hh = h * 0.5f; - float ccx = x + hw, ccy = y + hh; - float cap = fminf(hw, hh); + cap = fminf(w * 0.5f, h * 0.5f); rtl = sw_clampf(rtl, 0, cap); rtr = sw_clampf(rtr, 0, cap); rbr = sw_clampf(rbr, 0, cap); rbl = sw_clampf(rbl, 0, cap); - - int x0 = (int)floorf(x) - 1, y0 = (int)floorf(y) - 1; - int x1 = (int)ceilf(x + w) + 1, y1 = (int)ceilf(y + h) + 1; - for (int yy = y0; yy < y1; ++yy) { - if (yy < cv->cy0 || yy >= cv->cy1) continue; - for (int xx = x0; xx < x1; ++xx) { - if (xx < cv->cx0 || xx >= cv->cx1) continue; - float px = (float)xx + 0.5f - ccx, py = (float)yy + 0.5f - ccy; - float r = px < 0 ? (py < 0 ? rtl : rbl) : (py < 0 ? rtr : rbr); - float qx = fabsf(px) - (hw - r), qy = fabsf(py) - (hh - r); - float mx = fmaxf(qx, 0.0f), my = fmaxf(qy, 0.0f); - float d = sqrtf(mx * mx + my * my) + fminf(fmaxf(qx, qy), 0.0f) - r; - float cov = sw_clampf(0.5f - fabsf(d + t * 0.5f) + t * 0.5f, 0.0f, 1.0f); - if (cov > 0) sw_blend(cv, xx, yy, c, cov); - } - } + if (t > cap) t = cap; + + /* the straight runs, between the corners */ + if (w - rtl - rtr > 0.0f) sw_fill_rect(cv, x + rtl, y, w - rtl - rtr, t, c); + if (w - rbl - rbr > 0.0f) sw_fill_rect(cv, x + rbl, y + h - t, w - rbl - rbr, t, c); + if (h - rtl - rbl > 0.0f) sw_fill_rect(cv, x, y + rtl, t, h - rtl - rbl, c); + if (h - rtr - rbr > 0.0f) sw_fill_rect(cv, x + w - t, y + rtr, t, h - rtr - rbr, c); + + sw_corner_ring(cv, x + rtl, y + rtl, rtl, t, -1, -1, c); + sw_corner_ring(cv, x + w - rtr, y + rtr, rtr, t, +1, -1, c); + sw_corner_ring(cv, x + w - rbr, y + h - rbr, rbr, t, +1, +1, c); + sw_corner_ring(cv, x + rbl, y + h - rbl, rbl, t, -1, +1, c); } + void sw_text(Canvas *cv, Font *f, float x, float baseline, Col c, const char *s) { const char *p = s; @@ -581,13 +744,19 @@ void sw_text(Canvas *cv, Font *f, float x, float baseline, Col c, const char *s) int gy = (int)lroundf(baseline) + g->y0; int pass, passes = f->synth ? 2 : 1; for (pass = 0; pass < passes; ++pass) { + int bx = gx + pass; + int x0 = bx < cv->cx0 ? cv->cx0 - bx : 0; + int x1 = bx + g->w > cv->cx1 ? cv->cx1 - bx : g->w; for (int yy = 0; yy < g->h; ++yy) { int ty = gy + yy; + const unsigned char *row; + uint32_t *dst; if (ty < cv->cy0 || ty >= cv->cy1) continue; - const unsigned char *row = g->bmp + (size_t)yy * (size_t)g->w; - for (int xx = 0; xx < g->w; ++xx) { - unsigned char a = row[xx]; - if (a) sw_blend(cv, gx + pass + xx, ty, c, (float)a / 255.0f); + row = g->bmp + (size_t)yy * (size_t)g->w; + dst = &cv->px[(size_t)ty * (size_t)cv->w + bx]; + for (int xx = x0; xx < x1; ++xx) { + unsigned a = row[xx]; + if (a) sw_put(&dst[xx], c, SW_MUL(c.a, a)); } } } @@ -599,85 +768,231 @@ void sw_text(Canvas *cv, Font *f, float x, float baseline, Col c, const char *s) void sw_canvas_fill(Canvas *cv, Col c) { - sw_fill_rect(cv, (float)cv->cx0, (float)cv->cy0, - (float)(cv->cx1 - cv->cx0), (float)(cv->cy1 - cv->cy0), c); + int y; + if (!c.a) return; + for (y = cv->cy0; y < cv->cy1; ++y) sw_span(cv, cv->cx0, cv->cx1, y, c, c.a); +} + +void sw_canvas_bg(Canvas *cv, Col c) +{ + uint32_t v = ((uint32_t)c.a << 24) | (SW_MUL(c.r, c.a) << 16) + | (SW_MUL(c.g, c.a) << 8) | SW_MUL(c.b, c.a); + int y; + for (y = cv->cy0; y < cv->cy1; ++y) { + uint32_t *p = &cv->px[(size_t)y * (size_t)cv->w + cv->cx0]; + uint32_t *e = p + (cv->cx1 - cv->cx0); + while (p < e) *p++ = v; + } } /* A filled triangle, by half-plane tests on a 2x2 grid per pixel. swbr never * needed one; swas draws its wheel slices and its chevrons out of them. */ +/* A filled triangle as a distance field: the signed distance to each of the + * three edges, normalised so it is in pixels, and the smallest of them is the + * coverage. One pass, no supersampling, and the span is stepped rather than + * recomputed from scratch. */ void sw_fill_tri(Canvas *cv, float x0, float y0, float x1, float y1, float x2, float y2, Col c) { + float minx, maxx, miny, maxy, d; + float nx[3], ny[3], off[3]; + int ix0, iy0, ix1, iy1, y, e; + if (!c.a) return; - float minx = fminf(x0, fminf(x1, x2)), maxx = fmaxf(x0, fmaxf(x1, x2)); - float miny = fminf(y0, fminf(y1, y2)), maxy = fmaxf(y0, fmaxf(y1, y2)); - int ix0 = sw_clampi((int)floorf(minx), cv->cx0, cv->cx1); - int iy0 = sw_clampi((int)floorf(miny), cv->cy0, cv->cy1); - int ix1 = sw_clampi((int)ceilf(maxx) + 1, cv->cx0, cv->cx1); - int iy1 = sw_clampi((int)ceilf(maxy) + 1, cv->cy0, cv->cy1); - - float d = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0); + d = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0); if (fabsf(d) < 1e-6f) return; /* degenerate */ - float inv = 1.0f / d; - - for (int y = iy0; y < iy1; ++y) { - for (int x = ix0; x < ix1; ++x) { - int hits = 0; - for (int sy = 0; sy < 2; ++sy) { - for (int sx = 0; sx < 2; ++sx) { - float px = (float)x + 0.25f + 0.5f * (float)sx; - float py = (float)y + 0.25f + 0.5f * (float)sy; - float l0 = ((x1 - px) * (y2 - py) - (x2 - px) * (y1 - py)) * inv; - float l1 = ((x2 - px) * (y0 - py) - (x0 - px) * (y2 - py)) * inv; - float l2 = 1.0f - l0 - l1; - if (l0 >= 0.0f && l1 >= 0.0f && l2 >= 0.0f) hits++; - } + if (d < 0.0f) { /* make it counter-clockwise */ + float tx = x1, ty = y1; + x1 = x2; y1 = y2; x2 = tx; y2 = ty; + } + + minx = fminf(x0, fminf(x1, x2)); maxx = fmaxf(x0, fmaxf(x1, x2)); + miny = fminf(y0, fminf(y1, y2)); maxy = fmaxf(y0, fmaxf(y1, y2)); + ix0 = (int)floorf(minx) - 1; ix1 = (int)ceilf(maxx) + 1; + iy0 = (int)floorf(miny) - 1; iy1 = (int)ceilf(maxy) + 1; + if (ix0 < cv->cx0) ix0 = cv->cx0; + if (ix1 > cv->cx1) ix1 = cv->cx1; + if (iy0 < cv->cy0) iy0 = cv->cy0; + if (iy1 > cv->cy1) iy1 = cv->cy1; + if (ix0 >= ix1 || iy0 >= iy1) return; + + { /* inward unit normals, one per edge */ + float ex[3], ey[3], ax[3], ay[3]; + ax[0] = x0; ay[0] = y0; ex[0] = x1 - x0; ey[0] = y1 - y0; + ax[1] = x1; ay[1] = y1; ex[1] = x2 - x1; ey[1] = y2 - y1; + ax[2] = x2; ay[2] = y2; ex[2] = x0 - x2; ey[2] = y0 - y2; + for (e = 0; e < 3; ++e) { + float len = sqrtf(ex[e] * ex[e] + ey[e] * ey[e]); + if (len < 1e-6f) return; + nx[e] = -ey[e] / len; + ny[e] = ex[e] / len; + off[e] = -(nx[e] * ax[e] + ny[e] * ay[e]); + } + } + + for (y = iy0; y < iy1; ++y) { + float py = (float)y + 0.5f; + float base[3]; + uint32_t *row = &cv->px[(size_t)y * (size_t)cv->w]; + int x; + for (e = 0; e < 3; ++e) base[e] = ny[e] * py + off[e] + nx[e] * ((float)ix0 + 0.5f); + for (x = ix0; x < ix1; ++x) { + float dmin = base[0], cov; + if (base[1] < dmin) dmin = base[1]; + if (base[2] < dmin) dmin = base[2]; + cov = 0.5f + dmin; + if (cov > 0.0f) { + if (cov > 1.0f) cov = 1.0f; + sw_put(&row[x], c, (unsigned)((float)c.a * cov + 0.5f)); } - if (hits) sw_blend(cv, x, y, c, (float)hits * 0.25f); + base[0] += nx[0]; base[1] += nx[1]; base[2] += nx[2]; } } } void sw_fill_circle(Canvas *cv, float cx, float cy, float rad, Col c) { - sw_fill_round(cv, cx - rad, cy - rad, rad * 2.0f, rad * 2.0f, - rad, rad, rad, rad, c); + int y0, y1, y; + if (!c.a || rad <= 0.0f) return; + y0 = (int)floorf(cy - rad) - 1; y1 = (int)ceilf(cy + rad) + 1; + if (y0 < cv->cy0) y0 = cv->cy0; + if (y1 > cv->cy1) y1 = cv->cy1; + for (y = y0; y < y1; ++y) { + float dy = (float)y + 0.5f - cy; + float q = rad * rad - dy * dy; + float hw = q > 0.0f ? sqrtf(q) : 0.0f; + int x0 = (int)floorf(cx - hw) - 1, x1 = (int)ceilf(cx + hw) + 1, x; + uint32_t *row = &cv->px[(size_t)y * (size_t)cv->w]; + int fx0, fx1; + if (x0 < cv->cx0) x0 = cv->cx0; + if (x1 > cv->cx1) x1 = cv->cx1; + /* the middle of the span is solid; only its ends need a distance */ + fx0 = (int)ceilf(cx - hw + 0.5f); if (fx0 < x0) fx0 = x0; + fx1 = (int)floorf(cx + hw - 0.5f); if (fx1 > x1) fx1 = x1; + for (x = x0; x < x1; ++x) { + float dx, d, cov; + if (x == fx0 && fx1 > fx0) { /* jump the solid middle */ + sw_span(cv, fx0, fx1, y, c, c.a); + x = fx1 - 1; + continue; + } + dx = (float)x + 0.5f - cx; + d = sqrtf(dx * dx + dy * dy) - rad; + cov = 0.5f - d; + if (cov <= 0.0f) continue; + if (cov > 1.0f) cov = 1.0f; + sw_put(&row[x], c, (unsigned)((float)c.a * cov + 0.5f)); + } + } } -/* A wedge of an annulus: the slice shape a radial menu is made of. Tested - * per pixel against the two radii and the two angles, four samples a pixel so - * the long curved edges do not stair-step. A0 may exceed a1 going the other - * way round; the wedge is always drawn the short way from a0 to a1. */ +/* A wedge of an annulus: the slice shape a radial menu is made of. + * + * Written as a distance field rather than by sampling. Both radial edges are + * circles and both angular edges are rays through the centre, and the signed + * distance to a ray through the origin is just the cross product with its + * unit vector — so one sqrt a pixel covers all four edges, with no atan2 and + * no supersampling. Rows outside the outer circle are skipped and the hole in + * the middle is jumped over, which is most of a thick wheel. + * + * a0 may exceed a1 going the other way round; the wedge is always drawn the + * short way from a0 to a1. */ void sw_fill_sector(Canvas *cv, float cx, float cy, float r0, float r1, float a0, float a1, Col c) { + const float TAU = 6.283185307f; + float span, u0x, u0y, u1x, u1y, sx0, sy0, sx1, sy1; + int wide, y0, y1, y; + if (!c.a || r1 <= 0.0f || r1 <= r0) return; - int ix0 = sw_clampi((int)floorf(cx - r1) - 1, cv->cx0, cv->cx1); - int iy0 = sw_clampi((int)floorf(cy - r1) - 1, cv->cy0, cv->cy1); - int ix1 = sw_clampi((int)ceilf(cx + r1) + 1, cv->cx0, cv->cx1); - int iy1 = sw_clampi((int)ceilf(cy + r1) + 1, cv->cy0, cv->cy1); - - float span = a1 - a0; - while (span < 0.0f) span += 6.283185307f; - while (span > 6.283185307f) span -= 6.283185307f; - float r0s = r0 > 0.0f ? r0 * r0 : -1.0f, r1s = r1 * r1; - - for (int y = iy0; y < iy1; ++y) { - for (int x = ix0; x < ix1; ++x) { - int hits = 0; - for (int sy = 0; sy < 2; ++sy) { - for (int sx = 0; sx < 2; ++sx) { - float dx = (float)x + 0.25f + 0.5f * (float)sx - cx; - float dy = (float)y + 0.25f + 0.5f * (float)sy - cy; - float d2 = dx * dx + dy * dy; - if (d2 < r0s || d2 > r1s) continue; - float da = atan2f(dy, dx) - a0; - while (da < 0.0f) da += 6.283185307f; - while (da > 6.283185307f) da -= 6.283185307f; - if (da <= span) hits++; - } + if (r0 < 0.0f) r0 = 0.0f; + + span = a1 - a0; + while (span < 0.0f) span += TAU; + while (span >= TAU) span -= TAU; + if (span >= TAU - 1e-4f) { /* a full ring: no angular edges */ + span = TAU; + } + wide = span > 3.14159265f; + u0x = cosf(a0); u0y = sinf(a0); + u1x = cosf(a0 + span); u1y = sinf(a0 + span); + + /* The wedge's own box, not the whole circle's: a slice of a eleven-slot + wheel touches about a tenth of the disc, and walking the rest of it is + what made a frame cost tens of milliseconds. */ + { + float bx0 = 0.0f, by0 = 0.0f, bx1 = 0.0f, by1 = 0.0f; + float pts[8], k; + int i, nq; + pts[0] = r0 * u0x; pts[1] = r0 * u0y; + pts[2] = r1 * u0x; pts[3] = r1 * u0y; + pts[4] = r0 * u1x; pts[5] = r0 * u1y; + pts[6] = r1 * u1x; pts[7] = r1 * u1y; + bx0 = bx1 = pts[0]; by0 = by1 = pts[1]; + for (i = 1; i < 4; ++i) { + if (pts[i * 2] < bx0) bx0 = pts[i * 2]; + if (pts[i * 2] > bx1) bx1 = pts[i * 2]; + if (pts[i * 2 + 1] < by0) by0 = pts[i * 2 + 1]; + if (pts[i * 2 + 1] > by1) by1 = pts[i * 2 + 1]; + } + /* and wherever the arc crosses an axis inside the wedge */ + for (nq = 0; nq < 4; ++nq) { + float ang = (float)nq * 1.570796327f; + float rel = ang - a0; + while (rel < 0.0f) rel += TAU; + while (rel >= TAU) rel -= TAU; + if (rel > span) continue; + k = r1; + if (nq == 0 && k > bx1) bx1 = k; + if (nq == 1 && k > by1) by1 = k; + if (nq == 2 && -k < bx0) bx0 = -k; + if (nq == 3 && -k < by0) by0 = -k; + } + sy0 = cy + by0; sy1 = cy + by1; + sx0 = cx + bx0; sx1 = cx + bx1; + } + + y0 = (int)floorf(sy0) - 1; y1 = (int)ceilf(sy1) + 1; + if (y0 < cv->cy0) y0 = cv->cy0; + if (y1 > cv->cy1) y1 = cv->cy1; + + for (y = y0; y < y1; ++y) { + float dy = (float)y + 0.5f - cy; + float q = r1 * r1 - dy * dy; + float ow = q > 0.0f ? sqrtf(q) : 0.0f; + float qi = r0 * r0 - dy * dy; + float iw = (r0 > 0.0f && qi > 0.0f) ? sqrtf(qi) : -1.0f; + int x0, x1, x; + int hole0 = 0, hole1 = 0; + uint32_t *row = &cv->px[(size_t)y * (size_t)cv->w]; + float lo = cx - ow, hi = cx + ow; + if (lo < sx0) lo = sx0; + if (hi > sx1) hi = sx1; + x0 = (int)floorf(lo) - 1; x1 = (int)ceilf(hi) + 1; + if (x0 < cv->cx0) x0 = cv->cx0; + if (x1 > cv->cx1) x1 = cv->cx1; + if (iw > 1.0f) { /* the untouched middle */ + hole0 = (int)ceilf(cx - iw + 1.0f); + hole1 = (int)floorf(cx + iw - 1.0f); + } + for (x = x0; x < x1; ++x) { + float dx, len, d, da, cov; + if (hole1 > hole0 && x >= hole0 && x < hole1) { x = hole1 - 1; continue; } + dx = (float)x + 0.5f - cx; + len = sqrtf(dx * dx + dy * dy); + d = r1 - len; /* inside the outer circle */ + if (r0 > 0.0f) { float di = len - r0; if (di < d) d = di; } + if (d <= -1.0f) continue; + if (span < TAU) { + float e0 = u0x * dy - u0y * dx; /* left of the first ray */ + float e1 = dx * u1y - dy * u1x; /* right of the second */ + da = wide ? (e0 > e1 ? e0 : e1) : (e0 < e1 ? e0 : e1); + if (da < d) d = da; } - if (hits) sw_blend(cv, x, y, c, (float)hits * 0.25f); + cov = 0.5f + d; + if (cov <= 0.0f) continue; + if (cov > 1.0f) cov = 1.0f; + sw_put(&row[x], c, (unsigned)((float)c.a * cov + 0.5f)); } } } @@ -692,6 +1007,7 @@ void sw_blit(Canvas *cv, Image img, float x, float y, float w, float h, if (!img.rgba || img.w <= 0 || img.h <= 0 || w <= 0.0f || h <= 0.0f) return; if (alpha <= 0.0f) return; int stride = img.stride > 0 ? img.stride : img.w * 4; + unsigned ga = alpha >= 1.0f ? 255u : (unsigned)(alpha * 255.0f + 0.5f); int ix0 = sw_clampi((int)floorf(x), cv->cx0, cv->cx1); int iy0 = sw_clampi((int)floorf(y), cv->cy0, cv->cy1); @@ -724,8 +1040,13 @@ void sw_blit(Canvas *cv, Image img, float x, float y, float w, float h, } } if (!n || !as) continue; - Col c = { (uint8_t)(rs / n), (uint8_t)(gs / n), (uint8_t)(bs / n), 255 }; - sw_blend(cv, dx, dy, c, (float)(as / n) / 255.0f * alpha); + { + Col c; + unsigned av = as / n; + c.r = (uint8_t)(rs / n); c.g = (uint8_t)(gs / n); + c.b = (uint8_t)(bs / n); c.a = 255; + sw_blend8(cv, dx, dy, c, SW_MUL(av, ga)); + } } } } @@ -779,37 +1100,51 @@ void sw_blit_canvas(Canvas *dst, const Canvas *src, float x, float y, { int ix0, iy0, ix1, iy1, dx, dy, bx, by; float sx, sy; + unsigned ga; if (!dst || !src || !src->px || w <= 0.0f || h <= 0.0f || alpha <= 0.0f) return; + ga = alpha >= 1.0f ? 255u : (unsigned)(alpha * 255.0f + 0.5f); + if (!ga) return; ix0 = sw_clampi((int)floorf(x), dst->cx0, dst->cx1); iy0 = sw_clampi((int)floorf(y), dst->cy0, dst->cy1); ix1 = sw_clampi((int)ceilf(x + w), dst->cx0, dst->cx1); iy1 = sw_clampi((int)ceilf(y + h), dst->cy0, dst->cy1); - /* 1:1 and aligned: a straight premultiplied-over copy, no resampling */ + /* 1:1 and aligned: compose premultiplied over premultiplied, which needs + no un-premultiply and no resampling. This is the backdrop's own path, + so it runs on every frame of a fade. */ if (tint.r == 255 && tint.g == 255 && tint.b == 255 && (int)w == src->w && (int)h == src->h && x == floorf(x) && y == floorf(y)) { int ox = (int)x, oy = (int)y; for (dy = iy0; dy < iy1; ++dy) { const uint32_t *srow; + uint32_t *drow; int sy1 = dy - oy; if (sy1 < 0 || sy1 >= src->h) continue; srow = src->px + (size_t)sy1 * (size_t)src->w; + drow = dst->px + (size_t)dy * (size_t)dst->w; for (dx = ix0; dx < ix1; ++dx) { - uint32_t p; - unsigned av; - Col c; + uint32_t sp, dp; + unsigned sa, sr, sg, sb, ia; int sx1 = dx - ox; if (sx1 < 0 || sx1 >= src->w) continue; - p = srow[sx1]; - av = (p >> 24) & 0xff; - if (!av) continue; - c.r = (uint8_t)sw_clampi((int)((p >> 16 & 0xff) * 255 / av), 0, 255); - c.g = (uint8_t)sw_clampi((int)((p >> 8 & 0xff) * 255 / av), 0, 255); - c.b = (uint8_t)sw_clampi((int)((p & 0xff) * 255 / av), 0, 255); - c.a = 255; - sw_blend(dst, dx, dy, c, (float)av / 255.0f * alpha); + sp = srow[sx1]; + sa = (sp >> 24) & 0xffu; + if (!sa) continue; + sr = (sp >> 16) & 0xffu; sg = (sp >> 8) & 0xffu; sb = sp & 0xffu; + if (ga != 255u) { + sa = SW_MUL(sa, ga); sr = SW_MUL(sr, ga); + sg = SW_MUL(sg, ga); sb = SW_MUL(sb, ga); + if (!sa) continue; + } + if (sa == 255u) { drow[dx] = 0xff000000u | (sr << 16) | (sg << 8) | sb; continue; } + dp = drow[dx]; + ia = 255u - sa; + drow[dx] = ((sa + SW_MUL((dp >> 24) & 0xffu, ia)) << 24) + | ((sr + SW_MUL((dp >> 16) & 0xffu, ia)) << 16) + | ((sg + SW_MUL((dp >> 8) & 0xffu, ia)) << 8) + | (sb + SW_MUL(dp & 0xffu, ia)); } } return; @@ -858,7 +1193,7 @@ void sw_blit_canvas(Canvas *dst, const Canvas *src, float x, float y, c.g = (uint8_t)(c.g * tint.g / 255); c.b = (uint8_t)(c.b * tint.b / 255); } - sw_blend(dst, dx, dy, c, a * alpha); + sw_blend8(dst, dx, dy, c, SW_MUL((unsigned)(as / n), ga)); } } } diff --git a/swas.c b/swas.c index 7ccdf7f..68797d0 100644 --- a/swas.c +++ b/swas.c @@ -57,7 +57,7 @@ typedef struct { float x,y,w,h; } Rect; #define APP_ID "swas" /* wayland app_id / X11 WM_CLASS */ -#define SWAS_VERSION "1.3" +#define SWAS_VERSION "1.3.1" #ifndef SWAS_BUILD /* set by the Makefile: md5 of this file */ #define SWAS_BUILD "unknown" #endif @@ -883,6 +883,11 @@ static int ov_adopt(void){ return 1; } +static uint64_t now_ms_(void){ + struct timespec ts; clock_gettime(CLOCK_MONOTONIC,&ts); + return (uint64_t)ts.tv_sec*1000u + (uint64_t)(ts.tv_nsec/1000000); +} + static void ov_spawn(Config*c){ OV_LOG=c->overview_debug; int to[2], from[2]; @@ -906,6 +911,29 @@ static void ov_spawn(Config*c){ fcntl(OV_OUT,F_SETFL,O_NONBLOCK); } +static void ov_poll(void); + +/* The overview is only wanted once an app is actually being dragged + somewhere, so it is started then rather than at launch: opening the wheel + opens the wheel and nothing else. swov is up in about fifteen + milliseconds, so the wait here is normally over before the drag has moved + far; if it is not, the drag falls back to the plain workspace tiles. */ +static int OV_SPAWNED; +static int ov_bring_up(Config *c, int wait_ms) +{ + uint64_t t0; + if(OV_READY) return 1; + if(!c->overview) return 0; + if(!OV_SPAWNED){ OV_SPAWNED=1; ov_spawn(c); } + if(OV_OUT<0) return 0; + t0=now_ms_(); + while(!OV_READY && (int64_t)(now_ms_()-t0) < wait_ms){ + sw_dispatch(4); + ov_poll(); + } + return OV_READY; +} + /* whatever it has said since the last frame */ static void ov_poll(void){ if(OV_OUT<0) return; @@ -921,12 +949,9 @@ static void ov_poll(void){ *nl='\0'; if(OV_LOG) fprintf(stderr,"swas: <- %s\n",start); if(!strcmp(start,"ready")){ + /* Nothing to raise: the wheel is a layer surface above the + backdrop's, so it is already in front with the keyboard. */ OV_READY=1; - /* On the layer shell we are already above everything, - so there is nothing to raise. */ - /* On Wayland a window cannot lift itself, and the backdrop - mapped on top of us. swov is talking to sway anyway. */ - ov_send("raise " APP_ID); } else if(!strncmp(start,"target ",7)){ /* "target 3", "target 3 current", "target 3 beside 42 left" */ char buf[160]; snprintf(buf,sizeof buf,"%s",start+7); @@ -1013,10 +1038,6 @@ static pid_t WATCH_PID; static double WATCH_AT; static char WATCH_NAME[64]; -static uint64_t now_ms_(void){ - struct timespec ts; clock_gettime(CLOCK_MONOTONIC,&ts); - return (uint64_t)ts.tv_sec*1000u + (uint64_t)(ts.tv_nsec/1000000); -} static double now_secs_(void){ return (double)now_ms_()/1000.0; } static void err_say(const char*fmt,...){ @@ -1517,6 +1538,8 @@ int main(int argc,char**argv){ float arc=cfg.arc_deg*(float)DEG; if(arc<60*DEG)arc=60*DEG; if(arc>330*DEG)arc=330*DEG; float ui=cfg.ui_scale; + int redraw=1; /* something changed, so the frame is worth drawing */ + float want_scale=1.0f, want_corner=0.0f; while(running){ if(!sw_dispatch(0)) break; /* the compositor went away */ @@ -1537,10 +1560,12 @@ int main(int argc,char**argv){ if(want>1.0f) want=1.0f; } float step = cfg.drop_ms>0 ? 16.0f/(float)cfg.drop_ms : 1.0f; + float cwant = dragging ? 1.0f : 0.0f; + want_scale = want; + want_corner = cwant; if(wheel_scale<want){ wheel_scale+=step; if(wheel_scale>want)wheel_scale=want; } else if(wheel_scale>want){ wheel_scale-=step; if(wheel_scale<want)wheel_scale=want; } - float cwant = dragging ? 1.0f : 0.0f; if(corner_t<cwant){ corner_t+=step; if(corner_t>cwant)corner_t=cwant; } else if(corner_t>cwant){ corner_t-=step; if(corner_t<cwant)corner_t=cwant; } } @@ -1629,6 +1654,7 @@ int main(int argc,char**argv){ SwEvent ev; while(sw_poll(&ev)){ + redraw=1; if(ev.type==SW_EV_CLOSED) running=0; else if(ev.type==SW_EV_KB_ENTER) had_focus=1; else if(ev.type==SW_EV_KB_LEAVE){ @@ -1649,12 +1675,10 @@ int main(int argc,char**argv){ if(press_down && !dragging && cfg.drop && press_item>=0){ float dx=mx-press_x, dy=my-press_y; if(dx*dx+dy*dy > (float)(cfg.drop_px*cfg.drop_px)){ + ov_bring_up(&cfg, 300); if(OV_READY){ dragging=1; ov_send("drag on"); - /* the app on the pointer is drawn on this - surface, so it has to be the one in front */ - ov_send("raise " APP_ID); } else { wsp_load(&cfg); /* first drag pays for the list */ @@ -1745,11 +1769,12 @@ int main(int argc,char**argv){ workspace gets rearranged. */ if(p>0) ov_send("assign %d %s",(int)p,OV_TARGET); adopt_to(&cfg,p,OV_TARGET,OV_BESIDE,OV_EDGE); - /* the new window takes the focus as it opens; - the wheel is meant to stay in front of it */ + /* the new window takes the focus as it + opens, but it is an ordinary window and + the wheel is a layer surface above it, so + the wheel stays in front by itself */ if(OV_ONCE) running=0; /* one go */ else if(OV_HERE && cfg.drop_close_here) running=0; - else ov_send("raise " APP_ID); } ov_send("drag off"); OV_TARGET[0]='\0'; OV_HERE=0; @@ -1812,16 +1837,26 @@ int main(int argc,char**argv){ step = arc/(float)(vis>0?vis:1); /* ------------------------------ render ---------------------------- */ - /* Every primitive is antialiased as it is drawn — the sectors and - triangles sample four times a pixel, the rounded rectangles come - from a distance field, the glyphs from stb_truetype — so the old - supersampled render target is gone and `ssaa` is ignored. */ + /* Every primitive is antialiased as it is drawn — the sectors come + out of a distance field, the triangles out of half-plane distances, + the glyphs out of stb_truetype — so the old supersampled render + target is gone and `ssaa` is ignored. + A frame is only drawn when something has changed or something is + still moving: a wheel sitting still costs nothing. */ + { + int moving = (wheel_scale!=want_scale) || (corner_t!=want_corner) + || page_dir!=0 || ERR_TEXT[0]!=0 || WATCH_PID!=0; + if(cfg.animate && cfg.anim_ms>0 && + (int64_t)(now_ms_()-anim0) < (int64_t)cfg.anim_ms) moving=1; + if(!redraw && !moving){ sw_dispatch(16); continue; } + } + Canvas *ren = sw_win_begin(win); if(!ren){ sw_dispatch(8); continue; } /* both buffers still on screen */ /* With swov behind us it draws its own scrim; a second one over the top only makes the thing you are aiming at harder to see. */ - if(!OV_READY) sw_canvas_fill(ren,cfg.bg); + if(!OV_READY) sw_canvas_bg(ren,cfg.bg); float slotw=step; /* everything inside the wheel is measured against this, so shrinking @@ -1980,12 +2015,13 @@ int main(int argc,char**argv){ } sw_win_end(win); + redraw=0; if(!ov_started && cfg.drop){ ov_started=1; /* only now: the wheel is already up */ - /* an overview that started us is already there; otherwise bring - one up ourselves, if we were asked to */ - if(!ov_adopt() && cfg.overview) ov_spawn(&cfg); + /* An overview that started us is already there. One of our own + waits until an app is being dragged — see ov_bring_up. */ + ov_adopt(); } sw_dispatch(16); /* ~60 fps, and the compositor's events */ }