summaryrefslogtreecommitdiff
path: root/src/Remember.cc
blob: 9783c17f477055de0a45f54fc1979b228514b7bf (plain)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// Remember.cc for Fluxbox Window Manager
// Copyright (c) 2002 Xavier Brouckaert
// Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id: Remember.cc,v 1.7 2003/04/26 14:36:21 rathnor Exp $

#include "Remember.hh"
#include "StringUtil.hh"
#include "Screen.hh"
#include "Window.hh"
#include "WinClient.hh"
#include "FbMenu.hh"
#include "MenuItem.hh"
#include "App.hh"

// TODO get rid of these
#define RC_PATH "fluxbox"
#define RC_INIT_FILE "init"

#include <X11/Xlib.h>

//use GNU extensions
#ifndef	 _GNU_SOURCE
#define	 _GNU_SOURCE
#endif // _GNU_SOURCE


#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <memory>

using namespace std;

#ifndef    MAXPATHLEN
#define    MAXPATHLEN 255
#endif // MAXPATHLEN

namespace {

class RememberMenuItem : public FbTk::MenuItem {
public:
    RememberMenuItem(const char *label, Remember &remember,
                     FluxboxWindow &fbwin,
                     Remember::Attribute attrib) :
        FbTk::MenuItem(label), m_remember(remember), 
        m_win(fbwin), m_attrib(attrib) {}

    bool isSelected() const {
        return m_remember.isRemembered(m_win.winClient(), m_attrib);
    }

    bool isEnabled() const {
        if (m_attrib != Remember::REM_JUMPWORKSPACE) 
            return true;
        else 
            return (m_remember.isRemembered(m_win.winClient(), Remember::REM_WORKSPACE));
    }

    void click(int button, int time) {
        if (isSelected()) {
            m_remember.forgetAttrib(m_win.winClient(), m_attrib);
        } else {
            m_remember.rememberAttrib(m_win.winClient(), m_attrib);
        }
        m_remember.save();
        FbTk::MenuItem::click(button, time);
    }

private:
    // my remember manager
    Remember &m_remember;
    FluxboxWindow &m_win;
    Remember::Attribute m_attrib;
};

FbTk::Menu *createRememberMenu(Remember &remember, FluxboxWindow &win) {
    // each fluxboxwindow has its own windowmenu
    // so we also create a remember menu just for it...
    BScreen &screen = win.getScreen();
    FbTk::Menu *menu = new FbMenu(*screen.menuTheme(), 
                                  screen.getScreenNumber(), 
                                  *screen.getImageControl(), 
                                  *screen.layerManager().getLayer(Fluxbox::instance()->getMenuLayer()));

    // TODO: nls
    menu->insert(new RememberMenuItem("Workspace", remember, win,
                                      Remember::REM_WORKSPACE));
    menu->insert(new RememberMenuItem("Jump to workspace", remember, win,
                                      Remember::REM_JUMPWORKSPACE));
    menu->insert(new RememberMenuItem("Dimensions", remember, win,
                                      Remember::REM_DIMENSIONS));
    menu->insert(new RememberMenuItem("Position", remember, win,
                                      Remember::REM_POSITION));
    menu->insert(new RememberMenuItem("Sticky", remember, win,
                                      Remember::REM_STUCKSTATE));
    menu->insert(new RememberMenuItem("Decorations", remember, win,
                                      Remember::REM_DECOSTATE));
    menu->insert(new RememberMenuItem("Shaded", remember, win,
                                      Remember::REM_SHADEDSTATE));
    menu->insert(new RememberMenuItem("Layer", remember, win,
                                      Remember::REM_LAYER));
    //    menu->insert(new RememberMenuItem("Tab", remember, win,
    //                                     Remember::REM_TABSTATE));
    menu->insert(new RememberMenuItem("Save on close", remember, win,
                                      Remember::REM_SAVEONCLOSE));

    menu->update();
    return menu;
};

const char * getWMClass(Window w) {
    XClassHint ch;
    
    if (XGetClassHint(FbTk::App::instance()->display(), w, &ch) == 0) {
        cerr<<"Failed to read class hint!"<<endl;
        return 0;
    } else {
        string m_class_name;
        if (ch.res_name != 0) {
            //m_instance_name = const_cast<char *>(ch.res_name);
            XFree(ch.res_name);
        } else {}
        //m_instance_name = "";
        
        if (ch.res_class != 0) {
            m_class_name = const_cast<char *>(ch.res_class);
            XFree(ch.res_class);
        } else {
            m_class_name = "";
        }
        return m_class_name.c_str();
    }
}

};

