Setting the title of a Motif Window - c

I am trying to set the title of a toplevel Motif 2.1 window.
From O'Reilly Volume Six A, I have seen that in Motif 2.1 XtVaOpenApplication is recommended to create a toplevel Widget.
In this appendix it can be seen how options and XtNumber(options) are used to act on resources via argument list.
I have tried to use it to generate an optional flag -title WINDOW_TITLE while invoking the program, without sucess.
This is what I have tried:
#include <stdlib.h>
#include <stdio.h>
#include <Xm/Xm.h>
#include <Xm/PushB.h>
static XrmOptionDescRec options[] = {
{ "-title", "XmNtitle", XrmoptionIsArg, NULL },
};
int main(int argc, char *argv[]) {
Widget toplevel; /* Top Level Button */
XtAppContext app; /* Application Context */
char *window_title = NULL; /* Top Level Window Title */
/* INITIALIZE TOP LEVEL WINDOW */
XtSetLanguageProc(NULL, NULL, NULL);
toplevel = XtVaOpenApplication( &app, argv[0], options, XtNumber(options), &argc, argv, NULL, sessionShellWidgetClass, NULL);
/* REALIZE TOPLEVEL WINDOW AND LAUNCH APPLICATION LOOP */
XtRealizeWidget(toplevel);
XtAppMainLoop(app);
return 0;
}
The program compiles but it does not react to -title MYTITLE command line argument.
This is the makefile (works on FreeBSD 12):
test: test.o
cc -L/usr/local/lib -O -o test test.o -lXm -lXt -lX11
test.o: test.c
cc -I/usr/local/include -c test.c
How can I change the title of the window based on an optional argument named -title?

The correct xrm option line is
{"-title", ".title", XrmoptionSepArg, NULL}
You don't actually need to specify it because it is in the default Xt option table.
In general you omit XmN when specifying xrm resource names.

In my Motif programs, I use Xlib directly, since Motif does not seem to handle correctly the UTF8 setting of window titles using XmNtitle property. Most modern window managers expect UTF8 string passed as _NET_MW_NAME.
void setWindowTitleUTF8(Widget w, char *title)
{
static Atom atoms[3];
static bool first = TRUE;
if (first)
{
static char *atom_names[] = {"_NET_WM_NAME", "_NET_WM_ICON", "UTF8_STRING"};
first = FALSE;
XInternAtoms(XtDisplay(w), atom_names, 3, FALSE, atoms);
}
XChangeProperty(XtDisplay(w), XtWindow(w), atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8,
PropModeReplace, (unsigned char *) title, strlen(title));
}
Make sure you call it on XtParent() of your widget, so it is applied to top level widget shell.

