Subversion Repositories HomeAutomation

Rev

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. #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.    
  61.     LOG.Debug(std::string(__FUNCTION__) + " called!");
  62.    
  63.     if (args.Length() < 1)
  64.     {
  65.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  66.         return v8::Boolean::New(false);
  67.     }
  68.    
  69.     Display* display = XOpenDisplay(":0");
  70.    
  71.     if (display == NULL)
  72.     {
  73.         LOG.Warning("Could not locate display :0");
  74.         return v8::Boolean::New(false);
  75.     }
  76.    
  77.     // Get the root window for the current display.
  78.     Window winRoot = XDefaultRootWindow(display);
  79.    
  80.     // Find the window which has the current keyboard focus.
  81.     Window winFocus;
  82.     int    revert;
  83.     XGetInputFocus(display, &winFocus, &revert);
  84.    
  85.     XKeyEvent event;
  86.    
  87.     event.display     = display;
  88.     event.window      = winFocus;
  89.     event.root        = winRoot;
  90.     event.subwindow   = None;
  91.     event.time        = CurrentTime;
  92.     event.x           = 1;
  93.     event.y           = 1;
  94.     event.x_root      = 1;
  95.     event.y_root      = 1;
  96.     event.same_screen = True;
  97.     event.keycode     = XKeysymToKeycode(display, args[0]->Int32Value());
  98.     event.state       = 0;
  99.     event.type        = KeyPress;
  100.  
  101.     LOG.Debug("Sent code " + boost::lexical_cast<std::string>(args[0]->Int32Value()));
  102.    
  103.     XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
  104.    
  105.     event.type = KeyRelease;
  106.    
  107.     XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
  108.    
  109.     XCloseDisplay(display);
  110.    
  111.     return v8::Boolean::New(true);
  112. }
  113.    
  114. }; // namespace plugin
  115. }; // namespace vm
  116. }; // namespace atom
  117.