Application::Application() {
    workspace_remember =
	dimensions_remember =
	position_remember =
	stuckstate_remember =
	decostate_remember =
	shadedstate_remember =
	tabstate_remember =
	jumpworkspace_remember =
        layer_remember =
	save_on_close_remember = false;
}

Remember::Remember() {
    load();
}

Application* Remember::add(const char* app_name) {
    if (!app_name)
        return 0;
    Application* a = new Application();
    apps[app_name] = a;
    return a;
}

Application* Remember::find(WinClient &winclient) {
    return find(getWMClass(winclient.window()));
}

Application* Remember::add(WinClient &winclient) {
    return add(getWMClass(winclient.window()));
}


Application* Remember::find(const char* app_name) {
    if (!app_name)
        return 0;
    Apps::iterator i = apps.find(app_name);
    if (i!=apps.end())
        return i->second;
    else
        return 0;
}

int Remember::parseApp(ifstream &file, Application *a) {
    string line;
    int row = 0;
    while (! file.eof()) {
        if (getline(file, line)) {
            row++;
            if (line[0] != '#') { //the line is commented
                int parse_pos = 0, err = 0;
                string str_key, str_label;
                err = StringUtil::getStringBetween(str_key, line.c_str(), '[', ']');
                if (err > 0 ) {
                    parse_pos += err;
                    err = StringUtil::getStringBetween(str_label, 
                                                       line.c_str() + parse_pos, 
                                                       '{', '}');
                    if (err>0) {
                        parse_pos += err;
                    }
                } else
                    continue; //read next line
                if (!str_key.size())
                    continue; //read next line
                if (str_key == "Workspace") {
                    unsigned int w;
                    istringstream iss(str_label.c_str());
                    iss >> w;
                    a->rememberWorkspace(w);
                } else if (str_key == "Layer") {
                    unsigned int l;
                    istringstream iss(str_label.c_str());
                    iss >> l;
                    a->rememberLayer(l);
                } else if (str_key == "Dimensions") {
                    unsigned int h,w;
                    istringstream iss(str_label.c_str());
                    iss >> w >> h;
                    a->rememberDimensions(w,h);
                } else if (str_key == "Position") {
                    unsigned int x,y;
                    istringstream iss(str_label);
                    iss >> x >> y;
                    a->rememberPosition(x,y);
                } else if (str_key == "Shaded") {
                    a->rememberShadedstate((str_label=="yes"));
                } else if (str_key == "Tab") {
                    a->rememberTabstate((str_label=="yes"));
                } else if (str_key == "Deco") {
                    if (str_label == "NONE") {
                        a->rememberDecostate((unsigned int) 0);
                    } else if (str_label == "NORMAL") {
                        a->rememberDecostate((unsigned int) 0xfffffff);
                    } else if (str_label == "TINY") {
                        a->rememberDecostate((unsigned int)
                                             FluxboxWindow::DECORM_TITLEBAR 
                                             | FluxboxWindow::DECORM_ICONIFY
                                             | FluxboxWindow::DECORM_MENU
                                             );
                    } else if (str_label == "TOOL") {
                        a->rememberDecostate((unsigned int)
                                             FluxboxWindow::DECORM_TITLEBAR
                                             | FluxboxWindow::DECORM_MENU
                                             );
                    } else {
                        unsigned int mask;
                        const char * str = str_label.c_str();
                        // it'll have at least one char and \0, so this is safe
                        istringstream iss(str);
                        // check for hex
                        if (str[0] == '0' && str[1] == 'x') {
                            iss.seekg(2);
                            iss >> hex;
                        }
                        iss >> mask ;
                        a->rememberDecostate(mask);
                    }
                } else if (str_key == "Sticky") {
                    a->rememberStuckstate((str_label=="yes"));
                } else if (str_key == "Jump") {
                    a->rememberJumpworkspace((str_label=="yes"));
                } else if (str_key == "Close") {
                    a->rememberSaveOnClose((str_label=="yes"));
                } else if (str_key == "end") {
                    return row;
                } else {
                    cerr << "Unsupported apps key = " << str_key << endl;
                }
            }
        }
    }
    return row;
}

