The Pedigree Project  0.1
Input.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/native/input/Input.h"
21 #include "modules/subsys/pedigree-c/pedigree-syscalls.h"
22 #include <cstdio>
23 
24 using namespace Input;
25 
26 static void event_callback(size_t p1, size_t p2, uintptr_t *pBuffer, size_t p4)
27 {
28  if (pBuffer[1])
29  {
30  callback_t cb = reinterpret_cast<callback_t>(pBuffer[1]);
32  reinterpret_cast<Input::InputNotification *>(&pBuffer[2]);
33  cb(*pNote);
34  }
35 
36  pedigree_event_return();
37 }
38 
39 void Input::installCallback(CallbackType type, callback_t cb)
40 {
41  pedigree_input_install_callback(
42  reinterpret_cast<void *>(event_callback), static_cast<uint32_t>(type),
43  reinterpret_cast<uintptr_t>(cb));
44 }
45 
46 void Input::removeCallback(callback_t cb)
47 {
48  pedigree_input_remove_callback(reinterpret_cast<void *>(cb));
49 }
50 
51 void Input::inhibitEvents()
52 {
53  pedigree_input_inhibit_events(1);
54 }
55 
56 void Input::uninhibitEvents()
57 {
58  pedigree_input_inhibit_events(0);
59 }
60 
61 void Input::loadKeymapFromFile(const char *path)
62 {
63  // Load the file in to a buffer.
64  FILE *pFile = fopen(path, "r");
65  if (!pFile)
66  {
67  fprintf(
68  stderr, "Input::loadKeymapFromFile: Error opening file `%s'.\n",
69  path);
70  return;
71  }
72 
73  // Get the length of the file
74  fseek(pFile, 0, SEEK_END);
75  size_t nLength = ftell(pFile);
76  fseek(pFile, 0, SEEK_SET);
77 
78  // Read the file
79  uint32_t *pBuffer = new uint32_t[(nLength / sizeof(uint32_t)) + 1];
80  fread(pBuffer, 1, nLength, pFile);
81  fclose(pFile);
82 
83  if (pedigree_load_keymap(pBuffer, nLength))
84  fprintf(stderr, "Input::loadKeymapFromFile: Error loading keymap\n");
85 }
Definition: Input.h:26