A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/***************************************************************************
2* __________ __ ___.
3* Open \______ \ ____ ____ | | _\_ |__ _______ ___
4* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7* \/ \/ \/ \/ \/
8* $Id$
9*
10* New greyscale framework
11* Parameter handling
12*
13* This is a generic framework to display 129 shades of grey on low-depth
14* bitmap LCDs (Archos b&w, Iriver & Ipod 4-grey) within plugins.
15*
16* Copyright (C) 2008 Jens Arnold
17*
18* This program is free software; you can redistribute it and/or
19* modify it under the terms of the GNU General Public License
20* as published by the Free Software Foundation; either version 2
21* of the License, or (at your option) any later version.
22*
23* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
24* KIND, either express or implied.
25*
26****************************************************************************/
27
28#include "plugin.h"
29#include "grey.h"
30
31/* Default greylib viewport struct */
32struct viewport _grey_default_vp;
33
34/* Set position of the top left corner of the greyscale overlay
35 Note that depending on the target LCD, either x or y gets rounded
36 to the nearest multiple of 4 or 8 */
37void grey_set_position(int x, int y)
38{
39#if LCD_PIXELFORMAT == HORIZONTAL_PACKING
40 _grey_info.bx = (x + 4) >> 3;
41 x = 8 * _grey_info.bx;
42#else /* vertical packing or vertical interleaved */
43#if (LCD_DEPTH == 1) || (LCD_PIXELFORMAT == VERTICAL_INTERLEAVED)
44 _grey_info.by = (y + 4) >> 3;
45 y = 8 * _grey_info.by;
46#elif LCD_DEPTH == 2
47 _grey_info.by = (y + 2) >> 2;
48 y = 4 * _grey_info.by;
49#endif
50#endif /* LCD_PIXELFORMAT */
51 _grey_info.x = x;
52 _grey_info.y = y;
53
54 if (_grey_info.flags & _GREY_RUNNING)
55 {
56#ifdef SIMULATOR
57 rb->sim_lcd_ex_update_rect(_grey_info.x, _grey_info.y,
58 _grey_info.width,
59 _grey_info.height);
60 grey_deferred_lcd_update();
61#else
62 _grey_info.flags |= _GREY_DEFERRED_UPDATE;
63#endif
64 }
65}
66
67/* Set the draw mode for subsequent drawing operations */
68void grey_set_drawmode(int mode)
69{
70 _grey_info.vp->drawmode = mode & (DRMODE_SOLID|DRMODE_INVERSEVID);
71}
72
73/* Return the current draw mode */
74int grey_get_drawmode(void)
75{
76 return _grey_info.vp->drawmode;
77}
78
79/* Set the foreground shade for subsequent drawing operations */
80void grey_set_foreground(unsigned brightness)
81{
82 _grey_info.vp->fg_pattern = brightness;
83}
84
85/* Return the current foreground shade */
86unsigned grey_get_foreground(void)
87{
88 return _grey_info.vp->fg_pattern;
89}
90
91/* Set the background shade for subsequent drawing operations */
92void grey_set_background(unsigned brightness)
93{
94 _grey_info.vp->bg_pattern = brightness;
95}
96
97/* Return the current background shade */
98unsigned grey_get_background(void)
99{
100 return _grey_info.vp->bg_pattern;
101}
102
103/* Set draw mode, foreground and background shades at once */
104void grey_set_drawinfo(int mode, unsigned fg_brightness, unsigned bg_brightness)
105{
106 grey_set_drawmode(mode);
107 grey_set_foreground(fg_brightness);
108 grey_set_background(bg_brightness);
109}
110
111/* Set font for the text output routines */
112void grey_setfont(int newfont)
113{
114 _grey_info.vp->font = newfont;
115}
116
117/* Get width and height of a text when printed with the current font */
118int grey_getstringsize(const unsigned char *str, int *w, int *h)
119{
120 return rb->font_getstringsize(str, w, h, _grey_info.vp->font);
121}
122
123/* Helper to establish visible area between viewport and framebuffer */
124static void grey_update_clip_rect(void)
125{
126 if (!(_grey_info.flags & GREY_BUFFERED))
127 return; /* no chunky buffer */
128
129 struct viewport *vp = _grey_info.vp;
130
131 if (!vp || !_grey_info.curbuffer)
132 return;
133
134 /* Get overall intersection of framebuffer and viewport in viewport
135 coordinates so that later clipping of drawing is kept as simple as
136 possible. If l <= r and/or b <= t after intersecting, draw routines
137 will see this as an empty area. */
138 _grey_info.clip_l = _grey_info.cb_x - vp->x;
139 _grey_info.clip_t = _grey_info.cb_y - vp->y;
140 _grey_info.clip_r = _grey_info.clip_l + _grey_info.cb_width;
141 _grey_info.clip_b = _grey_info.clip_t + _grey_info.cb_height;
142
143 if (_grey_info.clip_l < 0)
144 _grey_info.clip_l = 0;
145
146 if (_grey_info.clip_t < 0)
147 _grey_info.clip_t = 0;
148
149 if (_grey_info.clip_r > vp->width)
150 _grey_info.clip_r = vp->width;
151
152 if (_grey_info.clip_b > vp->height)
153 _grey_info.clip_b = vp->height;
154}
155
156/* Set current grey viewport for draw routines */
157struct viewport *grey_set_viewport(struct viewport *vp)
158{
159 struct viewport *last_vp = _grey_info.vp;
160 if (vp == NULL)
161 vp = &_grey_default_vp;
162
163 if (_grey_info.vp != vp)
164 {
165 _grey_info.vp = vp;
166 grey_update_clip_rect();
167 }
168 return last_vp;
169}
170
171/* Set viewport to default settings */
172void grey_viewport_set_fullscreen(struct viewport *vp,
173 const enum screen_type screen)
174{
175 if (vp == NULL)
176 vp = &_grey_default_vp;
177
178 vp->x = 0;
179 vp->y = 0;
180 vp->width = _grey_info.width;
181 vp->height = _grey_info.height;
182 vp->fg_pattern = 0;
183 vp->bg_pattern = 255;
184 vp->drawmode = DRMODE_SOLID;
185 vp->font = FONT_SYSFIXED;
186
187 if (vp == _grey_info.vp)
188 grey_update_clip_rect(); /* is current one in use */
189
190 (void)screen;
191}
192
193void grey_viewport_set_pos(struct viewport *vp,
194 int x, int y, int width, int height)
195{
196 if (vp == NULL || vp == &_grey_default_vp)
197 return; /* Cannot be moved or resized */
198
199 if (width < 0)
200 width = 0; /* 'tis okay */
201
202 if (height < 0)
203 height = 0;
204
205 vp->x = x;
206 vp->y = y;
207 vp->width = width;
208 vp->height = height;
209
210 if (vp == _grey_info.vp)
211 grey_update_clip_rect(); /* is current one in use */
212}
213
214/* Set current grey chunky buffer for draw routines */
215void grey_set_framebuffer(unsigned char *buffer)
216{
217 if (!(_grey_info.flags & GREY_BUFFERED))
218 return; /* no chunky buffer */
219
220 if (buffer == NULL)
221 buffer = _grey_info.buffer; /* Default */
222
223 if (buffer != _grey_info.curbuffer)
224 {
225 _grey_info.curbuffer = buffer;
226
227 if (buffer == _grey_info.buffer)
228 {
229 /* Setting to default fb resets dimensions */
230 grey_framebuffer_set_pos(0, 0, 0, 0);
231 }
232 }
233}
234
235/* Specify the dimensions of the current framebuffer */
236void grey_framebuffer_set_pos(int x, int y, int width, int height)
237{
238 if (!(_grey_info.flags & GREY_BUFFERED))
239 return; /* no chunky buffer */
240
241 if (_grey_info.curbuffer == _grey_info.buffer)
242 {
243 /* This cannot be moved or resized */
244 x = 0;
245 y = 0;
246 width = _grey_info.width;
247 height = _grey_info.height;
248 }
249 else if (width <= 0 || height <= 0)
250 return;
251
252 if (x == _grey_info.cb_x && y == _grey_info.cb_y &&
253 width == _grey_info.cb_width && height == _grey_info.cb_height)
254 return; /* No change */
255
256 _grey_info.cb_x = x;
257 _grey_info.cb_y = y;
258 _grey_info.cb_width = width;
259 _grey_info.cb_height = height;
260
261 grey_update_clip_rect();
262}