Skip to content
This repository was archived by the owner on Jun 15, 2020. It is now read-only.

Commit 78dde56

Browse files
committed
mnttool: Refresh automatically, by dentonlt
1 parent 66bcf51 commit 78dde56

File tree

3 files changed

+242
-124
lines changed

3 files changed

+242
-124
lines changed

mnttool/mnttool.cxx

Lines changed: 152 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// generated by Fast Light User Interface Designer (fluid) version 1.0110
1+
// generated by Fast Light User Interface Designer (fluid) version 1.0304
22

33
#include <libintl.h>
44
#include "mnttool.h"
@@ -11,7 +11,17 @@
1111
#include <locale.h>
1212
#include <stdint.h>
1313
#include <FL/Fl_Button.H>
14+
#include <sys/inotify.h>
1415
using namespace std;
16+
/**
17+
for watching /etc/fstab
18+
*/
19+
static int infd = -1; // inotify fd
20+
static void infd_read(void *);
21+
static void reset_watch();
22+
/**
23+
other globals
24+
*/
1525
static int size, selected, xPos, yPos;
1626
static string filemgr, winPos;
1727
static istringstream ins;
@@ -22,106 +32,146 @@ vector<string> mountLabels;
2232

2333
void getPos() {
2434
xPos = w->x();
25-
yPos = w->y();
35+
yPos = w->y();
2636
}
2737

2838
void refresh() {
2939
getMountables();
30-
if ( size == 0 )
31-
exit(1);
32-
33-
pack->clear();
34-
35-
for (int i=0; i < size; i++)
36-
{
37-
Fl_Button* btn[i];
38-
39-
btn[i] = new Fl_Button(0,0,80,25);
40-
btn[i]->label(mountList[i].c_str());
41-
btn[i]->tooltip(mountLabels[i].c_str());
42-
btn[i]->callback((Fl_Callback*)btnCallback,(void*)(uintptr_t)i);
43-
44-
if ( mountState[i] == 0)
45-
btn[i]->color((Fl_Color)2);
46-
else
47-
btn[i]->color((Fl_Color)1);
48-
49-
pack->add(btn[i]);
50-
51-
}
52-
53-
Fl_Button* btnRefresh;
54-
btnRefresh = new Fl_Button(0,0,80,25);
55-
btnRefresh->label("Refresh");
56-
btnRefresh->callback((Fl_Callback*)btnRefreshCallback);
57-
pack->add(btnRefresh);
58-
59-
selected = 0;
60-
pack->redraw();
61-
w->resize(0,0,80,(25*(size+1)));
62-
w->position(xPos,yPos);
63-
w->redraw();
40+
if ( size == 0 )
41+
exit(1);
42+
43+
pack->clear();
44+
45+
for (int i=0; i < size; i++)
46+
{
47+
Fl_Button* btn[i];
48+
49+
btn[i] = new Fl_Button(0,0,80,25);
50+
btn[i]->label(mountList[i].c_str());
51+
btn[i]->tooltip(mountLabels[i].c_str());
52+
btn[i]->callback((Fl_Callback*)btnCallback,(void*)(uintptr_t)i);
53+
54+
if ( mountState[i] == 0)
55+
btn[i]->color((Fl_Color)2);
56+
else
57+
btn[i]->color((Fl_Color)1);
58+
59+
pack->add(btn[i]);
60+
61+
}
62+
63+
Fl_Button* btnRefresh;
64+
btnRefresh = new Fl_Button(0,0,80,25);
65+
btnRefresh->label("Refresh");
66+
btnRefresh->callback((Fl_Callback*)btnRefreshCallback);
67+
pack->add(btnRefresh);
68+
69+
selected = 0;
70+
pack->redraw();
71+
w->resize(0,0,80,(25*(size+1)));
72+
w->position(xPos,yPos);
73+
w->redraw();
6474
}
6575

6676
void getMountables() {
6777
mountList.clear();
68-
mountState.clear();
69-
70-
system("mountables.sh");
71-
72-
ifstream fin("/tmp/mountables");
73-
string line;
74-
string commandHead = "grep '/mnt/";
75-
string commandTail = " ' /etc/mtab >/dev/null";
76-
77-
while ( getline(fin,line) )
78-
{
79-
int sepator = line.find("~",0);
80-
string device = line.substr(0, sepator);
81-
string label = line.substr(sepator+1);
82-
mountList.push_back(device);
83-
mountState.push_back(system((commandHead + device + commandTail).c_str()));
84-
mountLabels.push_back(label);
85-
}
86-
fin.close();
87-
unlink("/tmp/mountables");
88-
89-
size = mountList.size();
78+
mountState.clear();
79+
80+
system("mountables.sh");
81+
82+
ifstream fin("/tmp/mountables");
83+
string line;
84+
string commandHead = "grep '/mnt/";
85+
string commandTail = " ' /etc/mtab >/dev/null";
86+
87+
while ( getline(fin,line) )
88+
{
89+
int sepator = line.find("~",0);
90+
string device = line.substr(0, sepator);
91+
string label = line.substr(sepator+1);
92+
mountList.push_back(device);
93+
mountState.push_back(system((commandHead + device + commandTail).c_str()));
94+
mountLabels.push_back(label);
95+
}
96+
fin.close();
97+
unlink("/tmp/mountables");
98+
99+
size = mountList.size();
90100
}
91101

