Subversion Repositories HomeAutomation

Rev

Rev 1740 | 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. #include <boost/lexical_cast.hpp>
  24.  
  25. #include <X11/Xlib.h>
  26.  
  27. #include "vm/Manager.h"
  28.  
  29. namespace atom {
  30. namespace vm {
  31. namespace plugin {
  32.  
  33. logging::Logger Xorg::LOG("vm::plugin::xorg");
  34.    
  35. Xorg::Xorg(boost::asio::io_service& io_service) : Plugin(io_service)
  36. {
  37.     this->name_ = "xorg";
  38.    
  39.     this->ExportFunction("XorgExport_SendKey",       Xorg::Export_SendKey);
  40. }
  41.  
  42. Xorg::~Xorg()
  43. {
  44. }
  45.  
  46. void Xorg::InitializeDone()
  47. {
  48.     Plugin::InitializeDone();
  49. }
  50.  
  51. void Xorg::CallOutput(unsigned int request_id, std::string output)
  52. {
  53.     LOG.Info(output);
  54. }
  55.  
  56. Value Xorg::Export_SendKey(const v8::Arguments& args)
  57. {
  58.     v8::Locker lock;
  59.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  60.     v8::HandleScope handle_scope;
  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 handle_scope.Close(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 handle_scope.Close(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 handle_scope.Close(v8::Boolean::New(true));
  113. }
  114.    
  115. }; // namespace plugin
  116. }; // namespace vm
  117. }; // namespace atom
  118.