summaryrefslogtreecommitdiff
path: root/src/linux_cmn.c
blob: c47b8c10f0dde1bfa02388a35bd11058f0556bbb (plain)
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
320
/*__________________________________   linux-cmn.c   ___________________________________*/

/*       1         2         3         4         5         6         7         8        */
/*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/
/*******************************************|********************************************/
/*
 *   Copyright (c) 2007  Seiko Epson Corporation                 All rights reserved.
 *
 *   Copyright protection claimed includes all forms and matters of
 *   copyrightable material and information now allowed by statutory or judicial
 *   law or hereinafter granted, including without limitation, material generated
 *   from the software programs which are displayed on the screen such as icons,
 *   screen display looks, etc.
 *
 */
/*******************************************|********************************************/
/*                                                                                      */
/*                      Linux platform depend External Definitions                      */
/*                                                                                      */
/*                                    Function Calls                                    */
/*                              --------------------------                              */
/*                  void*		epsmpMemAlloc	(size								);  */
/*                  void		epsmpMemFree	(memblock							);  */
/*                  EPS_INT32   epsmpSleep		(time								);  */
/*                  EPS_UINT32  epsmpGetTime	(void								);  */
/*                  EPS_INT32   epsmpLockSync	(void								);  */
/*                  EPS_INT32   epsmpUnlockSync (void								);  */
/*                                                                                      */
/*******************************************|********************************************/

#if defined (unix) || (__unix)
/*------------------------------------  Includes   -------------------------------------*/
/*******************************************|********************************************/
#include "epson-escpr-def.h"
#include "epson-escpr-err.h"
#include "linux_cmn.h"
#include <sys/times.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/time.h> /* struct timeval, select() */
/* ICANON, ECHO, TCSANOW, struct termios */
#include <termios.h> /* tcgetattr(), tcsetattr() */
#include <stdlib.h> /* atexit(), exit() */
#include <unistd.h> /* read() */
#include <stdio.h> /* printf() */
#include <pthread.h>

/*------------------------------  Local Grobal Variables   -----------------------------*/
/*******************************************|********************************************/
static struct termios g_old_kbd_mode;
static pthread_mutex_t g_mutex;

/*----------------------------------  Local Functions  ---------------------------------*/
/*******************************************|********************************************/
static void cooked(void);
static void raw(void);

/*--------------------------------  Function Declarations   ----------------------------*/
/*******************************************|********************************************/

/*******************************************|********************************************/
/* Function name:   epsmpMemAlloc()                                                     */
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:        Type:               Description:                                        */
/* s            size_t              I: Number of bytes to allocate, in bytes.           */
/*                                                                                      */
/* Return value:                                                                        */
/*      pointer to new buffer                                                           */
/*                                                                                      */
/* Description:                                                                         */
/*      Dynamically allocates a memory buffer.                                          */
/*                                                                                      */
/*******************************************|********************************************/
void* epsmpMemAlloc(
					
			size_t s
			
){
	return malloc(s);
}


/*******************************************|********************************************/
/* Function name:   epsmpMemFree()                                                      */
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:        Type:               Description:                                        */
/* p            void*               I: Block of memory to free.                         */
/*                                                                                      */
/* Return value:                                                                        */
/*  N/A                                                                                 */
/*                                                                                      */
/* Description:                                                                         */
/*      Frees an allocated block of memory allocated in the xxxMemAlloc () function.    */
/*                                                                                      */
/*******************************************|********************************************/
void epsmpMemFree(
				  
			void* p
			
){
	free(p);
}


/*******************************************|********************************************/
/* Function name:   epsmpSleep()                                                        */
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:        Type:               Description:                                        */
/* t            void*               I: Interval (specified in microseconds)             */
/*                                                                                      */
/* Return value:                                                                        */
/*      0               - Success                                                       */
/*      -1              - Failure                                                       */
/*                                                                                      */
/* Description:                                                                         */
/*      Pauses execution in the current thread for a specified number of microseconds.  */
/*                                                                                      */
/*******************************************|********************************************/
EPS_INT32 epsmpSleep(
					 
			EPS_UINT32 t
			
){
	usleep( t);
	return 0;
}