92102
void btnCallback(Fl_Widget*, void* userdata) {
93103
getPos();
94-
int results;
95-
selected = (long)userdata;
96-
if (mountState.at(selected) == 0) // mounted
97-
{
98-
results = system(("sudo umount /dev/" + mountList.at(selected)).c_str());
99-
if (results == 0)
100-
{
101-
pack->child(selected)->color((Fl_Color)1);
102-
mountState.at(selected)=256;
103-
}
104-
}
105-
else
106-
{
107-
results = system(("sudo mount /dev/" + mountList.at(selected)).c_str());
108-
if (results == 0)
109-
{
110-
pack->child(selected)->color((Fl_Color)2);
111-
mountState.at(selected)=0;
112-
if (filemgr.length() > 0)
113-
{
114-
system((filemgr + " /mnt/" + mountList.at(selected) +"&" ).c_str());
115-
exit(0);
116-
}
117-
}
118-
}
104+
int results;
105+
selected = (long)userdata;
106+
if (mountState.at(selected) == 0) // mounted
107+
{
108+
results = system(("sudo umount /dev/" + mountList.at(selected)).c_str());
109+
if (results == 0)
110+
{
111+
pack->child(selected)->color((Fl_Color)1);
112+
mountState.at(selected)=256;
113+
}
114+
}
115+
else
116+
{
117+
results = system(("sudo mount /dev/" + mountList.at(selected)).c_str());
118+
if (results == 0)
119+
{
120+
pack->child(selected)->color((Fl_Color)2);
121+
mountState.at(selected)=0;
122+
if (filemgr.length() > 0)
123+
{
124+
system((filemgr + " /mnt/" + mountList.at(selected) +"&" ).c_str());
125+
exit(0);
126+
}
127+
}
128+
}
119129
}
120130

121131
void btnRefreshCallback(Fl_Widget*, void* userdata) {
122132
getPos();
123-
system("sudo rebuildfstab");
124-
refresh();
133+
system("sudo rebuildfstab");
134+
refresh();
135+
}
136+
137+
/**
138+
called by FLTK when infd has data; schedules infd_read()
139+
*/
140+
static void infd_ready_cb(int, void *) {
141+
Fl::add_timeout(0, infd_read); // schedule infd_read
142+
}
143+
144+
/**
145+
clear inotify queue, refresh, reset_watch
146+
*/
147+
static void infd_read(void *) {
148+
// read/flush the inotify buf - we're only watching one event.
149+
const unsigned iebuflen = sizeof(inotify_event)*32;
150+
char iebuf[iebuflen];
151+
read(infd, &iebuf, iebuflen);
152+
153+
refresh();
154+
reset_watch(); // set new watch
155+
}
156+
157+
/**
158+
add inotify watch on /etc/fstab; ask FLTK to call infd_ready_cb() on changes
159+
*/
160+
static void reset_watch() {
161+
if(infd < 0) infd = inotify_init();
162+
if(infd < 0) {
163+
Fl::warning("mnttool: inotify_init() failed in reset_watch(). Auto-update disabled.");
164+
return;
165+
}
166+
167+
if(inotify_add_watch(infd, "/etc/fstab", IN_CLOSE_WRITE | IN_ONESHOT) < 0) {
168+
Fl::warning("mnttool: inotify_add_watch() failed in reset_watch(). Auto-update disabled.");
169+
close(infd);
170+
infd = -1;
171+
return;
172+
}
173+
174+
Fl::add_fd(infd, FL_READ, infd_ready_cb);
125175
}
126176

127177
Fl_Double_Window *w=(Fl_Double_Window *)0;
@@ -130,28 +180,29 @@ Fl_Pack *pack=(Fl_Pack *)0;
130180

131181
int main(int argc, char **argv) {
132182
Fl::args(argc, argv);
133-
setlocale(LC_ALL, "");
134-
bindtextdomain("tinycore","/usr/local/share/locale");
135-
textdomain("tinycore");
136-
{ w = new Fl_Double_Window(85, 745);
183+
setlocale(LC_ALL, "");
184+
bindtextdomain("tinycore","/usr/local/share/locale");
185+
textdomain("tinycore");
186+
{ w = new Fl_Double_Window(85, 745, gettext("mnttool"));
137187
{ pack = new Fl_Pack(0, 0, 80, 500);
138188
pack->end();
139189
} // Fl_Pack* pack
140190
w->end();
141191
} // Fl_Double_Window* w
142192
if (getenv("FILEMGR"))
143-
filemgr = getenv("FILEMGR");
144-
145-
xPos=80;
146-
yPos=60;
147-
148-
if (getenv("MNTTOOL")) {
149-
winPos = getenv("MNTTOOL");
150-
ins.str(winPos);
151-
ins >> xPos >> yPos;
152-
}
153-
154-
refresh();
193+
filemgr = getenv("FILEMGR");
194+
195+
xPos=80;
196+
yPos=60;
197+
198+
if (getenv("MNTTOOL")) {
199+
winPos = getenv("MNTTOOL");
200+
ins.str(winPos);
201+
ins >> xPos >> yPos;
202+
}
203+
204+
refresh();
205+
reset_watch();
155206
w->show(argc, argv);
156207
return Fl::run();
157208
}

0 commit comments

Comments
 (0)