refactoring

This commit is contained in:
PoliEcho 2024-12-08 15:32:54 +01:00
parent 34cd9075a8
commit 4de8c66a03
10 changed files with 177 additions and 60 deletions

View File

@ -1,2 +1,37 @@
all:
g++ main.cpp -lasound -g -o TabletPcControl-server
# Compiler and flags
CPPC = g++
CPPC_FLAGS = -s -O3 -lasound -Wall -Wextra
# Debug flags:
# CPPC_FLAGS = -ggdb -lncurses -lcurl -lmenu -lpanel -Wall -Wextra
SRC_PATH := src
OBJ_PATH := build/obj
BIN_PATH := build/bin
SRC_FILES := $(shell find $(SRC_PATH) -name '*.cpp')
# Generate corresponding object file paths by replacing src/ with build/obj/
OBJ_FILES := $(patsubst $(SRC_PATH)/%.cpp,$(OBJ_PATH)/%.o,$(SRC_FILES))
all: make-build-dir $(BIN_PATH)/TabletPcControl-server
make-build-dir:
mkdir -p $(OBJ_PATH)
mkdir -p $(BIN_PATH)
$(BIN_PATH)/TabletPcControl-server: $(OBJ_FILES)
$(CPPC) $(CPPC_FLAGS) $^ -o $@
$(OBJ_PATH)/%.o: $(SRC_PATH)/%.cpp
$(CPPC) $(CPPC_FLAGS) -c $< -o $@
clean:
rm -fr build
.PHONY: all clean

4
build.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
read -p "enter secret password: " SECRET
echo "#define SECRET \"$SECRET\"" > src/secret.h
make -j$(nproc)

View File

115
src/alsa.cpp Normal file
View File

@ -0,0 +1,115 @@
#include "helper_funcs.h"
#include <alsa/asoundlib.h>
#include <arpa/inet.h>
#include <cerrno>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <unistd.h>
namespace alsa {
int get_volume(const char *selem_name) {
long min, max, volume;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
// Open mixer
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle,
"default"); // Use "default" for the default sound card
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
// Allocate and set mixer element ID
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_name(sid, selem_name);
// Find the mixer element
snd_mixer_elem_t *elem = snd_mixer_find_selem(handle, sid);
int volume_percent;
if (elem) {
// Get the playback volume range
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
// Get the current playback volume for front left channel
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT,
&volume);
// Calculate the volume percentage
volume_percent = (volume - min) * 100 / (max - min);
} else {
std::cerr << "Mixer element not found\n";
snd_mixer_close(handle);
safe_exit(ENODEV);
}
// Clean up
snd_mixer_close(handle);
return volume_percent;
}
void set_volume(const char *selem_name, long volume_percent) {
long min, max;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
// Open mixer
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, "default"); // Use "default" instead of hw:0
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
// Allocate and set mixer element ID
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_name(sid, selem_name);
// Find the mixer element
snd_mixer_elem_t *elem = snd_mixer_find_selem(handle, sid);
if (elem) {
// Get the playback volume range
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
// Calculate the new volume value
long volume = min + (volume_percent * (max - min) / 100);
// Set the playback volume
snd_mixer_selem_set_playback_volume_all(elem, volume);
printf("Volume set to %ld%%\n", volume_percent);
} else {
fprintf(stderr, "Mixer element not found\n");
}
// Clean up
snd_mixer_close(handle);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <selem_name> <volume_percent>\n", argv[0]);
return 1;
}
const char *selem_name = argv[1];
long volume_percent = atol(argv[2]);
set_volume(selem_name, volume_percent);
return 0;
}
} // namespace alsa

4
src/alsa.h Normal file
View File

@ -0,0 +1,4 @@
namespace alsa {
int get_volume(const char *selem_name);
void set_volume(const char *selem_name, long volume_percent);
} // namespace alsa

10
src/helper_funcs.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "main.h"
#include <iostream>
#include <unistd.h>
void safe_exit(int code) {
std::clog << "Exiting\n";
close(serverSocket);
exit(code);
}

1
src/helper_funcs.h Normal file
View File

@ -0,0 +1 @@
void safe_exit(int code);

View File

@ -1,25 +1,21 @@
#include "alsa.h"
#include "helper_funcs.h"
#include "secret.h"
#include <alsa/asoundlib.h>
#include <arpa/inet.h>
#include <array>
#include <cerrno>
#include <codecvt>
#include <csignal>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <memory>
#include <netdb.h>
#include <netinet/in.h>
#include <sched.h>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
@ -31,56 +27,6 @@
int serverSocket;
void safe_exit(int code) {
std::clog << "Exiting\n";
close(serverSocket);
exit(code);
}
int get_volume(const char *selem_name) {
long min, max, volume;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
// Open mixer
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle,
"default"); // Use "default" for the default sound card
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
// Allocate and set mixer element ID
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_name(sid, selem_name);
// Find the mixer element
snd_mixer_elem_t *elem = snd_mixer_find_selem(handle, sid);
int volume_percent;
if (elem) {
// Get the playback volume range
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
// Get the current playback volume for front left channel
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT,
&volume);
// Calculate the volume percentage
volume_percent = (volume - min) * 100 / (max - min);
} else {
std::cerr << "Mixer element not found\n";
snd_mixer_close(handle);
safe_exit(ENODEV);
}
// Clean up
snd_mixer_close(handle);
return volume_percent;
}
int main(int argc, char *argv[]) {
// signal handlers
@ -130,11 +76,11 @@ accept_new_connection:
int volume_percent_old;
while (1) {
volume_percent_old = volume_percent;
volume_percent = get_volume("Master");
volume_percent = alsa::get_volume("Master");
if (volume_percent != volume_percent_old) {
}
usleep(100000);
sprintf(bufferSend, "Volume: %d%%\n", get_volume("Master"));
sprintf(bufferSend, "Volume: %d%%\n", alsa::get_volume("Master"));
if (send(clientSocket, bufferSend, strlen(bufferSend), 0) == -1) {
std::cerr << "send() returned an error code\n";
safe_exit(-1);

1
src/main.h Normal file
View File

@ -0,0 +1 @@
extern int serverSocket;

1
src/secret.h Normal file
View File

@ -0,0 +1 @@
#define SECRET "1234"