Subversion Repositories HomeAutomation

Rev

Rev 1647 | Rev 1660 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "Xorg.h"
  22.  
  23. #ifdef USE_PLUGIN_XORG
  24.  
  25. #include <boost/lexical_cast.hpp>
  26.  
  27. #include <X11/Xlib.h>
  28.  
  29. #include "vm/Manager.h"
  30.  
  31. namespace atom {
  32. namespace vm {
  33. namespace plugin {
  34.  
  35. logging::Logger Xorg::LOG("vm::plugin::xorg");
  36.    
  37. Xorg::Xorg(boost::asio::io_service& io_service) : Plugin(io_service)
  38. {
  39.     this->name_ = "xorg";
  40.    
  41.     this->ExportFunction("XorgExport_SendKey",       Xorg::Export_SendKey);
  42. }
  43.  
  44. Xorg::~Xorg()
  45. {
  46. }
  47.  
  48. void Xorg::InitializeDone()
  49. {
  50.     Plugin::InitializeDone();
  51. }
  52.  
  53. void Xorg::CallOutput(unsigned int request_id, std::string output)
  54. {
  55.     LOG.Info(output);
  56. }
  57.  
  58. Value Xorg::Export_SendKey(const v8::Arguments& args)
  59. {
  60.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  61.    
  62.     LOG.Debug(std::string(__FUNCTION__) + " called!");
  63.    
  64.     if (args.Length() < 1)
  65.     {
  66.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  67.         return v8::Boolean::New(false);
  68.     }
  69.    
  70.     Display* display = XOpenDisplay(":0");
  71.    
  72.     if (display == NULL)
  73.     {
  74.         LOG.Warning("Could not locate display :0");
  75.         return v8::Boolean::New(false);
  76.     }
  77.    
  78.     // Get the root window for the current display.
  79.     Window winRoot = XDefaultRootWindow(display);
  80.    
  81.     // Find the window which has the current keyboard focus.
  82.     Window winFocus;
  83.     int    revert;
  84.     XGetInputFocus(display, &winFocus, &revert);
  85.    
  86.     XKeyEvent event;
  87.    
  88.     event.display     = display;
  89.     event.window      = winFocus;
  90.     event.root        = winRoot;
  91.     event.subwindow   = None;
  92.     event.time        = CurrentTime;
  93.     event.x           = 1;
  94.     event.y           = 1;
  95.     event.x_root      = 1;
  96.     event.y_root      = 1;
  97.     event.same_screen = True;
  98.     event.keycode     = XKeysymToKeycode(display, args[0]->Int32Value());
  99.     event.state       = 0;
  100.     event.type        = KeyPress;
  101.  
  102.     LOG.Debug("Sent code " + boost::lexical_cast<std::string>(args[0]->Int32Value()));
  103.    
  104.     XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
  105.    
  106.     event.type = KeyRelease;
  107.    
  108.     XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
  109.    
  110.     XCloseDisplay(display);
  111.    
  112.     return v8::Boolean::New(true);
  113. }
  114.    
  115. }; // namespace plugin
  116. }; // namespace vm
  117. }; // namespace atom
  118.  
  119. #endif // USE_PLUGIN_XORG
  120.