xref: /haiku/src/tests/servers/input/view_input_device/ViewInputDevice.cpp (revision 2aec91b51e53ac69b50ec06b629aa200212231bd)
1*2aec91b5SJérôme Duval //-------------------------------------------------------------------------
2*2aec91b5SJérôme Duval // Handy InputDevice that queue all events from app_server's ViewHWInterface.
3*2aec91b5SJérôme Duval //-------------------------------------------------------------------------
4*2aec91b5SJérôme Duval /*
5*2aec91b5SJérôme Duval 	Copyright 1999, Be Incorporated.   All Rights Reserved.
6*2aec91b5SJérôme Duval 	This file may be used under the terms of the Be Sample Code License.
7*2aec91b5SJérôme Duval */
8*2aec91b5SJérôme Duval 
9*2aec91b5SJérôme Duval #include <stdlib.h>
10*2aec91b5SJérôme Duval #include <string.h>
11*2aec91b5SJérôme Duval #include <unistd.h>
12*2aec91b5SJérôme Duval 
13*2aec91b5SJérôme Duval #include <Debug.h>
14*2aec91b5SJérôme Duval #include <FindDirectory.h>
15*2aec91b5SJérôme Duval #include <InterfaceDefs.h>
16*2aec91b5SJérôme Duval #include <Message.h>
17*2aec91b5SJérôme Duval #include <OS.h>
18*2aec91b5SJérôme Duval #include <add-ons/input_server/InputServerDevice.h>
19*2aec91b5SJérôme Duval #include <SupportDefs.h>
20*2aec91b5SJérôme Duval 
21*2aec91b5SJérôme Duval extern "C" _EXPORT BInputServerDevice* instantiate_input_device();
22*2aec91b5SJérôme Duval 
23*2aec91b5SJérôme Duval class ViewInputDevice;
24*2aec91b5SJérôme Duval 
25*2aec91b5SJérôme Duval class ViewInputDevice : public BInputServerDevice {
26*2aec91b5SJérôme Duval public:
27*2aec91b5SJérôme Duval 	ViewInputDevice();
28*2aec91b5SJérôme Duval 	virtual ~ViewInputDevice();
29*2aec91b5SJérôme Duval 
30*2aec91b5SJérôme Duval 	virtual status_t Start(const char *device, void *cookie);
31*2aec91b5SJérôme Duval 	virtual	status_t Stop(const char *device, void *cookie);
32*2aec91b5SJérôme Duval 
33*2aec91b5SJérôme Duval private:
34*2aec91b5SJérôme Duval 	thread_id _Thread;
35*2aec91b5SJérôme Duval 	static int32 _StartWatchPort(void *arg);
36*2aec91b5SJérôme Duval 	void _WatchPort();
37*2aec91b5SJérôme Duval 	port_id _Port;
38*2aec91b5SJérôme Duval };
39*2aec91b5SJérôme Duval 
40*2aec91b5SJérôme Duval 
instantiate_input_device()41*2aec91b5SJérôme Duval BInputServerDevice* instantiate_input_device()
42*2aec91b5SJérôme Duval {
43*2aec91b5SJérôme Duval 	return (new ViewInputDevice());
44*2aec91b5SJérôme Duval }
45*2aec91b5SJérôme Duval 
46*2aec91b5SJérôme Duval 
ViewInputDevice()47*2aec91b5SJérôme Duval ViewInputDevice::ViewInputDevice()
48*2aec91b5SJérôme Duval 	: BInputServerDevice()
49*2aec91b5SJérôme Duval {
50*2aec91b5SJérôme Duval 	input_device_ref *device[2];
51*2aec91b5SJérôme Duval 	input_device_ref mover = {"ViewInputDevice", B_POINTING_DEVICE, NULL};
52*2aec91b5SJérôme Duval 	device[0] = &mover;
53*2aec91b5SJérôme Duval 	device[1] = NULL;
54*2aec91b5SJérôme Duval 	_Port = -1;
55*2aec91b5SJérôme Duval 
56*2aec91b5SJérôme Duval 	RegisterDevices(device);
57*2aec91b5SJérôme Duval }
58*2aec91b5SJérôme Duval 
59*2aec91b5SJérôme Duval 
~ViewInputDevice()60*2aec91b5SJérôme Duval ViewInputDevice::~ViewInputDevice()
61*2aec91b5SJérôme Duval {
62*2aec91b5SJérôme Duval }
63*2aec91b5SJérôme Duval 
64*2aec91b5SJérôme Duval 
Start(const char * device,void * cookie)65*2aec91b5SJérôme Duval status_t ViewInputDevice::Start(const char *device, void *cookie)
66*2aec91b5SJérôme Duval {
67*2aec91b5SJérôme Duval 	_Port = find_port("ViewInputDevice");
68*2aec91b5SJérôme Duval 	if(_Port < 0) {
69*2aec91b5SJérôme Duval 		_sPrintf("ViewInputDevice: find_port error: (0x%x) %s\n",_Port,strerror(_Port));
70*2aec91b5SJérôme Duval 		return(_Port);
71*2aec91b5SJérôme Duval 	}
72*2aec91b5SJérôme Duval 	_Thread = spawn_thread(_StartWatchPort, device, B_REAL_TIME_DISPLAY_PRIORITY+4, this);
73*2aec91b5SJérôme Duval 	resume_thread(_Thread);
74*2aec91b5SJérôme Duval 	return (B_NO_ERROR);
75*2aec91b5SJérôme Duval }
76*2aec91b5SJérôme Duval 
77*2aec91b5SJérôme Duval 
Stop(const char * device,void * cookie)78*2aec91b5SJérôme Duval status_t ViewInputDevice::Stop(const char *device, void *cookie)
79*2aec91b5SJérôme Duval {
80*2aec91b5SJérôme Duval 	kill_thread(_Thread);
81*2aec91b5SJérôme Duval 	_Port = -1;
82*2aec91b5SJérôme Duval 	return (B_NO_ERROR);
83*2aec91b5SJérôme Duval }
84*2aec91b5SJérôme Duval 
_StartWatchPort(void * arg)85*2aec91b5SJérôme Duval int32 ViewInputDevice::_StartWatchPort(void *arg)
86*2aec91b5SJérôme Duval {
87*2aec91b5SJérôme Duval 	ViewInputDevice *self = (ViewInputDevice*)arg;
88*2aec91b5SJérôme Duval 	self->_WatchPort();
89*2aec91b5SJérôme Duval 	return (B_NO_ERROR);
90*2aec91b5SJérôme Duval }
91*2aec91b5SJérôme Duval 
_WatchPort()92*2aec91b5SJérôme Duval void ViewInputDevice::_WatchPort()
93*2aec91b5SJérôme Duval {
94*2aec91b5SJérôme Duval 	int32 code;
95*2aec91b5SJérôme Duval 	ssize_t length;
96*2aec91b5SJérôme Duval 	char *buffer;
97*2aec91b5SJérôme Duval 	BMessage *event;
98*2aec91b5SJérôme Duval 	status_t err;
99*2aec91b5SJérôme Duval 
100*2aec91b5SJérôme Duval 	while (true) {
101*2aec91b5SJérôme Duval 		// Block until we find the size of the next message
102*2aec91b5SJérôme Duval 		length = port_buffer_size(_Port);
103*2aec91b5SJérôme Duval 		buffer = (char*)malloc(length);
104*2aec91b5SJérôme Duval 		event = NULL;
105*2aec91b5SJérôme Duval 		event = new BMessage();
106*2aec91b5SJérôme Duval 		err = read_port(_Port, &code, buffer, length);
107*2aec91b5SJérôme Duval 		if(err != length) {
108*2aec91b5SJérôme Duval 			if(err >= 0) {
109*2aec91b5SJérôme Duval 				_sPrintf("ViewInputDevice: failed to read full packet (read %u of %u)\n",err,length);
110*2aec91b5SJérôme Duval 			} else {
111*2aec91b5SJérôme Duval 				_sPrintf("ViewInputDevice: read_port error: (0x%x) %s\n",err,strerror(err));
112*2aec91b5SJérôme Duval 			}
113*2aec91b5SJérôme Duval 		} else if ((err = event->Unflatten(buffer)) < 0) {
114*2aec91b5SJérôme Duval 			_sPrintf("ViewInputDevice: (0x%x) %s\n",err,strerror(err));
115*2aec91b5SJérôme Duval 		} else {
116*2aec91b5SJérôme Duval 			EnqueueMessage(event);
117*2aec91b5SJérôme Duval 			event = NULL;
118*2aec91b5SJérôme Duval 		}
119*2aec91b5SJérôme Duval 		free( buffer);
120*2aec91b5SJérôme Duval 		if(event!=NULL) {
121*2aec91b5SJérôme Duval 			delete(event);
122*2aec91b5SJérôme Duval 			event = NULL;
123*2aec91b5SJérôme Duval 		}
124*2aec91b5SJérôme Duval 	}
125*2aec91b5SJérôme Duval }
126*2aec91b5SJérôme Duval 
127