]>
Dogcows Code - chaz/openbox/blob - fifo_menu.c
e50810789898e5c2a92d6c2d8b51dc32f3a1ac9a
8 #include "kernel/menu.h"
9 #include "kernel/event.h"
11 static char *PLUGIN_NAME
= "fifo_menu";
13 typedef struct Fifo_Menu_Data
{
15 char *buf
; /* buffer to hold partially read menu */
16 unsigned long buflen
; /* how many bytes are in the buffer */
17 int fd
; /* file descriptor to read menu from */
18 event_fd_handler
*handler
;
21 #define FIFO_MENU(m) ((Menu *)m)
22 #define FIFO_MENU_DATA(m) ((Fifo_Menu_Data *)((Menu *)m)->plugin_data)
25 void fifo_menu_clean_up(Menu
*m
) {
26 if (FIFO_MENU_DATA(m
)->buf
!= NULL
) {
27 g_free(FIFO_MENU_DATA(m
)->buf
);
28 FIFO_MENU_DATA(m
)->buf
= NULL
;
29 FIFO_MENU_DATA(m
)->buflen
= 0;
32 if (FIFO_MENU_DATA(m
)->fd
!= -1) {
33 close(FIFO_MENU_DATA(m
)->fd
);
34 FIFO_MENU_DATA(m
)->fd
= -1;
38 void plugin_setup_config() { }
41 void plugin_shutdown() { }
43 void fifo_menu_handler(int fd
, Menu
*menu
) {
45 unsigned long num_read
;
47 /* because gdb is dumb */
48 Fifo_Menu_Data
*d
= FIFO_MENU_DATA(menu
);
51 /* if the menu is shown this will go into busy loop :(
54 unsigned long num_realloc
;
55 /* if we have less than a quarter BUFSIZ left, allocate more */
56 num_realloc
= (BUFSIZ
- (FIFO_MENU_DATA(menu
)->buflen
% BUFSIZ
) <
60 tmpbuf
= g_try_realloc(FIFO_MENU_DATA(menu
)->buf
,
61 FIFO_MENU_DATA(menu
)->buflen
+ num_realloc
);
64 g_warning("Unable to allocate memory for read()");
68 FIFO_MENU_DATA(menu
)->buf
= tmpbuf
;
71 FIFO_MENU_DATA(menu
)->buf
+ FIFO_MENU_DATA(menu
)->buflen
,
74 if (num_read
== 0) { /* eof */
75 unsigned long count
= 0;
83 (found
= strchr(&FIFO_MENU_DATA(menu
)->buf
[count
], '\n'))) {
84 FIFO_MENU_DATA(menu
)->buf
85 [found
- FIFO_MENU_DATA(menu
)->buf
] = '\0';
87 menu_entry_new_separator
88 (&FIFO_MENU_DATA(menu
)->buf
[count
]));
89 count
= found
- FIFO_MENU_DATA(menu
)->buf
+ 1;
92 FIFO_MENU_DATA(menu
)->buf
[FIFO_MENU_DATA(menu
)->buflen
] = '\0';
93 fifo_menu_clean_up(menu
);
95 event_remove_fd(FIFO_MENU_DATA(menu
)->handler
->fd
);
97 if ((FIFO_MENU_DATA(menu
)->fd
=
98 open(FIFO_MENU_DATA(menu
)->fifo
, O_NONBLOCK
| O_RDONLY
)) == -1) {
99 g_warning("Can't reopen FIFO");
100 fifo_menu_clean_up(menu
);
104 FIFO_MENU_DATA(menu
)->handler
->fd
= FIFO_MENU_DATA(menu
)->fd
;
106 event_add_fd_handler(FIFO_MENU_DATA(menu
)->handler
);
107 } else if (num_read
> 0) {
108 FIFO_MENU_DATA(menu
)->buflen
+= num_read
;
109 FIFO_MENU_DATA(menu
)->buf
[FIFO_MENU_DATA(menu
)->buflen
] = '\0';
114 void plugin_destroy (Menu
*m
)
116 fifo_menu_clean_up(m
);
117 if (FIFO_MENU_DATA(m
)->handler
!= NULL
) {
118 g_free(FIFO_MENU_DATA(m
)->handler
);
119 FIFO_MENU_DATA(m
)->handler
= NULL
;
122 if (FIFO_MENU_DATA(m
)->fifo
!= NULL
) {
123 g_free(FIFO_MENU_DATA(m
)->fifo
);
124 FIFO_MENU_DATA(m
)->fifo
= NULL
;
127 g_free(m
->plugin_data
);
132 void *plugin_create() /* TODO: need config */
138 Fifo_Menu_Data
*d
= g_new(Fifo_Menu_Data
, 1);
139 Menu
*m
= menu_new("", PLUGIN_NAME
, NULL
);
146 m
->plugin
= PLUGIN_NAME
;
150 m
->plugin_data
= (void *)d
;
152 dir
= g_build_filename(g_get_home_dir(), ".openbox", PLUGIN_NAME
, NULL
);
154 if (mkdir(dir
, S_IRWXU
| S_IRWXG
| S_IRWXO
) == -1 && errno
!= EEXIST
) {
155 /* technically, if ~/.openbox/fifo_menu exists and isn't a directory
156 this will fail, but we don't care because mkfifo will fail and warn
158 g_warning("Can't create %s: %s", dir
, strerror(errno
));
164 fifo
= g_build_filename(g_get_home_dir(), ".openbox", PLUGIN_NAME
,
166 if (mkfifo(fifo
, S_IRUSR
| S_IWUSR
|
167 S_IRGRP
| S_IWGRP
| /* let umask do its thing */
168 S_IROTH
| S_IWOTH
) == -1 && errno
!= EEXIST
) {
169 g_warning("Can't create FIFO %s: %s", fifo
, strerror(errno
));
176 /* open in non-blocking mode so we don't wait for a process to open FIFO
178 if ((d
->fd
= open(fifo
, O_NONBLOCK
| O_RDONLY
)) == -1) {
179 g_warning("Can't open FIFO %s: %s", fifo
, strerror(errno
));
188 h
= g_new(event_fd_handler
, 1);
191 g_warning("Out of memory");
201 h
->handler
= fifo_menu_handler
;
204 event_add_fd_handler(h
);
This page took 0.043676 seconds and 3 git commands to generate.