char_mouse.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Jiri Svoboda
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00041 #include <ipc/mouse.h>
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <async.h>
00045 #include <errno.h>
00046 #include <devmap.h>
00047 
00048 #include <char_mouse.h>
00049 #include <mouse_port.h>
00050 #include <mouse_proto.h>
00051 
00052 #define NAME       "mouse"
00053 #define NAMESPACE  "hid_in"
00054 
00055 int client_phone = -1;
00056 
00057 void mouse_handle_byte(int byte)
00058 {
00059 /*      printf("mouse byte: 0x%x\n", byte);*/
00060         mouse_proto_parse_byte(byte);
00061 }
00062 
00063 void mouse_ev_btn(int button, int press)
00064 {
00065 /*      printf("ev_btn: button %d, press %d\n", button, press);*/
00066         if (client_phone != -1) {
00067                 async_msg_2(client_phone, MEVENT_BUTTON, button, press);
00068         }
00069 }
00070 
00071 void mouse_ev_move(int dx, int dy)
00072 {
00073 /*      printf("ev_move: dx %d, dy %d\n", dx, dy);*/
00074         if (client_phone != -1)
00075                 async_msg_2(client_phone, MEVENT_MOVE, dx, dy);
00076 }
00077 
00078 static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
00079 {
00080         ipc_callid_t callid;
00081         ipc_call_t call;
00082         int retval;
00083 
00084         async_answer_0(iid, EOK);
00085 
00086         while (1) {
00087                 callid = async_get_call(&call);
00088                 switch (IPC_GET_IMETHOD(call)) {
00089                 case IPC_M_PHONE_HUNGUP:
00090                         if (client_phone != -1) {
00091                                 async_hangup(client_phone);
00092                                 client_phone = -1;
00093                         }
00094 
00095                         async_answer_0(callid, EOK);
00096                         return;
00097                 case IPC_M_CONNECT_TO_ME:
00098                         if (client_phone != -1) {
00099                                 retval = ELIMIT;
00100                                 break;
00101                         }
00102                         client_phone = IPC_GET_ARG5(call);
00103                         retval = 0;
00104                         break;
00105                 default:
00106                         retval = EINVAL;
00107                 }
00108                 async_answer_0(callid, retval);
00109         }
00110 }
00111 
00112 
00113 int main(int argc, char **argv)
00114 {
00115         printf(NAME ": Chardev mouse driver\n");
00116 
00117         /* Initialize port. */
00118         if (mouse_port_init() != 0)
00119                 return -1;
00120 
00121         /* Initialize protocol driver. */
00122         if (mouse_proto_init() != 0)
00123                 return -1;
00124 
00125         /* Register driver */
00126         int rc = devmap_driver_register(NAME, client_connection);
00127         if (rc < 0) {
00128                 printf(NAME ": Unable to register driver (%d)\n", rc);
00129                 return -1;
00130         }
00131 
00132         char dev_path[DEVMAP_NAME_MAXLEN + 1];
00133         snprintf(dev_path, DEVMAP_NAME_MAXLEN, "%s/%s", NAMESPACE, NAME);
00134 
00135         devmap_handle_t devmap_handle;
00136         if (devmap_device_register(dev_path, &devmap_handle) != EOK) {
00137                 printf(NAME ": Unable to register device %s\n", dev_path);
00138                 return -1;
00139         }
00140 
00141         printf(NAME ": Accepting connections\n");
00142         task_retval(0);
00143         async_manager();
00144 
00145         /* Not reached. */
00146         return 0;
00147 }
00148 

Generated on Thu Jun 2 07:45:49 2011 for HelenOS/USB by  doxygen 1.4.7