screen-thirds based shortcuts, xinerama aware strut overide, auto unmaximize
[kwingrid.git] / main.cc
1 #include <iostream>
2 #include <kuniqueapplication.h>
3 #include <klocale.h>
4 #include <kcmdlineargs.h>
5 #include <kaboutdata.h>
6 #include <kdebug.h>
7 #include <kglobalaccel.h>
8 #include <kactioncollection.h>
9
10 #include "kwingrid.h"
11
12 /* Die optimalen werte für hgap und vgap berechnen sich zu:
13  *
14  * gap = size%split + n*split
15  *
16  * für n = 0,1,2,... und size die Bildschirmgröße in der jeweiligen Richtung
17  */
18
19 int main(int argc, char **argv)
20 {
21     KAboutData * aboutdata = new KAboutData("kwingrid",
22                                             "KWinGrid",
23                                             ki18n("Window Grid"),
24                                             "0.1",
25                                             KLocalizedString(),
26                                             KAboutData::License_GPL,
27                                             ki18n("(C) 1999,2000,2002,2004 Stefan Bund"));
28     aboutdata->addAuthor(ki18n("Stefan Bund"),ki18n("Developer"),"stefab@j32.de",
29                          "http://www.j32.de");
30
31     KCmdLineOptions winGridOpts;
32     winGridOpts.add("split <width>", ki18n("split"), 0);
33     winGridOpts.add("ignorestruts <screen>", ki18n("ignorestruts. -1 = all screens"), "");
34     winGridOpts.add("reserve-north <n>", ki18n("reserve north"), "");
35     winGridOpts.add("reserve-south <n>", ki18n("reserve south"), "");
36     winGridOpts.add("reserve-west <n>", ki18n("reserve west"), "");
37     winGridOpts.add("reserve-east <n>", ki18n("reserve east"), "");
38     winGridOpts.add("southstrut <n>", ki18n("set south strut size"), "");
39     winGridOpts.add("strutscreen <n>", ki18n("set screen to apply manual strut to. -1 = all screens"), "");
40     winGridOpts.add("+hgap", ki18n("hgap"), 0);
41     winGridOpts.add("+vgap", ki18n("vgap"), 0);
42     winGridOpts.add("+hsplit", ki18n("hsplit"), 0);
43     winGridOpts.add("+vsplit", ki18n("vsplit"), 0);
44
45     KCmdLineArgs::init(argc, argv, aboutdata);
46     KCmdLineArgs::addCmdLineOptions(winGridOpts);
47     KUniqueApplication::addCmdLineOptions();
48
49     if (! KUniqueApplication::start()) {
50         kdError() << "KWinGrid is already running!" << endl;
51         return 0;
52     }
53
54     KApplication * app = new KUniqueApplication;
55     KCmdLineArgs * args = KCmdLineArgs::parsedArgs();
56
57     if (args->count()!=4) {
58         kdError() << "Invalid arguments. Try --help\n";
59         return 0;
60     }
61
62     int split = 0;
63     int ignorestruts = -2;
64     if (args->isSet("split"))
65         split = args->getOption("split").toInt();
66
67     if (args->isSet("ignorestruts"))
68         ignorestruts = args->getOption("ignorestruts").toInt();
69
70     if (args->count() != 4) {
71         std::cerr << "invalid number of arguments" << std::endl;
72         return 1;
73     }
74
75     int southstrut (0);
76     int strutscreen (-2);
77     if (args->isSet("southstrut"))
78         southstrut = args->getOption("southstrut").toInt();
79     if (args->isSet("strutscreen"))
80         strutscreen = args->getOption("strutscreen").toInt();
81
82     int rn = 0;
83     int rs = 0;
84     int re = 0;
85     int rw = 0;
86
87     if (args->isSet("reserve-north"))
88         rn = args->getOption("reserve-north").toInt();
89     if (args->isSet("reserve-south"))
90         rs = args->getOption("reserve-south").toInt();
91     if (args->isSet("reserve-west"))
92         rw = args->getOption("reserve-west").toInt();
93     if (args->isSet("reserve-east"))
94         re = args->getOption("reserve-east").toInt();
95
96     int hgap = args->arg(0).toInt();
97     int vgap = args->arg(1).toInt();
98     int hsplit = args->arg(2).toInt();
99     int vsplit = args->arg(3).toInt();
100
101     args->clear();
102
103     KWinGrid * winGrid = new KWinGrid(hgap,vgap,hsplit,vsplit,split,ignorestruts,
104                                       rn, rs, rw, re, southstrut, strutscreen);
105
106     KActionCollection * actions = new KActionCollection(winGrid);
107
108 #define ACTION(pos, key, slot)                                          \
109     KAction * slot = new KAction(winGrid);                              \
110     actions->addAction("Move " pos, slot);                              \
111     slot->setHelpText("Move active window" pos);                        \
112     slot->setGlobalShortcut(KShortcut(                                  \
113         Qt::ALT+Qt::SHIFT+Qt::Key_ ## key, Qt::META+Qt::SHIFT+Qt::Key_ ## key)); \
114     QObject::connect(slot, SIGNAL(triggered(bool)), winGrid, SLOT(slot()))
115
116     ACTION("top-left", I, move_TL);
117     ACTION("top-right", O, move_TR);
118     ACTION("bottom-left", K, move_BL);
119     ACTION("bottom-right", L, move_BR);
120
121     ACTION("top-left (3x2 grid)", T, move_00);
122     ACTION("top-middle (3x2 grid)", Y, move_10);
123     ACTION("top-right (3x2 grid)", U, move_20);
124     ACTION("bottom-left (3x2 grid)", G, move_01);
125     ACTION("bottom-middle (3x2 grid)", H, move_11);
126     ACTION("bottom-right (3x2 grid)", J, move_21);
127
128 #undef ACTION
129 #define ACTION(size, key, slot)                                         \
130     KAction * slot = new KAction(winGrid);                              \
131     actions->addAction("Resize " size, slot);                           \
132     slot->setHelpText("Resize " size);                                  \
133     slot->setGlobalShortcut(KShortcut(                                  \
134         Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_ ## key, Qt::META+Qt::CTRL+Qt::SHIFT+Qt::Key_ ## key)); \
135     QObject::connect(slot, SIGNAL(triggered(bool)), winGrid, SLOT(slot()));
136
137     ACTION("quarter", I, resize_Q);
138
139     ACTION("horizontal", O, resize_H);
140     ACTION("vertical", K, resize_V);
141     ACTION("full", L, resize_F);
142
143     ACTION("top-left (3x2 grid)", T, resize_00);
144     ACTION("top-middle (3x2 grid)", Y, resize_10);
145     ACTION("top-right (3x2 grid)", U, resize_20);
146     ACTION("bottom-left (3x2 grid)", G, resize_01);
147     ACTION("bottom-middle (3x2 grid)", H, resize_11);
148     ACTION("bottom-right (3x2 grid)", J, resize_21);
149
150 #undef ACTION
151 #define ACTION(dir, key, slot)                                          \
152     KAction * slot = new KAction(winGrid);                              \
153     actions->addAction("Move " dir, slot);                              \
154     slot->setHelpText("Move " dir);                                     \
155     slot->setGlobalShortcut(KShortcut(                                  \
156         Qt::ALT+Qt::SHIFT+Qt::Key_ ## key, Qt::META+Qt::SHIFT+Qt::Key_ ## key)); \
157     QObject::connect(slot, SIGNAL(triggered(bool)), winGrid, SLOT(slot()));
158
159     ACTION("left", Left, move_L);
160     ACTION("right", Right, move_R);
161     ACTION("up", Up, move_U);
162     ACTION("down", Down, move_D);
163
164 #undef ACTION
165 #define ACTION(resize, key, slot)                                       \
166     KAction * slot = new KAction(winGrid);                              \
167     actions->addAction(resize " size", slot);                           \
168     slot->setHelpText(resize " size");                                  \
169     slot->setGlobalShortcut(KShortcut(                                  \
170         Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_ ## key,Qt::META+Qt::CTRL+Qt::SHIFT+Qt::Key_ ## key)); \
171     QObject::connect(slot, SIGNAL(triggered(bool)), winGrid, SLOT(slot()));
172
173     ACTION("Increase horizontal", Right, resize_IH);
174     ACTION("Increase vertical", Down, resize_IV);
175     ACTION("Decrease horizontal", Left, resize_DH);
176     ACTION("Decrease vertical", Up, resize_DV);
177
178 #undef ACTION
179
180     int ret = app->exec();
181
182     delete app;
183     return ret;
184 }