How to use libxcb on mac? - c

My laptop is Macbook Pro.
I installed libxcb on it and tried a simple example that x.org given:
#include <unistd.h> /* pause() */
#include <xcb/xcb.h>
int main ()
{
xcb_connection_t *c;
xcb_screen_t *screen;
xcb_window_t win;
/* Open the connection to the X server */
c = xcb_connect (NULL, NULL);
/* Get the first screen */
screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
/* Ask for our window's Id */
win = xcb_generate_id(c);
/* Create the window */
xcb_create_window (c, /* Connection */
XCB_COPY_FROM_PARENT, /* depth (same as root)*/
win, /* window Id */
screen->root, /* parent window */
0, 0, /* x, y */
150, 150, /* width, height */
10, /* border_width */
XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
screen->root_visual, /* visual */
0, NULL); /* masks, not used yet */
/* Map the window on the screen */
xcb_map_window (c, win);
/* Make sure commands are sent before we pause, so window is shown */
xcb_flush (c);
pause (); /* hold client until Ctrl-C */
return 0;
}
But there is no window shown on screen. Is there something I forget?

Related

How do I pass a widget as client_data to a Xt/Motif signal handler?

I want a Motif application to redraw a Drawing Area widget on reception of SIGUSR1 signal.
I have configured the signal using the Xt functionalities in X11R6:
/* CONFIGURE READ SIGNAL, TRAPS SIGUSR1 TO FORCE APPLICATION READ DATA FILE */
signal(SIGUSR1, signal_usr1_handler);
signal_id = XtAppAddSignal (app, read_data, (XtPointer)chart_area);
Where signal_usr1_handler is the OS Unix handler and read_data is the Xt signal handler.
For the Xt signal handler I am trying to pass the widget chart_area which is a Drawing Area Widget.
These are the handlers:
/* SIGNAL HANDLER */
void signal_usr1_handler() {
printf("SIGUSR1 RECEIVED\n");
XtNoticeSignal(signal_id);
}
/* READ DATA XT SIGNAL HANDLER */
void read_data(XtPointer client_data, XtSignalId *id) {
posx += 5;
printf("XT HANDLES SIGUSR1\n");
XmDrawingAreaCallbackStruct da_struct;
da_struct.reason = XmCR_EXPOSE;
da_struct.event = (XEvent *) NULL;
da_struct.window = XtWindow((Widget)client_data);
XtCallCallbacks((Widget)client_data, XmNexposeCallback, (XtPointer) &da_struct);
}
The application raises a Segmentation fault (core dumped) exactly when executing the XtCallCallbacks function.
This is the complete source code:
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <Xm/Xm.h>
#include <Xm/DrawingA.h>
#include <Xm/Label.h>
/* FUNCTION DECLARATIONS */
void draw_chart(Widget widget, XtPointer client_data, XtPointer call_data);
void signal_usr1_handler();
void read_data(XtPointer client_data, XtSignalId *id);
/* XT SIGNAL ID */
XtSignalId signal_id; /* Signal ID used to receive data read via SIGUSR1 */
int main(int argc, char *argv[]) {
/* WIDGETS */
XtAppContext app; /* Application Context */
Widget toplevel; /* Top Level Button */
Widget chart_area; /* Drawing Area Widget to draw the chart */
/* DRAWING AREA RELATED */
XGCValues gcv;
GC gc;
/* RESOURCE VALUE ARRAYS/COUNT */
Arg al[10];
int ac;
/* INITIALIZE TOP LEVEL WINDOW */
XtSetLanguageProc(NULL, NULL, NULL);
toplevel = XtVaOpenApplication(
&app, argv[0], NULL, 0, &argc, argv, NULL, sessionShellWidgetClass,
XmNwidth, 400, XmNheight, 300, NULL
);
/* CREATE AND MANAGE DRAWING CANVAS WIDGET */
ac=0;
chart_area = XmCreateDrawingArea(toplevel, "chart_area", al, ac);
/* CREATE GRAPHICS CONTEXT */
gcv.foreground = WhitePixelOfScreen(XtScreen(chart_area));
gcv.background = BlackPixelOfScreen(XtScreen(chart_area));
gc = XCreateGC (
XtDisplay(chart_area),
RootWindowOfScreen(XtScreen(chart_area)),
(GCForeground | GCBackground),
&gcv);
/* ASSIGN GRAPHICS CONTEXT */
XtVaSetValues(chart_area, XmNuserData, gc, NULL);
/* ASSIGN CALLBACKS AND MANAGE WIDGET */
XtAddCallback(chart_area, XmNexposeCallback, draw_chart, NULL);
XtManageChild(chart_area);
/* CONFIGURE READ SIGNAL, TRAPS SIGUSR1 TO FORCE APPLICATION READ DATA FILE */
signal(SIGUSR1, signal_usr1_handler);
signal_id = XtAppAddSignal (app, read_data, (XtPointer)chart_area);
/* REALIZE TOPLEVEL WINDOW AND LAUNCH APPLICATION LOOP */
XtRealizeWidget(toplevel);
XtAppMainLoop(app);
return 0;
}
void draw_chart(Widget widget, XtPointer client_data, XtPointer call_data) {
XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct *) call_data;
XEvent *event = cbs->event;
Display *dpy = event->xany.display;
GC gc;
/* DRAW LINE */
if(cbs->reason == XmCR_EXPOSE || cbs->reason == XmCR_ACTIVATE) {
XtVaGetValues(widget, XmNuserData, &gc, NULL);
XDrawLine(dpy, cbs->window, gc, 10, 10, posx, 200);
}
}
/* SIGNAL HANDLER */
void signal_usr1_handler() {
printf("SIGUSR1 RECEIVED\n");
XtNoticeSignal(signal_id);
}
/* READ DATA XT SIGNAL HANDLER */
void read_data(XtPointer client_data, XtSignalId *id) {
/* ... READ DATA AND PROCESSING GOES HERE ... */
posx += 5;
printf("XT HANDLES SIGUSR1\n");
XmDrawingAreaCallbackStruct da_struct;
da_struct.reason = XmCR_EXPOSE;
da_struct.event = (XEvent *) NULL;
da_struct.window = XtWindow((Widget)client_data);
/* XtCallCallbacks((Widget)client_data, XmNexposeCallback, (XtPointer) &da_struct); */
}
The following causes the crash:
Display *dpy = event->xany.display;
Because event was set to NULL.
Also you may need to deal with gc by passing its address and receiving XmUserData as
GC *gc;
then using *gc,

