fix dependency
[profile/ivi/ico-vic-carsimulator.git] / src / CJoyStick.h
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the 
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 /**
11  * @brief   Gets the value of the joystick operation
12  * @file    CJoyStick.h
13  */
14
15 #ifndef CJOYSTICK_H_
16 #define CJOYSTICK_H_
17
18 #include <fcntl.h>
19 #include <linux/joystick.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <poll.h>
24 #include <string>
25 #include <queue>
26 #include <pthread.h>
27 #include <errno.h>
28 #include <sys/time.h>
29 #include "ico-util/ico_log.h"
30
31 #define D_DEV_DIR_PATH      "/dev/input/"
32 #define D_DEV_NAME_PARTS_JS "js"
33 #define D_DEV_NAME_G25      "Driving Force GT"
34 #define D_DEV_NAME_G27      "G27 Racing Wheel"
35
36 template <typename T>
37 class CJoyStickQueue
38 {
39 public:
40     CJoyStickQueue()
41     {
42 /*
43         pthread_mutex_init(&mutex, NULL);
44         pthread_cond_init(&cond, NULL);
45 */
46         mutex = PTHREAD_MUTEX_INITIALIZER;
47         cond = PTHREAD_COND_INITIALIZER;
48     }
49
50     ~CJoyStickQueue()
51     {
52         if (pthread_mutex_destroy(&mutex) != 0) {
53             ICO_ERR("pthread_mutex_destroy error[errno=%d]", errno);
54         }
55         if (pthread_cond_destroy(&cond) != 0) {
56             ICO_ERR("pthread_cond_destroy error[errno=%d]", errno);
57         }
58     }
59
60     int size()
61     {
62         if (pthread_mutex_lock(&mutex) != 0) {
63             ICO_ERR("pthread_mutex_lock error[errno=%d]", errno);
64         }
65         int ret = mQueue.size();
66         if (pthread_mutex_unlock(&mutex) != 0) {
67             ICO_ERR("pthread_mutex_unlock error[errno=%d]", errno);
68         }
69         return ret;
70     }
71
72     int pop(T& item)
73     {
74         struct timeval now;
75         struct timespec timeout;
76         long nsec;
77
78         if (pthread_mutex_lock(&mutex) != 0) {
79             ICO_ERR("pthread_mutex_lock error[errno=%d]", errno);
80         }
81
82         if (!mQueue.size()) {
83             gettimeofday(&now, NULL);
84             nsec = now.tv_usec * 1000 + 50*1000000;
85             timeout.tv_nsec = nsec % 1000000000;
86             if (nsec < 1000000000) {
87                 timeout.tv_sec = now.tv_sec;
88             } else {
89                 timeout.tv_sec = now.tv_sec + 1;
90             }
91             if (pthread_cond_timedwait(&cond, &mutex, &timeout) != 0){
92                 if (pthread_mutex_unlock(&mutex) != 0) {
93                     ICO_ERR("pthread_mutex_unlock error[errno=%d]", errno);
94                 }
95                 return -1;
96             }
97         }
98
99         item = mQueue.front();
100         mQueue.pop();
101
102         if (pthread_mutex_unlock(&mutex) != 0) {
103             ICO_ERR("pthread_mutex_unlock error[errno=%d]", errno);
104         }
105
106         return 0;
107     }
108
109     void push(T item)
110     {
111         if (pthread_mutex_lock(&mutex) != 0) {
112             ICO_ERR("pthread_mutex_lock error[errno=%d]", errno);
113         }
114
115         mQueue.push(item);
116         pthread_cond_signal(&cond);
117
118         if (pthread_mutex_unlock(&mutex) != 0) {
119             ICO_ERR("pthread_mutex_unlock error[errno=%d]", errno);
120         }
121     }
122
123 private:
124     pthread_mutex_t mutex;
125     pthread_cond_t cond;
126     std::queue<T> mQueue;
127 };
128
129 class CJoyStick
130 {
131   public:
132             CJoyStick();
133     virtual ~CJoyStick();
134
135     virtual int Open();
136     virtual int Close();
137     virtual int Read(int *number, int *value);
138     virtual int ReadData();
139
140     int GetAxisCount() const;
141     int GetButtonsCount() const;
142
143     enum TYPE
144     {
145         AXIS = 0,
146         BUTTONS
147     };
148     int  deviceOpen(const std::string& dirNM, const std::string& fileParts,
149                     const std::string& deviceNM, std::string& getDevice);
150     void getDevices(const std::string& dir,
151                     const std::vector<std::string>& matching,
152                     std::vector<std::string>& filesList) const;
153     virtual bool getDeviceName(int fd, char* devNM, size_t sz);
154     bool recvJS();
155     static void *loop(void *arg);
156
157   protected:
158     char m_strJoyStick[64];
159     int m_nJoyStickID;
160
161     unsigned char m_ucAxes;
162     unsigned char m_ucButtons;
163     char m_strDevName[80];
164
165     fd_set m_stSet;
166     struct pollfd fds;
167      CJoyStickQueue<struct js_event > queue;
168  private:
169     pthread_t  m_threadid;
170 };
171
172 #endif /* CJOYSTICK_H_ */
173 /**
174  * End of File.(CJoyStick.h)
175  */