#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <wchar.h>
#include <termios.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include "common.h"
#include "definitions.h"
Go to the source code of this file.
|
void | sound () |
| Makes a beep sound.
|
|
void | clearCLI (void) |
| Call the system function to clear the cli.
|
|
void | exitFailure (void) |
| Exit the program after cleaning everything on screen.
|
|
void | delayTimer (int time) |
| Call the system sleep.
|
|
int | intLen (int value) |
| Calculate the length of a given number.
|
|
void | widthSpacing (int filledWidth) |
| Apply an horizontal spacing based on window width and what need to be printed.
|
|
void | printCentered (wchar_t *text) |
| Print text centered horizontally based on string length.
|
|
void | heightSpacing (int filledHeight) |
| Apply a vertical spacing based on window heigth and what need to be printed.
|
|
void | u_cleanup (void) |
|
int | u_kbhit (void) |
|
int | u_getchar (void) |
|
int | waitUserInput (void) |
| Loop until user press a valid key.
|
|
void | waitUser (void) |
| Wait until Enter key is pressed.
|
|
|
const wchar_t | chSq = 0x2705 |
| Assign unicode character (green check square) to this global variable.
|
|
const wchar_t | crSq = 0x274E |
| Assign unicode character (green crossed square) to this global variable.
|
|
const wchar_t | eSq = 0x2B1C |
| Assign unicode character (white square) to this global variable.
|
|
const wchar_t | fSq = 0x2B1B |
| Assign unicode character (black square) to this global variable.
|
|
const int | timeLimit = 1000 |
| Time waiting before letting the tetromino fall one row below.
|
|
struct winsize | w |
| Global variable type struct winsize used for terminal width and height.
|
|
◆ clearCLI()
Call the system function to clear the cli.
Definition at line 25 of file common.c.
25 {
26
27 int res;
28
29 res = system("clear");
30 if (res == 1){
32 exit(EXIT_FAILURE);
33 }
34}
void clearCLI(void)
Call the system function to clear the cli.
◆ delayTimer()
void delayTimer |
( |
int | timeDelay | ) |
|
Call the system sleep.
- Parameters
-
[in] | timeDelay | Time of the sleep in seconds. |
Definition at line 41 of file common.c.
◆ exitFailure()
void exitFailure |
( |
void | | ) |
|
Exit the program after cleaning everything on screen.
Definition at line 36 of file common.c.
36 {
38 exit(EXIT_FAILURE);
39}
◆ heightSpacing()
void heightSpacing |
( |
int | filledHeight | ) |
|
Apply a vertical spacing based on window heigth and what need to be printed.
- Parameters
-
[in] | filledHeight | Height of what is printed that need to be centered. |
Definition at line 76 of file common.c.
76 {
77
78 int i;
79
80 ioctl(STDOUT_FILENO, TIOCGWINSZ, &
w);
81 for(i=0; i<(
w.ws_row-filledHeight)/2; i++)
82 wprintf(L"\r\n");
83}
struct winsize w
Global variable type struct winsize used for terminal width and height.
◆ intLen()
Calculate the length of a given number.
- Parameters
-
[in] | value | Number which needs his length calculated. |
[in] | length | Return the length of the number. |
Definition at line 47 of file common.c.
47 {
48
49 int length = 0;
50
51 if (value != 0){
52 while(value!=0){
53 value = value/10;
54 length++;
55 }
56 }
57
58 return (length==0)?1:length;
59
60}
◆ printCentered()
void printCentered |
( |
wchar_t * | text | ) |
|
Print text centered horizontally based on string length.
- Parameters
-
[in] | text | Text that need to be printed on screen (doesn't include special characters). |
Definition at line 71 of file common.c.
71 {
73 wprintf(L"%ls\r\n", text);
74}
void widthSpacing(int filledWidth)
Apply an horizontal spacing based on window width and what need to be printed.
◆ sound()
Makes a beep sound.
Definition at line 21 of file common.c.
21 {
22 wprintf(L"\a");
23}
◆ u_cleanup()
Definition at line 87 of file common.c.
87 {
88 tcsetattr(0, TCSANOW, &orig_term);
89}
◆ u_getchar()
Definition at line 118 of file common.c.
118 {
119
120 int ret;
121 unsigned char buf;
122
123 if(read(0, &buf, 1)!=1)
124 ret=0;
125 else
126 ret=buf;
127 return ret;
128}
◆ u_kbhit()
Definition at line 91 of file common.c.
91 {
92
93 struct termios t;
94 fd_set rfd;
95 struct timeval to;
96 static int first_hit=0;
97
98 if(first_hit==0){
99 if(tcgetattr(0, &t)!=0)
100 exit(0);
101 orig_term=t;
102 cfmakeraw(&t);
103 if(tcsetattr(0, TCSANOW, &t)!=0)
104 exit(0);
106 first_hit=1;
107 }
108
109 FD_ZERO(&rfd);
110 FD_SET(0, &rfd);
111 to.tv_sec=0;
112 to.tv_usec=0;
113 if(select(1, &rfd, NULL, NULL, &to)==1)
114 return 1;
115 return 0;
116}
◆ waitUser()
Wait until Enter key is pressed.
Definition at line 146 of file common.c.
146 {
147 int key=2;
150 }
151}
int waitUserInput(void)
Loop until user press a valid key.
◆ waitUserInput()
int waitUserInput |
( |
void | | ) |
|
Loop until user press a valid key.
- Parameters
-
[in] | time | Time in microseconds which the usleep is called. |
Definition at line 130 of file common.c.
130 {
131
132 int key=0;
133
134 while(1){
139 return key;
140 }
142 }
144}
void exitFailure(void)
Exit the program after cleaning everything on screen.
◆ widthSpacing()
void widthSpacing |
( |
int | filledWidth | ) |
|
Apply an horizontal spacing based on window width and what need to be printed.
- Parameters
-
[in] | filledWidth | Width of what is printed that need to be centered. |
Definition at line 62 of file common.c.
62 {
63
64 int i;
65
66 ioctl(STDOUT_FILENO, TIOCGWINSZ, &
w);
67 for(i=0; i<(
w.ws_col-filledWidth)/2; i++)
68 wprintf(L" ");
69}
◆ chSq
const wchar_t chSq = 0x2705 |
Assign unicode character (green check square) to this global variable.
Definition at line 14 of file common.c.
◆ crSq
const wchar_t crSq = 0x274E |
Assign unicode character (green crossed square) to this global variable.
Definition at line 15 of file common.c.
◆ eSq
const wchar_t eSq = 0x2B1C |
Assign unicode character (white square) to this global variable.
Definition at line 16 of file common.c.
◆ fSq
const wchar_t fSq = 0x2B1B |
Assign unicode character (black square) to this global variable.
Definition at line 17 of file common.c.
◆ timeLimit
const int timeLimit = 1000 |
Time waiting before letting the tetromino fall one row below.
Definition at line 19 of file common.c.
Global variable type struct winsize
used for terminal width and height.
Definition at line 45 of file common.c.