ucmd_wm.c 70 KB

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