Contiki 3.x
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  */
34 
35 #include <mouse.h>
36 #include <stdlib.h>
37 #include <modload.h>
38 
39 #include "cfs/cfs.h"
40 #include "ctk.h"
41 #include "ctk-mouse.h"
42 
43 #if CTK_CONF_MOUSE_SUPPORT
44 
45 static struct mouse_pos pos;
46 static uint8_t okay;
47 
48 /*-----------------------------------------------------------------------------------*/
49 void
50 ctk_mouse_init(void)
51 {
52  struct mod_ctrl module_control = {cfs_read};
53 
54  module_control.callerdata = cfs_open("contiki.mou", CFS_READ);
55  okay = module_control.callerdata >= 0;
56  if(okay) {
57  okay = mod_load(&module_control) == MLOAD_OK;
58  if(okay) {
59  okay = mouse_install(&mouse_def_callbacks, module_control.module) == MOUSE_ERR_OK;
60  if(okay) {
61  atexit((void (*)(void))mouse_uninstall);
62  } else {
63  mod_free(module_control.module);
64  }
65  }
66  cfs_close(module_control.callerdata);
67  }
68 }
69 /*-----------------------------------------------------------------------------------*/
70 unsigned short
71 ctk_mouse_x(void)
72 {
73  if(okay) {
74  mouse_pos(&pos);
75  }
76  return pos.x;
77 }
78 /*-----------------------------------------------------------------------------------*/
79 unsigned short
80 ctk_mouse_y(void)
81 {
82  if(okay) {
83  mouse_pos(&pos);
84  }
85  return pos.y;
86 }
87 /*-----------------------------------------------------------------------------------*/
88 unsigned char
89 ctk_mouse_button(void)
90 {
91  if(okay) {
92  return mouse_buttons();
93  }
94  return 0;
95 }
96 /*-----------------------------------------------------------------------------------*/
97 unsigned char
98 ctk_mouse_xtoc(unsigned short x)
99 {
100  return MOUSE_CONF_XTOC(x);
101 }
102 /*-----------------------------------------------------------------------------------*/
103 unsigned char
104 ctk_mouse_ytoc(unsigned short y)
105 {
106  return MOUSE_CONF_YTOC(y);
107 }
108 /*-----------------------------------------------------------------------------------*/
109 void
110 ctk_mouse_hide(void)
111 {
112  if(okay) {
113  mouse_hide();
114  }
115 }
116 /*-----------------------------------------------------------------------------------*/
117 void
118 ctk_mouse_show(void)
119 {
120  if(okay) {
121  mouse_show();
122  }
123 }
124 /*-----------------------------------------------------------------------------------*/
125 #endif /* CTK_CONF_MOUSE_SUPPORT */
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:996
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:90
CTK header file.
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1032
CFS header file.