📄 initapp.c
字号:
/* initapp.c - initialise X Server connection, X application, and widget library Copyright (C) 1996-2000 Paul Sheer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. *//* setup application *//* Colormap allocation: A colormap is allocated as follows into the array color_pixels: ('i' refers to color_pixels[i]) These are allocated in sequential palette cells, hence (for pseudocolor only) color_pixels[j + i] = color_pixels[j] + i, for j = 0,16,43. Obviously in TrueColor this is not true. ,----------+------------------------------------------. | i | colors | +----------+------------------------------------------+ | 0-15 | 16 levels of the widget colors that | | | make up button bevels etc. Starting from | | | (i=0) black (for shadowed bevels), up | | | to (i=15) bright highlighted bevels | | | those facing up-to-the-left. These | | | are sequential (see next block). | +----------+------------------------------------------+ | 16-42 | 3^3 combinations of RGB, vis. (0,0,0), | | | (0,0,127), (0,0,255), (0,127,0), ... | | | ... (255,255,255). | +----------+------------------------------------------+ | 43-106 | 64 levels of grey. (optional) | +----------+------------------------------------------+ | 107-> | For other colors. (Not used at present) | `----------+------------------------------------------' *//* Thence macros are defined in coolwidgets.h for color lookup */#include <config.h>#include <stdio.h>#include <my_string.h>#include <stdlib.h>#include <pwd.h>#include <sys/types.h>#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/Xatom.h>#include <X11/Xresource.h>#define DEF_APP_GLOB /* so that globals get defined not externed */#include "coolwidget.h"#include "xim.h"#include "stringtools.h"#include <signal.h>#include <sys/types.h>#if HAVE_SYS_WAIT_H# include <sys/wait.h>#endif#ifdef HAVE_SYS_TIME_H#include <sys/time.h>#endif#include "mad.h"static int verbose_operation = 0;CInitData *given = 0;/* defaults */#define DEFAULT_DISPLAY NULL#define DEFAULT_GEOM NULL#define DEFAULT_FONT "-*-fixed-bold-r-*--13-*-*-*-*-80-*"#define FONT_FALLBACK "-*"#define DEFAULT_BG_COLOR 0 /* not used *************/ #define DEFAULT_WIDGET_COLOR_R "0.9"#define DEFAULT_WIDGET_COLOR_G "1.1"#define DEFAULT_WIDGET_COLOR_B "1.4"#define DEFAULT_BDWIDTH 1struct look *look = 0;#ifdef NEXT_LOOKextern struct look look_next;#endifextern struct look look_cool;extern struct look look_gtk;struct resource_param { char *name; char **value;};static char *init_display = DEFAULT_DISPLAY;char *init_geometry = DEFAULT_GEOM;char *init_font = DEFAULT_FONT;char *init_widget_font = 0;char *init_bg_color = DEFAULT_BG_COLOR;char *init_fg_color_red = DEFAULT_WIDGET_COLOR_R;char *init_fg_color_green = DEFAULT_WIDGET_COLOR_G;char *init_fg_color_blue = DEFAULT_WIDGET_COLOR_B;char *init_look = "gtk";Atom ATOM_WM_PROTOCOLS, ATOM_WM_DELETE_WINDOW;Atom ATOM_WM_NAME, ATOM_WM_NORMAL_HINTS, ATOM_WM_TAKE_FOCUS;/* Resources */struct resource_param resources[] ={ {"display", &init_display}, {"geometry", &init_geometry}, {"font", &init_font}, {"widget_font", &init_widget_font}, {"fg_red", &init_fg_color_red}, {"fg_blue", &init_fg_color_blue}, {"fg_green", &init_fg_color_green}, {0, 0}};static void alloccolorerror (void){/* Translations in initapp.c are of a lower priority than other files, since they only output when cooledit is run in verbose mode */ fprintf (stderr, _ ("Cannot allocate colors. Could be to many applications\ntrying to use the colormap. If closing other apps doesn't\nhelp, then your graphics hardware may be inadequite.\n")); exit (1);}static void init_widgets (void){ int i; last_widget = 1; /*widget[0] is never used since index 0 is used to indicate an error message */ for (i = 0; i < MAX_NUMBER_OF_WIDGETS; i++) CIndex (i) = NULL; /*initialise */}static void open_display (char *app_name, int wait_for_display){ if (wait_for_display) { CDisplay = 0; while (!(CDisplay = XOpenDisplay (init_display))) sleep (1); } else { if ((CDisplay = XOpenDisplay (init_display)) == NULL) { fprintf (stderr, _ ("%s: can't open display named \"%s\"\n"), app_name, XDisplayName (init_display)); exit (1); } } CRoot = DefaultRootWindow (CDisplay); if (verbose_operation) printf (_ ("Opened display \"%s\"\n"), XDisplayName (init_display));}static void get_resources (void){ int i; char *type; XrmValue value; XrmDatabase rdb; XrmInitialize (); rdb = XrmGetFileDatabase (catstrs (getenv ("HOME"), "/.Xdefaults", 0)); if (rdb != NULL) { for (i = 0; resources[i].name; i++) { char *rname = catstrs (CAppName, "*", resources[i].name, 0); if (XrmGetResource (rdb, rname, rname, &type, &value)) { *resources[i].value = value.addr; } } }}static void load_font (void){ char f[256]; if (CPushFont ("editor", init_font)) exit (1); sprintf (f, init_widget_font, FONT_HEIGHT - 1); if (CPushFont ("widget", f)) { fprintf (stderr, _ ("%s: falling back to font %s\n"), CAppName, FONT_FALLBACK); if (CPushFont ("widget", FONT_FALLBACK)) exit (1); }}static void visual_comments (int class){ switch (class) { case PseudoColor: printf ("PseudoColor"); if (CDepth >= 7)/* 'Depth' is the number of color bits per graphics pixel */ printf (_ (" - depth ok, this will work.\n")); else/* 'Depth' is the number of color bits per graphics pixel */ printf (_ (" - depth low, this may not work.\n")); break; case GrayScale: printf ("Grayscale -\n");/* 'Visual' is the hardware method of displaying graphics */ printf (_ ("Mmmmh, haven't tried this visual class, let's see what happens.\n")); break; case DirectColor: printf ("DirectColor -\n");/* 'Visual' is the hardware method of displaying graphics */ printf (_ ("Mmmmh, haven't tried this visual class, let's see what happens.\n")); break; case StaticColor: printf ("StaticColor - ");/* "Let us attempt to use this Visual even though it may not work" */ printf (_ ("lets give it a try.\n")); break; case StaticGray: printf ("StaticGray - ");/* "Let us attempt to use this Visual even though it may not work" */ printf (_ ("lets give it a try.\n")); break; case TrueColor: printf ("TrueColor - ");/* "Adequite" (with sarcasm) : i.e. it is actually the best kind of Visual " */ printf (_ ("fine.\n")); break; default:/* 'Visual' is the method hardware method of displaying graphics */ CError (_ ("?\nVisual class unknown.\n")); break; }}/* must be free'd */XColor *get_cells (Colormap cmap, int *size){ int i; XColor *c; *size = DisplayCells (CDisplay, DefaultScreen (CDisplay)); c = CMalloc (*size * sizeof (XColor)); for (i = 0; i < *size; i++) c[i].pixel = i; XQueryColors (CDisplay, cmap, c, *size); return c;}#define BitsPerRGBofVisual(v) (v->bits_per_rgb)/* find the closest color without allocating it */int CGetCloseColor (XColor * cells, int ncells, XColor color, long *error){ unsigned long merror = (unsigned long) -1; unsigned long e; int min = 0, i; unsigned long mask = 0xFFFF0000UL; mask >>= min (BitsPerRGBofVisual (CVisual), 5); for (i = 0; i < ncells; i++) { e = 8 * abs ((int) (color.red & mask) - (cells[i].red & mask)) + 10 * abs ((int) (color.green & mask) - (cells[i].green & mask)) + 5 * abs ((int) (color.blue & mask) - (cells[i].blue & mask)); if (e < merror) { merror = e; min = i; } } merror = 8 * abs ((int) (color.red & mask) - (cells[min].red & mask)) + 10 * abs ((int) (color.green & mask) - (cells[min].green & mask)) + 5 * abs ((int) (color.blue & mask) - (cells[min].blue & mask)); if (error) *error = (long) merror; return min;}#define grey_intense(i) (i * 65535 / 63)/* return -1 if not found. Meaning that another coolwidget app is not running */int find_coolwidget_grey_scale (XColor * c, int ncells){ unsigned long mask = 0xFFFF0000UL; int i, j; mask >>= BitsPerRGBofVisual (CVisual); for (i = 0; i < ncells; i++) { for (j = 0; j < 64; j++) if (!((c[i + j].green & mask) == (grey_intense (j) & mask) && c[i + j].red == c[i + j].green && c[i + j].green == c[i + j].blue)) goto cont; return i; cont:; } return -1;}void CAllocColorCells (Colormap colormap, Bool contig, unsigned long plane_masks[], unsigned int nplanes, unsigned long pixels[], unsigned int npixels){ if (!XAllocColorCells (CDisplay, colormap, contig, plane_masks, nplanes, pixels, npixels)) alloccolorerror ();}void CAllocColor (Colormap cmap, XColor * c){ if (!XAllocColor (CDisplay, cmap, c)) alloccolorerror ();}static void get_grey_colors (XColor * color, int i){ color->red = color->green = grey_intense (i); color->blue = grey_intense (i); color->flags = DoRed | DoBlue | DoGreen;}static void get_button_color (XColor * color, int i){ (*look->get_button_color) (color, i);}int option_invert_colors = 0;int option_invert_crome = 0;int option_invert_red_green = 0;int option_invert_green_blue = 0;int option_invert_red_blue = 0;#define clip(x,a,b) ((x) >= (b) ? (b) : ((x) <= (a) ? (a) : (x)))#define cswap(a,b) {float t; t = (a); (a) = (b); (b) = t;}/* inverts the cromiance - this is for editor background colors that are very light */static int transform (int color){ float r, g, b, y, y_max, c1, c1_max, c2, c2_max; r = (float) ((color >> 16) & 0xFF); g = (float) ((color >> 8) & 0xFF); b = (float) ((color >> 0) & 0xFF); y_max = 0.3 * 240.0 + 0.6 * 240.0 + 0.1 * 240.0; if (option_invert_red_green) cswap (r, g) if (option_invert_green_blue) cswap (g, b) if (option_invert_red_blue) cswap (r, b) y = 0.3000 * r + 0.6000 * g + 0.1000 * b; c1 = -0.1500 * r - 0.3000 * g + 0.4500 * b; c2 = 0.4375 * r - 0.3750 * g - 0.0625 * b; c1_max = -0.1500 * 255.0 - 0.3000 * 255.0 + 0.4500 * 255.0; c2_max = 0.4375 * 255.0 - 0.3750 * 255.0 - 0.0625 * 255.0; if (option_invert_crome) { c1 = c1_max - c1; c2 = c2_max - c2; } if (option_invert_colors) y = y_max - y; r = 1.0 * y + 0.0000 * c1 + 1.6 * c2; g = 1.0 * y - 0.3333 * c1 - 0.8 * c2; b = 1.0 * y + 2.0000 * c1 + 0.0 * c2; r = clip (r, 0.0, 255.0); g = clip (g, 0.0, 255.0); b = clip (b, 0.0, 255.0); return (int) (((int) r) << 16) | (((int) g) << 8) | (((int) b) << 0);}/* colours */int option_color_0 = 0x080808;int option_color_1 = 0x000065;int option_color_2 = 0x0000FF;int option_color_3 = 0x008B00;int option_color_4 = 0x008B8B;int option_color_5 = 0x009ACD;int option_color_6 = 0x00FF00;int option_color_7 = 0x00FA9A;int option_color_8 = 0x00FFFF;int option_color_9 = 0x8B2500;int option_color_10 = 0x8B008B;int option_color_11 = 0x7D26CD;int option_color_12 = 0x8B7500;int option_color_13 = 0x7F7F7F;int option_color_14 = 0x7B68EE;int option_color_15 = 0x7FFF00;int option_color_16 = 0x87CEEB;int option_color_17 = 0x7FFFD4;int option_color_18 = 0xEE0000;int option_color_19 = 0xEE1289;int option_color_20 = 0xEE00EE;int option_color_21 = 0xCD6600;int option_color_22 = 0xF8B7B7;int option_color_23 = 0xE066FF;int option_color_24 = 0xEEEE00;int option_color_25 = 0xEEE685;int option_color_26 = 0xF8F8FF;/* takes 0-26 and converts it to RGB */static void get_general_colors (XColor * color, int i){ unsigned long c = 0; switch (i) { case 0: c = transform (option_color_0); break; case 1: c = transform (option_color_1); break; case 2: c = transform (option_color_2); break; case 3: c = transform (option_color_3); break; case 4: c = transform (option_color_4); break; case 5: c = transform (option_color_5); break; case 6: c = transform (option_color_6); break; case 7: c = transform (option_color_7); break; case 8: c = transform (option_color_8); break; case 9: c = transform (option_color_9); break; case 10: c = transform (option_color_10); break; case 11: c = transform (option_color_11); break; case 12: c = transform (option_color_12); break; case 13: c = transform (option_color_13); break; case 14: c = transform (option_color_14); break; case 15: c = transform (option_color_15); break; case 16: c = transform (option_color_16); break; case 17: c = transform (option_color_17);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -