ucmd_wm.c 21 KB

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