1c72fcc34Sopenharmony_ci/*
2c72fcc34Sopenharmony_ci * proc_files.c - shows ALSA system information files
3c72fcc34Sopenharmony_ci * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
4c72fcc34Sopenharmony_ci *
5c72fcc34Sopenharmony_ci * This program is free software: you can redistribute it and/or modify
6c72fcc34Sopenharmony_ci * it under the terms of the GNU General Public License as published by
7c72fcc34Sopenharmony_ci * the Free Software Foundation, either version 2 of the License, or
8c72fcc34Sopenharmony_ci * (at your option) any later version.
9c72fcc34Sopenharmony_ci *
10c72fcc34Sopenharmony_ci * This program is distributed in the hope that it will be useful,
11c72fcc34Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
12c72fcc34Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13c72fcc34Sopenharmony_ci * GNU General Public License for more details.
14c72fcc34Sopenharmony_ci *
15c72fcc34Sopenharmony_ci * You should have received a copy of the GNU General Public License
16c72fcc34Sopenharmony_ci * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17c72fcc34Sopenharmony_ci */
18c72fcc34Sopenharmony_ci
19c72fcc34Sopenharmony_ci#include "aconfig.h"
20c72fcc34Sopenharmony_ci#include <assert.h>
21c72fcc34Sopenharmony_ci#include <menu.h>
22c72fcc34Sopenharmony_ci#include <unistd.h>
23c72fcc34Sopenharmony_ci#include "gettext_curses.h"
24c72fcc34Sopenharmony_ci#include "utils.h"
25c72fcc34Sopenharmony_ci#include "die.h"
26c72fcc34Sopenharmony_ci#include "mem.h"
27c72fcc34Sopenharmony_ci#include "colors.h"
28c72fcc34Sopenharmony_ci#include "widget.h"
29c72fcc34Sopenharmony_ci#include "textbox.h"
30c72fcc34Sopenharmony_ci#include "proc_files.h"
31c72fcc34Sopenharmony_ci#include "menu_widget.h"
32c72fcc34Sopenharmony_ci
33c72fcc34Sopenharmony_cistatic struct widget proc_widget;
34c72fcc34Sopenharmony_cistatic ITEM *items[7];
35c72fcc34Sopenharmony_cistatic unsigned int items_count;
36c72fcc34Sopenharmony_cistatic MENU *menu;
37c72fcc34Sopenharmony_ci
38c72fcc34Sopenharmony_cistatic void on_handle_key(int key)
39c72fcc34Sopenharmony_ci{
40c72fcc34Sopenharmony_ci	ITEM *item;
41c72fcc34Sopenharmony_ci
42c72fcc34Sopenharmony_ci	switch (menu_widget_handle_key(menu, key)) {
43c72fcc34Sopenharmony_ci		case KEY_ENTER:
44c72fcc34Sopenharmony_ci			item = current_item(menu);
45c72fcc34Sopenharmony_ci			if (item)
46c72fcc34Sopenharmony_ci				show_textfile(item_name(item));
47c72fcc34Sopenharmony_ci			break;
48c72fcc34Sopenharmony_ci		case KEY_CANCEL:
49c72fcc34Sopenharmony_ci			proc_widget.close();
50c72fcc34Sopenharmony_ci			break;
51c72fcc34Sopenharmony_ci	}
52c72fcc34Sopenharmony_ci}
53c72fcc34Sopenharmony_ci
54c72fcc34Sopenharmony_cistatic void create(void)
55c72fcc34Sopenharmony_ci{
56c72fcc34Sopenharmony_ci	menu_widget_create(&proc_widget, menu, _("Select File"));
57c72fcc34Sopenharmony_ci}
58c72fcc34Sopenharmony_ci
59c72fcc34Sopenharmony_cistatic void on_close(void)
60c72fcc34Sopenharmony_ci{
61c72fcc34Sopenharmony_ci	unsigned int i;
62c72fcc34Sopenharmony_ci
63c72fcc34Sopenharmony_ci	unpost_menu(menu);
64c72fcc34Sopenharmony_ci	free_menu(menu);
65c72fcc34Sopenharmony_ci	for (i = 0; i < items_count; ++i)
66c72fcc34Sopenharmony_ci		free_item(items[i]);
67c72fcc34Sopenharmony_ci	widget_free(&proc_widget);
68c72fcc34Sopenharmony_ci}
69c72fcc34Sopenharmony_ci
70c72fcc34Sopenharmony_cistatic void add_item(const char *file_name)
71c72fcc34Sopenharmony_ci{
72c72fcc34Sopenharmony_ci	if (access(file_name, F_OK) == 0) {
73c72fcc34Sopenharmony_ci		items[items_count] = new_item(file_name, NULL);
74c72fcc34Sopenharmony_ci		if (!items[items_count])
75c72fcc34Sopenharmony_ci			fatal_error("cannot create menu item");
76c72fcc34Sopenharmony_ci		++items_count;
77c72fcc34Sopenharmony_ci		assert(items_count < ARRAY_SIZE(items));
78c72fcc34Sopenharmony_ci	}
79c72fcc34Sopenharmony_ci}
80c72fcc34Sopenharmony_ci
81c72fcc34Sopenharmony_cistatic struct widget proc_widget = {
82c72fcc34Sopenharmony_ci	.handle_key = on_handle_key,
83c72fcc34Sopenharmony_ci	.window_size_changed = create,
84c72fcc34Sopenharmony_ci	.close = on_close,
85c72fcc34Sopenharmony_ci};
86c72fcc34Sopenharmony_ci
87c72fcc34Sopenharmony_civoid create_proc_files_list(void)
88c72fcc34Sopenharmony_ci{
89c72fcc34Sopenharmony_ci	items_count = 0;
90c72fcc34Sopenharmony_ci	add_item("/proc/asound/version");
91c72fcc34Sopenharmony_ci	add_item("/proc/asound/cards");
92c72fcc34Sopenharmony_ci	add_item("/proc/asound/devices");
93c72fcc34Sopenharmony_ci	add_item("/proc/asound/oss/devices");
94c72fcc34Sopenharmony_ci	add_item("/proc/asound/timers");
95c72fcc34Sopenharmony_ci	add_item("/proc/asound/pcm");
96c72fcc34Sopenharmony_ci	items[items_count] = NULL;
97c72fcc34Sopenharmony_ci
98c72fcc34Sopenharmony_ci	menu = new_menu(items);
99c72fcc34Sopenharmony_ci	if (!menu)
100c72fcc34Sopenharmony_ci		fatal_error("cannot create menu");
101c72fcc34Sopenharmony_ci	set_menu_fore(menu, attrs.menu_selected);
102c72fcc34Sopenharmony_ci	set_menu_back(menu, attrs.menu);
103c72fcc34Sopenharmony_ci	set_menu_mark(menu, NULL);
104c72fcc34Sopenharmony_ci	menu_opts_off(menu, O_SHOWDESC);
105c72fcc34Sopenharmony_ci
106c72fcc34Sopenharmony_ci	create();
107c72fcc34Sopenharmony_ci}
108