Skip to content

Instantly share code, notes, and snippets.

@Eleobert

Eleobert/main.c Secret

Last active June 3, 2018 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eleobert/d0a08875655b053a17e87f8c1c18f179 to your computer and use it in GitHub Desktop.
Save Eleobert/d0a08875655b053a17e87f8c1c18f179 to your computer and use it in GitHub Desktop.
X11 window
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <locale.h>
#include <wchar.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
//to compile -> gcc main.c -o out -lX11
void setWindowTitle(const char* title, Window *win, Display *display){
Atom wm_Name = XInternAtom(display, "_NET_WM_NAME", 0);
Atom utf8Str = XInternAtom(display, "UTF8_STRING", 0);
Atom win_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", True );
Atom not_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE_DIALOG", False );
XChangeProperty(display, *win, wm_Name, utf8Str, 8, PropModeReplace, (const unsigned char*)title, strlen(title));
XChangeProperty( display,*win, win_type, XA_ATOM,
32, PropModeReplace, (unsigned char *)&not_type,
2);
}
void split(const wchar_t* text, const wchar_t* seps,wchar_t ***str, int *count){
/*wchar_t *last, *tok;
wchar_t* data;
int i;
*count = 0;
data = wcsdup(text);
for (tok = wcstok(data, seps, &last); tok != NULL; tok = wcstok(NULL, seps, &last))
(*count)++;
free(data);
fflush(stdout);
data = wcsdup(text);
*str = (wchar_t **)malloc((size_t)(*count)*sizeof(wchar_t*));
for (i = 0, tok = wcstok(data, seps, &last); tok != NULL; tok = wcstok(NULL, seps, &last), i++)
(*str)[i] = wcsdup(tok);
free(data);*/
}
void ShowWindow(const char* title, const wchar_t* text)
{
Display* dpy = NULL;
Window win;
int i;
wchar_t** text_splitted;
int textLines;
setlocale(LC_ALL,"");
const wchar_t *sep = L"\n";
split(text,sep , &text_splitted, &textLines); //<-comment this line, and everything will be ok
//---------------just for debugging propuses
/*for(i = 0; i < textLines; i++){
wprintf(L"%ls\n", text_splitted[i]);
}
fflush(stdout);*/
//------------------------------------------
dpy = XOpenDisplay(NULL);
if(dpy == NULL){
}
int ds = DefaultScreen(dpy);
win = XCreateSimpleWindow(dpy, RootWindow(dpy, ds), 0, 0, 800, 100,0, BlackPixel(dpy, ds), WhitePixel(dpy, ds));
XSelectInput(dpy, win, ExposureMask | KeyPressMask);
XMapWindow(dpy, win);
//allow windows to be closed by pressing cross button
Atom WM_DELETE_WINDOW = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(dpy, win, &WM_DELETE_WINDOW, 1);
XGCValues gcValues;
gcValues.font = XLoadFont(dpy, "7x13");
GC gc = XCreateGC(dpy, win, GCFont + GCForeground, &gcValues);
XUnmapWindow( dpy, win );
XMapRaised( dpy, win );
XFlush( dpy );
setWindowTitle(title, &win, dpy);
char *def_string;
char **missing_charset_list;
int missing_charset_count;
XFontSet fs;
fs = XCreateFontSet(dpy,
"-*-*-medium-r-*-*-*-140-75-75-*-*-*-*" ,
&missing_charset_list, &missing_charset_count, &def_string);
if (missing_charset_count /* && debug */) {
fprintf(stderr, "Missing charsets: ");
for(int i = 0; i < missing_charset_count; i++){
printf(missing_charset_list[i]);
}
XFreeStringList(missing_charset_list);
}
XEvent e;
bool quit = false;
while( !quit ) {
XNextEvent( dpy, &e );
switch (e.type){
case Expose:{
XwcDrawString( dpy, win, fs, gc, 10, 50, text, wcslen(text));
XFlush(dpy);
break;
}
case ClientMessage:
//if window's cross button pressed
if((unsigned int)(e.xclient.data.l[0]) == WM_DELETE_WINDOW)
quit = true;
break;
default:
break;
}
}
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
return;
}
int main(){
ShowWindow("Emergências e falácias. уровнем. 水平. المستوى.手書き. 필적",
L"Este é um texto de teste. \nуровнем. \nالمستوى, 水平, 手書き");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment