chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / src / setup.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  * Copyright (C) 2004-2008 Kim Woelders
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies of the Software, its documentation and marketing & publicity
14  * materials, and acknowledgment shall be given in the documentation, materials
15  * and software packages that this Software was used.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "E.h"
25 #include "events.h"
26 #include "ewins.h"
27 #include "screen.h"
28 #include "session.h"
29 #include "xwin.h"
30 #include <signal.h>
31 #include <X11/keysym.h>
32
33 static void
34 HandleXIOError(void)
35 {
36    SessionExit(EEXIT_ERROR, NULL);
37 }
38
39 /*
40  * This function sets up all of our connections to X
41  */
42 void
43 SetupX(const char *dstr)
44 {
45    int                 err;
46    char                buf[128];
47    long                mask;
48
49    if (!dstr)
50       dstr = getenv("DISPLAY");
51    if (!dstr)
52       dstr = ":0";
53
54    /* Open a connection to the diplay nominated by the DISPLAY variable */
55    err = EDisplayOpen(dstr, Dpy.screen);
56    if (err)
57      {
58         Alert(_("Enlightenment cannot connect to the display nominated by\n"
59                 "your shell's DISPLAY environment variable. You may set this\n"
60                 "variable to indicate which display name Enlightenment is to\n"
61                 "connect to. It may be that you do not have an Xserver already\n"
62                 "running to serve that Display connection, or that you do not\n"
63                 "have permission to connect to that display. Please make sure\n"
64                 "all is correct before trying again. Run an Xserver by running\n"
65                 "xdm or startx first, or contact your local system\n"
66                 "administrator, or Xserver vendor, or read the X, xdm and\n"
67                 "startx manual pages before proceeding.\n"));
68         EExit(1);
69      }
70
71    if (getenv("ESYNCHRONIZE"))
72       XSynchronize(disp, True);
73
74    Dpy.screens = ScreenCount(disp);
75    Dpy.screen = DefaultScreen(disp);
76
77    if (Mode.wm.master ||
78        Mode.wm.master_screen < 0 || Mode.wm.master_screen >= Dpy.screens)
79       Mode.wm.master_screen = Dpy.screen;
80
81    /* Start up on multiple heads, if appropriate */
82    if (Dpy.screens > 1 && !Mode.wm.single && !Mode.wm.restart)
83      {
84         int                 i;
85
86         for (i = 0; i < Dpy.screens; i++)
87           {
88              pid_t               pid;
89
90              if (i == Dpy.screen)
91                 continue;
92
93              pid = fork();
94              if (pid)
95                {
96                   /* We are the master */
97                   Mode.wm.child_count++;
98                   Mode.wm.children =
99                      EREALLOC(pid_t, Mode.wm.children, Mode.wm.child_count);
100                   Mode.wm.children[Mode.wm.child_count - 1] = pid;
101                }
102              else
103                {
104                   /* We are a slave */
105                   EDisplayDisconnect();
106                   Mode.wm.master = 0;
107                   Mode.wm.pid = getpid();
108                   Dpy.screen = i;
109                   ExtInitWinSet(None);
110 #ifdef SIGSTOP
111                   kill(getpid(), SIGSTOP);
112 #endif
113                   EDisplayOpen(dstr, i);
114                   /* Terminate the loop as I am the child process... */
115                   break;
116                }
117           }
118      }
119
120    Dpy.name = Estrdup(DisplayString(disp));
121    Esetenv("DISPLAY", Dpy.name);
122
123    Dpy.pixel_black = BlackPixel(disp, Dpy.screen);
124    Dpy.pixel_white = WhitePixel(disp, Dpy.screen);
125
126    EDisplaySetErrorHandlers(HandleXIOError);
127
128    /* Root defaults */
129    RROOT = ERegisterWindow(DefaultRootWindow(disp), NULL);
130
131    if (Mode.wm.window)
132      {
133         VROOT = ECreateWindow(RROOT, 0, 0, Mode.wm.win_w, Mode.wm.win_h, 0);
134
135         /* Enable eesh and edox to pick up the virtual root */
136         Esnprintf(buf, sizeof(buf), "%#lx", WinGetXwin(VROOT));
137         Esetenv("ENL_WM_ROOT", buf);
138      }
139    else
140      {
141         /* Running E normally on the root window */
142         VROOT = RROOT;
143      }
144
145    /* Initialise event handling */
146    EventsInit();
147
148    /* select all the root window events to start managing */
149    Dpy.last_error_code = 0;
150    mask =
151       StructureNotifyMask | SubstructureNotifyMask | SubstructureRedirectMask;
152    ESelectInput(VROOT, mask);
153    ESync(0);
154    if (Dpy.last_error_code)
155      {
156         AlertX(_("Another Window Manager is already running"),
157                _("OK"), NULL, NULL,
158                _("Another Window Manager is already running.\n" "\n"
159                  "You will have to quit your current Window Manager first before\n"
160                  "you can successfully run Enlightenment.\n"));
161         EExit(1);
162      }
163
164    /* warn, if necessary about X version problems */
165    if (ProtocolVersion(disp) != 11)
166      {
167         AlertX(_("X server version error"), _("Ignore this error"), "",
168                _("Quit Enlightenment"),
169                _("WARNING:\n"
170                  "This is not an X11 Xserver. It in fact talks the X%i protocol.\n"
171                  "This may mean Enlightenment will either not function, or\n"
172                  "function incorrectly. If it is later than X11, then your\n"
173                  "server is one the author of Enlightenment neither have\n"
174                  "access to, nor have heard of.\n"), ProtocolVersion(disp));
175      }
176
177    /* damn that bloody numlock stuff - ok I'd rather XFree got fixed to not */
178    /* have it as a modifier and everyone have to write specific code to mask */
179    /* it out - but well.... */
180    /* ok under Xfree Numlock and Scollock are lock modifiers and we need */
181    /* to hunt them down to mask them out - EVIL EVIL EVIL hack but needed */
182    {
183       XModifierKeymap    *mod;
184       KeyCode             nl, sl;
185       unsigned int        numlock, scrollock;
186       int                 i;
187       int                 masks[8] = {
188          ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask,
189          Mod4Mask, Mod5Mask
190       };
191
192       numlock = scrollock = 0;
193       mod = XGetModifierMapping(disp);
194       nl = EKeysymToKeycode(XK_Num_Lock);
195       sl = EKeysymToKeycode(XK_Scroll_Lock);
196       if ((mod) && (mod->max_keypermod > 0))
197         {
198            for (i = 0; i < (8 * mod->max_keypermod); i++)
199              {
200                 if ((nl) && (mod->modifiermap[i] == nl))
201                    numlock = masks[i / mod->max_keypermod];
202                 else if ((sl) && (mod->modifiermap[i] == sl))
203                    scrollock = masks[i / mod->max_keypermod];
204              }
205         }
206       Mode.masks.mod_combos[0] = 0;
207       Mode.masks.mod_combos[1] = LockMask;
208       if (numlock)
209         {
210            Mode.masks.mod_combos[2] = numlock;
211            Mode.masks.mod_combos[5] = LockMask | numlock;
212         }
213       if (scrollock)
214         {
215            Mode.masks.mod_combos[3] = scrollock;
216            Mode.masks.mod_combos[6] = LockMask | scrollock;
217         }
218       if (numlock && scrollock)
219         {
220            Mode.masks.mod_combos[4] = numlock | scrollock;
221            Mode.masks.mod_combos[7] = LockMask | numlock | scrollock;
222         }
223
224       Mode.masks.mod_key_mask =
225          (ShiftMask | ControlMask | Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask |
226           Mod5Mask) & (~(numlock | scrollock | LockMask));
227
228       if (mod)
229          XFreeModifiermap(mod);
230    }
231
232    ScreenInit();
233    ZoomInit();
234 }