1 /*__________________________________ linux-cmn.c ___________________________________*/
\r
3 /* 1 2 3 4 5 6 7 8 */
\r
4 /*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/
\r
5 /*******************************************|********************************************/
\r
7 * Copyright (c) 2007 Seiko Epson Corporation All rights reserved.
\r
9 * Copyright protection claimed includes all forms and matters of
\r
10 * copyrightable material and information now allowed by statutory or judicial
\r
11 * law or hereinafter granted, including without limitation, material generated
\r
12 * from the software programs which are displayed on the screen such as icons,
\r
13 * screen display looks, etc.
\r
16 /*******************************************|********************************************/
\r
18 /* Linux platform depend External Definitions */
\r
20 /* Function Calls */
\r
21 /* -------------------------- */
\r
22 /* void* epsmpMemAlloc (size ); */
\r
23 /* void epsmpMemFree (memblock ); */
\r
24 /* EPS_INT32 epsmpSleep (time ); */
\r
25 /* EPS_UINT32 epsmpGetTime (void ); */
\r
26 /* EPS_INT32 epsmpLockSync (void ); */
\r
27 /* EPS_INT32 epsmpUnlockSync (void ); */
\r
29 /*******************************************|********************************************/
\r
31 #if defined (unix) || (__unix)
\r
32 /*------------------------------------ Includes -------------------------------------*/
\r
33 /*******************************************|********************************************/
\r
34 #include "epson-escpr-def.h"
\r
35 #include "epson-escpr-err.h"
\r
36 #include "linux_cmn.h"
\r
37 #include <sys/times.h>
\r
40 #include <sys/ioctl.h>
\r
41 #include <sys/time.h> /* struct timeval, select() */
\r
42 /* ICANON, ECHO, TCSANOW, struct termios */
\r
43 #include <termios.h> /* tcgetattr(), tcsetattr() */
\r
44 #include <stdlib.h> /* atexit(), exit() */
\r
45 #include <unistd.h> /* read() */
\r
46 #include <stdio.h> /* printf() */
\r
47 #include <pthread.h>
\r
49 /*------------------------------ Local Grobal Variables -----------------------------*/
\r
50 /*******************************************|********************************************/
\r
51 static struct termios g_old_kbd_mode;
\r
52 static pthread_mutex_t g_mutex;
\r
54 /*---------------------------------- Local Functions ---------------------------------*/
\r
55 /*******************************************|********************************************/
\r
56 static void cooked(void);
\r
57 static void raw(void);
\r
59 /*-------------------------------- Function Declarations ----------------------------*/
\r
60 /*******************************************|********************************************/
\r
62 /*******************************************|********************************************/
\r
63 /* Function name: epsmpMemAlloc() */
\r
67 /* Name: Type: Description: */
\r
68 /* s size_t I: Number of bytes to allocate, in bytes. */
\r
71 /* pointer to new buffer */
\r
74 /* Dynamically allocates a memory buffer. */
\r
76 /*******************************************|********************************************/
\r
77 void* epsmpMemAlloc(
\r
86 /*******************************************|********************************************/
\r
87 /* Function name: epsmpMemFree() */
\r
91 /* Name: Type: Description: */
\r
92 /* p void* I: Block of memory to free. */
\r
98 /* Frees an allocated block of memory allocated in the xxxMemAlloc () function. */
\r
100 /*******************************************|********************************************/
\r
110 /*******************************************|********************************************/
\r
111 /* Function name: epsmpSleep() */
\r
115 /* Name: Type: Description: */
\r
116 /* t void* I: Interval (specified in microseconds) */
\r
118 /* Return value: */
\r
123 /* Pauses execution in the current thread for a specified number of microseconds. */
\r
125 /*******************************************|********************************************/
\r
126 EPS_INT32 epsmpSleep(
\r
136 /*******************************************|********************************************/
\r
137 /* Function name: epsmpGetTime() */
\r
141 /* Name: Type: Description: */
\r
144 /* Return value: */
\r
148 /* The time of the system is acquired. */
\r
150 /*******************************************|********************************************/
\r
151 EPS_UINT32 epsmpGetTime(void)
\r
154 return (times(&tm)*10);
\r
158 /*******************************************|********************************************/
\r
159 /* Function name: epsmpGetLocalTime() */
\r
163 /* Name: Type: Description: */
\r
164 /* epsTime EPS_LOCAL_TIME* O: time structure. */
\r
166 /* Return value: */
\r
170 /* get the system localtime. */
\r
172 /*******************************************|********************************************/
\r
173 EPS_UINT32 epsmpGetLocalTime(EPS_LOCAL_TIME *epsTime)
\r
179 t = localtime(&now);
\r
181 epsTime->year = 1900;
\r
192 /*******************************************|********************************************/
\r
193 /* Function name: epsmpLockSync() */
\r
197 /* Name: Type: Description: */
\r
200 /* Return value: */
\r
205 /* Lock the synchronization object. */
\r
207 /*******************************************|********************************************/
\r
208 EPS_INT32 epsmpLockSync(void)
\r
210 return pthread_mutex_lock(&g_mutex);
\r
214 /*******************************************|********************************************/
\r
215 /* Function name: epsmpUnlockSync() */
\r
219 /* Name: Type: Description: */
\r
222 /* Return value: */
\r
227 /* UnLock the synchronization object. */
\r
229 /*******************************************|********************************************/
\r
230 EPS_INT32 epsmpUnlockSync(void)
\r
232 return pthread_mutex_unlock(&g_mutex);
\r
237 static void cooked(void)
\r
239 tcsetattr(0, TCSANOW, &g_old_kbd_mode);
\r
242 static void raw(void)
\r
245 struct termios new_kbd_mode;
\r
249 tcgetattr(0, &g_old_kbd_mode);
\r
250 memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
\r
251 new_kbd_mode.c_lflag &= ~(ICANON | ECHO);
\r
252 new_kbd_mode.c_cc[VTIME] = 0;
\r
253 new_kbd_mode.c_cc[VMIN] = 1;
\r
254 tcsetattr(0, TCSANOW, &new_kbd_mode);
\r
260 EPS_INT32 _kbhit(void)
\r
262 struct timeval timeout;
\r
263 fd_set read_handles;
\r
267 FD_ZERO(&read_handles);
\r
268 FD_SET(0, &read_handles);
\r
269 timeout.tv_sec = timeout.tv_usec = 0;
\r
270 status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
\r
271 tcsetattr(0, TCSANOW, &g_old_kbd_mode);
\r
274 printf("select() failed in kbhit()\n");
\r
280 EPS_INT32 _getch(void)
\r
282 unsigned char temp;
\r
285 if(read(0, &temp, 1) != 1){
\r
286 tcsetattr(0, TCSANOW, &g_old_kbd_mode);
\r
289 tcsetattr(0, TCSANOW, &g_old_kbd_mode);
\r
294 void _endthread(void)
\r
296 pthread_exit(NULL);
\r
300 void InitMutex(void)
\r
302 pthread_mutex_init(&g_mutex, NULL); /*default (fast) mutex*/
\r
305 void TermMutex(void)
\r
307 pthread_mutex_destroy(&g_mutex);
\r
313 /*__________________________________ linux-cmn.c ___________________________________*/
\r
315 /*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/
\r
316 /* 1 2 3 4 5 6 7 8 */
\r
317 /*******************************************|********************************************/
\r
318 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
\r
319 /***** End of File *** End of File *** End of File *** End of File *** End of File ******/
\r
320 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
\r