void Remember::load() {

    string apps_string = getenv("HOME")+string("/.")+RC_PATH+string("/")+"apps";
#ifdef DEBUG
    cerr<<__FILE__<<"("<<__FUNCTION__<<"): Loading apps file ["<<apps_string<<"]"<<endl;
#endif // DEBUG
    ifstream apps_file(apps_string.c_str());

    if (!apps_file.fail()) {
        if (!apps_file.eof()) {
            string line;
            int row = 0;
            while (getline(apps_file, line) && ! apps_file.eof()) {
                row++;
                if (line[0] == '#')
                    continue;
                string key;
                int pos=0;
                int err = StringUtil::getStringBetween(key, line.c_str(), '[', ']');

                if (err >0 && key == "app") {
                    pos += err;
                    string label;
                    err = StringUtil::getStringBetween(label, line.c_str()+pos, '(', ')');
                    if (err>0) {
                        Application *a;
                        Apps::iterator i = apps.find(label);
                        if (i==apps.end()) {
                            a = new Application();
                            apps[label] = a;
                        } else
                            a = i->second;
                        row += parseApp(apps_file, a);
                    } else
                        cerr<<"Error1 in apps file. Line("<<row<<")"<<endl;
                } else
                    cerr<<"Error2 in apps file. Line("<<row<<")"<<endl;

            }
        } else {
#ifdef DEBUG
            cerr<<__FILE__<<"("<<__FUNCTION__<< ") Empty apps file" << endl;
#endif
        }
    } else {
        cerr << "apps file failure" << endl;
    }
}

void Remember::save() {
#ifdef DEBUG
    cerr<<__FILE__<<"("<<__FUNCTION__<<"): Saving apps file..."<<endl;
#endif // DEBUG
    string apps_string = getenv("HOME")+string("/.")+RC_PATH+string("/")+"apps";
    ofstream apps_file(apps_string.c_str());
    Apps::iterator it = apps.begin();
    Apps::iterator it_end = apps.end();
    for (; it != it_end; ++it) {
        apps_file << "[app] (" <<  it->first << ")" << endl;
        Application *a = it->second;
        if (a->workspace_remember) {
            apps_file << "  [Workspace]\t{" << a->workspace << "}" << endl;
        }
        if (a->dimensions_remember) {
            apps_file << "  [Dimensions]\t{" << a->w << " " << a->h << "}" << endl;
        }
        if (a->position_remember) {
            apps_file << "  [Position]\t{" << a->x << " " << a->y << "}" << endl;
        }
        if (a->shadedstate_remember) {
            apps_file << "  [Shaded]\t{" << ((a->shadedstate)?"yes":"no") << "}" << endl;
        }
        if (a->tabstate_remember) {
            apps_file << "  [Tab]\t\t{" << ((a->tabstate)?"yes":"no") << "}" << endl;
        }
        if (a->decostate_remember) {
            switch (a->decostate) {
            case (0) :
                apps_file << "  [Deco]\t{NONE}" << endl; 
                break;
            case (0xffffffff):
            case (FluxboxWindow::DECORM_LAST - 1):
                apps_file << "  [Deco]\t{NORMAL}" << endl;
                break;
            case (FluxboxWindow::DECORM_TITLEBAR 
                  | FluxboxWindow::DECORM_ICONIFY
                  | FluxboxWindow::DECORM_MENU):
                apps_file << "  [Deco]\t{TOOL}" << endl;
                break;
            case (FluxboxWindow::DECORM_TITLEBAR 
                  | FluxboxWindow::DECORM_MENU):
                apps_file << "  [Deco]\t{TINY}" << endl;
                break;
            default:
                apps_file << "  [Deco]\t{0x"<<hex<<a->decostate<<dec<<"}"<<endl;
                break;
            }
        }
        if (a->stuckstate_remember) {
            apps_file << "  [Sticky]\t{" << ((a->stuckstate)?"yes":"no") << "}" << endl;
        }
        if (a->jumpworkspace_remember) {
            apps_file << "  [Jump]\t{" << ((a->jumpworkspace)?"yes":"no") << "}" << endl;
        }
        if (a->layer_remember) {
            apps_file << "  [Layer]\t{" << a->layer << "}" << endl;
        }
        if (a->save_on_close_remember) {
            apps_file << "  [Close]\t{" << ((a->save_on_close)?"yes":"no") << "}" << endl;
        }
        apps_file << "[end]" << endl;
    }
}

