Files
lotherk 0ea754ba51 dtchat: finish the call window (T7.6)
The call window gains a Camera button that cycles the video source (/dev/video0..3, then the test pattern) through a hook in localui; the status line names the new backend (e.g. video source: v4l2:/dev/video0), and the button is insensitive in --call-demo. The in-call status now says 'in the call - LAN only, no relay', the note the plan asks for. Verified live between two UIs under Xvfb: the Camera button switched the sender from camera to pattern and back, the status line showed the backend, and video kept flowing; screenshots build/mcde-shots/dtchat-t76-camera.png, dtchat-t76b.png, dtchat-t76-video-off.png, dtchat-t76-full.png. Video off, full screen and resize-aware rendering were committed in 4e967b8a3c.
2026-11-17 09:25:02 +00:00

614 lines
16 KiB
C

/*
* ui_call.c -- the call window: video, push-to-talk, hang up
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The video area keeps the newest I420 frame; on every expose (and when a new
* frame arrives) it is scaled bilinearly into a rectangle that keeps the
* aspect ratio, converted to 0x00RRGGBB with the BT.601 matrix and written
* with one XPutImage. The pixels live in one buffer, so a frame is composed
* before it reaches the server (no tearing between planes). Push-to-talk is
* a toggle that calls dc_call_send_ptt(); hang up calls
* dc_call_hangup() and tells the owner.
*/
#define _POSIX_C_SOURCE 200809L
#include "ui_call.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <Xm/DialogS.h>
#include <Xm/DrawingA.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/ToggleB.h>
#include <Xm/Xm.h>
#define VIDEO_W 640
#define VIDEO_H 360
struct DtCallWindow {
Widget shell;
Widget form;
Widget area;
Widget status;
Widget talk;
Widget answer;
Widget hangup;
int incoming;
DcMediaCall *call;
DtCallHangupFn fn;
void *arg;
char *peer_name;
unsigned char *frame; /* W x H I420 */
int fw;
int fh;
int has_frame;
int talking;
int closed;
uint32_t *pixels; /* pw x ph, 0x00RRGGBB */
int pw;
int ph;
XImage *image;
int dirty;
XtIntervalId size_timer;
Widget video_toggle;
Widget camera;
Widget full_toggle;
DtCallCameraFn camera_fn;
void *camera_arg;
int video_on;
int fullscreen;
int screen_w;
int screen_h;
int normal_x;
int normal_y;
int normal_w;
int normal_h;
};
static void
render(DtCallWindow *w)
{
Dimension wdim, hdim;
int dw, dh;
int vw = w->fw;
int vh = w->fh;
int tw, th, ox, oy;
unsigned char *scaled;
size_t i;
if (!w->video_on)
return;
if (w->area == NULL || !XtIsRealized(w->area))
return;
XtVaGetValues(w->area, XmNwidth, &wdim, XmNheight, &hdim, NULL);
dw = (int)wdim;
dh = (int)hdim;
if (dw < 8 || dh < 8)
return;
if (getenv("DTCHAT_CALL_DEBUG") != NULL)
fprintf(stderr, "call render: area %dx%d frame %dx%d has=%d on=%d\n",
dw, dh, vw, vh, w->has_frame, w->video_on);
if (!w->has_frame)
return;
if (w->pixels == NULL || w->pw != dw || w->ph != dh) {
uint32_t *pixels = calloc((size_t)dw * dh, sizeof(uint32_t));
if (pixels == NULL)
return;
free(w->pixels);
w->pixels = pixels;
w->pw = dw;
w->ph = dh;
if (w->image != NULL) {
w->image->data = NULL;
XDestroyImage(w->image);
w->image = NULL;
}
}
/* fit the frame into the area, bars stay black */
tw = dw;
th = vw > 0 ? (int)((long)dw * vh / vw) : dh;
if (th > dh) {
th = dh;
tw = vh > 0 ? (int)((long)dh * vw / vh) : dw;
}
if (tw < 1)
tw = 1;
if (th < 1)
th = 1;
ox = (dw - tw) / 2;
oy = (dh - th) / 2;
for (i = 0; i < (size_t)dw * dh; i++)
w->pixels[i] = 0;
scaled = malloc((size_t)tw * th * 3 / 2);
if (scaled == NULL)
return;
dc_i420_scale(w->frame, vw, vh, scaled, tw, th);
{
uint32_t *rgb = malloc((size_t)tw * th * sizeof(uint32_t));
int y;
if (rgb == NULL) {
free(scaled);
return;
}
dc_i420_to_xrgb(scaled, tw, th, rgb, tw);
for (y = 0; y < th; y++)
memcpy(w->pixels + (size_t)(oy + y) * dw + ox,
rgb + (size_t)y * tw, (size_t)tw * sizeof(uint32_t));
free(rgb);
}
free(scaled);
if (w->image == NULL) {
Display *dpy = XtDisplay(w->area);
int screen = DefaultScreen(dpy);
/* 0x00RRGGBB words match the RGB masks of the usual TrueColor
* Xvfb/X.Org visuals */
w->image = XCreateImage(dpy, DefaultVisual(dpy, screen),
(unsigned)DefaultDepth(dpy, screen), ZPixmap, 0,
(char *)w->pixels, (unsigned)dw, (unsigned)dh, 32, 0);
}
if (w->image != NULL) {
w->image->data = (char *)w->pixels;
XPutImage(XtDisplay(w->area), XtWindow(w->area),
DefaultGC(XtDisplay(w->area), DefaultScreen(XtDisplay(w->area))),
w->image, 0, 0, 0, 0, (unsigned)dw, (unsigned)dh);
XFlush(XtDisplay(w->area));
}
w->dirty = 0;
}
static void
area_expose(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
(void)widget;
(void)call;
render(w);
}
static void
area_resize(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
(void)widget;
(void)call;
render(w);
}
static void
talk_cb(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
int on = XmToggleButtonGetState(widget) ? 1 : 0;
(void)call;
w->talking = on;
if (w->call != NULL && dc_call_send_ptt(w->call, on) != 0) {
dtchat_call_window_set_status(w, "cannot send the talk state");
return;
}
dtchat_call_window_set_status(w, on ? "talking" :
"push-to-talk is off");
}
static void
answer_cb(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
(void)widget;
(void)call;
if (w->call == NULL || w->closed)
return;
if (dc_call_accept(w->call) != 0) {
dtchat_call_window_set_status(w, "cannot answer the call");
return;
}
XmToggleButtonSetState(w->talk, False, False);
XtSetSensitive(w->answer, False);
w->incoming = 0;
dtchat_call_window_set_status(w, "in the call");
}
static void
video_cb(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
int on = XmToggleButtonGetState(widget) ? 1 : 0;
(void)call;
w->video_on = on;
if (on) {
w->dirty = 1;
render(w);
dtchat_call_window_set_status(w, "video is on");
return;
}
/* keep the area in place (a hidden drawing area would let the form
* shrink) and paint it with the background */
if (w->area != NULL && XtIsRealized(w->area)) {
XClearArea(XtDisplay(w->area), XtWindow(w->area), 0, 0, 0, 0,
True);
XFlush(XtDisplay(w->area));
}
dtchat_call_window_set_status(w, "video is off");
}
static void
camera_cb(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
const char *name;
(void)widget;
(void)call;
if (w->camera_fn == NULL)
return;
name = w->camera_fn(w->camera_arg);
if (name == NULL) {
dtchat_call_window_set_status(w, "no camera");
return;
}
{
char line[160];
snprintf(line, sizeof(line), "video source: %s", name);
dtchat_call_window_set_status(w, line);
}
}
static void
full_cb(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
int on = XmToggleButtonGetState(widget) ? 1 : 0;
(void)call;
w->fullscreen = on;
if (on) {
XtVaSetValues(w->form, XmNwidth, w->screen_w,
XmNheight, w->screen_h, NULL);
XtVaSetValues(w->shell, XmNx, 0, XmNy, 0,
XmNwidth, w->screen_w, XmNheight, w->screen_h, NULL);
XtVaSetValues(w->area, XmNwidth, w->screen_w,
XmNheight, w->screen_h - 70, NULL);
} else {
XtVaSetValues(w->form, XmNwidth, w->normal_w,
XmNheight, w->normal_h, NULL);
XtVaSetValues(w->shell, XmNx, w->normal_x, XmNy, w->normal_y,
XmNwidth, w->normal_w, XmNheight, w->normal_h, NULL);
XtVaSetValues(w->area, XmNwidth, VIDEO_W, XmNheight, VIDEO_H,
NULL);
}
w->dirty = 1;
render(w);
}
static void
hangup_cb(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
(void)widget;
(void)call;
if (w->closed)
return;
w->closed = 1;
if (w->call != NULL)
(void)dc_call_hangup(w->call);
if (w->fn != NULL)
w->fn(w->arg);
}
static void
destroyed_cb(Widget widget, XtPointer client, XtPointer call)
{
DtCallWindow *w = client;
(void)widget;
(void)call;
w->shell = NULL;
}
static void
size_cb(XtPointer client, XtIntervalId *id)
{
DtCallWindow *w = client;
(void)id;
w->size_timer = 0;
if (w->shell == NULL)
return;
XtVaSetValues(w->form, XmNwidth, w->normal_w, XmNheight,
w->normal_h, NULL);
XtVaSetValues(w->shell, XmNwidth, w->normal_w, XmNheight,
w->normal_h, NULL);
}
static void
set_label(Widget widget, const char *text)
{
XmString x = XmStringCreateLocalized((char *)text);
XtVaSetValues(widget, XmNlabelString, x, NULL);
XmStringFree(x);
}
DtCallWindow *
dtchat_call_window_create(Widget parent, DcMediaCall *call,
const char *peer_name, DtCallHangupFn hangup, void *arg)
{
DtCallWindow *w;
Widget form, buttons, close;
char title[256];
XmString x;
if (parent == NULL)
return NULL;
w = calloc(1, sizeof(*w));
if (w == NULL)
return NULL;
w->call = call;
w->fn = hangup;
w->arg = arg;
w->peer_name = strdup(peer_name != NULL ? peer_name : "call");
w->frame = calloc(1, (size_t)DC_VIDEO_W * DC_VIDEO_H * 3 / 2);
if (w->frame == NULL || w->peer_name == NULL) {
dtchat_call_window_destroy(w);
return NULL;
}
w->fw = DC_VIDEO_W;
w->fh = DC_VIDEO_H;
w->video_on = 1;
w->screen_w = 1024;
w->screen_h = 768;
/* buttons (40) + video + status line and the shell's border */
w->normal_w = VIDEO_W;
w->normal_h = VIDEO_H + 40 + 20;
w->normal_x = 140;
w->normal_y = 104;
form = XmCreateFormDialog(parent, "callWindow", NULL, 0);
if (form == NULL) {
dtchat_call_window_destroy(w);
return NULL;
}
w->form = form;
w->shell = XtParent(form);
{
Display *dpy = XtDisplay(form);
w->screen_w = XDisplayWidth(dpy, DefaultScreen(dpy));
w->screen_h = XDisplayHeight(dpy, DefaultScreen(dpy));
if (w->normal_x + w->normal_w > w->screen_w)
w->normal_x = (w->screen_w - w->normal_w) / 2;
if (w->normal_y + w->normal_h > w->screen_h)
w->normal_y = (w->screen_h - w->normal_h) / 2;
}
snprintf(title, sizeof(title), "Call - %s",
peer_name != NULL ? peer_name : "peer");
XtVaSetValues(w->shell, XmNtitle, title, XmNwidth, 500,
XmNheight, 420, XmNdeleteResponse, XmDESTROY, NULL);
XtAddCallback(w->shell, XmNdestroyCallback, destroyed_cb, w);
buttons = XtVaCreateManagedWidget("buttons", xmFormWidgetClass, form,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, XmNheight, 40, NULL);
w->talk = XmCreateToggleButton(buttons, "talk", NULL, 0);
set_label(w->talk, "Push to talk");
XtVaSetValues(w->talk, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM, XmNleftOffset, 6,
XmNtopOffset, 6, NULL);
XtAddCallback(w->talk, XmNvalueChangedCallback, talk_cb, w);
XtManageChild(w->talk);
w->answer = XmCreatePushButton(buttons, "answer", NULL, 0);
set_label(w->answer, "Answer");
XtVaSetValues(w->answer, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, w->talk,
XmNleftOffset, 12, XmNtopOffset, 6, NULL);
XtAddCallback(w->answer, XmNactivateCallback, answer_cb, w);
XtManageChild(w->answer);
XtSetSensitive(w->answer, False);
w->hangup = XmCreatePushButton(buttons, "hangup", NULL, 0);
set_label(w->hangup, "Hang up");
XtVaSetValues(w->hangup, XmNtopAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, XmNrightOffset, 6,
XmNtopOffset, 6, NULL);
XtAddCallback(w->hangup, XmNactivateCallback, hangup_cb, w);
XtManageChild(w->hangup);
close = XmCreatePushButton(buttons, "close", NULL, 0);
set_label(close, "Close");
XtVaSetValues(close, XmNtopAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, w->hangup,
XmNrightOffset, 6, XmNtopOffset, 6, NULL);
XtAddCallback(close, XmNactivateCallback, hangup_cb, w);
XtManageChild(close);
w->video_toggle = XmCreateToggleButton(buttons, "video", NULL, 0);
set_label(w->video_toggle, "Video");
XmToggleButtonSetState(w->video_toggle, True, False);
XtVaSetValues(w->video_toggle, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, w->answer,
XmNleftOffset, 12, XmNtopOffset, 6, NULL);
XtAddCallback(w->video_toggle, XmNvalueChangedCallback, video_cb, w);
XtManageChild(w->video_toggle);
w->camera = XmCreatePushButton(buttons, "camera", NULL, 0);
set_label(w->camera, "Camera");
XtVaSetValues(w->camera, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget,
w->video_toggle, XmNleftOffset, 12, XmNtopOffset, 6, NULL);
XtAddCallback(w->camera, XmNactivateCallback, camera_cb, w);
XtManageChild(w->camera);
w->full_toggle = XmCreateToggleButton(buttons, "full", NULL, 0);
set_label(w->full_toggle, "Full screen");
XtVaSetValues(w->full_toggle, XmNtopAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_WIDGET, XmNrightWidget, close,
XmNrightOffset, 12, XmNtopOffset, 6, NULL);
XtAddCallback(w->full_toggle, XmNvalueChangedCallback, full_cb, w);
XtManageChild(w->full_toggle);
w->area = XtVaCreateManagedWidget("video", xmDrawingAreaWidgetClass,
form, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, buttons,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNwidth, VIDEO_W, XmNheight, VIDEO_H,
XmNmarginWidth, 0, XmNmarginHeight, 0, NULL);
XtAddCallback(w->area, XmNexposeCallback, area_expose, w);
XtAddCallback(w->area, XmNresizeCallback, area_resize, w);
w->status = XmCreateLabel(form, "status", NULL, 0);
x = XmStringCreateLocalized("connecting");
XtVaSetValues(w->status, XmNlabelString, x,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, w->area,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmarginLeft, 6, XmNmarginTop, 4, NULL);
XmStringFree(x);
XtManageChild(w->status);
if (call == NULL) {
XtSetSensitive(w->talk, False);
XtSetSensitive(w->hangup, False);
XtSetSensitive(w->answer, False);
XtSetSensitive(w->camera, False);
}
XtManageChild(form);
XtPopup(w->shell, XtGrabNone);
/* where the dialog manager put it is where full screen returns to */
{
Position x = 0, y = 0;
XtVaGetValues(w->shell, XmNx, &x, XmNy, &y, NULL);
w->normal_x = (int)x;
w->normal_y = (int)y;
}
/* after the popup the form has its natural height; ask for the full
* window (video + status + buttons) now */
XtVaSetValues(w->form, XmNwidth, w->normal_w, XmNheight,
w->normal_h, NULL);
XtVaSetValues(w->shell, XmNwidth, w->normal_w, XmNheight,
w->normal_h, NULL);
w->size_timer = XtAppAddTimeOut(
XtWidgetToApplicationContext(form), 50, size_cb, w);
return w;
}
void
dtchat_call_window_destroy(DtCallWindow *w)
{
if (w == NULL)
return;
if (w->size_timer != 0) {
XtRemoveTimeOut(w->size_timer);
w->size_timer = 0;
}
if (w->shell != NULL) {
Widget shell = w->shell;
w->shell = NULL;
XtDestroyWidget(shell);
}
if (w->image != NULL) {
w->image->data = NULL;
XDestroyImage(w->image);
w->image = NULL;
}
free(w->pixels);
free(w->frame);
free(w->peer_name);
free(w);
}
Widget
dtchat_call_window_shell(const DtCallWindow *w)
{
return w != NULL ? w->shell : NULL;
}
void
dtchat_call_window_set_frame(DtCallWindow *w, const unsigned char *i420, int fw,
int fh)
{
size_t size;
if (w == NULL || i420 == NULL || fw <= 0 || fh <= 0)
return;
if (fw != w->fw || fh != w->fh) {
size = (size_t)fw * fh * 3 / 2;
{
unsigned char *frame = realloc(w->frame, size);
if (frame == NULL)
return;
w->frame = frame;
}
w->fw = fw;
w->fh = fh;
}
memcpy(w->frame, i420, (size_t)fw * fh * 3 / 2);
w->has_frame = 1;
w->dirty = 1;
if (getenv("DTCHAT_CALL_DEBUG") != NULL)
fprintf(stderr, "call set_frame: %dx%d realized=%d\n", fw, fh,
w->area != NULL && XtIsRealized(w->area));
render(w);
}
void
dtchat_call_window_set_status(DtCallWindow *w, const char *text)
{
if (w == NULL)
return;
if (w->status != NULL)
set_label(w->status, text != NULL ? text : "");
}
int
dtchat_call_window_has_frame(const DtCallWindow *w)
{
return w != NULL ? w->has_frame : 0;
}
void
dtchat_call_window_set_camera_hook(DtCallWindow *w, DtCallCameraFn fn,
void *arg)
{
if (w == NULL)
return;
w->camera_fn = fn;
w->camera_arg = arg;
if (w->camera != NULL)
XtSetSensitive(w->camera, fn != NULL ? True : False);
}
void
dtchat_call_window_set_incoming(DtCallWindow *w, int incoming)
{
if (w == NULL || w->answer == NULL)
return;
w->incoming = incoming ? 1 : 0;
XtSetSensitive(w->answer, incoming ? True : False);
if (incoming)
dtchat_call_window_set_status(w, "incoming call - Answer");
}
void
dtchat_call_window_set_talking(DtCallWindow *w, int on)
{
if (w == NULL || w->talk == NULL)
return;
XmToggleButtonSetState(w->talk, on ? True : False, False);
w->talking = on ? 1 : 0;
}