ucmd_wm.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  1. /* Ring-3 window manager: maps the shadow buffer and each window's content,
  2. * draws the desktop/chrome and composites the terminal cell grids or gfx
  3. * canvases. All glyphs and colours are userland; the kernel only routes
  4. * input, stores window state and copies damage to VRAM. */
  5. #include "ucmd_lib.h"
  6. #include "uwm_lib.h"
  7. #include "version.h"
  8. #define COL_BG 0x202030u
  9. /* The active title bar uses the toolkit highlight (ngui_theme_dark.selection);
  10. * the inactive one stays neutral. Windows have no visible frame. */
  11. #define COL_TITLE_ON 0x365CA6u
  12. #define COL_TITLE_OFF 0x646464u
  13. #define COL_CLOSE 0xC64040u
  14. #define COL_WHITE 0xFFFFFFu
  15. #define COL_BANNER 0x55FF55u
  16. struct uwin {
  17. struct wm_window_info info;
  18. struct wm_map_info map;
  19. int mapped;
  20. unsigned int ack_seq;
  21. /* WIN_NORMAL, or maximized/snapped to a screen half; restore_* is the
  22. * geometry to return to when the window leaves that state. */
  23. int state;
  24. int restore_x, restore_y;
  25. unsigned int restore_w, restore_h;
  26. };
  27. /* Double-click detection on the title bar. */
  28. #define WM_DBLCLICK_MS 500
  29. #define WM_DBLCLICK_SLOP 8
  30. /* A maximized window restores only after the pointer actually starts a drag,
  31. * so a plain click on its title bar leaves it maximized. */
  32. #define WM_DRAG_START 3
  33. /* Window placement states. Maximize fills the work area below the bar; the
  34. * snap states fill its left or right half at full height. */
  35. #define WIN_NORMAL 0
  36. #define WIN_MAXIMIZED 1
  37. #define WIN_SNAP_LEFT 2
  38. #define WIN_SNAP_RIGHT 3
  39. /* Releasing a title-bar drag with the pointer this close to the left/right
  40. * screen edge snaps the window to that half; at the top edge it maximizes. */
  41. #define WM_SNAP_EDGE 2
  42. /* Edge resistance: a dragged window's edge stops at a screen edge or at the
  43. * facing edge of another window until the pointer overshoots it by this
  44. * many pixels, which makes exact side-by-side placement easy. */
  45. #define WM_RESIST 24
  46. static struct usurf screen;
  47. static struct uwin wins[WM_MAX_WINDOWS];
  48. static unsigned int fbw, fbh;
  49. static int drag_id = -1;
  50. static int drag_dx, drag_dy;
  51. static int drag_mx, drag_my; /* press position, for restore-on-drag */
  52. static int drag_moved; /* the pointer passed WM_DRAG_START */
  53. static int resize_id = -1;
  54. static int resize_edges; /* WM_EDGE_* bits captured at the press */
  55. static int resize_ox, resize_oy;
  56. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  57. static unsigned int last_title_ms;
  58. static int last_title_id = -1, last_title_x, last_title_y;
  59. static unsigned int prev_buttons;
  60. static int full_redraw = 1;
  61. static int boot_term_spawned;
  62. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  63. static int bar_temp_c = -1;
  64. static int bar_batt_pct = -1;
  65. static int bar_batt_charging;
  66. static int bar_ac_online = -1;
  67. /* Battery/AC status icon: the Adwaita AC adapter on AC, the battery outline
  68. * with our own level fill otherwise. The rightmost bar item; hovering it
  69. * shows a percentage tooltip below it. */
  70. #define BAR_PWR_W 24
  71. #define BAR_PWR_Y 7
  72. #define BAR_TIP_H 20
  73. static int bar_pwr_x = -1;
  74. static int bar_pwr_hot;
  75. static int bar_pwr_tip_x = -1;
  76. static int bar_pwr_tip_y;
  77. static unsigned int bar_pwr_tip_w, bar_pwr_tip_h;
  78. static int bar_pwr_tip_want; /* tooltip computed for this frame */
  79. static int bar_pwr_tip_on; /* tooltip pixels currently on screen */
  80. static int bar_pwr_tip_on_x, bar_pwr_tip_on_y;
  81. static unsigned int bar_pwr_tip_on_w, bar_pwr_tip_on_h;
  82. /* Brightness OSD: a centered bubble shown after the kernel's backlight
  83. * seq changes, then hidden after OSD_MS. */
  84. #define OSD_MS 3000
  85. #define OSD_W 210
  86. #define OSD_H 60
  87. static int osd_show; /* should be visible this frame */
  88. static int osd_visible; /* pixels are on screen */
  89. static int osd_level;
  90. static unsigned int osd_until;
  91. static unsigned int osd_seen_seq;
  92. static int osd_seen_valid;
  93. static int osd_x, osd_y;
  94. static unsigned int osd_w, osd_h;
  95. static char bar_msg[24];
  96. static unsigned int bar_msg_until;
  97. static int bar_msg_dirty;
  98. /* Rectangle of the right-aligned date/time text, so a click there can open
  99. * the Date & Time app. w == 0 means "not drawn" (e.g. kexec banner). */
  100. static int bar_clock_x = -1;
  101. static unsigned int bar_clock_w;
  102. static int bar_clock_hot;
  103. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  104. /* Network status icon (right side of the bar). */
  105. #define BAR_NET_W 24
  106. #define BAR_NET_Y 7
  107. #define BAR_NET_NONE 0
  108. #define BAR_NET_WIRED 1
  109. #define BAR_NET_WIFI 2
  110. #define BAR_NET_USB 3
  111. static int bar_net_x = -1;
  112. static unsigned int bar_net_w;
  113. static int bar_net_hot;
  114. static int bar_net_kind;
  115. static int bar_net_bars;
  116. static unsigned int bar_net_color = 0xFF5050u;
  117. static int control_pid;
  118. static void bar_notice(const char *msg)
  119. {
  120. unsigned int i;
  121. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  122. bar_msg[i] = msg[i];
  123. bar_msg[i] = '\0';
  124. bar_msg_until = u_ticks_ms() + 2000;
  125. bar_msg_dirty = 1;
  126. }
  127. static int free_window_slot(void)
  128. {
  129. unsigned int i;
  130. for (i = 0; i < WM_MAX_WINDOWS; i++)
  131. if (!wins[i].info.used)
  132. return 1;
  133. return 0;
  134. }
  135. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  136. {
  137. return (r << 16) | (g << 8) | b;
  138. }
  139. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  140. * integer interpolation the kernel bar uses. */
  141. static unsigned int rainbow_rgb(unsigned int hue)
  142. {
  143. unsigned int seg = hue / 256, f = hue % 256;
  144. unsigned int r1, g1, b1, r2, g2, b2;
  145. switch (seg) {
  146. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  147. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  148. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  149. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  150. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  151. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  152. }
  153. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  154. (g1 * (256 - f) + g2 * f) / 256,
  155. (b1 * (256 - f) + b2 * f) / 256);
  156. }
  157. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  158. unsigned int rgb, int cx, int cy,
  159. unsigned int cw, unsigned int ch)
  160. {
  161. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  162. if (x0 < cx) x0 = cx;
  163. if (y0 < cy) y0 = cy;
  164. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  165. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  166. if (x1 > x0 && y1 > y0)
  167. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  168. (unsigned int)(y1 - y0), rgb);
  169. }
  170. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  171. unsigned int h, const char *label)
  172. {
  173. int tw = (int)usurf_text_width(&screen, label);
  174. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  175. usurf_text(&screen, (int)x + ((int)w - tw) / 2, (int)(y + 3),
  176. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  177. }
  178. static int name_is(const char *a, const char *b)
  179. {
  180. while (*a && *b) {
  181. if (*a != *b)
  182. return 0;
  183. a++;
  184. b++;
  185. }
  186. return *a == *b;
  187. }
  188. static int net_rssi_bars(int rssi)
  189. {
  190. if (rssi >= -55)
  191. return 4;
  192. if (rssi >= -65)
  193. return 3;
  194. if (rssi >= -75)
  195. return 2;
  196. return 1;
  197. }
  198. /* Small top-bar pictograms: Adwaita symbolic masks from the pack, centered
  199. * in their 24 px hit slots. */
  200. static void bar_draw_net_icon(int x, int y)
  201. {
  202. enum uwm_icon ic;
  203. switch (bar_net_kind) {
  204. case BAR_NET_WIRED:
  205. ic = UWM_ICON_NET_WIRED;
  206. break;
  207. case BAR_NET_WIFI:
  208. ic = bar_net_bars > 0 ? UWM_ICON_NET_WIFI_0 + bar_net_bars :
  209. UWM_ICON_NET_WIFI_OFF;
  210. break;
  211. case BAR_NET_USB:
  212. ic = UWM_ICON_NET_USB;
  213. break;
  214. default:
  215. ic = UWM_ICON_NET_WIRED_OFF;
  216. break;
  217. }
  218. (void)usurf_icon(&screen, x + 4, y, ic, bar_net_color);
  219. }
  220. /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
  221. * only made when WiFi is the active netdev, so a machine without a card does
  222. * not run a PCI scan on every tick. */
  223. static int poll_net_status(void)
  224. {
  225. struct sys_netinfo ni;
  226. int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
  227. unsigned int color = 0xFF5050u;
  228. int changed;
  229. int i;
  230. if (u_net_info(&ni) == 1 && ni.present) {
  231. for (i = 0; i < 4; i++)
  232. if (ni.ip[i])
  233. have_ip = 1;
  234. if (name_is(ni.name, "wifi")) {
  235. struct sys_wifi_status ws;
  236. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
  237. ws.present) {
  238. kind = BAR_NET_WIFI;
  239. if (ws.state >= SYS_WIFI_ST_ASSOC) {
  240. bars = net_rssi_bars(ws.rssi);
  241. color = (ws.state >= SYS_WIFI_ST_LINK &&
  242. have_ip) ?
  243. 0x55FF55u : 0xFFD250u;
  244. } else {
  245. /* Card present, not associated. */
  246. bars = 0;
  247. color = 0xFF5050u;
  248. }
  249. }
  250. } else if (name_is(ni.name, "rndis")) {
  251. kind = BAR_NET_USB;
  252. color = have_ip ? 0x55FF55u : 0xFFD250u;
  253. } else {
  254. kind = BAR_NET_WIRED;
  255. color = have_ip ? 0x55FF55u : 0xFFD250u;
  256. }
  257. }
  258. changed = kind != bar_net_kind || bars != bar_net_bars ||
  259. color != bar_net_color;
  260. bar_net_kind = kind;
  261. bar_net_bars = bars;
  262. bar_net_color = color;
  263. return changed;
  264. }
  265. static unsigned int temp_color(int c)
  266. {
  267. if (c >= 80)
  268. return 0xFF5050u;
  269. if (c >= 60)
  270. return 0xFFD250u;
  271. return 0x55FF55u;
  272. }
  273. /* "43C" (no libc printf here). */
  274. static unsigned int temp_text(char *b, int c)
  275. {
  276. unsigned int n = 0, v = (unsigned int)c;
  277. if (v >= 100)
  278. b[n++] = (char)('0' + (v / 100) % 10);
  279. if (v >= 10)
  280. b[n++] = (char)('0' + (v / 10) % 10);
  281. b[n++] = (char)('0' + v % 10);
  282. b[n++] = 'C';
  283. b[n] = '\0';
  284. return n;
  285. }
  286. /* "87%" (no libc printf here). */
  287. static unsigned int pct_text(char *b, int pct)
  288. {
  289. unsigned int n = 0, v = (unsigned int)pct;
  290. if (v >= 100)
  291. b[n++] = (char)('0' + (v / 100) % 10);
  292. if (v >= 10)
  293. b[n++] = (char)('0' + (v / 10) % 10);
  294. b[n++] = (char)('0' + v % 10);
  295. b[n++] = '%';
  296. b[n] = '\0';
  297. return n;
  298. }
  299. static unsigned int batt_color(int pct)
  300. {
  301. if (pct <= 10)
  302. return 0xFF5050u;
  303. return 0x55FF55u;
  304. }
  305. /* Battery: the Adwaita outline with our own level fill over the lower
  306. * interior. The symbol's body is striped, so the interior is cleared to a
  307. * dim shade of the state colour first; the level then reads as a bright
  308. * block inside the outline. A critical charge turns the outline red too.
  309. * Geometry of battery-symbolic at 16 px: body x2..13, cavity x4..11 and
  310. * y4..13. */
  311. static unsigned int batt_dim(unsigned int c)
  312. {
  313. return (c >> 2) & 0x3F3F3F;
  314. }
  315. static void bar_icon_battery(int x, int y, int pct, unsigned int c)
  316. {
  317. unsigned int fill = (unsigned int)((10 * pct + 99) / 100);
  318. int ix = x + 4;
  319. if (fill > 10)
  320. fill = 10;
  321. if (!usurf_icon(&screen, ix, y, UWM_ICON_BATTERY, c))
  322. return;
  323. usurf_fill(&screen, ix + 4, y + 4, 8, 10, batt_dim(c));
  324. if (fill)
  325. usurf_fill(&screen, ix + 4, y + 14 - (int)fill, 8, fill, c);
  326. }
  327. /* AC adapter: the Adwaita plug symbol. */
  328. static void bar_icon_ac(int x, int y, unsigned int c)
  329. {
  330. (void)usurf_icon(&screen, x + 4, y, UWM_ICON_AC, c);
  331. }
  332. /* Percentage tooltip: a small box below the status icon. It is drawn after
  333. * the windows (see bar_tooltip_overlay), so it always sits on top. */
  334. static void bar_draw_tooltip(void)
  335. {
  336. char lb[8];
  337. unsigned int lw;
  338. int tx;
  339. if (!bar_pwr_tip_want || bar_pwr_tip_x < 0)
  340. return;
  341. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  342. bar_pwr_tip_w, bar_pwr_tip_h, 0x161622u);
  343. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  344. bar_pwr_tip_w, 1, 0x646464u);
  345. usurf_fill(&screen, bar_pwr_tip_x,
  346. bar_pwr_tip_y + (int)bar_pwr_tip_h - 1,
  347. bar_pwr_tip_w, 1, 0x646464u);
  348. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  349. 1, bar_pwr_tip_h, 0x646464u);
  350. usurf_fill(&screen, bar_pwr_tip_x + (int)bar_pwr_tip_w - 1,
  351. bar_pwr_tip_y, 1, bar_pwr_tip_h, 0x646464u);
  352. (void)pct_text(lb, bar_batt_pct);
  353. lw = usurf_text_width(&screen, lb);
  354. tx = bar_pwr_tip_x + ((int)bar_pwr_tip_w - (int)lw) / 2;
  355. usurf_text(&screen, tx, bar_pwr_tip_y + 3, lb,
  356. batt_color(bar_batt_pct), 0, 0, fbw, fbh);
  357. }
  358. /* Publish the geometry computed by draw_bar() and redraw the tooltip. */
  359. static void bar_tooltip_commit(void)
  360. {
  361. if (bar_pwr_tip_want) {
  362. bar_draw_tooltip();
  363. bar_pwr_tip_on = 1;
  364. bar_pwr_tip_on_x = bar_pwr_tip_x;
  365. bar_pwr_tip_on_y = bar_pwr_tip_y;
  366. bar_pwr_tip_on_w = bar_pwr_tip_w;
  367. bar_pwr_tip_on_h = bar_pwr_tip_h;
  368. } else {
  369. bar_pwr_tip_on = 0;
  370. }
  371. }
  372. static void draw_bar(void)
  373. {
  374. char dt[48];
  375. unsigned int n, i, x, y;
  376. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  377. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  378. 0, 0, fbw, WM_BAR_H);
  379. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  380. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  381. WM_BTN_CLOCK_H, "Clock");
  382. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  383. WM_BTN_COLORS_H, "Colors");
  384. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  385. WM_BTN_FILES_H, "Files");
  386. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  387. WM_BTN_MINES_H, "Mines");
  388. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  389. WM_BTN_WEB_H, "Web");
  390. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  391. WM_BTN_CARDS_H, "Cards");
  392. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  393. WM_BTN_GALLERY_H, "Gallery");
  394. bar_button(WM_BTN_CHAT_X, WM_BTN_CHAT_Y, WM_BTN_CHAT_W,
  395. WM_BTN_CHAT_H, "Chat");
  396. bar_button(WM_BTN_DISKS_X, WM_BTN_DISKS_Y, WM_BTN_DISKS_W,
  397. WM_BTN_DISKS_H, "Disks");
  398. if (bar_msg_until &&
  399. (int)(u_ticks_ms() - bar_msg_until) < 0)
  400. usurf_text(&screen, (int)(WM_BTN_END_X + 8),
  401. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  402. /* Rainbow separator (same y as the kernel's old bar). */
  403. for (x = 0; x < fbw; x++) {
  404. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  405. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  406. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  407. }
  408. if (bar_kexec_busy) {
  409. static const char msg[] = "loading new kernel over serial";
  410. unsigned int dots = bar_kexec_total ?
  411. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  412. bar_clock_x = -1;
  413. bar_clock_w = 0;
  414. bar_clock_hot = 0;
  415. bar_net_x = -1;
  416. bar_net_w = 0;
  417. bar_pwr_x = -1;
  418. bar_pwr_tip_x = -1;
  419. bar_pwr_tip_want = 0;
  420. n = 0;
  421. while (msg[n])
  422. dt[n] = msg[n], n++;
  423. for (i = 0; i < 12; i++)
  424. dt[n + i] = (i < dots) ? '.' : ' ';
  425. n += 12;
  426. dt[n] = '\0';
  427. usurf_text(&screen,
  428. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  429. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  430. } else {
  431. unsigned int clock_chars = 0;
  432. int right = (int)fbw - 4;
  433. int min_x = (int)WM_BTN_END_X + 4;
  434. bar_clock_x = -1;
  435. bar_clock_w = 0;
  436. bar_net_x = -1;
  437. bar_net_w = 0;
  438. bar_pwr_x = -1;
  439. bar_pwr_tip_x = -1;
  440. bar_pwr_tip_want = 0;
  441. /* The AC/battery status icon is the rightmost item. */
  442. if (bar_batt_pct >= 0 || bar_ac_online > 0) {
  443. int px = right - BAR_PWR_W;
  444. if (px >= min_x) {
  445. if (bar_pwr_hot)
  446. usurf_fill(&screen, px - 3, 2,
  447. BAR_PWR_W + 6,
  448. WM_BAR_H - 7, 0x3A3A55u);
  449. if (bar_ac_online > 0)
  450. bar_icon_ac(px, BAR_PWR_Y, 0x55FF55u);
  451. else
  452. bar_icon_battery(px, BAR_PWR_Y,
  453. bar_batt_pct,
  454. batt_color(bar_batt_pct));
  455. bar_pwr_x = px;
  456. right = px - 8;
  457. if (bar_pwr_hot && bar_batt_pct >= 0) {
  458. char lb[8];
  459. unsigned int lw, bw;
  460. int bx;
  461. (void)pct_text(lb, bar_batt_pct);
  462. lw = usurf_text_width(&screen, lb);
  463. bw = lw + 10;
  464. bx = px + BAR_PWR_W - (int)bw;
  465. if (bx < 4)
  466. bx = 4;
  467. if (bx + (int)bw > (int)fbw - 4)
  468. bx = (int)fbw - 4 - (int)bw;
  469. bar_pwr_tip_x = bx;
  470. bar_pwr_tip_y = WM_BAR_H + 2;
  471. bar_pwr_tip_w = bw;
  472. bar_pwr_tip_h = BAR_TIP_H;
  473. bar_pwr_tip_want = 1;
  474. }
  475. }
  476. }
  477. /* The network status sits to the left of the power icon, so it
  478. * survives narrow screens and a full temperature cluster. */
  479. {
  480. int nx = right - BAR_NET_W;
  481. if (nx >= min_x) {
  482. if (bar_net_hot)
  483. usurf_fill(&screen, nx - 3, 2,
  484. BAR_NET_W + 6,
  485. WM_BAR_H - 7, 0x3A3A55u);
  486. bar_draw_net_icon(nx, BAR_NET_Y);
  487. bar_net_x = nx;
  488. bar_net_w = BAR_NET_W;
  489. right = nx - 8;
  490. }
  491. }
  492. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  493. int tw = (int)usurf_text_width(&screen, dt);
  494. int tx = right - tw;
  495. n = 0;
  496. while (dt[n] && n < sizeof(dt) - 1)
  497. n++;
  498. bar_clock_x = tx;
  499. bar_clock_w = (unsigned int)tw;
  500. if (bar_clock_hot)
  501. usurf_fill(&screen, tx - 5, 2,
  502. (unsigned int)tw + 10,
  503. WM_BAR_H - 7, 0x3A3A55u);
  504. usurf_text(&screen, tx, 5, dt, COL_BANNER, 0, 0,
  505. fbw, WM_BAR_H);
  506. clock_chars = (unsigned int)tw;
  507. }
  508. if (clock_chars)
  509. right -= (int)clock_chars + 8;
  510. if (bar_temp_c >= 0) {
  511. char tb[12];
  512. int tx;
  513. (void)temp_text(tb, bar_temp_c);
  514. tx = right - (int)usurf_text_width(&screen, tb);
  515. if (tx >= min_x) {
  516. usurf_text(&screen, tx, 5, tb,
  517. temp_color(bar_temp_c), 0, 0,
  518. fbw, WM_BAR_H);
  519. right = tx - 8;
  520. }
  521. }
  522. }
  523. }
  524. /* A popover is a flat, chromeless panel: the canvas fills the window. */
  525. static void draw_popover_clip(unsigned int idx, int cx, int cy,
  526. unsigned int cw, unsigned int ch)
  527. {
  528. struct uwin *u = &wins[idx];
  529. struct wm_window_info *w = &u->info;
  530. unsigned int cwid = w->content_w, chei = w->content_h;
  531. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  532. struct uwm_canvas c;
  533. c.px = (const unsigned short *)u->map.va;
  534. c.w = u->map.w;
  535. c.h = u->map.h;
  536. if (c.w == cwid && c.h == chei) {
  537. usurf_canvas(&screen, &c, w->x, w->y,
  538. cx, cy, cw, ch);
  539. return;
  540. }
  541. }
  542. fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
  543. }
  544. static void draw_window_clip(unsigned int idx, int cx, int cy,
  545. unsigned int cw, unsigned int ch)
  546. {
  547. struct uwin *u = &wins[idx];
  548. struct wm_window_info *w = &u->info;
  549. int x = w->x, y = w->y;
  550. unsigned int pw = w->w;
  551. int bx = x + (int)pw - WM_BORDER_W - WM_CLOSE_W;
  552. int cony = y + WM_BORDER_W + WM_TITLE_H;
  553. int content_x = x + WM_BORDER_W;
  554. unsigned int content_w = w->content_w;
  555. unsigned int content_h = w->content_h;
  556. if (w->popover) {
  557. draw_popover_clip(idx, cx, cy, cw, ch);
  558. return;
  559. }
  560. /* Title bar and close button; windows have no visible frame. */
  561. fill_clip(x, y, pw, WM_TITLE_H,
  562. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  563. usurf_text(&screen, x + 3, y + 2, w->title, COL_WHITE,
  564. cx, cy, cw, ch);
  565. fill_clip(bx, y, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE, cx, cy, cw, ch);
  566. usurf_text(&screen, bx + 4, y + 2, "x", COL_WHITE,
  567. cx, cy, cw, ch);
  568. /* Content: every window presents an RGB565 canvas (the terminal
  569. * renders its cells into one; gfx commands own theirs). A mapped
  570. * canvas covers the whole content area, so it does not need the black
  571. * underlay. */
  572. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  573. struct uwm_canvas c;
  574. c.px = (const unsigned short *)u->map.va;
  575. c.w = u->map.w;
  576. c.h = u->map.h;
  577. if (c.w != content_w || c.h != content_h)
  578. fill_clip(content_x, cony, content_w, content_h,
  579. 0x000000u, cx, cy, cw, ch);
  580. usurf_canvas(&screen, &c, content_x, cony,
  581. cx, cy, cw, ch);
  582. } else {
  583. fill_clip(content_x, cony, content_w, content_h,
  584. 0x000000u, cx, cy, cw, ch);
  585. }
  586. }
  587. /* Front-to-back window order: popovers float above everything, then the
  588. * focused window, then the others in descending slot order. Painting and
  589. * hit-testing share it. */
  590. static unsigned int build_order(int *order)
  591. {
  592. unsigned int i, n = 0;
  593. for (i = 0; i < WM_MAX_WINDOWS; i++)
  594. if (wins[i].info.used && wins[i].info.popover)
  595. order[n++] = (int)i;
  596. for (i = 0; i < WM_MAX_WINDOWS; i++)
  597. if (wins[i].info.used && wins[i].info.focused &&
  598. !wins[i].info.popover)
  599. order[n++] = (int)i;
  600. for (i = WM_MAX_WINDOWS; i-- > 0;)
  601. if (wins[i].info.used && !wins[i].info.focused &&
  602. !wins[i].info.popover)
  603. order[n++] = (int)i;
  604. return n;
  605. }
  606. struct orect {
  607. int x, y;
  608. unsigned int w, h;
  609. };
  610. #define OCC_MAX 64
  611. /* r minus cut, split into up to four disjoint rectangles. */
  612. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  613. const struct orect *cut)
  614. {
  615. int rx0 = r->x, ry0 = r->y;
  616. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  617. int cx0 = cut->x, cy0 = cut->y;
  618. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  619. unsigned int n = 0;
  620. if (cx0 < rx0) cx0 = rx0;
  621. if (cy0 < ry0) cy0 = ry0;
  622. if (cx1 > rx1) cx1 = rx1;
  623. if (cy1 > ry1) cy1 = ry1;
  624. if (cx1 <= cx0 || cy1 <= cy0) {
  625. out[0] = *r;
  626. return 1;
  627. }
  628. if (cy0 > ry0) {
  629. out[n].x = rx0;
  630. out[n].y = ry0;
  631. out[n].w = (unsigned int)(rx1 - rx0);
  632. out[n].h = (unsigned int)(cy0 - ry0);
  633. n++;
  634. }
  635. if (cy1 < ry1) {
  636. out[n].x = rx0;
  637. out[n].y = cy1;
  638. out[n].w = (unsigned int)(rx1 - rx0);
  639. out[n].h = (unsigned int)(ry1 - cy1);
  640. n++;
  641. }
  642. if (cx0 > rx0) {
  643. out[n].x = rx0;
  644. out[n].y = cy0;
  645. out[n].w = (unsigned int)(cx0 - rx0);
  646. out[n].h = (unsigned int)(cy1 - cy0);
  647. n++;
  648. }
  649. if (cx1 < rx1) {
  650. out[n].x = cx1;
  651. out[n].y = cy0;
  652. out[n].w = (unsigned int)(rx1 - cx1);
  653. out[n].h = (unsigned int)(cy1 - cy0);
  654. n++;
  655. }
  656. return n;
  657. }
  658. /* Back-to-front repaint, used only if the visible-region list overflows. */
  659. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  660. {
  661. unsigned int i;
  662. usurf_fill(&screen, x, y, w, h, COL_BG);
  663. for (i = 0; i < WM_MAX_WINDOWS; i++)
  664. if (wins[i].info.used && !wins[i].info.focused &&
  665. !wins[i].info.popover &&
  666. x < wins[i].info.x + (int)wins[i].info.w &&
  667. x + (int)w > wins[i].info.x &&
  668. y < wins[i].info.y + (int)wins[i].info.h &&
  669. y + (int)h > wins[i].info.y)
  670. draw_window_clip(i, x, y, w, h);
  671. for (i = 0; i < WM_MAX_WINDOWS; i++)
  672. if (wins[i].info.used && wins[i].info.focused &&
  673. !wins[i].info.popover &&
  674. x < wins[i].info.x + (int)wins[i].info.w &&
  675. x + (int)w > wins[i].info.x &&
  676. y < wins[i].info.y + (int)wins[i].info.h &&
  677. y + (int)h > wins[i].info.y)
  678. draw_window_clip(i, x, y, w, h);
  679. for (i = 0; i < WM_MAX_WINDOWS; i++)
  680. if (wins[i].info.used && wins[i].info.popover &&
  681. x < wins[i].info.x + (int)wins[i].info.w &&
  682. x + (int)w > wins[i].info.x &&
  683. y < wins[i].info.y + (int)wins[i].info.h &&
  684. y + (int)h > wins[i].info.y)
  685. draw_window_clip(i, x, y, w, h);
  686. }
  687. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  688. {
  689. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  690. struct orect dmg;
  691. int order[WM_MAX_WINDOWS];
  692. unsigned int n, i, j, ncur, nnext;
  693. int overflow = 0;
  694. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  695. if (w == 0 || h == 0)
  696. return;
  697. if (x0 < 0) x0 = 0;
  698. if (y0 < 0) y0 = 0;
  699. if (x1 > (int)fbw) x1 = (int)fbw;
  700. if (y1 > (int)fbh) y1 = (int)fbh;
  701. if (x1 <= x0 || y1 <= y0)
  702. return;
  703. dmg.x = x0;
  704. dmg.y = y0;
  705. dmg.w = (unsigned int)(x1 - x0);
  706. dmg.h = (unsigned int)(y1 - y0);
  707. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  708. n = build_order(order);
  709. for (i = 0; i < n; i++) {
  710. struct uwin *u = &wins[order[i]];
  711. int wx0 = u->info.x, wy0 = u->info.y;
  712. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  713. if (wx0 < dmg.x) wx0 = dmg.x;
  714. if (wy0 < dmg.y) wy0 = dmg.y;
  715. if (wx1 > x1) wx1 = x1;
  716. if (wy1 > y1) wy1 = y1;
  717. if (wx1 <= wx0 || wy1 <= wy0)
  718. continue;
  719. cur[0].x = wx0;
  720. cur[0].y = wy0;
  721. cur[0].w = (unsigned int)(wx1 - wx0);
  722. cur[0].h = (unsigned int)(wy1 - wy0);
  723. ncur = 1;
  724. for (j = 0; j < i && ncur; j++) {
  725. struct uwin *f = &wins[order[j]];
  726. struct orect fr;
  727. unsigned int k;
  728. fr.x = f->info.x;
  729. fr.y = f->info.y;
  730. fr.w = f->info.w;
  731. fr.h = f->info.h;
  732. nnext = 0;
  733. for (k = 0; k < ncur; k++) {
  734. if (nnext + 4 > OCC_MAX) {
  735. overflow = 1;
  736. break;
  737. }
  738. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  739. }
  740. if (overflow)
  741. break;
  742. for (k = 0; k < nnext; k++)
  743. cur[k] = nxt[k];
  744. ncur = nnext;
  745. }
  746. if (overflow)
  747. break;
  748. for (j = 0; j < ncur; j++)
  749. draw_window_clip((unsigned int)order[i], cur[j].x,
  750. cur[j].y, cur[j].w, cur[j].h);
  751. }
  752. if (overflow)
  753. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  754. }
  755. /* Invisible resize border: edge bits for a point in the band around the
  756. * window's outer edge (WM_RESIZE_IN px inside and WM_RESIZE_OUT px outside).
  757. * Popovers have no chrome and never resize. */
  758. #define WM_EDGE_LEFT 1
  759. #define WM_EDGE_RIGHT 2
  760. #define WM_EDGE_TOP 4
  761. #define WM_EDGE_BOTTOM 8
  762. static int resize_edges_at(const struct uwin *u, int mx, int my)
  763. {
  764. const struct wm_window_info *w = &u->info;
  765. int l = w->x, t = w->y;
  766. int r = l + (int)w->w, b = t + (int)w->h;
  767. int near_x, near_y, e = 0;
  768. if (w->popover)
  769. return 0;
  770. near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
  771. near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
  772. if (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN)
  773. e |= WM_EDGE_LEFT;
  774. if (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT)
  775. e |= WM_EDGE_RIGHT;
  776. if (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN)
  777. e |= WM_EDGE_TOP;
  778. if (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT)
  779. e |= WM_EDGE_BOTTOM;
  780. return e;
  781. }
  782. /* Topmost window at (mx,my): its resize band when the point is inside one,
  783. * otherwise the window rectangle. *edges is 0 for a plain rectangle hit.
  784. * Bands are not considered inside the top bar, which the WM owns. */
  785. static int resize_hit(int mx, int my, int *edges)
  786. {
  787. int order[WM_MAX_WINDOWS];
  788. unsigned int n = build_order(order), i;
  789. *edges = 0;
  790. for (i = 0; i < n; i++) {
  791. struct uwin *u = &wins[order[i]];
  792. int e = 0;
  793. if (!u->info.used)
  794. continue;
  795. if (my >= WM_BAR_H)
  796. e = resize_edges_at(u, mx, my);
  797. if (e) {
  798. *edges = e;
  799. return order[i];
  800. }
  801. if (mx >= u->info.x && my >= u->info.y &&
  802. mx < u->info.x + (int)u->info.w &&
  803. my < u->info.y + (int)u->info.h)
  804. return order[i];
  805. }
  806. return -1;
  807. }
  808. static int resize_cursor(int edges)
  809. {
  810. switch (edges) {
  811. case WM_EDGE_LEFT:
  812. case WM_EDGE_RIGHT:
  813. return CURSOR_EW_RESIZE;
  814. case WM_EDGE_TOP:
  815. case WM_EDGE_BOTTOM:
  816. return CURSOR_NS_RESIZE;
  817. case WM_EDGE_LEFT | WM_EDGE_TOP:
  818. case WM_EDGE_RIGHT | WM_EDGE_BOTTOM:
  819. return CURSOR_NWSE_RESIZE;
  820. case WM_EDGE_RIGHT | WM_EDGE_TOP:
  821. case WM_EDGE_LEFT | WM_EDGE_BOTTOM:
  822. return CURSOR_NESW_RESIZE;
  823. default:
  824. return CURSOR_DEFAULT;
  825. }
  826. }
  827. /* Close button hit area (the top-right WM_CLOSE_W columns of the title bar). */
  828. static int close_hit(const struct wm_window_info *w, int mx, int my)
  829. {
  830. if (w->popover)
  831. return 0;
  832. return mx >= w->x + (int)w->w - WM_CLOSE_W &&
  833. mx < w->x + (int)w->w &&
  834. my >= w->y && my < w->y + WM_TITLE_H;
  835. }
  836. static void clamp_geom(struct uwin *u, int *x, int *y)
  837. {
  838. /* Match wm_clamp_window(): allow the window to hang off the left,
  839. * right and bottom edges but keep the title bar reachable and below
  840. * the system bar. */
  841. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  842. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  843. int miny = WM_BAR_H;
  844. int maxy = (int)fbh - WM_BORDER_W - WM_TITLE_H;
  845. if (minx > maxx)
  846. minx = maxx = 0;
  847. if (miny > maxy)
  848. miny = maxy = WM_BAR_H;
  849. if (*x < minx)
  850. *x = minx;
  851. else if (*x > maxx)
  852. *x = maxx;
  853. if (*y < miny)
  854. *y = miny;
  855. else if (*y > maxy)
  856. *y = maxy;
  857. }
  858. static void add_rect(struct wm_present_info *p, int x, int y,
  859. unsigned int w, unsigned int h)
  860. {
  861. if (w == 0 || h == 0)
  862. return;
  863. if (p->count < 4) {
  864. p->rect[p->count].x = x;
  865. p->rect[p->count].y = y;
  866. p->rect[p->count].w = w;
  867. p->rect[p->count].h = h;
  868. p->count++;
  869. } else {
  870. /* Four is enough for normal updates; fold the rest into the
  871. * last rect rather than dropping pixels. */
  872. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  873. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  874. int nx1 = x + (int)w, ny1 = y + (int)h;
  875. if (x < x0) x0 = x;
  876. if (y < y0) y0 = y;
  877. if (nx1 > x1) x1 = nx1;
  878. if (ny1 > y1) y1 = ny1;
  879. p->rect[3].x = x0;
  880. p->rect[3].y = y0;
  881. p->rect[3].w = (unsigned int)(x1 - x0);
  882. p->rect[3].h = (unsigned int)(y1 - y0);
  883. }
  884. }
  885. /* Redraw the power tooltip above whatever a render pass just painted. */
  886. static void bar_tooltip_overlay(struct wm_present_info *p)
  887. {
  888. if (!bar_pwr_tip_on || !bar_pwr_tip_want)
  889. return;
  890. bar_draw_tooltip();
  891. add_rect(p, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  892. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  893. }
  894. /* Center the brightness bubble in the work area, clamped on small screens. */
  895. static void osd_geom(void)
  896. {
  897. int x, y;
  898. osd_w = OSD_W;
  899. osd_h = OSD_H;
  900. x = ((int)fbw - (int)osd_w) / 2;
  901. y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
  902. if (x < 0)
  903. x = 0;
  904. if (x + (int)osd_w > (int)fbw)
  905. x = (int)fbw - (int)osd_w;
  906. if (y < WM_BAR_H)
  907. y = WM_BAR_H;
  908. if (y + (int)osd_h > (int)fbh)
  909. y = (int)fbh - (int)osd_h;
  910. osd_x = x;
  911. osd_y = y;
  912. }
  913. /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
  914. static void osd_sun(int cx, int cy)
  915. {
  916. static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
  917. int i;
  918. for (i = -5; i <= 5; i++)
  919. usurf_fill(&screen, cx - half[i + 5], cy + i,
  920. half[i + 5] * 2 + 1, 1, 0xFFD250u);
  921. usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
  922. usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
  923. usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
  924. usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
  925. }
  926. static void osd_draw(void)
  927. {
  928. char pct[8];
  929. unsigned int n = 0;
  930. unsigned int bar_x, bar_y, bar_w;
  931. int x, y, tw;
  932. osd_geom();
  933. x = osd_x;
  934. y = osd_y;
  935. usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
  936. usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
  937. usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
  938. usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
  939. usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
  940. 0x70709Au);
  941. osd_sun(x + 22, y + (int)osd_h / 2);
  942. if (osd_level >= 100) {
  943. pct[n++] = '1';
  944. pct[n++] = '0';
  945. pct[n++] = '0';
  946. } else {
  947. if (osd_level >= 10)
  948. pct[n++] = (char)('0' + osd_level / 10);
  949. pct[n++] = (char)('0' + osd_level % 10);
  950. }
  951. pct[n++] = '%';
  952. pct[n] = '\0';
  953. usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
  954. x, y, osd_w, osd_h);
  955. tw = (int)usurf_text_width(&screen, pct);
  956. usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
  957. 0xFFD250u, x, y, osd_w, osd_h);
  958. bar_x = (unsigned int)(x + 42);
  959. bar_y = (unsigned int)(y + 35);
  960. bar_w = osd_w - 42 - 14;
  961. usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
  962. usurf_fill(&screen, (int)bar_x, (int)bar_y,
  963. (bar_w * (unsigned int)osd_level + 50) / 100, 10,
  964. 0xFFD250u);
  965. }
  966. /* Put the power tooltip and the brightness OSD back on top after a render
  967. * pass that may have overwritten them. */
  968. static void wm_overlays(struct wm_present_info *p)
  969. {
  970. bar_tooltip_overlay(p);
  971. if (osd_show) {
  972. osd_draw();
  973. add_rect(p, osd_x, osd_y, osd_w, osd_h);
  974. }
  975. }
  976. /* A focused window is topmost, so the shadow contains exactly its pixels in
  977. * the old rectangle. Move that block and repaint only the old part the new
  978. * rectangle does not cover. When either rectangle is partly off-screen the
  979. * shadow has no valid pixels for the hidden part, so repaint the new rect
  980. * instead of copying desktop background into the window. */
  981. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  982. {
  983. struct wm_present_info present;
  984. struct orect oldr, newr, pieces[4];
  985. unsigned int np, k;
  986. int w = (int)u->info.w, h = (int)u->info.h;
  987. int old_on = ox >= 0 && oy >= 0 &&
  988. ox + w <= (int)fbw && oy + h <= (int)fbh;
  989. int new_on = nx >= 0 && ny >= 0 &&
  990. nx + w <= (int)fbw && ny + h <= (int)fbh;
  991. if (u_wm_begin() != 0)
  992. return;
  993. if (old_on && new_on)
  994. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  995. else
  996. draw_rect(nx, ny, u->info.w, u->info.h);
  997. oldr.x = ox;
  998. oldr.y = oy;
  999. oldr.w = u->info.w;
  1000. oldr.h = u->info.h;
  1001. newr.x = nx;
  1002. newr.y = ny;
  1003. newr.w = u->info.w;
  1004. newr.h = u->info.h;
  1005. np = rect_sub(pieces, &oldr, &newr);
  1006. for (k = 0; k < np; k++)
  1007. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  1008. present.count = 0;
  1009. present.immediate = 1;
  1010. add_rect(&present, nx, ny, u->info.w, u->info.h);
  1011. for (k = 0; k < np; k++)
  1012. add_rect(&present, pieces[k].x, pieces[k].y,
  1013. pieces[k].w, pieces[k].h);
  1014. wm_overlays(&present);
  1015. u_wm_present(&present);
  1016. }
  1017. static int iabs(int v)
  1018. {
  1019. return v < 0 ? -v : v;
  1020. }
  1021. /* Repaint the old and new rectangles of a geometry change. */
  1022. static void redraw_geom(struct uwin *u, int ox, int oy,
  1023. unsigned int ow, unsigned int oh)
  1024. {
  1025. struct wm_present_info present;
  1026. unsigned int i;
  1027. present.count = 0;
  1028. present.immediate = 1;
  1029. add_rect(&present, ox, oy, ow, oh);
  1030. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  1031. if (u_wm_begin() == 0) {
  1032. for (i = 0; i < present.count; i++)
  1033. draw_rect(present.rect[i].x, present.rect[i].y,
  1034. present.rect[i].w, present.rect[i].h);
  1035. wm_overlays(&present);
  1036. u_wm_present(&present);
  1037. }
  1038. }
  1039. /* Resize (when needed), move and remap a window in one step. */
  1040. static int set_window_geometry(struct uwin *u, unsigned int idx,
  1041. int nx, int ny, unsigned int nw, unsigned int nh)
  1042. {
  1043. struct wm_window_info ni;
  1044. int ox = u->info.x, oy = u->info.y;
  1045. unsigned int ow = u->info.w, oh = u->info.h;
  1046. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  1047. if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
  1048. nh - WM_TITLE_H - 2 * WM_BORDER_W) != 0)
  1049. return -1;
  1050. }
  1051. if (nx != ox || ny != oy)
  1052. u_wm_set_geom(idx, nx, ny);
  1053. u->mapped = 0;
  1054. if (u_wm_info(idx, &ni) == 1)
  1055. u->info = ni;
  1056. if (u_wm_map(idx, &u->map) == 0)
  1057. u->mapped = 1;
  1058. redraw_geom(u, ox, oy, ow, oh);
  1059. return 0;
  1060. }
  1061. static int restore_window(struct uwin *u, unsigned int idx)
  1062. {
  1063. if (u->state == WIN_NORMAL)
  1064. return 0;
  1065. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  1066. u->restore_w, u->restore_h) != 0)
  1067. return -1;
  1068. u->state = WIN_NORMAL;
  1069. return 0;
  1070. }
  1071. /* Maximize or snap a window to a half of the work area below the system
  1072. * bar. The restore geometry is recorded only when the window leaves the
  1073. * normal state, so maximize -> snap -> restore returns to the original. */
  1074. static int place_window(struct uwin *u, unsigned int idx, int state)
  1075. {
  1076. unsigned int half = fbw / 2;
  1077. unsigned int h = fbh - WM_BAR_H;
  1078. int x = 0;
  1079. unsigned int w = fbw;
  1080. if (state == u->state)
  1081. return 0;
  1082. if (state == WIN_SNAP_LEFT) {
  1083. w = half;
  1084. } else if (state == WIN_SNAP_RIGHT) {
  1085. x = (int)half;
  1086. w = fbw - half;
  1087. }
  1088. if (u->state == WIN_NORMAL) {
  1089. u->restore_x = u->info.x;
  1090. u->restore_y = u->info.y;
  1091. u->restore_w = u->info.w;
  1092. u->restore_h = u->info.h;
  1093. }
  1094. if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
  1095. return -1;
  1096. u->state = state;
  1097. return 0;
  1098. }
  1099. /* Double-click title bar: fill the work area below the system bar, or go back
  1100. * to the geometry the window had before it was maximized. A snapped window
  1101. * maximizes (keeping its pre-snap restore geometry). */
  1102. static int toggle_maximize(struct uwin *u, unsigned int idx)
  1103. {
  1104. if (u->state == WIN_MAXIMIZED)
  1105. return restore_window(u, idx);
  1106. return place_window(u, idx, WIN_MAXIMIZED);
  1107. }
  1108. /* One axis of edge resistance. lo/len is the window's span on that axis
  1109. * before the move, *pos the proposed start. dir < 0: the window's start side
  1110. * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
  1111. * its end side (right/bottom) while moving higher. Within WM_RESIST pixels
  1112. * of overshoot the window stays on the edge; *best keeps the nearest edge. */
  1113. static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
  1114. {
  1115. int over;
  1116. if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
  1117. over = e - *pos;
  1118. if (over <= WM_RESIST && over < *best) {
  1119. *best = over;
  1120. *pos = e;
  1121. }
  1122. } else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
  1123. over = *pos + len - e;
  1124. if (over <= WM_RESIST && over < *best) {
  1125. *best = over;
  1126. *pos = e - len;
  1127. }
  1128. }
  1129. }
  1130. /* Span overlap test: edges of another window only resist when the two
  1131. * windows share some extent on the other axis (they would actually touch). */
  1132. static int spans_overlap(int a, int alen, int b, int blen)
  1133. {
  1134. return a < b + blen && b < a + alen;
  1135. }
  1136. /* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
  1137. * screen edges and the facing edges of every other window. The proposed
  1138. * position is still pure pointer arithmetic, so once the overshoot passes
  1139. * WM_RESIST the window jumps to follow the pointer again. */
  1140. static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
  1141. {
  1142. int ox = u->info.x, oy = u->info.y;
  1143. int w = (int)u->info.w, h = (int)u->info.h;
  1144. int px = *nx, py = *ny;
  1145. int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
  1146. int i;
  1147. resist_edge(ox, w, &px, 0, -1, &bestx);
  1148. resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
  1149. resist_edge(oy, h, &py, (int)fbh, 1, &besty);
  1150. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1151. struct wm_window_info *o = &wins[i].info;
  1152. int t;
  1153. if (i == idx || !o->used)
  1154. continue;
  1155. if (spans_overlap(oy, h, o->y, (int)o->h)) {
  1156. /* our left side against its right, our right
  1157. * side against its left */
  1158. t = *nx;
  1159. resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
  1160. resist_edge(ox, w, &t, o->x, 1, &bestx);
  1161. if (t != *nx)
  1162. px = t;
  1163. }
  1164. if (spans_overlap(ox, w, o->x, (int)o->w)) {
  1165. t = *ny;
  1166. resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
  1167. resist_edge(oy, h, &t, o->y, 1, &besty);
  1168. if (t != *ny)
  1169. py = t;
  1170. }
  1171. }
  1172. *nx = px;
  1173. *ny = py;
  1174. }
  1175. /* The top-bar date/time text is also a button: it opens the Date & Time
  1176. * editor. The rectangle is recorded by draw_bar(). */
  1177. static int bar_clock_hit(unsigned int mx, unsigned int my)
  1178. {
  1179. return bar_clock_w && bar_clock_x >= 0 && my < WM_BAR_H &&
  1180. (int)mx >= bar_clock_x - 5 &&
  1181. (int)mx < bar_clock_x + (int)bar_clock_w + 5;
  1182. }
  1183. static int bar_net_hit(unsigned int mx, unsigned int my)
  1184. {
  1185. return bar_net_w && bar_net_x >= 0 && my < WM_BAR_H &&
  1186. (int)mx >= bar_net_x - 3 &&
  1187. (int)mx < bar_net_x + (int)bar_net_w + 3;
  1188. }
  1189. /* The AC/battery icon opens the control panel on a click; hovering it also
  1190. * reveals the percentage tooltip below it. The tooltip area keeps the hover
  1191. * alive but is not a click target. */
  1192. static int bar_pwr_hit(unsigned int mx, unsigned int my)
  1193. {
  1194. if (my >= WM_BAR_H)
  1195. return bar_pwr_tip_on &&
  1196. (int)mx >= bar_pwr_tip_on_x &&
  1197. (int)mx < bar_pwr_tip_on_x + (int)bar_pwr_tip_on_w &&
  1198. (int)my >= bar_pwr_tip_on_y &&
  1199. (int)my < bar_pwr_tip_on_y + (int)bar_pwr_tip_on_h;
  1200. return bar_pwr_x >= 0 &&
  1201. (int)mx >= bar_pwr_x - 3 &&
  1202. (int)mx < bar_pwr_x + BAR_PWR_W + 3;
  1203. }
  1204. /* The whole right status area is one button: the clock, temperature, network
  1205. * and power items plus the gaps between them. The launcher buttons end at
  1206. * WM_BTN_END_X, so this never overlaps them. */
  1207. static int bar_status_hit(unsigned int mx, unsigned int my)
  1208. {
  1209. return my < WM_BAR_H && (int)mx >= WM_BTN_END_X + 4;
  1210. }
  1211. static void do_update(struct wm_status *st)
  1212. {
  1213. struct wm_present_info present;
  1214. int need = 0;
  1215. int bar_dirty = 0;
  1216. unsigned int i;
  1217. char dt[20];
  1218. static char last_dt[20];
  1219. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  1220. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  1221. unsigned int msg_now = u_ticks_ms();
  1222. int osd_hide = 0, osd_repaint = 0;
  1223. /* The kernel bumps backlight_seq on every brightness key/command.
  1224. * Pop the OSD on a change and keep it up for OSD_MS after the last
  1225. * one. The first poll only records the current sequence, so a WM
  1226. * restart does not flash a stale bubble. */
  1227. if (st->backlight_present) {
  1228. if (!osd_seen_valid) {
  1229. osd_seen_valid = 1;
  1230. osd_seen_seq = st->backlight_seq;
  1231. } else if (st->backlight_seq != osd_seen_seq) {
  1232. osd_seen_seq = st->backlight_seq;
  1233. osd_level = (int)st->backlight_level;
  1234. osd_until = msg_now + OSD_MS;
  1235. osd_show = 1;
  1236. }
  1237. } else if (osd_show) {
  1238. osd_show = 0;
  1239. osd_hide = 1;
  1240. }
  1241. if (osd_show && (int)(msg_now - osd_until) >= 0) {
  1242. osd_show = 0;
  1243. osd_hide = 1;
  1244. }
  1245. if (osd_hide && osd_visible)
  1246. osd_repaint = 1;
  1247. /* Spawn the first terminal once the kernel says the storage scan is
  1248. * done, so a shell command can never race the mount table. */
  1249. if (st->boot_ready && !boot_term_spawned) {
  1250. boot_term_spawned = 1;
  1251. if (!free_window_slot()) {
  1252. bar_notice("window limit");
  1253. full_redraw = 1;
  1254. } else if (u_wm_create_term() < 0) {
  1255. bar_notice("cannot start term");
  1256. full_redraw = 1;
  1257. } else {
  1258. full_redraw = 1;
  1259. }
  1260. }
  1261. if (bar_msg_dirty ||
  1262. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  1263. bar_dirty = 1;
  1264. bar_msg_dirty = 0;
  1265. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  1266. bar_msg_until = 0;
  1267. }
  1268. bar_kexec_busy = st->kexec_busy;
  1269. bar_kexec_progress = st->kexec_progress;
  1270. bar_kexec_total = st->kexec_total;
  1271. if (kexec_changed)
  1272. bar_dirty = 1;
  1273. {
  1274. int hot = bar_clock_hit(st->mouse.x, st->mouse.y);
  1275. if (hot != bar_clock_hot) {
  1276. bar_clock_hot = hot;
  1277. bar_dirty = 1;
  1278. }
  1279. }
  1280. {
  1281. int hot = bar_net_hit(st->mouse.x, st->mouse.y);
  1282. if (hot != bar_net_hot) {
  1283. bar_net_hot = hot;
  1284. bar_dirty = 1;
  1285. }
  1286. }
  1287. {
  1288. int hot = bar_pwr_hit(st->mouse.x, st->mouse.y);
  1289. if (hot != bar_pwr_hot) {
  1290. bar_pwr_hot = hot;
  1291. bar_dirty = 1;
  1292. }
  1293. }
  1294. /* Refresh cached window state and mappings before opening a frame:
  1295. * MAP may unmap/remap content. */
  1296. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1297. struct wm_window_info info;
  1298. int r = u_wm_info(i, &info);
  1299. if (r < 0) {
  1300. u_exit(1);
  1301. }
  1302. if (r == 0 || !info.used) {
  1303. if (wins[i].info.used)
  1304. full_redraw = 1;
  1305. wins[i].info.used = 0;
  1306. wins[i].mapped = 0;
  1307. continue;
  1308. }
  1309. {
  1310. int pid_changed = wins[i].info.used &&
  1311. info.pid != wins[i].info.pid;
  1312. if (!wins[i].info.used ||
  1313. info.content_gen != wins[i].info.content_gen ||
  1314. pid_changed)
  1315. full_redraw = 1;
  1316. wins[i].info = info;
  1317. if (!wins[i].mapped || pid_changed ||
  1318. wins[i].map.generation != info.content_gen) {
  1319. struct wm_map_info map;
  1320. if (u_wm_map(i, &map) == 0) {
  1321. wins[i].map = map;
  1322. wins[i].mapped = 1;
  1323. } else {
  1324. wins[i].mapped = 0;
  1325. }
  1326. }
  1327. }
  1328. }
  1329. /* Popovers are WM-anchored overlays, flush against the bottom of the
  1330. * bar and the right screen edge. The WM owns the geometry, so the app
  1331. * does not need a placement ABI. */
  1332. {
  1333. int control_found = 0;
  1334. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1335. struct wm_window_info ni;
  1336. int nx, ny;
  1337. if (!wins[i].info.used || !wins[i].info.popover)
  1338. continue;
  1339. if (control_pid > 0 &&
  1340. wins[i].info.pid == (unsigned int)control_pid)
  1341. control_found = 1;
  1342. nx = (int)fbw - (int)wins[i].info.w;
  1343. ny = WM_BAR_H;
  1344. if (nx != wins[i].info.x || ny != wins[i].info.y) {
  1345. if (u_wm_set_geom(i, nx, ny) == 0 &&
  1346. u_wm_info(i, &ni) == 1) {
  1347. wins[i].info = ni;
  1348. full_redraw = 1;
  1349. }
  1350. }
  1351. }
  1352. if (!control_found && control_pid > 0 &&
  1353. !u_alive((unsigned int)control_pid))
  1354. control_pid = 0;
  1355. }
  1356. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  1357. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  1358. {
  1359. static unsigned int last_rtc_ms;
  1360. unsigned int now_ms = u_ticks_ms();
  1361. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  1362. last_rtc_ms = now_ms;
  1363. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  1364. for (i = 0; i < sizeof(dt); i++)
  1365. if (dt[i] != last_dt[i]) {
  1366. for (i = 0; i < sizeof(dt); i++)
  1367. last_dt[i] = dt[i];
  1368. bar_dirty = 1;
  1369. break;
  1370. }
  1371. }
  1372. {
  1373. struct sys_cputemp ct;
  1374. if (u_cpu_temp(&ct) == 0 &&
  1375. ct.temp_c != bar_temp_c) {
  1376. bar_temp_c = ct.temp_c;
  1377. bar_dirty = 1;
  1378. }
  1379. }
  1380. {
  1381. struct sys_powerinfo pi;
  1382. int pct = -1, charging = 0, ac = -1;
  1383. unsigned int bi;
  1384. if (u_power_info(&pi, 0) == 0) {
  1385. /* A laptop EC (ThinkPad or Chrome
  1386. * EC) marks a machine with AC/battery;
  1387. * desktops show neither. */
  1388. if (pi.present) {
  1389. ac = pi.ac_online;
  1390. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  1391. bi++) {
  1392. struct sys_battery *b =
  1393. &pi.battery[bi];
  1394. if (!b->present ||
  1395. !b->percent_valid)
  1396. continue;
  1397. pct = (int)b->percent;
  1398. charging = (int)b->charging;
  1399. break;
  1400. }
  1401. }
  1402. if (pct != bar_batt_pct ||
  1403. charging != bar_batt_charging ||
  1404. ac != bar_ac_online) {
  1405. bar_batt_pct = pct;
  1406. bar_batt_charging = charging;
  1407. bar_ac_online = ac;
  1408. bar_dirty = 1;
  1409. }
  1410. }
  1411. }
  1412. if (poll_net_status())
  1413. bar_dirty = 1;
  1414. }
  1415. }
  1416. if (full_redraw) {
  1417. if (u_wm_begin() != 0) {
  1418. full_redraw = 1;
  1419. return;
  1420. }
  1421. full_redraw = 0;
  1422. need = 1;
  1423. {
  1424. draw_rect(0, 0, fbw, fbh);
  1425. draw_bar();
  1426. bar_tooltip_commit();
  1427. if (osd_show)
  1428. osd_draw();
  1429. osd_visible = osd_show;
  1430. present.count = 0;
  1431. add_rect(&present, 0, 0, fbw, fbh);
  1432. present.immediate = 0;
  1433. u_wm_present(&present);
  1434. /* Ack every window that was clean at the time of the
  1435. * redraw. If new damage arrived during it, its seq
  1436. * differs and the kernel leaves it dirty. */
  1437. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1438. if (wins[i].info.used && wins[i].mapped)
  1439. u_wm_ack(i, wins[i].info.dirty_seq);
  1440. }
  1441. return;
  1442. }
  1443. /* Partial cell/canvas updates and the bar clock. */
  1444. present.count = 0;
  1445. present.immediate = 0;
  1446. if (osd_repaint) {
  1447. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1448. need = 1;
  1449. }
  1450. if (osd_show)
  1451. need = 1;
  1452. if (bar_dirty) {
  1453. need = 1;
  1454. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1455. }
  1456. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1457. struct uwin *u = &wins[i];
  1458. struct wm_window_info *w = &u->info;
  1459. int rx, ry;
  1460. unsigned int rw, rh;
  1461. if (!w->used || !w->dirty)
  1462. continue;
  1463. if (!w->dmg_w || !w->dmg_h)
  1464. continue;
  1465. rx = w->x + (w->popover ? 0 : WM_BORDER_W) + (int)w->dmg_x;
  1466. ry = w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H) +
  1467. (int)w->dmg_y;
  1468. rw = w->dmg_w;
  1469. rh = w->dmg_h;
  1470. add_rect(&present, rx, ry, rw, rh);
  1471. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1472. need = 1;
  1473. }
  1474. /* Repaint the previous tooltip area so hiding or moving it restores
  1475. * the window/desktop underneath; the new one is drawn after the
  1476. * windows below. */
  1477. if (bar_pwr_tip_on) {
  1478. need = 1;
  1479. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1480. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1481. }
  1482. if (!need) {
  1483. /* Drag movement is handled below; a stationary mouse can be
  1484. * left to the kernel's cursor overlay. */
  1485. return;
  1486. }
  1487. if (u_wm_begin() != 0)
  1488. return;
  1489. for (i = 0; i < present.count; i++)
  1490. draw_rect(present.rect[i].x, present.rect[i].y,
  1491. present.rect[i].w, present.rect[i].h);
  1492. if (bar_dirty)
  1493. draw_bar();
  1494. if (bar_pwr_tip_want) {
  1495. bar_tooltip_commit();
  1496. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1497. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1498. } else {
  1499. bar_pwr_tip_on = 0;
  1500. }
  1501. if (osd_show) {
  1502. osd_draw();
  1503. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1504. }
  1505. osd_visible = osd_show;
  1506. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1507. struct uwin *u = &wins[i];
  1508. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1509. !u->ack_seq)
  1510. continue;
  1511. if (u->ack_seq == u->info.dirty_seq)
  1512. u_wm_ack(i, u->info.dirty_seq);
  1513. }
  1514. u_wm_present(&present);
  1515. }
  1516. static int spawn_app(const char *path)
  1517. {
  1518. int pid = u_spawn(path, "", 0, "/");
  1519. if (pid > 0)
  1520. full_redraw = 1;
  1521. return pid;
  1522. }
  1523. static void spawn_app_checked(const char *path)
  1524. {
  1525. if (!free_window_slot()) {
  1526. bar_notice("window limit");
  1527. full_redraw = 1;
  1528. return;
  1529. }
  1530. if (spawn_app(path) < 0) {
  1531. bar_notice("spawn failed");
  1532. full_redraw = 1;
  1533. }
  1534. }
  1535. /* Any status target opens one control panel; a second click closes it. */
  1536. static void open_control_panel(void)
  1537. {
  1538. unsigned int i;
  1539. if (control_pid > 0 && u_alive((unsigned int)control_pid)) {
  1540. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1541. if (wins[i].info.used &&
  1542. wins[i].info.pid == (unsigned int)control_pid) {
  1543. if (u_wm_close(i) == 0) {
  1544. control_pid = 0;
  1545. full_redraw = 1;
  1546. } else {
  1547. u_wm_focus(i);
  1548. full_redraw = 1;
  1549. }
  1550. return;
  1551. }
  1552. /* The process is alive but has not created its window yet. */
  1553. return;
  1554. }
  1555. if (!free_window_slot()) {
  1556. bar_notice("window limit");
  1557. full_redraw = 1;
  1558. return;
  1559. }
  1560. control_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  1561. if (control_pid < 0) {
  1562. control_pid = 0;
  1563. bar_notice("spawn failed");
  1564. full_redraw = 1;
  1565. } else {
  1566. full_redraw = 1;
  1567. }
  1568. }
  1569. /* Index of a popover containing the point, or -1. */
  1570. static int popover_under(unsigned int mx, unsigned int my)
  1571. {
  1572. unsigned int i;
  1573. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1574. struct wm_window_info *w = &wins[i].info;
  1575. if (w->used && w->popover &&
  1576. mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  1577. mx < (unsigned int)(w->x + (int)w->w) &&
  1578. my < (unsigned int)(w->y + (int)w->h))
  1579. return (int)i;
  1580. }
  1581. return -1;
  1582. }
  1583. /* A press anywhere outside an open popover dismisses it; the click then
  1584. * continues normally so the target under the pointer still reacts. */
  1585. static void dismiss_popover_outside(unsigned int mx, unsigned int my)
  1586. {
  1587. unsigned int i;
  1588. if (popover_under(mx, my) >= 0)
  1589. return;
  1590. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1591. if (!wins[i].info.used || !wins[i].info.popover)
  1592. continue;
  1593. if (wins[i].info.pid == (unsigned int)control_pid)
  1594. control_pid = 0;
  1595. if (u_wm_close(i) == 0) {
  1596. wins[i].info.used = 0;
  1597. wins[i].mapped = 0;
  1598. full_redraw = 1;
  1599. }
  1600. break;
  1601. }
  1602. }
  1603. /* Top-bar launcher under (mx, my)? Same rectangles as the click chain in
  1604. * handle_mouse(); every launcher shares WM_BTN_Y/WM_BTN_H. */
  1605. static int bar_launcher_hit(unsigned int mx, unsigned int my)
  1606. {
  1607. static const int x0[] = {
  1608. WM_BTN_X, WM_BTN_CLOCK_X, WM_BTN_COLORS_X, WM_BTN_FILES_X,
  1609. WM_BTN_MINES_X, WM_BTN_WEB_X, WM_BTN_CARDS_X, WM_BTN_GALLERY_X,
  1610. WM_BTN_CHAT_X, WM_BTN_DISKS_X
  1611. };
  1612. static const int w[] = {
  1613. WM_BTN_W, WM_BTN_CLOCK_W, WM_BTN_COLORS_W, WM_BTN_FILES_W,
  1614. WM_BTN_MINES_W, WM_BTN_WEB_W, WM_BTN_CARDS_W, WM_BTN_GALLERY_W,
  1615. WM_BTN_CHAT_W, WM_BTN_DISKS_W
  1616. };
  1617. unsigned int i;
  1618. if ((int)my < WM_BTN_Y || (int)my >= WM_BTN_Y + WM_BTN_H)
  1619. return 0;
  1620. for (i = 0; i < sizeof(x0) / sizeof(x0[0]); i++)
  1621. if ((int)mx >= x0[i] && (int)mx < x0[i] + w[i])
  1622. return 1;
  1623. return 0;
  1624. }
  1625. /* Apply one edge-drag step. The grabbed edges follow the pointer while the
  1626. * opposite edges stay put; the work area clamps the active edges. */
  1627. static void resize_window_drag(struct uwin *u, unsigned int idx,
  1628. unsigned int mx, unsigned int my)
  1629. {
  1630. int dx = (int)mx - (int)resize_mx;
  1631. int dy = (int)my - (int)resize_my;
  1632. int left = resize_ox;
  1633. int right = resize_ox + (int)resize_ow;
  1634. int top = resize_oy;
  1635. int bottom = resize_oy + (int)resize_oh;
  1636. int nx, ny, nw, nh;
  1637. if (resize_edges & WM_EDGE_LEFT) {
  1638. left = resize_ox + dx;
  1639. if (left < 0)
  1640. left = 0;
  1641. } else if (resize_edges & WM_EDGE_RIGHT) {
  1642. right = resize_ox + (int)resize_ow + dx;
  1643. if (right > (int)fbw)
  1644. right = (int)fbw;
  1645. }
  1646. if (resize_edges & WM_EDGE_TOP) {
  1647. top = resize_oy + dy;
  1648. if (top < WM_BAR_H)
  1649. top = WM_BAR_H;
  1650. } else if (resize_edges & WM_EDGE_BOTTOM) {
  1651. bottom = resize_oy + (int)resize_oh + dy;
  1652. if (bottom > (int)fbh)
  1653. bottom = (int)fbh;
  1654. }
  1655. if ((resize_edges & (WM_EDGE_LEFT | WM_EDGE_RIGHT)) &&
  1656. right - left < 200) {
  1657. if (resize_edges & WM_EDGE_LEFT)
  1658. left = right - 200;
  1659. else
  1660. right = left + 200;
  1661. }
  1662. if ((resize_edges & (WM_EDGE_TOP | WM_EDGE_BOTTOM)) &&
  1663. bottom - top < 140) {
  1664. if (resize_edges & WM_EDGE_TOP)
  1665. top = bottom - 140;
  1666. else
  1667. bottom = top + 140;
  1668. }
  1669. if (left < 0)
  1670. left = 0;
  1671. if (top < WM_BAR_H)
  1672. top = WM_BAR_H;
  1673. nx = left;
  1674. ny = top;
  1675. nw = right - left;
  1676. nh = bottom - top;
  1677. if (nw > (int)fbw)
  1678. nw = (int)fbw;
  1679. if (nh > (int)fbh - WM_BAR_H)
  1680. nh = (int)fbh - WM_BAR_H;
  1681. if (nx == u->info.x && ny == u->info.y &&
  1682. nw == (int)u->info.w && nh == (int)u->info.h)
  1683. return;
  1684. if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
  1685. (unsigned int)nh) == 0)
  1686. u->state = WIN_NORMAL;
  1687. }
  1688. /* Capture the pointer for an edge resize that started at (mx,my). */
  1689. static void begin_resize(unsigned int idx, int edges, unsigned int mx,
  1690. unsigned int my)
  1691. {
  1692. struct uwin *u = &wins[idx];
  1693. resize_id = (int)idx;
  1694. resize_edges = edges;
  1695. resize_mx = mx;
  1696. resize_my = my;
  1697. resize_ox = u->info.x;
  1698. resize_oy = u->info.y;
  1699. resize_ow = u->info.w;
  1700. resize_oh = u->info.h;
  1701. u_wm_mouse_capture(1);
  1702. }
  1703. /* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the
  1704. * invisible resize border and while resizing, the move cross while a
  1705. * title-bar drag is under way, a hand over the launchers and the status
  1706. * area. Everywhere else the kernel picks the window's own content cursor
  1707. * (or the arrow). */
  1708. static void update_cursor(unsigned int mx, unsigned int my)
  1709. {
  1710. static int current = CURSOR_NO_OVERRIDE;
  1711. int want = CURSOR_NO_OVERRIDE;
  1712. if (resize_id >= 0) {
  1713. want = resize_cursor(resize_edges);
  1714. } else if (drag_id >= 0) {
  1715. if (drag_moved)
  1716. want = CURSOR_MOVE;
  1717. } else if (my < WM_BAR_H) {
  1718. if (bar_launcher_hit(mx, my) || bar_status_hit(mx, my))
  1719. want = CURSOR_POINTER;
  1720. } else {
  1721. int edges = 0;
  1722. int idx = resize_hit((int)mx, (int)my, &edges);
  1723. if (idx >= 0 && edges &&
  1724. !close_hit(&wins[idx].info, (int)mx, (int)my))
  1725. want = resize_cursor(edges);
  1726. }
  1727. if (want != current && u_wm_set_cursor(want) == 0)
  1728. current = want;
  1729. }
  1730. static void handle_mouse(struct wm_status *st)
  1731. {
  1732. unsigned int buttons = st->mouse.buttons;
  1733. unsigned int mx = st->mouse.x, my = st->mouse.y;
  1734. int down = (buttons & 1) != 0;
  1735. int was = (prev_buttons & 1) != 0;
  1736. if (down && !was) {
  1737. int prev_title_id = last_title_id;
  1738. unsigned int now = u_ticks_ms();
  1739. if (!bar_status_hit(mx, my))
  1740. dismiss_popover_outside(mx, my);
  1741. last_title_id = -1;
  1742. if (bar_status_hit(mx, my)) {
  1743. open_control_panel();
  1744. } else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  1745. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  1746. if (!free_window_slot()) {
  1747. bar_notice("window limit");
  1748. full_redraw = 1;
  1749. } else if (u_wm_create_term() < 0) {
  1750. bar_notice("cannot start term");
  1751. full_redraw = 1;
  1752. } else {
  1753. full_redraw = 1;
  1754. }
  1755. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  1756. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  1757. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  1758. spawn_app_checked("/ramfs/bin/clock");
  1759. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  1760. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  1761. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  1762. spawn_app_checked("/ramfs/bin/colors");
  1763. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  1764. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  1765. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  1766. spawn_app_checked("/ramfs/bin/files");
  1767. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  1768. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  1769. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  1770. spawn_app_checked("/ramfs/bin/mines");
  1771. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  1772. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  1773. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  1774. spawn_app_checked("/ramfs/bin/browser");
  1775. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  1776. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  1777. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  1778. spawn_app_checked("/ramfs/bin/solitaire");
  1779. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  1780. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  1781. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  1782. spawn_app_checked("/ramfs/bin/gallery");
  1783. } else if (mx >= WM_BTN_CHAT_X && my >= WM_BTN_CHAT_Y &&
  1784. mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
  1785. my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
  1786. spawn_app_checked("/ramfs/bin/chat");
  1787. } else if (mx >= WM_BTN_DISKS_X && my >= WM_BTN_DISKS_Y &&
  1788. mx < WM_BTN_DISKS_X + WM_BTN_DISKS_W &&
  1789. my < WM_BTN_DISKS_Y + WM_BTN_DISKS_H) {
  1790. spawn_app_checked("/ramfs/bin/disks");
  1791. } else {
  1792. int edges = 0;
  1793. int idx = resize_hit((int)mx, (int)my, &edges);
  1794. if (idx >= 0) {
  1795. struct uwin *u = &wins[idx];
  1796. int pmx = (int)mx, pmy = (int)my;
  1797. u_wm_focus((unsigned int)idx);
  1798. if (u->info.popover) {
  1799. /* A popover has no title bar, close
  1800. * button or resize border; the app owns
  1801. * its content. */
  1802. } else if (close_hit(&u->info, pmx, pmy)) {
  1803. u_wm_close((unsigned int)idx);
  1804. full_redraw = 1;
  1805. } else if (edges) {
  1806. begin_resize((unsigned int)idx, edges,
  1807. mx, my);
  1808. } else if (pmy < u->info.y + WM_TITLE_H) {
  1809. if (prev_title_id == idx &&
  1810. now - last_title_ms <= WM_DBLCLICK_MS &&
  1811. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  1812. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  1813. last_title_ms = 0;
  1814. if (toggle_maximize(u,
  1815. (unsigned int)idx) != 0)
  1816. bar_notice("resize failed");
  1817. } else {
  1818. last_title_id = idx;
  1819. last_title_ms = now;
  1820. last_title_x = pmx;
  1821. last_title_y = pmy;
  1822. drag_id = idx;
  1823. drag_mx = pmx;
  1824. drag_my = pmy;
  1825. drag_dx = pmx - u->info.x;
  1826. drag_dy = pmy - u->info.y;
  1827. drag_moved = 0;
  1828. u_wm_mouse_capture(1);
  1829. }
  1830. }
  1831. }
  1832. }
  1833. } else if (down && was && resize_id >= 0) {
  1834. struct uwin *u = &wins[resize_id];
  1835. if (u->info.used)
  1836. resize_window_drag(u, (unsigned int)resize_id, mx, my);
  1837. else {
  1838. resize_id = -1;
  1839. u_wm_mouse_capture(0);
  1840. }
  1841. } else if (down && was && drag_id >= 0) {
  1842. struct uwin *u = &wins[drag_id];
  1843. if (u->info.used) {
  1844. int ox, oy, nx, ny;
  1845. if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  1846. iabs((int)my - drag_my) >= WM_DRAG_START)
  1847. drag_moved = 1;
  1848. /* A maximized or snapped window is restored as soon as
  1849. * the drag starts. Keep the grab point at the same
  1850. * fraction of the title bar, so the restored (narrower)
  1851. * window stays under the pointer. A click that does
  1852. * not move must leave it in place. */
  1853. if (u->state != WIN_NORMAL && drag_moved) {
  1854. unsigned int ow = u->info.w;
  1855. int gx = drag_mx - u->info.x;
  1856. int gy = drag_my - u->info.y;
  1857. if (restore_window(u,
  1858. (unsigned int)drag_id) == 0) {
  1859. if (ow)
  1860. gx = gx * (int)u->info.w /
  1861. (int)ow;
  1862. drag_dx = gx;
  1863. drag_dy = gy;
  1864. }
  1865. }
  1866. if (u->state == WIN_NORMAL) {
  1867. ox = u->info.x;
  1868. oy = u->info.y;
  1869. nx = (int)mx - drag_dx;
  1870. ny = (int)my - drag_dy;
  1871. resist_geom(u, drag_id, &nx, &ny);
  1872. clamp_geom(u, &nx, &ny);
  1873. if (nx != ox || ny != oy) {
  1874. struct wm_present_info present;
  1875. unsigned int i;
  1876. u_wm_set_geom((unsigned int)drag_id,
  1877. nx, ny);
  1878. u->info.x = nx;
  1879. u->info.y = ny;
  1880. if (u->info.focused) {
  1881. move_window_pixels(u, ox, oy,
  1882. nx, ny);
  1883. } else {
  1884. present.count = 0;
  1885. present.immediate = 1;
  1886. add_rect(&present, ox, oy,
  1887. u->info.w, u->info.h);
  1888. add_rect(&present, nx, ny,
  1889. u->info.w, u->info.h);
  1890. if (u_wm_begin() == 0) {
  1891. for (i = 0;
  1892. i < present.count; i++)
  1893. draw_rect(
  1894. present.rect[i].x,
  1895. present.rect[i].y,
  1896. present.rect[i].w,
  1897. present.rect[i].h);
  1898. wm_overlays(&present);
  1899. u_wm_present(&present);
  1900. }
  1901. }
  1902. }
  1903. }
  1904. } else {
  1905. drag_id = -1;
  1906. u_wm_mouse_capture(0);
  1907. }
  1908. } else if (!down && was) {
  1909. /* Dropping a window with the pointer on the left/right screen
  1910. * edge snaps it to that half; on the top edge it maximizes. */
  1911. if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&
  1912. wins[drag_id].state == WIN_NORMAL) {
  1913. struct uwin *u = &wins[drag_id];
  1914. int st = WIN_NORMAL;
  1915. if ((int)mx < WM_SNAP_EDGE)
  1916. st = WIN_SNAP_LEFT;
  1917. else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
  1918. st = WIN_SNAP_RIGHT;
  1919. else if ((int)my < WM_SNAP_EDGE)
  1920. st = WIN_MAXIMIZED;
  1921. if (st != WIN_NORMAL &&
  1922. place_window(u, (unsigned int)drag_id, st) != 0)
  1923. bar_notice("resize failed");
  1924. }
  1925. drag_id = -1;
  1926. resize_id = -1;
  1927. u_wm_mouse_capture(0);
  1928. }
  1929. prev_buttons = buttons;
  1930. update_cursor(mx, my);
  1931. }
  1932. static char *put_str(char *p, const char *s)
  1933. {
  1934. while (*s)
  1935. *p++ = *s++;
  1936. return p;
  1937. }
  1938. static char *put_dec(char *p, unsigned int v)
  1939. {
  1940. char b[10];
  1941. unsigned int n = 0;
  1942. do {
  1943. b[n++] = (char)('0' + v % 10);
  1944. v /= 10;
  1945. } while (v);
  1946. while (n)
  1947. *p++ = b[--n];
  1948. return p;
  1949. }
  1950. /* Expose the last interval in /share so the target can be measured without
  1951. * a serial console. */
  1952. static void write_stats(void)
  1953. {
  1954. char b[80], *p = b;
  1955. unsigned int now = u_ticks_ms();
  1956. p = put_str(p, "t=");
  1957. p = put_dec(p, now / 1000);
  1958. p = put_str(p, "s frames=");
  1959. p = put_dec(p, stat_frames);
  1960. p = put_str(p, " ms=");
  1961. p = put_dec(p, stat_ms);
  1962. p = put_str(p, " max=");
  1963. p = put_dec(p, stat_max);
  1964. p = put_str(p, " px=");
  1965. p = put_dec(p, stat_px);
  1966. p = put_str(p, "\n");
  1967. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  1968. (int)(p - b));
  1969. }
  1970. void _start(const char *arg)
  1971. {
  1972. struct wm_screen_info si;
  1973. struct wm_status st;
  1974. unsigned int i;
  1975. (void)arg;
  1976. if (u_wm_attach(&si) != 0)
  1977. u_exit(1);
  1978. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  1979. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  1980. fbw = si.width;
  1981. fbh = si.height;
  1982. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1983. wins[i].info.used = 0;
  1984. wins[i].mapped = 0;
  1985. }
  1986. full_redraw = 1;
  1987. for (;;) {
  1988. unsigned int t0, t1;
  1989. if (u_wm_poll(&st) != 0)
  1990. u_exit(1);
  1991. if (st.full_redraw)
  1992. full_redraw = 1;
  1993. usurf_pixels = 0;
  1994. t0 = u_ticks_ms();
  1995. handle_mouse(&st);
  1996. do_update(&st);
  1997. t1 = u_ticks_ms();
  1998. if (usurf_pixels) {
  1999. unsigned int dt = t1 - t0;
  2000. stat_frames++;
  2001. stat_ms += dt;
  2002. stat_px += usurf_pixels;
  2003. if (dt > stat_max)
  2004. stat_max = dt;
  2005. }
  2006. if (t1 - stat_last >= 2000) {
  2007. stat_last = t1;
  2008. if (stat_frames) {
  2009. write_stats();
  2010. stat_frames = stat_ms = stat_px = stat_max = 0;
  2011. }
  2012. }
  2013. u_sleep_ms(10);
  2014. }
  2015. }