ucmd_wm.c 75 KB

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