aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 57e3ced5e393721edad2be2a68355ed212de7e2b (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
#include <arpa/inet.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <unistd.h>

#include "config.c"
#include "tinyosc.c"
#include "tinyosc.h"

/*
    chat-loop / main.c. sends text to vrchat using osc.
    Copyright (C) 2026 iceyrazor

    This program 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.

    This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
*/

static volatile bool keepRunning = true;

// handle Ctrl+C
static void sigintHandler(int x)
{
    keepRunning = false;
}

// execute command and put output into ret_buf buffer
void execlb(char* cmd, char* ret_buf)
{
    int link[2];
    pid_t pid;
    char buf[2048];

    if (pipe(link) == -1)
        exit(-5);

    if ((pid = fork()) == -1)
        exit(-5);

    if (pid == 0) {

        dup2(link[1], STDOUT_FILENO);
        close(link[0]);
        close(link[1]);
        execl("/bin/sh", "sh", "-c", cmd, (char*)0);
        exit(0);

    } else {

        close(link[1]);
        int nbytes = read(link[0], buf, sizeof(buf));
        if (nbytes > 2047)
            nbytes = 2047;
        snprintf(ret_buf, nbytes + 1, "%s", buf);
        wait(NULL);
        close(link[0]);
    }
}

int main()
{
    char wbuffer[2048]; // declare a 2Kb buffer to read packet data into
    int port = 9000;
    struct sockaddr_in servaddr;

    int socketfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (socketfd < 0) {
        perror("ERROR:");
        return -1;
    }

    servaddr.sin_family = AF_INET; // IPv4
    servaddr.sin_port = htons(port); // Server port
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Server IP
                                                       // register the SIGINT handler (Ctrl+C)
    signal(SIGINT, &sigintHandler);

    char* dist = (char*)malloc(sizeof(char) * 2048);
    execlb("uname -r", dist);
    dist[strlen(dist) - 1] = '\0';

    init_arr();

    char* comb_string = (char*)malloc(sizeof(char) * 2048);
    char* song_title = (char*)malloc(sizeof(char) * 2048);

    int i = 0;

    while (keepRunning) {
        execlb("src/title.sh", song_title);
        sprintf(comb_string, "%s\nLinux %s\n%s\n%s", title, dist, arr[i], song_title);

        int len = 0;
        len = tosc_writeMessage(wbuffer, sizeof(wbuffer), "/chatbox/input", "sTF", comb_string);
        sendto(socketfd, wbuffer, len, MSG_CONFIRM, (const struct sockaddr*)&servaddr, sizeof(servaddr));

        sleep(3);

        i = i + 1;
        if (i >= arr_size)
            i = 0;
    }

    free(comb_string);
    free(dist);
    free(song_title);

    return 0;
}