Blame view

ui.c 3.82 KB
ea1218f1   henyxia   UI finished
1
2
3
4
5
6
7
8
9
10
  #include <stdio.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <string.h>
  #include <time.h>
  #include "ui.h"
  
  #define	TIME_LENGTH			24
  #define	HEADER_TEXT_LENGTH	24
  #define	LOG_LINES			25
297a72df   henyxia   Added sender
11
  #define	LOG_LENGTH			82
cdaa873f   henyxia   Log fixed
12
13
  #define	IPS					20
  #define	SCREEN_TIME			1000000/IPS
19082eeb   henyxia   UI design not wor...
14
  #define	SPACES				"                                                                                                                                                                                                                                                                  "
ea1218f1   henyxia   UI finished
15
  
ea1218f1   henyxia   UI finished
16
17
18
19
  char	uilog[LOG_LINES][LOG_LENGTH];
  char	started[TIME_LENGTH];
  char	uid[HEADER_TEXT_LENGTH];
  char	uidDate[HEADER_TEXT_LENGTH];
cdaa873f   henyxia   Log fixed
20
21
22
23
24
25
26
27
28
29
30
31
32
  pid_t	mainPid;
  
  void* drawUI(void* we)
  {
  	while(1)
  	{
  		printf("\x1b[2J\x1b[1;1H");
  		displayUI();
  		usleep(SCREEN_TIME);
  	}
  
  	return NULL;
  }
ea1218f1   henyxia   UI finished
33
34
35
  
  void initUILog()
  {
cdaa873f   henyxia   Log fixed
36
37
  	started[0]='\0';
  	mainPid = getpid();
ea1218f1   henyxia   UI finished
38
39
40
41
42
43
  	uid[0]='\0';
  	uidDate[0]='\0';
  	for(int i=0; i<LOG_LINES; i++)
  		uilog[i][0] = '\0';
  }
  
19082eeb   henyxia   UI design not wor...
44
  char* fillHeaderWithSpaces(char* buf, char* text)
ea1218f1   henyxia   UI finished
45
  {
19082eeb   henyxia   UI design not wor...
46
  	char tmpretheader[HEADER_TEXT_LENGTH+1];
ea1218f1   henyxia   UI finished
47
48
49
50
  	strcpy(tmpretheader, text);
  	while(strlen(tmpretheader) < HEADER_TEXT_LENGTH)
  		strcat(tmpretheader, " ");
  
19082eeb   henyxia   UI design not wor...
51
52
53
54
55
  	for(int i=0; i<strlen(tmpretheader); i++)
  		buf[i] = tmpretheader[i];
  	buf[strlen(tmpretheader)] = '\0';
  
  	return buf;
ea1218f1   henyxia   UI finished
56
57
58
59
  }
  
  void addToLog(char* newStr)
  {
cdaa873f   henyxia   Log fixed
60
  	for(int i=LOG_LINES-2; i>=0; i--)
ea1218f1   henyxia   UI finished
61
62
63
64
  		strcpy(uilog[i+1], uilog[i]);
  	strcpy(uilog[0], newStr);
  }
  
19082eeb   henyxia   UI design not wor...
65
  char* fillLogWithSpaces(char* buf, char* text)
ea1218f1   henyxia   UI finished
66
  {
19082eeb   henyxia   UI design not wor...
67
  	strncpy(buf, text, LOG_LENGTH);
19082eeb   henyxia   UI design not wor...
68
  	if(strlen(buf) > 0)
297a72df   henyxia   Added sender
69
  		strncat(buf, SPACES, LOG_LENGTH-strlen(buf)-1);
19082eeb   henyxia   UI design not wor...
70
  	else
297a72df   henyxia   Added sender
71
  		strncat(buf, SPACES, LOG_LENGTH-strlen(buf)-1-12);
19082eeb   henyxia   UI design not wor...
72
73
  
  	return buf;
ea1218f1   henyxia   UI finished
74
75
76
77
78
79
80
81
82
83
  }
  
  
  void setStartTime(char* sT)
  {
  	strcpy(started, sT);
  }
  
  void displayUI()
  {
19082eeb   henyxia   UI design not wor...
84
  	char	buffer[LOG_LENGTH];
ea1218f1   henyxia   UI finished
85
86
  	//header
  	printf("\u250F\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n");
19082eeb   henyxia   UI design not wor...
87
88
89
  	printf("\u2503 %s \u2503      T H E      \u2503", fillHeaderWithSpaces(buffer, "Started since"));
     	printf(" %s \u2503\n", fillHeaderWithSpaces(buffer, "No Tag Detected"));
  	printf("\u2503 %s \u2503    T W E E K    \u2503 ", fillHeaderWithSpaces(buffer, started));
ea1218f1   henyxia   UI finished
90
  	sprintf(buffer, "%s", strlen(uidDate) > 0 ? uidDate : "Waiting for a tag");
19082eeb   henyxia   UI design not wor...
91
  	printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer));
cdaa873f   henyxia   Log fixed
92
  	sprintf(buffer, "PID %d", mainPid);
19082eeb   henyxia   UI design not wor...
93
  	printf("\u2503 %s \u2503  P R O J E C T  \u2503 ", fillHeaderWithSpaces(buffer, buffer));
ea1218f1   henyxia   UI finished
94
  	sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope");
19082eeb   henyxia   UI design not wor...
95
96
  	printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer));
  	printf("\u2522\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n");
ea1218f1   henyxia   UI finished
97
  	//body
19082eeb   henyxia   UI design not wor...
98
99
100
101
102
103
  	int i=0;
  	while(i<LOG_LINES)
  	{
  		printf("\u2503 %s \u2503\n", fillLogWithSpaces(buffer, uilog[LOG_LINES-i-1]));
  		i++;
  	}
ea1218f1   henyxia   UI finished
104
105
  	printf("\u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251B\n");
  }