ucmd_wm.c 19 KB

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