Please investigate further yourself (I'm no X/Motif expert), but sth seems to be off with the argument parsing.
Replacing options with NULL ant its size with 0 in call XtVaOpenApplication call seems to do the trick:
toplevel = XtVaOpenApplication( &app, argv[0], NULL, 0, &argc, argv, NULL, sessionShellWidgetClass, NULL);

Related

Can't run shell command in VTE GTK4 Fake Terminal

I made a small sample code in gtk4 + vte to run a fake terminal with a button in the bottom to run a simple command when the button is clicked.
main.c
#include <gtk/gtk.h>
#include <vte/vte.h>
#define WINDOW_HEIGHT 400
#define WINDOW_WIDTH 600
GtkApplication *app;
GtkWidget *window, *terminal, *grid, *scrollview1,*button;
int status;
void run_button(void){
char **argv_test[2] = {
"echo\0",
"Hello!!\0"
};
// can't run this command in fake terminal!
vte_terminal_spawn_async(VTE_TERMINAL(terminal),VTE_PTY_NO_HELPER,NULL,argv_test,NULL,G_SPAWN_SEARCH_PATH,NULL,NULL,NULL,2000,NULL,NULL,NULL);
}
void window_renderer(GtkApplication *app, gpointer user_data) {
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window),"My terminal");
gtk_window_set_default_size (GTK_WINDOW(window), WINDOW_WIDTH, WINDOW_HEIGHT);
grid = gtk_grid_new();
gtk_window_set_child(GTK_WINDOW(window), grid);
gtk_widget_set_vexpand(grid,TRUE);
gtk_widget_set_hexpand(grid,TRUE);
scrollview1 = gtk_scrolled_window_new();
gtk_grid_attach(GTK_GRID(grid), scrollview1, 0, 0, 1, 1);
gtk_widget_set_size_request(GTK_WIDGET(scrollview1),WINDOW_WIDTH,WINDOW_HEIGHT);
button = gtk_button_new_with_label("Run!");
gtk_grid_attach(GTK_GRID(grid), button, 0, 1, 1, 1);
g_signal_connect(button,"clicked", G_CALLBACK(run_button), NULL);
terminal = vte_terminal_new();
gtk_window_set_child(GTK_WINDOW(scrollview1), terminal);
gtk_widget_show(window);
}
int main(int argc, char **argv)
{
app = gtk_application_new(NULL, G_APPLICATION_FLAGS_NONE);
g_signal_connect(app,"activate", G_CALLBACK(window_renderer), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
Makefile
CFLAGS += -Wall
CFLAGS += `pkg-config --cflags gtk4 vte-2.91-gtk4`
LIBS += `pkg-config --libs gtk4 vte-2.91-gtk4`
.PHONY: all clean
all: main
main:main.c
gcc $(CFLAGS) main.c -o main $(LIBS)
clean:
rm main
I have compiled VTE (for gtk4) and gtk4 developer package, building a gtk4 app is not an issue!
When running the built binary, the program crashes when I click the button with a segmentation fault, probably because of a pointer not properly initialized (according to tutorials point).
Can someone help me to find out what I have missed?
I tested out your code and I believe I found the main issue with your terminal command along with some warnings that were displayed when I tested. First off, from reviewing other sample code using the "vte_terminal_spawn_async" function, the character string set included an ending array element of "NULL". So instead of the following code snippet.
char **argv_test[2] = {
"echo\0",
"Hello!!\0"
};
You would need to add in a third array element as in the following revised code snippet.
char *argv_test[3] = {
"echo\0", "Hello!!\0", NULL
};
If you notice one other minor revision to the above code snippet, I revised the definition of the character array from "char ** argv_test[3]" to "char *argv_test[3]". The compiler was listing a warning about incompatible pointer references. Also, when I was testing out this code I was receiving a warning and not able to view the terminal when the "Run" button was clicked.
Gtk-CRITICAL **: 14:49:46.383: gtk_window_set_child: assertion 'GTK_IS_WINDOW (window)' failed
From some previous troubleshooting with another user, we had determined that the child setup for the scrolled window needed to utilize the scrolled window child reference function along with the scrolled window macro. So I revised the scrolled window child assignment to be as follows.
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrollview1), terminal);
Making those small revisions resulted in displaying the terminal with your "echo Hello" command (I clicked the button twice in my testing).
I believe that if you try out those minor revisions to your code, you will be able to view your terminal and progress with your coding.
Regards.

New to makefiles - no clue how to make it work

When compiling my program, the makefile outputs this error:
cc sherpa.c -o -O2 -Wall
sherpa.c:4:10: fatal error: vte/vte.h: No such file or directory
was gonna post the code here but the formatting is weird so here's the github link https://github.com/amogus3016/sherpa
EDIT: code below
// Sherpa Terminal Emulator //
// This program is licensed under the GNU GPL 3.0, which provides absolutely NO WARRANTY //
__________________
| |
| // |
| // |
| // |
| // |
| // |
|________________|
#include <vte/vte.h>
static void
child_ready(VteTerminal *terminal, GPid pid, GError *error, gpointer user_Data)
{
if (!terminal) return;
if (pid == -1) gtk_main_quit();
}
int
main(int argc, char *argv[])
{
GtkWidget *window, *terminal;
/* init gtk window and terminal */
gtk_init(&argc, &argv);
terminal = vte_terminal_new();
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Sherpa");
/* start new shell */
gchar **envp = g_get_environ();
gchar **command = (gchar *[]){g_strdup(g_environ_getenv(envp, "SHELL")), NULL };
g_strfreev(envp);
vte_terminal_spawn_async(VTE_TERMINAL(terminal),
VTE_PTY_DEFAULT,
NULL, /* working dir */
command, /* command */
NULL, /* environment */
0, /* spawn flags */
NULL, NULL, /* child setup */
NULL, /* child pid */
-1, /* timeout */
NULL, /* cancellable */
child_ready, /* callback */
NULL); /* user_data */
/* connect signals */
g_signal_connect(window, "delete-event", gtk_main_quit, NULL);
g_signal_connect(terminal, "child-exited", gtk_main_quit, NULL);
/* combine widgets and run main loop */
gtk_container_add(GTK_CONTAINER(window), terminal);
gtk_widget_show_all(window);
gtk_main();
}
and the makefile:
sherpa: sherpa.c
$(CC) sherpa.c -o -O2 -Wall
printf("Building executable [|]")
printf("Building executable [/]")
printf("Building executable [-]")
printf("Building executable [\]")
printf("Building executable [|]")
mv ./sherpa /usr/bin
printf("Installing [|]")
printf("Installing [/]")
printf("Installing [-]")
printf("Installing [\]")
printf("Installing [|]")
IMO, you should rely on the default rules that make uses. In your case, since you are building an executable named sherpa from a file named sherpa.c, you can pretty much rely on the built-in rules for everything. The only thing atypical you are doing is using mv to install the executable, and the more typical usage would be to put that in a separate rule. In other words, your entire Makefile could simply be:
CFLAGS += $$(pkg-config --cflags vte-2.91)
LDFLAGS += $$(pkg-config --libs vte-2.91)
.PHONY: install all
all: sherpa
install: sherpa
mv ./sherpa /usr/bin
If you don't include the install recipe (which, IMO, you should not. Installing a package should have more infrastructure than merely copying the executable to /usr/bin), you can delete the last 4 lines of the file. (This would require that the user type make sherpa instead of just make, so keeping all: sherpa is reasonable, but you don't need to specify the build command.)
By using += when assigning CFLAGS and LDFLAGS, you allow the user to add flags at run time. eg, you can invoke CFLAGS='-g -O2' make to build the executable with optimizations enabled.

