source: branches/uq/lang/tcl/src/RpScreenSize.c @ 5222

Last change on this file since 5222 was 5222, checked in by mmh, 9 years ago

add ScreenSize? command and use it instead of broken tk screenwidth

File size: 2.8 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture::ScreenSize
4 *
5 *  TK doesn't know how to read the screen size properly, so until
6 *  it gets fixed, Rappture can get the correct size using this.
7 * ======================================================================
8 *  AUTHOR:  Martin Hunt, Purdue University
9 *  Copyright (c) 2015  HUBzero Foundation, LLC
10 *
11 *  See the file "license.terms" for information on usage and
12 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * ======================================================================
14 */
15#include "config.h"
16#include <tcl.h>
17#include <stdlib.h>
18#include <stddef.h>
19#include <string.h>
20#include <stdio.h>
21#include <X11/extensions/Xrandr.h>
22
23static Tcl_CmdProc RpScreenSizeCmd;
24
25/*
26 * ------------------------------------------------------------------------
27 *  RpScreenSize_Init()
28 *
29 *  Called in Rappture_Init() to initialize the commands defined
30 *  in this file.
31 * ------------------------------------------------------------------------
32 */
33int
34RpScreenSize_Init(interp)
35    Tcl_Interp *interp;  /* interpreter being initialized */
36{
37    /* install the sysinfo command */
38    Tcl_CreateCommand(interp, "::Rappture::ScreenSize", RpScreenSizeCmd,
39        (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
40    return TCL_OK;
41}
42
43/*
44 * ------------------------------------------------------------------------
45 *  RpScreenSizeCmd()
46 *
47 *  Gets the screen width and height.
48 *
49 *  Returns TCL_OK on success, and TCL_ERROR (along with an error
50 *  message in the interpreter) if anything goes wrong.
51 * ------------------------------------------------------------------------
52 */
53static int
54RpScreenSizeCmd(cdata, interp, argc, argv)
55    ClientData cdata;         /* not used */
56    Tcl_Interp *interp;       /* interpreter handling this request */
57    int argc;                 /* number of command line args */
58    CONST84 char *argv[];     /* strings for command line args */
59{
60    Tcl_Obj *resultPtr;
61    char *display_name = "";
62    int w, h, screen;
63
64    // parse through command line options
65    if (argc != 2) {
66        Tcl_AppendResult(interp,
67                "wrong # args: should be \"", argv[0],
68                " screen\"", (char*)NULL);
69        return TCL_ERROR;
70    }
71
72    display_name = (char *)argv[1];
73
74    resultPtr = Tcl_GetObjResult(interp);
75
76    Display *dpy = XOpenDisplay(display_name);
77    if (dpy == NULL) {
78        Tcl_AppendResult(interp,
79                "Could not find display for screen ",
80                display_name, (char*)NULL);
81        return TCL_ERROR;
82    }
83    screen = DefaultScreen(dpy);
84
85    w = DisplayWidth(dpy, screen);
86    Tcl_ListObjAppendElement(interp, resultPtr, Tcl_NewIntObj(w));
87
88    h = DisplayHeight(dpy, screen);
89    Tcl_ListObjAppendElement(interp, resultPtr, Tcl_NewIntObj(h));
90
91    return TCL_OK;
92}
Note: See TracBrowser for help on using the repository browser.