ucmd_wm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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. #define COL_BORDER 0xC8C8C8u
  10. #define COL_TITLE_ON 0x465A96u
  11. #define COL_TITLE_OFF 0x646464u
  12. #define COL_CLOSE 0xC64040u
  13. #define COL_WHITE 0xFFFFFFu
  14. #define COL_BANNER 0x55FF55u
  15. struct uwin {
  16. struct wm_window_info info;
  17. struct wm_map_info map;
  18. int mapped;
  19. unsigned int ack_seq;
  20. /* Double-click title bar: the geometry to return to when maximized. */
  21. int maximized;
  22. int restore_x, restore_y;
  23. unsigned int restore_w, restore_h;
  24. };
  25. /* Double-click detection on the title bar. */
  26. #define WM_DBLCLICK_MS 500
  27. #define WM_DBLCLICK_SLOP 8
  28. /* A maximized window restores only after the pointer actually starts a drag,
  29. * so a plain click on its title bar leaves it maximized. */
  30. #define WM_DRAG_START 3
  31. static struct usurf screen;
  32. static struct uwin wins[WM_MAX_WINDOWS];
  33. static unsigned int fbw, fbh;
  34. static int drag_id = -1;
  35. static int drag_dx, drag_dy;
  36. static int drag_mx, drag_my; /* press position, for restore-on-drag */
  37. static int resize_id = -1;
  38. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  39. static unsigned int last_title_ms;
  40. static int last_title_id = -1, last_title_x, last_title_y;
  41. static unsigned int prev_buttons;
  42. static int full_redraw = 1;
  43. static int boot_term_spawned;
  44. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  45. static int bar_temp_c = -1;
  46. static int bar_batt_pct = -1;
  47. static int bar_batt_charging;
  48. static int bar_ac_online = -1;
  49. static char bar_msg[24];
  50. static unsigned int bar_msg_until;
  51. static int bar_msg_dirty;
  52. /* Rectangle of the right-aligned date/time text, so a click there can open
  53. * the Date & Time app. w == 0 means "not drawn" (e.g. kexec banner). */
  54. static int bar_clock_x = -1;
  55. static unsigned int bar_clock_w;
  56. static int bar_clock_hot;
  57. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  58. /* Network status icon (right side of the bar). */
  59. #define BAR_NET_W 24
  60. #define BAR_NET_Y 6
  61. #define BAR_NET_NONE 0
  62. #define BAR_NET_WIRED 1
  63. #define BAR_NET_WIFI 2
  64. #define BAR_NET_USB 3
  65. static int bar_net_x = -1;
  66. static unsigned int bar_net_w;
  67. static int bar_net_hot;
  68. static int bar_net_kind;
  69. static int bar_net_bars;
  70. static unsigned int bar_net_color = 0xFF5050u;
  71. static int netmenu_pid;
  72. static int netmenu_placed;
  73. static void bar_notice(const char *msg)
  74. {
  75. unsigned int i;
  76. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  77. bar_msg[i] = msg[i];
  78. bar_msg[i] = '\0';
  79. bar_msg_until = u_ticks_ms() + 2000;
  80. bar_msg_dirty = 1;
  81. }
  82. static int free_window_slot(void)
  83. {
  84. unsigned int i;
  85. for (i = 0; i < WM_MAX_WINDOWS; i++)
  86. if (!wins[i].info.used)
  87. return 1;
  88. return 0;
  89. }
  90. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  91. {
  92. return (r << 16) | (g << 8) | b;
  93. }
  94. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  95. * integer interpolation the kernel bar uses. */
  96. static unsigned int rainbow_rgb(unsigned int hue)
  97. {
  98. unsigned int seg = hue / 256, f = hue % 256;
  99. unsigned int r1, g1, b1, r2, g2, b2;
  100. switch (seg) {
  101. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  102. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  103. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  104. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  105. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  106. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  107. }
  108. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  109. (g1 * (256 - f) + g2 * f) / 256,
  110. (b1 * (256 - f) + b2 * f) / 256);
  111. }
  112. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  113. unsigned int rgb, int cx, int cy,
  114. unsigned int cw, unsigned int ch)
  115. {
  116. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  117. if (x0 < cx) x0 = cx;
  118. if (y0 < cy) y0 = cy;
  119. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  120. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  121. if (x1 > x0 && y1 > y0)
  122. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  123. (unsigned int)(y1 - y0), rgb);
  124. }
  125. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  126. unsigned int h, const char *label)
  127. {
  128. int tw = (int)usurf_text_width(&screen, label);
  129. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  130. usurf_text(&screen, (int)x + ((int)w - tw) / 2, (int)(y + 3),
  131. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  132. }
  133. static int name_is(const char *a, const char *b)
  134. {
  135. while (*a && *b) {
  136. if (*a != *b)
  137. return 0;
  138. a++;
  139. b++;
  140. }
  141. return *a == *b;
  142. }
  143. static int net_rssi_bars(int rssi)
  144. {
  145. if (rssi >= -55)
  146. return 4;
  147. if (rssi >= -65)
  148. return 3;
  149. if (rssi >= -75)
  150. return 2;
  151. return 1;
  152. }
  153. /* Small top-bar pictograms, drawn directly into the shadow. */
  154. static void bar_icon_cable(int x, int y, unsigned int c)
  155. {
  156. usurf_fill(&screen, x + 1, y + 6, 9, 8, c);
  157. usurf_fill(&screen, x + 3, y + 8, 2, 2, COL_BG);
  158. usurf_fill(&screen, x + 6, y + 8, 2, 2, COL_BG);
  159. usurf_fill(&screen, x + 10, y + 9, 7, 2, c);
  160. usurf_fill(&screen, x + 17, y + 5, 5, 10, c);
  161. usurf_fill(&screen, x + 19, y + 7, 2, 3, COL_BG);
  162. }
  163. static void bar_icon_wifi(int x, int y, unsigned int c, int bars)
  164. {
  165. int i;
  166. for (i = 0; i < 4; i++) {
  167. int h = 3 + i * 3;
  168. usurf_fill(&screen, x + 3 + i * 5, y + 16 - h, 3, h,
  169. i < bars ? c : 0x4A4A5Au);
  170. }
  171. }
  172. static void bar_icon_x(int x, int y, unsigned int c)
  173. {
  174. int i;
  175. for (i = 0; i < 12; i++) {
  176. usurf_fill(&screen, x + 6 + i, y + 4 + i, 2, 2, c);
  177. usurf_fill(&screen, x + 17 - i, y + 4 + i, 2, 2, c);
  178. }
  179. }
  180. static void bar_icon_usb(int x, int y, unsigned int c)
  181. {
  182. usurf_text(&screen, x + 1, y + 1, "USB", c, 0, 0, fbw, WM_BAR_H);
  183. }
  184. static void bar_draw_net_icon(int x, int y)
  185. {
  186. switch (bar_net_kind) {
  187. case BAR_NET_WIRED:
  188. bar_icon_cable(x, y, bar_net_color);
  189. break;
  190. case BAR_NET_WIFI:
  191. bar_icon_wifi(x, y, bar_net_color, bar_net_bars);
  192. break;
  193. case BAR_NET_USB:
  194. bar_icon_usb(x, y, bar_net_color);
  195. break;
  196. default:
  197. bar_icon_x(x, y, bar_net_color);
  198. break;
  199. }
  200. }
  201. /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
  202. * only made when WiFi is the active netdev, so a machine without a card does
  203. * not run a PCI scan on every tick. */
  204. static int poll_net_status(void)
  205. {
  206. struct sys_netinfo ni;
  207. int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
  208. unsigned int color = 0xFF5050u;
  209. int changed;
  210. int i;
  211. if (u_net_info(&ni) == 1 && ni.present) {
  212. for (i = 0; i < 4; i++)
  213. if (ni.ip[i])
  214. have_ip = 1;
  215. if (name_is(ni.name, "wifi")) {
  216. struct sys_wifi_status ws;
  217. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
  218. ws.present && ws.state >= SYS_WIFI_ST_ASSOC) {
  219. kind = BAR_NET_WIFI;
  220. bars = net_rssi_bars(ws.rssi);
  221. color = (ws.state >= SYS_WIFI_ST_LINK && have_ip) ?
  222. 0x55FF55u : 0xFFD250u;
  223. }
  224. } else if (name_is(ni.name, "rndis")) {
  225. kind = BAR_NET_USB;
  226. color = have_ip ? 0x55FF55u : 0xFFD250u;
  227. } else {
  228. kind = BAR_NET_WIRED;
  229. color = have_ip ? 0x55FF55u : 0xFFD250u;
  230. }
  231. }
  232. changed = kind != bar_net_kind || bars != bar_net_bars ||
  233. color != bar_net_color;
  234. bar_net_kind = kind;
  235. bar_net_bars = bars;
  236. bar_net_color = color;
  237. return changed;
  238. }
  239. static unsigned int temp_color(int c)
  240. {
  241. if (c >= 80)
  242. return 0xFF5050u;
  243. if (c >= 60)
  244. return 0xFFD250u;
  245. return 0x55FF55u;
  246. }
  247. /* "CPU 43C" (no libc printf here). */
  248. static unsigned int temp_text(char *b, int c)
  249. {
  250. unsigned int n = 0, v = (unsigned int)c;
  251. b[n++] = 'C';
  252. b[n++] = 'P';
  253. b[n++] = 'U';
  254. b[n++] = ' ';
  255. if (v >= 100)
  256. b[n++] = (char)('0' + (v / 100) % 10);
  257. if (v >= 10)
  258. b[n++] = (char)('0' + (v / 10) % 10);
  259. b[n++] = (char)('0' + v % 10);
  260. b[n++] = 'C';
  261. b[n] = '\0';
  262. return n;
  263. }
  264. /* "BAT 87%" (no libc printf here). */
  265. static unsigned int batt_text(char *b, int pct)
  266. {
  267. unsigned int n = 0, v = (unsigned int)pct;
  268. b[n++] = 'B';
  269. b[n++] = 'A';
  270. b[n++] = 'T';
  271. b[n++] = ' ';
  272. if (v >= 100)
  273. b[n++] = (char)('0' + (v / 100) % 10);
  274. if (v >= 10)
  275. b[n++] = (char)('0' + (v / 10) % 10);
  276. b[n++] = (char)('0' + v % 10);
  277. b[n++] = '%';
  278. b[n] = '\0';
  279. return n;
  280. }
  281. static unsigned int batt_color(int pct, int charging, int ac)
  282. {
  283. if (pct <= 10)
  284. return 0xFF5050u;
  285. if (pct <= 20)
  286. return 0xFFD250u;
  287. if (charging || ac > 0)
  288. return 0x55FF55u;
  289. return 0x55D2FFu;
  290. }
  291. /* "AC on" / "AC off" (no libc printf here). Only drawn on machines whose
  292. * ThinkPad EC reported the adapter state. */
  293. static unsigned int ac_text(char *b, int online)
  294. {
  295. unsigned int n = 0;
  296. b[n++] = 'A';
  297. b[n++] = 'C';
  298. b[n++] = ' ';
  299. b[n++] = 'o';
  300. if (online) {
  301. b[n++] = 'n';
  302. } else {
  303. b[n++] = 'f';
  304. b[n++] = 'f';
  305. }
  306. b[n] = '\0';
  307. return n;
  308. }
  309. static unsigned int ac_color(int online)
  310. {
  311. return online ? 0x55FF55u : 0xFFD250u;
  312. }
  313. static void draw_bar(void)
  314. {
  315. char dt[48];
  316. unsigned int n, i, x, y;
  317. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  318. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  319. 0, 0, fbw, WM_BAR_H);
  320. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  321. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  322. WM_BTN_CLOCK_H, "Clock");
  323. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  324. WM_BTN_COLORS_H, "Colors");
  325. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  326. WM_BTN_FILES_H, "Files");
  327. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  328. WM_BTN_MINES_H, "Mines");
  329. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  330. WM_BTN_WEB_H, "Web");
  331. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  332. WM_BTN_CARDS_H, "Cards");
  333. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  334. WM_BTN_GALLERY_H, "Gallery");
  335. bar_button(WM_BTN_CHAT_X, WM_BTN_CHAT_Y, WM_BTN_CHAT_W,
  336. WM_BTN_CHAT_H, "Chat");
  337. if (bar_msg_until &&
  338. (int)(u_ticks_ms() - bar_msg_until) < 0)
  339. usurf_text(&screen, (int)(WM_BTN_END_X + 8),
  340. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  341. /* Rainbow separator (same y as the kernel's old bar). */
  342. for (x = 0; x < fbw; x++) {
  343. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  344. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  345. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  346. }
  347. if (bar_kexec_busy) {
  348. static const char msg[] = "loading new kernel over serial";
  349. unsigned int dots = bar_kexec_total ?
  350. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  351. bar_clock_x = -1;
  352. bar_clock_w = 0;
  353. bar_clock_hot = 0;
  354. bar_net_x = -1;
  355. bar_net_w = 0;
  356. n = 0;
  357. while (msg[n])
  358. dt[n] = msg[n], n++;
  359. for (i = 0; i < 12; i++)
  360. dt[n + i] = (i < dots) ? '.' : ' ';
  361. n += 12;
  362. dt[n] = '\0';
  363. usurf_text(&screen,
  364. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  365. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  366. } else {
  367. unsigned int clock_chars = 0;
  368. int right = (int)fbw - 4;
  369. int min_x = (int)WM_BTN_END_X + 4;
  370. bar_clock_x = -1;
  371. bar_clock_w = 0;
  372. bar_net_x = -1;
  373. bar_net_w = 0;
  374. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  375. int tw = (int)usurf_text_width(&screen, dt);
  376. int tx = right - tw;
  377. n = 0;
  378. while (dt[n] && n < sizeof(dt) - 1)
  379. n++;
  380. bar_clock_x = tx;
  381. bar_clock_w = (unsigned int)tw;
  382. if (bar_clock_hot)
  383. usurf_fill(&screen, tx - 5, 2,
  384. (unsigned int)tw + 10,
  385. WM_BAR_H - 4, 0x3A3A55u);
  386. usurf_text(&screen, tx, 5, dt, COL_BANNER, 0, 0,
  387. fbw, WM_BAR_H);
  388. clock_chars = (unsigned int)tw;
  389. }
  390. if (clock_chars)
  391. right -= (int)clock_chars + 8;
  392. if (bar_temp_c >= 0) {
  393. char tb[12];
  394. int tx;
  395. (void)temp_text(tb, bar_temp_c);
  396. tx = right - (int)usurf_text_width(&screen, tb);
  397. if (tx >= min_x) {
  398. usurf_text(&screen, tx, 5, tb,
  399. temp_color(bar_temp_c), 0, 0,
  400. fbw, WM_BAR_H);
  401. right = tx - 8;
  402. }
  403. }
  404. if (bar_batt_pct >= 0) {
  405. char bb[10];
  406. int bx;
  407. (void)batt_text(bb, bar_batt_pct);
  408. bx = right - (int)usurf_text_width(&screen, bb);
  409. if (bx >= min_x) {
  410. usurf_text(&screen, bx, 5, bb,
  411. batt_color(bar_batt_pct,
  412. bar_batt_charging,
  413. bar_ac_online),
  414. 0, 0, fbw, WM_BAR_H);
  415. right = bx - 8;
  416. }
  417. }
  418. if (bar_ac_online >= 0) {
  419. char ab[8];
  420. int ax;
  421. (void)ac_text(ab, bar_ac_online);
  422. ax = right - (int)usurf_text_width(&screen, ab);
  423. if (ax >= min_x) {
  424. usurf_text(&screen, ax, 5, ab,
  425. ac_color(bar_ac_online), 0, 0,
  426. fbw, WM_BAR_H);
  427. right = ax - 8;
  428. }
  429. }
  430. {
  431. int nx = right - BAR_NET_W;
  432. if (nx >= min_x) {
  433. if (bar_net_hot)
  434. usurf_fill(&screen, nx - 3, 2,
  435. BAR_NET_W + 6,
  436. WM_BAR_H - 4, 0x3A3A55u);
  437. bar_draw_net_icon(nx, BAR_NET_Y);
  438. bar_net_x = nx;
  439. bar_net_w = BAR_NET_W;
  440. }
  441. }
  442. }
  443. }
  444. static void draw_window_clip(unsigned int idx, int cx, int cy,
  445. unsigned int cw, unsigned int ch)
  446. {
  447. struct uwin *u = &wins[idx];
  448. struct wm_window_info *w = &u->info;
  449. int x = w->x, y = w->y;
  450. unsigned int pw = w->w, ph = w->h;
  451. int bx = x + (int)pw - 1 - WM_CLOSE_W;
  452. int cony = y + 1 + WM_TITLE_H;
  453. int content_x = x + 1;
  454. unsigned int content_w = w->content_w;
  455. unsigned int content_h = w->content_h;
  456. /* Border, title bar, close button. */
  457. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  458. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  459. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  460. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  461. cx, cy, cw, ch);
  462. fill_clip(bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  463. cx, cy, cw, ch);
  464. usurf_text(&screen, bx + 4, y + 3, "x", COL_WHITE,
  465. cx, cy, cw, ch);
  466. /* Content: every window presents an RGB565 canvas (the terminal
  467. * renders its cells into one; gfx commands own theirs). A mapped
  468. * canvas covers the whole content area, so it does not need the black
  469. * underlay. */
  470. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  471. struct uwm_canvas c;
  472. c.px = (const unsigned short *)u->map.va;
  473. c.w = u->map.w;
  474. c.h = u->map.h;
  475. if (c.w != content_w || c.h != content_h)
  476. fill_clip(content_x, cony, content_w, content_h,
  477. 0x000000u, cx, cy, cw, ch);
  478. usurf_canvas(&screen, &c, content_x, cony,
  479. cx, cy, cw, ch);
  480. } else {
  481. fill_clip(content_x, cony, content_w, content_h,
  482. 0x000000u, cx, cy, cw, ch);
  483. }
  484. /* Resize grip (bottom-right corner). */
  485. if (w->focused) {
  486. unsigned int i;
  487. for (i = 0; i < 10; i++) {
  488. fill_clip(x + (int)pw - 3 - (int)i,
  489. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  490. cx, cy, cw, ch);
  491. fill_clip(x + (int)pw - 4,
  492. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  493. cx, cy, cw, ch);
  494. }
  495. }
  496. }
  497. /* Front-to-back window order: the focused window is on top, the others
  498. * follow in descending slot order. Painting and hit-testing share it. */
  499. static unsigned int build_order(int *order)
  500. {
  501. unsigned int i, n = 0;
  502. for (i = 0; i < WM_MAX_WINDOWS; i++)
  503. if (wins[i].info.used && wins[i].info.focused)
  504. order[n++] = (int)i;
  505. for (i = WM_MAX_WINDOWS; i-- > 0;)
  506. if (wins[i].info.used && !wins[i].info.focused)
  507. order[n++] = (int)i;
  508. return n;
  509. }
  510. struct orect {
  511. int x, y;
  512. unsigned int w, h;
  513. };
  514. #define OCC_MAX 64
  515. /* r minus cut, split into up to four disjoint rectangles. */
  516. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  517. const struct orect *cut)
  518. {
  519. int rx0 = r->x, ry0 = r->y;
  520. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  521. int cx0 = cut->x, cy0 = cut->y;
  522. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  523. unsigned int n = 0;
  524. if (cx0 < rx0) cx0 = rx0;
  525. if (cy0 < ry0) cy0 = ry0;
  526. if (cx1 > rx1) cx1 = rx1;
  527. if (cy1 > ry1) cy1 = ry1;
  528. if (cx1 <= cx0 || cy1 <= cy0) {
  529. out[0] = *r;
  530. return 1;
  531. }
  532. if (cy0 > ry0) {
  533. out[n].x = rx0;
  534. out[n].y = ry0;
  535. out[n].w = (unsigned int)(rx1 - rx0);
  536. out[n].h = (unsigned int)(cy0 - ry0);
  537. n++;
  538. }
  539. if (cy1 < ry1) {
  540. out[n].x = rx0;
  541. out[n].y = cy1;
  542. out[n].w = (unsigned int)(rx1 - rx0);
  543. out[n].h = (unsigned int)(ry1 - cy1);
  544. n++;
  545. }
  546. if (cx0 > rx0) {
  547. out[n].x = rx0;
  548. out[n].y = cy0;
  549. out[n].w = (unsigned int)(cx0 - rx0);
  550. out[n].h = (unsigned int)(cy1 - cy0);
  551. n++;
  552. }
  553. if (cx1 < rx1) {
  554. out[n].x = cx1;
  555. out[n].y = cy0;
  556. out[n].w = (unsigned int)(rx1 - cx1);
  557. out[n].h = (unsigned int)(cy1 - cy0);
  558. n++;
  559. }
  560. return n;
  561. }
  562. /* Back-to-front repaint, used only if the visible-region list overflows. */
  563. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  564. {
  565. unsigned int i;
  566. usurf_fill(&screen, x, y, w, h, COL_BG);
  567. for (i = 0; i < WM_MAX_WINDOWS; i++)
  568. if (wins[i].info.used && !wins[i].info.focused &&
  569. x < wins[i].info.x + (int)wins[i].info.w &&
  570. x + (int)w > wins[i].info.x &&
  571. y < wins[i].info.y + (int)wins[i].info.h &&
  572. y + (int)h > wins[i].info.y)
  573. draw_window_clip(i, x, y, w, h);
  574. for (i = 0; i < WM_MAX_WINDOWS; i++)
  575. if (wins[i].info.used && wins[i].info.focused &&
  576. x < wins[i].info.x + (int)wins[i].info.w &&
  577. x + (int)w > wins[i].info.x &&
  578. y < wins[i].info.y + (int)wins[i].info.h &&
  579. y + (int)h > wins[i].info.y)
  580. draw_window_clip(i, x, y, w, h);
  581. }
  582. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  583. {
  584. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  585. struct orect dmg;
  586. int order[WM_MAX_WINDOWS];
  587. unsigned int n, i, j, ncur, nnext;
  588. int overflow = 0;
  589. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  590. if (w == 0 || h == 0)
  591. return;
  592. if (x0 < 0) x0 = 0;
  593. if (y0 < 0) y0 = 0;
  594. if (x1 > (int)fbw) x1 = (int)fbw;
  595. if (y1 > (int)fbh) y1 = (int)fbh;
  596. if (x1 <= x0 || y1 <= y0)
  597. return;
  598. dmg.x = x0;
  599. dmg.y = y0;
  600. dmg.w = (unsigned int)(x1 - x0);
  601. dmg.h = (unsigned int)(y1 - y0);
  602. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  603. n = build_order(order);
  604. for (i = 0; i < n; i++) {
  605. struct uwin *u = &wins[order[i]];
  606. int wx0 = u->info.x, wy0 = u->info.y;
  607. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  608. if (wx0 < dmg.x) wx0 = dmg.x;
  609. if (wy0 < dmg.y) wy0 = dmg.y;
  610. if (wx1 > x1) wx1 = x1;
  611. if (wy1 > y1) wy1 = y1;
  612. if (wx1 <= wx0 || wy1 <= wy0)
  613. continue;
  614. cur[0].x = wx0;
  615. cur[0].y = wy0;
  616. cur[0].w = (unsigned int)(wx1 - wx0);
  617. cur[0].h = (unsigned int)(wy1 - wy0);
  618. ncur = 1;
  619. for (j = 0; j < i && ncur; j++) {
  620. struct uwin *f = &wins[order[j]];
  621. struct orect fr;
  622. unsigned int k;
  623. fr.x = f->info.x;
  624. fr.y = f->info.y;
  625. fr.w = f->info.w;
  626. fr.h = f->info.h;
  627. nnext = 0;
  628. for (k = 0; k < ncur; k++) {
  629. if (nnext + 4 > OCC_MAX) {
  630. overflow = 1;
  631. break;
  632. }
  633. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  634. }
  635. if (overflow)
  636. break;
  637. for (k = 0; k < nnext; k++)
  638. cur[k] = nxt[k];
  639. ncur = nnext;
  640. }
  641. if (overflow)
  642. break;
  643. for (j = 0; j < ncur; j++)
  644. draw_window_clip((unsigned int)order[i], cur[j].x,
  645. cur[j].y, cur[j].w, cur[j].h);
  646. }
  647. if (overflow)
  648. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  649. }
  650. static int window_at(int mx, int my)
  651. {
  652. int order[WM_MAX_WINDOWS];
  653. unsigned int n = build_order(order), i;
  654. for (i = 0; i < n; i++) {
  655. struct wm_window_info *w = &wins[order[i]].info;
  656. if (mx >= w->x && my >= w->y &&
  657. mx < w->x + (int)w->w &&
  658. my < w->y + (int)w->h)
  659. return order[i];
  660. }
  661. return -1;
  662. }
  663. static void clamp_geom(struct uwin *u, int *x, int *y)
  664. {
  665. /* Match wm_clamp_window(): allow the window to hang off the left,
  666. * right and bottom edges but keep the title bar reachable and below
  667. * the system bar. */
  668. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  669. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  670. int miny = WM_BAR_H;
  671. int maxy = (int)fbh - WM_TITLE_H;
  672. if (minx > maxx)
  673. minx = maxx = 0;
  674. if (miny > maxy)
  675. miny = maxy = WM_BAR_H;
  676. if (*x < minx)
  677. *x = minx;
  678. else if (*x > maxx)
  679. *x = maxx;
  680. if (*y < miny)
  681. *y = miny;
  682. else if (*y > maxy)
  683. *y = maxy;
  684. }
  685. static void add_rect(struct wm_present_info *p, int x, int y,
  686. unsigned int w, unsigned int h)
  687. {
  688. if (w == 0 || h == 0)
  689. return;
  690. if (p->count < 4) {
  691. p->rect[p->count].x = x;
  692. p->rect[p->count].y = y;
  693. p->rect[p->count].w = w;
  694. p->rect[p->count].h = h;
  695. p->count++;
  696. } else {
  697. /* Four is enough for normal updates; fold the rest into the
  698. * last rect rather than dropping pixels. */
  699. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  700. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  701. int nx1 = x + (int)w, ny1 = y + (int)h;
  702. if (x < x0) x0 = x;
  703. if (y < y0) y0 = y;
  704. if (nx1 > x1) x1 = nx1;
  705. if (ny1 > y1) y1 = ny1;
  706. p->rect[3].x = x0;
  707. p->rect[3].y = y0;
  708. p->rect[3].w = (unsigned int)(x1 - x0);
  709. p->rect[3].h = (unsigned int)(y1 - y0);
  710. }
  711. }
  712. /* A focused window is topmost, so the shadow contains exactly its pixels in
  713. * the old rectangle. Move that block and repaint only the old part the new
  714. * rectangle does not cover. When either rectangle is partly off-screen the
  715. * shadow has no valid pixels for the hidden part, so repaint the new rect
  716. * instead of copying desktop background into the window. */
  717. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  718. {
  719. struct wm_present_info present;
  720. struct orect oldr, newr, pieces[4];
  721. unsigned int np, k;
  722. int w = (int)u->info.w, h = (int)u->info.h;
  723. int old_on = ox >= 0 && oy >= 0 &&
  724. ox + w <= (int)fbw && oy + h <= (int)fbh;
  725. int new_on = nx >= 0 && ny >= 0 &&
  726. nx + w <= (int)fbw && ny + h <= (int)fbh;
  727. if (u_wm_begin() != 0)
  728. return;
  729. if (old_on && new_on)
  730. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  731. else
  732. draw_rect(nx, ny, u->info.w, u->info.h);
  733. oldr.x = ox;
  734. oldr.y = oy;
  735. oldr.w = u->info.w;
  736. oldr.h = u->info.h;
  737. newr.x = nx;
  738. newr.y = ny;
  739. newr.w = u->info.w;
  740. newr.h = u->info.h;
  741. np = rect_sub(pieces, &oldr, &newr);
  742. for (k = 0; k < np; k++)
  743. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  744. present.count = 0;
  745. present.immediate = 1;
  746. add_rect(&present, nx, ny, u->info.w, u->info.h);
  747. for (k = 0; k < np; k++)
  748. add_rect(&present, pieces[k].x, pieces[k].y,
  749. pieces[k].w, pieces[k].h);
  750. u_wm_present(&present);
  751. }
  752. static int iabs(int v)
  753. {
  754. return v < 0 ? -v : v;
  755. }
  756. /* Repaint the old and new rectangles of a geometry change. */
  757. static void redraw_geom(struct uwin *u, int ox, int oy,
  758. unsigned int ow, unsigned int oh)
  759. {
  760. struct wm_present_info present;
  761. unsigned int i;
  762. present.count = 0;
  763. present.immediate = 1;
  764. add_rect(&present, ox, oy, ow, oh);
  765. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  766. if (u_wm_begin() == 0) {
  767. for (i = 0; i < present.count; i++)
  768. draw_rect(present.rect[i].x, present.rect[i].y,
  769. present.rect[i].w, present.rect[i].h);
  770. u_wm_present(&present);
  771. }
  772. }
  773. /* Resize (when needed), move and remap a window in one step. */
  774. static int set_window_geometry(struct uwin *u, unsigned int idx,
  775. int nx, int ny, unsigned int nw, unsigned int nh)
  776. {
  777. struct wm_window_info ni;
  778. int ox = u->info.x, oy = u->info.y;
  779. unsigned int ow = u->info.w, oh = u->info.h;
  780. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  781. if (u_wm_resize(idx, nw - 2, nh - WM_TITLE_H - 2) != 0)
  782. return -1;
  783. }
  784. if (nx != ox || ny != oy)
  785. u_wm_set_geom(idx, nx, ny);
  786. u->mapped = 0;
  787. if (u_wm_info(idx, &ni) == 1)
  788. u->info = ni;
  789. if (u_wm_map(idx, &u->map) == 0)
  790. u->mapped = 1;
  791. redraw_geom(u, ox, oy, ow, oh);
  792. return 0;
  793. }
  794. static int restore_window(struct uwin *u, unsigned int idx)
  795. {
  796. if (!u->maximized)
  797. return 0;
  798. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  799. u->restore_w, u->restore_h) != 0)
  800. return -1;
  801. u->maximized = 0;
  802. return 0;
  803. }
  804. /* Double-click title bar: fill the work area below the system bar, or go back
  805. * to the geometry the window had before it was maximized. */
  806. static int toggle_maximize(struct uwin *u, unsigned int idx)
  807. {
  808. if (u->maximized)
  809. return restore_window(u, idx);
  810. u->restore_x = u->info.x;
  811. u->restore_y = u->info.y;
  812. u->restore_w = u->info.w;
  813. u->restore_h = u->info.h;
  814. if (set_window_geometry(u, idx, 0, WM_BAR_H,
  815. fbw, fbh - WM_BAR_H) != 0)
  816. return -1;
  817. u->maximized = 1;
  818. return 0;
  819. }
  820. /* The top-bar date/time text is also a button: it opens the Date & Time
  821. * editor. The rectangle is recorded by draw_bar(). */
  822. static int bar_clock_hit(unsigned int mx, unsigned int my)
  823. {
  824. return bar_clock_w && bar_clock_x >= 0 && my < WM_BAR_H &&
  825. (int)mx >= bar_clock_x - 5 &&
  826. (int)mx < bar_clock_x + (int)bar_clock_w + 5;
  827. }
  828. static int bar_net_hit(unsigned int mx, unsigned int my)
  829. {
  830. return bar_net_w && bar_net_x >= 0 && my < WM_BAR_H &&
  831. (int)mx >= bar_net_x - 3 &&
  832. (int)mx < bar_net_x + (int)bar_net_w + 3;
  833. }
  834. static void do_update(struct wm_status *st)
  835. {
  836. struct wm_present_info present;
  837. int need = 0;
  838. int bar_dirty = 0;
  839. unsigned int i;
  840. char dt[20];
  841. static char last_dt[20];
  842. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  843. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  844. unsigned int msg_now = u_ticks_ms();
  845. /* Spawn the first terminal once the kernel says the storage scan is
  846. * done, so a shell command can never race the mount table. */
  847. if (st->boot_ready && !boot_term_spawned) {
  848. boot_term_spawned = 1;
  849. if (!free_window_slot()) {
  850. bar_notice("window limit");
  851. full_redraw = 1;
  852. } else if (u_wm_create_term() < 0) {
  853. bar_notice("cannot start term");
  854. full_redraw = 1;
  855. } else {
  856. full_redraw = 1;
  857. }
  858. }
  859. if (bar_msg_dirty ||
  860. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  861. bar_dirty = 1;
  862. bar_msg_dirty = 0;
  863. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  864. bar_msg_until = 0;
  865. }
  866. bar_kexec_busy = st->kexec_busy;
  867. bar_kexec_progress = st->kexec_progress;
  868. bar_kexec_total = st->kexec_total;
  869. if (kexec_changed)
  870. bar_dirty = 1;
  871. {
  872. int hot = bar_clock_hit(st->mouse.x, st->mouse.y);
  873. if (hot != bar_clock_hot) {
  874. bar_clock_hot = hot;
  875. bar_dirty = 1;
  876. }
  877. }
  878. {
  879. int hot = bar_net_hit(st->mouse.x, st->mouse.y);
  880. if (hot != bar_net_hot) {
  881. bar_net_hot = hot;
  882. bar_dirty = 1;
  883. }
  884. }
  885. /* Refresh cached window state and mappings before opening a frame:
  886. * MAP may unmap/remap content. */
  887. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  888. struct wm_window_info info;
  889. int r = u_wm_info(i, &info);
  890. if (r < 0) {
  891. u_exit(1);
  892. }
  893. if (r == 0 || !info.used) {
  894. if (wins[i].info.used)
  895. full_redraw = 1;
  896. wins[i].info.used = 0;
  897. wins[i].mapped = 0;
  898. continue;
  899. }
  900. if (!wins[i].info.used ||
  901. info.content_gen != wins[i].info.content_gen)
  902. full_redraw = 1;
  903. wins[i].info = info;
  904. if (!wins[i].mapped ||
  905. wins[i].map.generation != info.content_gen) {
  906. struct wm_map_info map;
  907. if (u_wm_map(i, &map) == 0) {
  908. wins[i].map = map;
  909. wins[i].mapped = 1;
  910. } else {
  911. wins[i].mapped = 0;
  912. }
  913. }
  914. }
  915. /* Anchor the network menu under its bar icon once it has created its
  916. * window. The WM owns the geometry syscall, so the app itself does
  917. * not need a special placement ABI. */
  918. if (netmenu_pid > 0) {
  919. int found = 0;
  920. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  921. struct wm_window_info ni;
  922. if (!wins[i].info.used ||
  923. wins[i].info.pid != (unsigned int)netmenu_pid)
  924. continue;
  925. found = 1;
  926. if (!netmenu_placed) {
  927. int nx = (bar_net_x >= 0 ?
  928. bar_net_x + (int)bar_net_w :
  929. (int)fbw) - (int)wins[i].info.w;
  930. int ny = WM_BAR_H + 2;
  931. if (nx < 4)
  932. nx = 4;
  933. if (nx + (int)wins[i].info.w > (int)fbw - 4)
  934. nx = (int)fbw - 4 -
  935. (int)wins[i].info.w;
  936. if (u_wm_set_geom(i, nx, ny) == 0 &&
  937. u_wm_info(i, &ni) == 1) {
  938. wins[i].info = ni;
  939. full_redraw = 1;
  940. }
  941. netmenu_placed = 1;
  942. }
  943. break;
  944. }
  945. if (!found && !u_alive((unsigned int)netmenu_pid)) {
  946. netmenu_pid = 0;
  947. netmenu_placed = 0;
  948. }
  949. }
  950. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  951. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  952. {
  953. static unsigned int last_rtc_ms;
  954. unsigned int now_ms = u_ticks_ms();
  955. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  956. last_rtc_ms = now_ms;
  957. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  958. for (i = 0; i < sizeof(dt); i++)
  959. if (dt[i] != last_dt[i]) {
  960. for (i = 0; i < sizeof(dt); i++)
  961. last_dt[i] = dt[i];
  962. bar_dirty = 1;
  963. break;
  964. }
  965. }
  966. {
  967. struct sys_cputemp ct;
  968. if (u_cpu_temp(&ct) == 0 &&
  969. ct.temp_c != bar_temp_c) {
  970. bar_temp_c = ct.temp_c;
  971. bar_dirty = 1;
  972. }
  973. }
  974. {
  975. struct sys_powerinfo pi;
  976. int pct = -1, charging = 0, ac = -1;
  977. unsigned int bi;
  978. if (u_power_info(&pi, 0) == 0) {
  979. /* A laptop EC (ThinkPad or Chrome
  980. * EC) marks a machine with AC/battery;
  981. * desktops show neither. */
  982. if (pi.present) {
  983. ac = pi.ac_online;
  984. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  985. bi++) {
  986. struct sys_battery *b =
  987. &pi.battery[bi];
  988. if (!b->present ||
  989. !b->percent_valid)
  990. continue;
  991. pct = (int)b->percent;
  992. charging = (int)b->charging;
  993. break;
  994. }
  995. }
  996. if (pct != bar_batt_pct ||
  997. charging != bar_batt_charging ||
  998. ac != bar_ac_online) {
  999. bar_batt_pct = pct;
  1000. bar_batt_charging = charging;
  1001. bar_ac_online = ac;
  1002. bar_dirty = 1;
  1003. }
  1004. }
  1005. }
  1006. if (poll_net_status())
  1007. bar_dirty = 1;
  1008. }
  1009. }
  1010. if (full_redraw) {
  1011. if (u_wm_begin() != 0) {
  1012. full_redraw = 1;
  1013. return;
  1014. }
  1015. full_redraw = 0;
  1016. need = 1;
  1017. {
  1018. draw_rect(0, 0, fbw, fbh);
  1019. draw_bar();
  1020. present.count = 0;
  1021. add_rect(&present, 0, 0, fbw, fbh);
  1022. present.immediate = 0;
  1023. u_wm_present(&present);
  1024. /* Ack every window that was clean at the time of the
  1025. * redraw. If new damage arrived during it, its seq
  1026. * differs and the kernel leaves it dirty. */
  1027. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1028. if (wins[i].info.used && wins[i].mapped)
  1029. u_wm_ack(i, wins[i].info.dirty_seq);
  1030. }
  1031. return;
  1032. }
  1033. /* Partial cell/canvas updates and the bar clock. */
  1034. present.count = 0;
  1035. present.immediate = 0;
  1036. if (bar_dirty) {
  1037. need = 1;
  1038. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1039. }
  1040. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1041. struct uwin *u = &wins[i];
  1042. struct wm_window_info *w = &u->info;
  1043. int rx, ry;
  1044. unsigned int rw, rh;
  1045. if (!w->used || !w->dirty)
  1046. continue;
  1047. if (!w->dmg_w || !w->dmg_h)
  1048. continue;
  1049. rx = w->x + 1 + (int)w->dmg_x;
  1050. ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_y;
  1051. rw = w->dmg_w;
  1052. rh = w->dmg_h;
  1053. add_rect(&present, rx, ry, rw, rh);
  1054. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1055. need = 1;
  1056. }
  1057. if (!need) {
  1058. /* Drag movement is handled below; a stationary mouse can be
  1059. * left to the kernel's cursor overlay. */
  1060. return;
  1061. }
  1062. if (u_wm_begin() != 0)
  1063. return;
  1064. for (i = 0; i < present.count; i++)
  1065. draw_rect(present.rect[i].x, present.rect[i].y,
  1066. present.rect[i].w, present.rect[i].h);
  1067. if (bar_dirty)
  1068. draw_bar();
  1069. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1070. struct uwin *u = &wins[i];
  1071. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1072. !u->ack_seq)
  1073. continue;
  1074. if (u->ack_seq == u->info.dirty_seq)
  1075. u_wm_ack(i, u->info.dirty_seq);
  1076. }
  1077. u_wm_present(&present);
  1078. }
  1079. static int spawn_app(const char *path)
  1080. {
  1081. int pid = u_spawn(path, "", 0, "/");
  1082. if (pid > 0)
  1083. full_redraw = 1;
  1084. return pid;
  1085. }
  1086. static void spawn_app_checked(const char *path)
  1087. {
  1088. if (!free_window_slot()) {
  1089. bar_notice("window limit");
  1090. full_redraw = 1;
  1091. return;
  1092. }
  1093. if (spawn_app(path) < 0) {
  1094. bar_notice("spawn failed");
  1095. full_redraw = 1;
  1096. }
  1097. }
  1098. /* The network icon opens one menu at a time; a second click focuses it. */
  1099. static void open_netmenu(void)
  1100. {
  1101. unsigned int i;
  1102. if (netmenu_pid > 0 && u_alive((unsigned int)netmenu_pid)) {
  1103. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1104. if (wins[i].info.used &&
  1105. wins[i].info.pid == (unsigned int)netmenu_pid) {
  1106. u_wm_focus(i);
  1107. full_redraw = 1;
  1108. return;
  1109. }
  1110. }
  1111. if (!free_window_slot()) {
  1112. bar_notice("window limit");
  1113. full_redraw = 1;
  1114. return;
  1115. }
  1116. netmenu_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  1117. netmenu_placed = 0;
  1118. if (netmenu_pid < 0) {
  1119. netmenu_pid = 0;
  1120. bar_notice("spawn failed");
  1121. full_redraw = 1;
  1122. } else {
  1123. full_redraw = 1;
  1124. }
  1125. }
  1126. static void handle_mouse(struct wm_status *st)
  1127. {
  1128. unsigned int buttons = st->mouse.buttons;
  1129. unsigned int mx = st->mouse.x, my = st->mouse.y;
  1130. int down = (buttons & 1) != 0;
  1131. int was = (prev_buttons & 1) != 0;
  1132. if (down && !was) {
  1133. int prev_title_id = last_title_id;
  1134. unsigned int now = u_ticks_ms();
  1135. last_title_id = -1;
  1136. if (bar_net_hit(mx, my)) {
  1137. open_netmenu();
  1138. } else if (bar_clock_hit(mx, my)) {
  1139. spawn_app_checked("/ramfs/bin/settime");
  1140. } else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  1141. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  1142. if (!free_window_slot()) {
  1143. bar_notice("window limit");
  1144. full_redraw = 1;
  1145. } else if (u_wm_create_term() < 0) {
  1146. bar_notice("cannot start term");
  1147. full_redraw = 1;
  1148. } else {
  1149. full_redraw = 1;
  1150. }
  1151. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  1152. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  1153. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  1154. spawn_app_checked("/ramfs/bin/clock");
  1155. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  1156. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  1157. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  1158. spawn_app_checked("/ramfs/bin/colors");
  1159. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  1160. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  1161. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  1162. spawn_app_checked("/ramfs/bin/files");
  1163. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  1164. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  1165. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  1166. spawn_app_checked("/ramfs/bin/mines");
  1167. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  1168. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  1169. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  1170. spawn_app_checked("/ramfs/bin/browser");
  1171. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  1172. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  1173. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  1174. spawn_app_checked("/ramfs/bin/solitaire");
  1175. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  1176. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  1177. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  1178. spawn_app_checked("/ramfs/bin/gallery");
  1179. } else if (mx >= WM_BTN_CHAT_X && my >= WM_BTN_CHAT_Y &&
  1180. mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
  1181. my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
  1182. spawn_app_checked("/ramfs/bin/chat");
  1183. } else {
  1184. int idx = window_at((int)mx, (int)my);
  1185. if (idx >= 0) {
  1186. struct uwin *u = &wins[idx];
  1187. int pmx = (int)mx, pmy = (int)my;
  1188. int rx = u->info.x + (int)u->info.w - 1 -
  1189. WM_CLOSE_W;
  1190. int ry = u->info.y + 1;
  1191. u_wm_focus((unsigned int)idx);
  1192. if (pmx >= rx &&
  1193. pmx < u->info.x + (int)u->info.w &&
  1194. pmy >= ry && pmy < ry + WM_TITLE_H) {
  1195. u_wm_close((unsigned int)idx);
  1196. full_redraw = 1;
  1197. } else if (pmx >= u->info.x + (int)u->info.w - 16 &&
  1198. pmy >= u->info.y + (int)u->info.h - 16) {
  1199. resize_id = idx;
  1200. resize_mx = mx;
  1201. resize_my = my;
  1202. resize_ow = u->info.w;
  1203. resize_oh = u->info.h;
  1204. } else if (pmy < u->info.y + WM_TITLE_H) {
  1205. if (prev_title_id == idx &&
  1206. now - last_title_ms <= WM_DBLCLICK_MS &&
  1207. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  1208. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  1209. last_title_ms = 0;
  1210. if (toggle_maximize(u,
  1211. (unsigned int)idx) != 0)
  1212. bar_notice("resize failed");
  1213. } else {
  1214. last_title_id = idx;
  1215. last_title_ms = now;
  1216. last_title_x = pmx;
  1217. last_title_y = pmy;
  1218. drag_id = idx;
  1219. drag_mx = pmx;
  1220. drag_my = pmy;
  1221. drag_dx = pmx - u->info.x;
  1222. drag_dy = pmy - u->info.y;
  1223. }
  1224. }
  1225. }
  1226. }
  1227. } else if (down && was && resize_id >= 0) {
  1228. struct uwin *u = &wins[resize_id];
  1229. if (u->info.used) {
  1230. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  1231. int nh = (int)resize_oh + (int)my - (int)resize_my;
  1232. int maxw = (int)fbw;
  1233. int maxh = (int)fbh - WM_BAR_H;
  1234. if (nw < 200) nw = 200;
  1235. if (nh < 140) nh = 140;
  1236. if (nw > maxw) nw = maxw;
  1237. if (nh > maxh) nh = maxh;
  1238. /* Dragging the grip into the screen corner means "as
  1239. * large as the work area", even though the grip starts
  1240. * 16 px inside the window edge. */
  1241. if ((int)mx >= (int)fbw - 1)
  1242. nw = maxw;
  1243. if ((int)my >= (int)fbh - 1)
  1244. nh = maxh;
  1245. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  1246. if (set_window_geometry(u,
  1247. (unsigned int)resize_id, u->info.x,
  1248. u->info.y, (unsigned int)nw,
  1249. (unsigned int)nh) == 0)
  1250. u->maximized = 0;
  1251. }
  1252. } else {
  1253. resize_id = -1;
  1254. }
  1255. } else if (down && was && drag_id >= 0) {
  1256. struct uwin *u = &wins[drag_id];
  1257. if (u->info.used) {
  1258. int ox, oy, nx, ny;
  1259. int started = !u->maximized ||
  1260. iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  1261. iabs((int)my - drag_my) >= WM_DRAG_START;
  1262. /* A maximized window is restored as soon as the drag
  1263. * starts; keep the pointer's grab point relative to the
  1264. * restored geometry. A click that does not move must
  1265. * leave it maximized. */
  1266. if (u->maximized && started &&
  1267. restore_window(u, (unsigned int)drag_id) == 0) {
  1268. drag_dx = drag_mx - u->info.x;
  1269. drag_dy = drag_my - u->info.y;
  1270. }
  1271. if (!u->maximized) {
  1272. ox = u->info.x;
  1273. oy = u->info.y;
  1274. nx = (int)mx - drag_dx;
  1275. ny = (int)my - drag_dy;
  1276. clamp_geom(u, &nx, &ny);
  1277. if (nx != ox || ny != oy) {
  1278. struct wm_present_info present;
  1279. unsigned int i;
  1280. u_wm_set_geom((unsigned int)drag_id,
  1281. nx, ny);
  1282. u->info.x = nx;
  1283. u->info.y = ny;
  1284. if (u->info.focused) {
  1285. move_window_pixels(u, ox, oy,
  1286. nx, ny);
  1287. } else {
  1288. present.count = 0;
  1289. present.immediate = 1;
  1290. add_rect(&present, ox, oy,
  1291. u->info.w, u->info.h);
  1292. add_rect(&present, nx, ny,
  1293. u->info.w, u->info.h);
  1294. if (u_wm_begin() == 0) {
  1295. for (i = 0;
  1296. i < present.count; i++)
  1297. draw_rect(
  1298. present.rect[i].x,
  1299. present.rect[i].y,
  1300. present.rect[i].w,
  1301. present.rect[i].h);
  1302. u_wm_present(&present);
  1303. }
  1304. }
  1305. }
  1306. }
  1307. } else {
  1308. drag_id = -1;
  1309. }
  1310. } else if (!down && was) {
  1311. drag_id = -1;
  1312. resize_id = -1;
  1313. }
  1314. prev_buttons = buttons;
  1315. }
  1316. static char *put_str(char *p, const char *s)
  1317. {
  1318. while (*s)
  1319. *p++ = *s++;
  1320. return p;
  1321. }
  1322. static char *put_dec(char *p, unsigned int v)
  1323. {
  1324. char b[10];
  1325. unsigned int n = 0;
  1326. do {
  1327. b[n++] = (char)('0' + v % 10);
  1328. v /= 10;
  1329. } while (v);
  1330. while (n)
  1331. *p++ = b[--n];
  1332. return p;
  1333. }
  1334. /* Expose the last interval in /share so the target can be measured without
  1335. * a serial console. */
  1336. static void write_stats(void)
  1337. {
  1338. char b[80], *p = b;
  1339. unsigned int now = u_ticks_ms();
  1340. p = put_str(p, "t=");
  1341. p = put_dec(p, now / 1000);
  1342. p = put_str(p, "s frames=");
  1343. p = put_dec(p, stat_frames);
  1344. p = put_str(p, " ms=");
  1345. p = put_dec(p, stat_ms);
  1346. p = put_str(p, " max=");
  1347. p = put_dec(p, stat_max);
  1348. p = put_str(p, " px=");
  1349. p = put_dec(p, stat_px);
  1350. p = put_str(p, "\n");
  1351. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  1352. (int)(p - b));
  1353. }
  1354. void _start(const char *arg)
  1355. {
  1356. struct wm_screen_info si;
  1357. struct wm_status st;
  1358. unsigned int i;
  1359. (void)arg;
  1360. if (u_wm_attach(&si) != 0)
  1361. u_exit(1);
  1362. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  1363. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  1364. fbw = si.width;
  1365. fbh = si.height;
  1366. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1367. wins[i].info.used = 0;
  1368. wins[i].mapped = 0;
  1369. }
  1370. full_redraw = 1;
  1371. for (;;) {
  1372. unsigned int t0, t1;
  1373. if (u_wm_poll(&st) != 0)
  1374. u_exit(1);
  1375. if (st.full_redraw)
  1376. full_redraw = 1;
  1377. usurf_pixels = 0;
  1378. t0 = u_ticks_ms();
  1379. handle_mouse(&st);
  1380. do_update(&st);
  1381. t1 = u_ticks_ms();
  1382. if (usurf_pixels) {
  1383. unsigned int dt = t1 - t0;
  1384. stat_frames++;
  1385. stat_ms += dt;
  1386. stat_px += usurf_pixels;
  1387. if (dt > stat_max)
  1388. stat_max = dt;
  1389. }
  1390. if (t1 - stat_last >= 2000) {
  1391. stat_last = t1;
  1392. if (stat_frames) {
  1393. write_stats();
  1394. stat_frames = stat_ms = stat_px = stat_max = 0;
  1395. }
  1396. }
  1397. u_sleep_ms(10);
  1398. }
  1399. }