File: notionflux.c

package info (click to toggle)
notion 3%2B2015061300-2
  • links: PTS, VCS
  • area: non-free
  • in suites: stretch
  • size: 4,904 kB
  • ctags: 6,037
  • sloc: ansic: 46,788; sh: 2,005; makefile: 548; perl: 270
file content (249 lines) | stat: -rw-r--r-- 4,991 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * mod_notionflux/notionflux/notionflux.c
 *
 * Copyright (c) Tuomo Valkonen 2004-2005. 
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 */

#include <X11/Xlib.h>
#include <X11/Xatom.h>

#include <libtu/types.h>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "../notionflux.h"

static void die(const char *s)
{
    fprintf(stderr, "%s\n", s);
    exit(1);
}


static void die_e(const char *s)
{
    perror(s);
    exit(1);
}

static void mywrite(int fd, const char *buf, int n)
{
    while(n>0){
        int k;
        k=write(fd, buf, n);
        if(k<0 && (errno!=EAGAIN && errno!=EINTR))
            die_e("Writing");
        if(k>0){
            n-=k;
            buf+=k;
        }
    }
}


static int myread(int fd, char *buf, int n)
{
    int left=n;
    
    while(left>0){
        int k;
        k=read(fd, buf, left);
        if(k<0 && (errno!=EAGAIN && errno!=EINTR))
            die_e("Writing");
        if(k==0)
            break;
        if(k>0){
            left-=k;
            buf+=k;
        }
    }
    return n-left;
}


static char buf[MAX_DATA];


/*{{{ X */


static Display *dpy=NULL;


static ulong xwindow_get_property_(Window win, Atom atom, Atom type, 
                                   ulong n32expected, bool more, uchar **p,
                                   int *format)
{
    Atom real_type;
    ulong n=-1, extra=0;
    int status;
    
    do{
        status=XGetWindowProperty(dpy, win, atom, 0L, n32expected, 
                                  False, type, &real_type, format, &n,
                                  &extra, p);
        
        if(status!=Success || *p==NULL)
            return -1;
    
        if(extra==0 || !more)
            break;
        
        XFree((void*)*p);
        n32expected+=(extra+4)/4;
        more=FALSE;
    }while(1);

    if(n==0){
        XFree((void*)*p);
        *p=NULL;
        return -1;
    }
    
    return n;
}


ulong xwindow_get_property(Window win, Atom atom, Atom type, 
                           ulong n32expected, bool more, uchar **p)
{
    int format=0;
    return xwindow_get_property_(win, atom, type, n32expected, more, p, 
                                 &format);
}


char *xwindow_get_string_property(Window win, Atom a, int *nret)
{
    char *p;
    int n;
    
    n=xwindow_get_property(win, a, XA_STRING, 64L, TRUE, (uchar**)&p);
    
    if(nret!=NULL)
        *nret=n;
    
    return (n<=0 ? NULL : p);
}


void xwindow_set_string_property(Window win, Atom a, const char *value)
{
    if(value==NULL){
        XDeleteProperty(dpy, win, a);
    }else{
        XChangeProperty(dpy, win, a, XA_STRING,
                        8, PropModeReplace, (uchar*)value, strlen(value));
    }
}


static char *get_socket()
{
    Atom a;
    char *s;
    
    dpy=XOpenDisplay(NULL);
    
    if(dpy==NULL)
        die_e("Unable to open display.");
    
    a=XInternAtom(dpy, "_NOTION_MOD_NOTIONFLUX_SOCKET", True);
    
    if(a==None)
        die_e("Missing atom. Notion not running?");
    
    s=xwindow_get_string_property(DefaultRootWindow(dpy), a, NULL);
    
    XCloseDisplay(dpy);
    
    return s;
}


/*}}}*/


int main(int argc, char *argv[])
{
    int sock;
    struct sockaddr_un serv;
    const char *sockname;
    int use_stdin=1;
    char res;
    int n;
    
    if(argc>1){
        if(argc!=3 || strcmp(argv[1], "-e")!=0)
            die("Usage: notionflux [-e code]");
            
        if(strlen(argv[2])>=MAX_DATA)
            die("Too much data.");
        
        use_stdin=0;
    }
        
    sockname=get_socket();
    if(sockname==NULL)
        die("No socket.");
    
    if(strlen(sockname)>SOCK_MAX)
        die("Socket name too long.");
    
    sock=socket(AF_UNIX, SOCK_STREAM, 0);
    if(sock<0)
        die_e("Opening socket");
 
    serv.sun_family=AF_UNIX;
    strcpy(serv.sun_path, sockname);
    
    if(connect(sock, (struct sockaddr*)&serv, sizeof(struct sockaddr_un))<0)
        die_e("Connecting socket");
    
    if(!use_stdin){
        mywrite(sock, argv[2], strlen(argv[2])+1);
    }else{
        char c='\0';
        while(1){
            if(fgets(buf, MAX_DATA, stdin)==NULL)
                break;
            mywrite(sock, buf, strlen(buf));
        }
        mywrite(sock, &c, 1);
    }

    n=myread(sock, &res, 1);
    
    if(n!=1 || (res!='E' && res!='S'))
        die("Invalid response");

    while(1){
        n=myread(sock, buf, MAX_DATA);
        
        if(n==0)
            break;
        
        if(res=='S')
            mywrite(1, buf, n);
        else /* res=='E' */
            mywrite(2, buf, n);
        
        if(n<MAX_DATA)
            break;
    }

    return 0;
}