ucmd_wm.c 17 KB

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