ucmd_wm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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 char bar_msg[24];
  34. static unsigned int bar_msg_until;
  35. static int bar_msg_dirty;
  36. static void bar_notice(const char *msg)
  37. {
  38. unsigned int i;
  39. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  40. bar_msg[i] = msg[i];
  41. bar_msg[i] = '\0';
  42. bar_msg_until = u_ticks_ms() + 2000;
  43. bar_msg_dirty = 1;
  44. }
  45. static int free_window_slot(void)
  46. {
  47. unsigned int i;
  48. for (i = 0; i < WM_MAX_WINDOWS; i++)
  49. if (!wins[i].info.used)
  50. return 1;
  51. return 0;
  52. }
  53. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  54. {
  55. return (r << 16) | (g << 8) | b;
  56. }
  57. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  58. * integer interpolation the kernel bar uses. */
  59. static unsigned int rainbow_rgb(unsigned int hue)
  60. {
  61. unsigned int seg = hue / 256, f = hue % 256;
  62. unsigned int r1, g1, b1, r2, g2, b2;
  63. switch (seg) {
  64. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  65. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  66. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  67. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  68. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  69. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  70. }
  71. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  72. (g1 * (256 - f) + g2 * f) / 256,
  73. (b1 * (256 - f) + b2 * f) / 256);
  74. }
  75. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  76. unsigned int rgb, int cx, int cy,
  77. unsigned int cw, unsigned int ch)
  78. {
  79. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  80. if (x0 < cx) x0 = cx;
  81. if (y0 < cy) y0 = cy;
  82. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  83. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  84. if (x1 > x0 && y1 > y0)
  85. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  86. (unsigned int)(y1 - y0), rgb);
  87. }
  88. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  89. unsigned int h, const char *label)
  90. {
  91. unsigned int n = 0;
  92. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  93. while (label[n])
  94. n++;
  95. usurf_text(&screen, (int)(x + (w - n * 8) / 2), (int)(y + 3),
  96. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  97. }
  98. static unsigned int temp_color(int c)
  99. {
  100. if (c >= 80)
  101. return 0xFF5050u;
  102. if (c >= 60)
  103. return 0xFFD250u;
  104. return 0x55FF55u;
  105. }
  106. /* "CPU 43C" (no libc printf here). */
  107. static unsigned int temp_text(char *b, int c)
  108. {
  109. unsigned int n = 0, v = (unsigned int)c;
  110. b[n++] = 'C';
  111. b[n++] = 'P';
  112. b[n++] = 'U';
  113. b[n++] = ' ';
  114. if (v >= 100)
  115. b[n++] = (char)('0' + (v / 100) % 10);
  116. if (v >= 10)
  117. b[n++] = (char)('0' + (v / 10) % 10);
  118. b[n++] = (char)('0' + v % 10);
  119. b[n++] = 'C';
  120. b[n] = '\0';
  121. return n;
  122. }
  123. static void draw_bar(void)
  124. {
  125. char dt[48];
  126. unsigned int n, i, x, y;
  127. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  128. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  129. 0, 0, fbw, WM_BAR_H);
  130. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  131. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  132. WM_BTN_CLOCK_H, "Clock");
  133. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  134. WM_BTN_COLORS_H, "Colors");
  135. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  136. WM_BTN_FILES_H, "Files");
  137. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  138. WM_BTN_MINES_H, "Mines");
  139. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  140. WM_BTN_WEB_H, "Web");
  141. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  142. WM_BTN_CARDS_H, "Cards");
  143. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  144. WM_BTN_GALLERY_H, "Gallery");
  145. if (bar_msg_until &&
  146. (int)(u_ticks_ms() - bar_msg_until) < 0)
  147. usurf_text(&screen, (int)(WM_BTN_GALLERY_X + WM_BTN_GALLERY_W + 8),
  148. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  149. /* Rainbow separator (same y as the kernel's old bar). */
  150. for (x = 0; x < fbw; x++) {
  151. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  152. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  153. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  154. }
  155. if (bar_kexec_busy) {
  156. static const char msg[] = "loading new kernel over serial";
  157. unsigned int dots = bar_kexec_total ?
  158. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  159. n = 0;
  160. while (msg[n])
  161. dt[n] = msg[n], n++;
  162. for (i = 0; i < 12; i++)
  163. dt[n + i] = (i < dots) ? '.' : ' ';
  164. n += 12;
  165. dt[n] = '\0';
  166. usurf_text(&screen, (int)fbw - (int)n * 8 - 4, 5, dt,
  167. 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  168. } else {
  169. unsigned int clock_chars = 0;
  170. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  171. n = 0;
  172. while (dt[n] && n < sizeof(dt) - 1)
  173. n++;
  174. usurf_text(&screen, (int)fbw - (int)n * 8 - 4, 5, dt,
  175. COL_BANNER, 0, 0, fbw, WM_BAR_H);
  176. clock_chars = n;
  177. }
  178. if (bar_temp_c >= 0) {
  179. char tb[12];
  180. unsigned int tn = temp_text(tb, bar_temp_c);
  181. int tx = (int)fbw - 4 - (int)clock_chars * 8 -
  182. (clock_chars ? 8 : 0) - (int)tn * 8;
  183. if (tx >= WM_BTN_GALLERY_X + (int)WM_BTN_GALLERY_W + 4)
  184. usurf_text(&screen, tx, 5, tb,
  185. temp_color(bar_temp_c), 0, 0,
  186. fbw, WM_BAR_H);
  187. }
  188. }
  189. }
  190. static void draw_window_clip(unsigned int idx, int cx, int cy,
  191. unsigned int cw, unsigned int ch)
  192. {
  193. struct uwin *u = &wins[idx];
  194. struct wm_window_info *w = &u->info;
  195. int x = w->x, y = w->y;
  196. unsigned int pw = w->w, ph = w->h;
  197. unsigned int bx = (unsigned int)(x + (int)pw - 1 - WM_CLOSE_W);
  198. unsigned int cony = (unsigned int)(y + 1 + WM_TITLE_H);
  199. unsigned int content_x = (unsigned int)(x + 1);
  200. unsigned int content_w = w->content_w;
  201. unsigned int content_h = w->content_h;
  202. /* Border, title bar, close button. */
  203. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  204. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  205. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  206. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  207. cx, cy, cw, ch);
  208. fill_clip((int)bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  209. cx, cy, cw, ch);
  210. usurf_text(&screen, (int)bx + 4, y + 3, "x", COL_WHITE,
  211. cx, cy, cw, ch);
  212. /* Content: every window presents an RGB565 canvas (the terminal
  213. * renders its cells into one; gfx commands own theirs). */
  214. fill_clip((int)content_x, (int)cony, content_w, content_h,
  215. 0x000000u, cx, cy, cw, ch);
  216. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  217. struct uwm_canvas c;
  218. c.px = (const unsigned short *)u->map.va;
  219. c.w = u->map.w;
  220. c.h = u->map.h;
  221. usurf_canvas(&screen, &c, (int)content_x, (int)cony,
  222. cx, cy, cw, ch);
  223. }
  224. /* Resize grip (bottom-right corner). */
  225. if (w->focused) {
  226. unsigned int i;
  227. for (i = 0; i < 10; i++) {
  228. fill_clip(x + (int)pw - 3 - (int)i,
  229. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  230. cx, cy, cw, ch);
  231. fill_clip(x + (int)pw - 4,
  232. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  233. cx, cy, cw, ch);
  234. }
  235. }
  236. }
  237. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  238. {
  239. unsigned int i;
  240. if (w == 0 || h == 0 || x >= (int)fbw || y >= (int)fbh)
  241. return;
  242. usurf_fill(&screen, x, y, w, h, COL_BG);
  243. for (i = 0; i < WM_MAX_WINDOWS; i++)
  244. if (wins[i].info.used && !wins[i].info.focused &&
  245. x < wins[i].info.x + (int)wins[i].info.w &&
  246. x + (int)w > wins[i].info.x &&
  247. y < wins[i].info.y + (int)wins[i].info.h &&
  248. y + (int)h > wins[i].info.y)
  249. draw_window_clip(i, x, y, w, h);
  250. for (i = 0; i < WM_MAX_WINDOWS; i++)
  251. if (wins[i].info.used && wins[i].info.focused &&
  252. x < wins[i].info.x + (int)wins[i].info.w &&
  253. x + (int)w > wins[i].info.x &&
  254. y < wins[i].info.y + (int)wins[i].info.h &&
  255. y + (int)h > wins[i].info.y)
  256. draw_window_clip(i, x, y, w, h);
  257. }
  258. static int window_at(unsigned int mx, unsigned int my)
  259. {
  260. unsigned int i;
  261. for (i = 0; i < WM_MAX_WINDOWS; i++)
  262. if (wins[i].info.used && wins[i].info.focused &&
  263. mx >= (unsigned int)wins[i].info.x &&
  264. my >= (unsigned int)wins[i].info.y &&
  265. mx < (unsigned int)(wins[i].info.x + (int)wins[i].info.w) &&
  266. my < (unsigned int)(wins[i].info.y + (int)wins[i].info.h))
  267. return (int)i;
  268. /* Inactive windows are painted in slot order, so the highest slot is
  269. * the topmost one under the cursor. */
  270. for (i = WM_MAX_WINDOWS; i-- > 0; )
  271. if (wins[i].info.used && !wins[i].info.focused &&
  272. mx >= (unsigned int)wins[i].info.x &&
  273. my >= (unsigned int)wins[i].info.y &&
  274. mx < (unsigned int)(wins[i].info.x + (int)wins[i].info.w) &&
  275. my < (unsigned int)(wins[i].info.y + (int)wins[i].info.h))
  276. return (int)i;
  277. return -1;
  278. }
  279. static void clamp_geom(struct uwin *u, int *x, int *y)
  280. {
  281. int maxx = (int)fbw - (int)u->info.w;
  282. int maxy = (int)fbh - (int)u->info.h;
  283. if (*x < 0) *x = 0;
  284. if (*y < WM_BAR_H) *y = WM_BAR_H;
  285. if (maxx < 0) maxx = 0;
  286. if (maxy < WM_BAR_H) maxy = WM_BAR_H;
  287. if (*x > maxx) *x = maxx;
  288. if (*y > maxy) *y = maxy;
  289. }
  290. static void add_rect(struct wm_present_info *p, int x, int y,
  291. unsigned int w, unsigned int h)
  292. {
  293. if (w == 0 || h == 0)
  294. return;
  295. if (p->count < 4) {
  296. p->rect[p->count].x = x;
  297. p->rect[p->count].y = y;
  298. p->rect[p->count].w = w;
  299. p->rect[p->count].h = h;
  300. p->count++;
  301. } else {
  302. /* Four is enough for normal updates; fold the rest into the
  303. * last rect rather than dropping pixels. */
  304. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  305. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  306. int nx1 = x + (int)w, ny1 = y + (int)h;
  307. if (x < x0) x0 = x;
  308. if (y < y0) y0 = y;
  309. if (nx1 > x1) x1 = nx1;
  310. if (ny1 > y1) y1 = ny1;
  311. p->rect[3].x = x0;
  312. p->rect[3].y = y0;
  313. p->rect[3].w = (unsigned int)(x1 - x0);
  314. p->rect[3].h = (unsigned int)(y1 - y0);
  315. }
  316. }
  317. static void do_update(struct wm_status *st)
  318. {
  319. struct wm_present_info present;
  320. int need = 0;
  321. int bar_dirty = 0;
  322. unsigned int i;
  323. char dt[20];
  324. static char last_dt[20];
  325. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  326. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  327. unsigned int msg_now = u_ticks_ms();
  328. /* Spawn the first terminal once the kernel says the storage scan is
  329. * done, so a shell command can never race the mount table. */
  330. if (st->boot_ready && !boot_term_spawned) {
  331. boot_term_spawned = 1;
  332. if (!free_window_slot()) {
  333. bar_notice("window limit");
  334. full_redraw = 1;
  335. } else if (u_wm_create_term() < 0) {
  336. bar_notice("cannot start term");
  337. full_redraw = 1;
  338. } else {
  339. full_redraw = 1;
  340. }
  341. }
  342. if (bar_msg_dirty ||
  343. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  344. bar_dirty = 1;
  345. bar_msg_dirty = 0;
  346. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  347. bar_msg_until = 0;
  348. }
  349. bar_kexec_busy = st->kexec_busy;
  350. bar_kexec_progress = st->kexec_progress;
  351. bar_kexec_total = st->kexec_total;
  352. if (kexec_changed)
  353. bar_dirty = 1;
  354. /* Refresh cached window state and mappings before opening a frame:
  355. * MAP may unmap/remap content. */
  356. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  357. struct wm_window_info info;
  358. int r = u_wm_info(i, &info);
  359. if (r < 0) {
  360. u_exit(1);
  361. }
  362. if (r == 0 || !info.used) {
  363. if (wins[i].info.used)
  364. full_redraw = 1;
  365. wins[i].info.used = 0;
  366. wins[i].mapped = 0;
  367. continue;
  368. }
  369. if (!wins[i].info.used ||
  370. info.content_gen != wins[i].info.content_gen)
  371. full_redraw = 1;
  372. wins[i].info = info;
  373. if (!wins[i].mapped ||
  374. wins[i].map.generation != info.content_gen) {
  375. struct wm_map_info map;
  376. if (u_wm_map(i, &map) == 0) {
  377. wins[i].map = map;
  378. wins[i].mapped = 1;
  379. } else {
  380. wins[i].mapped = 0;
  381. }
  382. }
  383. }
  384. /* The RTC is slow (CMOS polling) and the temperature only changes
  385. * once a second; query both at 1 Hz instead of every 10 ms. */
  386. {
  387. static unsigned int last_rtc_ms;
  388. unsigned int now_ms = u_ticks_ms();
  389. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  390. last_rtc_ms = now_ms;
  391. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  392. for (i = 0; i < sizeof(dt); i++)
  393. if (dt[i] != last_dt[i]) {
  394. for (i = 0; i < sizeof(dt); i++)
  395. last_dt[i] = dt[i];
  396. bar_dirty = 1;
  397. break;
  398. }
  399. }
  400. {
  401. struct sys_cputemp ct;
  402. if (u_cpu_temp(&ct) == 0 &&
  403. ct.temp_c != bar_temp_c) {
  404. bar_temp_c = ct.temp_c;
  405. bar_dirty = 1;
  406. }
  407. }
  408. }
  409. }
  410. if (full_redraw) {
  411. if (u_wm_begin() != 0) {
  412. full_redraw = 1;
  413. return;
  414. }
  415. full_redraw = 0;
  416. need = 1;
  417. {
  418. usurf_fill(&screen, 0, 0, fbw, fbh, COL_BG);
  419. for (i = 0; i < WM_MAX_WINDOWS; i++)
  420. if (wins[i].info.used && !wins[i].info.focused)
  421. draw_window_clip(i, 0, 0, fbw, fbh);
  422. for (i = 0; i < WM_MAX_WINDOWS; i++)
  423. if (wins[i].info.used && wins[i].info.focused)
  424. draw_window_clip(i, 0, 0, fbw, fbh);
  425. draw_bar();
  426. present.count = 0;
  427. add_rect(&present, 0, 0, fbw, fbh);
  428. present.immediate = 0;
  429. u_wm_present(&present);
  430. /* Ack every window that was clean at the time of the
  431. * redraw. If new damage arrived during it, its seq
  432. * differs and the kernel leaves it dirty. */
  433. for (i = 0; i < WM_MAX_WINDOWS; i++)
  434. if (wins[i].info.used && wins[i].mapped)
  435. u_wm_ack(i, wins[i].info.dirty_seq);
  436. }
  437. return;
  438. }
  439. /* Partial cell/canvas updates and the bar clock. */
  440. present.count = 0;
  441. present.immediate = 0;
  442. if (bar_dirty) {
  443. need = 1;
  444. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  445. }
  446. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  447. struct uwin *u = &wins[i];
  448. struct wm_window_info *w = &u->info;
  449. int rx, ry;
  450. unsigned int rw, rh;
  451. if (!w->used || !w->dirty)
  452. continue;
  453. if (!w->dmg_w || !w->dmg_h)
  454. continue;
  455. rx = w->x + 1 + (int)w->dmg_x;
  456. ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_y;
  457. rw = w->dmg_w;
  458. rh = w->dmg_h;
  459. add_rect(&present, rx, ry, rw, rh);
  460. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  461. need = 1;
  462. }
  463. if (!need) {
  464. /* Drag movement is handled below; a stationary mouse can be
  465. * left to the kernel's cursor overlay. */
  466. return;
  467. }
  468. if (u_wm_begin() != 0)
  469. return;
  470. for (i = 0; i < present.count; i++)
  471. draw_rect(present.rect[i].x, present.rect[i].y,
  472. present.rect[i].w, present.rect[i].h);
  473. if (bar_dirty)
  474. draw_bar();
  475. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  476. struct uwin *u = &wins[i];
  477. if (!u->info.used || !u->info.dirty || !u->mapped ||
  478. !u->ack_seq)
  479. continue;
  480. if (u->ack_seq == u->info.dirty_seq)
  481. u_wm_ack(i, u->info.dirty_seq);
  482. }
  483. u_wm_present(&present);
  484. }
  485. static int spawn_app(const char *path)
  486. {
  487. int pid = u_spawn(path, "", 0, "/");
  488. if (pid > 0)
  489. full_redraw = 1;
  490. return pid;
  491. }
  492. static void spawn_app_checked(const char *path)
  493. {
  494. if (!free_window_slot()) {
  495. bar_notice("window limit");
  496. full_redraw = 1;
  497. return;
  498. }
  499. if (spawn_app(path) < 0) {
  500. bar_notice("spawn failed");
  501. full_redraw = 1;
  502. }
  503. }
  504. static void handle_mouse(struct wm_status *st)
  505. {
  506. unsigned int buttons = st->mouse.buttons;
  507. unsigned int mx = st->mouse.x, my = st->mouse.y;
  508. int down = (buttons & 1) != 0;
  509. int was = (prev_buttons & 1) != 0;
  510. if (down && !was) {
  511. if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  512. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  513. if (!free_window_slot()) {
  514. bar_notice("window limit");
  515. full_redraw = 1;
  516. } else if (u_wm_create_term() < 0) {
  517. bar_notice("cannot start term");
  518. full_redraw = 1;
  519. } else {
  520. full_redraw = 1;
  521. }
  522. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  523. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  524. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  525. spawn_app_checked("/ramfs/bin/clock");
  526. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  527. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  528. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  529. spawn_app_checked("/ramfs/bin/colors");
  530. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  531. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  532. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  533. spawn_app_checked("/ramfs/bin/files");
  534. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  535. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  536. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  537. spawn_app_checked("/ramfs/bin/mines");
  538. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  539. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  540. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  541. spawn_app_checked("/ramfs/bin/browser");
  542. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  543. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  544. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  545. spawn_app_checked("/ramfs/bin/solitaire");
  546. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  547. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  548. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  549. spawn_app_checked("/ramfs/bin/gallery");
  550. } else {
  551. int idx = window_at(mx, my);
  552. if (idx >= 0) {
  553. struct uwin *u = &wins[idx];
  554. u_wm_focus((unsigned int)idx);
  555. if (mx >= (unsigned int)(u->info.x + (int)u->info.w -
  556. 1 - WM_CLOSE_W) &&
  557. mx < (unsigned int)(u->info.x + (int)u->info.w) &&
  558. my >= (unsigned int)(u->info.y + 1) &&
  559. my < (unsigned int)(u->info.y + 1 + WM_TITLE_H)) {
  560. u_wm_close((unsigned int)idx);
  561. full_redraw = 1;
  562. } else if (mx >= (unsigned int)(u->info.x + (int)u->info.w - 16) &&
  563. my >= (unsigned int)(u->info.y + (int)u->info.h - 16)) {
  564. resize_id = idx;
  565. resize_mx = mx;
  566. resize_my = my;
  567. resize_ow = u->info.w;
  568. resize_oh = u->info.h;
  569. } else if (my < (unsigned int)(u->info.y + WM_TITLE_H)) {
  570. drag_id = idx;
  571. drag_dx = (int)mx - u->info.x;
  572. drag_dy = (int)my - u->info.y;
  573. }
  574. }
  575. }
  576. } else if (down && was && resize_id >= 0) {
  577. struct uwin *u = &wins[resize_id];
  578. if (u->info.used) {
  579. unsigned int oldw = u->info.w, oldh = u->info.h;
  580. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  581. int nh = (int)resize_oh + (int)my - (int)resize_my;
  582. int maxw = (int)fbw - u->info.x;
  583. int maxh = (int)fbh - u->info.y;
  584. if (nw < 200) nw = 200;
  585. if (nh < 140) nh = 140;
  586. if (nw > maxw) nw = maxw;
  587. if (nh > maxh) nh = maxh;
  588. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  589. struct wm_window_info ni;
  590. struct wm_map_info nm;
  591. struct wm_present_info present;
  592. unsigned int i;
  593. if (u_wm_resize((unsigned int)resize_id,
  594. (unsigned int)(nw - 2),
  595. (unsigned int)(nh - WM_TITLE_H - 2)) == 0) {
  596. if (u_wm_info((unsigned int)resize_id, &ni) == 1)
  597. u->info = ni;
  598. u->mapped = 0;
  599. if (u_wm_map((unsigned int)resize_id, &nm) == 0) {
  600. u->map = nm;
  601. u->mapped = 1;
  602. }
  603. present.count = 0;
  604. present.immediate = 1;
  605. add_rect(&present, u->info.x, u->info.y, oldw, oldh);
  606. add_rect(&present, u->info.x, u->info.y,
  607. u->info.w, u->info.h);
  608. if (u_wm_begin() == 0) {
  609. for (i = 0; i < present.count; i++)
  610. draw_rect(present.rect[i].x,
  611. present.rect[i].y,
  612. present.rect[i].w,
  613. present.rect[i].h);
  614. u_wm_present(&present);
  615. }
  616. }
  617. }
  618. } else {
  619. resize_id = -1;
  620. }
  621. } else if (down && was && drag_id >= 0) {
  622. struct uwin *u = &wins[drag_id];
  623. if (u->info.used) {
  624. int ox = u->info.x, oy = u->info.y;
  625. int nx = (int)mx - drag_dx;
  626. int ny = (int)my - drag_dy;
  627. clamp_geom(u, &nx, &ny);
  628. if (nx != ox || ny != oy) {
  629. struct wm_present_info present;
  630. unsigned int i;
  631. u_wm_set_geom((unsigned int)drag_id, nx, ny);
  632. u->info.x = nx;
  633. u->info.y = ny;
  634. present.count = 0;
  635. present.immediate = 1;
  636. add_rect(&present, ox, oy, u->info.w, u->info.h);
  637. add_rect(&present, nx, ny, u->info.w, u->info.h);
  638. if (u_wm_begin() == 0) {
  639. for (i = 0; i < present.count; i++)
  640. draw_rect(present.rect[i].x,
  641. present.rect[i].y,
  642. present.rect[i].w,
  643. present.rect[i].h);
  644. u_wm_present(&present);
  645. }
  646. }
  647. } else {
  648. drag_id = -1;
  649. }
  650. } else if (!down && was) {
  651. drag_id = -1;
  652. resize_id = -1;
  653. }
  654. prev_buttons = buttons;
  655. }
  656. void _start(const char *arg)
  657. {
  658. struct wm_screen_info si;
  659. struct wm_status st;
  660. unsigned int i;
  661. (void)arg;
  662. if (u_wm_attach(&si) != 0)
  663. u_exit(1);
  664. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  665. fbw = si.width;
  666. fbh = si.height;
  667. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  668. wins[i].info.used = 0;
  669. wins[i].mapped = 0;
  670. }
  671. full_redraw = 1;
  672. for (;;) {
  673. if (u_wm_poll(&st) != 0)
  674. u_exit(1);
  675. if (st.full_redraw)
  676. full_redraw = 1;
  677. handle_mouse(&st);
  678. do_update(&st);
  679. u_sleep_ms(10);
  680. }
  681. }