Accessing libraries and functions between multiple source files [duplicate]

This question already has answers here:
"Multiple definition", "first defined here" errors
(6 answers)
Closed 5 years ago.
I am running a program which makes use of the ncurses library to display windows in the terminal containing data being updated and inserted into its corresponding window from my main source file.
I have a separate file with its own header file which contains printf statements I would like to convert to ncurse functions. And display within the windows I created.
ncurses required some setup and made use of some small functions so im unsure how to use it within both files. I tried including it within my header file and linking to both my source files however I get multiple defenitions, first defined here errors.
Setup for ncurse windows within my main file:
/* Window defintion constants*/
#define PROTOPORT 36795 /* default protocol port number, booknumber */
#define QLEN 6 /* size of request queue */
#define MAXSIZE 256
#define NUMWINS 7
#define RES_BUF_SIZE 80
WINDOW *w[NUMWINS];
WINDOW *sw[NUMWINS];
WINDOW wh[NUMWINS];
void update_win(int i) {
touchwin(w[i]);
wrefresh(sw[i]);
}
/* add string to a window */
void wAddstr(int z, char c[255]);
void GUIshutdown(char * response) {
wmove(sw[4], 0, 0);
wclrtoeol(sw[4]);
wprintw(sw[4], "All finished. Press Enter to terminate the program.");
update_win(4);
wgetstr(sw[4], response);
/* End screen updating */
endwin();
echo();
}
//////////
int main(int argc, char *argv[]) {
/***********************************************/
/* setup ncurses for multiple windows */
/***********************************************/
setlocale(LC_ALL, ""); // this has to do with the character set to use
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
/* Clear screen before starting */
clear();
w[0] = newwin(0,0,0,0);
if (LINES != 43 || COLS != 132) {
move(0, 0);
addstr("Piggy3 requires a screen size of 132 columns and 43 rows");
move(1, 0);
addstr("Set screen size to 132 by 43 and try again");
move(2, 0);
addstr("Press enter to terminate program");
mvprintw(3,0,"%dx%d\n", COLS, LINES);
refresh();
getstr(response); // Pause so we can see the screen
endwin();
exit(EXIT_FAILURE);
}
/* create the 7 windows and the seven subwindows*/
for (a = 0; a < NUMWINS; a++) {
w[a] = newwin(WPOS[a][0], WPOS[a][1], WPOS[a][2], WPOS[a][3]);
sw[a] = subwin(w[a], WPOS[a][0] - 2, WPOS[a][1] - 2, WPOS[a][2] + 1, WPOS[a][3] + 1);
scrollok(sw[a], TRUE); // allows window to be automatically scrolled
wborder(w[a], 0, 0, 0, 0, 0, 0, 0, 0);
touchwin(w[a]);
wrefresh(w[a]);
wrefresh(sw[a]);
}
/***********************************************/
/* Windows */
/***********************************************/
/*
* Notes:
* sw[0] = upper left window,
* sw[1] = upper right window,
* sw[2] = bottom left window,
* sw[3] = bottom right window
* sw[4] = bottom command window
* sw[5] = bottom inputs window
* sw[6] = bottom errors menu.
*/
wmove(sw[0], 0, 0);
wprintw(sw[0], "This is the window where the data flowing from left to right ");
wprintw(sw[0], "will be displayed. Notice now we don't need to worry about when we reach the last ");
wprintw(sw[0], " position in the subwindow, we will just wrap around down to the next line of the subwindow");
wprintw(sw[0], " and can never mess up the border that is in the parent window");
wprintw(sw[inLeft], " Data coming from the left, head piggy\n");
wmove(sw[1], 4, 0);
waddstr(sw[1], "Data leaving right side");
wmove(sw[2], 4, 0);
waddstr(sw[2], "Data leaving the left side");
wmove(sw[3], 4, 0);
waddstr(sw[3], "Data arriving from the right");
wmove(sw[4], 0, 0);
waddstr(sw[4], "Commands: ");
wmove(sw[5], 0, 0);
waddstr(sw[5], "Data Entry: ");
wmove(sw[6], 0, 0);
waddstr(sw[6], "Errors: ");
for (int a = 0; a < NUMWINS; a++){
update_win(a);
}
wmove(sw[4], 0, 0);
wprintw(sw[4], "Press Enter to see the output in the upper left window scroll");
wgetstr(sw[4], response); // Pause so we can see the screen
wmove(sw[4], 0, 0);
wclrtoeol(sw[4]); // clears current line without clobbering borders
update_win(4);
}
You need header guards in your .h files.
#ifndef HEADER_FILE_NAME_H
#define HEADER_FILE_NAME_H
// include,definitions..
#endif //HEADER_FILE_NAME_H