bool Remember::isRemembered(WinClient &winclient, Attribute attrib) {
    Application *app = find(winclient);
    if (!app) return false;
    switch (attrib) {
    case REM_WORKSPACE:
        return app->workspace_remember;
        break;
    case REM_DIMENSIONS:
        return app->dimensions_remember;
        break;
    case REM_POSITION:
        return app->position_remember;
        break;
    case REM_STUCKSTATE:
        return app->stuckstate_remember;
        break;
    case REM_DECOSTATE:
        return app->decostate_remember;
        break;
    case REM_SHADEDSTATE:
        return app->shadedstate_remember;
        break;
        //    case REM_TABSTATE:
        //        return app->tabstate_remember;
        //        break;
    case REM_JUMPWORKSPACE:
        return app->jumpworkspace_remember;
        break;
    case REM_LAYER:
        return app->layer_remember;
        break;
    case REM_SAVEONCLOSE:
        return app->save_on_close_remember;
        break;
    case REM_LASTATTRIB:
    default:
        return false; // should never get here
    }
}

void Remember::rememberAttrib(WinClient &winclient, Attribute attrib) {
    FluxboxWindow *win = winclient.fbwindow();
    if (!win) return;
    Application *app = find(winclient);
    if (!app) {
        app = add(winclient);
        if (!app) return;
    }
    switch (attrib) {
    case REM_WORKSPACE:
        app->rememberWorkspace(win->getWorkspaceNumber());
        break;
    case REM_DIMENSIONS:
        app->rememberDimensions(win->getWidth(), win->getHeight());
        break;
    case REM_POSITION:
        app->rememberPosition(win->getXFrame(), win->getYFrame());
        break;
    case REM_STUCKSTATE:
        app->rememberShadedstate(win->isShaded());
        break;
    case REM_DECOSTATE:
        app->rememberDecostate(win->getDecorationMask());
        break;
    case REM_SHADEDSTATE:
        app->rememberStuckstate(win->isStuck());
        break;
        //    case REM_TABSTATE:
        //        break;
    case REM_JUMPWORKSPACE:
        app->rememberJumpworkspace(true);
        break;
    case REM_LAYER:
        app->rememberLayer(win->getLayerNum());
        break;
    case REM_SAVEONCLOSE:
        app->rememberSaveOnClose(true);
        break;
    case REM_LASTATTRIB:
    default:
        // nothing
        break;
    }
}

