ucmd_wm.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. };
  21. static struct usurf screen;
  22. static struct uwin wins[WM_MAX_WINDOWS];
  23. static unsigned int fbw, fbh;
  24. static int drag_id = -1;
  25. static int drag_dx, drag_dy;
  26. static int resize_id = -1;
  27. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  28. static unsigned int prev_buttons;
  29. static int full_redraw = 1;
  30. static int boot_term_spawned;
  31. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  32. static int bar_temp_c = -1;
  33. static int bar_batt_pct = -1;
  34. static int bar_batt_charging;
  35. static int bar_ac_online = -1;
  36. static char bar_msg[24];
  37. static unsigned int bar_msg_until;
  38. static int bar_msg_dirty;
  39. /* Rectangle of the right-aligned date/time text, so a click there can open
  40. * the Date & Time app. w == 0 means "not drawn" (e.g. kexec banner). */
  41. static int bar_clock_x = -1;
  42. static unsigned int bar_clock_w;
  43. static int bar_clock_hot;
  44. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  45. static void bar_notice(const char *msg)
  46. {
  47. unsigned int i;
  48. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  49. bar_msg[i] = msg[i];
  50. bar_msg[i] = '\0';
  51. bar_msg_until = u_ticks_ms() + 2000;
  52. bar_msg_dirty = 1;
  53. }
  54. static int free_window_slot(void)
  55. {
  56. unsigned int i;
  57. for (i = 0; i < WM_MAX_WINDOWS; i++)
  58. if (!wins[i].info.used)
  59. return 1;
  60. return 0;
  61. }
  62. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  63. {
  64. return (r << 16) | (g << 8) | b;
  65. }
  66. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  67. * integer interpolation the kernel bar uses. */
  68. static unsigned int rainbow_rgb(unsigned int hue)
  69. {
  70. unsigned int seg = hue / 256, f = hue % 256;
  71. unsigned int r1, g1, b1, r2, g2, b2;
  72. switch (seg) {
  73. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  74. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  75. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  76. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  77. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  78. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  79. }
  80. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  81. (g1 * (256 - f) + g2 * f) / 256,
  82. (b1 * (256 - f) + b2 * f) / 256);
  83. }
  84. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  85. unsigned int rgb, int cx, int cy,
  86. unsigned int cw, unsigned int ch)
  87. {
  88. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  89. if (x0 < cx) x0 = cx;
  90. if (y0 < cy) y0 = cy;
  91. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  92. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  93. if (x1 > x0 && y1 > y0)
  94. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  95. (unsigned int)(y1 - y0), rgb);
  96. }
  97. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  98. unsigned int h, const char *label)
  99. {
  100. int tw = (int)usurf_text_width(&screen, label);
  101. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  102. usurf_text(&screen, (int)x + ((int)w - tw) / 2, (int)(y + 3),
  103. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  104. }
  105. static unsigned int temp_color(int c)
  106. {
  107. if (c >= 80)
  108. return 0xFF5050u;
  109. if (c >= 60)
  110. return 0xFFD250u;
  111. return 0x55FF55u;
  112. }
  113. /* "CPU 43C" (no libc printf here). */
  114. static unsigned int temp_text(char *b, int c)
  115. {
  116. unsigned int n = 0, v = (unsigned int)c;
  117. b[n++] = 'C';
  118. b[n++] = 'P';
  119. b[n++] = 'U';
  120. b[n++] = ' ';
  121. if (v >= 100)
  122. b[n++] = (char)('0' + (v / 100) % 10);
  123. if (v >= 10)
  124. b[n++] = (char)('0' + (v / 10) % 10);
  125. b[n++] = (char)('0' + v % 10);
  126. b[n++] = 'C';
  127. b[n] = '\0';
  128. return n;
  129. }
  130. /* "BAT 87%" (no libc printf here). */
  131. static unsigned int batt_text(char *b, int pct)
  132. {
  133. unsigned int n = 0, v = (unsigned int)pct;
  134. b[n++] = 'B';
  135. b[n++] = 'A';
  136. b[n++] = 'T';
  137. b[n++] = ' ';
  138. if (v >= 100)
  139. b[n++] = (char)('0' + (v / 100) % 10);
  140. if (v >= 10)
  141. b[n++] = (char)('0' + (v / 10) % 10);
  142. b[n++] = (char)('0' + v % 10);
  143. b[n++] = '%';
  144. b[n] = '\0';
  145. return n;
  146. }
  147. static unsigned int batt_color(int pct, int charging, int ac)
  148. {
  149. if (pct <= 10)
  150. return 0xFF5050u;
  151. if (pct <= 20)
  152. return 0xFFD250u;
  153. if (charging || ac > 0)
  154. return 0x55FF55u;
  155. return 0x55D2FFu;
  156. }
  157. /* "AC on" / "AC off" (no libc printf here). Only drawn on machines whose
  158. * ThinkPad EC reported the adapter state. */
  159. static unsigned int ac_text(char *b, int online)
  160. {
  161. unsigned int n = 0;
  162. b[n++] = 'A';
  163. b[n++] = 'C';
  164. b[n++] = ' ';
  165. b[n++] = 'o';
  166. if (online) {
  167. b[n++] = 'n';
  168. } else {
  169. b[n++] = 'f';
  170. b[n++] = 'f';
  171. }
  172. b[n] = '\0';
  173. return n;
  174. }
  175. static unsigned int ac_color(int online)
  176. {
  177. return online ? 0x55FF55u : 0xFFD250u;
  178. }
  179. static void draw_bar(void)
  180. {
  181. char dt[48];
  182. unsigned int n, i, x, y;
  183. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  184. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  185. 0, 0, fbw, WM_BAR_H);
  186. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  187. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  188. WM_BTN_CLOCK_H, "Clock");
  189. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  190. WM_BTN_COLORS_H, "Colors");
  191. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  192. WM_BTN_FILES_H, "Files");
  193. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  194. WM_BTN_MINES_H, "Mines");
  195. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  196. WM_BTN_WEB_H, "Web");
  197. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  198. WM_BTN_CARDS_H, "Cards");
  199. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  200. WM_BTN_GALLERY_H, "Gallery");
  201. if (bar_msg_until &&
  202. (int)(u_ticks_ms() - bar_msg_until) < 0)
  203. usurf_text(&screen, (int)(WM_BTN_GALLERY_X + WM_BTN_GALLERY_W + 8),
  204. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  205. /* Rainbow separator (same y as the kernel's old bar). */
  206. for (x = 0; x < fbw; x++) {
  207. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  208. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  209. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  210. }
  211. if (bar_kexec_busy) {
  212. static const char msg[] = "loading new kernel over serial";
  213. unsigned int dots = bar_kexec_total ?
  214. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  215. bar_clock_x = -1;
  216. bar_clock_w = 0;
  217. bar_clock_hot = 0;
  218. n = 0;
  219. while (msg[n])
  220. dt[n] = msg[n], n++;
  221. for (i = 0; i < 12; i++)
  222. dt[n + i] = (i < dots) ? '.' : ' ';
  223. n += 12;
  224. dt[n] = '\0';
  225. usurf_text(&screen,
  226. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  227. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  228. } else {
  229. unsigned int clock_chars = 0;
  230. int right = (int)fbw - 4;
  231. int min_x = WM_BTN_GALLERY_X + (int)WM_BTN_GALLERY_W + 4;
  232. bar_clock_x = -1;
  233. bar_clock_w = 0;
  234. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  235. int tw = (int)usurf_text_width(&screen, dt);
  236. int tx = right - tw;
  237. n = 0;
  238. while (dt[n] && n < sizeof(dt) - 1)
  239. n++;
  240. bar_clock_x = tx;
  241. bar_clock_w = (unsigned int)tw;
  242. if (bar_clock_hot)
  243. usurf_fill(&screen, tx - 5, 2,
  244. (unsigned int)tw + 10,
  245. WM_BAR_H - 4, 0x3A3A55u);
  246. usurf_text(&screen, tx, 5, dt, COL_BANNER, 0, 0,
  247. fbw, WM_BAR_H);
  248. clock_chars = (unsigned int)tw;
  249. }
  250. if (clock_chars)
  251. right -= (int)clock_chars + 8;
  252. if (bar_temp_c >= 0) {
  253. char tb[12];
  254. int tx;
  255. (void)temp_text(tb, bar_temp_c);
  256. tx = right - (int)usurf_text_width(&screen, tb);
  257. if (tx >= min_x) {
  258. usurf_text(&screen, tx, 5, tb,
  259. temp_color(bar_temp_c), 0, 0,
  260. fbw, WM_BAR_H);
  261. right = tx - 8;
  262. }
  263. }
  264. if (bar_batt_pct >= 0) {
  265. char bb[10];
  266. int bx;
  267. (void)batt_text(bb, bar_batt_pct);
  268. bx = right - (int)usurf_text_width(&screen, bb);
  269. if (bx >= min_x) {
  270. usurf_text(&screen, bx, 5, bb,
  271. batt_color(bar_batt_pct,
  272. bar_batt_charging,
  273. bar_ac_online),
  274. 0, 0, fbw, WM_BAR_H);
  275. right = bx - 8;
  276. }
  277. }
  278. if (bar_ac_online >= 0) {
  279. char ab[8];
  280. int ax;
  281. (void)ac_text(ab, bar_ac_online);
  282. ax = right - (int)usurf_text_width(&screen, ab);
  283. if (ax >= min_x)
  284. usurf_text(&screen, ax, 5, ab,
  285. ac_color(bar_ac_online), 0, 0,
  286. fbw, WM_BAR_H);
  287. }
  288. }
  289. }
  290. static void draw_window_clip(unsigned int idx, int cx, int cy,
  291. unsigned int cw, unsigned int ch)
  292. {
  293. struct uwin *u = &wins[idx];
  294. struct wm_window_info *w = &u->info;
  295. int x = w->x, y = w->y;
  296. unsigned int pw = w->w, ph = w->h;
  297. unsigned int bx = (unsigned int)(x + (int)pw - 1 - WM_CLOSE_W);
  298. unsigned int cony = (unsigned int)(y + 1 + WM_TITLE_H);
  299. unsigned int content_x = (unsigned int)(x + 1);
  300. unsigned int content_w = w->content_w;
  301. unsigned int content_h = w->content_h;
  302. /* Border, title bar, close button. */
  303. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  304. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  305. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  306. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  307. cx, cy, cw, ch);
  308. fill_clip((int)bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  309. cx, cy, cw, ch);
  310. usurf_text(&screen, (int)bx + 4, y + 3, "x", COL_WHITE,
  311. cx, cy, cw, ch);
  312. /* Content: every window presents an RGB565 canvas (the terminal
  313. * renders its cells into one; gfx commands own theirs). A mapped
  314. * canvas covers the whole content area, so it does not need the black
  315. * underlay. */
  316. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  317. struct uwm_canvas c;
  318. c.px = (const unsigned short *)u->map.va;
  319. c.w = u->map.w;
  320. c.h = u->map.h;
  321. if (c.w != content_w || c.h != content_h)
  322. fill_clip((int)content_x, (int)cony, content_w, content_h,
  323. 0x000000u, cx, cy, cw, ch);
  324. usurf_canvas(&screen, &c, (int)content_x, (int)cony,
  325. cx, cy, cw, ch);
  326. } else {
  327. fill_clip((int)content_x, (int)cony, content_w, content_h,
  328. 0x000000u, cx, cy, cw, ch);
  329. }
  330. /* Resize grip (bottom-right corner). */
  331. if (w->focused) {
  332. unsigned int i;
  333. for (i = 0; i < 10; i++) {
  334. fill_clip(x + (int)pw - 3 - (int)i,
  335. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  336. cx, cy, cw, ch);
  337. fill_clip(x + (int)pw - 4,
  338. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  339. cx, cy, cw, ch);
  340. }
  341. }
  342. }
  343. /* Front-to-back window order: the focused window is on top, the others
  344. * follow in descending slot order. Painting and hit-testing share it. */
  345. static unsigned int build_order(int *order)
  346. {
  347. unsigned int i, n = 0;
  348. for (i = 0; i < WM_MAX_WINDOWS; i++)
  349. if (wins[i].info.used && wins[i].info.focused)
  350. order[n++] = (int)i;
  351. for (i = WM_MAX_WINDOWS; i-- > 0;)
  352. if (wins[i].info.used && !wins[i].info.focused)
  353. order[n++] = (int)i;
  354. return n;
  355. }
  356. struct orect {
  357. int x, y;
  358. unsigned int w, h;
  359. };
  360. #define OCC_MAX 64
  361. /* r minus cut, split into up to four disjoint rectangles. */
  362. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  363. const struct orect *cut)
  364. {
  365. int rx0 = r->x, ry0 = r->y;
  366. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  367. int cx0 = cut->x, cy0 = cut->y;
  368. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  369. unsigned int n = 0;
  370. if (cx0 < rx0) cx0 = rx0;
  371. if (cy0 < ry0) cy0 = ry0;
  372. if (cx1 > rx1) cx1 = rx1;
  373. if (cy1 > ry1) cy1 = ry1;
  374. if (cx1 <= cx0 || cy1 <= cy0) {
  375. out[0] = *r;
  376. return 1;
  377. }
  378. if (cy0 > ry0) {
  379. out[n].x = rx0;
  380. out[n].y = ry0;
  381. out[n].w = (unsigned int)(rx1 - rx0);
  382. out[n].h = (unsigned int)(cy0 - ry0);
  383. n++;
  384. }
  385. if (cy1 < ry1) {
  386. out[n].x = rx0;
  387. out[n].y = cy1;
  388. out[n].w = (unsigned int)(rx1 - rx0);
  389. out[n].h = (unsigned int)(ry1 - cy1);
  390. n++;
  391. }
  392. if (cx0 > rx0) {
  393. out[n].x = rx0;
  394. out[n].y = cy0;
  395. out[n].w = (unsigned int)(cx0 - rx0);
  396. out[n].h = (unsigned int)(cy1 - cy0);
  397. n++;
  398. }
  399. if (cx1 < rx1) {
  400. out[n].x = cx1;
  401. out[n].y = cy0;
  402. out[n].w = (unsigned int)(rx1 - cx1);
  403. out[n].h = (unsigned int)(cy1 - cy0);
  404. n++;
  405. }
  406. return n;
  407. }
  408. /* Back-to-front repaint, used only if the visible-region list overflows. */
  409. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  410. {
  411. unsigned int i;
  412. usurf_fill(&screen, x, y, w, h, COL_BG);
  413. for (i = 0; i < WM_MAX_WINDOWS; i++)
  414. if (wins[i].info.used && !wins[i].info.focused &&
  415. x < wins[i].info.x + (int)wins[i].info.w &&
  416. x + (int)w > wins[i].info.x &&
  417. y < wins[i].info.y + (int)wins[i].info.h &&
  418. y + (int)h > wins[i].info.y)
  419. draw_window_clip(i, x, y, w, h);
  420. for (i = 0; i < WM_MAX_WINDOWS; i++)
  421. if (wins[i].info.used && wins[i].info.focused &&
  422. x < wins[i].info.x + (int)wins[i].info.w &&
  423. x + (int)w > wins[i].info.x &&
  424. y < wins[i].info.y + (int)wins[i].info.h &&
  425. y + (int)h > wins[i].info.y)
  426. draw_window_clip(i, x, y, w, h);
  427. }
  428. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  429. {
  430. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  431. struct orect dmg;
  432. int order[WM_MAX_WINDOWS];
  433. unsigned int n, i, j, ncur, nnext;
  434. int overflow = 0;
  435. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  436. if (w == 0 || h == 0)
  437. return;
  438. if (x0 < 0) x0 = 0;
  439. if (y0 < 0) y0 = 0;
  440. if (x1 > (int)fbw) x1 = (int)fbw;
  441. if (y1 > (int)fbh) y1 = (int)fbh;
  442. if (x1 <= x0 || y1 <= y0)
  443. return;
  444. dmg.x = x0;
  445. dmg.y = y0;
  446. dmg.w = (unsigned int)(x1 - x0);
  447. dmg.h = (unsigned int)(y1 - y0);
  448. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  449. n = build_order(order);
  450. for (i = 0; i < n; i++) {
  451. struct uwin *u = &wins[order[i]];
  452. int wx0 = u->info.x, wy0 = u->info.y;
  453. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  454. if (wx0 < dmg.x) wx0 = dmg.x;
  455. if (wy0 < dmg.y) wy0 = dmg.y;
  456. if (wx1 > x1) wx1 = x1;
  457. if (wy1 > y1) wy1 = y1;
  458. if (wx1 <= wx0 || wy1 <= wy0)
  459. continue;
  460. cur[0].x = wx0;
  461. cur[0].y = wy0;
  462. cur[0].w = (unsigned int)(wx1 - wx0);
  463. cur[0].h = (unsigned int)(wy1 - wy0);
  464. ncur = 1;
  465. for (j = 0; j < i && ncur; j++) {
  466. struct uwin *f = &wins[order[j]];
  467. struct orect fr;
  468. unsigned int k;
  469. fr.x = f->info.x;
  470. fr.y = f->info.y;
  471. fr.w = f->info.w;
  472. fr.h = f->info.h;
  473. nnext = 0;
  474. for (k = 0; k < ncur; k++) {
  475. if (nnext + 4 > OCC_MAX) {
  476. overflow = 1;
  477. break;
  478. }
  479. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  480. }
  481. if (overflow)
  482. break;
  483. for (k = 0; k < nnext; k++)
  484. cur[k] = nxt[k];
  485. ncur = nnext;
  486. }
  487. if (overflow)
  488. break;
  489. for (j = 0; j < ncur; j++)
  490. draw_window_clip((unsigned int)order[i], cur[j].x,
  491. cur[j].y, cur[j].w, cur[j].h);
  492. }
  493. if (overflow)
  494. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  495. }
  496. static int window_at(unsigned int mx, unsigned int my)
  497. {
  498. int order[WM_MAX_WINDOWS];
  499. unsigned int n = build_order(order), i;
  500. for (i = 0; i < n; i++) {
  501. struct wm_window_info *w = &wins[order[i]].info;
  502. if (mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  503. mx < (unsigned int)(w->x + (int)w->w) &&
  504. my < (unsigned int)(w->y + (int)w->h))
  505. return order[i];
  506. }
  507. return -1;
  508. }
  509. static void clamp_geom(struct uwin *u, int *x, int *y)
  510. {
  511. int maxx = (int)fbw - (int)u->info.w;
  512. int maxy = (int)fbh - (int)u->info.h;
  513. if (*x < 0) *x = 0;
  514. if (*y < WM_BAR_H) *y = WM_BAR_H;
  515. if (maxx < 0) maxx = 0;
  516. if (maxy < WM_BAR_H) maxy = WM_BAR_H;
  517. if (*x > maxx) *x = maxx;
  518. if (*y > maxy) *y = maxy;
  519. }
  520. static void add_rect(struct wm_present_info *p, int x, int y,
  521. unsigned int w, unsigned int h)
  522. {
  523. if (w == 0 || h == 0)
  524. return;
  525. if (p->count < 4) {
  526. p->rect[p->count].x = x;
  527. p->rect[p->count].y = y;
  528. p->rect[p->count].w = w;
  529. p->rect[p->count].h = h;
  530. p->count++;
  531. } else {
  532. /* Four is enough for normal updates; fold the rest into the
  533. * last rect rather than dropping pixels. */
  534. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  535. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  536. int nx1 = x + (int)w, ny1 = y + (int)h;
  537. if (x < x0) x0 = x;
  538. if (y < y0) y0 = y;
  539. if (nx1 > x1) x1 = nx1;
  540. if (ny1 > y1) y1 = ny1;
  541. p->rect[3].x = x0;
  542. p->rect[3].y = y0;
  543. p->rect[3].w = (unsigned int)(x1 - x0);
  544. p->rect[3].h = (unsigned int)(y1 - y0);
  545. }
  546. }
  547. /* A focused window is topmost, so the shadow contains exactly its pixels in
  548. * the old rectangle. Move that block and repaint only the old part the new
  549. * rectangle does not cover. */
  550. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  551. {
  552. struct wm_present_info present;
  553. struct orect oldr, newr, pieces[4];
  554. unsigned int np, k;
  555. if (u_wm_begin() != 0)
  556. return;
  557. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  558. oldr.x = ox;
  559. oldr.y = oy;
  560. oldr.w = u->info.w;
  561. oldr.h = u->info.h;
  562. newr.x = nx;
  563. newr.y = ny;
  564. newr.w = u->info.w;
  565. newr.h = u->info.h;
  566. np = rect_sub(pieces, &oldr, &newr);
  567. for (k = 0; k < np; k++)
  568. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  569. present.count = 0;
  570. present.immediate = 1;
  571. add_rect(&present, nx, ny, u->info.w, u->info.h);
  572. for (k = 0; k < np; k++)
  573. add_rect(&present, pieces[k].x, pieces[k].y,
  574. pieces[k].w, pieces[k].h);
  575. u_wm_present(&present);
  576. }
  577. /* The top-bar date/time text is also a button: it opens the Date & Time
  578. * editor. The rectangle is recorded by draw_bar(). */
  579. static int bar_clock_hit(unsigned int mx, unsigned int my)
  580. {
  581. return bar_clock_w && bar_clock_x >= 0 && my < WM_BAR_H &&
  582. (int)mx >= bar_clock_x - 5 &&
  583. (int)mx < bar_clock_x + (int)bar_clock_w + 5;
  584. }
  585. static void do_update(struct wm_status *st)
  586. {
  587. struct wm_present_info present;
  588. int need = 0;
  589. int bar_dirty = 0;
  590. unsigned int i;
  591. char dt[20];
  592. static char last_dt[20];
  593. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  594. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  595. unsigned int msg_now = u_ticks_ms();
  596. /* Spawn the first terminal once the kernel says the storage scan is
  597. * done, so a shell command can never race the mount table. */
  598. if (st->boot_ready && !boot_term_spawned) {
  599. boot_term_spawned = 1;
  600. if (!free_window_slot()) {
  601. bar_notice("window limit");
  602. full_redraw = 1;
  603. } else if (u_wm_create_term() < 0) {
  604. bar_notice("cannot start term");
  605. full_redraw = 1;
  606. } else {
  607. full_redraw = 1;
  608. }
  609. }
  610. if (bar_msg_dirty ||
  611. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  612. bar_dirty = 1;
  613. bar_msg_dirty = 0;
  614. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  615. bar_msg_until = 0;
  616. }
  617. bar_kexec_busy = st->kexec_busy;
  618. bar_kexec_progress = st->kexec_progress;
  619. bar_kexec_total = st->kexec_total;
  620. if (kexec_changed)
  621. bar_dirty = 1;
  622. {
  623. int hot = bar_clock_hit(st->mouse.x, st->mouse.y);
  624. if (hot != bar_clock_hot) {
  625. bar_clock_hot = hot;
  626. bar_dirty = 1;
  627. }
  628. }
  629. /* Refresh cached window state and mappings before opening a frame:
  630. * MAP may unmap/remap content. */
  631. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  632. struct wm_window_info info;
  633. int r = u_wm_info(i, &info);
  634. if (r < 0) {
  635. u_exit(1);
  636. }
  637. if (r == 0 || !info.used) {
  638. if (wins[i].info.used)
  639. full_redraw = 1;
  640. wins[i].info.used = 0;
  641. wins[i].mapped = 0;
  642. continue;
  643. }
  644. if (!wins[i].info.used ||
  645. info.content_gen != wins[i].info.content_gen)
  646. full_redraw = 1;
  647. wins[i].info = info;
  648. if (!wins[i].mapped ||
  649. wins[i].map.generation != info.content_gen) {
  650. struct wm_map_info map;
  651. if (u_wm_map(i, &map) == 0) {
  652. wins[i].map = map;
  653. wins[i].mapped = 1;
  654. } else {
  655. wins[i].mapped = 0;
  656. }
  657. }
  658. }
  659. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  660. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  661. {
  662. static unsigned int last_rtc_ms;
  663. unsigned int now_ms = u_ticks_ms();
  664. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  665. last_rtc_ms = now_ms;
  666. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  667. for (i = 0; i < sizeof(dt); i++)
  668. if (dt[i] != last_dt[i]) {
  669. for (i = 0; i < sizeof(dt); i++)
  670. last_dt[i] = dt[i];
  671. bar_dirty = 1;
  672. break;
  673. }
  674. }
  675. {
  676. struct sys_cputemp ct;
  677. if (u_cpu_temp(&ct) == 0 &&
  678. ct.temp_c != bar_temp_c) {
  679. bar_temp_c = ct.temp_c;
  680. bar_dirty = 1;
  681. }
  682. }
  683. {
  684. struct sys_powerinfo pi;
  685. int pct = -1, charging = 0, ac = -1;
  686. unsigned int bi;
  687. if (u_power_info(&pi, 0) == 0) {
  688. /* The ThinkPad EC is the laptop marker:
  689. * desktops have no EC, so neither the
  690. * AC state nor a battery is shown. */
  691. if (pi.present) {
  692. ac = pi.ac_online;
  693. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  694. bi++) {
  695. struct sys_battery *b =
  696. &pi.battery[bi];
  697. if (!b->present ||
  698. !b->percent_valid)
  699. continue;
  700. pct = (int)b->percent;
  701. charging = (int)b->charging;
  702. break;
  703. }
  704. }
  705. if (pct != bar_batt_pct ||
  706. charging != bar_batt_charging ||
  707. ac != bar_ac_online) {
  708. bar_batt_pct = pct;
  709. bar_batt_charging = charging;
  710. bar_ac_online = ac;
  711. bar_dirty = 1;
  712. }
  713. }
  714. }
  715. }
  716. }
  717. if (full_redraw) {
  718. if (u_wm_begin() != 0) {
  719. full_redraw = 1;
  720. return;
  721. }
  722. full_redraw = 0;
  723. need = 1;
  724. {
  725. draw_rect(0, 0, fbw, fbh);
  726. draw_bar();
  727. present.count = 0;
  728. add_rect(&present, 0, 0, fbw, fbh);
  729. present.immediate = 0;
  730. u_wm_present(&present);
  731. /* Ack every window that was clean at the time of the
  732. * redraw. If new damage arrived during it, its seq
  733. * differs and the kernel leaves it dirty. */
  734. for (i = 0; i < WM_MAX_WINDOWS; i++)
  735. if (wins[i].info.used && wins[i].mapped)
  736. u_wm_ack(i, wins[i].info.dirty_seq);
  737. }
  738. return;
  739. }
  740. /* Partial cell/canvas updates and the bar clock. */
  741. present.count = 0;
  742. present.immediate = 0;
  743. if (bar_dirty) {
  744. need = 1;
  745. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  746. }
  747. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  748. struct uwin *u = &wins[i];
  749. struct wm_window_info *w = &u->info;
  750. int rx, ry;
  751. unsigned int rw, rh;
  752. if (!w->used || !w->dirty)
  753. continue;
  754. if (!w->dmg_w || !w->dmg_h)
  755. continue;
  756. rx = w->x + 1 + (int)w->dmg_x;
  757. ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_y;
  758. rw = w->dmg_w;
  759. rh = w->dmg_h;
  760. add_rect(&present, rx, ry, rw, rh);
  761. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  762. need = 1;
  763. }
  764. if (!need) {
  765. /* Drag movement is handled below; a stationary mouse can be
  766. * left to the kernel's cursor overlay. */
  767. return;
  768. }
  769. if (u_wm_begin() != 0)
  770. return;
  771. for (i = 0; i < present.count; i++)
  772. draw_rect(present.rect[i].x, present.rect[i].y,
  773. present.rect[i].w, present.rect[i].h);
  774. if (bar_dirty)
  775. draw_bar();
  776. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  777. struct uwin *u = &wins[i];
  778. if (!u->info.used || !u->info.dirty || !u->mapped ||
  779. !u->ack_seq)
  780. continue;
  781. if (u->ack_seq == u->info.dirty_seq)
  782. u_wm_ack(i, u->info.dirty_seq);
  783. }
  784. u_wm_present(&present);
  785. }
  786. static int spawn_app(const char *path)
  787. {
  788. int pid = u_spawn(path, "", 0, "/");
  789. if (pid > 0)
  790. full_redraw = 1;
  791. return pid;
  792. }
  793. static void spawn_app_checked(const char *path)
  794. {
  795. if (!free_window_slot()) {
  796. bar_notice("window limit");
  797. full_redraw = 1;
  798. return;
  799. }
  800. if (spawn_app(path) < 0) {
  801. bar_notice("spawn failed");
  802. full_redraw = 1;
  803. }
  804. }
  805. static void handle_mouse(struct wm_status *st)
  806. {
  807. unsigned int buttons = st->mouse.buttons;
  808. unsigned int mx = st->mouse.x, my = st->mouse.y;
  809. int down = (buttons & 1) != 0;
  810. int was = (prev_buttons & 1) != 0;
  811. if (down && !was) {
  812. if (bar_clock_hit(mx, my)) {
  813. spawn_app_checked("/ramfs/bin/settime");
  814. } else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  815. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  816. if (!free_window_slot()) {
  817. bar_notice("window limit");
  818. full_redraw = 1;
  819. } else if (u_wm_create_term() < 0) {
  820. bar_notice("cannot start term");
  821. full_redraw = 1;
  822. } else {
  823. full_redraw = 1;
  824. }
  825. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  826. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  827. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  828. spawn_app_checked("/ramfs/bin/clock");
  829. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  830. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  831. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  832. spawn_app_checked("/ramfs/bin/colors");
  833. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  834. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  835. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  836. spawn_app_checked("/ramfs/bin/files");
  837. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  838. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  839. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  840. spawn_app_checked("/ramfs/bin/mines");
  841. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  842. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  843. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  844. spawn_app_checked("/ramfs/bin/browser");
  845. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  846. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  847. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  848. spawn_app_checked("/ramfs/bin/solitaire");
  849. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  850. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  851. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  852. spawn_app_checked("/ramfs/bin/gallery");
  853. } else {
  854. int idx = window_at(mx, my);
  855. if (idx >= 0) {
  856. struct uwin *u = &wins[idx];
  857. u_wm_focus((unsigned int)idx);
  858. if (mx >= (unsigned int)(u->info.x + (int)u->info.w -
  859. 1 - WM_CLOSE_W) &&
  860. mx < (unsigned int)(u->info.x + (int)u->info.w) &&
  861. my >= (unsigned int)(u->info.y + 1) &&
  862. my < (unsigned int)(u->info.y + 1 + WM_TITLE_H)) {
  863. u_wm_close((unsigned int)idx);
  864. full_redraw = 1;
  865. } else if (mx >= (unsigned int)(u->info.x + (int)u->info.w - 16) &&
  866. my >= (unsigned int)(u->info.y + (int)u->info.h - 16)) {
  867. resize_id = idx;
  868. resize_mx = mx;
  869. resize_my = my;
  870. resize_ow = u->info.w;
  871. resize_oh = u->info.h;
  872. } else if (my < (unsigned int)(u->info.y + WM_TITLE_H)) {
  873. drag_id = idx;
  874. drag_dx = (int)mx - u->info.x;
  875. drag_dy = (int)my - u->info.y;
  876. }
  877. }
  878. }
  879. } else if (down && was && resize_id >= 0) {
  880. struct uwin *u = &wins[resize_id];
  881. if (u->info.used) {
  882. unsigned int oldw = u->info.w, oldh = u->info.h;
  883. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  884. int nh = (int)resize_oh + (int)my - (int)resize_my;
  885. int maxw = (int)fbw - u->info.x;
  886. int maxh = (int)fbh - u->info.y;
  887. if (nw < 200) nw = 200;
  888. if (nh < 140) nh = 140;
  889. if (nw > maxw) nw = maxw;
  890. if (nh > maxh) nh = maxh;
  891. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  892. struct wm_window_info ni;
  893. struct wm_map_info nm;
  894. struct wm_present_info present;
  895. unsigned int i;
  896. if (u_wm_resize((unsigned int)resize_id,
  897. (unsigned int)(nw - 2),
  898. (unsigned int)(nh - WM_TITLE_H - 2)) == 0) {
  899. if (u_wm_info((unsigned int)resize_id, &ni) == 1)
  900. u->info = ni;
  901. u->mapped = 0;
  902. if (u_wm_map((unsigned int)resize_id, &nm) == 0) {
  903. u->map = nm;
  904. u->mapped = 1;
  905. }
  906. present.count = 0;
  907. present.immediate = 1;
  908. add_rect(&present, u->info.x, u->info.y, oldw, oldh);
  909. add_rect(&present, u->info.x, u->info.y,
  910. u->info.w, u->info.h);
  911. if (u_wm_begin() == 0) {
  912. for (i = 0; i < present.count; i++)
  913. draw_rect(present.rect[i].x,
  914. present.rect[i].y,
  915. present.rect[i].w,
  916. present.rect[i].h);
  917. u_wm_present(&present);
  918. }
  919. }
  920. }
  921. } else {
  922. resize_id = -1;
  923. }
  924. } else if (down && was && drag_id >= 0) {
  925. struct uwin *u = &wins[drag_id];
  926. if (u->info.used) {
  927. int ox = u->info.x, oy = u->info.y;
  928. int nx = (int)mx - drag_dx;
  929. int ny = (int)my - drag_dy;
  930. clamp_geom(u, &nx, &ny);
  931. if (nx != ox || ny != oy) {
  932. struct wm_present_info present;
  933. unsigned int i;
  934. u_wm_set_geom((unsigned int)drag_id, nx, ny);
  935. u->info.x = nx;
  936. u->info.y = ny;
  937. if (u->info.focused) {
  938. move_window_pixels(u, ox, oy, nx, ny);
  939. } else {
  940. present.count = 0;
  941. present.immediate = 1;
  942. add_rect(&present, ox, oy, u->info.w, u->info.h);
  943. add_rect(&present, nx, ny, u->info.w, u->info.h);
  944. if (u_wm_begin() == 0) {
  945. for (i = 0; i < present.count; i++)
  946. draw_rect(present.rect[i].x,
  947. present.rect[i].y,
  948. present.rect[i].w,
  949. present.rect[i].h);
  950. u_wm_present(&present);
  951. }
  952. }
  953. }
  954. } else {
  955. drag_id = -1;
  956. }
  957. } else if (!down && was) {
  958. drag_id = -1;
  959. resize_id = -1;
  960. }
  961. prev_buttons = buttons;
  962. }
  963. static char *put_str(char *p, const char *s)
  964. {
  965. while (*s)
  966. *p++ = *s++;
  967. return p;
  968. }
  969. static char *put_dec(char *p, unsigned int v)
  970. {
  971. char b[10];
  972. unsigned int n = 0;
  973. do {
  974. b[n++] = (char)('0' + v % 10);
  975. v /= 10;
  976. } while (v);
  977. while (n)
  978. *p++ = b[--n];
  979. return p;
  980. }
  981. /* Expose the last interval in /share so the target can be measured without
  982. * a serial console. */
  983. static void write_stats(void)
  984. {
  985. char b[80], *p = b;
  986. unsigned int now = u_ticks_ms();
  987. p = put_str(p, "t=");
  988. p = put_dec(p, now / 1000);
  989. p = put_str(p, "s frames=");
  990. p = put_dec(p, stat_frames);
  991. p = put_str(p, " ms=");
  992. p = put_dec(p, stat_ms);
  993. p = put_str(p, " max=");
  994. p = put_dec(p, stat_max);
  995. p = put_str(p, " px=");
  996. p = put_dec(p, stat_px);
  997. p = put_str(p, "\n");
  998. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  999. (int)(p - b));
  1000. }
  1001. void _start(const char *arg)
  1002. {
  1003. struct wm_screen_info si;
  1004. struct wm_status st;
  1005. unsigned int i;
  1006. (void)arg;
  1007. if (u_wm_attach(&si) != 0)
  1008. u_exit(1);
  1009. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  1010. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  1011. fbw = si.width;
  1012. fbh = si.height;
  1013. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1014. wins[i].info.used = 0;
  1015. wins[i].mapped = 0;
  1016. }
  1017. full_redraw = 1;
  1018. for (;;) {
  1019. unsigned int t0, t1;
  1020. if (u_wm_poll(&st) != 0)
  1021. u_exit(1);
  1022. if (st.full_redraw)
  1023. full_redraw = 1;
  1024. usurf_pixels = 0;
  1025. t0 = u_ticks_ms();
  1026. handle_mouse(&st);
  1027. do_update(&st);
  1028. t1 = u_ticks_ms();
  1029. if (usurf_pixels) {
  1030. unsigned int dt = t1 - t0;
  1031. stat_frames++;
  1032. stat_ms += dt;
  1033. stat_px += usurf_pixels;
  1034. if (dt > stat_max)
  1035. stat_max = dt;
  1036. }
  1037. if (t1 - stat_last >= 2000) {
  1038. stat_last = t1;
  1039. if (stat_frames) {
  1040. write_stats();
  1041. stat_frames = stat_ms = stat_px = stat_max = 0;
  1042. }
  1043. }
  1044. u_sleep_ms(10);
  1045. }
  1046. }