ucmd_wm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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. /* The network status is the rightmost item, so it survives
  375. * narrow screens and a full temperature/battery cluster. */
  376. {
  377. int nx = right - BAR_NET_W;
  378. if (nx >= min_x) {
  379. if (bar_net_hot)
  380. usurf_fill(&screen, nx - 3, 2,
  381. BAR_NET_W + 6,
  382. WM_BAR_H - 4, 0x3A3A55u);
  383. bar_draw_net_icon(nx, BAR_NET_Y);
  384. bar_net_x = nx;
  385. bar_net_w = BAR_NET_W;
  386. right = nx - 8;
  387. }
  388. }
  389. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  390. int tw = (int)usurf_text_width(&screen, dt);
  391. int tx = right - tw;
  392. n = 0;
  393. while (dt[n] && n < sizeof(dt) - 1)
  394. n++;
  395. bar_clock_x = tx;
  396. bar_clock_w = (unsigned int)tw;
  397. if (bar_clock_hot)
  398. usurf_fill(&screen, tx - 5, 2,
  399. (unsigned int)tw + 10,
  400. WM_BAR_H - 4, 0x3A3A55u);
  401. usurf_text(&screen, tx, 5, dt, COL_BANNER, 0, 0,
  402. fbw, WM_BAR_H);
  403. clock_chars = (unsigned int)tw;
  404. }
  405. if (clock_chars)
  406. right -= (int)clock_chars + 8;
  407. if (bar_temp_c >= 0) {
  408. char tb[12];
  409. int tx;
  410. (void)temp_text(tb, bar_temp_c);
  411. tx = right - (int)usurf_text_width(&screen, tb);
  412. if (tx >= min_x) {
  413. usurf_text(&screen, tx, 5, tb,
  414. temp_color(bar_temp_c), 0, 0,
  415. fbw, WM_BAR_H);
  416. right = tx - 8;
  417. }
  418. }
  419. if (bar_batt_pct >= 0) {
  420. char bb[10];
  421. int bx;
  422. (void)batt_text(bb, bar_batt_pct);
  423. bx = right - (int)usurf_text_width(&screen, bb);
  424. if (bx >= min_x) {
  425. usurf_text(&screen, bx, 5, bb,
  426. batt_color(bar_batt_pct,
  427. bar_batt_charging,
  428. bar_ac_online),
  429. 0, 0, fbw, WM_BAR_H);
  430. right = bx - 8;
  431. }
  432. }
  433. if (bar_ac_online >= 0) {
  434. char ab[8];
  435. int ax;
  436. (void)ac_text(ab, bar_ac_online);
  437. ax = right - (int)usurf_text_width(&screen, ab);
  438. if (ax >= min_x) {
  439. usurf_text(&screen, ax, 5, ab,
  440. ac_color(bar_ac_online), 0, 0,
  441. fbw, WM_BAR_H);
  442. right = ax - 8;
  443. }
  444. }
  445. }
  446. }
  447. static void draw_window_clip(unsigned int idx, int cx, int cy,
  448. unsigned int cw, unsigned int ch)
  449. {
  450. struct uwin *u = &wins[idx];
  451. struct wm_window_info *w = &u->info;
  452. int x = w->x, y = w->y;
  453. unsigned int pw = w->w, ph = w->h;
  454. int bx = x + (int)pw - 1 - WM_CLOSE_W;
  455. int cony = y + 1 + WM_TITLE_H;
  456. int content_x = x + 1;
  457. unsigned int content_w = w->content_w;
  458. unsigned int content_h = w->content_h;
  459. /* Border, title bar, close button. */
  460. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  461. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  462. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  463. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  464. cx, cy, cw, ch);
  465. fill_clip(bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  466. cx, cy, cw, ch);
  467. usurf_text(&screen, bx + 4, y + 3, "x", COL_WHITE,
  468. cx, cy, cw, ch);
  469. /* Content: every window presents an RGB565 canvas (the terminal
  470. * renders its cells into one; gfx commands own theirs). A mapped
  471. * canvas covers the whole content area, so it does not need the black
  472. * underlay. */
  473. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  474. struct uwm_canvas c;
  475. c.px = (const unsigned short *)u->map.va;
  476. c.w = u->map.w;
  477. c.h = u->map.h;
  478. if (c.w != content_w || c.h != content_h)
  479. fill_clip(content_x, cony, content_w, content_h,
  480. 0x000000u, cx, cy, cw, ch);
  481. usurf_canvas(&screen, &c, content_x, cony,
  482. cx, cy, cw, ch);
  483. } else {
  484. fill_clip(content_x, cony, content_w, content_h,
  485. 0x000000u, cx, cy, cw, ch);
  486. }
  487. /* Resize grip (bottom-right corner). */
  488. if (w->focused) {
  489. unsigned int i;
  490. for (i = 0; i < 10; i++) {
  491. fill_clip(x + (int)pw - 3 - (int)i,
  492. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  493. cx, cy, cw, ch);
  494. fill_clip(x + (int)pw - 4,
  495. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  496. cx, cy, cw, ch);
  497. }
  498. }
  499. }
  500. /* Front-to-back window order: the focused window is on top, the others
  501. * follow in descending slot order. Painting and hit-testing share it. */
  502. static unsigned int build_order(int *order)
  503. {
  504. unsigned int i, n = 0;
  505. for (i = 0; i < WM_MAX_WINDOWS; i++)
  506. if (wins[i].info.used && wins[i].info.focused)
  507. order[n++] = (int)i;
  508. for (i = WM_MAX_WINDOWS; i-- > 0;)
  509. if (wins[i].info.used && !wins[i].info.focused)
  510. order[n++] = (int)i;
  511. return n;
  512. }
  513. struct orect {
  514. int x, y;
  515. unsigned int w, h;
  516. };
  517. #define OCC_MAX 64
  518. /* r minus cut, split into up to four disjoint rectangles. */
  519. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  520. const struct orect *cut)
  521. {
  522. int rx0 = r->x, ry0 = r->y;
  523. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  524. int cx0 = cut->x, cy0 = cut->y;
  525. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  526. unsigned int n = 0;
  527. if (cx0 < rx0) cx0 = rx0;
  528. if (cy0 < ry0) cy0 = ry0;
  529. if (cx1 > rx1) cx1 = rx1;
  530. if (cy1 > ry1) cy1 = ry1;
  531. if (cx1 <= cx0 || cy1 <= cy0) {
  532. out[0] = *r;
  533. return 1;
  534. }
  535. if (cy0 > ry0) {
  536. out[n].x = rx0;
  537. out[n].y = ry0;
  538. out[n].w = (unsigned int)(rx1 - rx0);
  539. out[n].h = (unsigned int)(cy0 - ry0);
  540. n++;
  541. }
  542. if (cy1 < ry1) {
  543. out[n].x = rx0;
  544. out[n].y = cy1;
  545. out[n].w = (unsigned int)(rx1 - rx0);
  546. out[n].h = (unsigned int)(ry1 - cy1);
  547. n++;
  548. }
  549. if (cx0 > rx0) {
  550. out[n].x = rx0;
  551. out[n].y = cy0;
  552. out[n].w = (unsigned int)(cx0 - rx0);
  553. out[n].h = (unsigned int)(cy1 - cy0);
  554. n++;
  555. }
  556. if (cx1 < rx1) {
  557. out[n].x = cx1;
  558. out[n].y = cy0;
  559. out[n].w = (unsigned int)(rx1 - cx1);
  560. out[n].h = (unsigned int)(cy1 - cy0);
  561. n++;
  562. }
  563. return n;
  564. }
  565. /* Back-to-front repaint, used only if the visible-region list overflows. */
  566. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  567. {
  568. unsigned int i;
  569. usurf_fill(&screen, x, y, w, h, COL_BG);
  570. for (i = 0; i < WM_MAX_WINDOWS; i++)
  571. if (wins[i].info.used && !wins[i].info.focused &&
  572. x < wins[i].info.x + (int)wins[i].info.w &&
  573. x + (int)w > wins[i].info.x &&
  574. y < wins[i].info.y + (int)wins[i].info.h &&
  575. y + (int)h > wins[i].info.y)
  576. draw_window_clip(i, x, y, w, h);
  577. for (i = 0; i < WM_MAX_WINDOWS; i++)
  578. if (wins[i].info.used && wins[i].info.focused &&
  579. x < wins[i].info.x + (int)wins[i].info.w &&
  580. x + (int)w > wins[i].info.x &&
  581. y < wins[i].info.y + (int)wins[i].info.h &&
  582. y + (int)h > wins[i].info.y)
  583. draw_window_clip(i, x, y, w, h);
  584. }
  585. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  586. {
  587. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  588. struct orect dmg;
  589. int order[WM_MAX_WINDOWS];
  590. unsigned int n, i, j, ncur, nnext;
  591. int overflow = 0;
  592. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  593. if (w == 0 || h == 0)
  594. return;
  595. if (x0 < 0) x0 = 0;
  596. if (y0 < 0) y0 = 0;
  597. if (x1 > (int)fbw) x1 = (int)fbw;
  598. if (y1 > (int)fbh) y1 = (int)fbh;
  599. if (x1 <= x0 || y1 <= y0)
  600. return;
  601. dmg.x = x0;
  602. dmg.y = y0;
  603. dmg.w = (unsigned int)(x1 - x0);
  604. dmg.h = (unsigned int)(y1 - y0);
  605. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  606. n = build_order(order);
  607. for (i = 0; i < n; i++) {
  608. struct uwin *u = &wins[order[i]];
  609. int wx0 = u->info.x, wy0 = u->info.y;
  610. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  611. if (wx0 < dmg.x) wx0 = dmg.x;
  612. if (wy0 < dmg.y) wy0 = dmg.y;
  613. if (wx1 > x1) wx1 = x1;
  614. if (wy1 > y1) wy1 = y1;
  615. if (wx1 <= wx0 || wy1 <= wy0)
  616. continue;
  617. cur[0].x = wx0;
  618. cur[0].y = wy0;
  619. cur[0].w = (unsigned int)(wx1 - wx0);
  620. cur[0].h = (unsigned int)(wy1 - wy0);
  621. ncur = 1;
  622. for (j = 0; j < i && ncur; j++) {
  623. struct uwin *f = &wins[order[j]];
  624. struct orect fr;
  625. unsigned int k;
  626. fr.x = f->info.x;
  627. fr.y = f->info.y;
  628. fr.w = f->info.w;
  629. fr.h = f->info.h;
  630. nnext = 0;
  631. for (k = 0; k < ncur; k++) {
  632. if (nnext + 4 > OCC_MAX) {
  633. overflow = 1;
  634. break;
  635. }
  636. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  637. }
  638. if (overflow)
  639. break;
  640. for (k = 0; k < nnext; k++)
  641. cur[k] = nxt[k];
  642. ncur = nnext;
  643. }
  644. if (overflow)
  645. break;
  646. for (j = 0; j < ncur; j++)
  647. draw_window_clip((unsigned int)order[i], cur[j].x,
  648. cur[j].y, cur[j].w, cur[j].h);
  649. }
  650. if (overflow)
  651. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  652. }
  653. static int window_at(int mx, int my)
  654. {
  655. int order[WM_MAX_WINDOWS];
  656. unsigned int n = build_order(order), i;
  657. for (i = 0; i < n; i++) {
  658. struct wm_window_info *w = &wins[order[i]].info;
  659. if (mx >= w->x && my >= w->y &&
  660. mx < w->x + (int)w->w &&
  661. my < w->y + (int)w->h)
  662. return order[i];
  663. }
  664. return -1;
  665. }
  666. static void clamp_geom(struct uwin *u, int *x, int *y)
  667. {
  668. /* Match wm_clamp_window(): allow the window to hang off the left,
  669. * right and bottom edges but keep the title bar reachable and below
  670. * the system bar. */
  671. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  672. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  673. int miny = WM_BAR_H;
  674. int maxy = (int)fbh - WM_TITLE_H;
  675. if (minx > maxx)
  676. minx = maxx = 0;
  677. if (miny > maxy)
  678. miny = maxy = WM_BAR_H;
  679. if (*x < minx)
  680. *x = minx;
  681. else if (*x > maxx)
  682. *x = maxx;
  683. if (*y < miny)
  684. *y = miny;
  685. else if (*y > maxy)
  686. *y = maxy;
  687. }
  688. static void add_rect(struct wm_present_info *p, int x, int y,
  689. unsigned int w, unsigned int h)
  690. {
  691. if (w == 0 || h == 0)
  692. return;
  693. if (p->count < 4) {
  694. p->rect[p->count].x = x;
  695. p->rect[p->count].y = y;
  696. p->rect[p->count].w = w;
  697. p->rect[p->count].h = h;
  698. p->count++;
  699. } else {
  700. /* Four is enough for normal updates; fold the rest into the
  701. * last rect rather than dropping pixels. */
  702. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  703. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  704. int nx1 = x + (int)w, ny1 = y + (int)h;
  705. if (x < x0) x0 = x;
  706. if (y < y0) y0 = y;
  707. if (nx1 > x1) x1 = nx1;
  708. if (ny1 > y1) y1 = ny1;
  709. p->rect[3].x = x0;
  710. p->rect[3].y = y0;
  711. p->rect[3].w = (unsigned int)(x1 - x0);
  712. p->rect[3].h = (unsigned int)(y1 - y0);
  713. }
  714. }
  715. /* A focused window is topmost, so the shadow contains exactly its pixels in
  716. * the old rectangle. Move that block and repaint only the old part the new
  717. * rectangle does not cover. When either rectangle is partly off-screen the
  718. * shadow has no valid pixels for the hidden part, so repaint the new rect
  719. * instead of copying desktop background into the window. */
  720. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  721. {
  722. struct wm_present_info present;
  723. struct orect oldr, newr, pieces[4];
  724. unsigned int np, k;
  725. int w = (int)u->info.w, h = (int)u->info.h;
  726. int old_on = ox >= 0 && oy >= 0 &&
  727. ox + w <= (int)fbw && oy + h <= (int)fbh;
  728. int new_on = nx >= 0 && ny >= 0 &&
  729. nx + w <= (int)fbw && ny + h <= (int)fbh;
  730. if (u_wm_begin() != 0)
  731. return;
  732. if (old_on && new_on)
  733. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  734. else
  735. draw_rect(nx, ny, u->info.w, u->info.h);
  736. oldr.x = ox;
  737. oldr.y = oy;
  738. oldr.w = u->info.w;
  739. oldr.h = u->info.h;
  740. newr.x = nx;
  741. newr.y = ny;
  742. newr.w = u->info.w;
  743. newr.h = u->info.h;
  744. np = rect_sub(pieces, &oldr, &newr);
  745. for (k = 0; k < np; k++)
  746. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  747. present.count = 0;
  748. present.immediate = 1;
  749. add_rect(&present, nx, ny, u->info.w, u->info.h);
  750. for (k = 0; k < np; k++)
  751. add_rect(&present, pieces[k].x, pieces[k].y,
  752. pieces[k].w, pieces[k].h);
  753. u_wm_present(&present);
  754. }
  755. static int iabs(int v)
  756. {
  757. return v < 0 ? -v : v;
  758. }
  759. /* Repaint the old and new rectangles of a geometry change. */
  760. static void redraw_geom(struct uwin *u, int ox, int oy,
  761. unsigned int ow, unsigned int oh)
  762. {
  763. struct wm_present_info present;
  764. unsigned int i;
  765. present.count = 0;
  766. present.immediate = 1;
  767. add_rect(&present, ox, oy, ow, oh);
  768. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  769. if (u_wm_begin() == 0) {
  770. for (i = 0; i < present.count; i++)
  771. draw_rect(present.rect[i].x, present.rect[i].y,
  772. present.rect[i].w, present.rect[i].h);
  773. u_wm_present(&present);
  774. }
  775. }
  776. /* Resize (when needed), move and remap a window in one step. */
  777. static int set_window_geometry(struct uwin *u, unsigned int idx,
  778. int nx, int ny, unsigned int nw, unsigned int nh)
  779. {
  780. struct wm_window_info ni;
  781. int ox = u->info.x, oy = u->info.y;
  782. unsigned int ow = u->info.w, oh = u->info.h;
  783. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  784. if (u_wm_resize(idx, nw - 2, nh - WM_TITLE_H - 2) != 0)
  785. return -1;
  786. }
  787. if (nx != ox || ny != oy)
  788. u_wm_set_geom(idx, nx, ny);
  789. u->mapped = 0;
  790. if (u_wm_info(idx, &ni) == 1)
  791. u->info = ni;
  792. if (u_wm_map(idx, &u->map) == 0)
  793. u->mapped = 1;
  794. redraw_geom(u, ox, oy, ow, oh);
  795. return 0;
  796. }
  797. static int restore_window(struct uwin *u, unsigned int idx)
  798. {
  799. if (!u->maximized)
  800. return 0;
  801. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  802. u->restore_w, u->restore_h) != 0)
  803. return -1;
  804. u->maximized = 0;
  805. return 0;
  806. }
  807. /* Double-click title bar: fill the work area below the system bar, or go back
  808. * to the geometry the window had before it was maximized. */
  809. static int toggle_maximize(struct uwin *u, unsigned int idx)
  810. {
  811. if (u->maximized)
  812. return restore_window(u, idx);
  813. u->restore_x = u->info.x;
  814. u->restore_y = u->info.y;
  815. u->restore_w = u->info.w;
  816. u->restore_h = u->info.h;
  817. if (set_window_geometry(u, idx, 0, WM_BAR_H,
  818. fbw, fbh - WM_BAR_H) != 0)
  819. return -1;
  820. u->maximized = 1;
  821. return 0;
  822. }
  823. /* The top-bar date/time text is also a button: it opens the Date & Time
  824. * editor. The rectangle is recorded by draw_bar(). */
  825. static int bar_clock_hit(unsigned int mx, unsigned int my)
  826. {
  827. return bar_clock_w && bar_clock_x >= 0 && my < WM_BAR_H &&
  828. (int)mx >= bar_clock_x - 5 &&
  829. (int)mx < bar_clock_x + (int)bar_clock_w + 5;
  830. }
  831. static int bar_net_hit(unsigned int mx, unsigned int my)
  832. {
  833. return bar_net_w && bar_net_x >= 0 && my < WM_BAR_H &&
  834. (int)mx >= bar_net_x - 3 &&
  835. (int)mx < bar_net_x + (int)bar_net_w + 3;
  836. }
  837. static void do_update(struct wm_status *st)
  838. {
  839. struct wm_present_info present;
  840. int need = 0;
  841. int bar_dirty = 0;
  842. unsigned int i;
  843. char dt[20];
  844. static char last_dt[20];
  845. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  846. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  847. unsigned int msg_now = u_ticks_ms();
  848. /* Spawn the first terminal once the kernel says the storage scan is
  849. * done, so a shell command can never race the mount table. */
  850. if (st->boot_ready && !boot_term_spawned) {
  851. boot_term_spawned = 1;
  852. if (!free_window_slot()) {
  853. bar_notice("window limit");
  854. full_redraw = 1;
  855. } else if (u_wm_create_term() < 0) {
  856. bar_notice("cannot start term");
  857. full_redraw = 1;
  858. } else {
  859. full_redraw = 1;
  860. }
  861. }
  862. if (bar_msg_dirty ||
  863. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  864. bar_dirty = 1;
  865. bar_msg_dirty = 0;
  866. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  867. bar_msg_until = 0;
  868. }
  869. bar_kexec_busy = st->kexec_busy;
  870. bar_kexec_progress = st->kexec_progress;
  871. bar_kexec_total = st->kexec_total;
  872. if (kexec_changed)
  873. bar_dirty = 1;
  874. {
  875. int hot = bar_clock_hit(st->mouse.x, st->mouse.y);
  876. if (hot != bar_clock_hot) {
  877. bar_clock_hot = hot;
  878. bar_dirty = 1;
  879. }
  880. }
  881. {
  882. int hot = bar_net_hit(st->mouse.x, st->mouse.y);
  883. if (hot != bar_net_hot) {
  884. bar_net_hot = hot;
  885. bar_dirty = 1;
  886. }
  887. }
  888. /* Refresh cached window state and mappings before opening a frame:
  889. * MAP may unmap/remap content. */
  890. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  891. struct wm_window_info info;
  892. int r = u_wm_info(i, &info);
  893. if (r < 0) {
  894. u_exit(1);
  895. }
  896. if (r == 0 || !info.used) {
  897. if (wins[i].info.used)
  898. full_redraw = 1;
  899. wins[i].info.used = 0;
  900. wins[i].mapped = 0;
  901. continue;
  902. }
  903. if (!wins[i].info.used ||
  904. info.content_gen != wins[i].info.content_gen)
  905. full_redraw = 1;
  906. wins[i].info = info;
  907. if (!wins[i].mapped ||
  908. wins[i].map.generation != info.content_gen) {
  909. struct wm_map_info map;
  910. if (u_wm_map(i, &map) == 0) {
  911. wins[i].map = map;
  912. wins[i].mapped = 1;
  913. } else {
  914. wins[i].mapped = 0;
  915. }
  916. }
  917. }
  918. /* Anchor the network menu under its bar icon once it has created its
  919. * window. The WM owns the geometry syscall, so the app itself does
  920. * not need a special placement ABI. */
  921. if (netmenu_pid > 0) {
  922. int found = 0;
  923. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  924. struct wm_window_info ni;
  925. if (!wins[i].info.used ||
  926. wins[i].info.pid != (unsigned int)netmenu_pid)
  927. continue;
  928. found = 1;
  929. if (!netmenu_placed) {
  930. int nx = (bar_net_x >= 0 ?
  931. bar_net_x + (int)bar_net_w :
  932. (int)fbw) - (int)wins[i].info.w;
  933. int ny = WM_BAR_H + 2;
  934. if (nx < 4)
  935. nx = 4;
  936. if (nx + (int)wins[i].info.w > (int)fbw - 4)
  937. nx = (int)fbw - 4 -
  938. (int)wins[i].info.w;
  939. if (u_wm_set_geom(i, nx, ny) == 0 &&
  940. u_wm_info(i, &ni) == 1) {
  941. wins[i].info = ni;
  942. full_redraw = 1;
  943. }
  944. netmenu_placed = 1;
  945. }
  946. break;
  947. }
  948. if (!found && !u_alive((unsigned int)netmenu_pid)) {
  949. netmenu_pid = 0;
  950. netmenu_placed = 0;
  951. }
  952. }
  953. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  954. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  955. {
  956. static unsigned int last_rtc_ms;
  957. unsigned int now_ms = u_ticks_ms();
  958. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  959. last_rtc_ms = now_ms;
  960. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  961. for (i = 0; i < sizeof(dt); i++)
  962. if (dt[i] != last_dt[i]) {
  963. for (i = 0; i < sizeof(dt); i++)
  964. last_dt[i] = dt[i];
  965. bar_dirty = 1;
  966. break;
  967. }
  968. }
  969. {
  970. struct sys_cputemp ct;
  971. if (u_cpu_temp(&ct) == 0 &&
  972. ct.temp_c != bar_temp_c) {
  973. bar_temp_c = ct.temp_c;
  974. bar_dirty = 1;
  975. }
  976. }
  977. {
  978. struct sys_powerinfo pi;
  979. int pct = -1, charging = 0, ac = -1;
  980. unsigned int bi;
  981. if (u_power_info(&pi, 0) == 0) {
  982. /* A laptop EC (ThinkPad or Chrome
  983. * EC) marks a machine with AC/battery;
  984. * desktops show neither. */
  985. if (pi.present) {
  986. ac = pi.ac_online;
  987. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  988. bi++) {
  989. struct sys_battery *b =
  990. &pi.battery[bi];
  991. if (!b->present ||
  992. !b->percent_valid)
  993. continue;
  994. pct = (int)b->percent;
  995. charging = (int)b->charging;
  996. break;
  997. }
  998. }
  999. if (pct != bar_batt_pct ||
  1000. charging != bar_batt_charging ||
  1001. ac != bar_ac_online) {
  1002. bar_batt_pct = pct;
  1003. bar_batt_charging = charging;
  1004. bar_ac_online = ac;
  1005. bar_dirty = 1;
  1006. }
  1007. }
  1008. }
  1009. if (poll_net_status())
  1010. bar_dirty = 1;
  1011. }
  1012. }
  1013. if (full_redraw) {
  1014. if (u_wm_begin() != 0) {
  1015. full_redraw = 1;
  1016. return;
  1017. }
  1018. full_redraw = 0;
  1019. need = 1;
  1020. {
  1021. draw_rect(0, 0, fbw, fbh);
  1022. draw_bar();
  1023. present.count = 0;
  1024. add_rect(&present, 0, 0, fbw, fbh);
  1025. present.immediate = 0;
  1026. u_wm_present(&present);
  1027. /* Ack every window that was clean at the time of the
  1028. * redraw. If new damage arrived during it, its seq
  1029. * differs and the kernel leaves it dirty. */
  1030. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1031. if (wins[i].info.used && wins[i].mapped)
  1032. u_wm_ack(i, wins[i].info.dirty_seq);
  1033. }
  1034. return;
  1035. }
  1036. /* Partial cell/canvas updates and the bar clock. */
  1037. present.count = 0;
  1038. present.immediate = 0;
  1039. if (bar_dirty) {
  1040. need = 1;
  1041. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1042. }
  1043. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1044. struct uwin *u = &wins[i];
  1045. struct wm_window_info *w = &u->info;
  1046. int rx, ry;
  1047. unsigned int rw, rh;
  1048. if (!w->used || !w->dirty)
  1049. continue;
  1050. if (!w->dmg_w || !w->dmg_h)
  1051. continue;
  1052. rx = w->x + 1 + (int)w->dmg_x;
  1053. ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_y;
  1054. rw = w->dmg_w;
  1055. rh = w->dmg_h;
  1056. add_rect(&present, rx, ry, rw, rh);
  1057. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1058. need = 1;
  1059. }
  1060. if (!need) {
  1061. /* Drag movement is handled below; a stationary mouse can be
  1062. * left to the kernel's cursor overlay. */
  1063. return;
  1064. }
  1065. if (u_wm_begin() != 0)
  1066. return;
  1067. for (i = 0; i < present.count; i++)
  1068. draw_rect(present.rect[i].x, present.rect[i].y,
  1069. present.rect[i].w, present.rect[i].h);
  1070. if (bar_dirty)
  1071. draw_bar();
  1072. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1073. struct uwin *u = &wins[i];
  1074. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1075. !u->ack_seq)
  1076. continue;
  1077. if (u->ack_seq == u->info.dirty_seq)
  1078. u_wm_ack(i, u->info.dirty_seq);
  1079. }
  1080. u_wm_present(&present);
  1081. }
  1082. static int spawn_app(const char *path)
  1083. {
  1084. int pid = u_spawn(path, "", 0, "/");
  1085. if (pid > 0)
  1086. full_redraw = 1;
  1087. return pid;
  1088. }
  1089. static void spawn_app_checked(const char *path)
  1090. {
  1091. if (!free_window_slot()) {
  1092. bar_notice("window limit");
  1093. full_redraw = 1;
  1094. return;
  1095. }
  1096. if (spawn_app(path) < 0) {
  1097. bar_notice("spawn failed");
  1098. full_redraw = 1;
  1099. }
  1100. }
  1101. /* The network icon opens one menu at a time; a second click focuses it. */
  1102. static void open_netmenu(void)
  1103. {
  1104. unsigned int i;
  1105. if (netmenu_pid > 0 && u_alive((unsigned int)netmenu_pid)) {
  1106. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1107. if (wins[i].info.used &&
  1108. wins[i].info.pid == (unsigned int)netmenu_pid) {
  1109. u_wm_focus(i);
  1110. full_redraw = 1;
  1111. return;
  1112. }
  1113. }
  1114. if (!free_window_slot()) {
  1115. bar_notice("window limit");
  1116. full_redraw = 1;
  1117. return;
  1118. }
  1119. netmenu_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  1120. netmenu_placed = 0;
  1121. if (netmenu_pid < 0) {
  1122. netmenu_pid = 0;
  1123. bar_notice("spawn failed");
  1124. full_redraw = 1;
  1125. } else {
  1126. full_redraw = 1;
  1127. }
  1128. }
  1129. static void handle_mouse(struct wm_status *st)
  1130. {
  1131. unsigned int buttons = st->mouse.buttons;
  1132. unsigned int mx = st->mouse.x, my = st->mouse.y;
  1133. int down = (buttons & 1) != 0;
  1134. int was = (prev_buttons & 1) != 0;
  1135. if (down && !was) {
  1136. int prev_title_id = last_title_id;
  1137. unsigned int now = u_ticks_ms();
  1138. last_title_id = -1;
  1139. if (bar_net_hit(mx, my)) {
  1140. open_netmenu();
  1141. } else if (bar_clock_hit(mx, my)) {
  1142. spawn_app_checked("/ramfs/bin/settime");
  1143. } else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  1144. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  1145. if (!free_window_slot()) {
  1146. bar_notice("window limit");
  1147. full_redraw = 1;
  1148. } else if (u_wm_create_term() < 0) {
  1149. bar_notice("cannot start term");
  1150. full_redraw = 1;
  1151. } else {
  1152. full_redraw = 1;
  1153. }
  1154. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  1155. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  1156. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  1157. spawn_app_checked("/ramfs/bin/clock");
  1158. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  1159. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  1160. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  1161. spawn_app_checked("/ramfs/bin/colors");
  1162. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  1163. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  1164. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  1165. spawn_app_checked("/ramfs/bin/files");
  1166. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  1167. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  1168. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  1169. spawn_app_checked("/ramfs/bin/mines");
  1170. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  1171. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  1172. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  1173. spawn_app_checked("/ramfs/bin/browser");
  1174. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  1175. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  1176. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  1177. spawn_app_checked("/ramfs/bin/solitaire");
  1178. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  1179. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  1180. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  1181. spawn_app_checked("/ramfs/bin/gallery");
  1182. } else if (mx >= WM_BTN_CHAT_X && my >= WM_BTN_CHAT_Y &&
  1183. mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
  1184. my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
  1185. spawn_app_checked("/ramfs/bin/chat");
  1186. } else {
  1187. int idx = window_at((int)mx, (int)my);
  1188. if (idx >= 0) {
  1189. struct uwin *u = &wins[idx];
  1190. int pmx = (int)mx, pmy = (int)my;
  1191. int rx = u->info.x + (int)u->info.w - 1 -
  1192. WM_CLOSE_W;
  1193. int ry = u->info.y + 1;
  1194. u_wm_focus((unsigned int)idx);
  1195. if (pmx >= rx &&
  1196. pmx < u->info.x + (int)u->info.w &&
  1197. pmy >= ry && pmy < ry + WM_TITLE_H) {
  1198. u_wm_close((unsigned int)idx);
  1199. full_redraw = 1;
  1200. } else if (pmx >= u->info.x + (int)u->info.w - 16 &&
  1201. pmy >= u->info.y + (int)u->info.h - 16) {
  1202. resize_id = idx;
  1203. resize_mx = mx;
  1204. resize_my = my;
  1205. resize_ow = u->info.w;
  1206. resize_oh = u->info.h;
  1207. } else if (pmy < u->info.y + WM_TITLE_H) {
  1208. if (prev_title_id == idx &&
  1209. now - last_title_ms <= WM_DBLCLICK_MS &&
  1210. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  1211. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  1212. last_title_ms = 0;
  1213. if (toggle_maximize(u,
  1214. (unsigned int)idx) != 0)
  1215. bar_notice("resize failed");
  1216. } else {
  1217. last_title_id = idx;
  1218. last_title_ms = now;
  1219. last_title_x = pmx;
  1220. last_title_y = pmy;
  1221. drag_id = idx;
  1222. drag_mx = pmx;
  1223. drag_my = pmy;
  1224. drag_dx = pmx - u->info.x;
  1225. drag_dy = pmy - u->info.y;
  1226. }
  1227. }
  1228. }
  1229. }
  1230. } else if (down && was && resize_id >= 0) {
  1231. struct uwin *u = &wins[resize_id];
  1232. if (u->info.used) {
  1233. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  1234. int nh = (int)resize_oh + (int)my - (int)resize_my;
  1235. int maxw = (int)fbw;
  1236. int maxh = (int)fbh - WM_BAR_H;
  1237. if (nw < 200) nw = 200;
  1238. if (nh < 140) nh = 140;
  1239. if (nw > maxw) nw = maxw;
  1240. if (nh > maxh) nh = maxh;
  1241. /* Dragging the grip into the screen corner means "as
  1242. * large as the work area", even though the grip starts
  1243. * 16 px inside the window edge. */
  1244. if ((int)mx >= (int)fbw - 1)
  1245. nw = maxw;
  1246. if ((int)my >= (int)fbh - 1)
  1247. nh = maxh;
  1248. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  1249. if (set_window_geometry(u,
  1250. (unsigned int)resize_id, u->info.x,
  1251. u->info.y, (unsigned int)nw,
  1252. (unsigned int)nh) == 0)
  1253. u->maximized = 0;
  1254. }
  1255. } else {
  1256. resize_id = -1;
  1257. }
  1258. } else if (down && was && drag_id >= 0) {
  1259. struct uwin *u = &wins[drag_id];
  1260. if (u->info.used) {
  1261. int ox, oy, nx, ny;
  1262. int started = !u->maximized ||
  1263. iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  1264. iabs((int)my - drag_my) >= WM_DRAG_START;
  1265. /* A maximized window is restored as soon as the drag
  1266. * starts; keep the pointer's grab point relative to the
  1267. * restored geometry. A click that does not move must
  1268. * leave it maximized. */
  1269. if (u->maximized && started &&
  1270. restore_window(u, (unsigned int)drag_id) == 0) {
  1271. drag_dx = drag_mx - u->info.x;
  1272. drag_dy = drag_my - u->info.y;
  1273. }
  1274. if (!u->maximized) {
  1275. ox = u->info.x;
  1276. oy = u->info.y;
  1277. nx = (int)mx - drag_dx;
  1278. ny = (int)my - drag_dy;
  1279. clamp_geom(u, &nx, &ny);
  1280. if (nx != ox || ny != oy) {
  1281. struct wm_present_info present;
  1282. unsigned int i;
  1283. u_wm_set_geom((unsigned int)drag_id,
  1284. nx, ny);
  1285. u->info.x = nx;
  1286. u->info.y = ny;
  1287. if (u->info.focused) {
  1288. move_window_pixels(u, ox, oy,
  1289. nx, ny);
  1290. } else {
  1291. present.count = 0;
  1292. present.immediate = 1;
  1293. add_rect(&present, ox, oy,
  1294. u->info.w, u->info.h);
  1295. add_rect(&present, nx, ny,
  1296. u->info.w, u->info.h);
  1297. if (u_wm_begin() == 0) {
  1298. for (i = 0;
  1299. i < present.count; i++)
  1300. draw_rect(
  1301. present.rect[i].x,
  1302. present.rect[i].y,
  1303. present.rect[i].w,
  1304. present.rect[i].h);
  1305. u_wm_present(&present);
  1306. }
  1307. }
  1308. }
  1309. }
  1310. } else {
  1311. drag_id = -1;
  1312. }
  1313. } else if (!down && was) {
  1314. drag_id = -1;
  1315. resize_id = -1;
  1316. }
  1317. prev_buttons = buttons;
  1318. }
  1319. static char *put_str(char *p, const char *s)
  1320. {
  1321. while (*s)
  1322. *p++ = *s++;
  1323. return p;
  1324. }
  1325. static char *put_dec(char *p, unsigned int v)
  1326. {
  1327. char b[10];
  1328. unsigned int n = 0;
  1329. do {
  1330. b[n++] = (char)('0' + v % 10);
  1331. v /= 10;
  1332. } while (v);
  1333. while (n)
  1334. *p++ = b[--n];
  1335. return p;
  1336. }
  1337. /* Expose the last interval in /share so the target can be measured without
  1338. * a serial console. */
  1339. static void write_stats(void)
  1340. {
  1341. char b[80], *p = b;
  1342. unsigned int now = u_ticks_ms();
  1343. p = put_str(p, "t=");
  1344. p = put_dec(p, now / 1000);
  1345. p = put_str(p, "s frames=");
  1346. p = put_dec(p, stat_frames);
  1347. p = put_str(p, " ms=");
  1348. p = put_dec(p, stat_ms);
  1349. p = put_str(p, " max=");
  1350. p = put_dec(p, stat_max);
  1351. p = put_str(p, " px=");
  1352. p = put_dec(p, stat_px);
  1353. p = put_str(p, "\n");
  1354. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  1355. (int)(p - b));
  1356. }
  1357. void _start(const char *arg)
  1358. {
  1359. struct wm_screen_info si;
  1360. struct wm_status st;
  1361. unsigned int i;
  1362. (void)arg;
  1363. if (u_wm_attach(&si) != 0)
  1364. u_exit(1);
  1365. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  1366. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  1367. fbw = si.width;
  1368. fbh = si.height;
  1369. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1370. wins[i].info.used = 0;
  1371. wins[i].mapped = 0;
  1372. }
  1373. full_redraw = 1;
  1374. for (;;) {
  1375. unsigned int t0, t1;
  1376. if (u_wm_poll(&st) != 0)
  1377. u_exit(1);
  1378. if (st.full_redraw)
  1379. full_redraw = 1;
  1380. usurf_pixels = 0;
  1381. t0 = u_ticks_ms();
  1382. handle_mouse(&st);
  1383. do_update(&st);
  1384. t1 = u_ticks_ms();
  1385. if (usurf_pixels) {
  1386. unsigned int dt = t1 - t0;
  1387. stat_frames++;
  1388. stat_ms += dt;
  1389. stat_px += usurf_pixels;
  1390. if (dt > stat_max)
  1391. stat_max = dt;
  1392. }
  1393. if (t1 - stat_last >= 2000) {
  1394. stat_last = t1;
  1395. if (stat_frames) {
  1396. write_stats();
  1397. stat_frames = stat_ms = stat_px = stat_max = 0;
  1398. }
  1399. }
  1400. u_sleep_ms(10);
  1401. }
  1402. }