ucmd_wm.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529
  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. /* The active title bar uses the toolkit highlight (ngui_theme_dark.selection);
  10. * the inactive one stays neutral. Windows have no visible frame. */
  11. #define COL_TITLE_ON 0x365CA6u
  12. #define COL_TITLE_OFF 0x646464u
  13. #define COL_CLOSE 0xC64040u
  14. #define COL_WHITE 0xFFFFFFu
  15. #define COL_BANNER 0x55FF55u
  16. struct uwin {
  17. struct wm_window_info info;
  18. struct wm_map_info map;
  19. int mapped;
  20. unsigned int ack_seq;
  21. /* WIN_NORMAL, or maximized/snapped to a screen half; restore_* is the
  22. * geometry to return to when the window leaves that state. */
  23. int state;
  24. int restore_x, restore_y;
  25. unsigned int restore_w, restore_h;
  26. };
  27. /* Double-click detection on the title bar. */
  28. #define WM_DBLCLICK_MS 500
  29. #define WM_DBLCLICK_SLOP 8
  30. /* A maximized window restores only after the pointer actually starts a drag,
  31. * so a plain click on its title bar leaves it maximized. */
  32. #define WM_DRAG_START 3
  33. /* Window placement states. Maximize fills the work area below the bar; the
  34. * snap states fill its left or right half at full height. */
  35. #define WIN_NORMAL 0
  36. #define WIN_MAXIMIZED 1
  37. #define WIN_SNAP_LEFT 2
  38. #define WIN_SNAP_RIGHT 3
  39. /* Releasing a title-bar drag with the pointer this close to the left/right
  40. * screen edge snaps the window to that half; at the top edge it maximizes. */
  41. #define WM_SNAP_EDGE 2
  42. /* Edge resistance: a dragged window's edge stops at a screen edge or at the
  43. * facing edge of another window until the pointer overshoots it by this
  44. * many pixels, which makes exact side-by-side placement easy. */
  45. #define WM_RESIST 24
  46. static struct usurf screen;
  47. static struct uwin wins[WM_MAX_WINDOWS];
  48. static unsigned int fbw, fbh;
  49. static int drag_id = -1;
  50. static int drag_dx, drag_dy;
  51. static int drag_mx, drag_my; /* press position, for restore-on-drag */
  52. static int drag_moved; /* the pointer passed WM_DRAG_START */
  53. static int resize_id = -1;
  54. static int resize_edges; /* WM_EDGE_* bits captured at the press */
  55. static int resize_ox, resize_oy;
  56. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  57. static unsigned int last_title_ms;
  58. static int last_title_id = -1, last_title_x, last_title_y;
  59. static unsigned int prev_buttons;
  60. static int full_redraw = 1;
  61. static int boot_term_spawned;
  62. static int boot_scan_spawned;
  63. static int boot_scan_pid;
  64. static int boot_scan_placed;
  65. static unsigned int boot_scan_deadline;
  66. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  67. static int bar_temp_c = -1;
  68. static int bar_batt_pct = -1;
  69. static int bar_batt_charging;
  70. static int bar_ac_online = -1;
  71. /* Both top-bar buttons share this geometry: one rounded pill that stops one
  72. * pixel above the rainbow separator (which starts at WM_BAR_H - 5). */
  73. #define BAR_BTN_Y 2
  74. #define BAR_BTN_H 22
  75. #define BAR_BTN_RADIUS 6
  76. /* Battery/AC status icon: the Adwaita AC adapter on AC, the battery outline
  77. * with our own level fill otherwise. The rightmost bar item. */
  78. #define BAR_PWR_W 24
  79. #define BAR_PWR_Y 7
  80. /* The clock, temperature, network and AC/battery labels are one status
  81. * button. draw_bar() records their bounding rectangle; only that rectangle
  82. * opens the control panel, so the empty part of the bar is not clickable. */
  83. static int bar_status_x = -1;
  84. static unsigned int bar_status_w;
  85. static int bar_status_hot;
  86. /* Brightness OSD: a centered bubble shown after the kernel's backlight
  87. * seq changes, then hidden after OSD_MS. */
  88. #define OSD_MS 3000
  89. #define OSD_W 210
  90. #define OSD_H 60
  91. static int osd_show; /* should be visible this frame */
  92. static int osd_visible; /* pixels are on screen */
  93. static int osd_level;
  94. static unsigned int osd_until;
  95. static unsigned int osd_seen_seq;
  96. static int osd_seen_valid;
  97. static int osd_x, osd_y;
  98. static unsigned int osd_w, osd_h;
  99. static char bar_msg[24];
  100. static unsigned int bar_msg_until;
  101. static int bar_msg_dirty;
  102. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  103. /* ---- Start menu --------------------------------------------------------
  104. * The NebOS banner in the top-left corner is the start button. It opens a
  105. * Windows-like panel drawn by the WM itself (not a window, so it never
  106. * consumes a WM_MAX_WINDOWS slot and floats above every canvas). The panel
  107. * lists every GUI app in the pack and ends with a Power row whose submenu
  108. * reboots or powers off the machine through the privileged kernel command
  109. * registry (SYS_SPAWN_KCMD). */
  110. /* The panel mirrors ngui_theme_dark and the netmenu popover: a flat
  111. * theme.bg surface without a rim, rounded theme.panel summary cards,
  112. * theme.panel_alt hover rows and the selection colour for an active item. */
  113. #define SM_BG 0x181C28u
  114. #define SM_PANEL 0x262B3Au
  115. #define SM_PANEL_ALT 0x32384Au
  116. #define SM_TEXT 0xE4E6F0u
  117. #define SM_TEXT_DIM 0x9197AAu
  118. #define SM_SEL 0x365CA6u
  119. #define SM_BTN_HOT 0x46516Cu
  120. #define SM_ERROR 0xEE7474u
  121. #define SM_W 252
  122. #define SM_PAD 12
  123. #define SM_TOP 10
  124. #define SM_GAP 6
  125. #define SM_SECT_H 22
  126. #define SM_ROW_H 26
  127. #define SM_MIN_ROW 20
  128. #define SM_BOTTOM 10
  129. #define SM_RADIUS 6
  130. #define SM_SUB_W 160
  131. #define SM_TILE 18
  132. #define SM_HIT_NONE (-1)
  133. #define SM_HIT_START (-2)
  134. #define SM_HIT_POWER (-3)
  135. #define SM_HIT_REBOOT (-4)
  136. #define SM_HIT_POWEROFF (-5)
  137. #define SM_HIT_MENU (-6) /* inside the panel but not on an entry */
  138. #define SM_ACT_SPAWN 0
  139. #define SM_ACT_TERM 1
  140. #define SM_ACT_SETTINGS 2
  141. struct sm_app {
  142. const char *label;
  143. const char *path;
  144. const char *arg;
  145. unsigned int tile;
  146. char letter;
  147. unsigned char action;
  148. };
  149. /* Every self-contained GUI application in the pack. Terminal and Settings
  150. * are WM actions (a new terminal canvas and the control panel); edit gets a
  151. * scratch file so it can start without a file argument. */
  152. static const struct sm_app sm_apps[] = {
  153. { "Terminal", "", "", 0x3C8CD2u, 'T', SM_ACT_TERM },
  154. { "Files", "/ramfs/bin/files", "", 0xE0A030u, 'F', SM_ACT_SPAWN },
  155. { "Web", "/ramfs/bin/browser", "", 0x4CA0E0u, 'W', SM_ACT_SPAWN },
  156. { "Gallery", "/ramfs/bin/gallery", "", 0xC864B4u, 'G', SM_ACT_SPAWN },
  157. { "Chat", "/ramfs/bin/chat", "", 0x55A055u, 'C', SM_ACT_SPAWN },
  158. { "Clock", "/ramfs/bin/clock", "", 0x8C8C9Cu, 'C', SM_ACT_SPAWN },
  159. { "Text editor", "/ramfs/bin/edit", "/share/UNTITLED.TXT", 0xD27850u, 'E', SM_ACT_SPAWN },
  160. { "Colors", "/ramfs/bin/colors", "", 0xC85A5Au, 'P', SM_ACT_SPAWN },
  161. { "Mines", "/ramfs/bin/mines", "", 0x8C6CD2u, 'M', SM_ACT_SPAWN },
  162. { "Cards", "/ramfs/bin/solitaire","", 0x3CA08Cu, 'S', SM_ACT_SPAWN },
  163. { "Disks", "/ramfs/bin/disks", "", 0x5A8CC8u, 'D', SM_ACT_SPAWN },
  164. { "Tasks", "/ramfs/bin/process", "", 0xE0B040u, 'P', SM_ACT_SPAWN },
  165. { "Settings", "", "", 0x7A7A9Au, 'S', SM_ACT_SETTINGS },
  166. { "Date & Time", "/ramfs/bin/settime", "", 0x5AA0A0u, 'D', SM_ACT_SPAWN },
  167. };
  168. #define SM_NAPP ((int)(sizeof(sm_apps) / sizeof(sm_apps[0])))
  169. struct sm_layout {
  170. int x, y, w, h;
  171. int row_h;
  172. int sect_y;
  173. int row_x, row_w;
  174. int list_y;
  175. int power_y;
  176. int sub_x, sub_y, sub_w, sub_h;
  177. };
  178. static int sm_open;
  179. static int sm_power_open;
  180. static int sm_start_hot;
  181. static int sm_hot; /* hovered app index, -1 = none */
  182. static int sm_power_hot; /* 0 none, 1 Power, 2 Reboot, 3 Power off */
  183. static void sm_layout_get(struct sm_layout *l);
  184. static void sm_draw(void);
  185. static void sm_add_present(struct wm_present_info *p);
  186. static int sm_update_hover(unsigned int mx, unsigned int my);
  187. static void sm_close(void);
  188. static void sm_launch(int idx);
  189. /* Network status icon (right side of the bar). */
  190. #define BAR_NET_W 24
  191. #define BAR_NET_Y 7
  192. #define BAR_NET_NONE 0
  193. #define BAR_NET_WIRED 1
  194. #define BAR_NET_WIFI 2
  195. #define BAR_NET_USB 3
  196. static int bar_net_kind;
  197. static int bar_net_bars;
  198. static unsigned int bar_net_color = 0xFF5050u;
  199. static int control_pid;
  200. static void bar_notice(const char *msg)
  201. {
  202. unsigned int i;
  203. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  204. bar_msg[i] = msg[i];
  205. bar_msg[i] = '\0';
  206. bar_msg_until = u_ticks_ms() + 2000;
  207. bar_msg_dirty = 1;
  208. }
  209. static int free_window_slot(void)
  210. {
  211. unsigned int i;
  212. for (i = 0; i < WM_MAX_WINDOWS; i++)
  213. if (!wins[i].info.used)
  214. return 1;
  215. return 0;
  216. }
  217. /* Find a window by owner pid without waiting for the cached sync state:
  218. * boot ordering needs it before the terminal is spawned. */
  219. static int window_index_pid(int pid, struct wm_window_info *out)
  220. {
  221. unsigned int i;
  222. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  223. struct wm_window_info info;
  224. if (u_wm_info(i, &info) == 1 && info.used &&
  225. (int)info.pid == pid) {
  226. if (out)
  227. *out = info;
  228. return (int)i;
  229. }
  230. }
  231. return -1;
  232. }
  233. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  234. {
  235. return (r << 16) | (g << 8) | b;
  236. }
  237. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  238. * integer interpolation the kernel bar uses. */
  239. static unsigned int rainbow_rgb(unsigned int hue)
  240. {
  241. unsigned int seg = hue / 256, f = hue % 256;
  242. unsigned int r1, g1, b1, r2, g2, b2;
  243. switch (seg) {
  244. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  245. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  246. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  247. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  248. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  249. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  250. }
  251. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  252. (g1 * (256 - f) + g2 * f) / 256,
  253. (b1 * (256 - f) + b2 * f) / 256);
  254. }
  255. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  256. unsigned int rgb, int cx, int cy,
  257. unsigned int cw, unsigned int ch)
  258. {
  259. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  260. if (x0 < cx) x0 = cx;
  261. if (y0 < cy) y0 = cy;
  262. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  263. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  264. if (x1 > x0 && y1 > y0)
  265. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  266. (unsigned int)(y1 - y0), rgb);
  267. }
  268. /* Rounded rectangle fill, matching ngui_fill_round_rect()'s pixel-centre
  269. * corner sampling (the popover's cards and tiles use a 6 px radius). */
  270. static int sm_isqrt(int v)
  271. {
  272. int r = 0, bit = 1 << 30;
  273. if (v <= 0)
  274. return 0;
  275. while (bit > v)
  276. bit >>= 2;
  277. while (bit) {
  278. if (v >= r + bit) {
  279. v -= r + bit;
  280. r = (r >> 1) + bit;
  281. } else {
  282. r >>= 1;
  283. }
  284. bit >>= 2;
  285. }
  286. return r;
  287. }
  288. static int sm_round_inset(int y, int h, int r)
  289. {
  290. int dy2, d2;
  291. if (r <= 0)
  292. return 0;
  293. if (y < r)
  294. dy2 = 2 * r - 2 * y - 1;
  295. else if (y >= h - r)
  296. dy2 = 2 * (y - (h - r)) + 1;
  297. else
  298. return 0;
  299. d2 = 4 * r * r - dy2 * dy2;
  300. if (d2 < 0)
  301. d2 = 0;
  302. return r - (sm_isqrt(d2) + 1) / 2;
  303. }
  304. static void sm_fill_round(int x, int y, int w, int h, int r,
  305. unsigned int rgb)
  306. {
  307. int yy, half;
  308. if (w <= 0 || h <= 0)
  309. return;
  310. half = w < h ? w / 2 : h / 2;
  311. if (r > half)
  312. r = half;
  313. if (r <= 0) {
  314. usurf_fill(&screen, x, y, (unsigned int)w, (unsigned int)h,
  315. rgb);
  316. return;
  317. }
  318. for (yy = 0; yy < h; yy++) {
  319. int li = 0, ri = 0;
  320. if (yy < r) {
  321. li = sm_round_inset(yy, h, r);
  322. ri = li;
  323. }
  324. if (yy >= h - r) {
  325. int i = sm_round_inset(yy, h, r);
  326. if (i > li)
  327. li = i;
  328. if (i > ri)
  329. ri = i;
  330. }
  331. if (w - li - ri > 0)
  332. usurf_fill(&screen, x + li, y + yy,
  333. (unsigned int)(w - li - ri), 1, rgb);
  334. }
  335. }
  336. /* One shared renderer for the start and status buttons: the same height,
  337. * corner radius and hover/active colours. */
  338. static void bar_button_bg(int x, int w, int active, int hot)
  339. {
  340. if (active)
  341. sm_fill_round(x, BAR_BTN_Y, w, BAR_BTN_H, BAR_BTN_RADIUS,
  342. SM_SEL);
  343. else if (hot)
  344. sm_fill_round(x, BAR_BTN_Y, w, BAR_BTN_H, BAR_BTN_RADIUS,
  345. SM_PANEL_ALT);
  346. }
  347. /* The NebOS banner doubles as the start button. Its width follows the
  348. * proportional font, so the hit area and the highlight use the same
  349. * geometry. */
  350. static void bar_start_geom(int *x, int *y, int *w, int *h)
  351. {
  352. *x = 2;
  353. *y = BAR_BTN_Y;
  354. *h = BAR_BTN_H;
  355. *w = (int)usurf_text_width(&screen, OS_NAME " v" OS_VERSION) + 20;
  356. }
  357. static int bar_start_hit(unsigned int mx, unsigned int my)
  358. {
  359. int x, y, w, h;
  360. bar_start_geom(&x, &y, &w, &h);
  361. return (int)mx >= x && (int)mx < x + w &&
  362. (int)my >= y && (int)my < y + h;
  363. }
  364. static void bar_start_button(void)
  365. {
  366. int x, y, w, h;
  367. bar_start_geom(&x, &y, &w, &h);
  368. bar_button_bg(x, w, sm_open, sm_start_hot);
  369. usurf_text(&screen, x + 10, y + (h - 16) / 2,
  370. OS_NAME " v" OS_VERSION,
  371. sm_open ? COL_WHITE : COL_BANNER, 0, 0, fbw, WM_BAR_H);
  372. }
  373. static int name_is(const char *a, const char *b)
  374. {
  375. while (*a && *b) {
  376. if (*a != *b)
  377. return 0;
  378. a++;
  379. b++;
  380. }
  381. return *a == *b;
  382. }
  383. static int net_rssi_bars(int rssi)
  384. {
  385. if (rssi >= -55)
  386. return 4;
  387. if (rssi >= -65)
  388. return 3;
  389. if (rssi >= -75)
  390. return 2;
  391. return 1;
  392. }
  393. /* Small top-bar pictograms: Adwaita symbolic masks from the pack, centered
  394. * in their 24 px hit slots. */
  395. static void bar_draw_net_icon(int x, int y)
  396. {
  397. enum uwm_icon ic;
  398. switch (bar_net_kind) {
  399. case BAR_NET_WIRED:
  400. ic = UWM_ICON_NET_WIRED;
  401. break;
  402. case BAR_NET_WIFI:
  403. ic = bar_net_bars > 0 ? UWM_ICON_NET_WIFI_0 + bar_net_bars :
  404. UWM_ICON_NET_WIFI_OFF;
  405. break;
  406. case BAR_NET_USB:
  407. ic = UWM_ICON_NET_USB;
  408. break;
  409. default:
  410. ic = UWM_ICON_NET_WIRED_OFF;
  411. break;
  412. }
  413. (void)usurf_icon(&screen, x + 4, y, ic, bar_net_color);
  414. }
  415. /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
  416. * only made when WiFi is the active netdev, so a machine without a card does
  417. * not run a PCI scan on every tick. */
  418. static int poll_net_status(void)
  419. {
  420. struct sys_netinfo ni;
  421. int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
  422. unsigned int color = 0xFF5050u;
  423. int changed;
  424. int i;
  425. if (u_net_info(&ni) == 1 && ni.present) {
  426. for (i = 0; i < 4; i++)
  427. if (ni.ip[i])
  428. have_ip = 1;
  429. if (name_is(ni.name, "wifi")) {
  430. struct sys_wifi_status ws;
  431. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
  432. ws.present) {
  433. kind = BAR_NET_WIFI;
  434. if (ws.state >= SYS_WIFI_ST_ASSOC) {
  435. bars = net_rssi_bars(ws.rssi);
  436. color = (ws.state >= SYS_WIFI_ST_LINK &&
  437. have_ip) ?
  438. 0x55FF55u : 0xFFD250u;
  439. } else {
  440. /* Card present, not associated. */
  441. bars = 0;
  442. color = 0xFF5050u;
  443. }
  444. }
  445. } else if (name_is(ni.name, "rndis")) {
  446. kind = BAR_NET_USB;
  447. color = have_ip ? 0x55FF55u : 0xFFD250u;
  448. } else {
  449. kind = BAR_NET_WIRED;
  450. color = have_ip ? 0x55FF55u : 0xFFD250u;
  451. }
  452. }
  453. changed = kind != bar_net_kind || bars != bar_net_bars ||
  454. color != bar_net_color;
  455. bar_net_kind = kind;
  456. bar_net_bars = bars;
  457. bar_net_color = color;
  458. return changed;
  459. }
  460. static unsigned int temp_color(int c)
  461. {
  462. if (c >= 80)
  463. return 0xFF5050u;
  464. if (c >= 60)
  465. return 0xFFD250u;
  466. return 0x55FF55u;
  467. }
  468. /* "43C" (no libc printf here). */
  469. static unsigned int temp_text(char *b, int c)
  470. {
  471. unsigned int n = 0, v = (unsigned int)c;
  472. if (v >= 100)
  473. b[n++] = (char)('0' + (v / 100) % 10);
  474. if (v >= 10)
  475. b[n++] = (char)('0' + (v / 10) % 10);
  476. b[n++] = (char)('0' + v % 10);
  477. b[n++] = 'C';
  478. b[n] = '\0';
  479. return n;
  480. }
  481. static unsigned int batt_color(int pct)
  482. {
  483. if (pct <= 10)
  484. return 0xFF5050u;
  485. return 0x55FF55u;
  486. }
  487. /* Battery: the Adwaita outline with our own level fill over the lower
  488. * interior. The symbol's body is striped, so the interior is cleared to a
  489. * dim shade of the state colour first; the level then reads as a bright
  490. * block inside the outline. A critical charge turns the outline red too.
  491. * Geometry of battery-symbolic at 16 px: body x2..13, cavity x4..11 and
  492. * y4..13. */
  493. static unsigned int batt_dim(unsigned int c)
  494. {
  495. return (c >> 2) & 0x3F3F3F;
  496. }
  497. static void bar_icon_battery(int x, int y, int pct, unsigned int c)
  498. {
  499. unsigned int fill = (unsigned int)((10 * pct + 99) / 100);
  500. int ix = x + 4;
  501. if (fill > 10)
  502. fill = 10;
  503. if (!usurf_icon(&screen, ix, y, UWM_ICON_BATTERY, c))
  504. return;
  505. usurf_fill(&screen, ix + 4, y + 4, 8, 10, batt_dim(c));
  506. if (fill)
  507. usurf_fill(&screen, ix + 4, y + 14 - (int)fill, 8, fill, c);
  508. }
  509. /* AC adapter: the Adwaita plug symbol. */
  510. static void bar_icon_ac(int x, int y, unsigned int c)
  511. {
  512. (void)usurf_icon(&screen, x + 4, y, UWM_ICON_AC, c);
  513. }
  514. static void draw_bar(void)
  515. {
  516. char dt[48];
  517. unsigned int n, i, x, y;
  518. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  519. bar_start_button();
  520. if (bar_msg_until &&
  521. (int)(u_ticks_ms() - bar_msg_until) < 0)
  522. usurf_text(&screen, (int)(WM_BAR_LEFT_END + 8),
  523. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  524. /* Rainbow separator (same y as the kernel's old bar). */
  525. for (x = 0; x < fbw; x++) {
  526. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  527. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  528. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  529. }
  530. if (bar_kexec_busy) {
  531. static const char msg[] = "loading new kernel over serial";
  532. unsigned int dots = bar_kexec_total ?
  533. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  534. bar_status_x = -1;
  535. bar_status_w = 0;
  536. n = 0;
  537. while (msg[n])
  538. dt[n] = msg[n], n++;
  539. for (i = 0; i < 12; i++)
  540. dt[n + i] = (i < dots) ? '.' : ' ';
  541. n += 12;
  542. dt[n] = '\0';
  543. usurf_text(&screen,
  544. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  545. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  546. } else {
  547. int right = (int)fbw - 4;
  548. int min_x = (int)WM_BAR_LEFT_END + 4;
  549. int pwr_px = -1, net_nx = -1, clock_tx = -1, temp_tx = -1;
  550. int temp_tw;
  551. int left;
  552. char cdt[12], tb[12];
  553. const char *clock_txt = dt;
  554. /* Lay the cluster out right to left: AC/battery, network, the
  555. * date/time and the CPU temperature. */
  556. if (bar_batt_pct >= 0 || bar_ac_online > 0) {
  557. pwr_px = right - BAR_PWR_W;
  558. if (pwr_px >= min_x)
  559. right = pwr_px - 8;
  560. else
  561. pwr_px = -1;
  562. }
  563. net_nx = right - BAR_NET_W;
  564. if (net_nx >= min_x)
  565. right = net_nx - 8;
  566. else
  567. net_nx = -1;
  568. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  569. int tw = (int)usurf_text_width(&screen, dt);
  570. int tx = right - tw;
  571. unsigned int k;
  572. if (tx < min_x) {
  573. /* Compact form "MM-DD HH:MM" from
  574. * "YYYY-MM-DD HH:MM:SS" when the status
  575. * cluster is too wide for the full date. */
  576. for (k = 0; k < 11 && dt[5 + k]; k++)
  577. cdt[k] = dt[5 + k];
  578. cdt[k] = '\0';
  579. clock_txt = cdt;
  580. tw = (int)usurf_text_width(&screen, cdt);
  581. tx = right - tw;
  582. }
  583. if (tx >= min_x) {
  584. clock_tx = tx;
  585. right = tx - 8;
  586. }
  587. }
  588. if (bar_temp_c >= 0) {
  589. (void)temp_text(tb, bar_temp_c);
  590. temp_tw = (int)usurf_text_width(&screen, tb);
  591. temp_tx = right - temp_tw;
  592. if (temp_tx < min_x)
  593. temp_tx = -1;
  594. }
  595. /* The status labels form one click target: record the whole
  596. * cluster and paint one rounded hover pill behind it. */
  597. left = pwr_px;
  598. if (net_nx >= 0)
  599. left = net_nx;
  600. if (clock_tx >= 0)
  601. left = clock_tx;
  602. if (temp_tx >= 0)
  603. left = temp_tx;
  604. if (left >= 0) {
  605. bar_status_x = left - 4;
  606. if (bar_status_x < 0)
  607. bar_status_x = 0;
  608. bar_status_w = (unsigned int)((int)fbw - 2 -
  609. bar_status_x);
  610. bar_button_bg(bar_status_x, (int)bar_status_w,
  611. control_pid > 0, bar_status_hot);
  612. } else {
  613. bar_status_x = -1;
  614. bar_status_w = 0;
  615. }
  616. if (pwr_px >= 0) {
  617. if (bar_ac_online > 0)
  618. bar_icon_ac(pwr_px, BAR_PWR_Y, 0x55FF55u);
  619. else
  620. bar_icon_battery(pwr_px, BAR_PWR_Y,
  621. bar_batt_pct,
  622. batt_color(bar_batt_pct));
  623. }
  624. if (net_nx >= 0)
  625. bar_draw_net_icon(net_nx, BAR_NET_Y);
  626. if (clock_tx >= 0)
  627. usurf_text(&screen, clock_tx, 5, clock_txt, COL_BANNER,
  628. 0, 0, fbw, WM_BAR_H);
  629. if (temp_tx >= 0)
  630. usurf_text(&screen, temp_tx, 5, tb,
  631. temp_color(bar_temp_c), 0, 0, fbw,
  632. WM_BAR_H);
  633. }
  634. }
  635. /* A popover is a flat, chromeless panel: the canvas fills the window. */
  636. static void draw_popover_clip(unsigned int idx, int cx, int cy,
  637. unsigned int cw, unsigned int ch)
  638. {
  639. struct uwin *u = &wins[idx];
  640. struct wm_window_info *w = &u->info;
  641. unsigned int cwid = w->content_w, chei = w->content_h;
  642. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  643. struct uwm_canvas c;
  644. c.px = (const unsigned short *)u->map.va;
  645. c.w = u->map.w;
  646. c.h = u->map.h;
  647. if (c.w == cwid && c.h == chei) {
  648. usurf_canvas(&screen, &c, w->x, w->y,
  649. cx, cy, cw, ch);
  650. return;
  651. }
  652. }
  653. fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
  654. }
  655. static void draw_window_clip(unsigned int idx, int cx, int cy,
  656. unsigned int cw, unsigned int ch)
  657. {
  658. struct uwin *u = &wins[idx];
  659. struct wm_window_info *w = &u->info;
  660. int x = w->x, y = w->y;
  661. unsigned int pw = w->w;
  662. int bx = x + (int)pw - WM_BORDER_W - WM_CLOSE_W;
  663. int cony = y + WM_BORDER_W + WM_TITLE_H;
  664. int content_x = x + WM_BORDER_W;
  665. unsigned int content_w = w->content_w;
  666. unsigned int content_h = w->content_h;
  667. if (w->popover) {
  668. draw_popover_clip(idx, cx, cy, cw, ch);
  669. return;
  670. }
  671. /* Title bar and close button; windows have no visible frame. */
  672. fill_clip(x, y, pw, WM_TITLE_H,
  673. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  674. usurf_text(&screen, x + 3, y + 2, w->title, COL_WHITE,
  675. cx, cy, cw, ch);
  676. fill_clip(bx, y, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE, cx, cy, cw, ch);
  677. usurf_text(&screen, bx + 4, y + 2, "x", COL_WHITE,
  678. cx, cy, cw, ch);
  679. /* Content: every window presents an RGB565 canvas (the terminal
  680. * renders its cells into one; gfx commands own theirs). A mapped
  681. * canvas covers the whole content area, so it does not need the black
  682. * underlay. */
  683. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  684. struct uwm_canvas c;
  685. c.px = (const unsigned short *)u->map.va;
  686. c.w = u->map.w;
  687. c.h = u->map.h;
  688. if (c.w != content_w || c.h != content_h)
  689. fill_clip(content_x, cony, content_w, content_h,
  690. 0x000000u, cx, cy, cw, ch);
  691. usurf_canvas(&screen, &c, content_x, cony,
  692. cx, cy, cw, ch);
  693. } else {
  694. fill_clip(content_x, cony, content_w, content_h,
  695. 0x000000u, cx, cy, cw, ch);
  696. }
  697. }
  698. /* Front-to-back window order: popovers float above everything, then the
  699. * focused window, then the others in descending slot order. Painting and
  700. * hit-testing share it. */
  701. static unsigned int build_order(int *order)
  702. {
  703. unsigned int i, n = 0;
  704. for (i = 0; i < WM_MAX_WINDOWS; i++)
  705. if (wins[i].info.used && wins[i].info.popover)
  706. order[n++] = (int)i;
  707. for (i = 0; i < WM_MAX_WINDOWS; i++)
  708. if (wins[i].info.used && wins[i].info.focused &&
  709. !wins[i].info.popover)
  710. order[n++] = (int)i;
  711. for (i = WM_MAX_WINDOWS; i-- > 0;)
  712. if (wins[i].info.used && !wins[i].info.focused &&
  713. !wins[i].info.popover)
  714. order[n++] = (int)i;
  715. return n;
  716. }
  717. struct orect {
  718. int x, y;
  719. unsigned int w, h;
  720. };
  721. #define OCC_MAX 64
  722. /* r minus cut, split into up to four disjoint rectangles. */
  723. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  724. const struct orect *cut)
  725. {
  726. int rx0 = r->x, ry0 = r->y;
  727. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  728. int cx0 = cut->x, cy0 = cut->y;
  729. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  730. unsigned int n = 0;
  731. if (cx0 < rx0) cx0 = rx0;
  732. if (cy0 < ry0) cy0 = ry0;
  733. if (cx1 > rx1) cx1 = rx1;
  734. if (cy1 > ry1) cy1 = ry1;
  735. if (cx1 <= cx0 || cy1 <= cy0) {
  736. out[0] = *r;
  737. return 1;
  738. }
  739. if (cy0 > ry0) {
  740. out[n].x = rx0;
  741. out[n].y = ry0;
  742. out[n].w = (unsigned int)(rx1 - rx0);
  743. out[n].h = (unsigned int)(cy0 - ry0);
  744. n++;
  745. }
  746. if (cy1 < ry1) {
  747. out[n].x = rx0;
  748. out[n].y = cy1;
  749. out[n].w = (unsigned int)(rx1 - rx0);
  750. out[n].h = (unsigned int)(ry1 - cy1);
  751. n++;
  752. }
  753. if (cx0 > rx0) {
  754. out[n].x = rx0;
  755. out[n].y = cy0;
  756. out[n].w = (unsigned int)(cx0 - rx0);
  757. out[n].h = (unsigned int)(cy1 - cy0);
  758. n++;
  759. }
  760. if (cx1 < rx1) {
  761. out[n].x = cx1;
  762. out[n].y = cy0;
  763. out[n].w = (unsigned int)(rx1 - cx1);
  764. out[n].h = (unsigned int)(cy1 - cy0);
  765. n++;
  766. }
  767. return n;
  768. }
  769. /* Back-to-front repaint, used only if the visible-region list overflows. */
  770. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  771. {
  772. unsigned int i;
  773. usurf_fill(&screen, x, y, w, h, COL_BG);
  774. for (i = 0; i < WM_MAX_WINDOWS; i++)
  775. if (wins[i].info.used && !wins[i].info.focused &&
  776. !wins[i].info.popover &&
  777. x < wins[i].info.x + (int)wins[i].info.w &&
  778. x + (int)w > wins[i].info.x &&
  779. y < wins[i].info.y + (int)wins[i].info.h &&
  780. y + (int)h > wins[i].info.y)
  781. draw_window_clip(i, x, y, w, h);
  782. for (i = 0; i < WM_MAX_WINDOWS; i++)
  783. if (wins[i].info.used && wins[i].info.focused &&
  784. !wins[i].info.popover &&
  785. x < wins[i].info.x + (int)wins[i].info.w &&
  786. x + (int)w > wins[i].info.x &&
  787. y < wins[i].info.y + (int)wins[i].info.h &&
  788. y + (int)h > wins[i].info.y)
  789. draw_window_clip(i, x, y, w, h);
  790. for (i = 0; i < WM_MAX_WINDOWS; i++)
  791. if (wins[i].info.used && wins[i].info.popover &&
  792. x < wins[i].info.x + (int)wins[i].info.w &&
  793. x + (int)w > wins[i].info.x &&
  794. y < wins[i].info.y + (int)wins[i].info.h &&
  795. y + (int)h > wins[i].info.y)
  796. draw_window_clip(i, x, y, w, h);
  797. }
  798. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  799. {
  800. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  801. struct orect dmg;
  802. int order[WM_MAX_WINDOWS];
  803. unsigned int n, i, j, ncur, nnext;
  804. int overflow = 0;
  805. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  806. if (w == 0 || h == 0)
  807. return;
  808. if (x0 < 0) x0 = 0;
  809. if (y0 < 0) y0 = 0;
  810. if (x1 > (int)fbw) x1 = (int)fbw;
  811. if (y1 > (int)fbh) y1 = (int)fbh;
  812. if (x1 <= x0 || y1 <= y0)
  813. return;
  814. dmg.x = x0;
  815. dmg.y = y0;
  816. dmg.w = (unsigned int)(x1 - x0);
  817. dmg.h = (unsigned int)(y1 - y0);
  818. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  819. n = build_order(order);
  820. for (i = 0; i < n; i++) {
  821. struct uwin *u = &wins[order[i]];
  822. int wx0 = u->info.x, wy0 = u->info.y;
  823. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  824. if (wx0 < dmg.x) wx0 = dmg.x;
  825. if (wy0 < dmg.y) wy0 = dmg.y;
  826. if (wx1 > x1) wx1 = x1;
  827. if (wy1 > y1) wy1 = y1;
  828. if (wx1 <= wx0 || wy1 <= wy0)
  829. continue;
  830. cur[0].x = wx0;
  831. cur[0].y = wy0;
  832. cur[0].w = (unsigned int)(wx1 - wx0);
  833. cur[0].h = (unsigned int)(wy1 - wy0);
  834. ncur = 1;
  835. for (j = 0; j < i && ncur; j++) {
  836. struct uwin *f = &wins[order[j]];
  837. struct orect fr;
  838. unsigned int k;
  839. fr.x = f->info.x;
  840. fr.y = f->info.y;
  841. fr.w = f->info.w;
  842. fr.h = f->info.h;
  843. nnext = 0;
  844. for (k = 0; k < ncur; k++) {
  845. if (nnext + 4 > OCC_MAX) {
  846. overflow = 1;
  847. break;
  848. }
  849. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  850. }
  851. if (overflow)
  852. break;
  853. for (k = 0; k < nnext; k++)
  854. cur[k] = nxt[k];
  855. ncur = nnext;
  856. }
  857. if (overflow)
  858. break;
  859. for (j = 0; j < ncur; j++)
  860. draw_window_clip((unsigned int)order[i], cur[j].x,
  861. cur[j].y, cur[j].w, cur[j].h);
  862. }
  863. if (overflow)
  864. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  865. }
  866. /* Invisible resize border: edge bits for a point in the band around the
  867. * window's outer edge (WM_RESIZE_IN px inside and WM_RESIZE_OUT px outside).
  868. * Popovers have no chrome and never resize. */
  869. #define WM_EDGE_LEFT 1
  870. #define WM_EDGE_RIGHT 2
  871. #define WM_EDGE_TOP 4
  872. #define WM_EDGE_BOTTOM 8
  873. static int resize_edges_at(const struct uwin *u, int mx, int my)
  874. {
  875. const struct wm_window_info *w = &u->info;
  876. int l = w->x, t = w->y;
  877. int r = l + (int)w->w, b = t + (int)w->h;
  878. int near_x, near_y, e = 0;
  879. if (w->popover)
  880. return 0;
  881. near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
  882. near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
  883. if (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN)
  884. e |= WM_EDGE_LEFT;
  885. if (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT)
  886. e |= WM_EDGE_RIGHT;
  887. if (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN)
  888. e |= WM_EDGE_TOP;
  889. if (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT)
  890. e |= WM_EDGE_BOTTOM;
  891. return e;
  892. }
  893. /* Topmost window at (mx,my): its resize band when the point is inside one,
  894. * otherwise the window rectangle. *edges is 0 for a plain rectangle hit.
  895. * Bands are not considered inside the top bar, which the WM owns. */
  896. static int resize_hit(int mx, int my, int *edges)
  897. {
  898. int order[WM_MAX_WINDOWS];
  899. unsigned int n = build_order(order), i;
  900. *edges = 0;
  901. for (i = 0; i < n; i++) {
  902. struct uwin *u = &wins[order[i]];
  903. int e = 0;
  904. if (!u->info.used)
  905. continue;
  906. if (my >= WM_BAR_H)
  907. e = resize_edges_at(u, mx, my);
  908. if (e) {
  909. *edges = e;
  910. return order[i];
  911. }
  912. if (mx >= u->info.x && my >= u->info.y &&
  913. mx < u->info.x + (int)u->info.w &&
  914. my < u->info.y + (int)u->info.h)
  915. return order[i];
  916. }
  917. return -1;
  918. }
  919. static int resize_cursor(int edges)
  920. {
  921. switch (edges) {
  922. case WM_EDGE_LEFT:
  923. case WM_EDGE_RIGHT:
  924. return CURSOR_EW_RESIZE;
  925. case WM_EDGE_TOP:
  926. case WM_EDGE_BOTTOM:
  927. return CURSOR_NS_RESIZE;
  928. case WM_EDGE_LEFT | WM_EDGE_TOP:
  929. case WM_EDGE_RIGHT | WM_EDGE_BOTTOM:
  930. return CURSOR_NWSE_RESIZE;
  931. case WM_EDGE_RIGHT | WM_EDGE_TOP:
  932. case WM_EDGE_LEFT | WM_EDGE_BOTTOM:
  933. return CURSOR_NESW_RESIZE;
  934. default:
  935. return CURSOR_DEFAULT;
  936. }
  937. }
  938. /* Close button hit area (the top-right WM_CLOSE_W columns of the title bar). */
  939. static int close_hit(const struct wm_window_info *w, int mx, int my)
  940. {
  941. if (w->popover)
  942. return 0;
  943. return mx >= w->x + (int)w->w - WM_CLOSE_W &&
  944. mx < w->x + (int)w->w &&
  945. my >= w->y && my < w->y + WM_TITLE_H;
  946. }
  947. static void clamp_geom(struct uwin *u, int *x, int *y)
  948. {
  949. /* Match wm_clamp_window(): allow the window to hang off the left,
  950. * right and bottom edges but keep the title bar reachable and below
  951. * the system bar. */
  952. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  953. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  954. int miny = WM_BAR_H;
  955. int maxy = (int)fbh - WM_BORDER_W - WM_TITLE_H;
  956. if (minx > maxx)
  957. minx = maxx = 0;
  958. if (miny > maxy)
  959. miny = maxy = WM_BAR_H;
  960. if (*x < minx)
  961. *x = minx;
  962. else if (*x > maxx)
  963. *x = maxx;
  964. if (*y < miny)
  965. *y = miny;
  966. else if (*y > maxy)
  967. *y = maxy;
  968. }
  969. static void add_rect(struct wm_present_info *p, int x, int y,
  970. unsigned int w, unsigned int h)
  971. {
  972. if (w == 0 || h == 0)
  973. return;
  974. if (p->count < 4) {
  975. p->rect[p->count].x = x;
  976. p->rect[p->count].y = y;
  977. p->rect[p->count].w = w;
  978. p->rect[p->count].h = h;
  979. p->count++;
  980. } else {
  981. /* Four is enough for normal updates; fold the rest into the
  982. * last rect rather than dropping pixels. */
  983. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  984. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  985. int nx1 = x + (int)w, ny1 = y + (int)h;
  986. if (x < x0) x0 = x;
  987. if (y < y0) y0 = y;
  988. if (nx1 > x1) x1 = nx1;
  989. if (ny1 > y1) y1 = ny1;
  990. p->rect[3].x = x0;
  991. p->rect[3].y = y0;
  992. p->rect[3].w = (unsigned int)(x1 - x0);
  993. p->rect[3].h = (unsigned int)(y1 - y0);
  994. }
  995. }
  996. /* Center the brightness bubble in the work area, clamped on small screens. */
  997. static void osd_geom(void)
  998. {
  999. int x, y;
  1000. osd_w = OSD_W;
  1001. osd_h = OSD_H;
  1002. x = ((int)fbw - (int)osd_w) / 2;
  1003. y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
  1004. if (x < 0)
  1005. x = 0;
  1006. if (x + (int)osd_w > (int)fbw)
  1007. x = (int)fbw - (int)osd_w;
  1008. if (y < WM_BAR_H)
  1009. y = WM_BAR_H;
  1010. if (y + (int)osd_h > (int)fbh)
  1011. y = (int)fbh - (int)osd_h;
  1012. osd_x = x;
  1013. osd_y = y;
  1014. }
  1015. /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
  1016. static void osd_sun(int cx, int cy)
  1017. {
  1018. static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
  1019. int i;
  1020. for (i = -5; i <= 5; i++)
  1021. usurf_fill(&screen, cx - half[i + 5], cy + i,
  1022. half[i + 5] * 2 + 1, 1, 0xFFD250u);
  1023. usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
  1024. usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
  1025. usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
  1026. usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
  1027. }
  1028. static void osd_draw(void)
  1029. {
  1030. char pct[8];
  1031. unsigned int n = 0;
  1032. unsigned int bar_x, bar_y, bar_w;
  1033. int x, y, tw;
  1034. osd_geom();
  1035. x = osd_x;
  1036. y = osd_y;
  1037. usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
  1038. usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
  1039. usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
  1040. usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
  1041. usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
  1042. 0x70709Au);
  1043. osd_sun(x + 22, y + (int)osd_h / 2);
  1044. if (osd_level >= 100) {
  1045. pct[n++] = '1';
  1046. pct[n++] = '0';
  1047. pct[n++] = '0';
  1048. } else {
  1049. if (osd_level >= 10)
  1050. pct[n++] = (char)('0' + osd_level / 10);
  1051. pct[n++] = (char)('0' + osd_level % 10);
  1052. }
  1053. pct[n++] = '%';
  1054. pct[n] = '\0';
  1055. usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
  1056. x, y, osd_w, osd_h);
  1057. tw = (int)usurf_text_width(&screen, pct);
  1058. usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
  1059. 0xFFD250u, x, y, osd_w, osd_h);
  1060. bar_x = (unsigned int)(x + 42);
  1061. bar_y = (unsigned int)(y + 35);
  1062. bar_w = osd_w - 42 - 14;
  1063. usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
  1064. usurf_fill(&screen, (int)bar_x, (int)bar_y,
  1065. (bar_w * (unsigned int)osd_level + 50) / 100, 10,
  1066. 0xFFD250u);
  1067. }
  1068. /* Put the brightness OSD and the start menu back on top after a render pass
  1069. * that may have overwritten them. */
  1070. static void wm_overlays(struct wm_present_info *p)
  1071. {
  1072. if (osd_show) {
  1073. osd_draw();
  1074. add_rect(p, osd_x, osd_y, osd_w, osd_h);
  1075. }
  1076. if (sm_open) {
  1077. sm_draw();
  1078. sm_add_present(p);
  1079. }
  1080. }
  1081. /* A focused window is topmost, so the shadow contains exactly its pixels in
  1082. * the old rectangle. Move that block and repaint only the old part the new
  1083. * rectangle does not cover. When either rectangle is partly off-screen the
  1084. * shadow has no valid pixels for the hidden part, so repaint the new rect
  1085. * instead of copying desktop background into the window. */
  1086. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  1087. {
  1088. struct wm_present_info present;
  1089. struct orect oldr, newr, pieces[4];
  1090. unsigned int np, k;
  1091. int w = (int)u->info.w, h = (int)u->info.h;
  1092. int old_on = ox >= 0 && oy >= 0 &&
  1093. ox + w <= (int)fbw && oy + h <= (int)fbh;
  1094. int new_on = nx >= 0 && ny >= 0 &&
  1095. nx + w <= (int)fbw && ny + h <= (int)fbh;
  1096. if (u_wm_begin() != 0)
  1097. return;
  1098. if (old_on && new_on)
  1099. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  1100. else
  1101. draw_rect(nx, ny, u->info.w, u->info.h);
  1102. oldr.x = ox;
  1103. oldr.y = oy;
  1104. oldr.w = u->info.w;
  1105. oldr.h = u->info.h;
  1106. newr.x = nx;
  1107. newr.y = ny;
  1108. newr.w = u->info.w;
  1109. newr.h = u->info.h;
  1110. np = rect_sub(pieces, &oldr, &newr);
  1111. for (k = 0; k < np; k++)
  1112. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  1113. present.count = 0;
  1114. present.immediate = 1;
  1115. add_rect(&present, nx, ny, u->info.w, u->info.h);
  1116. for (k = 0; k < np; k++)
  1117. add_rect(&present, pieces[k].x, pieces[k].y,
  1118. pieces[k].w, pieces[k].h);
  1119. wm_overlays(&present);
  1120. u_wm_present(&present);
  1121. }
  1122. static int iabs(int v)
  1123. {
  1124. return v < 0 ? -v : v;
  1125. }
  1126. /* Repaint the old and new rectangles of a geometry change. */
  1127. static void redraw_geom(struct uwin *u, int ox, int oy,
  1128. unsigned int ow, unsigned int oh)
  1129. {
  1130. struct wm_present_info present;
  1131. unsigned int i;
  1132. present.count = 0;
  1133. present.immediate = 1;
  1134. add_rect(&present, ox, oy, ow, oh);
  1135. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  1136. if (u_wm_begin() == 0) {
  1137. for (i = 0; i < present.count; i++)
  1138. draw_rect(present.rect[i].x, present.rect[i].y,
  1139. present.rect[i].w, present.rect[i].h);
  1140. wm_overlays(&present);
  1141. u_wm_present(&present);
  1142. }
  1143. }
  1144. /* Resize (when needed), move and remap a window in one step. */
  1145. static int set_window_geometry(struct uwin *u, unsigned int idx,
  1146. int nx, int ny, unsigned int nw, unsigned int nh)
  1147. {
  1148. struct wm_window_info ni;
  1149. int ox = u->info.x, oy = u->info.y;
  1150. unsigned int ow = u->info.w, oh = u->info.h;
  1151. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  1152. if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
  1153. nh - WM_TITLE_H - 2 * WM_BORDER_W) != 0)
  1154. return -1;
  1155. }
  1156. if (nx != ox || ny != oy)
  1157. u_wm_set_geom(idx, nx, ny);
  1158. u->mapped = 0;
  1159. if (u_wm_info(idx, &ni) == 1)
  1160. u->info = ni;
  1161. if (u_wm_map(idx, &u->map) == 0)
  1162. u->mapped = 1;
  1163. redraw_geom(u, ox, oy, ow, oh);
  1164. return 0;
  1165. }
  1166. static int restore_window(struct uwin *u, unsigned int idx)
  1167. {
  1168. if (u->state == WIN_NORMAL)
  1169. return 0;
  1170. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  1171. u->restore_w, u->restore_h) != 0)
  1172. return -1;
  1173. u->state = WIN_NORMAL;
  1174. return 0;
  1175. }
  1176. /* Maximize or snap a window to a half of the work area below the system
  1177. * bar. The restore geometry is recorded only when the window leaves the
  1178. * normal state, so maximize -> snap -> restore returns to the original. */
  1179. static int place_window(struct uwin *u, unsigned int idx, int state)
  1180. {
  1181. unsigned int half = fbw / 2;
  1182. unsigned int h = fbh - WM_BAR_H;
  1183. int x = 0;
  1184. unsigned int w = fbw;
  1185. if (state == u->state)
  1186. return 0;
  1187. if (state == WIN_SNAP_LEFT) {
  1188. w = half;
  1189. } else if (state == WIN_SNAP_RIGHT) {
  1190. x = (int)half;
  1191. w = fbw - half;
  1192. }
  1193. if (u->state == WIN_NORMAL) {
  1194. u->restore_x = u->info.x;
  1195. u->restore_y = u->info.y;
  1196. u->restore_w = u->info.w;
  1197. u->restore_h = u->info.h;
  1198. }
  1199. if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
  1200. return -1;
  1201. u->state = state;
  1202. return 0;
  1203. }
  1204. /* Double-click title bar: fill the work area below the system bar, or go back
  1205. * to the geometry the window had before it was maximized. A snapped window
  1206. * maximizes (keeping its pre-snap restore geometry). */
  1207. static int toggle_maximize(struct uwin *u, unsigned int idx)
  1208. {
  1209. if (u->state == WIN_MAXIMIZED)
  1210. return restore_window(u, idx);
  1211. return place_window(u, idx, WIN_MAXIMIZED);
  1212. }
  1213. /* One axis of edge resistance. lo/len is the window's span on that axis
  1214. * before the move, *pos the proposed start. dir < 0: the window's start side
  1215. * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
  1216. * its end side (right/bottom) while moving higher. Within WM_RESIST pixels
  1217. * of overshoot the window stays on the edge; *best keeps the nearest edge. */
  1218. static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
  1219. {
  1220. int over;
  1221. if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
  1222. over = e - *pos;
  1223. if (over <= WM_RESIST && over < *best) {
  1224. *best = over;
  1225. *pos = e;
  1226. }
  1227. } else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
  1228. over = *pos + len - e;
  1229. if (over <= WM_RESIST && over < *best) {
  1230. *best = over;
  1231. *pos = e - len;
  1232. }
  1233. }
  1234. }
  1235. /* Span overlap test: edges of another window only resist when the two
  1236. * windows share some extent on the other axis (they would actually touch). */
  1237. static int spans_overlap(int a, int alen, int b, int blen)
  1238. {
  1239. return a < b + blen && b < a + alen;
  1240. }
  1241. /* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
  1242. * screen edges and the facing edges of every other window. The proposed
  1243. * position is still pure pointer arithmetic, so once the overshoot passes
  1244. * WM_RESIST the window jumps to follow the pointer again. */
  1245. static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
  1246. {
  1247. int ox = u->info.x, oy = u->info.y;
  1248. int w = (int)u->info.w, h = (int)u->info.h;
  1249. int px = *nx, py = *ny;
  1250. int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
  1251. int i;
  1252. resist_edge(ox, w, &px, 0, -1, &bestx);
  1253. resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
  1254. resist_edge(oy, h, &py, (int)fbh, 1, &besty);
  1255. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1256. struct wm_window_info *o = &wins[i].info;
  1257. int t;
  1258. if (i == idx || !o->used)
  1259. continue;
  1260. if (spans_overlap(oy, h, o->y, (int)o->h)) {
  1261. /* our left side against its right, our right
  1262. * side against its left */
  1263. t = *nx;
  1264. resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
  1265. resist_edge(ox, w, &t, o->x, 1, &bestx);
  1266. if (t != *nx)
  1267. px = t;
  1268. }
  1269. if (spans_overlap(ox, w, o->x, (int)o->w)) {
  1270. t = *ny;
  1271. resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
  1272. resist_edge(oy, h, &t, o->y, 1, &besty);
  1273. if (t != *ny)
  1274. py = t;
  1275. }
  1276. }
  1277. *nx = px;
  1278. *ny = py;
  1279. }
  1280. /* The top-bar date/time text is also a button: it opens the Date & Time
  1281. * editor. The rectangle is recorded by draw_bar(). */
  1282. /* The whole right status cluster is one button: the clock, temperature,
  1283. * network and power items plus the gaps between them. Only the rectangle
  1284. * draw_bar() recorded is clickable, so the empty bar between the start
  1285. * button and the cluster does nothing. */
  1286. static int bar_status_hit(unsigned int mx, unsigned int my)
  1287. {
  1288. return bar_status_w && bar_status_x >= 0 &&
  1289. (int)my >= BAR_BTN_Y && (int)my < BAR_BTN_Y + BAR_BTN_H &&
  1290. (int)mx >= bar_status_x &&
  1291. (int)mx < bar_status_x + (int)bar_status_w;
  1292. }
  1293. static void sm_layout_get(struct sm_layout *l)
  1294. {
  1295. int avail = (int)fbh - WM_BAR_H - 8;
  1296. int row = SM_ROW_H;
  1297. int total = SM_TOP + SM_SECT_H + SM_NAPP * row + SM_GAP + row +
  1298. SM_BOTTOM;
  1299. /* Shrink the rows to the work area, so a 540-600 px panel still fits
  1300. * the whole list. The low bound is the smallest legible row. */
  1301. while (row > SM_MIN_ROW && total > avail) {
  1302. row--;
  1303. total = SM_TOP + SM_SECT_H + SM_NAPP * row + SM_GAP + row +
  1304. SM_BOTTOM;
  1305. }
  1306. l->x = 4;
  1307. l->y = WM_BAR_H + 2;
  1308. l->w = SM_W;
  1309. l->h = total;
  1310. l->row_h = row;
  1311. l->sect_y = l->y + SM_TOP;
  1312. l->row_x = l->x + SM_PAD;
  1313. l->row_w = l->w - 2 * SM_PAD;
  1314. l->list_y = l->sect_y + SM_SECT_H;
  1315. l->power_y = l->list_y + SM_NAPP * row + SM_GAP;
  1316. l->sub_w = SM_SUB_W;
  1317. l->sub_h = 2 * row + 8;
  1318. l->sub_x = l->x + l->w + 4;
  1319. if (l->sub_x + l->sub_w > (int)fbw - 2) {
  1320. if (l->x - l->sub_w - 4 >= 0)
  1321. l->sub_x = l->x - l->sub_w - 4;
  1322. else
  1323. l->sub_x = (int)fbw - 2 - l->sub_w;
  1324. }
  1325. /* The power row is the last menu row: open the submenu upwards and
  1326. * align its bottom with that row. */
  1327. l->sub_y = l->power_y + row - l->sub_h;
  1328. if (l->sub_y < WM_BAR_H + 2)
  1329. l->sub_y = WM_BAR_H + 2;
  1330. if (l->sub_y + l->sub_h > (int)fbh - 2)
  1331. l->sub_y = (int)fbh - 2 - l->sub_h;
  1332. if (l->sub_y < 0)
  1333. l->sub_y = 0;
  1334. }
  1335. static void sm_tile(int x, int y, int size, unsigned int rgb, char letter)
  1336. {
  1337. char s[2];
  1338. int tw, tx, ty;
  1339. if (size < 12)
  1340. size = 12;
  1341. s[0] = letter;
  1342. s[1] = '\0';
  1343. sm_fill_round(x, y, size, size, 4, rgb);
  1344. tw = (int)usurf_text_width(&screen, s);
  1345. tx = x + (size - tw) / 2;
  1346. ty = y + (size - 16) / 2;
  1347. usurf_text(&screen, tx, ty, s, COL_WHITE,
  1348. x, y, (unsigned int)size, (unsigned int)size);
  1349. }
  1350. static void sm_draw_app(int i, const struct sm_layout *l)
  1351. {
  1352. const struct sm_app *a = &sm_apps[i];
  1353. int ry = l->list_y + i * l->row_h;
  1354. int ty = ry + (l->row_h - 16) / 2;
  1355. int tile = l->row_h - 8;
  1356. if (tile > SM_TILE)
  1357. tile = SM_TILE;
  1358. if (sm_hot == i)
  1359. sm_fill_round(l->row_x, ry + 1, l->row_w,
  1360. l->row_h - 2, SM_RADIUS, SM_PANEL_ALT);
  1361. sm_tile(l->row_x + 10, ry + (l->row_h - tile) / 2, tile,
  1362. a->tile, a->letter);
  1363. usurf_text(&screen, l->row_x + 36, ty, a->label, SM_TEXT,
  1364. l->x, l->y, (unsigned int)l->w, (unsigned int)l->h);
  1365. }
  1366. static void sm_draw(void)
  1367. {
  1368. struct sm_layout l;
  1369. int i, ty;
  1370. if (!sm_open)
  1371. return;
  1372. sm_layout_get(&l);
  1373. /* Flat theme.bg panel with no rim, exactly like the netmenu popover.
  1374. * The Applications caption is the first item: there is no separate
  1375. * banner card. */
  1376. usurf_fill(&screen, l.x, l.y, (unsigned int)l.w, (unsigned int)l.h,
  1377. SM_BG);
  1378. usurf_text(&screen, l.x + SM_PAD, l.sect_y + 3, "Applications",
  1379. SM_TEXT_DIM, l.x, l.y, (unsigned int)l.w,
  1380. (unsigned int)l.h);
  1381. for (i = 0; i < SM_NAPP; i++)
  1382. sm_draw_app(i, &l);
  1383. /* The Power row is a quick-settings tile rather than a plain list
  1384. * row, so the submenu entry reads as the panel's last action. */
  1385. sm_fill_round(l.row_x, l.power_y, l.row_w, l.row_h, SM_RADIUS,
  1386. sm_power_open ? SM_SEL :
  1387. (sm_power_hot == 1 ? SM_BTN_HOT : SM_PANEL_ALT));
  1388. ty = l.power_y + (l.row_h - 16) / 2;
  1389. usurf_text(&screen, l.row_x + 12, ty, "Power",
  1390. sm_power_open || sm_power_hot == 1 ? COL_WHITE : SM_TEXT,
  1391. l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  1392. {
  1393. const char *arrow = sm_power_open ? "v" : ">";
  1394. int aw = (int)usurf_text_width(&screen, arrow);
  1395. usurf_text(&screen, l.row_x + l.row_w - 12 - aw, ty, arrow,
  1396. sm_power_open || sm_power_hot == 1 ? COL_WHITE :
  1397. SM_TEXT_DIM,
  1398. l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  1399. }
  1400. if (!sm_power_open)
  1401. return;
  1402. /* Submenu: a rounded panel card with two rounded selection rows. */
  1403. sm_fill_round(l.sub_x, l.sub_y, l.sub_w, l.sub_h, SM_RADIUS,
  1404. SM_PANEL);
  1405. for (i = 0; i < 2; i++) {
  1406. int sy = l.sub_y + 4 + i * l.row_h;
  1407. int sty = sy + (l.row_h - 16) / 2;
  1408. if (sm_power_hot == i + 2)
  1409. sm_fill_round(l.sub_x + 4, sy + 1, l.sub_w - 8,
  1410. l.row_h - 2, 4, SM_SEL);
  1411. usurf_text(&screen, l.sub_x + 12, sty,
  1412. i == 0 ? "Reboot" : "Power off",
  1413. i == 1 ? (sm_power_hot == 3 ? COL_WHITE : SM_ERROR)
  1414. : (sm_power_hot == 2 ? COL_WHITE : SM_TEXT),
  1415. l.sub_x, l.sub_y, (unsigned int)l.sub_w,
  1416. (unsigned int)l.sub_h);
  1417. }
  1418. }
  1419. static int sm_hit(unsigned int mx, unsigned int my)
  1420. {
  1421. struct sm_layout l;
  1422. int x = (int)mx, y = (int)my;
  1423. if (bar_start_hit(mx, my))
  1424. return SM_HIT_START;
  1425. if (!sm_open)
  1426. return SM_HIT_NONE;
  1427. sm_layout_get(&l);
  1428. if (sm_power_open &&
  1429. x >= l.sub_x && x < l.sub_x + l.sub_w &&
  1430. y >= l.sub_y && y < l.sub_y + l.sub_h) {
  1431. if (y >= l.sub_y + 4 && y < l.sub_y + 4 + 2 * l.row_h)
  1432. return (y - (l.sub_y + 4)) / l.row_h == 0 ?
  1433. SM_HIT_REBOOT : SM_HIT_POWEROFF;
  1434. return SM_HIT_MENU;
  1435. }
  1436. if (x < l.x || x >= l.x + l.w || y < l.y || y >= l.y + l.h)
  1437. return SM_HIT_NONE;
  1438. if (y >= l.list_y && y < l.list_y + SM_NAPP * l.row_h)
  1439. return (y - l.list_y) / l.row_h;
  1440. if (y >= l.power_y && y < l.power_y + l.row_h)
  1441. return SM_HIT_POWER;
  1442. return SM_HIT_MENU;
  1443. }
  1444. static int sm_update_hover(unsigned int mx, unsigned int my)
  1445. {
  1446. int hot = -1, pwr = 0, hit;
  1447. if (sm_open) {
  1448. hit = sm_hit(mx, my);
  1449. if (hit >= 0)
  1450. hot = hit;
  1451. else if (hit == SM_HIT_POWER)
  1452. pwr = 1;
  1453. else if (hit == SM_HIT_REBOOT)
  1454. pwr = 2;
  1455. else if (hit == SM_HIT_POWEROFF)
  1456. pwr = 3;
  1457. }
  1458. if (hot == sm_hot && pwr == sm_power_hot)
  1459. return 0;
  1460. sm_hot = hot;
  1461. sm_power_hot = pwr;
  1462. return 1;
  1463. }
  1464. static void sm_add_present(struct wm_present_info *p)
  1465. {
  1466. struct sm_layout l;
  1467. if (!sm_open)
  1468. return;
  1469. sm_layout_get(&l);
  1470. add_rect(p, l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  1471. if (sm_power_open)
  1472. add_rect(p, l.sub_x, l.sub_y, (unsigned int)l.sub_w,
  1473. (unsigned int)l.sub_h);
  1474. }
  1475. static void sm_close(void)
  1476. {
  1477. if (!sm_open && !sm_power_open)
  1478. return;
  1479. sm_open = 0;
  1480. sm_power_open = 0;
  1481. sm_hot = -1;
  1482. sm_power_hot = 0;
  1483. full_redraw = 1;
  1484. }
  1485. static void do_update(struct wm_status *st)
  1486. {
  1487. struct wm_present_info present;
  1488. int need = 0;
  1489. int bar_dirty = 0;
  1490. unsigned int i;
  1491. char dt[20];
  1492. static char last_dt[20];
  1493. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  1494. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  1495. unsigned int msg_now = u_ticks_ms();
  1496. int osd_hide = 0, osd_repaint = 0;
  1497. int sm_dirty;
  1498. /* The kernel bumps backlight_seq on every brightness key/command.
  1499. * Pop the OSD on a change and keep it up for OSD_MS after the last
  1500. * one. The first poll only records the current sequence, so a WM
  1501. * restart does not flash a stale bubble. */
  1502. if (st->backlight_present) {
  1503. if (!osd_seen_valid) {
  1504. osd_seen_valid = 1;
  1505. osd_seen_seq = st->backlight_seq;
  1506. } else if (st->backlight_seq != osd_seen_seq) {
  1507. osd_seen_seq = st->backlight_seq;
  1508. osd_level = (int)st->backlight_level;
  1509. osd_until = msg_now + OSD_MS;
  1510. osd_show = 1;
  1511. }
  1512. } else if (osd_show) {
  1513. osd_show = 0;
  1514. osd_hide = 1;
  1515. }
  1516. if (osd_show && (int)(msg_now - osd_until) >= 0) {
  1517. osd_show = 0;
  1518. osd_hide = 1;
  1519. }
  1520. if (osd_hide && osd_visible)
  1521. osd_repaint = 1;
  1522. sm_dirty = sm_update_hover(st->mouse.x, st->mouse.y);
  1523. /* The boot-scan window is created before the terminal so the terminal
  1524. * stays the newest (last) window and keeps the keyboard focus. Wait
  1525. * for the scan window to exist before starting the terminal; a failed
  1526. * or wedged helper is bounded so the shell is never held back. */
  1527. if (st->boot_ready && !boot_term_spawned) {
  1528. if (!boot_scan_spawned) {
  1529. boot_scan_spawned = 1;
  1530. if (free_window_slot()) {
  1531. boot_scan_pid = u_spawn("/ramfs/bin/bootscan",
  1532. "", 0, "/");
  1533. if (boot_scan_pid > 0)
  1534. boot_scan_deadline = msg_now + 2000u;
  1535. }
  1536. if (boot_scan_pid <= 0)
  1537. boot_scan_placed = 1;
  1538. }
  1539. if (boot_scan_pid > 0 && !boot_scan_placed) {
  1540. struct wm_window_info wi;
  1541. int idx = window_index_pid(boot_scan_pid, &wi);
  1542. if (idx >= 0) {
  1543. int nx = (int)fbw - (int)wi.w - 16;
  1544. int ny = WM_BAR_H + 8;
  1545. if (nx < 8)
  1546. nx = 8;
  1547. u_wm_set_geom((unsigned int)idx, nx, ny);
  1548. boot_scan_placed = 1;
  1549. } else if (!u_alive((unsigned int)boot_scan_pid) ||
  1550. (int)(msg_now - boot_scan_deadline) >= 0) {
  1551. if (u_alive((unsigned int)boot_scan_pid))
  1552. u_kill((unsigned int)boot_scan_pid);
  1553. boot_scan_placed = 1;
  1554. }
  1555. }
  1556. if (boot_scan_placed) {
  1557. boot_term_spawned = 1;
  1558. if (!free_window_slot()) {
  1559. bar_notice("window limit");
  1560. full_redraw = 1;
  1561. } else if (u_wm_create_term() < 0) {
  1562. bar_notice("cannot start term");
  1563. full_redraw = 1;
  1564. } else {
  1565. full_redraw = 1;
  1566. }
  1567. }
  1568. }
  1569. if (bar_msg_dirty ||
  1570. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  1571. bar_dirty = 1;
  1572. bar_msg_dirty = 0;
  1573. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  1574. bar_msg_until = 0;
  1575. }
  1576. bar_kexec_busy = st->kexec_busy;
  1577. bar_kexec_progress = st->kexec_progress;
  1578. bar_kexec_total = st->kexec_total;
  1579. if (kexec_changed)
  1580. bar_dirty = 1;
  1581. {
  1582. int hot = bar_start_hit(st->mouse.x, st->mouse.y);
  1583. if (hot != sm_start_hot) {
  1584. sm_start_hot = hot;
  1585. bar_dirty = 1;
  1586. }
  1587. }
  1588. {
  1589. int hot = bar_status_hit(st->mouse.x, st->mouse.y);
  1590. if (hot != bar_status_hot) {
  1591. bar_status_hot = hot;
  1592. bar_dirty = 1;
  1593. }
  1594. }
  1595. /* Refresh cached window state and mappings before opening a frame:
  1596. * MAP may unmap/remap content. */
  1597. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1598. struct wm_window_info info;
  1599. int r = u_wm_info(i, &info);
  1600. if (r < 0) {
  1601. u_exit(1);
  1602. }
  1603. if (r == 0 || !info.used) {
  1604. if (wins[i].info.used)
  1605. full_redraw = 1;
  1606. wins[i].info.used = 0;
  1607. wins[i].mapped = 0;
  1608. continue;
  1609. }
  1610. {
  1611. int pid_changed = wins[i].info.used &&
  1612. info.pid != wins[i].info.pid;
  1613. if (!wins[i].info.used ||
  1614. info.content_gen != wins[i].info.content_gen ||
  1615. pid_changed)
  1616. full_redraw = 1;
  1617. wins[i].info = info;
  1618. if (!wins[i].mapped || pid_changed ||
  1619. wins[i].map.generation != info.content_gen) {
  1620. struct wm_map_info map;
  1621. if (u_wm_map(i, &map) == 0) {
  1622. wins[i].map = map;
  1623. wins[i].mapped = 1;
  1624. } else {
  1625. wins[i].mapped = 0;
  1626. }
  1627. }
  1628. }
  1629. }
  1630. /* Popovers are WM-anchored overlays, flush against the bottom of the
  1631. * bar and the right screen edge. The WM owns the geometry, so the app
  1632. * does not need a placement ABI. */
  1633. {
  1634. int control_found = 0;
  1635. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1636. struct wm_window_info ni;
  1637. int nx, ny;
  1638. if (!wins[i].info.used || !wins[i].info.popover)
  1639. continue;
  1640. if (control_pid > 0 &&
  1641. wins[i].info.pid == (unsigned int)control_pid)
  1642. control_found = 1;
  1643. nx = (int)fbw - (int)wins[i].info.w;
  1644. ny = WM_BAR_H;
  1645. if (nx != wins[i].info.x || ny != wins[i].info.y) {
  1646. if (u_wm_set_geom(i, nx, ny) == 0 &&
  1647. u_wm_info(i, &ni) == 1) {
  1648. wins[i].info = ni;
  1649. full_redraw = 1;
  1650. }
  1651. }
  1652. }
  1653. if (!control_found && control_pid > 0 &&
  1654. !u_alive((unsigned int)control_pid)) {
  1655. control_pid = 0;
  1656. bar_dirty = 1;
  1657. }
  1658. }
  1659. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  1660. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  1661. {
  1662. static unsigned int last_rtc_ms;
  1663. unsigned int now_ms = u_ticks_ms();
  1664. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  1665. last_rtc_ms = now_ms;
  1666. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  1667. for (i = 0; i < sizeof(dt); i++)
  1668. if (dt[i] != last_dt[i]) {
  1669. for (i = 0; i < sizeof(dt); i++)
  1670. last_dt[i] = dt[i];
  1671. bar_dirty = 1;
  1672. break;
  1673. }
  1674. }
  1675. {
  1676. struct sys_cputemp ct;
  1677. if (u_cpu_temp(&ct) == 0 &&
  1678. ct.temp_c != bar_temp_c) {
  1679. bar_temp_c = ct.temp_c;
  1680. bar_dirty = 1;
  1681. }
  1682. }
  1683. {
  1684. struct sys_powerinfo pi;
  1685. int pct = -1, charging = 0, ac = -1;
  1686. unsigned int bi;
  1687. if (u_power_info(&pi, 0) == 0) {
  1688. /* A laptop EC (ThinkPad or Chrome
  1689. * EC) marks a machine with AC/battery;
  1690. * desktops show neither. */
  1691. if (pi.present) {
  1692. ac = pi.ac_online;
  1693. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  1694. bi++) {
  1695. struct sys_battery *b =
  1696. &pi.battery[bi];
  1697. if (!b->present ||
  1698. !b->percent_valid)
  1699. continue;
  1700. pct = (int)b->percent;
  1701. charging = (int)b->charging;
  1702. break;
  1703. }
  1704. }
  1705. if (pct != bar_batt_pct ||
  1706. charging != bar_batt_charging ||
  1707. ac != bar_ac_online) {
  1708. bar_batt_pct = pct;
  1709. bar_batt_charging = charging;
  1710. bar_ac_online = ac;
  1711. bar_dirty = 1;
  1712. }
  1713. }
  1714. }
  1715. if (poll_net_status())
  1716. bar_dirty = 1;
  1717. }
  1718. }
  1719. if (full_redraw) {
  1720. if (u_wm_begin() != 0) {
  1721. full_redraw = 1;
  1722. return;
  1723. }
  1724. full_redraw = 0;
  1725. need = 1;
  1726. {
  1727. draw_rect(0, 0, fbw, fbh);
  1728. draw_bar();
  1729. if (osd_show)
  1730. osd_draw();
  1731. osd_visible = osd_show;
  1732. sm_draw();
  1733. present.count = 0;
  1734. add_rect(&present, 0, 0, fbw, fbh);
  1735. present.immediate = 0;
  1736. u_wm_present(&present);
  1737. /* Ack every window that was clean at the time of the
  1738. * redraw. If new damage arrived during it, its seq
  1739. * differs and the kernel leaves it dirty. */
  1740. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1741. if (wins[i].info.used && wins[i].mapped)
  1742. u_wm_ack(i, wins[i].info.dirty_seq);
  1743. }
  1744. return;
  1745. }
  1746. /* Partial cell/canvas updates and the bar clock. */
  1747. present.count = 0;
  1748. present.immediate = 0;
  1749. if (osd_repaint) {
  1750. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1751. need = 1;
  1752. }
  1753. if (osd_show)
  1754. need = 1;
  1755. if (bar_dirty) {
  1756. need = 1;
  1757. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1758. }
  1759. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1760. struct uwin *u = &wins[i];
  1761. struct wm_window_info *w = &u->info;
  1762. int rx, ry;
  1763. unsigned int rw, rh;
  1764. if (!w->used || !w->dirty)
  1765. continue;
  1766. if (!w->dmg_w || !w->dmg_h)
  1767. continue;
  1768. rx = w->x + (w->popover ? 0 : WM_BORDER_W) + (int)w->dmg_x;
  1769. ry = w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H) +
  1770. (int)w->dmg_y;
  1771. rw = w->dmg_w;
  1772. rh = w->dmg_h;
  1773. add_rect(&present, rx, ry, rw, rh);
  1774. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1775. need = 1;
  1776. }
  1777. if (sm_dirty && sm_open)
  1778. need = 1;
  1779. if (!need) {
  1780. /* Drag movement is handled below; a stationary mouse can be
  1781. * left to the kernel's cursor overlay. */
  1782. return;
  1783. }
  1784. if (sm_open)
  1785. sm_add_present(&present);
  1786. if (u_wm_begin() != 0)
  1787. return;
  1788. for (i = 0; i < present.count; i++)
  1789. draw_rect(present.rect[i].x, present.rect[i].y,
  1790. present.rect[i].w, present.rect[i].h);
  1791. if (bar_dirty)
  1792. draw_bar();
  1793. if (osd_show) {
  1794. osd_draw();
  1795. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1796. }
  1797. osd_visible = osd_show;
  1798. sm_draw();
  1799. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1800. struct uwin *u = &wins[i];
  1801. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1802. !u->ack_seq)
  1803. continue;
  1804. if (u->ack_seq == u->info.dirty_seq)
  1805. u_wm_ack(i, u->info.dirty_seq);
  1806. }
  1807. u_wm_present(&present);
  1808. }
  1809. static void spawn_app_arg_checked(const char *path, const char *arg)
  1810. {
  1811. if (!free_window_slot()) {
  1812. bar_notice("window limit");
  1813. full_redraw = 1;
  1814. return;
  1815. }
  1816. if (u_spawn(path, arg, 0, "/") < 0) {
  1817. bar_notice("spawn failed");
  1818. full_redraw = 1;
  1819. } else {
  1820. full_redraw = 1;
  1821. }
  1822. }
  1823. /* Any status target opens one control panel; a second click closes it. */
  1824. static void open_control_panel(void)
  1825. {
  1826. unsigned int i;
  1827. if (control_pid > 0 && u_alive((unsigned int)control_pid)) {
  1828. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1829. if (wins[i].info.used &&
  1830. wins[i].info.pid == (unsigned int)control_pid) {
  1831. if (u_wm_close(i) == 0) {
  1832. control_pid = 0;
  1833. full_redraw = 1;
  1834. } else {
  1835. u_wm_focus(i);
  1836. full_redraw = 1;
  1837. }
  1838. return;
  1839. }
  1840. /* The process is alive but has not created its window yet. */
  1841. return;
  1842. }
  1843. if (!free_window_slot()) {
  1844. bar_notice("window limit");
  1845. full_redraw = 1;
  1846. return;
  1847. }
  1848. control_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  1849. if (control_pid < 0) {
  1850. control_pid = 0;
  1851. bar_notice("spawn failed");
  1852. full_redraw = 1;
  1853. } else {
  1854. full_redraw = 1;
  1855. }
  1856. }
  1857. /* Launch one start-menu entry. The terminal and the control panel are WM
  1858. * actions; every other entry spawns its ELF from the pack. */
  1859. static void sm_launch(int idx)
  1860. {
  1861. const struct sm_app *a;
  1862. if (idx < 0 || idx >= SM_NAPP) {
  1863. sm_close();
  1864. return;
  1865. }
  1866. a = &sm_apps[idx];
  1867. sm_close();
  1868. if (a->action == SM_ACT_TERM) {
  1869. if (!free_window_slot()) {
  1870. bar_notice("window limit");
  1871. full_redraw = 1;
  1872. } else if (u_wm_create_term() < 0) {
  1873. bar_notice("cannot start term");
  1874. full_redraw = 1;
  1875. } else {
  1876. full_redraw = 1;
  1877. }
  1878. } else if (a->action == SM_ACT_SETTINGS) {
  1879. open_control_panel();
  1880. } else {
  1881. spawn_app_arg_checked(a->path, a->arg);
  1882. }
  1883. }
  1884. /* Index of a popover containing the point, or -1. */
  1885. static int popover_under(unsigned int mx, unsigned int my)
  1886. {
  1887. unsigned int i;
  1888. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1889. struct wm_window_info *w = &wins[i].info;
  1890. if (w->used && w->popover &&
  1891. mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  1892. mx < (unsigned int)(w->x + (int)w->w) &&
  1893. my < (unsigned int)(w->y + (int)w->h))
  1894. return (int)i;
  1895. }
  1896. return -1;
  1897. }
  1898. /* A press anywhere outside an open popover dismisses it; the click then
  1899. * continues normally so the target under the pointer still reacts. */
  1900. static void dismiss_popover_outside(unsigned int mx, unsigned int my)
  1901. {
  1902. unsigned int i;
  1903. if (popover_under(mx, my) >= 0)
  1904. return;
  1905. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1906. if (!wins[i].info.used || !wins[i].info.popover)
  1907. continue;
  1908. if (wins[i].info.pid == (unsigned int)control_pid)
  1909. control_pid = 0;
  1910. if (u_wm_close(i) == 0) {
  1911. wins[i].info.used = 0;
  1912. wins[i].mapped = 0;
  1913. full_redraw = 1;
  1914. }
  1915. break;
  1916. }
  1917. }
  1918. /* Apply one edge-drag step. The grabbed edges follow the pointer while the
  1919. * opposite edges stay put; the work area clamps the active edges. */
  1920. static void resize_window_drag(struct uwin *u, unsigned int idx,
  1921. unsigned int mx, unsigned int my)
  1922. {
  1923. int dx = (int)mx - (int)resize_mx;
  1924. int dy = (int)my - (int)resize_my;
  1925. int left = resize_ox;
  1926. int right = resize_ox + (int)resize_ow;
  1927. int top = resize_oy;
  1928. int bottom = resize_oy + (int)resize_oh;
  1929. int nx, ny, nw, nh;
  1930. if (resize_edges & WM_EDGE_LEFT) {
  1931. left = resize_ox + dx;
  1932. if (left < 0)
  1933. left = 0;
  1934. } else if (resize_edges & WM_EDGE_RIGHT) {
  1935. right = resize_ox + (int)resize_ow + dx;
  1936. if (right > (int)fbw)
  1937. right = (int)fbw;
  1938. }
  1939. if (resize_edges & WM_EDGE_TOP) {
  1940. top = resize_oy + dy;
  1941. if (top < WM_BAR_H)
  1942. top = WM_BAR_H;
  1943. } else if (resize_edges & WM_EDGE_BOTTOM) {
  1944. bottom = resize_oy + (int)resize_oh + dy;
  1945. if (bottom > (int)fbh)
  1946. bottom = (int)fbh;
  1947. }
  1948. if ((resize_edges & (WM_EDGE_LEFT | WM_EDGE_RIGHT)) &&
  1949. right - left < 200) {
  1950. if (resize_edges & WM_EDGE_LEFT)
  1951. left = right - 200;
  1952. else
  1953. right = left + 200;
  1954. }
  1955. if ((resize_edges & (WM_EDGE_TOP | WM_EDGE_BOTTOM)) &&
  1956. bottom - top < 140) {
  1957. if (resize_edges & WM_EDGE_TOP)
  1958. top = bottom - 140;
  1959. else
  1960. bottom = top + 140;
  1961. }
  1962. if (left < 0)
  1963. left = 0;
  1964. if (top < WM_BAR_H)
  1965. top = WM_BAR_H;
  1966. nx = left;
  1967. ny = top;
  1968. nw = right - left;
  1969. nh = bottom - top;
  1970. if (nw > (int)fbw)
  1971. nw = (int)fbw;
  1972. if (nh > (int)fbh - WM_BAR_H)
  1973. nh = (int)fbh - WM_BAR_H;
  1974. if (nx == u->info.x && ny == u->info.y &&
  1975. nw == (int)u->info.w && nh == (int)u->info.h)
  1976. return;
  1977. if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
  1978. (unsigned int)nh) == 0)
  1979. u->state = WIN_NORMAL;
  1980. }
  1981. /* Capture the pointer for an edge resize that started at (mx,my). */
  1982. static void begin_resize(unsigned int idx, int edges, unsigned int mx,
  1983. unsigned int my)
  1984. {
  1985. struct uwin *u = &wins[idx];
  1986. resize_id = (int)idx;
  1987. resize_edges = edges;
  1988. resize_mx = mx;
  1989. resize_my = my;
  1990. resize_ox = u->info.x;
  1991. resize_oy = u->info.y;
  1992. resize_ow = u->info.w;
  1993. resize_oh = u->info.h;
  1994. u_wm_mouse_capture(1);
  1995. }
  1996. /* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the
  1997. * invisible resize border and while resizing, the move cross while a
  1998. * title-bar drag is under way, a hand over the start button, the open start
  1999. * menu and the status area. Everywhere else the kernel picks the window's
  2000. * own content cursor (or the arrow). */
  2001. static void update_cursor(unsigned int mx, unsigned int my)
  2002. {
  2003. static int current = CURSOR_NO_OVERRIDE;
  2004. int want = CURSOR_NO_OVERRIDE;
  2005. if (resize_id >= 0) {
  2006. want = resize_cursor(resize_edges);
  2007. } else if (drag_id >= 0) {
  2008. if (drag_moved)
  2009. want = CURSOR_MOVE;
  2010. } else if (sm_open) {
  2011. /* The menu is modal: a hand over its items, the arrow
  2012. * elsewhere, never a window's content cursor underneath. */
  2013. int hit = sm_hit(mx, my);
  2014. want = (hit == SM_HIT_NONE || hit == SM_HIT_MENU) ?
  2015. CURSOR_DEFAULT : CURSOR_POINTER;
  2016. } else if (my < WM_BAR_H) {
  2017. if (bar_start_hit(mx, my) || bar_status_hit(mx, my))
  2018. want = CURSOR_POINTER;
  2019. } else {
  2020. int edges = 0;
  2021. int idx = resize_hit((int)mx, (int)my, &edges);
  2022. if (idx >= 0 && edges &&
  2023. !close_hit(&wins[idx].info, (int)mx, (int)my))
  2024. want = resize_cursor(edges);
  2025. }
  2026. if (want != current && u_wm_set_cursor(want) == 0)
  2027. current = want;
  2028. }
  2029. static void handle_mouse(struct wm_status *st)
  2030. {
  2031. unsigned int buttons = st->mouse.buttons;
  2032. unsigned int mx = st->mouse.x, my = st->mouse.y;
  2033. int down = (buttons & 1) != 0;
  2034. int was = (prev_buttons & 1) != 0;
  2035. if (down && !was) {
  2036. int prev_title_id = last_title_id;
  2037. unsigned int now = u_ticks_ms();
  2038. int hit;
  2039. /* The start menu is modal: a press on an entry launches it,
  2040. * a press on Power opens its submenu, and any other press
  2041. * dismisses the menu without reaching the window below. */
  2042. if (sm_open) {
  2043. hit = sm_hit(mx, my);
  2044. last_title_id = -1;
  2045. if (hit == SM_HIT_START) {
  2046. sm_close();
  2047. } else if (hit == SM_HIT_POWER) {
  2048. sm_power_open = !sm_power_open;
  2049. full_redraw = 1;
  2050. } else if (hit == SM_HIT_REBOOT) {
  2051. sm_close();
  2052. if (u_spawn_kcmd("reboot", "", 0) < 0)
  2053. bar_notice("reboot failed");
  2054. } else if (hit == SM_HIT_POWEROFF) {
  2055. sm_close();
  2056. if (u_spawn_kcmd("poweroff", "", 0) < 0)
  2057. bar_notice("poweroff failed");
  2058. } else if (hit >= 0) {
  2059. sm_launch(hit);
  2060. } else if (hit != SM_HIT_MENU) {
  2061. sm_close();
  2062. }
  2063. goto mouse_done;
  2064. }
  2065. if (bar_start_hit(mx, my)) {
  2066. /* Opening the menu also closes an open control
  2067. * panel, so only one floating panel is visible. */
  2068. dismiss_popover_outside(mx, my);
  2069. last_title_id = -1;
  2070. sm_open = 1;
  2071. sm_power_open = 0;
  2072. sm_hot = -1;
  2073. sm_power_hot = 0;
  2074. full_redraw = 1;
  2075. goto mouse_done;
  2076. }
  2077. if (!bar_status_hit(mx, my))
  2078. dismiss_popover_outside(mx, my);
  2079. last_title_id = -1;
  2080. if (bar_status_hit(mx, my)) {
  2081. open_control_panel();
  2082. } else {
  2083. int edges = 0;
  2084. int idx = resize_hit((int)mx, (int)my, &edges);
  2085. if (idx >= 0) {
  2086. struct uwin *u = &wins[idx];
  2087. int pmx = (int)mx, pmy = (int)my;
  2088. u_wm_focus((unsigned int)idx);
  2089. if (u->info.popover) {
  2090. /* A popover has no title bar, close
  2091. * button or resize border; the app owns
  2092. * its content. */
  2093. } else if (close_hit(&u->info, pmx, pmy)) {
  2094. u_wm_close((unsigned int)idx);
  2095. full_redraw = 1;
  2096. } else if (edges) {
  2097. begin_resize((unsigned int)idx, edges,
  2098. mx, my);
  2099. } else if (pmy < u->info.y + WM_TITLE_H) {
  2100. if (prev_title_id == idx &&
  2101. now - last_title_ms <= WM_DBLCLICK_MS &&
  2102. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  2103. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  2104. last_title_ms = 0;
  2105. if (toggle_maximize(u,
  2106. (unsigned int)idx) != 0)
  2107. bar_notice("resize failed");
  2108. } else {
  2109. last_title_id = idx;
  2110. last_title_ms = now;
  2111. last_title_x = pmx;
  2112. last_title_y = pmy;
  2113. drag_id = idx;
  2114. drag_mx = pmx;
  2115. drag_my = pmy;
  2116. drag_dx = pmx - u->info.x;
  2117. drag_dy = pmy - u->info.y;
  2118. drag_moved = 0;
  2119. u_wm_mouse_capture(1);
  2120. }
  2121. }
  2122. }
  2123. }
  2124. } else if (down && was && resize_id >= 0) {
  2125. struct uwin *u = &wins[resize_id];
  2126. if (u->info.used)
  2127. resize_window_drag(u, (unsigned int)resize_id, mx, my);
  2128. else {
  2129. resize_id = -1;
  2130. u_wm_mouse_capture(0);
  2131. }
  2132. } else if (down && was && drag_id >= 0) {
  2133. struct uwin *u = &wins[drag_id];
  2134. if (u->info.used) {
  2135. int ox, oy, nx, ny;
  2136. if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  2137. iabs((int)my - drag_my) >= WM_DRAG_START)
  2138. drag_moved = 1;
  2139. /* A maximized or snapped window is restored as soon as
  2140. * the drag starts. Keep the grab point at the same
  2141. * fraction of the title bar, so the restored (narrower)
  2142. * window stays under the pointer. A click that does
  2143. * not move must leave it in place. */
  2144. if (u->state != WIN_NORMAL && drag_moved) {
  2145. unsigned int ow = u->info.w;
  2146. int gx = drag_mx - u->info.x;
  2147. int gy = drag_my - u->info.y;
  2148. if (restore_window(u,
  2149. (unsigned int)drag_id) == 0) {
  2150. if (ow)
  2151. gx = gx * (int)u->info.w /
  2152. (int)ow;
  2153. drag_dx = gx;
  2154. drag_dy = gy;
  2155. }
  2156. }
  2157. if (u->state == WIN_NORMAL) {
  2158. ox = u->info.x;
  2159. oy = u->info.y;
  2160. nx = (int)mx - drag_dx;
  2161. ny = (int)my - drag_dy;
  2162. resist_geom(u, drag_id, &nx, &ny);
  2163. clamp_geom(u, &nx, &ny);
  2164. if (nx != ox || ny != oy) {
  2165. struct wm_present_info present;
  2166. unsigned int i;
  2167. u_wm_set_geom((unsigned int)drag_id,
  2168. nx, ny);
  2169. u->info.x = nx;
  2170. u->info.y = ny;
  2171. if (u->info.focused) {
  2172. move_window_pixels(u, ox, oy,
  2173. nx, ny);
  2174. } else {
  2175. present.count = 0;
  2176. present.immediate = 1;
  2177. add_rect(&present, ox, oy,
  2178. u->info.w, u->info.h);
  2179. add_rect(&present, nx, ny,
  2180. u->info.w, u->info.h);
  2181. if (u_wm_begin() == 0) {
  2182. for (i = 0;
  2183. i < present.count; i++)
  2184. draw_rect(
  2185. present.rect[i].x,
  2186. present.rect[i].y,
  2187. present.rect[i].w,
  2188. present.rect[i].h);
  2189. wm_overlays(&present);
  2190. u_wm_present(&present);
  2191. }
  2192. }
  2193. }
  2194. }
  2195. } else {
  2196. drag_id = -1;
  2197. u_wm_mouse_capture(0);
  2198. }
  2199. } else if (!down && was) {
  2200. /* Dropping a window with the pointer on the left/right screen
  2201. * edge snaps it to that half; on the top edge it maximizes. */
  2202. if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&
  2203. wins[drag_id].state == WIN_NORMAL) {
  2204. struct uwin *u = &wins[drag_id];
  2205. int st = WIN_NORMAL;
  2206. if ((int)mx < WM_SNAP_EDGE)
  2207. st = WIN_SNAP_LEFT;
  2208. else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
  2209. st = WIN_SNAP_RIGHT;
  2210. else if ((int)my < WM_SNAP_EDGE)
  2211. st = WIN_MAXIMIZED;
  2212. if (st != WIN_NORMAL &&
  2213. place_window(u, (unsigned int)drag_id, st) != 0)
  2214. bar_notice("resize failed");
  2215. }
  2216. drag_id = -1;
  2217. resize_id = -1;
  2218. u_wm_mouse_capture(0);
  2219. }
  2220. mouse_done:
  2221. prev_buttons = buttons;
  2222. update_cursor(mx, my);
  2223. }
  2224. static char *put_str(char *p, const char *s)
  2225. {
  2226. while (*s)
  2227. *p++ = *s++;
  2228. return p;
  2229. }
  2230. static char *put_dec(char *p, unsigned int v)
  2231. {
  2232. char b[10];
  2233. unsigned int n = 0;
  2234. do {
  2235. b[n++] = (char)('0' + v % 10);
  2236. v /= 10;
  2237. } while (v);
  2238. while (n)
  2239. *p++ = b[--n];
  2240. return p;
  2241. }
  2242. /* Expose the last interval in /share so the target can be measured without
  2243. * a serial console. */
  2244. static void write_stats(void)
  2245. {
  2246. char b[80], *p = b;
  2247. unsigned int now = u_ticks_ms();
  2248. p = put_str(p, "t=");
  2249. p = put_dec(p, now / 1000);
  2250. p = put_str(p, "s frames=");
  2251. p = put_dec(p, stat_frames);
  2252. p = put_str(p, " ms=");
  2253. p = put_dec(p, stat_ms);
  2254. p = put_str(p, " max=");
  2255. p = put_dec(p, stat_max);
  2256. p = put_str(p, " px=");
  2257. p = put_dec(p, stat_px);
  2258. p = put_str(p, "\n");
  2259. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  2260. (int)(p - b));
  2261. }
  2262. void _start(const char *arg)
  2263. {
  2264. struct wm_screen_info si;
  2265. struct wm_status st;
  2266. unsigned int i;
  2267. (void)arg;
  2268. if (u_wm_attach(&si) != 0)
  2269. u_exit(1);
  2270. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  2271. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  2272. fbw = si.width;
  2273. fbh = si.height;
  2274. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2275. wins[i].info.used = 0;
  2276. wins[i].mapped = 0;
  2277. }
  2278. full_redraw = 1;
  2279. for (;;) {
  2280. unsigned int t0, t1;
  2281. if (u_wm_poll(&st) != 0)
  2282. u_exit(1);
  2283. if (st.full_redraw)
  2284. full_redraw = 1;
  2285. usurf_pixels = 0;
  2286. t0 = u_ticks_ms();
  2287. handle_mouse(&st);
  2288. do_update(&st);
  2289. t1 = u_ticks_ms();
  2290. if (usurf_pixels) {
  2291. unsigned int dt = t1 - t0;
  2292. stat_frames++;
  2293. stat_ms += dt;
  2294. stat_px += usurf_pixels;
  2295. if (dt > stat_max)
  2296. stat_max = dt;
  2297. }
  2298. if (t1 - stat_last >= 2000) {
  2299. stat_last = t1;
  2300. if (stat_frames) {
  2301. write_stats();
  2302. stat_frames = stat_ms = stat_px = stat_max = 0;
  2303. }
  2304. }
  2305. u_sleep_ms(10);
  2306. }
  2307. }