1use super::style::ClassicStyle;
8use super::ClassicWindow;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum CursorKind {
14 Arrow,
16 Move,
18 HorizontalSlide,
20 VerticalSlide,
22 Close,
24 Resize,
26}
27
28impl CursorKind {
29 fn winamps_own(self) -> &'static str {
32 match self {
33 CursorKind::Move => "wasabi-base/titlebar.cur",
34 CursorKind::HorizontalSlide => "wasabi-base/posbar.cur",
35 CursorKind::Close => "wasabi-base/close.cur",
36 CursorKind::Arrow => "wasabi-base/normal.cur",
37 CursorKind::VerticalSlide => "wasabi-base/eqslid.cur",
38 CursorKind::Resize => "wasabi-base/psize.cur",
39 }
40 }
41}
42
43pub const CURSOR_FILES: [(&str, CursorKind); 29] = {
46 use CursorKind::{Arrow, Close, HorizontalSlide, Move, Resize, VerticalSlide};
47 [
48 ("volbal.cur", HorizontalSlide),
49 ("posbar.cur", HorizontalSlide),
50 ("winbut.cur", Arrow),
51 ("min.cur", Arrow),
52 ("close.cur", Close),
53 ("mainmenu.cur", Arrow),
54 ("titlebar.cur", Move),
55 ("songname.cur", HorizontalSlide),
56 ("normal.cur", Arrow),
57 ("winbut.cur", Arrow),
58 ("min.cur", Arrow),
59 ("wsposbar.cur", HorizontalSlide),
60 ("close.cur", Close),
61 ("mmenu.cur", Arrow),
62 ("wsnormal.cur", Arrow),
63 ("pwinbut.cur", Arrow),
64 ("pclose.cur", Close),
65 ("ptbar.cur", Move),
66 ("pvscroll.cur", VerticalSlide),
67 ("psize.cur", Resize),
68 ("pnormal.cur", Arrow),
69 ("pwinbut.cur", Arrow),
70 ("pclose.cur", Close),
71 ("pwssize.cur", HorizontalSlide),
72 ("pwsnorm.cur", Arrow),
73 ("eqslid.cur", VerticalSlide),
74 ("eqclose.cur", Close),
75 ("eqtitle.cur", Move),
76 ("eqnormal.cur", Arrow),
77 ]
78};
79
80type Box4 = (i32, i32, i32, i32);
83
84fn within(b: Box4, w: i32, h: i32, x: i32, y: i32) -> bool {
85 let fix = |v: i32, size: i32| if v < 0 { v + size } else { v };
86 let (l, t, r, bt) = (fix(b.0, w), fix(b.1, h), fix(b.2, w), fix(b.3, h));
87 x >= l && x <= r && y >= t && y <= bt
88}
89
90fn first(boxes: &[Box4], w: i32, h: i32, x: i32, y: i32) -> usize {
92 boxes
93 .iter()
94 .position(|b| within(*b, w, h, x, y))
95 .unwrap_or(boxes.len())
96}
97
98pub fn slot_at(
101 win: ClassicWindow,
102 style: ClassicStyle,
103 w: i32,
104 h: i32,
105 x: i32,
106 y: i32,
107) -> Option<usize> {
108 Some(match win {
109 ClassicWindow::Main => first(
111 &[
112 (107, 58, 215, 66),
113 (18, 73, 263, 81),
114 (254, 3, 262, 12),
115 (244, 3, 253, 12),
116 (264, 3, 272, 12),
117 (5, 3, 17, 13),
118 (0, 0, 275, 13),
119 (105, 24, 266, 35),
120 ],
121 w,
122 h,
123 x,
124 y,
125 ),
126 ClassicWindow::MainShade => {
127 9 + first(
128 &[
129 (254, 3, 262, 12),
130 (244, 3, 253, 12),
131 (228, 3, 242, 12),
132 (264, 3, 272, 12),
133 (5, 3, 17, 13),
134 ],
135 w,
136 h,
137 x,
138 y,
139 )
140 }
141 ClassicWindow::Equalizer | ClassicWindow::EqualizerShade => {
142 let slider = (0..11).any(|i| {
143 let x0 = if i == 0 { 21 } else { 60 + 18 * i };
144 x >= x0 && x <= x0 + 12 && y > 38 && y < 99
145 });
146 if slider {
147 25
148 } else {
149 26 + first(&[(264, 3, 272, 12), (0, 0, 275, 13)], w, h, x, y)
150 }
151 }
152 ClassicWindow::Playlist => {
153 15 + first(
154 &[
155 (-21, 3, -13, 12),
156 (-11, 3, -3, 12),
157 (0, 0, -1, 13),
158 (-15, 20, -7, -38),
159 (-20, -20, -1, -1),
160 ],
161 w,
162 h,
163 x,
164 y,
165 )
166 }
167 ClassicWindow::PlaylistShade => {
168 21 + first(
169 &[(-21, 3, -13, 12), (-11, 3, -3, 12), (-29, 3, -20, 12)],
170 w,
171 h,
172 x,
173 y,
174 )
175 }
176 ClassicWindow::Video => [16, 17, 19, 20][first(
177 &[(-11, 3, -3, 12), (0, 0, -1, 13), (-20, -20, -1, -1)],
178 w,
179 h,
180 x,
181 y,
182 )],
183 ClassicWindow::Gen => {
184 let edge = i32::from(style == ClassicStyle::Winamp5666);
187 let held = |b: Box4| within((b.0, b.1, b.2 - edge, b.3 - edge), w, h, x, y);
188 if held((-11, 3, -3, 12)) {
189 16
190 } else if held((0, 0, -1, 13)) {
191 17
192 } else if held((-20, -20, -1, -1)) {
193 19
194 } else {
195 20
198 }
199 }
200 })
201}
202
203#[derive(Debug, Clone, PartialEq, Eq)]
205pub struct CursorFrame {
206 pub width: u32,
208 pub height: u32,
210 pub hot_x: u32,
212 pub hot_y: u32,
214 pub argb: Vec<u32>,
216}
217
218#[derive(Debug, Clone, PartialEq, Eq)]
220pub struct SkinCursor {
221 pub frames: Vec<CursorFrame>,
223 pub steps: Vec<(usize, u32)>,
226}
227
228fn u16_at(b: &[u8], i: usize) -> Option<u16> {
229 Some(u16::from_le_bytes(b.get(i..i + 2)?.try_into().ok()?))
230}
231
232fn u32_at(b: &[u8], i: usize) -> Option<u32> {
233 Some(u32::from_le_bytes(b.get(i..i + 4)?.try_into().ok()?))
234}
235
236pub fn decode(bytes: &[u8]) -> Option<SkinCursor> {
239 if bytes.get(0..4) == Some(b"RIFF") && bytes.get(8..12) == Some(b"ACON") {
240 return decode_ani(bytes);
241 }
242 let frame = decode_cur(bytes)?;
243 Some(SkinCursor {
244 frames: vec![frame],
245 steps: vec![(0, 0)],
246 })
247}
248
249fn decode_cur(b: &[u8]) -> Option<CursorFrame> {
251 let kind = u16_at(b, 2)?;
252 if u16_at(b, 0)? != 0 || !(kind == 1 || kind == 2) || u16_at(b, 4)? == 0 {
253 return None;
254 }
255 let (hx, hy) = (u16_at(b, 10)?, u16_at(b, 12)?);
256 let size = u32_at(b, 14)? as usize;
257 let at = u32_at(b, 18)? as usize;
258 let mut frame = decode_dib(b.get(at..at.checked_add(size)?)?)?;
259 if kind == 2 {
260 frame.hot_x = u32::from(hx).min(frame.width.saturating_sub(1));
261 frame.hot_y = u32::from(hy).min(frame.height.saturating_sub(1));
262 } else {
263 frame.hot_x = frame.width / 2;
264 frame.hot_y = frame.height / 2;
265 }
266 Some(frame)
267}
268
269fn decode_dib(d: &[u8]) -> Option<CursorFrame> {
274 let header = u32_at(d, 0)? as usize;
275 if header < 40 {
276 return None;
277 }
278 let width = i32::from_le_bytes(d.get(4..8)?.try_into().ok()?);
279 let height = i32::from_le_bytes(d.get(8..12)?.try_into().ok()?) / 2;
280 let bits = u16_at(d, 14)?;
281 let compression = u32_at(d, 16)?;
282 if width <= 0 || height <= 0 || width > 256 || height > 256 || compression != 0 {
283 return None;
284 }
285 let (w, h) = (width as usize, height as usize);
286 let colors = if bits <= 8 {
287 match u32_at(d, 32)? {
288 0 => 1usize << bits,
289 n => n as usize,
290 }
291 } else {
292 0
293 };
294 let palette: Vec<u32> = (0..colors)
295 .map(|i| {
296 let p = header + 4 * i;
297 d.get(p..p + 3).map_or(0, |c| {
298 (u32::from(c[2]) << 16) | (u32::from(c[1]) << 8) | u32::from(c[0])
299 })
300 })
301 .collect();
302 let xor_at = header + 4 * colors;
303 let xor_row = (w * usize::from(bits)).div_ceil(32) * 4;
304 let and_at = xor_at + xor_row * h;
305 let and_row = w.div_ceil(32) * 4;
306 let mut argb = vec![0u32; w * h];
307 let mut alpha_seen = false;
308 for y in 0..h {
309 let row = xor_at + (h - 1 - y) * xor_row;
310 for x in 0..w {
311 let px = match bits {
312 1 | 4 | 8 => {
313 let bit = x * usize::from(bits);
314 let byte = *d.get(row + bit / 8)?;
315 let shift = 8 - usize::from(bits) - bit % 8;
316 let idx = (usize::from(byte) >> shift) & ((1 << bits) - 1);
317 0xff00_0000 | palette.get(idx).copied().unwrap_or(0)
318 }
319 24 => {
320 let c = d.get(row + 3 * x..row + 3 * x + 3)?;
321 0xff00_0000 | (u32::from(c[2]) << 16) | (u32::from(c[1]) << 8) | u32::from(c[0])
322 }
323 32 => {
324 let c = d.get(row + 4 * x..row + 4 * x + 4)?;
325 alpha_seen |= c[3] != 0;
326 (u32::from(c[3]) << 24)
327 | (u32::from(c[2]) << 16)
328 | (u32::from(c[1]) << 8)
329 | u32::from(c[0])
330 }
331 _ => return None,
332 };
333 argb[y * w + x] = px;
334 }
335 }
336 if !alpha_seen {
337 for y in 0..h {
338 let row = and_at + (h - 1 - y) * and_row;
339 for x in 0..w {
340 let masked = d
341 .get(row + x / 8)
342 .is_some_and(|b| (b >> (7 - x % 8)) & 1 == 1);
343 let px = &mut argb[y * w + x];
344 *px = match (masked, *px & 0x00ff_ffff) {
345 (true, 0) => 0,
346 (true, c) => 0xff00_0000 | (c ^ 0x00ff_ffff),
347 (false, c) => 0xff00_0000 | c,
348 };
349 }
350 }
351 }
352 Some(CursorFrame {
353 width: w as u32,
354 height: h as u32,
355 hot_x: 0,
356 hot_y: 0,
357 argb,
358 })
359}
360
361fn decode_ani(b: &[u8]) -> Option<SkinCursor> {
364 let mut frames = Vec::new();
365 let mut steps_n = 0usize;
366 let mut jiffies = 0u32;
367 let mut sequenced = false;
368 let mut rates: Vec<u32> = Vec::new();
369 let mut seq: Vec<u32> = Vec::new();
370 let mut walk = vec![(12usize, b.len())];
371 while let Some((mut i, end)) = walk.pop() {
372 while i + 8 <= end {
373 let id = b.get(i..i + 4)?;
374 let n = u32_at(b, i + 4)? as usize;
375 let body = i + 8;
376 let stop = body.checked_add(n)?.min(end);
377 match id {
378 b"LIST" => walk.push((body + 4, stop)),
379 b"anih" => {
380 steps_n = u32_at(b, body + 8)? as usize;
381 jiffies = u32_at(b, body + 28)?;
382 sequenced = u32_at(b, body + 32)? & 2 != 0;
383 }
384 b"rate" => rates = (0..n / 4).filter_map(|k| u32_at(b, body + 4 * k)).collect(),
385 b"seq " => seq = (0..n / 4).filter_map(|k| u32_at(b, body + 4 * k)).collect(),
386 b"icon" => frames.push(decode_cur(b.get(body..stop)?)?),
387 _ => {}
388 }
389 i = body + n + (n & 1);
390 }
391 }
392 if frames.is_empty() {
393 return None;
394 }
395 let count = if steps_n == 0 { frames.len() } else { steps_n };
396 let steps = (0..count)
397 .map(|k| {
398 let frame = if sequenced {
399 seq.get(k).copied().unwrap_or(0) as usize
400 } else {
401 k
402 };
403 let jif = rates.get(k).copied().unwrap_or(jiffies).max(1);
404 (frame.min(frames.len() - 1), jif * 1000 / 60)
405 })
406 .collect();
407 Some(SkinCursor { frames, steps })
408}
409
410impl crate::runtime::SkinRuntime {
411 pub fn classic_cursor_at(&mut self, sx: i32, sy: i32) -> Option<usize> {
414 let o = self.hit_test_stack(sx, sy)?;
415 let l = self.layout_object(o)?;
416 let win = self.all_objects.iter().copied().find(|&c| {
417 matches!(self.objects[c].kind, crate::gui::Kind::ClassicWindow(_))
418 && self.layout_object(c) == Some(l)
419 })?;
420 let crate::gui::Kind::ClassicWindow(kind) = self.objects[win].kind else {
421 return None;
422 };
423 let (x, y) = self.layout_point(l, sx, sy);
424 let r = self.objects[win].base.rect;
425 slot_at(
426 kind,
427 self.classic_style,
428 r.width,
429 r.height,
430 x - r.left,
431 y - r.top,
432 )
433 }
434
435 pub fn classic_cursor(&mut self, slot: usize) -> Option<std::rc::Rc<SkinCursor>> {
439 let &(file, kind) = CURSOR_FILES.get(slot)?;
440 self.cursor_file(file)
441 .or_else(|| self.cursor_file(kind.winamps_own()))
442 }
443
444 fn cursor_file(&mut self, file: &'static str) -> Option<std::rc::Rc<SkinCursor>> {
445 if let Some(c) = self.classic_cursors.get(file) {
446 return c.clone();
447 }
448 let c = self
449 .files
450 .read(file)
451 .and_then(|b| decode(&b))
452 .map(std::rc::Rc::new);
453 self.classic_cursors.insert(file, c.clone());
454 c
455 }
456}
457
458#[cfg(test)]
459pub(crate) mod tests {
460 use super::{decode, slot_at, ClassicStyle, CURSOR_FILES};
461 use crate::classic::ClassicWindow;
462
463 pub(crate) fn cur(hot: (u16, u16)) -> Vec<u8> {
465 let mut dib = Vec::new();
466 dib.extend(40u32.to_le_bytes());
467 dib.extend(2i32.to_le_bytes());
468 dib.extend(4i32.to_le_bytes());
469 dib.extend(1u16.to_le_bytes());
470 dib.extend(24u16.to_le_bytes());
471 dib.extend([0u8; 24]);
472 dib.extend([0xff, 0, 0, 0xff, 0xff, 0xff, 0, 0]); dib.extend([0, 0, 0xff, 0, 0xff, 0, 0, 0]); dib.extend([0x40, 0, 0, 0]); dib.extend([0, 0, 0, 0]);
477 let mut f = vec![0, 0, 2, 0, 1, 0, 2, 2, 0, 0];
478 f.extend(hot.0.to_le_bytes());
479 f.extend(hot.1.to_le_bytes());
480 f.extend((dib.len() as u32).to_le_bytes());
481 f.extend(22u32.to_le_bytes());
482 f.extend(dib);
483 f
484 }
485
486 #[test]
487 fn a_cur_gives_its_picture_its_mask_and_its_tip() {
488 let c = decode(&cur((1, 0))).expect("decoded");
489 let f = &c.frames[0];
490 assert_eq!((f.width, f.height, f.hot_x, f.hot_y), (2, 2, 1, 0));
491 assert_eq!(
492 f.argb,
493 vec![0xffff_0000, 0xff00_ff00, 0xff00_00ff, 0xff00_0000]
494 );
495 assert_eq!(c.steps, vec![(0, 0)]);
496 }
497
498 #[test]
499 fn an_ani_steps_through_its_icons_at_its_rates() {
500 let icon = cur((0, 0));
501 let chunk = |id: &[u8], body: &[u8]| {
502 let mut c = id.to_vec();
503 c.extend((body.len() as u32).to_le_bytes());
504 c.extend(body);
505 if body.len() % 2 == 1 {
506 c.push(0);
507 }
508 c
509 };
510 let mut anih = Vec::new();
511 for v in [36u32, 2, 3, 0, 0, 0, 0, 6, 3] {
512 anih.extend(v.to_le_bytes());
513 }
514 let mut rate = Vec::new();
515 for v in [6u32, 12, 3] {
516 rate.extend(v.to_le_bytes());
517 }
518 let mut seq = Vec::new();
519 for v in [0u32, 1, 0] {
520 seq.extend(v.to_le_bytes());
521 }
522 let mut fram = b"fram".to_vec();
523 fram.extend(chunk(b"icon", &icon));
524 fram.extend(chunk(b"icon", &icon));
525 let mut body = b"ACON".to_vec();
526 body.extend(chunk(b"anih", &anih));
527 body.extend(chunk(b"rate", &rate));
528 body.extend(chunk(b"seq ", &seq));
529 body.extend(chunk(b"LIST", &fram));
530 let file = chunk(b"RIFF", &body);
531 let c = decode(&file).expect("decoded");
532 assert_eq!(c.frames.len(), 2);
533 assert_eq!(c.steps, vec![(0, 100), (1, 200), (0, 50)]);
534 }
535
536 #[test]
537 fn the_main_window_picks_winamps_cursors() {
538 let at = |x, y| {
539 slot_at(
540 ClassicWindow::Main,
541 ClassicStyle::Winamp5666,
542 275,
543 116,
544 x,
545 y,
546 )
547 .map(|s| CURSOR_FILES[s].0)
548 };
549 assert_eq!(
550 at(180, 60),
551 Some("volbal.cur"),
552 "balance shares the volume's"
553 );
554 assert_eq!(at(100, 75), Some("posbar.cur"));
555 assert_eq!(at(268, 5), Some("close.cur"));
556 assert_eq!(at(100, 5), Some("titlebar.cur"));
557 assert_eq!(at(150, 30), Some("songname.cur"));
558 assert_eq!(at(50, 100), Some("normal.cur"));
559 }
560
561 #[test]
562 fn the_other_windows_pick_theirs() {
563 let s = |w, ww, hh, x, y| {
564 slot_at(w, ClassicStyle::Winamp5666, ww, hh, x, y).map(|s| CURSOR_FILES[s].0)
565 };
566 assert_eq!(
567 s(ClassicWindow::Equalizer, 275, 116, 25, 60),
568 Some("eqslid.cur")
569 );
570 assert_eq!(
571 s(ClassicWindow::Equalizer, 275, 116, 80, 60),
572 Some("eqslid.cur")
573 );
574 assert_eq!(
575 s(ClassicWindow::Equalizer, 275, 116, 50, 60),
576 Some("eqnormal.cur")
577 );
578 assert_eq!(
579 s(ClassicWindow::Playlist, 300, 145, 290, 140),
580 Some("psize.cur")
581 );
582 assert_eq!(
583 s(ClassicWindow::Playlist, 300, 145, 289, 50),
584 Some("pvscroll.cur")
585 );
586 assert_eq!(
587 s(ClassicWindow::PlaylistShade, 300, 14, 275, 7),
588 Some("pwssize.cur")
589 );
590 assert_eq!(
591 s(ClassicWindow::MainShade, 275, 14, 230, 7),
592 Some("wsposbar.cur")
593 );
594 assert_eq!(
595 s(ClassicWindow::Video, 275, 232, 268, 5),
596 Some("pclose.cur")
597 );
598 assert_eq!(s(ClassicWindow::Gen, 275, 116, 100, 5), Some("ptbar.cur"));
599 assert_eq!(
600 s(ClassicWindow::Gen, 275, 116, 100, 50),
601 Some("pnormal.cur"),
602 "over the library too"
603 );
604 assert_eq!(
605 s(ClassicWindow::Gen, 275, 116, 100, 13),
606 Some("pnormal.cur")
607 );
608 assert_eq!(s(ClassicWindow::Gen, 275, 116, 273, 114), Some("psize.cur"));
609 assert_eq!(
610 s(ClassicWindow::Gen, 275, 116, 274, 114),
611 Some("pnormal.cur")
612 );
613 assert_eq!(s(ClassicWindow::Gen, 275, 116, 270, 110), Some("psize.cur"));
614 }
615
616 #[test]
617 fn a_2_91_gen_window_takes_its_boxes_edges() {
618 let at = |style, x, y| {
619 slot_at(ClassicWindow::Gen, style, 275, 116, x, y).map(|s| CURSOR_FILES[s].0)
620 };
621 assert_eq!(at(ClassicStyle::Winamp5666, 272, 5), Some("ptbar.cur"));
623 assert_eq!(at(ClassicStyle::Winamp291, 272, 5), Some("pclose.cur"));
624 assert_eq!(at(ClassicStyle::Winamp5666, 100, 13), Some("pnormal.cur"));
625 assert_eq!(at(ClassicStyle::Winamp291, 100, 13), Some("ptbar.cur"));
626 }
627}