draw: stop parsing Pango markup twice, store AttrList
[awesome.git] / common / socket.c
blob5070defb36b7746071b6bada542b119bad04a0fa
1 /*
2 * socket.c - awesome client, communicate with socket, common functions
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2007 daniel@brinkers.de
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdio.h>
24 #include <errno.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
28 #include "common/socket.h"
29 #include "common/util.h"
31 #define CONTROL_UNIX_SOCKET_PATH ".awesome_ctl."
33 /** Get a sockaddr_un struct with information feeded for opening a
34 * communication to the awesome socket for given display
35 * \param display the display number
36 * \return sockaddr_un struct ready to be used or NULL if a problem occured
38 struct sockaddr_un *
39 socket_getaddr(const char *display)
41 char *homedir, *tmp;
42 const char *real_display = NULL;
43 ssize_t path_len, display_len;
44 struct sockaddr_un *addr;
46 addr = p_new(struct sockaddr_un, 1);
47 homedir = getenv("HOME");
48 display_len = a_strlen(display);
49 if(display_len)
51 if((tmp = strchr(display, ':')))
52 real_display = tmp + 1;
53 else
54 real_display = display;
55 if((tmp = strrchr(display, '.')))
56 *tmp = '\0';
59 /* a_strlen(display) because we strcat on display and
60 * + 2 for / and \0 */
61 path_len = a_strlen(homedir) + sizeof(CONTROL_UNIX_SOCKET_PATH)-1
62 + (display_len ? (a_strlen(real_display)) : 1) + 2;
64 if(path_len >= ssizeof(addr->sun_path))
66 fprintf(stderr, "error: path of control UNIX domain socket is too long");
67 return NULL;
69 a_strcpy(addr->sun_path, path_len, homedir);
70 a_strcat(addr->sun_path, path_len, "/");
71 a_strcat(addr->sun_path, path_len, CONTROL_UNIX_SOCKET_PATH);
72 if(display_len)
73 a_strcat(addr->sun_path, path_len, real_display);
74 else
75 a_strcat(addr->sun_path, path_len, "0");
77 addr->sun_family = AF_UNIX;
79 return addr;
82 /** Get a AF_UNIX socket for communicating with awesome
83 * \return the socket file descriptor
85 int
86 socket_getclient(void)
88 int csfd;
90 csfd = socket(AF_UNIX, SOCK_DGRAM, 0);
92 if(csfd < 0)
93 warn("error opening UNIX domain socket: %s", strerror(errno));
95 return csfd;
97 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80