| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220 |
- /* Ring-3 window manager: maps the shadow buffer and each window's content,
- * draws the desktop/chrome and composites the terminal cell grids or gfx
- * canvases. All glyphs and colours are userland; the kernel only routes
- * input, stores window state and copies damage to VRAM. */
- #include "ucmd_lib.h"
- #include "uwm_lib.h"
- #include "version.h"
- #define COL_BG 0x202030u
- /* The active title bar uses the toolkit highlight (ngui_theme_dark.selection);
- * the inactive one stays neutral. Windows have no visible frame. */
- #define COL_TITLE_ON 0x365CA6u
- #define COL_TITLE_OFF 0x646464u
- #define COL_CLOSE 0xC64040u
- #define COL_WHITE 0xFFFFFFu
- #define COL_BANNER 0x55FF55u
- struct uwin {
- struct wm_window_info info;
- struct wm_map_info map;
- int mapped;
- unsigned int ack_seq;
- /* WIN_NORMAL, or maximized/snapped to a screen half; restore_* is the
- * geometry to return to when the window leaves that state. */
- int state;
- int restore_x, restore_y;
- unsigned int restore_w, restore_h;
- };
- /* Double-click detection on the title bar. */
- #define WM_DBLCLICK_MS 500
- #define WM_DBLCLICK_SLOP 8
- /* A maximized window restores only after the pointer actually starts a drag,
- * so a plain click on its title bar leaves it maximized. */
- #define WM_DRAG_START 3
- /* Window placement states. Maximize fills the work area below the bar; the
- * snap states fill its left or right half at full height. */
- #define WIN_NORMAL 0
- #define WIN_MAXIMIZED 1
- #define WIN_SNAP_LEFT 2
- #define WIN_SNAP_RIGHT 3
- /* Releasing a title-bar drag with the pointer this close to the left/right
- * screen edge snaps the window to that half; at the top edge it maximizes. */
- #define WM_SNAP_EDGE 2
- /* Edge resistance: a dragged window's edge stops at a screen edge or at the
- * facing edge of another window until the pointer overshoots it by this
- * many pixels, which makes exact side-by-side placement easy. */
- #define WM_RESIST 24
- static struct usurf screen;
- static struct uwin wins[WM_MAX_WINDOWS];
- static unsigned int fbw, fbh;
- static int drag_id = -1;
- static int drag_dx, drag_dy;
- static int drag_mx, drag_my; /* press position, for restore-on-drag */
- static int drag_moved; /* the pointer passed WM_DRAG_START */
- static int resize_id = -1;
- static int resize_edges; /* WM_EDGE_* bits captured at the press */
- static int resize_ox, resize_oy;
- static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
- static unsigned int last_title_ms;
- static int last_title_id = -1, last_title_x, last_title_y;
- static unsigned int prev_buttons;
- static int full_redraw = 1;
- static int boot_term_spawned;
- static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
- static int bar_temp_c = -1;
- static int bar_batt_pct = -1;
- static int bar_batt_charging;
- static int bar_ac_online = -1;
- /* Battery/AC status icon: a plug on AC, a level-filled battery otherwise.
- * The rightmost bar item; hovering it shows a percentage tooltip below it. */
- #define BAR_PWR_W 24
- #define BAR_PWR_Y 6
- #define BAR_TIP_H 20
- static int bar_pwr_x = -1;
- static int bar_pwr_hot;
- static int bar_pwr_tip_x = -1;
- static int bar_pwr_tip_y;
- static unsigned int bar_pwr_tip_w, bar_pwr_tip_h;
- static int bar_pwr_tip_want; /* tooltip computed for this frame */
- static int bar_pwr_tip_on; /* tooltip pixels currently on screen */
- static int bar_pwr_tip_on_x, bar_pwr_tip_on_y;
- static unsigned int bar_pwr_tip_on_w, bar_pwr_tip_on_h;
- /* Brightness OSD: a centered bubble shown after the kernel's backlight
- * seq changes, then hidden after OSD_MS. */
- #define OSD_MS 3000
- #define OSD_W 210
- #define OSD_H 60
- static int osd_show; /* should be visible this frame */
- static int osd_visible; /* pixels are on screen */
- static int osd_level;
- static unsigned int osd_until;
- static unsigned int osd_seen_seq;
- static int osd_seen_valid;
- static int osd_x, osd_y;
- static unsigned int osd_w, osd_h;
- static char bar_msg[24];
- static unsigned int bar_msg_until;
- static int bar_msg_dirty;
- /* Rectangle of the right-aligned date/time text, so a click there can open
- * the Date & Time app. w == 0 means "not drawn" (e.g. kexec banner). */
- static int bar_clock_x = -1;
- static unsigned int bar_clock_w;
- static int bar_clock_hot;
- static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
- /* Network status icon (right side of the bar). */
- #define BAR_NET_W 24
- #define BAR_NET_Y 6
- #define BAR_NET_NONE 0
- #define BAR_NET_WIRED 1
- #define BAR_NET_WIFI 2
- #define BAR_NET_USB 3
- static int bar_net_x = -1;
- static unsigned int bar_net_w;
- static int bar_net_hot;
- static int bar_net_kind;
- static int bar_net_bars;
- static unsigned int bar_net_color = 0xFF5050u;
- static int control_pid;
- static void bar_notice(const char *msg)
- {
- unsigned int i;
- for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
- bar_msg[i] = msg[i];
- bar_msg[i] = '\0';
- bar_msg_until = u_ticks_ms() + 2000;
- bar_msg_dirty = 1;
- }
- static int free_window_slot(void)
- {
- unsigned int i;
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (!wins[i].info.used)
- return 1;
- return 0;
- }
- static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
- {
- return (r << 16) | (g << 8) | b;
- }
- /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
- * integer interpolation the kernel bar uses. */
- static unsigned int rainbow_rgb(unsigned int hue)
- {
- unsigned int seg = hue / 256, f = hue % 256;
- unsigned int r1, g1, b1, r2, g2, b2;
- switch (seg) {
- case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
- case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
- case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
- case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
- case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
- default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
- }
- return rgb16((r1 * (256 - f) + r2 * f) / 256,
- (g1 * (256 - f) + g2 * f) / 256,
- (b1 * (256 - f) + b2 * f) / 256);
- }
- static void fill_clip(int x, int y, unsigned int w, unsigned int h,
- unsigned int rgb, int cx, int cy,
- unsigned int cw, unsigned int ch)
- {
- int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
- if (x0 < cx) x0 = cx;
- if (y0 < cy) y0 = cy;
- if (x1 > cx + (int)cw) x1 = cx + (int)cw;
- if (y1 > cy + (int)ch) y1 = cy + (int)ch;
- if (x1 > x0 && y1 > y0)
- usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
- (unsigned int)(y1 - y0), rgb);
- }
- static void bar_button(unsigned int x, unsigned int y, unsigned int w,
- unsigned int h, const char *label)
- {
- int tw = (int)usurf_text_width(&screen, label);
- usurf_fill(&screen, x, y, w, h, 0x963CD2u);
- usurf_text(&screen, (int)x + ((int)w - tw) / 2, (int)(y + 3),
- label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
- }
- static int name_is(const char *a, const char *b)
- {
- while (*a && *b) {
- if (*a != *b)
- return 0;
- a++;
- b++;
- }
- return *a == *b;
- }
- static int net_rssi_bars(int rssi)
- {
- if (rssi >= -55)
- return 4;
- if (rssi >= -65)
- return 3;
- if (rssi >= -75)
- return 2;
- return 1;
- }
- /* Small top-bar pictograms, drawn directly into the shadow. */
- static void bar_icon_cable(int x, int y, unsigned int c)
- {
- usurf_fill(&screen, x + 1, y + 6, 9, 8, c);
- usurf_fill(&screen, x + 3, y + 8, 2, 2, COL_BG);
- usurf_fill(&screen, x + 6, y + 8, 2, 2, COL_BG);
- usurf_fill(&screen, x + 10, y + 9, 7, 2, c);
- usurf_fill(&screen, x + 17, y + 5, 5, 10, c);
- usurf_fill(&screen, x + 19, y + 7, 2, 3, COL_BG);
- }
- static void bar_icon_wifi(int x, int y, unsigned int c, int bars)
- {
- int i;
- for (i = 0; i < 4; i++) {
- int h = 3 + i * 3;
- usurf_fill(&screen, x + 3 + i * 5, y + 16 - h, 3, h,
- i < bars ? c : 0x4A4A5Au);
- }
- }
- static void bar_icon_x(int x, int y, unsigned int c)
- {
- int i;
- for (i = 0; i < 12; i++) {
- usurf_fill(&screen, x + 6 + i, y + 4 + i, 2, 2, c);
- usurf_fill(&screen, x + 17 - i, y + 4 + i, 2, 2, c);
- }
- }
- static void bar_icon_usb(int x, int y, unsigned int c)
- {
- usurf_text(&screen, x + 1, y + 1, "USB", c, 0, 0, fbw, WM_BAR_H);
- }
- static void bar_draw_net_icon(int x, int y)
- {
- switch (bar_net_kind) {
- case BAR_NET_WIRED:
- bar_icon_cable(x, y, bar_net_color);
- break;
- case BAR_NET_WIFI:
- bar_icon_wifi(x, y, bar_net_color, bar_net_bars);
- break;
- case BAR_NET_USB:
- bar_icon_usb(x, y, bar_net_color);
- break;
- default:
- bar_icon_x(x, y, bar_net_color);
- break;
- }
- }
- /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
- * only made when WiFi is the active netdev, so a machine without a card does
- * not run a PCI scan on every tick. */
- static int poll_net_status(void)
- {
- struct sys_netinfo ni;
- int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
- unsigned int color = 0xFF5050u;
- int changed;
- int i;
- if (u_net_info(&ni) == 1 && ni.present) {
- for (i = 0; i < 4; i++)
- if (ni.ip[i])
- have_ip = 1;
- if (name_is(ni.name, "wifi")) {
- struct sys_wifi_status ws;
- if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
- ws.present && ws.state >= SYS_WIFI_ST_ASSOC) {
- kind = BAR_NET_WIFI;
- bars = net_rssi_bars(ws.rssi);
- color = (ws.state >= SYS_WIFI_ST_LINK && have_ip) ?
- 0x55FF55u : 0xFFD250u;
- }
- } else if (name_is(ni.name, "rndis")) {
- kind = BAR_NET_USB;
- color = have_ip ? 0x55FF55u : 0xFFD250u;
- } else {
- kind = BAR_NET_WIRED;
- color = have_ip ? 0x55FF55u : 0xFFD250u;
- }
- }
- changed = kind != bar_net_kind || bars != bar_net_bars ||
- color != bar_net_color;
- bar_net_kind = kind;
- bar_net_bars = bars;
- bar_net_color = color;
- return changed;
- }
- static unsigned int temp_color(int c)
- {
- if (c >= 80)
- return 0xFF5050u;
- if (c >= 60)
- return 0xFFD250u;
- return 0x55FF55u;
- }
- /* "43C" (no libc printf here). */
- static unsigned int temp_text(char *b, int c)
- {
- unsigned int n = 0, v = (unsigned int)c;
- if (v >= 100)
- b[n++] = (char)('0' + (v / 100) % 10);
- if (v >= 10)
- b[n++] = (char)('0' + (v / 10) % 10);
- b[n++] = (char)('0' + v % 10);
- b[n++] = 'C';
- b[n] = '\0';
- return n;
- }
- /* "87%" (no libc printf here). */
- static unsigned int pct_text(char *b, int pct)
- {
- unsigned int n = 0, v = (unsigned int)pct;
- if (v >= 100)
- b[n++] = (char)('0' + (v / 100) % 10);
- if (v >= 10)
- b[n++] = (char)('0' + (v / 10) % 10);
- b[n++] = (char)('0' + v % 10);
- b[n++] = '%';
- b[n] = '\0';
- return n;
- }
- static unsigned int batt_color(int pct)
- {
- if (pct <= 10)
- return 0xFF5050u;
- return 0x55FF55u;
- }
- /* Battery pictogram: outline, terminal and a fill proportional to the
- * percentage. A critical charge turns the outline red as well. */
- static void bar_icon_battery(int x, int y, int pct, unsigned int c)
- {
- unsigned int frame = c;
- unsigned int fill = (unsigned int)((14 * pct + 99) / 100);
- usurf_fill(&screen, x + 1, y + 2, 18, 2, frame);
- usurf_fill(&screen, x + 1, y + 14, 18, 2, frame);
- usurf_fill(&screen, x + 1, y + 2, 2, 14, frame);
- usurf_fill(&screen, x + 17, y + 2, 2, 14, frame);
- usurf_fill(&screen, x + 19, y + 6, 3, 6, frame);
- if (fill > 14)
- fill = 14;
- if (fill)
- usurf_fill(&screen, x + 3, y + 4, fill, 10, c);
- }
- /* AC plug pictogram: two prongs and the body, kept compact in height. */
- static void bar_icon_plug(int x, int y, unsigned int c)
- {
- usurf_fill(&screen, x + 7, y + 2, 3, 5, c);
- usurf_fill(&screen, x + 14, y + 2, 3, 5, c);
- usurf_fill(&screen, x + 5, y + 7, 14, 7, c);
- usurf_fill(&screen, x + 7, y + 9, 10, 3, COL_BG);
- }
- /* Percentage tooltip: a small box below the status icon. It is drawn after
- * the windows (see bar_tooltip_overlay), so it always sits on top. */
- static void bar_draw_tooltip(void)
- {
- char lb[8];
- unsigned int lw;
- int tx;
- if (!bar_pwr_tip_want || bar_pwr_tip_x < 0)
- return;
- usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
- bar_pwr_tip_w, bar_pwr_tip_h, 0x161622u);
- usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
- bar_pwr_tip_w, 1, 0x646464u);
- usurf_fill(&screen, bar_pwr_tip_x,
- bar_pwr_tip_y + (int)bar_pwr_tip_h - 1,
- bar_pwr_tip_w, 1, 0x646464u);
- usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
- 1, bar_pwr_tip_h, 0x646464u);
- usurf_fill(&screen, bar_pwr_tip_x + (int)bar_pwr_tip_w - 1,
- bar_pwr_tip_y, 1, bar_pwr_tip_h, 0x646464u);
- (void)pct_text(lb, bar_batt_pct);
- lw = usurf_text_width(&screen, lb);
- tx = bar_pwr_tip_x + ((int)bar_pwr_tip_w - (int)lw) / 2;
- usurf_text(&screen, tx, bar_pwr_tip_y + 3, lb,
- batt_color(bar_batt_pct), 0, 0, fbw, fbh);
- }
- /* Publish the geometry computed by draw_bar() and redraw the tooltip. */
- static void bar_tooltip_commit(void)
- {
- if (bar_pwr_tip_want) {
- bar_draw_tooltip();
- bar_pwr_tip_on = 1;
- bar_pwr_tip_on_x = bar_pwr_tip_x;
- bar_pwr_tip_on_y = bar_pwr_tip_y;
- bar_pwr_tip_on_w = bar_pwr_tip_w;
- bar_pwr_tip_on_h = bar_pwr_tip_h;
- } else {
- bar_pwr_tip_on = 0;
- }
- }
- static void draw_bar(void)
- {
- char dt[48];
- unsigned int n, i, x, y;
- usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
- usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
- 0, 0, fbw, WM_BAR_H);
- bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
- bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
- WM_BTN_CLOCK_H, "Clock");
- bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
- WM_BTN_COLORS_H, "Colors");
- bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
- WM_BTN_FILES_H, "Files");
- bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
- WM_BTN_MINES_H, "Mines");
- bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
- WM_BTN_WEB_H, "Web");
- bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
- WM_BTN_CARDS_H, "Cards");
- bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
- WM_BTN_GALLERY_H, "Gallery");
- bar_button(WM_BTN_CHAT_X, WM_BTN_CHAT_Y, WM_BTN_CHAT_W,
- WM_BTN_CHAT_H, "Chat");
- bar_button(WM_BTN_DISKS_X, WM_BTN_DISKS_Y, WM_BTN_DISKS_W,
- WM_BTN_DISKS_H, "Disks");
- if (bar_msg_until &&
- (int)(u_ticks_ms() - bar_msg_until) < 0)
- usurf_text(&screen, (int)(WM_BTN_END_X + 8),
- 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
- /* Rainbow separator (same y as the kernel's old bar). */
- for (x = 0; x < fbw; x++) {
- unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
- for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
- usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
- }
- if (bar_kexec_busy) {
- static const char msg[] = "loading new kernel over serial";
- unsigned int dots = bar_kexec_total ?
- (bar_kexec_progress * 12 / bar_kexec_total) : 0;
- bar_clock_x = -1;
- bar_clock_w = 0;
- bar_clock_hot = 0;
- bar_net_x = -1;
- bar_net_w = 0;
- bar_pwr_x = -1;
- bar_pwr_tip_x = -1;
- bar_pwr_tip_want = 0;
- n = 0;
- while (msg[n])
- dt[n] = msg[n], n++;
- for (i = 0; i < 12; i++)
- dt[n + i] = (i < dots) ? '.' : ' ';
- n += 12;
- dt[n] = '\0';
- usurf_text(&screen,
- (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
- 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
- } else {
- unsigned int clock_chars = 0;
- int right = (int)fbw - 4;
- int min_x = (int)WM_BTN_END_X + 4;
- bar_clock_x = -1;
- bar_clock_w = 0;
- bar_net_x = -1;
- bar_net_w = 0;
- bar_pwr_x = -1;
- bar_pwr_tip_x = -1;
- bar_pwr_tip_want = 0;
- /* The AC/battery status icon is the rightmost item. */
- if (bar_batt_pct >= 0 || bar_ac_online > 0) {
- int px = right - BAR_PWR_W;
- if (px >= min_x) {
- if (bar_pwr_hot)
- usurf_fill(&screen, px - 3, 2,
- BAR_PWR_W + 6,
- WM_BAR_H - 7, 0x3A3A55u);
- if (bar_ac_online > 0)
- bar_icon_plug(px, BAR_PWR_Y, 0x55FF55u);
- else
- bar_icon_battery(px, BAR_PWR_Y,
- bar_batt_pct,
- batt_color(bar_batt_pct));
- bar_pwr_x = px;
- right = px - 8;
- if (bar_pwr_hot && bar_batt_pct >= 0) {
- char lb[8];
- unsigned int lw, bw;
- int bx;
- (void)pct_text(lb, bar_batt_pct);
- lw = usurf_text_width(&screen, lb);
- bw = lw + 10;
- bx = px + BAR_PWR_W - (int)bw;
- if (bx < 4)
- bx = 4;
- if (bx + (int)bw > (int)fbw - 4)
- bx = (int)fbw - 4 - (int)bw;
- bar_pwr_tip_x = bx;
- bar_pwr_tip_y = WM_BAR_H + 2;
- bar_pwr_tip_w = bw;
- bar_pwr_tip_h = BAR_TIP_H;
- bar_pwr_tip_want = 1;
- }
- }
- }
- /* The network status sits to the left of the power icon, so it
- * survives narrow screens and a full temperature cluster. */
- {
- int nx = right - BAR_NET_W;
- if (nx >= min_x) {
- if (bar_net_hot)
- usurf_fill(&screen, nx - 3, 2,
- BAR_NET_W + 6,
- WM_BAR_H - 7, 0x3A3A55u);
- bar_draw_net_icon(nx, BAR_NET_Y);
- bar_net_x = nx;
- bar_net_w = BAR_NET_W;
- right = nx - 8;
- }
- }
- if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
- int tw = (int)usurf_text_width(&screen, dt);
- int tx = right - tw;
- n = 0;
- while (dt[n] && n < sizeof(dt) - 1)
- n++;
- bar_clock_x = tx;
- bar_clock_w = (unsigned int)tw;
- if (bar_clock_hot)
- usurf_fill(&screen, tx - 5, 2,
- (unsigned int)tw + 10,
- WM_BAR_H - 7, 0x3A3A55u);
- usurf_text(&screen, tx, 5, dt, COL_BANNER, 0, 0,
- fbw, WM_BAR_H);
- clock_chars = (unsigned int)tw;
- }
- if (clock_chars)
- right -= (int)clock_chars + 8;
- if (bar_temp_c >= 0) {
- char tb[12];
- int tx;
- (void)temp_text(tb, bar_temp_c);
- tx = right - (int)usurf_text_width(&screen, tb);
- if (tx >= min_x) {
- usurf_text(&screen, tx, 5, tb,
- temp_color(bar_temp_c), 0, 0,
- fbw, WM_BAR_H);
- right = tx - 8;
- }
- }
- }
- }
- /* A popover is a flat, chromeless panel: the canvas fills the window. */
- static void draw_popover_clip(unsigned int idx, int cx, int cy,
- unsigned int cw, unsigned int ch)
- {
- struct uwin *u = &wins[idx];
- struct wm_window_info *w = &u->info;
- unsigned int cwid = w->content_w, chei = w->content_h;
- if (u->mapped && u->map.kind == WM_KIND_GFX) {
- struct uwm_canvas c;
- c.px = (const unsigned short *)u->map.va;
- c.w = u->map.w;
- c.h = u->map.h;
- if (c.w == cwid && c.h == chei) {
- usurf_canvas(&screen, &c, w->x, w->y,
- cx, cy, cw, ch);
- return;
- }
- }
- fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
- }
- static void draw_window_clip(unsigned int idx, int cx, int cy,
- unsigned int cw, unsigned int ch)
- {
- struct uwin *u = &wins[idx];
- struct wm_window_info *w = &u->info;
- int x = w->x, y = w->y;
- unsigned int pw = w->w;
- int bx = x + (int)pw - WM_BORDER_W - WM_CLOSE_W;
- int cony = y + WM_BORDER_W + WM_TITLE_H;
- int content_x = x + WM_BORDER_W;
- unsigned int content_w = w->content_w;
- unsigned int content_h = w->content_h;
- if (w->popover) {
- draw_popover_clip(idx, cx, cy, cw, ch);
- return;
- }
- /* Title bar and close button; windows have no visible frame. */
- fill_clip(x, y, pw, WM_TITLE_H,
- w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
- usurf_text(&screen, x + 3, y + 2, w->title, COL_WHITE,
- cx, cy, cw, ch);
- fill_clip(bx, y, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE, cx, cy, cw, ch);
- usurf_text(&screen, bx + 4, y + 2, "x", COL_WHITE,
- cx, cy, cw, ch);
- /* Content: every window presents an RGB565 canvas (the terminal
- * renders its cells into one; gfx commands own theirs). A mapped
- * canvas covers the whole content area, so it does not need the black
- * underlay. */
- if (u->mapped && u->map.kind == WM_KIND_GFX) {
- struct uwm_canvas c;
- c.px = (const unsigned short *)u->map.va;
- c.w = u->map.w;
- c.h = u->map.h;
- if (c.w != content_w || c.h != content_h)
- fill_clip(content_x, cony, content_w, content_h,
- 0x000000u, cx, cy, cw, ch);
- usurf_canvas(&screen, &c, content_x, cony,
- cx, cy, cw, ch);
- } else {
- fill_clip(content_x, cony, content_w, content_h,
- 0x000000u, cx, cy, cw, ch);
- }
- }
- /* Front-to-back window order: popovers float above everything, then the
- * focused window, then the others in descending slot order. Painting and
- * hit-testing share it. */
- static unsigned int build_order(int *order)
- {
- unsigned int i, n = 0;
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && wins[i].info.popover)
- order[n++] = (int)i;
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && wins[i].info.focused &&
- !wins[i].info.popover)
- order[n++] = (int)i;
- for (i = WM_MAX_WINDOWS; i-- > 0;)
- if (wins[i].info.used && !wins[i].info.focused &&
- !wins[i].info.popover)
- order[n++] = (int)i;
- return n;
- }
- struct orect {
- int x, y;
- unsigned int w, h;
- };
- #define OCC_MAX 64
- /* r minus cut, split into up to four disjoint rectangles. */
- static unsigned int rect_sub(struct orect *out, const struct orect *r,
- const struct orect *cut)
- {
- int rx0 = r->x, ry0 = r->y;
- int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
- int cx0 = cut->x, cy0 = cut->y;
- int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
- unsigned int n = 0;
- if (cx0 < rx0) cx0 = rx0;
- if (cy0 < ry0) cy0 = ry0;
- if (cx1 > rx1) cx1 = rx1;
- if (cy1 > ry1) cy1 = ry1;
- if (cx1 <= cx0 || cy1 <= cy0) {
- out[0] = *r;
- return 1;
- }
- if (cy0 > ry0) {
- out[n].x = rx0;
- out[n].y = ry0;
- out[n].w = (unsigned int)(rx1 - rx0);
- out[n].h = (unsigned int)(cy0 - ry0);
- n++;
- }
- if (cy1 < ry1) {
- out[n].x = rx0;
- out[n].y = cy1;
- out[n].w = (unsigned int)(rx1 - rx0);
- out[n].h = (unsigned int)(ry1 - cy1);
- n++;
- }
- if (cx0 > rx0) {
- out[n].x = rx0;
- out[n].y = cy0;
- out[n].w = (unsigned int)(cx0 - rx0);
- out[n].h = (unsigned int)(cy1 - cy0);
- n++;
- }
- if (cx1 < rx1) {
- out[n].x = cx1;
- out[n].y = cy0;
- out[n].w = (unsigned int)(rx1 - cx1);
- out[n].h = (unsigned int)(cy1 - cy0);
- n++;
- }
- return n;
- }
- /* Back-to-front repaint, used only if the visible-region list overflows. */
- static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
- {
- unsigned int i;
- usurf_fill(&screen, x, y, w, h, COL_BG);
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && !wins[i].info.focused &&
- !wins[i].info.popover &&
- x < wins[i].info.x + (int)wins[i].info.w &&
- x + (int)w > wins[i].info.x &&
- y < wins[i].info.y + (int)wins[i].info.h &&
- y + (int)h > wins[i].info.y)
- draw_window_clip(i, x, y, w, h);
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && wins[i].info.focused &&
- !wins[i].info.popover &&
- x < wins[i].info.x + (int)wins[i].info.w &&
- x + (int)w > wins[i].info.x &&
- y < wins[i].info.y + (int)wins[i].info.h &&
- y + (int)h > wins[i].info.y)
- draw_window_clip(i, x, y, w, h);
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && wins[i].info.popover &&
- x < wins[i].info.x + (int)wins[i].info.w &&
- x + (int)w > wins[i].info.x &&
- y < wins[i].info.y + (int)wins[i].info.h &&
- y + (int)h > wins[i].info.y)
- draw_window_clip(i, x, y, w, h);
- }
- static void draw_rect(int x, int y, unsigned int w, unsigned int h)
- {
- static struct orect cur[OCC_MAX], nxt[OCC_MAX];
- struct orect dmg;
- int order[WM_MAX_WINDOWS];
- unsigned int n, i, j, ncur, nnext;
- int overflow = 0;
- int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
- if (w == 0 || h == 0)
- return;
- if (x0 < 0) x0 = 0;
- if (y0 < 0) y0 = 0;
- if (x1 > (int)fbw) x1 = (int)fbw;
- if (y1 > (int)fbh) y1 = (int)fbh;
- if (x1 <= x0 || y1 <= y0)
- return;
- dmg.x = x0;
- dmg.y = y0;
- dmg.w = (unsigned int)(x1 - x0);
- dmg.h = (unsigned int)(y1 - y0);
- usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
- n = build_order(order);
- for (i = 0; i < n; i++) {
- struct uwin *u = &wins[order[i]];
- int wx0 = u->info.x, wy0 = u->info.y;
- int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
- if (wx0 < dmg.x) wx0 = dmg.x;
- if (wy0 < dmg.y) wy0 = dmg.y;
- if (wx1 > x1) wx1 = x1;
- if (wy1 > y1) wy1 = y1;
- if (wx1 <= wx0 || wy1 <= wy0)
- continue;
- cur[0].x = wx0;
- cur[0].y = wy0;
- cur[0].w = (unsigned int)(wx1 - wx0);
- cur[0].h = (unsigned int)(wy1 - wy0);
- ncur = 1;
- for (j = 0; j < i && ncur; j++) {
- struct uwin *f = &wins[order[j]];
- struct orect fr;
- unsigned int k;
- fr.x = f->info.x;
- fr.y = f->info.y;
- fr.w = f->info.w;
- fr.h = f->info.h;
- nnext = 0;
- for (k = 0; k < ncur; k++) {
- if (nnext + 4 > OCC_MAX) {
- overflow = 1;
- break;
- }
- nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
- }
- if (overflow)
- break;
- for (k = 0; k < nnext; k++)
- cur[k] = nxt[k];
- ncur = nnext;
- }
- if (overflow)
- break;
- for (j = 0; j < ncur; j++)
- draw_window_clip((unsigned int)order[i], cur[j].x,
- cur[j].y, cur[j].w, cur[j].h);
- }
- if (overflow)
- draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
- }
- /* Invisible resize border: edge bits for a point in the band around the
- * window's outer edge (WM_RESIZE_IN px inside and WM_RESIZE_OUT px outside).
- * Popovers have no chrome and never resize. */
- #define WM_EDGE_LEFT 1
- #define WM_EDGE_RIGHT 2
- #define WM_EDGE_TOP 4
- #define WM_EDGE_BOTTOM 8
- static int resize_edges_at(const struct uwin *u, int mx, int my)
- {
- const struct wm_window_info *w = &u->info;
- int l = w->x, t = w->y;
- int r = l + (int)w->w, b = t + (int)w->h;
- int near_x, near_y, e = 0;
- if (w->popover)
- return 0;
- near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
- near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
- if (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN)
- e |= WM_EDGE_LEFT;
- if (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT)
- e |= WM_EDGE_RIGHT;
- if (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN)
- e |= WM_EDGE_TOP;
- if (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT)
- e |= WM_EDGE_BOTTOM;
- return e;
- }
- /* Topmost window at (mx,my): its resize band when the point is inside one,
- * otherwise the window rectangle. *edges is 0 for a plain rectangle hit.
- * Bands are not considered inside the top bar, which the WM owns. */
- static int resize_hit(int mx, int my, int *edges)
- {
- int order[WM_MAX_WINDOWS];
- unsigned int n = build_order(order), i;
- *edges = 0;
- for (i = 0; i < n; i++) {
- struct uwin *u = &wins[order[i]];
- int e = 0;
- if (!u->info.used)
- continue;
- if (my >= WM_BAR_H)
- e = resize_edges_at(u, mx, my);
- if (e) {
- *edges = e;
- return order[i];
- }
- if (mx >= u->info.x && my >= u->info.y &&
- mx < u->info.x + (int)u->info.w &&
- my < u->info.y + (int)u->info.h)
- return order[i];
- }
- return -1;
- }
- static int resize_cursor(int edges)
- {
- switch (edges) {
- case WM_EDGE_LEFT:
- case WM_EDGE_RIGHT:
- return CURSOR_EW_RESIZE;
- case WM_EDGE_TOP:
- case WM_EDGE_BOTTOM:
- return CURSOR_NS_RESIZE;
- case WM_EDGE_LEFT | WM_EDGE_TOP:
- case WM_EDGE_RIGHT | WM_EDGE_BOTTOM:
- return CURSOR_NWSE_RESIZE;
- case WM_EDGE_RIGHT | WM_EDGE_TOP:
- case WM_EDGE_LEFT | WM_EDGE_BOTTOM:
- return CURSOR_NESW_RESIZE;
- default:
- return CURSOR_DEFAULT;
- }
- }
- /* Close button hit area (the top-right WM_CLOSE_W columns of the title bar). */
- static int close_hit(const struct wm_window_info *w, int mx, int my)
- {
- if (w->popover)
- return 0;
- return mx >= w->x + (int)w->w - WM_CLOSE_W &&
- mx < w->x + (int)w->w &&
- my >= w->y && my < w->y + WM_TITLE_H;
- }
- static void clamp_geom(struct uwin *u, int *x, int *y)
- {
- /* Match wm_clamp_window(): allow the window to hang off the left,
- * right and bottom edges but keep the title bar reachable and below
- * the system bar. */
- int minx = WM_DRAG_VISIBLE - (int)u->info.w;
- int maxx = (int)fbw - WM_DRAG_VISIBLE;
- int miny = WM_BAR_H;
- int maxy = (int)fbh - WM_BORDER_W - WM_TITLE_H;
- if (minx > maxx)
- minx = maxx = 0;
- if (miny > maxy)
- miny = maxy = WM_BAR_H;
- if (*x < minx)
- *x = minx;
- else if (*x > maxx)
- *x = maxx;
- if (*y < miny)
- *y = miny;
- else if (*y > maxy)
- *y = maxy;
- }
- static void add_rect(struct wm_present_info *p, int x, int y,
- unsigned int w, unsigned int h)
- {
- if (w == 0 || h == 0)
- return;
- if (p->count < 4) {
- p->rect[p->count].x = x;
- p->rect[p->count].y = y;
- p->rect[p->count].w = w;
- p->rect[p->count].h = h;
- p->count++;
- } else {
- /* Four is enough for normal updates; fold the rest into the
- * last rect rather than dropping pixels. */
- int x0 = p->rect[3].x, y0 = p->rect[3].y;
- int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
- int nx1 = x + (int)w, ny1 = y + (int)h;
- if (x < x0) x0 = x;
- if (y < y0) y0 = y;
- if (nx1 > x1) x1 = nx1;
- if (ny1 > y1) y1 = ny1;
- p->rect[3].x = x0;
- p->rect[3].y = y0;
- p->rect[3].w = (unsigned int)(x1 - x0);
- p->rect[3].h = (unsigned int)(y1 - y0);
- }
- }
- /* Redraw the power tooltip above whatever a render pass just painted. */
- static void bar_tooltip_overlay(struct wm_present_info *p)
- {
- if (!bar_pwr_tip_on || !bar_pwr_tip_want)
- return;
- bar_draw_tooltip();
- add_rect(p, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
- bar_pwr_tip_on_w, bar_pwr_tip_on_h);
- }
- /* Center the brightness bubble in the work area, clamped on small screens. */
- static void osd_geom(void)
- {
- int x, y;
- osd_w = OSD_W;
- osd_h = OSD_H;
- x = ((int)fbw - (int)osd_w) / 2;
- y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
- if (x < 0)
- x = 0;
- if (x + (int)osd_w > (int)fbw)
- x = (int)fbw - (int)osd_w;
- if (y < WM_BAR_H)
- y = WM_BAR_H;
- if (y + (int)osd_h > (int)fbh)
- y = (int)fbh - (int)osd_h;
- osd_x = x;
- osd_y = y;
- }
- /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
- static void osd_sun(int cx, int cy)
- {
- static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
- int i;
- for (i = -5; i <= 5; i++)
- usurf_fill(&screen, cx - half[i + 5], cy + i,
- half[i + 5] * 2 + 1, 1, 0xFFD250u);
- usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
- usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
- usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
- usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
- }
- static void osd_draw(void)
- {
- char pct[8];
- unsigned int n = 0;
- unsigned int bar_x, bar_y, bar_w;
- int x, y, tw;
- osd_geom();
- x = osd_x;
- y = osd_y;
- usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
- usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
- usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
- usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
- usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
- 0x70709Au);
- osd_sun(x + 22, y + (int)osd_h / 2);
- if (osd_level >= 100) {
- pct[n++] = '1';
- pct[n++] = '0';
- pct[n++] = '0';
- } else {
- if (osd_level >= 10)
- pct[n++] = (char)('0' + osd_level / 10);
- pct[n++] = (char)('0' + osd_level % 10);
- }
- pct[n++] = '%';
- pct[n] = '\0';
- usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
- x, y, osd_w, osd_h);
- tw = (int)usurf_text_width(&screen, pct);
- usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
- 0xFFD250u, x, y, osd_w, osd_h);
- bar_x = (unsigned int)(x + 42);
- bar_y = (unsigned int)(y + 35);
- bar_w = osd_w - 42 - 14;
- usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
- usurf_fill(&screen, (int)bar_x, (int)bar_y,
- (bar_w * (unsigned int)osd_level + 50) / 100, 10,
- 0xFFD250u);
- }
- /* Put the power tooltip and the brightness OSD back on top after a render
- * pass that may have overwritten them. */
- static void wm_overlays(struct wm_present_info *p)
- {
- bar_tooltip_overlay(p);
- if (osd_show) {
- osd_draw();
- add_rect(p, osd_x, osd_y, osd_w, osd_h);
- }
- }
- /* A focused window is topmost, so the shadow contains exactly its pixels in
- * the old rectangle. Move that block and repaint only the old part the new
- * rectangle does not cover. When either rectangle is partly off-screen the
- * shadow has no valid pixels for the hidden part, so repaint the new rect
- * instead of copying desktop background into the window. */
- static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
- {
- struct wm_present_info present;
- struct orect oldr, newr, pieces[4];
- unsigned int np, k;
- int w = (int)u->info.w, h = (int)u->info.h;
- int old_on = ox >= 0 && oy >= 0 &&
- ox + w <= (int)fbw && oy + h <= (int)fbh;
- int new_on = nx >= 0 && ny >= 0 &&
- nx + w <= (int)fbw && ny + h <= (int)fbh;
- if (u_wm_begin() != 0)
- return;
- if (old_on && new_on)
- usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
- else
- draw_rect(nx, ny, u->info.w, u->info.h);
- oldr.x = ox;
- oldr.y = oy;
- oldr.w = u->info.w;
- oldr.h = u->info.h;
- newr.x = nx;
- newr.y = ny;
- newr.w = u->info.w;
- newr.h = u->info.h;
- np = rect_sub(pieces, &oldr, &newr);
- for (k = 0; k < np; k++)
- draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
- present.count = 0;
- present.immediate = 1;
- add_rect(&present, nx, ny, u->info.w, u->info.h);
- for (k = 0; k < np; k++)
- add_rect(&present, pieces[k].x, pieces[k].y,
- pieces[k].w, pieces[k].h);
- wm_overlays(&present);
- u_wm_present(&present);
- }
- static int iabs(int v)
- {
- return v < 0 ? -v : v;
- }
- /* Repaint the old and new rectangles of a geometry change. */
- static void redraw_geom(struct uwin *u, int ox, int oy,
- unsigned int ow, unsigned int oh)
- {
- struct wm_present_info present;
- unsigned int i;
- present.count = 0;
- present.immediate = 1;
- add_rect(&present, ox, oy, ow, oh);
- add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
- if (u_wm_begin() == 0) {
- for (i = 0; i < present.count; i++)
- draw_rect(present.rect[i].x, present.rect[i].y,
- present.rect[i].w, present.rect[i].h);
- wm_overlays(&present);
- u_wm_present(&present);
- }
- }
- /* Resize (when needed), move and remap a window in one step. */
- static int set_window_geometry(struct uwin *u, unsigned int idx,
- int nx, int ny, unsigned int nw, unsigned int nh)
- {
- struct wm_window_info ni;
- int ox = u->info.x, oy = u->info.y;
- unsigned int ow = u->info.w, oh = u->info.h;
- if ((int)nw != (int)ow || (int)nh != (int)oh) {
- if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
- nh - WM_TITLE_H - 2 * WM_BORDER_W) != 0)
- return -1;
- }
- if (nx != ox || ny != oy)
- u_wm_set_geom(idx, nx, ny);
- u->mapped = 0;
- if (u_wm_info(idx, &ni) == 1)
- u->info = ni;
- if (u_wm_map(idx, &u->map) == 0)
- u->mapped = 1;
- redraw_geom(u, ox, oy, ow, oh);
- return 0;
- }
- static int restore_window(struct uwin *u, unsigned int idx)
- {
- if (u->state == WIN_NORMAL)
- return 0;
- if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
- u->restore_w, u->restore_h) != 0)
- return -1;
- u->state = WIN_NORMAL;
- return 0;
- }
- /* Maximize or snap a window to a half of the work area below the system
- * bar. The restore geometry is recorded only when the window leaves the
- * normal state, so maximize -> snap -> restore returns to the original. */
- static int place_window(struct uwin *u, unsigned int idx, int state)
- {
- unsigned int half = fbw / 2;
- unsigned int h = fbh - WM_BAR_H;
- int x = 0;
- unsigned int w = fbw;
- if (state == u->state)
- return 0;
- if (state == WIN_SNAP_LEFT) {
- w = half;
- } else if (state == WIN_SNAP_RIGHT) {
- x = (int)half;
- w = fbw - half;
- }
- if (u->state == WIN_NORMAL) {
- u->restore_x = u->info.x;
- u->restore_y = u->info.y;
- u->restore_w = u->info.w;
- u->restore_h = u->info.h;
- }
- if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
- return -1;
- u->state = state;
- return 0;
- }
- /* Double-click title bar: fill the work area below the system bar, or go back
- * to the geometry the window had before it was maximized. A snapped window
- * maximizes (keeping its pre-snap restore geometry). */
- static int toggle_maximize(struct uwin *u, unsigned int idx)
- {
- if (u->state == WIN_MAXIMIZED)
- return restore_window(u, idx);
- return place_window(u, idx, WIN_MAXIMIZED);
- }
- /* One axis of edge resistance. lo/len is the window's span on that axis
- * before the move, *pos the proposed start. dir < 0: the window's start side
- * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
- * its end side (right/bottom) while moving higher. Within WM_RESIST pixels
- * of overshoot the window stays on the edge; *best keeps the nearest edge. */
- static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
- {
- int over;
- if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
- over = e - *pos;
- if (over <= WM_RESIST && over < *best) {
- *best = over;
- *pos = e;
- }
- } else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
- over = *pos + len - e;
- if (over <= WM_RESIST && over < *best) {
- *best = over;
- *pos = e - len;
- }
- }
- }
- /* Span overlap test: edges of another window only resist when the two
- * windows share some extent on the other axis (they would actually touch). */
- static int spans_overlap(int a, int alen, int b, int blen)
- {
- return a < b + blen && b < a + alen;
- }
- /* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
- * screen edges and the facing edges of every other window. The proposed
- * position is still pure pointer arithmetic, so once the overshoot passes
- * WM_RESIST the window jumps to follow the pointer again. */
- static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
- {
- int ox = u->info.x, oy = u->info.y;
- int w = (int)u->info.w, h = (int)u->info.h;
- int px = *nx, py = *ny;
- int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
- int i;
- resist_edge(ox, w, &px, 0, -1, &bestx);
- resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
- resist_edge(oy, h, &py, (int)fbh, 1, &besty);
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct wm_window_info *o = &wins[i].info;
- int t;
- if (i == idx || !o->used)
- continue;
- if (spans_overlap(oy, h, o->y, (int)o->h)) {
- /* our left side against its right, our right
- * side against its left */
- t = *nx;
- resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
- resist_edge(ox, w, &t, o->x, 1, &bestx);
- if (t != *nx)
- px = t;
- }
- if (spans_overlap(ox, w, o->x, (int)o->w)) {
- t = *ny;
- resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
- resist_edge(oy, h, &t, o->y, 1, &besty);
- if (t != *ny)
- py = t;
- }
- }
- *nx = px;
- *ny = py;
- }
- /* The top-bar date/time text is also a button: it opens the Date & Time
- * editor. The rectangle is recorded by draw_bar(). */
- static int bar_clock_hit(unsigned int mx, unsigned int my)
- {
- return bar_clock_w && bar_clock_x >= 0 && my < WM_BAR_H &&
- (int)mx >= bar_clock_x - 5 &&
- (int)mx < bar_clock_x + (int)bar_clock_w + 5;
- }
- static int bar_net_hit(unsigned int mx, unsigned int my)
- {
- return bar_net_w && bar_net_x >= 0 && my < WM_BAR_H &&
- (int)mx >= bar_net_x - 3 &&
- (int)mx < bar_net_x + (int)bar_net_w + 3;
- }
- /* The AC/battery icon opens the control panel on a click; hovering it also
- * reveals the percentage tooltip below it. The tooltip area keeps the hover
- * alive but is not a click target. */
- static int bar_pwr_hit(unsigned int mx, unsigned int my)
- {
- if (my >= WM_BAR_H)
- return bar_pwr_tip_on &&
- (int)mx >= bar_pwr_tip_on_x &&
- (int)mx < bar_pwr_tip_on_x + (int)bar_pwr_tip_on_w &&
- (int)my >= bar_pwr_tip_on_y &&
- (int)my < bar_pwr_tip_on_y + (int)bar_pwr_tip_on_h;
- return bar_pwr_x >= 0 &&
- (int)mx >= bar_pwr_x - 3 &&
- (int)mx < bar_pwr_x + BAR_PWR_W + 3;
- }
- /* The whole right status area is one button: the clock, temperature, network
- * and power items plus the gaps between them. The launcher buttons end at
- * WM_BTN_END_X, so this never overlaps them. */
- static int bar_status_hit(unsigned int mx, unsigned int my)
- {
- return my < WM_BAR_H && (int)mx >= WM_BTN_END_X + 4;
- }
- static void do_update(struct wm_status *st)
- {
- struct wm_present_info present;
- int need = 0;
- int bar_dirty = 0;
- unsigned int i;
- char dt[20];
- static char last_dt[20];
- int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
- (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
- unsigned int msg_now = u_ticks_ms();
- int osd_hide = 0, osd_repaint = 0;
- /* The kernel bumps backlight_seq on every brightness key/command.
- * Pop the OSD on a change and keep it up for OSD_MS after the last
- * one. The first poll only records the current sequence, so a WM
- * restart does not flash a stale bubble. */
- if (st->backlight_present) {
- if (!osd_seen_valid) {
- osd_seen_valid = 1;
- osd_seen_seq = st->backlight_seq;
- } else if (st->backlight_seq != osd_seen_seq) {
- osd_seen_seq = st->backlight_seq;
- osd_level = (int)st->backlight_level;
- osd_until = msg_now + OSD_MS;
- osd_show = 1;
- }
- } else if (osd_show) {
- osd_show = 0;
- osd_hide = 1;
- }
- if (osd_show && (int)(msg_now - osd_until) >= 0) {
- osd_show = 0;
- osd_hide = 1;
- }
- if (osd_hide && osd_visible)
- osd_repaint = 1;
- /* Spawn the first terminal once the kernel says the storage scan is
- * done, so a shell command can never race the mount table. */
- if (st->boot_ready && !boot_term_spawned) {
- boot_term_spawned = 1;
- if (!free_window_slot()) {
- bar_notice("window limit");
- full_redraw = 1;
- } else if (u_wm_create_term() < 0) {
- bar_notice("cannot start term");
- full_redraw = 1;
- } else {
- full_redraw = 1;
- }
- }
- if (bar_msg_dirty ||
- (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
- bar_dirty = 1;
- bar_msg_dirty = 0;
- if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
- bar_msg_until = 0;
- }
- bar_kexec_busy = st->kexec_busy;
- bar_kexec_progress = st->kexec_progress;
- bar_kexec_total = st->kexec_total;
- if (kexec_changed)
- bar_dirty = 1;
- {
- int hot = bar_clock_hit(st->mouse.x, st->mouse.y);
- if (hot != bar_clock_hot) {
- bar_clock_hot = hot;
- bar_dirty = 1;
- }
- }
- {
- int hot = bar_net_hit(st->mouse.x, st->mouse.y);
- if (hot != bar_net_hot) {
- bar_net_hot = hot;
- bar_dirty = 1;
- }
- }
- {
- int hot = bar_pwr_hit(st->mouse.x, st->mouse.y);
- if (hot != bar_pwr_hot) {
- bar_pwr_hot = hot;
- bar_dirty = 1;
- }
- }
- /* Refresh cached window state and mappings before opening a frame:
- * MAP may unmap/remap content. */
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct wm_window_info info;
- int r = u_wm_info(i, &info);
- if (r < 0) {
- u_exit(1);
- }
- if (r == 0 || !info.used) {
- if (wins[i].info.used)
- full_redraw = 1;
- wins[i].info.used = 0;
- wins[i].mapped = 0;
- continue;
- }
- {
- int pid_changed = wins[i].info.used &&
- info.pid != wins[i].info.pid;
- if (!wins[i].info.used ||
- info.content_gen != wins[i].info.content_gen ||
- pid_changed)
- full_redraw = 1;
- wins[i].info = info;
- if (!wins[i].mapped || pid_changed ||
- wins[i].map.generation != info.content_gen) {
- struct wm_map_info map;
- if (u_wm_map(i, &map) == 0) {
- wins[i].map = map;
- wins[i].mapped = 1;
- } else {
- wins[i].mapped = 0;
- }
- }
- }
- }
- /* Popovers are WM-anchored overlays, flush against the bottom of the
- * bar and the right screen edge. The WM owns the geometry, so the app
- * does not need a placement ABI. */
- {
- int control_found = 0;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct wm_window_info ni;
- int nx, ny;
- if (!wins[i].info.used || !wins[i].info.popover)
- continue;
- if (control_pid > 0 &&
- wins[i].info.pid == (unsigned int)control_pid)
- control_found = 1;
- nx = (int)fbw - (int)wins[i].info.w;
- ny = WM_BAR_H;
- if (nx != wins[i].info.x || ny != wins[i].info.y) {
- if (u_wm_set_geom(i, nx, ny) == 0 &&
- u_wm_info(i, &ni) == 1) {
- wins[i].info = ni;
- full_redraw = 1;
- }
- }
- }
- if (!control_found && control_pid > 0 &&
- !u_alive((unsigned int)control_pid))
- control_pid = 0;
- }
- /* The RTC is slow (CMOS polling) and the temperature/battery state only
- * changes once a second; query them at 1 Hz instead of every 10 ms. */
- {
- static unsigned int last_rtc_ms;
- unsigned int now_ms = u_ticks_ms();
- if (full_redraw || now_ms - last_rtc_ms >= 1000) {
- last_rtc_ms = now_ms;
- if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
- for (i = 0; i < sizeof(dt); i++)
- if (dt[i] != last_dt[i]) {
- for (i = 0; i < sizeof(dt); i++)
- last_dt[i] = dt[i];
- bar_dirty = 1;
- break;
- }
- }
- {
- struct sys_cputemp ct;
- if (u_cpu_temp(&ct) == 0 &&
- ct.temp_c != bar_temp_c) {
- bar_temp_c = ct.temp_c;
- bar_dirty = 1;
- }
- }
- {
- struct sys_powerinfo pi;
- int pct = -1, charging = 0, ac = -1;
- unsigned int bi;
- if (u_power_info(&pi, 0) == 0) {
- /* A laptop EC (ThinkPad or Chrome
- * EC) marks a machine with AC/battery;
- * desktops show neither. */
- if (pi.present) {
- ac = pi.ac_online;
- for (bi = 0; bi < SYS_POWER_BATT_MAX;
- bi++) {
- struct sys_battery *b =
- &pi.battery[bi];
- if (!b->present ||
- !b->percent_valid)
- continue;
- pct = (int)b->percent;
- charging = (int)b->charging;
- break;
- }
- }
- if (pct != bar_batt_pct ||
- charging != bar_batt_charging ||
- ac != bar_ac_online) {
- bar_batt_pct = pct;
- bar_batt_charging = charging;
- bar_ac_online = ac;
- bar_dirty = 1;
- }
- }
- }
- if (poll_net_status())
- bar_dirty = 1;
- }
- }
- if (full_redraw) {
- if (u_wm_begin() != 0) {
- full_redraw = 1;
- return;
- }
- full_redraw = 0;
- need = 1;
- {
- draw_rect(0, 0, fbw, fbh);
- draw_bar();
- bar_tooltip_commit();
- if (osd_show)
- osd_draw();
- osd_visible = osd_show;
- present.count = 0;
- add_rect(&present, 0, 0, fbw, fbh);
- present.immediate = 0;
- u_wm_present(&present);
- /* Ack every window that was clean at the time of the
- * redraw. If new damage arrived during it, its seq
- * differs and the kernel leaves it dirty. */
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used && wins[i].mapped)
- u_wm_ack(i, wins[i].info.dirty_seq);
- }
- return;
- }
- /* Partial cell/canvas updates and the bar clock. */
- present.count = 0;
- present.immediate = 0;
- if (osd_repaint) {
- add_rect(&present, osd_x, osd_y, osd_w, osd_h);
- need = 1;
- }
- if (osd_show)
- need = 1;
- if (bar_dirty) {
- need = 1;
- add_rect(&present, 0, 0, fbw, WM_BAR_H);
- }
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct uwin *u = &wins[i];
- struct wm_window_info *w = &u->info;
- int rx, ry;
- unsigned int rw, rh;
- if (!w->used || !w->dirty)
- continue;
- if (!w->dmg_w || !w->dmg_h)
- continue;
- rx = w->x + (w->popover ? 0 : WM_BORDER_W) + (int)w->dmg_x;
- ry = w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H) +
- (int)w->dmg_y;
- rw = w->dmg_w;
- rh = w->dmg_h;
- add_rect(&present, rx, ry, rw, rh);
- u->ack_seq = u->mapped ? w->dirty_seq : 0;
- need = 1;
- }
- /* Repaint the previous tooltip area so hiding or moving it restores
- * the window/desktop underneath; the new one is drawn after the
- * windows below. */
- if (bar_pwr_tip_on) {
- need = 1;
- add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
- bar_pwr_tip_on_w, bar_pwr_tip_on_h);
- }
- if (!need) {
- /* Drag movement is handled below; a stationary mouse can be
- * left to the kernel's cursor overlay. */
- return;
- }
- if (u_wm_begin() != 0)
- return;
- for (i = 0; i < present.count; i++)
- draw_rect(present.rect[i].x, present.rect[i].y,
- present.rect[i].w, present.rect[i].h);
- if (bar_dirty)
- draw_bar();
- if (bar_pwr_tip_want) {
- bar_tooltip_commit();
- add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
- bar_pwr_tip_on_w, bar_pwr_tip_on_h);
- } else {
- bar_pwr_tip_on = 0;
- }
- if (osd_show) {
- osd_draw();
- add_rect(&present, osd_x, osd_y, osd_w, osd_h);
- }
- osd_visible = osd_show;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct uwin *u = &wins[i];
- if (!u->info.used || !u->info.dirty || !u->mapped ||
- !u->ack_seq)
- continue;
- if (u->ack_seq == u->info.dirty_seq)
- u_wm_ack(i, u->info.dirty_seq);
- }
- u_wm_present(&present);
- }
- static int spawn_app(const char *path)
- {
- int pid = u_spawn(path, "", 0, "/");
- if (pid > 0)
- full_redraw = 1;
- return pid;
- }
- static void spawn_app_checked(const char *path)
- {
- if (!free_window_slot()) {
- bar_notice("window limit");
- full_redraw = 1;
- return;
- }
- if (spawn_app(path) < 0) {
- bar_notice("spawn failed");
- full_redraw = 1;
- }
- }
- /* Any status target opens one control panel; a second click closes it. */
- static void open_control_panel(void)
- {
- unsigned int i;
- if (control_pid > 0 && u_alive((unsigned int)control_pid)) {
- for (i = 0; i < WM_MAX_WINDOWS; i++)
- if (wins[i].info.used &&
- wins[i].info.pid == (unsigned int)control_pid) {
- if (u_wm_close(i) == 0) {
- control_pid = 0;
- full_redraw = 1;
- } else {
- u_wm_focus(i);
- full_redraw = 1;
- }
- return;
- }
- /* The process is alive but has not created its window yet. */
- return;
- }
- if (!free_window_slot()) {
- bar_notice("window limit");
- full_redraw = 1;
- return;
- }
- control_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
- if (control_pid < 0) {
- control_pid = 0;
- bar_notice("spawn failed");
- full_redraw = 1;
- } else {
- full_redraw = 1;
- }
- }
- /* Index of a popover containing the point, or -1. */
- static int popover_under(unsigned int mx, unsigned int my)
- {
- unsigned int i;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- struct wm_window_info *w = &wins[i].info;
- if (w->used && w->popover &&
- mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
- mx < (unsigned int)(w->x + (int)w->w) &&
- my < (unsigned int)(w->y + (int)w->h))
- return (int)i;
- }
- return -1;
- }
- /* A press anywhere outside an open popover dismisses it; the click then
- * continues normally so the target under the pointer still reacts. */
- static void dismiss_popover_outside(unsigned int mx, unsigned int my)
- {
- unsigned int i;
- if (popover_under(mx, my) >= 0)
- return;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- if (!wins[i].info.used || !wins[i].info.popover)
- continue;
- if (wins[i].info.pid == (unsigned int)control_pid)
- control_pid = 0;
- if (u_wm_close(i) == 0) {
- wins[i].info.used = 0;
- wins[i].mapped = 0;
- full_redraw = 1;
- }
- break;
- }
- }
- /* Top-bar launcher under (mx, my)? Same rectangles as the click chain in
- * handle_mouse(); every launcher shares WM_BTN_Y/WM_BTN_H. */
- static int bar_launcher_hit(unsigned int mx, unsigned int my)
- {
- static const int x0[] = {
- WM_BTN_X, WM_BTN_CLOCK_X, WM_BTN_COLORS_X, WM_BTN_FILES_X,
- WM_BTN_MINES_X, WM_BTN_WEB_X, WM_BTN_CARDS_X, WM_BTN_GALLERY_X,
- WM_BTN_CHAT_X, WM_BTN_DISKS_X
- };
- static const int w[] = {
- WM_BTN_W, WM_BTN_CLOCK_W, WM_BTN_COLORS_W, WM_BTN_FILES_W,
- WM_BTN_MINES_W, WM_BTN_WEB_W, WM_BTN_CARDS_W, WM_BTN_GALLERY_W,
- WM_BTN_CHAT_W, WM_BTN_DISKS_W
- };
- unsigned int i;
- if ((int)my < WM_BTN_Y || (int)my >= WM_BTN_Y + WM_BTN_H)
- return 0;
- for (i = 0; i < sizeof(x0) / sizeof(x0[0]); i++)
- if ((int)mx >= x0[i] && (int)mx < x0[i] + w[i])
- return 1;
- return 0;
- }
- /* Apply one edge-drag step. The grabbed edges follow the pointer while the
- * opposite edges stay put; the work area clamps the active edges. */
- static void resize_window_drag(struct uwin *u, unsigned int idx,
- unsigned int mx, unsigned int my)
- {
- int dx = (int)mx - (int)resize_mx;
- int dy = (int)my - (int)resize_my;
- int left = resize_ox;
- int right = resize_ox + (int)resize_ow;
- int top = resize_oy;
- int bottom = resize_oy + (int)resize_oh;
- int nx, ny, nw, nh;
- if (resize_edges & WM_EDGE_LEFT) {
- left = resize_ox + dx;
- if (left < 0)
- left = 0;
- } else if (resize_edges & WM_EDGE_RIGHT) {
- right = resize_ox + (int)resize_ow + dx;
- if (right > (int)fbw)
- right = (int)fbw;
- }
- if (resize_edges & WM_EDGE_TOP) {
- top = resize_oy + dy;
- if (top < WM_BAR_H)
- top = WM_BAR_H;
- } else if (resize_edges & WM_EDGE_BOTTOM) {
- bottom = resize_oy + (int)resize_oh + dy;
- if (bottom > (int)fbh)
- bottom = (int)fbh;
- }
- if ((resize_edges & (WM_EDGE_LEFT | WM_EDGE_RIGHT)) &&
- right - left < 200) {
- if (resize_edges & WM_EDGE_LEFT)
- left = right - 200;
- else
- right = left + 200;
- }
- if ((resize_edges & (WM_EDGE_TOP | WM_EDGE_BOTTOM)) &&
- bottom - top < 140) {
- if (resize_edges & WM_EDGE_TOP)
- top = bottom - 140;
- else
- bottom = top + 140;
- }
- if (left < 0)
- left = 0;
- if (top < WM_BAR_H)
- top = WM_BAR_H;
- nx = left;
- ny = top;
- nw = right - left;
- nh = bottom - top;
- if (nw > (int)fbw)
- nw = (int)fbw;
- if (nh > (int)fbh - WM_BAR_H)
- nh = (int)fbh - WM_BAR_H;
- if (nx == u->info.x && ny == u->info.y &&
- nw == (int)u->info.w && nh == (int)u->info.h)
- return;
- if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
- (unsigned int)nh) == 0)
- u->state = WIN_NORMAL;
- }
- /* Capture the pointer for an edge resize that started at (mx,my). */
- static void begin_resize(unsigned int idx, int edges, unsigned int mx,
- unsigned int my)
- {
- struct uwin *u = &wins[idx];
- resize_id = (int)idx;
- resize_edges = edges;
- resize_mx = mx;
- resize_my = my;
- resize_ox = u->info.x;
- resize_oy = u->info.y;
- resize_ow = u->info.w;
- resize_oh = u->info.h;
- u_wm_mouse_capture(1);
- }
- /* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the
- * invisible resize border and while resizing, the move cross while a
- * title-bar drag is under way, a hand over the launchers and the status
- * area. Everywhere else the kernel picks the window's own content cursor
- * (or the arrow). */
- static void update_cursor(unsigned int mx, unsigned int my)
- {
- static int current = CURSOR_NO_OVERRIDE;
- int want = CURSOR_NO_OVERRIDE;
- if (resize_id >= 0) {
- want = resize_cursor(resize_edges);
- } else if (drag_id >= 0) {
- if (drag_moved)
- want = CURSOR_MOVE;
- } else if (my < WM_BAR_H) {
- if (bar_launcher_hit(mx, my) || bar_status_hit(mx, my))
- want = CURSOR_POINTER;
- } else {
- int edges = 0;
- int idx = resize_hit((int)mx, (int)my, &edges);
- if (idx >= 0 && edges &&
- !close_hit(&wins[idx].info, (int)mx, (int)my))
- want = resize_cursor(edges);
- }
- if (want != current && u_wm_set_cursor(want) == 0)
- current = want;
- }
- static void handle_mouse(struct wm_status *st)
- {
- unsigned int buttons = st->mouse.buttons;
- unsigned int mx = st->mouse.x, my = st->mouse.y;
- int down = (buttons & 1) != 0;
- int was = (prev_buttons & 1) != 0;
- if (down && !was) {
- int prev_title_id = last_title_id;
- unsigned int now = u_ticks_ms();
- if (!bar_status_hit(mx, my))
- dismiss_popover_outside(mx, my);
- last_title_id = -1;
- if (bar_status_hit(mx, my)) {
- open_control_panel();
- } else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
- mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
- if (!free_window_slot()) {
- bar_notice("window limit");
- full_redraw = 1;
- } else if (u_wm_create_term() < 0) {
- bar_notice("cannot start term");
- full_redraw = 1;
- } else {
- full_redraw = 1;
- }
- } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
- mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
- my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
- spawn_app_checked("/ramfs/bin/clock");
- } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
- mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
- my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
- spawn_app_checked("/ramfs/bin/colors");
- } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
- mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
- my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
- spawn_app_checked("/ramfs/bin/files");
- } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
- mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
- my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
- spawn_app_checked("/ramfs/bin/mines");
- } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
- mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
- my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
- spawn_app_checked("/ramfs/bin/browser");
- } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
- mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
- my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
- spawn_app_checked("/ramfs/bin/solitaire");
- } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
- mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
- my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
- spawn_app_checked("/ramfs/bin/gallery");
- } else if (mx >= WM_BTN_CHAT_X && my >= WM_BTN_CHAT_Y &&
- mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
- my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
- spawn_app_checked("/ramfs/bin/chat");
- } else if (mx >= WM_BTN_DISKS_X && my >= WM_BTN_DISKS_Y &&
- mx < WM_BTN_DISKS_X + WM_BTN_DISKS_W &&
- my < WM_BTN_DISKS_Y + WM_BTN_DISKS_H) {
- spawn_app_checked("/ramfs/bin/disks");
- } else {
- int edges = 0;
- int idx = resize_hit((int)mx, (int)my, &edges);
- if (idx >= 0) {
- struct uwin *u = &wins[idx];
- int pmx = (int)mx, pmy = (int)my;
- u_wm_focus((unsigned int)idx);
- if (u->info.popover) {
- /* A popover has no title bar, close
- * button or resize border; the app owns
- * its content. */
- } else if (close_hit(&u->info, pmx, pmy)) {
- u_wm_close((unsigned int)idx);
- full_redraw = 1;
- } else if (edges) {
- begin_resize((unsigned int)idx, edges,
- mx, my);
- } else if (pmy < u->info.y + WM_TITLE_H) {
- if (prev_title_id == idx &&
- now - last_title_ms <= WM_DBLCLICK_MS &&
- iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
- iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
- last_title_ms = 0;
- if (toggle_maximize(u,
- (unsigned int)idx) != 0)
- bar_notice("resize failed");
- } else {
- last_title_id = idx;
- last_title_ms = now;
- last_title_x = pmx;
- last_title_y = pmy;
- drag_id = idx;
- drag_mx = pmx;
- drag_my = pmy;
- drag_dx = pmx - u->info.x;
- drag_dy = pmy - u->info.y;
- drag_moved = 0;
- u_wm_mouse_capture(1);
- }
- }
- }
- }
- } else if (down && was && resize_id >= 0) {
- struct uwin *u = &wins[resize_id];
- if (u->info.used)
- resize_window_drag(u, (unsigned int)resize_id, mx, my);
- else {
- resize_id = -1;
- u_wm_mouse_capture(0);
- }
- } else if (down && was && drag_id >= 0) {
- struct uwin *u = &wins[drag_id];
- if (u->info.used) {
- int ox, oy, nx, ny;
- if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
- iabs((int)my - drag_my) >= WM_DRAG_START)
- drag_moved = 1;
- /* A maximized or snapped window is restored as soon as
- * the drag starts. Keep the grab point at the same
- * fraction of the title bar, so the restored (narrower)
- * window stays under the pointer. A click that does
- * not move must leave it in place. */
- if (u->state != WIN_NORMAL && drag_moved) {
- unsigned int ow = u->info.w;
- int gx = drag_mx - u->info.x;
- int gy = drag_my - u->info.y;
- if (restore_window(u,
- (unsigned int)drag_id) == 0) {
- if (ow)
- gx = gx * (int)u->info.w /
- (int)ow;
- drag_dx = gx;
- drag_dy = gy;
- }
- }
- if (u->state == WIN_NORMAL) {
- ox = u->info.x;
- oy = u->info.y;
- nx = (int)mx - drag_dx;
- ny = (int)my - drag_dy;
- resist_geom(u, drag_id, &nx, &ny);
- clamp_geom(u, &nx, &ny);
- if (nx != ox || ny != oy) {
- struct wm_present_info present;
- unsigned int i;
- u_wm_set_geom((unsigned int)drag_id,
- nx, ny);
- u->info.x = nx;
- u->info.y = ny;
- if (u->info.focused) {
- move_window_pixels(u, ox, oy,
- nx, ny);
- } else {
- present.count = 0;
- present.immediate = 1;
- add_rect(&present, ox, oy,
- u->info.w, u->info.h);
- add_rect(&present, nx, ny,
- u->info.w, u->info.h);
- if (u_wm_begin() == 0) {
- for (i = 0;
- i < present.count; i++)
- draw_rect(
- present.rect[i].x,
- present.rect[i].y,
- present.rect[i].w,
- present.rect[i].h);
- wm_overlays(&present);
- u_wm_present(&present);
- }
- }
- }
- }
- } else {
- drag_id = -1;
- u_wm_mouse_capture(0);
- }
- } else if (!down && was) {
- /* Dropping a window with the pointer on the left/right screen
- * edge snaps it to that half; on the top edge it maximizes. */
- if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&
- wins[drag_id].state == WIN_NORMAL) {
- struct uwin *u = &wins[drag_id];
- int st = WIN_NORMAL;
- if ((int)mx < WM_SNAP_EDGE)
- st = WIN_SNAP_LEFT;
- else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
- st = WIN_SNAP_RIGHT;
- else if ((int)my < WM_SNAP_EDGE)
- st = WIN_MAXIMIZED;
- if (st != WIN_NORMAL &&
- place_window(u, (unsigned int)drag_id, st) != 0)
- bar_notice("resize failed");
- }
- drag_id = -1;
- resize_id = -1;
- u_wm_mouse_capture(0);
- }
- prev_buttons = buttons;
- update_cursor(mx, my);
- }
- static char *put_str(char *p, const char *s)
- {
- while (*s)
- *p++ = *s++;
- return p;
- }
- static char *put_dec(char *p, unsigned int v)
- {
- char b[10];
- unsigned int n = 0;
- do {
- b[n++] = (char)('0' + v % 10);
- v /= 10;
- } while (v);
- while (n)
- *p++ = b[--n];
- return p;
- }
- /* Expose the last interval in /share so the target can be measured without
- * a serial console. */
- static void write_stats(void)
- {
- char b[80], *p = b;
- unsigned int now = u_ticks_ms();
- p = put_str(p, "t=");
- p = put_dec(p, now / 1000);
- p = put_str(p, "s frames=");
- p = put_dec(p, stat_frames);
- p = put_str(p, " ms=");
- p = put_dec(p, stat_ms);
- p = put_str(p, " max=");
- p = put_dec(p, stat_max);
- p = put_str(p, " px=");
- p = put_dec(p, stat_px);
- p = put_str(p, "\n");
- u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
- (int)(p - b));
- }
- void _start(const char *arg)
- {
- struct wm_screen_info si;
- struct wm_status st;
- unsigned int i;
- (void)arg;
- if (u_wm_attach(&si) != 0)
- u_exit(1);
- usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
- usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
- fbw = si.width;
- fbh = si.height;
- for (i = 0; i < WM_MAX_WINDOWS; i++) {
- wins[i].info.used = 0;
- wins[i].mapped = 0;
- }
- full_redraw = 1;
- for (;;) {
- unsigned int t0, t1;
- if (u_wm_poll(&st) != 0)
- u_exit(1);
- if (st.full_redraw)
- full_redraw = 1;
- usurf_pixels = 0;
- t0 = u_ticks_ms();
- handle_mouse(&st);
- do_update(&st);
- t1 = u_ticks_ms();
- if (usurf_pixels) {
- unsigned int dt = t1 - t0;
- stat_frames++;
- stat_ms += dt;
- stat_px += usurf_pixels;
- if (dt > stat_max)
- stat_max = dt;
- }
- if (t1 - stat_last >= 2000) {
- stat_last = t1;
- if (stat_frames) {
- write_stats();
- stat_frames = stat_ms = stat_px = stat_max = 0;
- }
- }
- u_sleep_ms(10);
- }
- }
|