summaryrefslogtreecommitdiffstats
path: root/src/wakeup/waketab.cpp
blob: 77d75671b61635ea63b76b8f1e9726ebbba84237 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <ncurses.h>
#include "wakeup.h"
#include <vector>
#include "../lib.h"
#include "../measurement/sysfs.h"
#include "../display.h"
#include "../report/report.h"
#include "../report/report-maker.h"
#include "../report/report-data-html.h"
#include "wakeup_ethernet.h"
#include "wakeup_usb.h"

using namespace std;

static bool should_clear = false;

class wakeup_window *newtab_window;

class wakeup_window: public tab_window {
public:
	virtual void repaint(void);
	virtual void cursor_enter(void);
	virtual void expose(void);
	virtual void window_refresh(void);
};

static void init_wakeup(void)
{
	add_ethernet_wakeup();
	add_usb_wakeup();
}

void initialize_wakeup(void)
{
	class wakeup_window *win;

	win = new wakeup_window();

	create_tab("WakeUp", _("WakeUp"), win, _(" <ESC> Exit | <Enter> Toggle wakeup | <r> Window refresh"));

	init_wakeup();

	win->cursor_max = wakeup_all.size() - 1;

	if (newtab_window)
		delete newtab_window;

	newtab_window = win;
}

static void __wakeup_update_display(int cursor_pos)
{
	WINDOW *win;
	unsigned int i;

	win = get_ncurses_win("WakeUp");

	if (!win)
		return;

	if (should_clear) {
		should_clear = false;
		wclear(win);
	}

	wmove(win, 1,0);

	for (i = 0; i < wakeup_all.size(); i++) {
		char res[128];
		char desc[4096];
		pt_strcpy(res, wakeup_all[i]->wakeup_string());
		pt_strcpy(desc, wakeup_all[i]->description());
		while (strlen(res) < 12)
			strcat(res, " ");

		while (strlen(desc) < 103)
			strcat(desc, " ");

		if ((int)i != cursor_pos) {
			wattrset(win, A_NORMAL);
			wprintw(win, "   ");
		} else {
			wattrset(win, A_REVERSE);
			wprintw(win, ">> ");
		}
	wprintw(win, "%s  %s\n", _(res), _(desc));
	}
}

void wakeup_update_display(void)
{
	class tab_window *wt;

	wt = tab_windows["WakeUp"];
	if (!wt)
		return;
	wt->repaint();
}

void wakeup_window::repaint(void)
{
	__wakeup_update_display(cursor_pos);
}

void wakeup_window::cursor_enter(void)
{
	class wakeup *wake;
	const char *wakeup_toggle_script;
	wake = wakeup_all[cursor_pos];
	if (!wake)
		return;
	wakeup_toggle_script = wake->wakeup_toggle_script();
	wake->wakeup_toggle();
	ui_notify_user(">> %s\n", wakeup_toggle_script);
}

void report_show_wakeup(void)
{
	unsigned int i;
	int idx, rows = 0, cols;

	/* div attr css_class and css_id */
	tag_attr div_attr;
	init_div(&div_attr, "clear_block", "wakeup");

	/* Set Title attributes */
	tag_attr title_attr;
	init_title_attr(&title_attr);

	/* Set Table attributes, rows, and cols */
	table_attributes wakeup_table_css;
	cols=2;
	idx = cols;

	for (i = 0; i < wakeup_all.size(); i++) {
		int tgb;
		tgb = wakeup_all[i]->wakeup_value();
		if (tgb == WAKEUP_DISABLE)
			rows+=1;
	}

	/* add section */

	report.add_div(&div_attr);
	if (rows > 0){
		rows= rows + 1;
		init_wakeup_table_attr(&wakeup_table_css, rows, cols);

		/* Set array of data in row Major order */
		string *wakeup_data = new string[cols * rows];

		wakeup_data[0]=__("Description");
		wakeup_data[1]=__("Script");

		for (i = 0; i < wakeup_all.size(); i++) {
			int gb;
			gb = wakeup_all[i]->wakeup_value();
			if (gb != WAKEUP_DISABLE)
				continue;
			wakeup_data[idx]=string(wakeup_all[i]->description());
			idx+=1;
			wakeup_data[idx]=string(wakeup_all[i]->wakeup_toggle_script());
			idx+=1;
		}

		/* Report Output */
		report.add_title(&title_attr,__("Wake status of the devices"));
		report.add_table(wakeup_data, &wakeup_table_css);
		delete [] wakeup_data;
	}
}

void wakeup_window::expose(void)
{
	cursor_pos = 0;
	repaint();
}

void wakeup_window::window_refresh(void)
{
	clear_wakeup();
	should_clear = true;
	init_wakeup();
}

void clear_wakeup()
{
	for (size_t i = 0; i < wakeup_all.size(); i++) {
		delete wakeup_all[i];
	}
	wakeup_all.clear();
}