Home | History | Annotate | Line # | Download | only in libcurses
      1  1.12       uwe /*   $NetBSD: cchar.c,v 1.12 2020/07/02 23:43:01 uwe Exp $ */
      2   1.1     blymn 
      3   1.1     blymn /*
      4   1.1     blymn  * Copyright (c) 2005 The NetBSD Foundation Inc.
      5   1.1     blymn  * All rights reserved.
      6   1.1     blymn  *
      7   1.1     blymn  * This code is derived from code donated to the NetBSD Foundation
      8   1.1     blymn  * by Ruibiao Qiu <ruibiao (at) arl.wustl.edu,ruibiao (at) gmail.com>.
      9   1.1     blymn  *
     10   1.1     blymn  *
     11   1.1     blymn  * Redistribution and use in source and binary forms, with or without
     12   1.1     blymn  * modification, are permitted provided that the following conditions
     13   1.1     blymn  * are met:
     14   1.1     blymn  * 1. Redistributions of source code must retain the above copyright
     15   1.1     blymn  *	notice, this list of conditions and the following disclaimer.
     16   1.1     blymn  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1     blymn  *	notice, this list of conditions and the following disclaimer in the
     18   1.1     blymn  *	documentation and/or other materials provided with the distribution.
     19   1.1     blymn  * 3. Neither the name of the NetBSD Foundation nor the names of its
     20   1.1     blymn  *	contributors may be used to endorse or promote products derived
     21   1.1     blymn  *	from this software without specific prior written permission.
     22   1.1     blymn  *
     23   1.1     blymn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     24   1.1     blymn  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     25   1.1     blymn  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     26   1.1     blymn  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1     blymn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1     blymn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1     blymn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1     blymn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1     blymn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1     blymn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1     blymn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1     blymn  * SUCH DAMAGE.
     35   1.1     blymn  */
     36   1.1     blymn 
     37   1.1     blymn #include <sys/cdefs.h>
     38   1.1     blymn #ifndef lint
     39  1.12       uwe __RCSID("$NetBSD: cchar.c,v 1.12 2020/07/02 23:43:01 uwe Exp $");
     40   1.1     blymn #endif						  /* not lint */
     41   1.1     blymn 
     42   1.1     blymn #include <string.h>
     43   1.1     blymn 
     44   1.1     blymn #include "curses.h"
     45   1.1     blymn #include "curses_private.h"
     46   1.1     blymn 
     47   1.1     blymn /*
     48   1.1     blymn  * getcchar --
     49   1.5       wiz  *	get a wide-character string and rendition from a cchar_t
     50   1.1     blymn  */
     51   1.1     blymn int
     52   1.1     blymn getcchar(const cchar_t *wcval, wchar_t *wch, attr_t *attrs,
     53   1.6       roy          short *color_pair, void *opts)
     54   1.1     blymn {
     55   1.1     blymn 	wchar_t *wp;
     56   1.3     blymn 	size_t len;
     57   1.1     blymn 
     58   1.8       uwe 	if (__predict_false(opts != NULL))
     59   1.1     blymn 		return ERR;
     60   1.1     blymn 
     61   1.8       uwe 	wp = wmemchr(wcval->vals, L'\0', CCHARW_MAX);
     62   1.8       uwe 	len = wp ? wp - wcval->vals : CCHARW_MAX;
     63   1.1     blymn 
     64   1.1     blymn 	if (wch == NULL)
     65   1.6       roy 		return (int)len;
     66  1.10       uwe 
     67  1.10       uwe 	if (attrs == NULL || color_pair == NULL)
     68   1.1     blymn 		return ERR;
     69  1.10       uwe 
     70   1.3     blymn 	if (len > 0) {
     71   1.3     blymn 		*attrs = wcval->attributes;
     72   1.7       uwe 		if (__using_color)
     73   1.7       uwe 			*color_pair = PAIR_NUMBER(wcval->attributes);
     74   1.7       uwe 		else
     75   1.7       uwe 			*color_pair = 0;
     76   1.9       uwe 		wmemcpy(wch, wcval->vals, len);
     77   1.1     blymn 		wch[len] = L'\0';
     78   1.1     blymn 	}
     79   1.1     blymn 	return OK;
     80   1.1     blymn }
     81   1.1     blymn 
     82   1.1     blymn /*
     83   1.1     blymn  * setcchar --
     84   1.5       wiz  *	set cchar_t from a wide-character string and rendition
     85   1.1     blymn  */
     86   1.1     blymn int
     87   1.1     blymn setcchar(cchar_t *wcval, const wchar_t *wch, const attr_t attrs,
     88   1.6       roy 	 short color_pair, const void *opts)
     89   1.1     blymn {
     90   1.1     blymn 	int i;
     91   1.3     blymn 	size_t len;
     92   1.1     blymn 
     93   1.8       uwe 	if (__predict_false(opts != NULL))
     94   1.8       uwe 		return ERR;
     95   1.8       uwe 
     96   1.8       uwe 	len = wcslen(wch);
     97   1.8       uwe 	if (len > CCHARW_MAX || (len > 1 && wcwidth(wch[0]) < 0))
     98   1.1     blymn 		return ERR;
     99   1.1     blymn 
    100   1.1     blymn 	/*
    101   1.1     blymn 	 * If we have a following spacing-character, stop at that point.  We
    102   1.1     blymn 	 * are only interested in adding non-spacing characters.
    103   1.1     blymn 	 */
    104   1.1     blymn 	for (i = 1; i < len; ++i) {
    105   1.1     blymn 		if (wcwidth(wch[i]) != 0) {
    106   1.1     blymn 			len = i;
    107   1.1     blymn 			break;
    108   1.1     blymn 		}
    109   1.1     blymn 	}
    110   1.1     blymn 
    111   1.1     blymn 	memset(wcval, 0, sizeof(*wcval));
    112   1.1     blymn 	if (len != 0) {
    113   1.7       uwe 		wcval->attributes = attrs & ~__COLOR;
    114   1.7       uwe 		if (__using_color && color_pair)
    115   1.7       uwe 			wcval->attributes |= COLOR_PAIR(color_pair);
    116  1.12       uwe 		wcval->elements = len;
    117   1.1     blymn 		memcpy(&wcval->vals, wch, len * sizeof(wchar_t));
    118   1.1     blymn 	}
    119   1.1     blymn 
    120   1.1     blymn 	return OK;
    121   1.1     blymn }
    122   1.4  drochner 
    123   1.4  drochner void
    124   1.4  drochner __cursesi_chtype_to_cchar(chtype in, cchar_t *out)
    125   1.4  drochner {
    126   1.4  drochner 	unsigned int idx;
    127   1.4  drochner 
    128   1.4  drochner 	if (in & __ACS_IS_WACS) {
    129   1.4  drochner 		idx = in & __CHARTEXT;
    130   1.4  drochner 		if (idx < NUM_ACS) {
    131   1.4  drochner 			memcpy(out, &_wacs_char[idx], sizeof(cchar_t));
    132   1.4  drochner 			out->attributes |= in & __ATTRIBUTES;
    133   1.4  drochner 			return;
    134   1.4  drochner 		}
    135   1.4  drochner 	}
    136   1.4  drochner 	out->vals[0] = in & __CHARTEXT;
    137   1.4  drochner 	out->attributes = in & __ATTRIBUTES;
    138   1.4  drochner 	out->elements = 1;
    139   1.4  drochner }
    140