/
Vol_Controller.c
167 lines (126 loc) · 4.59 KB
/
Vol_Controller.c
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
/*
* Author: Sandeep Singh
* Date : Oct 4, 2012
*
* Copyright 2012 Sandeep Singh (sandeepsinghmails@gmail.com)
*
* This file is part of Alsa Volume Controller.
* Alsa Volume Controller is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alsa Volume Controller is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Alsa Volume Controller. If not, see <http://www.gnu.org/licenses/>.
*
* To Compile:
* $ gcc -o alsa_controller Vol_Controller.c `pkg-config --libs --cflags gtk+-2.0`
*/
#include <stdio.h>
#include <stdlib.h> /* For system() Function */
#include <gtk/gtk.h> /* GTK Library */
#define MAXSIZE 500 /* Buffer Size */
/* Buffer to Store the String to be print in the Window */
char Buffer[MAXSIZE];
void GetSystemVolume(void);
void increase(GtkWidget *widget, gpointer window_label)
{
/* Now, actually increase the Cound Level by 1 */
system("amixer --quiet set Master 1+");
/* Get System Volume */
GetSystemVolume();
/* Print Buffer Contents in GTK Window */
gtk_label_set_text(GTK_LABEL(window_label), Buffer);
}
void decrease(GtkWidget *widget, gpointer window_label)
{
/* Now, actually decrease the Cound Level by 1 */
system("amixer --quiet set Master 1-");
/* Get System Volume */
GetSystemVolume();
/* Print Buffer Contents in GTK Window */
gtk_label_set_text(GTK_LABEL(window_label), Buffer);
}
void maxvolume(GtkWidget *widget, gpointer window_label)
{
/* Now, actually increase the Sound Level by Maximum */
system("amixer --quiet set Master 31+");
/* Get System Volume */
GetSystemVolume();
/* Print Buffer Contents in GTK Window */
gtk_label_set_text(GTK_LABEL(window_label), Buffer);
}
void novolume(GtkWidget *widget, gpointer window_label)
{
/* Set the Sound Level to Zero */
system("amixer --quiet set Master 31-");
/* Get System Volume */
GetSystemVolume();
/* Print Buffer Contents in GTK Window */
gtk_label_set_text(GTK_LABEL(window_label), Buffer);
}
int main(int argc, char** argv)
{
GtkWidget *window_label;
GtkWidget *window;
GtkWidget *window_frame;
GtkWidget *increase_vol;
GtkWidget *decrease_vol;
GtkWidget *max_vol;
GtkWidget *zero_vol;
/* Initialize the GTK+ Library */
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
/* Set Title for GTK Window */
gtk_window_set_title(GTK_WINDOW(window), "ALSA Volume Controller");
/* Set Default Size of the GTK Window */
gtk_window_set_default_size(GTK_WINDOW(window), 560, 150);
window_frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), window_frame);
increase_vol = gtk_button_new_with_label("+");
gtk_widget_set_size_request(increase_vol, 80, 35);
gtk_fixed_put(GTK_FIXED(window_frame), increase_vol, 50, 20);
decrease_vol = gtk_button_new_with_label("-");
gtk_widget_set_size_request(decrease_vol, 80, 35);
gtk_fixed_put(GTK_FIXED(window_frame), decrease_vol, 50, 80);
max_vol = gtk_button_new_with_label("MAX");
gtk_widget_set_size_request(max_vol, 80, 35);
gtk_fixed_put(GTK_FIXED(window_frame), max_vol, 165, 20);
zero_vol = gtk_button_new_with_label("MUTE");
gtk_widget_set_size_request(zero_vol, 80, 35);
gtk_fixed_put(GTK_FIXED(window_frame), zero_vol, 165, 80);
window_label = gtk_label_new("copyright@SandeepSingh");
gtk_fixed_put(GTK_FIXED(window_frame), window_label, 280, 58);
gtk_widget_show_all(window);
g_signal_connect(window, "destroy",
G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect(increase_vol, "clicked",
G_CALLBACK(increase), window_label);
g_signal_connect(decrease_vol, "clicked",
G_CALLBACK(decrease), window_label);
g_signal_connect(max_vol, "clicked",
G_CALLBACK(maxvolume), window_label);
g_signal_connect(zero_vol, "clicked",
G_CALLBACK(novolume), window_label);
gtk_main();
return 0;
}
void GetSystemVolume(void)
{
FILE *fptr;
/* Open a Pipe for Reading */
fptr = popen("amixer get Master|grep Mono:", "r");
if (fptr == NULL)
{
printf("popen() Failed !\n");
}
/* Store the Output into the Buffer */
fgets(Buffer, sizeof(Buffer)-1, fptr);
/* Close the Pipe */
pclose(fptr);
}