· 4 years ago · Jul 27, 2021, 06:38 PM
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <fcntl.h>
5#include <sys/ioctl.h>
6#include <sys/mount.h>
7#include <dlfcn.h>
8#include "inkview.h"
9
10#define FONTSCALE 2 //ToDo: use the config api to make it customizable. Supported values: 1, 2, 3, 4.
11#define FILESPERPAGE (64/FONTSCALE) //Code is prepared for a variable, not a def
12
13extern const ibitmap background;
14
15ifont *listfont, *smallfont;
16
17int cursor=0, files=2; //Положение курсора и общее количество файлов в текущем дире
18char curfile[2048]="/mnt/ext1"; //Имя файла/дира под курсором
19char curpath[2048]=""; //Текущий путь со слешем в конце (пустая строка → папка "Мой компьютер", не соответствующая никакому диру)
20char temp[2048]; //Просто темп для конкатенации полного pathname
21
22/*void Print(char *cha, int i)
23{
24 char l[16384];
25 sprintf (l, cha, i);
26 ClearScreen();
27 SetFont(smallfont, BLACK);
28 DrawTextRect( 0, 779, 600, 10, l, ALIGN_CENTER);
29 FullUpdate();
30}*/
31
32void ListFiles() //Optimized for solid-state drives (no time penalty for actually re-reading directory from disk every time)
33{
34 int i;
35
36 ClearScreen();
37 SetFont(listfont, BLACK);
38
39 if (!curpath[0])
40 {
41 if (cursor<1)
42 {
43 DrawTextRect( 10, 0, 590, FONTSCALE*12, "E-Book Inner Memory", ALIGN_LEFT);
44 DrawTextRect( 0, FONTSCALE*12, 590, FONTSCALE*12, "MicroSD Card", ALIGN_LEFT);
45 strcpy (curfile,"/mnt/ext1");
46 } else {
47 DrawTextRect( 0, 0, 590, FONTSCALE*12, "E-Book Inner Memory", ALIGN_LEFT);
48 DrawTextRect( 10, FONTSCALE*12, 590, FONTSCALE*12, "MicroSD Card", ALIGN_LEFT);
49 strcpy (curfile,"/mnt/ext2");
50 }
51// FullUpdate();
52 SoftUpdate();
53 files=2;
54 return;
55 }
56
57 DIR *path = NULL;
58 path=opendir(curpath);
59
60 struct dirent *dir_entry;
61 struct stat file_info;
62
63 curfile[0]=0;
64 for (i=0; (dir_entry=readdir(path))!=NULL; i++)
65 {
66 strcpy (temp, curpath);
67 strcat (temp, dir_entry->d_name);
68 stat(dir_entry->d_name,&file_info);
69 if ( ((file_info.st_mode & S_IFMT) != S_IFDIR && (file_info.st_mode & S_IFMT) != S_IFREG) ||
70 (dir_entry->d_name[0]=='.' && !dir_entry->d_name[1]) ) {i--; continue;}
71
72 if (i/FILESPERPAGE != cursor/FILESPERPAGE) continue; //show only files from the same page
73
74 if (i==cursor)
75 {
76// DrawTextRect( 10, FONTSCALE*12*(i%FILESPERPAGE), 590, FONTSCALE*12, temp, ALIGN_LEFT);
77 DrawTextRect( 10, FONTSCALE*12*(i%FILESPERPAGE), 590, FONTSCALE*12, dir_entry->d_name, ALIGN_LEFT);
78 strcpy (curfile, dir_entry->d_name);
79 }
80// else DrawTextRect( 0, FONTSCALE*12*(i%FILESPERPAGE), 600, FONTSCALE*12, temp, ALIGN_LEFT);
81 else DrawTextRect( 0, FONTSCALE*12*(i%FILESPERPAGE), 600, FONTSCALE*12, dir_entry->d_name, ALIGN_LEFT);
82 }
83
84 files=i;
85 closedir(path);
86
87 SetFont(smallfont, BLACK);
88 sprintf (temp, "Page %i of %i", cursor/FILESPERPAGE+1, i/FILESPERPAGE+1);
89 DrawTextRect( 0, 785, 600, 12, temp, ALIGN_CENTER);
90
91// FullUpdate();
92 SoftUpdate();
93}
94
95int main_handler(int type, int par1, int par2)
96{
97 struct stat file_info;
98
99 if (type == EVT_INIT) // occurs once at startup, only in main handler
100 {
101 smallfont = OpenFont("DroidSans", 12, 1);
102 switch (FONTSCALE) //фундамент для изменяемого размера
103 {
104 case 1:
105 listfont = OpenFont("DroidSans", 12, 1); //may be just " = smallfont"? Should it work?
106 break;
107 case 2:
108 listfont = OpenFont("DroidSans", 24, 1);
109 break;
110 case 3:
111 listfont = OpenFont("DroidSans", 36, 1);
112 break;
113 case 4:
114 listfont = OpenFont("DroidSans", 48, 1);
115 break;
116 }
117 }
118
119 if (type == EVT_SHOW) // occurs when this event handler becomes active
120 {
121 StretchBitmap(0, 0, 600, 800, &background, 0);
122 FullUpdate();
123 }
124
125 if (type == EVT_KEYPRESS)
126 {
127 switch (par1)
128 {
129 case 24: //left on my 622
130 if (cursor>0) cursor--; else cursor = files-1; //we always have at least 1 file A. K. A. ".."
131 ListFiles();
132 break;
133
134 case 25: //right on my 622
135 if (cursor<files-1) cursor++; else cursor = 0;
136 ListFiles();
137 break;
138
139 case KEY_MENU:
140 strcpy (temp, curpath);
141 strcat (temp, curfile);
142 stat(temp,&file_info);
143//Print ("res=%i", stat(temp,&file_info) );
144 if ((file_info.st_mode & S_IFMT) == S_IFDIR)
145 {
146 if (curfile[0]=='.' && curfile[1]=='.' && !curfile[2]) //Dir Up
147 {
148 if (strlen(curpath)>10) // "/mnt/ext?/" top allowed
149 {
150 strrchr(curpath,'/')[0] = 0; //removing trailing slash;
151 strrchr(curpath,'/')[1] = 0; //removing last dir name;
152 } else curpath[0]=0; // Not a dir, but the windows-like "My Computer" folder
153 } else {
154 strcpy (curpath, temp);
155 strcat (curpath, "/");
156 cursor=0;
157 }
158 ListFiles();
159 break;
160 }
161
162// OpenBook(temp, "", 0);
163 OpenBook(temp, NULL, 0);
164//strcat (temp, "%i");
165//Print (temp,0);
166// CloseApp();
167 break;
168
169// default: Print ("key=%i", par1);
170 }
171 }
172
173 return 0;
174}
175
176int main(int argc, char **argv)
177{
178// unlink ("/mnt/ext1/system/bin/bookshelf.app");
179
180// chdir("/mnt/ext1/");
181 InkViewMain(main_handler);
182
183 return 0;
184}
185
186