From 41e20ee4bc01576d23fb8fd4f875385ce57eb36a Mon Sep 17 00:00:00 2001 From: Alexander Gramiak Date: Sat, 13 Apr 2019 08:59:07 -0600 Subject: [PATCH] Add terminal hook defined_color_hook * src/termhooks.h (defined_color_hook): New terminal hook. * src/xterm.c: * src/nsterm.m: * src/term.c: * src/w32term.c: Set defined_color_hook. * src/xfaces.c: Use defined_color_hook. (defined_color): Remove. * src/image.c: Remove redefinitions of x_defined_color, and use defined_color_hook. --- src/dispextern.h | 3 +++ src/image.c | 50 +++++++++++++++++++++++++++++------------ src/nsterm.m | 1 + src/term.c | 2 ++ src/termhooks.h | 12 ++++++++++ src/w32fns.c | 10 ++++----- src/w32term.c | 5 +++-- src/w32term.h | 4 ++-- src/xfaces.c | 58 ++++++++++++++++-------------------------------- src/xfns.c | 8 +++---- src/xterm.c | 1 + src/xterm.h | 2 +- 12 files changed, 89 insertions(+), 67 deletions(-) diff --git a/src/dispextern.h b/src/dispextern.h index 6c1cdf3e2d3..4d6d0371d38 100644 --- a/src/dispextern.h +++ b/src/dispextern.h @@ -3413,6 +3413,9 @@ void x_free_colors (struct frame *, unsigned long *, int); void update_face_from_frame_parameter (struct frame *, Lisp_Object, Lisp_Object); +extern bool tty_defined_color (struct frame *f, const char *, XColor *, bool, + bool); + Lisp_Object tty_color_name (struct frame *, int); void clear_face_cache (bool); unsigned long load_color (struct frame *, struct face *, Lisp_Object, diff --git a/src/image.c b/src/image.c index 1cf90c88c5c..0023b9369c1 100644 --- a/src/image.c +++ b/src/image.c @@ -89,8 +89,6 @@ typedef struct w32_bitmap_record Bitmap_Record; #define PIX_MASK_RETAIN 0 #define PIX_MASK_DRAW 1 -#define x_defined_color w32_defined_color - #endif /* HAVE_NTGUI */ #ifdef HAVE_NS @@ -101,8 +99,6 @@ typedef struct ns_bitmap_record Bitmap_Record; #define PIX_MASK_RETAIN 0 -#define x_defined_color(f, name, color_def, alloc) \ - ns_defined_color (f, name, color_def, alloc, 0) #endif /* HAVE_NS */ #if (defined HAVE_X_WINDOWS \ @@ -1424,7 +1420,11 @@ image_alloc_image_color (struct frame *f, struct image *img, eassert (STRINGP (color_name)); - if (x_defined_color (f, SSDATA (color_name), &color, 1) + if (FRAME_TERMINAL (f)->defined_color_hook (f, + SSDATA (color_name), + &color, + true, + false) && img->ncolors < min (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *img->colors, INT_MAX)) { @@ -4470,8 +4470,8 @@ xpm_load_image (struct frame *f, { if (xstrcasecmp (SSDATA (XCDR (specified_color)), "None") == 0) color_val = Qt; - else if (x_defined_color (f, SSDATA (XCDR (specified_color)), - &cdef, 0)) + else if (FRAME_TERMINAL (f)->defined_color_hook + (f, SSDATA (XCDR (specified_color)), &cdef, false, false)) color_val = make_fixnum (cdef.pixel); } } @@ -4479,7 +4479,8 @@ xpm_load_image (struct frame *f, { if (xstrcasecmp (max_color, "None") == 0) color_val = Qt; - else if (x_defined_color (f, max_color, &cdef, 0)) + else if (FRAME_TERMINAL (f)->defined_color_hook + (f, max_color, &cdef, false, false)) color_val = make_fixnum (cdef.pixel); } if (!NILP (color_val)) @@ -5681,7 +5682,11 @@ pbm_load (struct frame *f, struct image *img) #ifdef USE_CAIRO if (! fmt[PBM_FOREGROUND].count || ! STRINGP (fmt[PBM_FOREGROUND].value) - || ! x_defined_color (f, SSDATA (fmt[PBM_FOREGROUND].value), &xfg, 0)) + || ! FRAME_TERMINAL (f)->defined_color_hook (f, + SSDATA (fmt[PBM_FOREGROUND].value), + &xfg, + false, + false)) { xfg.pixel = fg; x_query_colors (f, &xfg, 1); @@ -5690,7 +5695,11 @@ pbm_load (struct frame *f, struct image *img) if (! fmt[PBM_BACKGROUND].count || ! STRINGP (fmt[PBM_BACKGROUND].value) - || ! x_defined_color (f, SSDATA (fmt[PBM_BACKGROUND].value), &xbg, 0)) + || ! FRAME_TERMINAL (f)->defined_color_hook (f, + SSDATA (fmt[PBM_BACKGROUND].value), + &xbg, + false, + false)) { xbg.pixel = bg; x_query_colors (f, &xbg, 1); @@ -6349,7 +6358,11 @@ png_load_body (struct frame *f, struct image *img, struct png_load_context *c) current frame background, ignoring any default background color set by the image. */ if (STRINGP (specified_bg) - ? x_defined_color (f, SSDATA (specified_bg), &color, false) + ? FRAME_TERMINAL (f)->defined_color_hook (f, + SSDATA (specified_bg), + &color, + false, + false) : (image_query_frame_background_color (f, &color), true)) /* The user specified `:background', use that. */ { @@ -8036,7 +8049,8 @@ gif_load (struct frame *f, struct image *img) if (STRINGP (specified_bg)) { XColor color; - if (x_defined_color (f, SSDATA (specified_bg), &color, 0)) + if (FRAME_TERMINAL (f)->defined_color_hook + (f, SSDATA (specified_bg), &color, false, false)) { uint32_t *dataptr = data32; int r = color.red/256; @@ -8797,7 +8811,11 @@ imagemagick_load_image (struct frame *f, struct image *img, specified_bg = image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP (specified_bg) - || !x_defined_color (f, SSDATA (specified_bg), &bgcolor, 0)) + || !FRAME_TERMINAL (f)->defined_color_hook (f, + SSDATA (specified_bg), + &bgcolor, + false, + false)) image_query_frame_background_color (f, &bgcolor); bg_wand = NewPixelWand (); @@ -9532,7 +9550,11 @@ svg_load_image (struct frame *f, struct image *img, char *contents, XColor background; Lisp_Object specified_bg = image_spec_value (img->spec, QCbackground, NULL); if (!STRINGP (specified_bg) - || !x_defined_color (f, SSDATA (specified_bg), &background, 0)) + || !FRAME_TERMINAL (f)->defined_color_hook (f, + SSDATA (specified_bg), + &background, + false, + false)) image_query_frame_background_color (f, &background); /* SVG pixmaps specify transparency in the last byte, so right diff --git a/src/nsterm.m b/src/nsterm.m index ae49b659b7a..1217eb8e189 100644 --- a/src/nsterm.m +++ b/src/nsterm.m @@ -5232,6 +5232,7 @@ ns_create_terminal (struct ns_display_info *dpyinfo) terminal->update_end_hook = ns_update_end; terminal->read_socket_hook = ns_read_socket; terminal->frame_up_to_date_hook = ns_frame_up_to_date; + terminal->defined_color_hook = ns_defined_color; terminal->mouse_position_hook = ns_mouse_position; terminal->get_focus_frame = ns_get_focus_frame; terminal->focus_frame_hook = ns_focus_frame; diff --git a/src/term.c b/src/term.c index 2de0a0e6649..6a8fc2ee932 100644 --- a/src/term.c +++ b/src/term.c @@ -3838,6 +3838,7 @@ clear_tty_hooks (struct terminal *terminal) terminal->update_begin_hook = 0; terminal->update_end_hook = 0; terminal->set_terminal_window_hook = 0; + terminal->defined_color_hook = 0; terminal->mouse_position_hook = 0; terminal->frame_rehighlight_hook = 0; terminal->frame_raise_lower_hook = 0; @@ -3881,6 +3882,7 @@ set_tty_hooks (struct terminal *terminal) terminal->menu_show_hook = &tty_menu_show; #endif terminal->set_terminal_window_hook = &tty_set_terminal_window; + terminal->defined_color_hook = &tty_defined_color; /* xfaces.c */ terminal->read_socket_hook = &tty_read_avail_input; /* keyboard.c */ terminal->delete_frame_hook = &tty_free_frame_resources; terminal->delete_terminal_hook = &delete_tty; diff --git a/src/termhooks.h b/src/termhooks.h index b66233cf1c5..fbc37261330 100644 --- a/src/termhooks.h +++ b/src/termhooks.h @@ -24,6 +24,7 @@ along with GNU Emacs. If not, see . */ /* Miscellanea. */ #include "lisp.h" +#include "dispextern.h" #include "systime.h" /* for Time */ struct glyph; @@ -488,6 +489,17 @@ struct terminal void (*update_end_hook) (struct frame *); void (*set_terminal_window_hook) (struct frame *, int); + /* Decide if color named COLOR_NAME is valid for the display + associated with the frame F; if so, return the RGB values in + COLOR_DEF. If ALLOC (and MAKEINDEX for NS), allocate a new + colormap cell. + + If MAKEINDEX (on NS), set COLOR_DEF pixel to ARGB. */ + bool (*defined_color_hook) (struct frame *f, const char *color_name, + XColor *color_def, + bool alloc, + bool makeIndex); + /* Multi-frame and mouse support hooks. */ /* Graphical window systems are expected to define all of the diff --git a/src/w32fns.c b/src/w32fns.c index 30e5479cdc8..525642bfaab 100644 --- a/src/w32fns.c +++ b/src/w32fns.c @@ -1173,9 +1173,9 @@ gamma_correct (struct frame *f, COLORREF *color) the selected frame; if so, return the rgb values in COLOR_DEF. If ALLOC is nonzero, allocate a new colormap cell. */ -int +bool w32_defined_color (struct frame *f, const char *color, XColor *color_def, - bool alloc_p) + bool alloc_p, bool _makeIndex) { register Lisp_Object tem; COLORREF w32_color_ref; @@ -1262,7 +1262,7 @@ w32_decode_color (struct frame *f, Lisp_Object arg, int def) /* w32_defined_color is responsible for coping with failures by looking for a near-miss. */ - if (w32_defined_color (f, SSDATA (arg), &cdef, true)) + if (w32_defined_color (f, SSDATA (arg), &cdef, true, false)) return cdef.pixel; /* defined_color failed; return an ultimate default. */ @@ -6105,7 +6105,7 @@ DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0, CHECK_STRING (color); - if (w32_defined_color (f, SSDATA (color), &foo, false)) + if (w32_defined_color (f, SSDATA (color), &foo, false, false)) return Qt; else return Qnil; @@ -6120,7 +6120,7 @@ DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0, CHECK_STRING (color); - if (w32_defined_color (f, SSDATA (color), &foo, false)) + if (w32_defined_color (f, SSDATA (color), &foo, false, false)) return list3i ((GetRValue (foo.pixel) << 8) | GetRValue (foo.pixel), (GetGValue (foo.pixel) << 8) | GetGValue (foo.pixel), (GetBValue (foo.pixel) << 8) | GetBValue (foo.pixel)); diff --git a/src/w32term.c b/src/w32term.c index fca1ef96ad0..9d050984f78 100644 --- a/src/w32term.c +++ b/src/w32term.c @@ -7159,6 +7159,7 @@ w32_create_terminal (struct w32_display_info *dpyinfo) terminal->update_end_hook = w32_update_end; terminal->read_socket_hook = w32_read_socket; terminal->frame_up_to_date_hook = w32_frame_up_to_date; + terminal->defined_color_hook = w32_defined_color; terminal->query_colors = w32_query_colors; terminal->mouse_position_hook = w32_mouse_position; terminal->get_focus_frame = w32_get_focus_frame; @@ -7262,8 +7263,8 @@ w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name) /* initialize palette with white and black */ { XColor color; - w32_defined_color (0, "white", &color, 1); - w32_defined_color (0, "black", &color, 1); + w32_defined_color (0, "white", &color, true, false); + w32_defined_color (0, "black", &color, true, false); } #ifdef WINDOWSNT diff --git a/src/w32term.h b/src/w32term.h index 6d2765961ce..d5bb08dc675 100644 --- a/src/w32term.h +++ b/src/w32term.h @@ -241,8 +241,8 @@ extern void w32_set_scroll_bar_default_height (struct frame *); extern struct w32_display_info *w32_term_init (Lisp_Object, char *, char *); -extern int w32_defined_color (struct frame *f, const char *color, - XColor *color_def, bool alloc_p); +extern bool w32_defined_color (struct frame *, const char *, XColor *, + bool, bool); extern int w32_display_pixel_height (struct w32_display_info *); extern int w32_display_pixel_width (struct w32_display_info *); extern void initialize_frame_menubar (struct frame *); diff --git a/src/xfaces.c b/src/xfaces.c index 925c3d54ff2..7e04c0e9953 100644 --- a/src/xfaces.c +++ b/src/xfaces.c @@ -893,11 +893,11 @@ tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color, return false; } -/* A version of defined_color for non-X frames. */ +/* An implementation of defined_color_hook for tty frames. */ -static bool +bool tty_defined_color (struct frame *f, const char *color_name, - XColor *color_def, bool alloc) + XColor *color_def, bool alloc, bool _makeIndex) { bool status = true; @@ -924,36 +924,6 @@ tty_defined_color (struct frame *f, const char *color_name, return status; } - -/* Decide if color named COLOR_NAME is valid for the display - associated with the frame F; if so, return the rgb values in - COLOR_DEF. If ALLOC, allocate a new colormap cell. - - This does the right thing for any type of frame. */ - -static bool -defined_color (struct frame *f, const char *color_name, XColor *color_def, - bool alloc) -{ - if (!FRAME_WINDOW_P (f)) - return tty_defined_color (f, color_name, color_def, alloc); -#ifdef HAVE_X_WINDOWS - else if (FRAME_X_P (f)) - return x_defined_color (f, color_name, color_def, alloc); -#endif -#ifdef HAVE_NTGUI - else if (FRAME_W32_P (f)) - return w32_defined_color (f, color_name, color_def, alloc); -#endif -#ifdef HAVE_NS - else if (FRAME_NS_P (f)) - return ns_defined_color (f, color_name, color_def, alloc, true); -#endif - else - emacs_abort (); -} - - /* Given the index IDX of a tty color on frame F, return its name, a Lisp string. */ @@ -998,7 +968,8 @@ face_color_gray_p (struct frame *f, const char *color_name) XColor color; bool gray_p; - if (defined_color (f, color_name, &color, false)) + if (FRAME_TERMINAL (f)->defined_color_hook + (f, color_name, &color, false, true)) gray_p = (/* Any color sufficiently close to black counts as gray. */ (color.red < 5000 && color.green < 5000 && color.blue < 5000) || @@ -1038,7 +1009,7 @@ face_color_supported_p (struct frame *f, const char *color_name, && face_color_gray_p (f, color_name))) : #endif - tty_defined_color (f, color_name, ¬_used, false); + tty_defined_color (f, color_name, ¬_used, false, false); } @@ -1082,9 +1053,10 @@ load_color2 (struct frame *f, struct face *face, Lisp_Object name, || target_index == LFACE_STRIKE_THROUGH_INDEX || target_index == LFACE_BOX_INDEX); - /* if the color map is full, defined_color will return a best match + /* if the color map is full, defined_color_hook will return a best match to the values in an existing cell. */ - if (!defined_color (f, SSDATA (name), color, true)) + if (!FRAME_TERMINAL (f)->defined_color_hook + (f, SSDATA (name), color, true, true)) { add_to_log ("Unable to load color \"%s\"", name); @@ -4235,11 +4207,19 @@ two lists of the form (RED GREEN BLUE) aforementioned. */) if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1)) && !(STRINGP (color1) - && defined_color (f, SSDATA (color1), &cdef1, false))) + && FRAME_TERMINAL (f)->defined_color_hook (f, + SSDATA (color1), + &cdef1, + false, + true))) signal_error ("Invalid color", color1); if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2)) && !(STRINGP (color2) - && defined_color (f, SSDATA (color2), &cdef2, false))) + && FRAME_TERMINAL (f)->defined_color_hook (f, + SSDATA (color2), + &cdef2, + false, + true))) signal_error ("Invalid color", color2); if (NILP (metric)) diff --git a/src/xfns.c b/src/xfns.c index c4bdb3f5cfb..2ceb55a30ad 100644 --- a/src/xfns.c +++ b/src/xfns.c @@ -653,7 +653,7 @@ gamma_correct (struct frame *f, XColor *color) bool x_defined_color (struct frame *f, const char *color_name, - XColor *color, bool alloc_p) + XColor *color, bool alloc_p, bool _makeIndex) { bool success_p = false; Colormap cmap = FRAME_X_COLORMAP (f); @@ -698,7 +698,7 @@ x_decode_color (struct frame *f, Lisp_Object color_name, int mono_color) /* x_defined_color is responsible for coping with failures by looking for a near-miss. */ - if (x_defined_color (f, SSDATA (color_name), &cdef, true)) + if (x_defined_color (f, SSDATA (color_name), &cdef, true, false)) return cdef.pixel; signal_error ("Undefined color", color_name); @@ -4110,7 +4110,7 @@ DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0, CHECK_STRING (color); - if (x_defined_color (f, SSDATA (color), &foo, false)) + if (x_defined_color (f, SSDATA (color), &foo, false, false)) return Qt; else return Qnil; @@ -4126,7 +4126,7 @@ DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0, CHECK_STRING (color); - if (x_defined_color (f, SSDATA (color), &foo, false)) + if (x_defined_color (f, SSDATA (color), &foo, false, false)) return list3i (foo.red, foo.green, foo.blue); else return Qnil; diff --git a/src/xterm.c b/src/xterm.c index a89f13955d3..d65ad98c4c6 100644 --- a/src/xterm.c +++ b/src/xterm.c @@ -13271,6 +13271,7 @@ x_create_terminal (struct x_display_info *dpyinfo) terminal->read_socket_hook = XTread_socket; terminal->frame_up_to_date_hook = XTframe_up_to_date; terminal->buffer_flipping_unblocked_hook = XTbuffer_flipping_unblocked_hook; + terminal->defined_color_hook = x_defined_color; terminal->query_colors = x_query_colors; terminal->mouse_position_hook = XTmouse_position; terminal->get_focus_frame = x_get_focus_frame; diff --git a/src/xterm.h b/src/xterm.h index 20d043e7172..f4007d6e7f3 100644 --- a/src/xterm.h +++ b/src/xterm.h @@ -1222,7 +1222,7 @@ extern void destroy_frame_xic (struct frame *); extern void xic_set_preeditarea (struct window *, int, int); extern void xic_set_statusarea (struct frame *); extern void xic_set_xfontset (struct frame *, const char *); -extern bool x_defined_color (struct frame *, const char *, XColor *, bool); +extern bool x_defined_color (struct frame *, const char *, XColor *, bool, bool); #ifdef HAVE_X_I18N extern void free_frame_xic (struct frame *); # if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT -- 2.39.2