~ubuntu-branches/debian/stretch/workrave/stretch

« back to all changes in this revision

Viewing changes to backend/src/unix/UnixInputMonitorFactory.cc

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2012-05-28 11:29:40 UTC
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20120528112940-e9s9s40sts0v9ivz
Tags: upstream-1.9.909+abc941eb70
ImportĀ upstreamĀ versionĀ 1.9.909+abc941eb70

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// UnixInputMonitorFactory.cc -- Factory to create input monitors
2
2
//
3
 
// Copyright (C) 2007 Rob Caelers <robc@krandor.nl>
 
3
// Copyright (C) 2007, 2012 Rob Caelers <robc@krandor.nl>
4
4
// All rights reserved.
5
5
//
6
6
// This program is free software: you can redistribute it and/or modify
22
22
#endif
23
23
 
24
24
#include <string>
 
25
#include <vector>
 
26
#include <algorithm>
25
27
 
26
28
#include "debug.hh"
27
29
 
 
30
#include "Core.hh"
28
31
#include "CoreFactory.hh"
29
32
#include "IConfigurator.hh"
 
33
#include "StringUtil.hh"
30
34
 
31
35
#include "UnixInputMonitorFactory.hh"
 
36
#include "RecordInputMonitor.hh"
32
37
#include "X11InputMonitor.hh"
 
38
#include "XScreenSaverMonitor.hh"
33
39
 
34
40
UnixInputMonitorFactory::UnixInputMonitorFactory()
 
41
  : error_reported(false)
35
42
{
36
43
  monitor = NULL;
37
44
}
38
45
 
 
46
 
39
47
void
40
48
UnixInputMonitorFactory::init(const std::string &display)
41
49
{
46
54
IInputMonitor *
47
55
UnixInputMonitorFactory::get_monitor(IInputMonitorFactory::MonitorCapability capability)
48
56
{
 
57
  TRACE_ENTER("UnixInputMonitorFactory::get_monitor");
49
58
  (void) capability;
50
 
  
 
59
 
51
60
  if (monitor == NULL)
52
61
    {
53
 
      monitor = new X11InputMonitor(display);
54
 
 
55
 
      bool init_ok = monitor->init();
56
 
      if (!init_ok)
57
 
        {
 
62
      bool initialized = false;
 
63
      string configure_monitor_method;
 
64
      
 
65
      vector<string> available_monitors;
 
66
      StringUtil::split(HAVE_MONITORS, ',', available_monitors);
 
67
 
 
68
      TRACE_MSG("available_monitors " << HAVE_MONITORS << " " << available_monitors.size());
 
69
      
 
70
      CoreFactory::get_configurator()->get_value_with_default("advanced/monitor",
 
71
                                                              configure_monitor_method,
 
72
                                                              "default");
 
73
 
 
74
      vector<string>::const_iterator start = available_monitors.end();
 
75
 
 
76
      if (configure_monitor_method != "default")
 
77
        {
 
78
          TRACE_MSG("use configured: " << configure_monitor_method);
 
79
          start = find(available_monitors.begin(), available_monitors.end(), configure_monitor_method);
 
80
        }
 
81
 
 
82
      if (start == available_monitors.end())
 
83
        {
 
84
          start = available_monitors.begin();
 
85
          TRACE_MSG("Start first available");
 
86
        }
 
87
 
 
88
      vector<string>::const_iterator loop = start;
 
89
      while(1)
 
90
        {
 
91
          string actual_monitor_method = *loop;
 
92
          TRACE_MSG("Test " <<  actual_monitor_method);
 
93
          
 
94
          if (actual_monitor_method == "record")
 
95
            {
 
96
              monitor = new RecordInputMonitor(display);
 
97
            }
 
98
          else if (actual_monitor_method == "screensaver")
 
99
            {
 
100
              monitor = new XScreenSaverMonitor();
 
101
            }
 
102
          else if (actual_monitor_method == "x11events")
 
103
            {
 
104
              monitor = new X11InputMonitor(display);
 
105
            }
 
106
 
 
107
          initialized = monitor->init();
 
108
 
 
109
          if (initialized)
 
110
            {
 
111
              TRACE_MSG("Success");
 
112
              break;
 
113
            }
 
114
          
58
115
          delete monitor;
59
116
          monitor = NULL;
 
117
 
 
118
          loop++;
 
119
          if (loop == available_monitors.end())
 
120
            {
 
121
              loop = available_monitors.begin();
 
122
            }
 
123
 
 
124
          if (loop == start)
 
125
            {
 
126
              break;
 
127
            }
 
128
        }
 
129
 
 
130
      if (!initialized)
 
131
        {
 
132
          TRACE_MSG("Non found");
 
133
 
 
134
          if (!error_reported)
 
135
            {
 
136
              error_reported = true;
 
137
              g_idle_add(static_report_failure, NULL);
 
138
            }
 
139
          
 
140
          CoreFactory::get_configurator()->set_value("advanced/monitor", "default");
 
141
          CoreFactory::get_configurator()->save();
 
142
 
 
143
          actual_monitor_method = "";
 
144
        }
 
145
      else
 
146
        {
 
147
          if (configure_monitor_method != "default")
 
148
            {
 
149
              CoreFactory::get_configurator()->set_value("advanced/monitor", actual_monitor_method);
 
150
              CoreFactory::get_configurator()->save();
 
151
            }
 
152
 
 
153
          TRACE_MSG("using " << actual_monitor_method);
60
154
        }
61
155
    }
62
156
 
 
157
  TRACE_EXIT();
63
158
  return monitor;
64
159
}
65
160
 
 
161
gboolean
 
162
UnixInputMonitorFactory::static_report_failure(void *data)
 
163
{
 
164
  (void) data;
 
165
 
 
166
  Core *core = Core::get_instance();
 
167
  core->post_event(CORE_EVENT_MONITOR_FAILURE);
 
168
 
 
169
  return FALSE;
 
170
}
 
171