A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 185 lines 5.3 kB view raw
1/*************************************************************************** 2 * __________ __ ___. 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 * \/ \/ \/ \/ \/ 8 * $Id$ 9 * 10 * Copyright (C) 2005 Linus Nielsen Feltzing 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * as published by the Free Software Foundation; either version 2 15 * of the License, or (at your option) any later version. 16 * 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 18 * KIND, either express or implied. 19 * 20 ****************************************************************************/ 21#include "plugin.h" 22#include "highscore.h" 23 24static bool highscore_updated = false; 25 26int highscore_save(char *filename, struct highscore *scores, int num_scores) 27{ 28 int i; 29 int fd; 30 int rc; 31 char buf[80]; 32 33 if(!highscore_updated) 34 return 1; 35 36 fd = rb->open(filename, O_WRONLY|O_CREAT, 0666); 37 if(fd < 0) 38 return -1; 39 40 for(i = 0;i < num_scores;i++) 41 { 42 rb->snprintf(buf, sizeof(buf), "%d:%d:%s\n", 43 scores[i].score, scores[i].level, scores[i].name); 44 rc = rb->write(fd, buf, rb->strlen(buf)); 45 if(rc < 0) 46 { 47 rb->close(fd); 48 return -2; 49 } 50 } 51 rb->close(fd); 52 highscore_updated = false; 53 return 0; 54} 55 56int highscore_load(char *filename, struct highscore *scores, int num_scores) 57{ 58 int i; 59 int fd; 60 char buf[80]; 61 char *score, *level, *name; 62 63 rb->memset(scores, 0, sizeof(struct highscore)*num_scores); 64 65 fd = rb->open(filename, O_RDONLY); 66 if(fd < 0) 67 return -1; 68 69 i = 0; 70 while(rb->read_line(fd, buf, sizeof(buf)) > 0 && i < num_scores) 71 { 72 DEBUGF("%s\n", buf); 73 74 if ( !rb->settings_parseline(buf, &score, &level) ) 75 continue; 76 if ( !rb->settings_parseline(level, &level, &name) ) 77 continue; 78 79 scores[i].score = rb->atoi(score); 80 scores[i].level = rb->atoi(level); 81 rb->strlcpy(scores[i].name, name, sizeof(scores[i].name)); 82 i++; 83 } 84 rb->close(fd); 85 highscore_updated = false; 86 return 0; 87} 88 89int highscore_update(int score, int level, const char *name, 90 struct highscore *scores, int num_scores) 91{ 92 int pos; 93 struct highscore *entry; 94 95 if (!highscore_would_update(score, scores, num_scores)) 96 return -1; 97 98 pos = num_scores-1; 99 while (pos > 0 && score > scores[pos-1].score) 100 { 101 /* move down one */ 102 rb->memcpy((void *)&scores[pos], (void *)&scores[pos-1], 103 sizeof(struct highscore)); 104 pos--; 105 } 106 107 entry = scores + pos; 108 entry->score = score; 109 entry->level = level; 110 rb->strlcpy(entry->name, name, sizeof(entry->name)); 111 112 highscore_updated = true; 113 return pos; 114} 115 116bool highscore_would_update(int score, struct highscore *scores, 117 int num_scores) 118{ 119 return (num_scores > 0) && (score > scores[num_scores-1].score); 120} 121 122#define MARGIN 5 123void highscore_show(int position, struct highscore *scores, int num_scores, 124 bool show_level) 125{ 126 int i, w, h; 127#if defined(HAVE_LCD_COLOR) || LCD_DEPTH >= 2 128 unsigned bgcolor = rb->lcd_get_background(); 129 unsigned fgcolor = rb->lcd_get_foreground(); 130#ifdef HAVE_LCD_COLOR 131 rb->lcd_set_background(LCD_BLACK); 132 rb->lcd_set_foreground(LCD_WHITE); 133#else 134 rb->lcd_set_background(LCD_WHITE); 135 rb->lcd_set_foreground(LCD_BLACK); 136#endif 137#endif 138 rb->lcd_clear_display(); 139 140 rb->lcd_setfont(FONT_UI); 141 rb->lcd_getstringsize("High Scores", &w, &h); 142 /* check wether it fits on screen */ 143 if ((4*h + h*(num_scores-1) + MARGIN) > LCD_HEIGHT) { 144 rb->lcd_setfont(FONT_SYSFIXED); 145 rb->lcd_getstringsize("High Scores", &w, &h); 146 } 147 rb->lcd_putsxy(LCD_WIDTH/2-w/2, MARGIN, "High Scores"); 148 rb->lcd_putsxy(LCD_WIDTH/4-w/4,2*h, "Score"); 149 150 if(show_level) { 151 rb->lcd_putsxy(LCD_WIDTH*3/4-w/4,2*h, "Level"); 152 } 153 154 for (i = 0; i<num_scores; i++) 155 { 156#ifdef HAVE_LCD_COLOR 157 if (i == position) { 158 rb->lcd_set_foreground(LCD_RGBPACK(245,0,0)); 159 } 160#endif 161 rb->lcd_putsxyf (MARGIN,3*h + h*i, "%d)", i+1); 162 rb->lcd_putsxyf (LCD_WIDTH/4-w/4,3*h + h*i, "%d", scores[i].score); 163 164 if(show_level) { 165 rb->lcd_putsxyf (LCD_WIDTH*3/4-w/4,3*h + h*i, "%d", scores[i].level); 166 } 167 168 if(i == position) { 169#ifdef HAVE_LCD_COLOR 170 rb->lcd_set_foreground(LCD_WHITE); 171#else 172 rb->lcd_hline(MARGIN, LCD_WIDTH-MARGIN*2, 3*h + h*(i+1) - 1); 173#endif 174 } 175 } 176 rb->lcd_update(); 177 rb->sleep(HZ/2); 178 rb->button_clear_queue(); 179 rb->button_get(true); 180 rb->lcd_setfont(FONT_SYSFIXED); 181#if defined(HAVE_LCD_COLOR) || LCD_DEPTH >= 2 182 rb->lcd_set_background(bgcolor); 183 rb->lcd_set_foreground(fgcolor); 184#endif 185}