bspwm/bspc.c

99 lines
2.9 KiB
C
Raw Normal View History

/* Copyright (c) 2012, Bastien Dejean
2013-10-08 21:05:56 +02:00
* All rights reserved.
2014-01-19 14:41:37 +01:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2014-01-19 14:41:37 +01:00
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
2014-01-19 14:41:37 +01:00
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
2013-10-08 21:05:56 +02:00
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2013-10-08 21:05:56 +02:00
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2015-12-22 19:25:45 +01:00
#include <stdio.h>
2012-08-02 20:41:25 +02:00
#include <stdlib.h>
2015-12-22 19:25:45 +01:00
#include <sys/stat.h>
2013-10-07 22:25:42 +02:00
#include <sys/types.h>
2012-08-02 20:41:25 +02:00
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
2012-10-03 20:07:08 +03:00
#include "helpers.h"
2012-08-02 22:37:20 +02:00
#include "common.h"
2012-08-02 20:41:25 +02:00
int main(int argc, char *argv[])
{
int fd;
struct sockaddr_un sock_address;
char msg[BUFSIZ], rsp[BUFSIZ];
2012-08-02 20:41:25 +02:00
2015-12-22 19:25:45 +01:00
if (argc < 2) {
err("No arguments given.\n");
2015-12-22 19:25:45 +01:00
}
2012-08-02 20:41:25 +02:00
sock_address.sun_family = AF_UNIX;
char *sp;
2015-12-22 19:25:45 +01:00
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
err("Failed to create the socket.\n");
2015-12-22 19:25:45 +01:00
}
sp = getenv(SOCKET_ENV_VAR);
if (sp != NULL) {
snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", sp);
} else {
char *host = NULL;
int dn = 0, sn = 0;
2015-12-22 19:25:45 +01:00
if (xcb_parse_display(NULL, &host, &dn, &sn) != 0) {
snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), SOCKET_PATH_TPL, host, dn, sn);
2015-12-22 19:25:45 +01:00
}
free(host);
}
2015-12-22 19:25:45 +01:00
if (connect(fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1) {
err("Failed to connect to the socket.\n");
2015-12-22 19:25:45 +01:00
}
argc--, argv++;
int msg_len = 0;
2012-08-02 20:41:25 +02:00
for (int offset = 0, rem = sizeof(msg), n = 0; argc > 0 && rem > 0; offset += n, rem -= n, argc--, argv++) {
n = snprintf(msg + offset, rem, "%s%c", *argv, 0);
msg_len += n;
}
2015-12-22 19:25:45 +01:00
if (send(fd, msg, msg_len, 0) == -1) {
err("Failed to send the data.\n");
2015-12-22 19:25:45 +01:00
}
2012-08-28 21:15:29 +02:00
int ret = 0, nb;
while ((nb = recv(fd, rsp, sizeof(rsp)-1, 0)) > 0) {
2014-02-17 11:55:34 +01:00
if (nb == 1 && rsp[0] < MSG_LENGTH) {
ret = rsp[0];
if (ret == MSG_UNKNOWN) {
warn("Unknown command.\n");
} else if (ret == MSG_SYNTAX) {
warn("Invalid syntax.\n");
}
} else {
rsp[nb] = '\0';
printf("%s", rsp);
fflush(stdout);
}
}
2012-08-07 12:17:47 +02:00
close(fd);
return ret;
2012-08-02 20:41:25 +02:00
}