<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>i3: src/ipc.c Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.2-20100208 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul> </div> <h1>src/ipc.c</h1><a href="a00068.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> * vim:ts=8:expandtab</span> <a name="l00003"></a>00003 <span class="comment"> *</span> <a name="l00004"></a>00004 <span class="comment"> * i3 - an improved dynamic tiling window manager</span> <a name="l00005"></a>00005 <span class="comment"> *</span> <a name="l00006"></a>00006 <span class="comment"> * © 2009-2010 Michael Stapelberg and contributors</span> <a name="l00007"></a>00007 <span class="comment"> *</span> <a name="l00008"></a>00008 <span class="comment"> * See file LICENSE for license information.</span> <a name="l00009"></a>00009 <span class="comment"> *</span> <a name="l00010"></a>00010 <span class="comment"> * ipc.c: Everything about the UNIX domain sockets for IPC</span> <a name="l00011"></a>00011 <span class="comment"> *</span> <a name="l00012"></a>00012 <span class="comment"> */</span> <a name="l00013"></a>00013 <span class="preprocessor">#include <sys/types.h></span> <a name="l00014"></a>00014 <span class="preprocessor">#include <sys/socket.h></span> <a name="l00015"></a>00015 <span class="preprocessor">#include <sys/stat.h></span> <a name="l00016"></a>00016 <span class="preprocessor">#include <sys/un.h></span> <a name="l00017"></a>00017 <span class="preprocessor">#include <fcntl.h></span> <a name="l00018"></a>00018 <span class="preprocessor">#include <unistd.h></span> <a name="l00019"></a>00019 <span class="preprocessor">#include <string.h></span> <a name="l00020"></a>00020 <span class="preprocessor">#include <errno.h></span> <a name="l00021"></a>00021 <span class="preprocessor">#include <err.h></span> <a name="l00022"></a>00022 <span class="preprocessor">#include <stdlib.h></span> <a name="l00023"></a>00023 <span class="preprocessor">#include <stdint.h></span> <a name="l00024"></a>00024 <span class="preprocessor">#include <stdio.h></span> <a name="l00025"></a>00025 <span class="preprocessor">#include <libgen.h></span> <a name="l00026"></a>00026 <span class="preprocessor">#include <ev.h></span> <a name="l00027"></a>00027 <span class="preprocessor">#include <yajl/yajl_gen.h></span> <a name="l00028"></a>00028 <span class="preprocessor">#include <yajl/yajl_parse.h></span> <a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="preprocessor">#include "<a class="code" href="a00047.html">queue.h</a>"</span> <a name="l00031"></a>00031 <span class="preprocessor">#include "<a class="code" href="a00042.html">ipc.h</a>"</span> <a name="l00032"></a>00032 <span class="preprocessor">#include "<a class="code" href="a00041.html">i3.h</a>"</span> <a name="l00033"></a>00033 <span class="preprocessor">#include "<a class="code" href="a00052.html">util.h</a>"</span> <a name="l00034"></a>00034 <span class="preprocessor">#include "<a class="code" href="a00033.html">commands.h</a>"</span> <a name="l00035"></a>00035 <span class="preprocessor">#include "<a class="code" href="a00044.html">log.h</a>"</span> <a name="l00036"></a>00036 <span class="preprocessor">#include "<a class="code" href="a00051.html">table.h</a>"</span> <a name="l00037"></a>00037 <span class="preprocessor">#include "<a class="code" href="a00048.html">randr.h</a>"</span> <a name="l00038"></a>00038 <span class="preprocessor">#include "<a class="code" href="a00034.html">config.h</a>"</span> <a name="l00039"></a>00039 <a name="l00040"></a>00040 <span class="comment">/* Shorter names for all those yajl_gen_* functions */</span> <a name="l00041"></a><a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">00041</a> <span class="preprocessor">#define y(x, ...) yajl_gen_ ## x (gen, ##__VA_ARGS__)</span> <a name="l00042"></a><a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">00042</a> <span class="preprocessor"></span><span class="preprocessor">#define ystr(str) yajl_gen_string(gen, (unsigned char*)str, strlen(str))</span> <a name="l00043"></a>00043 <span class="preprocessor"></span> <a name="l00044"></a><a class="code" href="a00068.html#a6c85bdb9ef63821dec47a855046b7eb5">00044</a> <a class="code" href="a00047.html#a4abce769a262e090c1f601605b9d6c76">TAILQ_HEAD</a>(ipc_client_head, <a class="code" href="a00017.html">ipc_client</a>) all_clients = <a class="code" href="a00047.html#ac269df7d4126ed9885156cbad2cbf8af">TAILQ_HEAD_INITIALIZER</a>(all_clients); <a name="l00045"></a>00045 <a name="l00046"></a>00046 <span class="comment">/*</span> <a name="l00047"></a>00047 <span class="comment"> * Puts the given socket file descriptor into non-blocking mode or dies if</span> <a name="l00048"></a>00048 <span class="comment"> * setting O_NONBLOCK failed. Non-blocking sockets are a good idea for our</span> <a name="l00049"></a>00049 <span class="comment"> * IPC model because we should by no means block the window manager.</span> <a name="l00050"></a>00050 <span class="comment"> *</span> <a name="l00051"></a>00051 <span class="comment"> */</span> <a name="l00052"></a>00052 static <span class="keywordtype">void</span> set_nonblock(<span class="keywordtype">int</span> sockfd) { <a name="l00053"></a>00053 <span class="keywordtype">int</span> flags = fcntl(sockfd, F_GETFL, 0); <a name="l00054"></a>00054 flags |= O_NONBLOCK; <a name="l00055"></a>00055 <span class="keywordflow">if</span> (fcntl(sockfd, F_SETFL, flags) < 0) <a name="l00056"></a>00056 err(-1, <span class="stringliteral">"Could not set O_NONBLOCK"</span>); <a name="l00057"></a>00057 } <a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="comment">/*</span> <a name="l00060"></a>00060 <span class="comment"> * Emulates mkdir -p (creates any missing folders)</span> <a name="l00061"></a>00061 <span class="comment"> *</span> <a name="l00062"></a>00062 <span class="comment"> */</span> <a name="l00063"></a><a class="code" href="a00068.html#a8b513e12d82663825d208b2363e2fecf">00063</a> <span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="a00068.html#a8b513e12d82663825d208b2363e2fecf">mkdirp</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *path) { <a name="l00064"></a>00064 <span class="keywordflow">if</span> (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) <a name="l00065"></a>00065 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00066"></a>00066 <span class="keywordflow">if</span> (errno != ENOENT) { <a name="l00067"></a>00067 <a class="code" href="a00044.html#a6828c68e0feb4b7fdfe0fc3ba9f3038b">ELOG</a>(<span class="stringliteral">"mkdir(%s) failed: %s\n"</span>, path, strerror(errno)); <a name="l00068"></a>00068 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00069"></a>00069 } <a name="l00070"></a>00070 <span class="keywordtype">char</span> *copy = strdup(path); <a name="l00071"></a>00071 <span class="comment">/* strip trailing slashes, if any */</span> <a name="l00072"></a>00072 <span class="keywordflow">while</span> (copy[strlen(copy)-1] == <span class="charliteral">'/'</span>) <a name="l00073"></a>00073 copy[strlen(copy)-1] = <span class="charliteral">'\0'</span>; <a name="l00074"></a>00074 <a name="l00075"></a>00075 <span class="keywordtype">char</span> *sep = strrchr(copy, <span class="charliteral">'/'</span>); <a name="l00076"></a>00076 <span class="keywordflow">if</span> (sep == NULL) <a name="l00077"></a>00077 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00078"></a>00078 *sep = <span class="charliteral">'\0'</span>; <a name="l00079"></a>00079 <span class="keywordtype">bool</span> result = <span class="keyword">false</span>; <a name="l00080"></a>00080 <span class="keywordflow">if</span> (<a class="code" href="a00068.html#a8b513e12d82663825d208b2363e2fecf">mkdirp</a>(copy)) <a name="l00081"></a>00081 result = <a class="code" href="a00068.html#a8b513e12d82663825d208b2363e2fecf">mkdirp</a>(path); <a name="l00082"></a>00082 free(copy); <a name="l00083"></a>00083 <a name="l00084"></a>00084 <span class="keywordflow">return</span> result; <a name="l00085"></a>00085 } <a name="l00086"></a>00086 <a name="l00087"></a><a class="code" href="a00068.html#a8fe6a5445971cf6e5b3192f40ea60b2a">00087</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="a00068.html#a8fe6a5445971cf6e5b3192f40ea60b2a">ipc_send_message</a>(<span class="keywordtype">int</span> fd, <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *payload, <a name="l00088"></a>00088 <span class="keywordtype">int</span> message_type, <span class="keywordtype">int</span> message_size) { <a name="l00089"></a>00089 <span class="keywordtype">int</span> buffer_size = strlen(<span class="stringliteral">"i3-ipc"</span>) + <span class="keyword">sizeof</span>(uint32_t) + <a name="l00090"></a>00090 <span class="keyword">sizeof</span>(uint32_t) + message_size; <a name="l00091"></a>00091 <span class="keywordtype">char</span> msg[buffer_size]; <a name="l00092"></a>00092 <span class="keywordtype">char</span> *walk = msg; <a name="l00093"></a>00093 <a name="l00094"></a>00094 strcpy(walk, <span class="stringliteral">"i3-ipc"</span>); <a name="l00095"></a>00095 walk += strlen(<span class="stringliteral">"i3-ipc"</span>); <a name="l00096"></a>00096 memcpy(walk, &message_size, <span class="keyword">sizeof</span>(uint32_t)); <a name="l00097"></a>00097 walk += <span class="keyword">sizeof</span>(uint32_t); <a name="l00098"></a>00098 memcpy(walk, &message_type, <span class="keyword">sizeof</span>(uint32_t)); <a name="l00099"></a>00099 walk += <span class="keyword">sizeof</span>(uint32_t); <a name="l00100"></a>00100 memcpy(walk, payload, message_size); <a name="l00101"></a>00101 <a name="l00102"></a>00102 <span class="keywordtype">int</span> sent_bytes = 0; <a name="l00103"></a>00103 <span class="keywordtype">int</span> bytes_to_go = buffer_size; <a name="l00104"></a>00104 <span class="keywordflow">while</span> (sent_bytes < bytes_to_go) { <a name="l00105"></a>00105 <span class="keywordtype">int</span> n = write(fd, msg + sent_bytes, bytes_to_go); <a name="l00106"></a>00106 <span class="keywordflow">if</span> (n == -1) { <a name="l00107"></a>00107 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"write() failed: %s\n"</span>, strerror(errno)); <a name="l00108"></a>00108 <span class="keywordflow">return</span>; <a name="l00109"></a>00109 } <a name="l00110"></a>00110 <a name="l00111"></a>00111 sent_bytes += n; <a name="l00112"></a>00112 bytes_to_go -= n; <a name="l00113"></a>00113 } <a name="l00114"></a>00114 } <a name="l00115"></a>00115 <a name="l00116"></a>00116 <span class="comment">/*</span> <a name="l00117"></a>00117 <span class="comment"> * Sends the specified event to all IPC clients which are currently connected</span> <a name="l00118"></a>00118 <span class="comment"> * and subscribed to this kind of event.</span> <a name="l00119"></a>00119 <span class="comment"> *</span> <a name="l00120"></a>00120 <span class="comment"> */</span> <a name="l00121"></a><a class="code" href="a00042.html#ad525c6fad9f4978ae1f06282ad9de6c6">00121</a> <span class="keywordtype">void</span> <a class="code" href="a00068.html#ad525c6fad9f4978ae1f06282ad9de6c6" title="Sends the specified event to all IPC clients which are currently connected and subscribed...">ipc_send_event</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *event, uint32_t message_type, <span class="keyword">const</span> <span class="keywordtype">char</span> *payload) { <a name="l00122"></a>00122 <a class="code" href="a00017.html">ipc_client</a> *current; <a name="l00123"></a>00123 <a class="code" href="a00047.html#a16e5fc168e7f3494d4b1df54447d871e">TAILQ_FOREACH</a>(current, &all_clients, clients) { <a name="l00124"></a>00124 <span class="comment">/* see if this client is interested in this event */</span> <a name="l00125"></a>00125 <span class="keywordtype">bool</span> interested = <span class="keyword">false</span>; <a name="l00126"></a>00126 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < current-><a class="code" href="a00017.html#a46ac6148b46c11a44deb54146c616d86">num_events</a>; i++) { <a name="l00127"></a>00127 <span class="keywordflow">if</span> (strcasecmp(current-><a class="code" href="a00017.html#a33769d4392c623c174ffd78c643fa2a3">events</a>[i], event) != 0) <a name="l00128"></a>00128 <span class="keywordflow">continue</span>; <a name="l00129"></a>00129 interested = <span class="keyword">true</span>; <a name="l00130"></a>00130 <span class="keywordflow">break</span>; <a name="l00131"></a>00131 } <a name="l00132"></a>00132 <span class="keywordflow">if</span> (!interested) <a name="l00133"></a>00133 <span class="keywordflow">continue</span>; <a name="l00134"></a>00134 <a name="l00135"></a>00135 <a class="code" href="a00068.html#a8fe6a5445971cf6e5b3192f40ea60b2a">ipc_send_message</a>(current-><a class="code" href="a00017.html#abbf0032a20872af40f2c48187482c479">fd</a>, (<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)payload, <a name="l00136"></a>00136 message_type, strlen(payload)); <a name="l00137"></a>00137 } <a name="l00138"></a>00138 } <a name="l00139"></a>00139 <a name="l00140"></a>00140 <span class="comment">/*</span> <a name="l00141"></a>00141 <span class="comment"> * Calls shutdown() on each socket and closes it. This function to be called</span> <a name="l00142"></a>00142 <span class="comment"> * when exiting or restarting only!</span> <a name="l00143"></a>00143 <span class="comment"> *</span> <a name="l00144"></a>00144 <span class="comment"> */</span> <a name="l00145"></a><a class="code" href="a00042.html#ae12d3a1c83452d7fb9b9ae9ce23bb424">00145</a> <span class="keywordtype">void</span> <a class="code" href="a00068.html#ae12d3a1c83452d7fb9b9ae9ce23bb424" title="Calls shutdown() on each socket and closes it.">ipc_shutdown</a>() { <a name="l00146"></a>00146 <a class="code" href="a00017.html">ipc_client</a> *current; <a name="l00147"></a>00147 <a class="code" href="a00047.html#a16e5fc168e7f3494d4b1df54447d871e">TAILQ_FOREACH</a>(current, &all_clients, clients) { <a name="l00148"></a>00148 shutdown(current-><a class="code" href="a00017.html#abbf0032a20872af40f2c48187482c479">fd</a>, SHUT_RDWR); <a name="l00149"></a>00149 close(current-><a class="code" href="a00017.html#abbf0032a20872af40f2c48187482c479">fd</a>); <a name="l00150"></a>00150 } <a name="l00151"></a>00151 } <a name="l00152"></a>00152 <a name="l00153"></a>00153 <span class="comment">/*</span> <a name="l00154"></a>00154 <span class="comment"> * Executes the command and returns whether it could be successfully parsed</span> <a name="l00155"></a>00155 <span class="comment"> * or not (at the moment, always returns true).</span> <a name="l00156"></a>00156 <span class="comment"> *</span> <a name="l00157"></a>00157 <span class="comment"> */</span> <a name="l00158"></a><a class="code" href="a00068.html#a30c9a68044bb51d56d31685fc1343a14">00158</a> <a class="code" href="a00042.html#a0d061102ea72ae69fcd76e0692ccac71">IPC_HANDLER</a>(command) { <a name="l00159"></a>00159 <span class="comment">/* To get a properly terminated buffer, we copy</span> <a name="l00160"></a>00160 <span class="comment"> * message_size bytes out of the buffer */</span> <a name="l00161"></a>00161 <span class="keywordtype">char</span> *command = <a class="code" href="a00077.html#ad475e1b898d9da9b2a8b92e3da51b3dd" title="Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there...">scalloc</a>(message_size); <a name="l00162"></a>00162 strncpy(command, (<span class="keyword">const</span> <span class="keywordtype">char</span>*)message, message_size); <a name="l00163"></a>00163 <a class="code" href="a00061.html#a321767a534b7214d81f4cfeaa4bd0736" title="Parses a command, see file CMDMODE for more information.">parse_command</a>(<a class="code" href="a00071.html#ada13c42121d64521629e06e7a5e1db16">global_conn</a>, (<span class="keyword">const</span> <span class="keywordtype">char</span>*)command); <a name="l00164"></a>00164 free(command); <a name="l00165"></a>00165 <a name="l00166"></a>00166 <span class="comment">/* For now, every command gets a positive acknowledge</span> <a name="l00167"></a>00167 <span class="comment"> * (will change with the new command parser) */</span> <a name="l00168"></a>00168 <span class="keyword">const</span> <span class="keywordtype">char</span> *reply = <span class="stringliteral">"{\"success\":true}"</span>; <a name="l00169"></a>00169 <a class="code" href="a00068.html#a8fe6a5445971cf6e5b3192f40ea60b2a">ipc_send_message</a>(fd, (<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)reply, <a name="l00170"></a>00170 I3_IPC_REPLY_TYPE_COMMAND, strlen(reply)); <a name="l00171"></a>00171 } <a name="l00172"></a>00172 <a name="l00173"></a>00173 <span class="comment">/*</span> <a name="l00174"></a>00174 <span class="comment"> * Formats the reply message for a GET_WORKSPACES request and sends it to the</span> <a name="l00175"></a>00175 <span class="comment"> * client</span> <a name="l00176"></a>00176 <span class="comment"> *</span> <a name="l00177"></a>00177 <span class="comment"> */</span> <a name="l00178"></a><a class="code" href="a00068.html#a948e9f8fbd8792ba0a9c9aeb340344a6">00178</a> <a class="code" href="a00042.html#a0d061102ea72ae69fcd76e0692ccac71">IPC_HANDLER</a>(get_workspaces) { <a name="l00179"></a>00179 <a class="code" href="a00025.html" title="The concept of Workspaces is known from various other window managers.">Workspace</a> *ws; <a name="l00180"></a>00180 <a name="l00181"></a>00181 <a class="code" href="a00007.html" title="A client is X11-speak for a window.">Client</a> *last_focused = <a class="code" href="a00047.html#a91bac7d0d32e3ec56bc579ace45ed8cd">SLIST_FIRST</a>(&(<a class="code" href="a00076.html#a6dc99dff7063221c1096561951c0386b">c_ws</a>->focus_stack)); <a name="l00182"></a>00182 <span class="keywordflow">if</span> (last_focused == <a class="code" href="a00047.html#a33283f951b262b1ac57d20c0e4e0862c">SLIST_END</a>(&(<a class="code" href="a00076.html#a6dc99dff7063221c1096561951c0386b">c_ws</a>->focus_stack))) <a name="l00183"></a>00183 last_focused = NULL; <a name="l00184"></a>00184 <a name="l00185"></a>00185 yajl_gen gen = yajl_gen_alloc(NULL, NULL); <a name="l00186"></a>00186 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(array_open); <a name="l00187"></a>00187 <a name="l00188"></a>00188 <a class="code" href="a00047.html#a16e5fc168e7f3494d4b1df54447d871e">TAILQ_FOREACH</a>(ws, <a class="code" href="a00076.html#afb45f3a3a9c66c3e3a5e4d372ae24da7">workspaces</a>, <a class="code" href="a00076.html#afb45f3a3a9c66c3e3a5e4d372ae24da7">workspaces</a>) { <a name="l00189"></a>00189 <span class="keywordflow">if</span> (ws-><a class="code" href="a00025.html#aeee203e013dae84555d28f8247a7e375" title="Backpointer to the output this workspace is on.">output</a> == NULL) <a name="l00190"></a>00190 <span class="keywordflow">continue</span>; <a name="l00191"></a>00191 <a name="l00192"></a>00192 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(map_open); <a name="l00193"></a>00193 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"num"</span>); <a name="l00194"></a>00194 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(integer, ws-><a class="code" href="a00025.html#a7dfb9b375b2740db75bb82eb0b2be41d" title="Number of this workspace, starting from 0.">num</a> + 1); <a name="l00195"></a>00195 <a name="l00196"></a>00196 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"name"</span>); <a name="l00197"></a>00197 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(ws-><a class="code" href="a00025.html#a2cf7e70b45757f1c61a34f5b50767765" title="Name of the workspace (in UTF-8).">utf8_name</a>); <a name="l00198"></a>00198 <a name="l00199"></a>00199 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"visible"</span>); <a name="l00200"></a>00200 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(<span class="keywordtype">bool</span>, ws-><a class="code" href="a00025.html#aeee203e013dae84555d28f8247a7e375" title="Backpointer to the output this workspace is on.">output</a>-><a class="code" href="a00026.html#a9bcf702b65d8f47f98eafbefff529ffe" title="Current workspace selected on this virtual screen.">current_workspace</a> == ws); <a name="l00201"></a>00201 <a name="l00202"></a>00202 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"focused"</span>); <a name="l00203"></a>00203 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(<span class="keywordtype">bool</span>, <a class="code" href="a00076.html#a6dc99dff7063221c1096561951c0386b">c_ws</a> == ws); <a name="l00204"></a>00204 <a name="l00205"></a>00205 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"rect"</span>); <a name="l00206"></a>00206 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(map_open); <a name="l00207"></a>00207 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"x"</span>); <a name="l00208"></a>00208 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(integer, ws-><a class="code" href="a00025.html#ac11bedb880b568b9c540fc9a376607ad" title="x, y, width, height">rect</a>.<a class="code" href="a00020.html#a6f5ab43ec9da45a9c3ddbc45d9aa3bbc">x</a>); <a name="l00209"></a>00209 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"y"</span>); <a name="l00210"></a>00210 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(integer, ws-><a class="code" href="a00025.html#ac11bedb880b568b9c540fc9a376607ad" title="x, y, width, height">rect</a>.<a class="code" href="a00020.html#ac77eddc4cc31fe7881da4a23cdca0ea5">y</a>); <a name="l00211"></a>00211 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"width"</span>); <a name="l00212"></a>00212 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(integer, ws-><a class="code" href="a00025.html#ac11bedb880b568b9c540fc9a376607ad" title="x, y, width, height">rect</a>.<a class="code" href="a00020.html#adb31dc42e48628c3e513c60296779ef8">width</a>); <a name="l00213"></a>00213 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"height"</span>); <a name="l00214"></a>00214 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(integer, ws-><a class="code" href="a00025.html#ac11bedb880b568b9c540fc9a376607ad" title="x, y, width, height">rect</a>.<a class="code" href="a00020.html#a0a074c07d144c9647e3eb60ef54f88c3">height</a>); <a name="l00215"></a>00215 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(map_close); <a name="l00216"></a>00216 <a name="l00217"></a>00217 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"output"</span>); <a name="l00218"></a>00218 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(ws-><a class="code" href="a00025.html#aeee203e013dae84555d28f8247a7e375" title="Backpointer to the output this workspace is on.">output</a>-><a class="code" href="a00026.html#a4c8b185b15cf45e3ca300cb54b2d4650" title="Name of the output.">name</a>); <a name="l00219"></a>00219 <a name="l00220"></a>00220 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"urgent"</span>); <a name="l00221"></a>00221 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(<span class="keywordtype">bool</span>, ws-><a class="code" href="a00025.html#ae43262c5fb60e7d7a93da1041835eeb2" title="True if any client on this workspace has its urgent flag set.">urgent</a>); <a name="l00222"></a>00222 <a name="l00223"></a>00223 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(map_close); <a name="l00224"></a>00224 } <a name="l00225"></a>00225 <a name="l00226"></a>00226 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(array_close); <a name="l00227"></a>00227 <a name="l00228"></a>00228 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *payload; <a name="l00229"></a>00229 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length; <a name="l00230"></a>00230 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(get_buf, &payload, &length); <a name="l00231"></a>00231 <a name="l00232"></a>00232 <a class="code" href="a00068.html#a8fe6a5445971cf6e5b3192f40ea60b2a">ipc_send_message</a>(fd, payload, I3_IPC_REPLY_TYPE_WORKSPACES, length); <a name="l00233"></a>00233 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(free); <a name="l00234"></a>00234 } <a name="l00235"></a>00235 <a name="l00236"></a>00236 <span class="comment">/*</span> <a name="l00237"></a>00237 <span class="comment"> * Formats the reply message for a GET_OUTPUTS request and sends it to the</span> <a name="l00238"></a>00238 <span class="comment"> * client</span> <a name="l00239"></a>00239 <span class="comment"> *</span> <a name="l00240"></a>00240 <span class="comment"> */</span> <a name="l00241"></a><a class="code" href="a00068.html#a6d037537a3696ad46474b5ba6cc6ccd9">00241</a> <a class="code" href="a00042.html#a0d061102ea72ae69fcd76e0692ccac71">IPC_HANDLER</a>(get_outputs) { <a name="l00242"></a>00242 <a class="code" href="a00026.html" title="An Output is a physical output on your graphics driver.">Output</a> *output; <a name="l00243"></a>00243 <a name="l00244"></a>00244 yajl_gen gen = yajl_gen_alloc(NULL, NULL); <a name="l00245"></a>00245 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(array_open); <a name="l00246"></a>00246 <a name="l00247"></a>00247 <a class="code" href="a00047.html#a16e5fc168e7f3494d4b1df54447d871e">TAILQ_FOREACH</a>(output, &<a class="code" href="a00073.html#a9fecba0738529e3d9ff0d4fa7c31ffa9">outputs</a>, <a class="code" href="a00073.html#a9fecba0738529e3d9ff0d4fa7c31ffa9">outputs</a>) { <a name="l00248"></a>00248 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(map_open); <a name="l00249"></a>00249 <a name="l00250"></a>00250 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"name"</span>); <a name="l00251"></a>00251 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(output-><a class="code" href="a00026.html#a4c8b185b15cf45e3ca300cb54b2d4650" title="Name of the output.">name</a>); <a name="l00252"></a>00252 <a name="l00253"></a>00253 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"active"</span>); <a name="l00254"></a>00254 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(<span class="keywordtype">bool</span>, output-><a class="code" href="a00026.html#a7c56e7415ebe0aa040614d1244e2da6b" title="Whether the output is currently active (has a CRTC attached with a valid mode).">active</a>); <a name="l00255"></a>00255 <a name="l00256"></a>00256 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"rect"</span>); <a name="l00257"></a>00257 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(map_open); <a name="l00258"></a>00258 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"x"</span>); <a name="l00259"></a>00259 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(integer, output-><a class="code" href="a00026.html#aedbb714bf5d5ef9a28b5ff048321ed99" title="x, y, width, height">rect</a>.<a class="code" href="a00020.html#a6f5ab43ec9da45a9c3ddbc45d9aa3bbc">x</a>); <a name="l00260"></a>00260 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"y"</span>); <a name="l00261"></a>00261 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(integer, output-><a class="code" href="a00026.html#aedbb714bf5d5ef9a28b5ff048321ed99" title="x, y, width, height">rect</a>.<a class="code" href="a00020.html#ac77eddc4cc31fe7881da4a23cdca0ea5">y</a>); <a name="l00262"></a>00262 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"width"</span>); <a name="l00263"></a>00263 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(integer, output-><a class="code" href="a00026.html#aedbb714bf5d5ef9a28b5ff048321ed99" title="x, y, width, height">rect</a>.<a class="code" href="a00020.html#adb31dc42e48628c3e513c60296779ef8">width</a>); <a name="l00264"></a>00264 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"height"</span>); <a name="l00265"></a>00265 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(integer, output-><a class="code" href="a00026.html#aedbb714bf5d5ef9a28b5ff048321ed99" title="x, y, width, height">rect</a>.<a class="code" href="a00020.html#a0a074c07d144c9647e3eb60ef54f88c3">height</a>); <a name="l00266"></a>00266 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(map_close); <a name="l00267"></a>00267 <a name="l00268"></a>00268 <a class="code" href="a00068.html#a9e853d0fe52033d8c444e88cf1065852">ystr</a>(<span class="stringliteral">"current_workspace"</span>); <a name="l00269"></a>00269 <span class="keywordflow">if</span> (output-><a class="code" href="a00026.html#a9bcf702b65d8f47f98eafbefff529ffe" title="Current workspace selected on this virtual screen.">current_workspace</a> == NULL) <a name="l00270"></a>00270 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(null); <a name="l00271"></a>00271 <span class="keywordflow">else</span> <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(integer, output-><a class="code" href="a00026.html#a9bcf702b65d8f47f98eafbefff529ffe" title="Current workspace selected on this virtual screen.">current_workspace</a>-><a class="code" href="a00025.html#a7dfb9b375b2740db75bb82eb0b2be41d" title="Number of this workspace, starting from 0.">num</a> + 1); <a name="l00272"></a>00272 <a name="l00273"></a>00273 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(map_close); <a name="l00274"></a>00274 } <a name="l00275"></a>00275 <a name="l00276"></a>00276 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(array_close); <a name="l00277"></a>00277 <a name="l00278"></a>00278 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *payload; <a name="l00279"></a>00279 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length; <a name="l00280"></a>00280 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(get_buf, &payload, &length); <a name="l00281"></a>00281 <a name="l00282"></a>00282 <a class="code" href="a00068.html#a8fe6a5445971cf6e5b3192f40ea60b2a">ipc_send_message</a>(fd, payload, I3_IPC_REPLY_TYPE_OUTPUTS, length); <a name="l00283"></a>00283 <a class="code" href="a00068.html#a16e56e533e1eb98fda15e06fdec03da6">y</a>(free); <a name="l00284"></a>00284 } <a name="l00285"></a>00285 <a name="l00286"></a>00286 <span class="comment">/*</span> <a name="l00287"></a>00287 <span class="comment"> * Callback for the YAJL parser (will be called when a string is parsed).</span> <a name="l00288"></a>00288 <span class="comment"> *</span> <a name="l00289"></a>00289 <span class="comment"> */</span> <a name="l00290"></a><a class="code" href="a00068.html#a7f11b4828bd231ec42d374a8e30a39e9">00290</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="a00068.html#a7f11b4828bd231ec42d374a8e30a39e9">add_subscription</a>(<span class="keywordtype">void</span> *extra, <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *s, <a name="l00291"></a>00291 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len) { <a name="l00292"></a>00292 <a class="code" href="a00017.html">ipc_client</a> *client = extra; <a name="l00293"></a>00293 <a name="l00294"></a>00294 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"should add subscription to extra %p, sub %.*s\n"</span>, client, len, s); <a name="l00295"></a>00295 <span class="keywordtype">int</span> <span class="keyword">event</span> = client-><a class="code" href="a00017.html#a46ac6148b46c11a44deb54146c616d86">num_events</a>; <a name="l00296"></a>00296 <a name="l00297"></a>00297 client-><a class="code" href="a00017.html#a46ac6148b46c11a44deb54146c616d86">num_events</a>++; <a name="l00298"></a>00298 client-><a class="code" href="a00017.html#a33769d4392c623c174ffd78c643fa2a3">events</a> = realloc(client-><a class="code" href="a00017.html#a33769d4392c623c174ffd78c643fa2a3">events</a>, client-><a class="code" href="a00017.html#a46ac6148b46c11a44deb54146c616d86">num_events</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">char</span>*)); <a name="l00299"></a>00299 <span class="comment">/* We copy the string because it is not null-terminated and strndup()</span> <a name="l00300"></a>00300 <span class="comment"> * is missing on some BSD systems */</span> <a name="l00301"></a>00301 client-><a class="code" href="a00017.html#a33769d4392c623c174ffd78c643fa2a3">events</a>[event] = <a class="code" href="a00077.html#ad475e1b898d9da9b2a8b92e3da51b3dd" title="Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there...">scalloc</a>(len+1); <a name="l00302"></a>00302 memcpy(client-><a class="code" href="a00017.html#a33769d4392c623c174ffd78c643fa2a3">events</a>[event], s, len); <a name="l00303"></a>00303 <a name="l00304"></a>00304 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"client is now subscribed to:\n"</span>); <a name="l00305"></a>00305 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < client-><a class="code" href="a00017.html#a46ac6148b46c11a44deb54146c616d86">num_events</a>; i++) <a name="l00306"></a>00306 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"event %s\n"</span>, client-><a class="code" href="a00017.html#a33769d4392c623c174ffd78c643fa2a3">events</a>[i]); <a name="l00307"></a>00307 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"(done)\n"</span>); <a name="l00308"></a>00308 <a name="l00309"></a>00309 <span class="keywordflow">return</span> 1; <a name="l00310"></a>00310 } <a name="l00311"></a>00311 <a name="l00312"></a>00312 <span class="comment">/*</span> <a name="l00313"></a>00313 <span class="comment"> * Subscribes this connection to the event types which were given as a JSON</span> <a name="l00314"></a>00314 <span class="comment"> * serialized array in the payload field of the message.</span> <a name="l00315"></a>00315 <span class="comment"> *</span> <a name="l00316"></a>00316 <span class="comment"> */</span> <a name="l00317"></a><a class="code" href="a00068.html#af5a7853e826d36c9b130d9054e7bf6fb">00317</a> <a class="code" href="a00042.html#a0d061102ea72ae69fcd76e0692ccac71">IPC_HANDLER</a>(subscribe) { <a name="l00318"></a>00318 yajl_handle p; <a name="l00319"></a>00319 yajl_callbacks callbacks; <a name="l00320"></a>00320 yajl_status stat; <a name="l00321"></a>00321 <a class="code" href="a00017.html">ipc_client</a> *current, *client = NULL; <a name="l00322"></a>00322 <a name="l00323"></a>00323 <span class="comment">/* Search the ipc_client structure for this connection */</span> <a name="l00324"></a>00324 <a class="code" href="a00047.html#a16e5fc168e7f3494d4b1df54447d871e">TAILQ_FOREACH</a>(current, &all_clients, clients) { <a name="l00325"></a>00325 <span class="keywordflow">if</span> (current-><a class="code" href="a00017.html#abbf0032a20872af40f2c48187482c479">fd</a> != fd) <a name="l00326"></a>00326 <span class="keywordflow">continue</span>; <a name="l00327"></a>00327 <a name="l00328"></a>00328 client = current; <a name="l00329"></a>00329 <span class="keywordflow">break</span>; <a name="l00330"></a>00330 } <a name="l00331"></a>00331 <a name="l00332"></a>00332 <span class="keywordflow">if</span> (client == NULL) { <a name="l00333"></a>00333 <a class="code" href="a00044.html#a6828c68e0feb4b7fdfe0fc3ba9f3038b">ELOG</a>(<span class="stringliteral">"Could not find ipc_client data structure for fd %d\n"</span>, fd); <a name="l00334"></a>00334 <span class="keywordflow">return</span>; <a name="l00335"></a>00335 } <a name="l00336"></a>00336 <a name="l00337"></a>00337 <span class="comment">/* Setup the JSON parser */</span> <a name="l00338"></a>00338 memset(&callbacks, 0, <span class="keyword">sizeof</span>(yajl_callbacks)); <a name="l00339"></a>00339 callbacks.yajl_string = <a class="code" href="a00068.html#a7f11b4828bd231ec42d374a8e30a39e9">add_subscription</a>; <a name="l00340"></a>00340 <a name="l00341"></a>00341 p = yajl_alloc(&callbacks, NULL, NULL, (<span class="keywordtype">void</span>*)client); <a name="l00342"></a>00342 stat = yajl_parse(p, (<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)message, message_size); <a name="l00343"></a>00343 <span class="keywordflow">if</span> (stat != yajl_status_ok) { <a name="l00344"></a>00344 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *err; <a name="l00345"></a>00345 err = yajl_get_error(p, <span class="keyword">true</span>, (<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)message, <a name="l00346"></a>00346 message_size); <a name="l00347"></a>00347 <a class="code" href="a00044.html#a6828c68e0feb4b7fdfe0fc3ba9f3038b">ELOG</a>(<span class="stringliteral">"YAJL parse error: %s\n"</span>, err); <a name="l00348"></a>00348 yajl_free_error(p, err); <a name="l00349"></a>00349 <a name="l00350"></a>00350 <span class="keyword">const</span> <span class="keywordtype">char</span> *reply = <span class="stringliteral">"{\"success\":false}"</span>; <a name="l00351"></a>00351 <a class="code" href="a00068.html#a8fe6a5445971cf6e5b3192f40ea60b2a">ipc_send_message</a>(fd, (<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)reply, <a name="l00352"></a>00352 I3_IPC_REPLY_TYPE_SUBSCRIBE, strlen(reply)); <a name="l00353"></a>00353 yajl_free(p); <a name="l00354"></a>00354 <span class="keywordflow">return</span>; <a name="l00355"></a>00355 } <a name="l00356"></a>00356 yajl_free(p); <a name="l00357"></a>00357 <span class="keyword">const</span> <span class="keywordtype">char</span> *reply = <span class="stringliteral">"{\"success\":true}"</span>; <a name="l00358"></a>00358 <a class="code" href="a00068.html#a8fe6a5445971cf6e5b3192f40ea60b2a">ipc_send_message</a>(fd, (<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)reply, <a name="l00359"></a>00359 I3_IPC_REPLY_TYPE_SUBSCRIBE, strlen(reply)); <a name="l00360"></a>00360 } <a name="l00361"></a>00361 <a name="l00362"></a>00362 <span class="comment">/* The index of each callback function corresponds to the numeric</span> <a name="l00363"></a>00363 <span class="comment"> * value of the message type (see include/i3/ipc.h) */</span> <a name="l00364"></a><a class="code" href="a00068.html#a68dd4f9828959ce3ad7e600890cae913">00364</a> <a class="code" href="a00042.html#a6b1a00999ddde8264249521fa10d7b08">handler_t</a> <a class="code" href="a00068.html#a68dd4f9828959ce3ad7e600890cae913">handlers</a>[4] = { <a name="l00365"></a>00365 handle_command, <a name="l00366"></a>00366 handle_get_workspaces, <a name="l00367"></a>00367 handle_subscribe, <a name="l00368"></a>00368 handle_get_outputs <a name="l00369"></a>00369 }; <a name="l00370"></a>00370 <a name="l00371"></a>00371 <span class="comment">/*</span> <a name="l00372"></a>00372 <span class="comment"> * Handler for activity on a client connection, receives a message from a</span> <a name="l00373"></a>00373 <span class="comment"> * client.</span> <a name="l00374"></a>00374 <span class="comment"> *</span> <a name="l00375"></a>00375 <span class="comment"> * For now, the maximum message size is 2048. I’m not sure for what the</span> <a name="l00376"></a>00376 <span class="comment"> * IPC interface will be used in the future, thus I’m not implementing a</span> <a name="l00377"></a>00377 <span class="comment"> * mechanism for arbitrarily long messages, as it seems like overkill</span> <a name="l00378"></a>00378 <span class="comment"> * at the moment.</span> <a name="l00379"></a>00379 <span class="comment"> *</span> <a name="l00380"></a>00380 <span class="comment"> */</span> <a name="l00381"></a><a class="code" href="a00068.html#a81fcba6a5550d614cc8d47b46aaa00bd">00381</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="a00068.html#a81fcba6a5550d614cc8d47b46aaa00bd">ipc_receive_message</a>(EV_P_ <span class="keyword">struct</span> ev_io *w, <span class="keywordtype">int</span> revents) { <a name="l00382"></a>00382 <span class="keywordtype">char</span> buf[2048]; <a name="l00383"></a>00383 <span class="keywordtype">int</span> n = read(w->fd, buf, <span class="keyword">sizeof</span>(buf)); <a name="l00384"></a>00384 <a name="l00385"></a>00385 <span class="comment">/* On error or an empty message, we close the connection */</span> <a name="l00386"></a>00386 <span class="keywordflow">if</span> (n <= 0) { <a name="l00387"></a>00387 <span class="preprocessor">#if 0</span> <a name="l00388"></a>00388 <span class="preprocessor"></span> <span class="comment">/* FIXME: I get these when closing a client socket,</span> <a name="l00389"></a>00389 <span class="comment"> * therefore we just treat them as an error. Is this</span> <a name="l00390"></a>00390 <span class="comment"> * correct? */</span> <a name="l00391"></a>00391 <span class="keywordflow">if</span> (errno == EAGAIN || errno == EWOULDBLOCK) <a name="l00392"></a>00392 <span class="keywordflow">return</span>; <a name="l00393"></a>00393 <span class="preprocessor">#endif</span> <a name="l00394"></a>00394 <span class="preprocessor"></span> <a name="l00395"></a>00395 <span class="comment">/* If not, there was some kind of error. We don’t bother</span> <a name="l00396"></a>00396 <span class="comment"> * and close the connection */</span> <a name="l00397"></a>00397 close(w->fd); <a name="l00398"></a>00398 <a name="l00399"></a>00399 <span class="comment">/* Delete the client from the list of clients */</span> <a name="l00400"></a>00400 <a class="code" href="a00017.html">ipc_client</a> *current; <a name="l00401"></a>00401 <a class="code" href="a00047.html#a16e5fc168e7f3494d4b1df54447d871e">TAILQ_FOREACH</a>(current, &all_clients, clients) { <a name="l00402"></a>00402 <span class="keywordflow">if</span> (current-><a class="code" href="a00017.html#abbf0032a20872af40f2c48187482c479">fd</a> != w->fd) <a name="l00403"></a>00403 <span class="keywordflow">continue</span>; <a name="l00404"></a>00404 <a name="l00405"></a>00405 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < current-><a class="code" href="a00017.html#a46ac6148b46c11a44deb54146c616d86">num_events</a>; i++) <a name="l00406"></a>00406 free(current-><a class="code" href="a00017.html#a33769d4392c623c174ffd78c643fa2a3">events</a>[i]); <a name="l00407"></a>00407 <span class="comment">/* We can call TAILQ_REMOVE because we break out of the</span> <a name="l00408"></a>00408 <span class="comment"> * TAILQ_FOREACH afterwards */</span> <a name="l00409"></a>00409 <a class="code" href="a00047.html#aaae87ca18c28066e1a96221e762d1209">TAILQ_REMOVE</a>(&all_clients, current, clients); <a name="l00410"></a>00410 <span class="keywordflow">break</span>; <a name="l00411"></a>00411 } <a name="l00412"></a>00412 <a name="l00413"></a>00413 ev_io_stop(EV_A_ w); <a name="l00414"></a>00414 <a name="l00415"></a>00415 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"IPC: client disconnected\n"</span>); <a name="l00416"></a>00416 <span class="keywordflow">return</span>; <a name="l00417"></a>00417 } <a name="l00418"></a>00418 <a name="l00419"></a>00419 <span class="comment">/* Terminate the message correctly */</span> <a name="l00420"></a>00420 buf[n] = <span class="charliteral">'\0'</span>; <a name="l00421"></a>00421 <a name="l00422"></a>00422 <span class="comment">/* Check if the message starts with the i3 IPC magic code */</span> <a name="l00423"></a>00423 <span class="keywordflow">if</span> (n < strlen(I3_IPC_MAGIC)) { <a name="l00424"></a>00424 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"IPC: message too short, ignoring\n"</span>); <a name="l00425"></a>00425 <span class="keywordflow">return</span>; <a name="l00426"></a>00426 } <a name="l00427"></a>00427 <a name="l00428"></a>00428 <span class="keywordflow">if</span> (strncmp(buf, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) { <a name="l00429"></a>00429 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"IPC: message does not start with the IPC magic\n"</span>); <a name="l00430"></a>00430 <span class="keywordflow">return</span>; <a name="l00431"></a>00431 } <a name="l00432"></a>00432 <a name="l00433"></a>00433 uint8_t *message = (uint8_t*)buf; <a name="l00434"></a>00434 <span class="keywordflow">while</span> (n > 0) { <a name="l00435"></a>00435 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"IPC: n = %d\n"</span>, n); <a name="l00436"></a>00436 message += strlen(I3_IPC_MAGIC); <a name="l00437"></a>00437 n -= strlen(I3_IPC_MAGIC); <a name="l00438"></a>00438 <a name="l00439"></a>00439 <span class="comment">/* The next 32 bit after the magic are the message size */</span> <a name="l00440"></a>00440 uint32_t message_size = *((uint32_t*)message); <a name="l00441"></a>00441 message += <span class="keyword">sizeof</span>(uint32_t); <a name="l00442"></a>00442 n -= <span class="keyword">sizeof</span>(uint32_t); <a name="l00443"></a>00443 <a name="l00444"></a>00444 <span class="keywordflow">if</span> (message_size > n) { <a name="l00445"></a>00445 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"IPC: Either the message size was wrong or the message was not read completely, dropping\n"</span>); <a name="l00446"></a>00446 <span class="keywordflow">return</span>; <a name="l00447"></a>00447 } <a name="l00448"></a>00448 <a name="l00449"></a>00449 <span class="comment">/* The last 32 bits of the header are the message type */</span> <a name="l00450"></a>00450 uint32_t message_type = *((uint32_t*)message); <a name="l00451"></a>00451 message += <span class="keyword">sizeof</span>(uint32_t); <a name="l00452"></a>00452 n -= <span class="keyword">sizeof</span>(uint32_t); <a name="l00453"></a>00453 <a name="l00454"></a>00454 <span class="keywordflow">if</span> (message_type >= (<span class="keyword">sizeof</span>(<a class="code" href="a00068.html#a68dd4f9828959ce3ad7e600890cae913">handlers</a>) / <span class="keyword">sizeof</span>(<a class="code" href="a00042.html#a6b1a00999ddde8264249521fa10d7b08">handler_t</a>))) <a name="l00455"></a>00455 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"Unhandled message type: %d\n"</span>, message_type); <a name="l00456"></a>00456 <span class="keywordflow">else</span> { <a name="l00457"></a>00457 <a class="code" href="a00042.html#a6b1a00999ddde8264249521fa10d7b08">handler_t</a> h = <a class="code" href="a00068.html#a68dd4f9828959ce3ad7e600890cae913">handlers</a>[message_type]; <a name="l00458"></a>00458 h(w->fd, message, n, message_size, message_type); <a name="l00459"></a>00459 } <a name="l00460"></a>00460 n -= message_size; <a name="l00461"></a>00461 message += message_size; <a name="l00462"></a>00462 } <a name="l00463"></a>00463 } <a name="l00464"></a>00464 <a name="l00465"></a>00465 <span class="comment">/*</span> <a name="l00466"></a>00466 <span class="comment"> * Handler for activity on the listening socket, meaning that a new client</span> <a name="l00467"></a>00467 <span class="comment"> * has just connected and we should accept() him. Sets up the event handler</span> <a name="l00468"></a>00468 <span class="comment"> * for activity on the new connection and inserts the file descriptor into</span> <a name="l00469"></a>00469 <span class="comment"> * the list of clients.</span> <a name="l00470"></a>00470 <span class="comment"> *</span> <a name="l00471"></a>00471 <span class="comment"> */</span> <a name="l00472"></a><a class="code" href="a00042.html#ab60981ffa93c190e40d52e05f6c8a4d8">00472</a> <span class="keywordtype">void</span> <a class="code" href="a00068.html#ab60981ffa93c190e40d52e05f6c8a4d8" title="Handler for activity on the listening socket, meaning that a new client has just...">ipc_new_client</a>(EV_P_ <span class="keyword">struct</span> ev_io *w, <span class="keywordtype">int</span> revents) { <a name="l00473"></a>00473 <span class="keyword">struct </span>sockaddr_un peer; <a name="l00474"></a>00474 socklen_t len = <span class="keyword">sizeof</span>(<span class="keyword">struct </span>sockaddr_un); <a name="l00475"></a>00475 <span class="keywordtype">int</span> client; <a name="l00476"></a>00476 <span class="keywordflow">if</span> ((client = accept(w->fd, (<span class="keyword">struct</span> sockaddr*)&peer, &len)) < 0) { <a name="l00477"></a>00477 <span class="keywordflow">if</span> (errno == EINTR) <a name="l00478"></a>00478 <span class="keywordflow">return</span>; <a name="l00479"></a>00479 <span class="keywordflow">else</span> perror(<span class="stringliteral">"accept()"</span>); <a name="l00480"></a>00480 <span class="keywordflow">return</span>; <a name="l00481"></a>00481 } <a name="l00482"></a>00482 <a name="l00483"></a>00483 set_nonblock(client); <a name="l00484"></a>00484 <a name="l00485"></a>00485 <span class="keyword">struct </span>ev_io *<span class="keyword">package </span>= scalloc(sizeof(struct ev_io)); <a name="l00486"></a>00486 ev_io_init(package, <a class="code" href="a00068.html#a81fcba6a5550d614cc8d47b46aaa00bd">ipc_receive_message</a>, client, EV_READ); <a name="l00487"></a>00487 ev_io_start(EV_A_ package); <a name="l00488"></a>00488 <a name="l00489"></a>00489 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"IPC: new client connected\n"</span>); <a name="l00490"></a>00490 <a name="l00491"></a>00491 <a class="code" href="a00017.html">ipc_client</a> *<span class="keyword">new</span> = <a class="code" href="a00077.html#ad475e1b898d9da9b2a8b92e3da51b3dd" title="Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there...">scalloc</a>(<span class="keyword">sizeof</span>(<a class="code" href="a00017.html">ipc_client</a>)); <a name="l00492"></a>00492 <span class="keyword">new</span>->fd = client; <a name="l00493"></a>00493 <a name="l00494"></a>00494 <a class="code" href="a00047.html#a512bee16b46f601078c96c776d2648c3">TAILQ_INSERT_TAIL</a>(&all_clients, <span class="keyword">new</span>, clients); <a name="l00495"></a>00495 } <a name="l00496"></a>00496 <a name="l00497"></a>00497 <span class="comment">/*</span> <a name="l00498"></a>00498 <span class="comment"> * Creates the UNIX domain socket at the given path, sets it to non-blocking</span> <a name="l00499"></a>00499 <span class="comment"> * mode, bind()s and listen()s on it.</span> <a name="l00500"></a>00500 <span class="comment"> *</span> <a name="l00501"></a>00501 <span class="comment"> */</span> <a name="l00502"></a><a class="code" href="a00042.html#a173c2426068f80edfacc45e1fca642d4">00502</a> <span class="keywordtype">int</span> <a class="code" href="a00068.html#a173c2426068f80edfacc45e1fca642d4" title="Creates the UNIX domain socket at the given path, sets it to non-blocking mode, bind()s...">ipc_create_socket</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *filename) { <a name="l00503"></a>00503 <span class="keywordtype">int</span> sockfd; <a name="l00504"></a>00504 <a name="l00505"></a>00505 <span class="keywordtype">char</span> *globbed = <a class="code" href="a00062.html#aea199116bec04e7987dad157e10b49b5" title="This function resolves ~ in pathnames.">glob_path</a>(filename); <a name="l00506"></a>00506 <a class="code" href="a00044.html#a287e239c800086178591f5e53f5bb82c">DLOG</a>(<span class="stringliteral">"Creating IPC-socket at %s\n"</span>, globbed); <a name="l00507"></a>00507 <span class="keywordtype">char</span> *copy = <a class="code" href="a00077.html#a11822d1f4b182be3fa03d2a5af536e1a" title="Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there...">sstrdup</a>(globbed); <a name="l00508"></a>00508 <span class="keyword">const</span> <span class="keywordtype">char</span> *dir = dirname(copy); <a name="l00509"></a>00509 <span class="keywordflow">if</span> (!<a class="code" href="a00062.html#a7c81151479c55671cd41ad9d1b3d3f52" title="Checks if the given path exists by calling stat().">path_exists</a>(dir)) <a name="l00510"></a>00510 <a class="code" href="a00068.html#a8b513e12d82663825d208b2363e2fecf">mkdirp</a>(dir); <a name="l00511"></a>00511 free(copy); <a name="l00512"></a>00512 <a name="l00513"></a>00513 <span class="comment">/* Unlink the unix domain socket before */</span> <a name="l00514"></a>00514 unlink(globbed); <a name="l00515"></a>00515 <a name="l00516"></a>00516 <span class="keywordflow">if</span> ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) { <a name="l00517"></a>00517 perror(<span class="stringliteral">"socket()"</span>); <a name="l00518"></a>00518 free(globbed); <a name="l00519"></a>00519 <span class="keywordflow">return</span> -1; <a name="l00520"></a>00520 } <a name="l00521"></a>00521 <a name="l00522"></a>00522 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC); <a name="l00523"></a>00523 <a name="l00524"></a>00524 <span class="keyword">struct </span>sockaddr_un addr; <a name="l00525"></a>00525 memset(&addr, 0, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> sockaddr_un)); <a name="l00526"></a>00526 addr.sun_family = AF_LOCAL; <a name="l00527"></a>00527 strcpy(addr.sun_path, globbed); <a name="l00528"></a>00528 <span class="keywordflow">if</span> (bind(sockfd, (<span class="keyword">struct</span> sockaddr*)&addr, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> sockaddr_un)) < 0) { <a name="l00529"></a>00529 perror(<span class="stringliteral">"bind()"</span>); <a name="l00530"></a>00530 free(globbed); <a name="l00531"></a>00531 <span class="keywordflow">return</span> -1; <a name="l00532"></a>00532 } <a name="l00533"></a>00533 <a name="l00534"></a>00534 free(globbed); <a name="l00535"></a>00535 set_nonblock(sockfd); <a name="l00536"></a>00536 <a name="l00537"></a>00537 <span class="keywordflow">if</span> (listen(sockfd, 5) < 0) { <a name="l00538"></a>00538 perror(<span class="stringliteral">"listen()"</span>); <a name="l00539"></a>00539 <span class="keywordflow">return</span> -1; <a name="l00540"></a>00540 } <a name="l00541"></a>00541 <a name="l00542"></a>00542 <span class="keywordflow">return</span> sockfd; <a name="l00543"></a>00543 } </pre></div></div> <hr class="footer"/><address style="text-align: right;"><small>Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address> </body> </html>