Contiki 2.5
ctk-mouse.c
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Oliver Schmidt <ol.sc@web.de>
32  *
33  * $Id: ctk-mouse.c,v 1.3 2009/10/18 09:33:08 oliverschmidt Exp $
34  */
35 
36 #include <mouse.h>
37 #include <stdlib.h>
38 #include <modload.h>
39 
40 #include "cfs/cfs.h"
41 #include "ctk.h"
42 #include "ctk-mouse.h"
43 
44 #if CTK_CONF_MOUSE_SUPPORT
45 
46 static struct mouse_pos pos;
47 static u8_t okay;
48 
49 /*-----------------------------------------------------------------------------------*/
50 void
51 ctk_mouse_init(void)
52 {
53  struct mod_ctrl module_control = {cfs_read};
54 
55  module_control.callerdata = cfs_open(mouse_stddrv, CFS_READ);
56  okay = module_control.callerdata >= 0;
57  if(okay) {
58  okay = mod_load(&module_control) == MLOAD_OK;
59  if(okay) {
60  okay = mouse_install(&mouse_def_callbacks, module_control.module) == MOUSE_ERR_OK;
61  if(okay) {
62  atexit((void (*)(void))mouse_uninstall);
63  } else {
64  mod_free(module_control.module);
65  }
66  }
67  cfs_close(module_control.callerdata);
68  }
69 }
70 /*-----------------------------------------------------------------------------------*/
71 unsigned short
72 ctk_mouse_x(void)
73 {
74  if(okay) {
75  mouse_pos(&pos);
76  }
77  return pos.x;
78 }
79 /*-----------------------------------------------------------------------------------*/
80 unsigned short
81 ctk_mouse_y(void)
82 {
83  if(okay) {
84  mouse_pos(&pos);
85  }
86  return pos.y;
87 }
88 /*-----------------------------------------------------------------------------------*/
89 unsigned char
90 ctk_mouse_button(void)
91 {
92  if(okay) {
93  return mouse_buttons();
94  }
95  return 0;
96 }
97 /*-----------------------------------------------------------------------------------*/
98 unsigned char
99 ctk_mouse_xtoc(unsigned short x)
100 {
101  return MOUSE_CONF_XTOC(x);
102 }
103 /*-----------------------------------------------------------------------------------*/
104 unsigned char
105 ctk_mouse_ytoc(unsigned short y)
106 {
107  return MOUSE_CONF_YTOC(y);
108 }
109 /*-----------------------------------------------------------------------------------*/
110 void
111 ctk_mouse_hide(void)
112 {
113  if(okay) {
114  mouse_hide();
115  }
116 }
117 /*-----------------------------------------------------------------------------------*/
118 void
119 ctk_mouse_show(void)
120 {
121  if(okay) {
122  mouse_show();
123  }
124 }
125 /*-----------------------------------------------------------------------------------*/
126 #endif /* CTK_CONF_MOUSE_SUPPORT */