@@ -165,6 +165,50 @@ void modeset_paint_buffer(struct modeset_buf *buf) {
165165 }
166166 }
167167
168+ //display custom message
169+ if (osd_custom_message ) {
170+ FILE * file = fopen ("/run/pixelpilot.msg" , "r" );
171+ if (file != NULL ) {
172+
173+ if (fgets (custom_msg , sizeof (custom_msg ), file ) == NULL ) {
174+ perror ("Error reading from file" );
175+ fclose (file );
176+ }
177+ fclose (file );
178+ if (unlink ("/run/pixelpilot.msg" ) != 0 ) {
179+ perror ("Error deleting the file" );
180+ }
181+ custom_msg_refresh_count = 1 ;
182+ }
183+ if (custom_msg_refresh_count > 0 ) {
184+
185+ if (custom_msg_refresh_count ++ > 5 ) custom_msg_refresh_count = 0 ;
186+
187+ size_t msg_length = strlen (custom_msg );
188+
189+ // Ensure null termination at the 80th position to prevent overflow
190+ custom_msg [79 ] = '\0' ;
191+
192+ // Find the first newline character, if it exists
193+ char * newline_pos = strchr (custom_msg , '\n' );
194+ if (newline_pos != NULL ) {
195+ * newline_pos = '\0' ; // Null-terminate at the newline
196+ }
197+
198+ // Measure the text width
199+ cairo_text_extents_t extents ;
200+ cairo_text_extents (cr , custom_msg , & extents );
201+
202+ // Calculate the position to center the text horizontally
203+ double x = (buf -> width / 2 ) - (extents .width / 2 );
204+ double y = (buf -> height / 2 );
205+
206+ // Set the position and draw the text
207+ cairo_move_to (cr , x , y );
208+ cairo_show_text (cr , custom_msg );
209+ }
210+ }
211+
168212 if (!osd_vars .enable_telemetry ){
169213 return ;
170214 }
@@ -272,47 +316,6 @@ void modeset_paint_buffer(struct modeset_buf *buf) {
272316 minutes = 0 ;
273317 }
274318
275- //display custom message
276- if (osd_custom_message ) {
277- FILE * file = fopen ("/run/pixelpilot.msg" , "r" );
278- if (file != NULL ) {
279-
280- if (fgets (custom_msg , sizeof (custom_msg ), file ) == NULL ) {
281- perror ("Error reading from file" );
282- fclose (file );
283- }
284- fclose (file );
285- if (unlink ("/run/pixelpilot.msg" ) != 0 ) {
286- perror ("Error deleting the file" );
287- }
288- custom_msg_refresh_count = 1 ;
289- }
290- if (custom_msg_refresh_count > 0 ) {
291-
292- if (custom_msg_refresh_count ++ > 5 ) custom_msg_refresh_count = 0 ;
293-
294- size_t msg_length = strlen (custom_msg );
295-
296- //remove any trailing newline that fgets may read
297- if (msg_length > 0 && custom_msg [msg_length - 1 ] == '\n' ) {
298- custom_msg [msg_length - 1 ] = '\0' ;
299- msg_length -- ; // Adjust length after removing newline
300- }
301-
302- // Measure the text width
303- cairo_text_extents_t extents ;
304- cairo_text_extents (cr , custom_msg , & extents );
305-
306- // Calculate the position to center the text horizontally
307- double x = (buf -> width / 2 ) - (extents .width / 2 );
308- double y = (buf -> height / 2 );
309-
310- // Set the position and draw the text
311- cairo_move_to (cr , x , y );
312- cairo_show_text (cr , custom_msg );
313- }
314- }
315-
316319 cairo_fill (cr );
317320}
318321
0 commit comments