1
0
libflex/smartwatch/main.c
2024-10-01 23:22:22 +03:00

105 lines
2.9 KiB
C

#include <arpa/inet.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <libnotify/notify.h>
#include <libflex.h>
#define PORT 12345
int main(int argc, char **argv) {
if (!notify_init("smartwatch")) {
perror("notify_init");
return -1;
}
if (argc < 2) {
fprintf(stderr, "Usage: %s <path>\n", argv[0]);
return -1;
}
char *path = argv[1];
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd == -1) {
perror("socket");
return -1;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(PORT);
if (connect(client_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
return -1;
}
struct flx_msg msg = { 0 };
flx_msg_init_add_watch(&msg, path);
int rc = flx_write(client_fd, &msg);
assert(rc > 0);
struct flx_msg read_msg = { 0 };
uint8_t read_buffer[255] = { 0 };
size_t read_buffer_size = 0;
while (true) {
int msg_size = flx_read(read_buffer, sizeof(read_buffer), &read_buffer_size, client_fd, &read_msg);
if (msg_size == 0) {
break;
}
if (msg_size < 0) {
continue;
}
if (read_msg.action == FLX_ACT_NOTIFY) {
const char *notify_message = "Unknown.\n";
switch (read_msg.option) {
case FLX_NOTIFY_CREATE:
notify_message = "File created.\n";
break;
case FLX_NOTIFY_DELETE:
notify_message = "File deleted.\n";
break;
case FLX_NOTIFY_ACCESS:
notify_message = "File accessed.\n";
break;
case FLX_NOTIFY_CLOSE:
notify_message = "File written and closed.\n";
break;
case FLX_NOTIFY_MODIFY:
notify_message = "File modified.\n";
break;
case FLX_NOTIFY_MOVE:
notify_message = "File moved.\n";
break;
}
char *base_path = char_slice_dupe(read_msg.data[0]);
assert(base_path != NULL);
NotifyNotification *notification = notify_notification_new(base_path, notify_message, "dialog-information");
assert(notification != NULL);
notify_notification_set_urgency(notification, NOTIFY_URGENCY_CRITICAL);
notify_notification_show(notification, NULL);
g_object_unref(G_OBJECT(notification));
free(base_path);
}
memmove(read_buffer, read_buffer + msg_size, sizeof(read_buffer) - msg_size);
read_buffer_size -= msg_size;
}
close(client_fd);
return 0;
}