Xopendisplay segmentation fault

I'm carrying out a simple X11 program in C. But I got segmentation fault(11) because of XOpenDisplay(NULL).
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
Display *display;
Window window;
XEvent event;
char *msg = "Hello, World!";
int s;
/* /2/ some basic X11 setup */
/* open connection with the server */
display = XOpenDisplay(NULL);
if (display == NULL)
{
fprintf(stderr, "Cannot open display\n");
exit(1);
}
s = DefaultScreen(display);
/* create window */
window = XCreateSimpleWindow(display, RootWindow(display, s), 10, 10, 200, 200, 1,
BlackPixel(display, s), WhitePixel(display, s));
/* select kind of events we are interested in */
XSelectInput(display, window, ExposureMask | KeyPressMask);
/* map (show) the window */
XMapWindow(display, window);
/* /3/ event loop */
for (;;)
{
XNextEvent(display, &event);
/* /4/ draw or redraw the window */
if (event.type == Expose)
{
XFillRectangle(display, window, DefaultGC(display, s), 20, 20, 10, 10);
XDrawString(display, window, DefaultGC(display, s), 50, 50, msg, strlen(msg));
}
/* /5/ exit on key press */
if (event.type == KeyPress)
break;
}
/* /6/ close connection to server */
XCloseDisplay(display);
return 0;
}
command line:
gcc -o out test.c -I/usr/X11R6/include/ -L/usr/X11R6/lib/ -lX11
I executed it failed in Mac terminal, but it worked in Xcode. I don't know the reasons.

How could I implement a scrolling window inside another?