void Remember::forgetAttrib(WinClient &winclient, Attribute attrib) {
    FluxboxWindow *win = winclient.fbwindow();
    if (!win) return;
    Application *app = find(winclient);
    if (!app) {
        app = add(winclient);
        if (!app) return;
    }
    switch (attrib) {
    case REM_WORKSPACE:
        app->forgetWorkspace();
        break;
    case REM_DIMENSIONS:
        app->forgetDimensions();
        break;
    case REM_POSITION:
        app->forgetPosition();
        break;
    case REM_STUCKSTATE:
        app->forgetStuckstate();
        break;
    case REM_DECOSTATE:
        app->forgetDecostate();
        break;
    case REM_SHADEDSTATE:
        app->forgetShadedstate();
        break;
//    case REM_TABSTATE:
//        break;
    case REM_JUMPWORKSPACE:
        app->forgetJumpworkspace();
        break;
    case REM_LAYER:
        app->forgetLayer();
        break;
    case REM_SAVEONCLOSE:
        app->forgetSaveOnClose();
        break;
    case REM_LASTATTRIB:
    default:
        // nothing
        break;
    }
}

void Remember::setupWindow(FluxboxWindow &win) {
    WinClient &winclient = win.winClient();

    // add the menu, this -2 is somewhat dodgy... :-/
    // All windows get the remember menu.
    // TODO: nls
    win.getWindowmenu().insert("Remember...", 
                               createRememberMenu(*this, win), 
                               win.getWindowmenu().numberOfItems()-2);
    win.getWindowmenu().update();

    // we don't touch the window if it is a transient
    // of something else
    if (winclient.transientFor()) 
        return;

    Application *app = find(winclient);
    if (!app) return; // nothing to do

    BScreen &screen = win.getScreen();

    if (app->workspace_remember) {
        // TODO: fix placement to initialise properly
        screen.reassociateWindow(&win, app->workspace, true); 
        if (app->jumpworkspace_remember)
            screen.changeWorkspaceID(app->workspace);
    }

    if (app->dimensions_remember)
        win.resize(app->w, app->h);
    
    if (app->position_remember)
        win.move(app->x, app->y);

    if (app->shadedstate_remember)
        // if inconsistent...
        if (win.isShaded() && !app->shadedstate ||
            !win.isShaded() && app->shadedstate)
            win.shade(); // toggles

    // external tabs aren't available atm...
    //if (app->tabstate_remember) ...

    if (app->decostate_remember)
        win.setDecorationMask(app->decostate);

    if (app->stuckstate_remember)
        // if inconsistent...
        if (win.isStuck() && !app->stuckstate ||
            !win.isStuck() && app->stuckstate)
            win.stick(); // toggles

    if (app->layer_remember)
        win.moveToLayer(app->layer);

}

void Remember::updateWindowClose(FluxboxWindow &win) {
    // This doesn't work at present since fluxbox.cc is missing the windowclose stuff.
    // I don't trust it (particularly winClient()) while this is the case
    return;

    WinClient &winclient = win.winClient();
    Application *app = find(winclient);

    if (!app || !(app->save_on_close_remember && app->save_on_close))
        return;

    for (int attrib = 0; attrib <= REM_LASTATTRIB; attrib++) {
        if (isRemembered(winclient, (Attribute) attrib)) {
            rememberAttrib(winclient, (Attribute) attrib);
        }
    }
/*
    if (app->workspace_remember) 
        app->rememberWorkspace(win.getWorkspaceNumber());
    if (app->dimensions_remember)
        app->rememberDimensions(win.getWidth(), win.getHeight());
    if (app->position_remember)
        app->rememberPosition(win.getXFrame(), win.getYFrame());
    if (app->shadedstate_remember)
        app->rememberShadedstate(win.isShaded());
    // external tabs off atm
    //if (app->tabstate_remember) ...
    if (app->decostate_remember)
        app->rememberDecostate(win.getDecorationMask());
    if (app->stuckstate_remember)
        app->rememberStuckstate(win.isStuck());
    if (app->jumpworkspace_remember)
        app->rememberJumpworkspace(true);
*/
    save();
}