How to do a ansi color codes - c

The problem is that when i typed
printf("\033[1;32mHello World\033[0m");
it prints something like this
[1;32mHello World[0m
in the console. My code is
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(){
printf("\033[1;32mHello World\033[0m");
_getch();
return 0;
}
it displays:
a box with a question mark inside->[1;32mHello World[0m
but should be a color green text color Hello World.

Maybe in this way:
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hStdout, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hStdout, dwMode);
printf("\033[1;32mHello World\033[0m");
You need to initialize the appropriate mode Windows console.

Related

Make Xorg recognize libevdev virtual device

I've been trying to create a simple program using libevdev to make a virtual device that will simply move the mouse by 50 points on the X axis every second. The program runs just fine, however Xorg won't recognize the newly created virtual device.
I suppose it's going to be something trivial, but I cannot figure out what.
Xorgs' logs say:
[ 5860.310] (II) config/udev: Adding input device test device Mouse (/dev/input/event18)
[ 5860.310] (II) No input driver specified, ignoring this device.
[ 5860.310] (II) This device may have been added with another device file.
The program:
#include <libevdev/libevdev.h>
#include <libevdev/libevdev-uinput.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static void check(int i) {
if (i < 0) {
printf("%s\n", strerror(-i));
exit(1);
}
}
int main() {
struct libevdev* evdev = libevdev_new();
libevdev_set_name(evdev, "test device Mouse");
libevdev_set_id_vendor(evdev, 0x1);
libevdev_set_id_product(evdev, 0x1);
libevdev_set_id_version(evdev, 0x1);
libevdev_set_id_bustype(evdev, BUS_USB);
check(libevdev_enable_event_type(evdev, EV_REL));
check(libevdev_enable_event_code(evdev, EV_REL, REL_X, NULL));
check(libevdev_enable_event_code(evdev, EV_REL, REL_Y, NULL));
check(libevdev_enable_event_code(evdev, EV_SYN, SYN_REPORT, NULL));
struct libevdev_uinput* uinput;
check(libevdev_uinput_create_from_device(evdev, LIBEVDEV_UINPUT_OPEN_MANAGED, &uinput));
for (int i = 0; i < 1000; i++) {
check(libevdev_uinput_write_event(uinput, EV_REL, REL_X, 50));
check(libevdev_uinput_write_event(uinput, EV_SYN, SYN_REPORT, 0));
sleep(1);
}
}
What am I doing wrong?
The issue was that mouse buttons need to be enabled for mouse movements to work.
Adding these lines fixes the issue:
check(libevdev_enable_event_type(evdev, EV_KEY));
check(libevdev_enable_event_code(evdev, EV_KEY, BTN_LEFT, NULL));
check(libevdev_enable_event_code(evdev, EV_KEY, BTN_RIGHT, NULL));

Unicode symbols

I wrote the following code to generate Unicode symbols for card suits in C. It works fine and I don't need to change the font family or the code page of the console (I use Windows 10 and Dev-C++) but it seems that I can generate only those symbols. Indeed, if I try other values to generate other symbols, like for chess, dice, domino or others as shown here unicode symbols for games, that code does not work. Why? Thank you in advance for your help.
#include <stdio.h>
#include <fcntl.h>
#define SPADE L"\u2660"
#define CLUB L"\u2663"
#define HEART L"\u2665"
#define DIAMOND L"\u2666"
enum SUIT {spade = 1, club, heart, diamond};
void printSuit(int suitToSelect) {
_setmode(_fileno(stdout), _O_U16TEXT);
switch (suitToSelect) {
case spade:
wprintf(SPADE);
break;
case club:
wprintf(CLUB);
break;
case heart:
wprintf(HEART);
break;
case diamond:
wprintf(DIAMOND);
break;
}
_setmode(_fileno(stdout), _O_TEXT);
}
int main(void)
{
printSuit(spade);
printSuit(heart);
printSuit(club);
printSuit(diamond);
printf("\n");
printf("Normal text\n");
return 0;
}
the code below works fine with Dev-C++ and Windows 10.
You can see the nice result in the screenshot of the console.
You can select different fonts in the code
to visualize unicode symbols; each font family
prints the symbols in different styles. Try it!
I hope that this answer can help somebody
who had my problem.
Thank you.
#include <windows.h>
#include <stdio.h>
int main(void)
{
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof cfi;
cfi.nFont = 0;
cfi.dwFontSize.X = 20;
cfi.dwFontSize.Y = 40;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
wcscpy(cfi.FaceName, L"Dejavu Sans Mono");
//wcscpy(cfi.FaceName, L"MS Gothic");
//wcscpy(cfi.FaceName, L"MS Mincho");
//wcscpy(cfi.FaceName, L"NSimSun");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
SetConsoleOutputCP(65001);
printf("\u2660 \u2661 \u2662 \u2663 \u2664 \u2665 \u2666 \u2667 \n");
printf("\u265B \u265C \u265D \u265E\n");
printf("\u265F \u265A \u2655 \u2656 \u2657\n");
printf("\u2658 \u2659 \u2654 \n");
printf("\u2680 \u2681 \u2682 \u2683 \u2684 \u2685\n");
printf("\u2554 \u2555 \u2556 \u2563 \u255A \u255B \u255C \u255D\n");
return 0;
}

Accessing custom XResources colors with X11

I'm just starting my first ever program using the X11 library. To start, I'm just trying to access the colors from the user's color-scheme as defined in xrdb. For example, in my ~/.Xresources I have things like:
*color8: #073642
*color0: #002b36
I've also verified that these colors show up when I run xrdb -query. In my C program so far I have:
#include <X11/Xlib.h>
#include <X11/Xresource.h>
int main (int argc, char *argv[])
{
Display* display = XOpenDisplay (0);
XrmDatabase xrdb = XrmGetDatabase (display);
XrmValue v;
Colormap cmap = DefaultColormap (display, DefaultScreen (display));
XColor screenColor;
XColor exactColor;
if (! XAllocNamedColor (display, cmap "color0", &screenColor, &exactColor))
printf ("ERROR\n");
printf ("%u %u %u\n", screenColor.red, screenColor.green, screenColor.blue);
return 0;
}
But this errors. So what am I missing? Is there a better way to do what I'm trying to do? Thanks!
When you want to access parameters set in an Xresource file loaded by xrdb, you need to
xrdb = XrmGetStringDatabase(XResourceManagerString(display));
instead of XrmGetDatabase(...). Hope that addresses (lately) your question.

C get battery life under Windows 7

I'm trying to code a program that gets the % of a laptop battery and then displays a CMD showing a message (for example: 10% -> "Low battery!").
I've tried to google it, and it seems they all tried with C++ or C#.
Can anybody help me with C, please?
Edit: thanks zakinster for your reply. Shouldn't it look something like this? This code ain't working.
#include <Windows.h>
#include <Winbase.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
SYSTEM_POWER_STATUS status;
GetSystemPowerStatus(&status);
unsigned char battery = status.BatteryLifePercent;
printf("%s", battery);
}
GetSystemPowerStatus from the Win32 API should provide the information you need :
SYSTEM_POWER_STATUS status;
if(GetSystemPowerStatus(&status)) {
unsigned char battery = status.BatteryLifePercent;
/* battery := 0..100 or 255 if unknown */
if(battery == 255) {
printf("Battery level unknown !");
}
else {
printf("Battery level : %u%%.", battery);
}
}
else {
printf("Cannot get the power status, error %lu", GetLastError());
}
See the documentation of the SYSTEM_POWER_STATUS structure for a complete list of contained information.

Magickcore can't set/get a single pixel

I am using MagickCore in imagemagick Q8 and I can't set specific pixel, this is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <magick/MagickCore.h>
#include <string.h>
int main(int argc,char **argv)
{
Image *imagen;
ImageInfo *imagen_info;
ExceptionInfo *exception;
PixelPacket *q;
MagickCoreGenesis(*argv,MagickTrue);
exception=AcquireExceptionInfo();
imagen_info = AcquireImageInfo();
(void) CopyMagickString(imagen_info->filename,argv[1],MaxTextExtent);
ReadImage(imagen_info, exception);
q = GetAuthenticPixels(imagen,0,0,1,1,exception);
q->red = 255;
q->green = 123;
q->blue = 220;
SyncAuthenticPixels(imagen,exception);
/* Write the image then destroy it. */
WriteImage(imagen_info, imagen);
DestroyImage(imagen);
DestroyExceptionInfo(exception);
MagickCoreTerminus();
return 0;
}
I am trying to read an image from a file and then edit a pixel and then save image to disk.
What am I doing wrong?
From the example provided, your imagen variable remains in a NULL pointer. It should be assigned by the return value of ReadImage.
imagen = ReadImage(imagen_info, exception);
The only other issue I see would be the assignment of color values on the PixelPacket. Assuming your working with RGB, you would need to calculate the Quantum color value.
q->red = 255 * QuantumRange;
q->green = 123 * QuantumRange;
q->blue = 220 * QuantumRange;
Note: this will issue a compiler warning, see docs for working with colors

Resources