I am embedding my command line application into a ncurses-TUI.
My application (client/server) should be able to print a lot lines if needed, but it must stays into the windows I defined :
/* Removed code */
ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
// Initialize curses
initscr(); // initializes the screen
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
/* Removed code */
/* Create items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
{
my_items[i] = new_item(choices[i], choices[i]);
}
/* Removed code */
/* Create menu */
my_menu = new_menu((ITEM **)my_items);
/* Set menu option not to show the description */
menu_opts_off(my_menu, O_SHOWDESC);
/* Create the window to be associated with the menu */
my_menu_win = newwin(WIN_HEIGHT, WIN_WIDTH, WIN_YPOS, WIN_XPOS);
keypad(my_menu_win, TRUE);
/* Set main window and sub window */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, WIN_HEIGHT/2, WIN_WIDTH/2, WIN_HEIGHT/2-3, WIN_WIDTH/2-10));
/* Set menu mark to the string " * " */
set_menu_mark(my_menu, ">");
/* Print a border around the main window and print a title */
box(my_menu_win, 0, 0);
/* Removed code */
/* Post the menu */
post_menu(my_menu);
wrefresh(my_menu_win);
endwin(); //resets the screen
}
1 How can I add a windows inside this windows with a scrolling content where all the output that usually goes to the terminal would be print
2 ...or is it possible to link that windows with the terminal output ?

Combo box drop-down list not initially drawn

As the title states my problem is with the expanded list of a WinAPI combo box showing up blank when opened. Any subsequent updates (as when moving the cursor around) will redraw the affected items however. In addition the list won't respond to any mouse input. This happens in both Windows XP as well as 7.
As near as I can tell in Spy++ the modal list receives WM_ERASEBKGND but stops short of processing WM_PAINT. Showing a combo box in a modal dialog box works nicely, by the way, but creating the control as part of a regular top-level window or spawning the same dialog template as a modeless child window does not.
I'm guessing I've forgotten something rather basic and embarrassing, e.g. not setting a clipping style or calling DoDialogMagic in the message loop or some such, but I just can't seem to figure it out on my own.
Anyway, here's a minimal repro case:
#include <windows.h>
#include <commctrl.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "comctl32.lib")
INT CALLBACK _tWinMain(HINSTANCE instance, HINSTANCE parent, LPTSTR commands, INT show) {
static const TCHAR title[] = _T("Combo Problem");
HWND hwnd;
HWND combo;
MSG msg;
/* First create our parent window */
const WNDCLASS cls = {
/* style */ 0,
/* lpfnWndProc */ DefWindowProc,
/* cbClsExtra */ 0,
/* cbWndExtra */ 0,
/* hInstance */ instance,
/* hIcon */ NULL,
/* hCursor */ LoadCursor(NULL, IDC_ARROW),
/* hbrBackground */ (HBRUSH) (COLOR_INACTIVEBORDER + 1),
/* lpszMenuName */ NULL,
/* lpszClassName */ title
};
RegisterClass(&cls);
hwnd = CreateWindow (
/* lpClassName */ title,
/* lpWindowName */ title,
/* dwStyle */ WS_OVERLAPPEDWINDOW | WS_VISIBLE,
/* x */ CW_USEDEFAULT,
/* y */ CW_USEDEFAULT,
/* nWidth */ 125,
/* nHeight */ 70,
/* hWndParent */ NULL,
/* hMenu */ NULL,
/* hInstance */ instance,
/* lpParam */ NULL
);
/* Now create and populate the combo box itself */
InitCommonControls();
combo = CreateWindow (
/* lpClassName */ _T("COMBOBOX"),
/* lpWindowName */ _T(""),
/* dwStyle */ CBS_DROPDOWNLIST | WS_CHILD | WS_VISIBLE,
/* x */ 10,
/* y */ 10,
/* nWidth */ 100,
/* nHeight */ 150,
/* hWndParent */ hwnd,
/* hMenu */ NULL,
/* hInstance */ instance,
/* lpParam */ NULL
);
SendMessage(combo, CB_ADDSTRING, 0, (LPARAM) _T("Alpha"));
SendMessage(combo, CB_ADDSTRING, 0, (LPARAM) _T("Beta"));
SendMessage(combo, CB_ADDSTRING, 0, (LPARAM) _T("Gamma"));
/* Finally run the message pump */
while(GetMessage(&msg, hwnd, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
You are passing a hwnd to GetMessage, this is usually not what you want, just use NULL.

Resources