-
Notifications
You must be signed in to change notification settings - Fork 67
/
textviewer.c
320 lines (312 loc) · 12.5 KB
/
textviewer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include"nasmfunc.h"
#include"memory.h"
#include"sheet.h"
#include"graphic.h"
#include"buffer.h"
#include"timer.h"
#include"mtask.h"
#include"keyboard.h"
#include"mouse.h"
#include"textviewer.h"
#include<stdio.h>
#include<string.h>
void loadTextFile (char *fileName,struct Sheet *textViewerSheet,int* curPosX,int* curPosY,char curInput[21][75],int page)
{
(*curPosX)=0;
(*curPosY)=0;
for (int i=0;i<21;i++)
sprintf (curInput[i],"");
if (strcmp(fileName,"\\Text\\Welcome")==0)
{
sprintf (curInput[(*curPosY)++]," Welcome to my OS");
sprintf (curInput[(*curPosY)++]," This is my OS which can run on bare computer.");
sprintf (curInput[(*curPosY)++]," My OS support visualization,multitasking and the management of memory,");
sprintf (curInput[(*curPosY)++],"process and file.The detail of bottom layout includes start sector,system ");
sprintf (curInput[(*curPosY)++],"boot,protected mode,graphics,pallet,interrupt,segmentation,PCB and file.Th");
sprintf (curInput[(*curPosY)++],"e detail of upper layout includes buffer,multitask and communication,mouse");
sprintf (curInput[(*curPosY)++]," and keyboard's driver,typeface,timer,sheet,window and focus,and API.The a");
sprintf (curInput[(*curPosY)++],"pplication on this OS includes calculator,drawing,music player,note,task m");
sprintf (curInput[(*curPosY)++],"anager,memory manager and file manager.");
sprintf (curInput[(*curPosY)++]," The management of memory uses free table and first fit algorithm,inclu");
sprintf (curInput[(*curPosY)++],"ding memory test,memory alloc and auto free.The management of memory uses ");
sprintf (curInput[(*curPosY)++],"round robin algorithm,including task alloc,shift,sleep and communication b");
sprintf (curInput[(*curPosY)++],"etween tasks.The file system based on FAT,including text file,image file,m");
sprintf (curInput[(*curPosY)++],"usic file and menu.");
sprintf (curInput[(*curPosY)++]," This OS embodies my efforts.");
}else if (strcmp(fileName,"\\Music\\Mus1")==0)
{
sprintf (curInput[(*curPosY)++],"CCGGAAG.FFDDEEC.GGFFEED.GGFFEED.CCGGAAG.FFDDEEC... ");
}else if (strcmp(fileName,"\\Source\\Play.c")==0 && page==0)
{
sprintf (curInput[(*curPosY)++],"#include\"system.h\"");
sprintf (curInput[(*curPosY)++],"#include\"io.h\"");
sprintf (curInput[(*curPosY)++],"#include\"graphic.h\"");
sprintf (curInput[(*curPosY)++],"#include\"window.h\"");
sprintf (curInput[(*curPosY)++],"#include\"buffer.h\"");
sprintf (curInput[(*curPosY)++],"#include\"memory.h\"");
sprintf (curInput[(*curPosY)++],"#include\"file.h\"");
sprintf (curInput[(*curPosY)++],"#include<stdio.h>");
sprintf (curInput[(*curPosY)++],"int main(int argc,char **argv)");
sprintf (curInput[(*curPosY)++],"{");
sprintf (curInput[(*curPosY)++]," if (argc!=1)");
sprintf (curInput[(*curPosY)++]," return 0;");
sprintf (curInput[(*curPosY)++]," char *fileName=argv[0];");
sprintf (curInput[(*curPosY)++]," //Open File");
sprintf (curInput[(*curPosY)++]," struct File *fp;");
sprintf (curInput[(*curPosY)++]," if ((fp=fileOpen(fileName))==0)");
sprintf (curInput[(*curPosY)++]," return 0;");
sprintf (curInput[(*curPosY)++]," int length=fileLength(fp);");
sprintf (curInput[(*curPosY)++]," //File Buffer");
sprintf (curInput[(*curPosY)++]," unsigned char *fileBufC = (unsigned char *)allocMem(length,\"File\");");
sprintf (curInput[(*curPosY)++]," struct Buffer fileBuffer;");
}else if (strcmp(fileName,"\\Source\\Play.c")==0 && page==1)
{
sprintf (curInput[(*curPosY)++]," initBuffer(&fileBuffer,length,fileBufC);");
sprintf (curInput[(*curPosY)++]," readFile(fp,0,length,fileBuffer); ");
sprintf (curInput[(*curPosY)++],"");
sprintf (curInput[(*curPosY)++]," //Sheet Buffer");
sprintf (curInput[(*curPosY)++]," unsigned char *playerBufC=(unsigned char *)allocMem(70*28,\"Player\");");
sprintf (curInput[(*curPosY)++]," struct Buffer fileBuffer;");
sprintf (curInput[(*curPosY)++]," initBuffer(playerBufC,70*28,fileBuffer);");
sprintf (curInput[(*curPosY)++]," //Alloc Sheet");
sprintf (curInput[(*curPosY)++]," struct Sheet *playerSheet;");
sprintf (curInput[(*curPosY)++]," playerSheet=allocSheet();");
sprintf (curInput[(*curPosY)++]," setBufInSheet(playerSheet,playerBufC,70,28,-1);");
sprintf (curInput[(*curPosY)++]," //Init Sheet");
sprintf (curInput[(*curPosY)++]," makeWindow(playerSheet,70,28,\"Player\");");
sprintf (curInput[(*curPosY)++]," slideSheet(playerSheet,180,72);");
sprintf (curInput[(*curPosY)++]," updownSheet(playerSheet);");
sprintf (curInput[(*curPosY)++]," char s[8],data;");
sprintf (curInput[(*curPosY)++]," sprintf (s,\"Playing\");");
sprintf (curInput[(*curPosY)++]," putStrOnSht(musicPlayerSheet,70,28,BLACK,WHITE,str,8);");
sprintf (curInput[(*curPosY)++],"");
sprintf (curInput[(*curPosY)++]," //Init Timer");
sprintf (curInput[(*curPosY)++]," struct Timer *timerPlayer;");
}else if (strcmp(fileName,"\\Source\\Play.c")==0 && page==2)
{
sprintf (curInput[(*curPosY)++]," char bufferTime;");
sprintf (curInput[(*curPosY)++]," timerPlayer=allocTimer();");
sprintf (curInput[(*curPosY)++]," initTimer(timerPlayer,&bufferTime,1);");
sprintf (curInput[(*curPosY)++],"");
sprintf (curInput[(*curPosY)++]," char oneBeat;");
sprintf (curInput[(*curPosY)++]," while (1)");
sprintf (curInput[(*curPosY)++]," {");
sprintf (curInput[(*curPosY)++]," for (int i=0;i<length;i++)");
sprintf (curInput[(*curPosY)++]," {");
sprintf (curInput[(*curPosY)++]," //Close");
sprintf (curInput[(*curPosY)++]," if(isClosedWindow(playerSheet))");
sprintf (curInput[(*curPosY)++]," {");
sprintf (curInput[(*curPosY)++]," freeSheet(playerSheet);");
sprintf (curInput[(*curPosY)++]," freeMem(fileBufC,length);");
sprintf (curInput[(*curPosY)++]," freeMem(playerBufC,70*28);");
sprintf (curInput[(*curPosY)++]," PlayOneBeat(0);");
sprintf (curInput[(*curPosY)++]," return;");
sprintf (curInput[(*curPosY)++]," }");
sprintf (curInput[(*curPosY)++]," //Suspend");
sprintf (curInput[(*curPosY)++]," if (iskey(&data) && data==' ')");
sprintf (curInput[(*curPosY)++]," {");
}else if (strcmp(fileName,"\\Source\\Play.c")==0 && page==3)
{
sprintf (curInput[(*curPosY)++]," sprintf (s,\"Suspend\");");
sprintf (curInput[(*curPosY)++]," putStrOnSht(musicPlayerSheet,70,28,BLACK,WHITE,str,8);");
sprintf (curInput[(*curPosY)++]," while (!(iskey(&data) && data==' '));");
sprintf (curInput[(*curPosY)++]," //Continue");
sprintf (curInput[(*curPosY)++]," sprintf (s,\"Playing\");");
sprintf (curInput[(*curPosY)++]," putStrOnSht(musicPlayerSheet,70,28,BLACK,WHITE,str,8);");
sprintf (curInput[(*curPosY)++]," }");
sprintf (curInput[(*curPosY)++]," //OneBeat");
sprintf (curInput[(*curPosY)++]," getBuffer(fileBuffer,oneBeat);");
sprintf (curInput[(*curPosY)++]," playOneBeat(oneBeat);");
sprintf (curInput[(*curPosY)++]," //20ms");
sprintf (curInput[(*curPosY)++]," setTimer(timerPlayer,20);");
sprintf (curInput[(*curPosY)++]," while (1)");
sprintf (curInput[(*curPosY)++]," if (getBuffer(&bufferTime,&data))");
sprintf (curInput[(*curPosY)++]," break;");
sprintf (curInput[(*curPosY)++]," //Stop");
sprintf (curInput[(*curPosY)++]," PlayOneBeat(0);");
sprintf (curInput[(*curPosY)++]," //15ms");
sprintf (curInput[(*curPosY)++]," setTimer(timerPlayer,15);");
sprintf (curInput[(*curPosY)++]," while (1)");
sprintf (curInput[(*curPosY)++]," if (getBuffer(&bufferTime,&data))");
}else if (strcmp(fileName,"\\Source\\Play.c")==0 && page==4)
{
sprintf (curInput[(*curPosY)++]," break;");
sprintf (curInput[(*curPosY)++]," }");
sprintf (curInput[(*curPosY)++]," }");
sprintf (curInput[(*curPosY)++]," return 0");
sprintf (curInput[(*curPosY)++],"}");
}
for (int i=0;i<21;i++)
putStrAndBackOnSht(textViewerSheet,16,28+i*16,BLACK,WHITE,curInput[i],75);
}
void textViewerTask_Main(struct Task *task)
{
//初始化缓冲区
char bufferArray[128];
struct Buffer bufferTime;
initBuffer(&bufferTime,128,bufferArray);
//初始化定时器
struct Timer *timerCur;
timerCur=allocTimer();
initTimer(timerCur,&bufferTime,1);
setTimer(timerCur,50);
//显示窗口
struct Sheet *textViewerSheet;
unsigned char *textViewerBuffer;
textViewerSheet=allocSheet();
textViewerBuffer=(unsigned char *)allocMem_4k(650*400,"Text Viewer UI");//申请内存空间
setBufInSheet(textViewerSheet,textViewerBuffer,650,400,-1);
char str[128];
sprintf (str,"Text Viewer: %s",task->par[0]);
makeWindow(textViewerSheet,650,400,str);
slideSheet(textViewerSheet,180,72);
updownSheet(textViewerSheet,task->winID+1);
//输入的信息
char curInput[21][75];
int curPosX=0,curPosY=0,lengthX=0,lengthY=0;//光标
unsigned char data;
int page=0;
//struct Command command;//命令
int flag=0,f1=0;
for (int i=0;i<21;i++)
curInput[i][0]='\0';
loadTextFile(task->par[0],textViewerSheet,&curPosX,&curPosY,curInput,page);
while (1)
{
flag=0;
if (window.focus!=task->winID)//焦点不在,取消光标
{
if (f1==0)
{
boxfillOnSht(textViewerSheet,16+8*curPosX,28+curPosY*16,8,15,WHITE);
refreshSubInSheet(textViewerSheet,16+8*curPosX,28+curPosY*16,8,15);
f1=1;
}
continue;
}else if (timerCur->flag==TIMER_ALLOCED)//重新获得焦点,重启光标
{
initTimer(timerCur,&bufferTime,1);
setTimer(timerCur,50);
}
f1=0;
io_cli();
//检查鼠标键盘事件
if (getBuffer(&task->bufAll.key,&data))
{
//键盘
io_sti();
flag=1;
switch (data)
{
case 0x0e://退格键
if (curPosX>2)
{
curPosX--;
lengthX--;
for (int j=curPosX;j<=lengthX;j++)
curInput[curPosY][j]=curInput[curPosY][j+1];
curInput[curPosY][lengthX] = '\0';
}
break;
case 0x4b:
if (curPosX>0)
curPosX--;
break;
case 0x4d:
if (curPosX<lengthX)
curPosX++;
break;
case 0x48:
if (curPosY>0)
{
curPosY--;
lengthX=0;
while (curInput[curPosY][lengthX] != '\0')
lengthX++;
}
break;
case 0x50:
if (curPosY<lengthY)
{
curPosY++;
lengthX=0;
while (curInput[curPosY][lengthX] != '\0')
lengthX++;
}
break;
case 0x1c://回车键
boxfillOnSht(textViewerSheet,16+8*curPosX,28+curPosY*16,8,15,WHITE);
refreshSubInSheet(textViewerSheet,16+8*curPosX,28+curPosY*16,8,15);
curPosY++;
if (curPosY>lengthY)
lengthY=curPosY;
curInput[curPosY][0] = '\0';
curPosX=0;
lengthX=0;
break;
case 0x38:
page++;
loadTextFile(task->par[0],textViewerSheet,&curPosX,&curPosY,curInput,page);
break;
case 0x1d:
page--;
loadTextFile(task->par[0],textViewerSheet,&curPosX,&curPosY,curInput,page);
break;
default:
if (data<0x80 && keyboard.keyTable[data]>0 && curPosX<50)//字母,数字
{
curInput[curPosY][curPosX++] = keyboard.keyTable[data+keyboard.isShift*0x80];
if (curPosX>lengthX)
{
curInput[curPosY][curPosX] = '\0';
lengthX=curPosX;
}
}
break;
}
if (curPosY<21)
sprintf (str,"%s",curInput[curPosY]);
else sprintf (str,"");
putStrAndBackOnSht(textViewerSheet,16,28+curPosY*16,BLACK,WHITE,str,75);
}
if (getBuffer(&task->bufAll.mouse,&data))
{
io_sti();
flag=2;
switch(data)
{
case 0:
slideSheet(textViewerSheet,mdec.x,mdec.y);
break;
case 2:
freeSheet(textViewerSheet);
//freeMem((unsigned int)curInput,20);
freeMem((unsigned int)textViewerBuffer,312*210);
deleteWindow(task);
deleteTask(task);
}
}
if (getBuffer(&bufferTime,&data))
{
io_sti();
flag=3;
switch (data)
{
case 0:
initTimer(timerCur,&bufferTime,1);
boxfillOnSht(textViewerSheet,16+8*curPosX,28+curPosY*16,8,15,WHITE);
refreshSubInSheet(textViewerSheet,16+8*curPosX,28+curPosY*16,8,15);
setTimer(timerCur,50);
break;
case 1:
initTimer(timerCur,&bufferTime,0);
boxfillOnSht(textViewerSheet,16+8*curPosX,28+curPosY*16,8,15,BLACK);
refreshSubInSheet(textViewerSheet,16+8*curPosX,28+curPosY*16,8,15);
setTimer(timerCur,50);
break;
}
}
if (flag==0)
io_sti();
}
}