summaryrefslogtreecommitdiff
path: root/activity.c
blob: 1575a416c6f60eab021955ddc8a8c1a463a4d789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * activity.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <X11/Xlib.h>
#include <X11/extensions/scrnsaver.h>
#include <utmp.h>
#include <string.h>
#include <sstream>
#include <sys/stat.h>
#include <time.h>
#include <vdr/config.h>
#include "activity.h"

#ifdef USE_XSS
int cUserActivity::DisplayIdleTime(char *display) {
  Display *d;
  static XScreenSaverInfo *mitInfo = 0;
  static int event, error, result=-1;

  if (!(d = XOpenDisplay(display))) {
    syslog(LOG_ERR, "useractivity: Unable to open DISPLAY %s\n", display);
    return result;
  }

  if(XScreenSaverQueryExtension(d,&event,&error)) {
    if (!mitInfo) mitInfo = XScreenSaverAllocInfo();
    XScreenSaverQueryInfo(d, DefaultRootWindow (d), mitInfo);
    result = (mitInfo->idle/1000)/60;
  }
  else {
    syslog(LOG_ERR, "useractivity: MIT-SCREEN-SAVER missing\n");
    return result;
  }

  XCloseDisplay(d);
  return result;
}
#endif

int cUserActivity::DeviceIdleTime(char *device) {
  using namespace std;
  stringstream stream;
  struct stat stats;
  time_t idle,now;

  time(&now);

  stream << DEV_DIR << device;
  if(stat(stream.str().c_str(), &stats) == 0)
    idle = (now - stats.st_atime)/60;
  else
    idle = -1;

  return idle;
}


bool cUserActivity::ActiveUsers(void) {
  struct utmp *uptr;
  int idle;
  bool result=false;

  setutent();
  while((uptr = getutent())!=NULL) {
    if(uptr->ut_type == USER_PROCESS) {
      idle = DeviceIdleTime(uptr->ut_line);
#ifdef USE_XSS
      if(idle < 0 && strchr(uptr->ut_line, ':') != NULL)
        idle = DisplayIdleTime(uptr->ut_line);
#endif
      if(idle >= 0 && idle < Setup.MinUserInactivity) {
        result = true;
        break;
      }
    }
  }
  endutent();
  return result;
}

void cUserActivity::SetMinUserInactivity(int minutes) {
  Setup.MinUserInactivity = minutes;
}    

int cUserActivity::GetMinUserInactivity(void) {
  return Setup.MinUserInactivity;
}

char *cUserActivity::GetUsers(void) {
  struct utmp *uptr;
  int idle;
  using namespace std;
  stringstream stream;

  stream << "USER           DEVICE         IDLE" << endl;
  setutent();
  while((uptr = getutent())!=NULL) {
    if(uptr->ut_type == USER_PROCESS) {
      idle = DeviceIdleTime(uptr->ut_line);
#ifdef USE_XSS
      if(idle < 0 && strchr(uptr->ut_line, ':') != NULL)
        idle = DisplayIdleTime(uptr->ut_line);
#endif
      stream.width(15);
      stream << left << uptr->ut_user;
      stream.width(15);
      stream << left << uptr->ut_line;
      stream.width(0);
      stream << idle << endl;
    }
  }
  endutent();
  string resultString;
  resultString = stream.str();
  char *result = new char[resultString.size()+1];
  strcpy(result, resultString.c_str());
  return result;
}