First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / utils / xorgcfg / interface.c
blob9b22dc7cb2d39c5e012fc99bef9dfa02e1f01521
1 /*
2 * Copyright (c) 2000 by Conectiva S.A. (http://www.conectiva.com)
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * CONECTIVA LINUX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
22 * Except as contained in this notice, the name of Conectiva Linux shall
23 * not be used in advertising or otherwise to promote the sale, use or other
24 * dealings in this Software without prior written authorization from
25 * Conectiva Linux.
27 * Author: Paulo César Pereira de Andrade <pcpa@conectiva.com.br>
31 #include <X11/IntrinsicP.h>
32 #include <X11/StringDefs.h>
33 #include <X11/Composite.h>
34 #include <X11/Shell.h>
35 #include <X11/Xaw/AsciiText.h>
36 #include <X11/Xaw/Simple.h>
37 #include <X11/Xaw/Paned.h>
38 #include <X11/Xaw/Form.h>
39 #include <X11/Xaw/Command.h>
40 #include <X11/Xaw/MenuButton.h>
41 #include <X11/Xaw/SmeBSB.h>
42 #include <X11/Xaw/SmeLine.h>
43 #include <X11/Xaw/SimpleMenP.h>
44 #include <X11/Xaw/Dialog.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <time.h>
49 #include "xf86config.h"
50 #include "mouse-cfg.h"
51 #include "keyboard-cfg.h"
52 #include "card-cfg.h"
53 #include "monitor-cfg.h"
54 #include "screen-cfg.h"
55 #include "screen.h"
56 #include "cards.h"
57 #include "options.h"
58 #include "vidmode.h"
59 #include "help.h"
60 #include "stubs.h"
62 #define randomize() srand((unsigned)time((time_t*)NULL))
63 #ifdef PROJECT_ROOT
64 #define DefaultXFree86Dir PROJECT_ROOT
65 #else
66 #define DefaultXFree86Dir "/usr"
67 #endif
69 #define IS_KBDDRIV(S) ((strcasecmp((S),"kbd") == 0))
72 * Prototypes
74 void DrawCables(void);
75 static void DrawCable(Display*, Window, int, int, int, int);
76 static void ComputerEventHandler(Widget, XtPointer, XEvent*, Boolean*);
77 void SelectDeviceAction(Widget, XEvent*, String*, Cardinal*);
78 void MoveDeviceAction(Widget, XEvent*, String*, Cardinal*);
79 void UnselectDeviceAction(Widget, XEvent*, String*, Cardinal*);
80 void RenameLayoutAction(Widget, XEvent*, String*, Cardinal*);
81 void DevicePopupMenu(Widget, XEvent*, String*, Cardinal*);
82 void DevicePopdownMenu(Widget, XEvent*, String*, Cardinal*);
83 void AddDeviceCallback(Widget, XtPointer, XtPointer);
84 void QuitCallback(Widget, XtPointer, XtPointer);
85 void SmeConfigureDeviceCallback(Widget, XtPointer, XtPointer);
86 void ConfigureDeviceCallback(Widget, XtPointer, XtPointer);
87 void EnableDeviceCallback(Widget, XtPointer, XtPointer);
88 void DisableDeviceCallback(Widget, XtPointer, XtPointer);
89 void RemoveDeviceCallback(Widget, XtPointer, XtPointer);
90 void InitializeDevices(void);
91 void SetConfigModeCallback(Widget, XtPointer, XtPointer);
92 void SelectLayoutCallback(Widget, XtPointer, XtPointer);
93 void DefaultLayoutCallback(Widget, XtPointer, XtPointer);
94 void RemoveLayoutCallback(Widget, XtPointer, XtPointer);
95 void OptionsCallback(Widget, XtPointer, XtPointer);
96 xf86cfgDevice *AddDevice(int, XtPointer, int, int);
97 static Bool AskConfig(void);
98 void WriteConfigAction(Widget, XEvent*, String*, Cardinal*);
99 static void ScreenSetup(Bool);
100 void QuitAction(Widget, XEvent*, String*, Cardinal*);
101 void PopdownErrorCallback(Widget, XtPointer, XtPointer);
102 static void ErrorCancelAction(Widget, XEvent*, String*, Cardinal*);
103 static void QuitCancelAction(Widget, XEvent*, String*, Cardinal*);
104 static void HelpCallback(Widget, XtPointer, XtPointer);
105 void UpdateMenuDeviceList(int);
107 extern void AccessXConfigureStart(void);
108 extern void AccessXConfigureEnd(void);
109 extern void CloseAccessXAction(Widget, XEvent*, String*, Cardinal*);
111 #ifdef HAS_NCURSES
112 extern void TextMode(void);
113 #endif
115 static void Usage(void);
118 * Initialization
120 Widget toplevel, work, config, layout, layoutsme, layoutp, topMenu;
121 XtAppContext appcon;
123 Pixmap menuPixmap;
125 char *XF86Config_path = NULL;
126 char *XF86Module_path = NULL;
127 char *XFree86_path = NULL;
128 char *XF86Font_path = NULL;
129 char *XF86RGB_path = NULL;
130 char *XkbConfig_path = NULL;
131 char *XFree86Dir;
132 static char XF86Config_path_static[1024];
133 static char XkbConfig_path_static[1024];
134 Bool xf86config_set = False;
136 int textmode = False;
137 #ifdef USE_MODULES
138 int nomodules = False;
139 #endif
140 int noverify = False;
142 xf86cfgComputer computer;
143 xf86cfgDevice cpu_device;
144 Cursor no_cursor;
145 static Widget device, layoutm, popup, commands;
146 static int xpos, ypos;
147 int sxpos, sypos;
148 static char no_cursor_data[] = { 0,0,0,0, 0,0,0,0 };
149 static GC cablegc, cablegcshadow;
150 Atom wm_delete_window;
151 static Bool config_set = False;
152 static Widget mouseSme, mouseMenu, keyboardSme, keyboardMenu,
153 cardSme, cardMenu, monitorSme, monitorMenu;
155 int config_mode = CONFIG_LAYOUT;
157 static XtActionsRec actions[] = {
158 {"filter-card", CardFilterAction},
159 {"select-device", SelectDeviceAction},
160 {"move-device", MoveDeviceAction},
161 {"unselect-device", UnselectDeviceAction},
162 {"device-popup", DevicePopupMenu},
163 {"device-popdown", DevicePopdownMenu},
164 {"rename-layout", RenameLayoutAction},
165 {"write-config", WriteConfigAction},
166 {"quit", QuitAction},
167 {"vidmode-restore", VidmodeRestoreAction},
168 {"config-cancel", ConfigCancelAction},
169 {"options-cancel", OptionsCancelAction},
170 {"error-cancel", ErrorCancelAction},
171 {"quit-cancel", QuitCancelAction},
172 {"addmode-cancel", CancelAddModeAction},
173 {"accessx-close", CloseAccessXAction},
174 {"testmode-cancel", CancelTestModeAction},
175 {"help-close", HelpCancelAction},
176 {"expert-close", ExpertCloseAction},
177 #ifdef USE_MODULES
178 {"module-options-close", ModuleOptionsCancelAction},
179 #endif
182 static char *device_names[] = {
183 /* MOUSE */
184 "mouse",
185 /* KEYBOARD */
186 "keyboard",
187 /* CARD */
188 "card",
189 /* MONITOR */
190 "monitor",
191 /* SCREEN */
192 "screen",
195 static XtResource appResources[] = {
196 #if 0
197 {"config", __XCONFIGFILE__, XtRString, sizeof(char*),
198 0, XtRString, "/etc/X11/"__XCONFIGFILE__},
199 #endif
200 {"menuBitmap", "MenuBitmap", XtRString, sizeof(char*),
201 0, XtRString, "menu10"},
204 static void
205 Usage(void)
207 fprintf(stderr,
208 "Usage:\n"
209 " xorgcfg [-option ...]\n"
210 "\n"
211 "Options:\n"
212 " -config <"__XCONFIGFILE__"> Alternate configuration file.\n"
213 " -modulepath <module-path> "__XSERVERNAME__" modules location.\n"
214 " -serverpath <server-path> X server to start (if $DISPLAY is not defined).\n"
215 " -fontpath <font-path> Font path for fonts.\n"
216 " -rgbpath <rgb-path> Where the rgb.txt file is located.\n"
217 #ifdef HAS_NCURSES
218 " -textmode Use this option for the text only interface.\n"
219 #endif
220 #ifdef USE_MODULES
221 " -nomodules Use this option if xorgcfg is slow to start.\n"
222 " -verbose <number> Verbosity used in the loader (default 1).\n"
223 #endif
224 " -verify Verify modules/options integrity.\n"
227 exit(1);
231 * Implementation
234 main(int argc, char *argv[])
236 Widget pane, hpane, expert, popup, mouse, keyboard, card, monitor;
237 Widget bottom, sme, smemodeline, help, quit, layopt;
238 XColor color, tmp;
239 Pixmap pixmap;
240 XGCValues values;
241 XF86ConfLayoutPtr lay;
242 int i, startedx;
243 char *menuPixmapPath = NULL;
244 XrmValue from, to;
246 if ((XFree86Dir = getenv("XWINHOME")) == NULL)
247 XFree86Dir = DefaultXFree86Dir;
249 chdir(XFree86Dir);
251 #ifdef USE_MODULES
252 xf86Verbose = 1;
253 #endif
254 noverify = True;
256 for (i = 1; i < argc; i++) {
257 if (strcmp(argv[i], "-config") == 0 ||
258 strcmp(argv[i], "-xorgconfig") == 0 ||
259 strcmp(argv[i], "-xf86config") == 0) {
260 if (i + 1 < argc) {
261 XF86Config_path = argv[++i];
262 config_set = True;
264 } else if (strcmp(argv[i], "-modulepath") == 0) {
265 if (i + 1 < argc)
266 XF86Module_path = argv[++i];
267 } else if (strcmp(argv[i], "-serverpath") == 0) {
268 if (i + 1 < argc)
269 XFree86_path = argv[++i];
270 } else if (strcmp(argv[i], "-fontpath") == 0) {
271 if (i + 1 < argc)
272 XF86Font_path = argv[++i];
273 } else if (strcmp(argv[i], "-rgbpath") == 0) {
274 if (i + 1 < argc)
275 XF86RGB_path = argv[++i];
277 #ifdef HAS_NCURSES
278 else if (strcmp(argv[i], "-textmode") == 0)
279 textmode = True;
280 #endif
281 #ifdef USE_MODULES
282 else if (strcmp(argv[i], "-nomodules") == 0)
283 nomodules = True;
284 else if (strcmp(argv[i], "-verbose") == 0) {
285 if (i + 1 < argc)
286 xf86Verbose = atoi(argv[++i]);
288 #endif
289 else if (strcmp(argv[i], "-verify") == 0)
290 noverify = False;
291 else
292 Usage();
295 #ifdef HAS_NCURSES
296 if (textmode) {
297 TextMode();
298 exit(0);
300 #endif
302 startedx = startx();
303 if (XF86Config_path == NULL)
304 XF86Config_path = XtNewString(__XCONFIGFILE__);
305 if (XkbConfig_path == NULL) {
306 XmuSnprintf(XkbConfig_path_static, sizeof(XkbConfig_path_static),
307 "%s/%s%s", XFree86Dir, XkbConfigDir, XkbConfigFile);
308 XkbConfig_path = XkbConfig_path_static;
310 toplevel = XtAppInitialize(&appcon, "XOrgCfg",
311 NULL, 0,
312 &argc, argv,
313 NULL, NULL, 0);
314 if (DPY == NULL)
315 DPY = XtDisplay(toplevel);
317 XtGetApplicationResources(toplevel, (XtPointer)&menuPixmapPath,
318 appResources, XtNumber(appResources), NULL, 0);
319 if (menuPixmapPath && strlen(menuPixmapPath)) {
320 from.size = strlen(menuPixmapPath);
321 from.addr = menuPixmapPath;
322 to.size = sizeof(Pixmap);
323 to.addr = (XtPointer)&(menuPixmap);
324 XtConvertAndStore(toplevel, XtRString, &from, XtRBitmap, &to);
327 XtAppAddActions(appcon, actions, XtNumber(actions));
329 XawSimpleMenuAddGlobalActions(appcon);
330 XtRegisterGrabAction(DevicePopupMenu, True,
331 ButtonPressMask | ButtonReleaseMask,
332 GrabModeAsync, GrabModeAsync);
334 pane = XtCreateManagedWidget("pane", panedWidgetClass,
335 toplevel, NULL, 0);
336 hpane = XtVaCreateManagedWidget("hpane", panedWidgetClass, pane,
337 XtNorientation, XtorientHorizontal, NULL);
338 topMenu = XtCreateManagedWidget("topM", menuButtonWidgetClass,
339 hpane, NULL, 0);
340 expert = XtCreateManagedWidget("expert", commandWidgetClass, hpane, NULL, 0);
341 XtAddCallback(expert, XtNcallback, ExpertCallback, NULL);
342 popup = XtCreatePopupShell("menu", simpleMenuWidgetClass,
343 topMenu, NULL, 0);
344 sme = XtCreateManagedWidget("layout", smeBSBObjectClass,
345 popup, NULL, 0);
346 XtAddCallback(sme, XtNcallback, SetConfigModeCallback,
347 (XtPointer)CONFIG_LAYOUT);
348 sme = XtCreateManagedWidget("screen", smeBSBObjectClass,
349 popup, NULL, 0);
350 XtAddCallback(sme, XtNcallback, SetConfigModeCallback,
351 (XtPointer)CONFIG_SCREEN);
352 smemodeline = XtCreateManagedWidget("modeline", smeBSBObjectClass,
353 popup, NULL, 0);
354 XtAddCallback(smemodeline, XtNcallback, SetConfigModeCallback,
355 (XtPointer)CONFIG_MODELINE);
356 sme = XtCreateManagedWidget("accessx", smeBSBObjectClass,
357 popup, NULL, 0);
358 XtAddCallback(sme, XtNcallback, SetConfigModeCallback,
359 (XtPointer)CONFIG_ACCESSX);
361 commands = XtCreateManagedWidget("commands", formWidgetClass,
362 pane, NULL, 0);
364 mouse = XtVaCreateManagedWidget("mouse", menuButtonWidgetClass,
365 commands, XtNmenuName, "mouseP", NULL);
366 popup = XtCreatePopupShell("mouseP", simpleMenuWidgetClass,
367 mouse, NULL, 0);
368 sme = XtCreateManagedWidget("new", smeBSBObjectClass,
369 popup, NULL, 0);
370 XtAddCallback(sme, XtNcallback, AddDeviceCallback, (XtPointer)MOUSE);
371 mouseSme = XtCreateManagedWidget("configure", smeBSBObjectClass,
372 popup, NULL, 0);
373 XtAddCallback(mouseSme, XtNcallback, SmeConfigureDeviceCallback,
374 (XtPointer)MOUSE);
376 keyboard = XtVaCreateManagedWidget("keyboard", menuButtonWidgetClass,
377 commands, XtNmenuName, "keyboardP", NULL);
378 popup = XtCreatePopupShell("keyboardP", simpleMenuWidgetClass,
379 keyboard, NULL, 0);
380 sme = XtCreateManagedWidget("new", smeBSBObjectClass,
381 popup, NULL, 0);
382 XtAddCallback(sme, XtNcallback, AddDeviceCallback, (XtPointer)KEYBOARD);
383 keyboardSme = XtCreateManagedWidget("configure", smeBSBObjectClass,
384 popup, NULL, 0);
385 XtAddCallback(keyboardSme, XtNcallback, SmeConfigureDeviceCallback,
386 (XtPointer)KEYBOARD);
388 card = XtVaCreateManagedWidget("card", menuButtonWidgetClass,
389 commands, XtNmenuName, "cardP", NULL);
390 popup = XtCreatePopupShell("cardP", simpleMenuWidgetClass,
391 card, NULL, 0);
392 sme = XtCreateManagedWidget("new", smeBSBObjectClass,
393 popup, NULL, 0);
394 XtAddCallback(sme, XtNcallback, AddDeviceCallback, (XtPointer)CARD);
395 cardSme = XtCreateManagedWidget("configure", smeBSBObjectClass,
396 popup, NULL, 0);
397 XtAddCallback(cardSme, XtNcallback, SmeConfigureDeviceCallback,
398 (XtPointer)CARD);
400 monitor = XtVaCreateManagedWidget("monitor", menuButtonWidgetClass,
401 commands, XtNmenuName, "monitorP", NULL);
402 popup = XtCreatePopupShell("monitorP", simpleMenuWidgetClass,
403 monitor, NULL, 0);
404 sme = XtCreateManagedWidget("new", smeBSBObjectClass,
405 popup, NULL, 0);
406 XtAddCallback(sme, XtNcallback, AddDeviceCallback, (XtPointer)MONITOR);
407 monitorSme = XtCreateManagedWidget("configure", smeBSBObjectClass,
408 popup, NULL, 0);
409 XtAddCallback(monitorSme, XtNcallback, SmeConfigureDeviceCallback,
410 (XtPointer)MONITOR);
412 work = XtCreateManagedWidget("work", compositeWidgetClass,
413 pane, NULL, 0);
415 bottom = XtCreateManagedWidget("bottom", formWidgetClass,
416 pane, NULL, 0);
417 layoutm = XtCreateManagedWidget("select", menuButtonWidgetClass,
418 bottom, NULL, 0);
419 layout = XtVaCreateManagedWidget("layout", asciiTextWidgetClass,
420 bottom,
421 XtNeditType, XawtextEdit,
422 NULL);
423 layoutp = XtCreatePopupShell("menu", simpleMenuWidgetClass,
424 bottom, NULL, 0);
425 sme = XtCreateManagedWidget("new", smeBSBObjectClass, layoutp,
426 NULL, 0);
427 XtAddCallback(sme, XtNcallback, SelectLayoutCallback, NULL);
428 help = XtCreateManagedWidget("help", commandWidgetClass,
429 bottom, NULL, 0);
430 XtAddCallback(help, XtNcallback, HelpCallback, NULL);
431 quit = XtCreateManagedWidget("quit", commandWidgetClass,
432 bottom, NULL, 0);
433 XtAddCallback(quit, XtNcallback, QuitCallback, NULL);
435 XtRealizeWidget(toplevel);
436 XtRealizeWidget(topMenu);
438 pixmap = XCreateBitmapFromData(XtDisplay(toplevel), XtWindow(toplevel),
439 no_cursor_data, 8, 8);
440 XAllocNamedColor(XtDisplay(toplevel), toplevel->core.colormap, "black",
441 &color, &tmp);
442 no_cursor = XCreatePixmapCursor(XtDisplay(toplevel), pixmap, pixmap,
443 &color, &color, 0, 0);
445 XAllocNamedColor(XtDisplay(toplevel), toplevel->core.colormap, "gray55",
446 &color, &tmp);
447 values.line_width = 3;
448 values.foreground = color.pixel;
449 cablegcshadow = XCreateGC(XtDisplay(toplevel), XtWindow(toplevel),
450 GCForeground | GCLineWidth, &values);
451 XAllocNamedColor(XtDisplay(toplevel), toplevel->core.colormap, "gray85",
452 &color, &tmp);
453 values.line_width = 1;
454 values.foreground = color.pixel;
455 cablegc = XCreateGC(XtDisplay(toplevel), XtWindow(toplevel),
456 GCForeground | GCLineWidth, &values);
458 computer.cpu = XtCreateManagedWidget("cpu", simpleWidgetClass,
459 work, NULL, 0);
460 cpu_device.widget = computer.cpu;
461 cpu_device.type = SERVER;
463 XtAddEventHandler(work, ExposureMask, False,
464 ComputerEventHandler, (XtPointer)NULL);
466 wm_delete_window = XInternAtom(DPY, "WM_DELETE_WINDOW", False);
467 XSetWMProtocols(DPY, XtWindow(toplevel), &wm_delete_window, 1);
469 StartConfig();
470 InitializeDevices();
471 UpdateMenuDeviceList(MOUSE);
472 UpdateMenuDeviceList(KEYBOARD);
473 UpdateMenuDeviceList(CARD);
474 UpdateMenuDeviceList(MONITOR);
475 XtSetSensitive(smemodeline, VideoModeInitialize());
477 lay = XF86Config->conf_layout_lst;
478 while (lay != NULL) {
479 sme = XtVaCreateManagedWidget("sme", smeBSBObjectClass,
480 layoutp,
481 XtNlabel, lay->lay_identifier,
482 XtNmenuName, lay->lay_identifier,
483 XtNleftBitmap, menuPixmap,
484 NULL);
485 XtAddCallback(sme, XtNcallback, SelectLayoutCallback, (XtPointer)lay);
486 if (layoutsme == NULL)
487 layoutsme = sme;
488 layopt = XtCreatePopupShell(lay->lay_identifier, simpleMenuWidgetClass,
489 layoutp, NULL, 0);
490 sme = XtCreateManagedWidget("default", smeBSBObjectClass,
491 layopt, NULL, 0);
492 XtAddCallback(sme, XtNcallback, DefaultLayoutCallback, NULL);
493 sme = XtCreateManagedWidget("remove", smeBSBObjectClass,
494 layopt, NULL, 0);
495 XtAddCallback(sme, XtNcallback, RemoveLayoutCallback, NULL);
496 XtRealizeWidget(layopt);
498 lay = (XF86ConfLayoutPtr)(lay->list.next);
500 SelectLayoutCallback(layoutsme,
501 XF86Config->conf_layout_lst, NULL);
503 startaccessx();
504 if (startedx) {
505 switch (fork()) {
506 case 0: {
507 char path[PATH_MAX];
509 XmuSnprintf(path, sizeof(path), "%s/bin/twm", XFree86Dir);
510 execl(path, "twm", (void *)NULL);
511 exit(-127);
512 } break;
513 case -1:
514 fprintf(stderr, "Cannot fork.\n");
515 exit(1);
516 break;
517 default:
518 break;
522 #ifdef USE_MODULES
523 if (!nomodules)
524 LoaderInitializeOptions();
525 #endif
527 /* ReadCardsDatabase() must be called after LoaderInitializeOptions() */
528 ReadCardsDatabase();
530 if (!config_set && startedx) {
531 XtFree(XF86Config_path);
532 #ifndef XF86CONFIG
533 # define XF86CONFIG __XCONFIGFILE__
534 #endif
535 #ifdef XF86CONFIGDIR
536 XF86Config_path = XtNewString(XF86CONFIGDIR "/" XF86CONFIG);
537 #else
538 XF86Config_path = XtNewString("/etc/X11/" XF86CONFIG);
539 #endif
541 XtAppMainLoop(appcon);
542 if (startedx)
543 endx();
545 return (0);
548 static Widget shell_cf;
549 static int write_cf, asking_cf;
550 static int cf_state = 0;
551 #define CF_XF86Config 1
552 #define CF_XKBConfig 2
553 #define CF_First CF_XF86Config
554 #define CF_Last CF_XKBConfig
556 /*ARGSUSED*/
557 static void
558 WriteConfig(Widget w, XtPointer user_data, XtPointer call_data)
560 asking_cf = 0;
561 XtPopdown(shell_cf);
562 write_cf = (long)user_data;
565 /*ARGSUSED*/
566 void
567 QuitCancelAction(Widget w, XEvent *event, String *params, Cardinal *num_params)
569 WriteConfig(w, (XtPointer)-1, NULL);
572 /*ARGSUSED*/
573 void
574 WriteConfigAction(Widget w, XEvent *event,
575 String *params, Cardinal *num_params)
577 WriteConfig(w, (XtPointer)True, NULL);
580 static Bool
581 AskConfig(void)
583 static Widget dialog;
585 if (shell_cf == NULL) {
586 Arg args[1];
587 char *l, *label;
588 int len;
590 shell_cf = XtCreatePopupShell("quit", transientShellWidgetClass,
591 toplevel, NULL, 0);
592 dialog = XtVaCreateManagedWidget("ask", dialogWidgetClass, shell_cf,
593 XtNvalue, XF86Config_path, NULL);
594 XawDialogAddButton(dialog, "yes", WriteConfig, (XtPointer)1);
595 XawDialogAddButton(dialog, "no", WriteConfig, (XtPointer)0);
596 XawDialogAddButton(dialog, "cancel", WriteConfig, (XtPointer)-1);
597 XtRealizeWidget(shell_cf);
598 XSetWMProtocols(DPY, XtWindow(shell_cf), &wm_delete_window, 1);
599 XtSetArg(args[0], XtNlabel, &l);
600 XtGetValues(dialog, args, 1);
601 label = XtMalloc(len = (strlen(l) + strlen(XF86CONFIG) + 2));
602 XmuSnprintf(label, len, "%s\n", XF86CONFIG);
603 strcat(label, l);
604 XtSetArg(args[0], XtNlabel, label);
605 XtSetValues(dialog, args, 1);
606 XtFree(label);
608 else {
609 Arg args[2];
610 Cardinal num_args = 0;
611 char *l, *label = NULL, *str = "";
613 XtSetArg(args[0], XtNlabel, &l);
614 XtGetValues(dialog, args, 1);
615 switch (cf_state) {
616 case CF_XF86Config:
617 str = XF86CONFIG;
618 XtSetArg(args[num_args], XtNvalue, XF86Config_path);
619 ++num_args;
620 break;
621 case CF_XKBConfig:
622 str = "XKB";
623 XtSetArg(args[num_args], XtNvalue, XkbConfig_path);
624 ++num_args;
625 break;
627 l = strchr(l, '\n');
628 if (l != NULL) {
629 label = XtMalloc(strlen(str) + strlen(l) + 1);
630 strcpy(label, str);
631 strcat(label, l);
632 XtSetArg(args[num_args], XtNlabel, label);
633 ++num_args;
635 XtSetValues(dialog, args, num_args);
636 if (l != NULL)
637 XtFree(label);
640 asking_cf = 1;
642 XtPopup(shell_cf, XtGrabExclusive);
643 while (asking_cf)
644 XtAppProcessEvent(XtWidgetToApplicationContext(shell_cf), XtIMAll);
646 if (write_cf > 0) {
647 switch (cf_state) {
648 case CF_XF86Config:
649 XF86Config_path = XawDialogGetValueString(dialog);
650 XmuSnprintf(XF86Config_path_static,
651 sizeof(XF86Config_path_static),
652 "%s", XF86Config_path);
653 XF86Config_path = XF86Config_path_static;
654 break;
655 case CF_XKBConfig:
656 XkbConfig_path = XawDialogGetValueString(dialog);
657 XmuSnprintf(XkbConfig_path_static,
658 sizeof(XkbConfig_path_static),
659 "%s", XkbConfig_path);
660 XkbConfig_path = XkbConfig_path_static;
661 break;
665 return (write_cf);
668 /*ARGSUSED*/
669 void
670 PopdownErrorCallback(Widget w, XtPointer user_data, XtPointer call_data)
672 XtPopdown((Widget)user_data);
675 /*ARGSUSED*/
676 void
677 ErrorCancelAction(Widget w, XEvent *event, String *params, Cardinal *num_params)
679 XtPopdown((Widget)w);
682 /*ARGSUSED*/
683 void
684 QuitAction(Widget w, XEvent *event, String *params, Cardinal *num_params)
686 QuitCallback(w, NULL, NULL);
689 /*ARGSUSED*/
690 void
691 QuitCallback(Widget w, XtPointer user_data, XtPointer call_data)
693 for (cf_state = CF_First; cf_state <= CF_Last; cf_state++) {
694 if (cf_state == CF_XKBConfig && xkb_info == NULL)
695 continue;
697 switch (AskConfig()) {
698 case 0:
699 break;
700 case 1:
701 if ((cf_state == CF_XF86Config &&
702 !xf86writeConfigFile(XF86Config_path, XF86Config)) ||
703 (cf_state == CF_XKBConfig &&
704 !WriteXKBConfiguration(XkbConfig_path,
705 &xkb_info->config))) {
706 static Widget shell;
708 if (shell == NULL) {
709 Widget dialog;
711 shell = XtCreatePopupShell("error",
712 transientShellWidgetClass,
713 toplevel, NULL, 0);
714 dialog = XtVaCreateManagedWidget("notice",
715 dialogWidgetClass,
716 shell, XtNvalue, NULL,
717 NULL);
718 XawDialogAddButton(dialog, "ok", PopdownErrorCallback,
719 (XtPointer)shell);
720 XtRealizeWidget(shell);
721 XSetWMProtocols(DPY, XtWindow(shell),
722 &wm_delete_window, 1);
724 XtPopup(shell, XtGrabExclusive);
725 return;
727 break;
728 default:
729 return;
733 endx();
734 exit(0);
737 void
738 InitializeDevices(void)
740 xf86cfgDevice *device;
741 int mouse_x, mouse_y, keyboard_x, keyboard_y,
742 card_x, card_y, monitor_x, monitor_y, len;
743 XF86ConfInputPtr input = XF86Config->conf_input_lst;
744 XF86ConfDevicePtr card = XF86Config->conf_device_lst;
745 XF86ConfMonitorPtr monitor = XF86Config->conf_monitor_lst;
746 XF86OptionPtr flags = NULL;
747 char buffer[4096], *tip;
748 Arg args[1];
750 if (XF86Config->conf_flags != NULL)
751 flags = XF86Config->conf_flags->flg_option_lst;
753 len = 0;
754 while (flags && len < sizeof(buffer) - 1) {
755 len += XmuSnprintf(buffer + len, sizeof(buffer) - len,
756 "Option \"%s\"",
757 flags->opt_name);
758 if (flags->opt_val != NULL)
759 len += XmuSnprintf(buffer + len, sizeof(buffer) - len,
760 " \"%s\"\n",
761 flags->opt_val);
762 else
763 len += XmuSnprintf(buffer + len, sizeof(buffer) - len,
764 "%s", "\n");
765 flags = (XF86OptionPtr)(flags->list.next);
768 if (len) {
769 tip = XtNewString(buffer);
770 XtSetArg(args[0], XtNtip, tip);
771 XtSetValues(computer.cpu, args, 1);
774 #define DEFAULT_MOUSE_WIDTH 30
775 #define DEFAULT_MOUSE_HEIGHT 40
776 #define DEFAULT_KEYBOARD_WIDTH 48
777 #define DEFAULT_KEYBOARD_HEIGHT 36
778 mouse_x = work->core.width - (work->core.width >> 2);
779 mouse_y = work->core.height - DEFAULT_MOUSE_HEIGHT;
780 keyboard_x = 6;
781 keyboard_y = work->core.height - DEFAULT_KEYBOARD_HEIGHT;
783 while (input != NULL) {
784 if (input->inp_driver) {
785 if (strcasecmp(input->inp_driver, "mouse") == 0) {
786 device = AddDevice(MOUSE, (XtPointer)input, mouse_x, mouse_y);
787 SetTip(device);
788 if ((mouse_x += DEFAULT_MOUSE_WIDTH) > work->core.width) {
789 if ((mouse_y -= DEFAULT_MOUSE_HEIGHT) < (work->core.height >> 1))
790 mouse_y = work->core.height >> 1;
791 mouse_x = work->core.width - (work->core.width >> 2);
794 else if (IS_KBDDRIV(input->inp_driver)) {
795 device = AddDevice(KEYBOARD, (XtPointer)input, keyboard_x, keyboard_y);
796 SetTip(device);
797 if ((keyboard_x += DEFAULT_KEYBOARD_WIDTH) >
798 work->core.width - (work->core.width >> 2)) {
799 if ((keyboard_y -= DEFAULT_KEYBOARD_HEIGHT) < (work->core.height >> 1))
800 keyboard_y = work->core.height >> 1;
801 keyboard_x = 6;
805 input = (XF86ConfInputPtr)(input->list.next);
808 #define DEFAULT_CARD_WIDTH 45
809 #define DEFAULT_CARD_HEIGHT 46
810 card_x = 6;
811 card_y = (work->core.height >> 1) - 20 - DEFAULT_CARD_HEIGHT;
812 while (card != NULL) {
813 device = AddDevice(CARD, (XtPointer)card, card_x, card_y);
814 SetTip(device);
815 if ((card_x += DEFAULT_CARD_WIDTH) > work->core.width) {
816 if ((card_y -= DEFAULT_CARD_HEIGHT) < (work->core.height >> 2))
817 card_y = work->core.height >> 2;
818 card_x = 6;
820 card = (XF86ConfDevicePtr)(card->list.next);
823 #define DEFAULT_MONITOR_WIDTH 48
824 #define DEFAULT_MONITOR_HEIGHT 48
825 monitor_x = 6;
826 monitor_y = 6;
827 while (monitor != NULL) {
828 XF86ConfScreenPtr screen = XF86Config->conf_screen_lst;
830 device = AddDevice(MONITOR, (XtPointer)monitor, monitor_x, monitor_y);
831 SetTip(device);
832 if ((monitor_x += DEFAULT_MONITOR_WIDTH) > work->core.width) {
833 if ((monitor_y += DEFAULT_MONITOR_HEIGHT) >
834 (work->core.height >> 2) - DEFAULT_MONITOR_HEIGHT)
835 monitor_y = (work->core.height >> 2) - DEFAULT_MONITOR_HEIGHT;
836 monitor_x = 6;
839 while (screen != NULL) {
840 if (screen->scrn_monitor == monitor) {
841 card = XF86Config->conf_device_lst;
842 while (card != NULL) {
843 if (screen->scrn_device == card) {
844 xf86cfgScreen *scr = (xf86cfgScreen*)
845 XtCalloc(1, sizeof(xf86cfgScreen));
846 int i;
848 for (i = 0; i < computer.num_devices; i++)
849 if ((XF86ConfDevicePtr)(computer.devices[i]->config)
850 == card)
851 break;
852 scr->screen = screen;
853 scr->card = computer.devices[i];
854 scr->monitor = device;
855 scr->refcount = 0;
856 ++scr->card->refcount;
857 ++scr->monitor->refcount;
858 computer.screens = (xf86cfgScreen**)
859 XtRealloc((XtPointer)computer.screens,
860 sizeof(xf86cfgScreen*) *
861 (computer.num_screens + 1));
862 CreateScreenWidget(scr);
863 scr->type = SCREEN;
864 computer.screens[computer.num_screens++] = scr;
865 SetTip((xf86cfgDevice*)scr);
866 break;
868 card = (XF86ConfDevicePtr)(card->list.next);
870 device->state = USED;
872 screen = (XF86ConfScreenPtr)(screen->list.next);
875 monitor = (XF86ConfMonitorPtr)(monitor->list.next);
879 xf86cfgDevice *
880 AddDevice(int type, XtPointer config, int x, int y)
882 switch (type) {
883 case MOUSE:
884 case KEYBOARD:
885 case CARD:
886 case MONITOR:
887 computer.devices = (xf86cfgDevice**)
888 XtRealloc((XtPointer)computer.devices,
889 sizeof(xf86cfgDevice*) * (computer.num_devices + 1));
890 computer.devices[computer.num_devices] = (xf86cfgDevice*)
891 XtCalloc(1, sizeof(xf86cfgDevice));
892 computer.devices[computer.num_devices]->config = config;
893 computer.devices[computer.num_devices]->widget =
894 XtVaCreateManagedWidget(device_names[type], simpleWidgetClass,
895 work,
896 XtNx, x,
897 XtNy, y,
898 XtNtip, NULL,
899 NULL);
900 computer.devices[computer.num_devices]->type = type;
901 computer.devices[computer.num_devices]->state = UNUSED;
902 computer.devices[computer.num_devices]->refcount = 0;
903 ++computer.num_devices;
904 break;
905 default:
906 fprintf(stderr, "Bad argument to AddDevice.\n");
907 exit(1);
908 return (NULL);
911 UpdateMenuDeviceList(type);
913 return (computer.devices[computer.num_devices - 1]);
916 /*ARGSUSED*/
917 static void
918 HelpCallback(Widget w, XtPointer user_data, XtPointer call_data)
920 char *topic = NULL;
922 switch (config_mode) {
923 case CONFIG_LAYOUT:
924 topic = HELP_DEVICES;
925 break;
926 case CONFIG_SCREEN:
927 topic = HELP_SCREEN;
928 break;
929 case CONFIG_MODELINE:
930 topic = HELP_MODELINE;
931 break;
932 case CONFIG_ACCESSX:
933 topic = HELP_ACCESSX;
934 break;
936 Help(topic);
939 void
940 SelectLayoutCallback(Widget w, XtPointer user_data, XtPointer call_data)
942 int i, j;
943 XF86ConfLayoutPtr lay = (XF86ConfLayoutPtr)user_data;
944 XF86ConfInputrefPtr input;
945 XF86ConfAdjacencyPtr adj;
946 Widget sme, layopt;
947 Arg args[1];
948 char *str;
950 /* XXX Needs to check computer.layout,
951 * because this function should also create
952 * a new layout...
954 if (lay == computer.layout && computer.layout)
955 return;
957 if (computer.layout != NULL) {
958 for (i = 0; i < computer.num_layouts; i++) {
959 if (computer.layouts[i]->layout == computer.layout)
960 break;
962 if (i < computer.num_layouts) {
963 XtFree((XtPointer)computer.layouts[i]->screen);
964 XtFree((XtPointer)computer.layouts[i]->position);
966 else {
967 computer.layouts = (xf86cfgLayout**)
968 XtRealloc((XtPointer)computer.layouts, sizeof(xf86cfgLayout*) *
969 (computer.num_layouts + 1));
970 ++computer.num_layouts;
972 computer.layouts[i] = (xf86cfgLayout*)XtCalloc(1, sizeof(xf86cfgLayout));
973 computer.layouts[i]->layout = computer.layout;
974 computer.layouts[i]->num_layouts = computer.num_screens;
975 computer.layouts[i]->screen = (xf86cfgScreen**)
976 XtMalloc(sizeof(xf86cfgScreen*) * computer.num_screens);
977 computer.layouts[i]->position = (XPoint*)
978 XtMalloc(sizeof(XPoint) * computer.num_screens);
979 for (j = 0; j < computer.num_screens; j++) {
980 computer.layouts[i]->screen[j] = computer.screens[j];
981 computer.layouts[i]->position[j].x = computer.screens[j]->widget->core.x;
982 computer.layouts[i]->position[j].y = computer.screens[j]->widget->core.y;
986 if (lay != NULL) {
987 for (i = 0; i < computer.num_layouts; i++)
988 if (computer.layouts[i]->layout == lay) {
989 for (j = 0; j < computer.layouts[i]->num_layouts; j++) {
990 int k;
992 for (k = 0; k < computer.num_screens; k++)
993 if (computer.screens[k] == computer.layouts[i]->screen[j]) {
994 XtMoveWidget(computer.screens[k]->widget,
995 computer.layouts[i]->position[j].x,
996 computer.layouts[i]->position[j].y);
999 break;
1002 layoutsme = w;
1003 XtSetArg(args[0], XtNlabel, &str);
1004 XtGetValues(w, args, 1);
1005 XtSetArg(args[0], XtNstring, str);
1006 XtSetValues(layout, args, 1);
1009 computer.layout = lay;
1011 for (i = 0; i < computer.num_devices; i++)
1012 computer.devices[i]->state = UNUSED;
1013 for (i = 0; i < computer.num_screens; i++)
1014 computer.screens[i]->state = UNUSED;
1016 if (lay == NULL) {
1017 char name[64];
1018 XF86ConfLayoutPtr l;
1019 int num_layouts = 0;
1021 l = XF86Config->conf_layout_lst;
1022 while (l != NULL) {
1023 if (l->lay_adjacency_lst == NULL &&
1024 l->lay_inactive_lst == NULL &&
1025 l->lay_input_lst == NULL &&
1026 l->lay_option_lst == NULL &&
1027 l->lay_comment == NULL) {
1028 for (i = 0;
1029 i < ((CompositeWidget)layout)->composite.num_children; i++)
1030 if (strcmp(XtName(((CompositeWidget)layout)->composite.
1031 children[i]), l->lay_identifier) == 0) {
1032 layoutsme = ((CompositeWidget)layout)->composite.children[i];
1034 computer.layout = l;
1035 XtSetArg(args[0], XtNstring, l->lay_identifier);
1036 XtSetValues(layout, args, 1);
1037 if (config_mode == CONFIG_LAYOUT)
1038 DrawCables();
1039 if (config_mode == CONFIG_SCREEN)
1040 ScreenSetup(True);
1041 return;
1043 ++num_layouts;
1044 l = (XF86ConfLayoutPtr)(l->list.next);
1046 do {
1047 XmuSnprintf(name, sizeof(name), "Layout%d", num_layouts);
1048 ++num_layouts;
1049 } while (xf86findLayout(name,
1050 XF86Config->conf_layout_lst) != NULL);
1051 l = (XF86ConfLayoutPtr)XtCalloc(1, sizeof(XF86ConfLayoutRec));
1053 l->lay_identifier = XtNewString(name);
1054 XF86Config->conf_layout_lst =
1055 xf86addLayout(XF86Config->conf_layout_lst, l);
1056 layoutsme = XtVaCreateManagedWidget("sme", smeBSBObjectClass,
1057 layoutp,
1058 XtNlabel, name,
1059 XtNmenuName, l->lay_identifier,
1060 XtNleftBitmap, menuPixmap,
1061 NULL);
1062 XtAddCallback(layoutsme, XtNcallback,
1063 SelectLayoutCallback, (XtPointer)l);
1065 layopt = XtCreatePopupShell(l->lay_identifier, simpleMenuWidgetClass,
1066 layoutp, NULL, 0);
1067 sme = XtCreateManagedWidget("default", smeBSBObjectClass,
1068 layopt, NULL, 0);
1069 XtAddCallback(sme, XtNcallback, DefaultLayoutCallback, NULL);
1070 sme = XtCreateManagedWidget("remove", smeBSBObjectClass,
1071 layopt, NULL, 0);
1072 XtAddCallback(sme, XtNcallback, RemoveLayoutCallback, NULL);
1073 XtRealizeWidget(layopt);
1075 computer.layout = l;
1076 XtSetArg(args[0], XtNstring, name);
1077 XtSetValues(layout, args, 1);
1078 if (config_mode == CONFIG_LAYOUT)
1079 DrawCables();
1080 if (config_mode == CONFIG_SCREEN)
1081 ScreenSetup(True);
1082 return;
1085 input = lay->lay_input_lst;
1086 adj = lay->lay_adjacency_lst;
1088 for (i = 0; i < computer.num_devices; i++)
1089 if (computer.devices[i]->config != NULL &&
1090 (computer.devices[i]->type == MOUSE ||
1091 computer.devices[i]->type == KEYBOARD)) {
1092 while (input != NULL) {
1093 if (strcmp(input->iref_inputdev_str, ((XF86ConfInputPtr)
1094 (computer.devices[i]->config))->inp_identifier) == 0) {
1095 computer.devices[i]->state = USED;
1096 break;
1098 input = (XF86ConfInputrefPtr)(input->list.next);
1100 input = lay->lay_input_lst;
1103 for (i = 0; i < computer.num_devices; i++)
1104 if (computer.devices[i]->type == CARD) {
1105 while (adj != NULL) {
1106 XF86ConfScreenPtr screen = adj->adj_screen;
1108 if (computer.devices[i]->config != NULL &&
1109 strcmp(screen->scrn_device_str, ((XF86ConfDevicePtr)
1110 (computer.devices[i]->config))->dev_identifier) == 0) {
1111 int j;
1113 for (j = 0; j < computer.num_screens; j++)
1114 if (computer.screens[j]->card == computer.devices[i])
1115 break;
1116 computer.screens[j]->card->state = USED;
1117 if (computer.screens[j]->monitor != NULL)
1118 computer.screens[j]->monitor->state = USED;
1119 computer.screens[j]->state = USED;
1122 adj = (XF86ConfAdjacencyPtr)(adj->list.next);
1124 adj = lay->lay_adjacency_lst;
1127 if (config_mode == CONFIG_LAYOUT)
1128 DrawCables();
1129 else if (config_mode == CONFIG_SCREEN)
1130 ScreenSetup(True);
1133 /*ARGSUSED*/
1134 void
1135 DefaultLayoutCallback(Widget w, XtPointer user_data, XtPointer call_data)
1137 Widget layopt, sme;
1138 int i;
1139 char *str;
1140 XF86ConfLayoutPtr prev, tmp, lay;
1142 str = w && XtParent(w) ? XtName(XtParent(w)) : NULL;
1143 if (str == NULL)
1144 return;
1146 prev = XF86Config->conf_layout_lst;
1147 lay = xf86findLayout(str, prev);
1148 if (prev == lay)
1149 return;
1151 tmp = prev;
1152 while (tmp != NULL) {
1153 if (tmp == lay)
1154 break;
1155 prev = tmp;
1156 tmp = (XF86ConfLayoutPtr)(tmp->list.next);
1159 for (i = 1; i < ((CompositeWidget)layoutp)->composite.num_children; i++)
1160 XtDestroyWidget(((CompositeWidget)layoutp)->composite.children[i]);
1161 for (i = 0; i < layoutp->core.num_popups; i++)
1162 XtDestroyWidget(layoutp->core.popup_list[i]);
1164 prev->list.next = lay->list.next;
1165 lay->list.next = XF86Config->conf_layout_lst;
1166 XF86Config->conf_layout_lst = lay;
1168 layoutsme = NULL;
1169 lay = XF86Config->conf_layout_lst;
1170 while (lay != NULL) {
1171 sme = XtVaCreateManagedWidget("sme", smeBSBObjectClass,
1172 layoutp,
1173 XtNlabel, lay->lay_identifier,
1174 XtNmenuName, lay->lay_identifier,
1175 XtNleftBitmap, menuPixmap,
1176 NULL);
1177 XtAddCallback(sme, XtNcallback, SelectLayoutCallback, (XtPointer)lay);
1178 if (layoutsme == NULL)
1179 layoutsme = sme;
1180 layopt = XtCreatePopupShell(lay->lay_identifier, simpleMenuWidgetClass,
1181 layoutp, NULL, 0);
1182 sme = XtCreateManagedWidget("default", smeBSBObjectClass,
1183 layopt, NULL, 0);
1184 XtAddCallback(sme, XtNcallback, DefaultLayoutCallback, NULL);
1185 sme = XtCreateManagedWidget("remove", smeBSBObjectClass,
1186 layopt, NULL, 0);
1187 XtAddCallback(sme, XtNcallback, RemoveLayoutCallback, NULL);
1188 XtRealizeWidget(layopt);
1190 lay = (XF86ConfLayoutPtr)(lay->list.next);
1192 SelectLayoutCallback(layoutsme,
1193 XF86Config->conf_layout_lst, NULL);
1196 /*ARGSUSED*/
1197 void
1198 RemoveLayoutCallback(Widget w, XtPointer user_data, XtPointer call_data)
1200 XF86ConfLayoutPtr prev, tmp, lay, rem;
1201 Widget sme = NULL;
1202 int i;
1203 char *str;
1204 Arg args[1];
1206 str = w && XtParent(w) ? XtName(XtParent(w)) : NULL;
1207 if (str == NULL)
1208 return;
1210 prev = XF86Config->conf_layout_lst;
1211 lay = xf86findLayout(str, prev);
1212 tmp = prev;
1213 while (tmp != NULL) {
1214 if (tmp == lay)
1215 break;
1216 prev = tmp;
1217 tmp = (XF86ConfLayoutPtr)(tmp->list.next);
1220 rem = lay;
1221 if (tmp != NULL)
1222 lay = (XF86ConfLayoutPtr)(tmp->list.next);
1223 if (lay == NULL && prev != tmp)
1224 lay = prev;
1226 if (lay != NULL) {
1227 int i;
1229 for (i = 0; i < ((CompositeWidget)layoutp)->composite.num_children;
1230 i++) {
1231 XtSetArg(args[0], XtNlabel, &str);
1232 XtGetValues(((CompositeWidget)layoutp)->composite.children[i],
1233 args, 1);
1234 if (strcmp(lay->lay_identifier, str) == 0) {
1235 layoutsme = ((CompositeWidget)layoutp)->composite.children[i];
1236 break;
1239 SelectLayoutCallback(layoutsme, lay, NULL);
1241 else {
1242 computer.layout = NULL;
1243 XtSetArg(args[0], XtNstring, "");
1244 XtSetValues(layout, args, 1);
1246 for (i = 0; i < computer.num_devices; i++)
1247 computer.devices[i]->state = UNUSED;
1248 DrawCables();
1251 for (i = 0; i < ((CompositeWidget)layoutp)->composite.num_children; i++) {
1252 XtSetArg(args[0], XtNlabel, &str);
1253 XtGetValues(((CompositeWidget)layoutp)->composite.children[i], args, 1);
1254 if (strcmp(rem->lay_identifier, str) == 0) {
1255 sme = ((CompositeWidget)layoutp)->composite.children[i];
1256 break;
1260 xf86removeLayout(XF86Config, rem);
1261 if (sme)
1262 XtDestroyWidget(sme);
1265 void
1266 SetTip(xf86cfgDevice *device)
1268 XF86OptionPtr option = NULL;
1269 char *tip, buffer[4096];
1270 Arg args[1];
1271 int len = 0;
1273 XtSetArg(args[0], XtNtip, &tip);
1274 XtGetValues(device->widget, args, 1);
1276 switch (device->type) {
1277 case MOUSE: {
1278 XF86ConfInputPtr mouse = (XF86ConfInputPtr)device->config;
1280 if (mouse == NULL)
1281 return;
1282 len = XmuSnprintf(buffer, sizeof(buffer),
1283 "Identifier \"%s\"\n"
1284 "Driver \"mouse\"\n",
1285 mouse->inp_identifier);
1286 option = mouse->inp_option_lst;
1287 } break;
1288 case KEYBOARD: {
1289 XF86ConfInputPtr keyboard = (XF86ConfInputPtr)device->config;
1291 if (keyboard == NULL)
1292 return;
1293 len = XmuSnprintf(buffer, sizeof(buffer),
1294 "Identifier \"%s\"\n"
1295 "Driver \"keyboard\"\n",
1296 keyboard->inp_identifier);
1297 option = keyboard->inp_option_lst;
1298 } break;
1299 case CARD: {
1300 XF86ConfDevicePtr card = (XF86ConfDevicePtr)device->config;
1302 if (card == NULL)
1303 return;
1304 len = XmuSnprintf(buffer, sizeof(buffer),
1305 "Identifier \"%s\"\n"
1306 "Driver \"%s\"\n",
1307 card->dev_identifier,
1308 card->dev_driver);
1309 option = card->dev_option_lst;
1310 } break;
1311 case MONITOR: {
1312 XF86ConfMonitorPtr monitor = (XF86ConfMonitorPtr)device->config;
1314 if (monitor == NULL)
1315 return;
1316 if (monitor->mon_vendor != NULL)
1317 len = XmuSnprintf(buffer, sizeof(buffer),
1318 "Identifier \"%s\"\n"
1319 "Vendor \"%s\"\n",
1320 monitor->mon_identifier,
1321 monitor->mon_vendor);
1322 else
1323 len = XmuSnprintf(buffer, sizeof(buffer),
1324 "Identifier \"%s\"\n",
1325 monitor->mon_identifier);
1326 option = monitor->mon_option_lst;
1327 } break;
1328 case SCREEN: {
1329 XF86ConfScreenPtr screen = (XF86ConfScreenPtr)device->config;
1331 if (screen == NULL)
1332 return;
1333 len = XmuSnprintf(buffer, sizeof(buffer),
1334 "Identifier \"%s\"\n",
1335 screen->scrn_identifier);
1336 if (screen->scrn_device_str != NULL)
1337 len += XmuSnprintf(buffer + len, sizeof(buffer),
1338 "Device \"%s\"\n",
1339 screen->scrn_device_str);
1340 if (screen->scrn_monitor_str != NULL)
1341 len += XmuSnprintf(buffer + len, sizeof(buffer),
1342 "Monitor \"%s\"\n",
1343 screen->scrn_monitor_str);
1344 option = screen->scrn_option_lst;
1345 } break;
1346 case SERVER: {
1347 len = XmuSnprintf(buffer, sizeof(buffer),
1348 "%s\n", "Server Flags");
1349 option = XF86Config->conf_flags->flg_option_lst;
1350 } break;
1353 while (option && len < sizeof(buffer) - 1) {
1354 len += XmuSnprintf(buffer + len, sizeof(buffer) - len,
1355 "Option \"%s\"",
1356 option->opt_name);
1357 if (option->opt_val != NULL)
1358 len += XmuSnprintf(buffer + len, sizeof(buffer) - len,
1359 " \"%s\"\n",
1360 option->opt_val);
1361 else
1362 len += XmuSnprintf(buffer + len, sizeof(buffer) - len,
1363 "%s", "\n");
1364 option = (XF86OptionPtr)(option->list.next);
1367 tip = buffer;
1368 XtSetArg(args[0], XtNtip, tip);
1369 XtSetValues(device->widget, args, 1);
1372 /*ARGSUSED*/
1373 void
1374 AddDeviceCallback(Widget w, XtPointer user_data, XtPointer call_data)
1376 AddDevice((long)user_data, NULL, 6, 6);
1379 void
1380 SmeConfigureDeviceCallback(Widget w, XtPointer user_data, XtPointer call_data)
1382 int i;
1384 switch ((long)user_data) {
1385 case MOUSE:
1386 case KEYBOARD:
1387 case CARD:
1388 case MONITOR:
1389 for (i = 0; i < computer.num_devices; i++)
1390 if (computer.devices[i]->type == (long)user_data) {
1391 config = computer.devices[i]->widget;
1392 ConfigureDeviceCallback(w, NULL, NULL);
1394 break;
1396 /* hack for newly added devices */
1397 case -(MOUSE + 100):
1398 case -(KEYBOARD + 100):
1399 case -(CARD + 100):
1400 case -(MONITOR + 100):
1401 for (i = 0; i < computer.num_devices; i++)
1402 if (-(computer.devices[i]->type + 100) == (long)user_data &&
1403 computer.devices[i]->config == NULL) {
1404 config = computer.devices[i]->widget;
1405 ConfigureDeviceCallback(w, NULL, NULL);
1407 break;
1409 default:
1410 for (i = 0; i < computer.num_devices; i++)
1411 if (computer.devices[i]->config == user_data) {
1412 config = computer.devices[i]->widget;
1413 ConfigureDeviceCallback(w, NULL, NULL);
1415 break;
1419 void
1420 ConfigureDeviceCallback(Widget w, XtPointer user_data, XtPointer call_data)
1422 int i, j;
1424 if (config_mode == CONFIG_LAYOUT) {
1425 for (i = 0; i < computer.num_devices; i++) {
1426 if (computer.devices[i]->widget == config) {
1427 switch (computer.devices[i]->type) {
1428 case MOUSE: {
1429 XF86ConfInputPtr mouse =
1430 MouseConfig(computer.devices[i]->config);
1432 if (mouse != NULL && computer.devices[i]->config == NULL) {
1433 XF86Config->conf_input_lst =
1434 xf86addInput(XF86Config->conf_input_lst,
1435 mouse);
1436 computer.devices[i]->config = (XtPointer)mouse;
1438 SetTip(computer.devices[i]);
1439 } break;
1440 case KEYBOARD: {
1441 XF86ConfInputPtr keyboard =
1442 KeyboardConfig(computer.devices[i]->config);
1444 if (keyboard != NULL && computer.devices[i]->config == NULL) {
1445 XF86Config->conf_input_lst =
1446 xf86addInput(XF86Config->conf_input_lst,
1447 keyboard);
1448 computer.devices[i]->config = (XtPointer)keyboard;
1450 SetTip(computer.devices[i]);
1451 } break;
1452 case CARD: {
1453 XF86ConfDevicePtr card =
1454 CardConfig(computer.devices[i]->config);
1456 if (card != NULL && computer.devices[i]->config == NULL) {
1457 XF86Config->conf_device_lst =
1458 xf86addDevice(XF86Config->conf_device_lst,
1459 card);
1460 computer.devices[i]->config = (XtPointer)card;
1462 SetTip(computer.devices[i]);
1463 for (j = 0; j < computer.num_screens; j++)
1464 if (computer.screens[j]->card->widget == config)
1465 SetTip((xf86cfgDevice*)computer.screens[j]);
1466 } break;
1467 case MONITOR: {
1468 XF86ConfMonitorPtr monitor =
1469 MonitorConfig(computer.devices[i]->config);
1471 if (monitor != NULL && computer.devices[i]->config == NULL) {
1472 XF86Config->conf_monitor_lst =
1473 xf86addMonitor(XF86Config->conf_monitor_lst,
1474 monitor);
1475 computer.devices[i]->config = (XtPointer)monitor;
1477 SetTip(computer.devices[i]);
1478 for (j = 0; j < computer.num_screens; j++)
1479 if (computer.screens[j]->monitor->widget == config)
1480 SetTip((xf86cfgDevice*)computer.screens[j]);
1481 } break;
1483 /* Need to update because it may have been renamed */
1484 UpdateMenuDeviceList(computer.devices[i]->type);
1485 break;
1489 else if (config_mode == CONFIG_SCREEN) {
1490 for (i = 0; i < computer.num_screens; i++)
1491 if (computer.screens[i]->widget == config) {
1492 if (ScreenConfig(computer.screens[i]->screen) != NULL)
1493 SetTip((xf86cfgDevice*)computer.screens[i]);
1498 void
1499 OptionsCallback(Widget w, XtPointer user_data, XtPointer call_data)
1501 int i;
1502 XF86OptionPtr *options = NULL;
1503 #ifdef USE_MODULES
1504 xf86cfgModuleOptions *drv_opts = NULL;
1505 #endif
1507 if (config_mode == CONFIG_SCREEN) {
1508 for (i = 0; i < computer.num_screens; i++)
1509 if (computer.screens[i]->widget == config) {
1510 options = &(computer.screens[i]->screen->scrn_option_lst);
1511 break;
1514 else {
1515 for (i = 0; i < computer.num_devices; i++)
1516 if (computer.devices[i]->widget == config)
1517 break;
1519 if (i >= computer.num_devices) {
1520 if (XF86Config->conf_flags == NULL)
1521 XF86Config->conf_flags = (XF86ConfFlagsPtr)
1522 XtCalloc(1, sizeof(XF86ConfFlagsRec));
1523 options = &(XF86Config->conf_flags->flg_option_lst);
1525 else {
1526 switch (computer.devices[i]->type) {
1527 case MOUSE:
1528 case KEYBOARD:
1529 options = (XF86OptionPtr*)&(((XF86ConfInputPtr)
1530 (computer.devices[i]->config))->inp_option_lst);
1531 #ifdef USE_MODULES
1532 if (!nomodules) {
1533 char *drv = ((XF86ConfInputPtr)
1534 (computer.devices[i]->config))->inp_driver;
1536 if (drv) {
1537 drv_opts = module_options;
1538 while (drv_opts) {
1539 if (drv_opts->type == InputModule &&
1540 strcmp(drv_opts->name, drv) == 0)
1541 break;
1542 drv_opts = drv_opts->next;
1546 #endif
1548 break;
1549 case CARD:
1550 options = (XF86OptionPtr*)&(((XF86ConfDevicePtr)
1551 (computer.devices[i]->config))->dev_option_lst);
1552 #ifdef USE_MODULES
1553 if (!nomodules) {
1554 char *drv = ((XF86ConfDevicePtr)
1555 (computer.devices[i]->config))->dev_driver;
1557 if (drv) {
1558 drv_opts = module_options;
1559 while (drv_opts) {
1560 if (drv_opts->type == VideoModule &&
1561 strcmp(drv_opts->name, drv) == 0)
1562 break;
1563 drv_opts = drv_opts->next;
1567 #endif
1568 break;
1569 case MONITOR:
1570 options = (XF86OptionPtr*)&(((XF86ConfMonitorPtr)
1571 (computer.devices[i]->config))->mon_option_lst);
1572 break;
1577 #ifdef USE_MODULES
1578 OptionsPopup(options, drv_opts ? drv_opts->name : NULL,
1579 drv_opts ? drv_opts->option : NULL);
1580 #else
1581 OptionsPopup(options);
1582 #endif
1583 if (config_mode == CONFIG_SCREEN) {
1584 XF86OptionPtr option, options;
1585 int rotate = 0;
1587 options = computer.screens[i]->screen->scrn_option_lst;
1588 if ((option = xf86findOption(options, "Rotate")) != NULL) {
1589 if (option->opt_val != NULL)
1590 rotate = strcasecmp(option->opt_val, "CW") == 0 ? 1 :
1591 strcasecmp(option->opt_val, "CCW") == 0 ? -1 : 0;
1592 XtFree(option->opt_val);
1593 option->opt_val = XtNewString(rotate > 0 ? "CW" : "CCW");
1594 computer.screens[i]->rotate = rotate;
1596 else
1597 computer.screens[i]->rotate = 0;
1598 UpdateScreenUI();
1599 AdjustScreenUI();
1600 SetTip((xf86cfgDevice*)computer.screens[i]);
1602 else {
1603 if (i >= computer.num_devices)
1604 SetTip(&cpu_device);
1605 else
1606 SetTip(computer.devices[i]);
1610 void
1611 EnableDeviceCallback(Widget w, XtPointer user_data, XtPointer call_data)
1613 int i;
1615 if (config_mode == CONFIG_SCREEN) {
1616 for (i = 0; i < computer.num_screens; i++)
1617 if (computer.screens[i]->widget == config) {
1618 computer.screens[i]->state = USED;
1619 computer.screens[i]->card->state = USED;
1620 ScreenSetup(False);
1621 return;
1625 for (i = 0; i < computer.num_devices; i++)
1626 if (computer.devices[i]->widget == config) {
1627 if (computer.devices[i]->state == USED)
1628 return;
1629 computer.devices[i]->state = USED;
1630 DrawCables();
1631 break;
1633 if (i >= computer.num_devices || computer.layout == NULL)
1634 return;
1635 switch (computer.devices[i]->type) {
1636 case MOUSE:
1637 case KEYBOARD: {
1638 int nmouses = 0, nkeyboards = 0;
1639 XF86ConfInputPtr input = (XF86ConfInputPtr)
1640 (computer.devices[i]->config);
1641 XF86ConfInputrefPtr nex, iref = computer.layout->lay_input_lst;
1642 XF86OptionPtr option;
1644 nex = iref;
1645 while (nex != NULL) {
1646 if (strcasecmp(nex->iref_inputdev->inp_driver, "mouse") == 0)
1647 ++nmouses;
1648 else if (IS_KBDDRIV(nex->iref_inputdev->inp_driver))
1649 ++nkeyboards;
1650 iref = nex;
1651 nex = (XF86ConfInputrefPtr)(nex->list.next);
1653 nex = (XF86ConfInputrefPtr)XtCalloc(1, sizeof(XF86ConfInputrefRec));
1654 nex->list.next = NULL;
1655 nex->iref_inputdev = input;
1656 nex->iref_inputdev_str = XtNewString(input->inp_identifier);
1657 if (nmouses == 0 && computer.devices[i]->type == MOUSE)
1658 option = xf86newOption(XtNewString("CorePointer"), NULL);
1659 else if (nkeyboards == 0 && computer.devices[i]->type == KEYBOARD)
1660 option = xf86newOption(XtNewString("CoreKeyboard"), NULL);
1661 else
1662 option = xf86newOption(XtNewString("SendCoreEvents"), NULL);
1663 nex->iref_option_lst = option;
1664 computer.layout->lay_input_lst =
1665 xf86addInputref(computer.layout->lay_input_lst, nex);
1666 } break;
1667 case CARD:
1668 for (i = 0; i < computer.num_screens; i++) {
1669 if (computer.screens[i]->card->widget == config &&
1670 computer.screens[i]->state != USED) {
1671 XF86ConfAdjacencyPtr adj;
1673 adj = (XF86ConfAdjacencyPtr)
1674 XtCalloc(1, sizeof(XF86ConfAdjacencyRec));
1675 adj->adj_screen = computer.screens[i]->screen;
1676 adj->adj_screen_str = XtNewString(computer.screens[i]->
1677 screen->scrn_identifier);
1678 computer.layout->lay_adjacency_lst = (XF86ConfAdjacencyPtr)
1679 xf86addListItem((GenericListPtr)computer.layout->
1680 lay_adjacency_lst, (GenericListPtr)adj);
1681 computer.screens[i]->state = USED;
1684 break;
1685 case MONITOR:
1686 break;
1690 void
1691 DisableDeviceCallback(Widget w, XtPointer user_data, XtPointer call_data)
1693 int i;
1695 if (config_mode == CONFIG_SCREEN) {
1696 for (i = 0; i < computer.num_screens; i++)
1697 if (computer.screens[i]->widget == config) {
1698 computer.screens[i]->state = UNUSED;
1699 computer.screens[i]->card->state = UNUSED;
1700 ScreenSetup(False);
1701 return;
1705 for (i = 0; i < computer.num_devices; i++)
1706 if (computer.devices[i]->widget == config) {
1707 if (computer.devices[i]->state == UNUSED)
1708 return;
1709 computer.devices[i]->state = UNUSED;
1710 DrawCables();
1711 break;
1713 if (i >= computer.num_devices || computer.layout == NULL)
1714 return;
1715 switch (computer.devices[i]->type) {
1716 case MOUSE:
1717 case KEYBOARD:
1718 xf86removeInputRef(computer.layout,
1719 (XF86ConfInputPtr)(computer.devices[i]->config));
1720 break;
1721 case CARD: {
1722 XF86ConfAdjacencyPtr adj;
1723 int j;
1725 if (computer.layout == NULL)
1726 break;
1727 for (j = 0; j < computer.num_screens; j++)
1728 if (computer.screens[j]->card->widget == config) {
1729 adj = computer.layout->lay_adjacency_lst;
1730 while (adj != NULL) {
1731 if (adj->adj_screen == computer.screens[j]->screen) {
1732 xf86removeAdjacency(computer.layout, adj);
1733 break;
1735 adj = (XF86ConfAdjacencyPtr)(adj->list.next);
1737 computer.screens[j]->state = UNUSED;
1738 break;
1740 } break;
1741 case MONITOR:
1742 break;
1746 /* ARGSUSED */
1747 void
1748 RemoveDeviceCallback(Widget w, XtPointer user_data, XtPointer call_data)
1750 int i, j;
1752 for (i = 0; i < computer.num_screens; i++)
1753 if (computer.screens[i]->widget == config) {
1754 RemoveScreen(computer.screens[i]->monitor,
1755 computer.screens[i]->card);
1756 ScreenSetup(False);
1757 return;
1760 for (i = 0; i < computer.num_devices; i++) {
1761 if (computer.devices[i]->widget == config) {
1762 switch (computer.devices[i]->type) {
1763 case MOUSE:
1764 case KEYBOARD:
1765 xf86removeInput(XF86Config,
1766 (XF86ConfInputPtr)(computer.devices[i]->config));
1767 break;
1768 case CARD:
1769 case MONITOR:
1770 break;
1773 if (computer.devices[i]->type == CARD) {
1774 for (j = 0; j < computer.num_screens; j++)
1775 if (computer.screens[j]->card == computer.devices[i]) {
1776 RemoveScreen(computer.screens[j]->monitor,
1777 computer.devices[i]);
1778 --j;
1780 if (computer.devices[i]->refcount <= 0)
1781 xf86removeDevice(XF86Config,
1782 (XF86ConfDevicePtr)(computer.devices[i]->config));
1784 else if (computer.devices[i]->type == MONITOR) {
1785 for (j = 0; j < computer.num_screens; j++)
1786 if (computer.screens[j]->monitor == computer.devices[i]) {
1787 RemoveScreen(computer.devices[i],
1788 computer.screens[j]->card);
1789 --j;
1791 if (computer.devices[i]->refcount <= 0)
1792 xf86removeMonitor(XF86Config,
1793 (XF86ConfMonitorPtr)(computer.devices[i]->config));
1796 if (computer.devices[i]->refcount <= 0) {
1797 int type = computer.devices[i]->type;
1799 XtDestroyWidget(computer.devices[i]->widget);
1800 XtFree((XtPointer)computer.devices[i]);
1801 if (--computer.num_devices > i)
1802 memmove(&computer.devices[i], &computer.devices[i + 1],
1803 (computer.num_devices - i) * sizeof(xf86cfgDevice*));
1805 DrawCables();
1806 UpdateMenuDeviceList(type);
1809 break;
1814 void
1815 UpdateMenuDeviceList(int type)
1817 Widget sme = NULL, menu = NULL;
1818 int i, count;
1819 static char *mouseM = "mouseM", *keyboardM = "keyboardM",
1820 *cardM = "cardM", *monitorM = "monitorM";
1822 for (i = count = 0; i < computer.num_devices; i++)
1823 if (computer.devices[i]->type == type)
1824 ++count;
1826 switch (type) {
1827 case MOUSE:
1828 sme = mouseSme;
1829 menu = mouseMenu;
1830 break;
1831 case KEYBOARD:
1832 sme = keyboardSme;
1833 menu = keyboardMenu;
1834 break;
1835 case CARD:
1836 sme = cardSme;
1837 menu = cardMenu;
1838 break;
1839 case MONITOR:
1840 sme = monitorSme;
1841 menu = monitorMenu;
1842 break;
1845 if (menu)
1846 for (i = ((CompositeWidget)menu)->composite.num_children - 1; i >= 0; i--)
1847 XtDestroyWidget(((CompositeWidget)menu)->composite.children[i]);
1849 if (count < 2) {
1850 XtVaSetValues(sme, XtNmenuName, NULL, XtNleftBitmap, None, NULL);
1851 return;
1854 switch (type) {
1855 case MOUSE:
1856 if (mouseMenu == NULL)
1857 menu = mouseMenu =
1858 XtCreatePopupShell(mouseM, simpleMenuWidgetClass,
1859 XtParent(mouseSme), NULL, 0);
1860 XtVaSetValues(mouseSme, XtNmenuName, mouseM,
1861 XtNleftBitmap, menuPixmap, NULL);
1862 break;
1863 case KEYBOARD:
1864 if (keyboardMenu == NULL)
1865 menu = keyboardMenu =
1866 XtCreatePopupShell(keyboardM, simpleMenuWidgetClass,
1867 XtParent(keyboardSme), NULL, 0);
1868 XtVaSetValues(keyboardSme, XtNmenuName, keyboardM,
1869 XtNleftBitmap, menuPixmap, NULL);
1870 break;
1871 case CARD:
1872 if (cardMenu == NULL)
1873 menu = cardMenu =
1874 XtCreatePopupShell(cardM, simpleMenuWidgetClass,
1875 XtParent(cardSme), NULL, 0);
1876 XtVaSetValues(cardSme, XtNmenuName, cardM,
1877 XtNleftBitmap, menuPixmap, NULL);
1878 break;
1879 case MONITOR:
1880 if (monitorMenu == NULL)
1881 menu = monitorMenu =
1882 XtCreatePopupShell(monitorM, simpleMenuWidgetClass,
1883 XtParent(monitorSme), NULL, 0);
1884 XtVaSetValues(monitorSme, XtNmenuName, monitorM,
1885 XtNleftBitmap, menuPixmap, NULL);
1886 break;
1889 for (i = 0; i < computer.num_devices; i++)
1890 if (computer.devices[i]->type == type) {
1891 char *label = NULL;
1893 if (computer.devices[i]->config) {
1894 switch (type) {
1895 case MOUSE:
1896 case KEYBOARD:
1897 label = ((XF86ConfInputPtr)computer.devices[i]->config)
1898 ->inp_identifier;
1899 break;
1900 case CARD:
1901 label = ((XF86ConfDevicePtr)computer.devices[i]->config)
1902 ->dev_identifier;
1903 break;
1904 case MONITOR:
1905 label = ((XF86ConfMonitorPtr)computer.devices[i]->config)
1906 ->mon_identifier;
1907 break;
1910 else {
1911 switch (type) {
1912 case MOUSE:
1913 label = "newMouse";
1914 break;
1915 case KEYBOARD:
1916 label = "newKeyboard";
1917 break;
1918 case CARD:
1919 label = "newCard";
1920 break;
1921 case MONITOR:
1922 label = "newMonitor";
1923 break;
1927 sme = XtCreateManagedWidget(label, smeBSBObjectClass, menu, NULL, 0);
1928 XtAddCallback(sme, XtNcallback, SmeConfigureDeviceCallback,
1929 computer.devices[i]->config ?
1930 computer.devices[i]->config :
1931 (XtPointer) (-((long)type + 100)));
1935 /*ARGSUSED*/
1936 void
1937 SelectDeviceAction(Widget w, XEvent *event, String *params, Cardinal *num_params)
1939 device = w;
1940 xpos = event->xbutton.x_root;
1941 ypos = event->xbutton.y_root;
1942 XDefineCursor(XtDisplay(device), XtWindow(device), no_cursor);
1944 if (config_mode == CONFIG_SCREEN) {
1945 sxpos = device->core.x;
1946 sypos = device->core.y;
1950 /*ARGSUSED*/
1951 void
1952 MoveDeviceAction(Widget w, XEvent *event, String *params, Cardinal *num_params)
1954 int dx, dy, x, y, oldx, oldy;
1956 if (device == NULL || device != w)
1957 return;
1959 dx = event->xbutton.x_root - xpos;
1960 dy = event->xbutton.y_root - ypos;
1962 oldx = device->core.x;
1963 oldy = device->core.y;
1964 x = device->core.x + dx;
1965 y = device->core.y + dy;
1967 if (x < 0)
1968 x = 0;
1969 else if (x + device->core.width > XtParent(device)->core.width)
1970 x = XtParent(device)->core.width - device->core.width;
1971 if (y < 0)
1972 y = 0;
1973 else if (y + device->core.height > XtParent(device)->core.height)
1974 y = XtParent(device)->core.height - device->core.height;
1976 dx = x - oldx;
1977 dy = y - oldy;
1979 XRaiseWindow(XtDisplay(device), XtWindow(device));
1980 XtMoveWidget(device, x, y);
1982 xpos += dx;
1983 ypos += dy;
1986 /*ARGSUSED*/
1987 void
1988 UnselectDeviceAction(Widget w, XEvent *ev, String *params, Cardinal *num_params)
1990 if (device != NULL) {
1991 XUndefineCursor(XtDisplay(device), XtWindow(device));
1993 if (config_mode == CONFIG_SCREEN)
1994 ScreenSetup(False);
1995 device = NULL;
1999 /*ARGSUSED*/
2000 void
2001 DevicePopupMenu(Widget w, XEvent *event, String *params, Cardinal *num_params)
2003 static Widget configure, options, enable, disable, remove;
2004 static int first = 1;
2005 int i;
2006 xf86cfgDevice *dev;
2008 if (first) {
2009 first = 0;
2011 popup = XtCreatePopupShell("popup", simpleMenuWidgetClass,
2012 toplevel, NULL, 0);
2013 configure = XtCreateManagedWidget("configure", smeBSBObjectClass,
2014 popup, NULL, 0);
2015 XtAddCallback(configure, XtNcallback, ConfigureDeviceCallback, NULL);
2016 options = XtCreateManagedWidget("options", smeBSBObjectClass,
2017 popup, NULL, 0);
2018 XtAddCallback(options, XtNcallback, OptionsCallback, NULL);
2019 XtCreateManagedWidget("line", smeLineObjectClass,
2020 popup, NULL, 0);
2021 enable = XtCreateManagedWidget("enable", smeBSBObjectClass,
2022 popup, NULL, 0);
2023 XtAddCallback(enable, XtNcallback, EnableDeviceCallback, NULL);
2024 disable = XtCreateManagedWidget("disable", smeBSBObjectClass,
2025 popup, NULL, 0);
2026 XtAddCallback(disable, XtNcallback, DisableDeviceCallback, NULL);
2027 XtCreateManagedWidget("line", smeLineObjectClass,
2028 popup, NULL, 0);
2029 remove = XtCreateManagedWidget("remove", smeBSBObjectClass,
2030 popup, NULL, 0);
2031 XtAddCallback(remove, XtNcallback, RemoveDeviceCallback, NULL);
2033 XtRealizeWidget(popup);
2036 dev = NULL;
2037 if (config_mode == CONFIG_LAYOUT) {
2038 for (i = 0; i < computer.num_devices; i++)
2039 if (computer.devices[i]->widget == w) {
2040 dev = computer.devices[i];
2041 break;
2043 if (i >= computer.num_devices && strcmp(XtName(w), "cpu"))
2044 return;
2045 if (dev == NULL)
2046 dev = &cpu_device;
2048 else if (config_mode == CONFIG_SCREEN) {
2049 for (i = 0; i < computer.num_screens; i++)
2050 if (computer.screens[i]->widget == w) {
2051 dev = (xf86cfgDevice*)computer.screens[i];
2052 break;
2055 if (dev == NULL)
2056 return;
2058 config = w;
2060 if (dev->type != SERVER) {
2061 XtSetSensitive(configure, True);
2062 XtSetSensitive(remove, True);
2063 XtSetSensitive(options, dev->config != NULL);
2064 if (computer.layout == NULL || dev->config == NULL ||
2065 dev->type == MONITOR) {
2066 XtSetSensitive(enable, False);
2067 XtSetSensitive(disable, False);
2069 else if (dev->state == USED) {
2070 XtSetSensitive(enable, False);
2071 XtSetSensitive(disable, True);
2073 else {
2074 XtSetSensitive(enable, True);
2075 XtSetSensitive(disable, False);
2078 else {
2079 XtSetSensitive(configure, False);
2080 XtSetSensitive(options, True);
2081 XtSetSensitive(enable, False);
2082 XtSetSensitive(disable, False);
2083 XtSetSensitive(remove, False);
2086 XtMoveWidget(popup, event->xbutton.x_root, event->xbutton.y_root);
2087 XtPopup(popup, XtGrabNone);
2090 /*ARGSUSED*/
2091 void
2092 DevicePopdownMenu(Widget w, XEvent *event, String *params, Cardinal *num_params)
2094 if (popup && XtIsRealized(popup))
2095 XtPopdown(popup);
2098 void RenameLayoutAction(Widget w, XEvent *event,
2099 String *params, Cardinal *num_params)
2101 XF86ConfLayoutPtr lay = XF86Config->conf_layout_lst;
2102 Arg args[1];
2103 char *name;
2105 XtSetArg(args[0], XtNstring, &name);
2106 XtGetValues(layout, args, 1);
2108 if (computer.layout == NULL || (computer.layout &&
2109 strcasecmp(name, computer.layout->lay_identifier)) == 0)
2110 return;
2112 if (name == NULL && *name == '\0') {
2113 /* tell user about error */
2114 return;
2117 while (lay) {
2118 if (strcasecmp(name, lay->lay_identifier) == 0)
2119 /* tell user about error */
2120 return;
2121 lay = (XF86ConfLayoutPtr)(lay->list.next);
2124 XtSetArg(args[0], XtNlabel, name);
2125 XtSetValues(layoutsme, args, 1);
2126 xf86renameLayout(XF86Config, computer.layout, name);
2129 /*ARGSUSED*/
2130 static void
2131 ComputerEventHandler(Widget w, XtPointer closure,
2132 XEvent *event, Boolean *continue_to_dispatch)
2134 if (event->xexpose.count > 1)
2135 return;
2137 if (config_mode == CONFIG_LAYOUT)
2138 DrawCables();
2141 void
2142 DrawCables(void)
2144 Display *display;
2145 Window window;
2146 int ox, oy, i;
2147 xf86cfgScreen **scr = computer.screens;
2149 if (config_mode != CONFIG_LAYOUT)
2150 return;
2152 ox = computer.cpu->core.x + (computer.cpu->core.width >> 1);
2153 oy = computer.cpu->core.y + (computer.cpu->core.height >> 1);
2155 display = XtDisplay(work);
2156 window = XtWindow(work);
2157 XClearWindow(display, window);
2159 for (i = 0; i < computer.num_devices; i++) {
2160 if (computer.devices[i]->state == USED &&
2161 computer.devices[i]->type != MONITOR)
2162 DrawCable(display, window, ox, oy,
2163 computer.devices[i]->widget->core.x +
2164 (computer.devices[i]->widget->core.width>>1),
2165 computer.devices[i]->widget->core.y +
2166 (computer.devices[i]->widget->core.height>>1));
2169 for (i = 0; i < computer.num_screens; i++) {
2170 if (scr[i]->monitor != NULL)
2171 DrawCable(display, window,
2172 scr[i]->card->widget->core.x +
2173 (scr[i]->card->widget->core.width>>1),
2174 scr[i]->card->widget->core.y +
2175 (scr[i]->card->widget->core.height>>1),
2176 scr[i]->monitor->widget->core.x +
2177 (scr[i]->monitor->widget->core.width>>1),
2178 scr[i]->monitor->widget->core.y +
2179 (scr[i]->monitor->widget->core.height>>1));
2183 static void
2184 DrawCable(Display *display, Window window, int o_x, int o_y, int d_x, int d_y)
2186 XDrawLine(display, window, cablegcshadow, o_x, o_y, d_x, d_y);
2187 XDrawLine(display, window, cablegc, o_x, o_y, d_x, d_y);
2190 /*ARGSUSED*/
2191 void
2192 SetConfigModeCallback(Widget w, XtPointer user_data, XtPointer call_data)
2194 int i, mode = (long)user_data;
2195 Arg args[3];
2196 char *ptr;
2197 static Dimension height;
2199 if (mode == config_mode)
2200 return;
2201 XtSetArg(args[0], XtNlabel, &ptr);
2202 XtGetValues(w, args, 1);
2203 XtSetArg(args[0], XtNlabel, ptr);
2204 XtSetValues(topMenu, args, 1);
2206 if (config_mode == CONFIG_LAYOUT) {
2207 XtSetArg(args[0], XtNheight, &height);
2208 XtGetValues(commands, args, 1);
2209 for (i = 0; i < computer.num_devices; i++)
2210 XtUnmapWidget(computer.devices[i]->widget);
2211 XtUnmapWidget(commands);
2212 XtUnmapWidget(computer.cpu);
2213 XtSetSensitive(commands, False);
2214 XtSetArg(args[0], XtNheight, 1);
2215 XtSetArg(args[1], XtNmin, 1);
2216 XtSetArg(args[2], XtNmax, 1);
2217 XtSetValues(commands, args, 3);
2219 else if (config_mode == CONFIG_SCREEN) {
2220 for (i = 0; i < computer.num_screens; i++)
2221 XtUnmapWidget(computer.screens[i]->widget);
2223 else if (config_mode == CONFIG_MODELINE) {
2224 VideoModeConfigureEnd();
2225 XtSetSensitive(layout, True);
2226 XtSetSensitive(layoutm, True);
2228 else if (config_mode == CONFIG_ACCESSX) {
2229 AccessXConfigureEnd();
2230 XtSetSensitive(layout, True);
2231 XtSetSensitive(layoutm, True);
2234 config_mode = mode;
2235 XClearWindow(XtDisplay(work), XtWindow(work));
2236 if (mode == CONFIG_LAYOUT) {
2237 for (i = 0; i < computer.num_devices; i++)
2238 XtMapWidget(computer.devices[i]->widget);
2239 XtSetArg(args[0], XtNheight, height);
2240 XtSetArg(args[1], XtNmin, height);
2241 XtSetArg(args[2], XtNmax, height);
2242 XtSetValues(commands, args, 3);
2243 XtMapWidget(commands);
2244 XtMapWidget(computer.cpu);
2245 XtSetSensitive(commands, True);
2246 DrawCables();
2248 else if (mode == CONFIG_SCREEN) {
2249 for (i = 0; i < computer.num_screens; i++)
2250 XtMapWidget(computer.screens[i]->widget);
2251 ScreenSetup(True);
2253 else if (mode == CONFIG_MODELINE) {
2254 VideoModeConfigureStart();
2255 XtSetSensitive(layout, False);
2256 XtSetSensitive(layoutm, False);
2258 else if (mode == CONFIG_ACCESSX) {
2259 AccessXConfigureStart();
2260 XtSetSensitive(layout, False);
2261 XtSetSensitive(layoutm, False);
2265 static void
2266 ScreenSetup(Bool check)
2268 if (check) {
2269 int i;
2271 for (i = 0; i < computer.num_layouts; i++)
2272 if (computer.layouts[i]->layout == computer.layout)
2273 break;
2275 /* Just to put the screens in the correct positions */
2276 if (i >= computer.num_layouts)
2277 AdjustScreenUI();
2280 UpdateScreenUI();
2281 AdjustScreenUI();