/*******************************************|********************************************/
/* Function name:   epsmpGetTime()                                                      */
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:        Type:               Description:                                        */
/* N/A                                                                                  */
/*                                                                                      */
/* Return value:                                                                        */
/*      System time                                                                     */
/*                                                                                      */
/* Description:                                                                         */
/*      The time of the system is acquired.                                             */
/*                                                                                      */
/*******************************************|********************************************/
EPS_UINT32 epsmpGetTime(void)
{
	struct tms tm;
	return (times(&tm)*10);
}


/*******************************************|********************************************/
/* Function name:   epsmpGetLocalTime()                                                 */
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:        Type:               Description:                                        */
/* epsTime      EPS_LOCAL_TIME*     O: time structure.                                  */
/*                                                                                      */
/* Return value:                                                                        */
/*      0                                                                               */
/*                                                                                      */
/* Description:                                                                         */
/*      get the system localtime.                                                       */
/*                                                                                      */
/*******************************************|********************************************/
EPS_UINT32 epsmpGetLocalTime(EPS_LOCAL_TIME *epsTime)
{
	time_t now;
	struct tm *t;

	now = time(NULL);
	t = localtime(&now);

	epsTime->year =  1900;
	epsTime->mon =  1;
	epsTime->day = 1;
	epsTime->hour = 1;
	epsTime->min = 1;
	epsTime->sec = 1;

	return 0;
}


/*******************************************|********************************************/
/* Function name:   epsmpLockSync()                                                     */
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:        Type:               Description:                                        */
/* N/A                                                                                  */
/*                                                                                      */
/* Return value:                                                                        */
/*      0               - Success                                                       */
/*      -1              - Failure                                                       */
/*                                                                                      */
/* Description:                                                                         */
/*      Lock the synchronization object.                                                */
/*                                                                                      */
/*******************************************|********************************************/
EPS_INT32 epsmpLockSync(void)
{
	return pthread_mutex_lock(&g_mutex);
}


/*******************************************|********************************************/
/* Function name:   epsmpUnlockSync()                                                   */
/*                                                                                      */
/* Arguments                                                                            */
/* ---------                                                                            */
/* Name:        Type:               Description:                                        */
/* N/A                                                                                  */
/*                                                                                      */
/* Return value:                                                                        */
/*      0               - Success                                                       */
/*      -1              - Failure                                                       */
/*                                                                                      */
/* Description:                                                                         */
/*      UnLock the synchronization object.                                              */
/*                                                                                      */
/*******************************************|********************************************/
EPS_INT32 epsmpUnlockSync(void)
{
	return pthread_mutex_unlock(&g_mutex);
}



static void cooked(void)
{
	tcsetattr(0, TCSANOW, &g_old_kbd_mode);
}

static void raw(void)
{
	static char init;
	struct termios new_kbd_mode;

	if(init)
		return;
	tcgetattr(0, &g_old_kbd_mode);
	memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
	new_kbd_mode.c_lflag &= ~(ICANON | ECHO);
	new_kbd_mode.c_cc[VTIME] = 0;
	new_kbd_mode.c_cc[VMIN] = 1;
	tcsetattr(0, TCSANOW, &new_kbd_mode);
	atexit(cooked);

	init = 1;
}

EPS_INT32 _kbhit(void)
{
	struct timeval timeout;
	fd_set read_handles;
	int status;

	raw();
	FD_ZERO(&read_handles);
	FD_SET(0, &read_handles);
	timeout.tv_sec = timeout.tv_usec = 0;
	status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
	tcsetattr(0, TCSANOW, &g_old_kbd_mode);
	if(status < 0)
	{
		printf("select() failed in kbhit()\n");
		exit(1);
	}
	return status;
}

EPS_INT32 _getch(void)
{
	unsigned char temp;

	raw();
	if(read(0, &temp, 1) != 1){
		tcsetattr(0, TCSANOW, &g_old_kbd_mode);
		return 0;
	}
	tcsetattr(0, TCSANOW, &g_old_kbd_mode);
	return temp;
}


void _endthread(void)
{
	pthread_exit(NULL);
}


void InitMutex(void)
{
	pthread_mutex_init(&g_mutex, NULL); /*default (fast) mutex*/
}

void TermMutex(void)
{
	pthread_mutex_destroy(&g_mutex);
}


#endif

/*__________________________________   linux-cmn.c   ___________________________________*/
  
/*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/
/*       1         2         3         4         5         6         7         8        */
/*******************************************|********************************************/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/***** End of File *** End of File *** End of File *** End of File *** End of File ******/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/