ucmd_wm.c 75 KB

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