SDL + Mac OS X issue (window not responsive to keystrokes in certain situations)

I was trying to play around a bit with SDL on macOS (High Sierra 10.13.2) and I stumbled on what seems to be a rather incomprehensible mistake involving SDL windows not responding to keystrokes. I tried to construct two minimally different examples demarcating when this problem occurs: in fact, the examples do differ so minimally that it seems bizarre that their behaviour differs at all.
I have version 1.2.15 of the SDL libraries installed, and they were installed using the 'brew install sdl' command.
The first example (the one that does work correctly) is as follows:
/* sdl1.c */
#include "SDL/SDL.h"
SDL_Surface *screen;
SDL_Event event;
void initsdl (void) {
SDL_Init (SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode (500, 250, 32, SDL_SWSURFACE);
SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
while (1)
if (SDL_PollEvent(&event))
if (event.type == SDL_KEYDOWN)
if (event.key.keysym.sym == SDLK_ESCAPE)
break;
SDL_Quit ();
}
int main (int argc, char **argv) {
initsdl ();
return 0;
}
The other example is almost identical, but is spread over two files.
/* sdl2a.c */
#include "SDL/SDL.h"
SDL_Surface *screen;
SDL_Event event;
void initsdl (void) {
SDL_Init (SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode (500, 250, 32, SDL_SWSURFACE);
SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
while (1)
if (SDL_PollEvent(&event))
if (event.type == SDL_KEYDOWN)
if (event.key.keysym.sym == SDLK_ESCAPE)
break;
SDL_Quit ();
}
With the second file being:
/* sdl2b.c */
void initsdl(void);
int main (int argc, char **argv) {
initsdl ();
return (0);
}
As you can see, these programs are close to identical, with the only real difference being the fact that one of them is spread out over two files.
Now, I compile these files with the commands
cc -Wall -o sdl1 sdl1.c `sdl-config --libs` # (1)
and
cc -Wall -o sdl2 sdl2a.c sdl2b.c `sdl-config --libs` # (2)
I get two differences in the resulting behaviour.
First of all, the first example does not give any warnings whatsoever, whereas the second example gives the following warning:
ld: warning: object file (/usr/local/lib/libSDLmain.a(SDLMain.o)) was built for newer OSX version (10.13) than being linked (10.12)
The second difference is more problematic, however. Whereas the first example (the one who does give a compiler warning) behaves as it should, with a window appearing on screen, which correctly responds to the pressing of the ESC key before quitting, the second example simply does not respond to keystrokes at all, even after clicking on the window to make it active. This result is obtained by executing the binary resulting from the compilation directly from the terminal.

Getting keyboard modifiers state using Gnome libs (GDK) fetches initial state only

I'm trying to get the current keyboard modifiers state through gnome GDK or GTK library in aim to implement an accessibility gnome shell extension that shows that state.
I know how to get thier state using xlib, but there is not full binding for gnome gjs.
The code below get only the initial state. It does not update state.
/*
* compiling: gcc `pkg-config --cflags gdk-3.0` -o gdk_mod gdk_mod.c `pkg-config --libs gdk-3.0`
*/
#include <gdk/gdk.h>
int main (int argc, char **argv) {
gdk_init(&argc, &argv);
GdkDisplay * disp;
disp = gdk_display_open(NULL);
if (disp!=NULL) g_printf("display connected!\n");
GdkKeymap * kmap;
kmap = gdk_keymap_get_for_display(disp);
guint state;
state = gdk_keymap_get_modifier_state(kmap);
g_printf("mod state: %x\n", state);
while (1) {
g_usleep(1000000);
//kmap = gdk_keymap_get_for_display(disp);
state = gdk_keymap_get_modifier_state(kmap);
g_printf("mod state: %x\n", state);
}
}
Here an example output with CAPS lock active then inactive but no change:
$ ./gdk_mod
display found!
mod state: 2
mod state: 2
mod state: 2
mod state: 2
mod state: 2
^C
Currently using Kubuntu 15.04.
What's wrong with my code?
You are going to need to run the GTK+ event loop for this to work. The event loop is part of GLib's main loop. When you call gtk_main(), this main loop is run. I don't know if it polls for events or has events pushed to it, but it won't ask for the keyboard state on the fly like you were trying to do.
The easiest way to set up GDK is to do it via GTK+ by using gtk_init() and gtk_main(). You can use GDK on its own, but I don't know how. You seemed to have figured it out, which works.
And instead of calling g_usleep(), which just blocks your program, you can hook a periodic timeout into the main loop. This is done with g_timeout_add(). The function that you pass to g_timeout_add() returns a Boolean value that decides whether the timer should be stopped or not, so you don't have to worry about rescheduling your function, as GLib will do that for you.
Indeed, I need an event loop as andlabs said in his comment. His suggestion to use GTK gtk_init() & gtk_main() works perfectly.
/*
* compiling: gcc `pkg-config --cflags gtk+-3.0` -o gtk_xkbmod3 gtk_xkbmod3.c `pkg-config --libs gtk+-3.0`
*/
#include <gtk/gtk.h>
static void update(GdkKeymap * kmap) {
guint state;
state = gdk_keymap_get_modifier_state(kmap);
g_printf("%i\n", state);
}
int main (int argc, char **argv) {
gtk_init(&argc, &argv);
GdkKeymap * kmap;
kmap = gdk_keymap_get_default();
g_timeout_add_seconds(1, (GSourceFunc) update, kmap);
gtk_main();
}
I could also use GDK with GLib GMainLoop.
/*
* compiling: gcc `pkg-config --cflags gdk-3.0` -o gdk_xkbmod4 gdk_xkbmod4.c `pkg-config --libs gdk-3.0`
*/
#include <gdk/gdk.h>
GMainLoop *mainloop;
static void update(GdkKeymap * kmap) {
guint state;
state = gdk_keymap_get_modifier_state(kmap);
g_printf("%i\n", state);
}
int main (int argc, char **argv) {
gdk_init(&argc, &argv);
GdkKeymap * kmap;
kmap = gdk_keymap_get_default();
g_timeout_add_seconds(1, (GSourceFunc) update, kmap);
mainloop = g_main_loop_new(g_main_context_default(), FALSE);
g_main_loop_run(mainloop);
}
References:
Xfce Foundation Classes: The Main Event Loop
GTK+ Forums: GDK + GLib main loop

ANSI C compile error: expected expression before ‘,’ token

I continue to get this error when attempting to compile a bit of code I wrote up, with the location in the file being totally unhelpful. This uses gtk 2.0.
The following is what I receive at compile time:
charles#draton-generico:~/Documents/C89$ gcc -x c -ansi -g bahbahbah.c -o bahbahbah pkg-config --cflags --libs gtk+-2.0
bahbahbah.c: In function ‘main’:
bahbahbah.c:28:1: error: expected expression before ‘,’ token
The following is the code I am trying to compile:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
void closure(void)
{
gtk_main_quit();
printf("gtk_main_quit() has been called.\n");
}
void main(int argc, char* argv[])
{
gboolean check = gtk_init_check(&argc, &argv);
if (check == FALSE)
{
printf("Failed to initialize toolkit.\nTerminating.\n");
exit(EXIT_FAILURE);
}
else
{
printf("Initialized toolkit.\n");
GtkWidget* main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title((GtkWindow*)main_window, "BLAH");
gtk_window_set_default_size((GtkWindow*)main_window, 700, 700);
g_signal_connect(main_window, "delete-event", closure, void);
gtk_widget_show(main_window);
printf("Window created, sleeping in gtk_main().\n");
gtk_main();
}
printf("Exiting.\n");
exit(EXIT_SUCCESS);
}
Please help. :(
Use NULL replace void in line 28.
g_signal_connect(main_window, "delete-event", closure, NULL);
void is a Key-word!
There is the describe of g_signal_connect():
#define g_signal_connect(instance, detailed_signal, c_handler, data)
Connects a GCallback function to a signal for a particular object.
The handler will be called before the default handler of the signal.
instance :
the instance to connect to.
detailed_signal :
a string of the form "signal-name::detail".
c_handler :
the GCallback to connect.
data :
data to pass to c_handler calls.
Returns :
the handler id
So, you just want pass nothing to this func? Then you should use NULL.

Resources